2014-01-30 17:22:14 +01:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/hmac_drbg.h"
|
2017-04-06 12:55:43 +02:00
|
|
|
#include "string.h"
|
2015-02-06 14:43:58 +01:00
|
|
|
|
2014-01-30 17:22:14 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned char *p;
|
|
|
|
size_t len;
|
|
|
|
} entropy_ctx;
|
|
|
|
|
2017-10-19 09:49:19 +02:00
|
|
|
static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len )
|
2014-01-30 17:22:14 +01:00
|
|
|
{
|
|
|
|
entropy_ctx *ctx = (entropy_ctx *) data;
|
|
|
|
|
|
|
|
if( len > ctx->len )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
memcpy( buf, ctx->p, len );
|
|
|
|
|
|
|
|
ctx->p += len;
|
|
|
|
ctx->len -= len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_HMAC_DRBG_C
|
2014-01-30 17:22:14 +01:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2014-01-30 22:39:42 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void hmac_drbg_entropy_usage( int md_alg )
|
|
|
|
{
|
|
|
|
unsigned char out[16];
|
|
|
|
unsigned char buf[1024];
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_hmac_drbg_context ctx;
|
2014-01-30 22:39:42 +01:00
|
|
|
entropy_ctx entropy;
|
2019-10-22 19:10:33 +02:00
|
|
|
size_t i, reps = 10;
|
|
|
|
size_t default_entropy_len;
|
|
|
|
size_t expected_consumed_entropy = 0;
|
2014-01-30 22:39:42 +01:00
|
|
|
|
2015-04-28 22:07:14 +02:00
|
|
|
mbedtls_hmac_drbg_init( &ctx );
|
2014-01-30 22:39:42 +01:00
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
memset( out, 0, sizeof( out ) );
|
|
|
|
|
|
|
|
entropy.len = sizeof( buf );
|
|
|
|
entropy.p = buf;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_type( md_alg );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2019-10-22 19:10:33 +02:00
|
|
|
if( mbedtls_md_get_size( md_info ) <= 20 )
|
|
|
|
default_entropy_len = 16;
|
|
|
|
else if( mbedtls_md_get_size( md_info ) <= 28 )
|
|
|
|
default_entropy_len = 24;
|
|
|
|
else
|
|
|
|
default_entropy_len = 32;
|
2014-01-30 22:39:42 +01:00
|
|
|
|
|
|
|
/* Init must use entropy */
|
2017-10-19 09:49:19 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &entropy,
|
2014-01-30 22:39:42 +01:00
|
|
|
NULL, 0 ) == 0 );
|
2019-10-22 19:10:33 +02:00
|
|
|
/* default_entropy_len of entropy, plus half as much for the nonce */
|
|
|
|
expected_consumed_entropy += default_entropy_len * 3 / 2;
|
|
|
|
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
|
2014-01-30 22:39:42 +01:00
|
|
|
|
|
|
|
/* By default, PR is off and reseed_interval is large,
|
|
|
|
* so the next few calls should not use entropy */
|
|
|
|
for( i = 0; i < reps; i++ )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
|
2014-01-30 22:39:42 +01:00
|
|
|
buf, 16 ) == 0 );
|
|
|
|
}
|
2019-10-22 19:10:33 +02:00
|
|
|
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
|
2014-01-30 22:39:42 +01:00
|
|
|
|
|
|
|
/* While at it, make sure we didn't write past the requested length */
|
|
|
|
TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
|
|
|
|
TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
|
|
|
|
TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
|
|
|
|
TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
|
|
|
|
|
|
|
|
/* Set reseed_interval to the number of calls done,
|
|
|
|
* so the next call should reseed */
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_set_reseed_interval( &ctx, 2 * reps );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
2019-10-22 19:10:33 +02:00
|
|
|
expected_consumed_entropy += default_entropy_len;
|
|
|
|
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
|
2014-01-30 22:39:42 +01:00
|
|
|
|
|
|
|
/* The new few calls should not reseed */
|
|
|
|
for( i = 0; i < reps / 2; i++ )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) ,
|
2014-01-30 22:39:42 +01:00
|
|
|
buf, 16 ) == 0 );
|
|
|
|
}
|
2019-10-22 19:10:33 +02:00
|
|
|
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
|
2014-01-30 22:39:42 +01:00
|
|
|
|
|
|
|
/* Now enable PR, so the next few calls should all reseed */
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
2019-10-22 19:10:33 +02:00
|
|
|
expected_consumed_entropy += default_entropy_len;
|
|
|
|
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
|
2014-01-30 22:39:42 +01:00
|
|
|
|
|
|
|
/* Finally, check setting entropy_len */
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_set_entropy_len( &ctx, 42 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
2019-10-22 19:10:33 +02:00
|
|
|
expected_consumed_entropy += 42;
|
|
|
|
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
|
2014-01-30 22:39:42 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_set_entropy_len( &ctx, 13 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
2019-10-22 19:10:33 +02:00
|
|
|
expected_consumed_entropy += 13;
|
|
|
|
TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_free( &ctx );
|
2014-01-30 22:39:42 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2017-05-30 15:23:15 +02:00
|
|
|
void hmac_drbg_seed_file( int md_alg, char * path, int ret )
|
2014-01-30 21:11:16 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_hmac_drbg_context ctx;
|
2014-01-30 21:11:16 +01:00
|
|
|
|
2015-04-28 22:07:14 +02:00
|
|
|
mbedtls_hmac_drbg_init( &ctx );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_type( md_alg );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2020-06-10 14:08:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
NULL, 0 ) == 0 );
|
2014-01-30 21:11:16 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_write_seed_file( &ctx, path ) == ret );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_update_seed_file( &ctx, path ) == ret );
|
2014-01-30 21:11:16 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_free( &ctx );
|
2014-01-30 21:11:16 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-01-30 22:39:42 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void hmac_drbg_buf( int md_alg )
|
|
|
|
{
|
|
|
|
unsigned char out[16];
|
|
|
|
unsigned char buf[100];
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_hmac_drbg_context ctx;
|
2014-01-30 22:39:42 +01:00
|
|
|
size_t i;
|
|
|
|
|
2015-04-28 22:07:14 +02:00
|
|
|
mbedtls_hmac_drbg_init( &ctx );
|
2014-01-30 22:39:42 +01:00
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
memset( out, 0, sizeof( out ) );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_type( md_alg );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2015-04-28 22:07:14 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 );
|
2014-01-30 22:39:42 +01:00
|
|
|
|
|
|
|
/* Make sure it never tries to reseed (would segfault otherwise) */
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_set_reseed_interval( &ctx, 3 );
|
|
|
|
mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
|
2014-01-30 22:39:42 +01:00
|
|
|
|
|
|
|
for( i = 0; i < 30; i++ )
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
2014-01-30 22:39:42 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_free( &ctx );
|
2014-01-30 22:39:42 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-01-30 17:22:14 +01:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 12:05:32 +02:00
|
|
|
void hmac_drbg_no_reseed( int md_alg, data_t * entropy,
|
|
|
|
data_t * custom, data_t * add1,
|
|
|
|
data_t * add2, data_t * output )
|
2014-01-30 17:22:14 +01:00
|
|
|
{
|
2014-02-01 11:30:03 +01:00
|
|
|
unsigned char data[1024];
|
2014-01-30 17:22:14 +01:00
|
|
|
unsigned char my_output[512];
|
|
|
|
entropy_ctx p_entropy;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_hmac_drbg_context ctx;
|
2014-01-30 17:22:14 +01:00
|
|
|
|
2015-04-28 22:07:14 +02:00
|
|
|
mbedtls_hmac_drbg_init( &ctx );
|
2014-01-30 17:22:14 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
p_entropy.p = entropy->x;
|
|
|
|
p_entropy.len = entropy->len;
|
2014-01-30 17:22:14 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_type( md_alg );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2014-02-01 11:30:03 +01:00
|
|
|
|
|
|
|
/* Test the simplified buffer-based variant */
|
2017-06-09 05:32:58 +02:00
|
|
|
memcpy( data, entropy->x, p_entropy.len );
|
|
|
|
memcpy( data + p_entropy.len, custom->x, custom->len );
|
2015-04-28 22:07:14 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info,
|
2017-06-09 05:32:58 +02:00
|
|
|
data, p_entropy.len + custom->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
|
|
|
|
add1->x, add1->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
|
|
|
|
add2->x, add2->len ) == 0 );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
/* clear for second run */
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_free( &ctx );
|
2014-02-01 11:30:03 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
|
2014-02-01 11:30:03 +01:00
|
|
|
|
|
|
|
/* And now the normal entropy-based variant */
|
2017-10-19 09:49:19 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
|
2017-06-09 05:32:58 +02:00
|
|
|
custom->x, custom->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
|
|
|
|
add1->x, add1->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
|
|
|
|
add2->x, add2->len ) == 0 );
|
|
|
|
TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
|
2014-02-01 11:30:03 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_free( &ctx );
|
2014-01-31 09:54:14 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-29 12:05:32 +02:00
|
|
|
void hmac_drbg_nopr( int md_alg, data_t * entropy, data_t * custom,
|
|
|
|
data_t * add1, data_t * add2, data_t * add3,
|
|
|
|
data_t * output )
|
2014-01-31 09:54:14 +01:00
|
|
|
{
|
|
|
|
unsigned char my_output[512];
|
|
|
|
entropy_ctx p_entropy;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_hmac_drbg_context ctx;
|
2014-01-31 09:54:14 +01:00
|
|
|
|
2015-04-28 22:07:14 +02:00
|
|
|
mbedtls_hmac_drbg_init( &ctx );
|
2017-05-30 15:23:15 +02:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
p_entropy.p = entropy->x;
|
|
|
|
p_entropy.len = entropy->len;
|
2014-01-31 09:54:14 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_type( md_alg );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2017-10-19 09:49:19 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
|
2017-06-09 05:32:58 +02:00
|
|
|
custom->x, custom->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_reseed( &ctx, add1->x, add1->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
|
|
|
|
add2->x, add2->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
|
|
|
|
add3->x, add3->len ) == 0 );
|
2014-01-31 09:54:14 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
|
2014-01-30 17:22:14 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_free( &ctx );
|
2014-01-30 17:22:14 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-01-31 10:16:57 +01:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 12:05:32 +02:00
|
|
|
void hmac_drbg_pr( int md_alg, data_t * entropy, data_t * custom,
|
|
|
|
data_t * add1, data_t * add2, data_t * output )
|
2014-01-31 10:16:57 +01:00
|
|
|
{
|
|
|
|
unsigned char my_output[512];
|
|
|
|
entropy_ctx p_entropy;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_hmac_drbg_context ctx;
|
2014-01-31 10:16:57 +01:00
|
|
|
|
2015-04-28 22:07:14 +02:00
|
|
|
mbedtls_hmac_drbg_init( &ctx );
|
2014-01-31 10:16:57 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
p_entropy.p = entropy->x;
|
|
|
|
p_entropy.len = entropy->len;
|
2014-01-31 10:16:57 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_type( md_alg );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2017-10-19 09:49:19 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
|
2017-06-09 05:32:58 +02:00
|
|
|
custom->x, custom->len ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
|
|
|
|
add1->x, add1->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
|
|
|
|
add2->x, add2->len ) == 0 );
|
2014-01-31 10:16:57 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_free( &ctx );
|
2014-01-31 10:16:57 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2017-05-30 15:23:15 +02:00
|
|
|
void hmac_drbg_selftest( )
|
2014-01-31 11:12:09 +01:00
|
|
|
{
|
2016-09-09 10:10:28 +02:00
|
|
|
TEST_ASSERT( mbedtls_hmac_drbg_self_test( 1 ) == 0 );
|
2014-01-31 11:12:09 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|