From ca150724b340647de13dcf0ca767f09921c0f2d9 Mon Sep 17 00:00:00 2001 From: night1rider Date: Wed, 25 Feb 2026 12:48:20 -0700 Subject: [PATCH] Revert "Fix SHA3/Shake copy cleanup tests to heap-allocate shaCopy to avoid exceeding stack frame limit." This reverts commit d99fe3bbfdaa26250dcb030127adc7a31c7d9e6b. --- wolfcrypt/test/test.c | 126 ++++++++++++------------------------------ 1 file changed, 36 insertions(+), 90 deletions(-) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 24ebb4f810..80743ff8cc 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -5990,9 +5990,7 @@ exit: #ifndef WOLFSSL_NOSHA3_224 static wc_test_ret_t sha3_224_test(void) { - wc_Sha3 sha; - /* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */ - wc_Sha3 *shaCopy = NULL; + wc_Sha3 sha, shaCopy; byte hash[WC_SHA3_224_DIGEST_SIZE]; byte hashcopy[WC_SHA3_224_DIGEST_SIZE]; @@ -6072,26 +6070,22 @@ static wc_test_ret_t sha3_224_test(void) /* Copy cleanup test: verify Copy into a previously-used dst does not leak * resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */ - shaCopy = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), HEAP_HINT, - DYNAMIC_TYPE_TMP_BUFFER); - if (shaCopy == NULL) - ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit); ret = wc_InitSha3_224(&sha, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_InitSha3_224(shaCopy, HEAP_HINT, devId); + ret = wc_InitSha3_224(&shaCopy, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_224_Update(shaCopy, (byte*)b.input, (word32)b.inLen); + ret = wc_Sha3_224_Update(&shaCopy, (byte*)b.input, (word32)b.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); ret = wc_Sha3_224_Update(&sha, (byte*)a.input, (word32)a.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_224_Copy(&sha, shaCopy); + ret = wc_Sha3_224_Copy(&sha, &shaCopy); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_224_Final(shaCopy, hash); + ret = wc_Sha3_224_Final(&shaCopy, hash); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); if (XMEMCMP(hash, a.output, WC_SHA3_224_DIGEST_SIZE) != 0) @@ -6099,10 +6093,7 @@ static wc_test_ret_t sha3_224_test(void) exit: wc_Sha3_224_Free(&sha); - if (shaCopy != NULL) { - wc_Sha3_224_Free(shaCopy); - XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - } + wc_Sha3_224_Free(&shaCopy); return ret; } @@ -6111,9 +6102,7 @@ exit: #ifndef WOLFSSL_NOSHA3_256 static wc_test_ret_t sha3_256_test(void) { - wc_Sha3 sha; - /* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */ - wc_Sha3 *shaCopy = NULL; + wc_Sha3 sha, shaCopy; byte hash[WC_SHA3_256_DIGEST_SIZE]; byte hashcopy[WC_SHA3_256_DIGEST_SIZE]; @@ -6226,26 +6215,22 @@ static wc_test_ret_t sha3_256_test(void) /* Copy cleanup test: verify Copy into a previously-used dst does not leak * resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */ - shaCopy = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), HEAP_HINT, - DYNAMIC_TYPE_TMP_BUFFER); - if (shaCopy == NULL) - ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit); ret = wc_InitSha3_256(&sha, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_InitSha3_256(shaCopy, HEAP_HINT, devId); + ret = wc_InitSha3_256(&shaCopy, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_256_Update(shaCopy, (byte*)b.input, (word32)b.inLen); + ret = wc_Sha3_256_Update(&shaCopy, (byte*)b.input, (word32)b.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); ret = wc_Sha3_256_Update(&sha, (byte*)a.input, (word32)a.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_256_Copy(&sha, shaCopy); + ret = wc_Sha3_256_Copy(&sha, &shaCopy); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_256_Final(shaCopy, hash); + ret = wc_Sha3_256_Final(&shaCopy, hash); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); if (XMEMCMP(hash, a.output, WC_SHA3_256_DIGEST_SIZE) != 0) @@ -6253,10 +6238,7 @@ static wc_test_ret_t sha3_256_test(void) exit: wc_Sha3_256_Free(&sha); - if (shaCopy != NULL) { - wc_Sha3_256_Free(shaCopy); - XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - } + wc_Sha3_256_Free(&shaCopy); return ret; } @@ -6265,9 +6247,7 @@ exit: #ifndef WOLFSSL_NOSHA3_384 static wc_test_ret_t sha3_384_test(void) { - wc_Sha3 sha; - /* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */ - wc_Sha3 *shaCopy = NULL; + wc_Sha3 sha, shaCopy; byte hash[WC_SHA3_384_DIGEST_SIZE]; byte buf[64]; #ifndef NO_INTM_HASH_TEST @@ -6380,26 +6360,22 @@ static wc_test_ret_t sha3_384_test(void) /* Copy cleanup test: verify Copy into a previously-used dst does not leak * resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */ - shaCopy = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), HEAP_HINT, - DYNAMIC_TYPE_TMP_BUFFER); - if (shaCopy == NULL) - ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit); ret = wc_InitSha3_384(&sha, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_InitSha3_384(shaCopy, HEAP_HINT, devId); + ret = wc_InitSha3_384(&shaCopy, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_384_Update(shaCopy, (byte*)b.input, (word32)b.inLen); + ret = wc_Sha3_384_Update(&shaCopy, (byte*)b.input, (word32)b.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); ret = wc_Sha3_384_Update(&sha, (byte*)a.input, (word32)a.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_384_Copy(&sha, shaCopy); + ret = wc_Sha3_384_Copy(&sha, &shaCopy); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_384_Final(shaCopy, hash); + ret = wc_Sha3_384_Final(&shaCopy, hash); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); if (XMEMCMP(hash, a.output, WC_SHA3_384_DIGEST_SIZE) != 0) @@ -6407,10 +6383,7 @@ static wc_test_ret_t sha3_384_test(void) exit: wc_Sha3_384_Free(&sha); - if (shaCopy != NULL) { - wc_Sha3_384_Free(shaCopy); - XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - } + wc_Sha3_384_Free(&shaCopy); return ret; } @@ -6419,9 +6392,7 @@ exit: #ifndef WOLFSSL_NOSHA3_512 static wc_test_ret_t sha3_512_test(void) { - wc_Sha3 sha; - /* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */ - wc_Sha3 *shaCopy = NULL; + wc_Sha3 sha, shaCopy; byte hash[WC_SHA3_512_DIGEST_SIZE]; byte hashcopy[WC_SHA3_512_DIGEST_SIZE]; @@ -6515,26 +6486,22 @@ static wc_test_ret_t sha3_512_test(void) /* Copy cleanup test: verify Copy into a previously-used dst does not leak * resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */ - shaCopy = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), HEAP_HINT, - DYNAMIC_TYPE_TMP_BUFFER); - if (shaCopy == NULL) - ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit); ret = wc_InitSha3_512(&sha, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_InitSha3_512(shaCopy, HEAP_HINT, devId); + ret = wc_InitSha3_512(&shaCopy, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_512_Update(shaCopy, (byte*)b.input, (word32)b.inLen); + ret = wc_Sha3_512_Update(&shaCopy, (byte*)b.input, (word32)b.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); ret = wc_Sha3_512_Update(&sha, (byte*)a.input, (word32)a.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_512_Copy(&sha, shaCopy); + ret = wc_Sha3_512_Copy(&sha, &shaCopy); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Sha3_512_Final(shaCopy, hash); + ret = wc_Sha3_512_Final(&shaCopy, hash); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); if (XMEMCMP(hash, a.output, WC_SHA3_512_DIGEST_SIZE) != 0) @@ -6542,10 +6509,7 @@ static wc_test_ret_t sha3_512_test(void) exit: wc_Sha3_512_Free(&sha); - if (shaCopy != NULL) { - wc_Sha3_512_Free(shaCopy); - XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - } + wc_Sha3_512_Free(&shaCopy); return ret; } @@ -6767,9 +6731,7 @@ exit: WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void) { - wc_Shake sha; - /* Heap-allocated to avoid exceeding stack frame limit with two wc_Shake */ - wc_Shake *shaCopy = NULL; + wc_Shake sha, shaCopy; byte hash[250]; testVector a, b, c, d, e; @@ -6930,26 +6892,22 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void) /* Copy cleanup test: verify Copy into a previously-used dst does not leak * resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */ - shaCopy = (wc_Shake*)XMALLOC(sizeof(wc_Shake), HEAP_HINT, - DYNAMIC_TYPE_TMP_BUFFER); - if (shaCopy == NULL) - ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit); ret = wc_InitShake128(&sha, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_InitShake128(shaCopy, HEAP_HINT, devId); + ret = wc_InitShake128(&shaCopy, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Shake128_Update(shaCopy, (byte*)b.input, (word32)b.inLen); + ret = wc_Shake128_Update(&shaCopy, (byte*)b.input, (word32)b.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); ret = wc_Shake128_Update(&sha, (byte*)a.input, (word32)a.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Shake128_Copy(&sha, shaCopy); + ret = wc_Shake128_Copy(&sha, &shaCopy); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Shake128_Final(shaCopy, hash, (word32)a.outLen); + ret = wc_Shake128_Final(&shaCopy, hash, (word32)a.outLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); if (XMEMCMP(hash, a.output, a.outLen) != 0) @@ -6957,10 +6915,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void) exit: wc_Shake128_Free(&sha); - if (shaCopy != NULL) { - wc_Shake128_Free(shaCopy); - XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - } + wc_Shake128_Free(&shaCopy); #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER); @@ -7142,9 +7097,7 @@ exit: WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void) { - wc_Shake sha; - /* Heap-allocated to avoid exceeding stack frame limit with two wc_Shake */ - wc_Shake *shaCopy = NULL; + wc_Shake sha, shaCopy; byte hash[250]; testVector a, b, c, d, e; @@ -7304,26 +7257,22 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void) /* Copy cleanup test: verify Copy into a previously-used dst does not leak * resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */ - shaCopy = (wc_Shake*)XMALLOC(sizeof(wc_Shake), HEAP_HINT, - DYNAMIC_TYPE_TMP_BUFFER); - if (shaCopy == NULL) - ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit); ret = wc_InitShake256(&sha, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_InitShake256(shaCopy, HEAP_HINT, devId); + ret = wc_InitShake256(&shaCopy, HEAP_HINT, devId); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Shake256_Update(shaCopy, (byte*)b.input, (word32)b.inLen); + ret = wc_Shake256_Update(&shaCopy, (byte*)b.input, (word32)b.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); ret = wc_Shake256_Update(&sha, (byte*)a.input, (word32)a.inLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Shake256_Copy(&sha, shaCopy); + ret = wc_Shake256_Copy(&sha, &shaCopy); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); - ret = wc_Shake256_Final(shaCopy, hash, (word32)a.outLen); + ret = wc_Shake256_Final(&shaCopy, hash, (word32)a.outLen); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); if (XMEMCMP(hash, a.output, a.outLen) != 0) @@ -7331,10 +7280,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void) exit: wc_Shake256_Free(&sha); - if (shaCopy != NULL) { - wc_Shake256_Free(shaCopy); - XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - } + wc_Shake256_Free(&shaCopy); #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);