9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
13 #if defined(WANT_NOT_RND_MPI)
14 #if defined(POLARSSL_BIGNUM_C)
17 #error "not_rnd_mpi() need bignum.c"
23 typedef UINT32 uint32_t;
36 #define GET_UINT32_BE(n,b,i) \
38 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
39 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
40 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
41 | ( (uint32_t) (b)[(i) + 3] ); \
46 #define PUT_UINT32_BE(n,b,i) \
48 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
49 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
50 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
51 (b)[(i) + 3] = (unsigned char) ( (n) ); \
55 static int unhexify(
unsigned char *obuf,
const char *ibuf)
58 int len = strlen(ibuf) / 2;
59 assert(!(strlen(ibuf) %1));
64 if( c >=
'0' && c <=
'9' )
66 else if( c >=
'a' && c <=
'f' )
68 else if( c >=
'A' && c <=
'F' )
74 if( c2 >=
'0' && c2 <=
'9' )
76 else if( c2 >=
'a' && c2 <=
'f' )
78 else if( c2 >=
'A' && c2 <=
'F' )
83 *obuf++ = ( c << 4 ) | c2;
89 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
101 *obuf++ =
'a' + h - 10;
106 *obuf++ =
'a' + l - 10;
122 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
126 if( rng_state != NULL )
129 for( i = 0; i < len; ++i )
140 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
142 if( rng_state != NULL )
145 memset( output, 0, len );
172 if( rng_state == NULL )
181 memcpy( output, info->
buf, use_len );
182 info->
buf += use_len;
186 if( len - use_len > 0 )
187 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
216 uint32_t i, *k, sum, delta=0x9E3779B9;
217 unsigned char result[4];
219 if( rng_state == NULL )
226 size_t use_len = ( len > 4 ) ? 4 : len;
229 for( i = 0; i < 32; i++ )
231 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
233 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
237 memcpy( output, result, use_len );
244 #if defined(WANT_NOT_RND_MPI)
253 #define ciL (sizeof(t_uint))
254 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
255 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
257 char *str = (
char *) in;
266 X.
n = CHARS_TO_LIMBS( len );
272 assert( strlen( str ) / 2 == len );
285 #define TEST_SUITE_ACTIVE
293 if( test_errors == 1 )
294 printf(
"FAILED\n" );
295 printf(
" %s\n", test );
300 #define TEST_ASSERT( TEST ) \
301 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
302 if( test_errors) return; \
307 if( (*str)[0] !=
'"' ||
308 (*str)[strlen( *str ) - 1] !=
'"' )
310 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
315 (*str)[strlen( *str ) - 1] =
'\0';
327 for( i = 0; i < strlen( str ); i++ )
329 if( i == 0 && str[i] ==
'-' )
335 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
336 str[i - 1] ==
'0' && str[i] ==
'x' )
342 if( str[i] <
'0' || str[i] >
'9' )
352 *value = strtol( str, NULL, 16 );
354 *value = strtol( str, NULL, 10 );
361 printf(
"Expected integer for parameter and got: %s\n", str );
365 #ifdef POLARSSL_SHA1_C
366 void test_suite_sha1(
char *hex_src_string,
char *hex_hash_string )
368 unsigned char src_str[10000];
369 unsigned char hash_str[10000];
370 unsigned char output[41];
373 memset(src_str, 0x00, 10000);
374 memset(hash_str, 0x00, 10000);
375 memset(output, 0x00, 41);
377 src_len =
unhexify( src_str, hex_src_string );
379 sha1( src_str, src_len, output );
380 hexify( hash_str, output, 20 );
382 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
386 #ifdef POLARSSL_SHA256_C
387 void test_suite_sha224(
char *hex_src_string,
char *hex_hash_string )
389 unsigned char src_str[10000];
390 unsigned char hash_str[10000];
391 unsigned char output[57];
394 memset(src_str, 0x00, 10000);
395 memset(hash_str, 0x00, 10000);
396 memset(output, 0x00, 57);
398 src_len =
unhexify( src_str, hex_src_string );
400 sha256( src_str, src_len, output, 1 );
401 hexify( hash_str, output, 28 );
403 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
407 #ifdef POLARSSL_SHA256_C
408 void test_suite_sha256(
char *hex_src_string,
char *hex_hash_string )
410 unsigned char src_str[10000];
411 unsigned char hash_str[10000];
412 unsigned char output[65];
415 memset(src_str, 0x00, 10000);
416 memset(hash_str, 0x00, 10000);
417 memset(output, 0x00, 65);
419 src_len =
unhexify( src_str, hex_src_string );
421 sha256( src_str, src_len, output, 0 );
422 hexify( hash_str, output, 32 );
424 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
428 #ifdef POLARSSL_SHA512_C
429 void test_suite_sha384(
char *hex_src_string,
char *hex_hash_string )
431 unsigned char src_str[10000];
432 unsigned char hash_str[10000];
433 unsigned char output[97];
436 memset(src_str, 0x00, 10000);
437 memset(hash_str, 0x00, 10000);
438 memset(output, 0x00, 97);
440 src_len =
unhexify( src_str, hex_src_string );
442 sha512( src_str, src_len, output, 1 );
443 hexify( hash_str, output, 48 );
445 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
449 #ifdef POLARSSL_SHA512_C
450 void test_suite_sha512(
char *hex_src_string,
char *hex_hash_string )
452 unsigned char src_str[10000];
453 unsigned char hash_str[10000];
454 unsigned char output[129];
457 memset(src_str, 0x00, 10000);
458 memset(hash_str, 0x00, 10000);
459 memset(output, 0x00, 129);
461 src_len =
unhexify( src_str, hex_src_string );
463 sha512( src_str, src_len, output, 0);
464 hexify( hash_str, output, 64 );
466 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
470 #ifdef POLARSSL_SHA1_C
471 #ifdef POLARSSL_FS_IO
472 void test_suite_sha1_file(
char *filename,
char *hex_hash_string )
474 unsigned char hash_str[41];
475 unsigned char output[21];
477 memset(hash_str, 0x00, 41);
478 memset(output, 0x00, 21);
481 hexify( hash_str, output, 20 );
483 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
488 #ifdef POLARSSL_SHA256_C
489 #ifdef POLARSSL_FS_IO
490 void test_suite_sha224_file(
char *filename,
char *hex_hash_string )
492 unsigned char hash_str[57];
493 unsigned char output[29];
495 memset(hash_str, 0x00, 57);
496 memset(output, 0x00, 29);
499 hexify( hash_str, output, 28 );
501 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
506 #ifdef POLARSSL_SHA256_C
507 #ifdef POLARSSL_FS_IO
508 void test_suite_sha256_file(
char *filename,
char *hex_hash_string )
510 unsigned char hash_str[65];
511 unsigned char output[33];
513 memset(hash_str, 0x00, 65);
514 memset(output, 0x00, 33);
517 hexify( hash_str, output, 32 );
519 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
524 #ifdef POLARSSL_SHA512_C
525 #ifdef POLARSSL_FS_IO
526 void test_suite_sha384_file(
char *filename,
char *hex_hash_string )
528 unsigned char hash_str[97];
529 unsigned char output[49];
531 memset(hash_str, 0x00, 97);
532 memset(output, 0x00, 49);
535 hexify( hash_str, output, 48 );
537 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
542 #ifdef POLARSSL_SHA512_C
543 #ifdef POLARSSL_FS_IO
544 void test_suite_sha512_file(
char *filename,
char *hex_hash_string )
546 unsigned char hash_str[129];
547 unsigned char output[65];
549 memset(hash_str, 0x00, 129);
550 memset(output, 0x00, 65);
553 hexify( hash_str, output, 64 );
555 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
560 #ifdef POLARSSL_SHA1_C
561 #ifdef POLARSSL_SELF_TEST
562 void test_suite_sha1_selftest()
569 #ifdef POLARSSL_SHA256_C
570 #ifdef POLARSSL_SELF_TEST
571 void test_suite_sha256_selftest()
578 #ifdef POLARSSL_SHA512_C
579 #ifdef POLARSSL_SELF_TEST
580 void test_suite_sha512_selftest()
595 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
597 #if defined(POLARSSL_SHA1_C)
603 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
605 #if defined(POLARSSL_SHA512_C)
611 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
613 #if defined(POLARSSL_SHA256_C)
619 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
621 #if defined(POLARSSL_SELF_TEST)
638 #if defined(TEST_SUITE_ACTIVE)
639 if( strcmp( params[0],
"sha1" ) == 0 )
641 #ifdef POLARSSL_SHA1_C
643 char *param1 = params[1];
644 char *param2 = params[2];
648 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
655 test_suite_sha1( param1, param2 );
662 if( strcmp( params[0],
"sha224" ) == 0 )
664 #ifdef POLARSSL_SHA256_C
666 char *param1 = params[1];
667 char *param2 = params[2];
671 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
678 test_suite_sha224( param1, param2 );
685 if( strcmp( params[0],
"sha256" ) == 0 )
687 #ifdef POLARSSL_SHA256_C
689 char *param1 = params[1];
690 char *param2 = params[2];
694 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
701 test_suite_sha256( param1, param2 );
708 if( strcmp( params[0],
"sha384" ) == 0 )
710 #ifdef POLARSSL_SHA512_C
712 char *param1 = params[1];
713 char *param2 = params[2];
717 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
724 test_suite_sha384( param1, param2 );
731 if( strcmp( params[0],
"sha512" ) == 0 )
733 #ifdef POLARSSL_SHA512_C
735 char *param1 = params[1];
736 char *param2 = params[2];
740 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
747 test_suite_sha512( param1, param2 );
754 if( strcmp( params[0],
"sha1_file" ) == 0 )
756 #ifdef POLARSSL_SHA1_C
757 #ifdef POLARSSL_FS_IO
759 char *param1 = params[1];
760 char *param2 = params[2];
764 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
771 test_suite_sha1_file( param1, param2 );
779 if( strcmp( params[0],
"sha224_file" ) == 0 )
781 #ifdef POLARSSL_SHA256_C
782 #ifdef POLARSSL_FS_IO
784 char *param1 = params[1];
785 char *param2 = params[2];
789 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
796 test_suite_sha224_file( param1, param2 );
804 if( strcmp( params[0],
"sha256_file" ) == 0 )
806 #ifdef POLARSSL_SHA256_C
807 #ifdef POLARSSL_FS_IO
809 char *param1 = params[1];
810 char *param2 = params[2];
814 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
821 test_suite_sha256_file( param1, param2 );
829 if( strcmp( params[0],
"sha384_file" ) == 0 )
831 #ifdef POLARSSL_SHA512_C
832 #ifdef POLARSSL_FS_IO
834 char *param1 = params[1];
835 char *param2 = params[2];
839 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
846 test_suite_sha384_file( param1, param2 );
854 if( strcmp( params[0],
"sha512_file" ) == 0 )
856 #ifdef POLARSSL_SHA512_C
857 #ifdef POLARSSL_FS_IO
859 char *param1 = params[1];
860 char *param2 = params[2];
864 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
871 test_suite_sha512_file( param1, param2 );
879 if( strcmp( params[0],
"sha1_selftest" ) == 0 )
881 #ifdef POLARSSL_SHA1_C
882 #ifdef POLARSSL_SELF_TEST
887 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
892 test_suite_sha1_selftest( );
900 if( strcmp( params[0],
"sha256_selftest" ) == 0 )
902 #ifdef POLARSSL_SHA256_C
903 #ifdef POLARSSL_SELF_TEST
908 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
913 test_suite_sha256_selftest( );
921 if( strcmp( params[0],
"sha512_selftest" ) == 0 )
923 #ifdef POLARSSL_SHA512_C
924 #ifdef POLARSSL_SELF_TEST
929 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
934 test_suite_sha512_selftest( );
944 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
958 ret = fgets( buf, len, f );
962 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
963 buf[strlen(buf) - 1] =
'\0';
964 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
965 buf[strlen(buf) - 1] =
'\0';
978 while( *p !=
'\0' && p < buf + len )
988 if( p + 1 < buf + len )
1000 for( i = 0; i < cnt; i++ )
1007 if( *p ==
'\\' && *(p + 1) ==
'n' )
1012 else if( *p ==
'\\' && *(p + 1) ==
':' )
1017 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1033 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1034 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_shax.data";
1039 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1040 unsigned char alloc_buf[1000000];
1041 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1044 file = fopen( filename,
"r" );
1047 fprintf( stderr,
"Failed to open\n" );
1051 while( !feof( file ) )
1055 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1057 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
1058 fprintf( stdout,
" " );
1059 for( i = strlen( buf ) + 1; i < 67; i++ )
1060 fprintf( stdout,
"." );
1061 fprintf( stdout,
" " );
1066 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1070 if( strcmp( params[0],
"depends_on" ) == 0 )
1072 for( i = 1; i < cnt; i++ )
1076 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1087 if( skip == 1 || ret == 3 )
1090 fprintf( stdout,
"----\n" );
1093 else if( ret == 0 && test_errors == 0 )
1095 fprintf( stdout,
"PASS\n" );
1100 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1107 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1109 if( strlen(buf) != 0 )
1111 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1117 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1118 if( total_errors == 0 )
1119 fprintf( stdout,
"PASSED" );
1121 fprintf( stdout,
"FAILED" );
1123 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1124 total_tests - total_errors, total_tests, total_skipped );
1126 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1127 #if defined(POLARSSL_MEMORY_DEBUG)
1128 memory_buffer_alloc_status();
1130 memory_buffer_alloc_free();
1133 return( total_errors != 0 );
static int test_assert(int correct, char *test)
int sha1_self_test(int verbose)
Checkup routine.
int sha256_file(const char *path, unsigned char output[32], int is224)
Output = SHA-256( file contents )
void sha256(const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
Output = SHA-256( input buffer )
Info structure for the pseudo random function.
void sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
Output = SHA-1( input buffer )
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
Configuration options (set of defines)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int main(int argc, char *argv[])
Multi-precision integer library.
#define TEST_ASSERT(TEST)
int sha256_self_test(int verbose)
Checkup routine.
static int unhexify(unsigned char *obuf, const char *ibuf)
#define PUT_UINT32_BE(n, b, i)
int sha1_file(const char *path, unsigned char output[20])
Output = SHA-1( file contents )
int sha512_self_test(int verbose)
Checkup routine.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
void sha512(const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
Output = SHA-512( input buffer )
int sha512_file(const char *path, unsigned char output[64], int is384)
Output = SHA-512( file contents )
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int parse_arguments(char *buf, size_t len, char *params[50])
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
int verify_string(char **str)
SHA-1 cryptographic hash function.
int dispatch_test(int cnt, char *params[50])
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
SHA-384 and SHA-512 cryptographic hash function.
int verify_int(char *str, int *value)
SHA-224 and SHA-256 cryptographic hash function.
int get_line(FILE *f, char *buf, size_t len)