00001 #include <polarssl/config.h>
00002
00003 #ifdef POLARSSL_BLOWFISH_C
00004
00005 #include "polarssl/blowfish.h"
00006 #endif
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 #ifdef POLARSSL_BLOWFISH_C
00285
00286 #define TEST_SUITE_ACTIVE
00287
00288 static int test_assert( int correct, char *test )
00289 {
00290 if( correct )
00291 return( 0 );
00292
00293 test_errors++;
00294 if( test_errors == 1 )
00295 printf( "FAILED\n" );
00296 printf( " %s\n", test );
00297
00298 return( 1 );
00299 }
00300
00301 #define TEST_ASSERT( TEST ) \
00302 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
00303 if( test_errors) return; \
00304 } while (0)
00305
00306 int verify_string( char **str )
00307 {
00308 if( (*str)[0] != '"' ||
00309 (*str)[strlen( *str ) - 1] != '"' )
00310 {
00311 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
00312 return( -1 );
00313 }
00314
00315 (*str)++;
00316 (*str)[strlen( *str ) - 1] = '\0';
00317
00318 return( 0 );
00319 }
00320
00321 int verify_int( char *str, int *value )
00322 {
00323 size_t i;
00324 int minus = 0;
00325 int digits = 1;
00326 int hex = 0;
00327
00328 for( i = 0; i < strlen( str ); i++ )
00329 {
00330 if( i == 0 && str[i] == '-' )
00331 {
00332 minus = 1;
00333 continue;
00334 }
00335
00336 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
00337 str[i - 1] == '0' && str[i] == 'x' )
00338 {
00339 hex = 1;
00340 continue;
00341 }
00342
00343 if( str[i] < '0' || str[i] > '9' )
00344 {
00345 digits = 0;
00346 break;
00347 }
00348 }
00349
00350 if( digits )
00351 {
00352 if( hex )
00353 *value = strtol( str, NULL, 16 );
00354 else
00355 *value = strtol( str, NULL, 10 );
00356
00357 return( 0 );
00358 }
00359
00360 #ifdef POLARSSL_CIPHER_MODE_CBC
00361 if( strcmp( str, "POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH" ) == 0 )
00362 {
00363 *value = ( POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH );
00364 return( 0 );
00365 }
00366 #endif // POLARSSL_CIPHER_MODE_CBC
00367 if( strcmp( str, "POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH" ) == 0 )
00368 {
00369 *value = ( POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH );
00370 return( 0 );
00371 }
00372
00373
00374 printf( "Expected integer for parameter and got: %s\n", str );
00375 return( -1 );
00376 }
00377
00378 void test_suite_blowfish_encrypt_ecb( char *hex_key_string, char *hex_src_string,
00379 char *hex_dst_string, int setkey_result )
00380 {
00381 unsigned char key_str[100];
00382 unsigned char src_str[100];
00383 unsigned char dst_str[100];
00384 unsigned char output[100];
00385 blowfish_context ctx;
00386 int key_len;
00387
00388 memset(key_str, 0x00, 100);
00389 memset(src_str, 0x00, 100);
00390 memset(dst_str, 0x00, 100);
00391 memset(output, 0x00, 100);
00392
00393 key_len = unhexify( key_str, hex_key_string );
00394 unhexify( src_str, hex_src_string );
00395
00396 TEST_ASSERT( blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
00397 if( setkey_result == 0 )
00398 {
00399 TEST_ASSERT( blowfish_crypt_ecb( &ctx, BLOWFISH_ENCRYPT, src_str, output ) == 0 );
00400 hexify( dst_str, output, 8 );
00401
00402 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
00403 }
00404 }
00405
00406 void test_suite_blowfish_decrypt_ecb( char *hex_key_string, char *hex_src_string,
00407 char *hex_dst_string, int setkey_result )
00408 {
00409 unsigned char key_str[100];
00410 unsigned char src_str[100];
00411 unsigned char dst_str[100];
00412 unsigned char output[100];
00413 blowfish_context ctx;
00414 int key_len;
00415
00416 memset(key_str, 0x00, 100);
00417 memset(src_str, 0x00, 100);
00418 memset(dst_str, 0x00, 100);
00419 memset(output, 0x00, 100);
00420
00421 key_len = unhexify( key_str, hex_key_string );
00422 unhexify( src_str, hex_src_string );
00423
00424 TEST_ASSERT( blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
00425 if( setkey_result == 0 )
00426 {
00427 TEST_ASSERT( blowfish_crypt_ecb( &ctx, BLOWFISH_DECRYPT, src_str, output ) == 0 );
00428 hexify( dst_str, output, 8 );
00429
00430 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
00431 }
00432 }
00433
00434 #ifdef POLARSSL_CIPHER_MODE_CBC
00435 void test_suite_blowfish_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
00436 char *hex_src_string, char *hex_dst_string,
00437 int cbc_result )
00438 {
00439 unsigned char key_str[100];
00440 unsigned char iv_str[100];
00441 unsigned char src_str[100];
00442 unsigned char dst_str[100];
00443 unsigned char output[100];
00444 blowfish_context ctx;
00445 int key_len, data_len;
00446
00447 memset(key_str, 0x00, 100);
00448 memset(iv_str, 0x00, 100);
00449 memset(src_str, 0x00, 100);
00450 memset(dst_str, 0x00, 100);
00451 memset(output, 0x00, 100);
00452
00453 key_len = unhexify( key_str, hex_key_string );
00454 unhexify( iv_str, hex_iv_string );
00455 data_len = unhexify( src_str, hex_src_string );
00456
00457 blowfish_setkey( &ctx, key_str, key_len * 8 );
00458
00459 TEST_ASSERT( blowfish_crypt_cbc( &ctx, BLOWFISH_ENCRYPT, data_len , iv_str, src_str, output ) == cbc_result );
00460 if( cbc_result == 0 )
00461 {
00462 hexify( dst_str, output, data_len );
00463
00464 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
00465 }
00466 }
00467 #endif
00468
00469 #ifdef POLARSSL_CIPHER_MODE_CBC
00470 void test_suite_blowfish_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
00471 char *hex_src_string, char *hex_dst_string,
00472 int cbc_result )
00473 {
00474 unsigned char key_str[100];
00475 unsigned char iv_str[100];
00476 unsigned char src_str[100];
00477 unsigned char dst_str[100];
00478 unsigned char output[100];
00479 blowfish_context ctx;
00480 int key_len, data_len;
00481
00482 memset(key_str, 0x00, 100);
00483 memset(iv_str, 0x00, 100);
00484 memset(src_str, 0x00, 100);
00485 memset(dst_str, 0x00, 100);
00486 memset(output, 0x00, 100);
00487
00488 key_len = unhexify( key_str, hex_key_string );
00489 unhexify( iv_str, hex_iv_string );
00490 data_len = unhexify( src_str, hex_src_string );
00491
00492 blowfish_setkey( &ctx, key_str, key_len * 8 );
00493 TEST_ASSERT( blowfish_crypt_cbc( &ctx, BLOWFISH_DECRYPT, data_len , iv_str, src_str, output ) == cbc_result );
00494 if( cbc_result == 0)
00495 {
00496 hexify( dst_str, output, data_len );
00497
00498 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
00499 }
00500 }
00501 #endif
00502
00503 void test_suite_blowfish_encrypt_cfb64( char *hex_key_string, char *hex_iv_string,
00504 char *hex_src_string, char *hex_dst_string )
00505 {
00506 unsigned char key_str[100];
00507 unsigned char iv_str[100];
00508 unsigned char src_str[100];
00509 unsigned char dst_str[100];
00510 unsigned char output[100];
00511 blowfish_context ctx;
00512 size_t iv_offset = 0;
00513 int key_len, src_len;
00514
00515 memset(key_str, 0x00, 100);
00516 memset(iv_str, 0x00, 100);
00517 memset(src_str, 0x00, 100);
00518 memset(dst_str, 0x00, 100);
00519 memset(output, 0x00, 100);
00520
00521 key_len = unhexify( key_str, hex_key_string );
00522 unhexify( iv_str, hex_iv_string );
00523 src_len = unhexify( src_str, hex_src_string );
00524
00525 blowfish_setkey( &ctx, key_str, key_len * 8 );
00526 TEST_ASSERT( blowfish_crypt_cfb64( &ctx, BLOWFISH_ENCRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
00527 hexify( dst_str, output, src_len );
00528
00529 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
00530 }
00531
00532 void test_suite_blowfish_decrypt_cfb64( char *hex_key_string, char *hex_iv_string,
00533 char *hex_src_string, char *hex_dst_string )
00534 {
00535 unsigned char key_str[100];
00536 unsigned char iv_str[100];
00537 unsigned char src_str[100];
00538 unsigned char dst_str[100];
00539 unsigned char output[100];
00540 blowfish_context ctx;
00541 size_t iv_offset = 0;
00542 int key_len, src_len;
00543
00544 memset(key_str, 0x00, 100);
00545 memset(iv_str, 0x00, 100);
00546 memset(src_str, 0x00, 100);
00547 memset(dst_str, 0x00, 100);
00548 memset(output, 0x00, 100);
00549
00550 key_len = unhexify( key_str, hex_key_string );
00551 unhexify( iv_str, hex_iv_string );
00552 src_len = unhexify( src_str, hex_src_string );
00553
00554 blowfish_setkey( &ctx, key_str, key_len * 8 );
00555 TEST_ASSERT( blowfish_crypt_cfb64( &ctx, BLOWFISH_DECRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
00556 hexify( dst_str, output, src_len );
00557
00558 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
00559 }
00560
00561 void test_suite_blowfish_encrypt_ctr( char *hex_key_string, char *hex_iv_string,
00562 char *hex_src_string, char *hex_dst_string )
00563 {
00564 unsigned char key_str[100];
00565 unsigned char iv_str[100];
00566 unsigned char stream_str[100];
00567 unsigned char src_str[100];
00568 unsigned char dst_str[100];
00569 unsigned char output[100];
00570 blowfish_context ctx;
00571 size_t iv_offset = 0;
00572 int key_len, src_len;
00573
00574 memset(key_str, 0x00, 100);
00575 memset(iv_str, 0x00, 100);
00576 memset(stream_str, 0x00, 100);
00577 memset(src_str, 0x00, 100);
00578 memset(dst_str, 0x00, 100);
00579 memset(output, 0x00, 100);
00580
00581 key_len = unhexify( key_str, hex_key_string );
00582 unhexify( iv_str, hex_iv_string );
00583 src_len = unhexify( src_str, hex_src_string );
00584
00585 blowfish_setkey( &ctx, key_str, key_len * 8 );
00586 TEST_ASSERT( blowfish_crypt_ctr( &ctx, src_len, &iv_offset, iv_str, stream_str, src_str, output ) == 0 );
00587 hexify( dst_str, output, src_len );
00588
00589 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
00590 }
00591
00592
00593 #endif
00594
00595
00596 int dep_check( char *str )
00597 {
00598 if( str == NULL )
00599 return( 1 );
00600
00601
00602
00603 return( 1 );
00604 }
00605
00606 int dispatch_test(int cnt, char *params[50])
00607 {
00608 int ret;
00609 ((void) cnt);
00610 ((void) params);
00611
00612 #if defined(TEST_SUITE_ACTIVE)
00613 if( strcmp( params[0], "blowfish_encrypt_ecb" ) == 0 )
00614 {
00615
00616 char *param1 = params[1];
00617 char *param2 = params[2];
00618 char *param3 = params[3];
00619 int param4;
00620
00621 if( cnt != 5 )
00622 {
00623 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
00624 return( 2 );
00625 }
00626
00627 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00628 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00629 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00630 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
00631
00632 test_suite_blowfish_encrypt_ecb( param1, param2, param3, param4 );
00633 return ( 0 );
00634
00635 return ( 3 );
00636 }
00637 else
00638 if( strcmp( params[0], "blowfish_decrypt_ecb" ) == 0 )
00639 {
00640
00641 char *param1 = params[1];
00642 char *param2 = params[2];
00643 char *param3 = params[3];
00644 int param4;
00645
00646 if( cnt != 5 )
00647 {
00648 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
00649 return( 2 );
00650 }
00651
00652 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00653 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00654 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00655 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
00656
00657 test_suite_blowfish_decrypt_ecb( param1, param2, param3, param4 );
00658 return ( 0 );
00659
00660 return ( 3 );
00661 }
00662 else
00663 if( strcmp( params[0], "blowfish_encrypt_cbc" ) == 0 )
00664 {
00665 #ifdef POLARSSL_CIPHER_MODE_CBC
00666
00667 char *param1 = params[1];
00668 char *param2 = params[2];
00669 char *param3 = params[3];
00670 char *param4 = params[4];
00671 int param5;
00672
00673 if( cnt != 6 )
00674 {
00675 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
00676 return( 2 );
00677 }
00678
00679 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00680 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00681 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00682 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00683 if( verify_int( params[5], ¶m5 ) != 0 ) return( 2 );
00684
00685 test_suite_blowfish_encrypt_cbc( param1, param2, param3, param4, param5 );
00686 return ( 0 );
00687 #endif
00688
00689 return ( 3 );
00690 }
00691 else
00692 if( strcmp( params[0], "blowfish_decrypt_cbc" ) == 0 )
00693 {
00694 #ifdef POLARSSL_CIPHER_MODE_CBC
00695
00696 char *param1 = params[1];
00697 char *param2 = params[2];
00698 char *param3 = params[3];
00699 char *param4 = params[4];
00700 int param5;
00701
00702 if( cnt != 6 )
00703 {
00704 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
00705 return( 2 );
00706 }
00707
00708 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00709 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00710 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00711 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00712 if( verify_int( params[5], ¶m5 ) != 0 ) return( 2 );
00713
00714 test_suite_blowfish_decrypt_cbc( param1, param2, param3, param4, param5 );
00715 return ( 0 );
00716 #endif
00717
00718 return ( 3 );
00719 }
00720 else
00721 if( strcmp( params[0], "blowfish_encrypt_cfb64" ) == 0 )
00722 {
00723
00724 char *param1 = params[1];
00725 char *param2 = params[2];
00726 char *param3 = params[3];
00727 char *param4 = params[4];
00728
00729 if( cnt != 5 )
00730 {
00731 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
00732 return( 2 );
00733 }
00734
00735 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00736 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00737 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00738 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00739
00740 test_suite_blowfish_encrypt_cfb64( param1, param2, param3, param4 );
00741 return ( 0 );
00742
00743 return ( 3 );
00744 }
00745 else
00746 if( strcmp( params[0], "blowfish_decrypt_cfb64" ) == 0 )
00747 {
00748
00749 char *param1 = params[1];
00750 char *param2 = params[2];
00751 char *param3 = params[3];
00752 char *param4 = params[4];
00753
00754 if( cnt != 5 )
00755 {
00756 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
00757 return( 2 );
00758 }
00759
00760 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00761 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00762 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00763 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00764
00765 test_suite_blowfish_decrypt_cfb64( param1, param2, param3, param4 );
00766 return ( 0 );
00767
00768 return ( 3 );
00769 }
00770 else
00771 if( strcmp( params[0], "blowfish_encrypt_ctr" ) == 0 )
00772 {
00773
00774 char *param1 = params[1];
00775 char *param2 = params[2];
00776 char *param3 = params[3];
00777 char *param4 = params[4];
00778
00779 if( cnt != 5 )
00780 {
00781 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
00782 return( 2 );
00783 }
00784
00785 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00786 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00787 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00788 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00789
00790 test_suite_blowfish_encrypt_ctr( param1, param2, param3, param4 );
00791 return ( 0 );
00792
00793 return ( 3 );
00794 }
00795 else
00796
00797 {
00798 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
00799 fflush( stdout );
00800 return( 1 );
00801 }
00802 #else
00803 return( 3 );
00804 #endif
00805 return( ret );
00806 }
00807
00808 int get_line( FILE *f, char *buf, size_t len )
00809 {
00810 char *ret;
00811
00812 ret = fgets( buf, len, f );
00813 if( ret == NULL )
00814 return( -1 );
00815
00816 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
00817 buf[strlen(buf) - 1] = '\0';
00818 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
00819 buf[strlen(buf) - 1] = '\0';
00820
00821 return( 0 );
00822 }
00823
00824 int parse_arguments( char *buf, size_t len, char *params[50] )
00825 {
00826 int cnt = 0, i;
00827 char *cur = buf;
00828 char *p = buf, *q;
00829
00830 params[cnt++] = cur;
00831
00832 while( *p != '\0' && p < buf + len )
00833 {
00834 if( *p == '\\' )
00835 {
00836 *p++;
00837 *p++;
00838 continue;
00839 }
00840 if( *p == ':' )
00841 {
00842 if( p + 1 < buf + len )
00843 {
00844 cur = p + 1;
00845 params[cnt++] = cur;
00846 }
00847 *p = '\0';
00848 }
00849
00850 *p++;
00851 }
00852
00853
00854 for( i = 0; i < cnt; i++ )
00855 {
00856 p = params[i];
00857 q = params[i];
00858
00859 while( *p != '\0' )
00860 {
00861 if( *p == '\\' && *(p + 1) == 'n' )
00862 {
00863 p += 2;
00864 *(q++) = '\n';
00865 }
00866 else if( *p == '\\' && *(p + 1) == ':' )
00867 {
00868 p += 2;
00869 *(q++) = ':';
00870 }
00871 else if( *p == '\\' && *(p + 1) == '?' )
00872 {
00873 p += 2;
00874 *(q++) = '?';
00875 }
00876 else
00877 *(q++) = *(p++);
00878 }
00879 *q = '\0';
00880 }
00881
00882 return( cnt );
00883 }
00884
00885 int main()
00886 {
00887 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
00888 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_blowfish.data";
00889 FILE *file;
00890 char buf[5000];
00891 char *params[50];
00892
00893 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00894 unsigned char alloc_buf[1000000];
00895 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
00896 #endif
00897
00898 file = fopen( filename, "r" );
00899 if( file == NULL )
00900 {
00901 fprintf( stderr, "Failed to open\n" );
00902 return( 1 );
00903 }
00904
00905 while( !feof( file ) )
00906 {
00907 int skip = 0;
00908
00909 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00910 break;
00911 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
00912 fprintf( stdout, " " );
00913 for( i = strlen( buf ) + 1; i < 67; i++ )
00914 fprintf( stdout, "." );
00915 fprintf( stdout, " " );
00916 fflush( stdout );
00917
00918 total_tests++;
00919
00920 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00921 break;
00922 cnt = parse_arguments( buf, strlen(buf), params );
00923
00924 if( strcmp( params[0], "depends_on" ) == 0 )
00925 {
00926 for( i = 1; i < cnt; i++ )
00927 if( dep_check( params[i] ) != 0 )
00928 skip = 1;
00929
00930 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00931 break;
00932 cnt = parse_arguments( buf, strlen(buf), params );
00933 }
00934
00935 if( skip == 0 )
00936 {
00937 test_errors = 0;
00938 ret = dispatch_test( cnt, params );
00939 }
00940
00941 if( skip == 1 || ret == 3 )
00942 {
00943 total_skipped++;
00944 fprintf( stdout, "----\n" );
00945 fflush( stdout );
00946 }
00947 else if( ret == 0 && test_errors == 0 )
00948 {
00949 fprintf( stdout, "PASS\n" );
00950 fflush( stdout );
00951 }
00952 else if( ret == 2 )
00953 {
00954 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
00955 fclose(file);
00956 exit( 2 );
00957 }
00958 else
00959 total_errors++;
00960
00961 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00962 break;
00963 if( strlen(buf) != 0 )
00964 {
00965 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
00966 return( 1 );
00967 }
00968 }
00969 fclose(file);
00970
00971 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
00972 if( total_errors == 0 )
00973 fprintf( stdout, "PASSED" );
00974 else
00975 fprintf( stdout, "FAILED" );
00976
00977 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
00978 total_tests - total_errors, total_tests, total_skipped );
00979
00980 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00981 #if defined(POLARSSL_MEMORY_DEBUG)
00982 memory_buffer_alloc_status();
00983 #endif
00984 memory_buffer_alloc_free();
00985 #endif
00986
00987 return( total_errors != 0 );
00988 }
00989
00990