mbedtls/tests/suites/test_suite_ctr_drbg.function

239 lines
8.3 KiB
Plaintext
Raw Normal View History

/* BEGIN_HEADER */
2015-03-09 18:05:11 +01:00
#include "mbedtls/ctr_drbg.h"
static int test_offset_idx;
static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len )
{
2014-01-31 12:16:54 +01:00
const unsigned char *p = (unsigned char *) data;
memcpy( buf, p + test_offset_idx, len );
2014-01-31 12:16:54 +01:00
test_offset_idx += len;
return( 0 );
}
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_CTR_DRBG_C
* END_DEPENDENCIES
*/
/* BEGIN_CASE */
void ctr_drbg_special_behaviours( )
{
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 );
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 );
exit:
mbedtls_ctr_drbg_free( &ctx );
}
/* END_CASE */
/* BEGIN_CASE */
void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
char *add1_string, char *add2_string,
char *result_str )
{
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 output_str[512];
int add_init_len, add1_len, add2_len;
mbedtls_ctr_drbg_init( &ctx );
memset( output_str, 0, 512 );
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 );
test_offset_idx = 0;
mbedtls_ctr_drbg_set_entropy_len( &ctx, 32 );
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx,
mbedtls_test_entropy_func, entropy,
add_init, add_init_len ) == 0 );
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
hexify( output_str, buf, 16 );
TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
2014-06-18 16:44:11 +02:00
exit:
mbedtls_ctr_drbg_free( &ctx );
}
/* END_CASE */
/* BEGIN_CASE */
void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
char *add1_string, char *add_reseed_string,
char *add2_string, char *result_str )
{
unsigned char entropy[512];
unsigned char add_init[512];
unsigned char add1[512];
unsigned char add_reseed[512];
unsigned char add2[512];
mbedtls_ctr_drbg_context ctx;
unsigned char buf[512];
unsigned char output_str[512];
int add_init_len, add1_len, add_reseed_len, add2_len;
mbedtls_ctr_drbg_init( &ctx );
memset( output_str, 0, 512 );
unhexify( entropy, entropy_string );
add_init_len = unhexify( add_init, add_init_string );
add1_len = unhexify( add1, add1_string );
add_reseed_len = unhexify( add_reseed, add_reseed_string );
add2_len = unhexify( add2, add2_string );
test_offset_idx = 0;
mbedtls_ctr_drbg_set_entropy_len( &ctx, 32 );
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx,
mbedtls_test_entropy_func, entropy,
add_init, add_init_len ) == 0 );
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
hexify( output_str, buf, 16 );
TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
2014-06-18 16:44:11 +02:00
exit:
mbedtls_ctr_drbg_free( &ctx );
}
/* END_CASE */
2014-01-31 12:16:54 +01:00
/* BEGIN_CASE */
void ctr_drbg_entropy_usage( )
{
unsigned char out[16];
unsigned char add[16];
unsigned char entropy[1024];
mbedtls_ctr_drbg_context ctx;
2014-01-31 12:16:54 +01:00
size_t i, reps = 10;
int last_idx;
mbedtls_ctr_drbg_init( &ctx );
2014-01-31 12:16:54 +01:00
test_offset_idx = 0;
memset( entropy, 0, sizeof( entropy ) );
memset( out, 0, sizeof( out ) );
memset( add, 0, sizeof( add ) );
/* Set reseed interval before seed */
mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
2014-01-31 12:16:54 +01:00
/* Init must use entropy */
last_idx = test_offset_idx;
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_test_entropy_func, entropy, NULL, 0 ) == 0 );
2014-01-31 12:16:54 +01:00
TEST_ASSERT( last_idx < test_offset_idx );
/* By default, PR is off, and reseed interval was set to
* 2 * reps so the next few calls should not use entropy */
2014-01-31 12:16:54 +01:00
last_idx = test_offset_idx;
for( i = 0; i < reps; i++ )
{
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 12:16:54 +01: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 );
/* There have been 2 * reps calls to random. The next call should reseed */
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
2014-01-31 12:16:54 +01:00
TEST_ASSERT( last_idx < test_offset_idx );
/* Set reseed interval after seed */
mbedtls_ctr_drbg_set_reseed_interval( &ctx, 4 * reps + 1 );
/* The next few calls should not reseed */
2014-01-31 12:16:54 +01:00
last_idx = test_offset_idx;
for( i = 0; i < (2 * reps); i++ )
2014-01-31 12:16:54 +01: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 12:16:54 +01:00
add, sizeof( add ) ) == 0 );
}
TEST_ASSERT( last_idx == test_offset_idx );
/* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT).
* Make sure it's detected as an error and doesn't cause memory
* corruption. */
TEST_ASSERT( mbedtls_ctr_drbg_update_ret(
&ctx, entropy, sizeof( entropy ) ) != 0 );
2014-01-31 12:16:54 +01:00
/* Now enable PR, so the next few calls should all reseed */
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 12:16:54 +01:00
TEST_ASSERT( last_idx < test_offset_idx );
/* Finally, check setting entropy_len */
mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 );
2014-01-31 12:16:54 +01:00
last_idx = test_offset_idx;
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
2014-01-31 12:16:54 +01:00
TEST_ASSERT( test_offset_idx - last_idx == 42 );
mbedtls_ctr_drbg_set_entropy_len( &ctx, 13 );
2014-01-31 12:16:54 +01:00
last_idx = test_offset_idx;
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
2014-01-31 12:16:54 +01:00
TEST_ASSERT( test_offset_idx - last_idx == 13 );
2014-06-18 16:44:11 +02:00
exit:
mbedtls_ctr_drbg_free( &ctx );
2014-01-31 12:16:54 +01:00
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
2014-01-31 12:16:54 +01:00
void ctr_drbg_seed_file( char *path, int ret )
{
mbedtls_ctr_drbg_context ctx;
2014-01-31 12:16:54 +01:00
mbedtls_ctr_drbg_init( &ctx );
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
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 16:44:11 +02:00
exit:
mbedtls_ctr_drbg_free( &ctx );
2014-01-31 12:16:54 +01:00
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
void ctr_drbg_selftest( )
{
TEST_ASSERT( mbedtls_ctr_drbg_self_test( 1 ) == 0 );
}
/* END_CASE */