00001 #include <polarssl/config.h>
00002
00003
00004 #include <polarssl/sha1.h>
00005 #include <polarssl/sha256.h>
00006 #include <polarssl/sha512.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_SHA1_C
00366 void test_suite_sha1( char *hex_src_string, char *hex_hash_string )
00367 {
00368 unsigned char src_str[10000];
00369 unsigned char hash_str[10000];
00370 unsigned char output[41];
00371 int src_len;
00372
00373 memset(src_str, 0x00, 10000);
00374 memset(hash_str, 0x00, 10000);
00375 memset(output, 0x00, 41);
00376
00377 src_len = unhexify( src_str, hex_src_string );
00378
00379 sha1( src_str, src_len, output );
00380 hexify( hash_str, output, 20 );
00381
00382 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00383 }
00384 #endif
00385
00386 #ifdef POLARSSL_SHA256_C
00387 void test_suite_sha224(char *hex_src_string, char *hex_hash_string )
00388 {
00389 unsigned char src_str[10000];
00390 unsigned char hash_str[10000];
00391 unsigned char output[57];
00392 int src_len;
00393
00394 memset(src_str, 0x00, 10000);
00395 memset(hash_str, 0x00, 10000);
00396 memset(output, 0x00, 57);
00397
00398 src_len = unhexify( src_str, hex_src_string );
00399
00400 sha256( src_str, src_len, output, 1 );
00401 hexify( hash_str, output, 28 );
00402
00403 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00404 }
00405 #endif
00406
00407 #ifdef POLARSSL_SHA256_C
00408 void test_suite_sha256(char *hex_src_string, char *hex_hash_string )
00409 {
00410 unsigned char src_str[10000];
00411 unsigned char hash_str[10000];
00412 unsigned char output[65];
00413 int src_len;
00414
00415 memset(src_str, 0x00, 10000);
00416 memset(hash_str, 0x00, 10000);
00417 memset(output, 0x00, 65);
00418
00419 src_len = unhexify( src_str, hex_src_string );
00420
00421 sha256( src_str, src_len, output, 0 );
00422 hexify( hash_str, output, 32 );
00423
00424 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00425 }
00426 #endif
00427
00428 #ifdef POLARSSL_SHA512_C
00429 void test_suite_sha384(char *hex_src_string, char *hex_hash_string )
00430 {
00431 unsigned char src_str[10000];
00432 unsigned char hash_str[10000];
00433 unsigned char output[97];
00434 int src_len;
00435
00436 memset(src_str, 0x00, 10000);
00437 memset(hash_str, 0x00, 10000);
00438 memset(output, 0x00, 97);
00439
00440 src_len = unhexify( src_str, hex_src_string );
00441
00442 sha512( src_str, src_len, output, 1 );
00443 hexify( hash_str, output, 48 );
00444
00445 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00446 }
00447 #endif
00448
00449 #ifdef POLARSSL_SHA512_C
00450 void test_suite_sha512(char *hex_src_string, char *hex_hash_string )
00451 {
00452 unsigned char src_str[10000];
00453 unsigned char hash_str[10000];
00454 unsigned char output[129];
00455 int src_len;
00456
00457 memset(src_str, 0x00, 10000);
00458 memset(hash_str, 0x00, 10000);
00459 memset(output, 0x00, 129);
00460
00461 src_len = unhexify( src_str, hex_src_string );
00462
00463 sha512( src_str, src_len, output, 0);
00464 hexify( hash_str, output, 64 );
00465
00466 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00467 }
00468 #endif
00469
00470 #ifdef POLARSSL_SHA1_C
00471 #ifdef POLARSSL_FS_IO
00472 void test_suite_sha1_file( char *filename, char *hex_hash_string )
00473 {
00474 unsigned char hash_str[41];
00475 unsigned char output[21];
00476
00477 memset(hash_str, 0x00, 41);
00478 memset(output, 0x00, 21);
00479
00480 sha1_file( filename, output);
00481 hexify( hash_str, output, 20 );
00482
00483 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00484 }
00485 #endif
00486 #endif
00487
00488 #ifdef POLARSSL_SHA256_C
00489 #ifdef POLARSSL_FS_IO
00490 void test_suite_sha224_file( char *filename, char *hex_hash_string )
00491 {
00492 unsigned char hash_str[57];
00493 unsigned char output[29];
00494
00495 memset(hash_str, 0x00, 57);
00496 memset(output, 0x00, 29);
00497
00498 sha256_file( filename, output, 1);
00499 hexify( hash_str, output, 28 );
00500
00501 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00502 }
00503 #endif
00504 #endif
00505
00506 #ifdef POLARSSL_SHA256_C
00507 #ifdef POLARSSL_FS_IO
00508 void test_suite_sha256_file( char *filename, char *hex_hash_string )
00509 {
00510 unsigned char hash_str[65];
00511 unsigned char output[33];
00512
00513 memset(hash_str, 0x00, 65);
00514 memset(output, 0x00, 33);
00515
00516 sha256_file( filename, output, 0);
00517 hexify( hash_str, output, 32 );
00518
00519 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00520 }
00521 #endif
00522 #endif
00523
00524 #ifdef POLARSSL_SHA512_C
00525 #ifdef POLARSSL_FS_IO
00526 void test_suite_sha384_file( char *filename, char *hex_hash_string )
00527 {
00528 unsigned char hash_str[97];
00529 unsigned char output[49];
00530
00531 memset(hash_str, 0x00, 97);
00532 memset(output, 0x00, 49);
00533
00534 sha512_file( filename, output, 1);
00535 hexify( hash_str, output, 48 );
00536
00537 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00538 }
00539 #endif
00540 #endif
00541
00542 #ifdef POLARSSL_SHA512_C
00543 #ifdef POLARSSL_FS_IO
00544 void test_suite_sha512_file( char *filename, char *hex_hash_string )
00545 {
00546 unsigned char hash_str[129];
00547 unsigned char output[65];
00548
00549 memset(hash_str, 0x00, 129);
00550 memset(output, 0x00, 65);
00551
00552 sha512_file( filename, output, 0);
00553 hexify( hash_str, output, 64 );
00554
00555 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
00556 }
00557 #endif
00558 #endif
00559
00560 #ifdef POLARSSL_SHA1_C
00561 #ifdef POLARSSL_SELF_TEST
00562 void test_suite_sha1_selftest()
00563 {
00564 TEST_ASSERT( sha1_self_test( 0 ) == 0 );
00565 }
00566 #endif
00567 #endif
00568
00569 #ifdef POLARSSL_SHA256_C
00570 #ifdef POLARSSL_SELF_TEST
00571 void test_suite_sha256_selftest()
00572 {
00573 TEST_ASSERT( sha256_self_test( 0 ) == 0 );
00574 }
00575 #endif
00576 #endif
00577
00578 #ifdef POLARSSL_SHA512_C
00579 #ifdef POLARSSL_SELF_TEST
00580 void test_suite_sha512_selftest()
00581 {
00582 TEST_ASSERT( sha512_self_test( 0 ) == 0 );
00583 }
00584 #endif
00585 #endif
00586
00587
00588
00589
00590 int dep_check( char *str )
00591 {
00592 if( str == NULL )
00593 return( 1 );
00594
00595 if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
00596 {
00597 #if defined(POLARSSL_SELF_TEST)
00598 return( 0 );
00599 #else
00600 return( 1 );
00601 #endif
00602 }
00603 if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
00604 {
00605 #if defined(POLARSSL_SHA256_C)
00606 return( 0 );
00607 #else
00608 return( 1 );
00609 #endif
00610 }
00611 if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
00612 {
00613 #if defined(POLARSSL_SHA1_C)
00614 return( 0 );
00615 #else
00616 return( 1 );
00617 #endif
00618 }
00619 if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
00620 {
00621 #if defined(POLARSSL_SHA512_C)
00622 return( 0 );
00623 #else
00624 return( 1 );
00625 #endif
00626 }
00627
00628
00629 return( 1 );
00630 }
00631
00632 int dispatch_test(int cnt, char *params[50])
00633 {
00634 int ret;
00635 ((void) cnt);
00636 ((void) params);
00637
00638 #if defined(TEST_SUITE_ACTIVE)
00639 if( strcmp( params[0], "sha1" ) == 0 )
00640 {
00641 #ifdef POLARSSL_SHA1_C
00642
00643 char *param1 = params[1];
00644 char *param2 = params[2];
00645
00646 if( cnt != 3 )
00647 {
00648 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00649 return( 2 );
00650 }
00651
00652 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00653 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00654
00655 test_suite_sha1( param1, param2 );
00656 return ( 0 );
00657 #endif
00658
00659 return ( 3 );
00660 }
00661 else
00662 if( strcmp( params[0], "sha224" ) == 0 )
00663 {
00664 #ifdef POLARSSL_SHA256_C
00665
00666 char *param1 = params[1];
00667 char *param2 = params[2];
00668
00669 if( cnt != 3 )
00670 {
00671 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00672 return( 2 );
00673 }
00674
00675 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00676 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00677
00678 test_suite_sha224( param1, param2 );
00679 return ( 0 );
00680 #endif
00681
00682 return ( 3 );
00683 }
00684 else
00685 if( strcmp( params[0], "sha256" ) == 0 )
00686 {
00687 #ifdef POLARSSL_SHA256_C
00688
00689 char *param1 = params[1];
00690 char *param2 = params[2];
00691
00692 if( cnt != 3 )
00693 {
00694 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00695 return( 2 );
00696 }
00697
00698 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00699 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00700
00701 test_suite_sha256( param1, param2 );
00702 return ( 0 );
00703 #endif
00704
00705 return ( 3 );
00706 }
00707 else
00708 if( strcmp( params[0], "sha384" ) == 0 )
00709 {
00710 #ifdef POLARSSL_SHA512_C
00711
00712 char *param1 = params[1];
00713 char *param2 = params[2];
00714
00715 if( cnt != 3 )
00716 {
00717 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00718 return( 2 );
00719 }
00720
00721 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00722 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00723
00724 test_suite_sha384( param1, param2 );
00725 return ( 0 );
00726 #endif
00727
00728 return ( 3 );
00729 }
00730 else
00731 if( strcmp( params[0], "sha512" ) == 0 )
00732 {
00733 #ifdef POLARSSL_SHA512_C
00734
00735 char *param1 = params[1];
00736 char *param2 = params[2];
00737
00738 if( cnt != 3 )
00739 {
00740 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00741 return( 2 );
00742 }
00743
00744 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00745 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00746
00747 test_suite_sha512( param1, param2 );
00748 return ( 0 );
00749 #endif
00750
00751 return ( 3 );
00752 }
00753 else
00754 if( strcmp( params[0], "sha1_file" ) == 0 )
00755 {
00756 #ifdef POLARSSL_SHA1_C
00757 #ifdef POLARSSL_FS_IO
00758
00759 char *param1 = params[1];
00760 char *param2 = params[2];
00761
00762 if( cnt != 3 )
00763 {
00764 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00765 return( 2 );
00766 }
00767
00768 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00769 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00770
00771 test_suite_sha1_file( param1, param2 );
00772 return ( 0 );
00773 #endif
00774 #endif
00775
00776 return ( 3 );
00777 }
00778 else
00779 if( strcmp( params[0], "sha224_file" ) == 0 )
00780 {
00781 #ifdef POLARSSL_SHA256_C
00782 #ifdef POLARSSL_FS_IO
00783
00784 char *param1 = params[1];
00785 char *param2 = params[2];
00786
00787 if( cnt != 3 )
00788 {
00789 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00790 return( 2 );
00791 }
00792
00793 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00794 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00795
00796 test_suite_sha224_file( param1, param2 );
00797 return ( 0 );
00798 #endif
00799 #endif
00800
00801 return ( 3 );
00802 }
00803 else
00804 if( strcmp( params[0], "sha256_file" ) == 0 )
00805 {
00806 #ifdef POLARSSL_SHA256_C
00807 #ifdef POLARSSL_FS_IO
00808
00809 char *param1 = params[1];
00810 char *param2 = params[2];
00811
00812 if( cnt != 3 )
00813 {
00814 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00815 return( 2 );
00816 }
00817
00818 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00819 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00820
00821 test_suite_sha256_file( param1, param2 );
00822 return ( 0 );
00823 #endif
00824 #endif
00825
00826 return ( 3 );
00827 }
00828 else
00829 if( strcmp( params[0], "sha384_file" ) == 0 )
00830 {
00831 #ifdef POLARSSL_SHA512_C
00832 #ifdef POLARSSL_FS_IO
00833
00834 char *param1 = params[1];
00835 char *param2 = params[2];
00836
00837 if( cnt != 3 )
00838 {
00839 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00840 return( 2 );
00841 }
00842
00843 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00844 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00845
00846 test_suite_sha384_file( param1, param2 );
00847 return ( 0 );
00848 #endif
00849 #endif
00850
00851 return ( 3 );
00852 }
00853 else
00854 if( strcmp( params[0], "sha512_file" ) == 0 )
00855 {
00856 #ifdef POLARSSL_SHA512_C
00857 #ifdef POLARSSL_FS_IO
00858
00859 char *param1 = params[1];
00860 char *param2 = params[2];
00861
00862 if( cnt != 3 )
00863 {
00864 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00865 return( 2 );
00866 }
00867
00868 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00869 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00870
00871 test_suite_sha512_file( param1, param2 );
00872 return ( 0 );
00873 #endif
00874 #endif
00875
00876 return ( 3 );
00877 }
00878 else
00879 if( strcmp( params[0], "sha1_selftest" ) == 0 )
00880 {
00881 #ifdef POLARSSL_SHA1_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_sha1_selftest( );
00893 return ( 0 );
00894 #endif
00895 #endif
00896
00897 return ( 3 );
00898 }
00899 else
00900 if( strcmp( params[0], "sha256_selftest" ) == 0 )
00901 {
00902 #ifdef POLARSSL_SHA256_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_sha256_selftest( );
00914 return ( 0 );
00915 #endif
00916 #endif
00917
00918 return ( 3 );
00919 }
00920 else
00921 if( strcmp( params[0], "sha512_selftest" ) == 0 )
00922 {
00923 #ifdef POLARSSL_SHA512_C
00924 #ifdef POLARSSL_SELF_TEST
00925
00926
00927 if( cnt != 1 )
00928 {
00929 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
00930 return( 2 );
00931 }
00932
00933
00934 test_suite_sha512_selftest( );
00935 return ( 0 );
00936 #endif
00937 #endif
00938
00939 return ( 3 );
00940 }
00941 else
00942
00943 {
00944 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
00945 fflush( stdout );
00946 return( 1 );
00947 }
00948 #else
00949 return( 3 );
00950 #endif
00951 return( ret );
00952 }
00953
00954 int get_line( FILE *f, char *buf, size_t len )
00955 {
00956 char *ret;
00957
00958 ret = fgets( buf, len, f );
00959 if( ret == NULL )
00960 return( -1 );
00961
00962 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
00963 buf[strlen(buf) - 1] = '\0';
00964 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
00965 buf[strlen(buf) - 1] = '\0';
00966
00967 return( 0 );
00968 }
00969
00970 int parse_arguments( char *buf, size_t len, char *params[50] )
00971 {
00972 int cnt = 0, i;
00973 char *cur = buf;
00974 char *p = buf, *q;
00975
00976 params[cnt++] = cur;
00977
00978 while( *p != '\0' && p < buf + len )
00979 {
00980 if( *p == '\\' )
00981 {
00982 *p++;
00983 *p++;
00984 continue;
00985 }
00986 if( *p == ':' )
00987 {
00988 if( p + 1 < buf + len )
00989 {
00990 cur = p + 1;
00991 params[cnt++] = cur;
00992 }
00993 *p = '\0';
00994 }
00995
00996 *p++;
00997 }
00998
00999
01000 for( i = 0; i < cnt; i++ )
01001 {
01002 p = params[i];
01003 q = params[i];
01004
01005 while( *p != '\0' )
01006 {
01007 if( *p == '\\' && *(p + 1) == 'n' )
01008 {
01009 p += 2;
01010 *(q++) = '\n';
01011 }
01012 else if( *p == '\\' && *(p + 1) == ':' )
01013 {
01014 p += 2;
01015 *(q++) = ':';
01016 }
01017 else if( *p == '\\' && *(p + 1) == '?' )
01018 {
01019 p += 2;
01020 *(q++) = '?';
01021 }
01022 else
01023 *(q++) = *(p++);
01024 }
01025 *q = '\0';
01026 }
01027
01028 return( cnt );
01029 }
01030
01031 int main()
01032 {
01033 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
01034 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_shax.data";
01035 FILE *file;
01036 char buf[5000];
01037 char *params[50];
01038
01039 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01040 unsigned char alloc_buf[1000000];
01041 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
01042 #endif
01043
01044 file = fopen( filename, "r" );
01045 if( file == NULL )
01046 {
01047 fprintf( stderr, "Failed to open\n" );
01048 return( 1 );
01049 }
01050
01051 while( !feof( file ) )
01052 {
01053 int skip = 0;
01054
01055 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01056 break;
01057 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
01058 fprintf( stdout, " " );
01059 for( i = strlen( buf ) + 1; i < 67; i++ )
01060 fprintf( stdout, "." );
01061 fprintf( stdout, " " );
01062 fflush( stdout );
01063
01064 total_tests++;
01065
01066 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01067 break;
01068 cnt = parse_arguments( buf, strlen(buf), params );
01069
01070 if( strcmp( params[0], "depends_on" ) == 0 )
01071 {
01072 for( i = 1; i < cnt; i++ )
01073 if( dep_check( params[i] ) != 0 )
01074 skip = 1;
01075
01076 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01077 break;
01078 cnt = parse_arguments( buf, strlen(buf), params );
01079 }
01080
01081 if( skip == 0 )
01082 {
01083 test_errors = 0;
01084 ret = dispatch_test( cnt, params );
01085 }
01086
01087 if( skip == 1 || ret == 3 )
01088 {
01089 total_skipped++;
01090 fprintf( stdout, "----\n" );
01091 fflush( stdout );
01092 }
01093 else if( ret == 0 && test_errors == 0 )
01094 {
01095 fprintf( stdout, "PASS\n" );
01096 fflush( stdout );
01097 }
01098 else if( ret == 2 )
01099 {
01100 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
01101 fclose(file);
01102 exit( 2 );
01103 }
01104 else
01105 total_errors++;
01106
01107 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01108 break;
01109 if( strlen(buf) != 0 )
01110 {
01111 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
01112 return( 1 );
01113 }
01114 }
01115 fclose(file);
01116
01117 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
01118 if( total_errors == 0 )
01119 fprintf( stdout, "PASSED" );
01120 else
01121 fprintf( stdout, "FAILED" );
01122
01123 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
01124 total_tests - total_errors, total_tests, total_skipped );
01125
01126 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01127 #if defined(POLARSSL_MEMORY_DEBUG)
01128 memory_buffer_alloc_status();
01129 #endif
01130 memory_buffer_alloc_free();
01131 #endif
01132
01133 return( total_errors != 0 );
01134 }
01135
01136