From nobody Sat Apr 27 10:58:03 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.zohomail.com; dkim=fail; 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 1500055583393986.2022442869562; Fri, 14 Jul 2017 11:06:23 -0700 (PDT) Received: from localhost ([::1]:39280 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4yp-0006T2-Kx for importer@patchew.org; Fri, 14 Jul 2017 14:06:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55088) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4wt-0005Bz-M1 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4wr-00032J-4D for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:19 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:34408) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4wq-00031U-Vg for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:17 -0400 Received: by mail-pf0-x243.google.com with SMTP id c24so11760437pfe.1 for ; Fri, 14 Jul 2017 11:04:16 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id d185sm15034536pgc.39.2017.07.14.11.04.15 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=kLjfTKXYACheyjfd72zcKFg4tvUGQaic7q3VlkEPP2E=; b=cutBx93X1+zSElQTYnK1emvjBA4nucz9gHUVIrXxqCna5onPjCk68muYIj/tsHhocl jWhy+RP7zrxPkP26G9kadSL43RbKHO0THL+G5W/6MSny+SIV1FCZ6wy8AAvG9+Fj4eQ9 ZoThF/Kb32pA8sYZP0OIu9vQ/yOxtlRKoxMm8czIEMpL8VpXIs7/5lXEkNPSUW+uk0qF QghvHvp3CFrcnKnVg2KQY5CXoPvzyAjomlXdxt2tbVAsg4PiBq5FXFkdFcasFJa+JTTv hIVH24TdPtaqeAQOuFo+/fB04DO0fP7HuNsoO38GCsiEU5Eb9rZOCsiXcgjMZwBJ66vG +1tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=kLjfTKXYACheyjfd72zcKFg4tvUGQaic7q3VlkEPP2E=; b=Y1DNScK04PhbCjZ5V0GvIY+xWnZYjBsxwxSfqz2szXw3MAM+L7qdBlZHslG3H+uPtU KJpm7Q4bHbWaD8vVgk1/ikYHB08pZo9f7moaoLZK6NGKsCNu/gvnjB/4X19eqPjRDIG8 IuwzDV27s1xDEwbVlkiTi3xrVD2eS4P+77htCmhu1w/hYcnusLkSAMJ133ymiY3qpplb YyOlWrqpTVVbSXZMYyZuhraE8C4Cj1Xzgfl9EFv9gaBaQabtzfaGHHq44wc6YLxO6GQR EgNMFPhHKp+dvLrFml/9+U8rjGIHOn/oDqW0Mp43tM3lswkfUNbUbZLCx0h04F4Ov6zB j+gg== X-Gm-Message-State: AIVw113ua8p88M9jkkvXgPcv85klUT2Z8Z/bAm5Peq1B/ivBfd5DC0JZ ZokOEOOZZczhfw== X-Received: by 10.99.126.69 with SMTP id o5mr16649742pgn.2.1500055456125; Fri, 14 Jul 2017 11:04:16 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:03:54 -0400 Message-Id: <1500055451-14041-2-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" 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: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- 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 Sat Apr 27 10:58:03 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.zohomail.com; dkim=fail; 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 1500055584300551.7708823850606; Fri, 14 Jul 2017 11:06:24 -0700 (PDT) Received: from localhost ([::1]:39281 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4yn-0006TR-Tw for importer@patchew.org; Fri, 14 Jul 2017 14:06:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55089) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4wt-0005C0-MB for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4ws-00034O-HN for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:19 -0400 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:34410) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4ws-00032r-A6 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:18 -0400 Received: by mail-pf0-x244.google.com with SMTP id c24so11760505pfe.1 for ; Fri, 14 Jul 2017 11:04:18 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id q3sm20903903pfk.8.2017.07.14.11.04.16 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=zA6R4j+s3RaGE6vDHeuhYZ1YZgI7F9bAM2qBExxEYrk=; b=kCnedk6SZ1phWuSKgQlmBh+v0AzA5TaepehVlkVEFnDmZGpeeP+k5G50KT+LjqSiez v9/JKDBhmtIwB7akO/Fz540YGCB3TKz5vIF6IOQ90JmlvbnBPdPFyofOPfWxFpTz6wD3 /xfAIJTat1SlfOKF5W54vgnEVB9v34GeWb30eqIICpqkMoOPojnKcIrJkFS7n+GkBUii DWD/3g6YjJwAQGnmMJZWaItzkBmXZj2xC3FxcH6BOmKbrxx6BFIxMlSDXCrF1hmivSJL RAf1q2jHDmQP03+wVazoQ03ctWUX7ujwVeCezthc0qV27t4Zt10ll98mNWemIhvI5IrO TOTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=zA6R4j+s3RaGE6vDHeuhYZ1YZgI7F9bAM2qBExxEYrk=; b=hjdqVGSGpfTVl7kngxCLbC4KV/CCIMiyTDj1ndrgSdyzddcxATDCBlSWMtFJLCI+uz jllNB7vF4Kvk3f8ZC09bHa8nErB+tp01CfHcZ4pCRN5IMFgzWMV2ui9Oo4byzUk4iG4u eTiW9Yts1vuYlW5doje/bycF1q70/lnoeE6z/LDKBTLL+ej9VzwEFkAKXN+FG+zPk0Ee rkovljyT5gN7UvHyJRL8ZaA48OZkpbLBKLb74FoDm0BNmRP0Onkw4CE9pSzNPsmUYvre k0LxsQAwD+NTCE3DTOTWY0KpKRvmoHmpwZQOkINeg/bfCizA+rchXo3FTP9p0+D/Ux4D id8g== X-Gm-Message-State: AIVw1111gaXPyW2cCiOoBt/8Jif4cMp8fQg4dQd6HKhvp/xduiZp4OZY QlJ/r16TrU9vSfHBFAc= X-Received: by 10.101.90.69 with SMTP id z5mr15894289pgs.277.1500055457432; Fri, 14 Jul 2017 11:04:17 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:03:55 -0400 Message-Id: <1500055451-14041-3-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::244 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for gcrypt-backend impls. Reviewed-by: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- 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..4939386 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) { + 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 Sat Apr 27 10:58:03 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.zohomail.com; dkim=fail; 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 1500055584354807.5032993193618; Fri, 14 Jul 2017 11:06:24 -0700 (PDT) Received: from localhost ([::1]:39279 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4yo-0006Sn-0C for importer@patchew.org; Fri, 14 Jul 2017 14:06:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55127) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4wu-0005C8-VI for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4wt-00035c-Pq for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:20 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:36653) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4wt-00034t-Jo for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:19 -0400 Received: by mail-pg0-x244.google.com with SMTP id y129so11394210pgy.3 for ; Fri, 14 Jul 2017 11:04:19 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id z74sm21603981pfd.112.2017.07.14.11.04.17 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4oYrGBqUTGqt7+2pMsePic9NEyXfunjgsJDFVUucTzw=; b=nSfBkLR/QeT4hqqc6G7sHcfkypVoPeH+IRLJOeutraV13FhIhmyuHBFeP9DntfMfVe 3zyBhHrQvPEx9IQ18N1AenUWxrMMDevewYv+TkbcUL2ofIDO2J25OKLFb4rXtRr+KNwr pOuTSQxYHba6alkLW1xBdhlQwE709jOaYpF3fU6KZWzzKpXWZypDRsM+zA6IJeCFF6+k 5xtdMc5Tx8fu5G24Kuwhn83RuieB/U96XtJyOC/dmFg9m6K0afQKnPI3HqqnbiwNLIPr tGfrAQPZKKf0/f/ZzYAcAJRmj+asNE8pZtl+7ODHoEo2AGTZjt3VxBfACMocA6tJ614R tXKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4oYrGBqUTGqt7+2pMsePic9NEyXfunjgsJDFVUucTzw=; b=TeoeWXT38NbxDU2lnvOtPFIGhOVIYgRBLtxWbtu7j2Vm6sN2O0oqnGBJWZmCIx7Z9J AgV6MgMvyEo+7rnZLAr7PjwsZHGeEZidy4Ts98DugePb4tekxmTfU5cA/WSrioV8EeQN SYGR1QPy4EE9uMjnvlwJ0ea72qgy/fdak/xGlRnmSVIXW+I7GZ09B09JjbiNE2jBjidQ Kzeh1DubIugaXVxIJaT8GCj8YzDg64c3gOODz6U+gDA12JhEYc/RkCnSXW+uxInHKQHT +7JInbsf7J9o8WWQg0yjEOH0FTEqKGmmJmg0EzoOndtG16mionDRACKCP4MGpfUjEYE/ VQVQ== X-Gm-Message-State: AIVw1127/hTH+V2UfL9n77f5nGPg1M5sgIdD5zeH6SzrSdIsQ9jLXmac tDhoT7nkq99utw== X-Received: by 10.98.7.87 with SMTP id b84mr6445936pfd.216.1500055458563; Fri, 14 Jul 2017 11:04:18 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:03:56 -0400 Message-Id: <1500055451-14041-4-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::244 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for nettle-backend impls. Reviewed-by: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- 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 Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500055741834342.6532858687068; Fri, 14 Jul 2017 11:09:01 -0700 (PDT) Received: from localhost ([::1]:39291 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW51N-00007U-GJ for importer@patchew.org; Fri, 14 Jul 2017 14:08:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55153) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4ww-0005CQ-61 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4wu-00036g-Qq for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:22 -0400 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:33679) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4wu-00035w-K2 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:20 -0400 Received: by mail-pg0-x242.google.com with SMTP id u62so11375447pgb.0 for ; Fri, 14 Jul 2017 11:04:20 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id a2sm18430870pfe.101.2017.07.14.11.04.19 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=S1OqGz7HmjPUuyiqmTp94rpmbZ3LXpn0vdrkZUmN40I=; b=QIwHBIRVpH/bYQ2OrSuG10knEVAtsTcH8QVVbXVoqtk31BDuAm9DkSboV7oVP8UAdj AbDIGY2UjYYZMCD8UXXp3AcYnwZSTba6cSkSDmfm4OEqfDswdq0b/axLGgRZtxglyJma MNWOK/ti9jDRkS1wP2x8Cm+NQGruRjIXSnHzM0w+3eEuI20LJJmnq0XXyJNzdtNRmLO0 2k9hhUwbYHU4DF58xjbzq7JlHvXBYaHmKpTvW7EYCncYMzFJggG1BpW9HVoo51eFAG3o 9DPmXBEaoAbhGFaZVevhQKT29mLhE4w/m3QnYzpowHTRmwvBU8tFsHvK9cs+leDJilxu xHFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=S1OqGz7HmjPUuyiqmTp94rpmbZ3LXpn0vdrkZUmN40I=; b=jF1ghTEbR1jEPSNFrbF02GKkDbsAkCrZQwj46zpFcM+ZBQQPJP33nRRCUR1k0MNOAn HVsqAy2qZUu1S3Ovvzjo2PfMk5Z80nPIA/F6HQo+pgQDxq9KJol7KV34SLL1lhQoPWcg foOY1TmU70nu5sbBSqsevAAE22KuFQdghEA/tBoqcDDXWOd7liE0qfHHdwKCvrDsGKXT C/ELbQz9B3xeM+52IaZ2/S7Vk8KDOyRMycGSW2qhscQVDXkYQ37oQFagvJXQOSJo/UuP sUnBBbfcbizxPXoi+rcpfhwjTr3pGYb1CcO3Acu7lhrca/n7VaOv6eMVh0LPkqufnF3i uP7Q== X-Gm-Message-State: AIVw110/J8P2WJGbsOuik4Ml9ZEhzxvowmYPl/z5ZfDkdhoPRw9WoecG YGM6N3wRTvEILQ== X-Received: by 10.98.57.4 with SMTP id g4mr6490202pfa.155.1500055459760; Fri, 14 Jul 2017 11:04:19 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:03:57 -0400 Message-Id: <1500055451-14041-5-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::242 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for builtin-backend impls. Reviewed-by: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- 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..4ecd15e 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) { + 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 Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500055889275539.6395059593017; Fri, 14 Jul 2017 11:11:29 -0700 (PDT) Received: from localhost ([::1]:39303 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW53n-00038f-NX for importer@patchew.org; Fri, 14 Jul 2017 14:11:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55182) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4wy-0005Eb-30 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4ww-00038H-8k for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:24 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:35505) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4wv-00037n-WA for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:22 -0400 Received: by mail-pf0-x242.google.com with SMTP id q85so11727861pfq.2 for ; Fri, 14 Jul 2017 11:04:21 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id w66sm20303385pfi.63.2017.07.14.11.04.20 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=uxq0ODblstWlOT1yn/6nMk03/1icIp/MOwJpg+iSaT4=; b=iSQ15JKc4UuKhkxjBidSMNEArbwKpPDuMXm9XbdbhsuvV131nHwd/hPa/+e3u6RZ75 d2HMiMlsbjoafB88LQyDbAs6WIncQTwxvNOM61LNlPhMJKnGsGHDo7QAlpm4DEPuSict aiRvPm7uVkXs/zpxeijKbiaieH9hv4MmFhQxxvNv9FltPB71zKieHBaTShywy3d1Wt7k CQ7O8bvrQPL8jFiW/gAx9pkGJH4PNpbb6IXsSmWBLNcNXaSm9fe08yBy7bd6on5ZmS0W SV+1RjtOrt429u2Ms75ul7HRXUo/ZZPyQ6s/1N0WAgJqxeWNSiwll40xkMY0cuF3CbYT nKrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=uxq0ODblstWlOT1yn/6nMk03/1icIp/MOwJpg+iSaT4=; b=N/QDwXZcTFTmBipFOR4951MKE9/eTaVCZlIb6x6q4e/poyiy2ABAlfxkXNpgGKVpmw ogMuM/RjnM28ucA1jx4d+g2IigkFUnIsKMfO96SqSV5CB3c5Tln41xwywamjbRXTxYDL gS3aEeECfCeE3bN2X9ew1YeTB4MYvhFGtR6sTY3Ib/ACyLEJoAAVA7W+BCOT1uqTQK/n wwaO0I6FBL4HBNuaPLpHEjpZBRvR6V7cZ/HcWAlZDDPO8LLCh2ndhne/EGL28qX30sdO TkFeLUlVyruVlEsEUNl/HOYqYXE1RiJJhEglyHPbodARi5mf9qzRDdToTqpGiH416XVe cnPg== X-Gm-Message-State: AIVw110uwLgfA9byfmDxGNMK1eqYKlLFB3txBhHO83HRgnGfvC5UThLZ XrCoZf8gvKL9+Q== X-Received: by 10.84.217.139 with SMTP id p11mr17928875pli.214.1500055460982; Fri, 14 Jul 2017 11:04:20 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:03:58 -0400 Message-Id: <1500055451-14041-6-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" 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: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- 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 4ecd15e..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) { - 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 4939386..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) { - 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..0a3d2e5 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) { + 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 Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500055590313805.7823580501497; Fri, 14 Jul 2017 11:06:30 -0700 (PDT) Received: from localhost ([::1]:39283 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4yw-0006YY-SM for importer@patchew.org; Fri, 14 Jul 2017 14:06:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55188) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4wy-0005F1-JC for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4wx-000397-BA for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:24 -0400 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:36602) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4wx-00038d-40 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:23 -0400 Received: by mail-pf0-x244.google.com with SMTP id z6so11753940pfk.3 for ; Fri, 14 Jul 2017 11:04:23 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id f87sm19690168pfe.108.2017.07.14.11.04.21 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=3T/Ytu7nWKDWHiMOWPNZ4+oh0I1rtTMYFF0p9qzhNmw=; b=R9P9Wh43h8GGdMkOCKLInmkW9u/u2Fyg8uEZPb9vGLeNG4f/aTSXhgKti0A6RowqID dsR7NPNpgcR1HLFt+mubyz7b6lBgcd8rrIcDZLsdJk4VzNK9RxE2isK9mzNRsPYNwZ62 vFRFpS0gfrzLpui0LT0OcP5SE9CYtgyGl4NeNkRvV5ZTt80gFgC8rMpqwGcHd+D8sarq 0/KRiVrRCKQgv4O2Kx365wW6AFaL6EgNCXXYl4/pnK0SB1Ohpv0cvBCQTWjT6lE+qbF6 Z/A/VWwbwamcGikB27/ePvtX8s9JUAeVddKqwgIepmEX+IlbF+pwlPIdPLxaFecYXhs/ epkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=3T/Ytu7nWKDWHiMOWPNZ4+oh0I1rtTMYFF0p9qzhNmw=; b=MxmMUBQxeDBzbSeq93F90732ZkRUBpMwq25PnWpzW06AXEf6eTMn8jjKgHtJ6vYQnN iWc+b6T+yAVfRfMPoJAKDvPZsxH3/lGPydy6olPSArcRG6h5n7uFpf6l1C0LDjSg+43l Xf11s/SkR1OeYKT2V7+KEBTFKCl/RiKP4QWVP/pSKO841IWQgmEc6OUbmuzXC4PtIKaW uvCbuqC7Gm0kGDvoF0Bli3ZHIffc0gkzNmu9yXKJrjQUj2r7DU4YuG2tbSvfaO2FDJ8Z 21v6TclDKtb3vvudjaHmDHRpk1XglSGIJGhinWtkJTcWwmfWVGsbqKkTYHb4raBEvkVd ztXA== X-Gm-Message-State: AIVw111UAihAbE9drnRci0A5N0d4zt+RmcFEyhMr8x/kZroEcf2oa0xu JKJ+GPG0hRlKMg== X-Received: by 10.84.224.134 with SMTP id s6mr17094774plj.263.1500055462321; Fri, 14 Jul 2017 11:04:22 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:03:59 -0400 Message-Id: <1500055451-14041-7-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::244 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" 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: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- 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..a5871cc 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 Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500055741181839.5801701023314; Fri, 14 Jul 2017 11:09:01 -0700 (PDT) Received: from localhost ([::1]:39290 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW51M-00006q-IL for importer@patchew.org; Fri, 14 Jul 2017 14:08:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55217) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4x0-0005H6-Jx for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4wy-0003Az-Qv for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:26 -0400 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:35512) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4wy-00039z-IW for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:24 -0400 Received: by mail-pf0-x244.google.com with SMTP id q85so11728013pfq.2 for ; Fri, 14 Jul 2017 11:04:24 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id k18sm16317787pgf.5.2017.07.14.11.04.22 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jUvPIpQ5pnALbxS30vuZbBnrM19Mz0vY6KmXxcZNDsw=; b=DGVg4a7MrMEQWmnS3GpP0vPWlq9bZ8wlxCbcI4e1vc24IV65nbTv0kU5SRZ34hF1WJ UAQmnNEds1cihX0S+oRyTMcz20fWvZR7aRybTnjiN5CmfN5vzP1DRcHXACKz09Harloz Snq6iNElUtvdsZZopmI0H12/tZJaD29Qdc5xGQbqvE+cYRymmE8rR7A1iIKREo6cRU+1 UZvHBmMQ5OfnrFY5IUT4cFGCi17hWl+uHMZ7MbO8h3LWGP1y7l1jgR9fdrTYMK8nNlR8 Hjy88b85TC4c0fzHpQqzDMts8q5hXxyfpuqwAJKQtjJQThzsrDbhOhGIuRGMwlpiU2sj qSnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jUvPIpQ5pnALbxS30vuZbBnrM19Mz0vY6KmXxcZNDsw=; b=i1/tWZHyRSZNBWcCaLbwV7vmH7RifdYcuO+qFy2EihV0/nOjqsovg7YUbEiTLo6Lsi 6BY1xWdEkoP9EPIOZPJnEBsR3V4WegZn6nsWPIUK1v98Xr/PtkZy16tul0dtSagmbRPv tStouEHdn7Khl8h/cJpFsIqtXL286eQO4Lp3UL1QM6Ib5X2+XDV4pRYY8LkwM8U9dZhr wl3Sq/2kcS1UGsGPfRCWOi7KY96slOo88JUB4D0ThA41fSoEvrBYJdEcDlbmiZsf9xQw Vd0iwT1IAsWCUB4wG4RFvLogSTDiepj9iPPEZ14m7ghc2GdwTJEtc1F6tH+ABlPOYsJC jRdQ== X-Gm-Message-State: AIVw1117CZrmraueM0XVv9+Vn9fa1MHiAtgNjEVi7Qtgen85VFNVyyf9 t6L6yGJRcpMyPQ== X-Received: by 10.99.95.216 with SMTP id t207mr15940050pgb.19.1500055463562; Fri, 14 Jul 2017 11:04:23 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:00 -0400 Message-Id: <1500055451-14041-8-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::244 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 From: "Longpeng(Mike)" Moves crypto/hmac.h into include/crypto/, likes cipher.h and hash.h Reviewed-by: Daniel P. Berrange Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- 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 Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 150005588963489.75939020435897; Fri, 14 Jul 2017 11:11:29 -0700 (PDT) Received: from localhost ([::1]:39302 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW53m-00038A-Jp for importer@patchew.org; Fri, 14 Jul 2017 14:11:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55260) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4x2-0005JG-Sc for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4wz-0003BS-OX for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:28 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:34424) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4wz-0003B9-JE for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:25 -0400 Received: by mail-pf0-x243.google.com with SMTP id c24so11760875pfe.1 for ; Fri, 14 Jul 2017 11:04:25 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id d184sm19589542pfd.20.2017.07.14.11.04.24 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xeeBA9Xf4XM1R9yH0ixx/jOKrcEJCfPziyHxLwOeoiQ=; b=s3L4JGja2yN3oDDn8U81boXH4d6mOeOXWrepxyG3NDtCNArcQx7J8ms/3CXYLznNJz slPAwY7rUTcYEph7/Gg8UsBNz0lf+4fZKrJmJouCzE4zSRBi+0Adks/GJk97WnWZ6eGj ZdcSM5QONkPkgkm4e1XuFhx+wFVKSxhYoQz1En+lVNzdyJqvIvDSYSk4TDxS6obGinXn q6VMJnPayPbbzSykj3ibMVr+UN68l65F+FcsNS0n0wZA6fSa42o3LppYnHbQlqocqbEv BTsg2gzuk7PVnXLIEzkAfEomwAE5ZvzD2ied2AtrTXdnlbIWl60zwD4X2hZaQLyUaZIZ oNBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xeeBA9Xf4XM1R9yH0ixx/jOKrcEJCfPziyHxLwOeoiQ=; b=eDWfylj+glHRsnWDjfNAYIe6bmXEdF1tRXYsDi5Y9NW3fr8xJYcyAWbmIhQq42nMf/ ahFv0NaigeSp1JgoapXKwTvm21awn3F7xYHFpv5RlQB81FbxPhQBWdFj32DtIGUFs6y9 UwN6kM1c5n8Wbv52ELX54fb7J57aB8/2/d8cKO5t6/+p5aA0va0dGA6vGOLuuCLl7Ysc iL8xvrxTr/AwZF9ng6DBV3aZ5wWjRo7oKjYZ1PuOOvTIa+K2bne7Yn0lblED36PJlZ3j +5XbCklYHc9VE/D37/9Us1MCoNl7gfjwhPAJ9EdtYYGRA5zBJ/kbG1URrFxUBqFgzWw8 A5Ug== X-Gm-Message-State: AIVw1118JB9RFSDcoxa85FVUjKRjvpxCI92OHpQUNU0a/hJKPMNL19L6 mgtHMSGZ8kAdWA== X-Received: by 10.98.107.201 with SMTP id g192mr6510081pfc.65.1500055464786; Fri, 14 Jul 2017 11:04:24 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:01 -0400 Message-Id: <1500055451-14041-9-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" 1) Fix a handle-leak problem in qcrypto_hmac_new(), didn'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: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- 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..372ad7f 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) { + 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 Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500055744789416.5346694839419; Fri, 14 Jul 2017 11:09:04 -0700 (PDT) Received: from localhost ([::1]:39292 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW51S-0000CD-CY for importer@patchew.org; Fri, 14 Jul 2017 14:09:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55262) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4x2-0005JN-Sy for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4x1-0003DV-5S for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:28 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:36610) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4x1-0003CA-02 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:27 -0400 Received: by mail-pf0-x242.google.com with SMTP id z6so11754136pfk.3 for ; Fri, 14 Jul 2017 11:04:26 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id 79sm17426513pgg.46.2017.07.14.11.04.25 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=X/XrCDttJqKRe+u3ulI2zsgMyRFbIRjsUbnMWpnLe4o=; b=A7z/1gi17zRmxkZnVPvWfzh8ijCZ5nNA/+cQbvNlvPsk81biLWSrMkbTuY7hl6TIGM xBVvlrKcuF1hfZxBJ5jqDbB/L+yd768xhjyW8DCDxNAyWgdntLBOgk8/7CXNyvHq51XF R9I3GjG8fQy2HyRj7REwA5RFWKsuTpNTnpI16kWnrny1rXEbUJcNUqhdbXc9jIUWOqCw CpRaXvYMN/hfG9nxZygN8AffLbYrMaVjchlJt41v8zG7+IgG3UX3olQwZX1uX04CEeXy qMDDTP2K3+MA/4hlcHMzWzVQZViqRWK4F+M63r4JkzW2fRiCiOPQY+aY5xKnl9/RNFCM a4cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=X/XrCDttJqKRe+u3ulI2zsgMyRFbIRjsUbnMWpnLe4o=; b=CL2vQEkm9ygLX4ccs82fliv7sYfBYR6Jhk1C7BRSXd5SpJ2wKAKFX1JpzId4mq3uue gaT1MRX0JmdW5nf4t8ydPpj4GFj1xm1s8drz3kQ81bU4/HT2h1ixFjyfy24usI87Yx1p abMVyQJaNqfYdhb4HhpPs2dgzNvQWgT4uXPBSaFWkgkWpwmXDpVqt6Uaf0doHYWc2ltA h08fkzl/E3U74czfZvB3PVY87HDHkVl2E+c/M7AT3wwFvY3yCGl7gv9xCiZ8Kvf3/f04 PNnUZb2UCZqGAQuZCOHreaH4nRi97yZzRldrxuprJQxnXGRruC6kS3z/d9vYSkMdi1UH gFtg== X-Gm-Message-State: AIVw110+DBRdiHM2tBhpfI6vXuVLCC0gXgrjNmhqF8QtQvPb7etk595j uc6nvtlXvStrWVhsou0= X-Received: by 10.98.163.203 with SMTP id q72mr6431465pfl.97.1500055466235; Fri, 14 Jul 2017 11:04:26 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:02 -0400 Message-Id: <1500055451-14041-10-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" Extracts qcrypto_hmac_ctx_new() from qcrypto_hmac_new() for nettle-backend impls. Reviewed-by: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- 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..000dfd9 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) { + 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 Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 150005589152341.792493447456195; Fri, 14 Jul 2017 11:11:31 -0700 (PDT) Received: from localhost ([::1]:39305 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW53q-0003Cz-1r for importer@patchew.org; Fri, 14 Jul 2017 14:11:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55266) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4x3-0005Jg-59 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4x2-0003EX-8z for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:29 -0400 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:34103) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4x2-0003Dw-3j for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:28 -0400 Received: by mail-pg0-x242.google.com with SMTP id j186so11377544pge.1 for ; Fri, 14 Jul 2017 11:04:27 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id k73sm20575556pge.13.2017.07.14.11.04.26 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=tu9HnCtwAaYKj9xNpuvx5voUZ1kSyRjgyPTrhRHlOQI=; b=vE+TPMfsHhBDTh1Qhh5r5aApynT2Lib3QTIhWlikOXkvrc6wGdOpwmf1+uLQEpMtIS O/4fc0uRUa9HKc+LCkPSJo1jDzRv3E0r2riBELt6krw6J548w6EcioA6KnAjvslwf84y gSZ6uD+5QjHYQ7u/x1y7VtRBD0aTSo+Gr8909DdmN8+gZGzxZDRpq+aXPNtlurFrgJrv 68Rc7C3Wvp5VVgUwoVioS9+ldA9iXo3AJRalnRsLa2ViByXjvDTcYefHwAv7RkeYHjsv XawVsVy1ITHzRQuG0/ImPleKY780zd8KvmNjUdUM1Q+P7+pf7AF5EPcN6Hf+jpCoiF7p SLVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=tu9HnCtwAaYKj9xNpuvx5voUZ1kSyRjgyPTrhRHlOQI=; b=EGUCKHhEO4dcI7Yvv6XYHaEE6fE0V9uzozl1C5OGc49V7lt1HnObvPM3iHn7YL2REv 5fjUroCzyAx9jb9b5ttL3xm5oXVyw6L/RuDQz6Hn2L8rm5Kup4ff2A+BUvONO4z41ABU tu+eyB7AqYRxwZc6vpNz3FsyaNpuXrflZcQR+SfM3Lpib8Qfjgsl5rq9twFgVBq03JBO GbvMi+gPFCaKs5ubN58xedVk87xH9okM9sprsv5j0C9jpA6/jWxnefk4XbHF8OZbAKIe wxrwCwWgpTtfA6e2uXna9I+xa1NvtI1aCk0PsjCnKoMs6xgwy7vFSyPWeoplKYywL9Bs Rivw== X-Gm-Message-State: AIVw110VdvnTETT1k/EtX/EeuNMUeps+AfErFRzQj6KeHFpdk89Yzox4 ZJgpxY9VfCWa1TnepbY= X-Received: by 10.84.217.29 with SMTP id o29mr17431157pli.117.1500055467354; Fri, 14 Jul 2017 11:04:27 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:03 -0400 Message-Id: <1500055451-14041-11-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::242 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" Extracts qcrypto_hmac_ctx_new() from qcrypto_hmac_new() for glib-backend impls. Reviewed-by: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- 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..f0ccfd6 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) { + 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 Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500055889886615.8208958333101; Fri, 14 Jul 2017 11:11:29 -0700 (PDT) Received: from localhost ([::1]:39304 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW53o-0003B3-IQ for importer@patchew.org; Fri, 14 Jul 2017 14:11:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55305) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4x5-0005Lo-8N for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4x3-0003Fc-Mr for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:31 -0400 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:33688) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4x3-0003F6-F2 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:29 -0400 Received: by mail-pg0-x242.google.com with SMTP id u62so11375738pgb.0 for ; Fri, 14 Jul 2017 11:04:29 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id l8sm4906326pgs.30.2017.07.14.11.04.27 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Rey6cZZDn3hXg+RXI4njZUu9V26nOXnx6/qp2hgpwFg=; b=fw5iBOreYcgwhRyBcOpHgOMufxfMEIm/zHIziiT9HbF+zFnV9Fgb4G+nWvo3eJv9pt YJLMKBUzPM++WetZyriGPAZjS9LOHTIO5aGuBzlhEtfOrCEaeU0AbtK9W8xH0K4q397X hRTbbx18TxpzQw+++C3zWuyNKlD4LYJ4IUFT2YlNlib5aKSqWDMazAAId4IRWQ9S34W+ PjkdLQPu/KmgCVCyifcBWxkjGBVoghsc+trVqszIBYlhcGNPcB5m60oB1bJE7l6yRae8 zLJkAyYorWeHr9IuRIr1kx4HjD6JL0l4uD5RkM0WG/JMOsCprpc3XqyRIeEtcamqBd+s BOjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Rey6cZZDn3hXg+RXI4njZUu9V26nOXnx6/qp2hgpwFg=; b=oKZELaTrXhwD31W6UbKobRnt9qQmV9AOsBwPV0P/AH8ENYlfKAbX3bIE4qavCRIOUx qKUluSGG3sMX7/E46QqoqbSnwiWtHY+U5EuQh08rjBk32VfQa8WwwbeWYJe0cYfuU8Ai sa7tk8xfvIX55s7XegQ1k82s6LY2daScJ2LfWud23oeeMb+ntkVLoLinupiHgLSxGboy 2UPKWgXw8cFGroBKhVBTVnuiDSddXEaqlZhepna9hoOWKSVH1dLZJPfrkAOboc71JKIN Osreymz3jYP4a3AuQTJIjv3obdOaPg6vLRGK2eyldyMrMKSMub1svcX+uh3Ij+DiJTG9 kZag== X-Gm-Message-State: AIVw111nf87cls4WlwrdVSowUI0eWIbwG3D4ApDnESDQEToLPPM4q0sX 8JKNomUzCKSjzw== X-Received: by 10.101.90.3 with SMTP id y3mr15969046pgs.185.1500055468605; Fri, 14 Jul 2017 11:04:28 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:04 -0400 Message-Id: <1500055451-14041-12-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::242 Subject: [Qemu-devel] [PATCH v6 11/18] crypto: hmac: add hmac driver framework X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Longpeng\(Mike\)" , arei.gonglei@huawei.com, weidong.huang@huawei.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" 1) makes the public APIs in hmac-nettle/gcrypt/glib static, and rename them with "nettle/gcrypt/glib" prefix. 2) introduces hmac framework, including QCryptoHmacDriver and new public APIs. Reviewed-by: Daniel P. Berrange Signed-off-by: Longpeng(Mike) --- crypto/hmac-gcrypt.c | 51 ++++++++++++----------------------- crypto/hmac-glib.c | 75 +++++++++++++++++++++--------------------------= ---- crypto/hmac-nettle.c | 52 ++++++++++++----------------------- crypto/hmac.c | 44 ++++++++++++++++++++++++++++++ crypto/hmacpriv.h | 36 +++++++++++++++++++++++++ include/crypto/hmac.h | 1 + 6 files changed, 145 insertions(+), 114 deletions(-) create mode 100644 crypto/hmacpriv.h diff --git a/crypto/hmac-gcrypt.c b/crypto/hmac-gcrypt.c index 372ad7f..76ca61b 100644 --- a/crypto/hmac-gcrypt.c +++ b/crypto/hmac-gcrypt.c @@ -15,6 +15,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hmac.h" +#include "hmacpriv.h" #include =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) { - 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 f0ccfd6..8cf6b22 100644 --- a/crypto/hmac-glib.c +++ b/crypto/hmac-glib.c @@ -15,6 +15,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hmac.h" +#include "hmacpriv.h" =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) { - 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 000dfd9..1d5a915 100644 --- a/crypto/hmac-nettle.c +++ b/crypto/hmac-nettle.c @@ -15,6 +15,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hmac.h" +#include "hmacpriv.h" #include =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) { - 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..a4690e3 100644 --- a/crypto/hmac.c +++ b/crypto/hmac.c @@ -12,9 +12,22 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hmac.h" +#include "hmacpriv.h" =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) { + 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 Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500056040364981.2403945160821; Fri, 14 Jul 2017 11:14:00 -0700 (PDT) Received: from localhost ([::1]:39311 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW56D-0005TY-T2 for importer@patchew.org; Fri, 14 Jul 2017 14:13:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55324) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4x6-0005M1-Dy for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4x4-0003Gt-W1 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:32 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:34104) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4x4-0003G4-Mf for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:30 -0400 Received: by mail-pg0-x244.google.com with SMTP id j186so11377615pge.1 for ; Fri, 14 Jul 2017 11:04:30 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id b28sm22323841pfm.9.2017.07.14.11.04.29 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=K1+LVT/IkT3LMFEagadiJJi3/3N0SIE+5jvzX3A3kFU=; b=k9VUD86mSlGLm5hHgbEw4IWG66pf4JhofrKdy4PLIzNKSncQEd7OTw0mXInWMKdkL3 XhhviqsHQKPj513oxO6JG/3HRwI+XIEBoiVnAemOZ08XmZTyrsZK1mXstPmgnqcsHliT QVE3Sa6xx+LT+oyaJfddGn6m5BzfnV5P1hOfS5zkz4B1yPoS+utbiDIbV40dfm3q0uAZ thvSXSizwotog+iOSvr1TRm6L12+MMF95tQEIA+n0mubmwvM8P7wIlxvo+QfGhqFno7F 99gsybJuDLzxSsobywAG0UyhE50jWWfe0VhqALF8/LfxYV3ssY1xqRbmMlUEQZW0nyK1 q6zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=K1+LVT/IkT3LMFEagadiJJi3/3N0SIE+5jvzX3A3kFU=; b=LdLSbH6LdPfASwjoqoce9B2m+MTJ/ffBYRDIGEZ3QyJPAyBlxsgPr6bhHQmxa/w5G9 4oLPgwVeLeadajmRC7mbFff6it3qDWFbTTqcXf4tmWUxHFGmxOL9nvZMzNGx7nfK5+tY SsD7zpfRIfYnIP52yFRH6A5qSjUAo8iHvsCxVZA1qH/XkesGCCv2/yMj6bMPbLOmVX1d bp8d/0JE5u6Ponbql7ezyuEHcfrJbz7wCy+05h0XVb6CHSos2aEPNtOiHF8aQq61WBPr e+dVoTTuD6ghRP1vanVnMapJvWiYCozYeD4phjS7tykig8/Zl2u1RPuR+b5ORopRhnQR 8UIQ== X-Gm-Message-State: AIVw111ykfbliln6T1uB0SWymImoPZ5lsJL9KQOMjxKzXLFdtYHqeJHv VbZIO8VX186F5g== X-Received: by 10.98.148.25 with SMTP id m25mr6631046pfe.7.1500055469870; Fri, 14 Jul 2017 11:04:29 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:05 -0400 Message-Id: <1500055451-14041-13-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::244 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" 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 | 30 +++++++++++++ crypto/Makefile.objs | 1 + crypto/afalg.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ crypto/afalgpriv.h | 54 ++++++++++++++++++++++++ 4 files changed, 201 insertions(+) create mode 100644 crypto/afalg.c create mode 100644 crypto/afalgpriv.h diff --git a/configure b/configure index 902653a..4beab2a 100755 --- a/configure +++ b/configure @@ -375,6 +375,7 @@ libnfs=3D"" coroutine=3D"" coroutine_pool=3D"" debug_stack_usage=3D"no" +crypto_afalg=3D"no" seccomp=3D"" glusterfs=3D"" glusterfs_xlator_opt=3D"no" @@ -1124,6 +1125,8 @@ for opt do ;; --enable-debug-stack-usage) debug_stack_usage=3D"yes" ;; + --enable-crypto-afalg) crypto_afalg=3D"yes" + ;; --disable-docs) docs=3D"no" ;; --enable-docs) docs=3D"yes" @@ -1443,6 +1446,8 @@ Advanced options (experts only): xen pv domain builder --enable-debug-stack-usage track the maximum stack usage of stacks created= by qemu_alloc_stack + --enable-crypto-afalg + enable afalg-backend crypto and try to use it f= irst. =20 Optional features, enabled with --enable-FEATURE and disabled with --disable-FEATURE, default is enabled if available: @@ -4834,6 +4839,24 @@ 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 +#include +int main(void) { + int sock; + sock =3D socket(AF_ALG, SOCK_SEQPACKET, 0); + return sock; +} +EOF +if compile_prog "" "" ; then + have_afalg=3Dyes +fi + ################################################# # Sparc implicitly links with --relax, which is # incompatible with -r, so --no-relax should be @@ -5300,6 +5323,7 @@ echo "seccomp support $seccomp" echo "coroutine backend $coroutine" echo "coroutine pool $coroutine_pool" echo "debug stack usage $debug_stack_usage" +echo "crypto afalg $crypto_afalg" echo "GlusterFS support $glusterfs" echo "gcov $gcov_tool" echo "gcov enabled $gcov" @@ -5811,6 +5835,12 @@ if test "$debug_stack_usage" =3D "yes" ; then echo "CONFIG_DEBUG_STACK_USAGE=3Dy" >> $config_host_mak fi =20 +if test "$crypto_afalg" =3D "yes" ; then + if test "$have_afalg" =3D "yes" ; then + echo "CONFIG_AF_ALG=3Dy" >> $config_host_mak + fi +fi + if test "$open_by_handle_at" =3D "yes" ; then echo "CONFIG_OPEN_BY_HANDLE=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..10046bb --- /dev/null +++ b/crypto/afalg.c @@ -0,0 +1,116 @@ +/* + * 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 (strnlen(type, SALG_TYPE_LEN_MAX) >=3D SALG_TYPE_LEN_MAX) { + error_setg(errp, "Afalg type(%s) is larger than %d bytes", + type, SALG_TYPE_LEN_MAX); + return false; + } + + if (strnlen(name, SALG_NAME_LEN_MAX) >=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) { + return; + } + + if (afalg->msg) { + g_free(afalg->msg->msg_control); + g_free(afalg->msg); + } + + 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..76118cf --- /dev/null +++ b/crypto/afalgpriv.h @@ -0,0 +1,54 @@ +/* + * 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 + +#include + +#define SALG_TYPE_LEN_MAX 14 +#define SALG_NAME_LEN_MAX 64 + +typedef struct QCryptoAFAlg QCryptoAFAlg; + +struct QCryptoAFAlg { + int tfmfd; + int opfd; + 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 --=20 1.8.3.1 From nobody Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500056044640358.5322401247729; Fri, 14 Jul 2017 11:14:04 -0700 (PDT) Received: from localhost ([::1]:39314 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW56J-0005kT-9s for importer@patchew.org; Fri, 14 Jul 2017 14:14:03 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55349) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4x7-0005N6-UB for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4x6-0003Im-8x for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:33 -0400 Received: from mail-pg0-x243.google.com ([2607:f8b0:400e:c05::243]:34105) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4x6-0003IF-0A for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:32 -0400 Received: by mail-pg0-x243.google.com with SMTP id j186so11377651pge.1 for ; Fri, 14 Jul 2017 11:04:31 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id x3sm13743301pgx.29.2017.07.14.11.04.30 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=PYarjqA3DqM9B1mA4kcHxOSbxTZdqKzaJqnS1ISWQk0=; b=QCy+TBQmk+E579iciD+bqOaZy4DHpuAw+6Y0HAywGg9lp2MhY/EZDyTyCbcajftH6V g/+BONWTeMhIUspFxDlAVfv0HYrWMyEia6iNwvF3zl/DjUDS/gsgNx+1A+B0ELiqyaYS 2xlpds2XnLSCIIjPSHtyw6Lb3ALI2peu90lo0d7PsTNK30/IA9de3AOsP/MO8wd7Jib5 eAVMOWUcy/aKHreJPVLawaEt0vPAiPI3zJWGWmlLuFHfVYT9W77ti5dvhGT3Tu+Zi5YV Xi8RgT23awKrhurWF+H5s2fHnbJ/MCLs4hXlHkREeCrkuMq98hyZYQzWIEHdX4eqveGN F07w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=PYarjqA3DqM9B1mA4kcHxOSbxTZdqKzaJqnS1ISWQk0=; b=AaJXCSx4GgNQU/wv/zEzIsXvkUmTD866F+HM7dtHf3iwNpaw7MUJmgTylqvOsasgAE fUDAjJmMPKWPyRYGFC4r7WcC7x54HGcX9OvLFCQKYm1qz1ANSJW6fShx74q4wDXcq8M6 qnO24uxHXA6Uflfv57vnMyfNFaNrnOHL+QwY3KIBA4UC0boB74uL0cxt7yxtDcOwbCGa 8rfVcdbABbslY8T45JP6y0HM1ER+SsQO5ywD1DhPrSs1l7ObEKvWaDPb+ns2Oum301Li fShC/saoO2Yg0Hj1R6mgthtPjp4BH1pOz5Tq/aSSqGLgEchxbWsBvLTDAnUGRyJ4qWRx osCA== X-Gm-Message-State: AIVw113VcGHRbr9xuZUOLwU0BYU+m4a/uPvxrdCPl7Ub4sJnb9kQzYb7 X9j+3bwNQ9u/LPy/Dkc= X-Received: by 10.98.101.4 with SMTP id z4mr6460180pfb.163.1500055471019; Fri, 14 Jul 2017 11:04:31 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:06 -0400 Message-Id: <1500055451-14041-14-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::243 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" 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 | 226 ++++++++++++++++++++++++++++++++++++++++++++++= ++++ crypto/cipher.c | 23 ++++- crypto/cipherpriv.h | 16 ++++ 5 files changed, 271 insertions(+), 4 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 76118cf..d0941d4 100644 --- a/crypto/afalgpriv.h +++ b/crypto/afalgpriv.h @@ -19,6 +19,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..01343b2 --- /dev/null +++ b/crypto/cipher-afalg.c @@ -0,0 +1,226 @@ +/* + * 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; + + 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_strdup_printf("%s(%s)", mode_name, alg_name); + + 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 expect_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; + } + + g_free(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); + expect_niv =3D qcrypto_cipher_get_iv_len(alg, mode); + if (expect_niv) { + afalg->msg->msg_controllen +=3D CMSG_SPACE(ALG_MSGIV_LEN(expect_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_type =3D ALG_SET_OP; + afalg->cmsg->cmsg_len =3D CMSG_SPACE(ALG_OPTYPE_LEN); + if (expect_niv) { + afalg->cmsg =3D CMSG_NXTHDR(afalg->msg, afalg->cmsg); + afalg->cmsg->cmsg_type =3D ALG_SET_IV; + afalg->cmsg->cmsg_len =3D CMSG_SPACE(ALG_MSGIV_LEN(expect_niv)); + } + afalg->cmsg =3D CMSG_FIRSTHDR(afalg->msg); + + 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 expect_niv; + QCryptoAFAlg *afalg =3D cipher->opaque; + + expect_niv =3D qcrypto_cipher_get_iv_len(cipher->alg, cipher->mode); + if (niv !=3D expect_niv) { + error_setg(errp, "Set IV len(%zu) not match expected(%zu)", + niv, expect_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; + 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, rlen, done =3D 0; + uint32_t origin_controllen; + + origin_controllen =3D afalg->msg->msg_controllen; + /* movev ->cmsg to first header, for crypto-info */ + afalg->cmsg =3D CMSG_FIRSTHDR(afalg->msg); + + /* build encrypt msg */ + afalg->cmsg->cmsg_level =3D SOL_ALG; + 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 */ + rlen =3D read(afalg->opfd, out, ret); + if (rlen =3D=3D -1) { + error_setg_errno(errp, errno, "Get result from AF_ALG core fai= led"); + return -1; + } + assert(rlen =3D=3D ret); + + /* 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_controllen; + + 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 0a3d2e5..a487270 100644 --- a/crypto/cipher.c +++ b/crypto/cipher.c @@ -163,18 +163,33 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgori= thm alg, Error **errp) { QCryptoCipher *cipher; - void *ctx; + void *ctx =3D NULL; + 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; + } +#endif =20 - ctx =3D qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); if (!ctx) { - return NULL; + ctx =3D qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); + if (!ctx) { + error_free(err2); + return NULL; + } + + drv =3D &qcrypto_cipher_lib_driver; + error_free(err2); } =20 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; } diff --git a/crypto/cipherpriv.h b/crypto/cipherpriv.h index 4af5e85..77da4c2 100644 --- a/crypto/cipherpriv.h +++ b/crypto/cipherpriv.h @@ -15,6 +15,8 @@ #ifndef QCRYPTO_CIPHERPRIV_H #define QCRYPTO_CIPHERPRIV_H =20 +#include "qapi-types.h" + typedef struct QCryptoCipherDriver QCryptoCipherDriver; =20 struct QCryptoCipherDriver { @@ -37,4 +39,18 @@ struct QCryptoCipherDriver { void (*cipher_free)(QCryptoCipher *cipher); }; =20 +#ifdef CONFIG_AF_ALG + +#include "afalgpriv.h" + +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 + #endif --=20 1.8.3.1 From nobody Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500056232105336.0110558389214; Fri, 14 Jul 2017 11:17:12 -0700 (PDT) Received: from localhost ([::1]:39336 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW59J-0000bx-5i for importer@patchew.org; Fri, 14 Jul 2017 14:17:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55361) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4x8-0005Nk-Kg for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4x7-0003Jb-DD for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:34 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:36668) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4x7-0003JA-5M for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:33 -0400 Received: by mail-pg0-x241.google.com with SMTP id y129so11394675pgy.3 for ; Fri, 14 Jul 2017 11:04:33 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id y14sm17560272pfa.52.2017.07.14.11.04.31 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=h/vSD3/V1enal26E9liKlnm7amTt8CC3u5e55RBrSCE=; b=HZVbggSEMMkYITVHJGFvt9ubNGJhiPkKhjPgsNvagGNT1PlqcaE3/qLbCWdhbfohUU p6/0xVSdhBmT7yeeZZXchsvhwf1/NwmF0TH9GP/huA3c4A0eG0ZxQD/DDqFQkxYGgNYc YKrx7+pKWoNAdIpQVwNDlTXM2XIe57QAxqIJpxPAwjD/RfzVZBKmcsArBsXohY4sEPxE 0NND06aOYevs6sBh2qYTcvulzUUJmJhR35MPsnQ/r1brvfm1HWrxLD1ASqcinArObiCG LGPONSQg5IeMTrDS30AVevp0jzo0zYekXSPVWaKScJuTgno0in7aA6owHKX9meFkkZdH mJwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=h/vSD3/V1enal26E9liKlnm7amTt8CC3u5e55RBrSCE=; b=KMXn4xXJ+Q71X5ZEngYQ8sLsnMmTw6LObkzxSqwho3NtTgjSFYHacYCv7xr0LqCBQz PJO1clgi/eYkkANV/RGhyZVmPw2LcAJzw9LIU5c4pgGyHsxqTNJ5L/IhE0xq0RcpwIp7 juP2e+nf4nAZhHJ/EuSlbzkbtRuX+9HGnvF5OgVIzItVd4dOBWZX6yYj4QlVWelTYcWF cczQLgjewgR5yO5+SFOWTOQLWSuUETh/1s3fbidUSxG9XOnTJC/mED7zIA1z3RCaaRQh oPwRY3LNrR7+E8vTVHPGyFQ8QXhVoP3ypai1u9IRV/iyF4FrkHbgrYJHHZrK/XqG4kA1 Q5lg== X-Gm-Message-State: AIVw112GiGt2Orv19Rv4IcETJeatBrX8TupHCNaJc3GerS9FOCJhid+6 ivMMNhrF0tbmo3rApI0= X-Received: by 10.99.178.6 with SMTP id x6mr5306932pge.37.1500055472244; Fri, 14 Jul 2017 11:04:32 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:07 -0400 Message-Id: <1500055451-14041-15-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::241 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" 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 | 136 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ crypto/hash.c | 17 +++++++ crypto/hashpriv.h | 8 +++ 5 files changed, 163 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 d0941d4..f6550b5 100644 --- a/crypto/afalgpriv.h +++ b/crypto/afalgpriv.h @@ -24,6 +24,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..70ab414 --- /dev/null +++ b/crypto/hash-afalg.c @@ -0,0 +1,136 @@ +/* + * 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; + + 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_strdup_printf("%s", alg_name); + + 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; + } + + g_free(name); + + 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 expect_len =3D qcrypto_hash_digest_len(alg); + + if (*resultlen =3D=3D 0) { + *resultlen =3D expect_len; + *result =3D g_new0(uint8_t, *resultlen); + } else if (*resultlen !=3D expect_len) { + error_setg(errp, + "Result buffer size %zu is not match hash %d", + *resultlen, expect_len); + return -1; + } + + afalg =3D qcrypto_afalg_hash_ctx_new(alg, errp); + if (!afalg) { + 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; + ret =3D iov_send_recv(afalg->opfd, &outv, 1, + 0, iov_size(&outv, 1), false); + if (ret < 0) { + error_setg_errno(errp, errno, "Recv result from afalg-core failed"= ); + } else { + ret =3D 0; + } + +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..ac59c63 100644 --- a/crypto/hash.c +++ b/crypto/hash.c @@ -46,6 +46,23 @@ 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; + } + + /* + * TODO: + * Maybe we should treat some afalg errors as fatal + */ + error_free(*errp); +#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..cee26cc 100644 --- a/crypto/hashpriv.h +++ b/crypto/hashpriv.h @@ -28,4 +28,12 @@ struct QCryptoHashDriver { =20 extern QCryptoHashDriver qcrypto_hash_lib_driver; =20 +#ifdef CONFIG_AF_ALG + +#include "afalgpriv.h" + +extern QCryptoHashDriver qcrypto_hash_afalg_driver; + +#endif + #endif --=20 1.8.3.1 From nobody Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500056339309754.292437612429; Fri, 14 Jul 2017 11:18:59 -0700 (PDT) Received: from localhost ([::1]:39342 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW5B2-0001ro-0o for importer@patchew.org; Fri, 14 Jul 2017 14:18:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55397) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4xB-0005Qi-8Z for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4x8-0003Km-HO for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:37 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:35731) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4x8-0003K8-9V for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:34 -0400 Received: by mail-pg0-x241.google.com with SMTP id d193so11374380pgc.2 for ; Fri, 14 Jul 2017 11:04:34 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id y185sm17758527pgb.9.2017.07.14.11.04.32 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=IqChpPDAxMqZSZ/OXp/szN6Aqd/sjvGub/+s3znB5wc=; b=g8rlrwbVqCPjgYGZWHvRHdmec8t3e7kMJFAODR5rW9ZHL/NdMR/zdVyf2zxZQKBelV U/5n890SJj8RU4kkUKDFDk+ioeEMlv7JXWq6+mZjK28lYslAOWw6oWN+DFBcB9wxvVTp VIib3Ihe0FiFi5pUymvihUSLPMZzDaChPVSSPV0ufPlD57IFPer0/dp3YNQKRsLGO4XH wyRfwHtg+9MmOHh51OYc7Mov3Fe427P9jYshmRZL4aMo59EMhi01fukmfGYxXjRHP4xu vAe7Sg5moVugzRcrI/eMGQf9fmoNj2NrkBWLbyYel+q5iQB6ro0cRfAVmkATBYsqCW/+ uLUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=IqChpPDAxMqZSZ/OXp/szN6Aqd/sjvGub/+s3znB5wc=; b=tmKA7If2Zjcau16Dl11yMyRjx6IWUIzJH4sR7oZxoxlyCR/iRv5aP2+rV4C1afbduF AuChFY1y1nhRs6mHdadp9dvdCiw76KH6CiFdjkbpaEpswVMfZ04JyIJVR40TA/oIRtn0 TvltPHoKo5Trgl/wsyYiUV0U9HdDPYlWPMXNckEExeNv3dTtOKpluUw3pdmSoZ/VArkD 6TuH17vI6tyoU27oRr+lW6sBh9cI0agrTEuo/id3vzeE6N87IvF5Ja/rFl5VPxzaCLzr 1NzJuh+xe9uDNJrYl5TzLcgDJWLUEu0qlvh/SASENPCjFM4e6tuDOe3OlwMXQElmfgbs Cx1Q== X-Gm-Message-State: AIVw112ELd5VsJN666L/uh6oRKpDzt9Yngf1b1CLSUdw4vWIc4m+WHF7 XmsLPDJpyJVgtw== X-Received: by 10.98.70.206 with SMTP id o75mr6486923pfi.60.1500055473446; Fri, 14 Jul 2017 11:04:33 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:08 -0400 Message-Id: <1500055451-14041-16-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::241 Subject: [Qemu-devel] [PATCH v6 15/18] crypto: hmac: add af_alg-backend 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" 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 | 104 +++++++++++++++++++++++++++++++++++++++++++++---= ---- crypto/hmac.c | 22 +++++++++-- crypto/hmacpriv.h | 12 ++++++ 3 files changed, 121 insertions(+), 17 deletions(-) diff --git a/crypto/hash-afalg.c b/crypto/hash-afalg.c index 70ab414..cf34c69 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; @@ -53,18 +56,24 @@ qcrypto_afalg_hash_format_name(QCryptoHashAlgorithm alg, return NULL; } =20 - name =3D g_strdup_printf("%s", alg_name); + if (is_hmac) { + name =3D g_strdup_printf("hmac(%s)", alg_name); + } else { + name =3D g_strdup_printf("%s", alg_name); + } =20 return name; } =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; } @@ -77,19 +86,46 @@ qcrypto_afalg_hash_ctx_new(QCryptoHashAlgorithm alg, Er= ror **errp) =20 g_free(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; + } + } + 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 expect_len =3D qcrypto_hash_digest_len(alg); =20 if (*resultlen =3D=3D 0) { @@ -102,9 +138,13 @@ qcrypto_afalg_hash_bytesv(QCryptoHashAlgorithm alg, return -1; } =20 - afalg =3D qcrypto_afalg_hash_ctx_new(alg, errp); - if (!afalg) { - return -1; + if (is_hmac) { + afalg =3D hmac; + } else { + afalg =3D qcrypto_afalg_hash_ctx_new(alg, errp); + if (!afalg) { + return -1; + } } =20 /* send data to kernel's crypto core */ @@ -127,10 +167,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 a4690e3..37e2af2 100644 --- a/crypto/hmac.c +++ b/crypto/hmac.c @@ -89,17 +89,31 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, Error **errp) { QCryptoHmac *hmac; - void *ctx; + void *ctx =3D NULL; + 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; + } +#endif =20 - ctx =3D qcrypto_hmac_ctx_new(alg, key, nkey, errp); if (!ctx) { - return NULL; + ctx =3D qcrypto_hmac_ctx_new(alg, key, nkey, errp); + if (!ctx) { + return NULL; + } + + drv =3D &qcrypto_hmac_lib_driver; + error_free(err2); } =20 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; } diff --git a/crypto/hmacpriv.h b/crypto/hmacpriv.h index 2be389a..4387ca2 100644 --- a/crypto/hmacpriv.h +++ b/crypto/hmacpriv.h @@ -33,4 +33,16 @@ extern void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm a= lg, Error **errp); extern QCryptoHmacDriver qcrypto_hmac_lib_driver; =20 +#ifdef CONFIG_AF_ALG + +#include "afalgpriv.h" + +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 + #endif --=20 1.8.3.1 From nobody Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500056420777286.71097290373086; Fri, 14 Jul 2017 11:20:20 -0700 (PDT) Received: from localhost ([::1]:39351 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW5CN-0003nB-DX for importer@patchew.org; Fri, 14 Jul 2017 14:20:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55396) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4xB-0005Qg-8Q for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4x9-0003M7-Nh for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:37 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:33172) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4x9-0003LF-FV for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:35 -0400 Received: by mail-pf0-x243.google.com with SMTP id e199so11743221pfh.0 for ; Fri, 14 Jul 2017 11:04:35 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id t26sm21726232pfe.88.2017.07.14.11.04.34 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZfDKTPibyKEklujKREbwUVy8iB47XYMk/FQcb/qForc=; b=QxKUNn5NMDfb6nmjPw3CRsjMvICbBgg1S5WvXpb9NZ2kN1qqWIHzWfmmn6JJotk9dg 6TkDmA4OY3S8bWUCJ+S02kh6VDwmNU4ESbUpfRnYHYL5d8BPb4S8C81emGP2b57yRcI/ diMPV0DAOkSF06MjQPeDgkNXVZI3EgJyoDXbKYIQtvFyn5BjCvWw17+H34Ax2/2oWrWU u1yhdYpGOTXxLaXXN8iDlP+wfZ4dPVYIQvgSWeSGC/DtaWAlh7Cld4HA33L5ZGmftiox RiWtn5SGAJOdy/5PJyeqccS0rvOTIIXBNOw9rtmIR59D79JdnaXWwj25TT5OUN//pVYE ukPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZfDKTPibyKEklujKREbwUVy8iB47XYMk/FQcb/qForc=; b=VuxHFwr1R65feNDcse2um/LU6U5qjtvSsmR5ZBI7KCDIOVZfIcfMIXTFd3cv1rWyzd YYGPpANhrkQVjRllTEYOxHgueWugkqBeMMgoYOQyzJzvozn1ZrjMV3NNiNh06FKAUcRc 1L1F6LWIGns5q9ZBZf2nvktlf4cqNAQPv0VHS2oGJYcYgImOztZ8OA0ZOe/pdYgEXyyr jvCGYRgHmnp0nRZBATdZ+YkAclj2FCoPAsUMlmxzzrZO+ss9tRW6+OEcHByW4UKkN3f2 eGvMZet94uPf42+5SyVyKzXg0dy5eV7ZazQoPMSudtjqAlcS+dKpFCT+Z1uE2imsnyp5 vTsg== X-Gm-Message-State: AIVw113VjBzJ63s7vfV5DGhQRT2CtEytU07xM5jiPnDkF5Oe3dLDlybs PKmVTE3KF+wpkQ== X-Received: by 10.84.169.4 with SMTP id g4mr17755125plb.94.1500055474642; Fri, 14 Jul 2017 11:04:34 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:09 -0400 Message-Id: <1500055451-14041-17-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" 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 | 88 +++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 tests/benchmark-crypto-cipher.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 42e17e2..2f34c60 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -99,6 +99,7 @@ gcov-files-test-write-threshold-y =3D block/write-thresho= ld.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) @@ -532,6 +533,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 @@ -635,6 +637,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 @@ -803,6 +806,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" @@ -833,8 +837,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))} \ @@ -893,6 +897,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..c6a4092 --- /dev/null +++ b/tests/benchmark-crypto-cipher.c @@ -0,0 +1,88 @@ +/* + * 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("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 Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500056540556211.83723592968045; Fri, 14 Jul 2017 11:22:20 -0700 (PDT) Received: from localhost ([::1]:39361 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW5EJ-0005A4-4C for importer@patchew.org; Fri, 14 Jul 2017 14:22:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55407) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4xB-0005RT-Vi for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4xA-0003Mf-SC for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:37 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:35528) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4xA-0003MM-M8 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:36 -0400 Received: by mail-pf0-x242.google.com with SMTP id q85so11728537pfq.2 for ; Fri, 14 Jul 2017 11:04:36 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id d70sm20084326pfg.132.2017.07.14.11.04.35 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=X5byj+IoNv3TUKzxaIMkeIP4J98w9WGuH6jKXJfXuMU=; b=tSGwgIdO6ljhueaZuYsfVBUDCNPTnhBKGZcmGHNnA/asiNONCSeQknWEI9ym+J9Qbu 0LdDWVvfk5CvcH0OqD9BYPeg69VKGx/vIaKEy8zn+3V/wokqE/0k06kp9fA+So3BxvHP ZG5d9UoklOusYfuV2KPn0/m5E4vbFuLK728IaBA24DUsh4+mC7HhHk5ZRgZ3xrHhL1H1 tFPDD4bSjBHfmyWcD5WhhF9HcI5zPtZJK0RGV3yE75Hmu4EuRhuWZAXQBuGTdc0Izrya 26I+AoX+5La9T4lr7V3wC4P4SngOT+8cYMhADduOvXJBhfJBuUg/n8umb30BC4JTuspf 2tZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=X5byj+IoNv3TUKzxaIMkeIP4J98w9WGuH6jKXJfXuMU=; b=WRrwbNiJx1Qz1zr/dw5j/ipb7kQTs6olScOnKzxall06O2NmVWDZeR/rluT9MheZPS H5mQkqdrVpA5chYcCdGiF+yyuM+c4ADFq54B/zt5lZ2D7ttxydFi5QqmPSjKRNj+IX3H nclZQJc2MrsWgpVCUGJITnpDeQyEDxSjjUmbDuvEKJhWlmwqJIymPta87dytFgYxrYo4 snYT8YFuGQVBvQh1WdKlKyFBJzEy83sopvaEK/oGsjvBK2Yjz9Utp545IWBbKekMVayQ 6YXPIjxdO4a8OA3Sf50FGK5iVfL5TWF2ZYiLVxfIy61tArMzy1TBsmdc7EErsH0uU7+S bSrQ== X-Gm-Message-State: AIVw1132Bs3ZJLSmKV6S87QcXNTSpBaQC4l/3FkyNY1KUMsPzYwa//9s BP84K8pSqOktYg== X-Received: by 10.98.111.194 with SMTP id k185mr6418961pfc.235.1500055475837; Fri, 14 Jul 2017 11:04:35 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:10 -0400 Message-Id: <1500055451-14041-18-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" This patch add a hash speed benchmark, it helps us to measure the performance by using "make check-speed" or using "./tests/benchmark-crypto-hash" 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 2f34c60..c5a67a3 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -97,6 +97,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) @@ -635,6 +636,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 Sat Apr 27 10:58:04 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.zohomail.com; dkim=fail; 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 1500056046144724.3415135064882; Fri, 14 Jul 2017 11:14:06 -0700 (PDT) Received: from localhost ([::1]:39312 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW56I-0005ju-Mz for importer@patchew.org; Fri, 14 Jul 2017 14:14:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55434) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4xD-0005Tk-8V for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4xC-0003NF-13 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:39 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:36688) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4xB-0003N1-RB for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:37 -0400 Received: by mail-pg0-x241.google.com with SMTP id y129so11394972pgy.3 for ; Fri, 14 Jul 2017 11:04:37 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id a71sm20427781pfl.129.2017.07.14.11.04.36 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=KH+WmtkojMWa4mPuiAOJNqpbw+fR9dm5KLbXEYNbBrI=; b=DBcCHVbgtRUHn6w/6gJYdtLNTZqpRFbOep2aVQxx5G9/TSMeuPoXDztt8mV+qk41Ef lSyaGwXRI+OQ6S5zqaYv1kSzcp0RB5VR+1f9tobl++485dzjwMGFS2Fc6nGlljpBeZH2 1i3yPRJsEa1refYGaRqOTpqqUybpNTaGdqnoKKF4RkbCA7DZppQ/oP/JE0RQmnG5PMFf bY89ExyRzTyAPWOXsZ4iWM+fC+cDummQ0kL3IVmlcXNgoxEEBcCsZ423mn/OyzO3//VD MPJglHYOy46cO7WXjkdPbpYlDLbCiv0aQrcWMavf45RNMTwaIz3vQZHv9Ti2gkzFix7h XwsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=KH+WmtkojMWa4mPuiAOJNqpbw+fR9dm5KLbXEYNbBrI=; b=YDrl8XrkuaTI0KNntiFKnphe9M4w8GyhOh2QJnqefw/xZYO/s++2dZ3Xx9ZwNaFzSo klAVkhdIbBUdzm3hPKt5u35lTNp35ji1iqgNiDaA3hPuym8FfPbcpKtKBbSRvNYNYuGJ TlHJEFLtSDoafo60M8bQQQMWoh/L0L/HlReTH+QG2L8cIds8cGuaqubi4a20XimkDr8x iKYxFqcvZpLoTVihKYBSZ382Ire5ArqXBDGAcbUm1zhfwH0+hegiub0eN9OTq1Ewmfq+ 4AB1bZcc9WHHF1PQn9L7uzWYwWlnS3iWxuf4kiDOj83dzJw1CXZGS6rc4AlpVepR+ffo 9kZg== X-Gm-Message-State: AIVw112vB71Nsr6STgvl2dJUC4WninLv55WdGx0zWWm1QSOf2eobl6OZ 0H5dC65OnYUZFw== X-Received: by 10.99.100.5 with SMTP id y5mr16185030pgb.92.1500055477040; Fri, 14 Jul 2017 11:04:37 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:11 -0400 Message-Id: <1500055451-14041-19-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::241 Subject: [Qemu-devel] [PATCH v6 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" 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 | 82 +++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 84 insertions(+) create mode 100644 tests/benchmark-crypto-hmac.c diff --git a/tests/Makefile.include b/tests/Makefile.include index c5a67a3..18f9359 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -99,6 +99,7 @@ gcov-files-test-write-threshold-y =3D block/write-thresho= ld.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) @@ -638,6 +639,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..72408be --- /dev/null +++ b/tests/benchmark-crypto-hmac.c @@ -0,0 +1,82 @@ +/* + * 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; + } + + 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 { + 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); + + ret =3D qcrypto_hmac_bytesv(hmac, &iov, 1, &out, &out_len, &err); + g_assert(ret =3D=3D 0); + g_assert(err =3D=3D NULL); + + qcrypto_hmac_free(hmac); + + total +=3D chunk_size; + } while (g_test_timer_elapsed() < 5.0); + + total /=3D 1024 * 1024; /* to MB */ + + 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()); + + 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