Skip to content

Commit

Permalink
Merge pull request #7705 from aidangarske/SHA3-cryptocb
Browse files Browse the repository at this point in the history
Sha3.c wc_Sha3Update and  wc_Sha3Final Hash Type Change
  • Loading branch information
JacobBarthelmeh committed Jul 2, 2024
2 parents a490d4f + 804f25d commit 4ff0af7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
37 changes: 25 additions & 12 deletions wolfcrypt/src/sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion wolfssl/wolfcrypt/sha3.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ struct wc_Sha3 {

#ifdef WOLF_CRYPTO_CB
int devId;
int type; /* enum wc_HashType */
#endif

#ifdef WC_C_DYNAMIC_FALLBACK
Expand Down

0 comments on commit 4ff0af7

Please sign in to comment.