From 71bb7b77f05fd3e8f07c99249f9e645326fbd96a Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 19 Apr 2018 08:29:59 +0200 Subject: [PATCH] Switch PSA_HASH_FINAL_SIZE to PSA_HASH_SIZE Make this macro work on derived algorithms as well (HMAC, hash-and-sign, etc.). --- include/psa/crypto.h | 40 ++++++++++++++++++++-------------------- library/psa_crypto.c | 2 +- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index a463cc05a..c880586fe 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -814,23 +814,23 @@ typedef struct psa_hash_operation_s psa_hash_operation_t; * An implementation may return either 0 or the correct size * for a hash algorithm that it recognizes, but does not support. */ -#define PSA_HASH_FINAL_SIZE(alg) \ - ( \ - (alg) == PSA_ALG_MD2 ? 16 : \ - (alg) == PSA_ALG_MD4 ? 16 : \ - (alg) == PSA_ALG_MD5 ? 16 : \ - (alg) == PSA_ALG_RIPEMD160 ? 20 : \ - (alg) == PSA_ALG_SHA_1 ? 20 : \ - (alg) == PSA_ALG_SHA_224 ? 28 : \ - (alg) == PSA_ALG_SHA_256 ? 32 : \ - (alg) == PSA_ALG_SHA_384 ? 48 : \ - (alg) == PSA_ALG_SHA_512 ? 64 : \ - (alg) == PSA_ALG_SHA_512_224 ? 28 : \ - (alg) == PSA_ALG_SHA_512_256 ? 32 : \ - (alg) == PSA_ALG_SHA3_224 ? 28 : \ - (alg) == PSA_ALG_SHA3_256 ? 32 : \ - (alg) == PSA_ALG_SHA3_384 ? 48 : \ - (alg) == PSA_ALG_SHA3_512 ? 64 : \ +#define PSA_HASH_SIZE(alg) \ + ( \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \ + PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \ 0) /** Start a multipart hash operation. @@ -915,7 +915,7 @@ psa_status_t psa_hash_update(psa_hash_operation_t *operation, * \param hash_size Size of the \c hash buffer in bytes. * \param hash_length On success, the number of bytes * that make up the hash value. This is always - * #PSA_HASH_FINAL_SIZE(alg) where \c alg is the + * #PSA_HASH_SIZE(alg) where \c alg is the * hash algorithm that is calculated. * * \retval PSA_SUCCESS @@ -924,7 +924,7 @@ psa_status_t psa_hash_update(psa_hash_operation_t *operation, * The operation state is not valid (not started, or already completed). * \retval PSA_ERROR_BUFFER_TOO_SMALL * The size of the \c hash buffer is too small. You can determine a - * sufficient buffer size by calling #PSA_HASH_FINAL_SIZE(alg) + * sufficient buffer size by calling #PSA_HASH_SIZE(alg) * where \c alg is the hash algorithm that is calculated. * \retval PSA_ERROR_INSUFFICIENT_MEMORY * \retval PSA_ERROR_COMMUNICATION_FAILURE @@ -1020,7 +1020,7 @@ typedef struct psa_mac_operation_s psa_mac_operation_t; * for a MAC algorithm that it recognizes, but does not support. */ #define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \ - (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_FINAL_SIZE(PSA_ALG_HMAC_HASH(alg)) : \ + (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_HASH(alg)) : \ PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \ 0) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 3ea87f642..b66862c50 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -774,7 +774,7 @@ psa_status_t psa_hash_finish( psa_hash_operation_t *operation, size_t *hash_length ) { int ret; - size_t actual_hash_length = PSA_HASH_FINAL_SIZE( operation->alg ); + size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); /* Fill the output buffer with something that isn't a valid hash * (barring an attack on the hash and deliberately-crafted input),