2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_HEADER */
|
2018-08-03 18:23:09 +00:00
|
|
|
#include "mbedtls/entropy.h"
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/ctr_drbg.h"
|
2017-04-06 10:55:43 +00:00
|
|
|
#include "string.h"
|
2015-02-06 13:43:58 +00:00
|
|
|
|
2018-08-03 18:23:09 +00:00
|
|
|
static size_t test_offset_idx;
|
|
|
|
static size_t test_max_idx;
|
2017-10-19 07:49:19 +00:00
|
|
|
static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len )
|
2011-11-27 14:46:59 +00:00
|
|
|
{
|
2014-01-31 11:16:54 +00:00
|
|
|
const unsigned char *p = (unsigned char *) data;
|
2018-08-03 18:23:09 +00:00
|
|
|
if( test_offset_idx + len > test_max_idx )
|
|
|
|
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
2013-11-26 16:45:20 +00:00
|
|
|
memcpy( buf, p + test_offset_idx, len );
|
2014-01-31 11:16:54 +00:00
|
|
|
test_offset_idx += len;
|
2011-11-27 14:46:59 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_HEADER */
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 10:49:31 +00:00
|
|
|
* depends_on:MBEDTLS_CTR_DRBG_C
|
2013-08-20 09:48:36 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2016-07-14 12:21:10 +00:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 13:23:15 +00:00
|
|
|
void ctr_drbg_special_behaviours( )
|
2016-07-14 12:21:10 +00:00
|
|
|
{
|
|
|
|
mbedtls_ctr_drbg_context ctx;
|
|
|
|
unsigned char output[512];
|
|
|
|
unsigned char additional[512];
|
|
|
|
|
|
|
|
mbedtls_ctr_drbg_init( &ctx );
|
|
|
|
memset( output, 0, sizeof( output ) );
|
|
|
|
memset( additional, 0, sizeof( additional ) );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx,
|
|
|
|
output, MBEDTLS_CTR_DRBG_MAX_REQUEST + 1,
|
|
|
|
additional, 16 ) ==
|
|
|
|
MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG );
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx,
|
|
|
|
output, 16,
|
|
|
|
additional, MBEDTLS_CTR_DRBG_MAX_INPUT + 1 ) ==
|
|
|
|
MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, additional,
|
|
|
|
MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + 1 ) ==
|
|
|
|
MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
|
2017-01-17 23:04:22 +00:00
|
|
|
|
|
|
|
mbedtls_ctr_drbg_set_entropy_len( &ctx, ~0 );
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, additional,
|
|
|
|
MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ) ==
|
|
|
|
MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
|
2016-07-14 12:21:10 +00:00
|
|
|
exit:
|
|
|
|
mbedtls_ctr_drbg_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2018-08-03 18:24:54 +00:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ctr_drbg_validate_no_reseed( char *add_init_string, char *entropy_string,
|
|
|
|
char *add1_string, char *add2_string,
|
|
|
|
char *result_string )
|
|
|
|
{
|
|
|
|
unsigned char entropy[512];
|
|
|
|
unsigned char add_init[512];
|
|
|
|
unsigned char add1[512];
|
|
|
|
unsigned char add2[512];
|
|
|
|
mbedtls_ctr_drbg_context ctx;
|
|
|
|
unsigned char buf[512];
|
|
|
|
unsigned char result[512];
|
|
|
|
size_t entropy_len, add_init_len, add1_len, add2_len, result_len;
|
|
|
|
|
|
|
|
mbedtls_ctr_drbg_init( &ctx );
|
|
|
|
|
|
|
|
entropy_len = unhexify( entropy, entropy_string );
|
|
|
|
add_init_len = unhexify( add_init, add_init_string );
|
|
|
|
add1_len = unhexify( add1, add1_string );
|
|
|
|
add2_len = unhexify( add2, add2_string );
|
|
|
|
result_len = unhexify( result, result_string );
|
|
|
|
|
|
|
|
test_offset_idx = 0;
|
|
|
|
test_max_idx = entropy_len;
|
|
|
|
/* CTR_DRBG_Instantiate(entropy[:entropy_len], nonce, perso, <ignored>)
|
|
|
|
* where nonce||perso = add_init[add_init_len] */
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy, add_init, add_init_len, entropy_len ) == 0 );
|
|
|
|
|
|
|
|
/* CTR_DRBG_Generate(result_len * 8 bits, add1[:add1_len]) -> buf */
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, result_len, add1, add1_len ) == 0 );
|
|
|
|
/* CTR_DRBG_Generate(result_len * 8 bits, add2[:add2_len]) -> buf */
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, result_len, add2, add2_len ) == 0 );
|
|
|
|
TEST_ASSERT( memcmp( buf, result, result_len ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ctr_drbg_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-08-03 18:23:09 +00:00
|
|
|
char *result_string )
|
2011-11-27 14:46:59 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_context ctx;
|
2011-11-27 14:46:59 +00:00
|
|
|
unsigned char buf[512];
|
2018-08-03 18:23:09 +00:00
|
|
|
unsigned char result[512];
|
|
|
|
size_t entropy_len, add_init_len, add1_len, add2_len, result_len;
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2015-04-28 20:38:08 +00:00
|
|
|
mbedtls_ctr_drbg_init( &ctx );
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2018-08-03 18:23:09 +00:00
|
|
|
entropy_len = unhexify( entropy, entropy_string );
|
|
|
|
result_len = unhexify( result, result_string );
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2013-11-26 16:45:20 +00:00
|
|
|
test_offset_idx = 0;
|
2018-08-03 18:23:09 +00:00
|
|
|
test_max_idx = entropy_len;
|
2018-08-03 18:16:52 +00:00
|
|
|
/* CTR_DRBG_Instantiate(entropy[:entropy_len/3], nonce, perso, <ignored>)
|
|
|
|
* where nonce||perso = add_init[add_init_len] */
|
2018-08-03 18:23:09 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy, add_init, add_init_len, entropy_len / 3 ) == 0 );
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2018-08-03 18:16:52 +00:00
|
|
|
/* CTR_DRBG_Generate(result_len * 8 bits, add1[:add1_len]) -> buf */
|
|
|
|
/* Then reseed because of prediction resistance. */
|
2018-08-03 18:23:09 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, result_len, add1, add1_len ) == 0 );
|
2018-08-03 18:16:52 +00:00
|
|
|
/* CTR_DRBG_Generate(result_len * 8 bits, add2[:add2_len]) -> buf */
|
|
|
|
/* Then reseed because of prediction resistance. */
|
2018-08-03 18:23:09 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, result_len, add2, add2_len ) == 0 );
|
|
|
|
TEST_ASSERT( memcmp( buf, result, result_len ) == 0 );
|
2014-06-18 14:44:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_free( &ctx );
|
2011-11-27 14:46:59 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 10:05:32 +00:00
|
|
|
void ctr_drbg_validate_nopr( data_t * add_init, data_t * entropy,
|
|
|
|
data_t * add1, data_t * add_reseed,
|
2018-08-03 18:23:09 +00:00
|
|
|
char *add2_string, char *result_string )
|
2011-11-27 14:46:59 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_context ctx;
|
2011-11-27 14:46:59 +00:00
|
|
|
unsigned char buf[512];
|
2018-08-03 18:23:09 +00:00
|
|
|
unsigned char result[512];
|
|
|
|
size_t entropy_len, add_init_len, add1_len, add_reseed_len, add2_len, result_len;
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2015-04-28 20:38:08 +00:00
|
|
|
mbedtls_ctr_drbg_init( &ctx );
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2018-08-03 18:23:09 +00:00
|
|
|
entropy_len = unhexify( entropy, entropy_string );
|
|
|
|
result_len = unhexify( result, result_string );
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2013-11-26 16:45:20 +00:00
|
|
|
test_offset_idx = 0;
|
2018-08-03 18:23:09 +00:00
|
|
|
test_max_idx = entropy_len;
|
2018-08-03 18:16:52 +00:00
|
|
|
/* CTR_DRBG_Instantiate(entropy[:entropy_len/2], nonce, perso, <ignored>)
|
|
|
|
* where nonce||perso = add_init[add_init_len] */
|
2018-08-03 18:23:09 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy, add_init, add_init_len, entropy_len / 2 ) == 0 );
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2018-08-03 18:16:52 +00:00
|
|
|
/* CTR_DRBG_Generate(16 * 8 bits, add1[:add1_len]) -> buf */
|
2018-08-03 18:23:09 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, result_len, add1, add1_len ) == 0 );
|
2018-08-03 18:16:52 +00:00
|
|
|
/* CTR_DRBG_Reseed(entropy[entropy_len/2:entropy_len], add_reseed[:add_reseed_len]) */
|
2017-06-09 03:32:58 +00:00
|
|
|
TEST_ASSERT( hexcmp( buf, result_str->x, 16, result_str->len ) == 0 );
|
2018-08-03 18:16:52 +00:00
|
|
|
/* CTR_DRBG_Generate(16 * 8 bits, add2[:add2_len]) -> buf */
|
2018-08-03 18:23:09 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, result_len, add2, add2_len ) == 0 );
|
|
|
|
TEST_ASSERT( memcmp( buf, result, result_len ) == 0 );
|
2014-06-18 14:44:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_free( &ctx );
|
2011-11-27 14:46:59 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2014-01-31 11:04:06 +00:00
|
|
|
|
2014-01-31 11:16:54 +00:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 13:23:15 +00:00
|
|
|
void ctr_drbg_entropy_usage( )
|
2014-01-31 11:16:54 +00:00
|
|
|
{
|
|
|
|
unsigned char out[16];
|
|
|
|
unsigned char add[16];
|
|
|
|
unsigned char entropy[1024];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_context ctx;
|
2014-01-31 11:16:54 +00:00
|
|
|
size_t i, reps = 10;
|
2018-08-03 18:23:09 +00:00
|
|
|
size_t last_idx;
|
2014-01-31 11:16:54 +00:00
|
|
|
|
2015-04-28 20:38:08 +00:00
|
|
|
mbedtls_ctr_drbg_init( &ctx );
|
2014-01-31 11:16:54 +00:00
|
|
|
test_offset_idx = 0;
|
2018-08-03 18:23:09 +00:00
|
|
|
test_max_idx = sizeof( entropy );
|
2014-01-31 11:16:54 +00:00
|
|
|
memset( entropy, 0, sizeof( entropy ) );
|
|
|
|
memset( out, 0, sizeof( out ) );
|
|
|
|
memset( add, 0, sizeof( add ) );
|
|
|
|
|
|
|
|
/* Init must use entropy */
|
|
|
|
last_idx = test_offset_idx;
|
2017-10-19 07:49:19 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_test_entropy_func, entropy, NULL, 0 ) == 0 );
|
2014-01-31 11:16:54 +00:00
|
|
|
TEST_ASSERT( last_idx < test_offset_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++ )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
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,
|
2014-01-31 11:16:54 +00:00
|
|
|
add, sizeof( add ) ) == 0 );
|
|
|
|
}
|
|
|
|
TEST_ASSERT( last_idx == test_offset_idx );
|
|
|
|
|
|
|
|
/* 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 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
2014-01-31 11:16:54 +00:00
|
|
|
TEST_ASSERT( last_idx < test_offset_idx );
|
|
|
|
|
|
|
|
/* The new few calls should not reseed */
|
|
|
|
last_idx = test_offset_idx;
|
|
|
|
for( i = 0; i < reps / 2; i++ )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
|
2014-01-31 11:16:54 +00:00
|
|
|
add, sizeof( add ) ) == 0 );
|
|
|
|
}
|
|
|
|
TEST_ASSERT( last_idx == test_offset_idx );
|
|
|
|
|
2014-11-27 13:04:56 +00:00
|
|
|
/* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT)
|
|
|
|
* (just make sure it doesn't cause memory corruption) */
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_update( &ctx, entropy, sizeof( entropy ) );
|
2014-11-27 13:04:56 +00:00
|
|
|
|
2014-01-31 11:16:54 +00:00
|
|
|
/* Now enable PR, so the next few calls should all reseed */
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
2014-01-31 11:16:54 +00:00
|
|
|
TEST_ASSERT( last_idx < test_offset_idx );
|
|
|
|
|
|
|
|
/* Finally, check setting entropy_len */
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 );
|
2014-01-31 11:16:54 +00:00
|
|
|
last_idx = test_offset_idx;
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
2014-01-31 11:16:54 +00:00
|
|
|
TEST_ASSERT( test_offset_idx - last_idx == 42 );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_set_entropy_len( &ctx, 13 );
|
2014-01-31 11:16:54 +00:00
|
|
|
last_idx = test_offset_idx;
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
|
2014-01-31 11:16:54 +00:00
|
|
|
TEST_ASSERT( test_offset_idx - last_idx == 13 );
|
2014-06-18 14:44:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_free( &ctx );
|
2014-01-31 11:16:54 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2017-05-30 13:23:15 +00:00
|
|
|
void ctr_drbg_seed_file( char * path, int ret )
|
2014-01-31 11:16:54 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_context ctx;
|
2014-01-31 11:16:54 +00:00
|
|
|
|
2015-04-28 20:38:08 +00:00
|
|
|
mbedtls_ctr_drbg_init( &ctx );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_write_seed_file( &ctx, path ) == ret );
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_update_seed_file( &ctx, path ) == ret );
|
2014-06-18 14:44:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ctr_drbg_free( &ctx );
|
2014-01-31 11:16:54 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2017-05-30 13:23:15 +00:00
|
|
|
void ctr_drbg_selftest( )
|
2014-01-31 11:04:06 +00:00
|
|
|
{
|
2016-09-09 08:10:28 +00:00
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_self_test( 1 ) == 0 );
|
2014-01-31 11:04:06 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|