diff --git a/tests/suites/test_suite_ctr_drbg.function b/tests/suites/test_suite_ctr_drbg.function index 7f7d4950de..714af5598b 100644 --- a/tests/suites/test_suite_ctr_drbg.function +++ b/tests/suites/test_suite_ctr_drbg.function @@ -3,6 +3,15 @@ #include "mbedtls/ctr_drbg.h" #include "string.h" +/* Modes for ctr_drbg_validate */ +enum reseed_mode +{ + RESEED_NEVER, /* never reseed */ + RESEED_FIRST, /* instantiate, reseed, generate, generate */ + RESEED_SECOND, /* instantiate, generate, reseed, generate */ + RESEED_ALWAYS /* prediction resistance, no explicit reseed */ +}; + static size_t test_offset_idx; static size_t test_max_idx; static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len ) @@ -55,74 +64,107 @@ exit: /* END_CASE */ /* BEGIN_CASE */ - -/* BEGIN_CASE */ -void ctr_drbg_validate_no_reseed( char *add_init_string, char *entropy_string, - char *add1_string, char *add2_string, - char *result_string ) +void ctr_drbg_validate( int reseed_mode, char *nonce_string, + int entropy_len_arg, char *entropy_string, + char *reseed_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]; + unsigned char entropy[144]; + unsigned char nonce[64]; + unsigned char reseed[32]; + unsigned char add1[48]; + unsigned char add2[48]; 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; + unsigned char buf[64]; + unsigned char result[64]; + size_t entropy_chunk_len = (size_t) entropy_len_arg; + size_t nonce_len, reseed_len, add1_len, add2_len, result_len; + test_offset_idx = 0; mbedtls_ctr_drbg_init( &ctx ); - entropy_len = unhexify( entropy, entropy_string ); - add_init_len = unhexify( add_init, add_init_string ); + test_max_idx = unhexify( entropy, entropy_string ); + nonce_len = unhexify( nonce, nonce_string ); + reseed_len = unhexify( reseed, reseed_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, ) - * 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 ); + * where nonce||perso = nonce[nonce_len] */ + TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( + &ctx, + mbedtls_test_entropy_func, entropy, + nonce, nonce_len, + entropy_chunk_len ) == 0 ); + if( reseed_mode == RESEED_ALWAYS ) + mbedtls_ctr_drbg_set_prediction_resistance( + &ctx, + MBEDTLS_CTR_DRBG_PR_ON ); + + if( reseed_mode == RESEED_FIRST ) + { + /* CTR_DRBG_Reseed(entropy[idx:idx+entropy_len], + * reseed[:reseed_len]) */ + TEST_ASSERT( mbedtls_ctr_drbg_reseed( + &ctx, + reseed, reseed_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 ); + /* Then reseed if prediction resistance is enabled. */ + TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( + &ctx, + buf, result_len, + add1, add1_len ) == 0 ); + + + if( reseed_mode == RESEED_SECOND ) + { + /* CTR_DRBG_Reseed(entropy[idx:idx+entropy_len], + * reseed[:reseed_len]) */ + TEST_ASSERT( mbedtls_ctr_drbg_reseed( + &ctx, + reseed, reseed_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 ); + /* Then reseed if prediction resistance is enabled. */ + 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 */ + +/* BEGIN_CASE */ +void ctr_drbg_validate_no_reseed( char *add_init_string, char *entropy_string, + char *add1_string, char *add2_string, + char *result_string ) +{ + test_suite_ctr_drbg_validate( RESEED_NEVER, add_init_string, + strlen( entropy_string ) / 2, entropy_string, + "", add1_string, add2_string, + result_string ); + goto exit; +} +/* END_CASE */ + +/* BEGIN_CASE */ +void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string, + char *add1_string, char *add2_string, char *result_string ) { - 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 ); - result_len = unhexify( result, result_string ); - - test_offset_idx = 0; - test_max_idx = entropy_len; - /* CTR_DRBG_Instantiate(entropy[:entropy_len/3], nonce, perso, ) - * 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 / 3 ) == 0 ); - mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON ); - - /* CTR_DRBG_Generate(result_len * 8 bits, add1[:add1_len]) -> buf */ - /* Then reseed because of prediction resistance. */ - 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 */ - /* Then reseed because of prediction resistance. */ - 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 ); + test_suite_ctr_drbg_validate( RESEED_ALWAYS, add_init_string, + strlen( entropy_string ) / 6, entropy_string, + "", add1_string, add2_string, + result_string ); + goto exit; } /* END_CASE */ @@ -131,32 +173,11 @@ void ctr_drbg_validate_nopr( data_t * add_init, data_t * entropy, data_t * add1, data_t * add_reseed, char *add2_string, char *result_string ) { - mbedtls_ctr_drbg_context ctx; - unsigned char buf[512]; - unsigned char result[512]; - size_t entropy_len, add_init_len, add1_len, add_reseed_len, add2_len, result_len; - - mbedtls_ctr_drbg_init( &ctx ); - - entropy_len = unhexify( entropy, entropy_string ); - result_len = unhexify( result, result_string ); - - test_offset_idx = 0; - test_max_idx = entropy_len; - /* CTR_DRBG_Instantiate(entropy[:entropy_len/2], nonce, perso, ) - * 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 / 2 ) == 0 ); - - /* CTR_DRBG_Generate(16 * 8 bits, add1[:add1_len]) -> buf */ - TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, result_len, add1, add1_len ) == 0 ); - /* CTR_DRBG_Reseed(entropy[entropy_len/2:entropy_len], add_reseed[:add_reseed_len]) */ - TEST_ASSERT( hexcmp( buf, result_str->x, 16, result_str->len ) == 0 ); - /* CTR_DRBG_Generate(16 * 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 ); + test_suite_ctr_drbg_validate( RESEED_SECOND, add_init_string, + strlen( entropy_string ) / 4, entropy_string, + add_reseed_string, add1_string, add2_string, + result_string ); + goto exit; } /* END_CASE */