00001 #include <polarssl/config.h>
00002
00003
00004 #include <polarssl/md2.h>
00005 #include <polarssl/md4.h>
00006 #include <polarssl/md5.h>
00007
00008
00009 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00010 #include "polarssl/memory.h"
00011 #endif
00012
00013 #if defined(WANT_NOT_RND_MPI)
00014 #if defined(POLARSSL_BIGNUM_C)
00015 #include "polarssl/bignum.h"
00016 #else
00017 #error "not_rnd_mpi() need bignum.c"
00018 #endif
00019 #endif
00020
00021 #ifdef _MSC_VER
00022 #include <basetsd.h>
00023 typedef UINT32 uint32_t;
00024 #else
00025 #include <inttypes.h>
00026 #endif
00027
00028 #include <assert.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031
00032
00033
00034
00035 #ifndef GET_UINT32_BE
00036 #define GET_UINT32_BE(n,b,i) \
00037 { \
00038 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
00039 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
00040 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
00041 | ( (uint32_t) (b)[(i) + 3] ); \
00042 }
00043 #endif
00044
00045 #ifndef PUT_UINT32_BE
00046 #define PUT_UINT32_BE(n,b,i) \
00047 { \
00048 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
00049 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
00050 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
00051 (b)[(i) + 3] = (unsigned char) ( (n) ); \
00052 }
00053 #endif
00054
00055 static int unhexify(unsigned char *obuf, const char *ibuf)
00056 {
00057 unsigned char c, c2;
00058 int len = strlen(ibuf) / 2;
00059 assert(!(strlen(ibuf) %1));
00060
00061 while (*ibuf != 0)
00062 {
00063 c = *ibuf++;
00064 if( c >= '0' && c <= '9' )
00065 c -= '0';
00066 else if( c >= 'a' && c <= 'f' )
00067 c -= 'a' - 10;
00068 else if( c >= 'A' && c <= 'F' )
00069 c -= 'A' - 10;
00070 else
00071 assert( 0 );
00072
00073 c2 = *ibuf++;
00074 if( c2 >= '0' && c2 <= '9' )
00075 c2 -= '0';
00076 else if( c2 >= 'a' && c2 <= 'f' )
00077 c2 -= 'a' - 10;
00078 else if( c2 >= 'A' && c2 <= 'F' )
00079 c2 -= 'A' - 10;
00080 else
00081 assert( 0 );
00082
00083 *obuf++ = ( c << 4 ) | c2;
00084 }
00085
00086 return len;
00087 }
00088
00089 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
00090 {
00091 unsigned char l, h;
00092
00093 while (len != 0)
00094 {
00095 h = (*ibuf) / 16;
00096 l = (*ibuf) % 16;
00097
00098 if( h < 10 )
00099 *obuf++ = '0' + h;
00100 else
00101 *obuf++ = 'a' + h - 10;
00102
00103 if( l < 10 )
00104 *obuf++ = '0' + l;
00105 else
00106 *obuf++ = 'a' + l - 10;
00107
00108 ++ibuf;
00109 len--;
00110 }
00111 }
00112
00122 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
00123 {
00124 size_t i;
00125
00126 if( rng_state != NULL )
00127 rng_state = NULL;
00128
00129 for( i = 0; i < len; ++i )
00130 output[i] = rand();
00131
00132 return( 0 );
00133 }
00134
00140 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
00141 {
00142 if( rng_state != NULL )
00143 rng_state = NULL;
00144
00145 memset( output, 0, len );
00146
00147 return( 0 );
00148 }
00149
00150 typedef struct
00151 {
00152 unsigned char *buf;
00153 size_t length;
00154 } rnd_buf_info;
00155
00167 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
00168 {
00169 rnd_buf_info *info = (rnd_buf_info *) rng_state;
00170 size_t use_len;
00171
00172 if( rng_state == NULL )
00173 return( rnd_std_rand( NULL, output, len ) );
00174
00175 use_len = len;
00176 if( len > info->length )
00177 use_len = info->length;
00178
00179 if( use_len )
00180 {
00181 memcpy( output, info->buf, use_len );
00182 info->buf += use_len;
00183 info->length -= use_len;
00184 }
00185
00186 if( len - use_len > 0 )
00187 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
00188
00189 return( 0 );
00190 }
00191
00199 typedef struct
00200 {
00201 uint32_t key[16];
00202 uint32_t v0, v1;
00203 } rnd_pseudo_info;
00204
00213 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
00214 {
00215 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
00216 uint32_t i, *k, sum, delta=0x9E3779B9;
00217 unsigned char result[4];
00218
00219 if( rng_state == NULL )
00220 return( rnd_std_rand( NULL, output, len ) );
00221
00222 k = info->key;
00223
00224 while( len > 0 )
00225 {
00226 size_t use_len = ( len > 4 ) ? 4 : len;
00227 sum = 0;
00228
00229 for( i = 0; i < 32; i++ )
00230 {
00231 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
00232 sum += delta;
00233 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
00234 }
00235
00236 PUT_UINT32_BE( info->v0, result, 0 );
00237 memcpy( output, result, use_len );
00238 len -= use_len;
00239 }
00240
00241 return( 0 );
00242 }
00243
00244 #if defined(WANT_NOT_RND_MPI)
00245
00253 #define ciL (sizeof(t_uint))
00254 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
00255 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
00256 {
00257 char *str = (char *) in;
00258 mpi X;
00259
00260
00261
00262
00263
00264 X.s = 1;
00265 X.p = (t_uint *) out;
00266 X.n = CHARS_TO_LIMBS( len );
00267
00268
00269
00270
00271
00272 assert( strlen( str ) / 2 == len );
00273
00274 return( mpi_read_string( &X, 16, str ) );
00275 }
00276 #endif
00277
00278
00279 #include <stdio.h>
00280 #include <string.h>
00281
00282 static int test_errors = 0;
00283
00284
00285 #define TEST_SUITE_ACTIVE
00286
00287 static int test_assert( int correct, char *test )
00288 {
00289 if( correct )
00290 return( 0 );
00291
00292 test_errors++;
00293 if( test_errors == 1 )
00294 printf( "FAILED\n" );
00295 printf( " %s\n", test );
00296
00297 return( 1 );
00298 }
00299
00300 #define TEST_ASSERT( TEST ) \
00301 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
00302 if( test_errors) return; \
00303 } while (0)
00304
00305 int verify_string( char **str )
00306 {
00307 if( (*str)[0] != '"' ||
00308 (*str)[strlen( *str ) - 1] != '"' )
00309 {
00310 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
00311 return( -1 );
00312 }
00313
00314 (*str)++;
00315 (*str)[strlen( *str ) - 1] = '\0';
00316
00317 return( 0 );
00318 }
00319
00320 int verify_int( char *str, int *value )
00321 {
00322 size_t i;
00323 int minus = 0;
00324 int digits = 1;
00325 int hex = 0;
00326
00327 for( i = 0; i < strlen( str ); i++ )
00328 {
00329 if( i == 0 && str[i] == '-' )
00330 {
00331 minus = 1;
00332 continue;
00333 }
00334
00335 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
00336 str[i - 1] == '0' && str[i] == 'x' )
00337 {
00338 hex = 1;
00339 continue;
00340 }
00341
00342 if( str[i] < '0' || str[i] > '9' )
00343 {
00344 digits = 0;
00345 break;
00346 }
00347 }
00348
00349 if( digits )
00350 {
00351 if( hex )
00352 *value = strtol( str, NULL, 16 );
00353 else
00354 *value = strtol( str, NULL, 10 );
00355
00356 return( 0 );
00357 }
00358
00359
00360
00361 printf( "Expected integer for parameter and got: %s\n", str );
00362 return( -1 );
00363 }
00364
00365 #ifdef POLARSSL_MD2_C
00366 void test_suite_md2_text( char *text_src_string, char *hex_hash_string )
00367 {
00368 unsigned char src_str[1000];
00369 unsigned char hash_str[1000];
00370 unsigned char output[33];
00371
00372 memset(src_str, 0x00, 1000);
00373 memset(hash_str, 0x00, 1000);
00374 memset(output, 0x00, 33);
00375
00376 strcpy( (char *) src_str, text_src_string );
00377
00378 md2( src_str, strlen( (char *) src_str ), output );
00379 hexify( hash_str, output, 16 );
00380
00381 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00382 }
00383 #endif
00384
00385 #ifdef POLARSSL_MD4_C
00386 void test_suite_md4_text( char *text_src_string, char *hex_hash_string )
00387 {
00388 unsigned char src_str[1000];
00389 unsigned char hash_str[1000];
00390 unsigned char output[33];
00391
00392 memset(src_str, 0x00, 1000);
00393 memset(hash_str, 0x00, 1000);
00394 memset(output, 0x00, 33);
00395
00396 strcpy( (char *) src_str, text_src_string );
00397
00398 md4( src_str, strlen( (char *) src_str ), output );
00399 hexify( hash_str, output, 16 );
00400
00401 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00402 }
00403 #endif
00404
00405 #ifdef POLARSSL_MD5_C
00406 void test_suite_md5_text( char *text_src_string, char *hex_hash_string )
00407 {
00408 unsigned char src_str[1000];
00409 unsigned char hash_str[1000];
00410 unsigned char output[33];
00411
00412 memset(src_str, 0x00, 1000);
00413 memset(hash_str, 0x00, 1000);
00414 memset(output, 0x00, 33);
00415
00416 strcpy( (char *) src_str, text_src_string );
00417
00418 md5( src_str, strlen( (char *) src_str ), output );
00419 hexify( hash_str, output, 16 );
00420
00421 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00422 }
00423 #endif
00424
00425 #ifdef POLARSSL_MD2_C
00426 void test_suite_md2_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
00427 char *hex_hash_string )
00428 {
00429 unsigned char src_str[10000];
00430 unsigned char key_str[10000];
00431 unsigned char hash_str[10000];
00432 unsigned char output[33];
00433 int key_len, src_len;
00434
00435 memset(src_str, 0x00, 10000);
00436 memset(key_str, 0x00, 10000);
00437 memset(hash_str, 0x00, 10000);
00438 memset(output, 0x00, 33);
00439
00440 key_len = unhexify( key_str, hex_key_string );
00441 src_len = unhexify( src_str, hex_src_string );
00442
00443 md2_hmac( key_str, key_len, src_str, src_len, output );
00444 hexify( hash_str, output, 16 );
00445
00446 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
00447 }
00448 #endif
00449
00450 #ifdef POLARSSL_MD4_C
00451 void test_suite_md4_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
00452 char *hex_hash_string )
00453 {
00454 unsigned char src_str[10000];
00455 unsigned char key_str[10000];
00456 unsigned char hash_str[10000];
00457 unsigned char output[33];
00458 int key_len, src_len;
00459
00460 memset(src_str, 0x00, 10000);
00461 memset(key_str, 0x00, 10000);
00462 memset(hash_str, 0x00, 10000);
00463 memset(output, 0x00, 33);
00464
00465 key_len = unhexify( key_str, hex_key_string );
00466 src_len = unhexify( src_str, hex_src_string );
00467
00468 md4_hmac( key_str, key_len, src_str, src_len, output );
00469 hexify( hash_str, output, 16 );
00470
00471 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
00472 }
00473 #endif
00474
00475 #ifdef POLARSSL_MD5_C
00476 void test_suite_md5_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
00477 char *hex_hash_string )
00478 {
00479 unsigned char src_str[10000];
00480 unsigned char key_str[10000];
00481 unsigned char hash_str[10000];
00482 unsigned char output[33];
00483 int key_len, src_len;
00484
00485 memset(src_str, 0x00, 10000);
00486 memset(key_str, 0x00, 10000);
00487 memset(hash_str, 0x00, 10000);
00488 memset(output, 0x00, 33);
00489
00490 key_len = unhexify( key_str, hex_key_string );
00491 src_len = unhexify( src_str, hex_src_string );
00492
00493 md5_hmac( key_str, key_len, src_str, src_len, output );
00494 hexify( hash_str, output, 16 );
00495
00496 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
00497 }
00498 #endif
00499
00500 #ifdef POLARSSL_MD2_C
00501 #ifdef POLARSSL_FS_IO
00502 void test_suite_md2_file( char *filename, char *hex_hash_string )
00503 {
00504 unsigned char hash_str[65];
00505 unsigned char output[33];
00506
00507 memset(hash_str, 0x00, 65);
00508 memset(output, 0x00, 33);
00509
00510 md2_file( filename, output);
00511 hexify( hash_str, output, 16 );
00512
00513 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00514 }
00515 #endif
00516 #endif
00517
00518 #ifdef POLARSSL_MD4_C
00519 #ifdef POLARSSL_FS_IO
00520 void test_suite_md4_file( char *filename, char *hex_hash_string )
00521 {
00522 unsigned char hash_str[65];
00523 unsigned char output[33];
00524
00525 memset(hash_str, 0x00, 65);
00526 memset(output, 0x00, 33);
00527
00528 md4_file( filename, output);
00529 hexify( hash_str, output, 16 );
00530
00531 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00532 }
00533 #endif
00534 #endif
00535
00536 #ifdef POLARSSL_MD5_C
00537 #ifdef POLARSSL_FS_IO
00538 void test_suite_md5_file( char *filename, char *hex_hash_string )
00539 {
00540 unsigned char hash_str[65];
00541 unsigned char output[33];
00542
00543 memset(hash_str, 0x00, 65);
00544 memset(output, 0x00, 33);
00545
00546 md5_file( filename, output);
00547 hexify( hash_str, output, 16 );
00548
00549 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00550 }
00551 #endif
00552 #endif
00553
00554 #ifdef POLARSSL_MD2_C
00555 #ifdef POLARSSL_SELF_TEST
00556 void test_suite_md2_selftest()
00557 {
00558 TEST_ASSERT( md2_self_test( 0 ) == 0 );
00559 }
00560 #endif
00561 #endif
00562
00563 #ifdef POLARSSL_MD4_C
00564 #ifdef POLARSSL_SELF_TEST
00565 void test_suite_md4_selftest()
00566 {
00567 TEST_ASSERT( md4_self_test( 0 ) == 0 );
00568 }
00569 #endif
00570 #endif
00571
00572 #ifdef POLARSSL_MD5_C
00573 #ifdef POLARSSL_SELF_TEST
00574 void test_suite_md5_selftest()
00575 {
00576 TEST_ASSERT( md5_self_test( 0 ) == 0 );
00577 }
00578 #endif
00579 #endif
00580
00581
00582
00583
00584 int dep_check( char *str )
00585 {
00586 if( str == NULL )
00587 return( 1 );
00588
00589 if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
00590 {
00591 #if defined(POLARSSL_SELF_TEST)
00592 return( 0 );
00593 #else
00594 return( 1 );
00595 #endif
00596 }
00597 if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
00598 {
00599 #if defined(POLARSSL_MD5_C)
00600 return( 0 );
00601 #else
00602 return( 1 );
00603 #endif
00604 }
00605 if( strcmp( str, "POLARSSL_MD4_C" ) == 0 )
00606 {
00607 #if defined(POLARSSL_MD4_C)
00608 return( 0 );
00609 #else
00610 return( 1 );
00611 #endif
00612 }
00613 if( strcmp( str, "POLARSSL_MD2_C" ) == 0 )
00614 {
00615 #if defined(POLARSSL_MD2_C)
00616 return( 0 );
00617 #else
00618 return( 1 );
00619 #endif
00620 }
00621
00622
00623 return( 1 );
00624 }
00625
00626 int dispatch_test(int cnt, char *params[50])
00627 {
00628 int ret;
00629 ((void) cnt);
00630 ((void) params);
00631
00632 #if defined(TEST_SUITE_ACTIVE)
00633 if( strcmp( params[0], "md2_text" ) == 0 )
00634 {
00635 #ifdef POLARSSL_MD2_C
00636
00637 char *param1 = params[1];
00638 char *param2 = params[2];
00639
00640 if( cnt != 3 )
00641 {
00642 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00643 return( 2 );
00644 }
00645
00646 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00647 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00648
00649 test_suite_md2_text( param1, param2 );
00650 return ( 0 );
00651 #endif
00652
00653 return ( 3 );
00654 }
00655 else
00656 if( strcmp( params[0], "md4_text" ) == 0 )
00657 {
00658 #ifdef POLARSSL_MD4_C
00659
00660 char *param1 = params[1];
00661 char *param2 = params[2];
00662
00663 if( cnt != 3 )
00664 {
00665 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00666 return( 2 );
00667 }
00668
00669 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00670 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00671
00672 test_suite_md4_text( param1, param2 );
00673 return ( 0 );
00674 #endif
00675
00676 return ( 3 );
00677 }
00678 else
00679 if( strcmp( params[0], "md5_text" ) == 0 )
00680 {
00681 #ifdef POLARSSL_MD5_C
00682
00683 char *param1 = params[1];
00684 char *param2 = params[2];
00685
00686 if( cnt != 3 )
00687 {
00688 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00689 return( 2 );
00690 }
00691
00692 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00693 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00694
00695 test_suite_md5_text( param1, param2 );
00696 return ( 0 );
00697 #endif
00698
00699 return ( 3 );
00700 }
00701 else
00702 if( strcmp( params[0], "md2_hmac" ) == 0 )
00703 {
00704 #ifdef POLARSSL_MD2_C
00705
00706 int param1;
00707 char *param2 = params[2];
00708 char *param3 = params[3];
00709 char *param4 = params[4];
00710
00711 if( cnt != 5 )
00712 {
00713 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
00714 return( 2 );
00715 }
00716
00717 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00718 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00719 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00720 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00721
00722 test_suite_md2_hmac( param1, param2, param3, param4 );
00723 return ( 0 );
00724 #endif
00725
00726 return ( 3 );
00727 }
00728 else
00729 if( strcmp( params[0], "md4_hmac" ) == 0 )
00730 {
00731 #ifdef POLARSSL_MD4_C
00732
00733 int param1;
00734 char *param2 = params[2];
00735 char *param3 = params[3];
00736 char *param4 = params[4];
00737
00738 if( cnt != 5 )
00739 {
00740 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
00741 return( 2 );
00742 }
00743
00744 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00745 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00746 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00747 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00748
00749 test_suite_md4_hmac( param1, param2, param3, param4 );
00750 return ( 0 );
00751 #endif
00752
00753 return ( 3 );
00754 }
00755 else
00756 if( strcmp( params[0], "md5_hmac" ) == 0 )
00757 {
00758 #ifdef POLARSSL_MD5_C
00759
00760 int param1;
00761 char *param2 = params[2];
00762 char *param3 = params[3];
00763 char *param4 = params[4];
00764
00765 if( cnt != 5 )
00766 {
00767 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
00768 return( 2 );
00769 }
00770
00771 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00772 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00773 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00774 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00775
00776 test_suite_md5_hmac( param1, param2, param3, param4 );
00777 return ( 0 );
00778 #endif
00779
00780 return ( 3 );
00781 }
00782 else
00783 if( strcmp( params[0], "md2_file" ) == 0 )
00784 {
00785 #ifdef POLARSSL_MD2_C
00786 #ifdef POLARSSL_FS_IO
00787
00788 char *param1 = params[1];
00789 char *param2 = params[2];
00790
00791 if( cnt != 3 )
00792 {
00793 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00794 return( 2 );
00795 }
00796
00797 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00798 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00799
00800 test_suite_md2_file( param1, param2 );
00801 return ( 0 );
00802 #endif
00803 #endif
00804
00805 return ( 3 );
00806 }
00807 else
00808 if( strcmp( params[0], "md4_file" ) == 0 )
00809 {
00810 #ifdef POLARSSL_MD4_C
00811 #ifdef POLARSSL_FS_IO
00812
00813 char *param1 = params[1];
00814 char *param2 = params[2];
00815
00816 if( cnt != 3 )
00817 {
00818 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00819 return( 2 );
00820 }
00821
00822 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00823 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00824
00825 test_suite_md4_file( param1, param2 );
00826 return ( 0 );
00827 #endif
00828 #endif
00829
00830 return ( 3 );
00831 }
00832 else
00833 if( strcmp( params[0], "md5_file" ) == 0 )
00834 {
00835 #ifdef POLARSSL_MD5_C
00836 #ifdef POLARSSL_FS_IO
00837
00838 char *param1 = params[1];
00839 char *param2 = params[2];
00840
00841 if( cnt != 3 )
00842 {
00843 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00844 return( 2 );
00845 }
00846
00847 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00848 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00849
00850 test_suite_md5_file( param1, param2 );
00851 return ( 0 );
00852 #endif
00853 #endif
00854
00855 return ( 3 );
00856 }
00857 else
00858 if( strcmp( params[0], "md2_selftest" ) == 0 )
00859 {
00860 #ifdef POLARSSL_MD2_C
00861 #ifdef POLARSSL_SELF_TEST
00862
00863
00864 if( cnt != 1 )
00865 {
00866 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
00867 return( 2 );
00868 }
00869
00870
00871 test_suite_md2_selftest( );
00872 return ( 0 );
00873 #endif
00874 #endif
00875
00876 return ( 3 );
00877 }
00878 else
00879 if( strcmp( params[0], "md4_selftest" ) == 0 )
00880 {
00881 #ifdef POLARSSL_MD4_C
00882 #ifdef POLARSSL_SELF_TEST
00883
00884
00885 if( cnt != 1 )
00886 {
00887 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
00888 return( 2 );
00889 }
00890
00891
00892 test_suite_md4_selftest( );
00893 return ( 0 );
00894 #endif
00895 #endif
00896
00897 return ( 3 );
00898 }
00899 else
00900 if( strcmp( params[0], "md5_selftest" ) == 0 )
00901 {
00902 #ifdef POLARSSL_MD5_C
00903 #ifdef POLARSSL_SELF_TEST
00904
00905
00906 if( cnt != 1 )
00907 {
00908 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
00909 return( 2 );
00910 }
00911
00912
00913 test_suite_md5_selftest( );
00914 return ( 0 );
00915 #endif
00916 #endif
00917
00918 return ( 3 );
00919 }
00920 else
00921
00922 {
00923 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
00924 fflush( stdout );
00925 return( 1 );
00926 }
00927 #else
00928 return( 3 );
00929 #endif
00930 return( ret );
00931 }
00932
00933 int get_line( FILE *f, char *buf, size_t len )
00934 {
00935 char *ret;
00936
00937 ret = fgets( buf, len, f );
00938 if( ret == NULL )
00939 return( -1 );
00940
00941 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
00942 buf[strlen(buf) - 1] = '\0';
00943 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
00944 buf[strlen(buf) - 1] = '\0';
00945
00946 return( 0 );
00947 }
00948
00949 int parse_arguments( char *buf, size_t len, char *params[50] )
00950 {
00951 int cnt = 0, i;
00952 char *cur = buf;
00953 char *p = buf, *q;
00954
00955 params[cnt++] = cur;
00956
00957 while( *p != '\0' && p < buf + len )
00958 {
00959 if( *p == '\\' )
00960 {
00961 *p++;
00962 *p++;
00963 continue;
00964 }
00965 if( *p == ':' )
00966 {
00967 if( p + 1 < buf + len )
00968 {
00969 cur = p + 1;
00970 params[cnt++] = cur;
00971 }
00972 *p = '\0';
00973 }
00974
00975 *p++;
00976 }
00977
00978
00979 for( i = 0; i < cnt; i++ )
00980 {
00981 p = params[i];
00982 q = params[i];
00983
00984 while( *p != '\0' )
00985 {
00986 if( *p == '\\' && *(p + 1) == 'n' )
00987 {
00988 p += 2;
00989 *(q++) = '\n';
00990 }
00991 else if( *p == '\\' && *(p + 1) == ':' )
00992 {
00993 p += 2;
00994 *(q++) = ':';
00995 }
00996 else if( *p == '\\' && *(p + 1) == '?' )
00997 {
00998 p += 2;
00999 *(q++) = '?';
01000 }
01001 else
01002 *(q++) = *(p++);
01003 }
01004 *q = '\0';
01005 }
01006
01007 return( cnt );
01008 }
01009
01010 int main()
01011 {
01012 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
01013 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_mdx.data";
01014 FILE *file;
01015 char buf[5000];
01016 char *params[50];
01017
01018 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01019 unsigned char alloc_buf[1000000];
01020 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
01021 #endif
01022
01023 file = fopen( filename, "r" );
01024 if( file == NULL )
01025 {
01026 fprintf( stderr, "Failed to open\n" );
01027 return( 1 );
01028 }
01029
01030 while( !feof( file ) )
01031 {
01032 int skip = 0;
01033
01034 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01035 break;
01036 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
01037 fprintf( stdout, " " );
01038 for( i = strlen( buf ) + 1; i < 67; i++ )
01039 fprintf( stdout, "." );
01040 fprintf( stdout, " " );
01041 fflush( stdout );
01042
01043 total_tests++;
01044
01045 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01046 break;
01047 cnt = parse_arguments( buf, strlen(buf), params );
01048
01049 if( strcmp( params[0], "depends_on" ) == 0 )
01050 {
01051 for( i = 1; i < cnt; i++ )
01052 if( dep_check( params[i] ) != 0 )
01053 skip = 1;
01054
01055 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01056 break;
01057 cnt = parse_arguments( buf, strlen(buf), params );
01058 }
01059
01060 if( skip == 0 )
01061 {
01062 test_errors = 0;
01063 ret = dispatch_test( cnt, params );
01064 }
01065
01066 if( skip == 1 || ret == 3 )
01067 {
01068 total_skipped++;
01069 fprintf( stdout, "----\n" );
01070 fflush( stdout );
01071 }
01072 else if( ret == 0 && test_errors == 0 )
01073 {
01074 fprintf( stdout, "PASS\n" );
01075 fflush( stdout );
01076 }
01077 else if( ret == 2 )
01078 {
01079 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
01080 fclose(file);
01081 exit( 2 );
01082 }
01083 else
01084 total_errors++;
01085
01086 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01087 break;
01088 if( strlen(buf) != 0 )
01089 {
01090 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
01091 return( 1 );
01092 }
01093 }
01094 fclose(file);
01095
01096 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
01097 if( total_errors == 0 )
01098 fprintf( stdout, "PASSED" );
01099 else
01100 fprintf( stdout, "FAILED" );
01101
01102 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
01103 total_tests - total_errors, total_tests, total_skipped );
01104
01105 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01106 #if defined(POLARSSL_MEMORY_DEBUG)
01107 memory_buffer_alloc_status();
01108 #endif
01109 memory_buffer_alloc_free();
01110 #endif
01111
01112 return( total_errors != 0 );
01113 }
01114
01115