From nobody Sat Feb 7 07:24:42 2026 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; 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 1500456200468949.0380397261688; Wed, 19 Jul 2017 02:23:20 -0700 (PDT) Received: from localhost ([::1]:60709 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dXlCR-0006Mr-0J for importer@patchew.org; Wed, 19 Jul 2017 05:23:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38460) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dXl5k-0000ad-3J for qemu-devel@nongnu.org; Wed, 19 Jul 2017 05:16:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dXl5i-0001Jy-Di for qemu-devel@nongnu.org; Wed, 19 Jul 2017 05:16:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39772) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dXl5i-0001JC-3l for qemu-devel@nongnu.org; Wed, 19 Jul 2017 05:16:22 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 274DE8008D; Wed, 19 Jul 2017 09:16:21 +0000 (UTC) Received: from t460.redhat.com (ovpn-117-51.ams2.redhat.com [10.36.117.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id 72AE47E135; Wed, 19 Jul 2017 09:16:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 274DE8008D Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=berrange@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 274DE8008D From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Wed, 19 Jul 2017 10:15:47 +0100 Message-Id: <20170719091600.8555-6-berrange@redhat.com> In-Reply-To: <20170719091600.8555-1-berrange@redhat.com> References: <20170719091600.8555-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 19 Jul 2017 09:16:21 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL v2 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: Peter Maydell , "Longpeng\(Mike\)" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable 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: Gonglei Signed-off-by: Longpeng(Mike) Signed-off-by: Daniel P. Berrange --- crypto/cipher-builtin.c | 64 +++++++++++++++++-------------------------- crypto/cipher-gcrypt.c | 72 +++++++++++++++++++++------------------------= ---- crypto/cipher-nettle.c | 71 ++++++++++++++++++++-------------------------= --- crypto/cipher.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ crypto/cipherpriv.h | 40 +++++++++++++++++++++++++++ include/crypto/cipher.h | 1 + 6 files changed, 190 insertions(+), 123 deletions(-) create mode 100644 crypto/cipherpriv.h diff --git a/crypto/cipher-builtin.c b/crypto/cipher-builtin.c index 4ecd15eb9e..16a36d4532 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 493938629e..0489147398 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 e6d6e6cb69..c51f119cbc 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 5a9648942f..0a3d2e5b1d 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 0000000000..4af5e85436 --- /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 bec9f412b0..984fb8243f 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 2.13.0