9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
15 typedef UINT32 uint32_t;
28 #define GET_UINT32_BE(n,b,i) \
30 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
31 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
32 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
33 | ( (uint32_t) (b)[(i) + 3] ); \
38 #define PUT_UINT32_BE(n,b,i) \
40 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
41 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
42 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
43 (b)[(i) + 3] = (unsigned char) ( (n) ); \
47 static int unhexify(
unsigned char *obuf,
const char *ibuf)
50 int len = strlen(ibuf) / 2;
51 assert(!(strlen(ibuf) %1));
56 if( c >=
'0' && c <=
'9' )
58 else if( c >=
'a' && c <=
'f' )
60 else if( c >=
'A' && c <=
'F' )
66 if( c2 >=
'0' && c2 <=
'9' )
68 else if( c2 >=
'a' && c2 <=
'f' )
70 else if( c2 >=
'A' && c2 <=
'F' )
75 *obuf++ = ( c << 4 ) | c2;
81 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
93 *obuf++ =
'a' + h - 10;
98 *obuf++ =
'a' + l - 10;
114 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
118 if( rng_state != NULL )
121 for( i = 0; i < len; ++i )
132 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
134 if( rng_state != NULL )
137 memset( output, 0, len );
164 if( rng_state == NULL )
173 memcpy( output, info->
buf, use_len );
174 info->
buf += use_len;
178 if( len - use_len > 0 )
179 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
208 uint32_t i, *k, sum, delta=0x9E3779B9;
209 unsigned char result[4];
211 if( rng_state == NULL )
218 size_t use_len = ( len > 4 ) ? 4 : len;
221 for( i = 0; i < 32; i++ )
223 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
225 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
229 memcpy( output, result, use_len );
245 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
248 const char *ibuf = in;
250 assert( len == strlen(ibuf) / 2 );
251 assert(!(strlen(ibuf) %1));
253 obuf = out + (len - 1);
257 if( c >=
'0' && c <=
'9' )
259 else if( c >=
'a' && c <=
'f' )
261 else if( c >=
'A' && c <=
'F' )
267 if( c2 >=
'0' && c2 <=
'9' )
269 else if( c2 >=
'a' && c2 <=
'f' )
271 else if( c2 >=
'A' && c2 <=
'F' )
276 *obuf-- = ( c << 4 ) | c2;
289 #define TEST_SUITE_ACTIVE
298 printf(
"FAILED\n" );
299 printf(
" %s\n", test );
304 #define TEST_ASSERT( TEST ) \
305 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
306 if( test_errors) return; \
311 if( (*str)[0] !=
'"' ||
312 (*str)[strlen( *str ) - 1] !=
'"' )
314 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
319 (*str)[strlen( *str ) - 1] =
'\0';
331 for( i = 0; i < strlen( str ); i++ )
333 if( i == 0 && str[i] ==
'-' )
339 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
340 str[i - 1] ==
'0' && str[i] ==
'x' )
346 if( str[i] <
'0' || str[i] >
'9' )
356 *value = strtol( str, NULL, 16 );
358 *value = strtol( str, NULL, 10 );
365 printf(
"Expected integer for parameter and got: %s\n", str );
369 #ifdef POLARSSL_SHA1_C
370 void test_suite_sha1(
char *hex_src_string,
char *hex_hash_string )
372 unsigned char src_str[10000];
373 unsigned char hash_str[10000];
374 unsigned char output[41];
377 memset(src_str, 0x00, 10000);
378 memset(hash_str, 0x00, 10000);
379 memset(output, 0x00, 41);
381 src_len =
unhexify( src_str, hex_src_string );
383 sha1( src_str, src_len, output );
384 hexify( hash_str, output, 20 );
386 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
390 #ifdef POLARSSL_SHA256_C
391 void test_suite_sha224(
char *hex_src_string,
char *hex_hash_string )
393 unsigned char src_str[10000];
394 unsigned char hash_str[10000];
395 unsigned char output[57];
398 memset(src_str, 0x00, 10000);
399 memset(hash_str, 0x00, 10000);
400 memset(output, 0x00, 57);
402 src_len =
unhexify( src_str, hex_src_string );
404 sha256( src_str, src_len, output, 1 );
405 hexify( hash_str, output, 28 );
407 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
411 #ifdef POLARSSL_SHA256_C
412 void test_suite_sha256(
char *hex_src_string,
char *hex_hash_string )
414 unsigned char src_str[10000];
415 unsigned char hash_str[10000];
416 unsigned char output[65];
419 memset(src_str, 0x00, 10000);
420 memset(hash_str, 0x00, 10000);
421 memset(output, 0x00, 65);
423 src_len =
unhexify( src_str, hex_src_string );
425 sha256( src_str, src_len, output, 0 );
426 hexify( hash_str, output, 32 );
428 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
432 #ifdef POLARSSL_SHA512_C
433 void test_suite_sha384(
char *hex_src_string,
char *hex_hash_string )
435 unsigned char src_str[10000];
436 unsigned char hash_str[10000];
437 unsigned char output[97];
440 memset(src_str, 0x00, 10000);
441 memset(hash_str, 0x00, 10000);
442 memset(output, 0x00, 97);
444 src_len =
unhexify( src_str, hex_src_string );
446 sha512( src_str, src_len, output, 1 );
447 hexify( hash_str, output, 48 );
449 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
453 #ifdef POLARSSL_SHA512_C
454 void test_suite_sha512(
char *hex_src_string,
char *hex_hash_string )
456 unsigned char src_str[10000];
457 unsigned char hash_str[10000];
458 unsigned char output[129];
461 memset(src_str, 0x00, 10000);
462 memset(hash_str, 0x00, 10000);
463 memset(output, 0x00, 129);
465 src_len =
unhexify( src_str, hex_src_string );
467 sha512( src_str, src_len, output, 0);
468 hexify( hash_str, output, 64 );
470 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
474 #ifdef POLARSSL_SHA1_C
475 #ifdef POLARSSL_FS_IO
476 void test_suite_sha1_file(
char *filename,
char *hex_hash_string )
478 unsigned char hash_str[41];
479 unsigned char output[21];
481 memset(hash_str, 0x00, 41);
482 memset(output, 0x00, 21);
485 hexify( hash_str, output, 20 );
487 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
492 #ifdef POLARSSL_SHA256_C
493 #ifdef POLARSSL_FS_IO
494 void test_suite_sha224_file(
char *filename,
char *hex_hash_string )
496 unsigned char hash_str[57];
497 unsigned char output[29];
499 memset(hash_str, 0x00, 57);
500 memset(output, 0x00, 29);
503 hexify( hash_str, output, 28 );
505 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
510 #ifdef POLARSSL_SHA256_C
511 #ifdef POLARSSL_FS_IO
512 void test_suite_sha256_file(
char *filename,
char *hex_hash_string )
514 unsigned char hash_str[65];
515 unsigned char output[33];
517 memset(hash_str, 0x00, 65);
518 memset(output, 0x00, 33);
521 hexify( hash_str, output, 32 );
523 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
528 #ifdef POLARSSL_SHA512_C
529 #ifdef POLARSSL_FS_IO
530 void test_suite_sha384_file(
char *filename,
char *hex_hash_string )
532 unsigned char hash_str[97];
533 unsigned char output[49];
535 memset(hash_str, 0x00, 97);
536 memset(output, 0x00, 49);
539 hexify( hash_str, output, 48 );
541 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
546 #ifdef POLARSSL_SHA512_C
547 #ifdef POLARSSL_FS_IO
548 void test_suite_sha512_file(
char *filename,
char *hex_hash_string )
550 unsigned char hash_str[129];
551 unsigned char output[65];
553 memset(hash_str, 0x00, 129);
554 memset(output, 0x00, 65);
557 hexify( hash_str, output, 64 );
559 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
564 #ifdef POLARSSL_SHA1_C
565 #ifdef POLARSSL_SELF_TEST
566 void test_suite_sha1_selftest()
573 #ifdef POLARSSL_SHA256_C
574 #ifdef POLARSSL_SELF_TEST
575 void test_suite_sha256_selftest()
582 #ifdef POLARSSL_SHA512_C
583 #ifdef POLARSSL_SELF_TEST
584 void test_suite_sha512_selftest()
599 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
601 #if defined(POLARSSL_SHA512_C)
607 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
609 #if defined(POLARSSL_SHA1_C)
615 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
617 #if defined(POLARSSL_SHA256_C)
623 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
625 #if defined(POLARSSL_SELF_TEST)
642 #if defined(TEST_SUITE_ACTIVE)
643 if( strcmp( params[0],
"sha1" ) == 0 )
645 #ifdef POLARSSL_SHA1_C
647 char *param1 = params[1];
648 char *param2 = params[2];
652 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
659 test_suite_sha1( param1, param2 );
666 if( strcmp( params[0],
"sha224" ) == 0 )
668 #ifdef POLARSSL_SHA256_C
670 char *param1 = params[1];
671 char *param2 = params[2];
675 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
682 test_suite_sha224( param1, param2 );
689 if( strcmp( params[0],
"sha256" ) == 0 )
691 #ifdef POLARSSL_SHA256_C
693 char *param1 = params[1];
694 char *param2 = params[2];
698 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
705 test_suite_sha256( param1, param2 );
712 if( strcmp( params[0],
"sha384" ) == 0 )
714 #ifdef POLARSSL_SHA512_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_sha384( param1, param2 );
735 if( strcmp( params[0],
"sha512" ) == 0 )
737 #ifdef POLARSSL_SHA512_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_sha512( param1, param2 );
758 if( strcmp( params[0],
"sha1_file" ) == 0 )
760 #ifdef POLARSSL_SHA1_C
761 #ifdef POLARSSL_FS_IO
763 char *param1 = params[1];
764 char *param2 = params[2];
768 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
775 test_suite_sha1_file( param1, param2 );
783 if( strcmp( params[0],
"sha224_file" ) == 0 )
785 #ifdef POLARSSL_SHA256_C
786 #ifdef POLARSSL_FS_IO
788 char *param1 = params[1];
789 char *param2 = params[2];
793 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
800 test_suite_sha224_file( param1, param2 );
808 if( strcmp( params[0],
"sha256_file" ) == 0 )
810 #ifdef POLARSSL_SHA256_C
811 #ifdef POLARSSL_FS_IO
813 char *param1 = params[1];
814 char *param2 = params[2];
818 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
825 test_suite_sha256_file( param1, param2 );
833 if( strcmp( params[0],
"sha384_file" ) == 0 )
835 #ifdef POLARSSL_SHA512_C
836 #ifdef POLARSSL_FS_IO
838 char *param1 = params[1];
839 char *param2 = params[2];
843 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
850 test_suite_sha384_file( param1, param2 );
858 if( strcmp( params[0],
"sha512_file" ) == 0 )
860 #ifdef POLARSSL_SHA512_C
861 #ifdef POLARSSL_FS_IO
863 char *param1 = params[1];
864 char *param2 = params[2];
868 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
875 test_suite_sha512_file( param1, param2 );
883 if( strcmp( params[0],
"sha1_selftest" ) == 0 )
885 #ifdef POLARSSL_SHA1_C
886 #ifdef POLARSSL_SELF_TEST
891 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
896 test_suite_sha1_selftest( );
904 if( strcmp( params[0],
"sha256_selftest" ) == 0 )
906 #ifdef POLARSSL_SHA256_C
907 #ifdef POLARSSL_SELF_TEST
912 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
917 test_suite_sha256_selftest( );
925 if( strcmp( params[0],
"sha512_selftest" ) == 0 )
927 #ifdef POLARSSL_SHA512_C
928 #ifdef POLARSSL_SELF_TEST
933 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
938 test_suite_sha512_selftest( );
948 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
962 ret = fgets( buf, len, f );
966 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
967 buf[strlen(buf) - 1] =
'\0';
968 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
969 buf[strlen(buf) - 1] =
'\0';
982 while( *p !=
'\0' && p < buf + len )
992 if( p + 1 < buf + len )
1004 for( i = 0; i < cnt; i++ )
1011 if( *p ==
'\\' && *(p + 1) ==
'n' )
1016 else if( *p ==
'\\' && *(p + 1) ==
':' )
1021 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1037 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1038 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_shax.data";
1043 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1044 unsigned char alloc_buf[1000000];
1045 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1048 file = fopen( filename,
"r" );
1051 fprintf( stderr,
"Failed to open\n" );
1055 while( !feof( file ) )
1059 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1061 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1062 fprintf( stdout,
" " );
1063 for( i = strlen( buf ) + 1; i < 67; i++ )
1064 fprintf( stdout,
"." );
1065 fprintf( stdout,
" " );
1070 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1074 if( strcmp( params[0],
"depends_on" ) == 0 )
1076 for( i = 1; i < cnt; i++ )
1080 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1091 if( skip == 1 || ret == 3 )
1094 fprintf( stdout,
"----\n" );
1099 fprintf( stdout,
"PASS\n" );
1104 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1111 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1113 if( strlen(buf) != 0 )
1115 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1121 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1122 if( total_errors == 0 )
1123 fprintf( stdout,
"PASSED" );
1125 fprintf( stdout,
"FAILED" );
1127 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1128 total_tests - total_errors, total_tests, total_skipped );
1130 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1131 #if defined(POLARSSL_MEMORY_DEBUG)
1132 memory_buffer_alloc_status();
1134 memory_buffer_alloc_free();
1137 return( total_errors != 0 );
static int test_assert(int correct, char *test)
static int not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
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[])
#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 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)