Split mbedtls_hmac_drbg_init() -> seed{,_buf}()

This commit is contained in:
Manuel Pégourié-Gonnard 2015-04-28 22:07:14 +02:00
parent c34e8dd265
commit f9e9481bc5
6 changed files with 53 additions and 22 deletions

View file

@ -38,6 +38,7 @@ void hmac_drbg_entropy_usage( int md_alg )
entropy_ctx entropy;
size_t last_len, i, reps = 10;
mbedtls_hmac_drbg_init( &ctx );
memset( buf, 0, sizeof( buf ) );
memset( out, 0, sizeof( out ) );
@ -49,7 +50,7 @@ void hmac_drbg_entropy_usage( int md_alg )
/* Init must use entropy */
last_len = entropy.len;
TEST_ASSERT( mbedtls_hmac_drbg_init( &ctx, md_info, mbedtls_entropy_func, &entropy,
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_entropy_func, &entropy,
NULL, 0 ) == 0 );
TEST_ASSERT( entropy.len < last_len );
@ -113,10 +114,12 @@ void hmac_drbg_seed_file( int md_alg, char *path, int ret )
const mbedtls_md_info_t *md_info;
mbedtls_hmac_drbg_context ctx;
mbedtls_hmac_drbg_init( &ctx );
md_info = mbedtls_md_info_from_type( md_alg );
TEST_ASSERT( md_info != NULL );
TEST_ASSERT( mbedtls_hmac_drbg_init( &ctx, md_info, rnd_std_rand, NULL,
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, rnd_std_rand, NULL,
NULL, 0 ) == 0 );
TEST_ASSERT( mbedtls_hmac_drbg_write_seed_file( &ctx, path ) == ret );
@ -136,12 +139,13 @@ void hmac_drbg_buf( int md_alg )
mbedtls_hmac_drbg_context ctx;
size_t i;
mbedtls_hmac_drbg_init( &ctx );
memset( buf, 0, sizeof( buf ) );
memset( out, 0, sizeof( out ) );
md_info = mbedtls_md_info_from_type( md_alg );
TEST_ASSERT( md_info != NULL );
TEST_ASSERT( mbedtls_hmac_drbg_init_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 );
TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 );
/* Make sure it never tries to reseed (would segfault otherwise) */
mbedtls_hmac_drbg_set_reseed_interval( &ctx, 3 );
@ -173,6 +177,7 @@ void hmac_drbg_no_reseed( int md_alg,
const mbedtls_md_info_t *md_info;
mbedtls_hmac_drbg_context ctx;
mbedtls_hmac_drbg_init( &ctx );
memset( my_output, 0, sizeof my_output );
custom_len = unhexify( custom, custom_hex );
@ -188,7 +193,7 @@ void hmac_drbg_no_reseed( int md_alg,
/* Test the simplified buffer-based variant */
memcpy( data, entropy, p_entropy.len );
memcpy( data + p_entropy.len, custom, custom_len );
TEST_ASSERT( mbedtls_hmac_drbg_init_buf( &ctx, md_info,
TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info,
data, p_entropy.len + custom_len ) == 0 );
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len,
add1, add1_len ) == 0 );
@ -201,7 +206,7 @@ void hmac_drbg_no_reseed( int md_alg,
TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
/* And now the normal entropy-based variant */
TEST_ASSERT( mbedtls_hmac_drbg_init( &ctx, md_info, mbedtls_entropy_func, &p_entropy,
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_entropy_func, &p_entropy,
custom, custom_len ) == 0 );
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len,
add1, add1_len ) == 0 );
@ -232,6 +237,7 @@ void hmac_drbg_nopr( int md_alg,
const mbedtls_md_info_t *md_info;
mbedtls_hmac_drbg_context ctx;
mbedtls_hmac_drbg_init( &ctx );
memset( my_output, 0, sizeof my_output );
custom_len = unhexify( custom, custom_hex );
@ -245,7 +251,7 @@ void hmac_drbg_nopr( int md_alg,
md_info = mbedtls_md_info_from_type( md_alg );
TEST_ASSERT( md_info != NULL );
TEST_ASSERT( mbedtls_hmac_drbg_init( &ctx, md_info, mbedtls_entropy_func, &p_entropy,
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_entropy_func, &p_entropy,
custom, custom_len ) == 0 );
TEST_ASSERT( mbedtls_hmac_drbg_reseed( &ctx, add1, add1_len ) == 0 );
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len,
@ -277,6 +283,7 @@ void hmac_drbg_pr( int md_alg,
const mbedtls_md_info_t *md_info;
mbedtls_hmac_drbg_context ctx;
mbedtls_hmac_drbg_init( &ctx );
memset( my_output, 0, sizeof my_output );
custom_len = unhexify( custom, custom_hex );
@ -289,7 +296,7 @@ void hmac_drbg_pr( int md_alg,
md_info = mbedtls_md_info_from_type( md_alg );
TEST_ASSERT( md_info != NULL );
TEST_ASSERT( mbedtls_hmac_drbg_init( &ctx, md_info, mbedtls_entropy_func, &p_entropy,
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_entropy_func, &p_entropy,
custom, custom_len ) == 0 );
mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len,