Skip to content

Commit 5fe1885

Browse files
CTR_DRBG: Test that (re)seeding grabs the expected amount of entropy
The code was testing that (re)seeding grabs at least one byte of entropy. Make this test more precise: we know exactly how many bytes are supposed to be consumed.
1 parent c06f3b5 commit 5fe1885

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

tests/suites/test_suite_ctr_drbg.function

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,11 @@ void ctr_drbg_entropy_usage( )
210210
memset( out, 0, sizeof( out ) );
211211
memset( add, 0, sizeof( add ) );
212212

213-
/* Init must use entropy */
213+
/* Init must use entropy for the entropy input and for the nonce. */
214214
last_idx = test_offset_idx;
215215
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_test_entropy_func, entropy, NULL, 0 ) == 0 );
216-
TEST_ASSERT( last_idx < test_offset_idx );
216+
TEST_EQUAL( test_offset_idx - last_idx,
217+
MBEDTLS_CTR_DRBG_ENTROPY_LEN + MBEDTLS_CTR_DRBG_NONCE_LEN );
217218

218219
/* By default, PR is off and reseed_interval is large,
219220
* so the next few calls should not use entropy */
@@ -257,7 +258,8 @@ void ctr_drbg_entropy_usage( )
257258
/* Now enable PR, so the next few calls should all reseed */
258259
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
259260
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
260-
TEST_ASSERT( last_idx < test_offset_idx );
261+
TEST_EQUAL( test_offset_idx - last_idx,
262+
MBEDTLS_CTR_DRBG_ENTROPY_LEN );
261263

262264
/* Finally, check setting entropy_len */
263265
mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 );

0 commit comments

Comments
 (0)