1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
7 #ifdef POLARSSL_ENTROPY_C
14 static size_t entropy_dummy_calls;
22 static int entropy_dummy_source(
void *data,
unsigned char *output,
23 size_t len,
size_t *olen )
25 entropy_dummy_calls++;
31 int *d = (
int *) data;
39 memset( output, 0x2a, *olen );
46 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
50 #if defined(POLARSSL_PLATFORM_C)
53 #define polarssl_malloc malloc
54 #define polarssl_free free
59 typedef UINT32 uint32_t;
72 #define GET_UINT32_BE(n,b,i) \
74 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
75 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
76 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
77 | ( (uint32_t) (b)[(i) + 3] ); \
82 #define PUT_UINT32_BE(n,b,i) \
84 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
85 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
86 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
87 (b)[(i) + 3] = (unsigned char) ( (n) ); \
91 static int unhexify(
unsigned char *obuf,
const char *ibuf)
94 int len = strlen(ibuf) / 2;
95 assert(!(strlen(ibuf) %1));
100 if( c >=
'0' && c <=
'9' )
102 else if( c >=
'a' && c <=
'f' )
104 else if( c >=
'A' && c <=
'F' )
110 if( c2 >=
'0' && c2 <=
'9' )
112 else if( c2 >=
'a' && c2 <=
'f' )
114 else if( c2 >=
'A' && c2 <=
'F' )
119 *obuf++ = ( c << 4 ) | c2;
125 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
137 *obuf++ =
'a' + h - 10;
142 *obuf++ =
'a' + l - 10;
159 size_t actual_len = len != 0 ? len : 1;
164 memset( p, 0x00, actual_len );
183 *olen = strlen(ibuf) / 2;
189 assert( obuf != NULL );
205 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
207 #if !defined(__OpenBSD__)
210 if( rng_state != NULL )
213 for( i = 0; i < len; ++i )
216 if( rng_state != NULL )
219 arc4random_buf( output, len );
230 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
232 if( rng_state != NULL )
235 memset( output, 0, len );
262 if( rng_state == NULL )
271 memcpy( output, info->
buf, use_len );
272 info->
buf += use_len;
276 if( len - use_len > 0 )
277 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
306 uint32_t i, *k, sum, delta=0x9E3779B9;
307 unsigned char result[4], *out = output;
309 if( rng_state == NULL )
316 size_t use_len = ( len > 4 ) ? 4 : len;
319 for( i = 0; i < 32; i++ )
321 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
323 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
327 memcpy( out, result, use_len );
339 #if defined(POLARSSL_PLATFORM_C)
342 #define polarssl_printf printf
343 #define polarssl_malloc malloc
344 #define polarssl_free free
349 #ifdef POLARSSL_ENTROPY_C
351 #define TEST_SUITE_ACTIVE
353 static int test_assert(
int correct,
const char *test )
359 if( test_errors == 1 )
360 printf(
"FAILED\n" );
361 printf(
" %s\n", test );
366 #define TEST_ASSERT( TEST ) \
367 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
368 if( test_errors) goto exit; \
373 if( (*str)[0] !=
'"' ||
374 (*str)[strlen( *str ) - 1] !=
'"' )
376 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
381 (*str)[strlen( *str ) - 1] =
'\0';
393 for( i = 0; i < strlen( str ); i++ )
395 if( i == 0 && str[i] ==
'-' )
401 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
402 str[i - 1] ==
'0' && str[i] ==
'x' )
408 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
409 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
410 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
420 *value = strtol( str, NULL, 16 );
422 *value = strtol( str, NULL, 10 );
427 #ifdef POLARSSL_FS_IO
428 if( strcmp( str,
"POLARSSL_ERR_ENTROPY_FILE_IO_ERROR" ) == 0 )
433 #endif // POLARSSL_FS_IO
434 if( strcmp( str,
"POLARSSL_ERR_ENTROPY_SOURCE_FAILED" ) == 0 )
441 printf(
"Expected integer for parameter and got: %s\n", str );
445 #ifdef POLARSSL_FS_IO
446 void test_suite_entropy_seed_file(
char *path,
int ret )
460 void test_suite_entropy_too_many_sources( )
481 void test_suite_entropy_func_len(
int len,
int ret )
493 for( i = 0; i < 8; i++ )
496 for( j = 0; j <
sizeof( buf ); j++ )
501 for( j = 0; j < (size_t) len; j++ )
504 for( j = len; j <
sizeof( buf ); j++ )
511 void test_suite_entropy_source_fail(
char *path )
515 unsigned char buf[16];
526 #if defined(POLARSSL_FS_IO)
539 void test_suite_entropy_threshold(
int threshold,
int chunk_size,
int result )
548 &chunk_size, threshold ) == 0 );
550 entropy_dummy_calls = 0;
556 TEST_ASSERT( entropy_dummy_calls == (
size_t) result );
567 #ifdef POLARSSL_SELF_TEST
568 void test_suite_entropy_selftest( )
597 #if defined(TEST_SUITE_ACTIVE)
598 if( strcmp( params[0],
"entropy_seed_file" ) == 0 )
600 #ifdef POLARSSL_FS_IO
602 char *param1 = params[1];
607 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
612 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
614 test_suite_entropy_seed_file( param1, param2 );
621 if( strcmp( params[0],
"entropy_too_many_sources" ) == 0 )
627 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
632 test_suite_entropy_too_many_sources( );
638 if( strcmp( params[0],
"entropy_func_len" ) == 0 )
646 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
650 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
651 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
653 test_suite_entropy_func_len( param1, param2 );
659 if( strcmp( params[0],
"entropy_source_fail" ) == 0 )
662 char *param1 = params[1];
666 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
672 test_suite_entropy_source_fail( param1 );
678 if( strcmp( params[0],
"entropy_threshold" ) == 0 )
687 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
691 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
692 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
693 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
695 test_suite_entropy_threshold( param1, param2, param3 );
701 if( strcmp( params[0],
"entropy_selftest" ) == 0 )
703 #ifdef POLARSSL_SELF_TEST
708 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
713 test_suite_entropy_selftest( );
722 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
736 ret = fgets( buf, len, f );
740 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
741 buf[strlen(buf) - 1] =
'\0';
742 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
743 buf[strlen(buf) - 1] =
'\0';
756 while( *p !=
'\0' && p < buf + len )
766 if( p + 1 < buf + len )
778 for( i = 0; i < cnt; i++ )
785 if( *p ==
'\\' && *(p + 1) ==
'n' )
790 else if( *p ==
'\\' && *(p + 1) ==
':' )
795 else if( *p ==
'\\' && *(p + 1) ==
'?' )
811 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
812 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.8/tests/suites/test_suite_entropy.data";
817 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
818 unsigned char alloc_buf[1000000];
822 file = fopen( filename,
"r" );
825 fprintf( stderr,
"Failed to open\n" );
829 while( !feof( file ) )
833 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
835 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
836 fprintf( stdout,
" " );
837 for( i = strlen( buf ) + 1; i < 67; i++ )
838 fprintf( stdout,
"." );
839 fprintf( stdout,
" " );
844 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
848 if( strcmp( params[0],
"depends_on" ) == 0 )
850 for( i = 1; i < cnt; i++ )
854 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
865 if( skip == 1 || ret == 3 )
868 fprintf( stdout,
"----\n" );
871 else if( ret == 0 && test_errors == 0 )
873 fprintf( stdout,
"PASS\n" );
878 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
885 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
887 if( strlen(buf) != 0 )
889 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
895 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
896 if( total_errors == 0 )
897 fprintf( stdout,
"PASSED" );
899 fprintf( stdout,
"FAILED" );
901 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
902 total_tests - total_errors, total_tests, total_skipped );
904 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
905 #if defined(POLARSSL_MEMORY_DEBUG)
906 memory_buffer_alloc_status();
911 return( total_errors != 0 );