Skip to content

Commit b87cb3e

Browse files
committed
Fix SHA3/Shake copy cleanup tests to heap-allocate shaCopy to avoid exceeding stack frame limit.
1 parent 4713ad5 commit b87cb3e

1 file changed

Lines changed: 90 additions & 36 deletions

File tree

wolfcrypt/test/test.c

Lines changed: 90 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5990,7 +5990,9 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void)
59905990
#ifndef WOLFSSL_NOSHA3_224
59915991
static wc_test_ret_t sha3_224_test(void)
59925992
{
5993-
wc_Sha3 sha, shaCopy;
5993+
wc_Sha3 sha;
5994+
/* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */
5995+
wc_Sha3 *shaCopy = NULL;
59945996
byte hash[WC_SHA3_224_DIGEST_SIZE];
59955997
byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
59965998

@@ -6070,30 +6072,37 @@ static wc_test_ret_t sha3_224_test(void)
60706072

60716073
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
60726074
* resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */
6075+
shaCopy = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), HEAP_HINT,
6076+
DYNAMIC_TYPE_TMP_BUFFER);
6077+
if (shaCopy == NULL)
6078+
ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
60736079
ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
60746080
if (ret != 0)
60756081
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6076-
ret = wc_InitSha3_224(&shaCopy, HEAP_HINT, devId);
6082+
ret = wc_InitSha3_224(shaCopy, HEAP_HINT, devId);
60776083
if (ret != 0)
60786084
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6079-
ret = wc_Sha3_224_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
6085+
ret = wc_Sha3_224_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
60806086
if (ret != 0)
60816087
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
60826088
ret = wc_Sha3_224_Update(&sha, (byte*)a.input, (word32)a.inLen);
60836089
if (ret != 0)
60846090
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6085-
ret = wc_Sha3_224_Copy(&sha, &shaCopy);
6091+
ret = wc_Sha3_224_Copy(&sha, shaCopy);
60866092
if (ret != 0)
60876093
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6088-
ret = wc_Sha3_224_Final(&shaCopy, hash);
6094+
ret = wc_Sha3_224_Final(shaCopy, hash);
60896095
if (ret != 0)
60906096
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
60916097
if (XMEMCMP(hash, a.output, WC_SHA3_224_DIGEST_SIZE) != 0)
60926098
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
60936099

60946100
exit:
60956101
wc_Sha3_224_Free(&sha);
6096-
wc_Sha3_224_Free(&shaCopy);
6102+
if (shaCopy != NULL) {
6103+
wc_Sha3_224_Free(shaCopy);
6104+
XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
6105+
}
60976106

60986107
return ret;
60996108
}
@@ -6102,7 +6111,9 @@ static wc_test_ret_t sha3_224_test(void)
61026111
#ifndef WOLFSSL_NOSHA3_256
61036112
static wc_test_ret_t sha3_256_test(void)
61046113
{
6105-
wc_Sha3 sha, shaCopy;
6114+
wc_Sha3 sha;
6115+
/* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */
6116+
wc_Sha3 *shaCopy = NULL;
61066117
byte hash[WC_SHA3_256_DIGEST_SIZE];
61076118
byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
61086119

@@ -6215,30 +6226,37 @@ static wc_test_ret_t sha3_256_test(void)
62156226

62166227
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
62176228
* resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */
6229+
shaCopy = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), HEAP_HINT,
6230+
DYNAMIC_TYPE_TMP_BUFFER);
6231+
if (shaCopy == NULL)
6232+
ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
62186233
ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
62196234
if (ret != 0)
62206235
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6221-
ret = wc_InitSha3_256(&shaCopy, HEAP_HINT, devId);
6236+
ret = wc_InitSha3_256(shaCopy, HEAP_HINT, devId);
62226237
if (ret != 0)
62236238
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6224-
ret = wc_Sha3_256_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
6239+
ret = wc_Sha3_256_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
62256240
if (ret != 0)
62266241
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
62276242
ret = wc_Sha3_256_Update(&sha, (byte*)a.input, (word32)a.inLen);
62286243
if (ret != 0)
62296244
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6230-
ret = wc_Sha3_256_Copy(&sha, &shaCopy);
6245+
ret = wc_Sha3_256_Copy(&sha, shaCopy);
62316246
if (ret != 0)
62326247
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6233-
ret = wc_Sha3_256_Final(&shaCopy, hash);
6248+
ret = wc_Sha3_256_Final(shaCopy, hash);
62346249
if (ret != 0)
62356250
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
62366251
if (XMEMCMP(hash, a.output, WC_SHA3_256_DIGEST_SIZE) != 0)
62376252
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
62386253

62396254
exit:
62406255
wc_Sha3_256_Free(&sha);
6241-
wc_Sha3_256_Free(&shaCopy);
6256+
if (shaCopy != NULL) {
6257+
wc_Sha3_256_Free(shaCopy);
6258+
XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
6259+
}
62426260

