PolarSSL v1.1.4
|
00001 #include "fct.h" 00002 00003 #include <polarssl/cipher.h> 00004 00005 #include <polarssl/config.h> 00006 00007 #ifdef _MSC_VER 00008 #include <basetsd.h> 00009 typedef UINT32 uint32_t; 00010 #else 00011 #include <inttypes.h> 00012 #endif 00013 00014 /* 00015 * 32-bit integer manipulation macros (big endian) 00016 */ 00017 #ifndef GET_ULONG_BE 00018 #define GET_ULONG_BE(n,b,i) \ 00019 { \ 00020 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \ 00021 | ( (unsigned long) (b)[(i) + 1] << 16 ) \ 00022 | ( (unsigned long) (b)[(i) + 2] << 8 ) \ 00023 | ( (unsigned long) (b)[(i) + 3] ); \ 00024 } 00025 #endif 00026 00027 #ifndef PUT_ULONG_BE 00028 #define PUT_ULONG_BE(n,b,i) \ 00029 { \ 00030 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 00031 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 00032 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 00033 (b)[(i) + 3] = (unsigned char) ( (n) ); \ 00034 } 00035 #endif 00036 00037 int unhexify(unsigned char *obuf, const char *ibuf) 00038 { 00039 unsigned char c, c2; 00040 int len = strlen(ibuf) / 2; 00041 assert(!(strlen(ibuf) %1)); // must be even number of bytes 00042 00043 while (*ibuf != 0) 00044 { 00045 c = *ibuf++; 00046 if( c >= '0' && c <= '9' ) 00047 c -= '0'; 00048 else if( c >= 'a' && c <= 'f' ) 00049 c -= 'a' - 10; 00050 else if( c >= 'A' && c <= 'F' ) 00051 c -= 'A' - 10; 00052 else 00053 assert( 0 ); 00054 00055 c2 = *ibuf++; 00056 if( c2 >= '0' && c2 <= '9' ) 00057 c2 -= '0'; 00058 else if( c2 >= 'a' && c2 <= 'f' ) 00059 c2 -= 'a' - 10; 00060 else if( c2 >= 'A' && c2 <= 'F' ) 00061 c2 -= 'A' - 10; 00062 else 00063 assert( 0 ); 00064 00065 *obuf++ = ( c << 4 ) | c2; 00066 } 00067 00068 return len; 00069 } 00070 00071 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len) 00072 { 00073 unsigned char l, h; 00074 00075 while (len != 0) 00076 { 00077 h = (*ibuf) / 16; 00078 l = (*ibuf) % 16; 00079 00080 if( h < 10 ) 00081 *obuf++ = '0' + h; 00082 else 00083 *obuf++ = 'a' + h - 10; 00084 00085 if( l < 10 ) 00086 *obuf++ = '0' + l; 00087 else 00088 *obuf++ = 'a' + l - 10; 00089 00090 ++ibuf; 00091 len--; 00092 } 00093 } 00094 00104 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len ) 00105 { 00106 size_t i; 00107 00108 if( rng_state != NULL ) 00109 rng_state = NULL; 00110 00111 for( i = 0; i < len; ++i ) 00112 output[i] = rand(); 00113 00114 return( 0 ); 00115 } 00116 00122 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len ) 00123 { 00124 if( rng_state != NULL ) 00125 rng_state = NULL; 00126 00127 memset( output, 0, len ); 00128 00129 return( 0 ); 00130 } 00131 00132 typedef struct 00133 { 00134 unsigned char *buf; 00135 size_t length; 00136 } rnd_buf_info; 00137 00149 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len ) 00150 { 00151 rnd_buf_info *info = (rnd_buf_info *) rng_state; 00152 size_t use_len; 00153 00154 if( rng_state == NULL ) 00155 return( rnd_std_rand( NULL, output, len ) ); 00156 00157 use_len = len; 00158 if( len > info->length ) 00159 use_len = info->length; 00160 00161 if( use_len ) 00162 { 00163 memcpy( output, info->buf, use_len ); 00164 info->buf += use_len; 00165 info->length -= use_len; 00166 } 00167 00168 if( len - use_len > 0 ) 00169 return( rnd_std_rand( NULL, output + use_len, len - use_len ) ); 00170 00171 return( 0 ); 00172 } 00173 00181 typedef struct 00182 { 00183 uint32_t key[16]; 00184 uint32_t v0, v1; 00185 } rnd_pseudo_info; 00186 00195 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len ) 00196 { 00197 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state; 00198 uint32_t i, *k, sum, delta=0x9E3779B9; 00199 unsigned char result[4]; 00200 00201 if( rng_state == NULL ) 00202 return( rnd_std_rand( NULL, output, len ) ); 00203 00204 k = info->key; 00205 00206 while( len > 0 ) 00207 { 00208 size_t use_len = ( len > 4 ) ? 4 : len; 00209 sum = 0; 00210 00211 for( i = 0; i < 32; i++ ) 00212 { 00213 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]); 00214 sum += delta; 00215 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]); 00216 } 00217 00218 PUT_ULONG_BE( info->v0, result, 0 ); 00219 memcpy( output, result, use_len ); 00220 len -= use_len; 00221 } 00222 00223 return( 0 ); 00224 } 00225 00226 00227 FCT_BGN() 00228 { 00229 #ifdef POLARSSL_CIPHER_C 00230 00231 00232 FCT_SUITE_BGN(test_suite_cipher) 00233 { 00234 #ifdef POLARSSL_SELF_TEST 00235 00236 FCT_TEST_BGN(cipher_selftest) 00237 { 00238 fct_chk( cipher_self_test( 0 ) == 0 ); 00239 } 00240 FCT_TEST_END(); 00241 #endif /* POLARSSL_SELF_TEST */ 00242 00243 00244 FCT_TEST_BGN(decrypt_empty_buffer) 00245 unsigned char key[32]; 00246 unsigned char iv[16]; 00247 00248 cipher_context_t ctx_dec; 00249 const cipher_info_t *cipher_info; 00250 00251 unsigned char encbuf[64]; 00252 unsigned char decbuf[64]; 00253 00254 size_t outlen = 0; 00255 00256 memset( key, 0, 32 ); 00257 memset( iv , 0, 16 ); 00258 00259 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 00260 00261 memset( encbuf, 0, 64 ); 00262 memset( decbuf, 0, 64 ); 00263 00264 /* Initialise enc and dec contexts */ 00265 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 00266 fct_chk( NULL != cipher_info); 00267 00268 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 00269 00270 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 00271 00272 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 00273 00274 /* decode 0-byte string */ 00275 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) ); 00276 fct_chk( 0 == outlen ); 00277 fct_chk( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 00278 fct_chk( 0 == outlen ); 00279 00280 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 00281 FCT_TEST_END(); 00282 00283 #ifdef POLARSSL_AES_C 00284 00285 FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes) 00286 size_t length = 0; 00287 unsigned char key[32]; 00288 unsigned char iv[16]; 00289 00290 const cipher_info_t *cipher_info; 00291 cipher_context_t ctx_dec; 00292 cipher_context_t ctx_enc; 00293 00294 unsigned char inbuf[64]; 00295 unsigned char encbuf[64]; 00296 unsigned char decbuf[64]; 00297 00298 size_t outlen = 0; 00299 size_t enclen = 0; 00300 00301 memset( key, 0, 32 ); 00302 memset( iv , 0, 16 ); 00303 00304 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 00305 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 00306 00307 memset( inbuf, 5, 64 ); 00308 memset( encbuf, 0, 64 ); 00309 memset( decbuf, 0, 64 ); 00310 00311 /* Check and get info structures */ 00312 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 00313 fct_chk( NULL != cipher_info ); 00314 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 00315 00316 /* Initialise enc and dec contexts */ 00317 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 00318 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 00319 00320 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 00321 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 00322 00323 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 00324 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 00325 00326 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00327 { 00328 enclen = cipher_get_block_size( &ctx_enc ) 00329 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 00330 } 00331 else 00332 { 00333 enclen = length; 00334 } 00335 00336 /* encode length number of bytes from inbuf */ 00337 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 00338 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00339 { 00340 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 00341 } 00342 else 00343 { 00344 fct_chk( outlen == enclen ); 00345 } 00346 00347 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 00348 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00349 { 00350 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 00351 } 00352 else 00353 { 00354 fct_chk( outlen == 0 ); 00355 } 00356 00357 00358 /* decode the previously encoded string */ 00359 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 00360 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00361 { 00362 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 00363 } 00364 else 00365 { 00366 fct_chk( enclen == outlen ); 00367 } 00368 00369 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 00370 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00371 { 00372 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 00373 } 00374 else 00375 { 00376 fct_chk( outlen == 0 ); 00377 } 00378 00379 00380 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 00381 00382 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 00383 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 00384 FCT_TEST_END(); 00385 #endif /* POLARSSL_AES_C */ 00386 00387 #ifdef POLARSSL_AES_C 00388 00389 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_byte) 00390 size_t length = 1; 00391 unsigned char key[32]; 00392 unsigned char iv[16]; 00393 00394 const cipher_info_t *cipher_info; 00395 cipher_context_t ctx_dec; 00396 cipher_context_t ctx_enc; 00397 00398 unsigned char inbuf[64]; 00399 unsigned char encbuf[64]; 00400 unsigned char decbuf[64]; 00401 00402 size_t outlen = 0; 00403 size_t enclen = 0; 00404 00405 memset( key, 0, 32 ); 00406 memset( iv , 0, 16 ); 00407 00408 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 00409 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 00410 00411 memset( inbuf, 5, 64 ); 00412 memset( encbuf, 0, 64 ); 00413 memset( decbuf, 0, 64 ); 00414 00415 /* Check and get info structures */ 00416 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 00417 fct_chk( NULL != cipher_info ); 00418 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 00419 00420 /* Initialise enc and dec contexts */ 00421 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 00422 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 00423 00424 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 00425 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 00426 00427 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 00428 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 00429 00430 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00431 { 00432 enclen = cipher_get_block_size( &ctx_enc ) 00433 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 00434 } 00435 else 00436 { 00437 enclen = length; 00438 } 00439 00440 /* encode length number of bytes from inbuf */ 00441 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 00442 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00443 { 00444 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 00445 } 00446 else 00447 { 00448 fct_chk( outlen == enclen ); 00449 } 00450 00451 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 00452 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00453 { 00454 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 00455 } 00456 else 00457 { 00458 fct_chk( outlen == 0 ); 00459 } 00460 00461 00462 /* decode the previously encoded string */ 00463 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 00464 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00465 { 00466 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 00467 } 00468 else 00469 { 00470 fct_chk( enclen == outlen ); 00471 } 00472 00473 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 00474 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00475 { 00476 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 00477 } 00478 else 00479 { 00480 fct_chk( outlen == 0 ); 00481 } 00482 00483 00484 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 00485 00486 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 00487 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 00488 FCT_TEST_END(); 00489 #endif /* POLARSSL_AES_C */ 00490 00491 #ifdef POLARSSL_AES_C 00492 00493 FCT_TEST_BGN(aes_encrypt_and_decrypt_2_bytes) 00494 size_t length = 2; 00495 unsigned char key[32]; 00496 unsigned char iv[16]; 00497 00498 const cipher_info_t *cipher_info; 00499 cipher_context_t ctx_dec; 00500 cipher_context_t ctx_enc; 00501 00502 unsigned char inbuf[64]; 00503 unsigned char encbuf[64]; 00504 unsigned char decbuf[64]; 00505 00506 size_t outlen = 0; 00507 size_t enclen = 0; 00508 00509 memset( key, 0, 32 ); 00510 memset( iv , 0, 16 ); 00511 00512 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 00513 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 00514 00515 memset( inbuf, 5, 64 ); 00516 memset( encbuf, 0, 64 ); 00517 memset( decbuf, 0, 64 ); 00518 00519 /* Check and get info structures */ 00520 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 00521 fct_chk( NULL != cipher_info ); 00522 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 00523 00524 /* Initialise enc and dec contexts */ 00525 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 00526 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 00527 00528 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 00529 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 00530 00531 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 00532 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 00533 00534 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00535 { 00536 enclen = cipher_get_block_size( &ctx_enc ) 00537 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 00538 } 00539 else 00540 { 00541 enclen = length; 00542 } 00543 00544 /* encode length number of bytes from inbuf */ 00545 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 00546 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00547 { 00548 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 00549 } 00550 else 00551 { 00552 fct_chk( outlen == enclen ); 00553 } 00554 00555 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 00556 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00557 { 00558 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 00559 } 00560 else 00561 { 00562 fct_chk( outlen == 0 ); 00563 } 00564 00565 00566 /* decode the previously encoded string */ 00567 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 00568 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00569 { 00570 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 00571 } 00572 else 00573 { 00574 fct_chk( enclen == outlen ); 00575 } 00576 00577 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 00578 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00579 { 00580 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 00581 } 00582 else 00583 { 00584 fct_chk( outlen == 0 ); 00585 } 00586 00587 00588 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 00589 00590 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 00591 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 00592 FCT_TEST_END(); 00593 #endif /* POLARSSL_AES_C */ 00594 00595 #ifdef POLARSSL_AES_C 00596 00597 FCT_TEST_BGN(aes_encrypt_and_decrypt_7_bytes) 00598 size_t length = 7; 00599 unsigned char key[32]; 00600 unsigned char iv[16]; 00601 00602 const cipher_info_t *cipher_info; 00603 cipher_context_t ctx_dec; 00604 cipher_context_t ctx_enc; 00605 00606 unsigned char inbuf[64]; 00607 unsigned char encbuf[64]; 00608 unsigned char decbuf[64]; 00609 00610 size_t outlen = 0; 00611 size_t enclen = 0; 00612 00613 memset( key, 0, 32 ); 00614 memset( iv , 0, 16 ); 00615 00616 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 00617 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 00618 00619 memset( inbuf, 5, 64 ); 00620 memset( encbuf, 0, 64 ); 00621 memset( decbuf, 0, 64 ); 00622 00623 /* Check and get info structures */ 00624 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 00625 fct_chk( NULL != cipher_info ); 00626 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 00627 00628 /* Initialise enc and dec contexts */ 00629 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 00630 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 00631 00632 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 00633 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 00634 00635 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 00636 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 00637 00638 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00639 { 00640 enclen = cipher_get_block_size( &ctx_enc ) 00641 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 00642 } 00643 else 00644 { 00645 enclen = length; 00646 } 00647 00648 /* encode length number of bytes from inbuf */ 00649 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 00650 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00651 { 00652 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 00653 } 00654 else 00655 { 00656 fct_chk( outlen == enclen ); 00657 } 00658 00659 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 00660 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00661 { 00662 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 00663 } 00664 else 00665 { 00666 fct_chk( outlen == 0 ); 00667 } 00668 00669 00670 /* decode the previously encoded string */ 00671 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 00672 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00673 { 00674 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 00675 } 00676 else 00677 { 00678 fct_chk( enclen == outlen ); 00679 } 00680 00681 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 00682 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00683 { 00684 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 00685 } 00686 else 00687 { 00688 fct_chk( outlen == 0 ); 00689 } 00690 00691 00692 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 00693 00694 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 00695 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 00696 FCT_TEST_END(); 00697 #endif /* POLARSSL_AES_C */ 00698 00699 #ifdef POLARSSL_AES_C 00700 00701 FCT_TEST_BGN(aes_encrypt_and_decrypt_8_bytes) 00702 size_t length = 8; 00703 unsigned char key[32]; 00704 unsigned char iv[16]; 00705 00706 const cipher_info_t *cipher_info; 00707 cipher_context_t ctx_dec; 00708 cipher_context_t ctx_enc; 00709 00710 unsigned char inbuf[64]; 00711 unsigned char encbuf[64]; 00712 unsigned char decbuf[64]; 00713 00714 size_t outlen = 0; 00715 size_t enclen = 0; 00716 00717 memset( key, 0, 32 ); 00718 memset( iv , 0, 16 ); 00719 00720 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 00721 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 00722 00723 memset( inbuf, 5, 64 ); 00724 memset( encbuf, 0, 64 ); 00725 memset( decbuf, 0, 64 ); 00726 00727 /* Check and get info structures */ 00728 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 00729 fct_chk( NULL != cipher_info ); 00730 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 00731 00732 /* Initialise enc and dec contexts */ 00733 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 00734 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 00735 00736 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 00737 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 00738 00739 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 00740 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 00741 00742 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00743 { 00744 enclen = cipher_get_block_size( &ctx_enc ) 00745 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 00746 } 00747 else 00748 { 00749 enclen = length; 00750 } 00751 00752 /* encode length number of bytes from inbuf */ 00753 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 00754 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00755 { 00756 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 00757 } 00758 else 00759 { 00760 fct_chk( outlen == enclen ); 00761 } 00762 00763 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 00764 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00765 { 00766 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 00767 } 00768 else 00769 { 00770 fct_chk( outlen == 0 ); 00771 } 00772 00773 00774 /* decode the previously encoded string */ 00775 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 00776 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00777 { 00778 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 00779 } 00780 else 00781 { 00782 fct_chk( enclen == outlen ); 00783 } 00784 00785 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 00786 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00787 { 00788 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 00789 } 00790 else 00791 { 00792 fct_chk( outlen == 0 ); 00793 } 00794 00795 00796 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 00797 00798 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 00799 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 00800 FCT_TEST_END(); 00801 #endif /* POLARSSL_AES_C */ 00802 00803 #ifdef POLARSSL_AES_C 00804 00805 FCT_TEST_BGN(aes_encrypt_and_decrypt_9_bytes) 00806 size_t length = 9; 00807 unsigned char key[32]; 00808 unsigned char iv[16]; 00809 00810 const cipher_info_t *cipher_info; 00811 cipher_context_t ctx_dec; 00812 cipher_context_t ctx_enc; 00813 00814 unsigned char inbuf[64]; 00815 unsigned char encbuf[64]; 00816 unsigned char decbuf[64]; 00817 00818 size_t outlen = 0; 00819 size_t enclen = 0; 00820 00821 memset( key, 0, 32 ); 00822 memset( iv , 0, 16 ); 00823 00824 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 00825 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 00826 00827 memset( inbuf, 5, 64 ); 00828 memset( encbuf, 0, 64 ); 00829 memset( decbuf, 0, 64 ); 00830 00831 /* Check and get info structures */ 00832 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 00833 fct_chk( NULL != cipher_info ); 00834 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 00835 00836 /* Initialise enc and dec contexts */ 00837 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 00838 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 00839 00840 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 00841 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 00842 00843 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 00844 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 00845 00846 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00847 { 00848 enclen = cipher_get_block_size( &ctx_enc ) 00849 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 00850 } 00851 else 00852 { 00853 enclen = length; 00854 } 00855 00856 /* encode length number of bytes from inbuf */ 00857 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 00858 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00859 { 00860 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 00861 } 00862 else 00863 { 00864 fct_chk( outlen == enclen ); 00865 } 00866 00867 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 00868 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00869 { 00870 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 00871 } 00872 else 00873 { 00874 fct_chk( outlen == 0 ); 00875 } 00876 00877 00878 /* decode the previously encoded string */ 00879 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 00880 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00881 { 00882 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 00883 } 00884 else 00885 { 00886 fct_chk( enclen == outlen ); 00887 } 00888 00889 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 00890 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00891 { 00892 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 00893 } 00894 else 00895 { 00896 fct_chk( outlen == 0 ); 00897 } 00898 00899 00900 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 00901 00902 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 00903 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 00904 FCT_TEST_END(); 00905 #endif /* POLARSSL_AES_C */ 00906 00907 #ifdef POLARSSL_AES_C 00908 00909 FCT_TEST_BGN(aes_encrypt_and_decrypt_15_bytes) 00910 size_t length = 15; 00911 unsigned char key[32]; 00912 unsigned char iv[16]; 00913 00914 const cipher_info_t *cipher_info; 00915 cipher_context_t ctx_dec; 00916 cipher_context_t ctx_enc; 00917 00918 unsigned char inbuf[64]; 00919 unsigned char encbuf[64]; 00920 unsigned char decbuf[64]; 00921 00922 size_t outlen = 0; 00923 size_t enclen = 0; 00924 00925 memset( key, 0, 32 ); 00926 memset( iv , 0, 16 ); 00927 00928 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 00929 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 00930 00931 memset( inbuf, 5, 64 ); 00932 memset( encbuf, 0, 64 ); 00933 memset( decbuf, 0, 64 ); 00934 00935 /* Check and get info structures */ 00936 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 00937 fct_chk( NULL != cipher_info ); 00938 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 00939 00940 /* Initialise enc and dec contexts */ 00941 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 00942 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 00943 00944 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 00945 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 00946 00947 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 00948 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 00949 00950 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00951 { 00952 enclen = cipher_get_block_size( &ctx_enc ) 00953 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 00954 } 00955 else 00956 { 00957 enclen = length; 00958 } 00959 00960 /* encode length number of bytes from inbuf */ 00961 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 00962 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00963 { 00964 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 00965 } 00966 else 00967 { 00968 fct_chk( outlen == enclen ); 00969 } 00970 00971 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 00972 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00973 { 00974 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 00975 } 00976 else 00977 { 00978 fct_chk( outlen == 0 ); 00979 } 00980 00981 00982 /* decode the previously encoded string */ 00983 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 00984 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00985 { 00986 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 00987 } 00988 else 00989 { 00990 fct_chk( enclen == outlen ); 00991 } 00992 00993 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 00994 if( POLARSSL_MODE_CBC == cipher_info->mode ) 00995 { 00996 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 00997 } 00998 else 00999 { 01000 fct_chk( outlen == 0 ); 01001 } 01002 01003 01004 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 01005 01006 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 01007 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 01008 FCT_TEST_END(); 01009 #endif /* POLARSSL_AES_C */ 01010 01011 #ifdef POLARSSL_AES_C 01012 01013 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes) 01014 size_t length = 16; 01015 unsigned char key[32]; 01016 unsigned char iv[16]; 01017 01018 const cipher_info_t *cipher_info; 01019 cipher_context_t ctx_dec; 01020 cipher_context_t ctx_enc; 01021 01022 unsigned char inbuf[64]; 01023 unsigned char encbuf[64]; 01024 unsigned char decbuf[64]; 01025 01026 size_t outlen = 0; 01027 size_t enclen = 0; 01028 01029 memset( key, 0, 32 ); 01030 memset( iv , 0, 16 ); 01031 01032 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 01033 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 01034 01035 memset( inbuf, 5, 64 ); 01036 memset( encbuf, 0, 64 ); 01037 memset( decbuf, 0, 64 ); 01038 01039 /* Check and get info structures */ 01040 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 01041 fct_chk( NULL != cipher_info ); 01042 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 01043 01044 /* Initialise enc and dec contexts */ 01045 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 01046 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 01047 01048 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 01049 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 01050 01051 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 01052 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 01053 01054 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01055 { 01056 enclen = cipher_get_block_size( &ctx_enc ) 01057 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 01058 } 01059 else 01060 { 01061 enclen = length; 01062 } 01063 01064 /* encode length number of bytes from inbuf */ 01065 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 01066 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01067 { 01068 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 01069 } 01070 else 01071 { 01072 fct_chk( outlen == enclen ); 01073 } 01074 01075 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 01076 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01077 { 01078 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 01079 } 01080 else 01081 { 01082 fct_chk( outlen == 0 ); 01083 } 01084 01085 01086 /* decode the previously encoded string */ 01087 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 01088 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01089 { 01090 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 01091 } 01092 else 01093 { 01094 fct_chk( enclen == outlen ); 01095 } 01096 01097 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 01098 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01099 { 01100 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 01101 } 01102 else 01103 { 01104 fct_chk( outlen == 0 ); 01105 } 01106 01107 01108 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 01109 01110 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 01111 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 01112 FCT_TEST_END(); 01113 #endif /* POLARSSL_AES_C */ 01114 01115 #ifdef POLARSSL_AES_C 01116 01117 FCT_TEST_BGN(aes_encrypt_and_decrypt_17_bytes) 01118 size_t length = 17; 01119 unsigned char key[32]; 01120 unsigned char iv[16]; 01121 01122 const cipher_info_t *cipher_info; 01123 cipher_context_t ctx_dec; 01124 cipher_context_t ctx_enc; 01125 01126 unsigned char inbuf[64]; 01127 unsigned char encbuf[64]; 01128 unsigned char decbuf[64]; 01129 01130 size_t outlen = 0; 01131 size_t enclen = 0; 01132 01133 memset( key, 0, 32 ); 01134 memset( iv , 0, 16 ); 01135 01136 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 01137 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 01138 01139 memset( inbuf, 5, 64 ); 01140 memset( encbuf, 0, 64 ); 01141 memset( decbuf, 0, 64 ); 01142 01143 /* Check and get info structures */ 01144 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 01145 fct_chk( NULL != cipher_info ); 01146 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 01147 01148 /* Initialise enc and dec contexts */ 01149 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 01150 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 01151 01152 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 01153 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 01154 01155 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 01156 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 01157 01158 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01159 { 01160 enclen = cipher_get_block_size( &ctx_enc ) 01161 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 01162 } 01163 else 01164 { 01165 enclen = length; 01166 } 01167 01168 /* encode length number of bytes from inbuf */ 01169 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 01170 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01171 { 01172 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 01173 } 01174 else 01175 { 01176 fct_chk( outlen == enclen ); 01177 } 01178 01179 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 01180 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01181 { 01182 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 01183 } 01184 else 01185 { 01186 fct_chk( outlen == 0 ); 01187 } 01188 01189 01190 /* decode the previously encoded string */ 01191 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 01192 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01193 { 01194 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 01195 } 01196 else 01197 { 01198 fct_chk( enclen == outlen ); 01199 } 01200 01201 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 01202 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01203 { 01204 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 01205 } 01206 else 01207 { 01208 fct_chk( outlen == 0 ); 01209 } 01210 01211 01212 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 01213 01214 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 01215 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 01216 FCT_TEST_END(); 01217 #endif /* POLARSSL_AES_C */ 01218 01219 #ifdef POLARSSL_AES_C 01220 01221 FCT_TEST_BGN(aes_encrypt_and_decrypt_31_bytes) 01222 size_t length = 31; 01223 unsigned char key[32]; 01224 unsigned char iv[16]; 01225 01226 const cipher_info_t *cipher_info; 01227 cipher_context_t ctx_dec; 01228 cipher_context_t ctx_enc; 01229 01230 unsigned char inbuf[64]; 01231 unsigned char encbuf[64]; 01232 unsigned char decbuf[64]; 01233 01234 size_t outlen = 0; 01235 size_t enclen = 0; 01236 01237 memset( key, 0, 32 ); 01238 memset( iv , 0, 16 ); 01239 01240 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 01241 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 01242 01243 memset( inbuf, 5, 64 ); 01244 memset( encbuf, 0, 64 ); 01245 memset( decbuf, 0, 64 ); 01246 01247 /* Check and get info structures */ 01248 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 01249 fct_chk( NULL != cipher_info ); 01250 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 01251 01252 /* Initialise enc and dec contexts */ 01253 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 01254 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 01255 01256 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 01257 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 01258 01259 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 01260 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 01261 01262 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01263 { 01264 enclen = cipher_get_block_size( &ctx_enc ) 01265 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 01266 } 01267 else 01268 { 01269 enclen = length; 01270 } 01271 01272 /* encode length number of bytes from inbuf */ 01273 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 01274 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01275 { 01276 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 01277 } 01278 else 01279 { 01280 fct_chk( outlen == enclen ); 01281 } 01282 01283 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 01284 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01285 { 01286 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 01287 } 01288 else 01289 { 01290 fct_chk( outlen == 0 ); 01291 } 01292 01293 01294 /* decode the previously encoded string */ 01295 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 01296 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01297 { 01298 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 01299 } 01300 else 01301 { 01302 fct_chk( enclen == outlen ); 01303 } 01304 01305 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 01306 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01307 { 01308 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 01309 } 01310 else 01311 { 01312 fct_chk( outlen == 0 ); 01313 } 01314 01315 01316 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 01317 01318 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 01319 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 01320 FCT_TEST_END(); 01321 #endif /* POLARSSL_AES_C */ 01322 01323 #ifdef POLARSSL_AES_C 01324 01325 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes) 01326 size_t length = 32; 01327 unsigned char key[32]; 01328 unsigned char iv[16]; 01329 01330 const cipher_info_t *cipher_info; 01331 cipher_context_t ctx_dec; 01332 cipher_context_t ctx_enc; 01333 01334 unsigned char inbuf[64]; 01335 unsigned char encbuf[64]; 01336 unsigned char decbuf[64]; 01337 01338 size_t outlen = 0; 01339 size_t enclen = 0; 01340 01341 memset( key, 0, 32 ); 01342 memset( iv , 0, 16 ); 01343 01344 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 01345 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 01346 01347 memset( inbuf, 5, 64 ); 01348 memset( encbuf, 0, 64 ); 01349 memset( decbuf, 0, 64 ); 01350 01351 /* Check and get info structures */ 01352 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 01353 fct_chk( NULL != cipher_info ); 01354 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 01355 01356 /* Initialise enc and dec contexts */ 01357 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 01358 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 01359 01360 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 01361 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 01362 01363 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 01364 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 01365 01366 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01367 { 01368 enclen = cipher_get_block_size( &ctx_enc ) 01369 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 01370 } 01371 else 01372 { 01373 enclen = length; 01374 } 01375 01376 /* encode length number of bytes from inbuf */ 01377 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 01378 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01379 { 01380 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 01381 } 01382 else 01383 { 01384 fct_chk( outlen == enclen ); 01385 } 01386 01387 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 01388 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01389 { 01390 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 01391 } 01392 else 01393 { 01394 fct_chk( outlen == 0 ); 01395 } 01396 01397 01398 /* decode the previously encoded string */ 01399 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 01400 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01401 { 01402 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 01403 } 01404 else 01405 { 01406 fct_chk( enclen == outlen ); 01407 } 01408 01409 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 01410 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01411 { 01412 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 01413 } 01414 else 01415 { 01416 fct_chk( outlen == 0 ); 01417 } 01418 01419 01420 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 01421 01422 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 01423 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 01424 FCT_TEST_END(); 01425 #endif /* POLARSSL_AES_C */ 01426 01427 #ifdef POLARSSL_AES_C 01428 01429 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes) 01430 size_t length = 33; 01431 unsigned char key[32]; 01432 unsigned char iv[16]; 01433 01434 const cipher_info_t *cipher_info; 01435 cipher_context_t ctx_dec; 01436 cipher_context_t ctx_enc; 01437 01438 unsigned char inbuf[64]; 01439 unsigned char encbuf[64]; 01440 unsigned char decbuf[64]; 01441 01442 size_t outlen = 0; 01443 size_t enclen = 0; 01444 01445 memset( key, 0, 32 ); 01446 memset( iv , 0, 16 ); 01447 01448 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 01449 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 01450 01451 memset( inbuf, 5, 64 ); 01452 memset( encbuf, 0, 64 ); 01453 memset( decbuf, 0, 64 ); 01454 01455 /* Check and get info structures */ 01456 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 01457 fct_chk( NULL != cipher_info ); 01458 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 01459 01460 /* Initialise enc and dec contexts */ 01461 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 01462 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 01463 01464 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 01465 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 01466 01467 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 01468 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 01469 01470 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01471 { 01472 enclen = cipher_get_block_size( &ctx_enc ) 01473 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 01474 } 01475 else 01476 { 01477 enclen = length; 01478 } 01479 01480 /* encode length number of bytes from inbuf */ 01481 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 01482 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01483 { 01484 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 01485 } 01486 else 01487 { 01488 fct_chk( outlen == enclen ); 01489 } 01490 01491 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 01492 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01493 { 01494 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 01495 } 01496 else 01497 { 01498 fct_chk( outlen == 0 ); 01499 } 01500 01501 01502 /* decode the previously encoded string */ 01503 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 01504 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01505 { 01506 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 01507 } 01508 else 01509 { 01510 fct_chk( enclen == outlen ); 01511 } 01512 01513 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 01514 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01515 { 01516 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 01517 } 01518 else 01519 { 01520 fct_chk( outlen == 0 ); 01521 } 01522 01523 01524 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 01525 01526 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 01527 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 01528 FCT_TEST_END(); 01529 #endif /* POLARSSL_AES_C */ 01530 01531 #ifdef POLARSSL_AES_C 01532 01533 FCT_TEST_BGN(aes_encrypt_and_decrypt_47_bytes) 01534 size_t length = 47; 01535 unsigned char key[32]; 01536 unsigned char iv[16]; 01537 01538 const cipher_info_t *cipher_info; 01539 cipher_context_t ctx_dec; 01540 cipher_context_t ctx_enc; 01541 01542 unsigned char inbuf[64]; 01543 unsigned char encbuf[64]; 01544 unsigned char decbuf[64]; 01545 01546 size_t outlen = 0; 01547 size_t enclen = 0; 01548 01549 memset( key, 0, 32 ); 01550 memset( iv , 0, 16 ); 01551 01552 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 01553 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 01554 01555 memset( inbuf, 5, 64 ); 01556 memset( encbuf, 0, 64 ); 01557 memset( decbuf, 0, 64 ); 01558 01559 /* Check and get info structures */ 01560 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 01561 fct_chk( NULL != cipher_info ); 01562 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 01563 01564 /* Initialise enc and dec contexts */ 01565 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 01566 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 01567 01568 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 01569 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 01570 01571 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 01572 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 01573 01574 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01575 { 01576 enclen = cipher_get_block_size( &ctx_enc ) 01577 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 01578 } 01579 else 01580 { 01581 enclen = length; 01582 } 01583 01584 /* encode length number of bytes from inbuf */ 01585 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 01586 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01587 { 01588 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 01589 } 01590 else 01591 { 01592 fct_chk( outlen == enclen ); 01593 } 01594 01595 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 01596 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01597 { 01598 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 01599 } 01600 else 01601 { 01602 fct_chk( outlen == 0 ); 01603 } 01604 01605 01606 /* decode the previously encoded string */ 01607 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 01608 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01609 { 01610 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 01611 } 01612 else 01613 { 01614 fct_chk( enclen == outlen ); 01615 } 01616 01617 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 01618 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01619 { 01620 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 01621 } 01622 else 01623 { 01624 fct_chk( outlen == 0 ); 01625 } 01626 01627 01628 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 01629 01630 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 01631 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 01632 FCT_TEST_END(); 01633 #endif /* POLARSSL_AES_C */ 01634 01635 #ifdef POLARSSL_AES_C 01636 01637 FCT_TEST_BGN(aes_encrypt_and_decrypt_48_bytes) 01638 size_t length = 48; 01639 unsigned char key[32]; 01640 unsigned char iv[16]; 01641 01642 const cipher_info_t *cipher_info; 01643 cipher_context_t ctx_dec; 01644 cipher_context_t ctx_enc; 01645 01646 unsigned char inbuf[64]; 01647 unsigned char encbuf[64]; 01648 unsigned char decbuf[64]; 01649 01650 size_t outlen = 0; 01651 size_t enclen = 0; 01652 01653 memset( key, 0, 32 ); 01654 memset( iv , 0, 16 ); 01655 01656 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 01657 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 01658 01659 memset( inbuf, 5, 64 ); 01660 memset( encbuf, 0, 64 ); 01661 memset( decbuf, 0, 64 ); 01662 01663 /* Check and get info structures */ 01664 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 01665 fct_chk( NULL != cipher_info ); 01666 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 01667 01668 /* Initialise enc and dec contexts */ 01669 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 01670 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 01671 01672 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 01673 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 01674 01675 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 01676 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 01677 01678 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01679 { 01680 enclen = cipher_get_block_size( &ctx_enc ) 01681 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 01682 } 01683 else 01684 { 01685 enclen = length; 01686 } 01687 01688 /* encode length number of bytes from inbuf */ 01689 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 01690 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01691 { 01692 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 01693 } 01694 else 01695 { 01696 fct_chk( outlen == enclen ); 01697 } 01698 01699 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 01700 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01701 { 01702 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 01703 } 01704 else 01705 { 01706 fct_chk( outlen == 0 ); 01707 } 01708 01709 01710 /* decode the previously encoded string */ 01711 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 01712 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01713 { 01714 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 01715 } 01716 else 01717 { 01718 fct_chk( enclen == outlen ); 01719 } 01720 01721 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 01722 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01723 { 01724 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 01725 } 01726 else 01727 { 01728 fct_chk( outlen == 0 ); 01729 } 01730 01731 01732 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 01733 01734 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 01735 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 01736 FCT_TEST_END(); 01737 #endif /* POLARSSL_AES_C */ 01738 01739 #ifdef POLARSSL_AES_C 01740 01741 FCT_TEST_BGN(aes_encrypt_and_decrypt_49_bytes) 01742 size_t length = 49; 01743 unsigned char key[32]; 01744 unsigned char iv[16]; 01745 01746 const cipher_info_t *cipher_info; 01747 cipher_context_t ctx_dec; 01748 cipher_context_t ctx_enc; 01749 01750 unsigned char inbuf[64]; 01751 unsigned char encbuf[64]; 01752 unsigned char decbuf[64]; 01753 01754 size_t outlen = 0; 01755 size_t enclen = 0; 01756 01757 memset( key, 0, 32 ); 01758 memset( iv , 0, 16 ); 01759 01760 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 01761 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 01762 01763 memset( inbuf, 5, 64 ); 01764 memset( encbuf, 0, 64 ); 01765 memset( decbuf, 0, 64 ); 01766 01767 /* Check and get info structures */ 01768 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 01769 fct_chk( NULL != cipher_info ); 01770 fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info ); 01771 01772 /* Initialise enc and dec contexts */ 01773 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 01774 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 01775 01776 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 01777 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 01778 01779 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 01780 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 01781 01782 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01783 { 01784 enclen = cipher_get_block_size( &ctx_enc ) 01785 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 01786 } 01787 else 01788 { 01789 enclen = length; 01790 } 01791 01792 /* encode length number of bytes from inbuf */ 01793 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 01794 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01795 { 01796 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 01797 } 01798 else 01799 { 01800 fct_chk( outlen == enclen ); 01801 } 01802 01803 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 01804 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01805 { 01806 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 01807 } 01808 else 01809 { 01810 fct_chk( outlen == 0 ); 01811 } 01812 01813 01814 /* decode the previously encoded string */ 01815 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 01816 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01817 { 01818 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 01819 } 01820 else 01821 { 01822 fct_chk( enclen == outlen ); 01823 } 01824 01825 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 01826 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01827 { 01828 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 01829 } 01830 else 01831 { 01832 fct_chk( outlen == 0 ); 01833 } 01834 01835 01836 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 01837 01838 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 01839 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 01840 FCT_TEST_END(); 01841 #endif /* POLARSSL_AES_C */ 01842 01843 #ifdef POLARSSL_AES_C 01844 01845 FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes_in_multiple_parts) 01846 size_t first_length = 0; 01847 size_t second_length = 0; 01848 size_t length = first_length + second_length; 01849 unsigned char key[32]; 01850 unsigned char iv[16]; 01851 01852 cipher_context_t ctx_dec; 01853 cipher_context_t ctx_enc; 01854 const cipher_info_t *cipher_info; 01855 01856 unsigned char inbuf[64]; 01857 unsigned char encbuf[64]; 01858 unsigned char decbuf[64]; 01859 01860 size_t outlen = 0; 01861 size_t totaloutlen = 0; 01862 size_t enclen = 0; 01863 01864 memset( key, 0, 32 ); 01865 memset( iv , 0, 16 ); 01866 01867 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 01868 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 01869 01870 memset( inbuf, 5, 64 ); 01871 memset( encbuf, 0, 64 ); 01872 memset( decbuf, 0, 64 ); 01873 01874 /* Initialise enc and dec contexts */ 01875 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 01876 fct_chk( NULL != cipher_info); 01877 01878 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 01879 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 01880 01881 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 01882 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 01883 01884 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 01885 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 01886 01887 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01888 { 01889 enclen = cipher_get_block_size(&ctx_enc ) 01890 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 01891 } 01892 else 01893 { 01894 enclen = length; 01895 } 01896 01897 /* encode length number of bytes from inbuf */ 01898 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 01899 totaloutlen = outlen; 01900 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 01901 totaloutlen += outlen; 01902 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01903 { 01904 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 01905 } 01906 else 01907 { 01908 fct_chk( totaloutlen == enclen ); 01909 } 01910 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 01911 totaloutlen += outlen; 01912 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01913 { 01914 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 01915 } 01916 else 01917 { 01918 fct_chk( outlen == 0 ); 01919 } 01920 01921 /* decode the previously encoded string */ 01922 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 01923 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01924 { 01925 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 01926 } 01927 else 01928 { 01929 fct_chk( enclen == outlen ); 01930 } 01931 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 01932 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01933 { 01934 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 01935 } 01936 else 01937 { 01938 fct_chk( outlen == 0 ); 01939 } 01940 01941 01942 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 01943 01944 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 01945 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 01946 FCT_TEST_END(); 01947 #endif /* POLARSSL_AES_C */ 01948 01949 #ifdef POLARSSL_AES_C 01950 01951 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_1) 01952 size_t first_length = 1; 01953 size_t second_length = 0; 01954 size_t length = first_length + second_length; 01955 unsigned char key[32]; 01956 unsigned char iv[16]; 01957 01958 cipher_context_t ctx_dec; 01959 cipher_context_t ctx_enc; 01960 const cipher_info_t *cipher_info; 01961 01962 unsigned char inbuf[64]; 01963 unsigned char encbuf[64]; 01964 unsigned char decbuf[64]; 01965 01966 size_t outlen = 0; 01967 size_t totaloutlen = 0; 01968 size_t enclen = 0; 01969 01970 memset( key, 0, 32 ); 01971 memset( iv , 0, 16 ); 01972 01973 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 01974 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 01975 01976 memset( inbuf, 5, 64 ); 01977 memset( encbuf, 0, 64 ); 01978 memset( decbuf, 0, 64 ); 01979 01980 /* Initialise enc and dec contexts */ 01981 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 01982 fct_chk( NULL != cipher_info); 01983 01984 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 01985 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 01986 01987 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 01988 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 01989 01990 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 01991 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 01992 01993 if( POLARSSL_MODE_CBC == cipher_info->mode ) 01994 { 01995 enclen = cipher_get_block_size(&ctx_enc ) 01996 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 01997 } 01998 else 01999 { 02000 enclen = length; 02001 } 02002 02003 /* encode length number of bytes from inbuf */ 02004 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 02005 totaloutlen = outlen; 02006 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 02007 totaloutlen += outlen; 02008 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02009 { 02010 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 02011 } 02012 else 02013 { 02014 fct_chk( totaloutlen == enclen ); 02015 } 02016 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 02017 totaloutlen += outlen; 02018 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02019 { 02020 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 02021 } 02022 else 02023 { 02024 fct_chk( outlen == 0 ); 02025 } 02026 02027 /* decode the previously encoded string */ 02028 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 02029 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02030 { 02031 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 02032 } 02033 else 02034 { 02035 fct_chk( enclen == outlen ); 02036 } 02037 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 02038 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02039 { 02040 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 02041 } 02042 else 02043 { 02044 fct_chk( outlen == 0 ); 02045 } 02046 02047 02048 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 02049 02050 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 02051 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 02052 FCT_TEST_END(); 02053 #endif /* POLARSSL_AES_C */ 02054 02055 #ifdef POLARSSL_AES_C 02056 02057 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_2) 02058 size_t first_length = 0; 02059 size_t second_length = 1; 02060 size_t length = first_length + second_length; 02061 unsigned char key[32]; 02062 unsigned char iv[16]; 02063 02064 cipher_context_t ctx_dec; 02065 cipher_context_t ctx_enc; 02066 const cipher_info_t *cipher_info; 02067 02068 unsigned char inbuf[64]; 02069 unsigned char encbuf[64]; 02070 unsigned char decbuf[64]; 02071 02072 size_t outlen = 0; 02073 size_t totaloutlen = 0; 02074 size_t enclen = 0; 02075 02076 memset( key, 0, 32 ); 02077 memset( iv , 0, 16 ); 02078 02079 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 02080 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 02081 02082 memset( inbuf, 5, 64 ); 02083 memset( encbuf, 0, 64 ); 02084 memset( decbuf, 0, 64 ); 02085 02086 /* Initialise enc and dec contexts */ 02087 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 02088 fct_chk( NULL != cipher_info); 02089 02090 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 02091 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 02092 02093 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 02094 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 02095 02096 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 02097 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 02098 02099 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02100 { 02101 enclen = cipher_get_block_size(&ctx_enc ) 02102 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 02103 } 02104 else 02105 { 02106 enclen = length; 02107 } 02108 02109 /* encode length number of bytes from inbuf */ 02110 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 02111 totaloutlen = outlen; 02112 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 02113 totaloutlen += outlen; 02114 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02115 { 02116 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 02117 } 02118 else 02119 { 02120 fct_chk( totaloutlen == enclen ); 02121 } 02122 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 02123 totaloutlen += outlen; 02124 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02125 { 02126 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 02127 } 02128 else 02129 { 02130 fct_chk( outlen == 0 ); 02131 } 02132 02133 /* decode the previously encoded string */ 02134 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 02135 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02136 { 02137 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 02138 } 02139 else 02140 { 02141 fct_chk( enclen == outlen ); 02142 } 02143 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 02144 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02145 { 02146 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 02147 } 02148 else 02149 { 02150 fct_chk( outlen == 0 ); 02151 } 02152 02153 02154 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 02155 02156 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 02157 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 02158 FCT_TEST_END(); 02159 #endif /* POLARSSL_AES_C */ 02160 02161 #ifdef POLARSSL_AES_C 02162 02163 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_1) 02164 size_t first_length = 16; 02165 size_t second_length = 0; 02166 size_t length = first_length + second_length; 02167 unsigned char key[32]; 02168 unsigned char iv[16]; 02169 02170 cipher_context_t ctx_dec; 02171 cipher_context_t ctx_enc; 02172 const cipher_info_t *cipher_info; 02173 02174 unsigned char inbuf[64]; 02175 unsigned char encbuf[64]; 02176 unsigned char decbuf[64]; 02177 02178 size_t outlen = 0; 02179 size_t totaloutlen = 0; 02180 size_t enclen = 0; 02181 02182 memset( key, 0, 32 ); 02183 memset( iv , 0, 16 ); 02184 02185 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 02186 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 02187 02188 memset( inbuf, 5, 64 ); 02189 memset( encbuf, 0, 64 ); 02190 memset( decbuf, 0, 64 ); 02191 02192 /* Initialise enc and dec contexts */ 02193 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 02194 fct_chk( NULL != cipher_info); 02195 02196 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 02197 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 02198 02199 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 02200 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 02201 02202 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 02203 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 02204 02205 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02206 { 02207 enclen = cipher_get_block_size(&ctx_enc ) 02208 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 02209 } 02210 else 02211 { 02212 enclen = length; 02213 } 02214 02215 /* encode length number of bytes from inbuf */ 02216 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 02217 totaloutlen = outlen; 02218 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 02219 totaloutlen += outlen; 02220 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02221 { 02222 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 02223 } 02224 else 02225 { 02226 fct_chk( totaloutlen == enclen ); 02227 } 02228 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 02229 totaloutlen += outlen; 02230 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02231 { 02232 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 02233 } 02234 else 02235 { 02236 fct_chk( outlen == 0 ); 02237 } 02238 02239 /* decode the previously encoded string */ 02240 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 02241 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02242 { 02243 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 02244 } 02245 else 02246 { 02247 fct_chk( enclen == outlen ); 02248 } 02249 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 02250 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02251 { 02252 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 02253 } 02254 else 02255 { 02256 fct_chk( outlen == 0 ); 02257 } 02258 02259 02260 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 02261 02262 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 02263 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 02264 FCT_TEST_END(); 02265 #endif /* POLARSSL_AES_C */ 02266 02267 #ifdef POLARSSL_AES_C 02268 02269 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_2) 02270 size_t first_length = 0; 02271 size_t second_length = 16; 02272 size_t length = first_length + second_length; 02273 unsigned char key[32]; 02274 unsigned char iv[16]; 02275 02276 cipher_context_t ctx_dec; 02277 cipher_context_t ctx_enc; 02278 const cipher_info_t *cipher_info; 02279 02280 unsigned char inbuf[64]; 02281 unsigned char encbuf[64]; 02282 unsigned char decbuf[64]; 02283 02284 size_t outlen = 0; 02285 size_t totaloutlen = 0; 02286 size_t enclen = 0; 02287 02288 memset( key, 0, 32 ); 02289 memset( iv , 0, 16 ); 02290 02291 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 02292 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 02293 02294 memset( inbuf, 5, 64 ); 02295 memset( encbuf, 0, 64 ); 02296 memset( decbuf, 0, 64 ); 02297 02298 /* Initialise enc and dec contexts */ 02299 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 02300 fct_chk( NULL != cipher_info); 02301 02302 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 02303 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 02304 02305 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 02306 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 02307 02308 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 02309 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 02310 02311 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02312 { 02313 enclen = cipher_get_block_size(&ctx_enc ) 02314 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 02315 } 02316 else 02317 { 02318 enclen = length; 02319 } 02320 02321 /* encode length number of bytes from inbuf */ 02322 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 02323 totaloutlen = outlen; 02324 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 02325 totaloutlen += outlen; 02326 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02327 { 02328 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 02329 } 02330 else 02331 { 02332 fct_chk( totaloutlen == enclen ); 02333 } 02334 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 02335 totaloutlen += outlen; 02336 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02337 { 02338 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 02339 } 02340 else 02341 { 02342 fct_chk( outlen == 0 ); 02343 } 02344 02345 /* decode the previously encoded string */ 02346 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 02347 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02348 { 02349 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 02350 } 02351 else 02352 { 02353 fct_chk( enclen == outlen ); 02354 } 02355 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 02356 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02357 { 02358 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 02359 } 02360 else 02361 { 02362 fct_chk( outlen == 0 ); 02363 } 02364 02365 02366 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 02367 02368 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 02369 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 02370 FCT_TEST_END(); 02371 #endif /* POLARSSL_AES_C */ 02372 02373 #ifdef POLARSSL_AES_C 02374 02375 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_3) 02376 size_t first_length = 1; 02377 size_t second_length = 15; 02378 size_t length = first_length + second_length; 02379 unsigned char key[32]; 02380 unsigned char iv[16]; 02381 02382 cipher_context_t ctx_dec; 02383 cipher_context_t ctx_enc; 02384 const cipher_info_t *cipher_info; 02385 02386 unsigned char inbuf[64]; 02387 unsigned char encbuf[64]; 02388 unsigned char decbuf[64]; 02389 02390 size_t outlen = 0; 02391 size_t totaloutlen = 0; 02392 size_t enclen = 0; 02393 02394 memset( key, 0, 32 ); 02395 memset( iv , 0, 16 ); 02396 02397 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 02398 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 02399 02400 memset( inbuf, 5, 64 ); 02401 memset( encbuf, 0, 64 ); 02402 memset( decbuf, 0, 64 ); 02403 02404 /* Initialise enc and dec contexts */ 02405 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 02406 fct_chk( NULL != cipher_info); 02407 02408 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 02409 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 02410 02411 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 02412 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 02413 02414 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 02415 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 02416 02417 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02418 { 02419 enclen = cipher_get_block_size(&ctx_enc ) 02420 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 02421 } 02422 else 02423 { 02424 enclen = length; 02425 } 02426 02427 /* encode length number of bytes from inbuf */ 02428 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 02429 totaloutlen = outlen; 02430 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 02431 totaloutlen += outlen; 02432 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02433 { 02434 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 02435 } 02436 else 02437 { 02438 fct_chk( totaloutlen == enclen ); 02439 } 02440 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 02441 totaloutlen += outlen; 02442 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02443 { 02444 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 02445 } 02446 else 02447 { 02448 fct_chk( outlen == 0 ); 02449 } 02450 02451 /* decode the previously encoded string */ 02452 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 02453 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02454 { 02455 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 02456 } 02457 else 02458 { 02459 fct_chk( enclen == outlen ); 02460 } 02461 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 02462 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02463 { 02464 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 02465 } 02466 else 02467 { 02468 fct_chk( outlen == 0 ); 02469 } 02470 02471 02472 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 02473 02474 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 02475 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 02476 FCT_TEST_END(); 02477 #endif /* POLARSSL_AES_C */ 02478 02479 #ifdef POLARSSL_AES_C 02480 02481 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_4) 02482 size_t first_length = 15; 02483 size_t second_length = 1; 02484 size_t length = first_length + second_length; 02485 unsigned char key[32]; 02486 unsigned char iv[16]; 02487 02488 cipher_context_t ctx_dec; 02489 cipher_context_t ctx_enc; 02490 const cipher_info_t *cipher_info; 02491 02492 unsigned char inbuf[64]; 02493 unsigned char encbuf[64]; 02494 unsigned char decbuf[64]; 02495 02496 size_t outlen = 0; 02497 size_t totaloutlen = 0; 02498 size_t enclen = 0; 02499 02500 memset( key, 0, 32 ); 02501 memset( iv , 0, 16 ); 02502 02503 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 02504 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 02505 02506 memset( inbuf, 5, 64 ); 02507 memset( encbuf, 0, 64 ); 02508 memset( decbuf, 0, 64 ); 02509 02510 /* Initialise enc and dec contexts */ 02511 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 02512 fct_chk( NULL != cipher_info); 02513 02514 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 02515 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 02516 02517 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 02518 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 02519 02520 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 02521 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 02522 02523 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02524 { 02525 enclen = cipher_get_block_size(&ctx_enc ) 02526 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 02527 } 02528 else 02529 { 02530 enclen = length; 02531 } 02532 02533 /* encode length number of bytes from inbuf */ 02534 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 02535 totaloutlen = outlen; 02536 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 02537 totaloutlen += outlen; 02538 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02539 { 02540 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 02541 } 02542 else 02543 { 02544 fct_chk( totaloutlen == enclen ); 02545 } 02546 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 02547 totaloutlen += outlen; 02548 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02549 { 02550 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 02551 } 02552 else 02553 { 02554 fct_chk( outlen == 0 ); 02555 } 02556 02557 /* decode the previously encoded string */ 02558 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 02559 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02560 { 02561 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 02562 } 02563 else 02564 { 02565 fct_chk( enclen == outlen ); 02566 } 02567 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 02568 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02569 { 02570 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 02571 } 02572 else 02573 { 02574 fct_chk( outlen == 0 ); 02575 } 02576 02577 02578 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 02579 02580 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 02581 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 02582 FCT_TEST_END(); 02583 #endif /* POLARSSL_AES_C */ 02584 02585 #ifdef POLARSSL_AES_C 02586 02587 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 02588 size_t first_length = 15; 02589 size_t second_length = 7; 02590 size_t length = first_length + second_length; 02591 unsigned char key[32]; 02592 unsigned char iv[16]; 02593 02594 cipher_context_t ctx_dec; 02595 cipher_context_t ctx_enc; 02596 const cipher_info_t *cipher_info; 02597 02598 unsigned char inbuf[64]; 02599 unsigned char encbuf[64]; 02600 unsigned char decbuf[64]; 02601 02602 size_t outlen = 0; 02603 size_t totaloutlen = 0; 02604 size_t enclen = 0; 02605 02606 memset( key, 0, 32 ); 02607 memset( iv , 0, 16 ); 02608 02609 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 02610 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 02611 02612 memset( inbuf, 5, 64 ); 02613 memset( encbuf, 0, 64 ); 02614 memset( decbuf, 0, 64 ); 02615 02616 /* Initialise enc and dec contexts */ 02617 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 02618 fct_chk( NULL != cipher_info); 02619 02620 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 02621 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 02622 02623 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 02624 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 02625 02626 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 02627 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 02628 02629 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02630 { 02631 enclen = cipher_get_block_size(&ctx_enc ) 02632 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 02633 } 02634 else 02635 { 02636 enclen = length; 02637 } 02638 02639 /* encode length number of bytes from inbuf */ 02640 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 02641 totaloutlen = outlen; 02642 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 02643 totaloutlen += outlen; 02644 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02645 { 02646 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 02647 } 02648 else 02649 { 02650 fct_chk( totaloutlen == enclen ); 02651 } 02652 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 02653 totaloutlen += outlen; 02654 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02655 { 02656 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 02657 } 02658 else 02659 { 02660 fct_chk( outlen == 0 ); 02661 } 02662 02663 /* decode the previously encoded string */ 02664 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 02665 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02666 { 02667 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 02668 } 02669 else 02670 { 02671 fct_chk( enclen == outlen ); 02672 } 02673 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 02674 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02675 { 02676 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 02677 } 02678 else 02679 { 02680 fct_chk( outlen == 0 ); 02681 } 02682 02683 02684 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 02685 02686 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 02687 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 02688 FCT_TEST_END(); 02689 #endif /* POLARSSL_AES_C */ 02690 02691 #ifdef POLARSSL_AES_C 02692 02693 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 02694 size_t first_length = 16; 02695 size_t second_length = 6; 02696 size_t length = first_length + second_length; 02697 unsigned char key[32]; 02698 unsigned char iv[16]; 02699 02700 cipher_context_t ctx_dec; 02701 cipher_context_t ctx_enc; 02702 const cipher_info_t *cipher_info; 02703 02704 unsigned char inbuf[64]; 02705 unsigned char encbuf[64]; 02706 unsigned char decbuf[64]; 02707 02708 size_t outlen = 0; 02709 size_t totaloutlen = 0; 02710 size_t enclen = 0; 02711 02712 memset( key, 0, 32 ); 02713 memset( iv , 0, 16 ); 02714 02715 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 02716 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 02717 02718 memset( inbuf, 5, 64 ); 02719 memset( encbuf, 0, 64 ); 02720 memset( decbuf, 0, 64 ); 02721 02722 /* Initialise enc and dec contexts */ 02723 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 02724 fct_chk( NULL != cipher_info); 02725 02726 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 02727 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 02728 02729 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 02730 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 02731 02732 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 02733 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 02734 02735 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02736 { 02737 enclen = cipher_get_block_size(&ctx_enc ) 02738 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 02739 } 02740 else 02741 { 02742 enclen = length; 02743 } 02744 02745 /* encode length number of bytes from inbuf */ 02746 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 02747 totaloutlen = outlen; 02748 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 02749 totaloutlen += outlen; 02750 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02751 { 02752 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 02753 } 02754 else 02755 { 02756 fct_chk( totaloutlen == enclen ); 02757 } 02758 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 02759 totaloutlen += outlen; 02760 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02761 { 02762 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 02763 } 02764 else 02765 { 02766 fct_chk( outlen == 0 ); 02767 } 02768 02769 /* decode the previously encoded string */ 02770 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 02771 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02772 { 02773 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 02774 } 02775 else 02776 { 02777 fct_chk( enclen == outlen ); 02778 } 02779 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 02780 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02781 { 02782 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 02783 } 02784 else 02785 { 02786 fct_chk( outlen == 0 ); 02787 } 02788 02789 02790 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 02791 02792 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 02793 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 02794 FCT_TEST_END(); 02795 #endif /* POLARSSL_AES_C */ 02796 02797 #ifdef POLARSSL_AES_C 02798 02799 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 02800 size_t first_length = 17; 02801 size_t second_length = 6; 02802 size_t length = first_length + second_length; 02803 unsigned char key[32]; 02804 unsigned char iv[16]; 02805 02806 cipher_context_t ctx_dec; 02807 cipher_context_t ctx_enc; 02808 const cipher_info_t *cipher_info; 02809 02810 unsigned char inbuf[64]; 02811 unsigned char encbuf[64]; 02812 unsigned char decbuf[64]; 02813 02814 size_t outlen = 0; 02815 size_t totaloutlen = 0; 02816 size_t enclen = 0; 02817 02818 memset( key, 0, 32 ); 02819 memset( iv , 0, 16 ); 02820 02821 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 02822 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 02823 02824 memset( inbuf, 5, 64 ); 02825 memset( encbuf, 0, 64 ); 02826 memset( decbuf, 0, 64 ); 02827 02828 /* Initialise enc and dec contexts */ 02829 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 02830 fct_chk( NULL != cipher_info); 02831 02832 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 02833 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 02834 02835 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 02836 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 02837 02838 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 02839 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 02840 02841 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02842 { 02843 enclen = cipher_get_block_size(&ctx_enc ) 02844 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 02845 } 02846 else 02847 { 02848 enclen = length; 02849 } 02850 02851 /* encode length number of bytes from inbuf */ 02852 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 02853 totaloutlen = outlen; 02854 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 02855 totaloutlen += outlen; 02856 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02857 { 02858 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 02859 } 02860 else 02861 { 02862 fct_chk( totaloutlen == enclen ); 02863 } 02864 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 02865 totaloutlen += outlen; 02866 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02867 { 02868 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 02869 } 02870 else 02871 { 02872 fct_chk( outlen == 0 ); 02873 } 02874 02875 /* decode the previously encoded string */ 02876 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 02877 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02878 { 02879 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 02880 } 02881 else 02882 { 02883 fct_chk( enclen == outlen ); 02884 } 02885 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 02886 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02887 { 02888 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 02889 } 02890 else 02891 { 02892 fct_chk( outlen == 0 ); 02893 } 02894 02895 02896 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 02897 02898 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 02899 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 02900 FCT_TEST_END(); 02901 #endif /* POLARSSL_AES_C */ 02902 02903 #ifdef POLARSSL_AES_C 02904 02905 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes_in_multiple_parts_1) 02906 size_t first_length = 16; 02907 size_t second_length = 16; 02908 size_t length = first_length + second_length; 02909 unsigned char key[32]; 02910 unsigned char iv[16]; 02911 02912 cipher_context_t ctx_dec; 02913 cipher_context_t ctx_enc; 02914 const cipher_info_t *cipher_info; 02915 02916 unsigned char inbuf[64]; 02917 unsigned char encbuf[64]; 02918 unsigned char decbuf[64]; 02919 02920 size_t outlen = 0; 02921 size_t totaloutlen = 0; 02922 size_t enclen = 0; 02923 02924 memset( key, 0, 32 ); 02925 memset( iv , 0, 16 ); 02926 02927 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 02928 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 02929 02930 memset( inbuf, 5, 64 ); 02931 memset( encbuf, 0, 64 ); 02932 memset( decbuf, 0, 64 ); 02933 02934 /* Initialise enc and dec contexts */ 02935 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); 02936 fct_chk( NULL != cipher_info); 02937 02938 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 02939 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 02940 02941 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 02942 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 02943 02944 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 02945 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 02946 02947 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02948 { 02949 enclen = cipher_get_block_size(&ctx_enc ) 02950 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 02951 } 02952 else 02953 { 02954 enclen = length; 02955 } 02956 02957 /* encode length number of bytes from inbuf */ 02958 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 02959 totaloutlen = outlen; 02960 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 02961 totaloutlen += outlen; 02962 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02963 { 02964 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 02965 } 02966 else 02967 { 02968 fct_chk( totaloutlen == enclen ); 02969 } 02970 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 02971 totaloutlen += outlen; 02972 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02973 { 02974 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 02975 } 02976 else 02977 { 02978 fct_chk( outlen == 0 ); 02979 } 02980 02981 /* decode the previously encoded string */ 02982 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 02983 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02984 { 02985 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 02986 } 02987 else 02988 { 02989 fct_chk( enclen == outlen ); 02990 } 02991 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 02992 if( POLARSSL_MODE_CBC == cipher_info->mode ) 02993 { 02994 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 02995 } 02996 else 02997 { 02998 fct_chk( outlen == 0 ); 02999 } 03000 03001 03002 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03003 03004 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03005 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03006 FCT_TEST_END(); 03007 #endif /* POLARSSL_AES_C */ 03008 03009 #ifdef POLARSSL_AES_C 03010 #ifdef POLARSSL_CIPHER_MODE_CFB 03011 03012 FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes) 03013 size_t length = 0; 03014 unsigned char key[32]; 03015 unsigned char iv[16]; 03016 03017 const cipher_info_t *cipher_info; 03018 cipher_context_t ctx_dec; 03019 cipher_context_t ctx_enc; 03020 03021 unsigned char inbuf[64]; 03022 unsigned char encbuf[64]; 03023 unsigned char decbuf[64]; 03024 03025 size_t outlen = 0; 03026 size_t enclen = 0; 03027 03028 memset( key, 0, 32 ); 03029 memset( iv , 0, 16 ); 03030 03031 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03032 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03033 03034 memset( inbuf, 5, 64 ); 03035 memset( encbuf, 0, 64 ); 03036 memset( decbuf, 0, 64 ); 03037 03038 /* Check and get info structures */ 03039 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 03040 fct_chk( NULL != cipher_info ); 03041 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 03042 03043 /* Initialise enc and dec contexts */ 03044 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03045 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03046 03047 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 03048 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 03049 03050 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03051 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03052 03053 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03054 { 03055 enclen = cipher_get_block_size( &ctx_enc ) 03056 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03057 } 03058 else 03059 { 03060 enclen = length; 03061 } 03062 03063 /* encode length number of bytes from inbuf */ 03064 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03065 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03066 { 03067 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03068 } 03069 else 03070 { 03071 fct_chk( outlen == enclen ); 03072 } 03073 03074 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03075 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03076 { 03077 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03078 } 03079 else 03080 { 03081 fct_chk( outlen == 0 ); 03082 } 03083 03084 03085 /* decode the previously encoded string */ 03086 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03087 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03088 { 03089 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03090 } 03091 else 03092 { 03093 fct_chk( enclen == outlen ); 03094 } 03095 03096 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03097 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03098 { 03099 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03100 } 03101 else 03102 { 03103 fct_chk( outlen == 0 ); 03104 } 03105 03106 03107 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03108 03109 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03110 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03111 FCT_TEST_END(); 03112 #endif /* POLARSSL_AES_C */ 03113 #endif /* POLARSSL_CIPHER_MODE_CFB */ 03114 03115 #ifdef POLARSSL_AES_C 03116 #ifdef POLARSSL_CIPHER_MODE_CFB 03117 03118 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_byte) 03119 size_t length = 1; 03120 unsigned char key[32]; 03121 unsigned char iv[16]; 03122 03123 const cipher_info_t *cipher_info; 03124 cipher_context_t ctx_dec; 03125 cipher_context_t ctx_enc; 03126 03127 unsigned char inbuf[64]; 03128 unsigned char encbuf[64]; 03129 unsigned char decbuf[64]; 03130 03131 size_t outlen = 0; 03132 size_t enclen = 0; 03133 03134 memset( key, 0, 32 ); 03135 memset( iv , 0, 16 ); 03136 03137 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03138 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03139 03140 memset( inbuf, 5, 64 ); 03141 memset( encbuf, 0, 64 ); 03142 memset( decbuf, 0, 64 ); 03143 03144 /* Check and get info structures */ 03145 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 03146 fct_chk( NULL != cipher_info ); 03147 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 03148 03149 /* Initialise enc and dec contexts */ 03150 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03151 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03152 03153 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 03154 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 03155 03156 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03157 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03158 03159 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03160 { 03161 enclen = cipher_get_block_size( &ctx_enc ) 03162 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03163 } 03164 else 03165 { 03166 enclen = length; 03167 } 03168 03169 /* encode length number of bytes from inbuf */ 03170 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03171 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03172 { 03173 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03174 } 03175 else 03176 { 03177 fct_chk( outlen == enclen ); 03178 } 03179 03180 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03181 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03182 { 03183 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03184 } 03185 else 03186 { 03187 fct_chk( outlen == 0 ); 03188 } 03189 03190 03191 /* decode the previously encoded string */ 03192 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03193 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03194 { 03195 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03196 } 03197 else 03198 { 03199 fct_chk( enclen == outlen ); 03200 } 03201 03202 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03203 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03204 { 03205 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03206 } 03207 else 03208 { 03209 fct_chk( outlen == 0 ); 03210 } 03211 03212 03213 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03214 03215 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03216 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03217 FCT_TEST_END(); 03218 #endif /* POLARSSL_AES_C */ 03219 #endif /* POLARSSL_CIPHER_MODE_CFB */ 03220 03221 #ifdef POLARSSL_AES_C 03222 #ifdef POLARSSL_CIPHER_MODE_CFB 03223 03224 FCT_TEST_BGN(aes_encrypt_and_decrypt_2_bytes) 03225 size_t length = 2; 03226 unsigned char key[32]; 03227 unsigned char iv[16]; 03228 03229 const cipher_info_t *cipher_info; 03230 cipher_context_t ctx_dec; 03231 cipher_context_t ctx_enc; 03232 03233 unsigned char inbuf[64]; 03234 unsigned char encbuf[64]; 03235 unsigned char decbuf[64]; 03236 03237 size_t outlen = 0; 03238 size_t enclen = 0; 03239 03240 memset( key, 0, 32 ); 03241 memset( iv , 0, 16 ); 03242 03243 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03244 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03245 03246 memset( inbuf, 5, 64 ); 03247 memset( encbuf, 0, 64 ); 03248 memset( decbuf, 0, 64 ); 03249 03250 /* Check and get info structures */ 03251 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 03252 fct_chk( NULL != cipher_info ); 03253 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 03254 03255 /* Initialise enc and dec contexts */ 03256 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03257 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03258 03259 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 03260 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 03261 03262 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03263 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03264 03265 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03266 { 03267 enclen = cipher_get_block_size( &ctx_enc ) 03268 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03269 } 03270 else 03271 { 03272 enclen = length; 03273 } 03274 03275 /* encode length number of bytes from inbuf */ 03276 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03277 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03278 { 03279 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03280 } 03281 else 03282 { 03283 fct_chk( outlen == enclen ); 03284 } 03285 03286 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03287 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03288 { 03289 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03290 } 03291 else 03292 { 03293 fct_chk( outlen == 0 ); 03294 } 03295 03296 03297 /* decode the previously encoded string */ 03298 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03299 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03300 { 03301 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03302 } 03303 else 03304 { 03305 fct_chk( enclen == outlen ); 03306 } 03307 03308 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03309 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03310 { 03311 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03312 } 03313 else 03314 { 03315 fct_chk( outlen == 0 ); 03316 } 03317 03318 03319 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03320 03321 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03322 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03323 FCT_TEST_END(); 03324 #endif /* POLARSSL_AES_C */ 03325 #endif /* POLARSSL_CIPHER_MODE_CFB */ 03326 03327 #ifdef POLARSSL_AES_C 03328 #ifdef POLARSSL_CIPHER_MODE_CFB 03329 03330 FCT_TEST_BGN(aes_encrypt_and_decrypt_7_bytes) 03331 size_t length = 7; 03332 unsigned char key[32]; 03333 unsigned char iv[16]; 03334 03335 const cipher_info_t *cipher_info; 03336 cipher_context_t ctx_dec; 03337 cipher_context_t ctx_enc; 03338 03339 unsigned char inbuf[64]; 03340 unsigned char encbuf[64]; 03341 unsigned char decbuf[64]; 03342 03343 size_t outlen = 0; 03344 size_t enclen = 0; 03345 03346 memset( key, 0, 32 ); 03347 memset( iv , 0, 16 ); 03348 03349 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03350 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03351 03352 memset( inbuf, 5, 64 ); 03353 memset( encbuf, 0, 64 ); 03354 memset( decbuf, 0, 64 ); 03355 03356 /* Check and get info structures */ 03357 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 03358 fct_chk( NULL != cipher_info ); 03359 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 03360 03361 /* Initialise enc and dec contexts */ 03362 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03363 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03364 03365 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 03366 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 03367 03368 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03369 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03370 03371 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03372 { 03373 enclen = cipher_get_block_size( &ctx_enc ) 03374 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03375 } 03376 else 03377 { 03378 enclen = length; 03379 } 03380 03381 /* encode length number of bytes from inbuf */ 03382 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03383 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03384 { 03385 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03386 } 03387 else 03388 { 03389 fct_chk( outlen == enclen ); 03390 } 03391 03392 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03393 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03394 { 03395 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03396 } 03397 else 03398 { 03399 fct_chk( outlen == 0 ); 03400 } 03401 03402 03403 /* decode the previously encoded string */ 03404 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03405 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03406 { 03407 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03408 } 03409 else 03410 { 03411 fct_chk( enclen == outlen ); 03412 } 03413 03414 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03415 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03416 { 03417 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03418 } 03419 else 03420 { 03421 fct_chk( outlen == 0 ); 03422 } 03423 03424 03425 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03426 03427 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03428 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03429 FCT_TEST_END(); 03430 #endif /* POLARSSL_AES_C */ 03431 #endif /* POLARSSL_CIPHER_MODE_CFB */ 03432 03433 #ifdef POLARSSL_AES_C 03434 #ifdef POLARSSL_CIPHER_MODE_CFB 03435 03436 FCT_TEST_BGN(aes_encrypt_and_decrypt_8_bytes) 03437 size_t length = 8; 03438 unsigned char key[32]; 03439 unsigned char iv[16]; 03440 03441 const cipher_info_t *cipher_info; 03442 cipher_context_t ctx_dec; 03443 cipher_context_t ctx_enc; 03444 03445 unsigned char inbuf[64]; 03446 unsigned char encbuf[64]; 03447 unsigned char decbuf[64]; 03448 03449 size_t outlen = 0; 03450 size_t enclen = 0; 03451 03452 memset( key, 0, 32 ); 03453 memset( iv , 0, 16 ); 03454 03455 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03456 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03457 03458 memset( inbuf, 5, 64 ); 03459 memset( encbuf, 0, 64 ); 03460 memset( decbuf, 0, 64 ); 03461 03462 /* Check and get info structures */ 03463 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 03464 fct_chk( NULL != cipher_info ); 03465 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 03466 03467 /* Initialise enc and dec contexts */ 03468 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03469 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03470 03471 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 03472 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 03473 03474 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03475 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03476 03477 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03478 { 03479 enclen = cipher_get_block_size( &ctx_enc ) 03480 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03481 } 03482 else 03483 { 03484 enclen = length; 03485 } 03486 03487 /* encode length number of bytes from inbuf */ 03488 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03489 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03490 { 03491 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03492 } 03493 else 03494 { 03495 fct_chk( outlen == enclen ); 03496 } 03497 03498 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03499 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03500 { 03501 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03502 } 03503 else 03504 { 03505 fct_chk( outlen == 0 ); 03506 } 03507 03508 03509 /* decode the previously encoded string */ 03510 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03511 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03512 { 03513 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03514 } 03515 else 03516 { 03517 fct_chk( enclen == outlen ); 03518 } 03519 03520 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03521 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03522 { 03523 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03524 } 03525 else 03526 { 03527 fct_chk( outlen == 0 ); 03528 } 03529 03530 03531 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03532 03533 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03534 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03535 FCT_TEST_END(); 03536 #endif /* POLARSSL_AES_C */ 03537 #endif /* POLARSSL_CIPHER_MODE_CFB */ 03538 03539 #ifdef POLARSSL_AES_C 03540 #ifdef POLARSSL_CIPHER_MODE_CFB 03541 03542 FCT_TEST_BGN(aes_encrypt_and_decrypt_9_bytes) 03543 size_t length = 9; 03544 unsigned char key[32]; 03545 unsigned char iv[16]; 03546 03547 const cipher_info_t *cipher_info; 03548 cipher_context_t ctx_dec; 03549 cipher_context_t ctx_enc; 03550 03551 unsigned char inbuf[64]; 03552 unsigned char encbuf[64]; 03553 unsigned char decbuf[64]; 03554 03555 size_t outlen = 0; 03556 size_t enclen = 0; 03557 03558 memset( key, 0, 32 ); 03559 memset( iv , 0, 16 ); 03560 03561 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03562 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03563 03564 memset( inbuf, 5, 64 ); 03565 memset( encbuf, 0, 64 ); 03566 memset( decbuf, 0, 64 ); 03567 03568 /* Check and get info structures */ 03569 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 03570 fct_chk( NULL != cipher_info ); 03571 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 03572 03573 /* Initialise enc and dec contexts */ 03574 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03575 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03576 03577 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 03578 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 03579 03580 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03581 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03582 03583 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03584 { 03585 enclen = cipher_get_block_size( &ctx_enc ) 03586 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03587 } 03588 else 03589 { 03590 enclen = length; 03591 } 03592 03593 /* encode length number of bytes from inbuf */ 03594 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03595 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03596 { 03597 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03598 } 03599 else 03600 { 03601 fct_chk( outlen == enclen ); 03602 } 03603 03604 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03605 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03606 { 03607 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03608 } 03609 else 03610 { 03611 fct_chk( outlen == 0 ); 03612 } 03613 03614 03615 /* decode the previously encoded string */ 03616 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03617 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03618 { 03619 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03620 } 03621 else 03622 { 03623 fct_chk( enclen == outlen ); 03624 } 03625 03626 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03627 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03628 { 03629 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03630 } 03631 else 03632 { 03633 fct_chk( outlen == 0 ); 03634 } 03635 03636 03637 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03638 03639 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03640 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03641 FCT_TEST_END(); 03642 #endif /* POLARSSL_AES_C */ 03643 #endif /* POLARSSL_CIPHER_MODE_CFB */ 03644 03645 #ifdef POLARSSL_AES_C 03646 #ifdef POLARSSL_CIPHER_MODE_CFB 03647 03648 FCT_TEST_BGN(aes_encrypt_and_decrypt_15_bytes) 03649 size_t length = 15; 03650 unsigned char key[32]; 03651 unsigned char iv[16]; 03652 03653 const cipher_info_t *cipher_info; 03654 cipher_context_t ctx_dec; 03655 cipher_context_t ctx_enc; 03656 03657 unsigned char inbuf[64]; 03658 unsigned char encbuf[64]; 03659 unsigned char decbuf[64]; 03660 03661 size_t outlen = 0; 03662 size_t enclen = 0; 03663 03664 memset( key, 0, 32 ); 03665 memset( iv , 0, 16 ); 03666 03667 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03668 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03669 03670 memset( inbuf, 5, 64 ); 03671 memset( encbuf, 0, 64 ); 03672 memset( decbuf, 0, 64 ); 03673 03674 /* Check and get info structures */ 03675 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 03676 fct_chk( NULL != cipher_info ); 03677 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 03678 03679 /* Initialise enc and dec contexts */ 03680 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03681 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03682 03683 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 03684 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 03685 03686 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03687 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03688 03689 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03690 { 03691 enclen = cipher_get_block_size( &ctx_enc ) 03692 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03693 } 03694 else 03695 { 03696 enclen = length; 03697 } 03698 03699 /* encode length number of bytes from inbuf */ 03700 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03701 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03702 { 03703 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03704 } 03705 else 03706 { 03707 fct_chk( outlen == enclen ); 03708 } 03709 03710 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03711 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03712 { 03713 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03714 } 03715 else 03716 { 03717 fct_chk( outlen == 0 ); 03718 } 03719 03720 03721 /* decode the previously encoded string */ 03722 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03723 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03724 { 03725 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03726 } 03727 else 03728 { 03729 fct_chk( enclen == outlen ); 03730 } 03731 03732 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03733 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03734 { 03735 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03736 } 03737 else 03738 { 03739 fct_chk( outlen == 0 ); 03740 } 03741 03742 03743 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03744 03745 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03746 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03747 FCT_TEST_END(); 03748 #endif /* POLARSSL_AES_C */ 03749 #endif /* POLARSSL_CIPHER_MODE_CFB */ 03750 03751 #ifdef POLARSSL_AES_C 03752 #ifdef POLARSSL_CIPHER_MODE_CFB 03753 03754 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes) 03755 size_t length = 16; 03756 unsigned char key[32]; 03757 unsigned char iv[16]; 03758 03759 const cipher_info_t *cipher_info; 03760 cipher_context_t ctx_dec; 03761 cipher_context_t ctx_enc; 03762 03763 unsigned char inbuf[64]; 03764 unsigned char encbuf[64]; 03765 unsigned char decbuf[64]; 03766 03767 size_t outlen = 0; 03768 size_t enclen = 0; 03769 03770 memset( key, 0, 32 ); 03771 memset( iv , 0, 16 ); 03772 03773 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03774 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03775 03776 memset( inbuf, 5, 64 ); 03777 memset( encbuf, 0, 64 ); 03778 memset( decbuf, 0, 64 ); 03779 03780 /* Check and get info structures */ 03781 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 03782 fct_chk( NULL != cipher_info ); 03783 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 03784 03785 /* Initialise enc and dec contexts */ 03786 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03787 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03788 03789 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 03790 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 03791 03792 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03793 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03794 03795 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03796 { 03797 enclen = cipher_get_block_size( &ctx_enc ) 03798 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03799 } 03800 else 03801 { 03802 enclen = length; 03803 } 03804 03805 /* encode length number of bytes from inbuf */ 03806 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03807 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03808 { 03809 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03810 } 03811 else 03812 { 03813 fct_chk( outlen == enclen ); 03814 } 03815 03816 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03817 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03818 { 03819 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03820 } 03821 else 03822 { 03823 fct_chk( outlen == 0 ); 03824 } 03825 03826 03827 /* decode the previously encoded string */ 03828 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03829 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03830 { 03831 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03832 } 03833 else 03834 { 03835 fct_chk( enclen == outlen ); 03836 } 03837 03838 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03839 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03840 { 03841 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03842 } 03843 else 03844 { 03845 fct_chk( outlen == 0 ); 03846 } 03847 03848 03849 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03850 03851 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03852 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03853 FCT_TEST_END(); 03854 #endif /* POLARSSL_AES_C */ 03855 #endif /* POLARSSL_CIPHER_MODE_CFB */ 03856 03857 #ifdef POLARSSL_AES_C 03858 #ifdef POLARSSL_CIPHER_MODE_CFB 03859 03860 FCT_TEST_BGN(aes_encrypt_and_decrypt_17_bytes) 03861 size_t length = 17; 03862 unsigned char key[32]; 03863 unsigned char iv[16]; 03864 03865 const cipher_info_t *cipher_info; 03866 cipher_context_t ctx_dec; 03867 cipher_context_t ctx_enc; 03868 03869 unsigned char inbuf[64]; 03870 unsigned char encbuf[64]; 03871 unsigned char decbuf[64]; 03872 03873 size_t outlen = 0; 03874 size_t enclen = 0; 03875 03876 memset( key, 0, 32 ); 03877 memset( iv , 0, 16 ); 03878 03879 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03880 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03881 03882 memset( inbuf, 5, 64 ); 03883 memset( encbuf, 0, 64 ); 03884 memset( decbuf, 0, 64 ); 03885 03886 /* Check and get info structures */ 03887 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 03888 fct_chk( NULL != cipher_info ); 03889 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 03890 03891 /* Initialise enc and dec contexts */ 03892 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03893 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03894 03895 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 03896 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 03897 03898 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03899 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03900 03901 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03902 { 03903 enclen = cipher_get_block_size( &ctx_enc ) 03904 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03905 } 03906 else 03907 { 03908 enclen = length; 03909 } 03910 03911 /* encode length number of bytes from inbuf */ 03912 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03913 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03914 { 03915 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03916 } 03917 else 03918 { 03919 fct_chk( outlen == enclen ); 03920 } 03921 03922 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03923 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03924 { 03925 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03926 } 03927 else 03928 { 03929 fct_chk( outlen == 0 ); 03930 } 03931 03932 03933 /* decode the previously encoded string */ 03934 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03935 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03936 { 03937 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03938 } 03939 else 03940 { 03941 fct_chk( enclen == outlen ); 03942 } 03943 03944 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03945 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03946 { 03947 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03948 } 03949 else 03950 { 03951 fct_chk( outlen == 0 ); 03952 } 03953 03954 03955 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03956 03957 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03958 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03959 FCT_TEST_END(); 03960 #endif /* POLARSSL_AES_C */ 03961 #endif /* POLARSSL_CIPHER_MODE_CFB */ 03962 03963 #ifdef POLARSSL_AES_C 03964 #ifdef POLARSSL_CIPHER_MODE_CFB 03965 03966 FCT_TEST_BGN(aes_encrypt_and_decrypt_31_bytes) 03967 size_t length = 31; 03968 unsigned char key[32]; 03969 unsigned char iv[16]; 03970 03971 const cipher_info_t *cipher_info; 03972 cipher_context_t ctx_dec; 03973 cipher_context_t ctx_enc; 03974 03975 unsigned char inbuf[64]; 03976 unsigned char encbuf[64]; 03977 unsigned char decbuf[64]; 03978 03979 size_t outlen = 0; 03980 size_t enclen = 0; 03981 03982 memset( key, 0, 32 ); 03983 memset( iv , 0, 16 ); 03984 03985 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03986 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03987 03988 memset( inbuf, 5, 64 ); 03989 memset( encbuf, 0, 64 ); 03990 memset( decbuf, 0, 64 ); 03991 03992 /* Check and get info structures */ 03993 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 03994 fct_chk( NULL != cipher_info ); 03995 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 03996 03997 /* Initialise enc and dec contexts */ 03998 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03999 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04000 04001 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 04002 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 04003 04004 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04005 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04006 04007 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04008 { 04009 enclen = cipher_get_block_size( &ctx_enc ) 04010 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04011 } 04012 else 04013 { 04014 enclen = length; 04015 } 04016 04017 /* encode length number of bytes from inbuf */ 04018 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04019 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04020 { 04021 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04022 } 04023 else 04024 { 04025 fct_chk( outlen == enclen ); 04026 } 04027 04028 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04029 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04030 { 04031 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04032 } 04033 else 04034 { 04035 fct_chk( outlen == 0 ); 04036 } 04037 04038 04039 /* decode the previously encoded string */ 04040 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04041 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04042 { 04043 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04044 } 04045 else 04046 { 04047 fct_chk( enclen == outlen ); 04048 } 04049 04050 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04051 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04052 { 04053 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04054 } 04055 else 04056 { 04057 fct_chk( outlen == 0 ); 04058 } 04059 04060 04061 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04062 04063 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04064 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04065 FCT_TEST_END(); 04066 #endif /* POLARSSL_AES_C */ 04067 #endif /* POLARSSL_CIPHER_MODE_CFB */ 04068 04069 #ifdef POLARSSL_AES_C 04070 #ifdef POLARSSL_CIPHER_MODE_CFB 04071 04072 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes) 04073 size_t length = 32; 04074 unsigned char key[32]; 04075 unsigned char iv[16]; 04076 04077 const cipher_info_t *cipher_info; 04078 cipher_context_t ctx_dec; 04079 cipher_context_t ctx_enc; 04080 04081 unsigned char inbuf[64]; 04082 unsigned char encbuf[64]; 04083 unsigned char decbuf[64]; 04084 04085 size_t outlen = 0; 04086 size_t enclen = 0; 04087 04088 memset( key, 0, 32 ); 04089 memset( iv , 0, 16 ); 04090 04091 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04092 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04093 04094 memset( inbuf, 5, 64 ); 04095 memset( encbuf, 0, 64 ); 04096 memset( decbuf, 0, 64 ); 04097 04098 /* Check and get info structures */ 04099 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 04100 fct_chk( NULL != cipher_info ); 04101 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 04102 04103 /* Initialise enc and dec contexts */ 04104 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04105 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04106 04107 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 04108 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 04109 04110 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04111 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04112 04113 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04114 { 04115 enclen = cipher_get_block_size( &ctx_enc ) 04116 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04117 } 04118 else 04119 { 04120 enclen = length; 04121 } 04122 04123 /* encode length number of bytes from inbuf */ 04124 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04125 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04126 { 04127 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04128 } 04129 else 04130 { 04131 fct_chk( outlen == enclen ); 04132 } 04133 04134 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04135 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04136 { 04137 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04138 } 04139 else 04140 { 04141 fct_chk( outlen == 0 ); 04142 } 04143 04144 04145 /* decode the previously encoded string */ 04146 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04147 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04148 { 04149 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04150 } 04151 else 04152 { 04153 fct_chk( enclen == outlen ); 04154 } 04155 04156 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04157 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04158 { 04159 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04160 } 04161 else 04162 { 04163 fct_chk( outlen == 0 ); 04164 } 04165 04166 04167 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04168 04169 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04170 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04171 FCT_TEST_END(); 04172 #endif /* POLARSSL_AES_C */ 04173 #endif /* POLARSSL_CIPHER_MODE_CFB */ 04174 04175 #ifdef POLARSSL_AES_C 04176 #ifdef POLARSSL_CIPHER_MODE_CFB 04177 04178 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes) 04179 size_t length = 33; 04180 unsigned char key[32]; 04181 unsigned char iv[16]; 04182 04183 const cipher_info_t *cipher_info; 04184 cipher_context_t ctx_dec; 04185 cipher_context_t ctx_enc; 04186 04187 unsigned char inbuf[64]; 04188 unsigned char encbuf[64]; 04189 unsigned char decbuf[64]; 04190 04191 size_t outlen = 0; 04192 size_t enclen = 0; 04193 04194 memset( key, 0, 32 ); 04195 memset( iv , 0, 16 ); 04196 04197 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04198 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04199 04200 memset( inbuf, 5, 64 ); 04201 memset( encbuf, 0, 64 ); 04202 memset( decbuf, 0, 64 ); 04203 04204 /* Check and get info structures */ 04205 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 04206 fct_chk( NULL != cipher_info ); 04207 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 04208 04209 /* Initialise enc and dec contexts */ 04210 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04211 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04212 04213 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 04214 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 04215 04216 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04217 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04218 04219 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04220 { 04221 enclen = cipher_get_block_size( &ctx_enc ) 04222 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04223 } 04224 else 04225 { 04226 enclen = length; 04227 } 04228 04229 /* encode length number of bytes from inbuf */ 04230 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04231 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04232 { 04233 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04234 } 04235 else 04236 { 04237 fct_chk( outlen == enclen ); 04238 } 04239 04240 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04241 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04242 { 04243 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04244 } 04245 else 04246 { 04247 fct_chk( outlen == 0 ); 04248 } 04249 04250 04251 /* decode the previously encoded string */ 04252 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04253 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04254 { 04255 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04256 } 04257 else 04258 { 04259 fct_chk( enclen == outlen ); 04260 } 04261 04262 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04263 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04264 { 04265 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04266 } 04267 else 04268 { 04269 fct_chk( outlen == 0 ); 04270 } 04271 04272 04273 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04274 04275 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04276 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04277 FCT_TEST_END(); 04278 #endif /* POLARSSL_AES_C */ 04279 #endif /* POLARSSL_CIPHER_MODE_CFB */ 04280 04281 #ifdef POLARSSL_AES_C 04282 #ifdef POLARSSL_CIPHER_MODE_CFB 04283 04284 FCT_TEST_BGN(aes_encrypt_and_decrypt_47_bytes) 04285 size_t length = 47; 04286 unsigned char key[32]; 04287 unsigned char iv[16]; 04288 04289 const cipher_info_t *cipher_info; 04290 cipher_context_t ctx_dec; 04291 cipher_context_t ctx_enc; 04292 04293 unsigned char inbuf[64]; 04294 unsigned char encbuf[64]; 04295 unsigned char decbuf[64]; 04296 04297 size_t outlen = 0; 04298 size_t enclen = 0; 04299 04300 memset( key, 0, 32 ); 04301 memset( iv , 0, 16 ); 04302 04303 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04304 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04305 04306 memset( inbuf, 5, 64 ); 04307 memset( encbuf, 0, 64 ); 04308 memset( decbuf, 0, 64 ); 04309 04310 /* Check and get info structures */ 04311 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 04312 fct_chk( NULL != cipher_info ); 04313 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 04314 04315 /* Initialise enc and dec contexts */ 04316 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04317 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04318 04319 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 04320 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 04321 04322 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04323 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04324 04325 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04326 { 04327 enclen = cipher_get_block_size( &ctx_enc ) 04328 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04329 } 04330 else 04331 { 04332 enclen = length; 04333 } 04334 04335 /* encode length number of bytes from inbuf */ 04336 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04337 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04338 { 04339 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04340 } 04341 else 04342 { 04343 fct_chk( outlen == enclen ); 04344 } 04345 04346 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04347 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04348 { 04349 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04350 } 04351 else 04352 { 04353 fct_chk( outlen == 0 ); 04354 } 04355 04356 04357 /* decode the previously encoded string */ 04358 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04359 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04360 { 04361 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04362 } 04363 else 04364 { 04365 fct_chk( enclen == outlen ); 04366 } 04367 04368 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04369 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04370 { 04371 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04372 } 04373 else 04374 { 04375 fct_chk( outlen == 0 ); 04376 } 04377 04378 04379 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04380 04381 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04382 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04383 FCT_TEST_END(); 04384 #endif /* POLARSSL_AES_C */ 04385 #endif /* POLARSSL_CIPHER_MODE_CFB */ 04386 04387 #ifdef POLARSSL_AES_C 04388 #ifdef POLARSSL_CIPHER_MODE_CFB 04389 04390 FCT_TEST_BGN(aes_encrypt_and_decrypt_48_bytes) 04391 size_t length = 48; 04392 unsigned char key[32]; 04393 unsigned char iv[16]; 04394 04395 const cipher_info_t *cipher_info; 04396 cipher_context_t ctx_dec; 04397 cipher_context_t ctx_enc; 04398 04399 unsigned char inbuf[64]; 04400 unsigned char encbuf[64]; 04401 unsigned char decbuf[64]; 04402 04403 size_t outlen = 0; 04404 size_t enclen = 0; 04405 04406 memset( key, 0, 32 ); 04407 memset( iv , 0, 16 ); 04408 04409 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04410 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04411 04412 memset( inbuf, 5, 64 ); 04413 memset( encbuf, 0, 64 ); 04414 memset( decbuf, 0, 64 ); 04415 04416 /* Check and get info structures */ 04417 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 04418 fct_chk( NULL != cipher_info ); 04419 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 04420 04421 /* Initialise enc and dec contexts */ 04422 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04423 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04424 04425 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 04426 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 04427 04428 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04429 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04430 04431 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04432 { 04433 enclen = cipher_get_block_size( &ctx_enc ) 04434 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04435 } 04436 else 04437 { 04438 enclen = length; 04439 } 04440 04441 /* encode length number of bytes from inbuf */ 04442 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04443 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04444 { 04445 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04446 } 04447 else 04448 { 04449 fct_chk( outlen == enclen ); 04450 } 04451 04452 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04453 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04454 { 04455 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04456 } 04457 else 04458 { 04459 fct_chk( outlen == 0 ); 04460 } 04461 04462 04463 /* decode the previously encoded string */ 04464 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04465 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04466 { 04467 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04468 } 04469 else 04470 { 04471 fct_chk( enclen == outlen ); 04472 } 04473 04474 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04475 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04476 { 04477 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04478 } 04479 else 04480 { 04481 fct_chk( outlen == 0 ); 04482 } 04483 04484 04485 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04486 04487 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04488 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04489 FCT_TEST_END(); 04490 #endif /* POLARSSL_AES_C */ 04491 #endif /* POLARSSL_CIPHER_MODE_CFB */ 04492 04493 #ifdef POLARSSL_AES_C 04494 #ifdef POLARSSL_CIPHER_MODE_CFB 04495 04496 FCT_TEST_BGN(aes_encrypt_and_decrypt_49_bytes) 04497 size_t length = 49; 04498 unsigned char key[32]; 04499 unsigned char iv[16]; 04500 04501 const cipher_info_t *cipher_info; 04502 cipher_context_t ctx_dec; 04503 cipher_context_t ctx_enc; 04504 04505 unsigned char inbuf[64]; 04506 unsigned char encbuf[64]; 04507 unsigned char decbuf[64]; 04508 04509 size_t outlen = 0; 04510 size_t enclen = 0; 04511 04512 memset( key, 0, 32 ); 04513 memset( iv , 0, 16 ); 04514 04515 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04516 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04517 04518 memset( inbuf, 5, 64 ); 04519 memset( encbuf, 0, 64 ); 04520 memset( decbuf, 0, 64 ); 04521 04522 /* Check and get info structures */ 04523 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 04524 fct_chk( NULL != cipher_info ); 04525 fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info ); 04526 04527 /* Initialise enc and dec contexts */ 04528 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04529 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04530 04531 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 04532 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 04533 04534 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04535 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04536 04537 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04538 { 04539 enclen = cipher_get_block_size( &ctx_enc ) 04540 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04541 } 04542 else 04543 { 04544 enclen = length; 04545 } 04546 04547 /* encode length number of bytes from inbuf */ 04548 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04549 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04550 { 04551 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04552 } 04553 else 04554 { 04555 fct_chk( outlen == enclen ); 04556 } 04557 04558 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04559 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04560 { 04561 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04562 } 04563 else 04564 { 04565 fct_chk( outlen == 0 ); 04566 } 04567 04568 04569 /* decode the previously encoded string */ 04570 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04571 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04572 { 04573 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04574 } 04575 else 04576 { 04577 fct_chk( enclen == outlen ); 04578 } 04579 04580 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04581 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04582 { 04583 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04584 } 04585 else 04586 { 04587 fct_chk( outlen == 0 ); 04588 } 04589 04590 04591 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04592 04593 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04594 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04595 FCT_TEST_END(); 04596 #endif /* POLARSSL_AES_C */ 04597 #endif /* POLARSSL_CIPHER_MODE_CFB */ 04598 04599 #ifdef POLARSSL_AES_C 04600 #ifdef POLARSSL_CIPHER_MODE_CFB 04601 04602 FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes_in_multiple_parts) 04603 size_t first_length = 0; 04604 size_t second_length = 0; 04605 size_t length = first_length + second_length; 04606 unsigned char key[32]; 04607 unsigned char iv[16]; 04608 04609 cipher_context_t ctx_dec; 04610 cipher_context_t ctx_enc; 04611 const cipher_info_t *cipher_info; 04612 04613 unsigned char inbuf[64]; 04614 unsigned char encbuf[64]; 04615 unsigned char decbuf[64]; 04616 04617 size_t outlen = 0; 04618 size_t totaloutlen = 0; 04619 size_t enclen = 0; 04620 04621 memset( key, 0, 32 ); 04622 memset( iv , 0, 16 ); 04623 04624 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04625 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04626 04627 memset( inbuf, 5, 64 ); 04628 memset( encbuf, 0, 64 ); 04629 memset( decbuf, 0, 64 ); 04630 04631 /* Initialise enc and dec contexts */ 04632 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 04633 fct_chk( NULL != cipher_info); 04634 04635 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04636 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04637 04638 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 04639 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 04640 04641 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04642 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04643 04644 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04645 { 04646 enclen = cipher_get_block_size(&ctx_enc ) 04647 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04648 } 04649 else 04650 { 04651 enclen = length; 04652 } 04653 04654 /* encode length number of bytes from inbuf */ 04655 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 04656 totaloutlen = outlen; 04657 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 04658 totaloutlen += outlen; 04659 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04660 { 04661 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04662 } 04663 else 04664 { 04665 fct_chk( totaloutlen == enclen ); 04666 } 04667 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 04668 totaloutlen += outlen; 04669 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04670 { 04671 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04672 } 04673 else 04674 { 04675 fct_chk( outlen == 0 ); 04676 } 04677 04678 /* decode the previously encoded string */ 04679 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04680 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04681 { 04682 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04683 } 04684 else 04685 { 04686 fct_chk( enclen == outlen ); 04687 } 04688 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04689 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04690 { 04691 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04692 } 04693 else 04694 { 04695 fct_chk( outlen == 0 ); 04696 } 04697 04698 04699 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04700 04701 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04702 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04703 FCT_TEST_END(); 04704 #endif /* POLARSSL_AES_C */ 04705 #endif /* POLARSSL_CIPHER_MODE_CFB */ 04706 04707 #ifdef POLARSSL_AES_C 04708 #ifdef POLARSSL_CIPHER_MODE_CFB 04709 04710 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_1) 04711 size_t first_length = 1; 04712 size_t second_length = 0; 04713 size_t length = first_length + second_length; 04714 unsigned char key[32]; 04715 unsigned char iv[16]; 04716 04717 cipher_context_t ctx_dec; 04718 cipher_context_t ctx_enc; 04719 const cipher_info_t *cipher_info; 04720 04721 unsigned char inbuf[64]; 04722 unsigned char encbuf[64]; 04723 unsigned char decbuf[64]; 04724 04725 size_t outlen = 0; 04726 size_t totaloutlen = 0; 04727 size_t enclen = 0; 04728 04729 memset( key, 0, 32 ); 04730 memset( iv , 0, 16 ); 04731 04732 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04733 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04734 04735 memset( inbuf, 5, 64 ); 04736 memset( encbuf, 0, 64 ); 04737 memset( decbuf, 0, 64 ); 04738 04739 /* Initialise enc and dec contexts */ 04740 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 04741 fct_chk( NULL != cipher_info); 04742 04743 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04744 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04745 04746 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 04747 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 04748 04749 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04750 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04751 04752 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04753 { 04754 enclen = cipher_get_block_size(&ctx_enc ) 04755 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04756 } 04757 else 04758 { 04759 enclen = length; 04760 } 04761 04762 /* encode length number of bytes from inbuf */ 04763 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 04764 totaloutlen = outlen; 04765 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 04766 totaloutlen += outlen; 04767 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04768 { 04769 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04770 } 04771 else 04772 { 04773 fct_chk( totaloutlen == enclen ); 04774 } 04775 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 04776 totaloutlen += outlen; 04777 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04778 { 04779 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04780 } 04781 else 04782 { 04783 fct_chk( outlen == 0 ); 04784 } 04785 04786 /* decode the previously encoded string */ 04787 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04788 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04789 { 04790 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04791 } 04792 else 04793 { 04794 fct_chk( enclen == outlen ); 04795 } 04796 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04797 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04798 { 04799 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04800 } 04801 else 04802 { 04803 fct_chk( outlen == 0 ); 04804 } 04805 04806 04807 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04808 04809 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04810 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04811 FCT_TEST_END(); 04812 #endif /* POLARSSL_AES_C */ 04813 #endif /* POLARSSL_CIPHER_MODE_CFB */ 04814 04815 #ifdef POLARSSL_AES_C 04816 #ifdef POLARSSL_CIPHER_MODE_CFB 04817 04818 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_2) 04819 size_t first_length = 0; 04820 size_t second_length = 1; 04821 size_t length = first_length + second_length; 04822 unsigned char key[32]; 04823 unsigned char iv[16]; 04824 04825 cipher_context_t ctx_dec; 04826 cipher_context_t ctx_enc; 04827 const cipher_info_t *cipher_info; 04828 04829 unsigned char inbuf[64]; 04830 unsigned char encbuf[64]; 04831 unsigned char decbuf[64]; 04832 04833 size_t outlen = 0; 04834 size_t totaloutlen = 0; 04835 size_t enclen = 0; 04836 04837 memset( key, 0, 32 ); 04838 memset( iv , 0, 16 ); 04839 04840 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04841 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04842 04843 memset( inbuf, 5, 64 ); 04844 memset( encbuf, 0, 64 ); 04845 memset( decbuf, 0, 64 ); 04846 04847 /* Initialise enc and dec contexts */ 04848 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 04849 fct_chk( NULL != cipher_info); 04850 04851 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04852 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04853 04854 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 04855 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 04856 04857 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04858 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04859 04860 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04861 { 04862 enclen = cipher_get_block_size(&ctx_enc ) 04863 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04864 } 04865 else 04866 { 04867 enclen = length; 04868 } 04869 04870 /* encode length number of bytes from inbuf */ 04871 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 04872 totaloutlen = outlen; 04873 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 04874 totaloutlen += outlen; 04875 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04876 { 04877 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04878 } 04879 else 04880 { 04881 fct_chk( totaloutlen == enclen ); 04882 } 04883 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 04884 totaloutlen += outlen; 04885 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04886 { 04887 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04888 } 04889 else 04890 { 04891 fct_chk( outlen == 0 ); 04892 } 04893 04894 /* decode the previously encoded string */ 04895 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04896 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04897 { 04898 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04899 } 04900 else 04901 { 04902 fct_chk( enclen == outlen ); 04903 } 04904 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04905 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04906 { 04907 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04908 } 04909 else 04910 { 04911 fct_chk( outlen == 0 ); 04912 } 04913 04914 04915 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04916 04917 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04918 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04919 FCT_TEST_END(); 04920 #endif /* POLARSSL_AES_C */ 04921 #endif /* POLARSSL_CIPHER_MODE_CFB */ 04922 04923 #ifdef POLARSSL_AES_C 04924 #ifdef POLARSSL_CIPHER_MODE_CFB 04925 04926 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_1) 04927 size_t first_length = 16; 04928 size_t second_length = 0; 04929 size_t length = first_length + second_length; 04930 unsigned char key[32]; 04931 unsigned char iv[16]; 04932 04933 cipher_context_t ctx_dec; 04934 cipher_context_t ctx_enc; 04935 const cipher_info_t *cipher_info; 04936 04937 unsigned char inbuf[64]; 04938 unsigned char encbuf[64]; 04939 unsigned char decbuf[64]; 04940 04941 size_t outlen = 0; 04942 size_t totaloutlen = 0; 04943 size_t enclen = 0; 04944 04945 memset( key, 0, 32 ); 04946 memset( iv , 0, 16 ); 04947 04948 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04949 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04950 04951 memset( inbuf, 5, 64 ); 04952 memset( encbuf, 0, 64 ); 04953 memset( decbuf, 0, 64 ); 04954 04955 /* Initialise enc and dec contexts */ 04956 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 04957 fct_chk( NULL != cipher_info); 04958 04959 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04960 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04961 04962 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 04963 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 04964 04965 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04966 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04967 04968 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04969 { 04970 enclen = cipher_get_block_size(&ctx_enc ) 04971 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04972 } 04973 else 04974 { 04975 enclen = length; 04976 } 04977 04978 /* encode length number of bytes from inbuf */ 04979 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 04980 totaloutlen = outlen; 04981 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 04982 totaloutlen += outlen; 04983 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04984 { 04985 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04986 } 04987 else 04988 { 04989 fct_chk( totaloutlen == enclen ); 04990 } 04991 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 04992 totaloutlen += outlen; 04993 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04994 { 04995 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04996 } 04997 else 04998 { 04999 fct_chk( outlen == 0 ); 05000 } 05001 05002 /* decode the previously encoded string */ 05003 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05004 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05005 { 05006 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05007 } 05008 else 05009 { 05010 fct_chk( enclen == outlen ); 05011 } 05012 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05013 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05014 { 05015 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05016 } 05017 else 05018 { 05019 fct_chk( outlen == 0 ); 05020 } 05021 05022 05023 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05024 05025 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05026 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05027 FCT_TEST_END(); 05028 #endif /* POLARSSL_AES_C */ 05029 #endif /* POLARSSL_CIPHER_MODE_CFB */ 05030 05031 #ifdef POLARSSL_AES_C 05032 #ifdef POLARSSL_CIPHER_MODE_CFB 05033 05034 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_2) 05035 size_t first_length = 0; 05036 size_t second_length = 16; 05037 size_t length = first_length + second_length; 05038 unsigned char key[32]; 05039 unsigned char iv[16]; 05040 05041 cipher_context_t ctx_dec; 05042 cipher_context_t ctx_enc; 05043 const cipher_info_t *cipher_info; 05044 05045 unsigned char inbuf[64]; 05046 unsigned char encbuf[64]; 05047 unsigned char decbuf[64]; 05048 05049 size_t outlen = 0; 05050 size_t totaloutlen = 0; 05051 size_t enclen = 0; 05052 05053 memset( key, 0, 32 ); 05054 memset( iv , 0, 16 ); 05055 05056 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05057 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05058 05059 memset( inbuf, 5, 64 ); 05060 memset( encbuf, 0, 64 ); 05061 memset( decbuf, 0, 64 ); 05062 05063 /* Initialise enc and dec contexts */ 05064 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 05065 fct_chk( NULL != cipher_info); 05066 05067 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05068 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05069 05070 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 05071 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 05072 05073 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05074 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05075 05076 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05077 { 05078 enclen = cipher_get_block_size(&ctx_enc ) 05079 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05080 } 05081 else 05082 { 05083 enclen = length; 05084 } 05085 05086 /* encode length number of bytes from inbuf */ 05087 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05088 totaloutlen = outlen; 05089 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05090 totaloutlen += outlen; 05091 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05092 { 05093 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05094 } 05095 else 05096 { 05097 fct_chk( totaloutlen == enclen ); 05098 } 05099 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05100 totaloutlen += outlen; 05101 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05102 { 05103 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05104 } 05105 else 05106 { 05107 fct_chk( outlen == 0 ); 05108 } 05109 05110 /* decode the previously encoded string */ 05111 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05112 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05113 { 05114 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05115 } 05116 else 05117 { 05118 fct_chk( enclen == outlen ); 05119 } 05120 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05121 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05122 { 05123 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05124 } 05125 else 05126 { 05127 fct_chk( outlen == 0 ); 05128 } 05129 05130 05131 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05132 05133 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05134 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05135 FCT_TEST_END(); 05136 #endif /* POLARSSL_AES_C */ 05137 #endif /* POLARSSL_CIPHER_MODE_CFB */ 05138 05139 #ifdef POLARSSL_AES_C 05140 #ifdef POLARSSL_CIPHER_MODE_CFB 05141 05142 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_3) 05143 size_t first_length = 1; 05144 size_t second_length = 15; 05145 size_t length = first_length + second_length; 05146 unsigned char key[32]; 05147 unsigned char iv[16]; 05148 05149 cipher_context_t ctx_dec; 05150 cipher_context_t ctx_enc; 05151 const cipher_info_t *cipher_info; 05152 05153 unsigned char inbuf[64]; 05154 unsigned char encbuf[64]; 05155 unsigned char decbuf[64]; 05156 05157 size_t outlen = 0; 05158 size_t totaloutlen = 0; 05159 size_t enclen = 0; 05160 05161 memset( key, 0, 32 ); 05162 memset( iv , 0, 16 ); 05163 05164 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05165 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05166 05167 memset( inbuf, 5, 64 ); 05168 memset( encbuf, 0, 64 ); 05169 memset( decbuf, 0, 64 ); 05170 05171 /* Initialise enc and dec contexts */ 05172 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 05173 fct_chk( NULL != cipher_info); 05174 05175 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05176 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05177 05178 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 05179 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 05180 05181 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05182 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05183 05184 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05185 { 05186 enclen = cipher_get_block_size(&ctx_enc ) 05187 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05188 } 05189 else 05190 { 05191 enclen = length; 05192 } 05193 05194 /* encode length number of bytes from inbuf */ 05195 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05196 totaloutlen = outlen; 05197 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05198 totaloutlen += outlen; 05199 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05200 { 05201 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05202 } 05203 else 05204 { 05205 fct_chk( totaloutlen == enclen ); 05206 } 05207 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05208 totaloutlen += outlen; 05209 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05210 { 05211 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05212 } 05213 else 05214 { 05215 fct_chk( outlen == 0 ); 05216 } 05217 05218 /* decode the previously encoded string */ 05219 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05220 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05221 { 05222 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05223 } 05224 else 05225 { 05226 fct_chk( enclen == outlen ); 05227 } 05228 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05229 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05230 { 05231 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05232 } 05233 else 05234 { 05235 fct_chk( outlen == 0 ); 05236 } 05237 05238 05239 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05240 05241 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05242 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05243 FCT_TEST_END(); 05244 #endif /* POLARSSL_AES_C */ 05245 #endif /* POLARSSL_CIPHER_MODE_CFB */ 05246 05247 #ifdef POLARSSL_AES_C 05248 #ifdef POLARSSL_CIPHER_MODE_CFB 05249 05250 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_4) 05251 size_t first_length = 15; 05252 size_t second_length = 1; 05253 size_t length = first_length + second_length; 05254 unsigned char key[32]; 05255 unsigned char iv[16]; 05256 05257 cipher_context_t ctx_dec; 05258 cipher_context_t ctx_enc; 05259 const cipher_info_t *cipher_info; 05260 05261 unsigned char inbuf[64]; 05262 unsigned char encbuf[64]; 05263 unsigned char decbuf[64]; 05264 05265 size_t outlen = 0; 05266 size_t totaloutlen = 0; 05267 size_t enclen = 0; 05268 05269 memset( key, 0, 32 ); 05270 memset( iv , 0, 16 ); 05271 05272 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05273 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05274 05275 memset( inbuf, 5, 64 ); 05276 memset( encbuf, 0, 64 ); 05277 memset( decbuf, 0, 64 ); 05278 05279 /* Initialise enc and dec contexts */ 05280 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 05281 fct_chk( NULL != cipher_info); 05282 05283 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05284 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05285 05286 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 05287 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 05288 05289 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05290 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05291 05292 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05293 { 05294 enclen = cipher_get_block_size(&ctx_enc ) 05295 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05296 } 05297 else 05298 { 05299 enclen = length; 05300 } 05301 05302 /* encode length number of bytes from inbuf */ 05303 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05304 totaloutlen = outlen; 05305 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05306 totaloutlen += outlen; 05307 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05308 { 05309 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05310 } 05311 else 05312 { 05313 fct_chk( totaloutlen == enclen ); 05314 } 05315 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05316 totaloutlen += outlen; 05317 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05318 { 05319 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05320 } 05321 else 05322 { 05323 fct_chk( outlen == 0 ); 05324 } 05325 05326 /* decode the previously encoded string */ 05327 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05328 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05329 { 05330 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05331 } 05332 else 05333 { 05334 fct_chk( enclen == outlen ); 05335 } 05336 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05337 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05338 { 05339 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05340 } 05341 else 05342 { 05343 fct_chk( outlen == 0 ); 05344 } 05345 05346 05347 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05348 05349 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05350 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05351 FCT_TEST_END(); 05352 #endif /* POLARSSL_AES_C */ 05353 #endif /* POLARSSL_CIPHER_MODE_CFB */ 05354 05355 #ifdef POLARSSL_AES_C 05356 #ifdef POLARSSL_CIPHER_MODE_CFB 05357 05358 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 05359 size_t first_length = 15; 05360 size_t second_length = 7; 05361 size_t length = first_length + second_length; 05362 unsigned char key[32]; 05363 unsigned char iv[16]; 05364 05365 cipher_context_t ctx_dec; 05366 cipher_context_t ctx_enc; 05367 const cipher_info_t *cipher_info; 05368 05369 unsigned char inbuf[64]; 05370 unsigned char encbuf[64]; 05371 unsigned char decbuf[64]; 05372 05373 size_t outlen = 0; 05374 size_t totaloutlen = 0; 05375 size_t enclen = 0; 05376 05377 memset( key, 0, 32 ); 05378 memset( iv , 0, 16 ); 05379 05380 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05381 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05382 05383 memset( inbuf, 5, 64 ); 05384 memset( encbuf, 0, 64 ); 05385 memset( decbuf, 0, 64 ); 05386 05387 /* Initialise enc and dec contexts */ 05388 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 05389 fct_chk( NULL != cipher_info); 05390 05391 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05392 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05393 05394 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 05395 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 05396 05397 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05398 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05399 05400 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05401 { 05402 enclen = cipher_get_block_size(&ctx_enc ) 05403 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05404 } 05405 else 05406 { 05407 enclen = length; 05408 } 05409 05410 /* encode length number of bytes from inbuf */ 05411 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05412 totaloutlen = outlen; 05413 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05414 totaloutlen += outlen; 05415 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05416 { 05417 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05418 } 05419 else 05420 { 05421 fct_chk( totaloutlen == enclen ); 05422 } 05423 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05424 totaloutlen += outlen; 05425 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05426 { 05427 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05428 } 05429 else 05430 { 05431 fct_chk( outlen == 0 ); 05432 } 05433 05434 /* decode the previously encoded string */ 05435 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05436 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05437 { 05438 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05439 } 05440 else 05441 { 05442 fct_chk( enclen == outlen ); 05443 } 05444 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05445 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05446 { 05447 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05448 } 05449 else 05450 { 05451 fct_chk( outlen == 0 ); 05452 } 05453 05454 05455 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05456 05457 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05458 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05459 FCT_TEST_END(); 05460 #endif /* POLARSSL_AES_C */ 05461 #endif /* POLARSSL_CIPHER_MODE_CFB */ 05462 05463 #ifdef POLARSSL_AES_C 05464 #ifdef POLARSSL_CIPHER_MODE_CFB 05465 05466 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 05467 size_t first_length = 16; 05468 size_t second_length = 6; 05469 size_t length = first_length + second_length; 05470 unsigned char key[32]; 05471 unsigned char iv[16]; 05472 05473 cipher_context_t ctx_dec; 05474 cipher_context_t ctx_enc; 05475 const cipher_info_t *cipher_info; 05476 05477 unsigned char inbuf[64]; 05478 unsigned char encbuf[64]; 05479 unsigned char decbuf[64]; 05480 05481 size_t outlen = 0; 05482 size_t totaloutlen = 0; 05483 size_t enclen = 0; 05484 05485 memset( key, 0, 32 ); 05486 memset( iv , 0, 16 ); 05487 05488 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05489 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05490 05491 memset( inbuf, 5, 64 ); 05492 memset( encbuf, 0, 64 ); 05493 memset( decbuf, 0, 64 ); 05494 05495 /* Initialise enc and dec contexts */ 05496 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 05497 fct_chk( NULL != cipher_info); 05498 05499 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05500 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05501 05502 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 05503 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 05504 05505 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05506 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05507 05508 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05509 { 05510 enclen = cipher_get_block_size(&ctx_enc ) 05511 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05512 } 05513 else 05514 { 05515 enclen = length; 05516 } 05517 05518 /* encode length number of bytes from inbuf */ 05519 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05520 totaloutlen = outlen; 05521 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05522 totaloutlen += outlen; 05523 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05524 { 05525 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05526 } 05527 else 05528 { 05529 fct_chk( totaloutlen == enclen ); 05530 } 05531 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05532 totaloutlen += outlen; 05533 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05534 { 05535 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05536 } 05537 else 05538 { 05539 fct_chk( outlen == 0 ); 05540 } 05541 05542 /* decode the previously encoded string */ 05543 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05544 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05545 { 05546 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05547 } 05548 else 05549 { 05550 fct_chk( enclen == outlen ); 05551 } 05552 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05553 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05554 { 05555 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05556 } 05557 else 05558 { 05559 fct_chk( outlen == 0 ); 05560 } 05561 05562 05563 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05564 05565 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05566 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05567 FCT_TEST_END(); 05568 #endif /* POLARSSL_AES_C */ 05569 #endif /* POLARSSL_CIPHER_MODE_CFB */ 05570 05571 #ifdef POLARSSL_AES_C 05572 #ifdef POLARSSL_CIPHER_MODE_CFB 05573 05574 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 05575 size_t first_length = 17; 05576 size_t second_length = 6; 05577 size_t length = first_length + second_length; 05578 unsigned char key[32]; 05579 unsigned char iv[16]; 05580 05581 cipher_context_t ctx_dec; 05582 cipher_context_t ctx_enc; 05583 const cipher_info_t *cipher_info; 05584 05585 unsigned char inbuf[64]; 05586 unsigned char encbuf[64]; 05587 unsigned char decbuf[64]; 05588 05589 size_t outlen = 0; 05590 size_t totaloutlen = 0; 05591 size_t enclen = 0; 05592 05593 memset( key, 0, 32 ); 05594 memset( iv , 0, 16 ); 05595 05596 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05597 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05598 05599 memset( inbuf, 5, 64 ); 05600 memset( encbuf, 0, 64 ); 05601 memset( decbuf, 0, 64 ); 05602 05603 /* Initialise enc and dec contexts */ 05604 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 05605 fct_chk( NULL != cipher_info); 05606 05607 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05608 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05609 05610 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 05611 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 05612 05613 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05614 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05615 05616 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05617 { 05618 enclen = cipher_get_block_size(&ctx_enc ) 05619 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05620 } 05621 else 05622 { 05623 enclen = length; 05624 } 05625 05626 /* encode length number of bytes from inbuf */ 05627 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05628 totaloutlen = outlen; 05629 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05630 totaloutlen += outlen; 05631 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05632 { 05633 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05634 } 05635 else 05636 { 05637 fct_chk( totaloutlen == enclen ); 05638 } 05639 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05640 totaloutlen += outlen; 05641 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05642 { 05643 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05644 } 05645 else 05646 { 05647 fct_chk( outlen == 0 ); 05648 } 05649 05650 /* decode the previously encoded string */ 05651 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05652 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05653 { 05654 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05655 } 05656 else 05657 { 05658 fct_chk( enclen == outlen ); 05659 } 05660 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05661 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05662 { 05663 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05664 } 05665 else 05666 { 05667 fct_chk( outlen == 0 ); 05668 } 05669 05670 05671 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05672 05673 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05674 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05675 FCT_TEST_END(); 05676 #endif /* POLARSSL_AES_C */ 05677 #endif /* POLARSSL_CIPHER_MODE_CFB */ 05678 05679 #ifdef POLARSSL_AES_C 05680 #ifdef POLARSSL_CIPHER_MODE_CFB 05681 05682 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes_in_multiple_parts_1) 05683 size_t first_length = 16; 05684 size_t second_length = 16; 05685 size_t length = first_length + second_length; 05686 unsigned char key[32]; 05687 unsigned char iv[16]; 05688 05689 cipher_context_t ctx_dec; 05690 cipher_context_t ctx_enc; 05691 const cipher_info_t *cipher_info; 05692 05693 unsigned char inbuf[64]; 05694 unsigned char encbuf[64]; 05695 unsigned char decbuf[64]; 05696 05697 size_t outlen = 0; 05698 size_t totaloutlen = 0; 05699 size_t enclen = 0; 05700 05701 memset( key, 0, 32 ); 05702 memset( iv , 0, 16 ); 05703 05704 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05705 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05706 05707 memset( inbuf, 5, 64 ); 05708 memset( encbuf, 0, 64 ); 05709 memset( decbuf, 0, 64 ); 05710 05711 /* Initialise enc and dec contexts */ 05712 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); 05713 fct_chk( NULL != cipher_info); 05714 05715 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05716 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05717 05718 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 05719 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 05720 05721 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05722 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05723 05724 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05725 { 05726 enclen = cipher_get_block_size(&ctx_enc ) 05727 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05728 } 05729 else 05730 { 05731 enclen = length; 05732 } 05733 05734 /* encode length number of bytes from inbuf */ 05735 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05736 totaloutlen = outlen; 05737 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05738 totaloutlen += outlen; 05739 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05740 { 05741 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05742 } 05743 else 05744 { 05745 fct_chk( totaloutlen == enclen ); 05746 } 05747 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05748 totaloutlen += outlen; 05749 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05750 { 05751 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05752 } 05753 else 05754 { 05755 fct_chk( outlen == 0 ); 05756 } 05757 05758 /* decode the previously encoded string */ 05759 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05760 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05761 { 05762 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05763 } 05764 else 05765 { 05766 fct_chk( enclen == outlen ); 05767 } 05768 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05769 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05770 { 05771 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05772 } 05773 else 05774 { 05775 fct_chk( outlen == 0 ); 05776 } 05777 05778 05779 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05780 05781 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05782 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05783 FCT_TEST_END(); 05784 #endif /* POLARSSL_AES_C */ 05785 #endif /* POLARSSL_CIPHER_MODE_CFB */ 05786 05787 #ifdef POLARSSL_AES_C 05788 #ifdef POLARSSL_CIPHER_MODE_CTR 05789 05790 FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes) 05791 size_t length = 0; 05792 unsigned char key[32]; 05793 unsigned char iv[16]; 05794 05795 const cipher_info_t *cipher_info; 05796 cipher_context_t ctx_dec; 05797 cipher_context_t ctx_enc; 05798 05799 unsigned char inbuf[64]; 05800 unsigned char encbuf[64]; 05801 unsigned char decbuf[64]; 05802 05803 size_t outlen = 0; 05804 size_t enclen = 0; 05805 05806 memset( key, 0, 32 ); 05807 memset( iv , 0, 16 ); 05808 05809 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05810 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05811 05812 memset( inbuf, 5, 64 ); 05813 memset( encbuf, 0, 64 ); 05814 memset( decbuf, 0, 64 ); 05815 05816 /* Check and get info structures */ 05817 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 05818 fct_chk( NULL != cipher_info ); 05819 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 05820 05821 /* Initialise enc and dec contexts */ 05822 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05823 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05824 05825 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 05826 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 05827 05828 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05829 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05830 05831 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05832 { 05833 enclen = cipher_get_block_size( &ctx_enc ) 05834 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05835 } 05836 else 05837 { 05838 enclen = length; 05839 } 05840 05841 /* encode length number of bytes from inbuf */ 05842 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 05843 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05844 { 05845 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05846 } 05847 else 05848 { 05849 fct_chk( outlen == enclen ); 05850 } 05851 05852 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 05853 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05854 { 05855 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05856 } 05857 else 05858 { 05859 fct_chk( outlen == 0 ); 05860 } 05861 05862 05863 /* decode the previously encoded string */ 05864 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05865 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05866 { 05867 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05868 } 05869 else 05870 { 05871 fct_chk( enclen == outlen ); 05872 } 05873 05874 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05875 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05876 { 05877 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05878 } 05879 else 05880 { 05881 fct_chk( outlen == 0 ); 05882 } 05883 05884 05885 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05886 05887 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05888 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05889 FCT_TEST_END(); 05890 #endif /* POLARSSL_AES_C */ 05891 #endif /* POLARSSL_CIPHER_MODE_CTR */ 05892 05893 #ifdef POLARSSL_AES_C 05894 #ifdef POLARSSL_CIPHER_MODE_CTR 05895 05896 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_byte) 05897 size_t length = 1; 05898 unsigned char key[32]; 05899 unsigned char iv[16]; 05900 05901 const cipher_info_t *cipher_info; 05902 cipher_context_t ctx_dec; 05903 cipher_context_t ctx_enc; 05904 05905 unsigned char inbuf[64]; 05906 unsigned char encbuf[64]; 05907 unsigned char decbuf[64]; 05908 05909 size_t outlen = 0; 05910 size_t enclen = 0; 05911 05912 memset( key, 0, 32 ); 05913 memset( iv , 0, 16 ); 05914 05915 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05916 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05917 05918 memset( inbuf, 5, 64 ); 05919 memset( encbuf, 0, 64 ); 05920 memset( decbuf, 0, 64 ); 05921 05922 /* Check and get info structures */ 05923 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 05924 fct_chk( NULL != cipher_info ); 05925 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 05926 05927 /* Initialise enc and dec contexts */ 05928 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05929 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05930 05931 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 05932 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 05933 05934 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05935 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05936 05937 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05938 { 05939 enclen = cipher_get_block_size( &ctx_enc ) 05940 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05941 } 05942 else 05943 { 05944 enclen = length; 05945 } 05946 05947 /* encode length number of bytes from inbuf */ 05948 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 05949 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05950 { 05951 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05952 } 05953 else 05954 { 05955 fct_chk( outlen == enclen ); 05956 } 05957 05958 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 05959 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05960 { 05961 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05962 } 05963 else 05964 { 05965 fct_chk( outlen == 0 ); 05966 } 05967 05968 05969 /* decode the previously encoded string */ 05970 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05971 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05972 { 05973 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05974 } 05975 else 05976 { 05977 fct_chk( enclen == outlen ); 05978 } 05979 05980 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05981 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05982 { 05983 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05984 } 05985 else 05986 { 05987 fct_chk( outlen == 0 ); 05988 } 05989 05990 05991 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05992 05993 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05994 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05995 FCT_TEST_END(); 05996 #endif /* POLARSSL_AES_C */ 05997 #endif /* POLARSSL_CIPHER_MODE_CTR */ 05998 05999 #ifdef POLARSSL_AES_C 06000 #ifdef POLARSSL_CIPHER_MODE_CTR 06001 06002 FCT_TEST_BGN(aes_encrypt_and_decrypt_2_bytes) 06003 size_t length = 2; 06004 unsigned char key[32]; 06005 unsigned char iv[16]; 06006 06007 const cipher_info_t *cipher_info; 06008 cipher_context_t ctx_dec; 06009 cipher_context_t ctx_enc; 06010 06011 unsigned char inbuf[64]; 06012 unsigned char encbuf[64]; 06013 unsigned char decbuf[64]; 06014 06015 size_t outlen = 0; 06016 size_t enclen = 0; 06017 06018 memset( key, 0, 32 ); 06019 memset( iv , 0, 16 ); 06020 06021 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06022 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06023 06024 memset( inbuf, 5, 64 ); 06025 memset( encbuf, 0, 64 ); 06026 memset( decbuf, 0, 64 ); 06027 06028 /* Check and get info structures */ 06029 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 06030 fct_chk( NULL != cipher_info ); 06031 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 06032 06033 /* Initialise enc and dec contexts */ 06034 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06035 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06036 06037 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 06038 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 06039 06040 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06041 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06042 06043 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06044 { 06045 enclen = cipher_get_block_size( &ctx_enc ) 06046 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06047 } 06048 else 06049 { 06050 enclen = length; 06051 } 06052 06053 /* encode length number of bytes from inbuf */ 06054 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06055 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06056 { 06057 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06058 } 06059 else 06060 { 06061 fct_chk( outlen == enclen ); 06062 } 06063 06064 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06065 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06066 { 06067 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06068 } 06069 else 06070 { 06071 fct_chk( outlen == 0 ); 06072 } 06073 06074 06075 /* decode the previously encoded string */ 06076 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06077 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06078 { 06079 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06080 } 06081 else 06082 { 06083 fct_chk( enclen == outlen ); 06084 } 06085 06086 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06087 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06088 { 06089 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06090 } 06091 else 06092 { 06093 fct_chk( outlen == 0 ); 06094 } 06095 06096 06097 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06098 06099 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06100 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06101 FCT_TEST_END(); 06102 #endif /* POLARSSL_AES_C */ 06103 #endif /* POLARSSL_CIPHER_MODE_CTR */ 06104 06105 #ifdef POLARSSL_AES_C 06106 #ifdef POLARSSL_CIPHER_MODE_CTR 06107 06108 FCT_TEST_BGN(aes_encrypt_and_decrypt_7_bytes) 06109 size_t length = 7; 06110 unsigned char key[32]; 06111 unsigned char iv[16]; 06112 06113 const cipher_info_t *cipher_info; 06114 cipher_context_t ctx_dec; 06115 cipher_context_t ctx_enc; 06116 06117 unsigned char inbuf[64]; 06118 unsigned char encbuf[64]; 06119 unsigned char decbuf[64]; 06120 06121 size_t outlen = 0; 06122 size_t enclen = 0; 06123 06124 memset( key, 0, 32 ); 06125 memset( iv , 0, 16 ); 06126 06127 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06128 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06129 06130 memset( inbuf, 5, 64 ); 06131 memset( encbuf, 0, 64 ); 06132 memset( decbuf, 0, 64 ); 06133 06134 /* Check and get info structures */ 06135 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 06136 fct_chk( NULL != cipher_info ); 06137 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 06138 06139 /* Initialise enc and dec contexts */ 06140 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06141 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06142 06143 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 06144 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 06145 06146 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06147 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06148 06149 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06150 { 06151 enclen = cipher_get_block_size( &ctx_enc ) 06152 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06153 } 06154 else 06155 { 06156 enclen = length; 06157 } 06158 06159 /* encode length number of bytes from inbuf */ 06160 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06161 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06162 { 06163 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06164 } 06165 else 06166 { 06167 fct_chk( outlen == enclen ); 06168 } 06169 06170 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06171 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06172 { 06173 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06174 } 06175 else 06176 { 06177 fct_chk( outlen == 0 ); 06178 } 06179 06180 06181 /* decode the previously encoded string */ 06182 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06183 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06184 { 06185 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06186 } 06187 else 06188 { 06189 fct_chk( enclen == outlen ); 06190 } 06191 06192 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06193 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06194 { 06195 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06196 } 06197 else 06198 { 06199 fct_chk( outlen == 0 ); 06200 } 06201 06202 06203 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06204 06205 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06206 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06207 FCT_TEST_END(); 06208 #endif /* POLARSSL_AES_C */ 06209 #endif /* POLARSSL_CIPHER_MODE_CTR */ 06210 06211 #ifdef POLARSSL_AES_C 06212 #ifdef POLARSSL_CIPHER_MODE_CTR 06213 06214 FCT_TEST_BGN(aes_encrypt_and_decrypt_8_bytes) 06215 size_t length = 8; 06216 unsigned char key[32]; 06217 unsigned char iv[16]; 06218 06219 const cipher_info_t *cipher_info; 06220 cipher_context_t ctx_dec; 06221 cipher_context_t ctx_enc; 06222 06223 unsigned char inbuf[64]; 06224 unsigned char encbuf[64]; 06225 unsigned char decbuf[64]; 06226 06227 size_t outlen = 0; 06228 size_t enclen = 0; 06229 06230 memset( key, 0, 32 ); 06231 memset( iv , 0, 16 ); 06232 06233 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06234 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06235 06236 memset( inbuf, 5, 64 ); 06237 memset( encbuf, 0, 64 ); 06238 memset( decbuf, 0, 64 ); 06239 06240 /* Check and get info structures */ 06241 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 06242 fct_chk( NULL != cipher_info ); 06243 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 06244 06245 /* Initialise enc and dec contexts */ 06246 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06247 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06248 06249 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 06250 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 06251 06252 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06253 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06254 06255 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06256 { 06257 enclen = cipher_get_block_size( &ctx_enc ) 06258 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06259 } 06260 else 06261 { 06262 enclen = length; 06263 } 06264 06265 /* encode length number of bytes from inbuf */ 06266 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06267 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06268 { 06269 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06270 } 06271 else 06272 { 06273 fct_chk( outlen == enclen ); 06274 } 06275 06276 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06277 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06278 { 06279 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06280 } 06281 else 06282 { 06283 fct_chk( outlen == 0 ); 06284 } 06285 06286 06287 /* decode the previously encoded string */ 06288 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06289 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06290 { 06291 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06292 } 06293 else 06294 { 06295 fct_chk( enclen == outlen ); 06296 } 06297 06298 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06299 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06300 { 06301 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06302 } 06303 else 06304 { 06305 fct_chk( outlen == 0 ); 06306 } 06307 06308 06309 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06310 06311 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06312 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06313 FCT_TEST_END(); 06314 #endif /* POLARSSL_AES_C */ 06315 #endif /* POLARSSL_CIPHER_MODE_CTR */ 06316 06317 #ifdef POLARSSL_AES_C 06318 #ifdef POLARSSL_CIPHER_MODE_CTR 06319 06320 FCT_TEST_BGN(aes_encrypt_and_decrypt_9_bytes) 06321 size_t length = 9; 06322 unsigned char key[32]; 06323 unsigned char iv[16]; 06324 06325 const cipher_info_t *cipher_info; 06326 cipher_context_t ctx_dec; 06327 cipher_context_t ctx_enc; 06328 06329 unsigned char inbuf[64]; 06330 unsigned char encbuf[64]; 06331 unsigned char decbuf[64]; 06332 06333 size_t outlen = 0; 06334 size_t enclen = 0; 06335 06336 memset( key, 0, 32 ); 06337 memset( iv , 0, 16 ); 06338 06339 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06340 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06341 06342 memset( inbuf, 5, 64 ); 06343 memset( encbuf, 0, 64 ); 06344 memset( decbuf, 0, 64 ); 06345 06346 /* Check and get info structures */ 06347 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 06348 fct_chk( NULL != cipher_info ); 06349 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 06350 06351 /* Initialise enc and dec contexts */ 06352 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06353 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06354 06355 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 06356 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 06357 06358 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06359 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06360 06361 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06362 { 06363 enclen = cipher_get_block_size( &ctx_enc ) 06364 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06365 } 06366 else 06367 { 06368 enclen = length; 06369 } 06370 06371 /* encode length number of bytes from inbuf */ 06372 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06373 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06374 { 06375 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06376 } 06377 else 06378 { 06379 fct_chk( outlen == enclen ); 06380 } 06381 06382 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06383 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06384 { 06385 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06386 } 06387 else 06388 { 06389 fct_chk( outlen == 0 ); 06390 } 06391 06392 06393 /* decode the previously encoded string */ 06394 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06395 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06396 { 06397 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06398 } 06399 else 06400 { 06401 fct_chk( enclen == outlen ); 06402 } 06403 06404 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06405 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06406 { 06407 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06408 } 06409 else 06410 { 06411 fct_chk( outlen == 0 ); 06412 } 06413 06414 06415 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06416 06417 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06418 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06419 FCT_TEST_END(); 06420 #endif /* POLARSSL_AES_C */ 06421 #endif /* POLARSSL_CIPHER_MODE_CTR */ 06422 06423 #ifdef POLARSSL_AES_C 06424 #ifdef POLARSSL_CIPHER_MODE_CTR 06425 06426 FCT_TEST_BGN(aes_encrypt_and_decrypt_15_bytes) 06427 size_t length = 15; 06428 unsigned char key[32]; 06429 unsigned char iv[16]; 06430 06431 const cipher_info_t *cipher_info; 06432 cipher_context_t ctx_dec; 06433 cipher_context_t ctx_enc; 06434 06435 unsigned char inbuf[64]; 06436 unsigned char encbuf[64]; 06437 unsigned char decbuf[64]; 06438 06439 size_t outlen = 0; 06440 size_t enclen = 0; 06441 06442 memset( key, 0, 32 ); 06443 memset( iv , 0, 16 ); 06444 06445 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06446 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06447 06448 memset( inbuf, 5, 64 ); 06449 memset( encbuf, 0, 64 ); 06450 memset( decbuf, 0, 64 ); 06451 06452 /* Check and get info structures */ 06453 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 06454 fct_chk( NULL != cipher_info ); 06455 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 06456 06457 /* Initialise enc and dec contexts */ 06458 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06459 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06460 06461 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 06462 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 06463 06464 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06465 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06466 06467 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06468 { 06469 enclen = cipher_get_block_size( &ctx_enc ) 06470 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06471 } 06472 else 06473 { 06474 enclen = length; 06475 } 06476 06477 /* encode length number of bytes from inbuf */ 06478 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06479 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06480 { 06481 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06482 } 06483 else 06484 { 06485 fct_chk( outlen == enclen ); 06486 } 06487 06488 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06489 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06490 { 06491 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06492 } 06493 else 06494 { 06495 fct_chk( outlen == 0 ); 06496 } 06497 06498 06499 /* decode the previously encoded string */ 06500 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06501 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06502 { 06503 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06504 } 06505 else 06506 { 06507 fct_chk( enclen == outlen ); 06508 } 06509 06510 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06511 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06512 { 06513 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06514 } 06515 else 06516 { 06517 fct_chk( outlen == 0 ); 06518 } 06519 06520 06521 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06522 06523 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06524 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06525 FCT_TEST_END(); 06526 #endif /* POLARSSL_AES_C */ 06527 #endif /* POLARSSL_CIPHER_MODE_CTR */ 06528 06529 #ifdef POLARSSL_AES_C 06530 #ifdef POLARSSL_CIPHER_MODE_CTR 06531 06532 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes) 06533 size_t length = 16; 06534 unsigned char key[32]; 06535 unsigned char iv[16]; 06536 06537 const cipher_info_t *cipher_info; 06538 cipher_context_t ctx_dec; 06539 cipher_context_t ctx_enc; 06540 06541 unsigned char inbuf[64]; 06542 unsigned char encbuf[64]; 06543 unsigned char decbuf[64]; 06544 06545 size_t outlen = 0; 06546 size_t enclen = 0; 06547 06548 memset( key, 0, 32 ); 06549 memset( iv , 0, 16 ); 06550 06551 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06552 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06553 06554 memset( inbuf, 5, 64 ); 06555 memset( encbuf, 0, 64 ); 06556 memset( decbuf, 0, 64 ); 06557 06558 /* Check and get info structures */ 06559 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 06560 fct_chk( NULL != cipher_info ); 06561 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 06562 06563 /* Initialise enc and dec contexts */ 06564 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06565 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06566 06567 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 06568 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 06569 06570 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06571 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06572 06573 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06574 { 06575 enclen = cipher_get_block_size( &ctx_enc ) 06576 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06577 } 06578 else 06579 { 06580 enclen = length; 06581 } 06582 06583 /* encode length number of bytes from inbuf */ 06584 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06585 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06586 { 06587 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06588 } 06589 else 06590 { 06591 fct_chk( outlen == enclen ); 06592 } 06593 06594 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06595 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06596 { 06597 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06598 } 06599 else 06600 { 06601 fct_chk( outlen == 0 ); 06602 } 06603 06604 06605 /* decode the previously encoded string */ 06606 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06607 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06608 { 06609 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06610 } 06611 else 06612 { 06613 fct_chk( enclen == outlen ); 06614 } 06615 06616 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06617 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06618 { 06619 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06620 } 06621 else 06622 { 06623 fct_chk( outlen == 0 ); 06624 } 06625 06626 06627 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06628 06629 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06630 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06631 FCT_TEST_END(); 06632 #endif /* POLARSSL_AES_C */ 06633 #endif /* POLARSSL_CIPHER_MODE_CTR */ 06634 06635 #ifdef POLARSSL_AES_C 06636 #ifdef POLARSSL_CIPHER_MODE_CTR 06637 06638 FCT_TEST_BGN(aes_encrypt_and_decrypt_17_bytes) 06639 size_t length = 17; 06640 unsigned char key[32]; 06641 unsigned char iv[16]; 06642 06643 const cipher_info_t *cipher_info; 06644 cipher_context_t ctx_dec; 06645 cipher_context_t ctx_enc; 06646 06647 unsigned char inbuf[64]; 06648 unsigned char encbuf[64]; 06649 unsigned char decbuf[64]; 06650 06651 size_t outlen = 0; 06652 size_t enclen = 0; 06653 06654 memset( key, 0, 32 ); 06655 memset( iv , 0, 16 ); 06656 06657 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06658 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06659 06660 memset( inbuf, 5, 64 ); 06661 memset( encbuf, 0, 64 ); 06662 memset( decbuf, 0, 64 ); 06663 06664 /* Check and get info structures */ 06665 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 06666 fct_chk( NULL != cipher_info ); 06667 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 06668 06669 /* Initialise enc and dec contexts */ 06670 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06671 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06672 06673 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 06674 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 06675 06676 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06677 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06678 06679 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06680 { 06681 enclen = cipher_get_block_size( &ctx_enc ) 06682 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06683 } 06684 else 06685 { 06686 enclen = length; 06687 } 06688 06689 /* encode length number of bytes from inbuf */ 06690 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06691 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06692 { 06693 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06694 } 06695 else 06696 { 06697 fct_chk( outlen == enclen ); 06698 } 06699 06700 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06701 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06702 { 06703 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06704 } 06705 else 06706 { 06707 fct_chk( outlen == 0 ); 06708 } 06709 06710 06711 /* decode the previously encoded string */ 06712 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06713 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06714 { 06715 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06716 } 06717 else 06718 { 06719 fct_chk( enclen == outlen ); 06720 } 06721 06722 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06723 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06724 { 06725 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06726 } 06727 else 06728 { 06729 fct_chk( outlen == 0 ); 06730 } 06731 06732 06733 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06734 06735 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06736 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06737 FCT_TEST_END(); 06738 #endif /* POLARSSL_AES_C */ 06739 #endif /* POLARSSL_CIPHER_MODE_CTR */ 06740 06741 #ifdef POLARSSL_AES_C 06742 #ifdef POLARSSL_CIPHER_MODE_CTR 06743 06744 FCT_TEST_BGN(aes_encrypt_and_decrypt_31_bytes) 06745 size_t length = 31; 06746 unsigned char key[32]; 06747 unsigned char iv[16]; 06748 06749 const cipher_info_t *cipher_info; 06750 cipher_context_t ctx_dec; 06751 cipher_context_t ctx_enc; 06752 06753 unsigned char inbuf[64]; 06754 unsigned char encbuf[64]; 06755 unsigned char decbuf[64]; 06756 06757 size_t outlen = 0; 06758 size_t enclen = 0; 06759 06760 memset( key, 0, 32 ); 06761 memset( iv , 0, 16 ); 06762 06763 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06764 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06765 06766 memset( inbuf, 5, 64 ); 06767 memset( encbuf, 0, 64 ); 06768 memset( decbuf, 0, 64 ); 06769 06770 /* Check and get info structures */ 06771 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 06772 fct_chk( NULL != cipher_info ); 06773 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 06774 06775 /* Initialise enc and dec contexts */ 06776 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06777 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06778 06779 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 06780 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 06781 06782 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06783 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06784 06785 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06786 { 06787 enclen = cipher_get_block_size( &ctx_enc ) 06788 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06789 } 06790 else 06791 { 06792 enclen = length; 06793 } 06794 06795 /* encode length number of bytes from inbuf */ 06796 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06797 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06798 { 06799 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06800 } 06801 else 06802 { 06803 fct_chk( outlen == enclen ); 06804 } 06805 06806 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06807 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06808 { 06809 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06810 } 06811 else 06812 { 06813 fct_chk( outlen == 0 ); 06814 } 06815 06816 06817 /* decode the previously encoded string */ 06818 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06819 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06820 { 06821 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06822 } 06823 else 06824 { 06825 fct_chk( enclen == outlen ); 06826 } 06827 06828 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06829 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06830 { 06831 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06832 } 06833 else 06834 { 06835 fct_chk( outlen == 0 ); 06836 } 06837 06838 06839 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06840 06841 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06842 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06843 FCT_TEST_END(); 06844 #endif /* POLARSSL_AES_C */ 06845 #endif /* POLARSSL_CIPHER_MODE_CTR */ 06846 06847 #ifdef POLARSSL_AES_C 06848 #ifdef POLARSSL_CIPHER_MODE_CTR 06849 06850 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes) 06851 size_t length = 32; 06852 unsigned char key[32]; 06853 unsigned char iv[16]; 06854 06855 const cipher_info_t *cipher_info; 06856 cipher_context_t ctx_dec; 06857 cipher_context_t ctx_enc; 06858 06859 unsigned char inbuf[64]; 06860 unsigned char encbuf[64]; 06861 unsigned char decbuf[64]; 06862 06863 size_t outlen = 0; 06864 size_t enclen = 0; 06865 06866 memset( key, 0, 32 ); 06867 memset( iv , 0, 16 ); 06868 06869 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06870 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06871 06872 memset( inbuf, 5, 64 ); 06873 memset( encbuf, 0, 64 ); 06874 memset( decbuf, 0, 64 ); 06875 06876 /* Check and get info structures */ 06877 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 06878 fct_chk( NULL != cipher_info ); 06879 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 06880 06881 /* Initialise enc and dec contexts */ 06882 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06883 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06884 06885 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 06886 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 06887 06888 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06889 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06890 06891 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06892 { 06893 enclen = cipher_get_block_size( &ctx_enc ) 06894 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06895 } 06896 else 06897 { 06898 enclen = length; 06899 } 06900 06901 /* encode length number of bytes from inbuf */ 06902 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06903 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06904 { 06905 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06906 } 06907 else 06908 { 06909 fct_chk( outlen == enclen ); 06910 } 06911 06912 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06913 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06914 { 06915 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06916 } 06917 else 06918 { 06919 fct_chk( outlen == 0 ); 06920 } 06921 06922 06923 /* decode the previously encoded string */ 06924 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06925 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06926 { 06927 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06928 } 06929 else 06930 { 06931 fct_chk( enclen == outlen ); 06932 } 06933 06934 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06935 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06936 { 06937 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06938 } 06939 else 06940 { 06941 fct_chk( outlen == 0 ); 06942 } 06943 06944 06945 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06946 06947 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06948 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06949 FCT_TEST_END(); 06950 #endif /* POLARSSL_AES_C */ 06951 #endif /* POLARSSL_CIPHER_MODE_CTR */ 06952 06953 #ifdef POLARSSL_AES_C 06954 #ifdef POLARSSL_CIPHER_MODE_CTR 06955 06956 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes) 06957 size_t length = 33; 06958 unsigned char key[32]; 06959 unsigned char iv[16]; 06960 06961 const cipher_info_t *cipher_info; 06962 cipher_context_t ctx_dec; 06963 cipher_context_t ctx_enc; 06964 06965 unsigned char inbuf[64]; 06966 unsigned char encbuf[64]; 06967 unsigned char decbuf[64]; 06968 06969 size_t outlen = 0; 06970 size_t enclen = 0; 06971 06972 memset( key, 0, 32 ); 06973 memset( iv , 0, 16 ); 06974 06975 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06976 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06977 06978 memset( inbuf, 5, 64 ); 06979 memset( encbuf, 0, 64 ); 06980 memset( decbuf, 0, 64 ); 06981 06982 /* Check and get info structures */ 06983 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 06984 fct_chk( NULL != cipher_info ); 06985 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 06986 06987 /* Initialise enc and dec contexts */ 06988 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06989 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06990 06991 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 06992 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 06993 06994 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06995 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06996 06997 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06998 { 06999 enclen = cipher_get_block_size( &ctx_enc ) 07000 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07001 } 07002 else 07003 { 07004 enclen = length; 07005 } 07006 07007 /* encode length number of bytes from inbuf */ 07008 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 07009 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07010 { 07011 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07012 } 07013 else 07014 { 07015 fct_chk( outlen == enclen ); 07016 } 07017 07018 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 07019 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07020 { 07021 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07022 } 07023 else 07024 { 07025 fct_chk( outlen == 0 ); 07026 } 07027 07028 07029 /* decode the previously encoded string */ 07030 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07031 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07032 { 07033 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07034 } 07035 else 07036 { 07037 fct_chk( enclen == outlen ); 07038 } 07039 07040 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07041 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07042 { 07043 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07044 } 07045 else 07046 { 07047 fct_chk( outlen == 0 ); 07048 } 07049 07050 07051 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07052 07053 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07054 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07055 FCT_TEST_END(); 07056 #endif /* POLARSSL_AES_C */ 07057 #endif /* POLARSSL_CIPHER_MODE_CTR */ 07058 07059 #ifdef POLARSSL_AES_C 07060 #ifdef POLARSSL_CIPHER_MODE_CTR 07061 07062 FCT_TEST_BGN(aes_encrypt_and_decrypt_47_bytes) 07063 size_t length = 47; 07064 unsigned char key[32]; 07065 unsigned char iv[16]; 07066 07067 const cipher_info_t *cipher_info; 07068 cipher_context_t ctx_dec; 07069 cipher_context_t ctx_enc; 07070 07071 unsigned char inbuf[64]; 07072 unsigned char encbuf[64]; 07073 unsigned char decbuf[64]; 07074 07075 size_t outlen = 0; 07076 size_t enclen = 0; 07077 07078 memset( key, 0, 32 ); 07079 memset( iv , 0, 16 ); 07080 07081 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07082 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07083 07084 memset( inbuf, 5, 64 ); 07085 memset( encbuf, 0, 64 ); 07086 memset( decbuf, 0, 64 ); 07087 07088 /* Check and get info structures */ 07089 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 07090 fct_chk( NULL != cipher_info ); 07091 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 07092 07093 /* Initialise enc and dec contexts */ 07094 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07095 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07096 07097 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 07098 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 07099 07100 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07101 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07102 07103 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07104 { 07105 enclen = cipher_get_block_size( &ctx_enc ) 07106 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07107 } 07108 else 07109 { 07110 enclen = length; 07111 } 07112 07113 /* encode length number of bytes from inbuf */ 07114 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 07115 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07116 { 07117 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07118 } 07119 else 07120 { 07121 fct_chk( outlen == enclen ); 07122 } 07123 07124 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 07125 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07126 { 07127 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07128 } 07129 else 07130 { 07131 fct_chk( outlen == 0 ); 07132 } 07133 07134 07135 /* decode the previously encoded string */ 07136 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07137 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07138 { 07139 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07140 } 07141 else 07142 { 07143 fct_chk( enclen == outlen ); 07144 } 07145 07146 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07147 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07148 { 07149 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07150 } 07151 else 07152 { 07153 fct_chk( outlen == 0 ); 07154 } 07155 07156 07157 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07158 07159 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07160 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07161 FCT_TEST_END(); 07162 #endif /* POLARSSL_AES_C */ 07163 #endif /* POLARSSL_CIPHER_MODE_CTR */ 07164 07165 #ifdef POLARSSL_AES_C 07166 #ifdef POLARSSL_CIPHER_MODE_CTR 07167 07168 FCT_TEST_BGN(aes_encrypt_and_decrypt_48_bytes) 07169 size_t length = 48; 07170 unsigned char key[32]; 07171 unsigned char iv[16]; 07172 07173 const cipher_info_t *cipher_info; 07174 cipher_context_t ctx_dec; 07175 cipher_context_t ctx_enc; 07176 07177 unsigned char inbuf[64]; 07178 unsigned char encbuf[64]; 07179 unsigned char decbuf[64]; 07180 07181 size_t outlen = 0; 07182 size_t enclen = 0; 07183 07184 memset( key, 0, 32 ); 07185 memset( iv , 0, 16 ); 07186 07187 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07188 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07189 07190 memset( inbuf, 5, 64 ); 07191 memset( encbuf, 0, 64 ); 07192 memset( decbuf, 0, 64 ); 07193 07194 /* Check and get info structures */ 07195 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 07196 fct_chk( NULL != cipher_info ); 07197 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 07198 07199 /* Initialise enc and dec contexts */ 07200 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07201 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07202 07203 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 07204 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 07205 07206 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07207 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07208 07209 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07210 { 07211 enclen = cipher_get_block_size( &ctx_enc ) 07212 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07213 } 07214 else 07215 { 07216 enclen = length; 07217 } 07218 07219 /* encode length number of bytes from inbuf */ 07220 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 07221 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07222 { 07223 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07224 } 07225 else 07226 { 07227 fct_chk( outlen == enclen ); 07228 } 07229 07230 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 07231 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07232 { 07233 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07234 } 07235 else 07236 { 07237 fct_chk( outlen == 0 ); 07238 } 07239 07240 07241 /* decode the previously encoded string */ 07242 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07243 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07244 { 07245 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07246 } 07247 else 07248 { 07249 fct_chk( enclen == outlen ); 07250 } 07251 07252 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07253 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07254 { 07255 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07256 } 07257 else 07258 { 07259 fct_chk( outlen == 0 ); 07260 } 07261 07262 07263 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07264 07265 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07266 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07267 FCT_TEST_END(); 07268 #endif /* POLARSSL_AES_C */ 07269 #endif /* POLARSSL_CIPHER_MODE_CTR */ 07270 07271 #ifdef POLARSSL_AES_C 07272 #ifdef POLARSSL_CIPHER_MODE_CTR 07273 07274 FCT_TEST_BGN(aes_encrypt_and_decrypt_49_bytes) 07275 size_t length = 49; 07276 unsigned char key[32]; 07277 unsigned char iv[16]; 07278 07279 const cipher_info_t *cipher_info; 07280 cipher_context_t ctx_dec; 07281 cipher_context_t ctx_enc; 07282 07283 unsigned char inbuf[64]; 07284 unsigned char encbuf[64]; 07285 unsigned char decbuf[64]; 07286 07287 size_t outlen = 0; 07288 size_t enclen = 0; 07289 07290 memset( key, 0, 32 ); 07291 memset( iv , 0, 16 ); 07292 07293 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07294 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07295 07296 memset( inbuf, 5, 64 ); 07297 memset( encbuf, 0, 64 ); 07298 memset( decbuf, 0, 64 ); 07299 07300 /* Check and get info structures */ 07301 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 07302 fct_chk( NULL != cipher_info ); 07303 fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info ); 07304 07305 /* Initialise enc and dec contexts */ 07306 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07307 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07308 07309 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 07310 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 07311 07312 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07313 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07314 07315 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07316 { 07317 enclen = cipher_get_block_size( &ctx_enc ) 07318 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07319 } 07320 else 07321 { 07322 enclen = length; 07323 } 07324 07325 /* encode length number of bytes from inbuf */ 07326 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 07327 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07328 { 07329 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07330 } 07331 else 07332 { 07333 fct_chk( outlen == enclen ); 07334 } 07335 07336 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 07337 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07338 { 07339 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07340 } 07341 else 07342 { 07343 fct_chk( outlen == 0 ); 07344 } 07345 07346 07347 /* decode the previously encoded string */ 07348 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07349 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07350 { 07351 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07352 } 07353 else 07354 { 07355 fct_chk( enclen == outlen ); 07356 } 07357 07358 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07359 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07360 { 07361 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07362 } 07363 else 07364 { 07365 fct_chk( outlen == 0 ); 07366 } 07367 07368 07369 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07370 07371 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07372 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07373 FCT_TEST_END(); 07374 #endif /* POLARSSL_AES_C */ 07375 #endif /* POLARSSL_CIPHER_MODE_CTR */ 07376 07377 #ifdef POLARSSL_AES_C 07378 #ifdef POLARSSL_CIPHER_MODE_CTR 07379 07380 FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes_in_multiple_parts) 07381 size_t first_length = 0; 07382 size_t second_length = 0; 07383 size_t length = first_length + second_length; 07384 unsigned char key[32]; 07385 unsigned char iv[16]; 07386 07387 cipher_context_t ctx_dec; 07388 cipher_context_t ctx_enc; 07389 const cipher_info_t *cipher_info; 07390 07391 unsigned char inbuf[64]; 07392 unsigned char encbuf[64]; 07393 unsigned char decbuf[64]; 07394 07395 size_t outlen = 0; 07396 size_t totaloutlen = 0; 07397 size_t enclen = 0; 07398 07399 memset( key, 0, 32 ); 07400 memset( iv , 0, 16 ); 07401 07402 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07403 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07404 07405 memset( inbuf, 5, 64 ); 07406 memset( encbuf, 0, 64 ); 07407 memset( decbuf, 0, 64 ); 07408 07409 /* Initialise enc and dec contexts */ 07410 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 07411 fct_chk( NULL != cipher_info); 07412 07413 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07414 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07415 07416 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 07417 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 07418 07419 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07420 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07421 07422 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07423 { 07424 enclen = cipher_get_block_size(&ctx_enc ) 07425 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07426 } 07427 else 07428 { 07429 enclen = length; 07430 } 07431 07432 /* encode length number of bytes from inbuf */ 07433 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07434 totaloutlen = outlen; 07435 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07436 totaloutlen += outlen; 07437 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07438 { 07439 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07440 } 07441 else 07442 { 07443 fct_chk( totaloutlen == enclen ); 07444 } 07445 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07446 totaloutlen += outlen; 07447 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07448 { 07449 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07450 } 07451 else 07452 { 07453 fct_chk( outlen == 0 ); 07454 } 07455 07456 /* decode the previously encoded string */ 07457 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07458 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07459 { 07460 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07461 } 07462 else 07463 { 07464 fct_chk( enclen == outlen ); 07465 } 07466 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07467 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07468 { 07469 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07470 } 07471 else 07472 { 07473 fct_chk( outlen == 0 ); 07474 } 07475 07476 07477 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07478 07479 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07480 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07481 FCT_TEST_END(); 07482 #endif /* POLARSSL_AES_C */ 07483 #endif /* POLARSSL_CIPHER_MODE_CTR */ 07484 07485 #ifdef POLARSSL_AES_C 07486 #ifdef POLARSSL_CIPHER_MODE_CTR 07487 07488 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_1) 07489 size_t first_length = 1; 07490 size_t second_length = 0; 07491 size_t length = first_length + second_length; 07492 unsigned char key[32]; 07493 unsigned char iv[16]; 07494 07495 cipher_context_t ctx_dec; 07496 cipher_context_t ctx_enc; 07497 const cipher_info_t *cipher_info; 07498 07499 unsigned char inbuf[64]; 07500 unsigned char encbuf[64]; 07501 unsigned char decbuf[64]; 07502 07503 size_t outlen = 0; 07504 size_t totaloutlen = 0; 07505 size_t enclen = 0; 07506 07507 memset( key, 0, 32 ); 07508 memset( iv , 0, 16 ); 07509 07510 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07511 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07512 07513 memset( inbuf, 5, 64 ); 07514 memset( encbuf, 0, 64 ); 07515 memset( decbuf, 0, 64 ); 07516 07517 /* Initialise enc and dec contexts */ 07518 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 07519 fct_chk( NULL != cipher_info); 07520 07521 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07522 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07523 07524 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 07525 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 07526 07527 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07528 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07529 07530 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07531 { 07532 enclen = cipher_get_block_size(&ctx_enc ) 07533 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07534 } 07535 else 07536 { 07537 enclen = length; 07538 } 07539 07540 /* encode length number of bytes from inbuf */ 07541 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07542 totaloutlen = outlen; 07543 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07544 totaloutlen += outlen; 07545 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07546 { 07547 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07548 } 07549 else 07550 { 07551 fct_chk( totaloutlen == enclen ); 07552 } 07553 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07554 totaloutlen += outlen; 07555 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07556 { 07557 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07558 } 07559 else 07560 { 07561 fct_chk( outlen == 0 ); 07562 } 07563 07564 /* decode the previously encoded string */ 07565 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07566 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07567 { 07568 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07569 } 07570 else 07571 { 07572 fct_chk( enclen == outlen ); 07573 } 07574 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07575 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07576 { 07577 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07578 } 07579 else 07580 { 07581 fct_chk( outlen == 0 ); 07582 } 07583 07584 07585 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07586 07587 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07588 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07589 FCT_TEST_END(); 07590 #endif /* POLARSSL_AES_C */ 07591 #endif /* POLARSSL_CIPHER_MODE_CTR */ 07592 07593 #ifdef POLARSSL_AES_C 07594 #ifdef POLARSSL_CIPHER_MODE_CTR 07595 07596 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_2) 07597 size_t first_length = 0; 07598 size_t second_length = 1; 07599 size_t length = first_length + second_length; 07600 unsigned char key[32]; 07601 unsigned char iv[16]; 07602 07603 cipher_context_t ctx_dec; 07604 cipher_context_t ctx_enc; 07605 const cipher_info_t *cipher_info; 07606 07607 unsigned char inbuf[64]; 07608 unsigned char encbuf[64]; 07609 unsigned char decbuf[64]; 07610 07611 size_t outlen = 0; 07612 size_t totaloutlen = 0; 07613 size_t enclen = 0; 07614 07615 memset( key, 0, 32 ); 07616 memset( iv , 0, 16 ); 07617 07618 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07619 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07620 07621 memset( inbuf, 5, 64 ); 07622 memset( encbuf, 0, 64 ); 07623 memset( decbuf, 0, 64 ); 07624 07625 /* Initialise enc and dec contexts */ 07626 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 07627 fct_chk( NULL != cipher_info); 07628 07629 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07630 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07631 07632 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 07633 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 07634 07635 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07636 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07637 07638 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07639 { 07640 enclen = cipher_get_block_size(&ctx_enc ) 07641 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07642 } 07643 else 07644 { 07645 enclen = length; 07646 } 07647 07648 /* encode length number of bytes from inbuf */ 07649 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07650 totaloutlen = outlen; 07651 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07652 totaloutlen += outlen; 07653 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07654 { 07655 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07656 } 07657 else 07658 { 07659 fct_chk( totaloutlen == enclen ); 07660 } 07661 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07662 totaloutlen += outlen; 07663 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07664 { 07665 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07666 } 07667 else 07668 { 07669 fct_chk( outlen == 0 ); 07670 } 07671 07672 /* decode the previously encoded string */ 07673 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07674 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07675 { 07676 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07677 } 07678 else 07679 { 07680 fct_chk( enclen == outlen ); 07681 } 07682 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07683 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07684 { 07685 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07686 } 07687 else 07688 { 07689 fct_chk( outlen == 0 ); 07690 } 07691 07692 07693 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07694 07695 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07696 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07697 FCT_TEST_END(); 07698 #endif /* POLARSSL_AES_C */ 07699 #endif /* POLARSSL_CIPHER_MODE_CTR */ 07700 07701 #ifdef POLARSSL_AES_C 07702 #ifdef POLARSSL_CIPHER_MODE_CTR 07703 07704 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_1) 07705 size_t first_length = 16; 07706 size_t second_length = 0; 07707 size_t length = first_length + second_length; 07708 unsigned char key[32]; 07709 unsigned char iv[16]; 07710 07711 cipher_context_t ctx_dec; 07712 cipher_context_t ctx_enc; 07713 const cipher_info_t *cipher_info; 07714 07715 unsigned char inbuf[64]; 07716 unsigned char encbuf[64]; 07717 unsigned char decbuf[64]; 07718 07719 size_t outlen = 0; 07720 size_t totaloutlen = 0; 07721 size_t enclen = 0; 07722 07723 memset( key, 0, 32 ); 07724 memset( iv , 0, 16 ); 07725 07726 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07727 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07728 07729 memset( inbuf, 5, 64 ); 07730 memset( encbuf, 0, 64 ); 07731 memset( decbuf, 0, 64 ); 07732 07733 /* Initialise enc and dec contexts */ 07734 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 07735 fct_chk( NULL != cipher_info); 07736 07737 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07738 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07739 07740 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 07741 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 07742 07743 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07744 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07745 07746 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07747 { 07748 enclen = cipher_get_block_size(&ctx_enc ) 07749 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07750 } 07751 else 07752 { 07753 enclen = length; 07754 } 07755 07756 /* encode length number of bytes from inbuf */ 07757 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07758 totaloutlen = outlen; 07759 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07760 totaloutlen += outlen; 07761 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07762 { 07763 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07764 } 07765 else 07766 { 07767 fct_chk( totaloutlen == enclen ); 07768 } 07769 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07770 totaloutlen += outlen; 07771 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07772 { 07773 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07774 } 07775 else 07776 { 07777 fct_chk( outlen == 0 ); 07778 } 07779 07780 /* decode the previously encoded string */ 07781 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07782 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07783 { 07784 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07785 } 07786 else 07787 { 07788 fct_chk( enclen == outlen ); 07789 } 07790 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07791 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07792 { 07793 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07794 } 07795 else 07796 { 07797 fct_chk( outlen == 0 ); 07798 } 07799 07800 07801 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07802 07803 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07804 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07805 FCT_TEST_END(); 07806 #endif /* POLARSSL_AES_C */ 07807 #endif /* POLARSSL_CIPHER_MODE_CTR */ 07808 07809 #ifdef POLARSSL_AES_C 07810 #ifdef POLARSSL_CIPHER_MODE_CTR 07811 07812 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_2) 07813 size_t first_length = 0; 07814 size_t second_length = 16; 07815 size_t length = first_length + second_length; 07816 unsigned char key[32]; 07817 unsigned char iv[16]; 07818 07819 cipher_context_t ctx_dec; 07820 cipher_context_t ctx_enc; 07821 const cipher_info_t *cipher_info; 07822 07823 unsigned char inbuf[64]; 07824 unsigned char encbuf[64]; 07825 unsigned char decbuf[64]; 07826 07827 size_t outlen = 0; 07828 size_t totaloutlen = 0; 07829 size_t enclen = 0; 07830 07831 memset( key, 0, 32 ); 07832 memset( iv , 0, 16 ); 07833 07834 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07835 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07836 07837 memset( inbuf, 5, 64 ); 07838 memset( encbuf, 0, 64 ); 07839 memset( decbuf, 0, 64 ); 07840 07841 /* Initialise enc and dec contexts */ 07842 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 07843 fct_chk( NULL != cipher_info); 07844 07845 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07846 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07847 07848 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 07849 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 07850 07851 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07852 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07853 07854 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07855 { 07856 enclen = cipher_get_block_size(&ctx_enc ) 07857 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07858 } 07859 else 07860 { 07861 enclen = length; 07862 } 07863 07864 /* encode length number of bytes from inbuf */ 07865 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07866 totaloutlen = outlen; 07867 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07868 totaloutlen += outlen; 07869 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07870 { 07871 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07872 } 07873 else 07874 { 07875 fct_chk( totaloutlen == enclen ); 07876 } 07877 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07878 totaloutlen += outlen; 07879 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07880 { 07881 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07882 } 07883 else 07884 { 07885 fct_chk( outlen == 0 ); 07886 } 07887 07888 /* decode the previously encoded string */ 07889 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07890 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07891 { 07892 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07893 } 07894 else 07895 { 07896 fct_chk( enclen == outlen ); 07897 } 07898 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07899 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07900 { 07901 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07902 } 07903 else 07904 { 07905 fct_chk( outlen == 0 ); 07906 } 07907 07908 07909 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07910 07911 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07912 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07913 FCT_TEST_END(); 07914 #endif /* POLARSSL_AES_C */ 07915 #endif /* POLARSSL_CIPHER_MODE_CTR */ 07916 07917 #ifdef POLARSSL_AES_C 07918 #ifdef POLARSSL_CIPHER_MODE_CTR 07919 07920 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_3) 07921 size_t first_length = 1; 07922 size_t second_length = 15; 07923 size_t length = first_length + second_length; 07924 unsigned char key[32]; 07925 unsigned char iv[16]; 07926 07927 cipher_context_t ctx_dec; 07928 cipher_context_t ctx_enc; 07929 const cipher_info_t *cipher_info; 07930 07931 unsigned char inbuf[64]; 07932 unsigned char encbuf[64]; 07933 unsigned char decbuf[64]; 07934 07935 size_t outlen = 0; 07936 size_t totaloutlen = 0; 07937 size_t enclen = 0; 07938 07939 memset( key, 0, 32 ); 07940 memset( iv , 0, 16 ); 07941 07942 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07943 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07944 07945 memset( inbuf, 5, 64 ); 07946 memset( encbuf, 0, 64 ); 07947 memset( decbuf, 0, 64 ); 07948 07949 /* Initialise enc and dec contexts */ 07950 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 07951 fct_chk( NULL != cipher_info); 07952 07953 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07954 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07955 07956 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 07957 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 07958 07959 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07960 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07961 07962 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07963 { 07964 enclen = cipher_get_block_size(&ctx_enc ) 07965 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07966 } 07967 else 07968 { 07969 enclen = length; 07970 } 07971 07972 /* encode length number of bytes from inbuf */ 07973 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07974 totaloutlen = outlen; 07975 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07976 totaloutlen += outlen; 07977 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07978 { 07979 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07980 } 07981 else 07982 { 07983 fct_chk( totaloutlen == enclen ); 07984 } 07985 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07986 totaloutlen += outlen; 07987 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07988 { 07989 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07990 } 07991 else 07992 { 07993 fct_chk( outlen == 0 ); 07994 } 07995 07996 /* decode the previously encoded string */ 07997 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07998 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07999 { 08000 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08001 } 08002 else 08003 { 08004 fct_chk( enclen == outlen ); 08005 } 08006 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08007 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08008 { 08009 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08010 } 08011 else 08012 { 08013 fct_chk( outlen == 0 ); 08014 } 08015 08016 08017 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08018 08019 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08020 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08021 FCT_TEST_END(); 08022 #endif /* POLARSSL_AES_C */ 08023 #endif /* POLARSSL_CIPHER_MODE_CTR */ 08024 08025 #ifdef POLARSSL_AES_C 08026 #ifdef POLARSSL_CIPHER_MODE_CTR 08027 08028 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_4) 08029 size_t first_length = 15; 08030 size_t second_length = 1; 08031 size_t length = first_length + second_length; 08032 unsigned char key[32]; 08033 unsigned char iv[16]; 08034 08035 cipher_context_t ctx_dec; 08036 cipher_context_t ctx_enc; 08037 const cipher_info_t *cipher_info; 08038 08039 unsigned char inbuf[64]; 08040 unsigned char encbuf[64]; 08041 unsigned char decbuf[64]; 08042 08043 size_t outlen = 0; 08044 size_t totaloutlen = 0; 08045 size_t enclen = 0; 08046 08047 memset( key, 0, 32 ); 08048 memset( iv , 0, 16 ); 08049 08050 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08051 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08052 08053 memset( inbuf, 5, 64 ); 08054 memset( encbuf, 0, 64 ); 08055 memset( decbuf, 0, 64 ); 08056 08057 /* Initialise enc and dec contexts */ 08058 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 08059 fct_chk( NULL != cipher_info); 08060 08061 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08062 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08063 08064 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 08065 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 08066 08067 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08068 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08069 08070 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08071 { 08072 enclen = cipher_get_block_size(&ctx_enc ) 08073 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08074 } 08075 else 08076 { 08077 enclen = length; 08078 } 08079 08080 /* encode length number of bytes from inbuf */ 08081 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 08082 totaloutlen = outlen; 08083 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 08084 totaloutlen += outlen; 08085 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08086 { 08087 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08088 } 08089 else 08090 { 08091 fct_chk( totaloutlen == enclen ); 08092 } 08093 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 08094 totaloutlen += outlen; 08095 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08096 { 08097 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08098 } 08099 else 08100 { 08101 fct_chk( outlen == 0 ); 08102 } 08103 08104 /* decode the previously encoded string */ 08105 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08106 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08107 { 08108 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08109 } 08110 else 08111 { 08112 fct_chk( enclen == outlen ); 08113 } 08114 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08115 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08116 { 08117 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08118 } 08119 else 08120 { 08121 fct_chk( outlen == 0 ); 08122 } 08123 08124 08125 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08126 08127 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08128 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08129 FCT_TEST_END(); 08130 #endif /* POLARSSL_AES_C */ 08131 #endif /* POLARSSL_CIPHER_MODE_CTR */ 08132 08133 #ifdef POLARSSL_AES_C 08134 #ifdef POLARSSL_CIPHER_MODE_CTR 08135 08136 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 08137 size_t first_length = 15; 08138 size_t second_length = 7; 08139 size_t length = first_length + second_length; 08140 unsigned char key[32]; 08141 unsigned char iv[16]; 08142 08143 cipher_context_t ctx_dec; 08144 cipher_context_t ctx_enc; 08145 const cipher_info_t *cipher_info; 08146 08147 unsigned char inbuf[64]; 08148 unsigned char encbuf[64]; 08149 unsigned char decbuf[64]; 08150 08151 size_t outlen = 0; 08152 size_t totaloutlen = 0; 08153 size_t enclen = 0; 08154 08155 memset( key, 0, 32 ); 08156 memset( iv , 0, 16 ); 08157 08158 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08159 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08160 08161 memset( inbuf, 5, 64 ); 08162 memset( encbuf, 0, 64 ); 08163 memset( decbuf, 0, 64 ); 08164 08165 /* Initialise enc and dec contexts */ 08166 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 08167 fct_chk( NULL != cipher_info); 08168 08169 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08170 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08171 08172 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 08173 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 08174 08175 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08176 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08177 08178 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08179 { 08180 enclen = cipher_get_block_size(&ctx_enc ) 08181 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08182 } 08183 else 08184 { 08185 enclen = length; 08186 } 08187 08188 /* encode length number of bytes from inbuf */ 08189 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 08190 totaloutlen = outlen; 08191 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 08192 totaloutlen += outlen; 08193 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08194 { 08195 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08196 } 08197 else 08198 { 08199 fct_chk( totaloutlen == enclen ); 08200 } 08201 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 08202 totaloutlen += outlen; 08203 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08204 { 08205 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08206 } 08207 else 08208 { 08209 fct_chk( outlen == 0 ); 08210 } 08211 08212 /* decode the previously encoded string */ 08213 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08214 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08215 { 08216 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08217 } 08218 else 08219 { 08220 fct_chk( enclen == outlen ); 08221 } 08222 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08223 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08224 { 08225 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08226 } 08227 else 08228 { 08229 fct_chk( outlen == 0 ); 08230 } 08231 08232 08233 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08234 08235 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08236 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08237 FCT_TEST_END(); 08238 #endif /* POLARSSL_AES_C */ 08239 #endif /* POLARSSL_CIPHER_MODE_CTR */ 08240 08241 #ifdef POLARSSL_AES_C 08242 #ifdef POLARSSL_CIPHER_MODE_CTR 08243 08244 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 08245 size_t first_length = 16; 08246 size_t second_length = 6; 08247 size_t length = first_length + second_length; 08248 unsigned char key[32]; 08249 unsigned char iv[16]; 08250 08251 cipher_context_t ctx_dec; 08252 cipher_context_t ctx_enc; 08253 const cipher_info_t *cipher_info; 08254 08255 unsigned char inbuf[64]; 08256 unsigned char encbuf[64]; 08257 unsigned char decbuf[64]; 08258 08259 size_t outlen = 0; 08260 size_t totaloutlen = 0; 08261 size_t enclen = 0; 08262 08263 memset( key, 0, 32 ); 08264 memset( iv , 0, 16 ); 08265 08266 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08267 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08268 08269 memset( inbuf, 5, 64 ); 08270 memset( encbuf, 0, 64 ); 08271 memset( decbuf, 0, 64 ); 08272 08273 /* Initialise enc and dec contexts */ 08274 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 08275 fct_chk( NULL != cipher_info); 08276 08277 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08278 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08279 08280 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 08281 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 08282 08283 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08284 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08285 08286 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08287 { 08288 enclen = cipher_get_block_size(&ctx_enc ) 08289 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08290 } 08291 else 08292 { 08293 enclen = length; 08294 } 08295 08296 /* encode length number of bytes from inbuf */ 08297 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 08298 totaloutlen = outlen; 08299 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 08300 totaloutlen += outlen; 08301 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08302 { 08303 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08304 } 08305 else 08306 { 08307 fct_chk( totaloutlen == enclen ); 08308 } 08309 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 08310 totaloutlen += outlen; 08311 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08312 { 08313 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08314 } 08315 else 08316 { 08317 fct_chk( outlen == 0 ); 08318 } 08319 08320 /* decode the previously encoded string */ 08321 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08322 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08323 { 08324 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08325 } 08326 else 08327 { 08328 fct_chk( enclen == outlen ); 08329 } 08330 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08331 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08332 { 08333 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08334 } 08335 else 08336 { 08337 fct_chk( outlen == 0 ); 08338 } 08339 08340 08341 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08342 08343 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08344 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08345 FCT_TEST_END(); 08346 #endif /* POLARSSL_AES_C */ 08347 #endif /* POLARSSL_CIPHER_MODE_CTR */ 08348 08349 #ifdef POLARSSL_AES_C 08350 #ifdef POLARSSL_CIPHER_MODE_CTR 08351 08352 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 08353 size_t first_length = 17; 08354 size_t second_length = 6; 08355 size_t length = first_length + second_length; 08356 unsigned char key[32]; 08357 unsigned char iv[16]; 08358 08359 cipher_context_t ctx_dec; 08360 cipher_context_t ctx_enc; 08361 const cipher_info_t *cipher_info; 08362 08363 unsigned char inbuf[64]; 08364 unsigned char encbuf[64]; 08365 unsigned char decbuf[64]; 08366 08367 size_t outlen = 0; 08368 size_t totaloutlen = 0; 08369 size_t enclen = 0; 08370 08371 memset( key, 0, 32 ); 08372 memset( iv , 0, 16 ); 08373 08374 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08375 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08376 08377 memset( inbuf, 5, 64 ); 08378 memset( encbuf, 0, 64 ); 08379 memset( decbuf, 0, 64 ); 08380 08381 /* Initialise enc and dec contexts */ 08382 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 08383 fct_chk( NULL != cipher_info); 08384 08385 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08386 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08387 08388 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 08389 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 08390 08391 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08392 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08393 08394 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08395 { 08396 enclen = cipher_get_block_size(&ctx_enc ) 08397 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08398 } 08399 else 08400 { 08401 enclen = length; 08402 } 08403 08404 /* encode length number of bytes from inbuf */ 08405 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 08406 totaloutlen = outlen; 08407 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 08408 totaloutlen += outlen; 08409 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08410 { 08411 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08412 } 08413 else 08414 { 08415 fct_chk( totaloutlen == enclen ); 08416 } 08417 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 08418 totaloutlen += outlen; 08419 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08420 { 08421 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08422 } 08423 else 08424 { 08425 fct_chk( outlen == 0 ); 08426 } 08427 08428 /* decode the previously encoded string */ 08429 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08430 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08431 { 08432 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08433 } 08434 else 08435 { 08436 fct_chk( enclen == outlen ); 08437 } 08438 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08439 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08440 { 08441 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08442 } 08443 else 08444 { 08445 fct_chk( outlen == 0 ); 08446 } 08447 08448 08449 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08450 08451 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08452 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08453 FCT_TEST_END(); 08454 #endif /* POLARSSL_AES_C */ 08455 #endif /* POLARSSL_CIPHER_MODE_CTR */ 08456 08457 #ifdef POLARSSL_AES_C 08458 #ifdef POLARSSL_CIPHER_MODE_CTR 08459 08460 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes_in_multiple_parts_1) 08461 size_t first_length = 16; 08462 size_t second_length = 16; 08463 size_t length = first_length + second_length; 08464 unsigned char key[32]; 08465 unsigned char iv[16]; 08466 08467 cipher_context_t ctx_dec; 08468 cipher_context_t ctx_enc; 08469 const cipher_info_t *cipher_info; 08470 08471 unsigned char inbuf[64]; 08472 unsigned char encbuf[64]; 08473 unsigned char decbuf[64]; 08474 08475 size_t outlen = 0; 08476 size_t totaloutlen = 0; 08477 size_t enclen = 0; 08478 08479 memset( key, 0, 32 ); 08480 memset( iv , 0, 16 ); 08481 08482 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08483 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08484 08485 memset( inbuf, 5, 64 ); 08486 memset( encbuf, 0, 64 ); 08487 memset( decbuf, 0, 64 ); 08488 08489 /* Initialise enc and dec contexts */ 08490 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); 08491 fct_chk( NULL != cipher_info); 08492 08493 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08494 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08495 08496 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); 08497 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) ); 08498 08499 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08500 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08501 08502 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08503 { 08504 enclen = cipher_get_block_size(&ctx_enc ) 08505 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08506 } 08507 else 08508 { 08509 enclen = length; 08510 } 08511 08512 /* encode length number of bytes from inbuf */ 08513 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 08514 totaloutlen = outlen; 08515 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 08516 totaloutlen += outlen; 08517 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08518 { 08519 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08520 } 08521 else 08522 { 08523 fct_chk( totaloutlen == enclen ); 08524 } 08525 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 08526 totaloutlen += outlen; 08527 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08528 { 08529 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08530 } 08531 else 08532 { 08533 fct_chk( outlen == 0 ); 08534 } 08535 08536 /* decode the previously encoded string */ 08537 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08538 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08539 { 08540 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08541 } 08542 else 08543 { 08544 fct_chk( enclen == outlen ); 08545 } 08546 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08547 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08548 { 08549 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08550 } 08551 else 08552 { 08553 fct_chk( outlen == 0 ); 08554 } 08555 08556 08557 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08558 08559 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08560 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08561 FCT_TEST_END(); 08562 #endif /* POLARSSL_AES_C */ 08563 #endif /* POLARSSL_CIPHER_MODE_CTR */ 08564 08565 #ifdef POLARSSL_AES_C 08566 08567 FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes) 08568 size_t length = 0; 08569 unsigned char key[32]; 08570 unsigned char iv[16]; 08571 08572 const cipher_info_t *cipher_info; 08573 cipher_context_t ctx_dec; 08574 cipher_context_t ctx_enc; 08575 08576 unsigned char inbuf[64]; 08577 unsigned char encbuf[64]; 08578 unsigned char decbuf[64]; 08579 08580 size_t outlen = 0; 08581 size_t enclen = 0; 08582 08583 memset( key, 0, 32 ); 08584 memset( iv , 0, 16 ); 08585 08586 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08587 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08588 08589 memset( inbuf, 5, 64 ); 08590 memset( encbuf, 0, 64 ); 08591 memset( decbuf, 0, 64 ); 08592 08593 /* Check and get info structures */ 08594 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 08595 fct_chk( NULL != cipher_info ); 08596 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 08597 08598 /* Initialise enc and dec contexts */ 08599 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08600 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08601 08602 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 08603 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 08604 08605 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08606 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08607 08608 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08609 { 08610 enclen = cipher_get_block_size( &ctx_enc ) 08611 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08612 } 08613 else 08614 { 08615 enclen = length; 08616 } 08617 08618 /* encode length number of bytes from inbuf */ 08619 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 08620 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08621 { 08622 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08623 } 08624 else 08625 { 08626 fct_chk( outlen == enclen ); 08627 } 08628 08629 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 08630 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08631 { 08632 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08633 } 08634 else 08635 { 08636 fct_chk( outlen == 0 ); 08637 } 08638 08639 08640 /* decode the previously encoded string */ 08641 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08642 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08643 { 08644 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08645 } 08646 else 08647 { 08648 fct_chk( enclen == outlen ); 08649 } 08650 08651 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08652 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08653 { 08654 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08655 } 08656 else 08657 { 08658 fct_chk( outlen == 0 ); 08659 } 08660 08661 08662 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08663 08664 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08665 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08666 FCT_TEST_END(); 08667 #endif /* POLARSSL_AES_C */ 08668 08669 #ifdef POLARSSL_AES_C 08670 08671 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_byte) 08672 size_t length = 1; 08673 unsigned char key[32]; 08674 unsigned char iv[16]; 08675 08676 const cipher_info_t *cipher_info; 08677 cipher_context_t ctx_dec; 08678 cipher_context_t ctx_enc; 08679 08680 unsigned char inbuf[64]; 08681 unsigned char encbuf[64]; 08682 unsigned char decbuf[64]; 08683 08684 size_t outlen = 0; 08685 size_t enclen = 0; 08686 08687 memset( key, 0, 32 ); 08688 memset( iv , 0, 16 ); 08689 08690 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08691 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08692 08693 memset( inbuf, 5, 64 ); 08694 memset( encbuf, 0, 64 ); 08695 memset( decbuf, 0, 64 ); 08696 08697 /* Check and get info structures */ 08698 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 08699 fct_chk( NULL != cipher_info ); 08700 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 08701 08702 /* Initialise enc and dec contexts */ 08703 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08704 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08705 08706 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 08707 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 08708 08709 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08710 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08711 08712 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08713 { 08714 enclen = cipher_get_block_size( &ctx_enc ) 08715 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08716 } 08717 else 08718 { 08719 enclen = length; 08720 } 08721 08722 /* encode length number of bytes from inbuf */ 08723 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 08724 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08725 { 08726 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08727 } 08728 else 08729 { 08730 fct_chk( outlen == enclen ); 08731 } 08732 08733 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 08734 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08735 { 08736 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08737 } 08738 else 08739 { 08740 fct_chk( outlen == 0 ); 08741 } 08742 08743 08744 /* decode the previously encoded string */ 08745 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08746 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08747 { 08748 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08749 } 08750 else 08751 { 08752 fct_chk( enclen == outlen ); 08753 } 08754 08755 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08756 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08757 { 08758 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08759 } 08760 else 08761 { 08762 fct_chk( outlen == 0 ); 08763 } 08764 08765 08766 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08767 08768 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08769 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08770 FCT_TEST_END(); 08771 #endif /* POLARSSL_AES_C */ 08772 08773 #ifdef POLARSSL_AES_C 08774 08775 FCT_TEST_BGN(aes_encrypt_and_decrypt_2_bytes) 08776 size_t length = 2; 08777 unsigned char key[32]; 08778 unsigned char iv[16]; 08779 08780 const cipher_info_t *cipher_info; 08781 cipher_context_t ctx_dec; 08782 cipher_context_t ctx_enc; 08783 08784 unsigned char inbuf[64]; 08785 unsigned char encbuf[64]; 08786 unsigned char decbuf[64]; 08787 08788 size_t outlen = 0; 08789 size_t enclen = 0; 08790 08791 memset( key, 0, 32 ); 08792 memset( iv , 0, 16 ); 08793 08794 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08795 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08796 08797 memset( inbuf, 5, 64 ); 08798 memset( encbuf, 0, 64 ); 08799 memset( decbuf, 0, 64 ); 08800 08801 /* Check and get info structures */ 08802 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 08803 fct_chk( NULL != cipher_info ); 08804 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 08805 08806 /* Initialise enc and dec contexts */ 08807 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08808 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08809 08810 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 08811 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 08812 08813 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08814 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08815 08816 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08817 { 08818 enclen = cipher_get_block_size( &ctx_enc ) 08819 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08820 } 08821 else 08822 { 08823 enclen = length; 08824 } 08825 08826 /* encode length number of bytes from inbuf */ 08827 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 08828 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08829 { 08830 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08831 } 08832 else 08833 { 08834 fct_chk( outlen == enclen ); 08835 } 08836 08837 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 08838 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08839 { 08840 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08841 } 08842 else 08843 { 08844 fct_chk( outlen == 0 ); 08845 } 08846 08847 08848 /* decode the previously encoded string */ 08849 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08850 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08851 { 08852 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08853 } 08854 else 08855 { 08856 fct_chk( enclen == outlen ); 08857 } 08858 08859 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08860 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08861 { 08862 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08863 } 08864 else 08865 { 08866 fct_chk( outlen == 0 ); 08867 } 08868 08869 08870 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08871 08872 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08873 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08874 FCT_TEST_END(); 08875 #endif /* POLARSSL_AES_C */ 08876 08877 #ifdef POLARSSL_AES_C 08878 08879 FCT_TEST_BGN(aes_encrypt_and_decrypt_7_bytes) 08880 size_t length = 7; 08881 unsigned char key[32]; 08882 unsigned char iv[16]; 08883 08884 const cipher_info_t *cipher_info; 08885 cipher_context_t ctx_dec; 08886 cipher_context_t ctx_enc; 08887 08888 unsigned char inbuf[64]; 08889 unsigned char encbuf[64]; 08890 unsigned char decbuf[64]; 08891 08892 size_t outlen = 0; 08893 size_t enclen = 0; 08894 08895 memset( key, 0, 32 ); 08896 memset( iv , 0, 16 ); 08897 08898 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08899 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08900 08901 memset( inbuf, 5, 64 ); 08902 memset( encbuf, 0, 64 ); 08903 memset( decbuf, 0, 64 ); 08904 08905 /* Check and get info structures */ 08906 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 08907 fct_chk( NULL != cipher_info ); 08908 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 08909 08910 /* Initialise enc and dec contexts */ 08911 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08912 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08913 08914 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 08915 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 08916 08917 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08918 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08919 08920 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08921 { 08922 enclen = cipher_get_block_size( &ctx_enc ) 08923 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08924 } 08925 else 08926 { 08927 enclen = length; 08928 } 08929 08930 /* encode length number of bytes from inbuf */ 08931 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 08932 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08933 { 08934 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08935 } 08936 else 08937 { 08938 fct_chk( outlen == enclen ); 08939 } 08940 08941 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 08942 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08943 { 08944 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08945 } 08946 else 08947 { 08948 fct_chk( outlen == 0 ); 08949 } 08950 08951 08952 /* decode the previously encoded string */ 08953 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08954 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08955 { 08956 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08957 } 08958 else 08959 { 08960 fct_chk( enclen == outlen ); 08961 } 08962 08963 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08964 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08965 { 08966 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08967 } 08968 else 08969 { 08970 fct_chk( outlen == 0 ); 08971 } 08972 08973 08974 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08975 08976 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08977 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08978 FCT_TEST_END(); 08979 #endif /* POLARSSL_AES_C */ 08980 08981 #ifdef POLARSSL_AES_C 08982 08983 FCT_TEST_BGN(aes_encrypt_and_decrypt_8_bytes) 08984 size_t length = 8; 08985 unsigned char key[32]; 08986 unsigned char iv[16]; 08987 08988 const cipher_info_t *cipher_info; 08989 cipher_context_t ctx_dec; 08990 cipher_context_t ctx_enc; 08991 08992 unsigned char inbuf[64]; 08993 unsigned char encbuf[64]; 08994 unsigned char decbuf[64]; 08995 08996 size_t outlen = 0; 08997 size_t enclen = 0; 08998 08999 memset( key, 0, 32 ); 09000 memset( iv , 0, 16 ); 09001 09002 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 09003 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 09004 09005 memset( inbuf, 5, 64 ); 09006 memset( encbuf, 0, 64 ); 09007 memset( decbuf, 0, 64 ); 09008 09009 /* Check and get info structures */ 09010 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 09011 fct_chk( NULL != cipher_info ); 09012 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 09013 09014 /* Initialise enc and dec contexts */ 09015 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 09016 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 09017 09018 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 09019 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 09020 09021 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 09022 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 09023 09024 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09025 { 09026 enclen = cipher_get_block_size( &ctx_enc ) 09027 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 09028 } 09029 else 09030 { 09031 enclen = length; 09032 } 09033 09034 /* encode length number of bytes from inbuf */ 09035 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 09036 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09037 { 09038 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 09039 } 09040 else 09041 { 09042 fct_chk( outlen == enclen ); 09043 } 09044 09045 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 09046 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09047 { 09048 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 09049 } 09050 else 09051 { 09052 fct_chk( outlen == 0 ); 09053 } 09054 09055 09056 /* decode the previously encoded string */ 09057 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 09058 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09059 { 09060 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 09061 } 09062 else 09063 { 09064 fct_chk( enclen == outlen ); 09065 } 09066 09067 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 09068 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09069 { 09070 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 09071 } 09072 else 09073 { 09074 fct_chk( outlen == 0 ); 09075 } 09076 09077 09078 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 09079 09080 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 09081 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 09082 FCT_TEST_END(); 09083 #endif /* POLARSSL_AES_C */ 09084 09085 #ifdef POLARSSL_AES_C 09086 09087 FCT_TEST_BGN(aes_encrypt_and_decrypt_9_bytes) 09088 size_t length = 9; 09089 unsigned char key[32]; 09090 unsigned char iv[16]; 09091 09092 const cipher_info_t *cipher_info; 09093 cipher_context_t ctx_dec; 09094 cipher_context_t ctx_enc; 09095 09096 unsigned char inbuf[64]; 09097 unsigned char encbuf[64]; 09098 unsigned char decbuf[64]; 09099 09100 size_t outlen = 0; 09101 size_t enclen = 0; 09102 09103 memset( key, 0, 32 ); 09104 memset( iv , 0, 16 ); 09105 09106 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 09107 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 09108 09109 memset( inbuf, 5, 64 ); 09110 memset( encbuf, 0, 64 ); 09111 memset( decbuf, 0, 64 ); 09112 09113 /* Check and get info structures */ 09114 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 09115 fct_chk( NULL != cipher_info ); 09116 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 09117 09118 /* Initialise enc and dec contexts */ 09119 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 09120 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 09121 09122 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 09123 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 09124 09125 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 09126 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 09127 09128 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09129 { 09130 enclen = cipher_get_block_size( &ctx_enc ) 09131 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 09132 } 09133 else 09134 { 09135 enclen = length; 09136 } 09137 09138 /* encode length number of bytes from inbuf */ 09139 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 09140 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09141 { 09142 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 09143 } 09144 else 09145 { 09146 fct_chk( outlen == enclen ); 09147 } 09148 09149 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 09150 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09151 { 09152 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 09153 } 09154 else 09155 { 09156 fct_chk( outlen == 0 ); 09157 } 09158 09159 09160 /* decode the previously encoded string */ 09161 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 09162 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09163 { 09164 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 09165 } 09166 else 09167 { 09168 fct_chk( enclen == outlen ); 09169 } 09170 09171 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 09172 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09173 { 09174 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 09175 } 09176 else 09177 { 09178 fct_chk( outlen == 0 ); 09179 } 09180 09181 09182 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 09183 09184 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 09185 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 09186 FCT_TEST_END(); 09187 #endif /* POLARSSL_AES_C */ 09188 09189 #ifdef POLARSSL_AES_C 09190 09191 FCT_TEST_BGN(aes_encrypt_and_decrypt_15_bytes) 09192 size_t length = 15; 09193 unsigned char key[32]; 09194 unsigned char iv[16]; 09195 09196 const cipher_info_t *cipher_info; 09197 cipher_context_t ctx_dec; 09198 cipher_context_t ctx_enc; 09199 09200 unsigned char inbuf[64]; 09201 unsigned char encbuf[64]; 09202 unsigned char decbuf[64]; 09203 09204 size_t outlen = 0; 09205 size_t enclen = 0; 09206 09207 memset( key, 0, 32 ); 09208 memset( iv , 0, 16 ); 09209 09210 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 09211 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 09212 09213 memset( inbuf, 5, 64 ); 09214 memset( encbuf, 0, 64 ); 09215 memset( decbuf, 0, 64 ); 09216 09217 /* Check and get info structures */ 09218 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 09219 fct_chk( NULL != cipher_info ); 09220 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 09221 09222 /* Initialise enc and dec contexts */ 09223 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 09224 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 09225 09226 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 09227 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 09228 09229 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 09230 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 09231 09232 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09233 { 09234 enclen = cipher_get_block_size( &ctx_enc ) 09235 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 09236 } 09237 else 09238 { 09239 enclen = length; 09240 } 09241 09242 /* encode length number of bytes from inbuf */ 09243 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 09244 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09245 { 09246 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 09247 } 09248 else 09249 { 09250 fct_chk( outlen == enclen ); 09251 } 09252 09253 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 09254 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09255 { 09256 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 09257 } 09258 else 09259 { 09260 fct_chk( outlen == 0 ); 09261 } 09262 09263 09264 /* decode the previously encoded string */ 09265 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 09266 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09267 { 09268 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 09269 } 09270 else 09271 { 09272 fct_chk( enclen == outlen ); 09273 } 09274 09275 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 09276 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09277 { 09278 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 09279 } 09280 else 09281 { 09282 fct_chk( outlen == 0 ); 09283 } 09284 09285 09286 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 09287 09288 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 09289 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 09290 FCT_TEST_END(); 09291 #endif /* POLARSSL_AES_C */ 09292 09293 #ifdef POLARSSL_AES_C 09294 09295 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes) 09296 size_t length = 16; 09297 unsigned char key[32]; 09298 unsigned char iv[16]; 09299 09300 const cipher_info_t *cipher_info; 09301 cipher_context_t ctx_dec; 09302 cipher_context_t ctx_enc; 09303 09304 unsigned char inbuf[64]; 09305 unsigned char encbuf[64]; 09306 unsigned char decbuf[64]; 09307 09308 size_t outlen = 0; 09309 size_t enclen = 0; 09310 09311 memset( key, 0, 32 ); 09312 memset( iv , 0, 16 ); 09313 09314 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 09315 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 09316 09317 memset( inbuf, 5, 64 ); 09318 memset( encbuf, 0, 64 ); 09319 memset( decbuf, 0, 64 ); 09320 09321 /* Check and get info structures */ 09322 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 09323 fct_chk( NULL != cipher_info ); 09324 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 09325 09326 /* Initialise enc and dec contexts */ 09327 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 09328 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 09329 09330 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 09331 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 09332 09333 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 09334 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 09335 09336 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09337 { 09338 enclen = cipher_get_block_size( &ctx_enc ) 09339 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 09340 } 09341 else 09342 { 09343 enclen = length; 09344 } 09345 09346 /* encode length number of bytes from inbuf */ 09347 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 09348 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09349 { 09350 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 09351 } 09352 else 09353 { 09354 fct_chk( outlen == enclen ); 09355 } 09356 09357 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 09358 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09359 { 09360 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 09361 } 09362 else 09363 { 09364 fct_chk( outlen == 0 ); 09365 } 09366 09367 09368 /* decode the previously encoded string */ 09369 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 09370 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09371 { 09372 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 09373 } 09374 else 09375 { 09376 fct_chk( enclen == outlen ); 09377 } 09378 09379 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 09380 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09381 { 09382 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 09383 } 09384 else 09385 { 09386 fct_chk( outlen == 0 ); 09387 } 09388 09389 09390 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 09391 09392 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 09393 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 09394 FCT_TEST_END(); 09395 #endif /* POLARSSL_AES_C */ 09396 09397 #ifdef POLARSSL_AES_C 09398 09399 FCT_TEST_BGN(aes_encrypt_and_decrypt_17_bytes) 09400 size_t length = 17; 09401 unsigned char key[32]; 09402 unsigned char iv[16]; 09403 09404 const cipher_info_t *cipher_info; 09405 cipher_context_t ctx_dec; 09406 cipher_context_t ctx_enc; 09407 09408 unsigned char inbuf[64]; 09409 unsigned char encbuf[64]; 09410 unsigned char decbuf[64]; 09411 09412 size_t outlen = 0; 09413 size_t enclen = 0; 09414 09415 memset( key, 0, 32 ); 09416 memset( iv , 0, 16 ); 09417 09418 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 09419 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 09420 09421 memset( inbuf, 5, 64 ); 09422 memset( encbuf, 0, 64 ); 09423 memset( decbuf, 0, 64 ); 09424 09425 /* Check and get info structures */ 09426 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 09427 fct_chk( NULL != cipher_info ); 09428 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 09429 09430 /* Initialise enc and dec contexts */ 09431 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 09432 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 09433 09434 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 09435 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 09436 09437 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 09438 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 09439 09440 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09441 { 09442 enclen = cipher_get_block_size( &ctx_enc ) 09443 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 09444 } 09445 else 09446 { 09447 enclen = length; 09448 } 09449 09450 /* encode length number of bytes from inbuf */ 09451 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 09452 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09453 { 09454 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 09455 } 09456 else 09457 { 09458 fct_chk( outlen == enclen ); 09459 } 09460 09461 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 09462 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09463 { 09464 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 09465 } 09466 else 09467 { 09468 fct_chk( outlen == 0 ); 09469 } 09470 09471 09472 /* decode the previously encoded string */ 09473 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 09474 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09475 { 09476 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 09477 } 09478 else 09479 { 09480 fct_chk( enclen == outlen ); 09481 } 09482 09483 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 09484 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09485 { 09486 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 09487 } 09488 else 09489 { 09490 fct_chk( outlen == 0 ); 09491 } 09492 09493 09494 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 09495 09496 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 09497 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 09498 FCT_TEST_END(); 09499 #endif /* POLARSSL_AES_C */ 09500 09501 #ifdef POLARSSL_AES_C 09502 09503 FCT_TEST_BGN(aes_encrypt_and_decrypt_31_bytes) 09504 size_t length = 31; 09505 unsigned char key[32]; 09506 unsigned char iv[16]; 09507 09508 const cipher_info_t *cipher_info; 09509 cipher_context_t ctx_dec; 09510 cipher_context_t ctx_enc; 09511 09512 unsigned char inbuf[64]; 09513 unsigned char encbuf[64]; 09514 unsigned char decbuf[64]; 09515 09516 size_t outlen = 0; 09517 size_t enclen = 0; 09518 09519 memset( key, 0, 32 ); 09520 memset( iv , 0, 16 ); 09521 09522 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 09523 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 09524 09525 memset( inbuf, 5, 64 ); 09526 memset( encbuf, 0, 64 ); 09527 memset( decbuf, 0, 64 ); 09528 09529 /* Check and get info structures */ 09530 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 09531 fct_chk( NULL != cipher_info ); 09532 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 09533 09534 /* Initialise enc and dec contexts */ 09535 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 09536 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 09537 09538 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 09539 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 09540 09541 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 09542 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 09543 09544 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09545 { 09546 enclen = cipher_get_block_size( &ctx_enc ) 09547 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 09548 } 09549 else 09550 { 09551 enclen = length; 09552 } 09553 09554 /* encode length number of bytes from inbuf */ 09555 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 09556 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09557 { 09558 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 09559 } 09560 else 09561 { 09562 fct_chk( outlen == enclen ); 09563 } 09564 09565 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 09566 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09567 { 09568 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 09569 } 09570 else 09571 { 09572 fct_chk( outlen == 0 ); 09573 } 09574 09575 09576 /* decode the previously encoded string */ 09577 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 09578 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09579 { 09580 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 09581 } 09582 else 09583 { 09584 fct_chk( enclen == outlen ); 09585 } 09586 09587 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 09588 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09589 { 09590 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 09591 } 09592 else 09593 { 09594 fct_chk( outlen == 0 ); 09595 } 09596 09597 09598 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 09599 09600 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 09601 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 09602 FCT_TEST_END(); 09603 #endif /* POLARSSL_AES_C */ 09604 09605 #ifdef POLARSSL_AES_C 09606 09607 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes) 09608 size_t length = 32; 09609 unsigned char key[32]; 09610 unsigned char iv[16]; 09611 09612 const cipher_info_t *cipher_info; 09613 cipher_context_t ctx_dec; 09614 cipher_context_t ctx_enc; 09615 09616 unsigned char inbuf[64]; 09617 unsigned char encbuf[64]; 09618 unsigned char decbuf[64]; 09619 09620 size_t outlen = 0; 09621 size_t enclen = 0; 09622 09623 memset( key, 0, 32 ); 09624 memset( iv , 0, 16 ); 09625 09626 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 09627 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 09628 09629 memset( inbuf, 5, 64 ); 09630 memset( encbuf, 0, 64 ); 09631 memset( decbuf, 0, 64 ); 09632 09633 /* Check and get info structures */ 09634 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 09635 fct_chk( NULL != cipher_info ); 09636 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 09637 09638 /* Initialise enc and dec contexts */ 09639 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 09640 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 09641 09642 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 09643 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 09644 09645 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 09646 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 09647 09648 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09649 { 09650 enclen = cipher_get_block_size( &ctx_enc ) 09651 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 09652 } 09653 else 09654 { 09655 enclen = length; 09656 } 09657 09658 /* encode length number of bytes from inbuf */ 09659 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 09660 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09661 { 09662 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 09663 } 09664 else 09665 { 09666 fct_chk( outlen == enclen ); 09667 } 09668 09669 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 09670 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09671 { 09672 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 09673 } 09674 else 09675 { 09676 fct_chk( outlen == 0 ); 09677 } 09678 09679 09680 /* decode the previously encoded string */ 09681 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 09682 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09683 { 09684 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 09685 } 09686 else 09687 { 09688 fct_chk( enclen == outlen ); 09689 } 09690 09691 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 09692 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09693 { 09694 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 09695 } 09696 else 09697 { 09698 fct_chk( outlen == 0 ); 09699 } 09700 09701 09702 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 09703 09704 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 09705 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 09706 FCT_TEST_END(); 09707 #endif /* POLARSSL_AES_C */ 09708 09709 #ifdef POLARSSL_AES_C 09710 09711 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes) 09712 size_t length = 33; 09713 unsigned char key[32]; 09714 unsigned char iv[16]; 09715 09716 const cipher_info_t *cipher_info; 09717 cipher_context_t ctx_dec; 09718 cipher_context_t ctx_enc; 09719 09720 unsigned char inbuf[64]; 09721 unsigned char encbuf[64]; 09722 unsigned char decbuf[64]; 09723 09724 size_t outlen = 0; 09725 size_t enclen = 0; 09726 09727 memset( key, 0, 32 ); 09728 memset( iv , 0, 16 ); 09729 09730 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 09731 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 09732 09733 memset( inbuf, 5, 64 ); 09734 memset( encbuf, 0, 64 ); 09735 memset( decbuf, 0, 64 ); 09736 09737 /* Check and get info structures */ 09738 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 09739 fct_chk( NULL != cipher_info ); 09740 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 09741 09742 /* Initialise enc and dec contexts */ 09743 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 09744 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 09745 09746 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 09747 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 09748 09749 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 09750 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 09751 09752 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09753 { 09754 enclen = cipher_get_block_size( &ctx_enc ) 09755 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 09756 } 09757 else 09758 { 09759 enclen = length; 09760 } 09761 09762 /* encode length number of bytes from inbuf */ 09763 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 09764 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09765 { 09766 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 09767 } 09768 else 09769 { 09770 fct_chk( outlen == enclen ); 09771 } 09772 09773 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 09774 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09775 { 09776 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 09777 } 09778 else 09779 { 09780 fct_chk( outlen == 0 ); 09781 } 09782 09783 09784 /* decode the previously encoded string */ 09785 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 09786 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09787 { 09788 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 09789 } 09790 else 09791 { 09792 fct_chk( enclen == outlen ); 09793 } 09794 09795 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 09796 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09797 { 09798 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 09799 } 09800 else 09801 { 09802 fct_chk( outlen == 0 ); 09803 } 09804 09805 09806 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 09807 09808 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 09809 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 09810 FCT_TEST_END(); 09811 #endif /* POLARSSL_AES_C */ 09812 09813 #ifdef POLARSSL_AES_C 09814 09815 FCT_TEST_BGN(aes_encrypt_and_decrypt_47_bytes) 09816 size_t length = 47; 09817 unsigned char key[32]; 09818 unsigned char iv[16]; 09819 09820 const cipher_info_t *cipher_info; 09821 cipher_context_t ctx_dec; 09822 cipher_context_t ctx_enc; 09823 09824 unsigned char inbuf[64]; 09825 unsigned char encbuf[64]; 09826 unsigned char decbuf[64]; 09827 09828 size_t outlen = 0; 09829 size_t enclen = 0; 09830 09831 memset( key, 0, 32 ); 09832 memset( iv , 0, 16 ); 09833 09834 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 09835 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 09836 09837 memset( inbuf, 5, 64 ); 09838 memset( encbuf, 0, 64 ); 09839 memset( decbuf, 0, 64 ); 09840 09841 /* Check and get info structures */ 09842 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 09843 fct_chk( NULL != cipher_info ); 09844 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 09845 09846 /* Initialise enc and dec contexts */ 09847 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 09848 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 09849 09850 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 09851 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 09852 09853 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 09854 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 09855 09856 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09857 { 09858 enclen = cipher_get_block_size( &ctx_enc ) 09859 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 09860 } 09861 else 09862 { 09863 enclen = length; 09864 } 09865 09866 /* encode length number of bytes from inbuf */ 09867 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 09868 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09869 { 09870 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 09871 } 09872 else 09873 { 09874 fct_chk( outlen == enclen ); 09875 } 09876 09877 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 09878 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09879 { 09880 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 09881 } 09882 else 09883 { 09884 fct_chk( outlen == 0 ); 09885 } 09886 09887 09888 /* decode the previously encoded string */ 09889 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 09890 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09891 { 09892 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 09893 } 09894 else 09895 { 09896 fct_chk( enclen == outlen ); 09897 } 09898 09899 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 09900 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09901 { 09902 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 09903 } 09904 else 09905 { 09906 fct_chk( outlen == 0 ); 09907 } 09908 09909 09910 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 09911 09912 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 09913 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 09914 FCT_TEST_END(); 09915 #endif /* POLARSSL_AES_C */ 09916 09917 #ifdef POLARSSL_AES_C 09918 09919 FCT_TEST_BGN(aes_encrypt_and_decrypt_48_bytes) 09920 size_t length = 48; 09921 unsigned char key[32]; 09922 unsigned char iv[16]; 09923 09924 const cipher_info_t *cipher_info; 09925 cipher_context_t ctx_dec; 09926 cipher_context_t ctx_enc; 09927 09928 unsigned char inbuf[64]; 09929 unsigned char encbuf[64]; 09930 unsigned char decbuf[64]; 09931 09932 size_t outlen = 0; 09933 size_t enclen = 0; 09934 09935 memset( key, 0, 32 ); 09936 memset( iv , 0, 16 ); 09937 09938 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 09939 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 09940 09941 memset( inbuf, 5, 64 ); 09942 memset( encbuf, 0, 64 ); 09943 memset( decbuf, 0, 64 ); 09944 09945 /* Check and get info structures */ 09946 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 09947 fct_chk( NULL != cipher_info ); 09948 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 09949 09950 /* Initialise enc and dec contexts */ 09951 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 09952 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 09953 09954 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 09955 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 09956 09957 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 09958 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 09959 09960 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09961 { 09962 enclen = cipher_get_block_size( &ctx_enc ) 09963 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 09964 } 09965 else 09966 { 09967 enclen = length; 09968 } 09969 09970 /* encode length number of bytes from inbuf */ 09971 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 09972 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09973 { 09974 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 09975 } 09976 else 09977 { 09978 fct_chk( outlen == enclen ); 09979 } 09980 09981 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 09982 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09983 { 09984 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 09985 } 09986 else 09987 { 09988 fct_chk( outlen == 0 ); 09989 } 09990 09991 09992 /* decode the previously encoded string */ 09993 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 09994 if( POLARSSL_MODE_CBC == cipher_info->mode ) 09995 { 09996 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 09997 } 09998 else 09999 { 10000 fct_chk( enclen == outlen ); 10001 } 10002 10003 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 10004 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10005 { 10006 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 10007 } 10008 else 10009 { 10010 fct_chk( outlen == 0 ); 10011 } 10012 10013 10014 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 10015 10016 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 10017 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 10018 FCT_TEST_END(); 10019 #endif /* POLARSSL_AES_C */ 10020 10021 #ifdef POLARSSL_AES_C 10022 10023 FCT_TEST_BGN(aes_encrypt_and_decrypt_49_bytes) 10024 size_t length = 49; 10025 unsigned char key[32]; 10026 unsigned char iv[16]; 10027 10028 const cipher_info_t *cipher_info; 10029 cipher_context_t ctx_dec; 10030 cipher_context_t ctx_enc; 10031 10032 unsigned char inbuf[64]; 10033 unsigned char encbuf[64]; 10034 unsigned char decbuf[64]; 10035 10036 size_t outlen = 0; 10037 size_t enclen = 0; 10038 10039 memset( key, 0, 32 ); 10040 memset( iv , 0, 16 ); 10041 10042 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 10043 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 10044 10045 memset( inbuf, 5, 64 ); 10046 memset( encbuf, 0, 64 ); 10047 memset( decbuf, 0, 64 ); 10048 10049 /* Check and get info structures */ 10050 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 10051 fct_chk( NULL != cipher_info ); 10052 fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info ); 10053 10054 /* Initialise enc and dec contexts */ 10055 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 10056 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 10057 10058 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 10059 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 10060 10061 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 10062 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 10063 10064 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10065 { 10066 enclen = cipher_get_block_size( &ctx_enc ) 10067 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 10068 } 10069 else 10070 { 10071 enclen = length; 10072 } 10073 10074 /* encode length number of bytes from inbuf */ 10075 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 10076 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10077 { 10078 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 10079 } 10080 else 10081 { 10082 fct_chk( outlen == enclen ); 10083 } 10084 10085 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 10086 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10087 { 10088 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 10089 } 10090 else 10091 { 10092 fct_chk( outlen == 0 ); 10093 } 10094 10095 10096 /* decode the previously encoded string */ 10097 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 10098 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10099 { 10100 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 10101 } 10102 else 10103 { 10104 fct_chk( enclen == outlen ); 10105 } 10106 10107 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 10108 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10109 { 10110 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 10111 } 10112 else 10113 { 10114 fct_chk( outlen == 0 ); 10115 } 10116 10117 10118 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 10119 10120 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 10121 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 10122 FCT_TEST_END(); 10123 #endif /* POLARSSL_AES_C */ 10124 10125 #ifdef POLARSSL_AES_C 10126 10127 FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes_in_multiple_parts) 10128 size_t first_length = 0; 10129 size_t second_length = 0; 10130 size_t length = first_length + second_length; 10131 unsigned char key[32]; 10132 unsigned char iv[16]; 10133 10134 cipher_context_t ctx_dec; 10135 cipher_context_t ctx_enc; 10136 const cipher_info_t *cipher_info; 10137 10138 unsigned char inbuf[64]; 10139 unsigned char encbuf[64]; 10140 unsigned char decbuf[64]; 10141 10142 size_t outlen = 0; 10143 size_t totaloutlen = 0; 10144 size_t enclen = 0; 10145 10146 memset( key, 0, 32 ); 10147 memset( iv , 0, 16 ); 10148 10149 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 10150 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 10151 10152 memset( inbuf, 5, 64 ); 10153 memset( encbuf, 0, 64 ); 10154 memset( decbuf, 0, 64 ); 10155 10156 /* Initialise enc and dec contexts */ 10157 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 10158 fct_chk( NULL != cipher_info); 10159 10160 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 10161 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 10162 10163 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 10164 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 10165 10166 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 10167 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 10168 10169 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10170 { 10171 enclen = cipher_get_block_size(&ctx_enc ) 10172 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 10173 } 10174 else 10175 { 10176 enclen = length; 10177 } 10178 10179 /* encode length number of bytes from inbuf */ 10180 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 10181 totaloutlen = outlen; 10182 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 10183 totaloutlen += outlen; 10184 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10185 { 10186 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 10187 } 10188 else 10189 { 10190 fct_chk( totaloutlen == enclen ); 10191 } 10192 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 10193 totaloutlen += outlen; 10194 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10195 { 10196 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 10197 } 10198 else 10199 { 10200 fct_chk( outlen == 0 ); 10201 } 10202 10203 /* decode the previously encoded string */ 10204 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 10205 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10206 { 10207 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 10208 } 10209 else 10210 { 10211 fct_chk( enclen == outlen ); 10212 } 10213 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 10214 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10215 { 10216 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 10217 } 10218 else 10219 { 10220 fct_chk( outlen == 0 ); 10221 } 10222 10223 10224 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 10225 10226 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 10227 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 10228 FCT_TEST_END(); 10229 #endif /* POLARSSL_AES_C */ 10230 10231 #ifdef POLARSSL_AES_C 10232 10233 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_1) 10234 size_t first_length = 1; 10235 size_t second_length = 0; 10236 size_t length = first_length + second_length; 10237 unsigned char key[32]; 10238 unsigned char iv[16]; 10239 10240 cipher_context_t ctx_dec; 10241 cipher_context_t ctx_enc; 10242 const cipher_info_t *cipher_info; 10243 10244 unsigned char inbuf[64]; 10245 unsigned char encbuf[64]; 10246 unsigned char decbuf[64]; 10247 10248 size_t outlen = 0; 10249 size_t totaloutlen = 0; 10250 size_t enclen = 0; 10251 10252 memset( key, 0, 32 ); 10253 memset( iv , 0, 16 ); 10254 10255 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 10256 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 10257 10258 memset( inbuf, 5, 64 ); 10259 memset( encbuf, 0, 64 ); 10260 memset( decbuf, 0, 64 ); 10261 10262 /* Initialise enc and dec contexts */ 10263 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 10264 fct_chk( NULL != cipher_info); 10265 10266 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 10267 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 10268 10269 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 10270 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 10271 10272 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 10273 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 10274 10275 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10276 { 10277 enclen = cipher_get_block_size(&ctx_enc ) 10278 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 10279 } 10280 else 10281 { 10282 enclen = length; 10283 } 10284 10285 /* encode length number of bytes from inbuf */ 10286 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 10287 totaloutlen = outlen; 10288 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 10289 totaloutlen += outlen; 10290 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10291 { 10292 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 10293 } 10294 else 10295 { 10296 fct_chk( totaloutlen == enclen ); 10297 } 10298 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 10299 totaloutlen += outlen; 10300 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10301 { 10302 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 10303 } 10304 else 10305 { 10306 fct_chk( outlen == 0 ); 10307 } 10308 10309 /* decode the previously encoded string */ 10310 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 10311 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10312 { 10313 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 10314 } 10315 else 10316 { 10317 fct_chk( enclen == outlen ); 10318 } 10319 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 10320 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10321 { 10322 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 10323 } 10324 else 10325 { 10326 fct_chk( outlen == 0 ); 10327 } 10328 10329 10330 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 10331 10332 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 10333 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 10334 FCT_TEST_END(); 10335 #endif /* POLARSSL_AES_C */ 10336 10337 #ifdef POLARSSL_AES_C 10338 10339 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_2) 10340 size_t first_length = 0; 10341 size_t second_length = 1; 10342 size_t length = first_length + second_length; 10343 unsigned char key[32]; 10344 unsigned char iv[16]; 10345 10346 cipher_context_t ctx_dec; 10347 cipher_context_t ctx_enc; 10348 const cipher_info_t *cipher_info; 10349 10350 unsigned char inbuf[64]; 10351 unsigned char encbuf[64]; 10352 unsigned char decbuf[64]; 10353 10354 size_t outlen = 0; 10355 size_t totaloutlen = 0; 10356 size_t enclen = 0; 10357 10358 memset( key, 0, 32 ); 10359 memset( iv , 0, 16 ); 10360 10361 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 10362 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 10363 10364 memset( inbuf, 5, 64 ); 10365 memset( encbuf, 0, 64 ); 10366 memset( decbuf, 0, 64 ); 10367 10368 /* Initialise enc and dec contexts */ 10369 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 10370 fct_chk( NULL != cipher_info); 10371 10372 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 10373 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 10374 10375 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 10376 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 10377 10378 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 10379 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 10380 10381 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10382 { 10383 enclen = cipher_get_block_size(&ctx_enc ) 10384 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 10385 } 10386 else 10387 { 10388 enclen = length; 10389 } 10390 10391 /* encode length number of bytes from inbuf */ 10392 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 10393 totaloutlen = outlen; 10394 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 10395 totaloutlen += outlen; 10396 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10397 { 10398 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 10399 } 10400 else 10401 { 10402 fct_chk( totaloutlen == enclen ); 10403 } 10404 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 10405 totaloutlen += outlen; 10406 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10407 { 10408 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 10409 } 10410 else 10411 { 10412 fct_chk( outlen == 0 ); 10413 } 10414 10415 /* decode the previously encoded string */ 10416 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 10417 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10418 { 10419 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 10420 } 10421 else 10422 { 10423 fct_chk( enclen == outlen ); 10424 } 10425 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 10426 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10427 { 10428 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 10429 } 10430 else 10431 { 10432 fct_chk( outlen == 0 ); 10433 } 10434 10435 10436 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 10437 10438 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 10439 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 10440 FCT_TEST_END(); 10441 #endif /* POLARSSL_AES_C */ 10442 10443 #ifdef POLARSSL_AES_C 10444 10445 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_1) 10446 size_t first_length = 16; 10447 size_t second_length = 0; 10448 size_t length = first_length + second_length; 10449 unsigned char key[32]; 10450 unsigned char iv[16]; 10451 10452 cipher_context_t ctx_dec; 10453 cipher_context_t ctx_enc; 10454 const cipher_info_t *cipher_info; 10455 10456 unsigned char inbuf[64]; 10457 unsigned char encbuf[64]; 10458 unsigned char decbuf[64]; 10459 10460 size_t outlen = 0; 10461 size_t totaloutlen = 0; 10462 size_t enclen = 0; 10463 10464 memset( key, 0, 32 ); 10465 memset( iv , 0, 16 ); 10466 10467 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 10468 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 10469 10470 memset( inbuf, 5, 64 ); 10471 memset( encbuf, 0, 64 ); 10472 memset( decbuf, 0, 64 ); 10473 10474 /* Initialise enc and dec contexts */ 10475 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 10476 fct_chk( NULL != cipher_info); 10477 10478 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 10479 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 10480 10481 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 10482 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 10483 10484 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 10485 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 10486 10487 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10488 { 10489 enclen = cipher_get_block_size(&ctx_enc ) 10490 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 10491 } 10492 else 10493 { 10494 enclen = length; 10495 } 10496 10497 /* encode length number of bytes from inbuf */ 10498 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 10499 totaloutlen = outlen; 10500 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 10501 totaloutlen += outlen; 10502 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10503 { 10504 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 10505 } 10506 else 10507 { 10508 fct_chk( totaloutlen == enclen ); 10509 } 10510 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 10511 totaloutlen += outlen; 10512 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10513 { 10514 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 10515 } 10516 else 10517 { 10518 fct_chk( outlen == 0 ); 10519 } 10520 10521 /* decode the previously encoded string */ 10522 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 10523 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10524 { 10525 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 10526 } 10527 else 10528 { 10529 fct_chk( enclen == outlen ); 10530 } 10531 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 10532 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10533 { 10534 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 10535 } 10536 else 10537 { 10538 fct_chk( outlen == 0 ); 10539 } 10540 10541 10542 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 10543 10544 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 10545 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 10546 FCT_TEST_END(); 10547 #endif /* POLARSSL_AES_C */ 10548 10549 #ifdef POLARSSL_AES_C 10550 10551 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_2) 10552 size_t first_length = 0; 10553 size_t second_length = 16; 10554 size_t length = first_length + second_length; 10555 unsigned char key[32]; 10556 unsigned char iv[16]; 10557 10558 cipher_context_t ctx_dec; 10559 cipher_context_t ctx_enc; 10560 const cipher_info_t *cipher_info; 10561 10562 unsigned char inbuf[64]; 10563 unsigned char encbuf[64]; 10564 unsigned char decbuf[64]; 10565 10566 size_t outlen = 0; 10567 size_t totaloutlen = 0; 10568 size_t enclen = 0; 10569 10570 memset( key, 0, 32 ); 10571 memset( iv , 0, 16 ); 10572 10573 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 10574 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 10575 10576 memset( inbuf, 5, 64 ); 10577 memset( encbuf, 0, 64 ); 10578 memset( decbuf, 0, 64 ); 10579 10580 /* Initialise enc and dec contexts */ 10581 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 10582 fct_chk( NULL != cipher_info); 10583 10584 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 10585 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 10586 10587 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 10588 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 10589 10590 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 10591 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 10592 10593 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10594 { 10595 enclen = cipher_get_block_size(&ctx_enc ) 10596 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 10597 } 10598 else 10599 { 10600 enclen = length; 10601 } 10602 10603 /* encode length number of bytes from inbuf */ 10604 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 10605 totaloutlen = outlen; 10606 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 10607 totaloutlen += outlen; 10608 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10609 { 10610 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 10611 } 10612 else 10613 { 10614 fct_chk( totaloutlen == enclen ); 10615 } 10616 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 10617 totaloutlen += outlen; 10618 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10619 { 10620 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 10621 } 10622 else 10623 { 10624 fct_chk( outlen == 0 ); 10625 } 10626 10627 /* decode the previously encoded string */ 10628 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 10629 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10630 { 10631 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 10632 } 10633 else 10634 { 10635 fct_chk( enclen == outlen ); 10636 } 10637 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 10638 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10639 { 10640 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 10641 } 10642 else 10643 { 10644 fct_chk( outlen == 0 ); 10645 } 10646 10647 10648 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 10649 10650 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 10651 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 10652 FCT_TEST_END(); 10653 #endif /* POLARSSL_AES_C */ 10654 10655 #ifdef POLARSSL_AES_C 10656 10657 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_3) 10658 size_t first_length = 1; 10659 size_t second_length = 15; 10660 size_t length = first_length + second_length; 10661 unsigned char key[32]; 10662 unsigned char iv[16]; 10663 10664 cipher_context_t ctx_dec; 10665 cipher_context_t ctx_enc; 10666 const cipher_info_t *cipher_info; 10667 10668 unsigned char inbuf[64]; 10669 unsigned char encbuf[64]; 10670 unsigned char decbuf[64]; 10671 10672 size_t outlen = 0; 10673 size_t totaloutlen = 0; 10674 size_t enclen = 0; 10675 10676 memset( key, 0, 32 ); 10677 memset( iv , 0, 16 ); 10678 10679 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 10680 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 10681 10682 memset( inbuf, 5, 64 ); 10683 memset( encbuf, 0, 64 ); 10684 memset( decbuf, 0, 64 ); 10685 10686 /* Initialise enc and dec contexts */ 10687 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 10688 fct_chk( NULL != cipher_info); 10689 10690 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 10691 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 10692 10693 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 10694 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 10695 10696 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 10697 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 10698 10699 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10700 { 10701 enclen = cipher_get_block_size(&ctx_enc ) 10702 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 10703 } 10704 else 10705 { 10706 enclen = length; 10707 } 10708 10709 /* encode length number of bytes from inbuf */ 10710 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 10711 totaloutlen = outlen; 10712 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 10713 totaloutlen += outlen; 10714 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10715 { 10716 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 10717 } 10718 else 10719 { 10720 fct_chk( totaloutlen == enclen ); 10721 } 10722 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 10723 totaloutlen += outlen; 10724 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10725 { 10726 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 10727 } 10728 else 10729 { 10730 fct_chk( outlen == 0 ); 10731 } 10732 10733 /* decode the previously encoded string */ 10734 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 10735 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10736 { 10737 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 10738 } 10739 else 10740 { 10741 fct_chk( enclen == outlen ); 10742 } 10743 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 10744 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10745 { 10746 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 10747 } 10748 else 10749 { 10750 fct_chk( outlen == 0 ); 10751 } 10752 10753 10754 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 10755 10756 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 10757 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 10758 FCT_TEST_END(); 10759 #endif /* POLARSSL_AES_C */ 10760 10761 #ifdef POLARSSL_AES_C 10762 10763 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_4) 10764 size_t first_length = 15; 10765 size_t second_length = 1; 10766 size_t length = first_length + second_length; 10767 unsigned char key[32]; 10768 unsigned char iv[16]; 10769 10770 cipher_context_t ctx_dec; 10771 cipher_context_t ctx_enc; 10772 const cipher_info_t *cipher_info; 10773 10774 unsigned char inbuf[64]; 10775 unsigned char encbuf[64]; 10776 unsigned char decbuf[64]; 10777 10778 size_t outlen = 0; 10779 size_t totaloutlen = 0; 10780 size_t enclen = 0; 10781 10782 memset( key, 0, 32 ); 10783 memset( iv , 0, 16 ); 10784 10785 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 10786 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 10787 10788 memset( inbuf, 5, 64 ); 10789 memset( encbuf, 0, 64 ); 10790 memset( decbuf, 0, 64 ); 10791 10792 /* Initialise enc and dec contexts */ 10793 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 10794 fct_chk( NULL != cipher_info); 10795 10796 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 10797 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 10798 10799 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 10800 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 10801 10802 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 10803 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 10804 10805 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10806 { 10807 enclen = cipher_get_block_size(&ctx_enc ) 10808 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 10809 } 10810 else 10811 { 10812 enclen = length; 10813 } 10814 10815 /* encode length number of bytes from inbuf */ 10816 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 10817 totaloutlen = outlen; 10818 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 10819 totaloutlen += outlen; 10820 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10821 { 10822 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 10823 } 10824 else 10825 { 10826 fct_chk( totaloutlen == enclen ); 10827 } 10828 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 10829 totaloutlen += outlen; 10830 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10831 { 10832 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 10833 } 10834 else 10835 { 10836 fct_chk( outlen == 0 ); 10837 } 10838 10839 /* decode the previously encoded string */ 10840 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 10841 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10842 { 10843 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 10844 } 10845 else 10846 { 10847 fct_chk( enclen == outlen ); 10848 } 10849 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 10850 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10851 { 10852 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 10853 } 10854 else 10855 { 10856 fct_chk( outlen == 0 ); 10857 } 10858 10859 10860 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 10861 10862 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 10863 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 10864 FCT_TEST_END(); 10865 #endif /* POLARSSL_AES_C */ 10866 10867 #ifdef POLARSSL_AES_C 10868 10869 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 10870 size_t first_length = 15; 10871 size_t second_length = 7; 10872 size_t length = first_length + second_length; 10873 unsigned char key[32]; 10874 unsigned char iv[16]; 10875 10876 cipher_context_t ctx_dec; 10877 cipher_context_t ctx_enc; 10878 const cipher_info_t *cipher_info; 10879 10880 unsigned char inbuf[64]; 10881 unsigned char encbuf[64]; 10882 unsigned char decbuf[64]; 10883 10884 size_t outlen = 0; 10885 size_t totaloutlen = 0; 10886 size_t enclen = 0; 10887 10888 memset( key, 0, 32 ); 10889 memset( iv , 0, 16 ); 10890 10891 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 10892 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 10893 10894 memset( inbuf, 5, 64 ); 10895 memset( encbuf, 0, 64 ); 10896 memset( decbuf, 0, 64 ); 10897 10898 /* Initialise enc and dec contexts */ 10899 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 10900 fct_chk( NULL != cipher_info); 10901 10902 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 10903 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 10904 10905 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 10906 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 10907 10908 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 10909 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 10910 10911 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10912 { 10913 enclen = cipher_get_block_size(&ctx_enc ) 10914 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 10915 } 10916 else 10917 { 10918 enclen = length; 10919 } 10920 10921 /* encode length number of bytes from inbuf */ 10922 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 10923 totaloutlen = outlen; 10924 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 10925 totaloutlen += outlen; 10926 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10927 { 10928 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 10929 } 10930 else 10931 { 10932 fct_chk( totaloutlen == enclen ); 10933 } 10934 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 10935 totaloutlen += outlen; 10936 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10937 { 10938 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 10939 } 10940 else 10941 { 10942 fct_chk( outlen == 0 ); 10943 } 10944 10945 /* decode the previously encoded string */ 10946 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 10947 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10948 { 10949 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 10950 } 10951 else 10952 { 10953 fct_chk( enclen == outlen ); 10954 } 10955 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 10956 if( POLARSSL_MODE_CBC == cipher_info->mode ) 10957 { 10958 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 10959 } 10960 else 10961 { 10962 fct_chk( outlen == 0 ); 10963 } 10964 10965 10966 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 10967 10968 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 10969 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 10970 FCT_TEST_END(); 10971 #endif /* POLARSSL_AES_C */ 10972 10973 #ifdef POLARSSL_AES_C 10974 10975 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 10976 size_t first_length = 16; 10977 size_t second_length = 6; 10978 size_t length = first_length + second_length; 10979 unsigned char key[32]; 10980 unsigned char iv[16]; 10981 10982 cipher_context_t ctx_dec; 10983 cipher_context_t ctx_enc; 10984 const cipher_info_t *cipher_info; 10985 10986 unsigned char inbuf[64]; 10987 unsigned char encbuf[64]; 10988 unsigned char decbuf[64]; 10989 10990 size_t outlen = 0; 10991 size_t totaloutlen = 0; 10992 size_t enclen = 0; 10993 10994 memset( key, 0, 32 ); 10995 memset( iv , 0, 16 ); 10996 10997 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 10998 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 10999 11000 memset( inbuf, 5, 64 ); 11001 memset( encbuf, 0, 64 ); 11002 memset( decbuf, 0, 64 ); 11003 11004 /* Initialise enc and dec contexts */ 11005 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 11006 fct_chk( NULL != cipher_info); 11007 11008 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 11009 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 11010 11011 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 11012 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 11013 11014 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 11015 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 11016 11017 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11018 { 11019 enclen = cipher_get_block_size(&ctx_enc ) 11020 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 11021 } 11022 else 11023 { 11024 enclen = length; 11025 } 11026 11027 /* encode length number of bytes from inbuf */ 11028 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 11029 totaloutlen = outlen; 11030 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 11031 totaloutlen += outlen; 11032 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11033 { 11034 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 11035 } 11036 else 11037 { 11038 fct_chk( totaloutlen == enclen ); 11039 } 11040 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 11041 totaloutlen += outlen; 11042 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11043 { 11044 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 11045 } 11046 else 11047 { 11048 fct_chk( outlen == 0 ); 11049 } 11050 11051 /* decode the previously encoded string */ 11052 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 11053 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11054 { 11055 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 11056 } 11057 else 11058 { 11059 fct_chk( enclen == outlen ); 11060 } 11061 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 11062 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11063 { 11064 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 11065 } 11066 else 11067 { 11068 fct_chk( outlen == 0 ); 11069 } 11070 11071 11072 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 11073 11074 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 11075 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 11076 FCT_TEST_END(); 11077 #endif /* POLARSSL_AES_C */ 11078 11079 #ifdef POLARSSL_AES_C 11080 11081 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 11082 size_t first_length = 17; 11083 size_t second_length = 6; 11084 size_t length = first_length + second_length; 11085 unsigned char key[32]; 11086 unsigned char iv[16]; 11087 11088 cipher_context_t ctx_dec; 11089 cipher_context_t ctx_enc; 11090 const cipher_info_t *cipher_info; 11091 11092 unsigned char inbuf[64]; 11093 unsigned char encbuf[64]; 11094 unsigned char decbuf[64]; 11095 11096 size_t outlen = 0; 11097 size_t totaloutlen = 0; 11098 size_t enclen = 0; 11099 11100 memset( key, 0, 32 ); 11101 memset( iv , 0, 16 ); 11102 11103 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 11104 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 11105 11106 memset( inbuf, 5, 64 ); 11107 memset( encbuf, 0, 64 ); 11108 memset( decbuf, 0, 64 ); 11109 11110 /* Initialise enc and dec contexts */ 11111 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 11112 fct_chk( NULL != cipher_info); 11113 11114 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 11115 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 11116 11117 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 11118 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 11119 11120 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 11121 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 11122 11123 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11124 { 11125 enclen = cipher_get_block_size(&ctx_enc ) 11126 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 11127 } 11128 else 11129 { 11130 enclen = length; 11131 } 11132 11133 /* encode length number of bytes from inbuf */ 11134 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 11135 totaloutlen = outlen; 11136 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 11137 totaloutlen += outlen; 11138 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11139 { 11140 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 11141 } 11142 else 11143 { 11144 fct_chk( totaloutlen == enclen ); 11145 } 11146 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 11147 totaloutlen += outlen; 11148 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11149 { 11150 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 11151 } 11152 else 11153 { 11154 fct_chk( outlen == 0 ); 11155 } 11156 11157 /* decode the previously encoded string */ 11158 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 11159 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11160 { 11161 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 11162 } 11163 else 11164 { 11165 fct_chk( enclen == outlen ); 11166 } 11167 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 11168 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11169 { 11170 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 11171 } 11172 else 11173 { 11174 fct_chk( outlen == 0 ); 11175 } 11176 11177 11178 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 11179 11180 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 11181 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 11182 FCT_TEST_END(); 11183 #endif /* POLARSSL_AES_C */ 11184 11185 #ifdef POLARSSL_AES_C 11186 11187 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes_in_multiple_parts_1) 11188 size_t first_length = 16; 11189 size_t second_length = 16; 11190 size_t length = first_length + second_length; 11191 unsigned char key[32]; 11192 unsigned char iv[16]; 11193 11194 cipher_context_t ctx_dec; 11195 cipher_context_t ctx_enc; 11196 const cipher_info_t *cipher_info; 11197 11198 unsigned char inbuf[64]; 11199 unsigned char encbuf[64]; 11200 unsigned char decbuf[64]; 11201 11202 size_t outlen = 0; 11203 size_t totaloutlen = 0; 11204 size_t enclen = 0; 11205 11206 memset( key, 0, 32 ); 11207 memset( iv , 0, 16 ); 11208 11209 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 11210 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 11211 11212 memset( inbuf, 5, 64 ); 11213 memset( encbuf, 0, 64 ); 11214 memset( decbuf, 0, 64 ); 11215 11216 /* Initialise enc and dec contexts */ 11217 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); 11218 fct_chk( NULL != cipher_info); 11219 11220 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 11221 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 11222 11223 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) ); 11224 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) ); 11225 11226 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 11227 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 11228 11229 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11230 { 11231 enclen = cipher_get_block_size(&ctx_enc ) 11232 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 11233 } 11234 else 11235 { 11236 enclen = length; 11237 } 11238 11239 /* encode length number of bytes from inbuf */ 11240 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 11241 totaloutlen = outlen; 11242 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 11243 totaloutlen += outlen; 11244 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11245 { 11246 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 11247 } 11248 else 11249 { 11250 fct_chk( totaloutlen == enclen ); 11251 } 11252 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 11253 totaloutlen += outlen; 11254 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11255 { 11256 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 11257 } 11258 else 11259 { 11260 fct_chk( outlen == 0 ); 11261 } 11262 11263 /* decode the previously encoded string */ 11264 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 11265 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11266 { 11267 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 11268 } 11269 else 11270 { 11271 fct_chk( enclen == outlen ); 11272 } 11273 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 11274 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11275 { 11276 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 11277 } 11278 else 11279 { 11280 fct_chk( outlen == 0 ); 11281 } 11282 11283 11284 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 11285 11286 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 11287 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 11288 FCT_TEST_END(); 11289 #endif /* POLARSSL_AES_C */ 11290 11291 #ifdef POLARSSL_AES_C 11292 11293 FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes) 11294 size_t length = 0; 11295 unsigned char key[32]; 11296 unsigned char iv[16]; 11297 11298 const cipher_info_t *cipher_info; 11299 cipher_context_t ctx_dec; 11300 cipher_context_t ctx_enc; 11301 11302 unsigned char inbuf[64]; 11303 unsigned char encbuf[64]; 11304 unsigned char decbuf[64]; 11305 11306 size_t outlen = 0; 11307 size_t enclen = 0; 11308 11309 memset( key, 0, 32 ); 11310 memset( iv , 0, 16 ); 11311 11312 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 11313 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 11314 11315 memset( inbuf, 5, 64 ); 11316 memset( encbuf, 0, 64 ); 11317 memset( decbuf, 0, 64 ); 11318 11319 /* Check and get info structures */ 11320 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 11321 fct_chk( NULL != cipher_info ); 11322 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 11323 11324 /* Initialise enc and dec contexts */ 11325 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 11326 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 11327 11328 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 11329 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 11330 11331 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 11332 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 11333 11334 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11335 { 11336 enclen = cipher_get_block_size( &ctx_enc ) 11337 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 11338 } 11339 else 11340 { 11341 enclen = length; 11342 } 11343 11344 /* encode length number of bytes from inbuf */ 11345 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 11346 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11347 { 11348 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 11349 } 11350 else 11351 { 11352 fct_chk( outlen == enclen ); 11353 } 11354 11355 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 11356 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11357 { 11358 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 11359 } 11360 else 11361 { 11362 fct_chk( outlen == 0 ); 11363 } 11364 11365 11366 /* decode the previously encoded string */ 11367 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 11368 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11369 { 11370 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 11371 } 11372 else 11373 { 11374 fct_chk( enclen == outlen ); 11375 } 11376 11377 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 11378 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11379 { 11380 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 11381 } 11382 else 11383 { 11384 fct_chk( outlen == 0 ); 11385 } 11386 11387 11388 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 11389 11390 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 11391 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 11392 FCT_TEST_END(); 11393 #endif /* POLARSSL_AES_C */ 11394 11395 #ifdef POLARSSL_AES_C 11396 11397 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_byte) 11398 size_t length = 1; 11399 unsigned char key[32]; 11400 unsigned char iv[16]; 11401 11402 const cipher_info_t *cipher_info; 11403 cipher_context_t ctx_dec; 11404 cipher_context_t ctx_enc; 11405 11406 unsigned char inbuf[64]; 11407 unsigned char encbuf[64]; 11408 unsigned char decbuf[64]; 11409 11410 size_t outlen = 0; 11411 size_t enclen = 0; 11412 11413 memset( key, 0, 32 ); 11414 memset( iv , 0, 16 ); 11415 11416 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 11417 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 11418 11419 memset( inbuf, 5, 64 ); 11420 memset( encbuf, 0, 64 ); 11421 memset( decbuf, 0, 64 ); 11422 11423 /* Check and get info structures */ 11424 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 11425 fct_chk( NULL != cipher_info ); 11426 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 11427 11428 /* Initialise enc and dec contexts */ 11429 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 11430 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 11431 11432 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 11433 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 11434 11435 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 11436 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 11437 11438 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11439 { 11440 enclen = cipher_get_block_size( &ctx_enc ) 11441 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 11442 } 11443 else 11444 { 11445 enclen = length; 11446 } 11447 11448 /* encode length number of bytes from inbuf */ 11449 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 11450 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11451 { 11452 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 11453 } 11454 else 11455 { 11456 fct_chk( outlen == enclen ); 11457 } 11458 11459 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 11460 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11461 { 11462 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 11463 } 11464 else 11465 { 11466 fct_chk( outlen == 0 ); 11467 } 11468 11469 11470 /* decode the previously encoded string */ 11471 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 11472 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11473 { 11474 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 11475 } 11476 else 11477 { 11478 fct_chk( enclen == outlen ); 11479 } 11480 11481 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 11482 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11483 { 11484 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 11485 } 11486 else 11487 { 11488 fct_chk( outlen == 0 ); 11489 } 11490 11491 11492 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 11493 11494 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 11495 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 11496 FCT_TEST_END(); 11497 #endif /* POLARSSL_AES_C */ 11498 11499 #ifdef POLARSSL_AES_C 11500 11501 FCT_TEST_BGN(aes_encrypt_and_decrypt_2_bytes) 11502 size_t length = 2; 11503 unsigned char key[32]; 11504 unsigned char iv[16]; 11505 11506 const cipher_info_t *cipher_info; 11507 cipher_context_t ctx_dec; 11508 cipher_context_t ctx_enc; 11509 11510 unsigned char inbuf[64]; 11511 unsigned char encbuf[64]; 11512 unsigned char decbuf[64]; 11513 11514 size_t outlen = 0; 11515 size_t enclen = 0; 11516 11517 memset( key, 0, 32 ); 11518 memset( iv , 0, 16 ); 11519 11520 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 11521 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 11522 11523 memset( inbuf, 5, 64 ); 11524 memset( encbuf, 0, 64 ); 11525 memset( decbuf, 0, 64 ); 11526 11527 /* Check and get info structures */ 11528 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 11529 fct_chk( NULL != cipher_info ); 11530 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 11531 11532 /* Initialise enc and dec contexts */ 11533 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 11534 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 11535 11536 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 11537 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 11538 11539 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 11540 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 11541 11542 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11543 { 11544 enclen = cipher_get_block_size( &ctx_enc ) 11545 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 11546 } 11547 else 11548 { 11549 enclen = length; 11550 } 11551 11552 /* encode length number of bytes from inbuf */ 11553 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 11554 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11555 { 11556 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 11557 } 11558 else 11559 { 11560 fct_chk( outlen == enclen ); 11561 } 11562 11563 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 11564 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11565 { 11566 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 11567 } 11568 else 11569 { 11570 fct_chk( outlen == 0 ); 11571 } 11572 11573 11574 /* decode the previously encoded string */ 11575 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 11576 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11577 { 11578 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 11579 } 11580 else 11581 { 11582 fct_chk( enclen == outlen ); 11583 } 11584 11585 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 11586 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11587 { 11588 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 11589 } 11590 else 11591 { 11592 fct_chk( outlen == 0 ); 11593 } 11594 11595 11596 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 11597 11598 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 11599 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 11600 FCT_TEST_END(); 11601 #endif /* POLARSSL_AES_C */ 11602 11603 #ifdef POLARSSL_AES_C 11604 11605 FCT_TEST_BGN(aes_encrypt_and_decrypt_7_bytes) 11606 size_t length = 7; 11607 unsigned char key[32]; 11608 unsigned char iv[16]; 11609 11610 const cipher_info_t *cipher_info; 11611 cipher_context_t ctx_dec; 11612 cipher_context_t ctx_enc; 11613 11614 unsigned char inbuf[64]; 11615 unsigned char encbuf[64]; 11616 unsigned char decbuf[64]; 11617 11618 size_t outlen = 0; 11619 size_t enclen = 0; 11620 11621 memset( key, 0, 32 ); 11622 memset( iv , 0, 16 ); 11623 11624 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 11625 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 11626 11627 memset( inbuf, 5, 64 ); 11628 memset( encbuf, 0, 64 ); 11629 memset( decbuf, 0, 64 ); 11630 11631 /* Check and get info structures */ 11632 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 11633 fct_chk( NULL != cipher_info ); 11634 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 11635 11636 /* Initialise enc and dec contexts */ 11637 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 11638 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 11639 11640 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 11641 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 11642 11643 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 11644 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 11645 11646 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11647 { 11648 enclen = cipher_get_block_size( &ctx_enc ) 11649 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 11650 } 11651 else 11652 { 11653 enclen = length; 11654 } 11655 11656 /* encode length number of bytes from inbuf */ 11657 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 11658 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11659 { 11660 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 11661 } 11662 else 11663 { 11664 fct_chk( outlen == enclen ); 11665 } 11666 11667 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 11668 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11669 { 11670 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 11671 } 11672 else 11673 { 11674 fct_chk( outlen == 0 ); 11675 } 11676 11677 11678 /* decode the previously encoded string */ 11679 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 11680 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11681 { 11682 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 11683 } 11684 else 11685 { 11686 fct_chk( enclen == outlen ); 11687 } 11688 11689 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 11690 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11691 { 11692 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 11693 } 11694 else 11695 { 11696 fct_chk( outlen == 0 ); 11697 } 11698 11699 11700 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 11701 11702 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 11703 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 11704 FCT_TEST_END(); 11705 #endif /* POLARSSL_AES_C */ 11706 11707 #ifdef POLARSSL_AES_C 11708 11709 FCT_TEST_BGN(aes_encrypt_and_decrypt_8_bytes) 11710 size_t length = 8; 11711 unsigned char key[32]; 11712 unsigned char iv[16]; 11713 11714 const cipher_info_t *cipher_info; 11715 cipher_context_t ctx_dec; 11716 cipher_context_t ctx_enc; 11717 11718 unsigned char inbuf[64]; 11719 unsigned char encbuf[64]; 11720 unsigned char decbuf[64]; 11721 11722 size_t outlen = 0; 11723 size_t enclen = 0; 11724 11725 memset( key, 0, 32 ); 11726 memset( iv , 0, 16 ); 11727 11728 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 11729 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 11730 11731 memset( inbuf, 5, 64 ); 11732 memset( encbuf, 0, 64 ); 11733 memset( decbuf, 0, 64 ); 11734 11735 /* Check and get info structures */ 11736 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 11737 fct_chk( NULL != cipher_info ); 11738 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 11739 11740 /* Initialise enc and dec contexts */ 11741 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 11742 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 11743 11744 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 11745 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 11746 11747 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 11748 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 11749 11750 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11751 { 11752 enclen = cipher_get_block_size( &ctx_enc ) 11753 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 11754 } 11755 else 11756 { 11757 enclen = length; 11758 } 11759 11760 /* encode length number of bytes from inbuf */ 11761 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 11762 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11763 { 11764 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 11765 } 11766 else 11767 { 11768 fct_chk( outlen == enclen ); 11769 } 11770 11771 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 11772 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11773 { 11774 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 11775 } 11776 else 11777 { 11778 fct_chk( outlen == 0 ); 11779 } 11780 11781 11782 /* decode the previously encoded string */ 11783 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 11784 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11785 { 11786 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 11787 } 11788 else 11789 { 11790 fct_chk( enclen == outlen ); 11791 } 11792 11793 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 11794 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11795 { 11796 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 11797 } 11798 else 11799 { 11800 fct_chk( outlen == 0 ); 11801 } 11802 11803 11804 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 11805 11806 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 11807 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 11808 FCT_TEST_END(); 11809 #endif /* POLARSSL_AES_C */ 11810 11811 #ifdef POLARSSL_AES_C 11812 11813 FCT_TEST_BGN(aes_encrypt_and_decrypt_9_bytes) 11814 size_t length = 9; 11815 unsigned char key[32]; 11816 unsigned char iv[16]; 11817 11818 const cipher_info_t *cipher_info; 11819 cipher_context_t ctx_dec; 11820 cipher_context_t ctx_enc; 11821 11822 unsigned char inbuf[64]; 11823 unsigned char encbuf[64]; 11824 unsigned char decbuf[64]; 11825 11826 size_t outlen = 0; 11827 size_t enclen = 0; 11828 11829 memset( key, 0, 32 ); 11830 memset( iv , 0, 16 ); 11831 11832 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 11833 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 11834 11835 memset( inbuf, 5, 64 ); 11836 memset( encbuf, 0, 64 ); 11837 memset( decbuf, 0, 64 ); 11838 11839 /* Check and get info structures */ 11840 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 11841 fct_chk( NULL != cipher_info ); 11842 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 11843 11844 /* Initialise enc and dec contexts */ 11845 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 11846 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 11847 11848 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 11849 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 11850 11851 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 11852 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 11853 11854 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11855 { 11856 enclen = cipher_get_block_size( &ctx_enc ) 11857 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 11858 } 11859 else 11860 { 11861 enclen = length; 11862 } 11863 11864 /* encode length number of bytes from inbuf */ 11865 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 11866 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11867 { 11868 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 11869 } 11870 else 11871 { 11872 fct_chk( outlen == enclen ); 11873 } 11874 11875 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 11876 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11877 { 11878 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 11879 } 11880 else 11881 { 11882 fct_chk( outlen == 0 ); 11883 } 11884 11885 11886 /* decode the previously encoded string */ 11887 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 11888 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11889 { 11890 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 11891 } 11892 else 11893 { 11894 fct_chk( enclen == outlen ); 11895 } 11896 11897 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 11898 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11899 { 11900 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 11901 } 11902 else 11903 { 11904 fct_chk( outlen == 0 ); 11905 } 11906 11907 11908 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 11909 11910 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 11911 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 11912 FCT_TEST_END(); 11913 #endif /* POLARSSL_AES_C */ 11914 11915 #ifdef POLARSSL_AES_C 11916 11917 FCT_TEST_BGN(aes_encrypt_and_decrypt_15_bytes) 11918 size_t length = 15; 11919 unsigned char key[32]; 11920 unsigned char iv[16]; 11921 11922 const cipher_info_t *cipher_info; 11923 cipher_context_t ctx_dec; 11924 cipher_context_t ctx_enc; 11925 11926 unsigned char inbuf[64]; 11927 unsigned char encbuf[64]; 11928 unsigned char decbuf[64]; 11929 11930 size_t outlen = 0; 11931 size_t enclen = 0; 11932 11933 memset( key, 0, 32 ); 11934 memset( iv , 0, 16 ); 11935 11936 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 11937 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 11938 11939 memset( inbuf, 5, 64 ); 11940 memset( encbuf, 0, 64 ); 11941 memset( decbuf, 0, 64 ); 11942 11943 /* Check and get info structures */ 11944 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 11945 fct_chk( NULL != cipher_info ); 11946 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 11947 11948 /* Initialise enc and dec contexts */ 11949 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 11950 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 11951 11952 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 11953 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 11954 11955 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 11956 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 11957 11958 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11959 { 11960 enclen = cipher_get_block_size( &ctx_enc ) 11961 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 11962 } 11963 else 11964 { 11965 enclen = length; 11966 } 11967 11968 /* encode length number of bytes from inbuf */ 11969 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 11970 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11971 { 11972 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 11973 } 11974 else 11975 { 11976 fct_chk( outlen == enclen ); 11977 } 11978 11979 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 11980 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11981 { 11982 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 11983 } 11984 else 11985 { 11986 fct_chk( outlen == 0 ); 11987 } 11988 11989 11990 /* decode the previously encoded string */ 11991 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 11992 if( POLARSSL_MODE_CBC == cipher_info->mode ) 11993 { 11994 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 11995 } 11996 else 11997 { 11998 fct_chk( enclen == outlen ); 11999 } 12000 12001 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 12002 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12003 { 12004 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 12005 } 12006 else 12007 { 12008 fct_chk( outlen == 0 ); 12009 } 12010 12011 12012 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 12013 12014 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 12015 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 12016 FCT_TEST_END(); 12017 #endif /* POLARSSL_AES_C */ 12018 12019 #ifdef POLARSSL_AES_C 12020 12021 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes) 12022 size_t length = 16; 12023 unsigned char key[32]; 12024 unsigned char iv[16]; 12025 12026 const cipher_info_t *cipher_info; 12027 cipher_context_t ctx_dec; 12028 cipher_context_t ctx_enc; 12029 12030 unsigned char inbuf[64]; 12031 unsigned char encbuf[64]; 12032 unsigned char decbuf[64]; 12033 12034 size_t outlen = 0; 12035 size_t enclen = 0; 12036 12037 memset( key, 0, 32 ); 12038 memset( iv , 0, 16 ); 12039 12040 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 12041 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 12042 12043 memset( inbuf, 5, 64 ); 12044 memset( encbuf, 0, 64 ); 12045 memset( decbuf, 0, 64 ); 12046 12047 /* Check and get info structures */ 12048 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 12049 fct_chk( NULL != cipher_info ); 12050 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 12051 12052 /* Initialise enc and dec contexts */ 12053 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 12054 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 12055 12056 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 12057 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 12058 12059 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 12060 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 12061 12062 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12063 { 12064 enclen = cipher_get_block_size( &ctx_enc ) 12065 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 12066 } 12067 else 12068 { 12069 enclen = length; 12070 } 12071 12072 /* encode length number of bytes from inbuf */ 12073 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 12074 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12075 { 12076 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 12077 } 12078 else 12079 { 12080 fct_chk( outlen == enclen ); 12081 } 12082 12083 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 12084 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12085 { 12086 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 12087 } 12088 else 12089 { 12090 fct_chk( outlen == 0 ); 12091 } 12092 12093 12094 /* decode the previously encoded string */ 12095 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 12096 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12097 { 12098 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 12099 } 12100 else 12101 { 12102 fct_chk( enclen == outlen ); 12103 } 12104 12105 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 12106 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12107 { 12108 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 12109 } 12110 else 12111 { 12112 fct_chk( outlen == 0 ); 12113 } 12114 12115 12116 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 12117 12118 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 12119 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 12120 FCT_TEST_END(); 12121 #endif /* POLARSSL_AES_C */ 12122 12123 #ifdef POLARSSL_AES_C 12124 12125 FCT_TEST_BGN(aes_encrypt_and_decrypt_17_bytes) 12126 size_t length = 17; 12127 unsigned char key[32]; 12128 unsigned char iv[16]; 12129 12130 const cipher_info_t *cipher_info; 12131 cipher_context_t ctx_dec; 12132 cipher_context_t ctx_enc; 12133 12134 unsigned char inbuf[64]; 12135 unsigned char encbuf[64]; 12136 unsigned char decbuf[64]; 12137 12138 size_t outlen = 0; 12139 size_t enclen = 0; 12140 12141 memset( key, 0, 32 ); 12142 memset( iv , 0, 16 ); 12143 12144 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 12145 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 12146 12147 memset( inbuf, 5, 64 ); 12148 memset( encbuf, 0, 64 ); 12149 memset( decbuf, 0, 64 ); 12150 12151 /* Check and get info structures */ 12152 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 12153 fct_chk( NULL != cipher_info ); 12154 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 12155 12156 /* Initialise enc and dec contexts */ 12157 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 12158 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 12159 12160 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 12161 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 12162 12163 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 12164 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 12165 12166 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12167 { 12168 enclen = cipher_get_block_size( &ctx_enc ) 12169 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 12170 } 12171 else 12172 { 12173 enclen = length; 12174 } 12175 12176 /* encode length number of bytes from inbuf */ 12177 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 12178 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12179 { 12180 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 12181 } 12182 else 12183 { 12184 fct_chk( outlen == enclen ); 12185 } 12186 12187 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 12188 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12189 { 12190 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 12191 } 12192 else 12193 { 12194 fct_chk( outlen == 0 ); 12195 } 12196 12197 12198 /* decode the previously encoded string */ 12199 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 12200 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12201 { 12202 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 12203 } 12204 else 12205 { 12206 fct_chk( enclen == outlen ); 12207 } 12208 12209 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 12210 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12211 { 12212 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 12213 } 12214 else 12215 { 12216 fct_chk( outlen == 0 ); 12217 } 12218 12219 12220 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 12221 12222 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 12223 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 12224 FCT_TEST_END(); 12225 #endif /* POLARSSL_AES_C */ 12226 12227 #ifdef POLARSSL_AES_C 12228 12229 FCT_TEST_BGN(aes_encrypt_and_decrypt_31_bytes) 12230 size_t length = 31; 12231 unsigned char key[32]; 12232 unsigned char iv[16]; 12233 12234 const cipher_info_t *cipher_info; 12235 cipher_context_t ctx_dec; 12236 cipher_context_t ctx_enc; 12237 12238 unsigned char inbuf[64]; 12239 unsigned char encbuf[64]; 12240 unsigned char decbuf[64]; 12241 12242 size_t outlen = 0; 12243 size_t enclen = 0; 12244 12245 memset( key, 0, 32 ); 12246 memset( iv , 0, 16 ); 12247 12248 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 12249 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 12250 12251 memset( inbuf, 5, 64 ); 12252 memset( encbuf, 0, 64 ); 12253 memset( decbuf, 0, 64 ); 12254 12255 /* Check and get info structures */ 12256 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 12257 fct_chk( NULL != cipher_info ); 12258 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 12259 12260 /* Initialise enc and dec contexts */ 12261 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 12262 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 12263 12264 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 12265 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 12266 12267 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 12268 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 12269 12270 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12271 { 12272 enclen = cipher_get_block_size( &ctx_enc ) 12273 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 12274 } 12275 else 12276 { 12277 enclen = length; 12278 } 12279 12280 /* encode length number of bytes from inbuf */ 12281 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 12282 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12283 { 12284 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 12285 } 12286 else 12287 { 12288 fct_chk( outlen == enclen ); 12289 } 12290 12291 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 12292 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12293 { 12294 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 12295 } 12296 else 12297 { 12298 fct_chk( outlen == 0 ); 12299 } 12300 12301 12302 /* decode the previously encoded string */ 12303 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 12304 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12305 { 12306 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 12307 } 12308 else 12309 { 12310 fct_chk( enclen == outlen ); 12311 } 12312 12313 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 12314 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12315 { 12316 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 12317 } 12318 else 12319 { 12320 fct_chk( outlen == 0 ); 12321 } 12322 12323 12324 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 12325 12326 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 12327 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 12328 FCT_TEST_END(); 12329 #endif /* POLARSSL_AES_C */ 12330 12331 #ifdef POLARSSL_AES_C 12332 12333 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes) 12334 size_t length = 32; 12335 unsigned char key[32]; 12336 unsigned char iv[16]; 12337 12338 const cipher_info_t *cipher_info; 12339 cipher_context_t ctx_dec; 12340 cipher_context_t ctx_enc; 12341 12342 unsigned char inbuf[64]; 12343 unsigned char encbuf[64]; 12344 unsigned char decbuf[64]; 12345 12346 size_t outlen = 0; 12347 size_t enclen = 0; 12348 12349 memset( key, 0, 32 ); 12350 memset( iv , 0, 16 ); 12351 12352 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 12353 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 12354 12355 memset( inbuf, 5, 64 ); 12356 memset( encbuf, 0, 64 ); 12357 memset( decbuf, 0, 64 ); 12358 12359 /* Check and get info structures */ 12360 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 12361 fct_chk( NULL != cipher_info ); 12362 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 12363 12364 /* Initialise enc and dec contexts */ 12365 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 12366 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 12367 12368 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 12369 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 12370 12371 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 12372 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 12373 12374 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12375 { 12376 enclen = cipher_get_block_size( &ctx_enc ) 12377 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 12378 } 12379 else 12380 { 12381 enclen = length; 12382 } 12383 12384 /* encode length number of bytes from inbuf */ 12385 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 12386 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12387 { 12388 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 12389 } 12390 else 12391 { 12392 fct_chk( outlen == enclen ); 12393 } 12394 12395 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 12396 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12397 { 12398 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 12399 } 12400 else 12401 { 12402 fct_chk( outlen == 0 ); 12403 } 12404 12405 12406 /* decode the previously encoded string */ 12407 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 12408 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12409 { 12410 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 12411 } 12412 else 12413 { 12414 fct_chk( enclen == outlen ); 12415 } 12416 12417 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 12418 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12419 { 12420 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 12421 } 12422 else 12423 { 12424 fct_chk( outlen == 0 ); 12425 } 12426 12427 12428 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 12429 12430 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 12431 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 12432 FCT_TEST_END(); 12433 #endif /* POLARSSL_AES_C */ 12434 12435 #ifdef POLARSSL_AES_C 12436 12437 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes) 12438 size_t length = 33; 12439 unsigned char key[32]; 12440 unsigned char iv[16]; 12441 12442 const cipher_info_t *cipher_info; 12443 cipher_context_t ctx_dec; 12444 cipher_context_t ctx_enc; 12445 12446 unsigned char inbuf[64]; 12447 unsigned char encbuf[64]; 12448 unsigned char decbuf[64]; 12449 12450 size_t outlen = 0; 12451 size_t enclen = 0; 12452 12453 memset( key, 0, 32 ); 12454 memset( iv , 0, 16 ); 12455 12456 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 12457 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 12458 12459 memset( inbuf, 5, 64 ); 12460 memset( encbuf, 0, 64 ); 12461 memset( decbuf, 0, 64 ); 12462 12463 /* Check and get info structures */ 12464 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 12465 fct_chk( NULL != cipher_info ); 12466 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 12467 12468 /* Initialise enc and dec contexts */ 12469 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 12470 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 12471 12472 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 12473 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 12474 12475 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 12476 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 12477 12478 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12479 { 12480 enclen = cipher_get_block_size( &ctx_enc ) 12481 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 12482 } 12483 else 12484 { 12485 enclen = length; 12486 } 12487 12488 /* encode length number of bytes from inbuf */ 12489 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 12490 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12491 { 12492 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 12493 } 12494 else 12495 { 12496 fct_chk( outlen == enclen ); 12497 } 12498 12499 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 12500 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12501 { 12502 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 12503 } 12504 else 12505 { 12506 fct_chk( outlen == 0 ); 12507 } 12508 12509 12510 /* decode the previously encoded string */ 12511 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 12512 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12513 { 12514 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 12515 } 12516 else 12517 { 12518 fct_chk( enclen == outlen ); 12519 } 12520 12521 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 12522 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12523 { 12524 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 12525 } 12526 else 12527 { 12528 fct_chk( outlen == 0 ); 12529 } 12530 12531 12532 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 12533 12534 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 12535 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 12536 FCT_TEST_END(); 12537 #endif /* POLARSSL_AES_C */ 12538 12539 #ifdef POLARSSL_AES_C 12540 12541 FCT_TEST_BGN(aes_encrypt_and_decrypt_47_bytes) 12542 size_t length = 47; 12543 unsigned char key[32]; 12544 unsigned char iv[16]; 12545 12546 const cipher_info_t *cipher_info; 12547 cipher_context_t ctx_dec; 12548 cipher_context_t ctx_enc; 12549 12550 unsigned char inbuf[64]; 12551 unsigned char encbuf[64]; 12552 unsigned char decbuf[64]; 12553 12554 size_t outlen = 0; 12555 size_t enclen = 0; 12556 12557 memset( key, 0, 32 ); 12558 memset( iv , 0, 16 ); 12559 12560 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 12561 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 12562 12563 memset( inbuf, 5, 64 ); 12564 memset( encbuf, 0, 64 ); 12565 memset( decbuf, 0, 64 ); 12566 12567 /* Check and get info structures */ 12568 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 12569 fct_chk( NULL != cipher_info ); 12570 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 12571 12572 /* Initialise enc and dec contexts */ 12573 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 12574 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 12575 12576 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 12577 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 12578 12579 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 12580 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 12581 12582 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12583 { 12584 enclen = cipher_get_block_size( &ctx_enc ) 12585 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 12586 } 12587 else 12588 { 12589 enclen = length; 12590 } 12591 12592 /* encode length number of bytes from inbuf */ 12593 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 12594 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12595 { 12596 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 12597 } 12598 else 12599 { 12600 fct_chk( outlen == enclen ); 12601 } 12602 12603 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 12604 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12605 { 12606 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 12607 } 12608 else 12609 { 12610 fct_chk( outlen == 0 ); 12611 } 12612 12613 12614 /* decode the previously encoded string */ 12615 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 12616 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12617 { 12618 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 12619 } 12620 else 12621 { 12622 fct_chk( enclen == outlen ); 12623 } 12624 12625 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 12626 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12627 { 12628 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 12629 } 12630 else 12631 { 12632 fct_chk( outlen == 0 ); 12633 } 12634 12635 12636 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 12637 12638 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 12639 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 12640 FCT_TEST_END(); 12641 #endif /* POLARSSL_AES_C */ 12642 12643 #ifdef POLARSSL_AES_C 12644 12645 FCT_TEST_BGN(aes_encrypt_and_decrypt_48_bytes) 12646 size_t length = 48; 12647 unsigned char key[32]; 12648 unsigned char iv[16]; 12649 12650 const cipher_info_t *cipher_info; 12651 cipher_context_t ctx_dec; 12652 cipher_context_t ctx_enc; 12653 12654 unsigned char inbuf[64]; 12655 unsigned char encbuf[64]; 12656 unsigned char decbuf[64]; 12657 12658 size_t outlen = 0; 12659 size_t enclen = 0; 12660 12661 memset( key, 0, 32 ); 12662 memset( iv , 0, 16 ); 12663 12664 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 12665 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 12666 12667 memset( inbuf, 5, 64 ); 12668 memset( encbuf, 0, 64 ); 12669 memset( decbuf, 0, 64 ); 12670 12671 /* Check and get info structures */ 12672 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 12673 fct_chk( NULL != cipher_info ); 12674 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 12675 12676 /* Initialise enc and dec contexts */ 12677 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 12678 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 12679 12680 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 12681 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 12682 12683 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 12684 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 12685 12686 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12687 { 12688 enclen = cipher_get_block_size( &ctx_enc ) 12689 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 12690 } 12691 else 12692 { 12693 enclen = length; 12694 } 12695 12696 /* encode length number of bytes from inbuf */ 12697 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 12698 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12699 { 12700 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 12701 } 12702 else 12703 { 12704 fct_chk( outlen == enclen ); 12705 } 12706 12707 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 12708 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12709 { 12710 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 12711 } 12712 else 12713 { 12714 fct_chk( outlen == 0 ); 12715 } 12716 12717 12718 /* decode the previously encoded string */ 12719 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 12720 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12721 { 12722 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 12723 } 12724 else 12725 { 12726 fct_chk( enclen == outlen ); 12727 } 12728 12729 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 12730 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12731 { 12732 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 12733 } 12734 else 12735 { 12736 fct_chk( outlen == 0 ); 12737 } 12738 12739 12740 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 12741 12742 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 12743 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 12744 FCT_TEST_END(); 12745 #endif /* POLARSSL_AES_C */ 12746 12747 #ifdef POLARSSL_AES_C 12748 12749 FCT_TEST_BGN(aes_encrypt_and_decrypt_49_bytes) 12750 size_t length = 49; 12751 unsigned char key[32]; 12752 unsigned char iv[16]; 12753 12754 const cipher_info_t *cipher_info; 12755 cipher_context_t ctx_dec; 12756 cipher_context_t ctx_enc; 12757 12758 unsigned char inbuf[64]; 12759 unsigned char encbuf[64]; 12760 unsigned char decbuf[64]; 12761 12762 size_t outlen = 0; 12763 size_t enclen = 0; 12764 12765 memset( key, 0, 32 ); 12766 memset( iv , 0, 16 ); 12767 12768 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 12769 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 12770 12771 memset( inbuf, 5, 64 ); 12772 memset( encbuf, 0, 64 ); 12773 memset( decbuf, 0, 64 ); 12774 12775 /* Check and get info structures */ 12776 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 12777 fct_chk( NULL != cipher_info ); 12778 fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info ); 12779 12780 /* Initialise enc and dec contexts */ 12781 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 12782 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 12783 12784 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 12785 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 12786 12787 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 12788 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 12789 12790 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12791 { 12792 enclen = cipher_get_block_size( &ctx_enc ) 12793 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 12794 } 12795 else 12796 { 12797 enclen = length; 12798 } 12799 12800 /* encode length number of bytes from inbuf */ 12801 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 12802 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12803 { 12804 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 12805 } 12806 else 12807 { 12808 fct_chk( outlen == enclen ); 12809 } 12810 12811 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 12812 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12813 { 12814 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 12815 } 12816 else 12817 { 12818 fct_chk( outlen == 0 ); 12819 } 12820 12821 12822 /* decode the previously encoded string */ 12823 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 12824 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12825 { 12826 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 12827 } 12828 else 12829 { 12830 fct_chk( enclen == outlen ); 12831 } 12832 12833 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 12834 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12835 { 12836 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 12837 } 12838 else 12839 { 12840 fct_chk( outlen == 0 ); 12841 } 12842 12843 12844 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 12845 12846 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 12847 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 12848 FCT_TEST_END(); 12849 #endif /* POLARSSL_AES_C */ 12850 12851 #ifdef POLARSSL_AES_C 12852 12853 FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes_in_multiple_parts) 12854 size_t first_length = 0; 12855 size_t second_length = 0; 12856 size_t length = first_length + second_length; 12857 unsigned char key[32]; 12858 unsigned char iv[16]; 12859 12860 cipher_context_t ctx_dec; 12861 cipher_context_t ctx_enc; 12862 const cipher_info_t *cipher_info; 12863 12864 unsigned char inbuf[64]; 12865 unsigned char encbuf[64]; 12866 unsigned char decbuf[64]; 12867 12868 size_t outlen = 0; 12869 size_t totaloutlen = 0; 12870 size_t enclen = 0; 12871 12872 memset( key, 0, 32 ); 12873 memset( iv , 0, 16 ); 12874 12875 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 12876 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 12877 12878 memset( inbuf, 5, 64 ); 12879 memset( encbuf, 0, 64 ); 12880 memset( decbuf, 0, 64 ); 12881 12882 /* Initialise enc and dec contexts */ 12883 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 12884 fct_chk( NULL != cipher_info); 12885 12886 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 12887 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 12888 12889 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 12890 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 12891 12892 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 12893 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 12894 12895 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12896 { 12897 enclen = cipher_get_block_size(&ctx_enc ) 12898 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 12899 } 12900 else 12901 { 12902 enclen = length; 12903 } 12904 12905 /* encode length number of bytes from inbuf */ 12906 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 12907 totaloutlen = outlen; 12908 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 12909 totaloutlen += outlen; 12910 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12911 { 12912 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 12913 } 12914 else 12915 { 12916 fct_chk( totaloutlen == enclen ); 12917 } 12918 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 12919 totaloutlen += outlen; 12920 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12921 { 12922 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 12923 } 12924 else 12925 { 12926 fct_chk( outlen == 0 ); 12927 } 12928 12929 /* decode the previously encoded string */ 12930 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 12931 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12932 { 12933 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 12934 } 12935 else 12936 { 12937 fct_chk( enclen == outlen ); 12938 } 12939 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 12940 if( POLARSSL_MODE_CBC == cipher_info->mode ) 12941 { 12942 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 12943 } 12944 else 12945 { 12946 fct_chk( outlen == 0 ); 12947 } 12948 12949 12950 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 12951 12952 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 12953 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 12954 FCT_TEST_END(); 12955 #endif /* POLARSSL_AES_C */ 12956 12957 #ifdef POLARSSL_AES_C 12958 12959 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_1) 12960 size_t first_length = 1; 12961 size_t second_length = 0; 12962 size_t length = first_length + second_length; 12963 unsigned char key[32]; 12964 unsigned char iv[16]; 12965 12966 cipher_context_t ctx_dec; 12967 cipher_context_t ctx_enc; 12968 const cipher_info_t *cipher_info; 12969 12970 unsigned char inbuf[64]; 12971 unsigned char encbuf[64]; 12972 unsigned char decbuf[64]; 12973 12974 size_t outlen = 0; 12975 size_t totaloutlen = 0; 12976 size_t enclen = 0; 12977 12978 memset( key, 0, 32 ); 12979 memset( iv , 0, 16 ); 12980 12981 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 12982 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 12983 12984 memset( inbuf, 5, 64 ); 12985 memset( encbuf, 0, 64 ); 12986 memset( decbuf, 0, 64 ); 12987 12988 /* Initialise enc and dec contexts */ 12989 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 12990 fct_chk( NULL != cipher_info); 12991 12992 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 12993 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 12994 12995 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 12996 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 12997 12998 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 12999 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 13000 13001 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13002 { 13003 enclen = cipher_get_block_size(&ctx_enc ) 13004 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 13005 } 13006 else 13007 { 13008 enclen = length; 13009 } 13010 13011 /* encode length number of bytes from inbuf */ 13012 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 13013 totaloutlen = outlen; 13014 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 13015 totaloutlen += outlen; 13016 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13017 { 13018 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 13019 } 13020 else 13021 { 13022 fct_chk( totaloutlen == enclen ); 13023 } 13024 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 13025 totaloutlen += outlen; 13026 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13027 { 13028 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 13029 } 13030 else 13031 { 13032 fct_chk( outlen == 0 ); 13033 } 13034 13035 /* decode the previously encoded string */ 13036 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 13037 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13038 { 13039 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 13040 } 13041 else 13042 { 13043 fct_chk( enclen == outlen ); 13044 } 13045 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 13046 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13047 { 13048 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 13049 } 13050 else 13051 { 13052 fct_chk( outlen == 0 ); 13053 } 13054 13055 13056 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 13057 13058 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 13059 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 13060 FCT_TEST_END(); 13061 #endif /* POLARSSL_AES_C */ 13062 13063 #ifdef POLARSSL_AES_C 13064 13065 FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_2) 13066 size_t first_length = 0; 13067 size_t second_length = 1; 13068 size_t length = first_length + second_length; 13069 unsigned char key[32]; 13070 unsigned char iv[16]; 13071 13072 cipher_context_t ctx_dec; 13073 cipher_context_t ctx_enc; 13074 const cipher_info_t *cipher_info; 13075 13076 unsigned char inbuf[64]; 13077 unsigned char encbuf[64]; 13078 unsigned char decbuf[64]; 13079 13080 size_t outlen = 0; 13081 size_t totaloutlen = 0; 13082 size_t enclen = 0; 13083 13084 memset( key, 0, 32 ); 13085 memset( iv , 0, 16 ); 13086 13087 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 13088 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 13089 13090 memset( inbuf, 5, 64 ); 13091 memset( encbuf, 0, 64 ); 13092 memset( decbuf, 0, 64 ); 13093 13094 /* Initialise enc and dec contexts */ 13095 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 13096 fct_chk( NULL != cipher_info); 13097 13098 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 13099 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 13100 13101 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 13102 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 13103 13104 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 13105 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 13106 13107 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13108 { 13109 enclen = cipher_get_block_size(&ctx_enc ) 13110 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 13111 } 13112 else 13113 { 13114 enclen = length; 13115 } 13116 13117 /* encode length number of bytes from inbuf */ 13118 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 13119 totaloutlen = outlen; 13120 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 13121 totaloutlen += outlen; 13122 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13123 { 13124 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 13125 } 13126 else 13127 { 13128 fct_chk( totaloutlen == enclen ); 13129 } 13130 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 13131 totaloutlen += outlen; 13132 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13133 { 13134 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 13135 } 13136 else 13137 { 13138 fct_chk( outlen == 0 ); 13139 } 13140 13141 /* decode the previously encoded string */ 13142 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 13143 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13144 { 13145 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 13146 } 13147 else 13148 { 13149 fct_chk( enclen == outlen ); 13150 } 13151 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 13152 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13153 { 13154 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 13155 } 13156 else 13157 { 13158 fct_chk( outlen == 0 ); 13159 } 13160 13161 13162 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 13163 13164 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 13165 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 13166 FCT_TEST_END(); 13167 #endif /* POLARSSL_AES_C */ 13168 13169 #ifdef POLARSSL_AES_C 13170 13171 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_1) 13172 size_t first_length = 16; 13173 size_t second_length = 0; 13174 size_t length = first_length + second_length; 13175 unsigned char key[32]; 13176 unsigned char iv[16]; 13177 13178 cipher_context_t ctx_dec; 13179 cipher_context_t ctx_enc; 13180 const cipher_info_t *cipher_info; 13181 13182 unsigned char inbuf[64]; 13183 unsigned char encbuf[64]; 13184 unsigned char decbuf[64]; 13185 13186 size_t outlen = 0; 13187 size_t totaloutlen = 0; 13188 size_t enclen = 0; 13189 13190 memset( key, 0, 32 ); 13191 memset( iv , 0, 16 ); 13192 13193 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 13194 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 13195 13196 memset( inbuf, 5, 64 ); 13197 memset( encbuf, 0, 64 ); 13198 memset( decbuf, 0, 64 ); 13199 13200 /* Initialise enc and dec contexts */ 13201 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 13202 fct_chk( NULL != cipher_info); 13203 13204 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 13205 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 13206 13207 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 13208 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 13209 13210 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 13211 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 13212 13213 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13214 { 13215 enclen = cipher_get_block_size(&ctx_enc ) 13216 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 13217 } 13218 else 13219 { 13220 enclen = length; 13221 } 13222 13223 /* encode length number of bytes from inbuf */ 13224 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 13225 totaloutlen = outlen; 13226 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 13227 totaloutlen += outlen; 13228 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13229 { 13230 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 13231 } 13232 else 13233 { 13234 fct_chk( totaloutlen == enclen ); 13235 } 13236 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 13237 totaloutlen += outlen; 13238 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13239 { 13240 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 13241 } 13242 else 13243 { 13244 fct_chk( outlen == 0 ); 13245 } 13246 13247 /* decode the previously encoded string */ 13248 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 13249 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13250 { 13251 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 13252 } 13253 else 13254 { 13255 fct_chk( enclen == outlen ); 13256 } 13257 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 13258 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13259 { 13260 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 13261 } 13262 else 13263 { 13264 fct_chk( outlen == 0 ); 13265 } 13266 13267 13268 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 13269 13270 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 13271 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 13272 FCT_TEST_END(); 13273 #endif /* POLARSSL_AES_C */ 13274 13275 #ifdef POLARSSL_AES_C 13276 13277 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_2) 13278 size_t first_length = 0; 13279 size_t second_length = 16; 13280 size_t length = first_length + second_length; 13281 unsigned char key[32]; 13282 unsigned char iv[16]; 13283 13284 cipher_context_t ctx_dec; 13285 cipher_context_t ctx_enc; 13286 const cipher_info_t *cipher_info; 13287 13288 unsigned char inbuf[64]; 13289 unsigned char encbuf[64]; 13290 unsigned char decbuf[64]; 13291 13292 size_t outlen = 0; 13293 size_t totaloutlen = 0; 13294 size_t enclen = 0; 13295 13296 memset( key, 0, 32 ); 13297 memset( iv , 0, 16 ); 13298 13299 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 13300 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 13301 13302 memset( inbuf, 5, 64 ); 13303 memset( encbuf, 0, 64 ); 13304 memset( decbuf, 0, 64 ); 13305 13306 /* Initialise enc and dec contexts */ 13307 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 13308 fct_chk( NULL != cipher_info); 13309 13310 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 13311 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 13312 13313 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 13314 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 13315 13316 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 13317 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 13318 13319 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13320 { 13321 enclen = cipher_get_block_size(&ctx_enc ) 13322 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 13323 } 13324 else 13325 { 13326 enclen = length; 13327 } 13328 13329 /* encode length number of bytes from inbuf */ 13330 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 13331 totaloutlen = outlen; 13332 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 13333 totaloutlen += outlen; 13334 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13335 { 13336 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 13337 } 13338 else 13339 { 13340 fct_chk( totaloutlen == enclen ); 13341 } 13342 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 13343 totaloutlen += outlen; 13344 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13345 { 13346 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 13347 } 13348 else 13349 { 13350 fct_chk( outlen == 0 ); 13351 } 13352 13353 /* decode the previously encoded string */ 13354 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 13355 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13356 { 13357 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 13358 } 13359 else 13360 { 13361 fct_chk( enclen == outlen ); 13362 } 13363 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 13364 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13365 { 13366 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 13367 } 13368 else 13369 { 13370 fct_chk( outlen == 0 ); 13371 } 13372 13373 13374 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 13375 13376 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 13377 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 13378 FCT_TEST_END(); 13379 #endif /* POLARSSL_AES_C */ 13380 13381 #ifdef POLARSSL_AES_C 13382 13383 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_3) 13384 size_t first_length = 1; 13385 size_t second_length = 15; 13386 size_t length = first_length + second_length; 13387 unsigned char key[32]; 13388 unsigned char iv[16]; 13389 13390 cipher_context_t ctx_dec; 13391 cipher_context_t ctx_enc; 13392 const cipher_info_t *cipher_info; 13393 13394 unsigned char inbuf[64]; 13395 unsigned char encbuf[64]; 13396 unsigned char decbuf[64]; 13397 13398 size_t outlen = 0; 13399 size_t totaloutlen = 0; 13400 size_t enclen = 0; 13401 13402 memset( key, 0, 32 ); 13403 memset( iv , 0, 16 ); 13404 13405 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 13406 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 13407 13408 memset( inbuf, 5, 64 ); 13409 memset( encbuf, 0, 64 ); 13410 memset( decbuf, 0, 64 ); 13411 13412 /* Initialise enc and dec contexts */ 13413 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 13414 fct_chk( NULL != cipher_info); 13415 13416 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 13417 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 13418 13419 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 13420 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 13421 13422 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 13423 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 13424 13425 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13426 { 13427 enclen = cipher_get_block_size(&ctx_enc ) 13428 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 13429 } 13430 else 13431 { 13432 enclen = length; 13433 } 13434 13435 /* encode length number of bytes from inbuf */ 13436 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 13437 totaloutlen = outlen; 13438 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 13439 totaloutlen += outlen; 13440 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13441 { 13442 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 13443 } 13444 else 13445 { 13446 fct_chk( totaloutlen == enclen ); 13447 } 13448 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 13449 totaloutlen += outlen; 13450 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13451 { 13452 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 13453 } 13454 else 13455 { 13456 fct_chk( outlen == 0 ); 13457 } 13458 13459 /* decode the previously encoded string */ 13460 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 13461 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13462 { 13463 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 13464 } 13465 else 13466 { 13467 fct_chk( enclen == outlen ); 13468 } 13469 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 13470 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13471 { 13472 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 13473 } 13474 else 13475 { 13476 fct_chk( outlen == 0 ); 13477 } 13478 13479 13480 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 13481 13482 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 13483 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 13484 FCT_TEST_END(); 13485 #endif /* POLARSSL_AES_C */ 13486 13487 #ifdef POLARSSL_AES_C 13488 13489 FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_4) 13490 size_t first_length = 15; 13491 size_t second_length = 1; 13492 size_t length = first_length + second_length; 13493 unsigned char key[32]; 13494 unsigned char iv[16]; 13495 13496 cipher_context_t ctx_dec; 13497 cipher_context_t ctx_enc; 13498 const cipher_info_t *cipher_info; 13499 13500 unsigned char inbuf[64]; 13501 unsigned char encbuf[64]; 13502 unsigned char decbuf[64]; 13503 13504 size_t outlen = 0; 13505 size_t totaloutlen = 0; 13506 size_t enclen = 0; 13507 13508 memset( key, 0, 32 ); 13509 memset( iv , 0, 16 ); 13510 13511 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 13512 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 13513 13514 memset( inbuf, 5, 64 ); 13515 memset( encbuf, 0, 64 ); 13516 memset( decbuf, 0, 64 ); 13517 13518 /* Initialise enc and dec contexts */ 13519 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 13520 fct_chk( NULL != cipher_info); 13521 13522 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 13523 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 13524 13525 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 13526 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 13527 13528 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 13529 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 13530 13531 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13532 { 13533 enclen = cipher_get_block_size(&ctx_enc ) 13534 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 13535 } 13536 else 13537 { 13538 enclen = length; 13539 } 13540 13541 /* encode length number of bytes from inbuf */ 13542 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 13543 totaloutlen = outlen; 13544 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 13545 totaloutlen += outlen; 13546 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13547 { 13548 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 13549 } 13550 else 13551 { 13552 fct_chk( totaloutlen == enclen ); 13553 } 13554 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 13555 totaloutlen += outlen; 13556 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13557 { 13558 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 13559 } 13560 else 13561 { 13562 fct_chk( outlen == 0 ); 13563 } 13564 13565 /* decode the previously encoded string */ 13566 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 13567 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13568 { 13569 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 13570 } 13571 else 13572 { 13573 fct_chk( enclen == outlen ); 13574 } 13575 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 13576 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13577 { 13578 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 13579 } 13580 else 13581 { 13582 fct_chk( outlen == 0 ); 13583 } 13584 13585 13586 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 13587 13588 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 13589 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 13590 FCT_TEST_END(); 13591 #endif /* POLARSSL_AES_C */ 13592 13593 #ifdef POLARSSL_AES_C 13594 13595 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 13596 size_t first_length = 15; 13597 size_t second_length = 7; 13598 size_t length = first_length + second_length; 13599 unsigned char key[32]; 13600 unsigned char iv[16]; 13601 13602 cipher_context_t ctx_dec; 13603 cipher_context_t ctx_enc; 13604 const cipher_info_t *cipher_info; 13605 13606 unsigned char inbuf[64]; 13607 unsigned char encbuf[64]; 13608 unsigned char decbuf[64]; 13609 13610 size_t outlen = 0; 13611 size_t totaloutlen = 0; 13612 size_t enclen = 0; 13613 13614 memset( key, 0, 32 ); 13615 memset( iv , 0, 16 ); 13616 13617 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 13618 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 13619 13620 memset( inbuf, 5, 64 ); 13621 memset( encbuf, 0, 64 ); 13622 memset( decbuf, 0, 64 ); 13623 13624 /* Initialise enc and dec contexts */ 13625 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 13626 fct_chk( NULL != cipher_info); 13627 13628 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 13629 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 13630 13631 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 13632 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 13633 13634 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 13635 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 13636 13637 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13638 { 13639 enclen = cipher_get_block_size(&ctx_enc ) 13640 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 13641 } 13642 else 13643 { 13644 enclen = length; 13645 } 13646 13647 /* encode length number of bytes from inbuf */ 13648 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 13649 totaloutlen = outlen; 13650 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 13651 totaloutlen += outlen; 13652 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13653 { 13654 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 13655 } 13656 else 13657 { 13658 fct_chk( totaloutlen == enclen ); 13659 } 13660 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 13661 totaloutlen += outlen; 13662 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13663 { 13664 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 13665 } 13666 else 13667 { 13668 fct_chk( outlen == 0 ); 13669 } 13670 13671 /* decode the previously encoded string */ 13672 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 13673 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13674 { 13675 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 13676 } 13677 else 13678 { 13679 fct_chk( enclen == outlen ); 13680 } 13681 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 13682 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13683 { 13684 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 13685 } 13686 else 13687 { 13688 fct_chk( outlen == 0 ); 13689 } 13690 13691 13692 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 13693 13694 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 13695 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 13696 FCT_TEST_END(); 13697 #endif /* POLARSSL_AES_C */ 13698 13699 #ifdef POLARSSL_AES_C 13700 13701 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 13702 size_t first_length = 16; 13703 size_t second_length = 6; 13704 size_t length = first_length + second_length; 13705 unsigned char key[32]; 13706 unsigned char iv[16]; 13707 13708 cipher_context_t ctx_dec; 13709 cipher_context_t ctx_enc; 13710 const cipher_info_t *cipher_info; 13711 13712 unsigned char inbuf[64]; 13713 unsigned char encbuf[64]; 13714 unsigned char decbuf[64]; 13715 13716 size_t outlen = 0; 13717 size_t totaloutlen = 0; 13718 size_t enclen = 0; 13719 13720 memset( key, 0, 32 ); 13721 memset( iv , 0, 16 ); 13722 13723 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 13724 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 13725 13726 memset( inbuf, 5, 64 ); 13727 memset( encbuf, 0, 64 ); 13728 memset( decbuf, 0, 64 ); 13729 13730 /* Initialise enc and dec contexts */ 13731 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 13732 fct_chk( NULL != cipher_info); 13733 13734 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 13735 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 13736 13737 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 13738 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 13739 13740 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 13741 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 13742 13743 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13744 { 13745 enclen = cipher_get_block_size(&ctx_enc ) 13746 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 13747 } 13748 else 13749 { 13750 enclen = length; 13751 } 13752 13753 /* encode length number of bytes from inbuf */ 13754 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 13755 totaloutlen = outlen; 13756 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 13757 totaloutlen += outlen; 13758 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13759 { 13760 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 13761 } 13762 else 13763 { 13764 fct_chk( totaloutlen == enclen ); 13765 } 13766 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 13767 totaloutlen += outlen; 13768 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13769 { 13770 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 13771 } 13772 else 13773 { 13774 fct_chk( outlen == 0 ); 13775 } 13776 13777 /* decode the previously encoded string */ 13778 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 13779 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13780 { 13781 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 13782 } 13783 else 13784 { 13785 fct_chk( enclen == outlen ); 13786 } 13787 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 13788 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13789 { 13790 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 13791 } 13792 else 13793 { 13794 fct_chk( outlen == 0 ); 13795 } 13796 13797 13798 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 13799 13800 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 13801 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 13802 FCT_TEST_END(); 13803 #endif /* POLARSSL_AES_C */ 13804 13805 #ifdef POLARSSL_AES_C 13806 13807 FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 13808 size_t first_length = 17; 13809 size_t second_length = 6; 13810 size_t length = first_length + second_length; 13811 unsigned char key[32]; 13812 unsigned char iv[16]; 13813 13814 cipher_context_t ctx_dec; 13815 cipher_context_t ctx_enc; 13816 const cipher_info_t *cipher_info; 13817 13818 unsigned char inbuf[64]; 13819 unsigned char encbuf[64]; 13820 unsigned char decbuf[64]; 13821 13822 size_t outlen = 0; 13823 size_t totaloutlen = 0; 13824 size_t enclen = 0; 13825 13826 memset( key, 0, 32 ); 13827 memset( iv , 0, 16 ); 13828 13829 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 13830 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 13831 13832 memset( inbuf, 5, 64 ); 13833 memset( encbuf, 0, 64 ); 13834 memset( decbuf, 0, 64 ); 13835 13836 /* Initialise enc and dec contexts */ 13837 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 13838 fct_chk( NULL != cipher_info); 13839 13840 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 13841 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 13842 13843 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 13844 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 13845 13846 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 13847 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 13848 13849 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13850 { 13851 enclen = cipher_get_block_size(&ctx_enc ) 13852 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 13853 } 13854 else 13855 { 13856 enclen = length; 13857 } 13858 13859 /* encode length number of bytes from inbuf */ 13860 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 13861 totaloutlen = outlen; 13862 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 13863 totaloutlen += outlen; 13864 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13865 { 13866 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 13867 } 13868 else 13869 { 13870 fct_chk( totaloutlen == enclen ); 13871 } 13872 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 13873 totaloutlen += outlen; 13874 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13875 { 13876 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 13877 } 13878 else 13879 { 13880 fct_chk( outlen == 0 ); 13881 } 13882 13883 /* decode the previously encoded string */ 13884 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 13885 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13886 { 13887 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 13888 } 13889 else 13890 { 13891 fct_chk( enclen == outlen ); 13892 } 13893 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 13894 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13895 { 13896 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 13897 } 13898 else 13899 { 13900 fct_chk( outlen == 0 ); 13901 } 13902 13903 13904 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 13905 13906 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 13907 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 13908 FCT_TEST_END(); 13909 #endif /* POLARSSL_AES_C */ 13910 13911 #ifdef POLARSSL_AES_C 13912 13913 FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes_in_multiple_parts_1) 13914 size_t first_length = 16; 13915 size_t second_length = 16; 13916 size_t length = first_length + second_length; 13917 unsigned char key[32]; 13918 unsigned char iv[16]; 13919 13920 cipher_context_t ctx_dec; 13921 cipher_context_t ctx_enc; 13922 const cipher_info_t *cipher_info; 13923 13924 unsigned char inbuf[64]; 13925 unsigned char encbuf[64]; 13926 unsigned char decbuf[64]; 13927 13928 size_t outlen = 0; 13929 size_t totaloutlen = 0; 13930 size_t enclen = 0; 13931 13932 memset( key, 0, 32 ); 13933 memset( iv , 0, 16 ); 13934 13935 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 13936 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 13937 13938 memset( inbuf, 5, 64 ); 13939 memset( encbuf, 0, 64 ); 13940 memset( decbuf, 0, 64 ); 13941 13942 /* Initialise enc and dec contexts */ 13943 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); 13944 fct_chk( NULL != cipher_info); 13945 13946 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 13947 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 13948 13949 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) ); 13950 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) ); 13951 13952 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 13953 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 13954 13955 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13956 { 13957 enclen = cipher_get_block_size(&ctx_enc ) 13958 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 13959 } 13960 else 13961 { 13962 enclen = length; 13963 } 13964 13965 /* encode length number of bytes from inbuf */ 13966 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 13967 totaloutlen = outlen; 13968 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 13969 totaloutlen += outlen; 13970 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13971 { 13972 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 13973 } 13974 else 13975 { 13976 fct_chk( totaloutlen == enclen ); 13977 } 13978 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 13979 totaloutlen += outlen; 13980 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13981 { 13982 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 13983 } 13984 else 13985 { 13986 fct_chk( outlen == 0 ); 13987 } 13988 13989 /* decode the previously encoded string */ 13990 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 13991 if( POLARSSL_MODE_CBC == cipher_info->mode ) 13992 { 13993 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 13994 } 13995 else 13996 { 13997 fct_chk( enclen == outlen ); 13998 } 13999 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 14000 if( POLARSSL_MODE_CBC == cipher_info->mode ) 14001 { 14002 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 14003 } 14004 else 14005 { 14006 fct_chk( outlen == 0 ); 14007 } 14008 14009 14010 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 14011 14012 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 14013 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 14014 FCT_TEST_END(); 14015 #endif /* POLARSSL_AES_C */ 14016 14017 } 14018 FCT_SUITE_END(); 14019 14020 #endif /* POLARSSL_CIPHER_C */ 14021 14022 } 14023 FCT_END(); 14024