15 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
21 typedef UINT32 uint32_t;
34 #define GET_UINT32_BE(n,b,i) \
36 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
37 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
38 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
39 | ( (uint32_t) (b)[(i) + 3] ); \
44 #define PUT_UINT32_BE(n,b,i) \
46 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
47 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
48 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
49 (b)[(i) + 3] = (unsigned char) ( (n) ); \
53 static int unhexify(
unsigned char *obuf,
const char *ibuf)
56 int len = strlen(ibuf) / 2;
57 assert(!(strlen(ibuf) %1));
62 if( c >=
'0' && c <=
'9' )
64 else if( c >=
'a' && c <=
'f' )
66 else if( c >=
'A' && c <=
'F' )
72 if( c2 >=
'0' && c2 <=
'9' )
74 else if( c2 >=
'a' && c2 <=
'f' )
76 else if( c2 >=
'A' && c2 <=
'F' )
81 *obuf++ = ( c << 4 ) | c2;
87 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
99 *obuf++ =
'a' + h - 10;
104 *obuf++ =
'a' + l - 10;
120 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
124 if( rng_state != NULL )
127 for( i = 0; i < len; ++i )
138 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
140 if( rng_state != NULL )
143 memset( output, 0, len );
170 if( rng_state == NULL )
179 memcpy( output, info->
buf, use_len );
180 info->
buf += use_len;
184 if( len - use_len > 0 )
185 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
214 uint32_t i, *k, sum, delta=0x9E3779B9;
215 unsigned char result[4];
217 if( rng_state == NULL )
224 size_t use_len = ( len > 4 ) ? 4 : len;
227 for( i = 0; i < 32; i++ )
229 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
231 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
235 memcpy( output, result, use_len );
251 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
254 const char *ibuf = in;
256 assert( len == strlen(ibuf) / 2 );
257 assert(!(strlen(ibuf) %1));
259 obuf = out + (len - 1);
263 if( c >=
'0' && c <=
'9' )
265 else if( c >=
'a' && c <=
'f' )
267 else if( c >=
'A' && c <=
'F' )
273 if( c2 >=
'0' && c2 <=
'9' )
275 else if( c2 >=
'a' && c2 <=
'f' )
277 else if( c2 >=
'A' && c2 <=
'F' )
282 *obuf-- = ( c << 4 ) | c2;
296 #define TEST_SUITE_ACTIVE
305 printf(
"FAILED\n" );
306 printf(
" %s\n", test );
311 #define TEST_ASSERT( TEST ) \
312 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
313 if( test_errors) return; \
318 if( (*str)[0] !=
'"' ||
319 (*str)[strlen( *str ) - 1] !=
'"' )
321 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
326 (*str)[strlen( *str ) - 1] =
'\0';
338 for( i = 0; i < strlen( str ); i++ )
340 if( i == 0 && str[i] ==
'-' )
346 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
347 str[i - 1] ==
'0' && str[i] ==
'x' )
353 if( str[i] <
'0' || str[i] >
'9' )
363 *value = strtol( str, NULL, 16 );
365 *value = strtol( str, NULL, 10 );
372 printf(
"Expected integer for parameter and got: %s\n", str );
376 void test_suite_md_text(
char *text_md_name,
char *text_src_string,
char *hex_hash_string )
379 unsigned char src_str[1000];
380 unsigned char hash_str[1000];
381 unsigned char output[100];
384 memset(md_name, 0x00, 100);
385 memset(src_str, 0x00, 1000);
386 memset(hash_str, 0x00, 1000);
387 memset(output, 0x00, 100);
389 strcpy( (
char *) src_str, text_src_string );
391 strncpy( (
char *) md_name, text_md_name, 100 );
395 TEST_ASSERT ( 0 ==
md( md_info, src_str, strlen( (
char *) src_str ), output ) );
398 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
401 void test_suite_md_hex(
char *text_md_name,
char *hex_src_string,
char *hex_hash_string )
404 unsigned char src_str[10000];
405 unsigned char hash_str[10000];
406 unsigned char output[100];
410 memset(md_name, 0x00, 100);
411 memset(src_str, 0x00, 10000);
412 memset(hash_str, 0x00, 10000);
413 memset(output, 0x00, 100);
415 strncpy( (
char *) md_name, text_md_name, 100 );
419 src_len =
unhexify( src_str, hex_src_string );
420 TEST_ASSERT ( 0 ==
md( md_info, src_str, src_len, output ) );
424 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
427 void test_suite_md_text_multi(
char *text_md_name,
char *text_src_string,
428 char *hex_hash_string )
431 unsigned char src_str[1000];
432 unsigned char hash_str[1000];
433 unsigned char output[100];
438 memset(md_name, 0x00, 100);
439 memset(src_str, 0x00, 1000);
440 memset(hash_str, 0x00, 1000);
441 memset(output, 0x00, 100);
443 strcpy( (
char *) src_str, text_src_string );
445 strncpy( (
char *) md_name, text_md_name, 100 );
458 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
461 void test_suite_md_hex_multi(
char *text_md_name,
char *hex_src_string,
462 char *hex_hash_string )
465 unsigned char src_str[10000];
466 unsigned char hash_str[10000];
467 unsigned char output[100];
472 memset(md_name, 0x00, 100);
473 memset(src_str, 0x00, 10000);
474 memset(hash_str, 0x00, 10000);
475 memset(output, 0x00, 100);
477 strncpy( (
char *) md_name, text_md_name, 100 );
482 src_len =
unhexify( src_str, hex_src_string );
492 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
495 void test_suite_md_hmac(
char *text_md_name,
int trunc_size,
char *hex_key_string,
496 char *hex_src_string,
char *hex_hash_string )
499 unsigned char src_str[10000];
500 unsigned char key_str[10000];
501 unsigned char hash_str[10000];
502 unsigned char output[100];
503 int key_len, src_len;
506 memset(md_name, 0x00, 100);
507 memset(src_str, 0x00, 10000);
508 memset(key_str, 0x00, 10000);
509 memset(hash_str, 0x00, 10000);
510 memset(output, 0x00, 100);
512 strncpy( (
char *) md_name, text_md_name, 100 );
516 key_len =
unhexify( key_str, hex_key_string );
517 src_len =
unhexify( src_str, hex_src_string );
522 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
525 void test_suite_md_hmac_multi(
char *text_md_name,
int trunc_size,
char *hex_key_string,
526 char *hex_src_string,
char *hex_hash_string )
529 unsigned char src_str[10000];
530 unsigned char key_str[10000];
531 unsigned char hash_str[10000];
532 unsigned char output[100];
533 int key_len, src_len;
537 memset(md_name, 0x00, 100);
538 memset(src_str, 0x00, 10000);
539 memset(key_str, 0x00, 10000);
540 memset(hash_str, 0x00, 10000);
541 memset(output, 0x00, 100);
543 strncpy( (
char *) md_name, text_md_name, 100 );
548 key_len =
unhexify( key_str, hex_key_string );
549 src_len =
unhexify( src_str, hex_src_string );
559 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
562 #ifdef POLARSSL_FS_IO
563 void test_suite_md_file(
char *text_md_name,
char *filename,
char *hex_hash_string )
566 unsigned char hash_str[1000];
567 unsigned char output[100];
570 memset(md_name, 0x00, 100);
571 memset(hash_str, 0x00, 1000);
572 memset(output, 0x00, 100);
574 strncpy( (
char *) md_name, text_md_name, 100 );
578 md_file( md_info, filename, output);
581 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
594 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
596 #if defined(POLARSSL_MD5_C)
602 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
604 #if defined(POLARSSL_SHA256_C)
610 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
612 #if defined(POLARSSL_SHA1_C)
618 if( strcmp( str,
"POLARSSL_MD4_C" ) == 0 )
620 #if defined(POLARSSL_MD4_C)
626 if( strcmp( str,
"POLARSSL_MD_C" ) == 0 )
628 #if defined(POLARSSL_MD_C)
634 if( strcmp( str,
"POLARSSL_MD2_C" ) == 0 )
636 #if defined(POLARSSL_MD2_C)
642 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
644 #if defined(POLARSSL_SHA512_C)
661 #if defined(TEST_SUITE_ACTIVE)
662 if( strcmp( params[0],
"md_text" ) == 0 )
665 char *param1 = params[1];
666 char *param2 = params[2];
667 char *param3 = params[3];
671 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
679 test_suite_md_text( param1, param2, param3 );
685 if( strcmp( params[0],
"md_hex" ) == 0 )
688 char *param1 = params[1];
689 char *param2 = params[2];
690 char *param3 = params[3];
694 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
702 test_suite_md_hex( param1, param2, param3 );
708 if( strcmp( params[0],
"md_text_multi" ) == 0 )
711 char *param1 = params[1];
712 char *param2 = params[2];
713 char *param3 = params[3];
717 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
725 test_suite_md_text_multi( param1, param2, param3 );
731 if( strcmp( params[0],
"md_hex_multi" ) == 0 )
734 char *param1 = params[1];
735 char *param2 = params[2];
736 char *param3 = params[3];
740 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
748 test_suite_md_hex_multi( param1, param2, param3 );
754 if( strcmp( params[0],
"md_hmac" ) == 0 )
757 char *param1 = params[1];
759 char *param3 = params[3];
760 char *param4 = params[4];
761 char *param5 = params[5];
765 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
770 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
775 test_suite_md_hmac( param1, param2, param3, param4, param5 );
781 if( strcmp( params[0],
"md_hmac_multi" ) == 0 )
784 char *param1 = params[1];
786 char *param3 = params[3];
787 char *param4 = params[4];
788 char *param5 = params[5];
792 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
797 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
802 test_suite_md_hmac_multi( param1, param2, param3, param4, param5 );
808 if( strcmp( params[0],
"md_file" ) == 0 )
810 #ifdef POLARSSL_FS_IO
812 char *param1 = params[1];
813 char *param2 = params[2];
814 char *param3 = params[3];
818 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
826 test_suite_md_file( param1, param2, param3 );
835 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
849 ret = fgets( buf, len, f );
853 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
854 buf[strlen(buf) - 1] =
'\0';
855 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
856 buf[strlen(buf) - 1] =
'\0';
869 while( *p !=
'\0' && p < buf + len )
879 if( p + 1 < buf + len )
891 for( i = 0; i < cnt; i++ )
898 if( *p ==
'\\' && *(p + 1) ==
'n' )
903 else if( *p ==
'\\' && *(p + 1) ==
':' )
908 else if( *p ==
'\\' && *(p + 1) ==
'?' )
924 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
925 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_md.data";
930 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
931 unsigned char alloc_buf[1000000];
932 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
935 file = fopen( filename,
"r" );
938 fprintf( stderr,
"Failed to open\n" );
942 while( !feof( file ) )
946 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
948 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
949 fprintf( stdout,
" " );
950 for( i = strlen( buf ) + 1; i < 67; i++ )
951 fprintf( stdout,
"." );
952 fprintf( stdout,
" " );
957 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
961 if( strcmp( params[0],
"depends_on" ) == 0 )
963 for( i = 1; i < cnt; i++ )
967 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
978 if( skip == 1 || ret == 3 )
981 fprintf( stdout,
"----\n" );
986 fprintf( stdout,
"PASS\n" );
991 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
998 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1000 if( strlen(buf) != 0 )
1002 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1008 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1009 if( total_errors == 0 )
1010 fprintf( stdout,
"PASSED" );
1012 fprintf( stdout,
"FAILED" );
1014 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1015 total_tests - total_errors, total_tests, total_skipped );
1017 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1018 #if defined(POLARSSL_MEMORY_DEBUG)
1019 memory_buffer_alloc_status();
1021 memory_buffer_alloc_free();
1024 return( total_errors != 0 );