62436261
return ret;
62446262
}
@@ -6247,7 +6265,9 @@ static wc_test_ret_t sha3_256_test(void)
62476265
#ifndef WOLFSSL_NOSHA3_384
62486266
static wc_test_ret_t sha3_384_test(void)
62496267
{
6250-
wc_Sha3 sha, shaCopy;
6268+
wc_Sha3 sha;
6269+
/* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */
6270+
wc_Sha3 *shaCopy = NULL;
62516271
byte hash[WC_SHA3_384_DIGEST_SIZE];
62526272
byte buf[64];
62536273
#ifndef NO_INTM_HASH_TEST
@@ -6360,30 +6380,37 @@ static wc_test_ret_t sha3_384_test(void)
63606380

63616381
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
63626382
* resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */
6383+
shaCopy = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), HEAP_HINT,
6384+
DYNAMIC_TYPE_TMP_BUFFER);
6385+
if (shaCopy == NULL)
6386+
ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
63636387
ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
63646388
if (ret != 0)
63656389
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6366-
ret = wc_InitSha3_384(&shaCopy, HEAP_HINT, devId);
6390+
ret = wc_InitSha3_384(shaCopy, HEAP_HINT, devId);
63676391
if (ret != 0)
63686392
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6369-
ret = wc_Sha3_384_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
6393+
ret = wc_Sha3_384_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
63706394
if (ret != 0)
63716395
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
63726396
ret = wc_Sha3_384_Update(&sha, (byte*)a.input, (word32)a.inLen);
63736397
if (ret != 0)
63746398
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6375-
ret = wc_Sha3_384_Copy(&sha, &shaCopy);
6399+
ret = wc_Sha3_384_Copy(&sha, shaCopy);
63766400
if (ret != 0)
63776401
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6378-
ret = wc_Sha3_384_Final(&shaCopy, hash);
6402+
ret = wc_Sha3_384_Final(shaCopy, hash);
63796403
if (ret != 0)
63806404
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
63816405
if (XMEMCMP(hash, a.output, WC_SHA3_384_DIGEST_SIZE) != 0)
63826406
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
63836407

63846408
exit:
63856409
wc_Sha3_384_Free(&sha);
6386-
wc_Sha3_384_Free(&shaCopy);
6410+
if (shaCopy != NULL) {
6411+
wc_Sha3_384_Free(shaCopy);
6412+
XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
6413+
}
63876414

63886415
return ret;
63896416
}
@@ -6392,7 +6419,9 @@ static wc_test_ret_t sha3_384_test(void)
63926419
#ifndef WOLFSSL_NOSHA3_512
63936420
static wc_test_ret_t sha3_512_test(void)
63946421
{
6395-
wc_Sha3 sha, shaCopy;
6422+
wc_Sha3 sha;
6423+
/* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */
6424+
wc_Sha3 *shaCopy = NULL;
63966425
byte hash[WC_SHA3_512_DIGEST_SIZE];
63976426
byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
63986427

@@ -6486,30 +6515,37 @@ static wc_test_ret_t sha3_512_test(void)
64866515

64876516
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
64886517
* resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */
6518+
shaCopy = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), HEAP_HINT,
6519+
DYNAMIC_TYPE_TMP_BUFFER);
6520+
if (shaCopy == NULL)
6521+
ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
64896522
ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
64906523
if (ret != 0)
64916524
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6492-
ret = wc_InitSha3_512(&shaCopy, HEAP_HINT, devId);
6525+
ret = wc_InitSha3_512(shaCopy, HEAP_HINT, devId);
64936526
if (ret != 0)
64946527
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6495-
ret = wc_Sha3_512_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
6528+
ret = wc_Sha3_512_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
64966529
if (ret != 0)
64976530
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
64986531
ret = wc_Sha3_512_Update(&sha, (byte*)a.input, (word32)a.inLen);
64996532
if (ret != 0)
65006533
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6501-
ret = wc_Sha3_512_Copy(&sha, &shaCopy);
6534+
ret = wc_Sha3_512_Copy(&sha, shaCopy);
65026535
if (ret != 0)
65036536
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6504-
ret = wc_Sha3_512_Final(&shaCopy, hash);
6537+
ret = wc_Sha3_512_Final(shaCopy, hash);
65056538
if (ret != 0)
65066539
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
65076540
if (XMEMCMP(hash, a.output, WC_SHA3_512_DIGEST_SIZE) != 0)
65086541
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
65096542

65106543
exit:
65116544
wc_Sha3_512_Free(&sha);
6512-
wc_Sha3_512_Free(&shaCopy);
6545+
if (shaCopy != NULL) {
6546+
wc_Sha3_512_Free(shaCopy);
6547+
XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
6548+
}
65136549

65146550
return ret;
65156551
}
@@ -6731,7 +6767,9 @@ static wc_test_ret_t shake128_absorb_test(wc_Shake* sha, byte *large_input_buf,
67316767

67326768
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void)
67336769
{
6734-
wc_Shake sha, shaCopy;
6770+
wc_Shake sha;
6771+
/* Heap-allocated to avoid exceeding stack frame limit with two wc_Shake */
6772+
wc_Shake *shaCopy = NULL;
67356773
byte hash[250];
67366774

67376775
testVector a, b, c, d, e;
@@ -6892,30 +6930,37 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void)
68926930

68936931
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
68946932
* resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */
6933+
shaCopy = (wc_Shake*)XMALLOC(sizeof(wc_Shake), HEAP_HINT,
6934+
DYNAMIC_TYPE_TMP_BUFFER);
6935+
if (shaCopy == NULL)
6936+
ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
68956937
ret = wc_InitShake128(&sha, HEAP_HINT, devId);
68966938
if (ret != 0)
68976939
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6898-
ret = wc_InitShake128(&shaCopy, HEAP_HINT, devId);
6940+
ret = wc_InitShake128(shaCopy, HEAP_HINT, devId);
68996941
if (ret != 0)
69006942
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6901-
ret = wc_Shake128_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
6943+
ret = wc_Shake128_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
69026944
if (ret != 0)
69036945
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
69046946
ret = wc_Shake128_Update(&sha, (byte*)a.input, (word32)a.inLen);
69056947
if (ret != 0)
69066948
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6907-
ret = wc_Shake128_Copy(&sha, &shaCopy);
6949+
ret = wc_Shake128_Copy(&sha, shaCopy);
69086950
if (ret != 0)
69096951
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6910-
ret = wc_Shake128_Final(&shaCopy, hash, (word32)a.outLen);
6952+
ret = wc_Shake128_Final(shaCopy, hash, (word32)a.outLen);
69116953
if (ret != 0)
69126954
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
69136955
if (XMEMCMP(hash, a.output, a.outLen) != 0)
69146956
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
69156957

69166958
exit:
69176959
wc_Shake128_Free(&sha);
6918-
wc_Shake128_Free(&shaCopy);
6960+
if (shaCopy != NULL) {
6961+
wc_Shake128_Free(shaCopy);
6962+
XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
6963+
}
69196964

69206965
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
69216966
XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
@@ -7097,7 +7142,9 @@ static wc_test_ret_t shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
70977142

70987143
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void)
70997144
{
7100-
wc_Shake sha, shaCopy;
7145+
wc_Shake sha;
7146+
/* Heap-allocated to avoid exceeding stack frame limit with two wc_Shake */
7147+
wc_Shake *shaCopy = NULL;
71017148
byte hash[250];
71027149

71037150
testVector a, b, c, d, e;
@@ -7257,30 +7304,37 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void)
72577304

72587305
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
72597306
* resources (e.g., hardware contexts). Detectable by valgrind/ASAN. */
7307+
shaCopy = (wc_Shake*)XMALLOC(sizeof(wc_Shake), HEAP_HINT,
7308+
DYNAMIC_TYPE_TMP_BUFFER);
7309+
if (shaCopy == NULL)
7310+
ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), exit);
72607311
ret = wc_InitShake256(&sha, HEAP_HINT, devId);
72617312
if (ret != 0)
72627313
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
7263-
ret = wc_InitShake256(&shaCopy, HEAP_HINT, devId);
7314+
ret = wc_InitShake256(shaCopy, HEAP_HINT, devId);
72647315
if (ret != 0)
72657316
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
7266-
ret = wc_Shake256_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
7317+
ret = wc_Shake256_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
72677318
if (ret != 0)
72687319
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
72697320
ret = wc_Shake256_Update(&sha, (byte*)a.input, (word32)a.inLen);
72707321
if (ret != 0)
72717322
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
7272-
ret = wc_Shake256_Copy(&sha, &shaCopy);
7323+
ret = wc_Shake256_Copy(&sha, shaCopy);
72737324
if (ret != 0)
72747325
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
7275-
ret = wc_Shake256_Final(&shaCopy, hash, (word32)a.outLen);
7326+
ret = wc_Shake256_Final(shaCopy, hash, (word32)a.outLen);
72767327
if (ret != 0)
72777328
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
72787329
if (XMEMCMP(hash, a.output, a.outLen) != 0)
72797330
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
72807331

72817332
exit:
72827333
wc_Shake256_Free(&sha);
7283-
wc_Shake256_Free(&shaCopy);
7334+
if (shaCopy != NULL) {
7335+
wc_Shake256_Free(shaCopy);
7336+
XFREE(shaCopy, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
7337+
}
72847338

72857339
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
72867340
XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);

0 commit comments

Comments
 (0)