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_DES_EDE_CBC" ) == 0 )
00365 {
00366 *value = ( POLARSSL_CIPHER_DES_EDE_CBC );
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_DES_CBC" ) == 0 )
00375 {
00376 *value = ( POLARSSL_CIPHER_DES_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_DES_EDE3_CBC" ) == 0 )
00390 {
00391 *value = ( POLARSSL_CIPHER_DES_EDE3_CBC );
00392 return( 0 );
00393 }
00394 if( strcmp( str, "POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
00395 {
00396 *value = ( POLARSSL_PADDING_ZEROS_AND_LEN );
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_DES_C" ) == 0 )
00896 {
00897 #if defined(POLARSSL_DES_C)
00898 return( 0 );
00899 #else
00900 return( 1 );
00901 #endif
00902 }
00903 if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
00904 {
00905 #if defined(POLARSSL_CIPHER_MODE_CBC)
00906 return( 0 );
00907 #else
00908 return( 1 );
00909 #endif
00910 }
00911 if( strcmp( str, "POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
00912 {
00913 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
00914 return( 0 );
00915 #else
00916 return( 1 );
00917 #endif
00918 }
00919
00920
00921 return( 1 );
00922 }
00923
00924 int dispatch_test(int cnt, char *params[50])
00925 {
00926 int ret;
00927 ((void) cnt);
00928 ((void) params);
00929
00930 #if defined(TEST_SUITE_ACTIVE)
00931 if( strcmp( params[0], "enc_dec_buf" ) == 0 )
00932 {
00933
00934 int param1;
00935 char *param2 = params[2];
00936 int param3;
00937 int param4;
00938 int param5;
00939
00940 if( cnt != 6 )
00941 {
00942 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
00943 return( 2 );
00944 }
00945
00946 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00947 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00948 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
00949 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
00950 if( verify_int( params[5], ¶m5 ) != 0 ) return( 2 );
00951
00952 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
00953 return ( 0 );
00954
00955 return ( 3 );
00956 }
00957 else
00958 if( strcmp( params[0], "enc_fail" ) == 0 )
00959 {
00960
00961 int param1;
00962 int param2;
00963 int param3;
00964 int param4;
00965 int param5;
00966
00967 if( cnt != 6 )
00968 {
00969 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
00970 return( 2 );
00971 }
00972
00973 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00974 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
00975 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
00976 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
00977 if( verify_int( params[5], ¶m5 ) != 0 ) return( 2 );
00978
00979 test_suite_enc_fail( param1, param2, param3, param4, param5 );
00980 return ( 0 );
00981
00982 return ( 3 );
00983 }
00984 else
00985 if( strcmp( params[0], "dec_empty_buf" ) == 0 )
00986 {
00987
00988
00989 if( cnt != 1 )
00990 {
00991 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
00992 return( 2 );
00993 }
00994
00995
00996 test_suite_dec_empty_buf( );
00997 return ( 0 );
00998
00999 return ( 3 );
01000 }
01001 else
01002 if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
01003 {
01004
01005 int param1;
01006 int param2;
01007 int param3;
01008 int param4;
01009
01010 if( cnt != 5 )
01011 {
01012 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
01013 return( 2 );
01014 }
01015
01016 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01017 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01018 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01019 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01020
01021 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
01022 return ( 0 );
01023
01024 return ( 3 );
01025 }
01026 else
01027 if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
01028 {
01029
01030 int param1;
01031 int param2;
01032 char *param3 = params[3];
01033 char *param4 = params[4];
01034 char *param5 = params[5];
01035 char *param6 = params[6];
01036 char *param7 = params[7];
01037 char *param8 = params[8];
01038 int param9;
01039 int param10;
01040
01041 if( cnt != 11 )
01042 {
01043 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
01044 return( 2 );
01045 }
01046
01047 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01048 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01049 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01050 if( verify_string( ¶m4 ) != 0 ) return( 2 );
01051 if( verify_string( ¶m5 ) != 0 ) return( 2 );
01052 if( verify_string( ¶m6 ) != 0 ) return( 2 );
01053 if( verify_string( ¶m7 ) != 0 ) return( 2 );
01054 if( verify_string( ¶m8 ) != 0 ) return( 2 );
01055 if( verify_int( params[9], ¶m9 ) != 0 ) return( 2 );
01056 if( verify_int( params[10], ¶m10 ) != 0 ) return( 2 );
01057
01058 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
01059 return ( 0 );
01060
01061 return ( 3 );
01062 }
01063 else
01064 if( strcmp( params[0], "test_vec_ecb" ) == 0 )
01065 {
01066
01067 int param1;
01068 int param2;
01069 char *param3 = params[3];
01070 char *param4 = params[4];
01071 char *param5 = params[5];
01072 int param6;
01073
01074 if( cnt != 7 )
01075 {
01076 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
01077 return( 2 );
01078 }
01079
01080 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01081 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01082 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01083 if( verify_string( ¶m4 ) != 0 ) return( 2 );
01084 if( verify_string( ¶m5 ) != 0 ) return( 2 );
01085 if( verify_int( params[6], ¶m6 ) != 0 ) return( 2 );
01086
01087 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
01088 return ( 0 );
01089
01090 return ( 3 );
01091 }
01092 else
01093 if( strcmp( params[0], "set_padding" ) == 0 )
01094 {
01095 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
01096
01097 int param1;
01098 int param2;
01099 int param3;
01100
01101 if( cnt != 4 )
01102 {
01103 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
01104 return( 2 );
01105 }
01106
01107 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01108 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01109 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01110
01111 test_suite_set_padding( param1, param2, param3 );
01112 return ( 0 );
01113 #endif
01114
01115 return ( 3 );
01116 }
01117 else
01118 if( strcmp( params[0], "check_padding" ) == 0 )
01119 {
01120 #ifdef POLARSSL_CIPHER_MODE_CBC
01121
01122 int param1;
01123 char *param2 = params[2];
01124 int param3;
01125 int param4;
01126
01127 if( cnt != 5 )
01128 {
01129 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
01130 return( 2 );
01131 }
01132
01133 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01134 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01135 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01136 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01137
01138 test_suite_check_padding( param1, param2, param3, param4 );
01139 return ( 0 );
01140 #endif
01141
01142 return ( 3 );
01143 }
01144 else
01145 if( strcmp( params[0], "cipher_selftest" ) == 0 )
01146 {
01147 #ifdef POLARSSL_SELF_TEST
01148
01149
01150 if( cnt != 1 )
01151 {
01152 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
01153 return( 2 );
01154 }
01155
01156
01157 test_suite_cipher_selftest( );
01158 return ( 0 );
01159 #endif
01160
01161 return ( 3 );
01162 }
01163 else
01164
01165 {
01166 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
01167 fflush( stdout );
01168 return( 1 );
01169 }
01170 #else
01171 return( 3 );
01172 #endif
01173 return( ret );
01174 }
01175
01176 int get_line( FILE *f, char *buf, size_t len )
01177 {
01178 char *ret;
01179
01180 ret = fgets( buf, len, f );
01181 if( ret == NULL )
01182 return( -1 );
01183
01184 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
01185 buf[strlen(buf) - 1] = '\0';
01186 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
01187 buf[strlen(buf) - 1] = '\0';
01188
01189 return( 0 );
01190 }
01191
01192 int parse_arguments( char *buf, size_t len, char *params[50] )
01193 {
01194 int cnt = 0, i;
01195 char *cur = buf;
01196 char *p = buf, *q;
01197
01198 params[cnt++] = cur;
01199
01200 while( *p != '\0' && p < buf + len )
01201 {
01202 if( *p == '\\' )
01203 {
01204 *p++;
01205 *p++;
01206 continue;
01207 }
01208 if( *p == ':' )
01209 {
01210 if( p + 1 < buf + len )
01211 {
01212 cur = p + 1;
01213 params[cnt++] = cur;
01214 }
01215 *p = '\0';
01216 }
01217
01218 *p++;
01219 }
01220
01221
01222 for( i = 0; i < cnt; i++ )
01223 {
01224 p = params[i];
01225 q = params[i];
01226
01227 while( *p != '\0' )
01228 {
01229 if( *p == '\\' && *(p + 1) == 'n' )
01230 {
01231 p += 2;
01232 *(q++) = '\n';
01233 }
01234 else if( *p == '\\' && *(p + 1) == ':' )
01235 {
01236 p += 2;
01237 *(q++) = ':';
01238 }
01239 else if( *p == '\\' && *(p + 1) == '?' )
01240 {
01241 p += 2;
01242 *(q++) = '?';
01243 }
01244 else
01245 *(q++) = *(p++);
01246 }
01247 *q = '\0';
01248 }
01249
01250 return( cnt );
01251 }
01252
01253 int main()
01254 {
01255 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
01256 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_cipher.des.data";
01257 FILE *file;
01258 char buf[5000];
01259 char *params[50];
01260
01261 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01262 unsigned char alloc_buf[1000000];
01263 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
01264 #endif
01265
01266 file = fopen( filename, "r" );
01267 if( file == NULL )
01268 {
01269 fprintf( stderr, "Failed to open\n" );
01270 return( 1 );
01271 }
01272
01273 while( !feof( file ) )
01274 {
01275 int skip = 0;
01276
01277 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01278 break;
01279 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
01280 fprintf( stdout, " " );
01281 for( i = strlen( buf ) + 1; i < 67; i++ )
01282 fprintf( stdout, "." );
01283 fprintf( stdout, " " );
01284 fflush( stdout );
01285
01286 total_tests++;
01287
01288 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01289 break;
01290 cnt = parse_arguments( buf, strlen(buf), params );
01291
01292 if( strcmp( params[0], "depends_on" ) == 0 )
01293 {
01294 for( i = 1; i < cnt; i++ )
01295 if( dep_check( params[i] ) != 0 )
01296 skip = 1;
01297
01298 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01299 break;
01300 cnt = parse_arguments( buf, strlen(buf), params );
01301 }
01302
01303 if( skip == 0 )
01304 {
01305 test_errors = 0;
01306 ret = dispatch_test( cnt, params );
01307 }
01308
01309 if( skip == 1 || ret == 3 )
01310 {
01311 total_skipped++;
01312 fprintf( stdout, "----\n" );
01313 fflush( stdout );
01314 }
01315 else if( ret == 0 && test_errors == 0 )
01316 {
01317 fprintf( stdout, "PASS\n" );
01318 fflush( stdout );
01319 }
01320 else if( ret == 2 )
01321 {
01322 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
01323 fclose(file);
01324 exit( 2 );
01325 }
01326 else
01327 total_errors++;
01328
01329 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01330 break;
01331 if( strlen(buf) != 0 )
01332 {
01333 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
01334 return( 1 );
01335 }
01336 }
01337 fclose(file);
01338
01339 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
01340 if( total_errors == 0 )
01341 fprintf( stdout, "PASSED" );
01342 else
01343 fprintf( stdout, "FAILED" );
01344
01345 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
01346 total_tests - total_errors, total_tests, total_skipped );
01347
01348 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01349 #if defined(POLARSSL_MEMORY_DEBUG)
01350 memory_buffer_alloc_status();
01351 #endif
01352 memory_buffer_alloc_free();
01353 #endif
01354
01355 return( total_errors != 0 );
01356 }
01357
01358