00001 #include <polarssl/config.h>
00002
00003 #ifdef POLARSSL_CIPHER_C
00004
00005 #include <polarssl/cipher.h>
00006
00007 #if defined(POLARSSL_GCM_C)
00008 #include <polarssl/gcm.h>
00009 #endif
00010 #endif
00011
00012
00013 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00014 #include "polarssl/memory.h"
00015 #endif
00016
00017 #if defined(WANT_NOT_RND_MPI)
00018 #if defined(POLARSSL_BIGNUM_C)
00019 #include "polarssl/bignum.h"
00020 #else
00021 #error "not_rnd_mpi() need bignum.c"
00022 #endif
00023 #endif
00024
00025 #ifdef _MSC_VER
00026 #include <basetsd.h>
00027 typedef UINT32 uint32_t;
00028 #else
00029 #include <inttypes.h>
00030 #endif
00031
00032 #include <assert.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035
00036
00037
00038
00039 #ifndef GET_UINT32_BE
00040 #define GET_UINT32_BE(n,b,i) \
00041 { \
00042 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
00043 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
00044 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
00045 | ( (uint32_t) (b)[(i) + 3] ); \
00046 }
00047 #endif
00048
00049 #ifndef PUT_UINT32_BE
00050 #define PUT_UINT32_BE(n,b,i) \
00051 { \
00052 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
00053 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
00054 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
00055 (b)[(i) + 3] = (unsigned char) ( (n) ); \
00056 }
00057 #endif
00058
00059 static int unhexify(unsigned char *obuf, const char *ibuf)
00060 {
00061 unsigned char c, c2;
00062 int len = strlen(ibuf) / 2;
00063 assert(!(strlen(ibuf) %1));
00064
00065 while (*ibuf != 0)
00066 {
00067 c = *ibuf++;
00068 if( c >= '0' && c <= '9' )
00069 c -= '0';
00070 else if( c >= 'a' && c <= 'f' )
00071 c -= 'a' - 10;
00072 else if( c >= 'A' && c <= 'F' )
00073 c -= 'A' - 10;
00074 else
00075 assert( 0 );
00076
00077 c2 = *ibuf++;
00078 if( c2 >= '0' && c2 <= '9' )
00079 c2 -= '0';
00080 else if( c2 >= 'a' && c2 <= 'f' )
00081 c2 -= 'a' - 10;
00082 else if( c2 >= 'A' && c2 <= 'F' )
00083 c2 -= 'A' - 10;
00084 else
00085 assert( 0 );
00086
00087 *obuf++ = ( c << 4 ) | c2;
00088 }
00089
00090 return len;
00091 }
00092
00093 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
00094 {
00095 unsigned char l, h;
00096
00097 while (len != 0)
00098 {
00099 h = (*ibuf) / 16;
00100 l = (*ibuf) % 16;
00101
00102 if( h < 10 )
00103 *obuf++ = '0' + h;
00104 else
00105 *obuf++ = 'a' + h - 10;
00106
00107 if( l < 10 )
00108 *obuf++ = '0' + l;
00109 else
00110 *obuf++ = 'a' + l - 10;
00111
00112 ++ibuf;
00113 len--;
00114 }
00115 }
00116
00126 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
00127 {
00128 size_t i;
00129
00130 if( rng_state != NULL )
00131 rng_state = NULL;
00132
00133 for( i = 0; i < len; ++i )
00134 output[i] = rand();
00135
00136 return( 0 );
00137 }
00138
00144 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
00145 {
00146 if( rng_state != NULL )
00147 rng_state = NULL;
00148
00149 memset( output, 0, len );
00150
00151 return( 0 );
00152 }
00153
00154 typedef struct
00155 {
00156 unsigned char *buf;
00157 size_t length;
00158 } rnd_buf_info;
00159
00171 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
00172 {
00173 rnd_buf_info *info = (rnd_buf_info *) rng_state;
00174 size_t use_len;
00175
00176 if( rng_state == NULL )
00177 return( rnd_std_rand( NULL, output, len ) );
00178
00179 use_len = len;
00180 if( len > info->length )
00181 use_len = info->length;
00182
00183 if( use_len )
00184 {
00185 memcpy( output, info->buf, use_len );
00186 info->buf += use_len;
00187 info->length -= use_len;
00188 }
00189
00190 if( len - use_len > 0 )
00191 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
00192
00193 return( 0 );
00194 }
00195
00203 typedef struct
00204 {
00205 uint32_t key[16];
00206 uint32_t v0, v1;
00207 } rnd_pseudo_info;
00208
00217 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
00218 {
00219 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
00220 uint32_t i, *k, sum, delta=0x9E3779B9;
00221 unsigned char result[4];
00222
00223 if( rng_state == NULL )
00224 return( rnd_std_rand( NULL, output, len ) );
00225
00226 k = info->key;
00227
00228 while( len > 0 )
00229 {
00230 size_t use_len = ( len > 4 ) ? 4 : len;
00231 sum = 0;
00232
00233 for( i = 0; i < 32; i++ )
00234 {
00235 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
00236 sum += delta;
00237 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
00238 }
00239
00240 PUT_UINT32_BE( info->v0, result, 0 );
00241 memcpy( output, result, use_len );
00242 len -= use_len;
00243 }
00244
00245 return( 0 );
00246 }
00247
00248 #if defined(WANT_NOT_RND_MPI)
00249
00257 #define ciL (sizeof(t_uint))
00258 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
00259 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
00260 {
00261 char *str = (char *) in;
00262 mpi X;
00263
00264
00265
00266
00267
00268 X.s = 1;
00269 X.p = (t_uint *) out;
00270 X.n = CHARS_TO_LIMBS( len );
00271
00272
00273
00274
00275
00276 assert( strlen( str ) / 2 == len );
00277
00278 return( mpi_read_string( &X, 16, str ) );
00279 }
00280 #endif
00281
00282
00283 #include <stdio.h>
00284 #include <string.h>
00285
00286 static int test_errors = 0;
00287
00288 #ifdef POLARSSL_CIPHER_C
00289
00290 #define TEST_SUITE_ACTIVE
00291
00292 static int test_assert( int correct, char *test )
00293 {
00294 if( correct )
00295 return( 0 );
00296
00297 test_errors++;
00298 if( test_errors == 1 )
00299 printf( "FAILED\n" );
00300 printf( " %s\n", test );
00301
00302 return( 1 );
00303 }
00304
00305 #define TEST_ASSERT( TEST ) \
00306 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
00307 if( test_errors) return; \
00308 } while (0)
00309
00310 int verify_string( char **str )
00311 {
00312 if( (*str)[0] != '"' ||
00313 (*str)[strlen( *str ) - 1] != '"' )
00314 {
00315 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
00316 return( -1 );
00317 }
00318
00319 (*str)++;
00320 (*str)[strlen( *str ) - 1] = '\0';
00321
00322 return( 0 );
00323 }
00324
00325 int verify_int( char *str, int *value )
00326 {
00327 size_t i;
00328 int minus = 0;
00329 int digits = 1;
00330 int hex = 0;
00331
00332 for( i = 0; i < strlen( str ); i++ )
00333 {
00334 if( i == 0 && str[i] == '-' )
00335 {
00336 minus = 1;
00337 continue;
00338 }
00339
00340 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
00341 str[i - 1] == '0' && str[i] == 'x' )
00342 {
00343 hex = 1;
00344 continue;
00345 }
00346
00347 if( str[i] < '0' || str[i] > '9' )
00348 {
00349 digits = 0;
00350 break;
00351 }
00352 }
00353
00354 if( digits )
00355 {
00356 if( hex )
00357 *value = strtol( str, NULL, 16 );
00358 else
00359 *value = strtol( str, NULL, 10 );
00360
00361 return( 0 );
00362 }
00363
00364 if( strcmp( str, "POLARSSL_PADDING_ZEROS" ) == 0 )
00365 {
00366 *value = ( POLARSSL_PADDING_ZEROS );
00367 return( 0 );
00368 }
00369 if( strcmp( str, "POLARSSL_CIPHER_AES_256_CFB128" ) == 0 )
00370 {
00371 *value = ( POLARSSL_CIPHER_AES_256_CFB128 );
00372 return( 0 );
00373 }
00374 if( strcmp( str, "POLARSSL_CIPHER_AES_128_ECB" ) == 0 )
00375 {
00376 *value = ( POLARSSL_CIPHER_AES_128_ECB );
00377 return( 0 );
00378 }
00379 if( strcmp( str, "POLARSSL_PADDING_NONE" ) == 0 )
00380 {
00381 *value = ( POLARSSL_PADDING_NONE );
00382 return( 0 );
00383 }
00384 if( strcmp( str, "POLARSSL_CIPHER_AES_192_ECB" ) == 0 )
00385 {
00386 *value = ( POLARSSL_CIPHER_AES_192_ECB );
00387 return( 0 );
00388 }
00389 if( strcmp( str, "POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED" ) == 0 )
00390 {
00391 *value = ( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
00392 return( 0 );
00393 }
00394 if( strcmp( str, "POLARSSL_CIPHER_AES_256_CBC" ) == 0 )
00395 {
00396 *value = ( POLARSSL_CIPHER_AES_256_CBC );
00397 return( 0 );
00398 }
00399 if( strcmp( str, "POLARSSL_CIPHER_AES_192_CFB128" ) == 0 )
00400 {
00401 *value = ( POLARSSL_CIPHER_AES_192_CFB128 );
00402 return( 0 );
00403 }
00404 if( strcmp( str, "POLARSSL_DECRYPT" ) == 0 )
00405 {
00406 *value = ( POLARSSL_DECRYPT );
00407 return( 0 );
00408 }
00409 if( strcmp( str, "POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
00410 {
00411 *value = ( POLARSSL_PADDING_ONE_AND_ZEROS );
00412 return( 0 );
00413 }
00414 if( strcmp( str, "POLARSSL_ERR_CIPHER_INVALID_PADDING" ) == 0 )
00415 {
00416 *value = ( POLARSSL_ERR_CIPHER_INVALID_PADDING );
00417 return( 0 );
00418 }
00419 if( strcmp( str, "POLARSSL_CIPHER_AES_256_ECB" ) == 0 )
00420 {
00421 *value = ( POLARSSL_CIPHER_AES_256_ECB );
00422 return( 0 );
00423 }
00424 if( strcmp( str, "POLARSSL_CIPHER_AES_128_CBC" ) == 0 )
00425 {
00426 *value = ( POLARSSL_CIPHER_AES_128_CBC );
00427 return( 0 );
00428 }
00429 if( strcmp( str, "POLARSSL_CIPHER_AES_128_CFB128" ) == 0 )
00430 {
00431 *value = ( POLARSSL_CIPHER_AES_128_CFB128 );
00432 return( 0 );
00433 }
00434 if( strcmp( str, "POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
00435 {
00436 *value = ( POLARSSL_PADDING_ZEROS_AND_LEN );
00437 return( 0 );
00438 }
00439 if( strcmp( str, "POLARSSL_CIPHER_AES_192_CBC" ) == 0 )
00440 {
00441 *value = ( POLARSSL_CIPHER_AES_192_CBC );
00442 return( 0 );
00443 }
00444 if( strcmp( str, "POLARSSL_ENCRYPT" ) == 0 )
00445 {
00446 *value = ( POLARSSL_ENCRYPT );
00447 return( 0 );
00448 }
00449 if( strcmp( str, "POLARSSL_CIPHER_AES_128_CTR" ) == 0 )
00450 {
00451 *value = ( POLARSSL_CIPHER_AES_128_CTR );
00452 return( 0 );
00453 }
00454 if( strcmp( str, "-1" ) == 0 )
00455 {
00456 *value = ( -1 );
00457 return( 0 );
00458 }
00459 if( strcmp( str, "POLARSSL_PADDING_PKCS7" ) == 0 )
00460 {
00461 *value = ( POLARSSL_PADDING_PKCS7 );
00462 return( 0 );
00463 }
00464
00465
00466 printf( "Expected integer for parameter and got: %s\n", str );
00467 return( -1 );
00468 }
00469
00470 void test_suite_enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
00471 int length_val, int pad_mode )
00472 {
00473 size_t length = length_val, outlen, total_len, i;
00474 unsigned char key[32];
00475 unsigned char iv[16];
00476 unsigned char ad[13];
00477 unsigned char tag[16];
00478 unsigned char inbuf[64];
00479 unsigned char encbuf[64];
00480 unsigned char decbuf[64];
00481
00482 const cipher_info_t *cipher_info;
00483 cipher_context_t ctx_dec;
00484 cipher_context_t ctx_enc;
00485
00486
00487
00488
00489 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
00490 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
00491
00492 memset( key, 0x2a, sizeof( key ) );
00493
00494
00495 cipher_info = cipher_info_from_type( cipher_id );
00496 TEST_ASSERT( NULL != cipher_info );
00497 TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
00498
00499
00500 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
00501 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
00502
00503 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
00504 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
00505
00506 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
00507 if( -1 != pad_mode )
00508 {
00509 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
00510 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
00511 }
00512 #else
00513 (void) pad_mode;
00514 #endif
00515
00516
00517
00518
00519 for( i = 0; i < 3; i++ )
00520 {
00521 memset( iv , 0x00 + i, sizeof( iv ) );
00522 memset( ad, 0x10 + i, sizeof( ad ) );
00523 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
00524
00525 memset( encbuf, 0, sizeof( encbuf ) );
00526 memset( decbuf, 0, sizeof( decbuf ) );
00527 memset( tag, 0, sizeof( tag ) );
00528
00529 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
00530 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
00531
00532 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
00533 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
00534
00535 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00536 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
00537 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
00538 #endif
00539
00540
00541 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
00542 total_len = outlen;
00543
00544 TEST_ASSERT( total_len == length ||
00545 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
00546 total_len < length &&
00547 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
00548
00549 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
00550 total_len += outlen;
00551
00552 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00553 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
00554 #endif
00555
00556 TEST_ASSERT( total_len == length ||
00557 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
00558 total_len > length &&
00559 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
00560
00561
00562 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
00563 total_len = outlen;
00564
00565 TEST_ASSERT( total_len == length ||
00566 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
00567 total_len < length &&
00568 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
00569
00570 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
00571 total_len += outlen;
00572
00573 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00574 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
00575 #endif
00576
00577
00578 TEST_ASSERT( total_len == length );
00579 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
00580 }
00581
00582
00583
00584
00585 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
00586 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
00587 }
00588
00589 void test_suite_enc_fail( int cipher_id, int pad_mode, int key_len,
00590 int length_val, int ret )
00591 {
00592 size_t length = length_val;
00593 unsigned char key[32];
00594 unsigned char iv[16];
00595
00596 const cipher_info_t *cipher_info;
00597 cipher_context_t ctx;
00598
00599 unsigned char inbuf[64];
00600 unsigned char encbuf[64];
00601
00602 size_t outlen = 0;
00603
00604 memset( key, 0, 32 );
00605 memset( iv , 0, 16 );
00606
00607 memset( &ctx, 0, sizeof( ctx ) );
00608
00609 memset( inbuf, 5, 64 );
00610 memset( encbuf, 0, 64 );
00611
00612
00613 cipher_info = cipher_info_from_type( cipher_id );
00614 TEST_ASSERT( NULL != cipher_info );
00615
00616
00617 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
00618 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
00619 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
00620 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
00621 #else
00622 (void) pad_mode;
00623 #endif
00624 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
00625 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
00626 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00627 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
00628 #endif
00629
00630
00631 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
00632 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
00633
00634
00635 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
00636 }
00637
00638 void test_suite_dec_empty_buf()
00639 {
00640 unsigned char key[32];
00641 unsigned char iv[16];
00642
00643 cipher_context_t ctx_dec;
00644 const cipher_info_t *cipher_info;
00645
00646 unsigned char encbuf[64];
00647 unsigned char decbuf[64];
00648
00649 size_t outlen = 0;
00650
00651 memset( key, 0, 32 );
00652 memset( iv , 0, 16 );
00653
00654 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
00655
00656 memset( encbuf, 0, 64 );
00657 memset( decbuf, 0, 64 );
00658
00659
00660 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
00661 TEST_ASSERT( NULL != cipher_info);
00662
00663 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
00664
00665 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
00666
00667 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
00668
00669 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
00670
00671 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00672 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
00673 #endif
00674
00675
00676 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
00677 TEST_ASSERT( 0 == outlen );
00678 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
00679 &ctx_dec, decbuf + outlen, &outlen ) );
00680 TEST_ASSERT( 0 == outlen );
00681
00682 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
00683 }
00684
00685 void test_suite_enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
00686 int second_length_val )
00687 {
00688 size_t first_length = first_length_val;
00689 size_t second_length = second_length_val;
00690 size_t length = first_length + second_length;
00691 unsigned char key[32];
00692 unsigned char iv[16];
00693
00694 cipher_context_t ctx_dec;
00695 cipher_context_t ctx_enc;
00696 const cipher_info_t *cipher_info;
00697
00698 unsigned char inbuf[64];
00699 unsigned char encbuf[64];
00700 unsigned char decbuf[64];
00701
00702 size_t outlen = 0;
00703 size_t totaloutlen = 0;
00704
00705 memset( key, 0, 32 );
00706 memset( iv , 0, 16 );
00707
00708 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
00709 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
00710
00711 memset( inbuf, 5, 64 );
00712 memset( encbuf, 0, 64 );
00713 memset( decbuf, 0, 64 );
00714
00715
00716 cipher_info = cipher_info_from_type( cipher_id );
00717 TEST_ASSERT( NULL != cipher_info);
00718
00719 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
00720 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
00721
00722 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
00723 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
00724
00725 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
00726 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
00727
00728 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
00729 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
00730
00731 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00732 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
00733 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
00734 #endif
00735
00736
00737 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
00738 totaloutlen = outlen;
00739 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
00740 totaloutlen += outlen;
00741 TEST_ASSERT( totaloutlen == length ||
00742 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
00743 totaloutlen < length &&
00744 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
00745
00746 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
00747 totaloutlen += outlen;
00748 TEST_ASSERT( totaloutlen == length ||
00749 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
00750 totaloutlen > length &&
00751 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
00752
00753
00754 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
00755 totaloutlen = outlen;
00756
00757 TEST_ASSERT( totaloutlen == length ||
00758 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
00759 totaloutlen < length &&
00760 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
00761
00762 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
00763 totaloutlen += outlen;
00764
00765 TEST_ASSERT( totaloutlen == length );
00766
00767 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
00768
00769 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
00770 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
00771 }
00772
00773 void test_suite_decrypt_test_vec( int cipher_id, int pad_mode,
00774 char *hex_key, char *hex_iv,
00775 char *hex_cipher, char *hex_clear,
00776 char *hex_ad, char *hex_tag,
00777 int finish_result, int tag_result )
00778 {
00779 unsigned char key[50];
00780 unsigned char iv[50];
00781 unsigned char cipher[200];
00782 unsigned char clear[200];
00783 unsigned char ad[200];
00784 unsigned char tag[20];
00785 size_t key_len, iv_len, cipher_len, clear_len;
00786 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00787 size_t ad_len, tag_len;
00788 #endif
00789 cipher_context_t ctx;
00790 unsigned char output[200];
00791 size_t outlen, total_len;
00792
00793 memset( key, 0x00, sizeof( key ) );
00794 memset( iv, 0x00, sizeof( iv ) );
00795 memset( cipher, 0x00, sizeof( cipher ) );
00796 memset( clear, 0x00, sizeof( clear ) );
00797 memset( ad, 0x00, sizeof( ad ) );
00798 memset( tag, 0x00, sizeof( tag ) );
00799 memset( output, 0x00, sizeof( output ) );
00800
00801 key_len = unhexify( key, hex_key );
00802 iv_len = unhexify( iv, hex_iv );
00803 cipher_len = unhexify( cipher, hex_cipher );
00804 clear_len = unhexify( clear, hex_clear );
00805 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00806 ad_len = unhexify( ad, hex_ad );
00807 tag_len = unhexify( tag, hex_tag );
00808 #else
00809 ((void) hex_ad);
00810 ((void) hex_tag);
00811 #endif
00812
00813
00814 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
00815 cipher_info_from_type( cipher_id ) ) );
00816 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
00817 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
00818 if( pad_mode != -1 )
00819 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
00820 #else
00821 (void) pad_mode;
00822 #endif
00823 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
00824 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
00825 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00826 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
00827 #endif
00828
00829
00830 total_len = 0;
00831 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
00832 total_len += outlen;
00833 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
00834 &outlen ) );
00835 total_len += outlen;
00836 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00837 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
00838 #endif
00839
00840
00841 if( 0 == finish_result && 0 == tag_result )
00842 {
00843 TEST_ASSERT( total_len == clear_len );
00844 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
00845 }
00846
00847 cipher_free_ctx( &ctx );
00848 }
00849
00850 void test_suite_test_vec_ecb( int cipher_id, int operation, char *hex_key,
00851 char *hex_input, char *hex_result,
00852 int finish_result )
00853 {
00854 unsigned char key[50];
00855 unsigned char input[16];
00856 unsigned char result[16];
00857 size_t key_len;
00858 cipher_context_t ctx;
00859 unsigned char output[32];
00860 size_t outlen;
00861
00862 memset( key, 0x00, sizeof( key ) );
00863 memset( input, 0x00, sizeof( input ) );
00864 memset( result, 0x00, sizeof( result ) );
00865 memset( output, 0x00, sizeof( output ) );
00866
00867
00868 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
00869 cipher_info_from_type( cipher_id ) ) );
00870
00871 key_len = unhexify( key, hex_key );
00872 TEST_ASSERT( unhexify( input, hex_input ) ==
00873 (int) cipher_get_block_size( &ctx ) );
00874 TEST_ASSERT( unhexify( result, hex_result ) ==
00875 (int) cipher_get_block_size( &ctx ) );
00876
00877 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
00878
00879 TEST_ASSERT( 0 == cipher_update( &ctx, input,
00880 cipher_get_block_size( &ctx ),
00881 output, &outlen ) );
00882 TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
00883 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
00884 &outlen ) );
00885 TEST_ASSERT( 0 == outlen );
00886
00887
00888 if( 0 == finish_result )
00889 TEST_ASSERT( 0 == memcmp( output, result,
00890 cipher_get_block_size( &ctx ) ) );
00891
00892 cipher_free_ctx( &ctx );
00893 }
00894
00895 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00896 void test_suite_set_padding( int cipher_id, int pad_mode, int ret )
00897 {
00898 const cipher_info_t *cipher_info;
00899 cipher_context_t ctx;
00900
00901 cipher_info = cipher_info_from_type( cipher_id );
00902 TEST_ASSERT( NULL != cipher_info );
00903 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
00904
00905 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
00906
00907 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
00908 }
00909 #endif
00910
00911 #ifdef POLARSSL_CIPHER_MODE_CBC
00912 void test_suite_check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
00913 {
00914 cipher_info_t cipher_info;
00915 cipher_context_t ctx;
00916 unsigned char input[16];
00917 size_t ilen, dlen;
00918
00919
00920 memset( &ctx, 0, sizeof( ctx ) );
00921 cipher_info.mode = POLARSSL_MODE_CBC;
00922 ctx.cipher_info = &cipher_info;
00923
00924 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
00925
00926 ilen = unhexify( input, input_str );
00927
00928 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
00929 if( 0 == ret )
00930 TEST_ASSERT( dlen == (size_t) dlen_check );
00931 }
00932 #endif
00933
00934 #ifdef POLARSSL_SELF_TEST
00935 void test_suite_cipher_selftest()
00936 {
00937 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
00938 }
00939 #endif
00940
00941
00942 #endif
00943
00944
00945 int dep_check( char *str )
00946 {
00947 if( str == NULL )
00948 return( 1 );
00949
00950 if( strcmp( str, "POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN" ) == 0 )
00951 {
00952 #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
00953 return( 0 );
00954 #else
00955 return( 1 );
00956 #endif
00957 }
00958 if( strcmp( str, "POLARSSL_CIPHER_MODE_CFB" ) == 0 )
00959 {
00960 #if defined(POLARSSL_CIPHER_MODE_CFB)
00961 return( 0 );
00962 #else
00963 return( 1 );
00964 #endif
00965 }
00966 if( strcmp( str, "POLARSSL_CIPHER_PADDING_ZEROS" ) == 0 )
00967 {
00968 #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
00969 return( 0 );
00970 #else
00971 return( 1 );
00972 #endif
00973 }
00974 if( strcmp( str, "POLARSSL_CIPHER_MODE_CTR" ) == 0 )
00975 {
00976 #if defined(POLARSSL_CIPHER_MODE_CTR)
00977 return( 0 );
00978 #else
00979 return( 1 );
00980 #endif
00981 }
00982 if( strcmp( str, "POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS" ) == 0 )
00983 {
00984 #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
00985 return( 0 );
00986 #else
00987 return( 1 );
00988 #endif
00989 }
00990 if( strcmp( str, "POLARSSL_AES_C" ) == 0 )
00991 {
00992 #if defined(POLARSSL_AES_C)
00993 return( 0 );
00994 #else
00995 return( 1 );
00996 #endif
00997 }
00998 if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
00999 {
01000 #if defined(POLARSSL_CIPHER_MODE_CBC)
01001 return( 0 );
01002 #else
01003 return( 1 );
01004 #endif
01005 }
01006 if( strcmp( str, "POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
01007 {
01008 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
01009 return( 0 );
01010 #else
01011 return( 1 );
01012 #endif
01013 }
01014
01015
01016 return( 1 );
01017 }
01018
01019 int dispatch_test(int cnt, char *params[50])
01020 {
01021 int ret;
01022 ((void) cnt);
01023 ((void) params);
01024
01025 #if defined(TEST_SUITE_ACTIVE)
01026 if( strcmp( params[0], "enc_dec_buf" ) == 0 )
01027 {
01028
01029 int param1;
01030 char *param2 = params[2];
01031 int param3;
01032 int param4;
01033 int param5;
01034
01035 if( cnt != 6 )
01036 {
01037 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
01038 return( 2 );
01039 }
01040
01041 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01042 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01043 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01044 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01045 if( verify_int( params[5], ¶m5 ) != 0 ) return( 2 );
01046
01047 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
01048 return ( 0 );
01049
01050 return ( 3 );
01051 }
01052 else
01053 if( strcmp( params[0], "enc_fail" ) == 0 )
01054 {
01055
01056 int param1;
01057 int param2;
01058 int param3;
01059 int param4;
01060 int param5;
01061
01062 if( cnt != 6 )
01063 {
01064 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
01065 return( 2 );
01066 }
01067
01068 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01069 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01070 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01071 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01072 if( verify_int( params[5], ¶m5 ) != 0 ) return( 2 );
01073
01074 test_suite_enc_fail( param1, param2, param3, param4, param5 );
01075 return ( 0 );
01076
01077 return ( 3 );
01078 }
01079 else
01080 if( strcmp( params[0], "dec_empty_buf" ) == 0 )
01081 {
01082
01083
01084 if( cnt != 1 )
01085 {
01086 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
01087 return( 2 );
01088 }
01089
01090
01091 test_suite_dec_empty_buf( );
01092 return ( 0 );
01093
01094 return ( 3 );
01095 }
01096 else
01097 if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
01098 {
01099
01100 int param1;
01101 int param2;
01102 int param3;
01103 int param4;
01104
01105 if( cnt != 5 )
01106 {
01107 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
01108 return( 2 );
01109 }
01110
01111 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01112 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01113 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01114 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01115
01116 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
01117 return ( 0 );
01118
01119 return ( 3 );
01120 }
01121 else
01122 if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
01123 {
01124
01125 int param1;
01126 int param2;
01127 char *param3 = params[3];
01128 char *param4 = params[4];
01129 char *param5 = params[5];
01130 char *param6 = params[6];
01131 char *param7 = params[7];
01132 char *param8 = params[8];
01133 int param9;
01134 int param10;
01135
01136 if( cnt != 11 )
01137 {
01138 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
01139 return( 2 );
01140 }
01141
01142 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01143 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01144 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01145 if( verify_string( ¶m4 ) != 0 ) return( 2 );
01146 if( verify_string( ¶m5 ) != 0 ) return( 2 );
01147 if( verify_string( ¶m6 ) != 0 ) return( 2 );
01148 if( verify_string( ¶m7 ) != 0 ) return( 2 );
01149 if( verify_string( ¶m8 ) != 0 ) return( 2 );
01150 if( verify_int( params[9], ¶m9 ) != 0 ) return( 2 );
01151 if( verify_int( params[10], ¶m10 ) != 0 ) return( 2 );
01152
01153 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
01154 return ( 0 );
01155
01156 return ( 3 );
01157 }
01158 else
01159 if( strcmp( params[0], "test_vec_ecb" ) == 0 )
01160 {
01161
01162 int param1;
01163 int param2;
01164 char *param3 = params[3];
01165 char *param4 = params[4];
01166 char *param5 = params[5];
01167 int param6;
01168
01169 if( cnt != 7 )
01170 {
01171 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
01172 return( 2 );
01173 }
01174
01175 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01176 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01177 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01178 if( verify_string( ¶m4 ) != 0 ) return( 2 );
01179 if( verify_string( ¶m5 ) != 0 ) return( 2 );
01180 if( verify_int( params[6], ¶m6 ) != 0 ) return( 2 );
01181
01182 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
01183 return ( 0 );
01184
01185 return ( 3 );
01186 }
01187 else
01188 if( strcmp( params[0], "set_padding" ) == 0 )
01189 {
01190 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
01191
01192 int param1;
01193 int param2;
01194 int param3;
01195
01196 if( cnt != 4 )
01197 {
01198 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
01199 return( 2 );
01200 }
01201
01202 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01203 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01204 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01205
01206 test_suite_set_padding( param1, param2, param3 );
01207 return ( 0 );
01208 #endif
01209
01210 return ( 3 );
01211 }
01212 else
01213 if( strcmp( params[0], "check_padding" ) == 0 )
01214 {
01215 #ifdef POLARSSL_CIPHER_MODE_CBC
01216
01217 int param1;
01218 char *param2 = params[2];
01219 int param3;
01220 int param4;
01221
01222 if( cnt != 5 )
01223 {
01224 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
01225 return( 2 );
01226 }
01227
01228 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01229 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01230 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01231 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01232
01233 test_suite_check_padding( param1, param2, param3, param4 );
01234 return ( 0 );
01235 #endif
01236
01237 return ( 3 );
01238 }
01239 else
01240 if( strcmp( params[0], "cipher_selftest" ) == 0 )
01241 {
01242 #ifdef POLARSSL_SELF_TEST
01243
01244
01245 if( cnt != 1 )
01246 {
01247 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
01248 return( 2 );
01249 }
01250
01251
01252 test_suite_cipher_selftest( );
01253 return ( 0 );
01254 #endif
01255
01256 return ( 3 );
01257 }
01258 else
01259
01260 {
01261 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
01262 fflush( stdout );
01263 return( 1 );
01264 }
01265 #else
01266 return( 3 );
01267 #endif
01268 return( ret );
01269 }
01270
01271 int get_line( FILE *f, char *buf, size_t len )
01272 {
01273 char *ret;
01274
01275 ret = fgets( buf, len, f );
01276 if( ret == NULL )
01277 return( -1 );
01278
01279 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
01280 buf[strlen(buf) - 1] = '\0';
01281 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
01282 buf[strlen(buf) - 1] = '\0';
01283
01284 return( 0 );
01285 }
01286
01287 int parse_arguments( char *buf, size_t len, char *params[50] )
01288 {
01289 int cnt = 0, i;
01290 char *cur = buf;
01291 char *p = buf, *q;
01292
01293 params[cnt++] = cur;
01294
01295 while( *p != '\0' && p < buf + len )
01296 {
01297 if( *p == '\\' )
01298 {
01299 *p++;
01300 *p++;
01301 continue;
01302 }
01303 if( *p == ':' )
01304 {
01305 if( p + 1 < buf + len )
01306 {
01307 cur = p + 1;
01308 params[cnt++] = cur;
01309 }
01310 *p = '\0';
01311 }
01312
01313 *p++;
01314 }
01315
01316
01317 for( i = 0; i < cnt; i++ )
01318 {
01319 p = params[i];
01320 q = params[i];
01321
01322 while( *p != '\0' )
01323 {
01324 if( *p == '\\' && *(p + 1) == 'n' )
01325 {
01326 p += 2;
01327 *(q++) = '\n';
01328 }
01329 else if( *p == '\\' && *(p + 1) == ':' )
01330 {
01331 p += 2;
01332 *(q++) = ':';
01333 }
01334 else if( *p == '\\' && *(p + 1) == '?' )
01335 {
01336 p += 2;
01337 *(q++) = '?';
01338 }
01339 else
01340 *(q++) = *(p++);
01341 }
01342 *q = '\0';
01343 }
01344
01345 return( cnt );
01346 }
01347
01348 int main()
01349 {
01350 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
01351 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_cipher.aes.data";
01352 FILE *file;
01353 char buf[5000];
01354 char *params[50];
01355
01356 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01357 unsigned char alloc_buf[1000000];
01358 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
01359 #endif
01360
01361 file = fopen( filename, "r" );
01362 if( file == NULL )
01363 {
01364 fprintf( stderr, "Failed to open\n" );
01365 return( 1 );
01366 }
01367
01368 while( !feof( file ) )
01369 {
01370 int skip = 0;
01371
01372 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01373 break;
01374 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
01375 fprintf( stdout, " " );
01376 for( i = strlen( buf ) + 1; i < 67; i++ )
01377 fprintf( stdout, "." );
01378 fprintf( stdout, " " );
01379 fflush( stdout );
01380
01381 total_tests++;
01382
01383 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01384 break;
01385 cnt = parse_arguments( buf, strlen(buf), params );
01386
01387 if( strcmp( params[0], "depends_on" ) == 0 )
01388 {
01389 for( i = 1; i < cnt; i++ )
01390 if( dep_check( params[i] ) != 0 )
01391 skip = 1;
01392
01393 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01394 break;
01395 cnt = parse_arguments( buf, strlen(buf), params );
01396 }
01397
01398 if( skip == 0 )
01399 {
01400 test_errors = 0;
01401 ret = dispatch_test( cnt, params );
01402 }
01403
01404 if( skip == 1 || ret == 3 )
01405 {
01406 total_skipped++;
01407 fprintf( stdout, "----\n" );
01408 fflush( stdout );
01409 }
01410 else if( ret == 0 && test_errors == 0 )
01411 {
01412 fprintf( stdout, "PASS\n" );
01413 fflush( stdout );
01414 }
01415 else if( ret == 2 )
01416 {
01417 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
01418 fclose(file);
01419 exit( 2 );
01420 }
01421 else
01422 total_errors++;
01423
01424 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01425 break;
01426 if( strlen(buf) != 0 )
01427 {
01428 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
01429 return( 1 );
01430 }
01431 }
01432 fclose(file);
01433
01434 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
01435 if( total_errors == 0 )
01436 fprintf( stdout, "PASSED" );
01437 else
01438 fprintf( stdout, "FAILED" );
01439
01440 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
01441 total_tests - total_errors, total_tests, total_skipped );
01442
01443 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01444 #if defined(POLARSSL_MEMORY_DEBUG)
01445 memory_buffer_alloc_status();
01446 #endif
01447 memory_buffer_alloc_free();
01448 #endif
01449
01450 return( total_errors != 0 );
01451 }
01452
01453