Skip to content

Commit eb85f18

Browse files
committed
rework RNG initialization/free in crypto tests to prevent uninitialized use
1 parent 4087366 commit eb85f18

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

test/wh_test_crypto.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ static int whTest_CryptoRng(whClientContext* ctx, int devId, WC_RNG* rng)
134134
uint8_t med[WH_TEST_RNG_MED];
135135
uint8_t big[WH_TEST_RNG_BIG];
136136

137-
/* test rng. Note this rng is used for many tests so is left inited */
138137
ret = wc_InitRng_ex(rng, NULL, devId);
139138
if (ret != 0) {
140139
WH_ERROR_PRINT("Failed to wc_InitRng_ex %d\n", ret);
@@ -152,6 +151,10 @@ static int whTest_CryptoRng(whClientContext* ctx, int devId, WC_RNG* rng)
152151
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
153152
}
154153
}
154+
ret = wc_FreeRng(rng);
155+
if (ret != 0) {
156+
WH_ERROR_PRINT("Failed to wc_FreeRng %d\n", ret);
157+
}
155158
}
156159
}
157160
if (ret == 0) {
@@ -3561,6 +3564,8 @@ int whTest_CryptoClientConfig(whClientConfig* config)
35613564
}
35623565
#endif /* WOLFHSM_CFG_TEST_VERBOSE */
35633566

3567+
/* First crypto test should be of RNG so we can iterate over and test all
3568+
* devIds before choosing one to run the rest of the tests on */
35643569
i = 0;
35653570
while ((ret == WH_ERROR_OK) && (i < WH_NUM_DEVIDS)) {
35663571
ret = whTest_CryptoRng(client, WH_DEV_IDS_ARRAY[i], rng);
@@ -3570,6 +3575,16 @@ int whTest_CryptoClientConfig(whClientConfig* config)
35703575
}
35713576
}
35723577

3578+
/* Now that we have tested all RNG devIds, reinitialize the default RNG
3579+
* devId (non-DMA) that will be used by the remainder of the tests for
3580+
* random input generation */
3581+
if (ret == 0) {
3582+
ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID);
3583+
if (ret != 0) {
3584+
WH_ERROR_PRINT("Failed to reinitialize RNG %d\n", ret);
3585+
}
3586+
}
3587+
35733588
if (ret == 0) {
35743589
/* Test Key Cache functions */
35753590
ret = whTest_KeyCache(client, WH_DEV_ID, rng);

0 commit comments

Comments
 (0)