[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v2 for-2.10 02/18] crypto: cipher: introduce qcr
From: |
Gonglei (Arei) |
Subject: |
Re: [Qemu-devel] [PATCH v2 for-2.10 02/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for gcrypt-backend |
Date: |
Fri, 21 Apr 2017 11:56:02 +0000 |
> -----Original Message-----
> From: longpeng
> Sent: Monday, April 17, 2017 9:33 AM
> To: address@hidden
> Cc: Gonglei (Arei); Huangweidong (C); address@hidden;
> address@hidden; address@hidden; address@hidden; longpeng
> Subject: [PATCH v2 for-2.10 02/18] crypto: cipher: introduce
> qcrypto_cipher_ctx_new for gcrypt-backend
>
> Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for
> gcrypt-backend impls.
>
> Signed-off-by: Longpeng(Mike) <address@hidden>
> ---
> crypto/cipher-gcrypt.c | 50
> +++++++++++++++++++++++++++++++++-----------------
> 1 file changed, 33 insertions(+), 17 deletions(-)
>
Reviewed-by: Gonglei <address@hidden>
> diff --git a/crypto/cipher-gcrypt.c b/crypto/cipher-gcrypt.c
> index 0ecffa2..871730b 100644
> --- a/crypto/cipher-gcrypt.c
> +++ b/crypto/cipher-gcrypt.c
> @@ -80,12 +80,12 @@ static void
> gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx,
> }
>
>
> -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(QCryptoCipherAlgorithm alg,
> return NULL;
> }
>
> - cipher = g_new0(QCryptoCipher, 1);
> - cipher->alg = alg;
> - cipher->mode = mode;
> -
> ctx = g_new0(QCryptoCipherGcrypt, 1);
>
> err = gcry_cipher_open(&ctx->handle, gcryalg, gcrymode, 0);
> @@ -174,7 +170,7 @@ QCryptoCipher
> *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
> gcry_strerror(err));
> goto error;
> }
> - if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
> + if (mode == QCRYPTO_CIPHER_MODE_XTS) {
> err = gcry_cipher_open(&ctx->tweakhandle, gcryalg, gcrymode, 0);
> if (err != 0) {
> error_setg(errp, "Cannot initialize cipher: %s",
> @@ -183,7 +179,7 @@ QCryptoCipher
> *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
> }
> }
>
> - if (cipher->alg == QCRYPTO_CIPHER_ALG_DES_RFB) {
> + if (alg == 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(QCryptoCipherAlgorithm alg,
> g_free(rfbkey);
> ctx->blocksize = 8;
> } else {
> - if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
> + if (mode == QCRYPTO_CIPHER_MODE_XTS) {
> nkey /= 2;
> err = gcry_cipher_setkey(ctx->handle, key, nkey);
> if (err != 0) {
> @@ -210,7 +206,7 @@ QCryptoCipher
> *qcrypto_cipher_new(QCryptoCipherAlgorithm 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(QCryptoCipherAlgorithm alg,
> }
> }
>
> - if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
> + if (mode == QCRYPTO_CIPHER_MODE_XTS) {
> if (ctx->blocksize != XTS_BLOCK_SIZE) {
> error_setg(errp,
> "Cipher block size %zu must equal XTS block
> size %d",
> @@ -240,12 +236,10 @@ QCryptoCipher
> *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
> ctx->iv = g_new0(uint8_t, ctx->blocksize);
> }
>
> - cipher->opaque = ctx;
> - return cipher;
> + return ctx;
>
> error:
> gcrypt_cipher_free_ctx(ctx, mode);
> - g_free(cipher);
> return NULL;
> }
>
> @@ -385,3 +379,25 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,
>
> return 0;
> }
> +
> +
> +QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
> + QCryptoCipherMode mode,
> + const uint8_t *key, size_t nkey,
> + Error **errp)
> +{
> + QCryptoCipher *cipher;
> + QCryptoCipherGcrypt *ctx;
> +
> + ctx = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
> + if (ctx == NULL) {
> + return NULL;
> + }
> +
> + cipher = g_new0(QCryptoCipher, 1);
> + cipher->alg = alg;
> + cipher->mode = mode;
> + cipher->opaque = ctx;
> +
> + return cipher;
> +}
> --
> 1.8.3.1
>
- [Qemu-devel] [PATCH v2 for-2.10 04/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for builtin-backend, (continued)
- [Qemu-devel] [PATCH v2 for-2.10 04/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for builtin-backend, Longpeng(Mike), 2017/04/16
- [Qemu-devel] [PATCH v2 for-2.10 01/18] crypto: cipher: introduce context free function, Longpeng(Mike), 2017/04/16
- [Qemu-devel] [PATCH v2 for-2.10 08/18] crypto: hmac: introduce qcrypto_hmac_ctx_new for gcrypt-backend, Longpeng(Mike), 2017/04/16
- [Qemu-devel] [PATCH v2 for-2.10 10/18] crypto: hmac: introduce qcrypto_hmac_ctx_new for glib-backend, Longpeng(Mike), 2017/04/16
- [Qemu-devel] [PATCH v2 for-2.10 18/18] tests: crypto: add hmac speed benchmark support, Longpeng(Mike), 2017/04/16
- [Qemu-devel] [PATCH v2 for-2.10 02/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for gcrypt-backend, Longpeng(Mike), 2017/04/16
- Re: [Qemu-devel] [PATCH v2 for-2.10 02/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for gcrypt-backend,
Gonglei (Arei) <=
- [Qemu-devel] [PATCH v2 for-2.10 12/18] crypto: introduce some common functions for af_alg backend, Longpeng(Mike), 2017/04/16
- [Qemu-devel] [PATCH v2 for-2.10 03/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for nettle-backend, Longpeng(Mike), 2017/04/16
- [Qemu-devel] [PATCH v2 for-2.10 06/18] crypto: hash: add hash driver framework, Longpeng(Mike), 2017/04/16
- [Qemu-devel] [PATCH v2 for-2.10 09/18] crypto: hmac: introduce qcrypto_hmac_ctx_new for nettle-backend, Longpeng(Mike), 2017/04/16
- [Qemu-devel] [PATCH v2 for-2.10 17/18] tests: crypto: add hash speed benchmark support, Longpeng(Mike), 2017/04/16