diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 59b10eace..690af7564 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -678,7 +678,10 @@ static int InitSha256(wc_Sha256* sha256) sha256->devCtx = NULL; #endif #ifdef WOLFSSL_SMALL_STACK_CACHE - sha256->W = NULL; + sha256->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, + sha256->heap, DYNAMIC_TYPE_DIGEST); + if (sha256->W == NULL) + return MEMORY_E; #endif ret = InitSha256(sha256); @@ -1163,7 +1166,10 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data, } #endif #ifdef WOLFSSL_SMALL_STACK_CACHE - sha256->W = NULL; + sha256->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, + sha256->heap, DYNAMIC_TYPE_DIGEST); + if (sha256->W == NULL) + return MEMORY_E; #endif #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256) @@ -1244,13 +1250,8 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data, #if defined(WOLFSSL_SMALL_STACK_CACHE) && !defined(WOLFSSL_NO_MALLOC) word32* W = sha256->W; - if (W == NULL) { - W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, - sha256->heap, DYNAMIC_TYPE_DIGEST); - if (W == NULL) - return MEMORY_E; - sha256->W = W; - } + if (W == NULL) + return BAD_FUNC_ARG; #elif defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) word32* W; W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, @@ -2065,6 +2066,15 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data, { int ret = 0; +#ifdef WOLFSSL_SMALL_STACK_CACHE + if (sha224->W == NULL) { + sha224->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, + sha224->heap, DYNAMIC_TYPE_DIGEST); + if (sha224->W == NULL) + return MEMORY_E; + } +#endif + sha224->digest[0] = 0xc1059ed8; sha224->digest[1] = 0x367cd507; sha224->digest[2] = 0x3070dd17; @@ -2575,7 +2585,12 @@ int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz) XMEMCPY(dst, src, sizeof(wc_Sha224)); #ifdef WOLFSSL_SMALL_STACK_CACHE - dst->W = NULL; + dst->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, + dst->heap, DYNAMIC_TYPE_DIGEST); + if (dst->W == NULL) { + XMEMSET(dst, 0, sizeof(wc_Sha224)); + return MEMORY_E; + } #endif #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) @@ -2727,7 +2742,12 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst) #endif #ifdef WOLFSSL_SMALL_STACK_CACHE - dst->W = NULL; + dst->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, + dst->heap, DYNAMIC_TYPE_DIGEST); + if (dst->W == NULL) { + XMEMSET(dst, 0, sizeof(wc_Sha256)); + return MEMORY_E; + } #endif #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) diff --git a/wolfcrypt/src/sha512.c b/wolfcrypt/src/sha512.c index ceabdc2b8..cf67478b2 100644 --- a/wolfcrypt/src/sha512.c +++ b/wolfcrypt/src/sha512.c @@ -870,7 +870,10 @@ static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId, sha512->heap = heap; #ifdef WOLFSSL_SMALL_STACK_CACHE - sha512->W = NULL; + sha512->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE, + sha512->heap, DYNAMIC_TYPE_DIGEST); + if (sha512->W == NULL) + return MEMORY_E; #endif #ifdef WOLF_CRYPTO_CB sha512->devId = devId; @@ -1031,14 +1034,10 @@ static int _Transform_Sha512(wc_Sha512* sha512) word32 j; word64 T[8]; -#ifdef WOLFSSL_SMALL_STACK_CACHE +#if defined(WOLFSSL_SMALL_STACK_CACHE) word64* W = sha512->W; - if (W == NULL) { - W = (word64*)XMALLOC(sizeof(word64) * 16, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER); - if (W == NULL) - return MEMORY_E; - sha512->W = W; - } + if (W == NULL) + return BAD_FUNC_ARG; #elif defined(WOLFSSL_SMALL_STACK) word64* W; W = (word64*) XMALLOC(sizeof(word64) * 16, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER); @@ -1646,7 +1645,7 @@ void wc_Sha512Free(wc_Sha512* sha512) #ifdef WOLFSSL_SMALL_STACK_CACHE if (sha512->W != NULL) { - ForceZero(sha512->W, sizeof(word64) * 16); + ForceZero(sha512->W, (sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE); XFREE(sha512->W, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER); sha512->W = NULL; } @@ -1699,7 +1698,12 @@ int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data) return BAD_FUNC_ARG; } -#ifdef WOLFSSL_SMALL_STACK + +#if defined(WOLFSSL_SMALL_STACK_CACHE) + if (sha->W == NULL) + return BAD_FUNC_ARG; + buffer = sha->W + 16; +#elif defined(WOLFSSL_SMALL_STACK) buffer = (word64*)XMALLOC(WC_SHA512_BLOCK_SIZE, sha->heap, DYNAMIC_TYPE_TMP_BUFFER); if (buffer == NULL) @@ -1733,7 +1737,7 @@ int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data) XMEMCPY(sha->buffer, buffer, WC_SHA512_BLOCK_SIZE); #endif -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE) ForceZero(buffer, WC_SHA512_BLOCK_SIZE); XFREE(buffer, sha->heap, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -1867,6 +1871,15 @@ static int InitSha384(wc_Sha384* sha384) return BAD_FUNC_ARG; } +#ifdef WOLFSSL_SMALL_STACK_CACHE + if (sha384->W == NULL) { + sha384->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE, + sha384->heap, DYNAMIC_TYPE_DIGEST); + if (sha384->W == NULL) + return MEMORY_E; + } +#endif + sha384->digest[0] = W64LIT(0xcbbb9d5dc1059ed8); sha384->digest[1] = W64LIT(0x629a292a367cd507); sha384->digest[2] = W64LIT(0x9159015a3070dd17); @@ -2106,7 +2119,7 @@ void wc_Sha384Free(wc_Sha384* sha384) #ifdef WOLFSSL_SMALL_STACK_CACHE if (sha384->W != NULL) { - ForceZero(sha384->W, sizeof(word64) * 16); + ForceZero(sha384->W, (sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE); XFREE(sha384->W, sha384->heap, DYNAMIC_TYPE_TMP_BUFFER); sha384->W = NULL; } @@ -2219,7 +2232,12 @@ int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst) XMEMCPY(dst, src, sizeof(wc_Sha512)); #ifdef WOLFSSL_SMALL_STACK_CACHE - dst->W = NULL; + dst->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE, + dst->heap, DYNAMIC_TYPE_DIGEST); + if (dst->W == NULL) { + XMEMSET(dst, 0, sizeof(wc_Sha512)); + return MEMORY_E; + } #endif #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \ @@ -2649,7 +2667,12 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst) XMEMCPY(dst, src, sizeof(wc_Sha384)); #ifdef WOLFSSL_SMALL_STACK_CACHE - dst->W = NULL; + dst->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA384_BLOCK_SIZE, + dst->heap, DYNAMIC_TYPE_DIGEST); + if (dst->W == NULL) { + XMEMSET(dst, 0, sizeof(wc_Sha384)); + return MEMORY_E; + } #endif #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \