Skip to content

Commit ca15072

Browse files
committed
Revert "Fix SHA3/Shake copy cleanup tests to heap-allocate shaCopy to avoid exceeding stack frame limit."
This reverts commit d99fe3b.
1 parent b87cb3e commit ca15072

1 file changed

Lines changed: 36 additions & 90 deletions

File tree

wolfcrypt/test/test.c

Lines changed: 36 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5990,9 +5990,7 @@ 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;
5994-
/* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */
5995-
wc_Sha3 *shaCopy = NULL;
5993+
wc_Sha3 sha, shaCopy;
59965994
byte hash[WC_SHA3_224_DIGEST_SIZE];
59975995
byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
59985996

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

60736071
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
60746072
* 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);
60796073
ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
60806074
if (ret != 0)
60816075
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6082-
ret = wc_InitSha3_224(shaCopy, HEAP_HINT, devId);
6076+
ret = wc_InitSha3_224(&shaCopy, HEAP_HINT, devId);
60836077
if (ret != 0)
60846078
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6085-
ret = wc_Sha3_224_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
6079+
ret = wc_Sha3_224_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
60866080
if (ret != 0)
60876081
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
60886082
ret = wc_Sha3_224_Update(&sha, (byte*)a.input, (word32)a.inLen);
60896083
if (ret != 0)
60906084
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6091-
ret = wc_Sha3_224_Copy(&sha, shaCopy);
6085+
ret = wc_Sha3_224_Copy(&sha, &shaCopy);
60926086
if (ret != 0)
60936087
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6094-
ret = wc_Sha3_224_Final(shaCopy, hash);
6088+
ret = wc_Sha3_224_Final(&shaCopy, hash);
60956089
if (ret != 0)
60966090
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
60976091
if (XMEMCMP(hash, a.output, WC_SHA3_224_DIGEST_SIZE) != 0)
60986092
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
60996093

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

61076098
return ret;
61086099
}
@@ -6111,9 +6102,7 @@ static wc_test_ret_t sha3_224_test(void)
61116102
#ifndef WOLFSSL_NOSHA3_256
61126103
static wc_test_ret_t sha3_256_test(void)
61136104
{
6114-
wc_Sha3 sha;
6115-
/* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */
6116-
wc_Sha3 *shaCopy = NULL;
6105+
wc_Sha3 sha, shaCopy;
61176106
byte hash[WC_SHA3_256_DIGEST_SIZE];
61186107
byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
61196108

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

62276216
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
62286217
* 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);
62336218
ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
62346219
if (ret != 0)
62356220
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6236-
ret = wc_InitSha3_256(shaCopy, HEAP_HINT, devId);
6221+
ret = wc_InitSha3_256(&shaCopy, HEAP_HINT, devId);
62376222
if (ret != 0)
62386223
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6239-
ret = wc_Sha3_256_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
6224+
ret = wc_Sha3_256_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
62406225
if (ret != 0)
62416226
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
62426227
ret = wc_Sha3_256_Update(&sha, (byte*)a.input, (word32)a.inLen);
62436228
if (ret != 0)
62446229
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6245-
ret = wc_Sha3_256_Copy(&sha, shaCopy);
6230+
ret = wc_Sha3_256_Copy(&sha, &shaCopy);
62466231
if (ret != 0)
62476232
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6248-
ret = wc_Sha3_256_Final(shaCopy, hash);
6233+
ret = wc_Sha3_256_Final(&shaCopy, hash);
62496234
if (ret != 0)
62506235
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
62516236
if (XMEMCMP(hash, a.output, WC_SHA3_256_DIGEST_SIZE) != 0)
62526237
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
62536238

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

62616243
return ret;
62626244
}
@@ -6265,9 +6247,7 @@ static wc_test_ret_t sha3_256_test(void)
62656247
#ifndef WOLFSSL_NOSHA3_384
62666248
static wc_test_ret_t sha3_384_test(void)
62676249
{
6268-
wc_Sha3 sha;
6269-
/* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */
6270-
wc_Sha3 *shaCopy = NULL;
6250+
wc_Sha3 sha, shaCopy;
62716251
byte hash[WC_SHA3_384_DIGEST_SIZE];
62726252
byte buf[64];
62736253
#ifndef NO_INTM_HASH_TEST
@@ -6380,37 +6360,30 @@ static wc_test_ret_t sha3_384_test(void)
63806360

63816361
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
63826362
* 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);
63876363
ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
63886364
if (ret != 0)
63896365
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6390-
ret = wc_InitSha3_384(shaCopy, HEAP_HINT, devId);
6366+
ret = wc_InitSha3_384(&shaCopy, HEAP_HINT, devId);
63916367
if (ret != 0)
63926368
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6393-
ret = wc_Sha3_384_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
6369+
ret = wc_Sha3_384_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
63946370
if (ret != 0)
63956371
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
63966372
ret = wc_Sha3_384_Update(&sha, (byte*)a.input, (word32)a.inLen);
63976373
if (ret != 0)
63986374
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6399-
ret = wc_Sha3_384_Copy(&sha, shaCopy);
6375+
ret = wc_Sha3_384_Copy(&sha, &shaCopy);
64006376
if (ret != 0)
64016377
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6402-
ret = wc_Sha3_384_Final(shaCopy, hash);
6378+
ret = wc_Sha3_384_Final(&shaCopy, hash);
64036379
if (ret != 0)
64046380
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
64056381
if (XMEMCMP(hash, a.output, WC_SHA3_384_DIGEST_SIZE) != 0)
64066382
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
64076383

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

64156388
return ret;
64166389
}
@@ -6419,9 +6392,7 @@ static wc_test_ret_t sha3_384_test(void)
64196392
#ifndef WOLFSSL_NOSHA3_512
64206393
static wc_test_ret_t sha3_512_test(void)
64216394
{
6422-
wc_Sha3 sha;
6423-
/* Heap-allocated to avoid exceeding stack frame limit with two wc_Sha3 */
6424-
wc_Sha3 *shaCopy = NULL;
6395+
wc_Sha3 sha, shaCopy;
64256396
byte hash[WC_SHA3_512_DIGEST_SIZE];
64266397
byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
64276398

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

65166487
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
65176488
* 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);
65226489
ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
65236490
if (ret != 0)
65246491
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6525-
ret = wc_InitSha3_512(shaCopy, HEAP_HINT, devId);
6492+
ret = wc_InitSha3_512(&shaCopy, HEAP_HINT, devId);
65266493
if (ret != 0)
65276494
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6528-
ret = wc_Sha3_512_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
6495+
ret = wc_Sha3_512_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
65296496
if (ret != 0)
65306497
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
65316498
ret = wc_Sha3_512_Update(&sha, (byte*)a.input, (word32)a.inLen);
65326499
if (ret != 0)
65336500
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6534-
ret = wc_Sha3_512_Copy(&sha, shaCopy);
6501+
ret = wc_Sha3_512_Copy(&sha, &shaCopy);
65356502
if (ret != 0)
65366503
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6537-
ret = wc_Sha3_512_Final(shaCopy, hash);
6504+
ret = wc_Sha3_512_Final(&shaCopy, hash);
65386505
if (ret != 0)
65396506
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
65406507
if (XMEMCMP(hash, a.output, WC_SHA3_512_DIGEST_SIZE) != 0)
65416508
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
65426509

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

65506514
return ret;
65516515
}
@@ -6767,9 +6731,7 @@ static wc_test_ret_t shake128_absorb_test(wc_Shake* sha, byte *large_input_buf,
67676731

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

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

69316893
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
69326894
* 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);
69376895
ret = wc_InitShake128(&sha, HEAP_HINT, devId);
69386896
if (ret != 0)
69396897
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6940-
ret = wc_InitShake128(shaCopy, HEAP_HINT, devId);
6898+
ret = wc_InitShake128(&shaCopy, HEAP_HINT, devId);
69416899
if (ret != 0)
69426900
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6943-
ret = wc_Shake128_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
6901+
ret = wc_Shake128_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
69446902
if (ret != 0)
69456903
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
69466904
ret = wc_Shake128_Update(&sha, (byte*)a.input, (word32)a.inLen);
69476905
if (ret != 0)
69486906
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6949-
ret = wc_Shake128_Copy(&sha, shaCopy);
6907+
ret = wc_Shake128_Copy(&sha, &shaCopy);
69506908
if (ret != 0)
69516909
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
6952-
ret = wc_Shake128_Final(shaCopy, hash, (word32)a.outLen);
6910+
ret = wc_Shake128_Final(&shaCopy, hash, (word32)a.outLen);
69536911
if (ret != 0)
69546912
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
69556913
if (XMEMCMP(hash, a.output, a.outLen) != 0)
69566914
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
69576915

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

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

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

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

73057258
/* Copy cleanup test: verify Copy into a previously-used dst does not leak
73067259
* 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);
73117260
ret = wc_InitShake256(&sha, HEAP_HINT, devId);
73127261
if (ret != 0)
73137262
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
7314-
ret = wc_InitShake256(shaCopy, HEAP_HINT, devId);
7263+
ret = wc_InitShake256(&shaCopy, HEAP_HINT, devId);
73157264
if (ret != 0)
73167265
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
7317-
ret = wc_Shake256_Update(shaCopy, (byte*)b.input, (word32)b.inLen);
7266+
ret = wc_Shake256_Update(&shaCopy, (byte*)b.input, (word32)b.inLen);
73187267
if (ret != 0)
73197268
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
73207269
ret = wc_Shake256_Update(&sha, (byte*)a.input, (word32)a.inLen);
73217270
if (ret != 0)
73227271
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
7323-
ret = wc_Shake256_Copy(&sha, shaCopy);
7272+
ret = wc_Shake256_Copy(&sha, &shaCopy);
73247273
if (ret != 0)
73257274
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
7326-
ret = wc_Shake256_Final(shaCopy, hash, (word32)a.outLen);
7275+
ret = wc_Shake256_Final(&shaCopy, hash, (word32)a.outLen);
73277276
if (ret != 0)
73287277
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
73297278
if (XMEMCMP(hash, a.output, a.outLen) != 0)
73307279
ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
73317280

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

73397285
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
73407286
XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);

0 commit comments

Comments
 (0)