diff --git a/wolfcrypt/src/cryptocb.c b/wolfcrypt/src/cryptocb.c index 56d42b8be..b676c40de 100644 --- a/wolfcrypt/src/cryptocb.c +++ b/wolfcrypt/src/cryptocb.c @@ -1596,6 +1596,40 @@ int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in, } #endif /* !NO_SHA */ +#ifdef WOLFSSL_SHA224 +int wc_CryptoCb_Sha224Hash(wc_Sha224* sha224, const byte* in, + word32 inSz, byte* digest) +{ + int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); + CryptoCb* dev; + + /* locate registered callback */ + if (sha224) { + dev = wc_CryptoCb_FindDevice(sha224->devId, WC_ALGO_TYPE_HASH); + } + else { + /* locate first callback and try using it */ + dev = wc_CryptoCb_FindDeviceByIndex(0); + } + + if (dev && dev->cb) { + wc_CryptoInfo cryptoInfo; + XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); + cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; + cryptoInfo.hash.type = WC_HASH_TYPE_SHA224; + cryptoInfo.hash.sha224 = sha224; + cryptoInfo.hash.in = in; + cryptoInfo.hash.inSz = inSz; + cryptoInfo.hash.digest = digest; + + ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); + } + + return wc_CryptoCb_TranslateErrorCode(ret); +} +#endif /* WOLFSSL_SHA224 */ + + #ifndef NO_SHA256 int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in, word32 inSz, byte* digest) diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 08434c90e..6bf2d49ab 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -2074,7 +2074,10 @@ static int Transform_Sha256(wc_Sha256* sha256, const byte* data) #ifdef WOLFSSL_SMALL_STACK_CACHE sha224->W = NULL; #endif - + #ifdef WOLF_CRYPTO_CB + sha224->devId = devId; + sha224->devCtx = NULL; + #endif #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) #if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224) /* We know this is a fresh, uninitialized item, so set to INIT */ @@ -2133,7 +2136,17 @@ static int Transform_Sha256(wc_Sha256* sha256, const byte* data) if (data == NULL) { return BAD_FUNC_ARG; } - + #ifdef WOLF_CRYPTO_CB + #ifndef WOLF_CRYPTO_CB_FIND + if (sha224->devId != INVALID_DEVID) + #endif + { + ret = wc_CryptoCb_Sha224Hash(sha224, data, len, NULL); + if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) + return ret; + /* fall-through when unavailable */ + } + #endif #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224) if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) { #if defined(HAVE_INTEL_QA) @@ -2160,7 +2173,17 @@ static int Transform_Sha256(wc_Sha256* sha256, const byte* data) if (sha224 == NULL || hash == NULL) { return BAD_FUNC_ARG; } - + #ifdef WOLF_CRYPTO_CB + #ifndef WOLF_CRYPTO_CB_FIND + if (sha224->devId != INVALID_DEVID) + #endif + { + ret = wc_CryptoCb_Sha224Hash(sha224, NULL, 0, hash); + if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) + return ret; + /* fall-through when unavailable */ + } + #endif #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224) if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) { #if defined(HAVE_INTEL_QA) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 1c38e9920..4562d5b8e 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -60741,6 +60741,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void) if (ret == 0) ret = sha_test(); #endif +#ifdef WOLFSSL_SHA224 + if (ret == 0) + ret = sha224_test(); +#endif #ifndef NO_SHA256 if (ret == 0) ret = sha256_test(); diff --git a/wolfssl/wolfcrypt/cryptocb.h b/wolfssl/wolfcrypt/cryptocb.h index af5f912ff..e1cd2cb94 100644 --- a/wolfssl/wolfcrypt/cryptocb.h +++ b/wolfssl/wolfcrypt/cryptocb.h @@ -639,6 +639,10 @@ WOLFSSL_LOCAL int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in, word32 inSz, byte* digest); #endif /* !NO_SHA */ +#ifdef WOLFSSL_SHA224 +WOLFSSL_LOCAL int wc_CryptoCb_Sha224Hash(wc_Sha224* sha224, const byte* in, + word32 inSz, byte* digest); +#endif /* WOLFSSL_SHA224 */ #ifndef NO_SHA256 WOLFSSL_LOCAL int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in, word32 inSz, byte* digest);