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