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_DES_C 00284 00285 FCT_TEST_BGN(des_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_DES_CBC ); 00313 fct_chk( NULL != cipher_info ); 00314 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 00321 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 00386 00387 #ifdef POLARSSL_DES_C 00388 00389 FCT_TEST_BGN(des_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_DES_CBC ); 00417 fct_chk( NULL != cipher_info ); 00418 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 00425 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 00490 00491 #ifdef POLARSSL_DES_C 00492 00493 FCT_TEST_BGN(des_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_DES_CBC ); 00521 fct_chk( NULL != cipher_info ); 00522 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 00529 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 00594 00595 #ifdef POLARSSL_DES_C 00596 00597 FCT_TEST_BGN(des_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_DES_CBC ); 00625 fct_chk( NULL != cipher_info ); 00626 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 00633 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 00698 00699 #ifdef POLARSSL_DES_C 00700 00701 FCT_TEST_BGN(des_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_DES_CBC ); 00729 fct_chk( NULL != cipher_info ); 00730 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 00737 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 00802 00803 #ifdef POLARSSL_DES_C 00804 00805 FCT_TEST_BGN(des_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_DES_CBC ); 00833 fct_chk( NULL != cipher_info ); 00834 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 00841 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 00906 00907 #ifdef POLARSSL_DES_C 00908 00909 FCT_TEST_BGN(des_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_DES_CBC ); 00937 fct_chk( NULL != cipher_info ); 00938 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 00945 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 01010 01011 #ifdef POLARSSL_DES_C 01012 01013 FCT_TEST_BGN(des_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_DES_CBC ); 01041 fct_chk( NULL != cipher_info ); 01042 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 01049 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 01114 01115 #ifdef POLARSSL_DES_C 01116 01117 FCT_TEST_BGN(des_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_DES_CBC ); 01145 fct_chk( NULL != cipher_info ); 01146 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 01153 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 01218 01219 #ifdef POLARSSL_DES_C 01220 01221 FCT_TEST_BGN(des_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_DES_CBC ); 01249 fct_chk( NULL != cipher_info ); 01250 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 01257 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 01322 01323 #ifdef POLARSSL_DES_C 01324 01325 FCT_TEST_BGN(des_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_DES_CBC ); 01353 fct_chk( NULL != cipher_info ); 01354 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 01361 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 01426 01427 #ifdef POLARSSL_DES_C 01428 01429 FCT_TEST_BGN(des_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_DES_CBC ); 01457 fct_chk( NULL != cipher_info ); 01458 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 01465 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 01530 01531 #ifdef POLARSSL_DES_C 01532 01533 FCT_TEST_BGN(des_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_DES_CBC ); 01561 fct_chk( NULL != cipher_info ); 01562 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 01569 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 01634 01635 #ifdef POLARSSL_DES_C 01636 01637 FCT_TEST_BGN(des_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_DES_CBC ); 01665 fct_chk( NULL != cipher_info ); 01666 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 01673 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 01738 01739 #ifdef POLARSSL_DES_C 01740 01741 FCT_TEST_BGN(des_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_DES_CBC ); 01769 fct_chk( NULL != cipher_info ); 01770 fct_chk( cipher_info_from_string( "DES-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, 56, POLARSSL_DECRYPT ) ); 01777 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 01842 01843 #ifdef POLARSSL_DES_C 01844 01845 FCT_TEST_BGN(des_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_DES_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, 56, POLARSSL_DECRYPT ) ); 01882 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 01948 01949 #ifdef POLARSSL_DES_C 01950 01951 FCT_TEST_BGN(des_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_DES_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, 56, POLARSSL_DECRYPT ) ); 01988 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 02054 02055 #ifdef POLARSSL_DES_C 02056 02057 FCT_TEST_BGN(des_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_DES_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, 56, POLARSSL_DECRYPT ) ); 02094 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 02160 02161 #ifdef POLARSSL_DES_C 02162 02163 FCT_TEST_BGN(des_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_DES_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, 56, POLARSSL_DECRYPT ) ); 02200 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 02266 02267 #ifdef POLARSSL_DES_C 02268 02269 FCT_TEST_BGN(des_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_DES_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, 56, POLARSSL_DECRYPT ) ); 02306 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 02372 02373 #ifdef POLARSSL_DES_C 02374 02375 FCT_TEST_BGN(des_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_DES_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, 56, POLARSSL_DECRYPT ) ); 02412 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 02478 02479 #ifdef POLARSSL_DES_C 02480 02481 FCT_TEST_BGN(des_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_DES_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, 56, POLARSSL_DECRYPT ) ); 02518 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 02584 02585 #ifdef POLARSSL_DES_C 02586 02587 FCT_TEST_BGN(des_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_DES_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, 56, POLARSSL_DECRYPT ) ); 02624 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 02690 02691 #ifdef POLARSSL_DES_C 02692 02693 FCT_TEST_BGN(des_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_DES_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, 56, POLARSSL_DECRYPT ) ); 02730 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 02796 02797 #ifdef POLARSSL_DES_C 02798 02799 FCT_TEST_BGN(des_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_DES_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, 56, POLARSSL_DECRYPT ) ); 02836 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 02902 02903 #ifdef POLARSSL_DES_C 02904 02905 FCT_TEST_BGN(des_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_DES_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, 56, POLARSSL_DECRYPT ) ); 02942 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */ 03008 03009 #ifdef POLARSSL_DES_C 03010 03011 FCT_TEST_BGN(des_encrypt_and_decrypt_0_bytes) 03012 size_t length = 0; 03013 unsigned char key[32]; 03014 unsigned char iv[16]; 03015 03016 const cipher_info_t *cipher_info; 03017 cipher_context_t ctx_dec; 03018 cipher_context_t ctx_enc; 03019 03020 unsigned char inbuf[64]; 03021 unsigned char encbuf[64]; 03022 unsigned char decbuf[64]; 03023 03024 size_t outlen = 0; 03025 size_t enclen = 0; 03026 03027 memset( key, 0, 32 ); 03028 memset( iv , 0, 16 ); 03029 03030 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03031 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03032 03033 memset( inbuf, 5, 64 ); 03034 memset( encbuf, 0, 64 ); 03035 memset( decbuf, 0, 64 ); 03036 03037 /* Check and get info structures */ 03038 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 03039 fct_chk( NULL != cipher_info ); 03040 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 03041 03042 /* Initialise enc and dec contexts */ 03043 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03044 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03045 03046 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 03047 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 03048 03049 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03050 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03051 03052 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03053 { 03054 enclen = cipher_get_block_size( &ctx_enc ) 03055 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03056 } 03057 else 03058 { 03059 enclen = length; 03060 } 03061 03062 /* encode length number of bytes from inbuf */ 03063 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03064 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03065 { 03066 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03067 } 03068 else 03069 { 03070 fct_chk( outlen == enclen ); 03071 } 03072 03073 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03074 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03075 { 03076 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03077 } 03078 else 03079 { 03080 fct_chk( outlen == 0 ); 03081 } 03082 03083 03084 /* decode the previously encoded string */ 03085 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03086 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03087 { 03088 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03089 } 03090 else 03091 { 03092 fct_chk( enclen == outlen ); 03093 } 03094 03095 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03096 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03097 { 03098 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03099 } 03100 else 03101 { 03102 fct_chk( outlen == 0 ); 03103 } 03104 03105 03106 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03107 03108 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03109 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03110 FCT_TEST_END(); 03111 #endif /* POLARSSL_DES_C */ 03112 03113 #ifdef POLARSSL_DES_C 03114 03115 FCT_TEST_BGN(des3_encrypt_and_decrypt_1_byte) 03116 size_t length = 1; 03117 unsigned char key[32]; 03118 unsigned char iv[16]; 03119 03120 const cipher_info_t *cipher_info; 03121 cipher_context_t ctx_dec; 03122 cipher_context_t ctx_enc; 03123 03124 unsigned char inbuf[64]; 03125 unsigned char encbuf[64]; 03126 unsigned char decbuf[64]; 03127 03128 size_t outlen = 0; 03129 size_t enclen = 0; 03130 03131 memset( key, 0, 32 ); 03132 memset( iv , 0, 16 ); 03133 03134 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03135 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03136 03137 memset( inbuf, 5, 64 ); 03138 memset( encbuf, 0, 64 ); 03139 memset( decbuf, 0, 64 ); 03140 03141 /* Check and get info structures */ 03142 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 03143 fct_chk( NULL != cipher_info ); 03144 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 03145 03146 /* Initialise enc and dec contexts */ 03147 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03148 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03149 03150 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 03151 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 03152 03153 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03154 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03155 03156 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03157 { 03158 enclen = cipher_get_block_size( &ctx_enc ) 03159 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03160 } 03161 else 03162 { 03163 enclen = length; 03164 } 03165 03166 /* encode length number of bytes from inbuf */ 03167 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03168 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03169 { 03170 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03171 } 03172 else 03173 { 03174 fct_chk( outlen == enclen ); 03175 } 03176 03177 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03178 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03179 { 03180 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03181 } 03182 else 03183 { 03184 fct_chk( outlen == 0 ); 03185 } 03186 03187 03188 /* decode the previously encoded string */ 03189 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03190 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03191 { 03192 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03193 } 03194 else 03195 { 03196 fct_chk( enclen == outlen ); 03197 } 03198 03199 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03200 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03201 { 03202 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03203 } 03204 else 03205 { 03206 fct_chk( outlen == 0 ); 03207 } 03208 03209 03210 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03211 03212 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03213 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03214 FCT_TEST_END(); 03215 #endif /* POLARSSL_DES_C */ 03216 03217 #ifdef POLARSSL_DES_C 03218 03219 FCT_TEST_BGN(des3_encrypt_and_decrypt_2_bytes) 03220 size_t length = 2; 03221 unsigned char key[32]; 03222 unsigned char iv[16]; 03223 03224 const cipher_info_t *cipher_info; 03225 cipher_context_t ctx_dec; 03226 cipher_context_t ctx_enc; 03227 03228 unsigned char inbuf[64]; 03229 unsigned char encbuf[64]; 03230 unsigned char decbuf[64]; 03231 03232 size_t outlen = 0; 03233 size_t enclen = 0; 03234 03235 memset( key, 0, 32 ); 03236 memset( iv , 0, 16 ); 03237 03238 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03239 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03240 03241 memset( inbuf, 5, 64 ); 03242 memset( encbuf, 0, 64 ); 03243 memset( decbuf, 0, 64 ); 03244 03245 /* Check and get info structures */ 03246 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 03247 fct_chk( NULL != cipher_info ); 03248 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 03249 03250 /* Initialise enc and dec contexts */ 03251 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03252 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03253 03254 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 03255 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 03256 03257 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03258 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03259 03260 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03261 { 03262 enclen = cipher_get_block_size( &ctx_enc ) 03263 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03264 } 03265 else 03266 { 03267 enclen = length; 03268 } 03269 03270 /* encode length number of bytes from inbuf */ 03271 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03272 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03273 { 03274 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03275 } 03276 else 03277 { 03278 fct_chk( outlen == enclen ); 03279 } 03280 03281 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03282 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03283 { 03284 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03285 } 03286 else 03287 { 03288 fct_chk( outlen == 0 ); 03289 } 03290 03291 03292 /* decode the previously encoded string */ 03293 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03294 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03295 { 03296 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03297 } 03298 else 03299 { 03300 fct_chk( enclen == outlen ); 03301 } 03302 03303 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03304 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03305 { 03306 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03307 } 03308 else 03309 { 03310 fct_chk( outlen == 0 ); 03311 } 03312 03313 03314 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03315 03316 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03317 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03318 FCT_TEST_END(); 03319 #endif /* POLARSSL_DES_C */ 03320 03321 #ifdef POLARSSL_DES_C 03322 03323 FCT_TEST_BGN(des3_encrypt_and_decrypt_7_bytes) 03324 size_t length = 7; 03325 unsigned char key[32]; 03326 unsigned char iv[16]; 03327 03328 const cipher_info_t *cipher_info; 03329 cipher_context_t ctx_dec; 03330 cipher_context_t ctx_enc; 03331 03332 unsigned char inbuf[64]; 03333 unsigned char encbuf[64]; 03334 unsigned char decbuf[64]; 03335 03336 size_t outlen = 0; 03337 size_t enclen = 0; 03338 03339 memset( key, 0, 32 ); 03340 memset( iv , 0, 16 ); 03341 03342 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03343 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03344 03345 memset( inbuf, 5, 64 ); 03346 memset( encbuf, 0, 64 ); 03347 memset( decbuf, 0, 64 ); 03348 03349 /* Check and get info structures */ 03350 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 03351 fct_chk( NULL != cipher_info ); 03352 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 03353 03354 /* Initialise enc and dec contexts */ 03355 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03356 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03357 03358 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 03359 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 03360 03361 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03362 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03363 03364 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03365 { 03366 enclen = cipher_get_block_size( &ctx_enc ) 03367 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03368 } 03369 else 03370 { 03371 enclen = length; 03372 } 03373 03374 /* encode length number of bytes from inbuf */ 03375 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03376 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03377 { 03378 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03379 } 03380 else 03381 { 03382 fct_chk( outlen == enclen ); 03383 } 03384 03385 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03386 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03387 { 03388 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03389 } 03390 else 03391 { 03392 fct_chk( outlen == 0 ); 03393 } 03394 03395 03396 /* decode the previously encoded string */ 03397 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03398 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03399 { 03400 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03401 } 03402 else 03403 { 03404 fct_chk( enclen == outlen ); 03405 } 03406 03407 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03408 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03409 { 03410 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03411 } 03412 else 03413 { 03414 fct_chk( outlen == 0 ); 03415 } 03416 03417 03418 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03419 03420 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03421 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03422 FCT_TEST_END(); 03423 #endif /* POLARSSL_DES_C */ 03424 03425 #ifdef POLARSSL_DES_C 03426 03427 FCT_TEST_BGN(des3_encrypt_and_decrypt_8_bytes) 03428 size_t length = 8; 03429 unsigned char key[32]; 03430 unsigned char iv[16]; 03431 03432 const cipher_info_t *cipher_info; 03433 cipher_context_t ctx_dec; 03434 cipher_context_t ctx_enc; 03435 03436 unsigned char inbuf[64]; 03437 unsigned char encbuf[64]; 03438 unsigned char decbuf[64]; 03439 03440 size_t outlen = 0; 03441 size_t enclen = 0; 03442 03443 memset( key, 0, 32 ); 03444 memset( iv , 0, 16 ); 03445 03446 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03447 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03448 03449 memset( inbuf, 5, 64 ); 03450 memset( encbuf, 0, 64 ); 03451 memset( decbuf, 0, 64 ); 03452 03453 /* Check and get info structures */ 03454 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 03455 fct_chk( NULL != cipher_info ); 03456 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 03457 03458 /* Initialise enc and dec contexts */ 03459 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03460 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03461 03462 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 03463 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 03464 03465 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03466 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03467 03468 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03469 { 03470 enclen = cipher_get_block_size( &ctx_enc ) 03471 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03472 } 03473 else 03474 { 03475 enclen = length; 03476 } 03477 03478 /* encode length number of bytes from inbuf */ 03479 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03480 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03481 { 03482 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03483 } 03484 else 03485 { 03486 fct_chk( outlen == enclen ); 03487 } 03488 03489 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03490 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03491 { 03492 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03493 } 03494 else 03495 { 03496 fct_chk( outlen == 0 ); 03497 } 03498 03499 03500 /* decode the previously encoded string */ 03501 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03502 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03503 { 03504 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03505 } 03506 else 03507 { 03508 fct_chk( enclen == outlen ); 03509 } 03510 03511 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03512 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03513 { 03514 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03515 } 03516 else 03517 { 03518 fct_chk( outlen == 0 ); 03519 } 03520 03521 03522 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03523 03524 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03525 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03526 FCT_TEST_END(); 03527 #endif /* POLARSSL_DES_C */ 03528 03529 #ifdef POLARSSL_DES_C 03530 03531 FCT_TEST_BGN(des3_encrypt_and_decrypt_9_bytes) 03532 size_t length = 9; 03533 unsigned char key[32]; 03534 unsigned char iv[16]; 03535 03536 const cipher_info_t *cipher_info; 03537 cipher_context_t ctx_dec; 03538 cipher_context_t ctx_enc; 03539 03540 unsigned char inbuf[64]; 03541 unsigned char encbuf[64]; 03542 unsigned char decbuf[64]; 03543 03544 size_t outlen = 0; 03545 size_t enclen = 0; 03546 03547 memset( key, 0, 32 ); 03548 memset( iv , 0, 16 ); 03549 03550 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03551 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03552 03553 memset( inbuf, 5, 64 ); 03554 memset( encbuf, 0, 64 ); 03555 memset( decbuf, 0, 64 ); 03556 03557 /* Check and get info structures */ 03558 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 03559 fct_chk( NULL != cipher_info ); 03560 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 03561 03562 /* Initialise enc and dec contexts */ 03563 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03564 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03565 03566 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 03567 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 03568 03569 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03570 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03571 03572 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03573 { 03574 enclen = cipher_get_block_size( &ctx_enc ) 03575 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03576 } 03577 else 03578 { 03579 enclen = length; 03580 } 03581 03582 /* encode length number of bytes from inbuf */ 03583 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03584 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03585 { 03586 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03587 } 03588 else 03589 { 03590 fct_chk( outlen == enclen ); 03591 } 03592 03593 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03594 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03595 { 03596 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03597 } 03598 else 03599 { 03600 fct_chk( outlen == 0 ); 03601 } 03602 03603 03604 /* decode the previously encoded string */ 03605 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03606 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03607 { 03608 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03609 } 03610 else 03611 { 03612 fct_chk( enclen == outlen ); 03613 } 03614 03615 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03616 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03617 { 03618 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03619 } 03620 else 03621 { 03622 fct_chk( outlen == 0 ); 03623 } 03624 03625 03626 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03627 03628 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03629 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03630 FCT_TEST_END(); 03631 #endif /* POLARSSL_DES_C */ 03632 03633 #ifdef POLARSSL_DES_C 03634 03635 FCT_TEST_BGN(des3_encrypt_and_decrypt_15_bytes) 03636 size_t length = 15; 03637 unsigned char key[32]; 03638 unsigned char iv[16]; 03639 03640 const cipher_info_t *cipher_info; 03641 cipher_context_t ctx_dec; 03642 cipher_context_t ctx_enc; 03643 03644 unsigned char inbuf[64]; 03645 unsigned char encbuf[64]; 03646 unsigned char decbuf[64]; 03647 03648 size_t outlen = 0; 03649 size_t enclen = 0; 03650 03651 memset( key, 0, 32 ); 03652 memset( iv , 0, 16 ); 03653 03654 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03655 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03656 03657 memset( inbuf, 5, 64 ); 03658 memset( encbuf, 0, 64 ); 03659 memset( decbuf, 0, 64 ); 03660 03661 /* Check and get info structures */ 03662 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 03663 fct_chk( NULL != cipher_info ); 03664 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 03665 03666 /* Initialise enc and dec contexts */ 03667 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03668 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03669 03670 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 03671 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 03672 03673 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03674 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03675 03676 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03677 { 03678 enclen = cipher_get_block_size( &ctx_enc ) 03679 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03680 } 03681 else 03682 { 03683 enclen = length; 03684 } 03685 03686 /* encode length number of bytes from inbuf */ 03687 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03688 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03689 { 03690 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03691 } 03692 else 03693 { 03694 fct_chk( outlen == enclen ); 03695 } 03696 03697 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03698 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03699 { 03700 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03701 } 03702 else 03703 { 03704 fct_chk( outlen == 0 ); 03705 } 03706 03707 03708 /* decode the previously encoded string */ 03709 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03710 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03711 { 03712 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03713 } 03714 else 03715 { 03716 fct_chk( enclen == outlen ); 03717 } 03718 03719 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03720 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03721 { 03722 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03723 } 03724 else 03725 { 03726 fct_chk( outlen == 0 ); 03727 } 03728 03729 03730 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03731 03732 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03733 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03734 FCT_TEST_END(); 03735 #endif /* POLARSSL_DES_C */ 03736 03737 #ifdef POLARSSL_DES_C 03738 03739 FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes) 03740 size_t length = 16; 03741 unsigned char key[32]; 03742 unsigned char iv[16]; 03743 03744 const cipher_info_t *cipher_info; 03745 cipher_context_t ctx_dec; 03746 cipher_context_t ctx_enc; 03747 03748 unsigned char inbuf[64]; 03749 unsigned char encbuf[64]; 03750 unsigned char decbuf[64]; 03751 03752 size_t outlen = 0; 03753 size_t enclen = 0; 03754 03755 memset( key, 0, 32 ); 03756 memset( iv , 0, 16 ); 03757 03758 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03759 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03760 03761 memset( inbuf, 5, 64 ); 03762 memset( encbuf, 0, 64 ); 03763 memset( decbuf, 0, 64 ); 03764 03765 /* Check and get info structures */ 03766 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 03767 fct_chk( NULL != cipher_info ); 03768 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 03769 03770 /* Initialise enc and dec contexts */ 03771 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03772 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03773 03774 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 03775 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 03776 03777 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03778 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03779 03780 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03781 { 03782 enclen = cipher_get_block_size( &ctx_enc ) 03783 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03784 } 03785 else 03786 { 03787 enclen = length; 03788 } 03789 03790 /* encode length number of bytes from inbuf */ 03791 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03792 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03793 { 03794 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03795 } 03796 else 03797 { 03798 fct_chk( outlen == enclen ); 03799 } 03800 03801 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03802 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03803 { 03804 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03805 } 03806 else 03807 { 03808 fct_chk( outlen == 0 ); 03809 } 03810 03811 03812 /* decode the previously encoded string */ 03813 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03814 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03815 { 03816 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03817 } 03818 else 03819 { 03820 fct_chk( enclen == outlen ); 03821 } 03822 03823 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03824 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03825 { 03826 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03827 } 03828 else 03829 { 03830 fct_chk( outlen == 0 ); 03831 } 03832 03833 03834 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03835 03836 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03837 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03838 FCT_TEST_END(); 03839 #endif /* POLARSSL_DES_C */ 03840 03841 #ifdef POLARSSL_DES_C 03842 03843 FCT_TEST_BGN(des3_encrypt_and_decrypt_17_bytes) 03844 size_t length = 17; 03845 unsigned char key[32]; 03846 unsigned char iv[16]; 03847 03848 const cipher_info_t *cipher_info; 03849 cipher_context_t ctx_dec; 03850 cipher_context_t ctx_enc; 03851 03852 unsigned char inbuf[64]; 03853 unsigned char encbuf[64]; 03854 unsigned char decbuf[64]; 03855 03856 size_t outlen = 0; 03857 size_t enclen = 0; 03858 03859 memset( key, 0, 32 ); 03860 memset( iv , 0, 16 ); 03861 03862 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03863 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03864 03865 memset( inbuf, 5, 64 ); 03866 memset( encbuf, 0, 64 ); 03867 memset( decbuf, 0, 64 ); 03868 03869 /* Check and get info structures */ 03870 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 03871 fct_chk( NULL != cipher_info ); 03872 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 03873 03874 /* Initialise enc and dec contexts */ 03875 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03876 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03877 03878 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 03879 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 03880 03881 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03882 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03883 03884 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03885 { 03886 enclen = cipher_get_block_size( &ctx_enc ) 03887 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03888 } 03889 else 03890 { 03891 enclen = length; 03892 } 03893 03894 /* encode length number of bytes from inbuf */ 03895 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 03896 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03897 { 03898 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 03899 } 03900 else 03901 { 03902 fct_chk( outlen == enclen ); 03903 } 03904 03905 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 03906 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03907 { 03908 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 03909 } 03910 else 03911 { 03912 fct_chk( outlen == 0 ); 03913 } 03914 03915 03916 /* decode the previously encoded string */ 03917 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 03918 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03919 { 03920 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 03921 } 03922 else 03923 { 03924 fct_chk( enclen == outlen ); 03925 } 03926 03927 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 03928 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03929 { 03930 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 03931 } 03932 else 03933 { 03934 fct_chk( outlen == 0 ); 03935 } 03936 03937 03938 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 03939 03940 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 03941 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 03942 FCT_TEST_END(); 03943 #endif /* POLARSSL_DES_C */ 03944 03945 #ifdef POLARSSL_DES_C 03946 03947 FCT_TEST_BGN(des3_encrypt_and_decrypt_31_bytes) 03948 size_t length = 31; 03949 unsigned char key[32]; 03950 unsigned char iv[16]; 03951 03952 const cipher_info_t *cipher_info; 03953 cipher_context_t ctx_dec; 03954 cipher_context_t ctx_enc; 03955 03956 unsigned char inbuf[64]; 03957 unsigned char encbuf[64]; 03958 unsigned char decbuf[64]; 03959 03960 size_t outlen = 0; 03961 size_t enclen = 0; 03962 03963 memset( key, 0, 32 ); 03964 memset( iv , 0, 16 ); 03965 03966 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 03967 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 03968 03969 memset( inbuf, 5, 64 ); 03970 memset( encbuf, 0, 64 ); 03971 memset( decbuf, 0, 64 ); 03972 03973 /* Check and get info structures */ 03974 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 03975 fct_chk( NULL != cipher_info ); 03976 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 03977 03978 /* Initialise enc and dec contexts */ 03979 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 03980 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 03981 03982 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 03983 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 03984 03985 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 03986 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 03987 03988 if( POLARSSL_MODE_CBC == cipher_info->mode ) 03989 { 03990 enclen = cipher_get_block_size( &ctx_enc ) 03991 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 03992 } 03993 else 03994 { 03995 enclen = length; 03996 } 03997 03998 /* encode length number of bytes from inbuf */ 03999 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04000 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04001 { 04002 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04003 } 04004 else 04005 { 04006 fct_chk( outlen == enclen ); 04007 } 04008 04009 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04010 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04011 { 04012 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04013 } 04014 else 04015 { 04016 fct_chk( outlen == 0 ); 04017 } 04018 04019 04020 /* decode the previously encoded string */ 04021 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04022 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04023 { 04024 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04025 } 04026 else 04027 { 04028 fct_chk( enclen == outlen ); 04029 } 04030 04031 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04032 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04033 { 04034 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04035 } 04036 else 04037 { 04038 fct_chk( outlen == 0 ); 04039 } 04040 04041 04042 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04043 04044 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04045 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04046 FCT_TEST_END(); 04047 #endif /* POLARSSL_DES_C */ 04048 04049 #ifdef POLARSSL_DES_C 04050 04051 FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes) 04052 size_t length = 32; 04053 unsigned char key[32]; 04054 unsigned char iv[16]; 04055 04056 const cipher_info_t *cipher_info; 04057 cipher_context_t ctx_dec; 04058 cipher_context_t ctx_enc; 04059 04060 unsigned char inbuf[64]; 04061 unsigned char encbuf[64]; 04062 unsigned char decbuf[64]; 04063 04064 size_t outlen = 0; 04065 size_t enclen = 0; 04066 04067 memset( key, 0, 32 ); 04068 memset( iv , 0, 16 ); 04069 04070 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04071 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04072 04073 memset( inbuf, 5, 64 ); 04074 memset( encbuf, 0, 64 ); 04075 memset( decbuf, 0, 64 ); 04076 04077 /* Check and get info structures */ 04078 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 04079 fct_chk( NULL != cipher_info ); 04080 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 04081 04082 /* Initialise enc and dec contexts */ 04083 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04084 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04085 04086 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 04087 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 04088 04089 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04090 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04091 04092 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04093 { 04094 enclen = cipher_get_block_size( &ctx_enc ) 04095 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04096 } 04097 else 04098 { 04099 enclen = length; 04100 } 04101 04102 /* encode length number of bytes from inbuf */ 04103 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04104 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04105 { 04106 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04107 } 04108 else 04109 { 04110 fct_chk( outlen == enclen ); 04111 } 04112 04113 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04114 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04115 { 04116 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04117 } 04118 else 04119 { 04120 fct_chk( outlen == 0 ); 04121 } 04122 04123 04124 /* decode the previously encoded string */ 04125 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04126 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04127 { 04128 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04129 } 04130 else 04131 { 04132 fct_chk( enclen == outlen ); 04133 } 04134 04135 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04136 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04137 { 04138 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04139 } 04140 else 04141 { 04142 fct_chk( outlen == 0 ); 04143 } 04144 04145 04146 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04147 04148 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04149 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04150 FCT_TEST_END(); 04151 #endif /* POLARSSL_DES_C */ 04152 04153 #ifdef POLARSSL_DES_C 04154 04155 FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes) 04156 size_t length = 33; 04157 unsigned char key[32]; 04158 unsigned char iv[16]; 04159 04160 const cipher_info_t *cipher_info; 04161 cipher_context_t ctx_dec; 04162 cipher_context_t ctx_enc; 04163 04164 unsigned char inbuf[64]; 04165 unsigned char encbuf[64]; 04166 unsigned char decbuf[64]; 04167 04168 size_t outlen = 0; 04169 size_t enclen = 0; 04170 04171 memset( key, 0, 32 ); 04172 memset( iv , 0, 16 ); 04173 04174 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04175 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04176 04177 memset( inbuf, 5, 64 ); 04178 memset( encbuf, 0, 64 ); 04179 memset( decbuf, 0, 64 ); 04180 04181 /* Check and get info structures */ 04182 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 04183 fct_chk( NULL != cipher_info ); 04184 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 04185 04186 /* Initialise enc and dec contexts */ 04187 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04188 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04189 04190 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 04191 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 04192 04193 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04194 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04195 04196 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04197 { 04198 enclen = cipher_get_block_size( &ctx_enc ) 04199 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04200 } 04201 else 04202 { 04203 enclen = length; 04204 } 04205 04206 /* encode length number of bytes from inbuf */ 04207 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04208 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04209 { 04210 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04211 } 04212 else 04213 { 04214 fct_chk( outlen == enclen ); 04215 } 04216 04217 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04218 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04219 { 04220 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04221 } 04222 else 04223 { 04224 fct_chk( outlen == 0 ); 04225 } 04226 04227 04228 /* decode the previously encoded string */ 04229 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04230 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04231 { 04232 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04233 } 04234 else 04235 { 04236 fct_chk( enclen == outlen ); 04237 } 04238 04239 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04240 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04241 { 04242 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04243 } 04244 else 04245 { 04246 fct_chk( outlen == 0 ); 04247 } 04248 04249 04250 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04251 04252 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04253 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04254 FCT_TEST_END(); 04255 #endif /* POLARSSL_DES_C */ 04256 04257 #ifdef POLARSSL_DES_C 04258 04259 FCT_TEST_BGN(des3_encrypt_and_decrypt_47_bytes) 04260 size_t length = 47; 04261 unsigned char key[32]; 04262 unsigned char iv[16]; 04263 04264 const cipher_info_t *cipher_info; 04265 cipher_context_t ctx_dec; 04266 cipher_context_t ctx_enc; 04267 04268 unsigned char inbuf[64]; 04269 unsigned char encbuf[64]; 04270 unsigned char decbuf[64]; 04271 04272 size_t outlen = 0; 04273 size_t enclen = 0; 04274 04275 memset( key, 0, 32 ); 04276 memset( iv , 0, 16 ); 04277 04278 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04279 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04280 04281 memset( inbuf, 5, 64 ); 04282 memset( encbuf, 0, 64 ); 04283 memset( decbuf, 0, 64 ); 04284 04285 /* Check and get info structures */ 04286 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 04287 fct_chk( NULL != cipher_info ); 04288 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 04289 04290 /* Initialise enc and dec contexts */ 04291 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04292 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04293 04294 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 04295 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 04296 04297 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04298 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04299 04300 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04301 { 04302 enclen = cipher_get_block_size( &ctx_enc ) 04303 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04304 } 04305 else 04306 { 04307 enclen = length; 04308 } 04309 04310 /* encode length number of bytes from inbuf */ 04311 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04312 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04313 { 04314 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04315 } 04316 else 04317 { 04318 fct_chk( outlen == enclen ); 04319 } 04320 04321 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04322 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04323 { 04324 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04325 } 04326 else 04327 { 04328 fct_chk( outlen == 0 ); 04329 } 04330 04331 04332 /* decode the previously encoded string */ 04333 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04334 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04335 { 04336 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04337 } 04338 else 04339 { 04340 fct_chk( enclen == outlen ); 04341 } 04342 04343 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04344 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04345 { 04346 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04347 } 04348 else 04349 { 04350 fct_chk( outlen == 0 ); 04351 } 04352 04353 04354 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04355 04356 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04357 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04358 FCT_TEST_END(); 04359 #endif /* POLARSSL_DES_C */ 04360 04361 #ifdef POLARSSL_DES_C 04362 04363 FCT_TEST_BGN(des3_encrypt_and_decrypt_48_bytes) 04364 size_t length = 48; 04365 unsigned char key[32]; 04366 unsigned char iv[16]; 04367 04368 const cipher_info_t *cipher_info; 04369 cipher_context_t ctx_dec; 04370 cipher_context_t ctx_enc; 04371 04372 unsigned char inbuf[64]; 04373 unsigned char encbuf[64]; 04374 unsigned char decbuf[64]; 04375 04376 size_t outlen = 0; 04377 size_t enclen = 0; 04378 04379 memset( key, 0, 32 ); 04380 memset( iv , 0, 16 ); 04381 04382 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04383 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04384 04385 memset( inbuf, 5, 64 ); 04386 memset( encbuf, 0, 64 ); 04387 memset( decbuf, 0, 64 ); 04388 04389 /* Check and get info structures */ 04390 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 04391 fct_chk( NULL != cipher_info ); 04392 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 04393 04394 /* Initialise enc and dec contexts */ 04395 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04396 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04397 04398 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 04399 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 04400 04401 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04402 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04403 04404 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04405 { 04406 enclen = cipher_get_block_size( &ctx_enc ) 04407 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04408 } 04409 else 04410 { 04411 enclen = length; 04412 } 04413 04414 /* encode length number of bytes from inbuf */ 04415 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04416 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04417 { 04418 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04419 } 04420 else 04421 { 04422 fct_chk( outlen == enclen ); 04423 } 04424 04425 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04426 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04427 { 04428 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04429 } 04430 else 04431 { 04432 fct_chk( outlen == 0 ); 04433 } 04434 04435 04436 /* decode the previously encoded string */ 04437 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04438 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04439 { 04440 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04441 } 04442 else 04443 { 04444 fct_chk( enclen == outlen ); 04445 } 04446 04447 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04448 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04449 { 04450 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04451 } 04452 else 04453 { 04454 fct_chk( outlen == 0 ); 04455 } 04456 04457 04458 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04459 04460 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04461 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04462 FCT_TEST_END(); 04463 #endif /* POLARSSL_DES_C */ 04464 04465 #ifdef POLARSSL_DES_C 04466 04467 FCT_TEST_BGN(des3_encrypt_and_decrypt_49_bytes) 04468 size_t length = 49; 04469 unsigned char key[32]; 04470 unsigned char iv[16]; 04471 04472 const cipher_info_t *cipher_info; 04473 cipher_context_t ctx_dec; 04474 cipher_context_t ctx_enc; 04475 04476 unsigned char inbuf[64]; 04477 unsigned char encbuf[64]; 04478 unsigned char decbuf[64]; 04479 04480 size_t outlen = 0; 04481 size_t enclen = 0; 04482 04483 memset( key, 0, 32 ); 04484 memset( iv , 0, 16 ); 04485 04486 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04487 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04488 04489 memset( inbuf, 5, 64 ); 04490 memset( encbuf, 0, 64 ); 04491 memset( decbuf, 0, 64 ); 04492 04493 /* Check and get info structures */ 04494 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 04495 fct_chk( NULL != cipher_info ); 04496 fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info ); 04497 04498 /* Initialise enc and dec contexts */ 04499 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04500 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04501 04502 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 04503 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 04504 04505 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04506 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04507 04508 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04509 { 04510 enclen = cipher_get_block_size( &ctx_enc ) 04511 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04512 } 04513 else 04514 { 04515 enclen = length; 04516 } 04517 04518 /* encode length number of bytes from inbuf */ 04519 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 04520 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04521 { 04522 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04523 } 04524 else 04525 { 04526 fct_chk( outlen == enclen ); 04527 } 04528 04529 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 04530 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04531 { 04532 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04533 } 04534 else 04535 { 04536 fct_chk( outlen == 0 ); 04537 } 04538 04539 04540 /* decode the previously encoded string */ 04541 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04542 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04543 { 04544 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04545 } 04546 else 04547 { 04548 fct_chk( enclen == outlen ); 04549 } 04550 04551 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04552 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04553 { 04554 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04555 } 04556 else 04557 { 04558 fct_chk( outlen == 0 ); 04559 } 04560 04561 04562 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04563 04564 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04565 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04566 FCT_TEST_END(); 04567 #endif /* POLARSSL_DES_C */ 04568 04569 #ifdef POLARSSL_DES_C 04570 04571 FCT_TEST_BGN(des3_encrypt_and_decrypt_0_bytes_in_multiple_parts) 04572 size_t first_length = 0; 04573 size_t second_length = 0; 04574 size_t length = first_length + second_length; 04575 unsigned char key[32]; 04576 unsigned char iv[16]; 04577 04578 cipher_context_t ctx_dec; 04579 cipher_context_t ctx_enc; 04580 const cipher_info_t *cipher_info; 04581 04582 unsigned char inbuf[64]; 04583 unsigned char encbuf[64]; 04584 unsigned char decbuf[64]; 04585 04586 size_t outlen = 0; 04587 size_t totaloutlen = 0; 04588 size_t enclen = 0; 04589 04590 memset( key, 0, 32 ); 04591 memset( iv , 0, 16 ); 04592 04593 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04594 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04595 04596 memset( inbuf, 5, 64 ); 04597 memset( encbuf, 0, 64 ); 04598 memset( decbuf, 0, 64 ); 04599 04600 /* Initialise enc and dec contexts */ 04601 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 04602 fct_chk( NULL != cipher_info); 04603 04604 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04605 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04606 04607 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 04608 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 04609 04610 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04611 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04612 04613 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04614 { 04615 enclen = cipher_get_block_size(&ctx_enc ) 04616 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04617 } 04618 else 04619 { 04620 enclen = length; 04621 } 04622 04623 /* encode length number of bytes from inbuf */ 04624 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 04625 totaloutlen = outlen; 04626 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 04627 totaloutlen += outlen; 04628 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04629 { 04630 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04631 } 04632 else 04633 { 04634 fct_chk( totaloutlen == enclen ); 04635 } 04636 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 04637 totaloutlen += outlen; 04638 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04639 { 04640 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04641 } 04642 else 04643 { 04644 fct_chk( outlen == 0 ); 04645 } 04646 04647 /* decode the previously encoded string */ 04648 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04649 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04650 { 04651 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04652 } 04653 else 04654 { 04655 fct_chk( enclen == outlen ); 04656 } 04657 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04658 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04659 { 04660 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04661 } 04662 else 04663 { 04664 fct_chk( outlen == 0 ); 04665 } 04666 04667 04668 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04669 04670 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04671 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04672 FCT_TEST_END(); 04673 #endif /* POLARSSL_DES_C */ 04674 04675 #ifdef POLARSSL_DES_C 04676 04677 FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_1) 04678 size_t first_length = 1; 04679 size_t second_length = 0; 04680 size_t length = first_length + second_length; 04681 unsigned char key[32]; 04682 unsigned char iv[16]; 04683 04684 cipher_context_t ctx_dec; 04685 cipher_context_t ctx_enc; 04686 const cipher_info_t *cipher_info; 04687 04688 unsigned char inbuf[64]; 04689 unsigned char encbuf[64]; 04690 unsigned char decbuf[64]; 04691 04692 size_t outlen = 0; 04693 size_t totaloutlen = 0; 04694 size_t enclen = 0; 04695 04696 memset( key, 0, 32 ); 04697 memset( iv , 0, 16 ); 04698 04699 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04700 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04701 04702 memset( inbuf, 5, 64 ); 04703 memset( encbuf, 0, 64 ); 04704 memset( decbuf, 0, 64 ); 04705 04706 /* Initialise enc and dec contexts */ 04707 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 04708 fct_chk( NULL != cipher_info); 04709 04710 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04711 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04712 04713 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 04714 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 04715 04716 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04717 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04718 04719 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04720 { 04721 enclen = cipher_get_block_size(&ctx_enc ) 04722 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04723 } 04724 else 04725 { 04726 enclen = length; 04727 } 04728 04729 /* encode length number of bytes from inbuf */ 04730 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 04731 totaloutlen = outlen; 04732 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 04733 totaloutlen += outlen; 04734 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04735 { 04736 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04737 } 04738 else 04739 { 04740 fct_chk( totaloutlen == enclen ); 04741 } 04742 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 04743 totaloutlen += outlen; 04744 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04745 { 04746 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04747 } 04748 else 04749 { 04750 fct_chk( outlen == 0 ); 04751 } 04752 04753 /* decode the previously encoded string */ 04754 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04755 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04756 { 04757 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04758 } 04759 else 04760 { 04761 fct_chk( enclen == outlen ); 04762 } 04763 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04764 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04765 { 04766 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04767 } 04768 else 04769 { 04770 fct_chk( outlen == 0 ); 04771 } 04772 04773 04774 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04775 04776 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04777 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04778 FCT_TEST_END(); 04779 #endif /* POLARSSL_DES_C */ 04780 04781 #ifdef POLARSSL_DES_C 04782 04783 FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_2) 04784 size_t first_length = 0; 04785 size_t second_length = 1; 04786 size_t length = first_length + second_length; 04787 unsigned char key[32]; 04788 unsigned char iv[16]; 04789 04790 cipher_context_t ctx_dec; 04791 cipher_context_t ctx_enc; 04792 const cipher_info_t *cipher_info; 04793 04794 unsigned char inbuf[64]; 04795 unsigned char encbuf[64]; 04796 unsigned char decbuf[64]; 04797 04798 size_t outlen = 0; 04799 size_t totaloutlen = 0; 04800 size_t enclen = 0; 04801 04802 memset( key, 0, 32 ); 04803 memset( iv , 0, 16 ); 04804 04805 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04806 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04807 04808 memset( inbuf, 5, 64 ); 04809 memset( encbuf, 0, 64 ); 04810 memset( decbuf, 0, 64 ); 04811 04812 /* Initialise enc and dec contexts */ 04813 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 04814 fct_chk( NULL != cipher_info); 04815 04816 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04817 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04818 04819 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 04820 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 04821 04822 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04823 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04824 04825 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04826 { 04827 enclen = cipher_get_block_size(&ctx_enc ) 04828 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04829 } 04830 else 04831 { 04832 enclen = length; 04833 } 04834 04835 /* encode length number of bytes from inbuf */ 04836 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 04837 totaloutlen = outlen; 04838 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 04839 totaloutlen += outlen; 04840 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04841 { 04842 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04843 } 04844 else 04845 { 04846 fct_chk( totaloutlen == enclen ); 04847 } 04848 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 04849 totaloutlen += outlen; 04850 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04851 { 04852 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04853 } 04854 else 04855 { 04856 fct_chk( outlen == 0 ); 04857 } 04858 04859 /* decode the previously encoded string */ 04860 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04861 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04862 { 04863 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04864 } 04865 else 04866 { 04867 fct_chk( enclen == outlen ); 04868 } 04869 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04870 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04871 { 04872 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04873 } 04874 else 04875 { 04876 fct_chk( outlen == 0 ); 04877 } 04878 04879 04880 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04881 04882 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04883 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04884 FCT_TEST_END(); 04885 #endif /* POLARSSL_DES_C */ 04886 04887 #ifdef POLARSSL_DES_C 04888 04889 FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_1) 04890 size_t first_length = 16; 04891 size_t second_length = 0; 04892 size_t length = first_length + second_length; 04893 unsigned char key[32]; 04894 unsigned char iv[16]; 04895 04896 cipher_context_t ctx_dec; 04897 cipher_context_t ctx_enc; 04898 const cipher_info_t *cipher_info; 04899 04900 unsigned char inbuf[64]; 04901 unsigned char encbuf[64]; 04902 unsigned char decbuf[64]; 04903 04904 size_t outlen = 0; 04905 size_t totaloutlen = 0; 04906 size_t enclen = 0; 04907 04908 memset( key, 0, 32 ); 04909 memset( iv , 0, 16 ); 04910 04911 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 04912 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 04913 04914 memset( inbuf, 5, 64 ); 04915 memset( encbuf, 0, 64 ); 04916 memset( decbuf, 0, 64 ); 04917 04918 /* Initialise enc and dec contexts */ 04919 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 04920 fct_chk( NULL != cipher_info); 04921 04922 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 04923 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 04924 04925 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 04926 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 04927 04928 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 04929 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 04930 04931 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04932 { 04933 enclen = cipher_get_block_size(&ctx_enc ) 04934 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 04935 } 04936 else 04937 { 04938 enclen = length; 04939 } 04940 04941 /* encode length number of bytes from inbuf */ 04942 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 04943 totaloutlen = outlen; 04944 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 04945 totaloutlen += outlen; 04946 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04947 { 04948 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 04949 } 04950 else 04951 { 04952 fct_chk( totaloutlen == enclen ); 04953 } 04954 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 04955 totaloutlen += outlen; 04956 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04957 { 04958 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 04959 } 04960 else 04961 { 04962 fct_chk( outlen == 0 ); 04963 } 04964 04965 /* decode the previously encoded string */ 04966 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 04967 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04968 { 04969 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 04970 } 04971 else 04972 { 04973 fct_chk( enclen == outlen ); 04974 } 04975 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 04976 if( POLARSSL_MODE_CBC == cipher_info->mode ) 04977 { 04978 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 04979 } 04980 else 04981 { 04982 fct_chk( outlen == 0 ); 04983 } 04984 04985 04986 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 04987 04988 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 04989 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 04990 FCT_TEST_END(); 04991 #endif /* POLARSSL_DES_C */ 04992 04993 #ifdef POLARSSL_DES_C 04994 04995 FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_2) 04996 size_t first_length = 0; 04997 size_t second_length = 16; 04998 size_t length = first_length + second_length; 04999 unsigned char key[32]; 05000 unsigned char iv[16]; 05001 05002 cipher_context_t ctx_dec; 05003 cipher_context_t ctx_enc; 05004 const cipher_info_t *cipher_info; 05005 05006 unsigned char inbuf[64]; 05007 unsigned char encbuf[64]; 05008 unsigned char decbuf[64]; 05009 05010 size_t outlen = 0; 05011 size_t totaloutlen = 0; 05012 size_t enclen = 0; 05013 05014 memset( key, 0, 32 ); 05015 memset( iv , 0, 16 ); 05016 05017 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05018 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05019 05020 memset( inbuf, 5, 64 ); 05021 memset( encbuf, 0, 64 ); 05022 memset( decbuf, 0, 64 ); 05023 05024 /* Initialise enc and dec contexts */ 05025 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 05026 fct_chk( NULL != cipher_info); 05027 05028 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05029 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05030 05031 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 05032 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 05033 05034 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05035 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05036 05037 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05038 { 05039 enclen = cipher_get_block_size(&ctx_enc ) 05040 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05041 } 05042 else 05043 { 05044 enclen = length; 05045 } 05046 05047 /* encode length number of bytes from inbuf */ 05048 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05049 totaloutlen = outlen; 05050 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05051 totaloutlen += outlen; 05052 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05053 { 05054 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05055 } 05056 else 05057 { 05058 fct_chk( totaloutlen == enclen ); 05059 } 05060 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05061 totaloutlen += outlen; 05062 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05063 { 05064 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05065 } 05066 else 05067 { 05068 fct_chk( outlen == 0 ); 05069 } 05070 05071 /* decode the previously encoded string */ 05072 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05073 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05074 { 05075 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05076 } 05077 else 05078 { 05079 fct_chk( enclen == outlen ); 05080 } 05081 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05082 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05083 { 05084 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05085 } 05086 else 05087 { 05088 fct_chk( outlen == 0 ); 05089 } 05090 05091 05092 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05093 05094 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05095 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05096 FCT_TEST_END(); 05097 #endif /* POLARSSL_DES_C */ 05098 05099 #ifdef POLARSSL_DES_C 05100 05101 FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_3) 05102 size_t first_length = 1; 05103 size_t second_length = 15; 05104 size_t length = first_length + second_length; 05105 unsigned char key[32]; 05106 unsigned char iv[16]; 05107 05108 cipher_context_t ctx_dec; 05109 cipher_context_t ctx_enc; 05110 const cipher_info_t *cipher_info; 05111 05112 unsigned char inbuf[64]; 05113 unsigned char encbuf[64]; 05114 unsigned char decbuf[64]; 05115 05116 size_t outlen = 0; 05117 size_t totaloutlen = 0; 05118 size_t enclen = 0; 05119 05120 memset( key, 0, 32 ); 05121 memset( iv , 0, 16 ); 05122 05123 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05124 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05125 05126 memset( inbuf, 5, 64 ); 05127 memset( encbuf, 0, 64 ); 05128 memset( decbuf, 0, 64 ); 05129 05130 /* Initialise enc and dec contexts */ 05131 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 05132 fct_chk( NULL != cipher_info); 05133 05134 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05135 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05136 05137 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 05138 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 05139 05140 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05141 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05142 05143 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05144 { 05145 enclen = cipher_get_block_size(&ctx_enc ) 05146 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05147 } 05148 else 05149 { 05150 enclen = length; 05151 } 05152 05153 /* encode length number of bytes from inbuf */ 05154 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05155 totaloutlen = outlen; 05156 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05157 totaloutlen += outlen; 05158 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05159 { 05160 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05161 } 05162 else 05163 { 05164 fct_chk( totaloutlen == enclen ); 05165 } 05166 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05167 totaloutlen += outlen; 05168 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05169 { 05170 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05171 } 05172 else 05173 { 05174 fct_chk( outlen == 0 ); 05175 } 05176 05177 /* decode the previously encoded string */ 05178 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05179 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05180 { 05181 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05182 } 05183 else 05184 { 05185 fct_chk( enclen == outlen ); 05186 } 05187 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05188 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05189 { 05190 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05191 } 05192 else 05193 { 05194 fct_chk( outlen == 0 ); 05195 } 05196 05197 05198 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05199 05200 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05201 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05202 FCT_TEST_END(); 05203 #endif /* POLARSSL_DES_C */ 05204 05205 #ifdef POLARSSL_DES_C 05206 05207 FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_4) 05208 size_t first_length = 15; 05209 size_t second_length = 1; 05210 size_t length = first_length + second_length; 05211 unsigned char key[32]; 05212 unsigned char iv[16]; 05213 05214 cipher_context_t ctx_dec; 05215 cipher_context_t ctx_enc; 05216 const cipher_info_t *cipher_info; 05217 05218 unsigned char inbuf[64]; 05219 unsigned char encbuf[64]; 05220 unsigned char decbuf[64]; 05221 05222 size_t outlen = 0; 05223 size_t totaloutlen = 0; 05224 size_t enclen = 0; 05225 05226 memset( key, 0, 32 ); 05227 memset( iv , 0, 16 ); 05228 05229 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05230 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05231 05232 memset( inbuf, 5, 64 ); 05233 memset( encbuf, 0, 64 ); 05234 memset( decbuf, 0, 64 ); 05235 05236 /* Initialise enc and dec contexts */ 05237 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 05238 fct_chk( NULL != cipher_info); 05239 05240 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05241 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05242 05243 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 05244 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 05245 05246 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05247 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05248 05249 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05250 { 05251 enclen = cipher_get_block_size(&ctx_enc ) 05252 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05253 } 05254 else 05255 { 05256 enclen = length; 05257 } 05258 05259 /* encode length number of bytes from inbuf */ 05260 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05261 totaloutlen = outlen; 05262 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05263 totaloutlen += outlen; 05264 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05265 { 05266 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05267 } 05268 else 05269 { 05270 fct_chk( totaloutlen == enclen ); 05271 } 05272 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05273 totaloutlen += outlen; 05274 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05275 { 05276 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05277 } 05278 else 05279 { 05280 fct_chk( outlen == 0 ); 05281 } 05282 05283 /* decode the previously encoded string */ 05284 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05285 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05286 { 05287 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05288 } 05289 else 05290 { 05291 fct_chk( enclen == outlen ); 05292 } 05293 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05294 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05295 { 05296 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05297 } 05298 else 05299 { 05300 fct_chk( outlen == 0 ); 05301 } 05302 05303 05304 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05305 05306 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05307 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05308 FCT_TEST_END(); 05309 #endif /* POLARSSL_DES_C */ 05310 05311 #ifdef POLARSSL_DES_C 05312 05313 FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 05314 size_t first_length = 15; 05315 size_t second_length = 7; 05316 size_t length = first_length + second_length; 05317 unsigned char key[32]; 05318 unsigned char iv[16]; 05319 05320 cipher_context_t ctx_dec; 05321 cipher_context_t ctx_enc; 05322 const cipher_info_t *cipher_info; 05323 05324 unsigned char inbuf[64]; 05325 unsigned char encbuf[64]; 05326 unsigned char decbuf[64]; 05327 05328 size_t outlen = 0; 05329 size_t totaloutlen = 0; 05330 size_t enclen = 0; 05331 05332 memset( key, 0, 32 ); 05333 memset( iv , 0, 16 ); 05334 05335 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05336 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05337 05338 memset( inbuf, 5, 64 ); 05339 memset( encbuf, 0, 64 ); 05340 memset( decbuf, 0, 64 ); 05341 05342 /* Initialise enc and dec contexts */ 05343 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 05344 fct_chk( NULL != cipher_info); 05345 05346 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05347 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05348 05349 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 05350 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 05351 05352 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05353 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05354 05355 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05356 { 05357 enclen = cipher_get_block_size(&ctx_enc ) 05358 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05359 } 05360 else 05361 { 05362 enclen = length; 05363 } 05364 05365 /* encode length number of bytes from inbuf */ 05366 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05367 totaloutlen = outlen; 05368 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05369 totaloutlen += outlen; 05370 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05371 { 05372 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05373 } 05374 else 05375 { 05376 fct_chk( totaloutlen == enclen ); 05377 } 05378 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05379 totaloutlen += outlen; 05380 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05381 { 05382 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05383 } 05384 else 05385 { 05386 fct_chk( outlen == 0 ); 05387 } 05388 05389 /* decode the previously encoded string */ 05390 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05391 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05392 { 05393 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05394 } 05395 else 05396 { 05397 fct_chk( enclen == outlen ); 05398 } 05399 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05400 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05401 { 05402 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05403 } 05404 else 05405 { 05406 fct_chk( outlen == 0 ); 05407 } 05408 05409 05410 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05411 05412 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05413 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05414 FCT_TEST_END(); 05415 #endif /* POLARSSL_DES_C */ 05416 05417 #ifdef POLARSSL_DES_C 05418 05419 FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 05420 size_t first_length = 16; 05421 size_t second_length = 6; 05422 size_t length = first_length + second_length; 05423 unsigned char key[32]; 05424 unsigned char iv[16]; 05425 05426 cipher_context_t ctx_dec; 05427 cipher_context_t ctx_enc; 05428 const cipher_info_t *cipher_info; 05429 05430 unsigned char inbuf[64]; 05431 unsigned char encbuf[64]; 05432 unsigned char decbuf[64]; 05433 05434 size_t outlen = 0; 05435 size_t totaloutlen = 0; 05436 size_t enclen = 0; 05437 05438 memset( key, 0, 32 ); 05439 memset( iv , 0, 16 ); 05440 05441 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05442 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05443 05444 memset( inbuf, 5, 64 ); 05445 memset( encbuf, 0, 64 ); 05446 memset( decbuf, 0, 64 ); 05447 05448 /* Initialise enc and dec contexts */ 05449 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 05450 fct_chk( NULL != cipher_info); 05451 05452 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05453 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05454 05455 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 05456 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 05457 05458 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05459 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05460 05461 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05462 { 05463 enclen = cipher_get_block_size(&ctx_enc ) 05464 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05465 } 05466 else 05467 { 05468 enclen = length; 05469 } 05470 05471 /* encode length number of bytes from inbuf */ 05472 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05473 totaloutlen = outlen; 05474 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05475 totaloutlen += outlen; 05476 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05477 { 05478 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05479 } 05480 else 05481 { 05482 fct_chk( totaloutlen == enclen ); 05483 } 05484 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05485 totaloutlen += outlen; 05486 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05487 { 05488 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05489 } 05490 else 05491 { 05492 fct_chk( outlen == 0 ); 05493 } 05494 05495 /* decode the previously encoded string */ 05496 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05497 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05498 { 05499 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05500 } 05501 else 05502 { 05503 fct_chk( enclen == outlen ); 05504 } 05505 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05506 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05507 { 05508 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05509 } 05510 else 05511 { 05512 fct_chk( outlen == 0 ); 05513 } 05514 05515 05516 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05517 05518 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05519 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05520 FCT_TEST_END(); 05521 #endif /* POLARSSL_DES_C */ 05522 05523 #ifdef POLARSSL_DES_C 05524 05525 FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 05526 size_t first_length = 17; 05527 size_t second_length = 6; 05528 size_t length = first_length + second_length; 05529 unsigned char key[32]; 05530 unsigned char iv[16]; 05531 05532 cipher_context_t ctx_dec; 05533 cipher_context_t ctx_enc; 05534 const cipher_info_t *cipher_info; 05535 05536 unsigned char inbuf[64]; 05537 unsigned char encbuf[64]; 05538 unsigned char decbuf[64]; 05539 05540 size_t outlen = 0; 05541 size_t totaloutlen = 0; 05542 size_t enclen = 0; 05543 05544 memset( key, 0, 32 ); 05545 memset( iv , 0, 16 ); 05546 05547 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05548 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05549 05550 memset( inbuf, 5, 64 ); 05551 memset( encbuf, 0, 64 ); 05552 memset( decbuf, 0, 64 ); 05553 05554 /* Initialise enc and dec contexts */ 05555 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 05556 fct_chk( NULL != cipher_info); 05557 05558 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05559 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05560 05561 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 05562 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 05563 05564 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05565 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05566 05567 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05568 { 05569 enclen = cipher_get_block_size(&ctx_enc ) 05570 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05571 } 05572 else 05573 { 05574 enclen = length; 05575 } 05576 05577 /* encode length number of bytes from inbuf */ 05578 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05579 totaloutlen = outlen; 05580 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05581 totaloutlen += outlen; 05582 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05583 { 05584 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05585 } 05586 else 05587 { 05588 fct_chk( totaloutlen == enclen ); 05589 } 05590 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05591 totaloutlen += outlen; 05592 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05593 { 05594 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05595 } 05596 else 05597 { 05598 fct_chk( outlen == 0 ); 05599 } 05600 05601 /* decode the previously encoded string */ 05602 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05603 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05604 { 05605 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05606 } 05607 else 05608 { 05609 fct_chk( enclen == outlen ); 05610 } 05611 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05612 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05613 { 05614 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05615 } 05616 else 05617 { 05618 fct_chk( outlen == 0 ); 05619 } 05620 05621 05622 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05623 05624 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05625 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05626 FCT_TEST_END(); 05627 #endif /* POLARSSL_DES_C */ 05628 05629 #ifdef POLARSSL_DES_C 05630 05631 FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes_in_multiple_parts_1) 05632 size_t first_length = 16; 05633 size_t second_length = 16; 05634 size_t length = first_length + second_length; 05635 unsigned char key[32]; 05636 unsigned char iv[16]; 05637 05638 cipher_context_t ctx_dec; 05639 cipher_context_t ctx_enc; 05640 const cipher_info_t *cipher_info; 05641 05642 unsigned char inbuf[64]; 05643 unsigned char encbuf[64]; 05644 unsigned char decbuf[64]; 05645 05646 size_t outlen = 0; 05647 size_t totaloutlen = 0; 05648 size_t enclen = 0; 05649 05650 memset( key, 0, 32 ); 05651 memset( iv , 0, 16 ); 05652 05653 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05654 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05655 05656 memset( inbuf, 5, 64 ); 05657 memset( encbuf, 0, 64 ); 05658 memset( decbuf, 0, 64 ); 05659 05660 /* Initialise enc and dec contexts */ 05661 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); 05662 fct_chk( NULL != cipher_info); 05663 05664 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05665 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05666 05667 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) ); 05668 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) ); 05669 05670 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05671 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05672 05673 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05674 { 05675 enclen = cipher_get_block_size(&ctx_enc ) 05676 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05677 } 05678 else 05679 { 05680 enclen = length; 05681 } 05682 05683 /* encode length number of bytes from inbuf */ 05684 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 05685 totaloutlen = outlen; 05686 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 05687 totaloutlen += outlen; 05688 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05689 { 05690 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05691 } 05692 else 05693 { 05694 fct_chk( totaloutlen == enclen ); 05695 } 05696 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 05697 totaloutlen += outlen; 05698 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05699 { 05700 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05701 } 05702 else 05703 { 05704 fct_chk( outlen == 0 ); 05705 } 05706 05707 /* decode the previously encoded string */ 05708 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05709 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05710 { 05711 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05712 } 05713 else 05714 { 05715 fct_chk( enclen == outlen ); 05716 } 05717 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05718 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05719 { 05720 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05721 } 05722 else 05723 { 05724 fct_chk( outlen == 0 ); 05725 } 05726 05727 05728 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05729 05730 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05731 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05732 FCT_TEST_END(); 05733 #endif /* POLARSSL_DES_C */ 05734 05735 #ifdef POLARSSL_DES_C 05736 05737 FCT_TEST_BGN(des3_encrypt_and_decrypt_0_bytes) 05738 size_t length = 0; 05739 unsigned char key[32]; 05740 unsigned char iv[16]; 05741 05742 const cipher_info_t *cipher_info; 05743 cipher_context_t ctx_dec; 05744 cipher_context_t ctx_enc; 05745 05746 unsigned char inbuf[64]; 05747 unsigned char encbuf[64]; 05748 unsigned char decbuf[64]; 05749 05750 size_t outlen = 0; 05751 size_t enclen = 0; 05752 05753 memset( key, 0, 32 ); 05754 memset( iv , 0, 16 ); 05755 05756 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05757 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05758 05759 memset( inbuf, 5, 64 ); 05760 memset( encbuf, 0, 64 ); 05761 memset( decbuf, 0, 64 ); 05762 05763 /* Check and get info structures */ 05764 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 05765 fct_chk( NULL != cipher_info ); 05766 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 05767 05768 /* Initialise enc and dec contexts */ 05769 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05770 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05771 05772 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 05773 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 05774 05775 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05776 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05777 05778 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05779 { 05780 enclen = cipher_get_block_size( &ctx_enc ) 05781 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05782 } 05783 else 05784 { 05785 enclen = length; 05786 } 05787 05788 /* encode length number of bytes from inbuf */ 05789 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 05790 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05791 { 05792 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05793 } 05794 else 05795 { 05796 fct_chk( outlen == enclen ); 05797 } 05798 05799 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 05800 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05801 { 05802 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05803 } 05804 else 05805 { 05806 fct_chk( outlen == 0 ); 05807 } 05808 05809 05810 /* decode the previously encoded string */ 05811 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05812 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05813 { 05814 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05815 } 05816 else 05817 { 05818 fct_chk( enclen == outlen ); 05819 } 05820 05821 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05822 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05823 { 05824 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05825 } 05826 else 05827 { 05828 fct_chk( outlen == 0 ); 05829 } 05830 05831 05832 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05833 05834 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05835 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05836 FCT_TEST_END(); 05837 #endif /* POLARSSL_DES_C */ 05838 05839 #ifdef POLARSSL_DES_C 05840 05841 FCT_TEST_BGN(des3_encrypt_and_decrypt_1_byte) 05842 size_t length = 1; 05843 unsigned char key[32]; 05844 unsigned char iv[16]; 05845 05846 const cipher_info_t *cipher_info; 05847 cipher_context_t ctx_dec; 05848 cipher_context_t ctx_enc; 05849 05850 unsigned char inbuf[64]; 05851 unsigned char encbuf[64]; 05852 unsigned char decbuf[64]; 05853 05854 size_t outlen = 0; 05855 size_t enclen = 0; 05856 05857 memset( key, 0, 32 ); 05858 memset( iv , 0, 16 ); 05859 05860 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05861 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05862 05863 memset( inbuf, 5, 64 ); 05864 memset( encbuf, 0, 64 ); 05865 memset( decbuf, 0, 64 ); 05866 05867 /* Check and get info structures */ 05868 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 05869 fct_chk( NULL != cipher_info ); 05870 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 05871 05872 /* Initialise enc and dec contexts */ 05873 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05874 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05875 05876 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 05877 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 05878 05879 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05880 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05881 05882 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05883 { 05884 enclen = cipher_get_block_size( &ctx_enc ) 05885 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05886 } 05887 else 05888 { 05889 enclen = length; 05890 } 05891 05892 /* encode length number of bytes from inbuf */ 05893 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 05894 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05895 { 05896 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 05897 } 05898 else 05899 { 05900 fct_chk( outlen == enclen ); 05901 } 05902 05903 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 05904 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05905 { 05906 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 05907 } 05908 else 05909 { 05910 fct_chk( outlen == 0 ); 05911 } 05912 05913 05914 /* decode the previously encoded string */ 05915 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 05916 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05917 { 05918 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 05919 } 05920 else 05921 { 05922 fct_chk( enclen == outlen ); 05923 } 05924 05925 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 05926 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05927 { 05928 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 05929 } 05930 else 05931 { 05932 fct_chk( outlen == 0 ); 05933 } 05934 05935 05936 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 05937 05938 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 05939 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 05940 FCT_TEST_END(); 05941 #endif /* POLARSSL_DES_C */ 05942 05943 #ifdef POLARSSL_DES_C 05944 05945 FCT_TEST_BGN(des3_encrypt_and_decrypt_2_bytes) 05946 size_t length = 2; 05947 unsigned char key[32]; 05948 unsigned char iv[16]; 05949 05950 const cipher_info_t *cipher_info; 05951 cipher_context_t ctx_dec; 05952 cipher_context_t ctx_enc; 05953 05954 unsigned char inbuf[64]; 05955 unsigned char encbuf[64]; 05956 unsigned char decbuf[64]; 05957 05958 size_t outlen = 0; 05959 size_t enclen = 0; 05960 05961 memset( key, 0, 32 ); 05962 memset( iv , 0, 16 ); 05963 05964 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 05965 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 05966 05967 memset( inbuf, 5, 64 ); 05968 memset( encbuf, 0, 64 ); 05969 memset( decbuf, 0, 64 ); 05970 05971 /* Check and get info structures */ 05972 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 05973 fct_chk( NULL != cipher_info ); 05974 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 05975 05976 /* Initialise enc and dec contexts */ 05977 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 05978 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 05979 05980 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 05981 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 05982 05983 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 05984 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 05985 05986 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05987 { 05988 enclen = cipher_get_block_size( &ctx_enc ) 05989 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 05990 } 05991 else 05992 { 05993 enclen = length; 05994 } 05995 05996 /* encode length number of bytes from inbuf */ 05997 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 05998 if( POLARSSL_MODE_CBC == cipher_info->mode ) 05999 { 06000 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06001 } 06002 else 06003 { 06004 fct_chk( outlen == enclen ); 06005 } 06006 06007 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06008 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06009 { 06010 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06011 } 06012 else 06013 { 06014 fct_chk( outlen == 0 ); 06015 } 06016 06017 06018 /* decode the previously encoded string */ 06019 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06020 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06021 { 06022 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06023 } 06024 else 06025 { 06026 fct_chk( enclen == outlen ); 06027 } 06028 06029 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06030 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06031 { 06032 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06033 } 06034 else 06035 { 06036 fct_chk( outlen == 0 ); 06037 } 06038 06039 06040 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06041 06042 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06043 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06044 FCT_TEST_END(); 06045 #endif /* POLARSSL_DES_C */ 06046 06047 #ifdef POLARSSL_DES_C 06048 06049 FCT_TEST_BGN(des3_encrypt_and_decrypt_7_bytes) 06050 size_t length = 7; 06051 unsigned char key[32]; 06052 unsigned char iv[16]; 06053 06054 const cipher_info_t *cipher_info; 06055 cipher_context_t ctx_dec; 06056 cipher_context_t ctx_enc; 06057 06058 unsigned char inbuf[64]; 06059 unsigned char encbuf[64]; 06060 unsigned char decbuf[64]; 06061 06062 size_t outlen = 0; 06063 size_t enclen = 0; 06064 06065 memset( key, 0, 32 ); 06066 memset( iv , 0, 16 ); 06067 06068 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06069 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06070 06071 memset( inbuf, 5, 64 ); 06072 memset( encbuf, 0, 64 ); 06073 memset( decbuf, 0, 64 ); 06074 06075 /* Check and get info structures */ 06076 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 06077 fct_chk( NULL != cipher_info ); 06078 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 06079 06080 /* Initialise enc and dec contexts */ 06081 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06082 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06083 06084 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 06085 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 06086 06087 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06088 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06089 06090 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06091 { 06092 enclen = cipher_get_block_size( &ctx_enc ) 06093 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06094 } 06095 else 06096 { 06097 enclen = length; 06098 } 06099 06100 /* encode length number of bytes from inbuf */ 06101 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06102 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06103 { 06104 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06105 } 06106 else 06107 { 06108 fct_chk( outlen == enclen ); 06109 } 06110 06111 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06112 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06113 { 06114 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06115 } 06116 else 06117 { 06118 fct_chk( outlen == 0 ); 06119 } 06120 06121 06122 /* decode the previously encoded string */ 06123 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06124 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06125 { 06126 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06127 } 06128 else 06129 { 06130 fct_chk( enclen == outlen ); 06131 } 06132 06133 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06134 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06135 { 06136 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06137 } 06138 else 06139 { 06140 fct_chk( outlen == 0 ); 06141 } 06142 06143 06144 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06145 06146 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06147 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06148 FCT_TEST_END(); 06149 #endif /* POLARSSL_DES_C */ 06150 06151 #ifdef POLARSSL_DES_C 06152 06153 FCT_TEST_BGN(des3_encrypt_and_decrypt_8_bytes) 06154 size_t length = 8; 06155 unsigned char key[32]; 06156 unsigned char iv[16]; 06157 06158 const cipher_info_t *cipher_info; 06159 cipher_context_t ctx_dec; 06160 cipher_context_t ctx_enc; 06161 06162 unsigned char inbuf[64]; 06163 unsigned char encbuf[64]; 06164 unsigned char decbuf[64]; 06165 06166 size_t outlen = 0; 06167 size_t enclen = 0; 06168 06169 memset( key, 0, 32 ); 06170 memset( iv , 0, 16 ); 06171 06172 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06173 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06174 06175 memset( inbuf, 5, 64 ); 06176 memset( encbuf, 0, 64 ); 06177 memset( decbuf, 0, 64 ); 06178 06179 /* Check and get info structures */ 06180 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 06181 fct_chk( NULL != cipher_info ); 06182 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 06183 06184 /* Initialise enc and dec contexts */ 06185 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06186 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06187 06188 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 06189 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 06190 06191 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06192 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06193 06194 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06195 { 06196 enclen = cipher_get_block_size( &ctx_enc ) 06197 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06198 } 06199 else 06200 { 06201 enclen = length; 06202 } 06203 06204 /* encode length number of bytes from inbuf */ 06205 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06206 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06207 { 06208 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06209 } 06210 else 06211 { 06212 fct_chk( outlen == enclen ); 06213 } 06214 06215 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06216 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06217 { 06218 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06219 } 06220 else 06221 { 06222 fct_chk( outlen == 0 ); 06223 } 06224 06225 06226 /* decode the previously encoded string */ 06227 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06228 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06229 { 06230 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06231 } 06232 else 06233 { 06234 fct_chk( enclen == outlen ); 06235 } 06236 06237 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06238 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06239 { 06240 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06241 } 06242 else 06243 { 06244 fct_chk( outlen == 0 ); 06245 } 06246 06247 06248 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06249 06250 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06251 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06252 FCT_TEST_END(); 06253 #endif /* POLARSSL_DES_C */ 06254 06255 #ifdef POLARSSL_DES_C 06256 06257 FCT_TEST_BGN(des3_encrypt_and_decrypt_9_bytes) 06258 size_t length = 9; 06259 unsigned char key[32]; 06260 unsigned char iv[16]; 06261 06262 const cipher_info_t *cipher_info; 06263 cipher_context_t ctx_dec; 06264 cipher_context_t ctx_enc; 06265 06266 unsigned char inbuf[64]; 06267 unsigned char encbuf[64]; 06268 unsigned char decbuf[64]; 06269 06270 size_t outlen = 0; 06271 size_t enclen = 0; 06272 06273 memset( key, 0, 32 ); 06274 memset( iv , 0, 16 ); 06275 06276 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06277 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06278 06279 memset( inbuf, 5, 64 ); 06280 memset( encbuf, 0, 64 ); 06281 memset( decbuf, 0, 64 ); 06282 06283 /* Check and get info structures */ 06284 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 06285 fct_chk( NULL != cipher_info ); 06286 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 06287 06288 /* Initialise enc and dec contexts */ 06289 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06290 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06291 06292 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 06293 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 06294 06295 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06296 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06297 06298 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06299 { 06300 enclen = cipher_get_block_size( &ctx_enc ) 06301 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06302 } 06303 else 06304 { 06305 enclen = length; 06306 } 06307 06308 /* encode length number of bytes from inbuf */ 06309 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06310 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06311 { 06312 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06313 } 06314 else 06315 { 06316 fct_chk( outlen == enclen ); 06317 } 06318 06319 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06320 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06321 { 06322 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06323 } 06324 else 06325 { 06326 fct_chk( outlen == 0 ); 06327 } 06328 06329 06330 /* decode the previously encoded string */ 06331 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06332 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06333 { 06334 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06335 } 06336 else 06337 { 06338 fct_chk( enclen == outlen ); 06339 } 06340 06341 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06342 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06343 { 06344 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06345 } 06346 else 06347 { 06348 fct_chk( outlen == 0 ); 06349 } 06350 06351 06352 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06353 06354 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06355 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06356 FCT_TEST_END(); 06357 #endif /* POLARSSL_DES_C */ 06358 06359 #ifdef POLARSSL_DES_C 06360 06361 FCT_TEST_BGN(des3_encrypt_and_decrypt_15_bytes) 06362 size_t length = 15; 06363 unsigned char key[32]; 06364 unsigned char iv[16]; 06365 06366 const cipher_info_t *cipher_info; 06367 cipher_context_t ctx_dec; 06368 cipher_context_t ctx_enc; 06369 06370 unsigned char inbuf[64]; 06371 unsigned char encbuf[64]; 06372 unsigned char decbuf[64]; 06373 06374 size_t outlen = 0; 06375 size_t enclen = 0; 06376 06377 memset( key, 0, 32 ); 06378 memset( iv , 0, 16 ); 06379 06380 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06381 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06382 06383 memset( inbuf, 5, 64 ); 06384 memset( encbuf, 0, 64 ); 06385 memset( decbuf, 0, 64 ); 06386 06387 /* Check and get info structures */ 06388 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 06389 fct_chk( NULL != cipher_info ); 06390 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 06391 06392 /* Initialise enc and dec contexts */ 06393 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06394 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06395 06396 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 06397 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 06398 06399 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06400 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06401 06402 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06403 { 06404 enclen = cipher_get_block_size( &ctx_enc ) 06405 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06406 } 06407 else 06408 { 06409 enclen = length; 06410 } 06411 06412 /* encode length number of bytes from inbuf */ 06413 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06414 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06415 { 06416 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06417 } 06418 else 06419 { 06420 fct_chk( outlen == enclen ); 06421 } 06422 06423 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06424 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06425 { 06426 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06427 } 06428 else 06429 { 06430 fct_chk( outlen == 0 ); 06431 } 06432 06433 06434 /* decode the previously encoded string */ 06435 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06436 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06437 { 06438 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06439 } 06440 else 06441 { 06442 fct_chk( enclen == outlen ); 06443 } 06444 06445 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06446 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06447 { 06448 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06449 } 06450 else 06451 { 06452 fct_chk( outlen == 0 ); 06453 } 06454 06455 06456 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06457 06458 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06459 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06460 FCT_TEST_END(); 06461 #endif /* POLARSSL_DES_C */ 06462 06463 #ifdef POLARSSL_DES_C 06464 06465 FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes) 06466 size_t length = 16; 06467 unsigned char key[32]; 06468 unsigned char iv[16]; 06469 06470 const cipher_info_t *cipher_info; 06471 cipher_context_t ctx_dec; 06472 cipher_context_t ctx_enc; 06473 06474 unsigned char inbuf[64]; 06475 unsigned char encbuf[64]; 06476 unsigned char decbuf[64]; 06477 06478 size_t outlen = 0; 06479 size_t enclen = 0; 06480 06481 memset( key, 0, 32 ); 06482 memset( iv , 0, 16 ); 06483 06484 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06485 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06486 06487 memset( inbuf, 5, 64 ); 06488 memset( encbuf, 0, 64 ); 06489 memset( decbuf, 0, 64 ); 06490 06491 /* Check and get info structures */ 06492 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 06493 fct_chk( NULL != cipher_info ); 06494 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 06495 06496 /* Initialise enc and dec contexts */ 06497 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06498 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06499 06500 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 06501 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 06502 06503 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06504 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06505 06506 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06507 { 06508 enclen = cipher_get_block_size( &ctx_enc ) 06509 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06510 } 06511 else 06512 { 06513 enclen = length; 06514 } 06515 06516 /* encode length number of bytes from inbuf */ 06517 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06518 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06519 { 06520 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06521 } 06522 else 06523 { 06524 fct_chk( outlen == enclen ); 06525 } 06526 06527 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06528 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06529 { 06530 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06531 } 06532 else 06533 { 06534 fct_chk( outlen == 0 ); 06535 } 06536 06537 06538 /* decode the previously encoded string */ 06539 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06540 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06541 { 06542 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06543 } 06544 else 06545 { 06546 fct_chk( enclen == outlen ); 06547 } 06548 06549 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06550 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06551 { 06552 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06553 } 06554 else 06555 { 06556 fct_chk( outlen == 0 ); 06557 } 06558 06559 06560 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06561 06562 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06563 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06564 FCT_TEST_END(); 06565 #endif /* POLARSSL_DES_C */ 06566 06567 #ifdef POLARSSL_DES_C 06568 06569 FCT_TEST_BGN(des3_encrypt_and_decrypt_17_bytes) 06570 size_t length = 17; 06571 unsigned char key[32]; 06572 unsigned char iv[16]; 06573 06574 const cipher_info_t *cipher_info; 06575 cipher_context_t ctx_dec; 06576 cipher_context_t ctx_enc; 06577 06578 unsigned char inbuf[64]; 06579 unsigned char encbuf[64]; 06580 unsigned char decbuf[64]; 06581 06582 size_t outlen = 0; 06583 size_t enclen = 0; 06584 06585 memset( key, 0, 32 ); 06586 memset( iv , 0, 16 ); 06587 06588 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06589 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06590 06591 memset( inbuf, 5, 64 ); 06592 memset( encbuf, 0, 64 ); 06593 memset( decbuf, 0, 64 ); 06594 06595 /* Check and get info structures */ 06596 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 06597 fct_chk( NULL != cipher_info ); 06598 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 06599 06600 /* Initialise enc and dec contexts */ 06601 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06602 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06603 06604 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 06605 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 06606 06607 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06608 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06609 06610 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06611 { 06612 enclen = cipher_get_block_size( &ctx_enc ) 06613 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06614 } 06615 else 06616 { 06617 enclen = length; 06618 } 06619 06620 /* encode length number of bytes from inbuf */ 06621 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06622 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06623 { 06624 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06625 } 06626 else 06627 { 06628 fct_chk( outlen == enclen ); 06629 } 06630 06631 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06632 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06633 { 06634 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06635 } 06636 else 06637 { 06638 fct_chk( outlen == 0 ); 06639 } 06640 06641 06642 /* decode the previously encoded string */ 06643 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06644 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06645 { 06646 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06647 } 06648 else 06649 { 06650 fct_chk( enclen == outlen ); 06651 } 06652 06653 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06654 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06655 { 06656 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06657 } 06658 else 06659 { 06660 fct_chk( outlen == 0 ); 06661 } 06662 06663 06664 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06665 06666 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06667 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06668 FCT_TEST_END(); 06669 #endif /* POLARSSL_DES_C */ 06670 06671 #ifdef POLARSSL_DES_C 06672 06673 FCT_TEST_BGN(des3_encrypt_and_decrypt_31_bytes) 06674 size_t length = 31; 06675 unsigned char key[32]; 06676 unsigned char iv[16]; 06677 06678 const cipher_info_t *cipher_info; 06679 cipher_context_t ctx_dec; 06680 cipher_context_t ctx_enc; 06681 06682 unsigned char inbuf[64]; 06683 unsigned char encbuf[64]; 06684 unsigned char decbuf[64]; 06685 06686 size_t outlen = 0; 06687 size_t enclen = 0; 06688 06689 memset( key, 0, 32 ); 06690 memset( iv , 0, 16 ); 06691 06692 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06693 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06694 06695 memset( inbuf, 5, 64 ); 06696 memset( encbuf, 0, 64 ); 06697 memset( decbuf, 0, 64 ); 06698 06699 /* Check and get info structures */ 06700 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 06701 fct_chk( NULL != cipher_info ); 06702 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 06703 06704 /* Initialise enc and dec contexts */ 06705 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06706 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06707 06708 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 06709 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 06710 06711 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06712 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06713 06714 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06715 { 06716 enclen = cipher_get_block_size( &ctx_enc ) 06717 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06718 } 06719 else 06720 { 06721 enclen = length; 06722 } 06723 06724 /* encode length number of bytes from inbuf */ 06725 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06726 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06727 { 06728 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06729 } 06730 else 06731 { 06732 fct_chk( outlen == enclen ); 06733 } 06734 06735 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06736 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06737 { 06738 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06739 } 06740 else 06741 { 06742 fct_chk( outlen == 0 ); 06743 } 06744 06745 06746 /* decode the previously encoded string */ 06747 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06748 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06749 { 06750 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06751 } 06752 else 06753 { 06754 fct_chk( enclen == outlen ); 06755 } 06756 06757 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06758 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06759 { 06760 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06761 } 06762 else 06763 { 06764 fct_chk( outlen == 0 ); 06765 } 06766 06767 06768 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06769 06770 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06771 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06772 FCT_TEST_END(); 06773 #endif /* POLARSSL_DES_C */ 06774 06775 #ifdef POLARSSL_DES_C 06776 06777 FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes) 06778 size_t length = 32; 06779 unsigned char key[32]; 06780 unsigned char iv[16]; 06781 06782 const cipher_info_t *cipher_info; 06783 cipher_context_t ctx_dec; 06784 cipher_context_t ctx_enc; 06785 06786 unsigned char inbuf[64]; 06787 unsigned char encbuf[64]; 06788 unsigned char decbuf[64]; 06789 06790 size_t outlen = 0; 06791 size_t enclen = 0; 06792 06793 memset( key, 0, 32 ); 06794 memset( iv , 0, 16 ); 06795 06796 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06797 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06798 06799 memset( inbuf, 5, 64 ); 06800 memset( encbuf, 0, 64 ); 06801 memset( decbuf, 0, 64 ); 06802 06803 /* Check and get info structures */ 06804 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 06805 fct_chk( NULL != cipher_info ); 06806 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 06807 06808 /* Initialise enc and dec contexts */ 06809 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06810 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06811 06812 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 06813 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 06814 06815 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06816 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06817 06818 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06819 { 06820 enclen = cipher_get_block_size( &ctx_enc ) 06821 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06822 } 06823 else 06824 { 06825 enclen = length; 06826 } 06827 06828 /* encode length number of bytes from inbuf */ 06829 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06830 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06831 { 06832 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06833 } 06834 else 06835 { 06836 fct_chk( outlen == enclen ); 06837 } 06838 06839 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06840 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06841 { 06842 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06843 } 06844 else 06845 { 06846 fct_chk( outlen == 0 ); 06847 } 06848 06849 06850 /* decode the previously encoded string */ 06851 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06852 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06853 { 06854 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06855 } 06856 else 06857 { 06858 fct_chk( enclen == outlen ); 06859 } 06860 06861 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06862 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06863 { 06864 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06865 } 06866 else 06867 { 06868 fct_chk( outlen == 0 ); 06869 } 06870 06871 06872 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06873 06874 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06875 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06876 FCT_TEST_END(); 06877 #endif /* POLARSSL_DES_C */ 06878 06879 #ifdef POLARSSL_DES_C 06880 06881 FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes) 06882 size_t length = 33; 06883 unsigned char key[32]; 06884 unsigned char iv[16]; 06885 06886 const cipher_info_t *cipher_info; 06887 cipher_context_t ctx_dec; 06888 cipher_context_t ctx_enc; 06889 06890 unsigned char inbuf[64]; 06891 unsigned char encbuf[64]; 06892 unsigned char decbuf[64]; 06893 06894 size_t outlen = 0; 06895 size_t enclen = 0; 06896 06897 memset( key, 0, 32 ); 06898 memset( iv , 0, 16 ); 06899 06900 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 06901 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 06902 06903 memset( inbuf, 5, 64 ); 06904 memset( encbuf, 0, 64 ); 06905 memset( decbuf, 0, 64 ); 06906 06907 /* Check and get info structures */ 06908 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 06909 fct_chk( NULL != cipher_info ); 06910 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 06911 06912 /* Initialise enc and dec contexts */ 06913 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 06914 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 06915 06916 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 06917 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 06918 06919 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 06920 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 06921 06922 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06923 { 06924 enclen = cipher_get_block_size( &ctx_enc ) 06925 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 06926 } 06927 else 06928 { 06929 enclen = length; 06930 } 06931 06932 /* encode length number of bytes from inbuf */ 06933 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 06934 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06935 { 06936 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 06937 } 06938 else 06939 { 06940 fct_chk( outlen == enclen ); 06941 } 06942 06943 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 06944 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06945 { 06946 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 06947 } 06948 else 06949 { 06950 fct_chk( outlen == 0 ); 06951 } 06952 06953 06954 /* decode the previously encoded string */ 06955 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 06956 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06957 { 06958 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 06959 } 06960 else 06961 { 06962 fct_chk( enclen == outlen ); 06963 } 06964 06965 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 06966 if( POLARSSL_MODE_CBC == cipher_info->mode ) 06967 { 06968 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 06969 } 06970 else 06971 { 06972 fct_chk( outlen == 0 ); 06973 } 06974 06975 06976 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 06977 06978 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 06979 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 06980 FCT_TEST_END(); 06981 #endif /* POLARSSL_DES_C */ 06982 06983 #ifdef POLARSSL_DES_C 06984 06985 FCT_TEST_BGN(des3_encrypt_and_decrypt_47_bytes) 06986 size_t length = 47; 06987 unsigned char key[32]; 06988 unsigned char iv[16]; 06989 06990 const cipher_info_t *cipher_info; 06991 cipher_context_t ctx_dec; 06992 cipher_context_t ctx_enc; 06993 06994 unsigned char inbuf[64]; 06995 unsigned char encbuf[64]; 06996 unsigned char decbuf[64]; 06997 06998 size_t outlen = 0; 06999 size_t enclen = 0; 07000 07001 memset( key, 0, 32 ); 07002 memset( iv , 0, 16 ); 07003 07004 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07005 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07006 07007 memset( inbuf, 5, 64 ); 07008 memset( encbuf, 0, 64 ); 07009 memset( decbuf, 0, 64 ); 07010 07011 /* Check and get info structures */ 07012 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 07013 fct_chk( NULL != cipher_info ); 07014 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 07015 07016 /* Initialise enc and dec contexts */ 07017 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07018 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07019 07020 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 07021 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 07022 07023 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07024 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07025 07026 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07027 { 07028 enclen = cipher_get_block_size( &ctx_enc ) 07029 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07030 } 07031 else 07032 { 07033 enclen = length; 07034 } 07035 07036 /* encode length number of bytes from inbuf */ 07037 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 07038 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07039 { 07040 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07041 } 07042 else 07043 { 07044 fct_chk( outlen == enclen ); 07045 } 07046 07047 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 07048 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07049 { 07050 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07051 } 07052 else 07053 { 07054 fct_chk( outlen == 0 ); 07055 } 07056 07057 07058 /* decode the previously encoded string */ 07059 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07060 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07061 { 07062 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07063 } 07064 else 07065 { 07066 fct_chk( enclen == outlen ); 07067 } 07068 07069 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07070 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07071 { 07072 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07073 } 07074 else 07075 { 07076 fct_chk( outlen == 0 ); 07077 } 07078 07079 07080 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07081 07082 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07083 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07084 FCT_TEST_END(); 07085 #endif /* POLARSSL_DES_C */ 07086 07087 #ifdef POLARSSL_DES_C 07088 07089 FCT_TEST_BGN(des3_encrypt_and_decrypt_48_bytes) 07090 size_t length = 48; 07091 unsigned char key[32]; 07092 unsigned char iv[16]; 07093 07094 const cipher_info_t *cipher_info; 07095 cipher_context_t ctx_dec; 07096 cipher_context_t ctx_enc; 07097 07098 unsigned char inbuf[64]; 07099 unsigned char encbuf[64]; 07100 unsigned char decbuf[64]; 07101 07102 size_t outlen = 0; 07103 size_t enclen = 0; 07104 07105 memset( key, 0, 32 ); 07106 memset( iv , 0, 16 ); 07107 07108 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07109 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07110 07111 memset( inbuf, 5, 64 ); 07112 memset( encbuf, 0, 64 ); 07113 memset( decbuf, 0, 64 ); 07114 07115 /* Check and get info structures */ 07116 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 07117 fct_chk( NULL != cipher_info ); 07118 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 07119 07120 /* Initialise enc and dec contexts */ 07121 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07122 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07123 07124 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 07125 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 07126 07127 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07128 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07129 07130 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07131 { 07132 enclen = cipher_get_block_size( &ctx_enc ) 07133 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07134 } 07135 else 07136 { 07137 enclen = length; 07138 } 07139 07140 /* encode length number of bytes from inbuf */ 07141 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 07142 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07143 { 07144 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07145 } 07146 else 07147 { 07148 fct_chk( outlen == enclen ); 07149 } 07150 07151 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 07152 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07153 { 07154 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07155 } 07156 else 07157 { 07158 fct_chk( outlen == 0 ); 07159 } 07160 07161 07162 /* decode the previously encoded string */ 07163 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07164 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07165 { 07166 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07167 } 07168 else 07169 { 07170 fct_chk( enclen == outlen ); 07171 } 07172 07173 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07174 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07175 { 07176 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07177 } 07178 else 07179 { 07180 fct_chk( outlen == 0 ); 07181 } 07182 07183 07184 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07185 07186 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07187 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07188 FCT_TEST_END(); 07189 #endif /* POLARSSL_DES_C */ 07190 07191 #ifdef POLARSSL_DES_C 07192 07193 FCT_TEST_BGN(des3_encrypt_and_decrypt_49_bytes) 07194 size_t length = 49; 07195 unsigned char key[32]; 07196 unsigned char iv[16]; 07197 07198 const cipher_info_t *cipher_info; 07199 cipher_context_t ctx_dec; 07200 cipher_context_t ctx_enc; 07201 07202 unsigned char inbuf[64]; 07203 unsigned char encbuf[64]; 07204 unsigned char decbuf[64]; 07205 07206 size_t outlen = 0; 07207 size_t enclen = 0; 07208 07209 memset( key, 0, 32 ); 07210 memset( iv , 0, 16 ); 07211 07212 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07213 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07214 07215 memset( inbuf, 5, 64 ); 07216 memset( encbuf, 0, 64 ); 07217 memset( decbuf, 0, 64 ); 07218 07219 /* Check and get info structures */ 07220 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 07221 fct_chk( NULL != cipher_info ); 07222 fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info ); 07223 07224 /* Initialise enc and dec contexts */ 07225 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07226 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07227 07228 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 07229 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 07230 07231 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07232 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07233 07234 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07235 { 07236 enclen = cipher_get_block_size( &ctx_enc ) 07237 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07238 } 07239 else 07240 { 07241 enclen = length; 07242 } 07243 07244 /* encode length number of bytes from inbuf */ 07245 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); 07246 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07247 { 07248 fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07249 } 07250 else 07251 { 07252 fct_chk( outlen == enclen ); 07253 } 07254 07255 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); 07256 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07257 { 07258 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07259 } 07260 else 07261 { 07262 fct_chk( outlen == 0 ); 07263 } 07264 07265 07266 /* decode the previously encoded string */ 07267 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07268 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07269 { 07270 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07271 } 07272 else 07273 { 07274 fct_chk( enclen == outlen ); 07275 } 07276 07277 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07278 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07279 { 07280 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07281 } 07282 else 07283 { 07284 fct_chk( outlen == 0 ); 07285 } 07286 07287 07288 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07289 07290 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07291 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07292 FCT_TEST_END(); 07293 #endif /* POLARSSL_DES_C */ 07294 07295 #ifdef POLARSSL_DES_C 07296 07297 FCT_TEST_BGN(des3_encrypt_and_decrypt_0_bytes_in_multiple_parts) 07298 size_t first_length = 0; 07299 size_t second_length = 0; 07300 size_t length = first_length + second_length; 07301 unsigned char key[32]; 07302 unsigned char iv[16]; 07303 07304 cipher_context_t ctx_dec; 07305 cipher_context_t ctx_enc; 07306 const cipher_info_t *cipher_info; 07307 07308 unsigned char inbuf[64]; 07309 unsigned char encbuf[64]; 07310 unsigned char decbuf[64]; 07311 07312 size_t outlen = 0; 07313 size_t totaloutlen = 0; 07314 size_t enclen = 0; 07315 07316 memset( key, 0, 32 ); 07317 memset( iv , 0, 16 ); 07318 07319 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07320 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07321 07322 memset( inbuf, 5, 64 ); 07323 memset( encbuf, 0, 64 ); 07324 memset( decbuf, 0, 64 ); 07325 07326 /* Initialise enc and dec contexts */ 07327 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 07328 fct_chk( NULL != cipher_info); 07329 07330 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07331 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07332 07333 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 07334 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 07335 07336 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07337 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07338 07339 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07340 { 07341 enclen = cipher_get_block_size(&ctx_enc ) 07342 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07343 } 07344 else 07345 { 07346 enclen = length; 07347 } 07348 07349 /* encode length number of bytes from inbuf */ 07350 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07351 totaloutlen = outlen; 07352 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07353 totaloutlen += outlen; 07354 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07355 { 07356 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07357 } 07358 else 07359 { 07360 fct_chk( totaloutlen == enclen ); 07361 } 07362 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07363 totaloutlen += outlen; 07364 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07365 { 07366 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07367 } 07368 else 07369 { 07370 fct_chk( outlen == 0 ); 07371 } 07372 07373 /* decode the previously encoded string */ 07374 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07375 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07376 { 07377 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07378 } 07379 else 07380 { 07381 fct_chk( enclen == outlen ); 07382 } 07383 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07384 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07385 { 07386 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07387 } 07388 else 07389 { 07390 fct_chk( outlen == 0 ); 07391 } 07392 07393 07394 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07395 07396 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07397 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07398 FCT_TEST_END(); 07399 #endif /* POLARSSL_DES_C */ 07400 07401 #ifdef POLARSSL_DES_C 07402 07403 FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_1) 07404 size_t first_length = 1; 07405 size_t second_length = 0; 07406 size_t length = first_length + second_length; 07407 unsigned char key[32]; 07408 unsigned char iv[16]; 07409 07410 cipher_context_t ctx_dec; 07411 cipher_context_t ctx_enc; 07412 const cipher_info_t *cipher_info; 07413 07414 unsigned char inbuf[64]; 07415 unsigned char encbuf[64]; 07416 unsigned char decbuf[64]; 07417 07418 size_t outlen = 0; 07419 size_t totaloutlen = 0; 07420 size_t enclen = 0; 07421 07422 memset( key, 0, 32 ); 07423 memset( iv , 0, 16 ); 07424 07425 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07426 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07427 07428 memset( inbuf, 5, 64 ); 07429 memset( encbuf, 0, 64 ); 07430 memset( decbuf, 0, 64 ); 07431 07432 /* Initialise enc and dec contexts */ 07433 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 07434 fct_chk( NULL != cipher_info); 07435 07436 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07437 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07438 07439 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 07440 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 07441 07442 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07443 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07444 07445 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07446 { 07447 enclen = cipher_get_block_size(&ctx_enc ) 07448 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07449 } 07450 else 07451 { 07452 enclen = length; 07453 } 07454 07455 /* encode length number of bytes from inbuf */ 07456 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07457 totaloutlen = outlen; 07458 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07459 totaloutlen += outlen; 07460 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07461 { 07462 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07463 } 07464 else 07465 { 07466 fct_chk( totaloutlen == enclen ); 07467 } 07468 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07469 totaloutlen += outlen; 07470 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07471 { 07472 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07473 } 07474 else 07475 { 07476 fct_chk( outlen == 0 ); 07477 } 07478 07479 /* decode the previously encoded string */ 07480 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07481 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07482 { 07483 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07484 } 07485 else 07486 { 07487 fct_chk( enclen == outlen ); 07488 } 07489 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07490 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07491 { 07492 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07493 } 07494 else 07495 { 07496 fct_chk( outlen == 0 ); 07497 } 07498 07499 07500 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07501 07502 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07503 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07504 FCT_TEST_END(); 07505 #endif /* POLARSSL_DES_C */ 07506 07507 #ifdef POLARSSL_DES_C 07508 07509 FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_2) 07510 size_t first_length = 0; 07511 size_t second_length = 1; 07512 size_t length = first_length + second_length; 07513 unsigned char key[32]; 07514 unsigned char iv[16]; 07515 07516 cipher_context_t ctx_dec; 07517 cipher_context_t ctx_enc; 07518 const cipher_info_t *cipher_info; 07519 07520 unsigned char inbuf[64]; 07521 unsigned char encbuf[64]; 07522 unsigned char decbuf[64]; 07523 07524 size_t outlen = 0; 07525 size_t totaloutlen = 0; 07526 size_t enclen = 0; 07527 07528 memset( key, 0, 32 ); 07529 memset( iv , 0, 16 ); 07530 07531 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07532 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07533 07534 memset( inbuf, 5, 64 ); 07535 memset( encbuf, 0, 64 ); 07536 memset( decbuf, 0, 64 ); 07537 07538 /* Initialise enc and dec contexts */ 07539 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 07540 fct_chk( NULL != cipher_info); 07541 07542 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07543 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07544 07545 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 07546 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 07547 07548 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07549 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07550 07551 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07552 { 07553 enclen = cipher_get_block_size(&ctx_enc ) 07554 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07555 } 07556 else 07557 { 07558 enclen = length; 07559 } 07560 07561 /* encode length number of bytes from inbuf */ 07562 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07563 totaloutlen = outlen; 07564 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07565 totaloutlen += outlen; 07566 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07567 { 07568 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07569 } 07570 else 07571 { 07572 fct_chk( totaloutlen == enclen ); 07573 } 07574 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07575 totaloutlen += outlen; 07576 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07577 { 07578 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07579 } 07580 else 07581 { 07582 fct_chk( outlen == 0 ); 07583 } 07584 07585 /* decode the previously encoded string */ 07586 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07587 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07588 { 07589 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07590 } 07591 else 07592 { 07593 fct_chk( enclen == outlen ); 07594 } 07595 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07596 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07597 { 07598 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07599 } 07600 else 07601 { 07602 fct_chk( outlen == 0 ); 07603 } 07604 07605 07606 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07607 07608 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07609 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07610 FCT_TEST_END(); 07611 #endif /* POLARSSL_DES_C */ 07612 07613 #ifdef POLARSSL_DES_C 07614 07615 FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_1) 07616 size_t first_length = 16; 07617 size_t second_length = 0; 07618 size_t length = first_length + second_length; 07619 unsigned char key[32]; 07620 unsigned char iv[16]; 07621 07622 cipher_context_t ctx_dec; 07623 cipher_context_t ctx_enc; 07624 const cipher_info_t *cipher_info; 07625 07626 unsigned char inbuf[64]; 07627 unsigned char encbuf[64]; 07628 unsigned char decbuf[64]; 07629 07630 size_t outlen = 0; 07631 size_t totaloutlen = 0; 07632 size_t enclen = 0; 07633 07634 memset( key, 0, 32 ); 07635 memset( iv , 0, 16 ); 07636 07637 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07638 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07639 07640 memset( inbuf, 5, 64 ); 07641 memset( encbuf, 0, 64 ); 07642 memset( decbuf, 0, 64 ); 07643 07644 /* Initialise enc and dec contexts */ 07645 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 07646 fct_chk( NULL != cipher_info); 07647 07648 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07649 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07650 07651 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 07652 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 07653 07654 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07655 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07656 07657 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07658 { 07659 enclen = cipher_get_block_size(&ctx_enc ) 07660 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07661 } 07662 else 07663 { 07664 enclen = length; 07665 } 07666 07667 /* encode length number of bytes from inbuf */ 07668 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07669 totaloutlen = outlen; 07670 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07671 totaloutlen += outlen; 07672 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07673 { 07674 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07675 } 07676 else 07677 { 07678 fct_chk( totaloutlen == enclen ); 07679 } 07680 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07681 totaloutlen += outlen; 07682 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07683 { 07684 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07685 } 07686 else 07687 { 07688 fct_chk( outlen == 0 ); 07689 } 07690 07691 /* decode the previously encoded string */ 07692 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07693 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07694 { 07695 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07696 } 07697 else 07698 { 07699 fct_chk( enclen == outlen ); 07700 } 07701 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07702 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07703 { 07704 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07705 } 07706 else 07707 { 07708 fct_chk( outlen == 0 ); 07709 } 07710 07711 07712 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07713 07714 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07715 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07716 FCT_TEST_END(); 07717 #endif /* POLARSSL_DES_C */ 07718 07719 #ifdef POLARSSL_DES_C 07720 07721 FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_2) 07722 size_t first_length = 0; 07723 size_t second_length = 16; 07724 size_t length = first_length + second_length; 07725 unsigned char key[32]; 07726 unsigned char iv[16]; 07727 07728 cipher_context_t ctx_dec; 07729 cipher_context_t ctx_enc; 07730 const cipher_info_t *cipher_info; 07731 07732 unsigned char inbuf[64]; 07733 unsigned char encbuf[64]; 07734 unsigned char decbuf[64]; 07735 07736 size_t outlen = 0; 07737 size_t totaloutlen = 0; 07738 size_t enclen = 0; 07739 07740 memset( key, 0, 32 ); 07741 memset( iv , 0, 16 ); 07742 07743 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07744 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07745 07746 memset( inbuf, 5, 64 ); 07747 memset( encbuf, 0, 64 ); 07748 memset( decbuf, 0, 64 ); 07749 07750 /* Initialise enc and dec contexts */ 07751 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 07752 fct_chk( NULL != cipher_info); 07753 07754 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07755 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07756 07757 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 07758 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 07759 07760 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07761 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07762 07763 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07764 { 07765 enclen = cipher_get_block_size(&ctx_enc ) 07766 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07767 } 07768 else 07769 { 07770 enclen = length; 07771 } 07772 07773 /* encode length number of bytes from inbuf */ 07774 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07775 totaloutlen = outlen; 07776 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07777 totaloutlen += outlen; 07778 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07779 { 07780 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07781 } 07782 else 07783 { 07784 fct_chk( totaloutlen == enclen ); 07785 } 07786 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07787 totaloutlen += outlen; 07788 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07789 { 07790 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07791 } 07792 else 07793 { 07794 fct_chk( outlen == 0 ); 07795 } 07796 07797 /* decode the previously encoded string */ 07798 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07799 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07800 { 07801 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07802 } 07803 else 07804 { 07805 fct_chk( enclen == outlen ); 07806 } 07807 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07808 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07809 { 07810 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07811 } 07812 else 07813 { 07814 fct_chk( outlen == 0 ); 07815 } 07816 07817 07818 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07819 07820 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07821 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07822 FCT_TEST_END(); 07823 #endif /* POLARSSL_DES_C */ 07824 07825 #ifdef POLARSSL_DES_C 07826 07827 FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_3) 07828 size_t first_length = 1; 07829 size_t second_length = 15; 07830 size_t length = first_length + second_length; 07831 unsigned char key[32]; 07832 unsigned char iv[16]; 07833 07834 cipher_context_t ctx_dec; 07835 cipher_context_t ctx_enc; 07836 const cipher_info_t *cipher_info; 07837 07838 unsigned char inbuf[64]; 07839 unsigned char encbuf[64]; 07840 unsigned char decbuf[64]; 07841 07842 size_t outlen = 0; 07843 size_t totaloutlen = 0; 07844 size_t enclen = 0; 07845 07846 memset( key, 0, 32 ); 07847 memset( iv , 0, 16 ); 07848 07849 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07850 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07851 07852 memset( inbuf, 5, 64 ); 07853 memset( encbuf, 0, 64 ); 07854 memset( decbuf, 0, 64 ); 07855 07856 /* Initialise enc and dec contexts */ 07857 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 07858 fct_chk( NULL != cipher_info); 07859 07860 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07861 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07862 07863 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 07864 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 07865 07866 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07867 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07868 07869 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07870 { 07871 enclen = cipher_get_block_size(&ctx_enc ) 07872 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07873 } 07874 else 07875 { 07876 enclen = length; 07877 } 07878 07879 /* encode length number of bytes from inbuf */ 07880 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07881 totaloutlen = outlen; 07882 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07883 totaloutlen += outlen; 07884 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07885 { 07886 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07887 } 07888 else 07889 { 07890 fct_chk( totaloutlen == enclen ); 07891 } 07892 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07893 totaloutlen += outlen; 07894 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07895 { 07896 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 07897 } 07898 else 07899 { 07900 fct_chk( outlen == 0 ); 07901 } 07902 07903 /* decode the previously encoded string */ 07904 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 07905 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07906 { 07907 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 07908 } 07909 else 07910 { 07911 fct_chk( enclen == outlen ); 07912 } 07913 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 07914 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07915 { 07916 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 07917 } 07918 else 07919 { 07920 fct_chk( outlen == 0 ); 07921 } 07922 07923 07924 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 07925 07926 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 07927 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 07928 FCT_TEST_END(); 07929 #endif /* POLARSSL_DES_C */ 07930 07931 #ifdef POLARSSL_DES_C 07932 07933 FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_4) 07934 size_t first_length = 15; 07935 size_t second_length = 1; 07936 size_t length = first_length + second_length; 07937 unsigned char key[32]; 07938 unsigned char iv[16]; 07939 07940 cipher_context_t ctx_dec; 07941 cipher_context_t ctx_enc; 07942 const cipher_info_t *cipher_info; 07943 07944 unsigned char inbuf[64]; 07945 unsigned char encbuf[64]; 07946 unsigned char decbuf[64]; 07947 07948 size_t outlen = 0; 07949 size_t totaloutlen = 0; 07950 size_t enclen = 0; 07951 07952 memset( key, 0, 32 ); 07953 memset( iv , 0, 16 ); 07954 07955 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 07956 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 07957 07958 memset( inbuf, 5, 64 ); 07959 memset( encbuf, 0, 64 ); 07960 memset( decbuf, 0, 64 ); 07961 07962 /* Initialise enc and dec contexts */ 07963 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 07964 fct_chk( NULL != cipher_info); 07965 07966 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 07967 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 07968 07969 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 07970 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 07971 07972 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 07973 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 07974 07975 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07976 { 07977 enclen = cipher_get_block_size(&ctx_enc ) 07978 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 07979 } 07980 else 07981 { 07982 enclen = length; 07983 } 07984 07985 /* encode length number of bytes from inbuf */ 07986 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 07987 totaloutlen = outlen; 07988 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 07989 totaloutlen += outlen; 07990 if( POLARSSL_MODE_CBC == cipher_info->mode ) 07991 { 07992 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 07993 } 07994 else 07995 { 07996 fct_chk( totaloutlen == enclen ); 07997 } 07998 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 07999 totaloutlen += outlen; 08000 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08001 { 08002 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08003 } 08004 else 08005 { 08006 fct_chk( outlen == 0 ); 08007 } 08008 08009 /* decode the previously encoded string */ 08010 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08011 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08012 { 08013 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08014 } 08015 else 08016 { 08017 fct_chk( enclen == outlen ); 08018 } 08019 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08020 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08021 { 08022 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08023 } 08024 else 08025 { 08026 fct_chk( outlen == 0 ); 08027 } 08028 08029 08030 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08031 08032 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08033 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08034 FCT_TEST_END(); 08035 #endif /* POLARSSL_DES_C */ 08036 08037 #ifdef POLARSSL_DES_C 08038 08039 FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 08040 size_t first_length = 15; 08041 size_t second_length = 7; 08042 size_t length = first_length + second_length; 08043 unsigned char key[32]; 08044 unsigned char iv[16]; 08045 08046 cipher_context_t ctx_dec; 08047 cipher_context_t ctx_enc; 08048 const cipher_info_t *cipher_info; 08049 08050 unsigned char inbuf[64]; 08051 unsigned char encbuf[64]; 08052 unsigned char decbuf[64]; 08053 08054 size_t outlen = 0; 08055 size_t totaloutlen = 0; 08056 size_t enclen = 0; 08057 08058 memset( key, 0, 32 ); 08059 memset( iv , 0, 16 ); 08060 08061 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08062 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08063 08064 memset( inbuf, 5, 64 ); 08065 memset( encbuf, 0, 64 ); 08066 memset( decbuf, 0, 64 ); 08067 08068 /* Initialise enc and dec contexts */ 08069 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 08070 fct_chk( NULL != cipher_info); 08071 08072 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08073 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08074 08075 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 08076 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 08077 08078 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08079 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08080 08081 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08082 { 08083 enclen = cipher_get_block_size(&ctx_enc ) 08084 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08085 } 08086 else 08087 { 08088 enclen = length; 08089 } 08090 08091 /* encode length number of bytes from inbuf */ 08092 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 08093 totaloutlen = outlen; 08094 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 08095 totaloutlen += outlen; 08096 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08097 { 08098 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08099 } 08100 else 08101 { 08102 fct_chk( totaloutlen == enclen ); 08103 } 08104 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 08105 totaloutlen += outlen; 08106 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08107 { 08108 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08109 } 08110 else 08111 { 08112 fct_chk( outlen == 0 ); 08113 } 08114 08115 /* decode the previously encoded string */ 08116 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08117 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08118 { 08119 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08120 } 08121 else 08122 { 08123 fct_chk( enclen == outlen ); 08124 } 08125 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08126 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08127 { 08128 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08129 } 08130 else 08131 { 08132 fct_chk( outlen == 0 ); 08133 } 08134 08135 08136 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08137 08138 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08139 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08140 FCT_TEST_END(); 08141 #endif /* POLARSSL_DES_C */ 08142 08143 #ifdef POLARSSL_DES_C 08144 08145 FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 08146 size_t first_length = 16; 08147 size_t second_length = 6; 08148 size_t length = first_length + second_length; 08149 unsigned char key[32]; 08150 unsigned char iv[16]; 08151 08152 cipher_context_t ctx_dec; 08153 cipher_context_t ctx_enc; 08154 const cipher_info_t *cipher_info; 08155 08156 unsigned char inbuf[64]; 08157 unsigned char encbuf[64]; 08158 unsigned char decbuf[64]; 08159 08160 size_t outlen = 0; 08161 size_t totaloutlen = 0; 08162 size_t enclen = 0; 08163 08164 memset( key, 0, 32 ); 08165 memset( iv , 0, 16 ); 08166 08167 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08168 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08169 08170 memset( inbuf, 5, 64 ); 08171 memset( encbuf, 0, 64 ); 08172 memset( decbuf, 0, 64 ); 08173 08174 /* Initialise enc and dec contexts */ 08175 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 08176 fct_chk( NULL != cipher_info); 08177 08178 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08179 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08180 08181 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 08182 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 08183 08184 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08185 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08186 08187 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08188 { 08189 enclen = cipher_get_block_size(&ctx_enc ) 08190 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08191 } 08192 else 08193 { 08194 enclen = length; 08195 } 08196 08197 /* encode length number of bytes from inbuf */ 08198 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 08199 totaloutlen = outlen; 08200 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 08201 totaloutlen += outlen; 08202 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08203 { 08204 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08205 } 08206 else 08207 { 08208 fct_chk( totaloutlen == enclen ); 08209 } 08210 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 08211 totaloutlen += outlen; 08212 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08213 { 08214 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08215 } 08216 else 08217 { 08218 fct_chk( outlen == 0 ); 08219 } 08220 08221 /* decode the previously encoded string */ 08222 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08223 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08224 { 08225 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08226 } 08227 else 08228 { 08229 fct_chk( enclen == outlen ); 08230 } 08231 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08232 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08233 { 08234 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08235 } 08236 else 08237 { 08238 fct_chk( outlen == 0 ); 08239 } 08240 08241 08242 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08243 08244 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08245 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08246 FCT_TEST_END(); 08247 #endif /* POLARSSL_DES_C */ 08248 08249 #ifdef POLARSSL_DES_C 08250 08251 FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1) 08252 size_t first_length = 17; 08253 size_t second_length = 6; 08254 size_t length = first_length + second_length; 08255 unsigned char key[32]; 08256 unsigned char iv[16]; 08257 08258 cipher_context_t ctx_dec; 08259 cipher_context_t ctx_enc; 08260 const cipher_info_t *cipher_info; 08261 08262 unsigned char inbuf[64]; 08263 unsigned char encbuf[64]; 08264 unsigned char decbuf[64]; 08265 08266 size_t outlen = 0; 08267 size_t totaloutlen = 0; 08268 size_t enclen = 0; 08269 08270 memset( key, 0, 32 ); 08271 memset( iv , 0, 16 ); 08272 08273 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08274 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08275 08276 memset( inbuf, 5, 64 ); 08277 memset( encbuf, 0, 64 ); 08278 memset( decbuf, 0, 64 ); 08279 08280 /* Initialise enc and dec contexts */ 08281 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 08282 fct_chk( NULL != cipher_info); 08283 08284 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08285 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08286 08287 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 08288 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 08289 08290 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08291 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08292 08293 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08294 { 08295 enclen = cipher_get_block_size(&ctx_enc ) 08296 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08297 } 08298 else 08299 { 08300 enclen = length; 08301 } 08302 08303 /* encode length number of bytes from inbuf */ 08304 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 08305 totaloutlen = outlen; 08306 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 08307 totaloutlen += outlen; 08308 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08309 { 08310 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08311 } 08312 else 08313 { 08314 fct_chk( totaloutlen == enclen ); 08315 } 08316 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 08317 totaloutlen += outlen; 08318 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08319 { 08320 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08321 } 08322 else 08323 { 08324 fct_chk( outlen == 0 ); 08325 } 08326 08327 /* decode the previously encoded string */ 08328 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08329 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08330 { 08331 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08332 } 08333 else 08334 { 08335 fct_chk( enclen == outlen ); 08336 } 08337 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08338 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08339 { 08340 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08341 } 08342 else 08343 { 08344 fct_chk( outlen == 0 ); 08345 } 08346 08347 08348 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08349 08350 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08351 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08352 FCT_TEST_END(); 08353 #endif /* POLARSSL_DES_C */ 08354 08355 #ifdef POLARSSL_DES_C 08356 08357 FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes_in_multiple_parts_1) 08358 size_t first_length = 16; 08359 size_t second_length = 16; 08360 size_t length = first_length + second_length; 08361 unsigned char key[32]; 08362 unsigned char iv[16]; 08363 08364 cipher_context_t ctx_dec; 08365 cipher_context_t ctx_enc; 08366 const cipher_info_t *cipher_info; 08367 08368 unsigned char inbuf[64]; 08369 unsigned char encbuf[64]; 08370 unsigned char decbuf[64]; 08371 08372 size_t outlen = 0; 08373 size_t totaloutlen = 0; 08374 size_t enclen = 0; 08375 08376 memset( key, 0, 32 ); 08377 memset( iv , 0, 16 ); 08378 08379 memset( &ctx_dec, 0, sizeof( ctx_dec ) ); 08380 memset( &ctx_enc, 0, sizeof( ctx_enc ) ); 08381 08382 memset( inbuf, 5, 64 ); 08383 memset( encbuf, 0, 64 ); 08384 memset( decbuf, 0, 64 ); 08385 08386 /* Initialise enc and dec contexts */ 08387 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); 08388 fct_chk( NULL != cipher_info); 08389 08390 fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); 08391 fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); 08392 08393 fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) ); 08394 fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) ); 08395 08396 fct_chk( 0 == cipher_reset( &ctx_dec, iv ) ); 08397 fct_chk( 0 == cipher_reset( &ctx_enc, iv ) ); 08398 08399 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08400 { 08401 enclen = cipher_get_block_size(&ctx_enc ) 08402 * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); 08403 } 08404 else 08405 { 08406 enclen = length; 08407 } 08408 08409 /* encode length number of bytes from inbuf */ 08410 fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); 08411 totaloutlen = outlen; 08412 fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); 08413 totaloutlen += outlen; 08414 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08415 { 08416 fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); 08417 } 08418 else 08419 { 08420 fct_chk( totaloutlen == enclen ); 08421 } 08422 fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); 08423 totaloutlen += outlen; 08424 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08425 { 08426 fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) ); 08427 } 08428 else 08429 { 08430 fct_chk( outlen == 0 ); 08431 } 08432 08433 /* decode the previously encoded string */ 08434 fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); 08435 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08436 { 08437 fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); 08438 } 08439 else 08440 { 08441 fct_chk( enclen == outlen ); 08442 } 08443 fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); 08444 if( POLARSSL_MODE_CBC == cipher_info->mode ) 08445 { 08446 fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); 08447 } 08448 else 08449 { 08450 fct_chk( outlen == 0 ); 08451 } 08452 08453 08454 fct_chk( 0 == memcmp(inbuf, decbuf, length) ); 08455 08456 fct_chk( 0 == cipher_free_ctx( &ctx_dec ) ); 08457 fct_chk( 0 == cipher_free_ctx( &ctx_enc ) ); 08458 FCT_TEST_END(); 08459 #endif /* POLARSSL_DES_C */ 08460 08461 } 08462 FCT_SUITE_END(); 08463 08464 #endif /* POLARSSL_CIPHER_C */ 08465 08466 } 08467 FCT_END(); 08468