mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-26 20:55:36 +01:00
CTR_DRBG entropy usage: test the exact amount of consumed entropy
This commit is contained in:
parent
97f59ab527
commit
58b56ce444
@ -197,7 +197,7 @@ void ctr_drbg_entropy_usage( )
|
||||
unsigned char entropy[1024];
|
||||
mbedtls_ctr_drbg_context ctx;
|
||||
size_t i, reps = 10;
|
||||
size_t last_idx;
|
||||
size_t expected_idx = 0;
|
||||
|
||||
mbedtls_ctr_drbg_init( &ctx );
|
||||
test_offset_idx = 0;
|
||||
@ -207,20 +207,19 @@ void ctr_drbg_entropy_usage( )
|
||||
memset( add, 0, sizeof( add ) );
|
||||
|
||||
/* Init must use entropy */
|
||||
last_idx = test_offset_idx;
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_test_entropy_func, entropy, NULL, 0 ) == 0 );
|
||||
TEST_ASSERT( last_idx < test_offset_idx );
|
||||
expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
|
||||
TEST_EQUAL( test_offset_idx, expected_idx );
|
||||
|
||||
/* By default, PR is off and reseed_interval is large,
|
||||
* so the next few calls should not use entropy */
|
||||
last_idx = test_offset_idx;
|
||||
for( i = 0; i < reps; i++ )
|
||||
{
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
|
||||
add, sizeof( add ) ) == 0 );
|
||||
}
|
||||
TEST_ASSERT( last_idx == test_offset_idx );
|
||||
TEST_EQUAL( test_offset_idx, expected_idx );
|
||||
|
||||
/* While at it, make sure we didn't write past the requested length */
|
||||
TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
|
||||
@ -232,17 +231,17 @@ void ctr_drbg_entropy_usage( )
|
||||
* so the next call should reseed */
|
||||
mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
||||
TEST_ASSERT( last_idx < test_offset_idx );
|
||||
expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
|
||||
TEST_EQUAL( test_offset_idx, expected_idx );
|
||||
|
||||
/* The new few calls should not reseed */
|
||||
last_idx = test_offset_idx;
|
||||
for( i = 0; i < reps / 2; i++ )
|
||||
{
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
|
||||
add, sizeof( add ) ) == 0 );
|
||||
}
|
||||
TEST_ASSERT( last_idx == test_offset_idx );
|
||||
TEST_EQUAL( test_offset_idx, expected_idx );
|
||||
|
||||
/* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT).
|
||||
* Make sure it's detected as an error and doesn't cause memory
|
||||
@ -253,18 +252,19 @@ void ctr_drbg_entropy_usage( )
|
||||
/* Now enable PR, so the next few calls should all reseed */
|
||||
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
||||
TEST_ASSERT( last_idx < test_offset_idx );
|
||||
expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
|
||||
TEST_EQUAL( test_offset_idx, expected_idx );
|
||||
|
||||
/* Finally, check setting entropy_len */
|
||||
mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 );
|
||||
last_idx = test_offset_idx;
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
||||
TEST_ASSERT( test_offset_idx - last_idx == 42 );
|
||||
expected_idx += 42;
|
||||
TEST_EQUAL( test_offset_idx, expected_idx );
|
||||
|
||||
mbedtls_ctr_drbg_set_entropy_len( &ctx, 13 );
|
||||
last_idx = test_offset_idx;
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
||||
TEST_ASSERT( test_offset_idx - last_idx == 13 );
|
||||
expected_idx += 13;
|
||||
TEST_EQUAL( test_offset_idx, expected_idx );
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctx );
|
||||
|
Loading…
Reference in New Issue
Block a user