1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
7 #ifdef POLARSSL_CTR_DRBG_C
12 int entropy_func(
void *data,
unsigned char *buf,
size_t len )
14 const unsigned char *p = (
unsigned char *) data;
15 memcpy( buf, p + test_offset_idx, len );
16 test_offset_idx += len;
22 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
26 #if defined(POLARSSL_PLATFORM_C)
29 #define polarssl_malloc malloc
30 #define polarssl_free free
35 typedef UINT32 uint32_t;
48 #define GET_UINT32_BE(n,b,i) \
50 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
51 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
52 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
53 | ( (uint32_t) (b)[(i) + 3] ); \
58 #define PUT_UINT32_BE(n,b,i) \
60 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
61 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
62 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
63 (b)[(i) + 3] = (unsigned char) ( (n) ); \
67 static int unhexify(
unsigned char *obuf,
const char *ibuf)
70 int len = strlen(ibuf) / 2;
71 assert(!(strlen(ibuf) %1));
76 if( c >=
'0' && c <=
'9' )
78 else if( c >=
'a' && c <=
'f' )
80 else if( c >=
'A' && c <=
'F' )
86 if( c2 >=
'0' && c2 <=
'9' )
88 else if( c2 >=
'a' && c2 <=
'f' )
90 else if( c2 >=
'A' && c2 <=
'F' )
95 *obuf++ = ( c << 4 ) | c2;
101 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
113 *obuf++ =
'a' + h - 10;
118 *obuf++ =
'a' + l - 10;
135 size_t actual_len = len != 0 ? len : 1;
140 memset( p, 0x00, actual_len );
159 *olen = strlen(ibuf) / 2;
165 assert( obuf != NULL );
181 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
183 #if !defined(__OpenBSD__)
186 if( rng_state != NULL )
189 for( i = 0; i < len; ++i )
192 if( rng_state != NULL )
195 arc4random_buf( output, len );
206 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
208 if( rng_state != NULL )
211 memset( output, 0, len );
238 if( rng_state == NULL )
247 memcpy( output, info->
buf, use_len );
248 info->
buf += use_len;
252 if( len - use_len > 0 )
253 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
282 uint32_t i, *k, sum, delta=0x9E3779B9;
283 unsigned char result[4], *out = output;
285 if( rng_state == NULL )
292 size_t use_len = ( len > 4 ) ? 4 : len;
295 for( i = 0; i < 32; i++ )
297 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
299 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
303 memcpy( out, result, use_len );
315 #if defined(POLARSSL_PLATFORM_C)
318 #define polarssl_printf printf
319 #define polarssl_malloc malloc
320 #define polarssl_free free
325 #ifdef POLARSSL_CTR_DRBG_C
327 #define TEST_SUITE_ACTIVE
329 static int test_assert(
int correct,
const char *test )
335 if( test_errors == 1 )
336 printf(
"FAILED\n" );
337 printf(
" %s\n", test );
342 #define TEST_ASSERT( TEST ) \
343 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
344 if( test_errors) goto exit; \
349 if( (*str)[0] !=
'"' ||
350 (*str)[strlen( *str ) - 1] !=
'"' )
352 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
357 (*str)[strlen( *str ) - 1] =
'\0';
369 for( i = 0; i < strlen( str ); i++ )
371 if( i == 0 && str[i] ==
'-' )
377 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
378 str[i - 1] ==
'0' && str[i] ==
'x' )
384 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
385 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
386 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
396 *value = strtol( str, NULL, 16 );
398 *value = strtol( str, NULL, 10 );
403 #ifdef POLARSSL_FS_IO
404 if( strcmp( str,
"POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR" ) == 0 )
409 #endif // POLARSSL_FS_IO
412 printf(
"Expected integer for parameter and got: %s\n", str );
416 void test_suite_ctr_drbg_validate_pr(
char *add_init_string,
char *entropy_string,
417 char *add1_string,
char *add2_string,
420 unsigned char entropy[512];
421 unsigned char add_init[512];
422 unsigned char add1[512];
423 unsigned char add2[512];
425 unsigned char buf[512];
426 unsigned char output_str[512];
427 int add_init_len, add1_len, add2_len;
429 memset( output_str, 0, 512 );
431 unhexify( entropy, entropy_string );
432 add_init_len =
unhexify( add_init, add_init_string );
433 add1_len =
unhexify( add1, add1_string );
434 add2_len =
unhexify( add2, add2_string );
442 hexify( output_str, buf, 16 );
443 TEST_ASSERT( strcmp( (
char *) output_str, result_str ) == 0 );
449 void test_suite_ctr_drbg_validate_nopr(
char *add_init_string,
char *entropy_string,
450 char *add1_string,
char *add_reseed_string,
451 char *add2_string,
char *result_str )
453 unsigned char entropy[512];
454 unsigned char add_init[512];
455 unsigned char add1[512];
456 unsigned char add_reseed[512];
457 unsigned char add2[512];
459 unsigned char buf[512];
460 unsigned char output_str[512];
461 int add_init_len, add1_len, add_reseed_len, add2_len;
463 memset( output_str, 0, 512 );
465 unhexify( entropy, entropy_string );
466 add_init_len =
unhexify( add_init, add_init_string );
467 add1_len =
unhexify( add1, add1_string );
468 add_reseed_len =
unhexify( add_reseed, add_reseed_string );
469 add2_len =
unhexify( add2, add2_string );
477 hexify( output_str, buf, 16 );
478 TEST_ASSERT( strcmp( (
char *) output_str, result_str ) == 0 );
484 void test_suite_ctr_drbg_entropy_usage( )
486 unsigned char out[16];
487 unsigned char add[16];
488 unsigned char entropy[1024];
494 memset( entropy, 0,
sizeof( entropy ) );
495 memset( out, 0,
sizeof( out ) );
496 memset( add, 0,
sizeof( add ) );
499 last_idx = test_offset_idx;
505 last_idx = test_offset_idx;
506 for( i = 0; i < reps; i++ )
510 add,
sizeof( add ) ) == 0 );
527 last_idx = test_offset_idx;
528 for( i = 0; i < reps / 2; i++ )
532 add,
sizeof( add ) ) == 0 );
543 last_idx = test_offset_idx;
548 last_idx = test_offset_idx;
556 #ifdef POLARSSL_FS_IO
557 void test_suite_ctr_drbg_seed_file(
char *path,
int ret )
570 #ifdef POLARSSL_SELF_TEST
571 void test_suite_ctr_drbg_selftest( )
600 #if defined(TEST_SUITE_ACTIVE)
601 if( strcmp( params[0],
"ctr_drbg_validate_pr" ) == 0 )
604 char *param1 = params[1];
605 char *param2 = params[2];
606 char *param3 = params[3];
607 char *param4 = params[4];
608 char *param5 = params[5];
612 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
622 test_suite_ctr_drbg_validate_pr( param1, param2, param3, param4, param5 );
628 if( strcmp( params[0],
"ctr_drbg_validate_nopr" ) == 0 )
631 char *param1 = params[1];
632 char *param2 = params[2];
633 char *param3 = params[3];
634 char *param4 = params[4];
635 char *param5 = params[5];
636 char *param6 = params[6];
640 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
651 test_suite_ctr_drbg_validate_nopr( param1, param2, param3, param4, param5, param6 );
657 if( strcmp( params[0],
"ctr_drbg_entropy_usage" ) == 0 )
663 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
668 test_suite_ctr_drbg_entropy_usage( );
674 if( strcmp( params[0],
"ctr_drbg_seed_file" ) == 0 )
676 #ifdef POLARSSL_FS_IO
678 char *param1 = params[1];
683 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
688 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
690 test_suite_ctr_drbg_seed_file( param1, param2 );
697 if( strcmp( params[0],
"ctr_drbg_selftest" ) == 0 )
699 #ifdef POLARSSL_SELF_TEST
704 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
709 test_suite_ctr_drbg_selftest( );
718 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
732 ret = fgets( buf, len, f );
736 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
737 buf[strlen(buf) - 1] =
'\0';
738 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
739 buf[strlen(buf) - 1] =
'\0';
752 while( *p !=
'\0' && p < buf + len )
762 if( p + 1 < buf + len )
774 for( i = 0; i < cnt; i++ )
781 if( *p ==
'\\' && *(p + 1) ==
'n' )
786 else if( *p ==
'\\' && *(p + 1) ==
':' )
791 else if( *p ==
'\\' && *(p + 1) ==
'?' )
807 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
808 const char *filename =
"/tmp/B.Smm9NI/BUILD/polarssl-1.3.9/tests/suites/test_suite_ctr_drbg.data";
813 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
814 unsigned char alloc_buf[1000000];
818 file = fopen( filename,
"r" );
821 fprintf( stderr,
"Failed to open\n" );
825 while( !feof( file ) )
829 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
831 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
832 fprintf( stdout,
" " );
833 for( i = strlen( buf ) + 1; i < 67; i++ )
834 fprintf( stdout,
"." );
835 fprintf( stdout,
" " );
840 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
844 if( strcmp( params[0],
"depends_on" ) == 0 )
846 for( i = 1; i < cnt; i++ )
850 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
861 if( skip == 1 || ret == 3 )
864 fprintf( stdout,
"----\n" );
867 else if( ret == 0 && test_errors == 0 )
869 fprintf( stdout,
"PASS\n" );
874 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
881 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
883 if( strlen(buf) != 0 )
885 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
891 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
892 if( total_errors == 0 )
893 fprintf( stdout,
"PASSED" );
895 fprintf( stdout,
"FAILED" );
897 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
898 total_tests - total_errors, total_tests, total_skipped );
900 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
901 #if defined(POLARSSL_MEMORY_DEBUG)
902 memory_buffer_alloc_status();
907 return( total_errors != 0 );
int ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_len)
CTR_DRBG generate random.
void ctr_drbg_set_prediction_resistance(ctr_drbg_context *ctx, int resistance)
Enable / disable prediction resistance (Default: Off)
#define CTR_DRBG_PR_ON
Prediction resistance enabled.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
Memory allocation layer (Deprecated to platform layer)
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int get_line(FILE *f, char *buf, size_t len)
Configuration options (set of defines)
void ctr_drbg_free(ctr_drbg_context *ctx)
Clear CTR_CRBG context data.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int ctr_drbg_init_entropy_len(ctr_drbg_context *, int(*)(void *, unsigned char *, size_t), void *, const unsigned char *, size_t, size_t)
static int test_assert(int correct, const char *test)
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
int ctr_drbg_random_with_add(void *p_rng, unsigned char *output, size_t output_len, const unsigned char *additional, size_t add_len)
CTR_DRBG generate random with additional update input.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int dispatch_test(int cnt, char *params[50])
#define POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR
Read/write error in file.
int ctr_drbg_reseed(ctr_drbg_context *ctx, const unsigned char *additional, size_t len)
CTR_DRBG reseeding (extracts data from entropy source)
CTR_DRBG context structure.
int verify_string(char **str)
void ctr_drbg_set_reseed_interval(ctr_drbg_context *ctx, int interval)
Set the reseed interval (Default: CTR_DRBG_RESEED_INTERVAL)
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
int ctr_drbg_update_seed_file(ctr_drbg_context *ctx, const char *path)
Read and update a seed file.
static int unhexify(unsigned char *obuf, const char *ibuf)
void ctr_drbg_set_entropy_len(ctr_drbg_context *ctx, size_t len)
Set the amount of entropy grabbed on each (re)seed (Default: CTR_DRBG_ENTROPY_LEN) ...
int ctr_drbg_self_test(int verbose)
Checkup routine.
int parse_arguments(char *buf, size_t len, char *params[50])
int ctr_drbg_init(ctr_drbg_context *ctx, int(*f_entropy)(void *, unsigned char *, size_t), void *p_entropy, const unsigned char *custom, size_t len)
CTR_DRBG initialization.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int ctr_drbg_write_seed_file(ctr_drbg_context *ctx, const char *path)
Write a seed file.
int verify_int(char *str, int *value)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int entropy_func(void *data, unsigned char *output, size_t len)
Retrieve entropy from the accumulator (Maximum length: ENTROPY_BLOCK_SIZE) (Thread-safe if POLARSSL_T...
#define PUT_UINT32_BE(n, b, i)
CTR_DRBG based on AES-256 (NIST SP 800-90)