From patchwork Fri Jul 14 18:04:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: long mike X-Patchwork-Id: 9841513 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 263B7602D8 for ; Fri, 14 Jul 2017 18:11:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1698D28752 for ; Fri, 14 Jul 2017 18:11:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0B729287A3; Fri, 14 Jul 2017 18:11:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4B48228752 for ; Fri, 14 Jul 2017 18:11:00 +0000 (UTC) Received: from localhost ([::1]:39294 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW53L-0001Jc-Fo for patchwork-qemu-devel@patchwork.kernel.org; Fri, 14 Jul 2017 14:10:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55305) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4x5-0005Lo-8N for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4x3-0003Fc-Mr for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:31 -0400 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:33688) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4x3-0003F6-F2 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:29 -0400 Received: by mail-pg0-x242.google.com with SMTP id u62so11375738pgb.0 for ; Fri, 14 Jul 2017 11:04:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Rey6cZZDn3hXg+RXI4njZUu9V26nOXnx6/qp2hgpwFg=; b=fw5iBOreYcgwhRyBcOpHgOMufxfMEIm/zHIziiT9HbF+zFnV9Fgb4G+nWvo3eJv9pt YJLMKBUzPM++WetZyriGPAZjS9LOHTIO5aGuBzlhEtfOrCEaeU0AbtK9W8xH0K4q397X hRTbbx18TxpzQw+++C3zWuyNKlD4LYJ4IUFT2YlNlib5aKSqWDMazAAId4IRWQ9S34W+ PjkdLQPu/KmgCVCyifcBWxkjGBVoghsc+trVqszIBYlhcGNPcB5m60oB1bJE7l6yRae8 zLJkAyYorWeHr9IuRIr1kx4HjD6JL0l4uD5RkM0WG/JMOsCprpc3XqyRIeEtcamqBd+s BOjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Rey6cZZDn3hXg+RXI4njZUu9V26nOXnx6/qp2hgpwFg=; b=oKZELaTrXhwD31W6UbKobRnt9qQmV9AOsBwPV0P/AH8ENYlfKAbX3bIE4qavCRIOUx qKUluSGG3sMX7/E46QqoqbSnwiWtHY+U5EuQh08rjBk32VfQa8WwwbeWYJe0cYfuU8Ai sa7tk8xfvIX55s7XegQ1k82s6LY2daScJ2LfWud23oeeMb+ntkVLoLinupiHgLSxGboy 2UPKWgXw8cFGroBKhVBTVnuiDSddXEaqlZhepna9hoOWKSVH1dLZJPfrkAOboc71JKIN Osreymz3jYP4a3AuQTJIjv3obdOaPg6vLRGK2eyldyMrMKSMub1svcX+uh3Ij+DiJTG9 kZag== X-Gm-Message-State: AIVw111nf87cls4WlwrdVSowUI0eWIbwG3D4ApDnESDQEToLPPM4q0sX 8JKNomUzCKSjzw== X-Received: by 10.101.90.3 with SMTP id y3mr15969046pgs.185.1500055468605; Fri, 14 Jul 2017 11:04:28 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id l8sm4906326pgs.30.2017.07.14.11.04.27 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:28 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:04 -0400 Message-Id: <1500055451-14041-12-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::242 Subject: [Qemu-devel] [PATCH v6 11/18] crypto: hmac: add hmac driver framework X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Longpeng\(Mike\)" , arei.gonglei@huawei.com, weidong.huang@huawei.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: "Longpeng(Mike)" 1) makes the public APIs in hmac-nettle/gcrypt/glib static, and rename them with "nettle/gcrypt/glib" prefix. 2) introduces hmac framework, including QCryptoHmacDriver and new public APIs. Reviewed-by: Daniel P. Berrange Signed-off-by: Longpeng(Mike) --- crypto/hmac-gcrypt.c | 51 ++++++++++++----------------------- crypto/hmac-glib.c | 75 +++++++++++++++++++++------------------------------ crypto/hmac-nettle.c | 52 ++++++++++++----------------------- crypto/hmac.c | 44 ++++++++++++++++++++++++++++++ crypto/hmacpriv.h | 36 +++++++++++++++++++++++++ include/crypto/hmac.h | 1 + 6 files changed, 145 insertions(+), 114 deletions(-) create mode 100644 crypto/hmacpriv.h diff --git a/crypto/hmac-gcrypt.c b/crypto/hmac-gcrypt.c index 372ad7f..76ca61b 100644 --- a/crypto/hmac-gcrypt.c +++ b/crypto/hmac-gcrypt.c @@ -15,6 +15,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hmac.h" +#include "hmacpriv.h" #include static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = { @@ -42,10 +43,9 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) return false; } -static QCryptoHmacGcrypt * -qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) +void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) { QCryptoHmacGcrypt *ctx; gcry_error_t err; @@ -81,27 +81,24 @@ error: return NULL; } -void qcrypto_hmac_free(QCryptoHmac *hmac) +static void +qcrypto_gcrypt_hmac_ctx_free(QCryptoHmac *hmac) { QCryptoHmacGcrypt *ctx; - if (!hmac) { - return; - } - ctx = hmac->opaque; gcry_mac_close(ctx->handle); g_free(ctx); - g_free(hmac); } -int qcrypto_hmac_bytesv(QCryptoHmac *hmac, - const struct iovec *iov, - size_t niov, - uint8_t **result, - size_t *resultlen, - Error **errp) +static int +qcrypto_gcrypt_hmac_bytesv(QCryptoHmac *hmac, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp) { QCryptoHmacGcrypt *ctx; gcry_error_t err; @@ -147,21 +144,7 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac, return 0; } -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) -{ - QCryptoHmac *hmac; - QCryptoHmacGcrypt *ctx; - - ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp); - if (!ctx) { - return NULL; - } - - hmac = g_new0(QCryptoHmac, 1); - hmac->alg = alg; - hmac->opaque = ctx; - - return hmac; -} +QCryptoHmacDriver qcrypto_hmac_lib_driver = { + .hmac_bytesv = qcrypto_gcrypt_hmac_bytesv, + .hmac_free = qcrypto_gcrypt_hmac_ctx_free, +}; diff --git a/crypto/hmac-glib.c b/crypto/hmac-glib.c index f0ccfd6..8cf6b22 100644 --- a/crypto/hmac-glib.c +++ b/crypto/hmac-glib.c @@ -15,6 +15,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hmac.h" +#include "hmacpriv.h" /* Support for HMAC Algos has been added in GLib 2.30 */ #if GLIB_CHECK_VERSION(2, 30, 0) @@ -49,10 +50,9 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) return false; } -static QCryptoHmacGlib * -qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) +void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) { QCryptoHmacGlib *ctx; @@ -78,27 +78,24 @@ error: return NULL; } -void qcrypto_hmac_free(QCryptoHmac *hmac) +static void +qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac) { QCryptoHmacGlib *ctx; - if (!hmac) { - return; - } - ctx = hmac->opaque; g_hmac_unref(ctx->ghmac); g_free(ctx); - g_free(hmac); } -int qcrypto_hmac_bytesv(QCryptoHmac *hmac, - const struct iovec *iov, - size_t niov, - uint8_t **result, - size_t *resultlen, - Error **errp) +static int +qcrypto_glib_hmac_bytesv(QCryptoHmac *hmac, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp) { QCryptoHmacGlib *ctx; int i, ret; @@ -129,25 +126,6 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac, return 0; } -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) -{ - QCryptoHmac *hmac; - QCryptoHmacGlib *ctx; - - ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp); - if (!ctx) { - return NULL; - } - - hmac = g_new0(QCryptoHmac, 1); - hmac->alg = alg; - hmac->opaque = ctx; - - return hmac; -} - #else bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) @@ -155,26 +133,33 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) return false; } -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) +void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) { return NULL; } -void qcrypto_hmac_free(QCryptoHmac *hmac) +static void +qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac) { return; } -int qcrypto_hmac_bytesv(QCryptoHmac *hmac, - const struct iovec *iov, - size_t niov, - uint8_t **result, - size_t *resultlen, - Error **errp) +static int +qcrypto_glib_hmac_bytesv(QCryptoHmac *hmac, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp) { return -1; } #endif + +QCryptoHmacDriver qcrypto_hmac_lib_driver = { + .hmac_bytesv = qcrypto_glib_hmac_bytesv, + .hmac_free = qcrypto_glib_hmac_ctx_free, +}; diff --git a/crypto/hmac-nettle.c b/crypto/hmac-nettle.c index 000dfd9..1d5a915 100644 --- a/crypto/hmac-nettle.c +++ b/crypto/hmac-nettle.c @@ -15,6 +15,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hmac.h" +#include "hmacpriv.h" #include typedef void (*qcrypto_nettle_hmac_setkey)(void *ctx, @@ -97,10 +98,9 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) return false; } -static QCryptoHmacNettle * -qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) +void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) { QCryptoHmacNettle *ctx; @@ -117,26 +117,22 @@ qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, return ctx; } -void qcrypto_hmac_free(QCryptoHmac *hmac) +static void +qcrypto_nettle_hmac_ctx_free(QCryptoHmac *hmac) { QCryptoHmacNettle *ctx; - if (!hmac) { - return; - } - ctx = hmac->opaque; - g_free(ctx); - g_free(hmac); } -int qcrypto_hmac_bytesv(QCryptoHmac *hmac, - const struct iovec *iov, - size_t niov, - uint8_t **result, - size_t *resultlen, - Error **errp) +static int +qcrypto_nettle_hmac_bytesv(QCryptoHmac *hmac, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp) { QCryptoHmacNettle *ctx; int i; @@ -169,21 +165,7 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac, return 0; } -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) -{ - QCryptoHmac *hmac; - QCryptoHmacNettle *ctx; - - ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp); - if (!ctx) { - return NULL; - } - - hmac = g_new0(QCryptoHmac, 1); - hmac->alg = alg; - hmac->opaque = ctx; - - return hmac; -} +QCryptoHmacDriver qcrypto_hmac_lib_driver = { + .hmac_bytesv = qcrypto_nettle_hmac_bytesv, + .hmac_free = qcrypto_nettle_hmac_ctx_free, +}; diff --git a/crypto/hmac.c b/crypto/hmac.c index 5750405..a4690e3 100644 --- a/crypto/hmac.c +++ b/crypto/hmac.c @@ -12,9 +12,22 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hmac.h" +#include "hmacpriv.h" static const char hex[] = "0123456789abcdef"; +int qcrypto_hmac_bytesv(QCryptoHmac *hmac, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp) +{ + QCryptoHmacDriver *drv = hmac->driver; + + return drv->hmac_bytesv(hmac, iov, niov, result, resultlen, errp); +} + int qcrypto_hmac_bytes(QCryptoHmac *hmac, const char *buf, size_t len, @@ -70,3 +83,34 @@ int qcrypto_hmac_digest(QCryptoHmac *hmac, return qcrypto_hmac_digestv(hmac, &iov, 1, digest, errp); } + +QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoHmac *hmac; + void *ctx; + + ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp); + if (!ctx) { + return NULL; + } + + hmac = g_new0(QCryptoHmac, 1); + hmac->alg = alg; + hmac->opaque = ctx; + hmac->driver = (void *)&qcrypto_hmac_lib_driver; + + return hmac; +} + +void qcrypto_hmac_free(QCryptoHmac *hmac) +{ + QCryptoHmacDriver *drv; + + if (hmac) { + drv = hmac->driver; + drv->hmac_free(hmac); + g_free(hmac); + } +} diff --git a/crypto/hmacpriv.h b/crypto/hmacpriv.h new file mode 100644 index 0000000..2be389a --- /dev/null +++ b/crypto/hmacpriv.h @@ -0,0 +1,36 @@ +/* + * QEMU Crypto hmac driver supports + * + * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. + * + * Authors: + * Longpeng(Mike) + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + * + */ + +#ifndef QCRYPTO_HMACPRIV_H +#define QCRYPTO_HMACPRIV_H + +typedef struct QCryptoHmacDriver QCryptoHmacDriver; + +struct QCryptoHmacDriver { + int (*hmac_bytesv)(QCryptoHmac *hmac, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp); + + void (*hmac_free)(QCryptoHmac *hmac); +}; + +extern void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp); +extern QCryptoHmacDriver qcrypto_hmac_lib_driver; + +#endif diff --git a/include/crypto/hmac.h b/include/crypto/hmac.h index 0d3acd7..5e88905 100644 --- a/include/crypto/hmac.h +++ b/include/crypto/hmac.h @@ -18,6 +18,7 @@ typedef struct QCryptoHmac QCryptoHmac; struct QCryptoHmac { QCryptoHashAlgorithm alg; void *opaque; + void *driver; }; /**