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