crypto: hash: add af_alg hash support
PATCH 16/19 crypto: hash: add af_alg hash support
Adds afalg-backend hash support: introduces some private APIs firstly, and then intergrates them into qcrypto_hash_afalg_driver.
Signed-off-by Longpeng(Mike) <[email protected]>
--- crypto/Makefile.objs | 1 + crypto/hash-afalg.c | 150 ++++++++++++++++++++++++++++++++++++++++++++ crypto/hash.c | 16 ++++- include/crypto/afalg-comm.h | 1 + include/crypto/hash.h | 1 + 5 files changed, 166 insertions(+), 3 deletions(-) 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-comm.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/hash-afalg.c new file +150 -0
150 lines not on this page · raw message
crypto/hash.c +13 -3
@@ -45,9 +45,19 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,size_t *resultlen, Error **errp) { - return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov, -result ,resultlen , -errp ); + int ret; + + ret = qcrypto_hash_afalg_driver.hash_bytesv (alg, iov, niov, + result, resultlen, + errp); +if (ret ==0 ) { +return ret; + } + + ret = qcrypto_hash_lib_driver.hash_bytesv (alg, iov, niov, + result, resultlen, + errp); +return ret; }
include/crypto/afalg-comm.h +1 -0
@@ -20,6 +20,7 @@#endif #define ALG_TYPE_CIPHER "skcipher" +#define ALG_TYPE_HASH "hash"#define ALG_OPTYPE_LEN 4#define ALG_MSGIV_LEN (len) (sizeof(struct af_alg_iv) + (len))
include/crypto/hash.h +1 -0
@@ -36,6 +36,7 @@ struct QCryptoHashDriver { };extern QCryptoHashDriver qcrypto_hash_lib_driver; +extern QCryptoHashDriver qcrypto_hash_afalg_driver;/** * qcrypto_hash_supports:
Signature · 1 line
1.8.3.1
Quoted 38 lines · Longpeng(Mike)
Adds afalg-backend hash support: introduces some private APIs firstly, and then intergrates them into
qcrypto_hash_afalg_driver.Signed-off-by Longpeng(Mike) <[email protected]>--- crypto/Makefile.objs | 1 + crypto/hash-afalg.c | 150 ++++++++++++++++++++++++++++++++++++++++++++ crypto/hash.c | 16 ++++- include/crypto/afalg-comm.h | 1 + include/crypto/hash.h | 1 + 5 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 crypto/hash-afalg.ccrypto/hash-afalg.c new file +24
-0+static int afalg_hash_format_name (QCryptoHashAlgorithm alg, +AfalgSocketAddress *afalg) +{ +const char *alg_name =NULL ; + +switch (alg) { +case QCRYPTO_HASH_ALG_MD5 : + alg_name ="md5" ; +break ; +case QCRYPTO_HASH_ALG_SHA1 : + alg_name ="sha1" ; +break ; +case QCRYPTO_HASH_ALG_SHA224 : + alg_name ="sha224" ; +break ; +case QCRYPTO_HASH_ALG_SHA256 : + alg_name ="sha256" ; +break ; +case QCRYPTO_HASH_ALG_SHA384 : + alg_name ="sha384" ; +break ; +case QCRYPTO_HASH_ALG_SHA512 : + alg_name ="sha512" ; +break ;
crypto/hash-afalg.c line 55
Quoted 10 lines
crypto/hash-afalg.c new file +10
-0+case QCRYPTO_HASH_ALG_RIPEMD160 : + alg_name ="rmd160" ; +break ; + +default : +return -1 ; + } + + afalg->name = (char *)g_new0 (int8_t,SALG_NAME_LEN_MAX ); +sprintf (afalg->name,"%s" , alg_name);
Another printf without any bounds checking.
Quoted 54 lines
crypto/hash-afalg.c new file +54
-0+ +return 0 ; +} + +static QCryptoAfalg *afalg_hash_ctx_new (QCryptoHashAlgorithm alg) +{ +SocketAddress *saddr =NULL ; +QCryptoAfalg *afalg =NULL ; +int ret =0 ; + + saddr =g_new0 (SocketAddress,1 ); + saddr->u.afalg.data =g_new0 (AfalgSocketAddress,1 ); + saddr->type =SOCKET_ADDRESS_KIND_AFALG ; + ret =afalg_hash_format_name (alg, saddr->u.afalg.data); +if (ret !=0 ) { + goto error; + } +afalg_comm_format_type (saddr->u.afalg.data,ALG_TYPE_HASH ); + + afalg =afalg_comm_alloc (saddr); +if (!afalg) { + goto error; + } + +/* prepare msg header */ + afalg->msg = g_new0(struct msghdr, 1); + +cleanup: +g_free (saddr->u.afalg.data->type); +g_free (saddr->u.afalg.data->name); +g_free (saddr->u.afalg.data); +g_free (saddr); +return afalg; + +error: +afalg_comm_free (afalg); + afalg =NULL ; + goto cleanup; +} + +static int afalg_hash_bytesv (QCryptoHashAlgorithm alg, +const struct iovec *iov, +size_t niov,uint8_t **result, +size_t *resultlen, +Error **errp) +{ +QCryptoAfalg *afalg =NULL ; +struct iovec outv; +int ret =0 ; +const int except_len =qcrypto_hash_digest_len (alg); + +if (*resultlen ==0 ) { + *resultlen = except_len; + *result =g_new0 (uint8_t, *resultlen);
crypto/hash-afalg.c line 119
Quoted 10 lines
crypto/hash-afalg.c new file +10
-0+ }else if (*resultlen != except_len) { +error_setg (errp, +"Result buffer size %zu is not match hash %d" , + *resultlen, except_len); +return -1 ; + } + + afalg =afalg_hash_ctx_new (alg); +if (afalg ==NULL ) { +error_setg (errp,"Alloc QCryptoAfalg object failed" );
Make afalg_hash_ctx_new() report the error
crypto/hash-afalg.c line 127
Quoted 8 lines
crypto/hash-afalg.c new file +8
-0+return -1 ; + } + +/* send data to kernel's crypto core */ + ret =iov_send_recv (afalg->opfd, iov, niov, +0 ,iov_size (iov, niov), true); +if (ret <0 ) { +error_setg (errp,"Send data to afalg-core failed" );
error_setg_errno()
Quoted 13 lines
crypto/hash-afalg.c new file +13
-0+ goto out; + } + +/* hash && get result */ + outv.iov_base = *result; + outv.iov_len = *resultlen; + afalg->msg->msg_iov = &outv; + afalg->msg->msg_iovlen =1 ; + ret =recvmsg (afalg->opfd, afalg->msg,0 ); +if (ret !=-1 ) { + ret =0 ; + }else { +error_setg (errp,"Recv result from afalg-core failed" );
crypto/hash-afalg.c line 150
Quoted 10 lines
crypto/hash-afalg.c new file +10
-0+ } + +out: +afalg_comm_free (afalg); +return ret; +} + +QCryptoHashDriver qcrypto_hash_afalg_driver = { + .hash_bytesv = afalg_hash_bytesv, +};
All methods should have a qcrypto_afalg_ name prefix.
Quoted 55 lines
crypto/hash.c +13
-3@@ -45,9 +45,19 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,size_t *resultlen, Error **errp) { - return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov, -result ,resultlen , -errp ); + int ret; + + ret = qcrypto_hash_afalg_driver.hash_bytesv (alg, iov, niov, + result, resultlen, + errp); +if (ret ==0 ) { +return ret; + } + + ret = qcrypto_hash_lib_driver.hash_bytesv (alg, iov, niov, + result, resultlen, + errp); +return ret; }include/crypto/afalg-comm.h +1
-0@@ -20,6 +20,7 @@#endif #define ALG_TYPE_CIPHER "skcipher" +#define ALG_TYPE_HASH "hash"#define ALG_OPTYPE_LEN 4#define ALG_MSGIV_LEN (len) (sizeof(struct af_alg_iv) + (len))include/crypto/hash.h +1
-0@@ -36,6 +36,7 @@ struct QCryptoHashDriver { };extern QCryptoHashDriver qcrypto_hash_lib_driver; +extern QCryptoHashDriver qcrypto_hash_afalg_driver;/** * qcrypto_hash_supports: Signature · 1 line
1.8.3.1
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/ :|