@@ -37,7 +37,9 @@ void hmac_drbg_entropy_usage( int md_alg )
37
37
const mbedtls_md_info_t *md_info;
38
38
mbedtls_hmac_drbg_context ctx;
39
39
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;
41
43
42
44
mbedtls_hmac_drbg_init( &ctx );
43
45
memset( buf, 0, sizeof( buf ) );
@@ -48,23 +50,29 @@ void hmac_drbg_entropy_usage( int md_alg )
48
50
49
51
md_info = mbedtls_md_info_from_type( md_alg );
50
52
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;
51
59
52
60
/* Init must use entropy */
53
- last_len = entropy.len;
54
61
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &entropy,
55
62
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 );
57
66
58
67
/* By default, PR is off and reseed_interval is large,
59
68
* so the next few calls should not use entropy */
60
- last_len = entropy.len;
61
69
for( i = 0; i < reps; i++ )
62
70
{
63
71
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
64
72
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
65
73
buf, 16 ) == 0 );
66
74
}
67
- TEST_ASSERT( entropy.len == last_len );
75
+ TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
68
76
69
77
/* While at it, make sure we didn't write past the requested length */
70
78
TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
@@ -76,33 +84,34 @@ void hmac_drbg_entropy_usage( int md_alg )
76
84
* so the next call should reseed */
77
85
mbedtls_hmac_drbg_set_reseed_interval( &ctx, 2 * reps );
78
86
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 );
80
89
81
90
/* The new few calls should not reseed */
82
- last_len = entropy.len;
83
91
for( i = 0; i < reps / 2; i++ )
84
92
{
85
93
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
86
94
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) ,
87
95
buf, 16 ) == 0 );
88
96
}
89
- TEST_ASSERT( entropy.len == last_len );
97
+ TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
90
98
91
99
/* Now enable PR, so the next few calls should all reseed */
92
100
mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
93
101
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 );
95
104
96
105
/* Finally, check setting entropy_len */
97
106
mbedtls_hmac_drbg_set_entropy_len( &ctx, 42 );
98
- last_len = entropy.len;
99
107
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 );
101
110
102
111
mbedtls_hmac_drbg_set_entropy_len( &ctx, 13 );
103
- last_len = entropy.len;
104
112
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 );
106
115
107
116
exit:
108
117
mbedtls_hmac_drbg_free( &ctx );
0 commit comments