crypto: add afalg-backend support
v6 cover crypto: add afalg-backend support
From: "Longpeng(Mike)" <[email protected]>
The AF_ALG socket family is the userspace interface for linux crypto API, users can use it to access hardware accelerators.
This patchset adds a afalg-backend for qemu crypto subsystem. QEMU would try to use afalg-backend first if configures '--enable-crypto -afalg' and hostos support AF_ALG.
I measured the performance about the afalg-backend impls, I tested how many data could be encrypted in 5 seconds.
NOTE: In some scenarios library-backend using CPU insns for crypto
would be faster.
test-environment: Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
*sha256*
chunk_size(bytes) MB/sec(afalg:sha256-ssse3) MB/sec(nettle) 512 93.03 185.87 1024 146.32 201.78 2048 213.32 210.93 4096 275.48 215.26 8192 321.77 217.49 16384 349.60 219.26 32768 363.59 219.73 65536 375.79 219.99
*hmac(sha256)*
chunk_size(bytes) MB/sec(afalg:sha256-ssse3) MB/sec(nettle) 512 71.26 165.55 1024 117.43 189.15 2048 180.96 203.24 4096 247.60 211.38 8192 301.99 215.65 16384 340.79 218.22 32768 365.51 219.49 65536 377.92 220.24
*cbc(aes128)*
chunk_size(bytes) MB/sec(afalg:cbc-aes-aesni) MB/sec(nettle) 512 371.76 188.41 1024 559.86 189.64 2048 768.66 192.11 4096 939.15 192.40 8192 1029.48 192.49 16384 1072.79 190.52 32768 1109.38 190.41 65536 1102.38 190.40
---
Changes since v5:
- use afalg-backend first only when configured with '--enable-crypto-afalg' and AF_ALG is supportted. [Daniel]
Changes since v4:
- remove 'name' field in 'struct CryptoAFAlg'. [Daniel]
- add error handling for read() returning less than requested. [Daniel]
- use iov_send_recv to recv msg in hash-afalg.c. [Daniel]
- refactor hmac benchmark as suggestion. [Daniel]
Changes since v3:
- add "Reviewed-by: Daniel P. Berrange <address@hidden>" in commit messages of PATCH 1/2/3/4/5/7/8/9/10/11.
- PATCH 12: use strlen() instead of qemu_strnlen() inqcrypto_afalg_build_saddr(). [Daniel]
- PATCH 12: rather than indenting the entire method, just return immediately if afalg=NULL. [Daniel]
- PATCH 13: use g_strdup_printf() instead of g_new0+snprintf() and remove redundant bounds check in qcrypto_afalg_cipher_format_name().
[Daniel]
- PATCH 13: s/except_niv/expect_niv s/origin_contorllen/origin_controllen.
[Daniel]
- PATCH 13: use '%zu' to print 'size_t' in qcrypto_afalg_cipher_setiv().
[Daniel]
- PATCH 13: remove qcrypto_cipher_using_afalg_drv(). [Daniel]
- PATCH 13: refactor the qcrypto_cipher_new() as Daniel's suggestion.
[Daniel]
- PATCH 13: correct the ->cmsg initialization intqcrypto_afalg_cipher_ctx_new() to
avoid different behaviour in test_cipher_null_iv(). [Daniel]
- PATCH 14: use g_strdup_printf() instead of g_new0+snprintf() and remove edundant bounds check in qcrypto_afalg_hash_format_name().
[Daniel]
- PATCH 14: s/except_len/expect_len. [Daniel]
- PATCH 14: free 'errp' if afalg_driver.hash_bytesv() failed. [Daniel]
- PATCH 14: maybe some afalg errors should be treated as fatal, but we have no idea yet, so add a "TODO" comment.
- PATCH 15: refactor the qcrypto_hmac_new() as Daniel's suggestion. [Daniel]
Changes since v2:
- init sockaddr_alg object when it's defined. [Gonglei]
- fix some superfluous initialization. [Gonglei]
- s/opeartion/operation/g in crypto/afalgpriv.h. [Gonglei]
- check 'niv' in qcrypto_afalg_cipher_setiv. [Gonglei]
Changes since v1:
- use "make check-speed" to testing the performance. [Daniel]
- put private definations into crypto/***priv.h. [Daniel]
- remove afalg socket from qapi-schema, put them into crypto/. [Daniel]
- some Error report change. [Daniel]
- s/QCryptoAfalg/QCryptoAFAlg. [Daniel]
- use snprintf with bounds checking instead of sprintf. [Daniel]
- use "qcrypto_afalg_" prefix and "qcrypto_nettle(gcrypt,glib,builtin)_"
prefix. [Daniel]
- add testing results in cover-letter. [Gonglei]
---
Longpeng(Mike) (18):
crypto: cipher: introduce context free function
crypto: cipher: introduce qcrypto_cipher_ctx_new for gcrypt-backend
crypto: cipher: introduce qcrypto_cipher_ctx_new for nettle-backend
crypto: cipher: introduce qcrypto_cipher_ctx_new for builtin-backend
crypto: cipher: add cipher driver framework
crypto: hash: add hash driver framework
crypto: hmac: move crypto/hmac.h into include/crypto/
crypto: hmac: introduce qcrypto_hmac_ctx_new for gcrypt-backend
crypto: hmac: introduce qcrypto_hmac_ctx_new for nettle-backend
crypto: hmac: introduce qcrypto_hmac_ctx_new for glib-backend
crypto: hmac: add hmac driver framework
crypto: introduce some common functions for af_alg backend
crypto: cipher: add afalg-backend cipher support
crypto: hash: add afalg-backend hash support
crypto: hmac: add af_alg-backend hmac support
tests: crypto: add cipher speed benchmark support
tests: crypto: add hash speed benchmark support
tests: crypto: add hmac speed benchmark support
configure | 30 ++++++ crypto/Makefile.objs | 3 + crypto/afalg.c | 116 +++++++++++++++++++++ crypto/afalgpriv.h | 64 ++++++++++++ crypto/cipher-afalg.c | 226 ++++++++++++++++++++++++++++++++++++++++ crypto/cipher-builtin.c | 125 +++++++++++----------- crypto/cipher-gcrypt.c | 105 ++++++++++--------- crypto/cipher-nettle.c | 84 ++++++++------- crypto/cipher.c | 80 ++++++++++++++ crypto/cipherpriv.h | 56 ++++++++++ crypto/hash-afalg.c | 214 +++++++++++++++++++++++++++++++++++++ crypto/hash-gcrypt.c | 19 ++-- crypto/hash-glib.c | 19 ++-- crypto/hash-nettle.c | 19 ++-- crypto/hash.c | 30 ++++++ crypto/hashpriv.h | 39 +++++++ crypto/hmac-gcrypt.c | 42 ++++---- crypto/hmac-glib.c | 63 ++++++----- crypto/hmac-nettle.c | 42 ++++---- crypto/hmac.c | 58 +++++++++++ crypto/hmac.h | 166 ----------------------------- crypto/hmacpriv.h | 48 +++++++++ include/crypto/cipher.h | 1 + include/crypto/hmac.h | 167 +++++++++++++++++++++++++++++ tests/Makefile.include | 13 ++- tests/benchmark-crypto-cipher.c | 88 ++++++++++++++++ tests/benchmark-crypto-hash.c | 67 ++++++++++++ tests/benchmark-crypto-hmac.c | 82 +++++++++++++++ 28 files changed, 1655 insertions(+), 411 deletions(-) create mode 100644 crypto/afalg.c create mode 100644 crypto/afalgpriv.h create mode 100644 crypto/cipher-afalg.c create mode 100644 crypto/cipherpriv.h create mode 100644 crypto/hash-afalg.c create mode 100644 crypto/hashpriv.h delete mode 100644 crypto/hmac.h create mode 100644 crypto/hmacpriv.h create mode 100644 include/crypto/hmac.h create mode 100644 tests/benchmark-crypto-cipher.c create mode 100644 tests/benchmark-crypto-hash.c create mode 100644 tests/benchmark-crypto-hmac.c
Signature · 1 line
1.8.3.1
PATCH v6 1/18 crypto: cipher: introduce context free function
From: "Longpeng(Mike)" <[email protected]>
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.
--- crypto/cipher-gcrypt.c | 31 ++++++++++++++++++------------- crypto/cipher-nettle.c | 18 ++++++++++++++---- 2 files changed, 32 insertions(+), 17 deletions(-)
crypto/cipher-gcrypt.c +18 -13
@@ -64,6 +64,22 @@ struct QCryptoCipherGcrypt {uint8_t *iv; }; +static void gcrypt_cipher_free_ctx (QCryptoCipherGcrypt *ctx, +QCryptoCipherMode mode) +{ +if (!ctx) { +return ; + } + +gcry_cipher_close (ctx->handle); +if (mode ==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(QCryptoCipherAlgorithm alg,return cipher; error: -gcry_cipher_close (ctx->handle); -if (cipher->mode ==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(QCryptoCipherAlgorithm alg,void qcrypto_cipher_free (QCryptoCipher *cipher) { -QCryptoCipherGcrypt *ctx;if (!cipher) {return ; } - ctx = cipher->opaque; -gcry_cipher_close (ctx->handle); -if (cipher->mode ==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); }
crypto/cipher-nettle.c +14 -4
@@ -249,6 +249,19 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, } +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) } ctx = 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); }
Signature · 1 line
1.8.3.1
PATCH v6 2/18 crypto: cipher: introduce qcrypto_cipher_ctx_new for gcrypt-backend
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for gcrypt-backend impls.
--- crypto/cipher-gcrypt.c | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-)
crypto/cipher-gcrypt.c +33 -17
@@ -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) { +return NULL ; + } + + cipher =g_new0 (QCryptoCipher,1 ); + cipher->alg = alg; + cipher->mode = mode; + cipher->opaque = ctx; + +return cipher; +}
Signature · 1 line
1.8.3.1
PATCH v6 3/18 crypto: cipher: introduce qcrypto_cipher_ctx_new for nettle-backend
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for nettle-backend impls.
--- crypto/cipher-nettle.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-)
crypto/cipher-nettle.c +29 -12
@@ -262,12 +262,12 @@ static void nettle_cipher_free_ctx(QCryptoCipherNettle *ctx) } -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;
@@ -287,12 +287,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,return NULL ; } - cipher =g_new0 (QCryptoCipher,1 ); - cipher->alg = alg; - cipher->mode = mode; - ctx =g_new0 (QCryptoCipherNettle,1 ); - cipher->opaque = ctx;switch (alg) {case QCRYPTO_CIPHER_ALG_DES_RFB :
@@ -436,10 +431,10 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, ctx->iv =g_new0 (uint8_t, ctx->blocksize); -return cipher; +return ctx; error: -qcrypto_cipher_free (cipher); +nettle_cipher_free_ctx (ctx);return NULL ; }
@@ -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 =qcrypto_cipher_ctx_new (alg, mode, key, nkey, errp); +if (!ctx) { +return NULL ; + } + + cipher =g_new0 (QCryptoCipher,1 ); + cipher->alg = alg; + cipher->mode = mode; + cipher->opaque = ctx; + +return cipher; +}
Signature · 1 line
1.8.3.1
PATCH v6 4/18 crypto: cipher: introduce qcrypto_cipher_ctx_new for builtin-backend
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for builtin-backend impls.
--- crypto/cipher-builtin.c | 101 ++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 46 deletions(-)
crypto/cipher-builtin.c +55 -46
@@ -235,23 +235,24 @@ static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher, -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; -if (cipher->mode !=QCRYPTO_CIPHER_MODE_CBC && - cipher->mode !=QCRYPTO_CIPHER_MODE_ECB && - cipher->mode !=QCRYPTO_CIPHER_MODE_XTS ) { +if (mode !=QCRYPTO_CIPHER_MODE_CBC && + mode !=QCRYPTO_CIPHER_MODE_ECB && + mode !=QCRYPTO_CIPHER_MODE_XTS ) {error_setg (errp,"Unsupported cipher mode %s" , - QCryptoCipherMode_lookup[cipher->mode]); -return -1 ; + QCryptoCipherMode_lookup[mode]); +return NULL ; } ctxt =g_new0 (QCryptoCipherBuiltin,1 ); -if (cipher->mode ==QCRYPTO_CIPHER_MODE_XTS ) { +if (mode ==QCRYPTO_CIPHER_MODE_XTS ) {if (AES_set_encrypt_key (key, nkey *4 , &ctxt->state.aes.key.enc) !=0 ) {error_setg (errp,"Failed to set encryption key" ); goto error;
@@ -291,13 +292,11 @@ static int qcrypto_cipher_init_aes(QCryptoCipher *cipher, ctxt->encrypt = qcrypto_cipher_encrypt_aes; ctxt->decrypt = qcrypto_cipher_decrypt_aes; - cipher->opaque = ctxt; - -return 0 ; +return ctxt; error:g_free (ctxt); -return -1 ; +return NULL ; }
@@ -370,16 +369,17 @@ static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher, } -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; -if (cipher->mode !=QCRYPTO_CIPHER_MODE_ECB ) { +if (mode !=QCRYPTO_CIPHER_MODE_ECB ) {error_setg (errp,"Unsupported cipher mode %s" , - QCryptoCipherMode_lookup[cipher->mode]); -return -1 ; + QCryptoCipherMode_lookup[mode]); +return NULL ; } ctxt =g_new0 (QCryptoCipherBuiltin,1 );
@@ -394,9 +394,7 @@ static int qcrypto_cipher_init_des_rfb(QCryptoCipher *cipher, ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb; ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb; - cipher->opaque = ctxt; - -return 0 ; +return ctxt; }
@@ -426,12 +424,13 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, } -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;switch (mode) {case QCRYPTO_CIPHER_MODE_ECB :
@@ -444,39 +443,27 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,return NULL ; } - cipher =g_new0 (QCryptoCipher,1 ); - cipher->alg = alg; - cipher->mode = mode; -if (!qcrypto_cipher_validate_key_length (alg, mode, nkey, errp)) { - goto error; +return NULL ; } -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 =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 =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 ; } -return cipher; - - error: -g_free (cipher); -return NULL ; +return ctxt; }void qcrypto_cipher_free (QCryptoCipher *cipher)
@@ -537,3 +524,25 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,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 =qcrypto_cipher_ctx_new (alg, mode, key, nkey, errp); +if (!ctxt) { +return NULL ; + } + + cipher =g_new0 (QCryptoCipher,1 ); + cipher->alg = alg; + cipher->mode = mode; + cipher->opaque = ctxt; + +return cipher; +}
Signature · 1 line
1.8.3.1
PATCH v6 5/18 crypto: cipher: add cipher driver framework
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
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.
--- 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
crypto/cipher-builtin.c +25 -39
@@ -22,6 +22,7 @@#include "crypto/aes.h" #include "crypto/desrfb.h" #include "crypto/xts.h" +#include "cipherpriv.h" typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext ;struct QCryptoCipherBuiltinAESContext {
@@ -466,25 +467,22 @@ static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,return ctxt; } -void qcrypto_cipher_free (QCryptoCipher *cipher) +static void +qcrypto_builtin_cipher_ctx_free (QCryptoCipher *cipher) {QCryptoCipherBuiltin *ctxt; -if (!cipher) { -return ; - } - ctxt = cipher->opaque; ctxt->free (cipher); -g_free (cipher); } -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 = cipher->opaque;
@@ -498,11 +496,12 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher, } -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 = cipher->opaque;
@@ -516,9 +515,10 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher, } -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 = cipher->opaque;
@@ -526,23 +526,9 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, } -QCryptoCipher *qcrypto_cipher_new (QCryptoCipherAlgorithm alg, -QCryptoCipherMode mode, -const uint8_t *key,size_t nkey, -Error **errp) -{ -QCryptoCipher *cipher; -QCryptoCipherBuiltin *ctxt; - - ctxt =qcrypto_cipher_ctx_new (alg, mode, key, nkey, errp); -if (!ctxt) { -return NULL ; - } - - cipher =g_new0 (QCryptoCipher,1 ); - cipher->alg = alg; - cipher->mode = mode; - cipher->opaque = ctxt; - -return cipher; -} +static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = { + .cipher_encrypt = qcrypto_builtin_cipher_encrypt, + .cipher_decrypt = qcrypto_builtin_cipher_decrypt, + .cipher_setiv = qcrypto_builtin_cipher_setiv, + .cipher_free = qcrypto_builtin_cipher_ctx_free, +};
crypto/cipher-gcrypt.c +30 -42
116 lines not on this page · raw message
crypto/cipher-nettle.c +29 -42
115 lines not on this page · raw message
crypto/cipher.c +65 -0
74 lines not on this page · raw message
crypto/cipherpriv.h new file +40 -0
@@ -0,0 +1,40 @@ +/* +* QEMU Crypto cipher driver supports +* +* Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. +* +* Authors: +* Longpeng(Mike) <[email protected]> +* +* 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
include/crypto/cipher.h +1 -0
@@ -80,6 +80,7 @@ struct QCryptoCipher {QCryptoCipherAlgorithm alg;QCryptoCipherMode mode;void *opaque; +void *driver; };/**
Signature · 1 line
1.8.3.1
PATCH v6 6/18 crypto: hash: add hash driver framework
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
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.
--- 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
crypto/hash-gcrypt.c +13 -6
@@ -22,6 +22,7 @@#include <gcrypt.h> #include "qapi/error.h" #include "crypto/hash.h" +#include "hashpriv.h" static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX ] = {
@@ -44,12 +45,13 @@ gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg) } -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 = { + .hash_bytesv = qcrypto_gcrypt_hash_bytesv, +};
crypto/hash-glib.c +13 -6
@@ -21,6 +21,7 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hash.h" +#include "hashpriv.h" static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX ] = {
@@ -47,12 +48,13 @@ gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg) } -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 = { + .hash_bytesv = qcrypto_glib_hash_bytesv, +};
crypto/hash-nettle.c +13 -6
@@ -21,6 +21,7 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hash.h" +#include "hashpriv.h" #include <nettle/md5.h> #include <nettle/sha.h> #include <nettle/ripemd160.h>
@@ -103,12 +104,13 @@ gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg) } -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,return 0 ; } + + +QCryptoHashDriver qcrypto_hash_lib_driver = { + .hash_bytesv = qcrypto_nettle_hash_bytesv, +};
crypto/hash.c +13 -0
@@ -21,6 +21,7 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hash.h" +#include "hashpriv.h" static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX ] = { [QCRYPTO_HASH_ALG_MD5 ] =16 ,
@@ -38,6 +39,18 @@ size_t qcrypto_hash_digest_len(QCryptoHashAlgorithm alg)return qcrypto_hash_alg_size[alg]; } +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); +} + int qcrypto_hash_bytes(QCryptoHashAlgorithm alg, const char *buf,
crypto/hashpriv.h new file +31 -0
@@ -0,0 +1,31 @@ +/* +* QEMU Crypto hash driver supports +* +* Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. +* +* Authors: +* Longpeng(Mike) <[email protected]> +* +* 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
Signature · 1 line
1.8.3.1
PATCH v6 7/18 crypto: hmac: move crypto/hmac.h into include/crypto/
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
Moves crypto/hmac.h into include/crypto/, likes cipher.h and hash.h
--- 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
crypto/hmac.h deleted +0 -166
@@ -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
include/crypto/hmac.h new file +166 -0
166 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v6 8/18 crypto: hmac: introduce qcrypto_hmac_ctx_new for gcrypt-backend
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
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.
--- crypto/hmac-gcrypt.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-)
crypto/hmac-gcrypt.c +25 -10
@@ -42,11 +42,11 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)return false; } -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;
@@ -56,9 +56,6 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm 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],
@@ -73,15 +70,14 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,if (err !=0 ) {error_setg (errp,"Cannot set key: %s" ,gcry_strerror (err)); +gcry_mac_close (ctx->handle); goto error; } - hmac->opaque = ctx; -return hmac; +return ctx; error:g_free (ctx); -g_free (hmac);return NULL ; }
@@ -150,3 +146,22 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac,return 0 ; } + +QCryptoHmac *qcrypto_hmac_new (QCryptoHashAlgorithm alg, +const uint8_t *key,size_t nkey, +Error **errp) +{ +QCryptoHmac *hmac; +QCryptoHmacGcrypt *ctx; + + ctx =qcrypto_hmac_ctx_new (alg, key, nkey, errp); +if (!ctx) { +return NULL ; + } + + hmac =g_new0 (QCryptoHmac,1 ); + hmac->alg = alg; + hmac->opaque = ctx; + +return hmac; +}
Signature · 1 line
1.8.3.1
PATCH v6 9/18 crypto: hmac: introduce qcrypto_hmac_ctx_new for nettle-backend
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
Extracts qcrypto_hmac_ctx_new() from qcrypto_hmac_new() for nettle-backend impls.
--- crypto/hmac-nettle.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
crypto/hmac-nettle.c +24 -10
@@ -97,11 +97,11 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)return false; } -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;if (!qcrypto_hmac_supports (alg)) {
@@ -110,16 +110,11 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm 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; +return ctx; }void qcrypto_hmac_free (QCryptoHmac *hmac)
@@ -173,3 +168,22 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac,return 0 ; } + +QCryptoHmac *qcrypto_hmac_new (QCryptoHashAlgorithm alg, +const uint8_t *key,size_t nkey, +Error **errp) +{ +QCryptoHmac *hmac; +QCryptoHmacNettle *ctx; + + ctx =qcrypto_hmac_ctx_new (alg, key, nkey, errp); +if (!ctx) { +return NULL ; + } + + hmac =g_new0 (QCryptoHmac,1 ); + hmac->alg = alg; + hmac->opaque = ctx; + +return hmac; +}
Signature · 1 line
1.8.3.1
PATCH v6 10/18 crypto: hmac: introduce qcrypto_hmac_ctx_new for glib-backend
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
Extracts qcrypto_hmac_ctx_new() from qcrypto_hmac_new() for glib-backend impls.
--- crypto/hmac-glib.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
crypto/hmac-glib.c +24 -10
@@ -49,11 +49,11 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)return false; } -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;if (!qcrypto_hmac_supports (alg)) {
@@ -62,9 +62,6 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm 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],
@@ -74,12 +71,10 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, goto error; } - hmac->opaque = ctx; -return hmac; +return ctx; error:g_free (ctx); -g_free (hmac);return NULL ; }
@@ -134,6 +129,25 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac, return 0; } +QCryptoHmac *qcrypto_hmac_new (QCryptoHashAlgorithm alg, +const uint8_t *key,size_t nkey, +Error **errp) +{ +QCryptoHmac *hmac; +QCryptoHmacGlib *ctx; + + ctx =qcrypto_hmac_ctx_new (alg, key, nkey, errp); +if (!ctx) { +return NULL ; + } + + hmac =g_new0 (QCryptoHmac,1 ); + hmac->alg = alg; + hmac->opaque = ctx; + +return hmac; +} +#else bool qcrypto_hmac_supports (QCryptoHashAlgorithm alg)
Signature · 1 line
1.8.3.1
PATCH v6 11/18 crypto: hmac: add hmac driver framework
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
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.
--- 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
crypto/hmac-gcrypt.c +17 -34
@@ -15,6 +15,7 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/hmac.h" +#include "hmacpriv.h" #include <gcrypt.h> static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX ] = {
@@ -42,10 +43,9 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)return false; } -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 ; } -void qcrypto_hmac_free (QCryptoHmac *hmac) +static void +qcrypto_gcrypt_hmac_ctx_free (QCryptoHmac *hmac) {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, -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 ; } -QCryptoHmac *qcrypto_hmac_new (QCryptoHashAlgorithm alg, -const uint8_t *key,size_t nkey, -Error **errp) -{ -QCryptoHmac *hmac; -QCryptoHmacGcrypt *ctx; - - ctx =qcrypto_hmac_ctx_new (alg, key, nkey, errp); -if (!ctx) { -return NULL ; - } - - hmac =g_new0 (QCryptoHmac,1 ); - hmac->alg = alg; - hmac->opaque = ctx; - -return hmac; -} +QCryptoHmacDriver qcrypto_hmac_lib_driver = { + .hmac_bytesv = qcrypto_gcrypt_hmac_bytesv, + .hmac_free = qcrypto_gcrypt_hmac_ctx_free, +};
crypto/hmac-glib.c +30 -45
124 lines not on this page · raw message
crypto/hmac-nettle.c +17 -35
80 lines not on this page · raw message
crypto/hmac.c +44 -0
56 lines not on this page · raw message
crypto/hmacpriv.h new file +36 -0
@@ -0,0 +1,36 @@ +/* +* QEMU Crypto hmac driver supports +* +* Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. +* +* Authors: +* Longpeng(Mike) <[email protected]> +* +* 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
include/crypto/hmac.h +1 -0
@@ -18,6 +18,7 @@ typedef struct QCryptoHmac QCryptoHmac;struct QCryptoHmac {QCryptoHashAlgorithm alg;void *opaque; +void *driver; };/**
Signature · 1 line
1.8.3.1
PATCH v6 12/18 crypto: introduce some common functions for af_alg backend
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
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.
--- 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
configure +30 -0
@@ -375,6 +375,7 @@ libnfs="" coroutine="" coroutine_pool="" debug_stack_usage="no" +crypto_afalg="no" seccomp="" glusterfs="" glusterfs_xlator_opt="no"
@@ -1124,6 +1125,8 @@ for opt do ;; --enable-debug-stack-usage) debug_stack_usage="yes" ;; + --enable-crypto-afalg) crypto_afalg="yes" + ;; --disable-docs) docs="no" ;; --enable-docs) docs="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 first. 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=yes fi +########################################## +# check for usable AF_ALG environment +hava_af_alg=no +cat > $TMPC << EOF +#include <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <linux/if_alg.h> +int main(void) { + int sock; + sock = socket(AF_ALG, SOCK_SEQPACKET, 0); + return sock; +} +EOF +if compile_prog "" "" ; then + have_afalg=yes +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" = "yes" ; then echo "CONFIG_DEBUG_STACK_USAGE=y" >> $config_host_mak fi +if test "$crypto_afalg" = "yes" ; then + if test "$have_afalg" = "yes" ; then + echo "CONFIG_AF_ALG=y" >> $config_host_mak + fi +fi + if test "$open_by_handle_at" = "yes" ; then echo "CONFIG_OPEN_BY_HANDLE=y" >> $config_host_mak fi
crypto/Makefile.objs +1 -0
@@ -10,6 +10,7 @@ crypto-obj-$(if $(CONFIG_NETTLE),n,$(if $(CONFIG_GCRYPT_HMAC),n,y)) += hmac-glib crypto-obj-y += aes.o crypto-obj-y += desrfb.o crypto-obj-y += cipher.o +crypto-obj-$(CONFIG_AF_ALG) += afalg.o crypto-obj-y += tlscreds.o crypto-obj-y += tlscredsanon.o crypto-obj-y += tlscredsx509.o
crypto/afalg.c new file +116 -0
116 lines not on this page · raw message
crypto/afalgpriv.h new file +54 -0
54 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH v6 12/18] crypto: introduce some common functions for af_alg backend
Quoted 30 lines · On Fri, Jul 14, 2017 at 02:04:05PM -0400, [email protected] wrote:
From: "Longpeng(Mike)" <[email protected]>
The
AF_ALGsocket family is the userspace interface for linux crypto API, this patch addsaf_algfamily support and some common functions foraf_algbackend. It'll be used by afalg-backend crypto latter.Signed-off-by Longpeng(Mike) <[email protected]>--- 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.hconfigure +1
-0@@ -375,6 +375,7 @@ libnfs="" coroutine="" coroutine_pool="" debug_stack_usage="no" +crypto_afalg="no" seccomp="" glusterfs="" glusterfs_xlator_opt="no"
configure for opt do · line 1129
Quoted 5 lines
configure +2
-0@@ -1124,6 +1125,8 @@ for opt do ;; --enable-debug-stack-usage) debug_stack_usage="yes" ;; + --enable-crypto-afalg) crypto_afalg="yes" + ;;
We should have an --disable flag too for consistency, even though it is the default currently.
Quoted 13 lines
configure +2
-0--disable-docs) docs="no" ;; --enable-docs) docs="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 first. Optional features, enabled with --enable-FEATURE and disabled with --disable-FEATURE, default is enabled if available:
configure if compile_prog "" "" ; then · line 4844
Quoted 6 lines
configure +3
-0@@ -4834,6 +4839,24 @@ if compile_prog "" "" ; then have_af_vsock=yes fi +########################################## +# check for usable AF_ALG environment +hava_af_alg=no
typo s/af_alg/afalg/
Quoted 4 lines
configure +4
-0+cat > $TMPC << EOF +#include <errno.h> +#include <sys/types.h> +#include <sys/socket.h>
configure if compile_prog "" "" ; then · line 4858
Quoted 10 lines
configure +10
-0+#include <linux/if_alg.h> +int main(void) { + int sock; + sock = socket(AF_ALG, SOCK_SEQPACKET, 0); + return sock; +} +EOF +if compile_prog "" "" ; then + have_afalg=yes +fi
If the user requests afalg and the compile test fails, we should report an error.
Quoted 13 lines
configure +2
-0+ ################################################# # 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"
configure if test "$debug_stack_usage" = "yes" ; then · line 5839
Quoted 5 lines
configure +2
-0@@ -5811,6 +5835,12 @@ if test "$debug_stack_usage" = "yes" ; then echo "CONFIG_DEBUG_STACK_USAGE=y" >> $config_host_mak fi +if test "$crypto_afalg" = "yes" ; then + if test "$have_afalg" = "yes" ; then
The check for have_afalg=yes is redundant if we report an error earlier
configure if test "$debug_stack_usage" = "yes" ; then · line 5846
Quoted 7 lines
configure +4
-0+ echo "CONFIG_AF_ALG=y" >> $config_host_mak + fi +fi + if test "$open_by_handle_at" = "yes" ; then echo "CONFIG_OPEN_BY_HANDLE=y" >> $config_host_mak fi
These are minor points, so I'm going to squash the following into your change when merging:
configure +13 -6
@@ -1127,6 +1127,8 @@ for opt do ;; --enable-crypto-afalg) crypto_afalg="yes" ;; + --disable-crypto-afalg) crypto_afalg="no" + ;; --disable-docs) docs="no" ;; --enable-docs) docs="yes"
@@ -1446,8 +1448,6 @@ 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 first. Optional features, enabled with --enable-FEATURE and disabled with --disable-FEATURE, default is enabled if available:
@@ -1523,6 +1523,7 @@ disabled with --disable-FEATURE, default is enabled if available: qom-cast-debug cast debugging support tools build qemu-io, qemu-nbd and qemu-image tools vxhs Veritas HyperScale vDisk backend support + crypto-afalg Linux AF_ALG crypto backend driver NOTE: The object files are built at the place where configure is launched EOF
@@ -4841,7 +4842,7 @@ fi ########################################## # check for usable AF_ALG environment -hava_af_alg=no +hava_afalg=no cat > $TMPC << EOF #include <errno.h> #include <sys/types.h>
@@ -4856,6 +4857,14 @@ EOF if compile_prog "" "" ; then have_afalg=yes fi +if test "$crypto_afalg" = "yes" +then + if test "$have_afalg" != "yes" + then + error_exit "AF_ALG requested but could not be detected" + fi +fi + ################################################# # Sparc implicitly links with --relax, which is
@@ -5836,9 +5845,7 @@ if test "$debug_stack_usage" = "yes" ; then fi if test "$crypto_afalg" = "yes" ; then - if test "$have_afalg" = "yes" ; then - echo "CONFIG_AF_ALG=y" >> $config_host_mak - fi + echo "CONFIG_AF_ALG=y" >> $config_host_mak fi if test "$open_by_handle_at" = "yes" ; then
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH v6 13/18 crypto: cipher: add afalg-backend cipher support
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
Adds afalg-backend cipher support: introduces some private APIs firstly, and then intergrates them into qcrypto_cipher_afalg_driver.
--- 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
crypto/Makefile.objs +1 -0
@@ -11,6 +11,7 @@ crypto-obj-y += aes.o crypto-obj-y += desrfb.o crypto-obj-y += cipher.o crypto-obj-$(CONFIG_AF_ALG) += afalg.o +crypto-obj-$(CONFIG_AF_ALG) += cipher-afalg.o crypto-obj-y += tlscreds.o crypto-obj-y += tlscredsanon.o crypto-obj-y += tlscredsx509.o
crypto/afalgpriv.h +9 -0
@@ -19,6 +19,15 @@#define SALG_TYPE_LEN_MAX 14#define SALG_NAME_LEN_MAX 64 +#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 ;struct QCryptoAFAlg {
crypto/cipher-afalg.c new file +226 -0
226 lines not on this page · raw message
crypto/cipher.c +19 -4
@@ -163,18 +163,33 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, Error **errp) {QCryptoCipher *cipher; -void *ctx; +void *ctx =NULL ; +Error *err2 =NULL ; +QCryptoCipherDriver *drv; + +#ifdef CONFIG_AF_ALG + ctx =qcrypto_afalg_cipher_ctx_new (alg, mode, key, nkey, &err2); +if (ctx) { + drv = &qcrypto_cipher_afalg_driver; + } +#endif - ctx =qcrypto_cipher_ctx_new (alg, mode, key, nkey, errp);if (!ctx) { -return NULL ; + ctx =qcrypto_cipher_ctx_new (alg, mode, key, nkey, errp); +if (!ctx) { +error_free (err2); +return NULL ; + } + + drv = &qcrypto_cipher_lib_driver; +error_free (err2); } cipher =g_new0 (QCryptoCipher,1 ); cipher->alg = alg; cipher->mode = mode; cipher->opaque = ctx; - cipher->driver = (void *)&qcrypto_cipher_lib_driver; + cipher->driver = (void *)drv;return cipher; }
crypto/cipherpriv.h +16 -0
@@ -15,6 +15,8 @@#ifndef QCRYPTO_CIPHERPRIV_H #define QCRYPTO_CIPHERPRIV_H +#include "qapi-types.h" +typedef struct QCryptoCipherDriver QCryptoCipherDriver ; struct QCryptoCipherDriver {
@@ -37,4 +39,18 @@ struct QCryptoCipherDriver { void (*cipher_free)(QCryptoCipher *cipher); }; +#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
Signature · 1 line
1.8.3.1
PATCH v6 14/18 crypto: hash: add afalg-backend hash support
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
Adds afalg-backend hash support: introduces some private APIs firstly, and then intergrates them into qcrypto_hash_afalg_driver.
--- 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
crypto/Makefile.objs +1 -0
@@ -12,6 +12,7 @@ crypto-obj-y += desrfb.o crypto-obj-y += cipher.o crypto-obj-$(CONFIG_AF_ALG) += afalg.o crypto-obj-$(CONFIG_AF_ALG) += cipher-afalg.o +crypto-obj-$(CONFIG_AF_ALG) += hash-afalg.o crypto-obj-y += tlscreds.o crypto-obj-y += tlscredsanon.o crypto-obj-y += tlscredsx509.o
crypto/afalgpriv.h +1 -0
@@ -24,6 +24,7 @@#endif #define AFALG_TYPE_CIPHER "skcipher" +#define AFALG_TYPE_HASH "hash"#define ALG_OPTYPE_LEN 4#define ALG_MSGIV_LEN (len) (sizeof(struct af_alg_iv) + (len))
crypto/hash-afalg.c new file +136 -0
136 lines not on this page · raw message
crypto/hash.c +17 -0
@@ -46,6 +46,23 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,size_t *resultlen, Error **errp) { +#ifdef CONFIG_AF_ALG + int ret; + + ret = qcrypto_hash_afalg_driver.hash_bytesv (alg, iov, niov, + result, resultlen, + errp); +if (ret ==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);
crypto/hashpriv.h +8 -0
@@ -28,4 +28,12 @@ struct QCryptoHashDriver {extern QCryptoHashDriver qcrypto_hash_lib_driver; +#ifdef CONFIG_AF_ALG + +#include "afalgpriv.h" + +extern QCryptoHashDriver qcrypto_hash_afalg_driver; + +#endif +#endif
Signature · 1 line
1.8.3.1
PATCH v6 15/18 crypto: hmac: add af_alg-backend hmac support
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
Adds afalg-backend hmac support: introduces some private APIs firstly, and then intergrates them into qcrypto_hmac_afalg_driver.
--- crypto/hash-afalg.c | 104 +++++++++++++++++++++++++++++++++++++++++++++------- crypto/hmac.c | 22 +++++++++-- crypto/hmacpriv.h | 12 ++++++ 3 files changed, 121 insertions(+), 17 deletions(-)
crypto/hash-afalg.c +91 -13
@@ -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" 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 ; } - name =g_strdup_printf ("%s" , alg_name); +if (is_hmac) { + name =g_strdup_printf ("hmac(%s)" , alg_name); + }else { + name =g_strdup_printf ("%s" , alg_name); + }return name; }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; - name =qcrypto_afalg_hash_format_name (alg, errp); + name =qcrypto_afalg_hash_format_name (alg, is_hmac, errp);if (!name) {return NULL ; }
@@ -77,19 +86,46 @@ qcrypto_afalg_hash_ctx_new(QCryptoHashAlgorithm alg, Error **errp)g_free (name); +/* HMAC needs setkey */ +if (is_hmac) { +if (qemu_setsockopt (afalg->tfmfd,SOL_ALG ,ALG_SET_KEY , + key, nkey) !=0 ) { +error_setg_errno (errp, errno,"Set hmac key failed" ); +qcrypto_afalg_comm_free (afalg); +return NULL ; + } + } +return afalg; } +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 =0 ; +bool is_hmac = (hmac !=NULL ) ? true : false;const int expect_len =qcrypto_hash_digest_len (alg);if (*resultlen ==0 ) {
@@ -102,9 +138,13 @@ qcrypto_afalg_hash_bytesv(QCryptoHashAlgorithm alg,return -1 ; } - afalg =qcrypto_afalg_hash_ctx_new (alg, errp); -if (!afalg) { -return -1 ; +if (is_hmac) { + afalg = hmac; + }else { + afalg =qcrypto_afalg_hash_ctx_new (alg, errp); +if (!afalg) { +return -1 ; + } }/* send data to kernel's crypto core */
@@ -127,10 +167,48 @@ qcrypto_afalg_hash_bytesv(QCryptoHashAlgorithm alg, } out: -qcrypto_afalg_comm_free (afalg); +if (!is_hmac) { +qcrypto_afalg_comm_free (afalg); + }return ret; } +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 = hmac->opaque; +qcrypto_afalg_comm_free (afalg); +} +QCryptoHashDriver qcrypto_hash_afalg_driver = { .hash_bytesv = qcrypto_afalg_hash_bytesv, }; + +QCryptoHmacDriver qcrypto_hmac_afalg_driver = { + .hmac_bytesv = qcrypto_afalg_hmac_bytesv, + .hmac_free = qcrypto_afalg_hmac_ctx_free, +};
crypto/hmac.c +18 -4
@@ -89,17 +89,31 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, Error **errp) {QCryptoHmac *hmac; -void *ctx; +void *ctx =NULL ; +Error *err2 =NULL ; +QCryptoHmacDriver *drv; + +#ifdef CONFIG_AF_ALG + ctx =qcrypto_afalg_hmac_ctx_new (alg, key, nkey, &err2); +if (ctx) { + drv = &qcrypto_hmac_afalg_driver; + } +#endif - ctx =qcrypto_hmac_ctx_new (alg, key, nkey, errp);if (!ctx) { -return NULL ; + ctx =qcrypto_hmac_ctx_new (alg, key, nkey, errp); +if (!ctx) { +return NULL ; + } + + drv = &qcrypto_hmac_lib_driver; +error_free (err2); } hmac =g_new0 (QCryptoHmac,1 ); hmac->alg = alg; hmac->opaque = ctx; - hmac->driver = (void *)&qcrypto_hmac_lib_driver; + hmac->driver = (void *)drv;return hmac; }
crypto/hmacpriv.h +12 -0
@@ -33,4 +33,16 @@ extern void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, Error **errp);extern QCryptoHmacDriver qcrypto_hmac_lib_driver; +#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
Signature · 1 line
1.8.3.1
PATCH v6 16/18 tests: crypto: add cipher speed benchmark support
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
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.
--- 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
tests/Makefile.include +7 -2
@@ -99,6 +99,7 @@ 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-speed-y += tests/benchmark-crypto-cipher$(EXESUF) check-unit-y += tests/test-crypto-secret$(EXESUF) check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlscredsx509$(EXESUF) check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlssession$(EXESUF)
@@ -532,6 +533,7 @@ test-qom-obj-y = $(qom-obj-y) $(test-util-obj-y) test-qapi-obj-y = 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 = $(crypto-obj-y) $(test-qom-obj-y) test-crypto-obj-y = $(crypto-obj-y) $(test-qom-obj-y) test-io-obj-y = $(io-obj-y) $(test-crypto-obj-y) test-block-obj-y = $(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-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/benchmark-crypto-cipher$(EXESUF): tests/benchmark-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)
@@ -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-qtest-%: $(check-qtest-y) $(GCOV) $(GCOV_OPTIONS) $$f -o `dirname $$f`; \ done,) -.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_=$${MALLOC_PERTURB_:-$$((RANDOM % 255 + 1))} \
@@ -893,6 +897,7 @@ check-tests/qapi-schema/doc-good.texi: tests/qapi-schema/doc-good.test.texi check-qapi-schema: $(patsubst %,check-%, $(check-qapi-schema-y)) check-tests/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:
tests/benchmark-crypto-cipher.c new file +88 -0
88 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v6 17/18 tests: crypto: add hash speed benchmark support
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
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.
--- tests/Makefile.include | 2 ++ tests/benchmark-crypto-hash.c | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/benchmark-crypto-hash.c
tests/Makefile.include +2 -0
@@ -97,6 +97,7 @@ gcov-files-test-keyval-y = util/keyval.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-speed-y += tests/benchmark-crypto-hash$(EXESUF) check-unit-y += tests/test-crypto-hmac$(EXESUF) check-unit-y += tests/test-crypto-cipher$(EXESUF) check-speed-y += tests/benchmark-crypto-cipher$(EXESUF)
@@ -635,6 +636,7 @@ 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-bitcnt$(EXESUF): tests/test-bitcnt.o $(test-util-obj-y) tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-obj-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-obj-y) tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypto-obj-y) tests/benchmark-crypto-cipher$(EXESUF): tests/benchmark-crypto-cipher.o $(test-crypto-obj-y)
tests/benchmark-crypto-hash.c new file +67 -0
67 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v6 18/18 tests: crypto: add hmac speed benchmark support
In reply to [email protected]From: "Longpeng(Mike)" <[email protected]>
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.
--- tests/Makefile.include | 2 ++ tests/benchmark-crypto-hmac.c | 82 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/benchmark-crypto-hmac.c
tests/Makefile.include +2 -0
@@ -99,6 +99,7 @@ gcov-files-test-write-threshold-y = block/write-threshold.c check-unit-y += tests/test-crypto-hash$(EXESUF) check-speed-y += tests/benchmark-crypto-hash$(EXESUF) check-unit-y += tests/test-crypto-hmac$(EXESUF) +check-speed-y += tests/benchmark-crypto-hmac$(EXESUF) check-unit-y += tests/test-crypto-cipher$(EXESUF) check-speed-y += tests/benchmark-crypto-cipher$(EXESUF) check-unit-y += 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-obj-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-obj-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-crypto-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-crypto-obj-y)
tests/benchmark-crypto-hmac.c new file +82 -0
82 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Quoted 162 lines · On Fri, Jul 14, 2017 at 02:03:53PM -0400, [email protected] wrote:
From: "Longpeng(Mike)" <[email protected]>
The
AF_ALGsocket family is the userspace interface for linux crypto API, users can use it to access hardware accelerators.This patchset adds a afalg-backend for qemu crypto subsystem. QEMU would try to use afalg-backend first if configures '--enable-crypto -afalg' and hostos support
AF_ALG.I measured the performance about the afalg-backend impls, I tested how many data could be encrypted in 5 seconds.
NOTE: In some scenarios library-backend using CPU insns for crypto
would be faster.test-environment: Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
*sha256*
chunk_size(bytes) MB/sec(afalg:sha256-ssse3) MB/sec(nettle) 512 93.03 185.87 1024 146.32 201.78 2048 213.32 210.93 4096 275.48 215.26 8192 321.77 217.49 16384 349.60 219.26 32768 363.59 219.73 65536 375.79 219.99*hmac(sha256)*
chunk_size(bytes) MB/sec(afalg:sha256-ssse3) MB/sec(nettle) 512 71.26 165.55 1024 117.43 189.15 2048 180.96 203.24 4096 247.60 211.38 8192 301.99 215.65 16384 340.79 218.22 32768 365.51 219.49 65536 377.92 220.24*cbc(aes128)*
chunk_size(bytes) MB/sec(afalg:cbc-aes-aesni) MB/sec(nettle) 512 371.76 188.41 1024 559.86 189.64 2048 768.66 192.11 4096 939.15 192.40 8192 1029.48 192.49 16384 1072.79 190.52 32768 1109.38 190.41 65536 1102.38 190.40---
Changes since v5:
- use afalg-backend first only when configured with '--enable-crypto-afalg' andAF_ALGis supportted. [Daniel]Changes since v4:
- remove 'name' field in 'struct CryptoAFAlg'. [Daniel]
- add error handling forread()returning less than requested. [Daniel]
- useiov_send_recvto recv msg inhash-afalg.c. [Daniel]
- refactor hmac benchmark as suggestion. [Daniel]Changes since v3:
- add "Reviewed-by: Daniel P. Berrange <address@hidden>" in commit messages of PATCH 1/2/3/4/5/7/8/9/10/11.
- PATCH 12: usestrlen()instead ofqemu_strnlen()inqcrypto_afalg_build_saddr(). [Daniel]
- PATCH 12: rather than indenting the entire method, just return immediately if afalg=NULL. [Daniel]
- PATCH 13: useg_strdup_printf()instead of g_new0+snprintf() and remove redundant bounds check inqcrypto_afalg_cipher_format_name().
[Daniel]
- PATCH 13: s/except_niv/expect_niv s/origin_contorllen/origin_controllen.
[Daniel]
- PATCH 13: use '%zu' to print 'size_t' inqcrypto_afalg_cipher_setiv().
[Daniel]
- PATCH 13: removeqcrypto_cipher_using_afalg_drv(). [Daniel]
- PATCH 13: refactor theqcrypto_cipher_new()as Daniel's suggestion.
[Daniel]
- PATCH 13: correct the ->cmsg initialization intqcrypto_afalg_cipher_ctx_new()to
avoid different behaviour intest_cipher_null_iv(). [Daniel]
- PATCH 14: useg_strdup_printf()instead of g_new0+snprintf() and remove edundant bounds check inqcrypto_afalg_hash_format_name().
[Daniel]
- PATCH 14: s/except_len/expect_len. [Daniel]
- PATCH 14: free 'errp' ifafalg_driver.hash_bytesv()failed. [Daniel]
- PATCH 14: maybe some afalg errors should be treated as fatal, but we have no idea yet, so add a "TODO" comment.
- PATCH 15: refactor theqcrypto_hmac_new()as Daniel's suggestion. [Daniel]Changes since v2:
- initsockaddr_algobject when it's defined. [Gonglei]
- fix some superfluous initialization. [Gonglei]
- s/opeartion/operation/g incrypto/afalgpriv.h. [Gonglei]
- check 'niv' inqcrypto_afalg_cipher_setiv. [Gonglei]Changes since v1:
- use "make check-speed" to testing the performance. [Daniel]
- put private definations intocrypto/***priv.h. [Daniel]
- remove afalg socket from qapi-schema, put them intocrypto/. [Daniel]
- some Error report change. [Daniel]
- s/QCryptoAfalg/QCryptoAFAlg. [Daniel]
- use snprintf with bounds checking instead of sprintf. [Daniel]
- use "qcrypto_afalg_" prefix and "qcrypto_nettle(gcrypt,glib,builtin)_"
prefix. [Daniel]
- add testing results in cover-letter. [Gonglei]---
Longpeng(Mike) (18):
crypto: cipher: introduce context free function
crypto: cipher: introduceqcrypto_cipher_ctx_newfor gcrypt-backend
crypto: cipher: introduceqcrypto_cipher_ctx_newfor nettle-backend
crypto: cipher: introduceqcrypto_cipher_ctx_newfor builtin-backend
crypto: cipher: add cipher driver framework
crypto: hash: add hash driver framework
crypto: hmac: movecrypto/hmac.hintoinclude/crypto/
crypto: hmac: introduceqcrypto_hmac_ctx_newfor gcrypt-backend
crypto: hmac: introduceqcrypto_hmac_ctx_newfor nettle-backend
crypto: hmac: introduceqcrypto_hmac_ctx_newfor glib-backend
crypto: hmac: add hmac driver framework
crypto: introduce some common functions foraf_algbackend
crypto: cipher: add afalg-backend cipher support
crypto: hash: add afalg-backend hash support
crypto: hmac: add af_alg-backend hmac support
tests: crypto: add cipher speed benchmark support
tests: crypto: add hash speed benchmark support
tests: crypto: add hmac speed benchmark supportconfigure | 30 ++++++ crypto/Makefile.objs | 3 + crypto/afalg.c | 116 +++++++++++++++++++++ crypto/afalgpriv.h | 64 ++++++++++++ crypto/cipher-afalg.c | 226 ++++++++++++++++++++++++++++++++++++++++ crypto/cipher-builtin.c | 125 +++++++++++----------- crypto/cipher-gcrypt.c | 105 ++++++++++--------- crypto/cipher-nettle.c | 84 ++++++++------- crypto/cipher.c | 80 ++++++++++++++ crypto/cipherpriv.h | 56 ++++++++++ crypto/hash-afalg.c | 214 +++++++++++++++++++++++++++++++++++++ crypto/hash-gcrypt.c | 19 ++-- crypto/hash-glib.c | 19 ++-- crypto/hash-nettle.c | 19 ++-- crypto/hash.c | 30 ++++++ crypto/hashpriv.h | 39 +++++++ crypto/hmac-gcrypt.c | 42 ++++---- crypto/hmac-glib.c | 63 ++++++----- crypto/hmac-nettle.c | 42 ++++---- crypto/hmac.c | 58 +++++++++++ crypto/hmac.h | 166 ----------------------------- crypto/hmacpriv.h | 48 +++++++++ include/crypto/cipher.h | 1 + include/crypto/hmac.h | 167 +++++++++++++++++++++++++++++ tests/Makefile.include | 13 ++- tests/benchmark-crypto-cipher.c | 88 ++++++++++++++++ tests/benchmark-crypto-hash.c | 67 ++++++++++++ tests/benchmark-crypto-hmac.c | 82 +++++++++++++++ 28 files changed, 1655 insertions(+), 411 deletions(-) create mode 100644 crypto/afalg.c create mode 100644 crypto/afalgpriv.h create mode 100644 crypto/cipher-afalg.c create mode 100644 crypto/cipherpriv.h
Quoted 8 lines
create mode 100644 crypto/hash-afalg.c create mode 100644 crypto/hashpriv.h delete mode 100644 crypto/hmac.h create mode 100644 crypto/hmacpriv.h create mode 100644 include/crypto/hmac.h create mode 100644 tests/benchmark-crypto-cipher.c create mode 100644 tests/benchmark-crypto-hash.c create mode 100644 tests/benchmark-crypto-hmac.c
Queued for merge.
Thanks for your contribution to QEMU
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|