mbedtls/tests/suites/test_suite_ctr_drbg.function

87 lines
2.8 KiB
Text
Raw Normal View History

BEGIN_HEADER
#include <polarssl/ctr_drbg.h>
int test_offset;
int entropy_func( void *data, unsigned char *buf, size_t len )
{
unsigned char *p = (unsigned char *) data;
memcpy( buf, p + test_offset, len );
test_offset += 32;
return( 0 );
}
int ctr_drbg_init_entropy_len(
ctr_drbg_context *ctx,
int (*f_entropy)(void *, unsigned char *, size_t),
void *p_entropy,
const unsigned char *custom,
size_t len,
size_t entropy_len );
END_HEADER
BEGIN_DEPENDENCIES
depends_on:POLARSSL_CTR_DRBG_C
END_DEPENDENCIES
BEGIN_CASE
ctr_drbg_validate_pr:add_init_string:entropy_string:add1_string:add2_string:result_str
{
unsigned char entropy[512];
unsigned char add_init[512];
unsigned char add1[512];
unsigned char add2[512];
ctr_drbg_context ctx;
unsigned char buf[512];
unsigned char output_str[512];
int add_init_len, add1_len, add2_len;
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 = 0;
TEST_ASSERT( ctr_drbg_init_entropy_len( &ctx, entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON );
TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
TEST_ASSERT( 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 );
}
END_CASE
BEGIN_CASE
ctr_drbg_validate_nopr:add_init_string:entropy_string:add1_string:add_reseed_string:add2_string:result_str
{
unsigned char entropy[512];
unsigned char add_init[512];
unsigned char add1[512];
unsigned char add_reseed[512];
unsigned char add2[512];
ctr_drbg_context ctx;
unsigned char buf[512];
unsigned char output_str[512];
int add_init_len, add1_len, add_reseed_len, add2_len;
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 = 0;
TEST_ASSERT( ctr_drbg_init_entropy_len( &ctx, entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
TEST_ASSERT( ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
TEST_ASSERT( 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 );
}
END_CASE