From 804f25d76bb1a4beb6b9c02a2a1261fc73cdc170 Mon Sep 17 00:00:00 2001 From: aidan garske Date: Tue, 2 Jul 2024 10:32:57 -0700 Subject: [PATCH] Sha3.c wc_Sha3Update and wc_Sha3Final changes so that hash type is determined in the processing functions. --- wolfcrypt/src/sha3.c | 37 +++++++++++++++++++++++++------------ wolfssl/wolfcrypt/sha3.h | 1 - 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/wolfcrypt/src/sha3.c b/wolfcrypt/src/sha3.c index dc0c3a4170..99f739b029 100644 --- a/wolfcrypt/src/sha3.c +++ b/wolfcrypt/src/sha3.c @@ -806,7 +806,7 @@ static int Sha3Final(wc_Sha3* sha3, byte padChar, byte* hash, byte p, word32 l) * devId Device identifier for asynchronous operation. * returns 0 on success. */ -static int wc_InitSha3(wc_Sha3* sha3, int type, void* heap, int devId) +static int wc_InitSha3(wc_Sha3* sha3, void* heap, int devId) { int ret = 0; @@ -823,12 +823,9 @@ static int wc_InitSha3(wc_Sha3* sha3, int type, void* heap, int devId) WOLFSSL_ASYNC_MARKER_SHA3, sha3->heap, devId); #elif defined(WOLF_CRYPTO_CB) sha3->devId = devId; - sha3->type = type; - #endif /* WOLFSSL_ASYNC_CRYPT */ (void)devId; - (void)type; return ret; } @@ -859,7 +856,15 @@ static int wc_Sha3Update(wc_Sha3* sha3, const byte* data, word32 len, byte p) if (sha3->devId != INVALID_DEVID) #endif { - ret = wc_CryptoCb_Sha3Hash(sha3, sha3->type, data, len, NULL); + int hash_type = WC_HASH_TYPE_NONE; + switch (p) { + case WC_SHA3_224_COUNT: hash_type = WC_HASH_TYPE_SHA3_224; break; + case WC_SHA3_256_COUNT: hash_type = WC_HASH_TYPE_SHA3_256; break; + case WC_SHA3_384_COUNT: hash_type = WC_HASH_TYPE_SHA3_384; break; + case WC_SHA3_512_COUNT: hash_type = WC_HASH_TYPE_SHA3_512; break; + default: return BAD_FUNC_ARG; + } + ret = wc_CryptoCb_Sha3Hash(sha3, hash_type, data, len, NULL); if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) return ret; /* fall-through when unavailable */ @@ -905,7 +910,15 @@ static int wc_Sha3Final(wc_Sha3* sha3, byte* hash, byte p, byte len) if (sha3->devId != INVALID_DEVID) #endif { - ret = wc_CryptoCb_Sha3Hash(sha3, sha3->type, NULL, 0, hash); + int hash_type = WC_HASH_TYPE_NONE; + switch (p) { + case WC_SHA3_224_COUNT: hash_type = WC_HASH_TYPE_SHA3_224; break; + case WC_SHA3_256_COUNT: hash_type = WC_HASH_TYPE_SHA3_256; break; + case WC_SHA3_384_COUNT: hash_type = WC_HASH_TYPE_SHA3_384; break; + case WC_SHA3_512_COUNT: hash_type = WC_HASH_TYPE_SHA3_512; break; + default: return BAD_FUNC_ARG; + } + ret = wc_CryptoCb_Sha3Hash(sha3, hash_type, NULL, 0, hash); if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) return ret; /* fall-through when unavailable */ @@ -1012,7 +1025,7 @@ static int wc_Sha3GetHash(wc_Sha3* sha3, byte* hash, byte p, byte len) */ int wc_InitSha3_224(wc_Sha3* sha3, void* heap, int devId) { - return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_224, heap, devId); + return wc_InitSha3(sha3, heap, devId); } /* Update the SHA3-224 hash state with message data. @@ -1084,7 +1097,7 @@ int wc_Sha3_224_Copy(wc_Sha3* src, wc_Sha3* dst) */ int wc_InitSha3_256(wc_Sha3* sha3, void* heap, int devId) { - return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_256, heap, devId); + return wc_InitSha3(sha3, heap, devId); } /* Update the SHA3-256 hash state with message data. @@ -1156,7 +1169,7 @@ int wc_Sha3_256_Copy(wc_Sha3* src, wc_Sha3* dst) */ int wc_InitSha3_384(wc_Sha3* sha3, void* heap, int devId) { - return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_384, heap, devId); + return wc_InitSha3(sha3, heap, devId); } /* Update the SHA3-384 hash state with message data. @@ -1228,7 +1241,7 @@ int wc_Sha3_384_Copy(wc_Sha3* src, wc_Sha3* dst) */ int wc_InitSha3_512(wc_Sha3* sha3, void* heap, int devId) { - return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_512, heap, devId); + return wc_InitSha3(sha3, heap, devId); } /* Update the SHA3-512 hash state with message data. @@ -1317,7 +1330,7 @@ int wc_Sha3_GetFlags(wc_Sha3* sha3, word32* flags) */ int wc_InitShake128(wc_Shake* shake, void* heap, int devId) { - return wc_InitSha3(shake, WC_HASH_TYPE_SHAKE128, heap, devId); + return wc_InitSha3(shake, heap, devId); } /* Update the SHAKE128 hash state with message data. @@ -1461,7 +1474,7 @@ int wc_Shake128_Copy(wc_Shake* src, wc_Shake* dst) */ int wc_InitShake256(wc_Shake* shake, void* heap, int devId) { - return wc_InitSha3(shake, WC_HASH_TYPE_SHAKE256, heap, devId); + return wc_InitSha3(shake, heap, devId); } /* Update the SHAKE256 hash state with message data. diff --git a/wolfssl/wolfcrypt/sha3.h b/wolfssl/wolfcrypt/sha3.h index 83fe813644..e1ce33a1bf 100644 --- a/wolfssl/wolfcrypt/sha3.h +++ b/wolfssl/wolfcrypt/sha3.h @@ -126,7 +126,6 @@ struct wc_Sha3 { #ifdef WOLF_CRYPTO_CB int devId; - int type; /* enum wc_HashType */ #endif #ifdef WC_C_DYNAMIC_FALLBACK