Merge qcrypto 2016/12/21
The following changes since commit 82ecffa8c050bf5bbc13329e9b65eac1caa5b55c:
Open 2.9 development tree (2016-12-20 16:20:16 +0000)
are available in the git repository at:
git://github.com/berrange/qemu tags/pull-qcrypto-2016-12-21-1
for you to fetch changes up to d1c7d71dcec794de02838cbb96a6f99fee13d8a5:
crypto: add HMAC algorithms testcases (2016-12-21 14:26:26 +0000)
----------------------------------------------------------------
Merge qcrypto 2016/12/21 v1
----------------------------------------------------------------
Longpeng(Mike) (6):
crypto: add 3des-ede support when using libgcrypt/nettle
crypto: add HMAC algorithms framework
crypto: support HMAC algorithms based on libgcrypt
crypto: support HMAC algorithms based on glib
crypto: support HMAC algorithms based on nettle
crypto: add HMAC algorithms testcases
Marc-André Lureau (1):
cipher: fix leak on initialization error
crypto/Makefile.objs | 4 + crypto/cipher-gcrypt.c | 6 + crypto/cipher-nettle.c | 42 ++++++- crypto/cipher.c | 7 +- crypto/hmac-gcrypt.c | 152 ++++++++++++++++++++++++++ crypto/hmac-glib.c | 166 ++++++++++++++++++++++++++++ crypto/hmac-nettle.c | 175 +++++++++++++++++++++++++++++ crypto/hmac.c | 72 ++++++++++++ crypto/hmac.h | 166 ++++++++++++++++++++++++++++ qapi/crypto.json | 3 +- tests/Makefile.include | 2 + tests/test-crypto-cipher.c | 119 ++++++++++++++++++++ tests/test-crypto-hmac.c | 266 +++++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 1174 insertions(+), 6 deletions(-) create mode 100644 crypto/hmac-gcrypt.c create mode 100644 crypto/hmac-glib.c create mode 100644 crypto/hmac-nettle.c create mode 100644 crypto/hmac.c create mode 100644 crypto/hmac.h create mode 100644 tests/test-crypto-hmac.c
Signature · 1 line
2.9.3
[Qemu-devel] [PULL v1 3/7] crypto: add HMAC algorithms framework
From: "Longpeng(Mike)" <[email protected]>
This patch introduce HMAC algorithms framework.
--- crypto/Makefile.objs | 4 ++ crypto/hmac-gcrypt.c | 45 ++++++++++++++ crypto/hmac-glib.c | 44 ++++++++++++++ crypto/hmac-nettle.c | 45 ++++++++++++++ crypto/hmac.c | 72 ++++++++++++++++++++++ crypto/hmac.h | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 376 insertions(+) create mode 100644 crypto/hmac-gcrypt.c create mode 100644 crypto/hmac-glib.c create mode 100644 crypto/hmac-nettle.c create mode 100644 crypto/hmac.c create mode 100644 crypto/hmac.h
crypto/Makefile.objs +4 -0
@@ -3,6 +3,10 @@ crypto-obj-y += hash.o crypto-obj-$(CONFIG_NETTLE) += hash-nettle.o crypto-obj-$(if $(CONFIG_NETTLE),n,$(CONFIG_GCRYPT)) += hash-gcrypt.o crypto-obj-$(if $(CONFIG_NETTLE),n,$(if $(CONFIG_GCRYPT),n,y)) += hash-glib.o +crypto-obj-y += hmac.o +crypto-obj-$(CONFIG_NETTLE) += hmac-nettle.o +crypto-obj-$(CONFIG_GCRYPT_HMAC) += hmac-gcrypt.o +crypto-obj-$(if $(CONFIG_NETTLE),n,$(if $(CONFIG_GCRYPT_HMAC),n,y)) += hmac-glib.o crypto-obj-y += aes.o crypto-obj-y += desrfb.o crypto-obj-y += cipher.o
crypto/hmac-gcrypt.c new file +45 -0
45 lines not on this page · raw message
crypto/hmac-glib.c new file +44 -0
44 lines not on this page · raw message
crypto/hmac-nettle.c new file +45 -0
45 lines not on this page · raw message
crypto/hmac.c new file +72 -0
72 lines not on this page · raw message
crypto/hmac.h new file +166 -0
166 lines not on this page · raw message
Signature · 1 line
2.9.3
Re: [Qemu-devel] [PULL v1 3/7] crypto: add HMAC algorithms framework
Hi Daniel,
Can you pick this patch together ?
[PATCH for-2.9 v4 1/6] configure: add CONFIG_GCRYPT_HMAC item
Or it will be picked by somebody else ?
:)
Quoted 7 lines · Daniel P. Berrange
From: "Longpeng(Mike)" <[email protected]>
This patch introduce HMAC algorithms framework.
Signed-off-by Longpeng(Mike) <[email protected]>Signed-off-by Daniel P. Berrange <[email protected]>---
......
Quoted 2 lines
crypto/hmac.h new file +2
-0+ +#endif
Signature · 2 lines
Regards, Longpeng(Mike)
Re: [Qemu-devel] [PULL v1 3/7] crypto: add HMAC algorithms framework
Quoted 4 lines · Longpeng (Mike)
Hi Daniel,
Can you pick this patch together ?
[PATCH for-2.9 v4 1/6] configure: addCONFIG_GCRYPT_HMACitem
Sigh yes. I don't know how i screwed up and missed that patch.
Will send a v2 pull request.
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
[Qemu-devel] [PULL v1 5/7] crypto: support HMAC algorithms based on glib
In reply to Daniel P. BerrangeFrom: "Longpeng(Mike)" <[email protected]>
This patch add glib-backed HMAC algorithms support
--- crypto/hmac-glib.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+)
crypto/hmac-glib.c +122 -0
@@ -16,6 +16,126 @@#include "qapi/error.h" #include "crypto/hmac.h" +/* Support for HMAC Algos has been added in GLib 2.30 */ +#if GLIB_CHECK_VERSION (2 ,30 ,0 ) + +static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX ] = { + [QCRYPTO_HASH_ALG_MD5 ] =G_CHECKSUM_MD5 , + [QCRYPTO_HASH_ALG_SHA1 ] =G_CHECKSUM_SHA1 , + [QCRYPTO_HASH_ALG_SHA256 ] =G_CHECKSUM_SHA256 , +/* Support for HMAC SHA-512 in GLib 2.42 */ +#if GLIB_CHECK_VERSION(2, 42, 0) + [QCRYPTO_HASH_ALG_SHA512 ] =G_CHECKSUM_SHA512 , +#else + [QCRYPTO_HASH_ALG_SHA512 ] =-1 , +#endif + [QCRYPTO_HASH_ALG_SHA224 ] =-1 , + [QCRYPTO_HASH_ALG_SHA384 ] =-1 , + [QCRYPTO_HASH_ALG_RIPEMD160 ] =-1 , +}; + +typedef struct QCryptoHmacGlib QCryptoHmacGlib ; +struct QCryptoHmacGlib { +GHmac *ghmac; +}; + +bool qcrypto_hmac_supports (QCryptoHashAlgorithm alg) +{ +if (alg <G_N_ELEMENTS (qcrypto_hmac_alg_map) && + qcrypto_hmac_alg_map[alg] !=-1 ) { +return true; + } + +return false; +} + +QCryptoHmac *qcrypto_hmac_new (QCryptoHashAlgorithm alg, +const uint8_t *key,size_t nkey, +Error **errp) +{ +QCryptoHmac *hmac; +QCryptoHmacGlib *ctx; + +if (!qcrypto_hmac_supports (alg)) { +error_setg (errp,"Unsupported hmac algorithm %s" , + QCryptoHashAlgorithm_lookup[alg]); +return NULL ; + } + + hmac =g_new0 (QCryptoHmac,1 ); + hmac->alg = alg; + + ctx =g_new0 (QCryptoHmacGlib,1 ); + + ctx->ghmac =g_hmac_new (qcrypto_hmac_alg_map[alg], + (const uint8_t *)key, nkey); +if (!ctx->ghmac) { +error_setg (errp,"Cannot initialize hmac and set key" ); + goto error; + } + + hmac->opaque = ctx; +return hmac; + +error: +g_free (ctx); +g_free (hmac); +return NULL ; +} + +void qcrypto_hmac_free (QCryptoHmac *hmac) +{ +QCryptoHmacGlib *ctx; + +if (!hmac) { +return ; + } + + ctx = hmac->opaque; +g_hmac_unref (ctx->ghmac); + +g_free (ctx); +g_free (hmac); +} + +int qcrypto_hmac_bytesv (QCryptoHmac *hmac, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +QCryptoHmacGlib *ctx; +int i, ret; + + ctx = hmac->opaque; + +for (i =0 ; i < niov; i++) { +g_hmac_update (ctx->ghmac, iov[i].iov_base, iov[i].iov_len); + } + + ret =g_checksum_type_get_length (qcrypto_hmac_alg_map[hmac->alg]); +if (ret <0 ) { +error_setg (errp,"Unable to get hmac length" ); +return -1 ; + } + +if (*resultlen ==0 ) { + *resultlen = ret; + *result =g_new0 (uint8_t, *resultlen); + }else if (*resultlen != ret) { +error_setg (errp,"Result buffer size %zu is smaller than hmac %d" , + *resultlen, ret); +return -1 ; + } + +g_hmac_get_digest (ctx->ghmac, *result, resultlen); + +return 0 ; +} + +#else +bool qcrypto_hmac_supports (QCryptoHashAlgorithm alg) {return false;
@@ -42,3 +162,5 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac, {return -1 ; } + +#endif
Signature · 1 line
2.9.3
[Qemu-devel] [PULL v1 1/7] cipher: fix leak on initialization error
In reply to Daniel P. BerrangeFrom: Marc-André Lureau <[email protected]>
On error path, ctx may be leaked. Assign ctx earlier, and call qcrypto_cipher_free() on error.
Spotted thanks to ASAN.
--- crypto/cipher-nettle.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
crypto/cipher-nettle.c +2 -3
@@ -254,6 +254,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, cipher->mode = mode; ctx =g_new0 (QCryptoCipherNettle,1 ); + cipher->opaque = ctx;switch (alg) {case QCRYPTO_CIPHER_ALG_DES_RFB :
@@ -384,13 +385,11 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, } ctx->iv =g_new0 (uint8_t, ctx->blocksize); - cipher->opaque = ctx;return cipher; error: -g_free (cipher); -g_free (ctx); +qcrypto_cipher_free (cipher);return NULL ; }
Signature · 1 line
2.9.3
[Qemu-devel] [PULL v1 6/7] crypto: support HMAC algorithms based on nettle
In reply to Daniel P. BerrangeFrom: "Longpeng(Mike)" <[email protected]>
This patch add nettle-backed HMAC algorithms support
--- crypto/hmac-nettle.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 133 insertions(+), 3 deletions(-)
crypto/hmac-nettle.c +133 -3
@@ -17,8 +17,83 @@#include "crypto/hmac.h" #include <nettle/hmac.h> +typedef void (*qcrypto_nettle_hmac_setkey )(void *ctx, +size_t key_length,const uint8_t *key); + +typedef void (*qcrypto_nettle_hmac_update )(void *ctx, +size_t length,const uint8_t *data); + +typedef void (*qcrypto_nettle_hmac_digest )(void *ctx, +size_t length,uint8_t *digest); + +typedef struct QCryptoHmacNettle QCryptoHmacNettle ; +struct QCryptoHmacNettle { +union qcrypto_nettle_hmac_ctx { +struct hmac_md5_ctx md5_ctx; +struct hmac_sha1_ctx sha1_ctx; +struct hmac_sha256_ctx sha256_ctx;/* equals hmac_sha224_ctx */ +struct hmac_sha512_ctx sha512_ctx;/* equals hmac_sha384_ctx */ +struct hmac_ripemd160_ctx ripemd160_ctx; + } u; +}; + +struct qcrypto_nettle_hmac_alg { +qcrypto_nettle_hmac_setkey setkey; +qcrypto_nettle_hmac_update update; +qcrypto_nettle_hmac_digest digest; +size_t len; +} qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX ] = { + [QCRYPTO_HASH_ALG_MD5 ] = { + .setkey = (qcrypto_nettle_hmac_setkey )hmac_md5_set_key, + .update = (qcrypto_nettle_hmac_update )hmac_md5_update, + .digest = (qcrypto_nettle_hmac_digest )hmac_md5_digest, + .len =MD5_DIGEST_SIZE , + }, + [QCRYPTO_HASH_ALG_SHA1 ] = { + .setkey = (qcrypto_nettle_hmac_setkey )hmac_sha1_set_key, + .update = (qcrypto_nettle_hmac_update )hmac_sha1_update, + .digest = (qcrypto_nettle_hmac_digest )hmac_sha1_digest, + .len =SHA1_DIGEST_SIZE , + }, + [QCRYPTO_HASH_ALG_SHA224 ] = { + .setkey = (qcrypto_nettle_hmac_setkey )hmac_sha224_set_key, + .update = (qcrypto_nettle_hmac_update )hmac_sha224_update, + .digest = (qcrypto_nettle_hmac_digest )hmac_sha224_digest, + .len =SHA224_DIGEST_SIZE , + }, + [QCRYPTO_HASH_ALG_SHA256 ] = { + .setkey = (qcrypto_nettle_hmac_setkey )hmac_sha256_set_key, + .update = (qcrypto_nettle_hmac_update )hmac_sha256_update, + .digest = (qcrypto_nettle_hmac_digest )hmac_sha256_digest, + .len =SHA256_DIGEST_SIZE , + }, + [QCRYPTO_HASH_ALG_SHA384 ] = { + .setkey = (qcrypto_nettle_hmac_setkey )hmac_sha384_set_key, + .update = (qcrypto_nettle_hmac_update )hmac_sha384_update, + .digest = (qcrypto_nettle_hmac_digest )hmac_sha384_digest, + .len =SHA384_DIGEST_SIZE , + }, + [QCRYPTO_HASH_ALG_SHA512 ] = { + .setkey = (qcrypto_nettle_hmac_setkey )hmac_sha512_set_key, + .update = (qcrypto_nettle_hmac_update )hmac_sha512_update, + .digest = (qcrypto_nettle_hmac_digest )hmac_sha512_digest, + .len =SHA512_DIGEST_SIZE , + }, + [QCRYPTO_HASH_ALG_RIPEMD160 ] = { + .setkey = (qcrypto_nettle_hmac_setkey )hmac_ripemd160_set_key, + .update = (qcrypto_nettle_hmac_update )hmac_ripemd160_update, + .digest = (qcrypto_nettle_hmac_digest )hmac_ripemd160_digest, + .len =RIPEMD160_DIGEST_SIZE , + }, +}; +bool qcrypto_hmac_supports (QCryptoHashAlgorithm alg) { +if (alg <G_N_ELEMENTS (qcrypto_hmac_alg_map) && + qcrypto_hmac_alg_map[alg].setkey !=NULL ) { +return true; + } +return false; }
@@ -26,12 +101,39 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, const uint8_t *key, size_t nkey, Error **errp) { - return NULL; + QCryptoHmac *hmac; +QCryptoHmacNettle *ctx; + +if (!qcrypto_hmac_supports (alg)) { +error_setg (errp,"Unsupported hmac algorithm %s" , + QCryptoHashAlgorithm_lookup[alg]); +return NULL ; + } + + hmac =g_new0 (QCryptoHmac,1 ); + hmac->alg = alg; + + ctx =g_new0 (QCryptoHmacNettle,1 ); + + qcrypto_hmac_alg_map[alg].setkey (&ctx->u, nkey, key); + + hmac->opaque = ctx; + +return hmac; }void qcrypto_hmac_free (QCryptoHmac *hmac) { -return ; +QCryptoHmacNettle *ctx; + +if (!hmac) { +return ; + } + + ctx = hmac->opaque; + +g_free (ctx); +g_free (hmac); } int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
@@ -41,5 +143,33 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac,size_t *resultlen, Error **errp) { -return -1 ; + QCryptoHmacNettle *ctx; +int i; + + ctx = (QCryptoHmacNettle *)hmac->opaque; + +for (i =0 ; i < niov; ++i) { +size_t len = iov[i].iov_len; +uint8_t *base = iov[i].iov_base; +while (len) { +size_t shortlen =MIN (len,UINT_MAX ); + qcrypto_hmac_alg_map[hmac->alg].update (&ctx->u, len, base); + len -= shortlen; + base += len; + } + } + +if (*resultlen ==0 ) { + *resultlen = qcrypto_hmac_alg_map[hmac->alg].len; + *result =g_new0 (uint8_t, *resultlen); + }else if (*resultlen != qcrypto_hmac_alg_map[hmac->alg].len) { +error_setg (errp, +"Result buffer size %zu is smaller than hash %zu" , + *resultlen, qcrypto_hmac_alg_map[hmac->alg].len); +return -1 ; + } + + qcrypto_hmac_alg_map[hmac->alg].digest (&ctx->u, *resultlen, *result); + +return 0 ; }
Signature · 1 line
2.9.3
[Qemu-devel] [PULL v1 2/7] crypto: add 3des-ede support when using libgcrypt/nettle
In reply to Daniel P. BerrangeFrom: "Longpeng(Mike)" <[email protected]>
Libgcrypt and nettle support 3des-ede, so this patch add 3des-ede support when using libgcrypt or nettle.
--- crypto/cipher-gcrypt.c | 6 +++ crypto/cipher-nettle.c | 37 ++++++++++++++ crypto/cipher.c | 7 ++- qapi/crypto.json | 3 +- tests/test-crypto-cipher.c | 119 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+), 3 deletions(-)
crypto/cipher-gcrypt.c +6 -0
@@ -29,6 +29,7 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, {switch (alg) {case QCRYPTO_CIPHER_ALG_DES_RFB : +case QCRYPTO_CIPHER_ALG_3DES :case QCRYPTO_CIPHER_ALG_AES_128 :case QCRYPTO_CIPHER_ALG_AES_192 :case QCRYPTO_CIPHER_ALG_AES_256 :
@@ -99,6 +100,10 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, gcryalg =GCRY_CIPHER_DES ;break ; +case QCRYPTO_CIPHER_ALG_3DES : + gcryalg =GCRY_CIPHER_3DES ; +break ; +case QCRYPTO_CIPHER_ALG_AES_128 : gcryalg =GCRY_CIPHER_AES128 ;break ;
@@ -200,6 +205,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,case QCRYPTO_CIPHER_ALG_TWOFISH_256 : ctx->blocksize =16 ;break ; +case QCRYPTO_CIPHER_ALG_3DES :case QCRYPTO_CIPHER_ALG_CAST5_128 : ctx->blocksize =8 ;break ;
crypto/cipher-nettle.c +37 -0
@@ -78,6 +78,18 @@ static void des_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,des_decrypt (ctx, length, dst, src); } +static void des3_encrypt_native (cipher_ctx_t ctx,cipher_length_t length, +uint8_t *dst,const uint8_t *src) +{ +des3_encrypt (ctx, length, dst, src); +} + +static void des3_decrypt_native (cipher_ctx_t ctx,cipher_length_t length, +uint8_t *dst,const uint8_t *src) +{ +des3_decrypt (ctx, length, dst, src); +} +static void cast128_encrypt_native (cipher_ctx_t ctx,cipher_length_t length,uint8_t *dst,const uint8_t *src) {
@@ -140,6 +152,18 @@ static void des_decrypt_wrapper(const void *ctx, size_t length,des_decrypt (ctx, length, dst, src); } +static void des3_encrypt_wrapper (const void *ctx,size_t length, +uint8_t *dst,const uint8_t *src) +{ +des3_encrypt (ctx, length, dst, src); +} + +static void des3_decrypt_wrapper (const void *ctx,size_t length, +uint8_t *dst,const uint8_t *src) +{ +des3_decrypt (ctx, length, dst, src); +} +static void cast128_encrypt_wrapper (const void *ctx,size_t length,uint8_t *dst,const uint8_t *src) {
@@ -197,6 +221,7 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, {switch (alg) {case QCRYPTO_CIPHER_ALG_DES_RFB : +case QCRYPTO_CIPHER_ALG_3DES :case QCRYPTO_CIPHER_ALG_AES_128 :case QCRYPTO_CIPHER_ALG_AES_192 :case QCRYPTO_CIPHER_ALG_AES_256 :
@@ -271,6 +296,18 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, ctx->blocksize =DES_BLOCK_SIZE ;break ; +case QCRYPTO_CIPHER_ALG_3DES : + ctx->ctx = g_new0(struct des3_ctx, 1); +des3_set_key (ctx->ctx, key); + + ctx->alg_encrypt_native = des3_encrypt_native; + ctx->alg_decrypt_native = des3_decrypt_native; + ctx->alg_encrypt_wrapper = des3_encrypt_wrapper; + ctx->alg_decrypt_wrapper = des3_decrypt_wrapper; + + ctx->blocksize =DES3_BLOCK_SIZE ; +break ; +case QCRYPTO_CIPHER_ALG_AES_128 :case QCRYPTO_CIPHER_ALG_AES_192 :case QCRYPTO_CIPHER_ALG_AES_256 :
crypto/cipher.c +5 -2
@@ -28,6 +28,7 @@ static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = { [QCRYPTO_CIPHER_ALG_AES_192] = 24, [QCRYPTO_CIPHER_ALG_AES_256] = 32, [QCRYPTO_CIPHER_ALG_DES_RFB] = 8, + [QCRYPTO_CIPHER_ALG_3DES] = 24, [QCRYPTO_CIPHER_ALG_CAST5_128] = 16, [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16, [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
@@ -42,6 +43,7 @@ static size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = { [QCRYPTO_CIPHER_ALG_AES_192] = 16, [QCRYPTO_CIPHER_ALG_AES_256] = 16, [QCRYPTO_CIPHER_ALG_DES_RFB] = 8, + [QCRYPTO_CIPHER_ALG_3DES] = 8, [QCRYPTO_CIPHER_ALG_CAST5_128] = 8, [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16, [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
@@ -107,8 +109,9 @@ qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg, }if (mode ==QCRYPTO_CIPHER_MODE_XTS ) { -if (alg ==QCRYPTO_CIPHER_ALG_DES_RFB ) { -error_setg (errp,"XTS mode not compatible with DES-RFB" ); +if (alg ==QCRYPTO_CIPHER_ALG_DES_RFB + || alg ==QCRYPTO_CIPHER_ALG_3DES ) { +error_setg (errp,"XTS mode not compatible with DES-RFB/3DES" );return false; }if (nkey %2 ) {
qapi/crypto.json +2 -1
@@ -63,6 +63,7 @@ # @aes-192: AES with 192 bit / 24 byte keys # @aes-256: AES with 256 bit / 32 byte keys # @des-rfb: RFB specific variant of single DES. Do not use except in VNC. +# @3des: 3DES(EDE) with 192 bit / 24 byte keys (since 2.9) # @cast5-128: Cast5 with 128 bit / 16 byte keys # @serpent-128: Serpent with 128 bit / 16 byte keys # @serpent-192: Serpent with 192 bit / 24 byte keys
@@ -75,7 +76,7 @@ { 'enum': 'QCryptoCipherAlgorithm', 'prefix': 'QCRYPTO_CIPHER_ALG', 'data': ['aes-128', 'aes-192', 'aes-256', - 'des-rfb', + 'des-rfb', '3des', 'cast5-128', 'serpent-128', 'serpent-192', 'serpent-256', 'twofish-128', 'twofish-192', 'twofish-256']}
tests/test-crypto-cipher.c +119 -0
125 lines not on this page · raw message
Signature · 1 line
2.9.3
[Qemu-devel] [PULL v1 7/7] crypto: add HMAC algorithms testcases
In reply to Daniel P. BerrangeFrom: "Longpeng(Mike)" <[email protected]>
This patch add HMAC algorithms testcases
--- tests/Makefile.include | 2 + tests/test-crypto-hmac.c | 266 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 268 insertions(+) create mode 100644 tests/test-crypto-hmac.c
tests/Makefile.include +2 -0
@@ -91,6 +91,7 @@ gcov-files-test-qemu-opts-y = qom/test-qemu-opts.c check-unit-y += tests/test-write-threshold$(EXESUF) gcov-files-test-write-threshold-y = block/write-threshold.c check-unit-y += tests/test-crypto-hash$(EXESUF) +check-unit-y += tests/test-crypto-hmac$(EXESUF) check-unit-y += tests/test-crypto-cipher$(EXESUF) check-unit-y += tests/test-crypto-secret$(EXESUF) check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlscredsx509$(EXESUF)
@@ -571,6 +572,7 @@ tests/test-opts-visitor$(EXESUF): tests/test-opts-visitor.o $(test-qapi-obj-y) tests/test-mul64$(EXESUF): tests/test-mul64.o $(test-util-obj-y) tests/test-bitops$(EXESUF): tests/test-bitops.o $(test-util-obj-y) tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-obj-y) +tests/test-crypto-hmac$(EXESUF): tests/test-crypto-hmac.o $(test-crypto-obj-y) tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypto-obj-y) tests/test-crypto-secret$(EXESUF): tests/test-crypto-secret.o $(test-crypto-obj-y) tests/test-crypto-xts$(EXESUF): tests/test-crypto-xts.o $(test-crypto-obj-y)
tests/test-crypto-hmac.c new file +266 -0
266 lines not on this page · raw message
Signature · 1 line
2.9.3
[Qemu-devel] [PULL v1 4/7] crypto: support HMAC algorithms based on libgcrypt
In reply to Daniel P. BerrangeFrom: "Longpeng(Mike)" <[email protected]>
This patch add HMAC algorithms based on libgcrypt support
--- crypto/hmac-gcrypt.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 2 deletions(-)
crypto/hmac-gcrypt.c +109 -2
@@ -17,8 +17,28 @@#include "crypto/hmac.h" #include <gcrypt.h> +static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX ] = { + [QCRYPTO_HASH_ALG_MD5 ] =GCRY_MAC_HMAC_MD5 , + [QCRYPTO_HASH_ALG_SHA1 ] =GCRY_MAC_HMAC_SHA1 , + [QCRYPTO_HASH_ALG_SHA224 ] =GCRY_MAC_HMAC_SHA224 , + [QCRYPTO_HASH_ALG_SHA256 ] =GCRY_MAC_HMAC_SHA256 , + [QCRYPTO_HASH_ALG_SHA384 ] =GCRY_MAC_HMAC_SHA384 , + [QCRYPTO_HASH_ALG_SHA512 ] =GCRY_MAC_HMAC_SHA512 , + [QCRYPTO_HASH_ALG_RIPEMD160 ] =GCRY_MAC_HMAC_RMD160 , +}; + +typedef struct QCryptoHmacGcrypt QCryptoHmacGcrypt ; +struct QCryptoHmacGcrypt { +gcry_mac_hd_t handle; +}; +bool qcrypto_hmac_supports (QCryptoHashAlgorithm alg) { +if (alg <G_N_ELEMENTS (qcrypto_hmac_alg_map) && + qcrypto_hmac_alg_map[alg] !=GCRY_MAC_NONE ) { +return true; + } +return false; }
@@ -26,12 +46,58 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, const uint8_t *key, size_t nkey, Error **errp) { + QCryptoHmac *hmac; +QCryptoHmacGcrypt *ctx; +gcry_error_t err; + +if (!qcrypto_hmac_supports (alg)) { +error_setg (errp,"Unsupported hmac algorithm %s" , + QCryptoHashAlgorithm_lookup[alg]); +return NULL ; + } + + hmac =g_new0 (QCryptoHmac,1 ); + hmac->alg = alg; + + ctx =g_new0 (QCryptoHmacGcrypt,1 ); + + err =gcry_mac_open (&ctx->handle, qcrypto_hmac_alg_map[alg], +GCRY_MAC_FLAG_SECURE ,NULL ); +if (err !=0 ) { +error_setg (errp,"Cannot initialize hmac: %s" , +gcry_strerror (err)); + goto error; + } + + err =gcry_mac_setkey (ctx->handle, (const void *)key, nkey); +if (err !=0 ) { +error_setg (errp,"Cannot set key: %s" , +gcry_strerror (err)); + goto error; + } + + hmac->opaque = ctx; +return hmac; + +error: +g_free (ctx); +g_free (hmac);return NULL ; }void qcrypto_hmac_free (QCryptoHmac *hmac) { -return ; +QCryptoHmacGcrypt *ctx; + +if (!hmac) { +return ; + } + + ctx = hmac->opaque; +gcry_mac_close (ctx->handle); + +g_free (ctx); +g_free (hmac); } int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
@@ -41,5 +107,46 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac,size_t *resultlen, Error **errp) { -return -1 ; + QCryptoHmacGcrypt *ctx; +gcry_error_t err; +uint32_t ret; +int i; + + ctx = hmac->opaque; + +for (i =0 ; i < niov; i++) { +gcry_mac_write (ctx->handle, iov[i].iov_base, iov[i].iov_len); + } + + ret =gcry_mac_get_algo_maclen (qcrypto_hmac_alg_map[hmac->alg]); +if (ret <=0 ) { +error_setg (errp,"Unable to get hmac length: %s" , +gcry_strerror (ret)); +return -1 ; + } + +if (*resultlen ==0 ) { + *resultlen = ret; + *result =g_new0 (uint8_t, *resultlen); + }else if (*resultlen != ret) { +error_setg (errp,"Result buffer size %zu is smaller than hmac %d" , + *resultlen, ret); +return -1 ; + } + + err =gcry_mac_read (ctx->handle, *result, resultlen); +if (err !=0 ) { +error_setg (errp,"Cannot get result: %s" , +gcry_strerror (err)); +return -1 ; + } + + err =gcry_mac_reset (ctx->handle); +if (err !=0 ) { +error_setg (errp,"Cannot reset hmac context: %s" , +gcry_strerror (err)); +return -1 ; + } + +return 0 ; }
Signature · 1 line
2.9.3