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_SHA256" ) == 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_SHA512" ) == 0 )
442 printf(
"Expected integer for parameter and got: %s\n", str );
446 void test_suite_hmac_drbg_entropy_usage(
int md_alg )
448 unsigned char out[16];
449 unsigned char buf[1024];
453 size_t last_len, i, reps = 10;
455 memset( buf, 0,
sizeof( buf ) );
456 memset( out, 0,
sizeof( out ) );
458 entropy.len =
sizeof( buf );
465 last_len = entropy.len;
472 last_len = entropy.len;
473 for( i = 0; i < reps; i++ )
494 last_len = entropy.len;
495 for( i = 0; i < reps / 2; i++ )
510 last_len = entropy.len;
515 last_len = entropy.len;
523 #ifdef POLARSSL_FS_IO
524 void test_suite_hmac_drbg_seed_file(
int md_alg,
char *path,
int ret )
535 TEST_ASSERT( hmac_drbg_write_seed_file( &ctx, path ) == ret );
536 TEST_ASSERT( hmac_drbg_update_seed_file( &ctx, path ) == ret );
543 void test_suite_hmac_drbg_buf(
int md_alg )
545 unsigned char out[16];
546 unsigned char buf[100];
551 memset( buf, 0,
sizeof( buf ) );
552 memset( out, 0,
sizeof( out ) );
562 for( i = 0; i < 30; i++ )
569 void test_suite_hmac_drbg_no_reseed(
int md_alg,
570 char *entropy_hex,
char *custom_hex,
571 char *add1_hex,
char *add2_hex,
574 unsigned char data[1024];
575 unsigned char entropy[512];
576 unsigned char custom[512];
577 unsigned char add1[512];
578 unsigned char add2[512];
579 unsigned char output[512];
580 unsigned char my_output[512];
581 size_t custom_len, add1_len, add2_len, out_len;
582 entropy_ctx p_entropy;
586 memset( my_output, 0,
sizeof my_output );
588 custom_len =
unhexify( custom, custom_hex );
589 add1_len =
unhexify( add1, add1_hex );
590 add2_len =
unhexify( add2, add2_hex );
591 out_len =
unhexify( output, output_hex );
592 p_entropy.len =
unhexify( entropy, entropy_hex );
593 p_entropy.p = entropy;
599 memcpy( data, entropy, p_entropy.len );
600 memcpy( data + p_entropy.len, custom, custom_len );
602 data, p_entropy.len + custom_len ) == 0 );
604 add1, add1_len ) == 0 );
606 add2, add2_len ) == 0 );
611 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
615 custom, custom_len ) == 0 );
617 add1, add1_len ) == 0 );
619 add2, add2_len ) == 0 );
620 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
626 void test_suite_hmac_drbg_nopr(
int md_alg,
627 char *entropy_hex,
char *custom_hex,
628 char *add1_hex,
char *add2_hex,
char *add3_hex,
631 unsigned char entropy[512];
632 unsigned char custom[512];
633 unsigned char add1[512];
634 unsigned char add2[512];
635 unsigned char add3[512];
636 unsigned char output[512];
637 unsigned char my_output[512];
638 size_t custom_len, add1_len, add2_len, add3_len, out_len;
639 entropy_ctx p_entropy;
643 memset( my_output, 0,
sizeof my_output );
645 custom_len =
unhexify( custom, custom_hex );
646 add1_len =
unhexify( add1, add1_hex );
647 add2_len =
unhexify( add2, add2_hex );
648 add3_len =
unhexify( add3, add3_hex );
649 out_len =
unhexify( output, output_hex );
650 p_entropy.len =
unhexify( entropy, entropy_hex );
651 p_entropy.p = entropy;
657 custom, custom_len ) == 0 );
660 add2, add2_len ) == 0 );
662 add3, add3_len ) == 0 );
664 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
670 void test_suite_hmac_drbg_pr(
int md_alg,
671 char *entropy_hex,
char *custom_hex,
672 char *add1_hex,
char *add2_hex,
675 unsigned char entropy[512];
676 unsigned char custom[512];
677 unsigned char add1[512];
678 unsigned char add2[512];
679 unsigned char output[512];
680 unsigned char my_output[512];
681 size_t custom_len, add1_len, add2_len, out_len;
682 entropy_ctx p_entropy;
686 memset( my_output, 0,
sizeof my_output );
688 custom_len =
unhexify( custom, custom_hex );
689 add1_len =
unhexify( add1, add1_hex );
690 add2_len =
unhexify( add2, add2_hex );
691 out_len =
unhexify( output, output_hex );
692 p_entropy.len =
unhexify( entropy, entropy_hex );
693 p_entropy.p = entropy;
699 custom, custom_len ) == 0 );
702 add1, add1_len ) == 0 );
704 add2, add2_len ) == 0 );
706 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
712 #ifdef POLARSSL_SELF_TEST
713 void test_suite_hmac_drbg_selftest( )
731 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
733 #if defined(POLARSSL_SHA256_C)
739 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
741 #if defined(POLARSSL_SHA512_C)
747 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
749 #if defined(POLARSSL_SHA1_C)
766 #if defined(TEST_SUITE_ACTIVE)
767 if( strcmp( params[0],
"hmac_drbg_entropy_usage" ) == 0 )
774 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
778 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
780 test_suite_hmac_drbg_entropy_usage( param1 );
786 if( strcmp( params[0],
"hmac_drbg_seed_file" ) == 0 )
788 #ifdef POLARSSL_FS_IO
791 char *param2 = params[2];
796 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
800 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
802 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
804 test_suite_hmac_drbg_seed_file( param1, param2, param3 );
811 if( strcmp( params[0],
"hmac_drbg_buf" ) == 0 )
818 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
822 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
824 test_suite_hmac_drbg_buf( param1 );
830 if( strcmp( params[0],
"hmac_drbg_no_reseed" ) == 0 )
834 char *param2 = params[2];
835 char *param3 = params[3];
836 char *param4 = params[4];
837 char *param5 = params[5];
838 char *param6 = params[6];
842 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
846 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
853 test_suite_hmac_drbg_no_reseed( param1, param2, param3, param4, param5, param6 );
859 if( strcmp( params[0],
"hmac_drbg_nopr" ) == 0 )
863 char *param2 = params[2];
864 char *param3 = params[3];
865 char *param4 = params[4];
866 char *param5 = params[5];
867 char *param6 = params[6];
868 char *param7 = params[7];
872 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
876 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
884 test_suite_hmac_drbg_nopr( param1, param2, param3, param4, param5, param6, param7 );
890 if( strcmp( params[0],
"hmac_drbg_pr" ) == 0 )
894 char *param2 = params[2];
895 char *param3 = params[3];
896 char *param4 = params[4];
897 char *param5 = params[5];
898 char *param6 = params[6];
902 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
906 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
913 test_suite_hmac_drbg_pr( param1, param2, param3, param4, param5, param6 );
919 if( strcmp( params[0],
"hmac_drbg_selftest" ) == 0 )
921 #ifdef POLARSSL_SELF_TEST
926 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
931 test_suite_hmac_drbg_selftest( );
940 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
954 ret = fgets( buf, len, f );
958 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
959 buf[strlen(buf) - 1] =
'\0';
960 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
961 buf[strlen(buf) - 1] =
'\0';
974 while( *p !=
'\0' && p < buf + len )
984 if( p + 1 < buf + len )
996 for( i = 0; i < cnt; i++ )
1003 if( *p ==
'\\' && *(p + 1) ==
'n' )
1008 else if( *p ==
'\\' && *(p + 1) ==
':' )
1013 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1029 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1030 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.9/tests/suites/test_suite_hmac_drbg.nopr.data";
1035 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1036 unsigned char alloc_buf[1000000];
1040 file = fopen( filename,
"r" );
1043 fprintf( stderr,
"Failed to open\n" );
1047 while( !feof( file ) )
1051 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1053 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
1054 fprintf( stdout,
" " );
1055 for( i = strlen( buf ) + 1; i < 67; i++ )
1056 fprintf( stdout,
"." );
1057 fprintf( stdout,
" " );
1062 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1066 if( strcmp( params[0],
"depends_on" ) == 0 )
1068 for( i = 1; i < cnt; i++ )
1072 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1083 if( skip == 1 || ret == 3 )
1086 fprintf( stdout,
"----\n" );
1089 else if( ret == 0 && test_errors == 0 )
1091 fprintf( stdout,
"PASS\n" );
1096 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1103 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1105 if( strlen(buf) != 0 )
1107 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1113 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1114 if( total_errors == 0 )
1115 fprintf( stdout,
"PASSED" );
1117 fprintf( stdout,
"FAILED" );
1119 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1120 total_tests - total_errors, total_tests, total_skipped );
1122 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1123 #if defined(POLARSSL_MEMORY_DEBUG)
1124 memory_buffer_alloc_status();
1129 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.
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 memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
static int unhexify(unsigned char *obuf, const char *ibuf)
Configuration options (set of defines)
static int test_assert(int correct, const char *test)
int main(int argc, char *argv[])
#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.
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_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
void hmac_drbg_free(hmac_drbg_context *ctx)
Free an HMAC_DRBG context.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int parse_arguments(char *buf, size_t len, char *params[50])
void hmac_drbg_set_prediction_resistance(hmac_drbg_context *ctx, int resistance)
Enable / disable prediction resistance (Default: Off)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
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.
#define PUT_UINT32_BE(n, b, i)
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 dispatch_test(int cnt, char *params[50])
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 int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int verify_int(char *str, int *value)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
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...
int get_line(FILE *f, char *buf, size_t len)
HMAC_DRBG (NIST SP 800-90A)