From 2a0b2443939acb2f60e731e9b39d17fc8b24a233 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Mon, 5 Jun 2023 14:45:34 -0600 Subject: [PATCH] add sanity check on hash size with STM32 port --- wolfcrypt/src/port/st/stm32.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/wolfcrypt/src/port/st/stm32.c b/wolfcrypt/src/port/st/stm32.c index 73b43a525..3b8c01411 100644 --- a/wolfcrypt/src/port/st/stm32.c +++ b/wolfcrypt/src/port/st/stm32.c @@ -984,8 +984,21 @@ int stm32_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash, pka_ecc.pPubKeyCurvePtY = Qybin; pka_ecc.RSign = Rbin; pka_ecc.SSign = Sbin; + XMEMSET(Hashbin, 0, STM32_MAX_ECC_SIZE); - XMEMCPY(Hashbin + (size - hashlen), hash, hashlen); + if (hashlen > STM32_MAX_ECC_SIZE) { + return ECC_BAD_ARG_E; + } + else if (hashlen > size) { + /* in the case that hashlen is larger than key size place hash at + * beginning of buffer */ + XMEMCPY(Hashbin, hash, hashlen); + } + else { + /* in all other cases where hashlen is equal to or less than the key + * size pad the Hashbin buffer with leading zero's */ + XMEMCPY(Hashbin + (size - hashlen), hash, hashlen); + } pka_ecc.hash = Hashbin; status = HAL_PKA_ECDSAVerif(&hpka, &pka_ecc, HAL_MAX_DELAY);