1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
7 #ifdef POLARSSL_HMAC_DRBG_C
17 int entropy_func(
void *data,
unsigned char *buf,
size_t len )
19 entropy_ctx *ctx = (entropy_ctx *) data;
24 memcpy( buf, ctx->p, len );
34 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
38 #if defined(POLARSSL_PLATFORM_C)
41 #define polarssl_malloc malloc
42 #define polarssl_free free
47 typedef UINT32 uint32_t;
60 #define GET_UINT32_BE(n,b,i) \
62 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
63 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
64 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
65 | ( (uint32_t) (b)[(i) + 3] ); \
70 #define PUT_UINT32_BE(n,b,i) \
72 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
73 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
74 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
75 (b)[(i) + 3] = (unsigned char) ( (n) ); \
79 static int unhexify(
unsigned char *obuf,
const char *ibuf)
82 int len = strlen(ibuf) / 2;
83 assert(!(strlen(ibuf) %1));
88 if( c >=
'0' && c <=
'9' )
90 else if( c >=
'a' && c <=
'f' )
92 else if( c >=
'A' && c <=
'F' )
98 if( c2 >=
'0' && c2 <=
'9' )
100 else if( c2 >=
'a' && c2 <=
'f' )
102 else if( c2 >=
'A' && c2 <=
'F' )
107 *obuf++ = ( c << 4 ) | c2;
113 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
125 *obuf++ =
'a' + h - 10;
130 *obuf++ =
'a' + l - 10;
147 size_t actual_len = len != 0 ? len : 1;
152 memset( p, 0x00, actual_len );
171 *olen = strlen(ibuf) / 2;
177 assert( obuf != NULL );
193 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
195 #if !defined(__OpenBSD__)
198 if( rng_state != NULL )
201 for( i = 0; i < len; ++i )
204 if( rng_state != NULL )
207 arc4random_buf( output, len );
218 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
220 if( rng_state != NULL )
223 memset( output, 0, len );
250 if( rng_state == NULL )
259 memcpy( output, info->
buf, use_len );
260 info->
buf += use_len;
264 if( len - use_len > 0 )
265 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
294 uint32_t i, *k, sum, delta=0x9E3779B9;
295 unsigned char result[4], *out = output;
297 if( rng_state == NULL )
304 size_t use_len = ( len > 4 ) ? 4 : len;
307 for( i = 0; i < 32; i++ )
309 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
311 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
315 memcpy( out, result, use_len );
327 #if defined(POLARSSL_PLATFORM_C)
330 #define polarssl_printf printf
331 #define polarssl_malloc malloc
332 #define polarssl_free free
337 #ifdef POLARSSL_HMAC_DRBG_C
339 #define TEST_SUITE_ACTIVE
341 static int test_assert(
int correct,
const char *test )
347 if( test_errors == 1 )
348 printf(
"FAILED\n" );
349 printf(
" %s\n", test );
354 #define TEST_ASSERT( TEST ) \
355 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
356 if( test_errors) goto exit; \
361 if( (*str)[0] !=
'"' ||
362 (*str)[strlen( *str ) - 1] !=
'"' )
364 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
369 (*str)[strlen( *str ) - 1] =
'\0';
381 for( i = 0; i < strlen( str ); i++ )
383 if( i == 0 && str[i] ==
'-' )
389 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
390 str[i - 1] ==
'0' && str[i] ==
'x' )
396 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
397 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
398 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
408 *value = strtol( str, NULL, 16 );
410 *value = strtol( str, NULL, 10 );
415 if( strcmp( str,
"POLARSSL_MD_SHA512" ) == 0 )
420 if( strcmp( str,
"POLARSSL_MD_SHA224" ) == 0 )
425 if( strcmp( str,
"POLARSSL_MD_SHA384" ) == 0 )
430 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
435 if( strcmp( str,
"POLARSSL_MD_SHA256" ) == 0 )
440 #ifdef POLARSSL_FS_IO
441 if( strcmp( str,
"POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR" ) == 0 )
446 #endif // POLARSSL_FS_IO
449 printf(
"Expected integer for parameter and got: %s\n", str );
453 void test_suite_hmac_drbg_entropy_usage(
int md_alg )
455 unsigned char out[16];
456 unsigned char buf[1024];
460 size_t last_len, i, reps = 10;
462 memset( buf, 0,
sizeof( buf ) );
463 memset( out, 0,
sizeof( out ) );
465 entropy.len =
sizeof( buf );
472 last_len = entropy.len;
479 last_len = entropy.len;
480 for( i = 0; i < reps; i++ )
501 last_len = entropy.len;
502 for( i = 0; i < reps / 2; i++ )
517 last_len = entropy.len;
522 last_len = entropy.len;
530 #ifdef POLARSSL_FS_IO
531 void test_suite_hmac_drbg_seed_file(
int md_alg,
char *path,
int ret )
542 TEST_ASSERT( hmac_drbg_write_seed_file( &ctx, path ) == ret );
543 TEST_ASSERT( hmac_drbg_update_seed_file( &ctx, path ) == ret );
550 void test_suite_hmac_drbg_buf(
int md_alg )
552 unsigned char out[16];
553 unsigned char buf[100];
558 memset( buf, 0,
sizeof( buf ) );
559 memset( out, 0,
sizeof( out ) );
569 for( i = 0; i < 30; i++ )
576 void test_suite_hmac_drbg_no_reseed(
int md_alg,
577 char *entropy_hex,
char *custom_hex,
578 char *add1_hex,
char *add2_hex,
581 unsigned char data[1024];
582 unsigned char entropy[512];
583 unsigned char custom[512];
584 unsigned char add1[512];
585 unsigned char add2[512];
586 unsigned char output[512];
587 unsigned char my_output[512];
588 size_t custom_len, add1_len, add2_len, out_len;
589 entropy_ctx p_entropy;
593 memset( my_output, 0,
sizeof my_output );
595 custom_len =
unhexify( custom, custom_hex );
596 add1_len =
unhexify( add1, add1_hex );
597 add2_len =
unhexify( add2, add2_hex );
598 out_len =
unhexify( output, output_hex );
599 p_entropy.len =
unhexify( entropy, entropy_hex );
600 p_entropy.p = entropy;
606 memcpy( data, entropy, p_entropy.len );
607 memcpy( data + p_entropy.len, custom, custom_len );
609 data, p_entropy.len + custom_len ) == 0 );
611 add1, add1_len ) == 0 );
613 add2, add2_len ) == 0 );
618 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
622 custom, custom_len ) == 0 );
624 add1, add1_len ) == 0 );
626 add2, add2_len ) == 0 );
627 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
633 void test_suite_hmac_drbg_nopr(
int md_alg,
634 char *entropy_hex,
char *custom_hex,
635 char *add1_hex,
char *add2_hex,
char *add3_hex,
638 unsigned char entropy[512];
639 unsigned char custom[512];
640 unsigned char add1[512];
641 unsigned char add2[512];
642 unsigned char add3[512];
643 unsigned char output[512];
644 unsigned char my_output[512];
645 size_t custom_len, add1_len, add2_len, add3_len, out_len;
646 entropy_ctx p_entropy;
650 memset( my_output, 0,
sizeof my_output );
652 custom_len =
unhexify( custom, custom_hex );
653 add1_len =
unhexify( add1, add1_hex );
654 add2_len =
unhexify( add2, add2_hex );
655 add3_len =
unhexify( add3, add3_hex );
656 out_len =
unhexify( output, output_hex );
657 p_entropy.len =
unhexify( entropy, entropy_hex );
658 p_entropy.p = entropy;
664 custom, custom_len ) == 0 );
667 add2, add2_len ) == 0 );
669 add3, add3_len ) == 0 );
671 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
677 void test_suite_hmac_drbg_pr(
int md_alg,
678 char *entropy_hex,
char *custom_hex,
679 char *add1_hex,
char *add2_hex,
682 unsigned char entropy[512];
683 unsigned char custom[512];
684 unsigned char add1[512];
685 unsigned char add2[512];
686 unsigned char output[512];
687 unsigned char my_output[512];
688 size_t custom_len, add1_len, add2_len, out_len;
689 entropy_ctx p_entropy;
693 memset( my_output, 0,
sizeof my_output );
695 custom_len =
unhexify( custom, custom_hex );
696 add1_len =
unhexify( add1, add1_hex );
697 add2_len =
unhexify( add2, add2_hex );
698 out_len =
unhexify( output, output_hex );
699 p_entropy.len =
unhexify( entropy, entropy_hex );
700 p_entropy.p = entropy;
706 custom, custom_len ) == 0 );
709 add1, add1_len ) == 0 );
711 add2, add2_len ) == 0 );
713 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
719 #ifdef POLARSSL_SELF_TEST
720 void test_suite_hmac_drbg_selftest( )
738 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
740 #if defined(POLARSSL_SHA1_C)
746 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
748 #if defined(POLARSSL_SHA512_C)
754 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
756 #if defined(POLARSSL_SHA256_C)
773 #if defined(TEST_SUITE_ACTIVE)
774 if( strcmp( params[0],
"hmac_drbg_entropy_usage" ) == 0 )
781 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
785 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
787 test_suite_hmac_drbg_entropy_usage( param1 );
793 if( strcmp( params[0],
"hmac_drbg_seed_file" ) == 0 )
795 #ifdef POLARSSL_FS_IO
798 char *param2 = params[2];
803 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
807 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
809 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
811 test_suite_hmac_drbg_seed_file( param1, param2, param3 );
818 if( strcmp( params[0],
"hmac_drbg_buf" ) == 0 )
825 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
829 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
831 test_suite_hmac_drbg_buf( param1 );
837 if( strcmp( params[0],
"hmac_drbg_no_reseed" ) == 0 )
841 char *param2 = params[2];
842 char *param3 = params[3];
843 char *param4 = params[4];
844 char *param5 = params[5];
845 char *param6 = params[6];
849 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
853 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
860 test_suite_hmac_drbg_no_reseed( param1, param2, param3, param4, param5, param6 );
866 if( strcmp( params[0],
"hmac_drbg_nopr" ) == 0 )
870 char *param2 = params[2];
871 char *param3 = params[3];
872 char *param4 = params[4];
873 char *param5 = params[5];
874 char *param6 = params[6];
875 char *param7 = params[7];
879 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
883 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
891 test_suite_hmac_drbg_nopr( param1, param2, param3, param4, param5, param6, param7 );
897 if( strcmp( params[0],
"hmac_drbg_pr" ) == 0 )
901 char *param2 = params[2];
902 char *param3 = params[3];
903 char *param4 = params[4];
904 char *param5 = params[5];
905 char *param6 = params[6];
909 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
913 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
920 test_suite_hmac_drbg_pr( param1, param2, param3, param4, param5, param6 );
926 if( strcmp( params[0],
"hmac_drbg_selftest" ) == 0 )
928 #ifdef POLARSSL_SELF_TEST
933 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
938 test_suite_hmac_drbg_selftest( );
947 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
961 ret = fgets( buf, len, f );
965 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
966 buf[strlen(buf) - 1] =
'\0';
967 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
968 buf[strlen(buf) - 1] =
'\0';
981 while( *p !=
'\0' && p < buf + len )
991 if( p + 1 < buf + len )
1003 for( i = 0; i < cnt; i++ )
1010 if( *p ==
'\\' && *(p + 1) ==
'n' )
1015 else if( *p ==
'\\' && *(p + 1) ==
':' )
1020 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1036 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1037 const char *filename =
"/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_hmac_drbg.misc.data";
1042 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1043 unsigned char alloc_buf[1000000];
1047 file = fopen( filename,
"r" );
1050 fprintf( stderr,
"Failed to open\n" );
1054 while( !feof( file ) )
1058 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1060 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
1061 fprintf( stdout,
" " );
1062 for( i = strlen( buf ) + 1; i < 67; i++ )
1063 fprintf( stdout,
"." );
1064 fprintf( stdout,
" " );
1069 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1073 if( strcmp( params[0],
"depends_on" ) == 0 )
1075 for( i = 1; i < cnt; i++ )
1079 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1090 if( skip == 1 || ret == 3 )
1093 fprintf( stdout,
"----\n" );
1096 else if( ret == 0 && test_errors == 0 )
1098 fprintf( stdout,
"PASS\n" );
1103 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1110 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1112 if( strlen(buf) != 0 )
1114 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1120 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1121 if( total_errors == 0 )
1122 fprintf( stdout,
"PASSED" );
1124 fprintf( stdout,
"FAILED" );
1126 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1127 total_tests - total_errors, total_tests, total_skipped );
1129 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1130 #if defined(POLARSSL_MEMORY_DEBUG)
1131 memory_buffer_alloc_status();
1136 return( total_errors != 0 );
void hmac_drbg_set_entropy_len(hmac_drbg_context *ctx, size_t len)
Set the amount of entropy grabbed on each reseed (Default: given by the security strength, which depends on the hash used, see hmac_drbg_init() )
Memory allocation layer (Deprecated to platform layer)
int hmac_drbg_random(void *p_rng, unsigned char *output, size_t out_len)
HMAC_DRBG generate random.
void hmac_drbg_set_reseed_interval(hmac_drbg_context *ctx, int interval)
Set the reseed interval (Default: POLARSSL_HMAC_DRBG_RESEED_INTERVAL)
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
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 parse_arguments(char *buf, size_t len, char *params[50])
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
static int unhexify(unsigned char *obuf, const char *ibuf)
Configuration options (set of defines)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
static int test_assert(int correct, const char *test)
#define POLARSSL_HMAC_DRBG_PR_ON
Prediction resistance enabled.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
#define POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR
Read/write error in file.
int get_line(FILE *f, char *buf, size_t len)
int hmac_drbg_reseed(hmac_drbg_context *ctx, const unsigned char *additional, size_t len)
HMAC_DRBG reseeding (extracts data from entropy source)
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
void hmac_drbg_free(hmac_drbg_context *ctx)
Free an HMAC_DRBG context.
#define PUT_UINT32_BE(n, b, i)
int dispatch_test(int cnt, char *params[50])
void hmac_drbg_set_prediction_resistance(hmac_drbg_context *ctx, int resistance)
Enable / disable prediction resistance (Default: Off)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int verify_string(char **str)
int hmac_drbg_init(hmac_drbg_context *ctx, const md_info_t *md_info, int(*f_entropy)(void *, unsigned char *, size_t), void *p_entropy, const unsigned char *custom, size_t len)
HMAC_DRBG initialisation.
int hmac_drbg_random_with_add(void *p_rng, unsigned char *output, size_t output_len, const unsigned char *additional, size_t add_len)
HMAC_DRBG generate random with additional update input.
int hmac_drbg_init_buf(hmac_drbg_context *ctx, const md_info_t *md_info, const unsigned char *data, size_t data_len)
Initilisation of simpified HMAC_DRBG (never reseeds).
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int verify_int(char *str, int *value)
Message digest information.
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...
HMAC_DRBG (NIST SP 800-90A)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.