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_MD2_C
366 void test_suite_md2_text(
char *text_src_string,
char *hex_hash_string )
368 unsigned char src_str[1000];
369 unsigned char hash_str[1000];
370 unsigned char output[33];
372 memset(src_str, 0x00, 1000);
373 memset(hash_str, 0x00, 1000);
374 memset(output, 0x00, 33);
376 strcpy( (
char *) src_str, text_src_string );
378 md2( src_str, strlen( (
char *) src_str ), output );
379 hexify( hash_str, output, 16 );
381 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
385 #ifdef POLARSSL_MD4_C
386 void test_suite_md4_text(
char *text_src_string,
char *hex_hash_string )
388 unsigned char src_str[1000];
389 unsigned char hash_str[1000];
390 unsigned char output[33];
392 memset(src_str, 0x00, 1000);
393 memset(hash_str, 0x00, 1000);
394 memset(output, 0x00, 33);
396 strcpy( (
char *) src_str, text_src_string );
398 md4( src_str, strlen( (
char *) src_str ), output );
399 hexify( hash_str, output, 16 );
401 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
405 #ifdef POLARSSL_MD5_C
406 void test_suite_md5_text(
char *text_src_string,
char *hex_hash_string )
408 unsigned char src_str[1000];
409 unsigned char hash_str[1000];
410 unsigned char output[33];
412 memset(src_str, 0x00, 1000);
413 memset(hash_str, 0x00, 1000);
414 memset(output, 0x00, 33);
416 strcpy( (
char *) src_str, text_src_string );
418 md5( src_str, strlen( (
char *) src_str ), output );
419 hexify( hash_str, output, 16 );
421 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
425 #ifdef POLARSSL_MD2_C
426 void test_suite_md2_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
427 char *hex_hash_string )
429 unsigned char src_str[10000];
430 unsigned char key_str[10000];
431 unsigned char hash_str[10000];
432 unsigned char output[33];
433 int key_len, src_len;
435 memset(src_str, 0x00, 10000);
436 memset(key_str, 0x00, 10000);
437 memset(hash_str, 0x00, 10000);
438 memset(output, 0x00, 33);
440 key_len =
unhexify( key_str, hex_key_string );
441 src_len =
unhexify( src_str, hex_src_string );
443 md2_hmac( key_str, key_len, src_str, src_len, output );
444 hexify( hash_str, output, 16 );
446 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
450 #ifdef POLARSSL_MD4_C
451 void test_suite_md4_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
452 char *hex_hash_string )
454 unsigned char src_str[10000];
455 unsigned char key_str[10000];
456 unsigned char hash_str[10000];
457 unsigned char output[33];
458 int key_len, src_len;
460 memset(src_str, 0x00, 10000);
461 memset(key_str, 0x00, 10000);
462 memset(hash_str, 0x00, 10000);
463 memset(output, 0x00, 33);
465 key_len =
unhexify( key_str, hex_key_string );
466 src_len =
unhexify( src_str, hex_src_string );
468 md4_hmac( key_str, key_len, src_str, src_len, output );
469 hexify( hash_str, output, 16 );
471 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
475 #ifdef POLARSSL_MD5_C
476 void test_suite_md5_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
477 char *hex_hash_string )
479 unsigned char src_str[10000];
480 unsigned char key_str[10000];
481 unsigned char hash_str[10000];
482 unsigned char output[33];
483 int key_len, src_len;
485 memset(src_str, 0x00, 10000);
486 memset(key_str, 0x00, 10000);
487 memset(hash_str, 0x00, 10000);
488 memset(output, 0x00, 33);
490 key_len =
unhexify( key_str, hex_key_string );
491 src_len =
unhexify( src_str, hex_src_string );
493 md5_hmac( key_str, key_len, src_str, src_len, output );
494 hexify( hash_str, output, 16 );
496 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
500 #ifdef POLARSSL_MD2_C
501 #ifdef POLARSSL_FS_IO
502 void test_suite_md2_file(
char *filename,
char *hex_hash_string )
504 unsigned char hash_str[65];
505 unsigned char output[33];
507 memset(hash_str, 0x00, 65);
508 memset(output, 0x00, 33);
511 hexify( hash_str, output, 16 );
513 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
518 #ifdef POLARSSL_MD4_C
519 #ifdef POLARSSL_FS_IO
520 void test_suite_md4_file(
char *filename,
char *hex_hash_string )
522 unsigned char hash_str[65];
523 unsigned char output[33];
525 memset(hash_str, 0x00, 65);
526 memset(output, 0x00, 33);
529 hexify( hash_str, output, 16 );
531 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
536 #ifdef POLARSSL_MD5_C
537 #ifdef POLARSSL_FS_IO
538 void test_suite_md5_file(
char *filename,
char *hex_hash_string )
540 unsigned char hash_str[65];
541 unsigned char output[33];
543 memset(hash_str, 0x00, 65);
544 memset(output, 0x00, 33);
547 hexify( hash_str, output, 16 );
549 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
554 #ifdef POLARSSL_MD2_C
555 #ifdef POLARSSL_SELF_TEST
556 void test_suite_md2_selftest()
563 #ifdef POLARSSL_MD4_C
564 #ifdef POLARSSL_SELF_TEST
565 void test_suite_md4_selftest()
572 #ifdef POLARSSL_MD5_C
573 #ifdef POLARSSL_SELF_TEST
574 void test_suite_md5_selftest()
589 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
591 #if defined(POLARSSL_MD5_C)
597 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
599 #if defined(POLARSSL_SELF_TEST)
605 if( strcmp( str,
"POLARSSL_MD4_C" ) == 0 )
607 #if defined(POLARSSL_MD4_C)
613 if( strcmp( str,
"POLARSSL_MD2_C" ) == 0 )
615 #if defined(POLARSSL_MD2_C)
632 #if defined(TEST_SUITE_ACTIVE)
633 if( strcmp( params[0],
"md2_text" ) == 0 )
635 #ifdef POLARSSL_MD2_C
637 char *param1 = params[1];
638 char *param2 = params[2];
642 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
649 test_suite_md2_text( param1, param2 );
656 if( strcmp( params[0],
"md4_text" ) == 0 )
658 #ifdef POLARSSL_MD4_C
660 char *param1 = params[1];
661 char *param2 = params[2];
665 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
672 test_suite_md4_text( param1, param2 );
679 if( strcmp( params[0],
"md5_text" ) == 0 )
681 #ifdef POLARSSL_MD5_C
683 char *param1 = params[1];
684 char *param2 = params[2];
688 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
695 test_suite_md5_text( param1, param2 );
702 if( strcmp( params[0],
"md2_hmac" ) == 0 )
704 #ifdef POLARSSL_MD2_C
707 char *param2 = params[2];
708 char *param3 = params[3];
709 char *param4 = params[4];
713 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
717 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
722 test_suite_md2_hmac( param1, param2, param3, param4 );
729 if( strcmp( params[0],
"md4_hmac" ) == 0 )
731 #ifdef POLARSSL_MD4_C
734 char *param2 = params[2];
735 char *param3 = params[3];
736 char *param4 = params[4];
740 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
744 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
749 test_suite_md4_hmac( param1, param2, param3, param4 );
756 if( strcmp( params[0],
"md5_hmac" ) == 0 )
758 #ifdef POLARSSL_MD5_C
761 char *param2 = params[2];
762 char *param3 = params[3];
763 char *param4 = params[4];
767 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
771 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
776 test_suite_md5_hmac( param1, param2, param3, param4 );
783 if( strcmp( params[0],
"md2_file" ) == 0 )
785 #ifdef POLARSSL_MD2_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_md2_file( param1, param2 );
808 if( strcmp( params[0],
"md4_file" ) == 0 )
810 #ifdef POLARSSL_MD4_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_md4_file( param1, param2 );
833 if( strcmp( params[0],
"md5_file" ) == 0 )
835 #ifdef POLARSSL_MD5_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_md5_file( param1, param2 );
858 if( strcmp( params[0],
"md2_selftest" ) == 0 )
860 #ifdef POLARSSL_MD2_C
861 #ifdef POLARSSL_SELF_TEST
866 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
871 test_suite_md2_selftest( );
879 if( strcmp( params[0],
"md4_selftest" ) == 0 )
881 #ifdef POLARSSL_MD4_C
882 #ifdef POLARSSL_SELF_TEST
887 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
892 test_suite_md4_selftest( );
900 if( strcmp( params[0],
"md5_selftest" ) == 0 )
902 #ifdef POLARSSL_MD5_C
903 #ifdef POLARSSL_SELF_TEST
908 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
913 test_suite_md5_selftest( );
923 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
937 ret = fgets( buf, len, f );
941 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
942 buf[strlen(buf) - 1] =
'\0';
943 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
944 buf[strlen(buf) - 1] =
'\0';
957 while( *p !=
'\0' && p < buf + len )
967 if( p + 1 < buf + len )
979 for( i = 0; i < cnt; i++ )
986 if( *p ==
'\\' && *(p + 1) ==
'n' )
991 else if( *p ==
'\\' && *(p + 1) ==
':' )
996 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1012 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1013 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_mdx.data";
1018 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1019 unsigned char alloc_buf[1000000];
1020 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1023 file = fopen( filename,
"r" );
1026 fprintf( stderr,
"Failed to open\n" );
1030 while( !feof( file ) )
1034 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1036 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
1037 fprintf( stdout,
" " );
1038 for( i = strlen( buf ) + 1; i < 67; i++ )
1039 fprintf( stdout,
"." );
1040 fprintf( stdout,
" " );
1045 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1049 if( strcmp( params[0],
"depends_on" ) == 0 )
1051 for( i = 1; i < cnt; i++ )
1055 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1066 if( skip == 1 || ret == 3 )
1069 fprintf( stdout,
"----\n" );
1072 else if( ret == 0 && test_errors == 0 )
1074 fprintf( stdout,
"PASS\n" );
1079 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1086 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1088 if( strlen(buf) != 0 )
1090 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1096 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1097 if( total_errors == 0 )
1098 fprintf( stdout,
"PASSED" );
1100 fprintf( stdout,
"FAILED" );
1102 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1103 total_tests - total_errors, total_tests, total_skipped );
1105 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1106 #if defined(POLARSSL_MEMORY_DEBUG)
1107 memory_buffer_alloc_status();
1109 memory_buffer_alloc_free();
1112 return( total_errors != 0 );
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
Info structure for the pseudo random function.
int md4_self_test(int verbose)
Checkup routine.
void md4_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[16])
Output = HMAC-MD4( hmac key, input buffer )
Configuration options (set of defines)
void md2(const unsigned char *input, size_t ilen, unsigned char output[16])
Output = MD2( input buffer )
void md5_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[16])
Output = HMAC-MD5( hmac key, input buffer )
void md4(const unsigned char *input, size_t ilen, unsigned char output[16])
Output = MD4( input buffer )
int main(int argc, char *argv[])
Multi-precision integer library.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
int md2_file(const char *path, unsigned char output[16])
Output = MD2( file contents )
int md5_self_test(int verbose)
Checkup routine.
int md5_file(const char *path, unsigned char output[16])
Output = MD5( file contents )
static int unhexify(unsigned char *obuf, const char *ibuf)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int parse_arguments(char *buf, size_t len, char *params[50])
static int test_assert(int correct, char *test)
int md2_self_test(int verbose)
Checkup routine.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
int verify_string(char **str)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int dispatch_test(int cnt, char *params[50])
void md2_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[16])
Output = HMAC-MD2( hmac key, input buffer )
#define TEST_ASSERT(TEST)
int verify_int(char *str, int *value)
MD4 message digest algorithm (hash function)
int md4_file(const char *path, unsigned char output[16])
Output = MD4( file contents )
MD5 message digest algorithm (hash function)
void md5(const unsigned char *input, size_t ilen, unsigned char output[16])
Output = MD5( input buffer )
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
#define PUT_UINT32_BE(n, b, i)
MD2 message digest algorithm (hash function)
int get_line(FILE *f, char *buf, size_t len)