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