hw/misc/aspeed_hace: Fix SG Accumulative Hash Calculations
Subsystems (2)
v2 cover hw/misc/aspeed_hace: Fix SG Accumulative Hash Calculations
The goal of this patch series is to fix accumulative hashing support in the Aspeed HACE module. The issue that stemmed this patch was a failure to boot an OpenBMC image using the "ast2600-evb" machine. The U-boot
2019.04 loader failed to verify image hashes.
These incorrect image hashes given by the HACE to the U-boot guest are due to an oversight in the HACE module. Previously when operating in scatter-gather accumulative mode, the HACE would cache the address provided by the guest which contained the source data. However, there was no deep copy, so when HACE generated the digest upon the reception of the final accumulative chunk the digest was incorrect, as the addresses provided had their regions overwritten by that time.
This fix consists of two main steps:
* Add an accumulative hashing function to the qcrypto library
* Modify the HACE module to use the accumulative hashing functions
All the crypto library backends (nettle, gnutls, etc.) support accumulative hashing, so it was trivial to create wrappers for those functions.
Changes in V2:
* Fixed error checking bug in libgcrypt crypto backend of accumulate_bytesv
Alejandro Zeise (2):
crypto: add support for accumulative hashing hw/misc/aspeed_hace: Fix SG Accumulative hashing
crypto/hash-gcrypt.c | 105 ++++++++++++++++++++++++++++++++++ crypto/hash-glib.c | 89 ++++++++++++++++++++++++++++ crypto/hash-gnutls.c | 82 ++++++++++++++++++++++++++ crypto/hash-nettle.c | 93 ++++++++++++++++++++++++++++++ crypto/hash.c | 42 ++++++++++++++ crypto/hashpriv.h | 11 ++++ hw/misc/aspeed_hace.c | 91 +++++++++++++++-------------- include/crypto/hash.h | 65 +++++++++++++++++++++ include/hw/misc/aspeed_hace.h | 4 ++ 9 files changed, 538 insertions(+), 44 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/2 crypto: add support for accumulative hashing
This change adds an accumulative hashing function (qcrypto_hash_accumulate_bytesv) and implementation for each of the crypto library backends that QEMU supports.
The QCrypto API did not support hashing in an accumulative mode. As such, hardware hash modules (like the HACE from Aspeed's SoCs) are unable to perform such operations correctly when the guest requires it.
The creation and freeing of each library's context is abstracted by the qcrypto_hash_accumulate_new_ctx and qcrypto_hash_accumulate_free_ctx functions.
Changes in V2:
* Fixed error checking bug in libgcrypt backend
--- crypto/hash-gcrypt.c | 105 ++++++++++++++++++++++++++++++++++++++++++ crypto/hash-glib.c | 89 +++++++++++++++++++++++++++++++++++ crypto/hash-gnutls.c | 82 +++++++++++++++++++++++++++++++++ crypto/hash-nettle.c | 93 +++++++++++++++++++++++++++++++++++++ crypto/hash.c | 42 +++++++++++++++++ crypto/hashpriv.h | 11 +++++ include/crypto/hash.h | 65 ++++++++++++++++++++++++++ 7 files changed, 487 insertions(+)
crypto/hash-gcrypt.c +105 -0
@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2016 Red Hat, Inc. * * This library is free software; you can redistribute it and/or
@@ -110,7 +111,111 @@ qcrypto_gcrypt_hash_bytesv(QCryptoHashAlgorithm alg,return -1 ; } +static +int qcrypto_gcrypt_hash_accumulate_new_ctx (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t **accumulate_ctx, +Error **errp) +{ +int ret; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + + ret =gcry_md_open ((gcry_md_hd_t *) accumulate_ctx, qcrypto_hash_alg_map[alg],0 ); + +if (ret <0 ) { +error_setg (errp, +"Unable to initialize hash algorithm: %s" , +gcry_strerror (ret)); +return -1 ; + } + +return 0 ; +} + +static +int qcrypto_gcrypt_hash_accumulate_free_ctx (qcrypto_hash_accumulate_ctx_t *hash_ctx, +Error **errp) +{ +if (hash_ctx !=NULL ) { +gcry_md_close ((gcry_md_hd_t ) hash_ctx); + } + +return 0 ; +} + +static +int qcrypto_gcrypt_hash_accumulate_bytesv (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t *accumulate_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +int i, ret; +gcry_md_hd_t ctx_copy; +unsigned char *digest; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + +for (i =0 ; i < niov; i++) { +gcry_md_write ((gcry_md_hd_t ) accumulate_ctx, iov[i].iov_base, iov[i].iov_len); + } + + ret =gcry_md_get_algo_dlen (qcrypto_hash_alg_map[alg]); +if (ret <=0 ) { +error_setg (errp, +"Unable to get hash 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 hash %d" , + *resultlen, ret); +return -1 ; + } + +/* +* Make a copy so we don't distort the main context +* by calculating the intermediate hash +*/ + ret =gcry_md_copy (&ctx_copy, (gcry_md_hd_t ) accumulate_ctx); +if (ret) { +error_setg (errp,"Unable to make copy: %s" ,gcry_strerror (ret)); +return -1 ; + } + + digest =gcry_md_read (ctx_copy,0 ); +if (!digest) { +error_setg (errp, +"No digest produced" ); +return -1 ; + } +memcpy (*result, digest, *resultlen); +gcry_md_close (ctx_copy); + +return 0 ; +} +QCryptoHashDriver qcrypto_hash_lib_driver = { .hash_bytesv = qcrypto_gcrypt_hash_bytesv, + .hash_accumulate_bytesv = qcrypto_gcrypt_hash_accumulate_bytesv, + .accumulate_new_ctx = qcrypto_gcrypt_hash_accumulate_new_ctx, + .accumulate_free_ctx = qcrypto_gcrypt_hash_accumulate_free_ctx, };
crypto/hash-glib.c +89 -0
101 lines not on this page · raw message
crypto/hash-gnutls.c +82 -0
94 lines not on this page · raw message
crypto/hash-nettle.c +93 -0
106 lines not on this page · raw message
crypto/hash.c +42 -0
54 lines not on this page · raw message
crypto/hashpriv.h +11 -0
@@ -1,6 +1,7 @@/* * QEMU Crypto hash driver supports * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. * * Authors:
@@ -24,6 +25,16 @@ struct QCryptoHashDriver {uint8_t **result, size_t *resultlen, Error **errp); +int (*hash_accumulate_bytesv)(QCryptoHashAlgorithm alg, +void *hash_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp); +int (*accumulate_new_ctx)(QCryptoHashAlgorithm alg,void **hash_ctx, +Error **errp); +int (*accumulate_free_ctx)(void *hash_ctx,Error **errp); };extern QCryptoHashDriver qcrypto_hash_lib_driver;
include/crypto/hash.h +65 -0
83 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 1/2] crypto: add support for accumulative hashing
Quoted 571 lines · Alejandro Zeise
This change adds an accumulative hashing function (
qcrypto_hash_accumulate_bytesv) and implementation for each of the crypto library backends that QEMU supports.The QCrypto API did not support hashing in an accumulative mode. As such, hardware hash modules (like the HACE from Aspeed's SoCs) are unable to perform such operations correctly when the guest requires it.
The creation and freeing of each library's context is abstracted by the
qcrypto_hash_accumulate_new_ctxandqcrypto_hash_accumulate_free_ctxfunctions.Changes in V2:
* Fixed error checking bug in libgcrypt backendSigned-off-by Alejandro Zeise <[email protected]>--- crypto/hash-gcrypt.c | 105 ++++++++++++++++++++++++++++++++++++++++++ crypto/hash-glib.c | 89 +++++++++++++++++++++++++++++++++++ crypto/hash-gnutls.c | 82 +++++++++++++++++++++++++++++++++ crypto/hash-nettle.c | 93 +++++++++++++++++++++++++++++++++++++ crypto/hash.c | 42 +++++++++++++++++ crypto/hashpriv.h | 11 +++++ include/crypto/hash.h | 65 ++++++++++++++++++++++++++ 7 files changed, 487 insertions(+)crypto/hash-gcrypt.c +105
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2016 Red Hat, Inc. * * This library is free software; you can redistribute it and/or @@ -110,7 +111,111 @@ qcrypto_gcrypt_hash_bytesv(QCryptoHashAlgorithm alg,return -1 ; } +static +int qcrypto_gcrypt_hash_accumulate_new_ctx (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t **accumulate_ctx, +Error **errp) +{ +int ret; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + + ret =gcry_md_open ((gcry_md_hd_t *) accumulate_ctx, qcrypto_hash_alg_map[alg],0 ); + +if (ret <0 ) { +error_setg (errp, +"Unable to initialize hash algorithm: %s" , +gcry_strerror (ret)); +return -1 ; + } + +return 0 ; +} + +static +int qcrypto_gcrypt_hash_accumulate_free_ctx (qcrypto_hash_accumulate_ctx_t *hash_ctx, +Error **errp) +{ +if (hash_ctx !=NULL ) { +gcry_md_close ((gcry_md_hd_t ) hash_ctx); + } + +return 0 ; +} + +static +int qcrypto_gcrypt_hash_accumulate_bytesv (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t *accumulate_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +int i, ret; +gcry_md_hd_t ctx_copy; +unsigned char *digest; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + +for (i =0 ; i < niov; i++) { +gcry_md_write ((gcry_md_hd_t ) accumulate_ctx, iov[i].iov_base, iov[i].iov_len); + } + + ret =gcry_md_get_algo_dlen (qcrypto_hash_alg_map[alg]); +if (ret <=0 ) { +error_setg (errp, +"Unable to get hash 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 hash %d" , + *resultlen, ret); +return -1 ; + } + +/* +* Make a copy so we don't distort the main context +* by calculating the intermediate hash +*/ + ret =gcry_md_copy (&ctx_copy, (gcry_md_hd_t ) accumulate_ctx); +if (ret) { +error_setg (errp,"Unable to make copy: %s" ,gcry_strerror (ret)); +return -1 ; + } + + digest =gcry_md_read (ctx_copy,0 ); +if (!digest) { +error_setg (errp, +"No digest produced" ); +return -1 ; + } +memcpy (*result, digest, *resultlen); +gcry_md_close (ctx_copy); + +return 0 ; +} +QCryptoHashDriver qcrypto_hash_lib_driver = { .hash_bytesv = qcrypto_gcrypt_hash_bytesv, + .hash_accumulate_bytesv = qcrypto_gcrypt_hash_accumulate_bytesv, + .accumulate_new_ctx = qcrypto_gcrypt_hash_accumulate_new_ctx, + .accumulate_free_ctx = qcrypto_gcrypt_hash_accumulate_free_ctx, };crypto/hash-glib.c +89
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2016 Red Hat, Inc. * * This library is free software; you can redistribute it and/or @@ -95,6 +96,94 @@ qcrypto_glib_hash_bytesv(QCryptoHashAlgorithm alg, } +static +int qcrypto_glib_hash_accumulate_new_ctx (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t **accumulate_ctx, +Error **errp) +{ +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + + *accumulate_ctx =g_checksum_new (qcrypto_hash_alg_map[alg]); + +return 0 ; +} + +static +int qcrypto_glib_hash_accumulate_free_ctx (qcrypto_hash_accumulate_ctx_t *hash_ctx, +Error **errp) +{ +if (hash_ctx !=NULL ) { +g_checksum_free ((GChecksum *) hash_ctx); + } + +return 0 ; +} + + +static +int qcrypto_glib_hash_accumulate_bytesv (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t *accumulate_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +int i, ret; +GChecksum *ctx_copy; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + +for (i =0 ; i < niov; i++) { +g_checksum_update ((GChecksum *) accumulate_ctx, iov[i].iov_base, iov[i].iov_len); + } + + ret =g_checksum_type_get_length (qcrypto_hash_alg_map[alg]); +if (ret <0 ) { +error_setg (errp,"%s" , +"Unable to get hash 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 hash %d" , + *resultlen, ret); +return -1 ; + } + +/* +Make a copy so we don't distort the main context +by calculating the intermediate hash. +*/ + ctx_copy =g_checksum_copy ((GChecksum *) accumulate_ctx); +if (ctx_copy ==NULL ) { +error_setg (errp,"Unable to make copy: %s" , __func__); +return -1 ; + } + +g_checksum_get_digest ((GChecksum *) ctx_copy, *result, resultlen); +g_checksum_free (ctx_copy); + +return 0 ; +} + +QCryptoHashDriver qcrypto_hash_lib_driver = { .hash_bytesv = qcrypto_glib_hash_bytesv, + .hash_accumulate_bytesv = qcrypto_glib_hash_accumulate_bytesv, + .accumulate_new_ctx = qcrypto_glib_hash_accumulate_new_ctx, + .accumulate_free_ctx = qcrypto_glib_hash_accumulate_free_ctx, };crypto/hash-gnutls.c +82
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2021 Red Hat, Inc. * * This library is free software; you can redistribute it and/or @@ -99,6 +100,87 @@ qcrypto_gnutls_hash_bytesv(QCryptoHashAlgorithm alg, } +static +int qcrypto_gnutls_hash_accumulate_new_ctx (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t **hash_ctx, +Error **errp) +{ +int ret; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + + ret =gnutls_hash_init ((gnutls_hash_hd_t *) hash_ctx, qcrypto_hash_alg_map[alg]); +if (ret <0 ) { +error_setg (errp, +"Unable to initialize hash algorithm: %s" , +gnutls_strerror (ret)); +return -1 ; + } + +return 0 ; +} + +static +int qcrypto_gnutls_hash_accumulate_free_ctx (qcrypto_hash_accumulate_ctx_t *hash_ctx, +Error **errp) +{ +if (hash_ctx !=NULL ) { +gnutls_hash_deinit ((gnutls_hash_hd_t ) hash_ctx,NULL ); + } + +return 0 ; +} + +static +int qcrypto_gnutls_hash_accumulate_bytesv (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t *hash_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +int i, ret; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + + ret =gnutls_hash_get_len (qcrypto_hash_alg_map[alg]); +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 hash %d" , + *resultlen, ret); +return -1 ; + } + +for (i =0 ; i < niov; i++) { +gnutls_hash ((gnutls_hash_hd_t ) hash_ctx, + iov[i].iov_base, iov[i].iov_len); + } + +/* Make a copy so we don't distort the main context */ +gnutls_hash_hd_t copy =gnutls_hash_copy ((gnutls_hash_hd_t ) hash_ctx); +gnutls_hash_deinit (copy, *result); + +return 0 ; +} + +QCryptoHashDriver qcrypto_hash_lib_driver = { .hash_bytesv = qcrypto_gnutls_hash_bytesv, + .hash_accumulate_bytesv = qcrypto_gnutls_hash_accumulate_bytesv, + .accumulate_new_ctx = qcrypto_gnutls_hash_accumulate_new_ctx, + .accumulate_free_ctx = qcrypto_gnutls_hash_accumulate_free_ctx, };crypto/hash-nettle.c +93
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2016 Red Hat, Inc. * * This library is free software; you can redistribute it and/or @@ -155,7 +156,99 @@ qcrypto_nettle_hash_bytesv(QCryptoHashAlgorithm alg,return 0 ; } +static +int qcrypto_nettle_hash_accumulate_new_ctx (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t **hash_ctx, +Error **errp) +{ +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + + *((union qcrypto_hash_ctx **) hash_ctx) =g_malloc (sizeof (union qcrypto_hash_ctx )); + + qcrypto_hash_alg_map[alg].init (*((union qcrypto_hash_ctx **) accumulate_ctx)); + +return 0 ; +} + +static +int qcrypto_nettle_hash_accumulate_free_ctx (qcrypto_hash_accumulate_ctx_t *hash_ctx, +Error **errp) +{ +g_free ((union qcrypto_hash_ctx *) hash_ctx); +return 0 ; +} + +static +int qcrypto_nettle_hash_accumulate_bytesv (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t *hash_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +union qcrypto_hash_ctx *ctx_copy; +int i; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + +for (i =0 ; i < niov; i++) { +/* Some versions of nettle have functions +* declared with 'int' instead of 'size_t' +* so to be safe avoid writing more than +* UINT_MAX bytes at a time +*/ +size_t len = iov[i].iov_len; +uint8_t *base = iov[i].iov_base; +while (len) { +size_t shortlen =MIN (len,UINT_MAX ); + qcrypto_hash_alg_map[alg].write ((union qcrypto_hash_ctx *) hash_ctx, + len, base); + len -= shortlen; + base += len; + } + } + +if (*resultlen ==0 ) { + *resultlen = qcrypto_hash_alg_map[alg].len; + *result =g_new0 (uint8_t, *resultlen); + }else if (*resultlen != qcrypto_hash_alg_map[alg].len) { +error_setg (errp, +"Result buffer size %zu is smaller than hash %zu" , + *resultlen, qcrypto_hash_alg_map[alg].len); +return -1 ; + } + +/* +Make a copy so we don't distort the main context +by calculating the intermediate hash +*/ + ctx_copy = g_new(union qcrypto_hash_ctx, 1); +memcpy (ctx_copy, (union qcrypto_hash_ctx *) hash_ctx, +sizeof (union qcrypto_hash_ctx )); + + qcrypto_hash_alg_map[alg].result (ctx_copy, + *resultlen, *result); + +g_free (ctx_copy); + +return 0 ; +} +QCryptoHashDriver qcrypto_hash_lib_driver = { .hash_bytesv = qcrypto_nettle_hash_bytesv, + .hash_accumulate_bytesv = qcrypto_nettle_hash_accumulate_bytesv, + .accumulate_new_ctx = qcrypto_nettle_hash_accumulate_new_ctx, + .accumulate_free_ctx = qcrypto_nettle_hash_accumulate_free_ctx, };crypto/hash.c +42
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2015 Red Hat, Inc. * * This library is free software; you can redistribute it and/or @@ -77,6 +78,47 @@ int qcrypto_hash_bytes(QCryptoHashAlgorithm alg,return qcrypto_hash_bytesv (alg, &iov,1 , result, resultlen, errp); } +int qcrypto_hash_accumulate_bytesv (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t *accumulate_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +#ifdef CONFIG_AF_ALG +qemu_log_mask (LOG_UNIMP ,"%s: AF_ALG support unimplemented.\n" , __func__); +return 1 ; +#else +return qcrypto_hash_lib_driver.hash_accumulate_bytesv (alg, accumulate_ctx, + iov, niov, result, + resultlen,NULL ); +#endif /* CONFIG_AF_ALG */ +} + +int qcrypto_hash_accumulate_new_ctx (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t **accumulate_ctx, +Error **errp) +{ +#ifdef CONFIG_AF_ALG +qemu_log_mask (LOG_UNIMP ,"%s: AF_ALG support unimplemented.\n" , __func__); +return 1 ; +#else +return qcrypto_hash_lib_driver.accumulate_new_ctx (alg, accumulate_ctx, errp); +#endif /* CONFIG_AF_ALG */ +} + +int qcrypto_hash_accumulate_free_ctx (qcrypto_hash_accumulate_ctx_t *accumulate_ctx, +Error **errp) +{ +#ifdef CONFIG_AF_ALG +qemu_log_mask (LOG_UNIMP ,"%s: AF_ALG support unimplemented.\n" , __func__); +return 1 ; +#else +return qcrypto_hash_lib_driver.accumulate_free_ctx (accumulate_ctx, errp); +#endif /* CONFIG_AF_ALG */ +} +static const char hex[] ="0123456789abcdef" ; int qcrypto_hash_digestv(QCryptoHashAlgorithm alg,crypto/hashpriv.h +11
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash driver supports * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. * * Authors: @@ -24,6 +25,16 @@ struct QCryptoHashDriver {uint8_t **result, size_t *resultlen, Error **errp); +int (*hash_accumulate_bytesv)(QCryptoHashAlgorithm alg, +void *hash_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp); +int (*accumulate_new_ctx)(QCryptoHashAlgorithm alg,void **hash_ctx, +Error **errp); +int (*accumulate_free_ctx)(void *hash_ctx,Error **errp); };extern QCryptoHashDriver qcrypto_hash_lib_driver;include/crypto/hash.h +1
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2015 Red Hat, Inc. * * This library is free software; you can redistribute it and/or
include/crypto/hash.h line 29
Quoted 4 lines
include/crypto/hash.h +1
-0@@ -25,6 +26,8 @@/* See also "QCryptoHashAlgorithm" defined in qapi/crypto.json */ +typedef void qcrypto_hash_accumulate_ctx_t ;
QCryptoHmac defines a similar API. I would do the same for the hash accumulative mode. See hmac files.
Thanks,
C.
Quoted 73 lines
include/crypto/hash.h +63
-0+/** * qcrypto_hash_supports: * @alg: the hash algorithm @@ -120,6 +123,68 @@ int qcrypto_hash_digestv(QCryptoHashAlgorithm alg,char **digest, Error **errp); +/** +* qcrypto_hash_accumulate_bytesv: +* @alg: the hash algorithm +* @accumulate_ctx: pointer to the algorithm's context for further hash operations +* @iov: the array of memory regions to hash +* @niov: the length of @iov +* @result: pointer to hold output hash +* @resultlen: pointer to hold length of @result +* @errp: pointer to a NULL-initialized error object +* +* Computes the hash across all the memory regions +* present in @iov using the existing hash context +* given in @accumulate_ctx. The @result pointer will be +* filled with raw bytes representing the computed +* hash, 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_hash_accumulate_bytesv (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t *accumulate_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp); + +/** +* qcrypto_hash_accumulate_new_ctx: +* @alg: the hash algorithm +* @accumulate_ctx: pointer to the pointer holding the algorithm's +* context for further hash operations. The pointer will +* be modified to point to the memory this function +* allocates to hold the context. +* @errp: pointer to a NULL-initialized error object +* +* Creates a new hashing context for the chosen algorithm for +* usage with qcrypto_hash_accumulate_bytesv(). +* This is useful for when one has multiple inputs to compute a hash, but +* not all are available at a single point in time, making qcrypto_hash_bytesv() +* inadequate. The @accumulate_ctx pointer must be released with a call to +* qcrypto_hash_accumulate_free_ctx() once all hash operations are complete. +* +* Returns: 0 on success, -1 on error +*/ +int qcrypto_hash_accumulate_new_ctx (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t **accumulate_ctx, +Error **errp); + +/** +* qcrypto_hash_accumulate_free_ctx: +* @accumulate_ctx: pointer to the algorithm's context for further hash operations +* @errp: pointer to a NULL-initialized error object +* +* frees a hashing context for the chosen algorithm. +* +* Returns: 0 on success, -1 on error +*/ +int qcrypto_hash_accumulate_free_ctx (qcrypto_hash_accumulate_ctx_t *accumulate_ctx, +Error **errp); +/** * qcrypto_hash_digest: * @alg: the hash algorithm
Re: [PATCH v2 1/2] crypto: add support for accumulative hashing
Quoted 38 lines · Cédric Le Goater
Quoted 37 lines · Alejandro Zeise
This change adds an accumulative hashing function (
qcrypto_hash_accumulate_bytesv) and implementation for each of the crypto library backends that QEMU supports.The QCrypto API did not support hashing in an accumulative mode. As such, hardware hash modules (like the HACE from Aspeed's SoCs) are unable to perform such operations correctly when the guest requires it.
The creation and freeing of each library's context is abstracted by the
qcrypto_hash_accumulate_new_ctxandqcrypto_hash_accumulate_free_ctxfunctions.Changes in V2:
* Fixed error checking bug in libgcrypt backendSigned-off-by Alejandro Zeise <[email protected]>--- crypto/hash-gcrypt.c | 105 ++++++++++++++++++++++++++++++++++++++++++ crypto/hash-glib.c | 89 +++++++++++++++++++++++++++++++++++ crypto/hash-gnutls.c | 82 +++++++++++++++++++++++++++++++++ crypto/hash-nettle.c | 93 +++++++++++++++++++++++++++++++++++++ crypto/hash.c | 42 +++++++++++++++++ crypto/hashpriv.h | 11 +++++ include/crypto/hash.h | 65 ++++++++++++++++++++++++++ 7 files changed, 487 insertions(+)include/crypto/hash.h +1
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2015 Red Hat, Inc. *
Quoted 8 lines
include/crypto/hash.h line 27
Quoted 4 lines
include/crypto/hash.h +1
-0* This library is free software; you can redistribute it and/or @@ -25,6 +26,8 @@/* See also "QCryptoHashAlgorithm" defined in qapi/crypto.json */ +typedef void qcrypto_hash_accumulate_ctx_t ;QCryptoHmac defines a similar API. I would do the same for the hash accumulative mode. See hmac files.
Yes, ideally the hmac APIs would match the hash APIs.
That said, the changes I've just requested are already a large amount of work, so I'm not going to demand that the hmac.c code is also changed.
Alejandro: if have you time to update hmac.c that would be awesome, but consider it optional.
With 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 :|
Re: [PATCH v2 1/2] crypto: add support for accumulative hashing
In reply to Alejandro ZeiseQuoted 244 lines · Alejandro Zeise
This change adds an accumulative hashing function (
qcrypto_hash_accumulate_bytesv) and implementation for each of the crypto library backends that QEMU supports.The QCrypto API did not support hashing in an accumulative mode. As such, hardware hash modules (like the HACE from Aspeed's SoCs) are unable to perform such operations correctly when the guest requires it.
The creation and freeing of each library's context is abstracted by the
qcrypto_hash_accumulate_new_ctxandqcrypto_hash_accumulate_free_ctxfunctions.Changes in V2:
* Fixed error checking bug in libgcrypt backendSigned-off-by Alejandro Zeise <[email protected]>--- crypto/hash-gcrypt.c | 105 ++++++++++++++++++++++++++++++++++++++++++ crypto/hash-glib.c | 89 +++++++++++++++++++++++++++++++++++ crypto/hash-gnutls.c | 82 +++++++++++++++++++++++++++++++++ crypto/hash-nettle.c | 93 +++++++++++++++++++++++++++++++++++++ crypto/hash.c | 42 +++++++++++++++++ crypto/hashpriv.h | 11 +++++ include/crypto/hash.h | 65 ++++++++++++++++++++++++++ 7 files changed, 487 insertions(+)crypto/hash-gcrypt.c +105
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2016 Red Hat, Inc. * * This library is free software; you can redistribute it and/or @@ -110,7 +111,111 @@ qcrypto_gcrypt_hash_bytesv(QCryptoHashAlgorithm alg,return -1 ; } +static +int qcrypto_gcrypt_hash_accumulate_new_ctx (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t **accumulate_ctx, +Error **errp) +{ +int ret; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + + ret =gcry_md_open ((gcry_md_hd_t *) accumulate_ctx, qcrypto_hash_alg_map[alg],0 ); + +if (ret <0 ) { +error_setg (errp, +"Unable to initialize hash algorithm: %s" , +gcry_strerror (ret)); +return -1 ; + } + +return 0 ; +} + +static +int qcrypto_gcrypt_hash_accumulate_free_ctx (qcrypto_hash_accumulate_ctx_t *hash_ctx, +Error **errp) +{ +if (hash_ctx !=NULL ) { +gcry_md_close ((gcry_md_hd_t ) hash_ctx); + } + +return 0 ; +} + +static +int qcrypto_gcrypt_hash_accumulate_bytesv (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t *accumulate_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +int i, ret; +gcry_md_hd_t ctx_copy; +unsigned char *digest; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + +for (i =0 ; i < niov; i++) { +gcry_md_write ((gcry_md_hd_t ) accumulate_ctx, iov[i].iov_base, iov[i].iov_len); + } + + ret =gcry_md_get_algo_dlen (qcrypto_hash_alg_map[alg]); +if (ret <=0 ) { +error_setg (errp, +"Unable to get hash 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 hash %d" , + *resultlen, ret); +return -1 ; + } + +/* +* Make a copy so we don't distort the main context +* by calculating the intermediate hash +*/ + ret =gcry_md_copy (&ctx_copy, (gcry_md_hd_t ) accumulate_ctx); +if (ret) { +error_setg (errp,"Unable to make copy: %s" ,gcry_strerror (ret)); +return -1 ; + } + + digest =gcry_md_read (ctx_copy,0 ); +if (!digest) { +error_setg (errp, +"No digest produced" ); +return -1 ; + } +memcpy (*result, digest, *resultlen); +gcry_md_close (ctx_copy); + +return 0 ; +} +QCryptoHashDriver qcrypto_hash_lib_driver = { .hash_bytesv = qcrypto_gcrypt_hash_bytesv, + .hash_accumulate_bytesv = qcrypto_gcrypt_hash_accumulate_bytesv, + .accumulate_new_ctx = qcrypto_gcrypt_hash_accumulate_new_ctx, + .accumulate_free_ctx = qcrypto_gcrypt_hash_accumulate_free_ctx, };crypto/hash-glib.c +79
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2016 Red Hat, Inc. * * This library is free software; you can redistribute it and/or @@ -95,6 +96,94 @@ qcrypto_glib_hash_bytesv(QCryptoHashAlgorithm alg, } +static +int qcrypto_glib_hash_accumulate_new_ctx (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t **accumulate_ctx, +Error **errp) +{ +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + + *accumulate_ctx =g_checksum_new (qcrypto_hash_alg_map[alg]); + +return 0 ; +} + +static +int qcrypto_glib_hash_accumulate_free_ctx (qcrypto_hash_accumulate_ctx_t *hash_ctx, +Error **errp) +{ +if (hash_ctx !=NULL ) { +g_checksum_free ((GChecksum *) hash_ctx); + } + +return 0 ; +} + + +static +int qcrypto_glib_hash_accumulate_bytesv (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t *accumulate_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +int i, ret; +GChecksum *ctx_copy; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + +for (i =0 ; i < niov; i++) { +g_checksum_update ((GChecksum *) accumulate_ctx, iov[i].iov_base, iov[i].iov_len); + } + + ret =g_checksum_type_get_length (qcrypto_hash_alg_map[alg]); +if (ret <0 ) { +error_setg (errp,"%s" , +"Unable to get hash 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 hash %d" , + *resultlen, ret); +return -1 ; + } + +/* +Make a copy so we don't distort the main context +by calculating the intermediate hash. +*/ + ctx_copy =g_checksum_copy ((GChecksum *) accumulate_ctx); +if (ctx_copy ==NULL ) { +error_setg (errp,"Unable to make copy: %s" , __func__); +return -1 ; + } +
crypto/hash-glib.c qcrypto_glib_hash_bytesv(QCryptoHashAlgorithm alg, · line 186
Quoted 10 lines
crypto/hash-glib.c +8
-0+g_checksum_get_digest ((GChecksum *) ctx_copy, *result, resultlen); +g_checksum_free (ctx_copy); + +return 0 ; +} + +QCryptoHashDriver qcrypto_hash_lib_driver = { .hash_bytesv = qcrypto_glib_hash_bytesv, + .hash_accumulate_bytesv = qcrypto_glib_hash_accumulate_bytesv,
These two methods are 99% copy+pate of each other which is highly undesirable. To address this we should get rid of the 'hash_bytesv' callback entirely. The existing single shot methods can be implemented entirely in terms of the new methods you are adding.
Quoted 22 lines
crypto/hash-glib.c +2
-0+ .accumulate_new_ctx = qcrypto_glib_hash_accumulate_new_ctx, + .accumulate_free_ctx = qcrypto_glib_hash_accumulate_free_ctx, };crypto/hash.c +3
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2015 Red Hat, Inc. * * This library is free software; you can redistribute it and/or @@ -77,6 +78,47 @@ int qcrypto_hash_bytes(QCryptoHashAlgorithm alg,return qcrypto_hash_bytesv (alg, &iov,1 , result, resultlen, errp); } +int qcrypto_hash_accumulate_bytesv(QCryptoHashAlgorithm alg, + qcrypto_hash_accumulate_ctx_t *accumulate_ctx,
crypto/hash.c int qcrypto_hash_bytes(QCryptoHashAlgorithm alg, · line 92
Quoted 10 lines
crypto/hash.c +10
-0+const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp) +{ +#ifdef CONFIG_AF_ALG + qemu_log_mask(LOG_UNIMP, "%s: AF_ALG support unimplemented.\n", __func__); +return 1 ; +#else
This isn't ok - we need to have this new API design supported by the afalg implementation.
Quoted 17 lines
crypto/hashpriv.h +2
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash driver supports * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. * * Authors: @@ -24,6 +25,16 @@ struct QCryptoHashDriver {uint8_t **result, size_t *resultlen, Error **errp); + int (*hash_accumulate_bytesv)(QCryptoHashAlgorithm alg,
crypto/hashpriv.h struct QCryptoHashDriver { · line 38
Quoted 10 lines
crypto/hashpriv.h +9
-0+void *hash_ctx, + const struct iovec *iov, + size_t niov, + uint8_t **result, + size_t *resultlen, + Error **errp); +int (*accumulate_new_ctx)(QCryptoHashAlgorithm alg,void **hash_ctx, +Error **errp); +int (*accumulate_free_ctx)(void *hash_ctx,Error **errp); };
We should split updating from finalizing, and use QCryptoHash as the type name for the context object. ie we need 4 callbacks in total:
QCryptoHash *(*hash_new)(QCryptoHashAlgorithm alg, Error **errp);
int (*hash_update)(QCryptoHashAlgorithm alg,
const struct iovec *iov,
size_t niov,
Error **errp);
int (*hash_finalize)(QCryptoHashAlgorithm alg,
uint8_t **result,
size_t *resultlen,
Error **errp);
(*hash_free)(QCryptoHash *hash);Quoted 13 lines
include/crypto/hash.h +1
-0@@ -1,6 +1,7 @@/* * QEMU Crypto hash algorithms * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (c) 2015 Red Hat, Inc. * * This library is free software; you can redistribute it and/or
include/crypto/hash.h line 30
Quoted 5 lines
include/crypto/hash.h +2
-0@@ -25,6 +26,8 @@/* See also "QCryptoHashAlgorithm" defined in qapi/crypto.json */ +typedef void qcrypto_hash_accumulate_ctx_t ; +
Style convention for types is initial caps, rather snake case. Also this can be massively shortened. So we get just
typedef struct QCryptoHash QCryptoHash;
Quoted 24 lines
include/crypto/hash.h +17
-0/** * qcrypto_hash_supports: * @alg: the hash algorithm @@ -120,6 +123,68 @@ int qcrypto_hash_digestv(QCryptoHashAlgorithm alg,char **digest, Error **errp); +/** +* qcrypto_hash_accumulate_bytesv: +* @alg: the hash algorithm +* @accumulate_ctx: pointer to the algorithm's context for further hash operations +* @iov: the array of memory regions to hash +* @niov: the length of @iov +* @result: pointer to hold output hash +* @resultlen: pointer to hold length of @result +* @errp: pointer to a NULL-initialized error object +* +* Computes the hash across all the memory regions +* present in @iov using the existing hash context +* given in @accumulate_ctx. The @result pointer will be +* filled with raw bytes representing the computed +* hash, which will have length @resultlen. The +* memory pointer in @result must be released +* with a call to g_free() when no longer required.
include/crypto/hash.h int qcrypto_hash_digestv(QCryptoHashAlgorithm alg, · line 152
Quoted 10 lines
include/crypto/hash.h +10
-0+* +* Returns: 0 on success, -1 on error +*/ +int qcrypto_hash_accumulate_bytesv (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t *accumulate_ctx, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp);
Again, separate updating from finalizing, but in the public API we'll need the three finalizing variants.
Passing in 'alg' every time is also redundant when we have a context.
So this should result in the following
int qcrypto_hash_update(QCryptoHash *hash,
const struct iovec *iov, size_t niov,
Error **errp);
int qcrypto_hash_finalize_bytes(QCryptoHash *hash,
uint8_t **result,
size_t *resultlen,
Error **errp);
int qcrypto_hash_finalize_digest(QCryptoHash *hash,
char **result,
Error **errp);
int qcrypto_hash_finalize_base64(QCryptoHash *hash,
char **result,
Error **errp);Quoted 12 lines
include/crypto/hash.h +12
-0+ +/** +* qcrypto_hash_accumulate_new_ctx: +* @alg: the hash algorithm +* @accumulate_ctx: pointer to the pointer holding the algorithm's +* context for further hash operations. The pointer will +* be modified to point to the memory this function +* allocates to hold the context. +* @errp: pointer to a NULL-initialized error object +* +* Creates a new hashing context for the chosen algorithm for +* usage with qcrypto_hash_accumulate_bytesv().
include/crypto/hash.h int qcrypto_hash_digestv(QCryptoHashAlgorithm alg, · line 174
Quoted 10 lines
include/crypto/hash.h +10
-0+* This is useful for when one has multiple inputs to compute a hash, but +* not all are available at a single point in time, making qcrypto_hash_bytesv() +* inadequate. The @accumulate_ctx pointer must be released with a call to +* qcrypto_hash_accumulate_free_ctx() once all hash operations are complete. +* +* Returns: 0 on success, -1 on error +*/ +int qcrypto_hash_accumulate_new_ctx (QCryptoHashAlgorithm alg, +qcrypto_hash_accumulate_ctx_t **accumulate_ctx, +Error **errp);
Needs to be
QCryptoHash *qcrypto_hash_new(QCryptoHashAlgorithm alg,
Error **errp);
include/crypto/hash.h int qcrypto_hash_digestv(QCryptoHashAlgorithm alg, · line 186
Quoted 12 lines
include/crypto/hash.h +12
-0+ +/** +* qcrypto_hash_accumulate_free_ctx: +* @accumulate_ctx: pointer to the algorithm's context for further hash operations +* @errp: pointer to a NULL-initialized error object +* +* frees a hashing context for the chosen algorithm. +* +* Returns: 0 on success, -1 on error +*/ +int qcrypto_hash_accumulate_free_ctx (qcrypto_hash_accumulate_ctx_t *accumulate_ctx, +Error **errp);
Needs to be
QCryptoHash *qcrypto_hash_free(QCryptoHash *hash)
We shouldn't have an Error **errp for the destructor, as we need to be able to create an cleanup func for it:
G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoHash, qcrypto_hash_free)
Basically this ends up with the hash code looking very like the cipher.c code in terms of both public API and the internal driver design.
Finally, overall I'd like this patch to be split into multiple patches, since the changes are going to be quite large
* Define the new public API in hash.h & hash.c
* Provide the new impl for each backend (one patch per driver please)
* Add test cases to test-crypto-hash.c for the new APIs
* Re-implement the existing public APIs in terms of the new public APIs
* Drop the old impl for each backend (one patch per driver please)
With 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 v2 2/2 hw/misc/aspeed_hace: Fix SG Accumulative hashing
In reply to Alejandro ZeiseMake the Aspeed HACE module use the new qcrypto accumulative hashing functions when in scatter-gather accumulative mode. A hash context will maintain a "running-hash" as each scatter-gather chunk is received.
Previously each scatter-gather "chunk" was cached
so the hash could be computed once the final chunk was received. However, the cache was a shallow copy, so once the guest overwrote the memory provided to HACE the final hash would not be correct.
Possibly related to: https://gitlab.com/qemu-project/qemu/-/issues/1121
--- hw/misc/aspeed_hace.c | 91 ++++++++++++++++++----------------- include/hw/misc/aspeed_hace.h | 4 ++ 2 files changed, 51 insertions(+), 44 deletions(-)
hw/misc/aspeed_hace.c +47 -44
@@ -1,6 +1,7 @@/* * ASPEED Hash and Crypto Engine * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (C) 2021 IBM Corp. * * Joel Stanley <[email protected]>
@@ -151,47 +152,15 @@ static int reconstruct_iov(AspeedHACEState *s, struct iovec *iov, int id,return iov_count; } -/** -* Generate iov for accumulative mode. -* -* @param s aspeed hace state object -* @param iov iov of the current request -* @param id index of the current iov -* @param req_len length of the current request -* -* @return count of iov -*/ -static int gen_acc_mode_iov (AspeedHACEState *s,struct iovec *iov,int id, -hwaddr *req_len) -{ -uint32_t pad_offset; -uint32_t total_msg_len; - s->total_req_len += *req_len; - -if (has_padding (s, &iov[id], *req_len, &total_msg_len, &pad_offset)) { -if (s->iov_count) { -return reconstruct_iov (s, iov, id, &pad_offset); - } - - *req_len -= s->total_req_len - total_msg_len; - s->total_req_len =0 ; - iov[id].iov_len = *req_len; - }else { - s->iov_cache[s->iov_count].iov_base = iov->iov_base; - s->iov_cache[s->iov_count].iov_len = *req_len; - ++s->iov_count; - } - -return id +1 ; -} -static void do_hash_operation (AspeedHACEState *s,int algo,bool sg_mode,bool acc_mode) {struct iovec iov[ASPEED_HACE_MAX_SG ]; +uint32_t total_msg_len; +uint32_t pad_offset; g_autofree uint8_t *digest_buf = NULL;size_t digest_len =0 ; -int niov =0 ; +bool sg_acc_mode_final_request = false;int i;void *haddr;
@@ -226,8 +195,15 @@ static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode, } iov[i].iov_base = haddr;if (acc_mode) { - niov =gen_acc_mode_iov (s, iov, i, &plen); - + s->total_req_len += plen; + +if (has_padding (s, &iov[i], plen, &total_msg_len, &pad_offset)) { +/* Padding being present indicates the final request */ + sg_acc_mode_final_request = true; + iov[i].iov_len = pad_offset; + }else { + iov[i].iov_len = plen; + } }else { iov[i].iov_len = plen; }
@@ -252,20 +228,42 @@ static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode,* required to check whether cache is empty. If no, we should * combine cached iov and the current iov. */ -uint32_t total_msg_len; -uint32_t pad_offset; s->total_req_len += len;if (has_padding (s, iov, len, &total_msg_len, &pad_offset)) { - niov =reconstruct_iov (s, iov,0 , &pad_offset); + i =reconstruct_iov (s, iov,0 , &pad_offset); } } } -if (niov) { - i = niov; - } +if (acc_mode) { +if (s->qcrypto_hash_context ==NULL && +qcrypto_hash_accumulate_new_ctx (algo, &s->qcrypto_hash_context,NULL )) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: qcrypto failed to create hash context\n" , + __func__); +return ; + } + +if (qcrypto_hash_accumulate_bytesv (algo, + s->qcrypto_hash_context, iov, i, + &digest_buf, + &digest_len,NULL ) <0 ) { + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: qcrypto failed\n" , __func__); +return ; + } -if (qcrypto_hash_bytesv (algo, iov, i, &digest_buf, &digest_len,NULL ) <0 ) { +if (sg_acc_mode_final_request) { +if (qcrypto_hash_accumulate_free_ctx (s->qcrypto_hash_context,NULL ) <0 ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: qcrypto failed to free context\n" , __func__); + } + + s->qcrypto_hash_context =NULL ; + s->iov_count =0 ; + s->total_req_len =0 ; + } + }else if (qcrypto_hash_bytesv (algo, iov, i, &digest_buf, &digest_len,NULL ) <0 ) {qemu_log_mask (LOG_GUEST_ERROR ,"%s: qcrypto failed\n" , __func__);return ; }
@@ -397,6 +395,11 @@ static void aspeed_hace_reset(DeviceState *dev) {struct AspeedHACEState *s =ASPEED_HACE (dev); +if (s->qcrypto_hash_context !=NULL ) { +qcrypto_hash_accumulate_free_ctx (s->qcrypto_hash_context,NULL ); + s->qcrypto_hash_context =NULL ; + } +memset (s->regs,0 ,sizeof (s->regs)); s->iov_count =0 ; s->total_req_len =0 ;
include/hw/misc/aspeed_hace.h +4 -0
@@ -1,6 +1,7 @@/* * ASPEED Hash and Crypto Engine * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (C) 2021 IBM Corp. * * SPDX-License-Identifier: GPL-2.0-or-later
@@ -10,6 +11,7 @@#define ASPEED_HACE_H #include "hw/sysbus.h" +#include "crypto/hash.h" #define TYPE_ASPEED_HACE "aspeed.hace"#define TYPE_ASPEED_AST2400_HACE TYPE_ASPEED_HACE "-ast2400"
@@ -35,6 +37,8 @@ struct AspeedHACEState {MemoryRegion *dram_mr;AddressSpace dram_as; + +qcrypto_hash_accumulate_ctx_t *qcrypto_hash_context; };
Signature · 1 line
2.34.1
Re: [PATCH v2 2/2] hw/misc/aspeed_hace: Fix SG Accumulative hashing
Hi Alejandro,
Quoted 10 lines · Alejandro Zeise
Make the Aspeed HACE module use the new qcrypto accumulative hashing functions when in scatter-gather accumulative mode. A hash context will maintain a "running-hash" as each scatter-gather chunk is received.
Previously each scatter-gather "chunk" was cached
so the hash could be computed once the final chunk was received. However, the cache was a shallow copy, so once the guest overwrote the memory provided to HACE the final hash would not be correct.Possibly related to: https://gitlab.com/qemu-project/qemu/-/issues/1121
Likely, Cc'ing John.
Quoted 13 lines
Signed-off-by Alejandro Zeise <[email protected]>--- hw/misc/aspeed_hace.c | 91 ++++++++++++++++++----------------- include/hw/misc/aspeed_hace.h | 4 ++ 2 files changed, 51 insertions(+), 44 deletions(-)hw/misc/aspeed_hace.c +0
-1@@ -252,20 +228,42 @@ static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode, -if (niov) {
hw/misc/aspeed_hace.c static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode, · line 237
Quoted 10 lines
hw/misc/aspeed_hace.c +8
-2- i = niov; - } +if (acc_mode) { +if (s->qcrypto_hash_context ==NULL && +qcrypto_hash_accumulate_new_ctx (algo, &s->qcrypto_hash_context,NULL )) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: qcrypto failed to create hash context\n" , + __func__); +return ; + }
Using a instance_init() handler, ...
hw/misc/aspeed_hace.c static void aspeed_hace_reset(DeviceState *dev) · line 401
Quoted 8 lines
hw/misc/aspeed_hace.c +4
-0@@ -397,6 +395,11 @@ static void aspeed_hace_reset(DeviceState *dev) {struct AspeedHACEState *s =ASPEED_HACE (dev); +if (s->qcrypto_hash_context !=NULL ) { +qcrypto_hash_accumulate_free_ctx (s->qcrypto_hash_context,NULL ); + s->qcrypto_hash_context =NULL ; + }
... and instance_finalize() could simplify a bit.
Regards,
Phil.
Re: [PATCH v2 2/2] hw/misc/aspeed_hace: Fix SG Accumulative hashing
In reply to Alejandro ZeiseHello Alejandro,
Quoted 11 lines · Alejandro Zeise
Make the Aspeed HACE module use the new qcrypto accumulative hashing functions when in scatter-gather accumulative mode. A hash context will maintain a "running-hash" as each scatter-gather chunk is received.
Previously each scatter-gather "chunk" was cached
so the hash could be computed once the final chunk was received. However, the cache was a shallow copy, so once the guest overwrote the memory provided to HACE the final hash would not be correct.Possibly related to: https://gitlab.com/qemu-project/qemu/-/issues/1121
Thanks for these changes.
However, this introduces a regression when compiling QEMU with --disable-gcrypt. It can be reproduced with :
make check-avocado AVOCADO_TAGS=machine:ast1030-evb
or download :
https://github.com/AspeedTech-BMC/zephyr/releases/download/v00.01.07/ast1030-evb-demo.zip
unzip and run :
path/to/qemu-system-arm -M ast1030-evb -kernel ./zephyr.bin -nographic
then, on the console :
uart:~$ hash test sha256_test tv[0]:PASS tv[1]:PASS tv[2]:PASS tv[3]:PASS tv[4]:PASS sha384_test tv[0]:hash_final error sha512_test tv[0]:Segmentation fault (core dumped)
I believe this is due to the change which now assigns s->total_req_len when accumulation mode is desired. If the crypto context fails to allocate (no gcrypt), states are not cleared in the model and previous values are used by the model when the OS runs the next test and QEMU segvs in has_padding().
To fix, I think we could move :
if (s->qcrypto_hash_context == NULL &&
qcrypto_hash_accumulate_new_ctx(algo, &s->qcrypto_hash_context, NULL)) {
qemu_log_mask(LOG_GUEST_ERROR,
"%s: qcrypto failed to create hash context\n",
__func__);
return;
}at the beginning of do_hash_operation() ?
C.
Quoted 178 lines
Signed-off-by Alejandro Zeise <[email protected]>--- hw/misc/aspeed_hace.c | 91 ++++++++++++++++++----------------- include/hw/misc/aspeed_hace.h | 4 ++ 2 files changed, 51 insertions(+), 44 deletions(-)hw/misc/aspeed_hace.c +47
-44@@ -1,6 +1,7 @@/* * ASPEED Hash and Crypto Engine * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (C) 2021 IBM Corp. * * Joel Stanley <[email protected]> @@ -151,47 +152,15 @@ static int reconstruct_iov(AspeedHACEState *s, struct iovec *iov, int id,return iov_count; } -/** -* Generate iov for accumulative mode. -* -* @param s aspeed hace state object -* @param iov iov of the current request -* @param id index of the current iov -* @param req_len length of the current request -* -* @return count of iov -*/ -static int gen_acc_mode_iov (AspeedHACEState *s,struct iovec *iov,int id, -hwaddr *req_len) -{ -uint32_t pad_offset; -uint32_t total_msg_len; - s->total_req_len += *req_len; - -if (has_padding (s, &iov[id], *req_len, &total_msg_len, &pad_offset)) { -if (s->iov_count) { -return reconstruct_iov (s, iov, id, &pad_offset); - } - - *req_len -= s->total_req_len - total_msg_len; - s->total_req_len =0 ; - iov[id].iov_len = *req_len; - }else { - s->iov_cache[s->iov_count].iov_base = iov->iov_base; - s->iov_cache[s->iov_count].iov_len = *req_len; - ++s->iov_count; - } - -return id +1 ; -} -static void do_hash_operation (AspeedHACEState *s,int algo,bool sg_mode,bool acc_mode) {struct iovec iov[ASPEED_HACE_MAX_SG ]; +uint32_t total_msg_len; +uint32_t pad_offset; g_autofree uint8_t *digest_buf = NULL;size_t digest_len =0 ; -int niov =0 ; +bool sg_acc_mode_final_request = false;int i;void *haddr;@@ -226,8 +195,15 @@ static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode, } iov[i].iov_base = haddr;if (acc_mode) { - niov =gen_acc_mode_iov (s, iov, i, &plen); - + s->total_req_len += plen; + +if (has_padding (s, &iov[i], plen, &total_msg_len, &pad_offset)) { +/* Padding being present indicates the final request */ + sg_acc_mode_final_request = true; + iov[i].iov_len = pad_offset; + }else { + iov[i].iov_len = plen; + } }else { iov[i].iov_len = plen; }@@ -252,20 +228,42 @@ static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode,* required to check whether cache is empty. If no, we should * combine cached iov and the current iov. */ -uint32_t total_msg_len; -uint32_t pad_offset; s->total_req_len += len;if (has_padding (s, iov, len, &total_msg_len, &pad_offset)) { - niov =reconstruct_iov (s, iov,0 , &pad_offset); + i =reconstruct_iov (s, iov,0 , &pad_offset); } } } -if (niov) { - i = niov; - } +if (acc_mode) { +if (s->qcrypto_hash_context ==NULL && +qcrypto_hash_accumulate_new_ctx (algo, &s->qcrypto_hash_context,NULL )) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: qcrypto failed to create hash context\n" , + __func__); +return ; + } + +if (qcrypto_hash_accumulate_bytesv (algo, + s->qcrypto_hash_context, iov, i, + &digest_buf, + &digest_len,NULL ) <0 ) { + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: qcrypto failed\n" , __func__); +return ; + } -if (qcrypto_hash_bytesv (algo, iov, i, &digest_buf, &digest_len,NULL ) <0 ) { +if (sg_acc_mode_final_request) { +if (qcrypto_hash_accumulate_free_ctx (s->qcrypto_hash_context,NULL ) <0 ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: qcrypto failed to free context\n" , __func__); + } + + s->qcrypto_hash_context =NULL ; + s->iov_count =0 ; + s->total_req_len =0 ; + } + }else if (qcrypto_hash_bytesv (algo, iov, i, &digest_buf, &digest_len,NULL ) <0 ) {qemu_log_mask (LOG_GUEST_ERROR ,"%s: qcrypto failed\n" , __func__);return ; }@@ -397,6 +395,11 @@ static void aspeed_hace_reset(DeviceState *dev) {struct AspeedHACEState *s =ASPEED_HACE (dev); +if (s->qcrypto_hash_context !=NULL ) { +qcrypto_hash_accumulate_free_ctx (s->qcrypto_hash_context,NULL ); + s->qcrypto_hash_context =NULL ; + } +memset (s->regs,0 ,sizeof (s->regs)); s->iov_count =0 ; s->total_req_len =0 ;include/hw/misc/aspeed_hace.h +4
-0@@ -1,6 +1,7 @@/* * ASPEED Hash and Crypto Engine * +* Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates * Copyright (C) 2021 IBM Corp. * * SPDX-License-Identifier: GPL-2.0-or-later @@ -10,6 +11,7 @@#define ASPEED_HACE_H #include "hw/sysbus.h" +#include "crypto/hash.h" #define TYPE_ASPEED_HACE "aspeed.hace"#define TYPE_ASPEED_AST2400_HACE TYPE_ASPEED_HACE "-ast2400"@@ -35,6 +37,8 @@ struct AspeedHACEState {MemoryRegion *dram_mr;AddressSpace dram_as; + +qcrypto_hash_accumulate_ctx_t *qcrypto_hash_context; };
RE: [PATCH v2 2/2] hw/misc/aspeed_hace: Fix SG Accumulative hashing
Hello Cédric,
Quoted 86 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, July 30, 2024 5:55 AM
To: Alejandro Zeise <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v2 2/2] hw/misc/aspeed_hace: Fix SG Accumulative hashingThis message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.
Hello Alejandro,
Quoted 20 lines · Alejandro Zeise
Make the Aspeed HACE module use the new qcrypto accumulative hashing functions when in scatter-gather accumulative mode. A hash context will maintain a "running-hash" as each scatter-gather chunk is received.
Previously each scatter-gather "chunk" was cached so the hash could be computed once the final chunk was received.
However, the cache was a shallow copy, so once the guest overwrote the memory provided to HACE the final hash would not be correct.Possibly related to:
https://secure-web.cisco.com/1m1Z2YCrV2vhocDftauu92ZeOegqeZfIRFUAJJwVk
F9fI5CEmq9q8M0oYYTKLIBE7H7Anc9W8PG2iYcK9L1uzXdg80qNty6Zu1X6QN7rQvV39cA qS8AoWcsYMaBzVo6gGKY2upM6_8eJmiOy2M7vrGmEaaUE-SnW6zCD1bbH3et6nErWgGfgs TIxuiRzgwvE7wrTPewA6isDxOsWeEcldCKuKbmYH2sSnQ3jpdZMKmpgX9tVZfICI3nwhwb wz8RYBg_4OH-8kl9ECEW6grKDPPi8Y0ni3zO8HPywr36nwkhAuYmK8hC27vBbf7qfloMu5 tzRGJBUPQcdSjWeK_4MtrxQCoIpUFEKPMqf-ielVMsCTsuYh0ABhB9ur84fcQpnXtTFuib WvRU6x-AQGW1WmZzgxUArNn7_Ha_Kf0PknI2b9LJIKaSCuNa4y37x0aQpO_BYL0LBaQEKA cUedbaxB-Q/https%3A%2F%2Fgitlab.com%2Fqemu-project%2Fqemu%2F-%2Fissues %2F1121
Buglink:Quoted 8 lines
https://secure-web.cisco.com/1ofiYFe1SNQgzRQ3E3PS9LITMhUsDJZIVWuOog_Vj
tgnTabxX9NqW5LYr4EPFrVCzPtMf-hAx87qIkUirL6wMbnKyhgCeobULUm0wBYi9botfSoDKFXa6vBO6GgNdVBAbGMSCO1IgmogBY_Q33lM6M7OFbMipxxsGDhIBJMeYneaqgr5QatdcgcmCgju8b2v60q-f7xxTJFtDihOD9rfCzPkspw9y9McZyzUqVOb-Fin-SYQEaC9sEN7pFC Zu59djaWocCIRIudGSVPubNCyV6-AfUNlAL6Bfh-e99_VoDpCMv6KCZGVooRjcdCvUW4uJ _qhVa7VQKJMQbHJEE-Qq1hdOlPbR2Ae5dQoizEatClH05JZLE1ljgu2JtAIrioZ7JyfmMn j-w8-ChDDGYPdtVNLKk0_Trhz4Z9WvvlC-gAWirx_aEwzAYF7FRC8I_oqAZUoDDxJRcSYd Yv4XjCcqGA/https%3A%2F%2Fgithub.com%2Fopenbmc%2Fqemu%2Fissues%2F36Thanks for these changes.
However, this introduces a regression when compiling QEMU with --disable-gcrypt. It can be reproduced with :
make check-avocado AVOCADO_TAGS=machine:ast1030-evb
or download :
unzip and run :
path/to/qemu-system-arm -M ast1030-evb -kernel ./zephyr.bin -nographic
then, on the console :
uart:~$ hash test sha256_test tv[0]:PASS tv[1]:PASS tv[2]:PASS tv[3]:PASS tv[4]:PASS sha384_test tv[0]:hash_final error sha512_test tv[0]:Segmentation fault (core dumped)I believe this is due to the change which now assigns
s->total_req_lenwhen accumulation mode is desired. If the crypto context fails to allocate (no gcrypt), states are not cleared in the model and previous values are used by the model when the OS runs the next test and QEMU segvs inhas_padding().To fix, I think we could move :
if (s->qcrypto_hash_context == NULL && qcrypto_hash_accumulate_new_ctx(algo, &s->qcrypto_hash_context, NULL)) { qemu_log_mask(LOG_GUEST_ERROR, "%s: qcrypto failed to create hash context\n", __func__); return; }
Quoted 3 lines
at the beginning of
do_hash_operation()?C.
Good catch. Moving the context allocation to the beginning of the function does appear to fix the issue. I will submit a new patch series with that change implemented.
Thanks,
Alejandro