From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492845792447917.3594519843622; Sat, 22 Apr 2017 00:23:12 -0700 (PDT) Received: from localhost ([::1]:34504 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pNu-0005GD-Li for importer@patchew.org; Sat, 22 Apr 2017 03:23:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48094) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pM3-0003Zl-NY for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM0-0003kB-Dd for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:15 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:3395 helo=dggrg03-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pLz-0003g1-QC for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:12 -0400 Received: from 172.30.72.57 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.57]) by dggrg03-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AMH64324; Sat, 22 Apr 2017 15:21:05 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:20:59 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:10 +0800 Message-ID: <1492845627-4384-2-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020204.58FB0462.00AA, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 36c4c339e4b825f3bbcfa71e897c4886 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.189 Subject: [Qemu-devel] [PATCH v3 01/18] crypto: cipher: introduce context free function 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactors the qcrypto_cipher_free(), splits it into two parts. One is gcrypt/nettle__cipher_free_ctx() to free the special context. This makes code more clear, what's more, it would be used by the later patch. Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) Reviewed-by: Daniel P. Berrange --- crypto/cipher-gcrypt.c | 31 ++++++++++++++++++------------- crypto/cipher-nettle.c | 18 ++++++++++++++---- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/crypto/cipher-gcrypt.c b/crypto/cipher-gcrypt.c index 6487eca..0ecffa2 100644 --- a/crypto/cipher-gcrypt.c +++ b/crypto/cipher-gcrypt.c @@ -64,6 +64,22 @@ struct QCryptoCipherGcrypt { uint8_t *iv; }; =20 +static void gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx, + QCryptoCipherMode mode) +{ + if (!ctx) { + return; + } + + gcry_cipher_close(ctx->handle); + if (mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { + gcry_cipher_close(ctx->tweakhandle); + } + g_free(ctx->iv); + g_free(ctx); +} + + QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, QCryptoCipherMode mode, const uint8_t *key, size_t nkey, @@ -228,11 +244,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorit= hm alg, return cipher; =20 error: - gcry_cipher_close(ctx->handle); - if (cipher->mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { - gcry_cipher_close(ctx->tweakhandle); - } - g_free(ctx); + gcrypt_cipher_free_ctx(ctx, mode); g_free(cipher); return NULL; } @@ -240,17 +252,10 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgori= thm alg, =20 void qcrypto_cipher_free(QCryptoCipher *cipher) { - QCryptoCipherGcrypt *ctx; if (!cipher) { return; } - ctx =3D cipher->opaque; - gcry_cipher_close(ctx->handle); - if (cipher->mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { - gcry_cipher_close(ctx->tweakhandle); - } - g_free(ctx->iv); - g_free(ctx); + gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode); g_free(cipher); } =20 diff --git a/crypto/cipher-nettle.c b/crypto/cipher-nettle.c index dfc9030..e04e3a1 100644 --- a/crypto/cipher-nettle.c +++ b/crypto/cipher-nettle.c @@ -249,6 +249,19 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm al= g, } =20 =20 +static void nettle_cipher_free_ctx(QCryptoCipherNettle *ctx) +{ + if (!ctx) { + return; + } + + g_free(ctx->iv); + g_free(ctx->ctx); + g_free(ctx->ctx_tweak); + g_free(ctx); +} + + QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, QCryptoCipherMode mode, const uint8_t *key, size_t nkey, @@ -440,10 +453,7 @@ void qcrypto_cipher_free(QCryptoCipher *cipher) } =20 ctx =3D cipher->opaque; - g_free(ctx->iv); - g_free(ctx->ctx); - g_free(ctx->ctx_tweak); - g_free(ctx); + nettle_cipher_free_ctx(ctx); g_free(cipher); } =20 --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492845945504670.8294270577355; Sat, 22 Apr 2017 00:25:45 -0700 (PDT) Received: from localhost ([::1]:34518 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pQO-0008Ap-2T for importer@patchew.org; Sat, 22 Apr 2017 03:25:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48130) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pM5-0003aH-Il for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM2-0003lP-BF for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:17 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3500 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM1-0003jY-OD for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:14 -0400 Received: from 172.30.72.56 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.56]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id ANE62805; Sat, 22 Apr 2017 15:21:10 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:20:59 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:11 +0800 Message-ID: <1492845627-4384-3-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020201.58FB0467.001E, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 7fb1a09112cac0ad28d53c8a9a46d5da X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v3 02/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for gcrypt-backend 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for gcrypt-backend impls. Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) Reviewed-by: Daniel P. Berrange --- crypto/cipher-gcrypt.c | 50 +++++++++++++++++++++++++++++++++-------------= ---- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/crypto/cipher-gcrypt.c b/crypto/cipher-gcrypt.c index 0ecffa2..871730b 100644 --- a/crypto/cipher-gcrypt.c +++ b/crypto/cipher-gcrypt.c @@ -80,12 +80,12 @@ static void gcrypt_cipher_free_ctx(QCryptoCipherGcrypt = *ctx, } =20 =20 -QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, - QCryptoCipherMode mode, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm = alg, + QCryptoCipherMode mode, + const uint8_t *key, + size_t nkey, + Error **errp) { - QCryptoCipher *cipher; QCryptoCipherGcrypt *ctx; gcry_error_t err; int gcryalg, gcrymode; @@ -162,10 +162,6 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorit= hm alg, return NULL; } =20 - cipher =3D g_new0(QCryptoCipher, 1); - cipher->alg =3D alg; - cipher->mode =3D mode; - ctx =3D g_new0(QCryptoCipherGcrypt, 1); =20 err =3D gcry_cipher_open(&ctx->handle, gcryalg, gcrymode, 0); @@ -174,7 +170,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorith= m alg, gcry_strerror(err)); goto error; } - if (cipher->mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { + if (mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { err =3D gcry_cipher_open(&ctx->tweakhandle, gcryalg, gcrymode, 0); if (err !=3D 0) { error_setg(errp, "Cannot initialize cipher: %s", @@ -183,7 +179,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorith= m alg, } } =20 - if (cipher->alg =3D=3D QCRYPTO_CIPHER_ALG_DES_RFB) { + if (alg =3D=3D QCRYPTO_CIPHER_ALG_DES_RFB) { /* We're using standard DES cipher from gcrypt, so we need * to munge the key so that the results are the same as the * bizarre RFB variant of DES :-) @@ -193,7 +189,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorith= m alg, g_free(rfbkey); ctx->blocksize =3D 8; } else { - if (cipher->mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { + if (mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { nkey /=3D 2; err =3D gcry_cipher_setkey(ctx->handle, key, nkey); if (err !=3D 0) { @@ -210,7 +206,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorith= m alg, gcry_strerror(err)); goto error; } - switch (cipher->alg) { + switch (alg) { case QCRYPTO_CIPHER_ALG_AES_128: case QCRYPTO_CIPHER_ALG_AES_192: case QCRYPTO_CIPHER_ALG_AES_256: @@ -230,7 +226,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorith= m alg, } } =20 - if (cipher->mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { + if (mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { if (ctx->blocksize !=3D XTS_BLOCK_SIZE) { error_setg(errp, "Cipher block size %zu must equal XTS block size %d= ", @@ -240,12 +236,10 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgori= thm alg, ctx->iv =3D g_new0(uint8_t, ctx->blocksize); } =20 - cipher->opaque =3D ctx; - return cipher; + return ctx; =20 error: gcrypt_cipher_free_ctx(ctx, mode); - g_free(cipher); return NULL; } =20 @@ -385,3 +379,25 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, =20 return 0; } + + +QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoCipher *cipher; + QCryptoCipherGcrypt *ctx; + + ctx =3D qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); + if (ctx =3D=3D NULL) { + return NULL; + } + + cipher =3D g_new0(QCryptoCipher, 1); + cipher->alg =3D alg; + cipher->mode =3D mode; + cipher->opaque =3D ctx; + + return cipher; +} --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492845793806974.5791140440253; Sat, 22 Apr 2017 00:23:13 -0700 (PDT) Received: from localhost ([::1]:34506 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pNw-0005Hj-59 for importer@patchew.org; Sat, 22 Apr 2017 03:23:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48123) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pM5-0003a1-GO for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM2-0003m6-S4 for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:17 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3501 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM2-0003k3-7O for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:14 -0400 Received: from 172.30.72.56 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.56]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id ANE62808; Sat, 22 Apr 2017 15:21:11 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:00 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:12 +0800 Message-ID: <1492845627-4384-4-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.58FB0467.0098, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: cad0522ea274c52b12ff766806873ec7 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v3 03/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for nettle-backend 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for nettle-backend impls. Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) Reviewed-by: Daniel P. Berrange --- crypto/cipher-nettle.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/crypto/cipher-nettle.c b/crypto/cipher-nettle.c index e04e3a1..e6d6e6c 100644 --- a/crypto/cipher-nettle.c +++ b/crypto/cipher-nettle.c @@ -262,12 +262,12 @@ static void nettle_cipher_free_ctx(QCryptoCipherNettl= e *ctx) } =20 =20 -QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, - QCryptoCipherMode mode, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoCipherNettle *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm = alg, + QCryptoCipherMode mode, + const uint8_t *key, + size_t nkey, + Error **errp) { - QCryptoCipher *cipher; QCryptoCipherNettle *ctx; uint8_t *rfbkey; =20 @@ -287,12 +287,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorit= hm alg, return NULL; } =20 - cipher =3D g_new0(QCryptoCipher, 1); - cipher->alg =3D alg; - cipher->mode =3D mode; - ctx =3D g_new0(QCryptoCipherNettle, 1); - cipher->opaque =3D ctx; =20 switch (alg) { case QCRYPTO_CIPHER_ALG_DES_RFB: @@ -436,10 +431,10 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgori= thm alg, =20 ctx->iv =3D g_new0(uint8_t, ctx->blocksize); =20 - return cipher; + return ctx; =20 error: - qcrypto_cipher_free(cipher); + nettle_cipher_free_ctx(ctx); return NULL; } =20 @@ -561,3 +556,25 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, memcpy(ctx->iv, iv, niv); return 0; } + + +QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoCipher *cipher; + QCryptoCipherNettle *ctx; + + ctx =3D qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); + if (!ctx) { + return NULL; + } + + cipher =3D g_new0(QCryptoCipher, 1); + cipher->alg =3D alg; + cipher->mode =3D mode; + cipher->opaque =3D ctx; + + return cipher; +} --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492845947689782.68334297622; Sat, 22 Apr 2017 00:25:47 -0700 (PDT) Received: from localhost ([::1]:34520 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pQQ-0008DF-9N for importer@patchew.org; Sat, 22 Apr 2017 03:25:46 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48124) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pM5-0003a4-HN for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM2-0003lz-KB for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:17 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3498 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM1-0003iy-OA for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:14 -0400 Received: from 172.30.72.56 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.56]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id ANE62800; Sat, 22 Apr 2017 15:21:10 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:00 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:13 +0800 Message-ID: <1492845627-4384-5-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020201.58FB0466.0054, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 154a072919d70c4b4978b2a93d00266a X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v3 04/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for builtin-backend 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for builtin-backend impls. Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) Reviewed-by: Daniel P. Berrange --- crypto/cipher-builtin.c | 101 ++++++++++++++++++++++++++------------------= ---- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/crypto/cipher-builtin.c b/crypto/cipher-builtin.c index b4bc2b9..8cf47d1 100644 --- a/crypto/cipher-builtin.c +++ b/crypto/cipher-builtin.c @@ -235,23 +235,24 @@ static int qcrypto_cipher_setiv_aes(QCryptoCipher *ci= pher, =20 =20 =20 -static int qcrypto_cipher_init_aes(QCryptoCipher *cipher, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoCipherBuiltin * +qcrypto_cipher_init_aes(QCryptoCipherMode mode, + const uint8_t *key, size_t nkey, + Error **errp) { QCryptoCipherBuiltin *ctxt; =20 - if (cipher->mode !=3D QCRYPTO_CIPHER_MODE_CBC && - cipher->mode !=3D QCRYPTO_CIPHER_MODE_ECB && - cipher->mode !=3D QCRYPTO_CIPHER_MODE_XTS) { + if (mode !=3D QCRYPTO_CIPHER_MODE_CBC && + mode !=3D QCRYPTO_CIPHER_MODE_ECB && + mode !=3D QCRYPTO_CIPHER_MODE_XTS) { error_setg(errp, "Unsupported cipher mode %s", - QCryptoCipherMode_lookup[cipher->mode]); - return -1; + QCryptoCipherMode_lookup[mode]); + return NULL; } =20 ctxt =3D g_new0(QCryptoCipherBuiltin, 1); =20 - if (cipher->mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { + if (mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { if (AES_set_encrypt_key(key, nkey * 4, &ctxt->state.aes.key.enc) != =3D 0) { error_setg(errp, "Failed to set encryption key"); goto error; @@ -291,13 +292,11 @@ static int qcrypto_cipher_init_aes(QCryptoCipher *cip= her, ctxt->encrypt =3D qcrypto_cipher_encrypt_aes; ctxt->decrypt =3D qcrypto_cipher_decrypt_aes; =20 - cipher->opaque =3D ctxt; - - return 0; + return ctxt; =20 error: g_free(ctxt); - return -1; + return NULL; } =20 =20 @@ -370,16 +369,17 @@ static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher= *cipher, } =20 =20 -static int qcrypto_cipher_init_des_rfb(QCryptoCipher *cipher, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoCipherBuiltin * +qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode, + const uint8_t *key, size_t nkey, + Error **errp) { QCryptoCipherBuiltin *ctxt; =20 - if (cipher->mode !=3D QCRYPTO_CIPHER_MODE_ECB) { + if (mode !=3D QCRYPTO_CIPHER_MODE_ECB) { error_setg(errp, "Unsupported cipher mode %s", - QCryptoCipherMode_lookup[cipher->mode]); - return -1; + QCryptoCipherMode_lookup[mode]); + return NULL; } =20 ctxt =3D g_new0(QCryptoCipherBuiltin, 1); @@ -394,9 +394,7 @@ static int qcrypto_cipher_init_des_rfb(QCryptoCipher *c= ipher, ctxt->encrypt =3D qcrypto_cipher_encrypt_des_rfb; ctxt->decrypt =3D qcrypto_cipher_decrypt_des_rfb; =20 - cipher->opaque =3D ctxt; - - return 0; + return ctxt; } =20 =20 @@ -426,12 +424,13 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm a= lg, } =20 =20 -QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, - QCryptoCipherMode mode, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm= alg, + QCryptoCipherMode mode, + const uint8_t *key, + size_t nkey, + Error **errp) { - QCryptoCipher *cipher; + QCryptoCipherBuiltin *ctxt; =20 switch (mode) { case QCRYPTO_CIPHER_MODE_ECB: @@ -444,39 +443,27 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgori= thm alg, return NULL; } =20 - cipher =3D g_new0(QCryptoCipher, 1); - cipher->alg =3D alg; - cipher->mode =3D mode; - if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) { - goto error; + return NULL; } =20 - switch (cipher->alg) { + switch (alg) { case QCRYPTO_CIPHER_ALG_DES_RFB: - if (qcrypto_cipher_init_des_rfb(cipher, key, nkey, errp) < 0) { - goto error; - } + ctxt =3D qcrypto_cipher_init_des_rfb(mode, key, nkey, errp); break; case QCRYPTO_CIPHER_ALG_AES_128: case QCRYPTO_CIPHER_ALG_AES_192: case QCRYPTO_CIPHER_ALG_AES_256: - if (qcrypto_cipher_init_aes(cipher, key, nkey, errp) < 0) { - goto error; - } + ctxt =3D qcrypto_cipher_init_aes(mode, key, nkey, errp); break; default: error_setg(errp, "Unsupported cipher algorithm %s", - QCryptoCipherAlgorithm_lookup[cipher->alg]); - goto error; + QCryptoCipherAlgorithm_lookup[alg]); + return NULL; } =20 - return cipher; - - error: - g_free(cipher); - return NULL; + return ctxt; } =20 void qcrypto_cipher_free(QCryptoCipher *cipher) @@ -537,3 +524,25 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, =20 return ctxt->setiv(cipher, iv, niv, errp); } + + +QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoCipher *cipher; + QCryptoCipherBuiltin *ctxt; + + ctxt =3D qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); + if (ctxt =3D=3D NULL) { + return NULL; + } + + cipher =3D g_new0(QCryptoCipher, 1); + cipher->alg =3D alg; + cipher->mode =3D mode; + cipher->opaque =3D ctxt; + + return cipher; +} --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492846263695777.5652167336068; Sat, 22 Apr 2017 00:31:03 -0700 (PDT) Received: from localhost ([::1]:34541 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pVW-0006VV-4i for importer@patchew.org; Sat, 22 Apr 2017 03:31:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48127) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pM5-0003aE-I9 for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM2-0003mB-S9 for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:17 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3497 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM1-0003ix-OG for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:14 -0400 Received: from 172.30.72.56 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.56]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id ANE62802; Sat, 22 Apr 2017 15:21:10 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:01 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:14 +0800 Message-ID: <1492845627-4384-6-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020201.58FB0466.009A, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 36ce2dc9b85c5c465efac6eeec19e7a9 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v3 05/18] crypto: cipher: add cipher 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 1) makes the public APIs in cipher-nettle/gcrypt/builtin static, and rename them with "nettle/gcrypt/builtin" prefix. 2) introduces cipher framework, including QCryptoCipherDriver and new public APIs. Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) Reviewed-by: Daniel P. Berrange --- crypto/cipher-builtin.c | 64 +++++++++++++++++-------------------------- crypto/cipher-gcrypt.c | 72 +++++++++++++++++++++------------------------= ---- crypto/cipher-nettle.c | 71 ++++++++++++++++++++-------------------------= --- crypto/cipher.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ crypto/cipherpriv.h | 40 +++++++++++++++++++++++++++ include/crypto/cipher.h | 1 + 6 files changed, 190 insertions(+), 123 deletions(-) create mode 100644 crypto/cipherpriv.h diff --git a/crypto/cipher-builtin.c b/crypto/cipher-builtin.c index 8cf47d1..16a36d4 100644 --- a/crypto/cipher-builtin.c +++ b/crypto/cipher-builtin.c @@ -22,6 +22,7 @@ #include "crypto/aes.h" #include "crypto/desrfb.h" #include "crypto/xts.h" +#include "cipherpriv.h" =20 typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESConte= xt; struct QCryptoCipherBuiltinAESContext { @@ -466,25 +467,22 @@ static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(Q= CryptoCipherAlgorithm alg, return ctxt; } =20 -void qcrypto_cipher_free(QCryptoCipher *cipher) +static void +qcrypto_builtin_cipher_ctx_free(QCryptoCipher *cipher) { QCryptoCipherBuiltin *ctxt; =20 - if (!cipher) { - return; - } - ctxt =3D cipher->opaque; ctxt->free(cipher); - g_free(cipher); } =20 =20 -int qcrypto_cipher_encrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int +qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherBuiltin *ctxt =3D cipher->opaque; =20 @@ -498,11 +496,12 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher, } =20 =20 -int qcrypto_cipher_decrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int +qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherBuiltin *ctxt =3D cipher->opaque; =20 @@ -516,9 +515,10 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher, } =20 =20 -int qcrypto_cipher_setiv(QCryptoCipher *cipher, - const uint8_t *iv, size_t niv, - Error **errp) +static int +qcrypto_builtin_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) { QCryptoCipherBuiltin *ctxt =3D cipher->opaque; =20 @@ -526,23 +526,9 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, } =20 =20 -QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, - QCryptoCipherMode mode, - const uint8_t *key, size_t nkey, - Error **errp) -{ - QCryptoCipher *cipher; - QCryptoCipherBuiltin *ctxt; - - ctxt =3D qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); - if (ctxt =3D=3D NULL) { - return NULL; - } - - cipher =3D g_new0(QCryptoCipher, 1); - cipher->alg =3D alg; - cipher->mode =3D mode; - cipher->opaque =3D ctxt; - - return cipher; -} +static struct QCryptoCipherDriver qcrypto_cipher_lib_driver =3D { + .cipher_encrypt =3D qcrypto_builtin_cipher_encrypt, + .cipher_decrypt =3D qcrypto_builtin_cipher_decrypt, + .cipher_setiv =3D qcrypto_builtin_cipher_setiv, + .cipher_free =3D qcrypto_builtin_cipher_ctx_free, +}; diff --git a/crypto/cipher-gcrypt.c b/crypto/cipher-gcrypt.c index 871730b..0489147 100644 --- a/crypto/cipher-gcrypt.c +++ b/crypto/cipher-gcrypt.c @@ -20,6 +20,7 @@ =20 #include "qemu/osdep.h" #include "crypto/xts.h" +#include "cipherpriv.h" =20 #include =20 @@ -64,8 +65,9 @@ struct QCryptoCipherGcrypt { uint8_t *iv; }; =20 -static void gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx, - QCryptoCipherMode mode) +static void +qcrypto_gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx, + QCryptoCipherMode mode) { if (!ctx) { return; @@ -239,18 +241,15 @@ static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QC= ryptoCipherAlgorithm alg, return ctx; =20 error: - gcrypt_cipher_free_ctx(ctx, mode); + qcrypto_gcrypt_cipher_free_ctx(ctx, mode); return NULL; } =20 =20 -void qcrypto_cipher_free(QCryptoCipher *cipher) +static void +qcrypto_gcrypt_cipher_ctx_free(QCryptoCipher *cipher) { - if (!cipher) { - return; - } - gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode); - g_free(cipher); + qcrypto_gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode); } =20 =20 @@ -274,11 +273,12 @@ static void qcrypto_gcrypt_xts_decrypt(const void *ct= x, g_assert(err =3D=3D 0); } =20 -int qcrypto_cipher_encrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int +qcrypto_gcrypt_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherGcrypt *ctx =3D cipher->opaque; gcry_error_t err; @@ -309,11 +309,12 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher, } =20 =20 -int qcrypto_cipher_decrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int +qcrypto_gcrypt_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherGcrypt *ctx =3D cipher->opaque; gcry_error_t err; @@ -343,9 +344,10 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher, return 0; } =20 -int qcrypto_cipher_setiv(QCryptoCipher *cipher, - const uint8_t *iv, size_t niv, - Error **errp) +static int +qcrypto_gcrypt_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) { QCryptoCipherGcrypt *ctx =3D cipher->opaque; gcry_error_t err; @@ -381,23 +383,9 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, } =20 =20 -QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, - QCryptoCipherMode mode, - const uint8_t *key, size_t nkey, - Error **errp) -{ - QCryptoCipher *cipher; - QCryptoCipherGcrypt *ctx; - - ctx =3D qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); - if (ctx =3D=3D NULL) { - return NULL; - } - - cipher =3D g_new0(QCryptoCipher, 1); - cipher->alg =3D alg; - cipher->mode =3D mode; - cipher->opaque =3D ctx; - - return cipher; -} +static struct QCryptoCipherDriver qcrypto_cipher_lib_driver =3D { + .cipher_encrypt =3D qcrypto_gcrypt_cipher_encrypt, + .cipher_decrypt =3D qcrypto_gcrypt_cipher_decrypt, + .cipher_setiv =3D qcrypto_gcrypt_cipher_setiv, + .cipher_free =3D qcrypto_gcrypt_cipher_ctx_free, +}; diff --git a/crypto/cipher-nettle.c b/crypto/cipher-nettle.c index e6d6e6c..c51f119 100644 --- a/crypto/cipher-nettle.c +++ b/crypto/cipher-nettle.c @@ -20,6 +20,7 @@ =20 #include "qemu/osdep.h" #include "crypto/xts.h" +#include "cipherpriv.h" =20 #include #include @@ -249,7 +250,8 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, } =20 =20 -static void nettle_cipher_free_ctx(QCryptoCipherNettle *ctx) +static void +qcrypto_nettle_cipher_free_ctx(QCryptoCipherNettle *ctx) { if (!ctx) { return; @@ -434,30 +436,27 @@ static QCryptoCipherNettle *qcrypto_cipher_ctx_new(QC= ryptoCipherAlgorithm alg, return ctx; =20 error: - nettle_cipher_free_ctx(ctx); + qcrypto_nettle_cipher_free_ctx(ctx); return NULL; } =20 =20 -void qcrypto_cipher_free(QCryptoCipher *cipher) +static void +qcrypto_nettle_cipher_ctx_free(QCryptoCipher *cipher) { QCryptoCipherNettle *ctx; =20 - if (!cipher) { - return; - } - ctx =3D cipher->opaque; - nettle_cipher_free_ctx(ctx); - g_free(cipher); + qcrypto_nettle_cipher_free_ctx(ctx); } =20 =20 -int qcrypto_cipher_encrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int +qcrypto_nettle_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherNettle *ctx =3D cipher->opaque; =20 @@ -499,11 +498,12 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher, } =20 =20 -int qcrypto_cipher_decrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int +qcrypto_nettle_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherNettle *ctx =3D cipher->opaque; =20 @@ -543,9 +543,10 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher, return 0; } =20 -int qcrypto_cipher_setiv(QCryptoCipher *cipher, - const uint8_t *iv, size_t niv, - Error **errp) +static int +qcrypto_nettle_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) { QCryptoCipherNettle *ctx =3D cipher->opaque; if (niv !=3D ctx->blocksize) { @@ -558,23 +559,9 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, } =20 =20 -QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, - QCryptoCipherMode mode, - const uint8_t *key, size_t nkey, - Error **errp) -{ - QCryptoCipher *cipher; - QCryptoCipherNettle *ctx; - - ctx =3D qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); - if (!ctx) { - return NULL; - } - - cipher =3D g_new0(QCryptoCipher, 1); - cipher->alg =3D alg; - cipher->mode =3D mode; - cipher->opaque =3D ctx; - - return cipher; -} +static struct QCryptoCipherDriver qcrypto_cipher_lib_driver =3D { + .cipher_encrypt =3D qcrypto_nettle_cipher_encrypt, + .cipher_decrypt =3D qcrypto_nettle_cipher_decrypt, + .cipher_setiv =3D qcrypto_nettle_cipher_setiv, + .cipher_free =3D qcrypto_nettle_cipher_ctx_free, +}; diff --git a/crypto/cipher.c b/crypto/cipher.c index 5a96489..a6e052c 100644 --- a/crypto/cipher.c +++ b/crypto/cipher.c @@ -21,6 +21,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/cipher.h" +#include "cipherpriv.h" =20 =20 static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] =3D { @@ -155,3 +156,67 @@ qcrypto_cipher_munge_des_rfb_key(const uint8_t *key, #else #include "crypto/cipher-builtin.c" #endif + +QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoCipher *cipher; + void *ctx; + + ctx =3D qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); + if (ctx =3D=3D NULL) { + return NULL; + } + + cipher =3D g_new0(QCryptoCipher, 1); + cipher->alg =3D alg; + cipher->mode =3D mode; + cipher->opaque =3D ctx; + cipher->driver =3D (void *)&qcrypto_cipher_lib_driver; + + return cipher; +} + + +int qcrypto_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) +{ + QCryptoCipherDriver *drv =3D cipher->driver; + return drv->cipher_encrypt(cipher, in, out, len, errp); +} + + +int qcrypto_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) +{ + QCryptoCipherDriver *drv =3D cipher->driver; + return drv->cipher_decrypt(cipher, in, out, len, errp); +} + + +int qcrypto_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) +{ + QCryptoCipherDriver *drv =3D cipher->driver; + return drv->cipher_setiv(cipher, iv, niv, errp); +} + + +void qcrypto_cipher_free(QCryptoCipher *cipher) +{ + QCryptoCipherDriver *drv; + if (cipher) { + drv =3D cipher->driver; + drv->cipher_free(cipher); + g_free(cipher); + } +} diff --git a/crypto/cipherpriv.h b/crypto/cipherpriv.h new file mode 100644 index 0000000..4af5e85 --- /dev/null +++ b/crypto/cipherpriv.h @@ -0,0 +1,40 @@ +/* + * QEMU Crypto cipher 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_CIPHERPRIV_H +#define QCRYPTO_CIPHERPRIV_H + +typedef struct QCryptoCipherDriver QCryptoCipherDriver; + +struct QCryptoCipherDriver { + int (*cipher_encrypt)(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp); + + int (*cipher_decrypt)(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp); + + int (*cipher_setiv)(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp); + + void (*cipher_free)(QCryptoCipher *cipher); +}; + +#endif diff --git a/include/crypto/cipher.h b/include/crypto/cipher.h index bec9f41..984fb82 100644 --- a/include/crypto/cipher.h +++ b/include/crypto/cipher.h @@ -80,6 +80,7 @@ struct QCryptoCipher { QCryptoCipherAlgorithm alg; QCryptoCipherMode mode; void *opaque; + void *driver; }; =20 /** --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492846101258354.9250501788697; Sat, 22 Apr 2017 00:28:21 -0700 (PDT) Received: from localhost ([::1]:34527 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pSt-0002iH-Nj for importer@patchew.org; Sat, 22 Apr 2017 03:28:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48135) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pM5-0003aJ-Jn for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM2-0003lu-In for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:17 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3499 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM1-0003jN-Nj for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:14 -0400 Received: from 172.30.72.56 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.56]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id ANE62804; Sat, 22 Apr 2017 15:21:10 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:02 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:15 +0800 Message-ID: <1492845627-4384-7-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.58FB0466.009A, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 6b663994856df5431f96fd7bb6016c74 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v3 06/18] crypto: hash: add hash 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 1) makes the public APIs in hash-nettle/gcrypt/glib static, and rename them with "nettle/gcrypt/glib" prefix. 2) introduces hash framework, including QCryptoHashDriver and new public APIs. Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) Reviewed-by: Daniel P. Berrange --- crypto/hash-gcrypt.c | 19 +++++++++++++------ crypto/hash-glib.c | 19 +++++++++++++------ crypto/hash-nettle.c | 19 +++++++++++++------ crypto/hash.c | 13 +++++++++++++ crypto/hashpriv.h | 31 +++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 crypto/hashpriv.h diff --git a/crypto/hash-gcrypt.c b/crypto/hash-gcrypt.c index 7690690..972beaa 100644 --- a/crypto/hash-gcrypt.c +++ b/crypto/hash-gcrypt.c @@ -22,6 +22,7 @@ #include #include "qapi/error.h" #include "crypto/hash.h" +#include "hashpriv.h" =20 =20 static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX] =3D { @@ -44,12 +45,13 @@ gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg) } =20 =20 -int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, - const struct iovec *iov, - size_t niov, - uint8_t **result, - size_t *resultlen, - Error **errp) +static int +qcrypto_gcrypt_hash_bytesv(QCryptoHashAlgorithm alg, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp) { int i, ret; gcry_md_hd_t md; @@ -107,3 +109,8 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, gcry_md_close(md); return -1; } + + +QCryptoHashDriver qcrypto_hash_lib_driver =3D { + .hash_bytesv =3D qcrypto_gcrypt_hash_bytesv, +}; diff --git a/crypto/hash-glib.c b/crypto/hash-glib.c index ec99ac9..fb16ac0 100644 --- a/crypto/hash-glib.c +++ b/crypto/hash-glib.c @@ -21,6 +21,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hash.h" +#include "hashpriv.h" =20 =20 static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX] =3D { @@ -47,12 +48,13 @@ gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg) } =20 =20 -int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, - const struct iovec *iov, - size_t niov, - uint8_t **result, - size_t *resultlen, - Error **errp) +static int +qcrypto_glib_hash_bytesv(QCryptoHashAlgorithm alg, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp) { int i, ret; GChecksum *cs; @@ -95,3 +97,8 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, g_checksum_free(cs); return -1; } + + +QCryptoHashDriver qcrypto_hash_lib_driver =3D { + .hash_bytesv =3D qcrypto_glib_hash_bytesv, +}; diff --git a/crypto/hash-nettle.c b/crypto/hash-nettle.c index 6a206dc..96f186f 100644 --- a/crypto/hash-nettle.c +++ b/crypto/hash-nettle.c @@ -21,6 +21,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hash.h" +#include "hashpriv.h" #include #include #include @@ -103,12 +104,13 @@ gboolean qcrypto_hash_supports(QCryptoHashAlgorithm a= lg) } =20 =20 -int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, - const struct iovec *iov, - size_t niov, - uint8_t **result, - size_t *resultlen, - Error **errp) +static int +qcrypto_nettle_hash_bytesv(QCryptoHashAlgorithm alg, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp) { int i; union qcrypto_hash_ctx ctx; @@ -152,3 +154,8 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, =20 return 0; } + + +QCryptoHashDriver qcrypto_hash_lib_driver =3D { + .hash_bytesv =3D qcrypto_nettle_hash_bytesv, +}; diff --git a/crypto/hash.c b/crypto/hash.c index 0f1ceac..c43fd87 100644 --- a/crypto/hash.c +++ b/crypto/hash.c @@ -21,6 +21,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hash.h" +#include "hashpriv.h" =20 static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX] =3D { [QCRYPTO_HASH_ALG_MD5] =3D 16, @@ -38,6 +39,18 @@ size_t qcrypto_hash_digest_len(QCryptoHashAlgorithm alg) return qcrypto_hash_alg_size[alg]; } =20 +int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp) +{ + return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov, + result, resultlen, + errp); +} + =20 int qcrypto_hash_bytes(QCryptoHashAlgorithm alg, const char *buf, diff --git a/crypto/hashpriv.h b/crypto/hashpriv.h new file mode 100644 index 0000000..5e505e0 --- /dev/null +++ b/crypto/hashpriv.h @@ -0,0 +1,31 @@ +/* + * QEMU Crypto hash 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_HASHPRIV_H +#define QCRYPTO_HASHPRIV_H + +typedef struct QCryptoHashDriver QCryptoHashDriver; + +struct QCryptoHashDriver { + int (*hash_bytesv)(QCryptoHashAlgorithm alg, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp); +}; + +extern QCryptoHashDriver qcrypto_hash_lib_driver; + +#endif --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492845947021467.4695550582712; Sat, 22 Apr 2017 00:25:47 -0700 (PDT) Received: from localhost ([::1]:34519 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pQP-0008CN-IP for importer@patchew.org; Sat, 22 Apr 2017 03:25:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48128) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pM5-0003aF-I1 for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM3-0003nC-BC for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:17 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3502 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM2-0003k1-2x for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:15 -0400 Received: from 172.30.72.56 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.56]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id ANE62807; Sat, 22 Apr 2017 15:21:11 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:02 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:16 +0800 Message-ID: <1492845627-4384-8-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020201.58FB0467.007A, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 9c506c42198170982dda52d64106575e X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v3 07/18] crypto: hmac: move crypto/hmac.h into include/crypto/ 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Moves crypto/hmac.h into include/crypto/, likes cipher.h and hash.h Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) Reviewed-by: Daniel P. Berrange --- crypto/hmac.h | 166 ----------------------------------------------= ---- include/crypto/hmac.h | 166 ++++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 166 insertions(+), 166 deletions(-) delete mode 100644 crypto/hmac.h create mode 100644 include/crypto/hmac.h diff --git a/crypto/hmac.h b/crypto/hmac.h deleted file mode 100644 index 0d3acd7..0000000 --- a/crypto/hmac.h +++ /dev/null @@ -1,166 +0,0 @@ -/* - * QEMU Crypto hmac algorithms - * - * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. - * - * 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_HMAC_H -#define QCRYPTO_HMAC_H - -#include "qapi-types.h" - -typedef struct QCryptoHmac QCryptoHmac; -struct QCryptoHmac { - QCryptoHashAlgorithm alg; - void *opaque; -}; - -/** - * qcrypto_hmac_supports: - * @alg: the hmac algorithm - * - * Determine if @alg hmac algorithm is supported by - * the current configured build - * - * Returns: - * true if the algorithm is supported, false otherwise - */ -bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg); - -/** - * qcrypto_hmac_new: - * @alg: the hmac algorithm - * @key: the key bytes - * @nkey: the length of @key - * @errp: pointer to a NULL-initialized error object - * - * Creates a new hmac object with the algorithm @alg - * - * The @key parameter provides the bytes representing - * the secret key to use. The @nkey parameter specifies - * the length of @key in bytes - * - * Note: must use qcrypto_hmac_free() to release the - * returned hmac object when no longer required - * - * Returns: - * a new hmac object, or NULL on error - */ -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp); - -/** - * qcrypto_hmac_free: - * @hmac: the hmac object - * - * Release the memory associated with @hmac that was - * previously allocated by qcrypto_hmac_new() - */ -void qcrypto_hmac_free(QCryptoHmac *hmac); - -/** - * qcrypto_hmac_bytesv: - * @hmac: the hmac object - * @iov: the array of memory regions to hmac - * @niov: the length of @iov - * @result: pointer to hold output hmac - * @resultlen: pointer to hold length of @result - * @errp: pointer to a NULL-initialized error object - * - * Computes the hmac across all the memory regions - * present in @iov. The @result pointer will be - * filled with raw bytes representing the computed - * hmac, which will have length @resultlen. The - * memory pointer in @result must be released - * with a call to g_free() when no longer required. - * - * Returns: - * 0 on success, -1 on error - */ -int qcrypto_hmac_bytesv(QCryptoHmac *hmac, - const struct iovec *iov, - size_t niov, - uint8_t **result, - size_t *resultlen, - Error **errp); - -/** - * qcrypto_hmac_bytes: - * @hmac: the hmac object - * @buf: the memory region to hmac - * @len: the length of @buf - * @result: pointer to hold output hmac - * @resultlen: pointer to hold length of @result - * @errp: pointer to a NULL-initialized error object - * - * Computes the hmac across all the memory region - * @buf of length @len. The @result pointer will be - * filled with raw bytes representing the computed - * hmac, which will have length @resultlen. The - * memory pointer in @result must be released - * with a call to g_free() when no longer required. - * - * Returns: - * 0 on success, -1 on error - */ -int qcrypto_hmac_bytes(QCryptoHmac *hmac, - const char *buf, - size_t len, - uint8_t **result, - size_t *resultlen, - Error **errp); - -/** - * qcrypto_hmac_digestv: - * @hmac: the hmac object - * @iov: the array of memory regions to hmac - * @niov: the length of @iov - * @digest: pointer to hold output hmac - * @errp: pointer to a NULL-initialized error object - * - * Computes the hmac across all the memory regions - * present in @iov. The @digest pointer will be - * filled with the printable hex digest of the computed - * hmac, which will be terminated by '\0'. The - * memory pointer in @digest must be released - * with a call to g_free() when no longer required. - * - * Returns: - * 0 on success, -1 on error - */ -int qcrypto_hmac_digestv(QCryptoHmac *hmac, - const struct iovec *iov, - size_t niov, - char **digest, - Error **errp); - -/** - * qcrypto_hmac_digest: - * @hmac: the hmac object - * @buf: the memory region to hmac - * @len: the length of @buf - * @digest: pointer to hold output hmac - * @errp: pointer to a NULL-initialized error object - * - * Computes the hmac across all the memory region - * @buf of length @len. The @digest pointer will be - * filled with the printable hex digest of the computed - * hmac, which will be terminated by '\0'. The - * memory pointer in @digest must be released - * with a call to g_free() when no longer required. - * - * Returns: 0 on success, -1 on error - */ -int qcrypto_hmac_digest(QCryptoHmac *hmac, - const char *buf, - size_t len, - char **digest, - Error **errp); - -#endif diff --git a/include/crypto/hmac.h b/include/crypto/hmac.h new file mode 100644 index 0000000..0d3acd7 --- /dev/null +++ b/include/crypto/hmac.h @@ -0,0 +1,166 @@ +/* + * QEMU Crypto hmac algorithms + * + * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. + * + * 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_HMAC_H +#define QCRYPTO_HMAC_H + +#include "qapi-types.h" + +typedef struct QCryptoHmac QCryptoHmac; +struct QCryptoHmac { + QCryptoHashAlgorithm alg; + void *opaque; +}; + +/** + * qcrypto_hmac_supports: + * @alg: the hmac algorithm + * + * Determine if @alg hmac algorithm is supported by + * the current configured build + * + * Returns: + * true if the algorithm is supported, false otherwise + */ +bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg); + +/** + * qcrypto_hmac_new: + * @alg: the hmac algorithm + * @key: the key bytes + * @nkey: the length of @key + * @errp: pointer to a NULL-initialized error object + * + * Creates a new hmac object with the algorithm @alg + * + * The @key parameter provides the bytes representing + * the secret key to use. The @nkey parameter specifies + * the length of @key in bytes + * + * Note: must use qcrypto_hmac_free() to release the + * returned hmac object when no longer required + * + * Returns: + * a new hmac object, or NULL on error + */ +QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp); + +/** + * qcrypto_hmac_free: + * @hmac: the hmac object + * + * Release the memory associated with @hmac that was + * previously allocated by qcrypto_hmac_new() + */ +void qcrypto_hmac_free(QCryptoHmac *hmac); + +/** + * qcrypto_hmac_bytesv: + * @hmac: the hmac object + * @iov: the array of memory regions to hmac + * @niov: the length of @iov + * @result: pointer to hold output hmac + * @resultlen: pointer to hold length of @result + * @errp: pointer to a NULL-initialized error object + * + * Computes the hmac across all the memory regions + * present in @iov. The @result pointer will be + * filled with raw bytes representing the computed + * hmac, which will have length @resultlen. The + * memory pointer in @result must be released + * with a call to g_free() when no longer required. + * + * Returns: + * 0 on success, -1 on error + */ +int qcrypto_hmac_bytesv(QCryptoHmac *hmac, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp); + +/** + * qcrypto_hmac_bytes: + * @hmac: the hmac object + * @buf: the memory region to hmac + * @len: the length of @buf + * @result: pointer to hold output hmac + * @resultlen: pointer to hold length of @result + * @errp: pointer to a NULL-initialized error object + * + * Computes the hmac across all the memory region + * @buf of length @len. The @result pointer will be + * filled with raw bytes representing the computed + * hmac, which will have length @resultlen. The + * memory pointer in @result must be released + * with a call to g_free() when no longer required. + * + * Returns: + * 0 on success, -1 on error + */ +int qcrypto_hmac_bytes(QCryptoHmac *hmac, + const char *buf, + size_t len, + uint8_t **result, + size_t *resultlen, + Error **errp); + +/** + * qcrypto_hmac_digestv: + * @hmac: the hmac object + * @iov: the array of memory regions to hmac + * @niov: the length of @iov + * @digest: pointer to hold output hmac + * @errp: pointer to a NULL-initialized error object + * + * Computes the hmac across all the memory regions + * present in @iov. The @digest pointer will be + * filled with the printable hex digest of the computed + * hmac, which will be terminated by '\0'. The + * memory pointer in @digest must be released + * with a call to g_free() when no longer required. + * + * Returns: + * 0 on success, -1 on error + */ +int qcrypto_hmac_digestv(QCryptoHmac *hmac, + const struct iovec *iov, + size_t niov, + char **digest, + Error **errp); + +/** + * qcrypto_hmac_digest: + * @hmac: the hmac object + * @buf: the memory region to hmac + * @len: the length of @buf + * @digest: pointer to hold output hmac + * @errp: pointer to a NULL-initialized error object + * + * Computes the hmac across all the memory region + * @buf of length @len. The @digest pointer will be + * filled with the printable hex digest of the computed + * hmac, which will be terminated by '\0'. The + * memory pointer in @digest must be released + * with a call to g_free() when no longer required. + * + * Returns: 0 on success, -1 on error + */ +int qcrypto_hmac_digest(QCryptoHmac *hmac, + const char *buf, + size_t len, + char **digest, + Error **errp); + +#endif --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492845792574875.4315922328445; Sat, 22 Apr 2017 00:23:12 -0700 (PDT) Received: from localhost ([::1]:34505 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pNv-0005Gm-68 for importer@patchew.org; Sat, 22 Apr 2017 03:23:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48132) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pM5-0003aI-JE for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM3-0003mX-23 for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:17 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3503 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM2-0003k8-Fe for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:15 -0400 Received: from 172.30.72.56 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.56]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id ANE62809; Sat, 22 Apr 2017 15:21:11 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:03 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:17 +0800 Message-ID: <1492845627-4384-9-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.58FB0467.00DE, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: a12ed611c1a92362618827764f4e7dac X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v3 08/18] crypto: hmac: introduce qcrypto_hmac_ctx_new for gcrypt-backend 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 1) Fix a handle-leak problem in qcrypto_hmac_new(), doesn't free ctx->handle if gcry_mac_setkey fails. 2) Extracts qcrypto_hmac_ctx_new() from qcrypto_hmac_new() for gcrypt-backend impls. Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) Reviewed-by: Daniel P. Berrange --- crypto/hmac-gcrypt.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/crypto/hmac-gcrypt.c b/crypto/hmac-gcrypt.c index 21189e6..42489f3 100644 --- a/crypto/hmac-gcrypt.c +++ b/crypto/hmac-gcrypt.c @@ -42,11 +42,11 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) return false; } =20 -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoHmacGcrypt * +qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) { - QCryptoHmac *hmac; QCryptoHmacGcrypt *ctx; gcry_error_t err; =20 @@ -56,9 +56,6 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, return NULL; } =20 - hmac =3D g_new0(QCryptoHmac, 1); - hmac->alg =3D alg; - ctx =3D g_new0(QCryptoHmacGcrypt, 1); =20 err =3D gcry_mac_open(&ctx->handle, qcrypto_hmac_alg_map[alg], @@ -73,15 +70,14 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, if (err !=3D 0) { error_setg(errp, "Cannot set key: %s", gcry_strerror(err)); + gcry_mac_close(ctx->handle); goto error; } =20 - hmac->opaque =3D ctx; - return hmac; + return ctx; =20 error: g_free(ctx); - g_free(hmac); return NULL; } =20 @@ -150,3 +146,22 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac, =20 return 0; } + +QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoHmac *hmac; + QCryptoHmacGcrypt *ctx; + + ctx =3D qcrypto_hmac_ctx_new(alg, key, nkey, errp); + if (ctx =3D=3D NULL) { + return NULL; + } + + hmac =3D g_new0(QCryptoHmac, 1); + hmac->alg =3D alg; + hmac->opaque =3D ctx; + + return hmac; +} --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492846101596598.5487186843753; Sat, 22 Apr 2017 00:28:21 -0700 (PDT) Received: from localhost ([::1]:34528 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pSu-0002it-62 for importer@patchew.org; Sat, 22 Apr 2017 03:28:20 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48126) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pM5-0003aD-Hy for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM3-0003nK-Jj for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:17 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3504 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM3-0003kY-1O for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:15 -0400 Received: from 172.30.72.56 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.56]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id ANE62813; Sat, 22 Apr 2017 15:21:12 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:03 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:18 +0800 Message-ID: <1492845627-4384-10-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.58FB0468.0087, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: a9e5ed94e0c7bf235d31939156e537b7 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v3 09/18] crypto: hmac: introduce qcrypto_hmac_ctx_new for nettle-backend 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extracts qcrypto_hmac_ctx_new() from qcrypto_hmac_new() for nettle-backend impls. Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) Reviewed-by: Daniel P. Berrange --- crypto/hmac-nettle.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/crypto/hmac-nettle.c b/crypto/hmac-nettle.c index 4a9e6b2..19fbb4f 100644 --- a/crypto/hmac-nettle.c +++ b/crypto/hmac-nettle.c @@ -97,11 +97,11 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) return false; } =20 -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoHmacNettle * +qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) { - QCryptoHmac *hmac; QCryptoHmacNettle *ctx; =20 if (!qcrypto_hmac_supports(alg)) { @@ -110,16 +110,11 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm al= g, return NULL; } =20 - hmac =3D g_new0(QCryptoHmac, 1); - hmac->alg =3D alg; - ctx =3D g_new0(QCryptoHmacNettle, 1); =20 qcrypto_hmac_alg_map[alg].setkey(&ctx->u, nkey, key); =20 - hmac->opaque =3D ctx; - - return hmac; + return ctx; } =20 void qcrypto_hmac_free(QCryptoHmac *hmac) @@ -173,3 +168,22 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac, =20 return 0; } + +QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoHmac *hmac; + QCryptoHmacNettle *ctx; + + ctx =3D qcrypto_hmac_ctx_new(alg, key, nkey, errp); + if (ctx =3D=3D NULL) { + return NULL; + } + + hmac =3D g_new0(QCryptoHmac, 1); + hmac->alg =3D alg; + hmac->opaque =3D ctx; + + return hmac; +} --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492846254624481.4937938417437; Sat, 22 Apr 2017 00:30:54 -0700 (PDT) Received: from localhost ([::1]:34538 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pVM-0006GL-Pf for importer@patchew.org; Sat, 22 Apr 2017 03:30:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48221) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pMA-0003fB-EZ for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM8-0003pw-AB for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:22 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:3463 helo=dggrg02-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM7-0003p3-Oe for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:20 -0400 Received: from 172.30.72.53 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.53]) by dggrg02-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AMF64402; Sat, 22 Apr 2017 15:21:16 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:04 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:19 +0800 Message-ID: <1492845627-4384-11-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.58FB046D.0038, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 89efb6155f85e0ae088b5ca7b86be1c4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.188 Subject: [Qemu-devel] [PATCH v3 10/18] crypto: hmac: introduce qcrypto_hmac_ctx_new for glib-backend 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extracts qcrypto_hmac_ctx_new() from qcrypto_hmac_new() for glib-backend impls. Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) Reviewed-by: Daniel P. Berrange --- crypto/hmac-glib.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/crypto/hmac-glib.c b/crypto/hmac-glib.c index 08a1fdd..d9f88d8 100644 --- a/crypto/hmac-glib.c +++ b/crypto/hmac-glib.c @@ -49,11 +49,11 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) return false; } =20 -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoHmacGlib * +qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) { - QCryptoHmac *hmac; QCryptoHmacGlib *ctx; =20 if (!qcrypto_hmac_supports(alg)) { @@ -62,9 +62,6 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, return NULL; } =20 - hmac =3D g_new0(QCryptoHmac, 1); - hmac->alg =3D alg; - ctx =3D g_new0(QCryptoHmacGlib, 1); =20 ctx->ghmac =3D g_hmac_new(qcrypto_hmac_alg_map[alg], @@ -74,12 +71,10 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, goto error; } =20 - hmac->opaque =3D ctx; - return hmac; + return ctx; =20 error: g_free(ctx); - g_free(hmac); return NULL; } =20 @@ -134,6 +129,25 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac, return 0; } =20 +QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoHmac *hmac; + QCryptoHmacGlib *ctx; + + ctx =3D qcrypto_hmac_ctx_new(alg, key, nkey, errp); + if (ctx =3D=3D NULL) { + return NULL; + } + + hmac =3D g_new0(QCryptoHmac, 1); + hmac->alg =3D alg; + hmac->opaque =3D ctx; + + return hmac; +} + #else =20 bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492846530617632.160488161367; Sat, 22 Apr 2017 00:35:30 -0700 (PDT) Received: from localhost ([::1]:34565 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pZp-0002fZ-AY for importer@patchew.org; Sat, 22 Apr 2017 03:35:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48216) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pMA-0003f7-DW for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM7-0003pP-FU for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:22 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:3457 helo=dggrg02-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM6-0003nd-Cu for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:19 -0400 Received: from 172.30.72.53 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.53]) by dggrg02-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AMF64393; Sat, 22 Apr 2017 15:21:15 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:05 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:20 +0800 Message-ID: <1492845627-4384-12-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020206.58FB046B.0067, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 758ea75d2da395a30950431ffa731ddd X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.188 Subject: [Qemu-devel] [PATCH v3 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Longpeng(Mike) Reviewed-by: Daniel P. Berrange --- 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 42489f3..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 =20 static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] =3D { @@ -42,10 +43,9 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) return false; } =20 -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; } =20 -void qcrypto_hmac_free(QCryptoHmac *hmac) +static void +qcrypto_gcrypt_hmac_ctx_free(QCryptoHmac *hmac) { QCryptoHmacGcrypt *ctx; =20 - if (!hmac) { - return; - } - ctx =3D hmac->opaque; gcry_mac_close(ctx->handle); =20 g_free(ctx); - g_free(hmac); } =20 -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; } =20 -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) -{ - QCryptoHmac *hmac; - QCryptoHmacGcrypt *ctx; - - ctx =3D qcrypto_hmac_ctx_new(alg, key, nkey, errp); - if (ctx =3D=3D NULL) { - return NULL; - } - - hmac =3D g_new0(QCryptoHmac, 1); - hmac->alg =3D alg; - hmac->opaque =3D ctx; - - return hmac; -} +QCryptoHmacDriver qcrypto_hmac_lib_driver =3D { + .hmac_bytesv =3D qcrypto_gcrypt_hmac_bytesv, + .hmac_free =3D qcrypto_gcrypt_hmac_ctx_free, +}; diff --git a/crypto/hmac-glib.c b/crypto/hmac-glib.c index d9f88d8..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" =20 /* 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; } =20 -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; =20 @@ -78,27 +78,24 @@ error: return NULL; } =20 -void qcrypto_hmac_free(QCryptoHmac *hmac) +static void +qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac) { QCryptoHmacGlib *ctx; =20 - if (!hmac) { - return; - } - ctx =3D hmac->opaque; g_hmac_unref(ctx->ghmac); =20 g_free(ctx); - g_free(hmac); } =20 -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; } =20 -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) -{ - QCryptoHmac *hmac; - QCryptoHmacGlib *ctx; - - ctx =3D qcrypto_hmac_ctx_new(alg, key, nkey, errp); - if (ctx =3D=3D NULL) { - return NULL; - } - - hmac =3D g_new0(QCryptoHmac, 1); - hmac->alg =3D alg; - hmac->opaque =3D ctx; - - return hmac; -} - #else =20 bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) @@ -155,26 +133,33 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) return false; } =20 -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; } =20 -void qcrypto_hmac_free(QCryptoHmac *hmac) +static void +qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac) { return; } =20 -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; } =20 #endif + +QCryptoHmacDriver qcrypto_hmac_lib_driver =3D { + .hmac_bytesv =3D qcrypto_glib_hmac_bytesv, + .hmac_free =3D qcrypto_glib_hmac_ctx_free, +}; diff --git a/crypto/hmac-nettle.c b/crypto/hmac-nettle.c index 19fbb4f..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 =20 typedef void (*qcrypto_nettle_hmac_setkey)(void *ctx, @@ -97,10 +98,9 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) return false; } =20 -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; =20 @@ -117,26 +117,22 @@ qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, return ctx; } =20 -void qcrypto_hmac_free(QCryptoHmac *hmac) +static void +qcrypto_nettle_hmac_ctx_free(QCryptoHmac *hmac) { QCryptoHmacNettle *ctx; =20 - if (!hmac) { - return; - } - ctx =3D hmac->opaque; - g_free(ctx); - g_free(hmac); } =20 -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; } =20 -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) -{ - QCryptoHmac *hmac; - QCryptoHmacNettle *ctx; - - ctx =3D qcrypto_hmac_ctx_new(alg, key, nkey, errp); - if (ctx =3D=3D NULL) { - return NULL; - } - - hmac =3D g_new0(QCryptoHmac, 1); - hmac->alg =3D alg; - hmac->opaque =3D ctx; - - return hmac; -} +QCryptoHmacDriver qcrypto_hmac_lib_driver =3D { + .hmac_bytesv =3D qcrypto_nettle_hmac_bytesv, + .hmac_free =3D qcrypto_nettle_hmac_ctx_free, +}; diff --git a/crypto/hmac.c b/crypto/hmac.c index 5750405..d040fbb 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" =20 static const char hex[] =3D "0123456789abcdef"; =20 +int qcrypto_hmac_bytesv(QCryptoHmac *hmac, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp) +{ + QCryptoHmacDriver *drv =3D 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, =20 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 =3D qcrypto_hmac_ctx_new(alg, key, nkey, errp); + if (ctx =3D=3D NULL) { + return NULL; + } + + hmac =3D g_new0(QCryptoHmac, 1); + hmac->alg =3D alg; + hmac->opaque =3D ctx; + hmac->driver =3D (void *)&qcrypto_hmac_lib_driver; + + return hmac; +} + +void qcrypto_hmac_free(QCryptoHmac *hmac) +{ + QCryptoHmacDriver *drv; + + if (hmac) { + drv =3D 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; }; =20 /** --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492846428730920.2369867386296; Sat, 22 Apr 2017 00:33:48 -0700 (PDT) Received: from localhost ([::1]:34549 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pYB-0000iR-CH for importer@patchew.org; Sat, 22 Apr 2017 03:33:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48222) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pMA-0003fC-Ec for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM8-0003q8-EG for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:22 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:3462 helo=dggrg02-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM7-0003ok-KA for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:20 -0400 Received: from 172.30.72.53 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.53]) by dggrg02-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AMF64403; Sat, 22 Apr 2017 15:21:16 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:05 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:21 +0800 Message-ID: <1492845627-4384-13-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020206.58FB046D.001A, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 8758e861ec1b140280d8d32ff9288443 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.188 Subject: [Qemu-devel] [PATCH v3 12/18] crypto: introduce some common functions for af_alg backend 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The AF_ALG socket family is the userspace interface for linux crypto API, this patch adds af_alg family support and some common functions for af_alg backend. It'll be used by afalg-backend crypto latter. Signed-off-by: Longpeng(Mike) --- configure | 21 +++++++++ crypto/Makefile.objs | 1 + crypto/afalg.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ crypto/afalgpriv.h | 59 ++++++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 crypto/afalg.c create mode 100644 crypto/afalgpriv.h diff --git a/configure b/configure index 6db3044..db0e183 100755 --- a/configure +++ b/configure @@ -4744,6 +4744,23 @@ if compile_prog "" "" ; then have_af_vsock=3Dyes fi =20 +########################################## +# check for usable AF_ALG environment +hava_af_alg=3Dno +cat > $TMPC << EOF +#include +#include +#include +int main(void) { + int sock; + sock =3D socket(AF_ALG, SOCK_SEQPACKET, 0); + return sock; +} +EOF +if compile_prog "" "" ; then + have_af_alg=3Dyes +fi + ################################################# # Sparc implicitly links with --relax, which is # incompatible with -r, so --no-relax should be @@ -5774,6 +5791,10 @@ if test "$have_af_vsock" =3D "yes" ; then echo "CONFIG_AF_VSOCK=3Dy" >> $config_host_mak fi =20 +if test "$have_af_alg" =3D "yes" ; then + echo "CONFIG_AF_ALG=3Dy" >> $config_host_mak +fi + if test "$have_sysmacros" =3D "yes" ; then echo "CONFIG_SYSMACROS=3Dy" >> $config_host_mak fi diff --git a/crypto/Makefile.objs b/crypto/Makefile.objs index 1f749f2..2be5a3a 100644 --- a/crypto/Makefile.objs +++ b/crypto/Makefile.objs @@ -10,6 +10,7 @@ crypto-obj-$(if $(CONFIG_NETTLE),n,$(if $(CONFIG_GCRYPT_H= MAC),n,y)) +=3D hmac-glib crypto-obj-y +=3D aes.o crypto-obj-y +=3D desrfb.o crypto-obj-y +=3D cipher.o +crypto-obj-$(CONFIG_AF_ALG) +=3D afalg.o crypto-obj-y +=3D tlscreds.o crypto-obj-y +=3D tlscredsanon.o crypto-obj-y +=3D tlscredsx509.o diff --git a/crypto/afalg.c b/crypto/afalg.c new file mode 100644 index 0000000..80c5cfd --- /dev/null +++ b/crypto/afalg.c @@ -0,0 +1,118 @@ +/* + * QEMU Crypto af_alg support + * + * 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. + */ +#include "qemu/osdep.h" +#include "qemu/cutils.h" +#include "qemu/sockets.h" +#include "qapi/error.h" +#include "afalgpriv.h" + +static bool +qcrypto_afalg_build_saddr(const char *type, const char *name, + struct sockaddr_alg *salg, Error **errp) +{ + salg->salg_family =3D AF_ALG; + + if (qemu_strnlen(type, SALG_TYPE_LEN_MAX) =3D=3D SALG_TYPE_LEN_MAX) { + error_setg(errp, "Afalg type(%s) is larger than %d bytes", + type, SALG_TYPE_LEN_MAX); + return false; + } + + if (qemu_strnlen(name, SALG_NAME_LEN_MAX) =3D=3D SALG_NAME_LEN_MAX) { + error_setg(errp, "Afalg name(%s) is larger than %d bytes", + name, SALG_NAME_LEN_MAX); + return false; + } + + pstrcpy((char *)salg->salg_type, SALG_TYPE_LEN_MAX, type); + pstrcpy((char *)salg->salg_name, SALG_NAME_LEN_MAX, name); + + return true; +} + +static int +qcrypto_afalg_socket_bind(const char *type, const char *name, + Error **errp) +{ + int sbind; + struct sockaddr_alg salg =3D {0}; + + if (!qcrypto_afalg_build_saddr(type, name, &salg, errp)) { + return -1; + } + + sbind =3D qemu_socket(AF_ALG, SOCK_SEQPACKET, 0); + if (sbind < 0) { + error_setg_errno(errp, errno, "Failed to create socket"); + return -1; + } + + if (bind(sbind, (const struct sockaddr *)&salg, sizeof(salg)) !=3D 0) { + error_setg_errno(errp, errno, "Failed to bind socket"); + closesocket(sbind); + return -1; + } + + return sbind; +} + +QCryptoAFAlg * +qcrypto_afalg_comm_alloc(const char *type, const char *name, + Error **errp) +{ + QCryptoAFAlg *afalg; + + afalg =3D g_new0(QCryptoAFAlg, 1); + /* initilize crypto API socket */ + afalg->opfd =3D -1; + afalg->tfmfd =3D qcrypto_afalg_socket_bind(type, name, errp); + if (afalg->tfmfd =3D=3D -1) { + goto error; + } + + afalg->opfd =3D qemu_accept(afalg->tfmfd, NULL, 0); + if (afalg->opfd =3D=3D -1) { + error_setg_errno(errp, errno, "Failed to accept socket"); + goto error; + } + + return afalg; + +error: + qcrypto_afalg_comm_free(afalg); + return NULL; +} + +void qcrypto_afalg_comm_free(QCryptoAFAlg *afalg) +{ + if (afalg) { + if (afalg->msg) { + g_free(afalg->msg->msg_control); + g_free(afalg->msg); + } + + if (afalg->name) { + g_free(afalg->name); + } + + if (afalg->tfmfd !=3D -1) { + closesocket(afalg->tfmfd); + } + + if (afalg->opfd !=3D -1) { + closesocket(afalg->opfd); + } + + g_free(afalg); + } +} diff --git a/crypto/afalgpriv.h b/crypto/afalgpriv.h new file mode 100644 index 0000000..f1b0ae5 --- /dev/null +++ b/crypto/afalgpriv.h @@ -0,0 +1,59 @@ +/* + * QEMU Crypto af_alg support + * + * 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_AFALGPRIV_H +#define QCRYPTO_AFALGPRIV_H + +#ifdef CONFIG_AF_ALG + +#include + +#define SALG_TYPE_LEN_MAX 14 +#define SALG_NAME_LEN_MAX 64 + +typedef struct QCryptoAFAlg QCryptoAFAlg; + +struct QCryptoAFAlg { + int tfmfd; + int opfd; + char *name; + struct msghdr *msg; + struct cmsghdr *cmsg; +}; + +/** + * qcrypto_afalg_comm_alloc: + * @type: the type of crypto operation + * @name: the name of crypto operation + * + * Allocate a QCryptoAFAlg object and bind itself to + * a AF_ALG socket. + * + * Returns: + * a new QCryptoAFAlg object, or NULL in error. + */ +QCryptoAFAlg * +qcrypto_afalg_comm_alloc(const char *type, const char *name, + Error **errp); + +/** + * afalg_comm_free: + * @afalg: the QCryptoAFAlg object + * + * Free the @afalg. + */ +void qcrypto_afalg_comm_free(QCryptoAFAlg *afalg); + +#endif + +#endif --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492845942402905.3062034734369; Sat, 22 Apr 2017 00:25:42 -0700 (PDT) Received: from localhost ([::1]:34517 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pQK-000870-Hl for importer@patchew.org; Sat, 22 Apr 2017 03:25:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48219) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pMA-0003f9-EY for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM7-0003pV-Jy for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:22 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:3459 helo=dggrg02-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM6-0003ns-Gr for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:19 -0400 Received: from 172.30.72.53 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.53]) by dggrg02-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AMF64397; Sat, 22 Apr 2017 15:21:15 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:06 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:22 +0800 Message-ID: <1492845627-4384-14-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.58FB046B.00C7, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 6b1f1cb96b5f88ad7697e11e24bb1c44 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.188 Subject: [Qemu-devel] [PATCH v3 13/18] crypto: cipher: add afalg-backend cipher support 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adds afalg-backend cipher support: introduces some private APIs firstly, and then intergrates them into qcrypto_cipher_afalg_driver. Signed-off-by: Longpeng(Mike) --- crypto/Makefile.objs | 1 + crypto/afalgpriv.h | 9 ++ crypto/cipher-afalg.c | 229 +++++++++++++++++++++++++++++++++++++++++= ++++ crypto/cipher.c | 28 +++++- crypto/cipherpriv.h | 11 +++ include/crypto/cipher.h | 8 ++ tests/test-crypto-cipher.c | 10 +- 7 files changed, 294 insertions(+), 2 deletions(-) create mode 100644 crypto/cipher-afalg.c diff --git a/crypto/Makefile.objs b/crypto/Makefile.objs index 2be5a3a..d2e8fa8 100644 --- a/crypto/Makefile.objs +++ b/crypto/Makefile.objs @@ -11,6 +11,7 @@ crypto-obj-y +=3D aes.o crypto-obj-y +=3D desrfb.o crypto-obj-y +=3D cipher.o crypto-obj-$(CONFIG_AF_ALG) +=3D afalg.o +crypto-obj-$(CONFIG_AF_ALG) +=3D cipher-afalg.o crypto-obj-y +=3D tlscreds.o crypto-obj-y +=3D tlscredsanon.o crypto-obj-y +=3D tlscredsx509.o diff --git a/crypto/afalgpriv.h b/crypto/afalgpriv.h index f1b0ae5..e384b15 100644 --- a/crypto/afalgpriv.h +++ b/crypto/afalgpriv.h @@ -21,6 +21,15 @@ #define SALG_TYPE_LEN_MAX 14 #define SALG_NAME_LEN_MAX 64 =20 +#ifndef SOL_ALG +#define SOL_ALG 279 +#endif + +#define AFALG_TYPE_CIPHER "skcipher" + +#define ALG_OPTYPE_LEN 4 +#define ALG_MSGIV_LEN(len) (sizeof(struct af_alg_iv) + (len)) + typedef struct QCryptoAFAlg QCryptoAFAlg; =20 struct QCryptoAFAlg { diff --git a/crypto/cipher-afalg.c b/crypto/cipher-afalg.c new file mode 100644 index 0000000..cce8e6b --- /dev/null +++ b/crypto/cipher-afalg.c @@ -0,0 +1,229 @@ +/* + * QEMU Crypto af_alg-backend cipher support + * + * 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. + */ +#include "qemu/osdep.h" +#include "qemu/sockets.h" +#include "qemu-common.h" +#include "qapi/error.h" +#include "crypto/cipher.h" +#include "cipherpriv.h" + + +static char * +qcrypto_afalg_cipher_format_name(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + Error **errp) +{ + char *name; + const char *alg_name; + const char *mode_name; + int ret; + + switch (alg) { + case QCRYPTO_CIPHER_ALG_AES_128: + case QCRYPTO_CIPHER_ALG_AES_192: + case QCRYPTO_CIPHER_ALG_AES_256: + alg_name =3D "aes"; + break; + case QCRYPTO_CIPHER_ALG_CAST5_128: + alg_name =3D "cast5"; + break; + case QCRYPTO_CIPHER_ALG_SERPENT_128: + case QCRYPTO_CIPHER_ALG_SERPENT_192: + case QCRYPTO_CIPHER_ALG_SERPENT_256: + alg_name =3D "serpent"; + break; + case QCRYPTO_CIPHER_ALG_TWOFISH_128: + case QCRYPTO_CIPHER_ALG_TWOFISH_192: + case QCRYPTO_CIPHER_ALG_TWOFISH_256: + alg_name =3D "twofish"; + break; + + default: + error_setg(errp, "Unsupported cipher algorithm %d", alg); + return NULL; + } + + mode_name =3D QCryptoCipherMode_lookup[mode]; + + name =3D g_new0(char, SALG_NAME_LEN_MAX); + ret =3D snprintf(name, SALG_NAME_LEN_MAX, "%s(%s)", mode_name, + alg_name); + if (ret < 0 || ret >=3D SALG_NAME_LEN_MAX) { + error_setg(errp, "Build ciphername(name=3D'%s',mode=3D'%s') failed= ", + alg_name, mode_name); + g_free(name); + return NULL; + } + + return name; +} + +QCryptoAFAlg * +qcrypto_afalg_cipher_ctx_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, + size_t nkey, Error **errp) +{ + QCryptoAFAlg *afalg; + size_t except_niv; + char *name; + + name =3D qcrypto_afalg_cipher_format_name(alg, mode, errp); + if (!name) { + return NULL; + } + + afalg =3D qcrypto_afalg_comm_alloc(AFALG_TYPE_CIPHER, name, errp); + if (!afalg) { + g_free(name); + return NULL; + } + afalg->name =3D name; + + /* setkey */ + if (qemu_setsockopt(afalg->tfmfd, SOL_ALG, ALG_SET_KEY, key, + nkey) !=3D 0) { + error_setg_errno(errp, errno, "Set key failed"); + qcrypto_afalg_comm_free(afalg); + return NULL; + } + + /* prepare msg header */ + afalg->msg =3D g_new0(struct msghdr, 1); + afalg->msg->msg_controllen +=3D CMSG_SPACE(ALG_OPTYPE_LEN); + except_niv =3D qcrypto_cipher_get_iv_len(alg, mode); + if (except_niv) { + afalg->msg->msg_controllen +=3D CMSG_SPACE(ALG_MSGIV_LEN(except_ni= v)); + } + afalg->msg->msg_control =3D g_new0(uint8_t, afalg->msg->msg_controllen= ); + + /* We use 1st msghdr for crypto-info and 2nd msghdr for IV-info */ + afalg->cmsg =3D CMSG_FIRSTHDR(afalg->msg); + afalg->cmsg->cmsg_level =3D SOL_ALG; + afalg->cmsg->cmsg_type =3D ALG_SET_OP; + afalg->cmsg->cmsg_len =3D CMSG_SPACE(ALG_OPTYPE_LEN); + + return afalg; +} + +static int +qcrypto_afalg_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, + size_t niv, Error **errp) +{ + struct af_alg_iv *alg_iv; + size_t except_niv; + QCryptoAFAlg *afalg =3D cipher->opaque; + + except_niv =3D qcrypto_cipher_get_iv_len(cipher->alg, cipher->mode); + if (niv !=3D except_niv) { + error_setg(errp, "Set IV len(%lu) not match excepted(%lu)", + niv, except_niv); + return -1; + } + + /* move ->cmsg to next msghdr, for IV-info */ + afalg->cmsg =3D CMSG_NXTHDR(afalg->msg, afalg->cmsg); + + /* build setiv msg */ + afalg->cmsg->cmsg_level =3D SOL_ALG; + afalg->cmsg->cmsg_type =3D ALG_SET_IV; + afalg->cmsg->cmsg_len =3D CMSG_SPACE(ALG_MSGIV_LEN(niv)); + alg_iv =3D (struct af_alg_iv *)CMSG_DATA(afalg->cmsg); + alg_iv->ivlen =3D niv; + memcpy(alg_iv->iv, iv, niv); + + return 0; +} + +static int +qcrypto_afalg_cipher_op(QCryptoAFAlg *afalg, + const void *in, void *out, + size_t len, bool do_encrypt, + Error **errp) +{ + uint32_t *type =3D NULL; + struct iovec iov; + size_t ret, done =3D 0; + uint32_t origin_contorllen; + + origin_contorllen =3D afalg->msg->msg_controllen; + /* movev ->cmsg to first header, for crypto-info */ + afalg->cmsg =3D CMSG_FIRSTHDR(afalg->msg); + + /* build encrypt msg */ + afalg->msg->msg_iov =3D &iov; + afalg->msg->msg_iovlen =3D 1; + type =3D (uint32_t *)CMSG_DATA(afalg->cmsg); + if (do_encrypt) { + *type =3D ALG_OP_ENCRYPT; + } else { + *type =3D ALG_OP_DECRYPT; + } + + do { + iov.iov_base =3D (void *)in + done; + iov.iov_len =3D len - done; + + /* send info to AF_ALG core */ + ret =3D sendmsg(afalg->opfd, afalg->msg, 0); + if (ret =3D=3D -1) { + error_setg_errno(errp, errno, "Send data to AF_ALG core failed= "); + return -1; + } + + /* encrypto && get result */ + if (ret !=3D read(afalg->opfd, out, ret)) { + error_setg_errno(errp, errno, "Get result from AF_ALG core fai= led"); + return -1; + } + + /* do not update IV for following chunks */ + afalg->msg->msg_controllen =3D 0; + done +=3D ret; + } while (done < len); + + afalg->msg->msg_controllen =3D origin_contorllen; + + return 0; +} + +static int +qcrypto_afalg_cipher_encrypt(QCryptoCipher *cipher, + const void *in, void *out, + size_t len, Error **errp) +{ + return qcrypto_afalg_cipher_op(cipher->opaque, in, out, + len, true, errp); +} + +static int +qcrypto_afalg_cipher_decrypt(QCryptoCipher *cipher, + const void *in, void *out, + size_t len, Error **errp) +{ + return qcrypto_afalg_cipher_op(cipher->opaque, in, out, + len, false, errp); +} + +static void qcrypto_afalg_comm_ctx_free(QCryptoCipher *cipher) +{ + qcrypto_afalg_comm_free(cipher->opaque); +} + +struct QCryptoCipherDriver qcrypto_cipher_afalg_driver =3D { + .cipher_encrypt =3D qcrypto_afalg_cipher_encrypt, + .cipher_decrypt =3D qcrypto_afalg_cipher_decrypt, + .cipher_setiv =3D qcrypto_afalg_cipher_setiv, + .cipher_free =3D qcrypto_afalg_comm_ctx_free, +}; diff --git a/crypto/cipher.c b/crypto/cipher.c index a6e052c..4a6f548 100644 --- a/crypto/cipher.c +++ b/crypto/cipher.c @@ -164,17 +164,34 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgori= thm alg, { QCryptoCipher *cipher; void *ctx; + Error *err2 =3D NULL; + QCryptoCipherDriver *drv; + +#ifdef CONFIG_AF_ALG + ctx =3D qcrypto_afalg_cipher_ctx_new(alg, mode, key, nkey, &err2); + if (ctx) { + drv =3D &qcrypto_cipher_afalg_driver; + goto set_cipher; + } +#endif =20 ctx =3D qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); if (ctx =3D=3D NULL) { + error_free(err2); return NULL; } =20 + drv =3D &qcrypto_cipher_lib_driver; + error_free(err2); + +#ifdef CONFIG_AF_ALG +set_cipher: +#endif cipher =3D g_new0(QCryptoCipher, 1); cipher->alg =3D alg; cipher->mode =3D mode; cipher->opaque =3D ctx; - cipher->driver =3D (void *)&qcrypto_cipher_lib_driver; + cipher->driver =3D (void *)drv; =20 return cipher; } @@ -220,3 +237,12 @@ void qcrypto_cipher_free(QCryptoCipher *cipher) g_free(cipher); } } + +bool qcrypto_cipher_using_afalg_drv(QCryptoCipher *cipher) +{ +#ifdef CONFIG_AF_ALG + return cipher->driver =3D=3D (void *)&qcrypto_cipher_afalg_driver; +#else + return false; +#endif +} diff --git a/crypto/cipherpriv.h b/crypto/cipherpriv.h index 4af5e85..91c6a7e 100644 --- a/crypto/cipherpriv.h +++ b/crypto/cipherpriv.h @@ -15,6 +15,9 @@ #ifndef QCRYPTO_CIPHERPRIV_H #define QCRYPTO_CIPHERPRIV_H =20 +#include "qapi-types.h" +#include "afalgpriv.h" + typedef struct QCryptoCipherDriver QCryptoCipherDriver; =20 struct QCryptoCipherDriver { @@ -37,4 +40,12 @@ struct QCryptoCipherDriver { void (*cipher_free)(QCryptoCipher *cipher); }; =20 +extern QCryptoAFAlg * +qcrypto_afalg_cipher_ctx_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, + size_t nkey, Error **errp); + +extern struct QCryptoCipherDriver qcrypto_cipher_afalg_driver; + #endif diff --git a/include/crypto/cipher.h b/include/crypto/cipher.h index 984fb82..037f602 100644 --- a/include/crypto/cipher.h +++ b/include/crypto/cipher.h @@ -233,4 +233,12 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, const uint8_t *iv, size_t niv, Error **errp); =20 +/** + * qcrypto_cipher_using_afalg_drv: + * @ the cipher object + * + * Returns: true if @cipher is using afalg driver, otherwise false. + */ +bool qcrypto_cipher_using_afalg_drv(QCryptoCipher *cipher); + #endif /* QCRYPTO_CIPHER_H */ diff --git a/tests/test-crypto-cipher.c b/tests/test-crypto-cipher.c index 07fa2fa..8bb3308 100644 --- a/tests/test-crypto-cipher.c +++ b/tests/test-crypto-cipher.c @@ -715,6 +715,7 @@ static void test_cipher_null_iv(void) uint8_t key[32] =3D { 0 }; uint8_t plaintext[32] =3D { 0 }; uint8_t ciphertext[32] =3D { 0 }; + Error *err =3D NULL; =20 cipher =3D qcrypto_cipher_new( QCRYPTO_CIPHER_ALG_AES_256, @@ -729,7 +730,14 @@ static void test_cipher_null_iv(void) plaintext, ciphertext, sizeof(plaintext), - &error_abort); + &err); + + if (qcrypto_cipher_using_afalg_drv(cipher)) { + g_assert(err !=3D NULL); + error_free_or_abort(&err); + } else { + g_assert(err =3D=3D NULL); + } =20 qcrypto_cipher_free(cipher); } --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492846422863909.1048479243784; Sat, 22 Apr 2017 00:33:42 -0700 (PDT) Received: from localhost ([::1]:34547 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pY5-0000Xy-1H for importer@patchew.org; Sat, 22 Apr 2017 03:33:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48223) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pMA-0003fD-Ec for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM7-0003pI-6r for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:22 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:3458 helo=dggrg02-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM6-0003ng-CW for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:19 -0400 Received: from 172.30.72.53 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.53]) by dggrg02-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AMF64395; Sat, 22 Apr 2017 15:21:15 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:06 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:23 +0800 Message-ID: <1492845627-4384-15-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.58FB046B.007B, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 369a40ca1abc38ddf75b343a426c8ff5 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.188 Subject: [Qemu-devel] [PATCH v3 14/18] crypto: hash: add afalg-backend hash support 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adds afalg-backend hash support: introduces some private APIs firstly, and then intergrates them into qcrypto_hash_afalg_driver. Signed-off-by: Longpeng(Mike) --- crypto/Makefile.objs | 1 + crypto/afalgpriv.h | 1 + crypto/hash-afalg.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ crypto/hash.c | 11 ++++ crypto/hashpriv.h | 4 ++ 5 files changed, 164 insertions(+) create mode 100644 crypto/hash-afalg.c diff --git a/crypto/Makefile.objs b/crypto/Makefile.objs index d2e8fa8..2b99e08 100644 --- a/crypto/Makefile.objs +++ b/crypto/Makefile.objs @@ -12,6 +12,7 @@ crypto-obj-y +=3D desrfb.o crypto-obj-y +=3D cipher.o crypto-obj-$(CONFIG_AF_ALG) +=3D afalg.o crypto-obj-$(CONFIG_AF_ALG) +=3D cipher-afalg.o +crypto-obj-$(CONFIG_AF_ALG) +=3D hash-afalg.o crypto-obj-y +=3D tlscreds.o crypto-obj-y +=3D tlscredsanon.o crypto-obj-y +=3D tlscredsx509.o diff --git a/crypto/afalgpriv.h b/crypto/afalgpriv.h index e384b15..a0950db 100644 --- a/crypto/afalgpriv.h +++ b/crypto/afalgpriv.h @@ -26,6 +26,7 @@ #endif =20 #define AFALG_TYPE_CIPHER "skcipher" +#define AFALG_TYPE_HASH "hash" =20 #define ALG_OPTYPE_LEN 4 #define ALG_MSGIV_LEN(len) (sizeof(struct af_alg_iv) + (len)) diff --git a/crypto/hash-afalg.c b/crypto/hash-afalg.c new file mode 100644 index 0000000..f577c83 --- /dev/null +++ b/crypto/hash-afalg.c @@ -0,0 +1,147 @@ +/* + * QEMU Crypto af_alg-backend hash support + * + * 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. + */ +#include "qemu/osdep.h" +#include "qemu/iov.h" +#include "qemu/sockets.h" +#include "qemu-common.h" +#include "qapi/error.h" +#include "crypto/hash.h" +#include "hashpriv.h" + +static char * +qcrypto_afalg_hash_format_name(QCryptoHashAlgorithm alg, + Error **errp) +{ + char *name; + const char *alg_name; + int ret; + + switch (alg) { + case QCRYPTO_HASH_ALG_MD5: + alg_name =3D "md5"; + break; + case QCRYPTO_HASH_ALG_SHA1: + alg_name =3D "sha1"; + break; + case QCRYPTO_HASH_ALG_SHA224: + alg_name =3D "sha224"; + break; + case QCRYPTO_HASH_ALG_SHA256: + alg_name =3D "sha256"; + break; + case QCRYPTO_HASH_ALG_SHA384: + alg_name =3D "sha384"; + break; + case QCRYPTO_HASH_ALG_SHA512: + alg_name =3D "sha512"; + break; + case QCRYPTO_HASH_ALG_RIPEMD160: + alg_name =3D "rmd160"; + break; + + default: + error_setg(errp, "Unsupported hash algorithm %d", alg); + return NULL; + } + + name =3D g_new0(char, SALG_NAME_LEN_MAX); + ret =3D snprintf(name, SALG_NAME_LEN_MAX, "%s", alg_name); + if (ret < 0 || ret >=3D SALG_NAME_LEN_MAX) { + error_setg(errp, "Build hash name(name=3D'%s') failed", + alg_name); + g_free(name); + return NULL; + } + + return name; +} + +static QCryptoAFAlg * +qcrypto_afalg_hash_ctx_new(QCryptoHashAlgorithm alg, Error **errp) +{ + QCryptoAFAlg *afalg; + char *name; + + name =3D qcrypto_afalg_hash_format_name(alg, errp); + if (!name) { + return NULL; + } + + afalg =3D qcrypto_afalg_comm_alloc(AFALG_TYPE_HASH, name, errp); + if (!afalg) { + g_free(name); + return NULL; + } + afalg->name =3D name; + + /* prepare msg header */ + afalg->msg =3D g_new0(struct msghdr, 1); + + return afalg; +} + +static int +qcrypto_afalg_hash_bytesv(QCryptoHashAlgorithm alg, + const struct iovec *iov, + size_t niov, uint8_t **result, + size_t *resultlen, + Error **errp) +{ + QCryptoAFAlg *afalg; + struct iovec outv; + int ret =3D 0; + const int except_len =3D qcrypto_hash_digest_len(alg); + + if (*resultlen =3D=3D 0) { + *resultlen =3D except_len; + *result =3D g_new0(uint8_t, *resultlen); + } else if (*resultlen !=3D except_len) { + error_setg(errp, + "Result buffer size %zu is not match hash %d", + *resultlen, except_len); + return -1; + } + + afalg =3D qcrypto_afalg_hash_ctx_new(alg, errp); + if (afalg =3D=3D NULL) { + return -1; + } + + /* send data to kernel's crypto core */ + ret =3D iov_send_recv(afalg->opfd, iov, niov, + 0, iov_size(iov, niov), true); + if (ret < 0) { + error_setg_errno(errp, errno, "Send data to afalg-core failed"); + goto out; + } + + /* hash && get result */ + outv.iov_base =3D *result; + outv.iov_len =3D *resultlen; + afalg->msg->msg_iov =3D &outv; + afalg->msg->msg_iovlen =3D 1; + ret =3D recvmsg(afalg->opfd, afalg->msg, 0); + if (ret !=3D -1) { + ret =3D 0; + } else { + error_setg_errno(errp, errno, "Recv result from afalg-core failed"= ); + } + +out: + qcrypto_afalg_comm_free(afalg); + return ret; +} + +QCryptoHashDriver qcrypto_hash_afalg_driver =3D { + .hash_bytesv =3D qcrypto_afalg_hash_bytesv, +}; diff --git a/crypto/hash.c b/crypto/hash.c index c43fd87..ba30c9b 100644 --- a/crypto/hash.c +++ b/crypto/hash.c @@ -46,6 +46,17 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, size_t *resultlen, Error **errp) { +#ifdef CONFIG_AF_ALG + int ret; + + ret =3D qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov, + result, resultlen, + errp); + if (ret =3D=3D 0) { + return ret; + } +#endif + return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov, result, resultlen, errp); diff --git a/crypto/hashpriv.h b/crypto/hashpriv.h index 5e505e0..d23662f 100644 --- a/crypto/hashpriv.h +++ b/crypto/hashpriv.h @@ -15,6 +15,8 @@ #ifndef QCRYPTO_HASHPRIV_H #define QCRYPTO_HASHPRIV_H =20 +#include "afalgpriv.h" + typedef struct QCryptoHashDriver QCryptoHashDriver; =20 struct QCryptoHashDriver { @@ -28,4 +30,6 @@ struct QCryptoHashDriver { =20 extern QCryptoHashDriver qcrypto_hash_lib_driver; =20 +extern QCryptoHashDriver qcrypto_hash_afalg_driver; + #endif --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492846425484527.4758542117165; Sat, 22 Apr 2017 00:33:45 -0700 (PDT) Received: from localhost ([::1]:34548 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pY8-0000ec-0k for importer@patchew.org; Sat, 22 Apr 2017 03:33:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48218) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pMA-0003f8-ES for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM7-0003pb-LQ for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:22 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:3460 helo=dggrg02-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM6-0003oO-Q5 for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:19 -0400 Received: from 172.30.72.53 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.53]) by dggrg02-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AMF64399; Sat, 22 Apr 2017 15:21:16 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:07 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:24 +0800 Message-ID: <1492845627-4384-16-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.58FB046C.005A, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: f4922de74b5fc7a8ecbe2b2124b5ad14 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.188 Subject: [Qemu-devel] [PATCH v3 15/18] crypto: hmac: add af_alg hmac support 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adds afalg-backend hmac support: introduces some private APIs firstly, and then intergrates them into qcrypto_hmac_afalg_driver. Signed-off-by: Longpeng(Mike) --- crypto/hash-afalg.c | 108 +++++++++++++++++++++++++++++++++++++++++++---= ---- crypto/hmac.c | 27 ++++++++++++- crypto/hmacpriv.h | 9 +++++ include/crypto/hmac.h | 8 ++++ 4 files changed, 136 insertions(+), 16 deletions(-) diff --git a/crypto/hash-afalg.c b/crypto/hash-afalg.c index f577c83..0670481 100644 --- a/crypto/hash-afalg.c +++ b/crypto/hash-afalg.c @@ -1,5 +1,5 @@ /* - * QEMU Crypto af_alg-backend hash support + * QEMU Crypto af_alg-backend hash/hmac support * * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. * @@ -16,10 +16,13 @@ #include "qemu-common.h" #include "qapi/error.h" #include "crypto/hash.h" +#include "crypto/hmac.h" #include "hashpriv.h" +#include "hmacpriv.h" =20 static char * qcrypto_afalg_hash_format_name(QCryptoHashAlgorithm alg, + bool is_hmac, Error **errp) { char *name; @@ -55,10 +58,14 @@ qcrypto_afalg_hash_format_name(QCryptoHashAlgorithm alg, } =20 name =3D g_new0(char, SALG_NAME_LEN_MAX); - ret =3D snprintf(name, SALG_NAME_LEN_MAX, "%s", alg_name); + if (is_hmac) { + ret =3D snprintf(name, SALG_NAME_LEN_MAX, "hmac(%s)", alg_name); + } else { /* hash */ + ret =3D snprintf(name, SALG_NAME_LEN_MAX, "%s", alg_name); + } if (ret < 0 || ret >=3D SALG_NAME_LEN_MAX) { - error_setg(errp, "Build hash name(name=3D'%s') failed", - alg_name); + error_setg(errp, "Build %s name(name=3D'%s') failed", + is_hmac ? "hmac" : "hash", alg_name); g_free(name); return NULL; } @@ -67,12 +74,14 @@ qcrypto_afalg_hash_format_name(QCryptoHashAlgorithm alg, } =20 static QCryptoAFAlg * -qcrypto_afalg_hash_ctx_new(QCryptoHashAlgorithm alg, Error **errp) +qcrypto_afalg_hash_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + bool is_hmac, Error **errp) { QCryptoAFAlg *afalg; char *name; =20 - name =3D qcrypto_afalg_hash_format_name(alg, errp); + name =3D qcrypto_afalg_hash_format_name(alg, is_hmac, errp); if (!name) { return NULL; } @@ -84,22 +93,49 @@ qcrypto_afalg_hash_ctx_new(QCryptoHashAlgorithm alg, Er= ror **errp) } afalg->name =3D name; =20 + /* HMAC needs setkey */ + if (is_hmac) { + if (qemu_setsockopt(afalg->tfmfd, SOL_ALG, ALG_SET_KEY, + key, nkey) !=3D 0) { + error_setg_errno(errp, errno, "Set hmac key failed"); + qcrypto_afalg_comm_free(afalg); + return NULL; + } + } + /* prepare msg header */ afalg->msg =3D g_new0(struct msghdr, 1); =20 return afalg; } =20 +static QCryptoAFAlg * +qcrypto_afalg_hash_ctx_new(QCryptoHashAlgorithm alg, + Error **errp) +{ + return qcrypto_afalg_hash_hmac_ctx_new(alg, NULL, 0, false, errp); +} + +QCryptoAFAlg * +qcrypto_afalg_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) +{ + return qcrypto_afalg_hash_hmac_ctx_new(alg, key, nkey, true, errp); +} + static int -qcrypto_afalg_hash_bytesv(QCryptoHashAlgorithm alg, - const struct iovec *iov, - size_t niov, uint8_t **result, - size_t *resultlen, - Error **errp) +qcrypto_afalg_hash_hmac_bytesv(QCryptoAFAlg *hmac, + QCryptoHashAlgorithm alg, + const struct iovec *iov, + size_t niov, uint8_t **result, + size_t *resultlen, + Error **errp) { QCryptoAFAlg *afalg; struct iovec outv; int ret =3D 0; + bool is_hmac =3D (hmac !=3D NULL) ? true : false; const int except_len =3D qcrypto_hash_digest_len(alg); =20 if (*resultlen =3D=3D 0) { @@ -112,9 +148,13 @@ qcrypto_afalg_hash_bytesv(QCryptoHashAlgorithm alg, return -1; } =20 - afalg =3D qcrypto_afalg_hash_ctx_new(alg, errp); - if (afalg =3D=3D NULL) { - return -1; + if (is_hmac) { + afalg =3D hmac; + } else { + afalg =3D qcrypto_afalg_hash_ctx_new(alg, errp); + if (afalg =3D=3D NULL) { + return -1; + } } =20 /* send data to kernel's crypto core */ @@ -138,10 +178,48 @@ qcrypto_afalg_hash_bytesv(QCryptoHashAlgorithm alg, } =20 out: - qcrypto_afalg_comm_free(afalg); + if (!is_hmac) { + qcrypto_afalg_comm_free(afalg); + } return ret; } =20 +static int +qcrypto_afalg_hash_bytesv(QCryptoHashAlgorithm alg, + const struct iovec *iov, + size_t niov, uint8_t **result, + size_t *resultlen, + Error **errp) +{ + return qcrypto_afalg_hash_hmac_bytesv(NULL, alg, iov, niov, result, + resultlen, errp); +} + +static int +qcrypto_afalg_hmac_bytesv(QCryptoHmac *hmac, + const struct iovec *iov, + size_t niov, uint8_t **result, + size_t *resultlen, + Error **errp) +{ + return qcrypto_afalg_hash_hmac_bytesv(hmac->opaque, hmac->alg, + iov, niov, result, resultlen, + errp); +} + +static void qcrypto_afalg_hmac_ctx_free(QCryptoHmac *hmac) +{ + QCryptoAFAlg *afalg; + + afalg =3D hmac->opaque; + qcrypto_afalg_comm_free(afalg); +} + QCryptoHashDriver qcrypto_hash_afalg_driver =3D { .hash_bytesv =3D qcrypto_afalg_hash_bytesv, }; + +QCryptoHmacDriver qcrypto_hmac_afalg_driver =3D { + .hmac_bytesv =3D qcrypto_afalg_hmac_bytesv, + .hmac_free =3D qcrypto_afalg_hmac_ctx_free, +}; diff --git a/crypto/hmac.c b/crypto/hmac.c index d040fbb..0a1a6e7 100644 --- a/crypto/hmac.c +++ b/crypto/hmac.c @@ -90,16 +90,32 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, { QCryptoHmac *hmac; void *ctx; + Error *err2 =3D NULL; + QCryptoHmacDriver *drv; + +#ifdef CONFIG_AF_ALG + ctx =3D qcrypto_afalg_hmac_ctx_new(alg, key, nkey, &err2); + if (ctx) { + drv =3D &qcrypto_hmac_afalg_driver; + goto set_hmac; + } +#endif =20 ctx =3D qcrypto_hmac_ctx_new(alg, key, nkey, errp); if (ctx =3D=3D NULL) { return NULL; } =20 + drv =3D &qcrypto_hmac_lib_driver; + error_free(err2); + +#ifdef CONFIG_AF_ALG +set_hmac: +#endif hmac =3D g_new0(QCryptoHmac, 1); hmac->alg =3D alg; hmac->opaque =3D ctx; - hmac->driver =3D (void *)&qcrypto_hmac_lib_driver; + hmac->driver =3D (void *)drv; =20 return hmac; } @@ -114,3 +130,12 @@ void qcrypto_hmac_free(QCryptoHmac *hmac) g_free(hmac); } } + +bool qcrypto_hmac_using_afalg_drv(QCryptoHmac *hmac) +{ +#ifdef CONFIG_AF_ALG + return hmac->driver =3D=3D &qcrypto_hmac_afalg_driver; +#else + return false; +#endif +} diff --git a/crypto/hmacpriv.h b/crypto/hmacpriv.h index 2be389a..2d1900f 100644 --- a/crypto/hmacpriv.h +++ b/crypto/hmacpriv.h @@ -15,6 +15,8 @@ #ifndef QCRYPTO_HMACPRIV_H #define QCRYPTO_HMACPRIV_H =20 +#include "afalgpriv.h" + typedef struct QCryptoHmacDriver QCryptoHmacDriver; =20 struct QCryptoHmacDriver { @@ -33,4 +35,11 @@ extern void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm a= lg, Error **errp); extern QCryptoHmacDriver qcrypto_hmac_lib_driver; =20 + +extern QCryptoAFAlg * +qcrypto_afalg_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp); +extern QCryptoHmacDriver qcrypto_hmac_afalg_driver; + #endif diff --git a/include/crypto/hmac.h b/include/crypto/hmac.h index 5e88905..450cdee 100644 --- a/include/crypto/hmac.h +++ b/include/crypto/hmac.h @@ -164,4 +164,12 @@ int qcrypto_hmac_digest(QCryptoHmac *hmac, char **digest, Error **errp); =20 +/** + * qcrypto_cipher_using_afalg_drv: + * @hmac: the hmac object + * + * Returns: True if @hmac using afalg driver, otherwise false. + */ +bool qcrypto_hmac_using_afalg_drv(QCryptoHmac *hmac); + #endif --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492846257825439.573300784401; Sat, 22 Apr 2017 00:30:57 -0700 (PDT) Received: from localhost ([::1]:34540 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pVQ-0006OZ-4G for importer@patchew.org; Sat, 22 Apr 2017 03:30:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48224) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pMA-0003fE-Ej for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM7-0003pi-U3 for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:22 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:3461 helo=dggrg02-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM7-0003oQ-2h for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:19 -0400 Received: from 172.30.72.53 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.53]) by dggrg02-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AMF64401; Sat, 22 Apr 2017 15:21:16 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:08 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:25 +0800 Message-ID: <1492845627-4384-17-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.58FB046C.00A4, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: bf7092185e8a82b7cc740cc2c5d47063 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.188 Subject: [Qemu-devel] [PATCH v3 16/18] tests: crypto: add cipher speed benchmark support 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now we have two qcrypto backends, libiary-backend and afalg-backend, but which one is faster? This patch add a cipher speed benchmark, it helps us to measure the performance by using "make check-speed" or using "./tests/benchmark-crypto-cipher" directly. Signed-off-by: Longpeng(Mike) --- tests/Makefile.include | 9 ++++- tests/benchmark-crypto-cipher.c | 90 +++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 tests/benchmark-crypto-cipher.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 579ec07..3a01523 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -101,6 +101,7 @@ gcov-files-test-write-threshold-y =3D block/write-thres= hold.c check-unit-y +=3D tests/test-crypto-hash$(EXESUF) check-unit-y +=3D tests/test-crypto-hmac$(EXESUF) check-unit-y +=3D tests/test-crypto-cipher$(EXESUF) +check-speed-y +=3D tests/benchmark-crypto-cipher$(EXESUF) check-unit-y +=3D tests/test-crypto-secret$(EXESUF) check-unit-$(CONFIG_GNUTLS) +=3D tests/test-crypto-tlscredsx509$(EXESUF) check-unit-$(CONFIG_GNUTLS) +=3D tests/test-crypto-tlssession$(EXESUF) @@ -524,6 +525,7 @@ test-qom-obj-y =3D $(qom-obj-y) $(test-util-obj-y) test-qapi-obj-y =3D tests/test-qapi-visit.o tests/test-qapi-types.o \ tests/test-qapi-event.o tests/test-qmp-introspect.o \ $(test-qom-obj-y) +benchmark-crypto-obj-y =3D $(crypto-obj-y) $(test-qom-obj-y) test-crypto-obj-y =3D $(crypto-obj-y) $(test-qom-obj-y) test-io-obj-y =3D $(io-obj-y) $(test-crypto-obj-y) test-block-obj-y =3D $(block-obj-y) $(test-io-obj-y) tests/iothread.o @@ -628,6 +630,7 @@ tests/test-bitcnt$(EXESUF): tests/test-bitcnt.o $(test-= util-obj-y) tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-ob= j-y) tests/test-crypto-hmac$(EXESUF): tests/test-crypto-hmac.o $(test-crypto-ob= j-y) tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypt= o-obj-y) +tests/benchmark-crypto-cipher$(EXESUF): tests/benchmark-crypto-cipher.o $(= test-crypto-obj-y) tests/test-crypto-secret$(EXESUF): tests/test-crypto-secret.o $(test-crypt= o-obj-y) tests/test-crypto-xts$(EXESUF): tests/test-crypto-xts.o $(test-crypto-obj-= y) =20 @@ -792,6 +795,7 @@ check-help: @echo " make check-qtest-TARGET Run qtest tests for given target" @echo " make check-qtest Run qtest tests" @echo " make check-unit Run qobject tests" + @echo " make check-speed Run qobject speed tests" @echo " make check-qapi-schema Run QAPI schema tests" @echo " make check-block Run block tests" @echo " make check-report.html Generates an HTML test report" @@ -822,8 +826,8 @@ $(patsubst %, check-qtest-%, $(QTEST_TARGETS)): check-q= test-%: $(check-qtest-y) $(GCOV) $(GCOV_OPTIONS) $$f -o `dirname $$f`; \ done,) =20 -.PHONY: $(patsubst %, check-%, $(check-unit-y)) -$(patsubst %, check-%, $(check-unit-y)): check-%: % +.PHONY: $(patsubst %, check-%, $(check-unit-y) $(check-speed-y)) +$(patsubst %, check-%, $(check-unit-y) $(check-speed-y)): check-%: % $(if $(CONFIG_GCOV),@rm -f *.gcda */*.gcda */*/*.gcda */*/*/*.gcda,) $(call quiet-command, \ MALLOC_PERTURB_=3D$${MALLOC_PERTURB_:-$$((RANDOM % 255 + 1))} \ @@ -882,6 +886,7 @@ check-tests/qapi-schema/doc-good.texi: tests/qapi-schem= a/doc-good.test.texi check-qapi-schema: $(patsubst %,check-%, $(check-qapi-schema-y)) check-tes= ts/qapi-schema/doc-good.texi check-qtest: $(patsubst %,check-qtest-%, $(QTEST_TARGETS)) check-unit: $(patsubst %,check-%, $(check-unit-y)) +check-speed: $(patsubst %,check-%, $(check-speed-y)) check-block: $(patsubst %,check-%, $(check-block-y)) check: check-qapi-schema check-unit check-qtest check-clean: diff --git a/tests/benchmark-crypto-cipher.c b/tests/benchmark-crypto-ciphe= r.c new file mode 100644 index 0000000..40594e3 --- /dev/null +++ b/tests/benchmark-crypto-cipher.c @@ -0,0 +1,90 @@ +/* + * QEMU Crypto cipher speed benchmark + * + * 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. + */ +#include "qemu/osdep.h" +#include "crypto/init.h" +#include "crypto/cipher.h" + +static void test_cipher_speed(const void *opaque) +{ + QCryptoCipher *cipher; + Error *err =3D NULL; + double total =3D 0.0; + size_t chunk_size =3D (size_t)opaque; + uint8_t *key =3D NULL, *iv =3D NULL; + uint8_t *plaintext =3D NULL, *ciphertext =3D NULL; + size_t nkey =3D qcrypto_cipher_get_key_len(QCRYPTO_CIPHER_ALG_AES_128); + size_t niv =3D qcrypto_cipher_get_iv_len(QCRYPTO_CIPHER_ALG_AES_128, + QCRYPTO_CIPHER_MODE_CBC); + + key =3D g_new0(uint8_t, nkey); + memset(key, g_test_rand_int(), nkey); + + iv =3D g_new0(uint8_t, niv); + memset(iv, g_test_rand_int(), niv); + + ciphertext =3D g_new0(uint8_t, chunk_size); + + plaintext =3D g_new0(uint8_t, chunk_size); + memset(plaintext, g_test_rand_int(), chunk_size); + + cipher =3D qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_128, + QCRYPTO_CIPHER_MODE_CBC, + key, nkey, &err); + g_assert(cipher !=3D NULL); + + g_assert(qcrypto_cipher_setiv(cipher, + iv, niv, + &err) =3D=3D 0); + + g_test_timer_start(); + do { + g_assert(qcrypto_cipher_encrypt(cipher, + plaintext, + ciphertext, + chunk_size, + &err) =3D=3D 0); + total +=3D chunk_size; + } while (g_test_timer_elapsed() < 5.0); + + total /=3D 1024 * 1024; /* to MB */ + + g_print("[drv:%s]", qcrypto_cipher_using_afalg_drv(cipher) ? + "afalg" : "libs"); + g_print("cbc(aes128): "); + g_print("Testing chunk_size %ld bytes ", chunk_size); + g_print("done: %.2f MB in %.2f secs: ", total, g_test_timer_last()); + g_print("%.2f MB/sec\n", total / g_test_timer_last()); + + qcrypto_cipher_free(cipher); + g_free(plaintext); + g_free(ciphertext); + g_free(iv); + g_free(key); +} + +int main(int argc, char **argv) +{ + size_t i; + char name[64]; + + g_test_init(&argc, &argv, NULL); + g_assert(qcrypto_init(NULL) =3D=3D 0); + + for (i =3D 512; i <=3D (64 * 1204); i *=3D 2) { + memset(name, 0 , sizeof(name)); + snprintf(name, sizeof(name), "/crypto/cipher/speed-%lu", i); + g_test_add_data_func(name, (void *)i, test_cipher_speed); + } + + return g_test_run(); +} --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492846099953831.4246416494453; Sat, 22 Apr 2017 00:28:19 -0700 (PDT) Received: from localhost ([::1]:34526 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pSs-0002fd-BE for importer@patchew.org; Sat, 22 Apr 2017 03:28:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48215) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pMA-0003f6-DB for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pM8-0003q2-D1 for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:22 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:3464 helo=dggrg02-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pM7-0003p5-Ps for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:20 -0400 Received: from 172.30.72.53 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.53]) by dggrg02-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AMF64404; Sat, 22 Apr 2017 15:21:17 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:08 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:26 +0800 Message-ID: <1492845627-4384-18-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020206.58FB046D.0048, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: fabc7596855d04363d6ba516cc592905 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.188 Subject: [Qemu-devel] [PATCH v3 17/18] tests: crypto: add hash speed benchmark support 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch add a hash speed benchmark, it helps us to measure the performance by using "make check-speed" or using "./tests/benchmark-crypto-cipher" directly. Signed-off-by: Longpeng(Mike) --- tests/Makefile.include | 2 ++ tests/benchmark-crypto-hash.c | 67 +++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 69 insertions(+) create mode 100644 tests/benchmark-crypto-hash.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 3a01523..045d16f 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -99,6 +99,7 @@ gcov-files-test-keyval-y =3D util/keyval.c check-unit-y +=3D tests/test-write-threshold$(EXESUF) gcov-files-test-write-threshold-y =3D block/write-threshold.c check-unit-y +=3D tests/test-crypto-hash$(EXESUF) +check-speed-y +=3D tests/benchmark-crypto-hash$(EXESUF) check-unit-y +=3D tests/test-crypto-hmac$(EXESUF) check-unit-y +=3D tests/test-crypto-cipher$(EXESUF) check-speed-y +=3D tests/benchmark-crypto-cipher$(EXESUF) @@ -628,6 +629,7 @@ tests/test-mul64$(EXESUF): tests/test-mul64.o $(test-ut= il-obj-y) tests/test-bitops$(EXESUF): tests/test-bitops.o $(test-util-obj-y) tests/test-bitcnt$(EXESUF): tests/test-bitcnt.o $(test-util-obj-y) tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-ob= j-y) +tests/benchmark-crypto-hash$(EXESUF): tests/benchmark-crypto-hash.o $(test= -crypto-obj-y) tests/test-crypto-hmac$(EXESUF): tests/test-crypto-hmac.o $(test-crypto-ob= j-y) tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypt= o-obj-y) tests/benchmark-crypto-cipher$(EXESUF): tests/benchmark-crypto-cipher.o $(= test-crypto-obj-y) diff --git a/tests/benchmark-crypto-hash.c b/tests/benchmark-crypto-hash.c new file mode 100644 index 0000000..6769d2a --- /dev/null +++ b/tests/benchmark-crypto-hash.c @@ -0,0 +1,67 @@ +/* + * QEMU Crypto hash speed benchmark + * + * 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. + */ +#include "qemu/osdep.h" +#include "crypto/init.h" +#include "crypto/hash.h" + +static void test_hash_speed(const void *opaque) +{ + size_t chunk_size =3D (size_t)opaque; + uint8_t *in =3D NULL, *out =3D NULL; + size_t out_len =3D 0; + double total =3D 0.0; + struct iovec iov; + int ret; + + in =3D g_new0(uint8_t, chunk_size); + memset(in, g_test_rand_int(), chunk_size); + + iov.iov_base =3D (char *)in; + iov.iov_len =3D chunk_size; + + g_test_timer_start(); + do { + ret =3D qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256, + &iov, 1, &out, &out_len, + NULL); + g_assert(ret =3D=3D 0); + + total +=3D chunk_size; + } while (g_test_timer_elapsed() < 5.0); + + total /=3D 1024 * 1024; /* to MB */ + g_print("sha256: "); + g_print("Testing chunk_size %ld bytes ", chunk_size); + g_print("done: %.2f MB in %.2f secs: ", total, g_test_timer_last()); + g_print("%.2f MB/sec\n", total / g_test_timer_last()); + + g_free(out); + g_free(in); +} + +int main(int argc, char **argv) +{ + size_t i; + char name[64]; + + g_test_init(&argc, &argv, NULL); + g_assert(qcrypto_init(NULL) =3D=3D 0); + + for (i =3D 512; i <=3D (64 * 1204); i *=3D 2) { + memset(name, 0 , sizeof(name)); + snprintf(name, sizeof(name), "/crypto/hash/speed-%lu", i); + g_test_add_data_func(name, (void *)i, test_hash_speed); + } + + return g_test_run(); +} --=20 1.8.3.1 From nobody Thu May 2 02:37:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 149284609708377.52569661975758; Sat, 22 Apr 2017 00:28:17 -0700 (PDT) Received: from localhost ([::1]:34525 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pSp-0002am-Dx for importer@patchew.org; Sat, 22 Apr 2017 03:28:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48274) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1pMF-0003lA-59 for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1pMB-0003s1-SE for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:27 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3505 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1pMB-0003qI-2G for qemu-devel@nongnu.org; Sat, 22 Apr 2017 03:21:23 -0400 Received: from 172.30.72.55 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.55]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id ANE62826; Sat, 22 Apr 2017 15:21:20 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 15:21:09 +0800 From: "Longpeng(Mike)" To: Date: Sat, 22 Apr 2017 15:20:27 +0800 Message-ID: <1492845627-4384-19-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> References: <1492845627-4384-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020201.58FB0470.008C, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 3cdaa6350a118a8249b807deb37b1aec X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v3 18/18] tests: crypto: add hmac speed benchmark support 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, longpeng.mike@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch add a hmac speed benchmark, it helps us to measure the performance by using "make check-speed" or using "./tests/benchmark-crypto-hmac" directly. Signed-off-by: Longpeng(Mike) --- tests/Makefile.include | 2 + tests/benchmark-crypto-hmac.c | 96 +++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 98 insertions(+) create mode 100644 tests/benchmark-crypto-hmac.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 045d16f..7b170be 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -101,6 +101,7 @@ gcov-files-test-write-threshold-y =3D block/write-thres= hold.c check-unit-y +=3D tests/test-crypto-hash$(EXESUF) check-speed-y +=3D tests/benchmark-crypto-hash$(EXESUF) check-unit-y +=3D tests/test-crypto-hmac$(EXESUF) +check-speed-y +=3D tests/benchmark-crypto-hmac$(EXESUF) check-unit-y +=3D tests/test-crypto-cipher$(EXESUF) check-speed-y +=3D tests/benchmark-crypto-cipher$(EXESUF) check-unit-y +=3D tests/test-crypto-secret$(EXESUF) @@ -631,6 +632,7 @@ tests/test-bitcnt$(EXESUF): tests/test-bitcnt.o $(test-= util-obj-y) tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-ob= j-y) tests/benchmark-crypto-hash$(EXESUF): tests/benchmark-crypto-hash.o $(test= -crypto-obj-y) tests/test-crypto-hmac$(EXESUF): tests/test-crypto-hmac.o $(test-crypto-ob= j-y) +tests/benchmark-crypto-hmac$(EXESUF): tests/benchmark-crypto-hmac.o $(test= -crypto-obj-y) tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypt= o-obj-y) tests/benchmark-crypto-cipher$(EXESUF): tests/benchmark-crypto-cipher.o $(= test-crypto-obj-y) tests/test-crypto-secret$(EXESUF): tests/test-crypto-secret.o $(test-crypt= o-obj-y) diff --git a/tests/benchmark-crypto-hmac.c b/tests/benchmark-crypto-hmac.c new file mode 100644 index 0000000..be2f2a5 --- /dev/null +++ b/tests/benchmark-crypto-hmac.c @@ -0,0 +1,96 @@ +/* + * QEMU Crypto hmac speed benchmark + * + * 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. + */ +#include "qemu/osdep.h" +#include "crypto/init.h" +#include "crypto/hmac.h" + +#define KEY "monkey monkey monkey monkey" + +static void test_hmac_speed(const void *opaque) +{ + size_t chunk_size =3D (size_t)opaque; + QCryptoHmac *hmac =3D NULL; + uint8_t *in =3D NULL, *out =3D NULL; + size_t out_len =3D 0; + double total =3D 0.0; + struct iovec iov; + Error *err =3D NULL; + int ret; + + if (!qcrypto_hmac_supports(QCRYPTO_HASH_ALG_SHA256)) { + return; + } + + hmac =3D qcrypto_hmac_new(QCRYPTO_HASH_ALG_SHA256, (const uint8_t *)KE= Y, + strlen(KEY), &err); + g_assert(err =3D=3D NULL); + g_assert(hmac !=3D NULL); + + in =3D g_new0(uint8_t, chunk_size); + memset(in, g_test_rand_int(), chunk_size); + + iov.iov_base =3D (char *)in; + iov.iov_len =3D chunk_size; + + g_test_timer_start(); + do { + ret =3D qcrypto_hmac_bytesv(hmac, &iov, 1, &out, &out_len, &err); + g_assert(ret =3D=3D 0); + g_assert(err =3D=3D NULL); + +#if !defined(CONFIG_NETTLE) && !defined(CONFIG_GCRYPT) + /* + * qcrypto_hmac_bytesv() uses g_checksum_get_digest() to get the + * digest. Once this function has been called, the #GChecksum is + * closed and can no longer be updated with g_checksum_update(). + * So...we must free glib-backend hmac object and renew one here. + */ + qcrypto_hmac_free(hmac); + hmac =3D qcrypto_hmac_new(QCRYPTO_HASH_ALG_SHA256, (const uint8_t = *)KEY, + strlen(KEY), &err); + g_assert(err =3D=3D NULL); + g_assert(hmac !=3D NULL); +#endif + total +=3D chunk_size; + } while (g_test_timer_elapsed() < 5.0); + + total /=3D 1024 * 1024; /* to MB */ + + g_print("[drv:%s]", qcrypto_hmac_using_afalg_drv(hmac) ? + "afalg" : "libs"); + g_print("hmac(sha256): "); + g_print("Testing chunk_size %ld bytes ", chunk_size); + g_print("done: %.2f MB in %.2f secs: ", total, g_test_timer_last()); + g_print("%.2f MB/sec\n", total / g_test_timer_last()); + + qcrypto_hmac_free(hmac); + g_free(out); + g_free(in); +} + +int main(int argc, char **argv) +{ + size_t i; + char name[64]; + + g_test_init(&argc, &argv, NULL); + g_assert(qcrypto_init(NULL) =3D=3D 0); + + for (i =3D 512; i <=3D (64 * 1204); i *=3D 2) { + memset(name, 0 , sizeof(name)); + snprintf(name, sizeof(name), "/crypto/hmac/speed-%lu", i); + g_test_add_data_func(name, (void *)i, test_hmac_speed); + } + + return g_test_run(); +} --=20 1.8.3.1