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_MD2_C
370 void test_suite_md2_text(
char *text_src_string,
char *hex_hash_string )
372 unsigned char src_str[1000];
373 unsigned char hash_str[1000];
374 unsigned char output[33];
376 memset(src_str, 0x00, 1000);
377 memset(hash_str, 0x00, 1000);
378 memset(output, 0x00, 33);
380 strcpy( (
char *) src_str, text_src_string );
382 md2( src_str, strlen( (
char *) src_str ), output );
383 hexify( hash_str, output, 16 );
385 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
389 #ifdef POLARSSL_MD4_C
390 void test_suite_md4_text(
char *text_src_string,
char *hex_hash_string )
392 unsigned char src_str[1000];
393 unsigned char hash_str[1000];
394 unsigned char output[33];
396 memset(src_str, 0x00, 1000);
397 memset(hash_str, 0x00, 1000);
398 memset(output, 0x00, 33);
400 strcpy( (
char *) src_str, text_src_string );
402 md4( src_str, strlen( (
char *) src_str ), output );
403 hexify( hash_str, output, 16 );
405 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
409 #ifdef POLARSSL_MD5_C
410 void test_suite_md5_text(
char *text_src_string,
char *hex_hash_string )
412 unsigned char src_str[1000];
413 unsigned char hash_str[1000];
414 unsigned char output[33];
416 memset(src_str, 0x00, 1000);
417 memset(hash_str, 0x00, 1000);
418 memset(output, 0x00, 33);
420 strcpy( (
char *) src_str, text_src_string );
422 md5( src_str, strlen( (
char *) src_str ), output );
423 hexify( hash_str, output, 16 );
425 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
429 #ifdef POLARSSL_MD2_C
430 void test_suite_md2_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
431 char *hex_hash_string )
433 unsigned char src_str[10000];
434 unsigned char key_str[10000];
435 unsigned char hash_str[10000];
436 unsigned char output[33];
437 int key_len, src_len;
439 memset(src_str, 0x00, 10000);
440 memset(key_str, 0x00, 10000);
441 memset(hash_str, 0x00, 10000);
442 memset(output, 0x00, 33);
444 key_len =
unhexify( key_str, hex_key_string );
445 src_len =
unhexify( src_str, hex_src_string );
447 md2_hmac( key_str, key_len, src_str, src_len, output );
448 hexify( hash_str, output, 16 );
450 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
454 #ifdef POLARSSL_MD4_C
455 void test_suite_md4_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
456 char *hex_hash_string )
458 unsigned char src_str[10000];
459 unsigned char key_str[10000];
460 unsigned char hash_str[10000];
461 unsigned char output[33];
462 int key_len, src_len;
464 memset(src_str, 0x00, 10000);
465 memset(key_str, 0x00, 10000);
466 memset(hash_str, 0x00, 10000);
467 memset(output, 0x00, 33);
469 key_len =
unhexify( key_str, hex_key_string );
470 src_len =
unhexify( src_str, hex_src_string );
472 md4_hmac( key_str, key_len, src_str, src_len, output );
473 hexify( hash_str, output, 16 );
475 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
479 #ifdef POLARSSL_MD5_C
480 void test_suite_md5_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
481 char *hex_hash_string )
483 unsigned char src_str[10000];
484 unsigned char key_str[10000];
485 unsigned char hash_str[10000];
486 unsigned char output[33];
487 int key_len, src_len;
489 memset(src_str, 0x00, 10000);
490 memset(key_str, 0x00, 10000);
491 memset(hash_str, 0x00, 10000);
492 memset(output, 0x00, 33);
494 key_len =
unhexify( key_str, hex_key_string );
495 src_len =
unhexify( src_str, hex_src_string );
497 md5_hmac( key_str, key_len, src_str, src_len, output );
498 hexify( hash_str, output, 16 );
500 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
504 #ifdef POLARSSL_MD2_C
505 #ifdef POLARSSL_FS_IO
506 void test_suite_md2_file(
char *filename,
char *hex_hash_string )
508 unsigned char hash_str[65];
509 unsigned char output[33];
511 memset(hash_str, 0x00, 65);
512 memset(output, 0x00, 33);
515 hexify( hash_str, output, 16 );
517 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
522 #ifdef POLARSSL_MD4_C
523 #ifdef POLARSSL_FS_IO
524 void test_suite_md4_file(
char *filename,
char *hex_hash_string )
526 unsigned char hash_str[65];
527 unsigned char output[33];
529 memset(hash_str, 0x00, 65);
530 memset(output, 0x00, 33);
533 hexify( hash_str, output, 16 );
535 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
540 #ifdef POLARSSL_MD5_C
541 #ifdef POLARSSL_FS_IO
542 void test_suite_md5_file(
char *filename,
char *hex_hash_string )
544 unsigned char hash_str[65];
545 unsigned char output[33];
547 memset(hash_str, 0x00, 65);
548 memset(output, 0x00, 33);
551 hexify( hash_str, output, 16 );
553 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
558 #ifdef POLARSSL_MD2_C
559 #ifdef POLARSSL_SELF_TEST
560 void test_suite_md2_selftest()
567 #ifdef POLARSSL_MD4_C
568 #ifdef POLARSSL_SELF_TEST
569 void test_suite_md4_selftest()
576 #ifdef POLARSSL_MD5_C
577 #ifdef POLARSSL_SELF_TEST
578 void test_suite_md5_selftest()
593 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
595 #if defined(POLARSSL_SELF_TEST)
601 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
603 #if defined(POLARSSL_MD5_C)
609 if( strcmp( str,
"POLARSSL_MD4_C" ) == 0 )
611 #if defined(POLARSSL_MD4_C)
617 if( strcmp( str,
"POLARSSL_MD2_C" ) == 0 )
619 #if defined(POLARSSL_MD2_C)
636 #if defined(TEST_SUITE_ACTIVE)
637 if( strcmp( params[0],
"md2_text" ) == 0 )
639 #ifdef POLARSSL_MD2_C
641 char *param1 = params[1];
642 char *param2 = params[2];
646 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
653 test_suite_md2_text( param1, param2 );
660 if( strcmp( params[0],
"md4_text" ) == 0 )
662 #ifdef POLARSSL_MD4_C
664 char *param1 = params[1];
665 char *param2 = params[2];
669 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
676 test_suite_md4_text( param1, param2 );
683 if( strcmp( params[0],
"md5_text" ) == 0 )
685 #ifdef POLARSSL_MD5_C
687 char *param1 = params[1];
688 char *param2 = params[2];
692 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
699 test_suite_md5_text( param1, param2 );
706 if( strcmp( params[0],
"md2_hmac" ) == 0 )
708 #ifdef POLARSSL_MD2_C
711 char *param2 = params[2];
712 char *param3 = params[3];
713 char *param4 = params[4];
717 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
721 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
726 test_suite_md2_hmac( param1, param2, param3, param4 );
733 if( strcmp( params[0],
"md4_hmac" ) == 0 )
735 #ifdef POLARSSL_MD4_C
738 char *param2 = params[2];
739 char *param3 = params[3];
740 char *param4 = params[4];
744 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
748 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
753 test_suite_md4_hmac( param1, param2, param3, param4 );
760 if( strcmp( params[0],
"md5_hmac" ) == 0 )
762 #ifdef POLARSSL_MD5_C
765 char *param2 = params[2];
766 char *param3 = params[3];
767 char *param4 = params[4];
771 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
775 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
780 test_suite_md5_hmac( param1, param2, param3, param4 );
787 if( strcmp( params[0],
"md2_file" ) == 0 )
789 #ifdef POLARSSL_MD2_C
790 #ifdef POLARSSL_FS_IO
792 char *param1 = params[1];
793 char *param2 = params[2];
797 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
804 test_suite_md2_file( param1, param2 );
812 if( strcmp( params[0],
"md4_file" ) == 0 )
814 #ifdef POLARSSL_MD4_C
815 #ifdef POLARSSL_FS_IO
817 char *param1 = params[1];
818 char *param2 = params[2];
822 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
829 test_suite_md4_file( param1, param2 );
837 if( strcmp( params[0],
"md5_file" ) == 0 )
839 #ifdef POLARSSL_MD5_C
840 #ifdef POLARSSL_FS_IO
842 char *param1 = params[1];
843 char *param2 = params[2];
847 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
854 test_suite_md5_file( param1, param2 );
862 if( strcmp( params[0],
"md2_selftest" ) == 0 )
864 #ifdef POLARSSL_MD2_C
865 #ifdef POLARSSL_SELF_TEST
870 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
875 test_suite_md2_selftest( );
883 if( strcmp( params[0],
"md4_selftest" ) == 0 )
885 #ifdef POLARSSL_MD4_C
886 #ifdef POLARSSL_SELF_TEST
891 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
896 test_suite_md4_selftest( );
904 if( strcmp( params[0],
"md5_selftest" ) == 0 )
906 #ifdef POLARSSL_MD5_C
907 #ifdef POLARSSL_SELF_TEST
912 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
917 test_suite_md5_selftest( );
927 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
941 ret = fgets( buf, len, f );
945 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
946 buf[strlen(buf) - 1] =
'\0';
947 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
948 buf[strlen(buf) - 1] =
'\0';
961 while( *p !=
'\0' && p < buf + len )
971 if( p + 1 < buf + len )
983 for( i = 0; i < cnt; i++ )
990 if( *p ==
'\\' && *(p + 1) ==
'n' )
995 else if( *p ==
'\\' && *(p + 1) ==
':' )
1000 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1016 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1017 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_mdx.data";
1022 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1023 unsigned char alloc_buf[1000000];
1024 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1027 file = fopen( filename,
"r" );
1030 fprintf( stderr,
"Failed to open\n" );
1034 while( !feof( file ) )
1038 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1040 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1041 fprintf( stdout,
" " );
1042 for( i = strlen( buf ) + 1; i < 67; i++ )
1043 fprintf( stdout,
"." );
1044 fprintf( stdout,
" " );
1049 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1053 if( strcmp( params[0],
"depends_on" ) == 0 )
1055 for( i = 1; i < cnt; i++ )
1059 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1070 if( skip == 1 || ret == 3 )
1073 fprintf( stdout,
"----\n" );
1078 fprintf( stdout,
"PASS\n" );
1083 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1090 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1092 if( strlen(buf) != 0 )
1094 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1100 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1101 if( total_errors == 0 )
1102 fprintf( stdout,
"PASSED" );
1104 fprintf( stdout,
"FAILED" );
1106 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1107 total_tests - total_errors, total_tests, total_skipped );
1109 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1110 #if defined(POLARSSL_MEMORY_DEBUG)
1111 memory_buffer_alloc_status();
1113 memory_buffer_alloc_free();
1116 return( total_errors != 0 );