Skip to content

Commit 58b56ce

Browse files
CTR_DRBG entropy usage: test the exact amount of consumed entropy
1 parent 97f59ab commit 58b56ce

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

tests/suites/test_suite_ctr_drbg.function

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void ctr_drbg_entropy_usage( )
197197
unsigned char entropy[1024];
198198
mbedtls_ctr_drbg_context ctx;
199199
size_t i, reps = 10;
200-
size_t last_idx;
200+
size_t expected_idx = 0;
201201

202202
mbedtls_ctr_drbg_init( &ctx );
203203
test_offset_idx = 0;
@@ -207,20 +207,19 @@ void ctr_drbg_entropy_usage( )
207207
memset( add, 0, sizeof( add ) );
208208

209209
/* Init must use entropy */
210-
last_idx = test_offset_idx;
211210
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_test_entropy_func, entropy, NULL, 0 ) == 0 );
212-
TEST_ASSERT( last_idx < test_offset_idx );
211+
expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
212+
TEST_EQUAL( test_offset_idx, expected_idx );
213213

214214
/* By default, PR is off and reseed_interval is large,
215215
* so the next few calls should not use entropy */
216-
last_idx = test_offset_idx;
217216
for( i = 0; i < reps; i++ )
218217
{
219218
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
220219
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
221220
add, sizeof( add ) ) == 0 );
222221
}
223-
TEST_ASSERT( last_idx == test_offset_idx );
222+
TEST_EQUAL( test_offset_idx, expected_idx );
224223

225224
/* While at it, make sure we didn't write past the requested length */
226225
TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
@@ -232,17 +231,17 @@ void ctr_drbg_entropy_usage( )
232231
* so the next call should reseed */
233232
mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
234233
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
235-
TEST_ASSERT( last_idx < test_offset_idx );
234+
expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
235+
TEST_EQUAL( test_offset_idx, expected_idx );
236236

237237
/* The new few calls should not reseed */
238-
last_idx = test_offset_idx;
239238
for( i = 0; i < reps / 2; i++ )
240239
{
241240
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
242241
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
243242
add, sizeof( add ) ) == 0 );
244243
}
245-
TEST_ASSERT( last_idx == test_offset_idx );
244+
TEST_EQUAL( test_offset_idx, expected_idx );
246245

247246
/* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT).
248247
* Make sure it's detected as an error and doesn't cause memory
@@ -253,18 +252,19 @@ void ctr_drbg_entropy_usage( )
253252
/* Now enable PR, so the next few calls should all reseed */
254253
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
255254
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
256-
TEST_ASSERT( last_idx < test_offset_idx );
255+
expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
256+
TEST_EQUAL( test_offset_idx, expected_idx );
257257

258258
/* Finally, check setting entropy_len */
259259
mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 );
260-
last_idx = test_offset_idx;
261260
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
262-
TEST_ASSERT( test_offset_idx - last_idx == 42 );
261+
expected_idx += 42;
262+
TEST_EQUAL( test_offset_idx, expected_idx );
263263

264264
mbedtls_ctr_drbg_set_entropy_len( &ctx, 13 );
265-
last_idx = test_offset_idx;
266265
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
267-
TEST_ASSERT( test_offset_idx - last_idx == 13 );
266+
expected_idx += 13;
267+
TEST_EQUAL( test_offset_idx, expected_idx );
268268

269269
exit:
270270
mbedtls_ctr_drbg_free( &ctx );

0 commit comments

Comments
 (0)