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