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 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00365 if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_CTR" ) == 0 )
00366 {
00367 *value = ( POLARSSL_CIPHER_CAMELLIA_128_CTR );
00368 return( 0 );
00369 }
00370 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00371 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00372 if( strcmp( str, "POLARSSL_CIPHER_NULL" ) == 0 )
00373 {
00374 *value = ( POLARSSL_CIPHER_NULL );
00375 return( 0 );
00376 }
00377 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00378 #ifdef POLARSSL_CIPHER_MODE_CBC
00379 if( strcmp( str, "POLARSSL_PADDING_ZEROS" ) == 0 )
00380 {
00381 *value = ( POLARSSL_PADDING_ZEROS );
00382 return( 0 );
00383 }
00384 #endif // POLARSSL_CIPHER_MODE_CBC
00385 #ifdef POLARSSL_CIPHER_MODE_CBC
00386 if( strcmp( str, "POLARSSL_PADDING_NONE" ) == 0 )
00387 {
00388 *value = ( POLARSSL_PADDING_NONE );
00389 return( 0 );
00390 }
00391 #endif // POLARSSL_CIPHER_MODE_CBC
00392 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00393 if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_CBC" ) == 0 )
00394 {
00395 *value = ( POLARSSL_CIPHER_CAMELLIA_128_CBC );
00396 return( 0 );
00397 }
00398 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00399 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00400 if( strcmp( str, "POLARSSL_CIPHER_BLOWFISH_CFB64" ) == 0 )
00401 {
00402 *value = ( POLARSSL_CIPHER_BLOWFISH_CFB64 );
00403 return( 0 );
00404 }
00405 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00406 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00407 if( strcmp( str, "POLARSSL_CIPHER_BLOWFISH_CTR" ) == 0 )
00408 {
00409 *value = ( POLARSSL_CIPHER_BLOWFISH_CTR );
00410 return( 0 );
00411 }
00412 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00413 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00414 if( strcmp( str, "POLARSSL_CIPHER_DES_CBC" ) == 0 )
00415 {
00416 *value = ( POLARSSL_CIPHER_DES_CBC );
00417 return( 0 );
00418 }
00419 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00420 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00421 if( strcmp( str, "POLARSSL_CIPHER_BLOWFISH_CBC" ) == 0 )
00422 {
00423 *value = ( POLARSSL_CIPHER_BLOWFISH_CBC );
00424 return( 0 );
00425 }
00426 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00427 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00428 if( strcmp( str, "POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE" ) == 0 )
00429 {
00430 *value = ( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
00431 return( 0 );
00432 }
00433 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00434 #ifdef POLARSSL_CIPHER_MODE_CBC
00435 if( strcmp( str, "POLARSSL_ERR_CIPHER_INVALID_PADDING" ) == 0 )
00436 {
00437 *value = ( POLARSSL_ERR_CIPHER_INVALID_PADDING );
00438 return( 0 );
00439 }
00440 #endif // POLARSSL_CIPHER_MODE_CBC
00441 #ifdef POLARSSL_CIPHER_MODE_CBC
00442 if( strcmp( str, "POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
00443 {
00444 *value = ( POLARSSL_PADDING_ONE_AND_ZEROS );
00445 return( 0 );
00446 }
00447 #endif // POLARSSL_CIPHER_MODE_CBC
00448 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00449 if( strcmp( str, "POLARSSL_ERR_CIPHER_BAD_INPUT_DATA" ) == 0 )
00450 {
00451 *value = ( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
00452 return( 0 );
00453 }
00454 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00455 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00456 if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_CFB128" ) == 0 )
00457 {
00458 *value = ( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
00459 return( 0 );
00460 }
00461 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00462 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00463 if( strcmp( str, "POLARSSL_CIPHER_AES_128_CBC" ) == 0 )
00464 {
00465 *value = ( POLARSSL_CIPHER_AES_128_CBC );
00466 return( 0 );
00467 }
00468 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00469 #ifdef POLARSSL_CIPHER_MODE_CBC
00470 if( strcmp( str, "POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
00471 {
00472 *value = ( POLARSSL_PADDING_ZEROS_AND_LEN );
00473 return( 0 );
00474 }
00475 #endif // POLARSSL_CIPHER_MODE_CBC
00476 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00477 if( strcmp( str, "POLARSSL_CIPHER_AES_128_CFB128" ) == 0 )
00478 {
00479 *value = ( POLARSSL_CIPHER_AES_128_CFB128 );
00480 return( 0 );
00481 }
00482 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00483 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00484 if( strcmp( str, "POLARSSL_CIPHER_AES_128_CTR" ) == 0 )
00485 {
00486 *value = ( POLARSSL_CIPHER_AES_128_CTR );
00487 return( 0 );
00488 }
00489 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00490 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00491 if( strcmp( str, "POLARSSL_PADDING_PKCS7" ) == 0 )
00492 {
00493 *value = ( POLARSSL_PADDING_PKCS7 );
00494 return( 0 );
00495 }
00496 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00497 #ifdef POLARSSL_CIPHER_MODE_CBC
00498 if( strcmp( str, "POLARSSL_PADDING_PKCS7" ) == 0 )
00499 {
00500 *value = ( POLARSSL_PADDING_PKCS7 );
00501 return( 0 );
00502 }
00503 #endif // POLARSSL_CIPHER_MODE_CBC
00504 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00505 if( strcmp( str, "-1" ) == 0 )
00506 {
00507 *value = ( -1 );
00508 return( 0 );
00509 }
00510 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
00511
00512
00513 printf( "Expected integer for parameter and got: %s\n", str );
00514 return( -1 );
00515 }
00516
00517 void test_suite_enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
00518 int length_val, int pad_mode )
00519 {
00520 size_t length = length_val, outlen, total_len, i;
00521 unsigned char key[32];
00522 unsigned char iv[16];
00523 unsigned char ad[13];
00524 unsigned char tag[16];
00525 unsigned char inbuf[64];
00526 unsigned char encbuf[64];
00527 unsigned char decbuf[64];
00528
00529 const cipher_info_t *cipher_info;
00530 cipher_context_t ctx_dec;
00531 cipher_context_t ctx_enc;
00532
00533
00534
00535
00536 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
00537 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
00538
00539 memset( key, 0x2a, sizeof( key ) );
00540
00541
00542 cipher_info = cipher_info_from_type( cipher_id );
00543 TEST_ASSERT( NULL != cipher_info );
00544 TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
00545
00546
00547 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
00548 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
00549
00550 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
00551 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
00552
00553 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
00554 if( -1 != pad_mode )
00555 {
00556 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
00557 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
00558 }
00559 #else
00560 (void) pad_mode;
00561 #endif
00562
00563
00564
00565
00566 for( i = 0; i < 3; i++ )
00567 {
00568 memset( iv , 0x00 + i, sizeof( iv ) );
00569 memset( ad, 0x10 + i, sizeof( ad ) );
00570 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
00571
00572 memset( encbuf, 0, sizeof( encbuf ) );
00573 memset( decbuf, 0, sizeof( decbuf ) );
00574 memset( tag, 0, sizeof( tag ) );
00575
00576 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
00577 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
00578
00579 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
00580 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
00581
00582 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00583 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
00584 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
00585 #endif
00586
00587
00588 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
00589 total_len = outlen;
00590
00591 TEST_ASSERT( total_len == length ||
00592 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
00593 total_len < length &&
00594 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
00595
00596 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
00597 total_len += outlen;
00598
00599 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00600 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
00601 #endif
00602
00603 TEST_ASSERT( total_len == length ||
00604 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
00605 total_len > length &&
00606 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
00607
00608
00609 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
00610 total_len = outlen;
00611
00612 TEST_ASSERT( total_len == length ||
00613 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
00614 total_len < length &&
00615 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
00616
00617 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
00618 total_len += outlen;
00619
00620 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00621 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
00622 #endif
00623
00624
00625 TEST_ASSERT( total_len == length );
00626 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
00627 }
00628
00629
00630
00631
00632 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
00633 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
00634 }
00635
00636 void test_suite_enc_fail( int cipher_id, int pad_mode, int key_len,
00637 int length_val, int ret )
00638 {
00639 size_t length = length_val;
00640 unsigned char key[32];
00641 unsigned char iv[16];
00642
00643 const cipher_info_t *cipher_info;
00644 cipher_context_t ctx;
00645
00646 unsigned char inbuf[64];
00647 unsigned char encbuf[64];
00648
00649 size_t outlen = 0;
00650
00651 memset( key, 0, 32 );
00652 memset( iv , 0, 16 );
00653
00654 memset( &ctx, 0, sizeof( ctx ) );
00655
00656 memset( inbuf, 5, 64 );
00657 memset( encbuf, 0, 64 );
00658
00659
00660 cipher_info = cipher_info_from_type( cipher_id );
00661 TEST_ASSERT( NULL != cipher_info );
00662
00663
00664 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
00665 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
00666 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
00667 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
00668 #else
00669 (void) pad_mode;
00670 #endif
00671 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
00672 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
00673 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00674 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
00675 #endif
00676
00677
00678 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
00679 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
00680
00681
00682 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
00683 }
00684
00685 void test_suite_dec_empty_buf()
00686 {
00687 unsigned char key[32];
00688 unsigned char iv[16];
00689
00690 cipher_context_t ctx_dec;
00691 const cipher_info_t *cipher_info;
00692
00693 unsigned char encbuf[64];
00694 unsigned char decbuf[64];
00695
00696 size_t outlen = 0;
00697
00698 memset( key, 0, 32 );
00699 memset( iv , 0, 16 );
00700
00701 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
00702
00703 memset( encbuf, 0, 64 );
00704 memset( decbuf, 0, 64 );
00705
00706
00707 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
00708 TEST_ASSERT( NULL != cipher_info);
00709
00710 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
00711
00712 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
00713
00714 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
00715
00716 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
00717
00718 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00719 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
00720 #endif
00721
00722
00723 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
00724 TEST_ASSERT( 0 == outlen );
00725 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
00726 &ctx_dec, decbuf + outlen, &outlen ) );
00727 TEST_ASSERT( 0 == outlen );
00728
00729 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
00730 }
00731
00732 void test_suite_enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
00733 int second_length_val )
00734 {
00735 size_t first_length = first_length_val;
00736 size_t second_length = second_length_val;
00737 size_t length = first_length + second_length;
00738 unsigned char key[32];
00739 unsigned char iv[16];
00740
00741 cipher_context_t ctx_dec;
00742 cipher_context_t ctx_enc;
00743 const cipher_info_t *cipher_info;
00744
00745 unsigned char inbuf[64];
00746 unsigned char encbuf[64];
00747 unsigned char decbuf[64];
00748
00749 size_t outlen = 0;
00750 size_t totaloutlen = 0;
00751
00752 memset( key, 0, 32 );
00753 memset( iv , 0, 16 );
00754
00755 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
00756 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
00757
00758 memset( inbuf, 5, 64 );
00759 memset( encbuf, 0, 64 );
00760 memset( decbuf, 0, 64 );
00761
00762
00763 cipher_info = cipher_info_from_type( cipher_id );
00764 TEST_ASSERT( NULL != cipher_info);
00765
00766 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
00767 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
00768
00769 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
00770 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
00771
00772 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
00773 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
00774
00775 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
00776 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
00777
00778 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00779 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
00780 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
00781 #endif
00782
00783
00784 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
00785 totaloutlen = outlen;
00786 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
00787 totaloutlen += outlen;
00788 TEST_ASSERT( totaloutlen == length ||
00789 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
00790 totaloutlen < length &&
00791 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
00792
00793 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
00794 totaloutlen += outlen;
00795 TEST_ASSERT( totaloutlen == length ||
00796 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
00797 totaloutlen > length &&
00798 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
00799
00800
00801 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
00802 totaloutlen = outlen;
00803
00804 TEST_ASSERT( totaloutlen == length ||
00805 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
00806 totaloutlen < length &&
00807 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
00808
00809 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
00810 totaloutlen += outlen;
00811
00812 TEST_ASSERT( totaloutlen == length );
00813
00814 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
00815
00816 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
00817 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
00818 }
00819
00820 void test_suite_decrypt_test_vec( int cipher_id, int pad_mode,
00821 char *hex_key, char *hex_iv,
00822 char *hex_cipher, char *hex_clear,
00823 char *hex_ad, char *hex_tag,
00824 int finish_result, int tag_result )
00825 {
00826 unsigned char key[50];
00827 unsigned char iv[50];
00828 unsigned char cipher[200];
00829 unsigned char clear[200];
00830 unsigned char ad[200];
00831 unsigned char tag[20];
00832 size_t key_len, iv_len, cipher_len, clear_len;
00833 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00834 size_t ad_len, tag_len;
00835 #endif
00836 cipher_context_t ctx;
00837 unsigned char output[200];
00838 size_t outlen, total_len;
00839
00840 memset( key, 0x00, sizeof( key ) );
00841 memset( iv, 0x00, sizeof( iv ) );
00842 memset( cipher, 0x00, sizeof( cipher ) );
00843 memset( clear, 0x00, sizeof( clear ) );
00844 memset( ad, 0x00, sizeof( ad ) );
00845 memset( tag, 0x00, sizeof( tag ) );
00846 memset( output, 0x00, sizeof( output ) );
00847
00848 key_len = unhexify( key, hex_key );
00849 iv_len = unhexify( iv, hex_iv );
00850 cipher_len = unhexify( cipher, hex_cipher );
00851 clear_len = unhexify( clear, hex_clear );
00852 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00853 ad_len = unhexify( ad, hex_ad );
00854 tag_len = unhexify( tag, hex_tag );
00855 #else
00856 ((void) hex_ad);
00857 ((void) hex_tag);
00858 #endif
00859
00860
00861 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
00862 cipher_info_from_type( cipher_id ) ) );
00863 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
00864 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
00865 if( pad_mode != -1 )
00866 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
00867 #else
00868 (void) pad_mode;
00869 #endif
00870 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
00871 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
00872 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00873 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
00874 #endif
00875
00876
00877 total_len = 0;
00878 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
00879 total_len += outlen;
00880 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
00881 &outlen ) );
00882 total_len += outlen;
00883 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00884 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
00885 #endif
00886
00887
00888 if( 0 == finish_result && 0 == tag_result )
00889 {
00890 TEST_ASSERT( total_len == clear_len );
00891 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
00892 }
00893
00894 cipher_free_ctx( &ctx );
00895 }
00896
00897 void test_suite_test_vec_ecb( int cipher_id, int operation, char *hex_key,
00898 char *hex_input, char *hex_result,
00899 int finish_result )
00900 {
00901 unsigned char key[50];
00902 unsigned char input[16];
00903 unsigned char result[16];
00904 size_t key_len;
00905 cipher_context_t ctx;
00906 unsigned char output[32];
00907 size_t outlen;
00908
00909 memset( key, 0x00, sizeof( key ) );
00910 memset( input, 0x00, sizeof( input ) );
00911 memset( result, 0x00, sizeof( result ) );
00912 memset( output, 0x00, sizeof( output ) );
00913
00914
00915 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
00916 cipher_info_from_type( cipher_id ) ) );
00917
00918 key_len = unhexify( key, hex_key );
00919 TEST_ASSERT( unhexify( input, hex_input ) ==
00920 (int) cipher_get_block_size( &ctx ) );
00921 TEST_ASSERT( unhexify( result, hex_result ) ==
00922 (int) cipher_get_block_size( &ctx ) );
00923
00924 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
00925
00926 TEST_ASSERT( 0 == cipher_update( &ctx, input,
00927 cipher_get_block_size( &ctx ),
00928 output, &outlen ) );
00929 TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
00930 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
00931 &outlen ) );
00932 TEST_ASSERT( 0 == outlen );
00933
00934
00935 if( 0 == finish_result )
00936 TEST_ASSERT( 0 == memcmp( output, result,
00937 cipher_get_block_size( &ctx ) ) );
00938
00939 cipher_free_ctx( &ctx );
00940 }
00941
00942 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
00943 void test_suite_set_padding( int cipher_id, int pad_mode, int ret )
00944 {
00945 const cipher_info_t *cipher_info;
00946 cipher_context_t ctx;
00947
00948 cipher_info = cipher_info_from_type( cipher_id );
00949 TEST_ASSERT( NULL != cipher_info );
00950 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
00951
00952 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
00953
00954 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
00955 }
00956 #endif
00957
00958 #ifdef POLARSSL_CIPHER_MODE_CBC
00959 void test_suite_check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
00960 {
00961 cipher_info_t cipher_info;
00962 cipher_context_t ctx;
00963 unsigned char input[16];
00964 size_t ilen, dlen;
00965
00966
00967 memset( &ctx, 0, sizeof( ctx ) );
00968 cipher_info.mode = POLARSSL_MODE_CBC;
00969 ctx.cipher_info = &cipher_info;
00970
00971 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
00972
00973 ilen = unhexify( input, input_str );
00974
00975 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
00976 if( 0 == ret )
00977 TEST_ASSERT( dlen == (size_t) dlen_check );
00978 }
00979 #endif
00980
00981 #ifdef POLARSSL_SELF_TEST
00982 void test_suite_cipher_selftest()
00983 {
00984 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
00985 }
00986 #endif
00987
00988
00989 #endif
00990
00991
00992 int dep_check( char *str )
00993 {
00994 if( str == NULL )
00995 return( 1 );
00996
00997 if( strcmp( str, "POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN" ) == 0 )
00998 {
00999 #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
01000 return( 0 );
01001 #else
01002 return( 1 );
01003 #endif
01004 }
01005 if( strcmp( str, "POLARSSL_CIPHER_PADDING_ZEROS" ) == 0 )
01006 {
01007 #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
01008 return( 0 );
01009 #else
01010 return( 1 );
01011 #endif
01012 }
01013 if( strcmp( str, "POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS" ) == 0 )
01014 {
01015 #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
01016 return( 0 );
01017 #else
01018 return( 1 );
01019 #endif
01020 }
01021 if( strcmp( str, "POLARSSL_AES_C" ) == 0 )
01022 {
01023 #if defined(POLARSSL_AES_C)
01024 return( 0 );
01025 #else
01026 return( 1 );
01027 #endif
01028 }
01029 if( strcmp( str, "POLARSSL_DES_C" ) == 0 )
01030 {
01031 #if defined(POLARSSL_DES_C)
01032 return( 0 );
01033 #else
01034 return( 1 );
01035 #endif
01036 }
01037 if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
01038 {
01039 #if defined(POLARSSL_CIPHER_MODE_CBC)
01040 return( 0 );
01041 #else
01042 return( 1 );
01043 #endif
01044 }
01045 if( strcmp( str, "POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
01046 {
01047 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
01048 return( 0 );
01049 #else
01050 return( 1 );
01051 #endif
01052 }
01053 if( strcmp( str, "POLARSSL_CIPHER_NULL_CIPHER" ) == 0 )
01054 {
01055 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
01056 return( 0 );
01057 #else
01058 return( 1 );
01059 #endif
01060 }
01061 if( strcmp( str, "POLARSSL_CIPHER_MODE_CFB" ) == 0 )
01062 {
01063 #if defined(POLARSSL_CIPHER_MODE_CFB)
01064 return( 0 );
01065 #else
01066 return( 1 );
01067 #endif
01068 }
01069 if( strcmp( str, "POLARSSL_BLOWFISH_C" ) == 0 )
01070 {
01071 #if defined(POLARSSL_BLOWFISH_C)
01072 return( 0 );
01073 #else
01074 return( 1 );
01075 #endif
01076 }
01077 if( strcmp( str, "POLARSSL_CIPHER_MODE_CTR" ) == 0 )
01078 {
01079 #if defined(POLARSSL_CIPHER_MODE_CTR)
01080 return( 0 );
01081 #else
01082 return( 1 );
01083 #endif
01084 }
01085 if( strcmp( str, "POLARSSL_CAMELLIA_C" ) == 0 )
01086 {
01087 #if defined(POLARSSL_CAMELLIA_C)
01088 return( 0 );
01089 #else
01090 return( 1 );
01091 #endif
01092 }
01093
01094
01095 return( 1 );
01096 }
01097
01098 int dispatch_test(int cnt, char *params[50])
01099 {
01100 int ret;
01101 ((void) cnt);
01102 ((void) params);
01103
01104 #if defined(TEST_SUITE_ACTIVE)
01105 if( strcmp( params[0], "enc_dec_buf" ) == 0 )
01106 {
01107
01108 int param1;
01109 char *param2 = params[2];
01110 int param3;
01111 int param4;
01112 int param5;
01113
01114 if( cnt != 6 )
01115 {
01116 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
01117 return( 2 );
01118 }
01119
01120 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01121 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01122 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01123 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01124 if( verify_int( params[5], ¶m5 ) != 0 ) return( 2 );
01125
01126 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
01127 return ( 0 );
01128
01129 return ( 3 );
01130 }
01131 else
01132 if( strcmp( params[0], "enc_fail" ) == 0 )
01133 {
01134
01135 int param1;
01136 int param2;
01137 int param3;
01138 int param4;
01139 int param5;
01140
01141 if( cnt != 6 )
01142 {
01143 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
01144 return( 2 );
01145 }
01146
01147 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01148 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01149 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01150 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01151 if( verify_int( params[5], ¶m5 ) != 0 ) return( 2 );
01152
01153 test_suite_enc_fail( param1, param2, param3, param4, param5 );
01154 return ( 0 );
01155
01156 return ( 3 );
01157 }
01158 else
01159 if( strcmp( params[0], "dec_empty_buf" ) == 0 )
01160 {
01161
01162
01163 if( cnt != 1 )
01164 {
01165 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
01166 return( 2 );
01167 }
01168
01169
01170 test_suite_dec_empty_buf( );
01171 return ( 0 );
01172
01173 return ( 3 );
01174 }
01175 else
01176 if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
01177 {
01178
01179 int param1;
01180 int param2;
01181 int param3;
01182 int param4;
01183
01184 if( cnt != 5 )
01185 {
01186 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
01187 return( 2 );
01188 }
01189
01190 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01191 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01192 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01193 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01194
01195 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
01196 return ( 0 );
01197
01198 return ( 3 );
01199 }
01200 else
01201 if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
01202 {
01203
01204 int param1;
01205 int param2;
01206 char *param3 = params[3];
01207 char *param4 = params[4];
01208 char *param5 = params[5];
01209 char *param6 = params[6];
01210 char *param7 = params[7];
01211 char *param8 = params[8];
01212 int param9;
01213 int param10;
01214
01215 if( cnt != 11 )
01216 {
01217 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
01218 return( 2 );
01219 }
01220
01221 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01222 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01223 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01224 if( verify_string( ¶m4 ) != 0 ) return( 2 );
01225 if( verify_string( ¶m5 ) != 0 ) return( 2 );
01226 if( verify_string( ¶m6 ) != 0 ) return( 2 );
01227 if( verify_string( ¶m7 ) != 0 ) return( 2 );
01228 if( verify_string( ¶m8 ) != 0 ) return( 2 );
01229 if( verify_int( params[9], ¶m9 ) != 0 ) return( 2 );
01230 if( verify_int( params[10], ¶m10 ) != 0 ) return( 2 );
01231
01232 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
01233 return ( 0 );
01234
01235 return ( 3 );
01236 }
01237 else
01238 if( strcmp( params[0], "test_vec_ecb" ) == 0 )
01239 {
01240
01241 int param1;
01242 int param2;
01243 char *param3 = params[3];
01244 char *param4 = params[4];
01245 char *param5 = params[5];
01246 int param6;
01247
01248 if( cnt != 7 )
01249 {
01250 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
01251 return( 2 );
01252 }
01253
01254 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01255 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01256 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01257 if( verify_string( ¶m4 ) != 0 ) return( 2 );
01258 if( verify_string( ¶m5 ) != 0 ) return( 2 );
01259 if( verify_int( params[6], ¶m6 ) != 0 ) return( 2 );
01260
01261 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
01262 return ( 0 );
01263
01264 return ( 3 );
01265 }
01266 else
01267 if( strcmp( params[0], "set_padding" ) == 0 )
01268 {
01269 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
01270
01271 int param1;
01272 int param2;
01273 int param3;
01274
01275 if( cnt != 4 )
01276 {
01277 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
01278 return( 2 );
01279 }
01280
01281 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01282 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01283 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01284
01285 test_suite_set_padding( param1, param2, param3 );
01286 return ( 0 );
01287 #endif
01288
01289 return ( 3 );
01290 }
01291 else
01292 if( strcmp( params[0], "check_padding" ) == 0 )
01293 {
01294 #ifdef POLARSSL_CIPHER_MODE_CBC
01295
01296 int param1;
01297 char *param2 = params[2];
01298 int param3;
01299 int param4;
01300
01301 if( cnt != 5 )
01302 {
01303 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
01304 return( 2 );
01305 }
01306
01307 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01308 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01309 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01310 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01311
01312 test_suite_check_padding( param1, param2, param3, param4 );
01313 return ( 0 );
01314 #endif
01315
01316 return ( 3 );
01317 }
01318 else
01319 if( strcmp( params[0], "cipher_selftest" ) == 0 )
01320 {
01321 #ifdef POLARSSL_SELF_TEST
01322
01323
01324 if( cnt != 1 )
01325 {
01326 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
01327 return( 2 );
01328 }
01329
01330
01331 test_suite_cipher_selftest( );
01332 return ( 0 );
01333 #endif
01334
01335 return ( 3 );
01336 }
01337 else
01338
01339 {
01340 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
01341 fflush( stdout );
01342 return( 1 );
01343 }
01344 #else
01345 return( 3 );
01346 #endif
01347 return( ret );
01348 }
01349
01350 int get_line( FILE *f, char *buf, size_t len )
01351 {
01352 char *ret;
01353
01354 ret = fgets( buf, len, f );
01355 if( ret == NULL )
01356 return( -1 );
01357
01358 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
01359 buf[strlen(buf) - 1] = '\0';
01360 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
01361 buf[strlen(buf) - 1] = '\0';
01362
01363 return( 0 );
01364 }
01365
01366 int parse_arguments( char *buf, size_t len, char *params[50] )
01367 {
01368 int cnt = 0, i;
01369 char *cur = buf;
01370 char *p = buf, *q;
01371
01372 params[cnt++] = cur;
01373
01374 while( *p != '\0' && p < buf + len )
01375 {
01376 if( *p == '\\' )
01377 {
01378 *p++;
01379 *p++;
01380 continue;
01381 }
01382 if( *p == ':' )
01383 {
01384 if( p + 1 < buf + len )
01385 {
01386 cur = p + 1;
01387 params[cnt++] = cur;
01388 }
01389 *p = '\0';
01390 }
01391
01392 *p++;
01393 }
01394
01395
01396 for( i = 0; i < cnt; i++ )
01397 {
01398 p = params[i];
01399 q = params[i];
01400
01401 while( *p != '\0' )
01402 {
01403 if( *p == '\\' && *(p + 1) == 'n' )
01404 {
01405 p += 2;
01406 *(q++) = '\n';
01407 }
01408 else if( *p == '\\' && *(p + 1) == ':' )
01409 {
01410 p += 2;
01411 *(q++) = ':';
01412 }
01413 else if( *p == '\\' && *(p + 1) == '?' )
01414 {
01415 p += 2;
01416 *(q++) = '?';
01417 }
01418 else
01419 *(q++) = *(p++);
01420 }
01421 *q = '\0';
01422 }
01423
01424 return( cnt );
01425 }
01426
01427 int main()
01428 {
01429 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
01430 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_cipher.padding.data";
01431 FILE *file;
01432 char buf[5000];
01433 char *params[50];
01434
01435 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01436 unsigned char alloc_buf[1000000];
01437 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
01438 #endif
01439
01440 file = fopen( filename, "r" );
01441 if( file == NULL )
01442 {
01443 fprintf( stderr, "Failed to open\n" );
01444 return( 1 );
01445 }
01446
01447 while( !feof( file ) )
01448 {
01449 int skip = 0;
01450
01451 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01452 break;
01453 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
01454 fprintf( stdout, " " );
01455 for( i = strlen( buf ) + 1; i < 67; i++ )
01456 fprintf( stdout, "." );
01457 fprintf( stdout, " " );
01458 fflush( stdout );
01459
01460 total_tests++;
01461
01462 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01463 break;
01464 cnt = parse_arguments( buf, strlen(buf), params );
01465
01466 if( strcmp( params[0], "depends_on" ) == 0 )
01467 {
01468 for( i = 1; i < cnt; i++ )
01469 if( dep_check( params[i] ) != 0 )
01470 skip = 1;
01471
01472 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01473 break;
01474 cnt = parse_arguments( buf, strlen(buf), params );
01475 }
01476
01477 if( skip == 0 )
01478 {
01479 test_errors = 0;
01480 ret = dispatch_test( cnt, params );
01481 }
01482
01483 if( skip == 1 || ret == 3 )
01484 {
01485 total_skipped++;
01486 fprintf( stdout, "----\n" );
01487 fflush( stdout );
01488 }
01489 else if( ret == 0 && test_errors == 0 )
01490 {
01491 fprintf( stdout, "PASS\n" );
01492 fflush( stdout );
01493 }
01494 else if( ret == 2 )
01495 {
01496 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
01497 fclose(file);
01498 exit( 2 );
01499 }
01500 else
01501 total_errors++;
01502
01503 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01504 break;
01505 if( strlen(buf) != 0 )
01506 {
01507 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
01508 return( 1 );
01509 }
01510 }
01511 fclose(file);
01512
01513 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
01514 if( total_errors == 0 )
01515 fprintf( stdout, "PASSED" );
01516 else
01517 fprintf( stdout, "FAILED" );
01518
01519 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
01520 total_tests - total_errors, total_tests, total_skipped );
01521
01522 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01523 #if defined(POLARSSL_MEMORY_DEBUG)
01524 memory_buffer_alloc_status();
01525 #endif
01526 memory_buffer_alloc_free();
01527 #endif
01528
01529 return( total_errors != 0 );
01530 }
01531
01532