1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
17 #if defined(POLARSSL_PLATFORM_C)
20 #define polarssl_malloc malloc
21 #define polarssl_free free
26 typedef UINT32 uint32_t;
39 #define GET_UINT32_BE(n,b,i) \
41 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
42 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
43 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
44 | ( (uint32_t) (b)[(i) + 3] ); \
49 #define PUT_UINT32_BE(n,b,i) \
51 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
52 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
53 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
54 (b)[(i) + 3] = (unsigned char) ( (n) ); \
58 static int unhexify(
unsigned char *obuf,
const char *ibuf)
61 int len = strlen(ibuf) / 2;
62 assert(!(strlen(ibuf) %1));
67 if( c >=
'0' && c <=
'9' )
69 else if( c >=
'a' && c <=
'f' )
71 else if( c >=
'A' && c <=
'F' )
77 if( c2 >=
'0' && c2 <=
'9' )
79 else if( c2 >=
'a' && c2 <=
'f' )
81 else if( c2 >=
'A' && c2 <=
'F' )
86 *obuf++ = ( c << 4 ) | c2;
92 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
104 *obuf++ =
'a' + h - 10;
109 *obuf++ =
'a' + l - 10;
126 size_t actual_len = len != 0 ? len : 1;
131 memset( p, 0x00, actual_len );
150 *olen = strlen(ibuf) / 2;
156 assert( obuf != NULL );
172 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
174 #if !defined(__OpenBSD__)
177 if( rng_state != NULL )
180 for( i = 0; i < len; ++i )
183 if( rng_state != NULL )
186 arc4random_buf( output, len );
197 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
199 if( rng_state != NULL )
202 memset( output, 0, len );
229 if( rng_state == NULL )
238 memcpy( output, info->
buf, use_len );
239 info->
buf += use_len;
243 if( len - use_len > 0 )
244 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
273 uint32_t i, *k, sum, delta=0x9E3779B9;
274 unsigned char result[4], *out = output;
276 if( rng_state == NULL )
283 size_t use_len = ( len > 4 ) ? 4 : len;
286 for( i = 0; i < 32; i++ )
288 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
290 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
294 memcpy( out, result, use_len );
306 #if defined(POLARSSL_PLATFORM_C)
309 #define polarssl_printf printf
310 #define polarssl_malloc malloc
311 #define polarssl_free free
317 #define TEST_SUITE_ACTIVE
326 printf(
"FAILED\n" );
327 printf(
" %s\n", test );
332 #define TEST_ASSERT( TEST ) \
333 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
334 if( test_errors) goto exit; \
339 if( (*str)[0] !=
'"' ||
340 (*str)[strlen( *str ) - 1] !=
'"' )
342 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
347 (*str)[strlen( *str ) - 1] =
'\0';
359 for( i = 0; i < strlen( str ); i++ )
361 if( i == 0 && str[i] ==
'-' )
367 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
368 str[i - 1] ==
'0' && str[i] ==
'x' )
374 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
375 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
376 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
386 *value = strtol( str, NULL, 16 );
388 *value = strtol( str, NULL, 10 );
395 printf(
"Expected integer for parameter and got: %s\n", str );
399 #ifdef POLARSSL_SHA1_C
400 void test_suite_sha1(
char *hex_src_string,
char *hex_hash_string )
402 unsigned char src_str[10000];
403 unsigned char hash_str[10000];
404 unsigned char output[41];
407 memset(src_str, 0x00, 10000);
408 memset(hash_str, 0x00, 10000);
409 memset(output, 0x00, 41);
411 src_len =
unhexify( src_str, hex_src_string );
413 sha1( src_str, src_len, output );
414 hexify( hash_str, output, 20 );
416 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
423 #ifdef POLARSSL_SHA256_C
424 void test_suite_sha224(
char *hex_src_string,
char *hex_hash_string )
426 unsigned char src_str[10000];
427 unsigned char hash_str[10000];
428 unsigned char output[57];
431 memset(src_str, 0x00, 10000);
432 memset(hash_str, 0x00, 10000);
433 memset(output, 0x00, 57);
435 src_len =
unhexify( src_str, hex_src_string );
437 sha256( src_str, src_len, output, 1 );
438 hexify( hash_str, output, 28 );
440 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
447 #ifdef POLARSSL_SHA256_C
448 void test_suite_sha256(
char *hex_src_string,
char *hex_hash_string )
450 unsigned char src_str[10000];
451 unsigned char hash_str[10000];
452 unsigned char output[65];
455 memset(src_str, 0x00, 10000);
456 memset(hash_str, 0x00, 10000);
457 memset(output, 0x00, 65);
459 src_len =
unhexify( src_str, hex_src_string );
461 sha256( src_str, src_len, output, 0 );
462 hexify( hash_str, output, 32 );
464 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
471 #ifdef POLARSSL_SHA512_C
472 void test_suite_sha384(
char *hex_src_string,
char *hex_hash_string )
474 unsigned char src_str[10000];
475 unsigned char hash_str[10000];
476 unsigned char output[97];
479 memset(src_str, 0x00, 10000);
480 memset(hash_str, 0x00, 10000);
481 memset(output, 0x00, 97);
483 src_len =
unhexify( src_str, hex_src_string );
485 sha512( src_str, src_len, output, 1 );
486 hexify( hash_str, output, 48 );
488 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
495 #ifdef POLARSSL_SHA512_C
496 void test_suite_sha512(
char *hex_src_string,
char *hex_hash_string )
498 unsigned char src_str[10000];
499 unsigned char hash_str[10000];
500 unsigned char output[129];
503 memset(src_str, 0x00, 10000);
504 memset(hash_str, 0x00, 10000);
505 memset(output, 0x00, 129);
507 src_len =
unhexify( src_str, hex_src_string );
509 sha512( src_str, src_len, output, 0);
510 hexify( hash_str, output, 64 );
512 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
519 #ifdef POLARSSL_SHA1_C
520 #ifdef POLARSSL_FS_IO
521 void test_suite_sha1_file(
char *filename,
char *hex_hash_string )
523 unsigned char hash_str[41];
524 unsigned char output[21];
526 memset(hash_str, 0x00, 41);
527 memset(output, 0x00, 21);
530 hexify( hash_str, output, 20 );
532 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
540 #ifdef POLARSSL_SHA256_C
541 #ifdef POLARSSL_FS_IO
542 void test_suite_sha224_file(
char *filename,
char *hex_hash_string )
544 unsigned char hash_str[57];
545 unsigned char output[29];
547 memset(hash_str, 0x00, 57);
548 memset(output, 0x00, 29);
551 hexify( hash_str, output, 28 );
553 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
561 #ifdef POLARSSL_SHA256_C
562 #ifdef POLARSSL_FS_IO
563 void test_suite_sha256_file(
char *filename,
char *hex_hash_string )
565 unsigned char hash_str[65];
566 unsigned char output[33];
568 memset(hash_str, 0x00, 65);
569 memset(output, 0x00, 33);
572 hexify( hash_str, output, 32 );
574 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
582 #ifdef POLARSSL_SHA512_C
583 #ifdef POLARSSL_FS_IO
584 void test_suite_sha384_file(
char *filename,
char *hex_hash_string )
586 unsigned char hash_str[97];
587 unsigned char output[49];
589 memset(hash_str, 0x00, 97);
590 memset(output, 0x00, 49);
593 hexify( hash_str, output, 48 );
595 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
603 #ifdef POLARSSL_SHA512_C
604 #ifdef POLARSSL_FS_IO
605 void test_suite_sha512_file(
char *filename,
char *hex_hash_string )
607 unsigned char hash_str[129];
608 unsigned char output[65];
610 memset(hash_str, 0x00, 129);
611 memset(output, 0x00, 65);
614 hexify( hash_str, output, 64 );
616 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
624 #ifdef POLARSSL_SHA1_C
625 #ifdef POLARSSL_SELF_TEST
626 void test_suite_sha1_selftest()
636 #ifdef POLARSSL_SHA256_C
637 #ifdef POLARSSL_SELF_TEST
638 void test_suite_sha256_selftest()
648 #ifdef POLARSSL_SHA512_C
649 #ifdef POLARSSL_SELF_TEST
650 void test_suite_sha512_selftest()
668 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
670 #if defined(POLARSSL_SHA1_C)
676 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
678 #if defined(POLARSSL_SELF_TEST)
684 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
686 #if defined(POLARSSL_SHA512_C)
692 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
694 #if defined(POLARSSL_SHA256_C)
711 #if defined(TEST_SUITE_ACTIVE)
712 if( strcmp( params[0],
"sha1" ) == 0 )
714 #ifdef POLARSSL_SHA1_C
716 char *param1 = params[1];
717 char *param2 = params[2];
721 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
728 test_suite_sha1( param1, param2 );
735 if( strcmp( params[0],
"sha224" ) == 0 )
737 #ifdef POLARSSL_SHA256_C
739 char *param1 = params[1];
740 char *param2 = params[2];
744 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
751 test_suite_sha224( param1, param2 );
758 if( strcmp( params[0],
"sha256" ) == 0 )
760 #ifdef POLARSSL_SHA256_C
762 char *param1 = params[1];
763 char *param2 = params[2];
767 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
774 test_suite_sha256( param1, param2 );
781 if( strcmp( params[0],
"sha384" ) == 0 )
783 #ifdef POLARSSL_SHA512_C
785 char *param1 = params[1];
786 char *param2 = params[2];
790 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
797 test_suite_sha384( param1, param2 );
804 if( strcmp( params[0],
"sha512" ) == 0 )
806 #ifdef POLARSSL_SHA512_C
808 char *param1 = params[1];
809 char *param2 = params[2];
813 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
820 test_suite_sha512( param1, param2 );
827 if( strcmp( params[0],
"sha1_file" ) == 0 )
829 #ifdef POLARSSL_SHA1_C
830 #ifdef POLARSSL_FS_IO
832 char *param1 = params[1];
833 char *param2 = params[2];
837 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
844 test_suite_sha1_file( param1, param2 );
852 if( strcmp( params[0],
"sha224_file" ) == 0 )
854 #ifdef POLARSSL_SHA256_C
855 #ifdef POLARSSL_FS_IO
857 char *param1 = params[1];
858 char *param2 = params[2];
862 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
869 test_suite_sha224_file( param1, param2 );
877 if( strcmp( params[0],
"sha256_file" ) == 0 )
879 #ifdef POLARSSL_SHA256_C
880 #ifdef POLARSSL_FS_IO
882 char *param1 = params[1];
883 char *param2 = params[2];
887 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
894 test_suite_sha256_file( param1, param2 );
902 if( strcmp( params[0],
"sha384_file" ) == 0 )
904 #ifdef POLARSSL_SHA512_C
905 #ifdef POLARSSL_FS_IO
907 char *param1 = params[1];
908 char *param2 = params[2];
912 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
919 test_suite_sha384_file( param1, param2 );
927 if( strcmp( params[0],
"sha512_file" ) == 0 )
929 #ifdef POLARSSL_SHA512_C
930 #ifdef POLARSSL_FS_IO
932 char *param1 = params[1];
933 char *param2 = params[2];
937 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
944 test_suite_sha512_file( param1, param2 );
952 if( strcmp( params[0],
"sha1_selftest" ) == 0 )
954 #ifdef POLARSSL_SHA1_C
955 #ifdef POLARSSL_SELF_TEST
960 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
965 test_suite_sha1_selftest( );
973 if( strcmp( params[0],
"sha256_selftest" ) == 0 )
975 #ifdef POLARSSL_SHA256_C
976 #ifdef POLARSSL_SELF_TEST
981 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
986 test_suite_sha256_selftest( );
994 if( strcmp( params[0],
"sha512_selftest" ) == 0 )
996 #ifdef POLARSSL_SHA512_C
997 #ifdef POLARSSL_SELF_TEST
1002 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1007 test_suite_sha512_selftest( );
1017 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1031 ret = fgets( buf, len, f );
1035 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1036 buf[strlen(buf) - 1] =
'\0';
1037 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1038 buf[strlen(buf) - 1] =
'\0';
1049 params[cnt++] = cur;
1051 while( *p !=
'\0' && p < buf + len )
1061 if( p + 1 < buf + len )
1064 params[cnt++] = cur;
1073 for( i = 0; i < cnt; i++ )
1080 if( *p ==
'\\' && *(p + 1) ==
'n' )
1085 else if( *p ==
'\\' && *(p + 1) ==
':' )
1090 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1106 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1107 const char *filename =
"/tmp/B.9R4AfU/BUILD/polarssl-1.3.9/tests/suites/test_suite_shax.data";
1112 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1113 unsigned char alloc_buf[1000000];
1117 file = fopen( filename,
"r" );
1120 fprintf( stderr,
"Failed to open\n" );
1124 while( !feof( file ) )
1128 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1130 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1131 fprintf( stdout,
" " );
1132 for( i = strlen( buf ) + 1; i < 67; i++ )
1133 fprintf( stdout,
"." );
1134 fprintf( stdout,
" " );
1139 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1143 if( strcmp( params[0],
"depends_on" ) == 0 )
1145 for( i = 1; i < cnt; i++ )
1149 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1160 if( skip == 1 || ret == 3 )
1163 fprintf( stdout,
"----\n" );
1168 fprintf( stdout,
"PASS\n" );
1173 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1180 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1182 if( strlen(buf) != 0 )
1184 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1190 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1191 if( total_errors == 0 )
1192 fprintf( stdout,
"PASSED" );
1194 fprintf( stdout,
"FAILED" );
1196 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1197 total_tests - total_errors, total_tests, total_skipped );
1199 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1200 #if defined(POLARSSL_MEMORY_DEBUG)
1201 memory_buffer_alloc_status();
1206 return( total_errors != 0 );