@@ -197,7 +197,7 @@ void ctr_drbg_entropy_usage( )
197
197
unsigned char entropy[1024];
198
198
mbedtls_ctr_drbg_context ctx;
199
199
size_t i, reps = 10;
200
- size_t last_idx ;
200
+ size_t expected_idx = 0 ;
201
201
202
202
mbedtls_ctr_drbg_init( &ctx );
203
203
test_offset_idx = 0;
@@ -207,20 +207,19 @@ void ctr_drbg_entropy_usage( )
207
207
memset( add, 0, sizeof( add ) );
208
208
209
209
/* Init must use entropy */
210
- last_idx = test_offset_idx;
211
210
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 );
213
213
214
214
/* By default, PR is off and reseed_interval is large,
215
215
* so the next few calls should not use entropy */
216
- last_idx = test_offset_idx;
217
216
for( i = 0; i < reps; i++ )
218
217
{
219
218
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
220
219
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
221
220
add, sizeof( add ) ) == 0 );
222
221
}
223
- TEST_ASSERT( last_idx == test_offset_idx );
222
+ TEST_EQUAL( test_offset_idx, expected_idx );
224
223
225
224
/* While at it, make sure we didn't write past the requested length */
226
225
TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
@@ -232,17 +231,17 @@ void ctr_drbg_entropy_usage( )
232
231
* so the next call should reseed */
233
232
mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
234
233
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 );
236
236
237
237
/* The new few calls should not reseed */
238
- last_idx = test_offset_idx;
239
238
for( i = 0; i < reps / 2; i++ )
240
239
{
241
240
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
242
241
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
243
242
add, sizeof( add ) ) == 0 );
244
243
}
245
- TEST_ASSERT( last_idx == test_offset_idx );
244
+ TEST_EQUAL( test_offset_idx, expected_idx );
246
245
247
246
/* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT).
248
247
* Make sure it's detected as an error and doesn't cause memory
@@ -253,18 +252,19 @@ void ctr_drbg_entropy_usage( )
253
252
/* Now enable PR, so the next few calls should all reseed */
254
253
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
255
254
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 );
257
257
258
258
/* Finally, check setting entropy_len */
259
259
mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 );
260
- last_idx = test_offset_idx;
261
260
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 );
263
263
264
264
mbedtls_ctr_drbg_set_entropy_len( &ctx, 13 );
265
- last_idx = test_offset_idx;
266
265
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 );
268
268
269
269
exit:
270
270
mbedtls_ctr_drbg_free( &ctx );
0 commit comments