From 45b34517fb01762b1ee3542e7dc8921f9aad86a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 30 Mar 2023 12:19:35 +0200 Subject: [PATCH] Keep MD and PSA max size in sync MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some TLS code is using MD_MAX_SIZE in parts that are common to USE_PSA and non-USE_PSA, then using PSA_HASH_MAX_SIZE in parts specific to USE_PSA, and having different values causes trouble. Signed-off-by: Manuel Pégourié-Gonnard --- include/mbedtls/md.h | 1 + include/psa/crypto_sizes.h | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/mbedtls/md.h b/include/mbedtls/md.h index 79e25ea1b4..657b5cc01c 100644 --- a/include/mbedtls/md.h +++ b/include/mbedtls/md.h @@ -151,6 +151,7 @@ typedef enum { MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */ } mbedtls_md_type_t; +/* Note: this should be kept in sync with PSA_HASH_MAX_SIZE */ #if defined(MBEDTLS_MD_CAN_SHA512) #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */ #elif defined(MBEDTLS_MD_CAN_SHA384) diff --git a/include/psa/crypto_sizes.h b/include/psa/crypto_sizes.h index 75d6582de1..94a8948fe4 100644 --- a/include/psa/crypto_sizes.h +++ b/include/psa/crypto_sizes.h @@ -128,12 +128,22 @@ /* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226, * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for * HMAC-SHA3-512. */ -#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384) +/* Note: PSA_HASH_MAX_SIZE should be kept in sync with MBEDTLS_MD_MAX_SIZE. */ +#if defined(PSA_WANT_ALG_SHA_512) #define PSA_HASH_MAX_SIZE 64 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128 -#else +#elif defined(PSA_WANT_ALG_SHA_384) +#define PSA_HASH_MAX_SIZE 48 +#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128 +#elif defined(PSA_WANT_ALG_SHA_256) #define PSA_HASH_MAX_SIZE 32 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64 +#elif defined(PSA_WANT_ALG_SHA_224) +#define PSA_HASH_MAX_SIZE 28 +#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64 +#else /* SHA-1 or smaller */ +#define PSA_HASH_MAX_SIZE 20 +#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64 #endif /** \def PSA_MAC_MAX_SIZE