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