Skip to content

Commit 4d2d4ff

Browse files
HMAC_DRBG entropy usage: test the exact amount of consumed entropy
1 parent 58b56ce commit 4d2d4ff

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

tests/suites/test_suite_hmac_drbg.function

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ void hmac_drbg_entropy_usage( int md_alg )
3737
const mbedtls_md_info_t *md_info;
3838
mbedtls_hmac_drbg_context ctx;
3939
entropy_ctx entropy;
40-
size_t last_len, i, reps = 10;
40+
size_t i, reps = 10;
41+
size_t default_entropy_len;
42+
size_t expected_consumed_entropy = 0;
4143

4244
mbedtls_hmac_drbg_init( &ctx );
4345
memset( buf, 0, sizeof( buf ) );
@@ -48,23 +50,29 @@ void hmac_drbg_entropy_usage( int md_alg )
4850

4951
md_info = mbedtls_md_info_from_type( md_alg );
5052
TEST_ASSERT( md_info != NULL );
53+
if( mbedtls_md_get_size( md_info ) <= 20 )
54+
default_entropy_len = 16;
55+
else if( mbedtls_md_get_size( md_info ) <= 28 )
56+
default_entropy_len = 24;
57+
else
58+
default_entropy_len = 32;
5159

5260
/* Init must use entropy */
53-
last_len = entropy.len;
5461
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &entropy,
5562
NULL, 0 ) == 0 );
56-
TEST_ASSERT( entropy.len < last_len );
63+
/* default_entropy_len of entropy, plus half as much for the nonce */
64+
expected_consumed_entropy += default_entropy_len * 3 / 2;
65+
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
5766

5867
/* By default, PR is off and reseed_interval is large,
5968
* so the next few calls should not use entropy */
60-
last_len = entropy.len;
6169
for( i = 0; i < reps; i++ )
6270
{
6371
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
6472
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
6573
buf, 16 ) == 0 );
6674
}
67-
TEST_ASSERT( entropy.len == last_len );
75+
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
6876

6977
/* While at it, make sure we didn't write past the requested length */
7078
TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
@@ -76,33 +84,34 @@ void hmac_drbg_entropy_usage( int md_alg )
7684
* so the next call should reseed */
7785
mbedtls_hmac_drbg_set_reseed_interval( &ctx, 2 * reps );
7886
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
79-
TEST_ASSERT( entropy.len < last_len );
87+
expected_consumed_entropy += default_entropy_len;
88+
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
8089

8190
/* The new few calls should not reseed */
82-
last_len = entropy.len;
8391
for( i = 0; i < reps / 2; i++ )
8492
{
8593
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
8694
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) ,
8795
buf, 16 ) == 0 );
8896
}
89-
TEST_ASSERT( entropy.len == last_len );
97+
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
9098

9199
/* Now enable PR, so the next few calls should all reseed */
92100
mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
93101
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
94-
TEST_ASSERT( entropy.len < last_len );
102+
expected_consumed_entropy += default_entropy_len;
103+
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
95104

96105
/* Finally, check setting entropy_len */
97106
mbedtls_hmac_drbg_set_entropy_len( &ctx, 42 );
98-
last_len = entropy.len;
99107
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
100-
TEST_ASSERT( (int) last_len - entropy.len == 42 );
108+
expected_consumed_entropy += 42;
109+
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
101110

102111
mbedtls_hmac_drbg_set_entropy_len( &ctx, 13 );
103-
last_len = entropy.len;
104112
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
105-
TEST_ASSERT( (int) last_len - entropy.len == 13 );
113+
expected_consumed_entropy += 13;
114+
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
106115

107116
exit:
108117
mbedtls_hmac_drbg_free( &ctx );

0 commit comments

Comments
 (0)