PolarSSL v1.2.8
test_suite_cipher.aes.c
Go to the documentation of this file.
1 #include "fct.h"
2 #include <polarssl/config.h>
3 
4 #include <polarssl/cipher.h>
5 
6 #ifdef _MSC_VER
7 #include <basetsd.h>
8 typedef UINT32 uint32_t;
9 #else
10 #include <inttypes.h>
11 #endif
12 
13 /*
14  * 32-bit integer manipulation macros (big endian)
15  */
16 #ifndef GET_UINT32_BE
17 #define GET_UINT32_BE(n,b,i) \
18 { \
19  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
20  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
21  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
22  | ( (uint32_t) (b)[(i) + 3] ); \
23 }
24 #endif
25 
26 #ifndef PUT_UINT32_BE
27 #define PUT_UINT32_BE(n,b,i) \
28 { \
29  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
30  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
31  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
32  (b)[(i) + 3] = (unsigned char) ( (n) ); \
33 }
34 #endif
35 
36 int unhexify(unsigned char *obuf, const char *ibuf)
37 {
38  unsigned char c, c2;
39  int len = strlen(ibuf) / 2;
40  assert(!(strlen(ibuf) %1)); // must be even number of bytes
41 
42  while (*ibuf != 0)
43  {
44  c = *ibuf++;
45  if( c >= '0' && c <= '9' )
46  c -= '0';
47  else if( c >= 'a' && c <= 'f' )
48  c -= 'a' - 10;
49  else if( c >= 'A' && c <= 'F' )
50  c -= 'A' - 10;
51  else
52  assert( 0 );
53 
54  c2 = *ibuf++;
55  if( c2 >= '0' && c2 <= '9' )
56  c2 -= '0';
57  else if( c2 >= 'a' && c2 <= 'f' )
58  c2 -= 'a' - 10;
59  else if( c2 >= 'A' && c2 <= 'F' )
60  c2 -= 'A' - 10;
61  else
62  assert( 0 );
63 
64  *obuf++ = ( c << 4 ) | c2;
65  }
66 
67  return len;
68 }
69 
70 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
71 {
72  unsigned char l, h;
73 
74  while (len != 0)
75  {
76  h = (*ibuf) / 16;
77  l = (*ibuf) % 16;
78 
79  if( h < 10 )
80  *obuf++ = '0' + h;
81  else
82  *obuf++ = 'a' + h - 10;
83 
84  if( l < 10 )
85  *obuf++ = '0' + l;
86  else
87  *obuf++ = 'a' + l - 10;
88 
89  ++ibuf;
90  len--;
91  }
92 }
93 
103 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
104 {
105  size_t i;
106 
107  if( rng_state != NULL )
108  rng_state = NULL;
109 
110  for( i = 0; i < len; ++i )
111  output[i] = rand();
112 
113  return( 0 );
114 }
115 
121 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
122 {
123  if( rng_state != NULL )
124  rng_state = NULL;
125 
126  memset( output, 0, len );
127 
128  return( 0 );
129 }
130 
131 typedef struct
132 {
133  unsigned char *buf;
134  size_t length;
135 } rnd_buf_info;
136 
148 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
149 {
150  rnd_buf_info *info = (rnd_buf_info *) rng_state;
151  size_t use_len;
152 
153  if( rng_state == NULL )
154  return( rnd_std_rand( NULL, output, len ) );
155 
156  use_len = len;
157  if( len > info->length )
158  use_len = info->length;
159 
160  if( use_len )
161  {
162  memcpy( output, info->buf, use_len );
163  info->buf += use_len;
164  info->length -= use_len;
165  }
166 
167  if( len - use_len > 0 )
168  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
169 
170  return( 0 );
171 }
172 
180 typedef struct
181 {
182  uint32_t key[16];
183  uint32_t v0, v1;
185 
194 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
195 {
196  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
197  uint32_t i, *k, sum, delta=0x9E3779B9;
198  unsigned char result[4];
199 
200  if( rng_state == NULL )
201  return( rnd_std_rand( NULL, output, len ) );
202 
203  k = info->key;
204 
205  while( len > 0 )
206  {
207  size_t use_len = ( len > 4 ) ? 4 : len;
208  sum = 0;
209 
210  for( i = 0; i < 32; i++ )
211  {
212  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
213  sum += delta;
214  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
215  }
216 
217  PUT_UINT32_BE( info->v0, result, 0 );
218  memcpy( output, result, use_len );
219  len -= use_len;
220  }
221 
222  return( 0 );
223 }
224 
225 
227 {
228 #ifdef POLARSSL_CIPHER_C
229 
230 
231  FCT_SUITE_BGN(test_suite_cipher)
232  {
233 #ifdef POLARSSL_SELF_TEST
234 
235  FCT_TEST_BGN(cipher_selftest)
236  {
237  fct_chk( cipher_self_test( 0 ) == 0 );
238  }
239  FCT_TEST_END();
240 #endif /* POLARSSL_SELF_TEST */
241 
242 
243  FCT_TEST_BGN(decrypt_empty_buffer)
244  unsigned char key[32];
245  unsigned char iv[16];
246 
247  cipher_context_t ctx_dec;
248  const cipher_info_t *cipher_info;
249 
250  unsigned char encbuf[64];
251  unsigned char decbuf[64];
252 
253  size_t outlen = 0;
254 
255  memset( key, 0, 32 );
256  memset( iv , 0, 16 );
257 
258  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
259 
260  memset( encbuf, 0, 64 );
261  memset( decbuf, 0, 64 );
262 
263  /* Initialise enc and dec contexts */
265  fct_chk( NULL != cipher_info);
266 
267  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
268 
269  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
270 
271  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
272 
273  /* decode 0-byte string */
274  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
275  fct_chk( 0 == outlen );
276  fct_chk( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
277  fct_chk( 0 == outlen );
278 
279  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
280  FCT_TEST_END();
281 
282 #ifdef POLARSSL_AES_C
283 
284  FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes)
285  size_t length = 0;
286  unsigned char key[32];
287  unsigned char iv[16];
288 
289  const cipher_info_t *cipher_info;
290  cipher_context_t ctx_dec;
291  cipher_context_t ctx_enc;
292 
293  unsigned char inbuf[64];
294  unsigned char encbuf[64];
295  unsigned char decbuf[64];
296 
297  size_t outlen = 0;
298  size_t enclen = 0;
299 
300  memset( key, 0, 32 );
301  memset( iv , 0, 16 );
302 
303  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
304  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
305 
306  memset( inbuf, 5, 64 );
307  memset( encbuf, 0, 64 );
308  memset( decbuf, 0, 64 );
309 
310  /* Check and get info structures */
312  fct_chk( NULL != cipher_info );
313  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
314 
315  /* Initialise enc and dec contexts */
316  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
317  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
318 
319  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
320  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
321 
322  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
323  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
324 
325  if( POLARSSL_MODE_CBC == cipher_info->mode )
326  {
327  enclen = cipher_get_block_size( &ctx_enc )
328  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
329  }
330  else
331  {
332  enclen = length;
333  }
334 
335  /* encode length number of bytes from inbuf */
336  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
337  if( POLARSSL_MODE_CBC == cipher_info->mode )
338  {
339  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
340  }
341  else
342  {
343  fct_chk( outlen == enclen );
344  }
345 
346  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
347  if( POLARSSL_MODE_CBC == cipher_info->mode )
348  {
349  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
350  }
351  else
352  {
353  fct_chk( outlen == 0 );
354  }
355 
356 
357  /* decode the previously encoded string */
358  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
359  if( POLARSSL_MODE_CBC == cipher_info->mode )
360  {
361  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
362  }
363  else
364  {
365  fct_chk( enclen == outlen );
366  }
367 
368  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
369  if( POLARSSL_MODE_CBC == cipher_info->mode )
370  {
371  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
372  }
373  else
374  {
375  fct_chk( outlen == 0 );
376  }
377 
378 
379  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
380 
381  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
382  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
383  FCT_TEST_END();
384 #endif /* POLARSSL_AES_C */
385 
386 #ifdef POLARSSL_AES_C
387 
388  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_byte)
389  size_t length = 1;
390  unsigned char key[32];
391  unsigned char iv[16];
392 
393  const cipher_info_t *cipher_info;
394  cipher_context_t ctx_dec;
395  cipher_context_t ctx_enc;
396 
397  unsigned char inbuf[64];
398  unsigned char encbuf[64];
399  unsigned char decbuf[64];
400 
401  size_t outlen = 0;
402  size_t enclen = 0;
403 
404  memset( key, 0, 32 );
405  memset( iv , 0, 16 );
406 
407  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
408  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
409 
410  memset( inbuf, 5, 64 );
411  memset( encbuf, 0, 64 );
412  memset( decbuf, 0, 64 );
413 
414  /* Check and get info structures */
416  fct_chk( NULL != cipher_info );
417  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
418 
419  /* Initialise enc and dec contexts */
420  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
421  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
422 
423  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
424  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
425 
426  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
427  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
428 
429  if( POLARSSL_MODE_CBC == cipher_info->mode )
430  {
431  enclen = cipher_get_block_size( &ctx_enc )
432  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
433  }
434  else
435  {
436  enclen = length;
437  }
438 
439  /* encode length number of bytes from inbuf */
440  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
441  if( POLARSSL_MODE_CBC == cipher_info->mode )
442  {
443  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
444  }
445  else
446  {
447  fct_chk( outlen == enclen );
448  }
449 
450  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
451  if( POLARSSL_MODE_CBC == cipher_info->mode )
452  {
453  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
454  }
455  else
456  {
457  fct_chk( outlen == 0 );
458  }
459 
460 
461  /* decode the previously encoded string */
462  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
463  if( POLARSSL_MODE_CBC == cipher_info->mode )
464  {
465  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
466  }
467  else
468  {
469  fct_chk( enclen == outlen );
470  }
471 
472  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
473  if( POLARSSL_MODE_CBC == cipher_info->mode )
474  {
475  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
476  }
477  else
478  {
479  fct_chk( outlen == 0 );
480  }
481 
482 
483  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
484 
485  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
486  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
487  FCT_TEST_END();
488 #endif /* POLARSSL_AES_C */
489 
490 #ifdef POLARSSL_AES_C
491 
492  FCT_TEST_BGN(aes_encrypt_and_decrypt_2_bytes)
493  size_t length = 2;
494  unsigned char key[32];
495  unsigned char iv[16];
496 
497  const cipher_info_t *cipher_info;
498  cipher_context_t ctx_dec;
499  cipher_context_t ctx_enc;
500 
501  unsigned char inbuf[64];
502  unsigned char encbuf[64];
503  unsigned char decbuf[64];
504 
505  size_t outlen = 0;
506  size_t enclen = 0;
507 
508  memset( key, 0, 32 );
509  memset( iv , 0, 16 );
510 
511  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
512  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
513 
514  memset( inbuf, 5, 64 );
515  memset( encbuf, 0, 64 );
516  memset( decbuf, 0, 64 );
517 
518  /* Check and get info structures */
520  fct_chk( NULL != cipher_info );
521  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
522 
523  /* Initialise enc and dec contexts */
524  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
525  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
526 
527  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
528  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
529 
530  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
531  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
532 
533  if( POLARSSL_MODE_CBC == cipher_info->mode )
534  {
535  enclen = cipher_get_block_size( &ctx_enc )
536  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
537  }
538  else
539  {
540  enclen = length;
541  }
542 
543  /* encode length number of bytes from inbuf */
544  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
545  if( POLARSSL_MODE_CBC == cipher_info->mode )
546  {
547  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
548  }
549  else
550  {
551  fct_chk( outlen == enclen );
552  }
553 
554  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
555  if( POLARSSL_MODE_CBC == cipher_info->mode )
556  {
557  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
558  }
559  else
560  {
561  fct_chk( outlen == 0 );
562  }
563 
564 
565  /* decode the previously encoded string */
566  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
567  if( POLARSSL_MODE_CBC == cipher_info->mode )
568  {
569  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
570  }
571  else
572  {
573  fct_chk( enclen == outlen );
574  }
575 
576  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
577  if( POLARSSL_MODE_CBC == cipher_info->mode )
578  {
579  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
580  }
581  else
582  {
583  fct_chk( outlen == 0 );
584  }
585 
586 
587  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
588 
589  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
590  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
591  FCT_TEST_END();
592 #endif /* POLARSSL_AES_C */
593 
594 #ifdef POLARSSL_AES_C
595 
596  FCT_TEST_BGN(aes_encrypt_and_decrypt_7_bytes)
597  size_t length = 7;
598  unsigned char key[32];
599  unsigned char iv[16];
600 
601  const cipher_info_t *cipher_info;
602  cipher_context_t ctx_dec;
603  cipher_context_t ctx_enc;
604 
605  unsigned char inbuf[64];
606  unsigned char encbuf[64];
607  unsigned char decbuf[64];
608 
609  size_t outlen = 0;
610  size_t enclen = 0;
611 
612  memset( key, 0, 32 );
613  memset( iv , 0, 16 );
614 
615  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
616  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
617 
618  memset( inbuf, 5, 64 );
619  memset( encbuf, 0, 64 );
620  memset( decbuf, 0, 64 );
621 
622  /* Check and get info structures */
624  fct_chk( NULL != cipher_info );
625  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
626 
627  /* Initialise enc and dec contexts */
628  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
629  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
630 
631  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
632  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
633 
634  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
635  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
636 
637  if( POLARSSL_MODE_CBC == cipher_info->mode )
638  {
639  enclen = cipher_get_block_size( &ctx_enc )
640  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
641  }
642  else
643  {
644  enclen = length;
645  }
646 
647  /* encode length number of bytes from inbuf */
648  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
649  if( POLARSSL_MODE_CBC == cipher_info->mode )
650  {
651  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
652  }
653  else
654  {
655  fct_chk( outlen == enclen );
656  }
657 
658  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
659  if( POLARSSL_MODE_CBC == cipher_info->mode )
660  {
661  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
662  }
663  else
664  {
665  fct_chk( outlen == 0 );
666  }
667 
668 
669  /* decode the previously encoded string */
670  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
671  if( POLARSSL_MODE_CBC == cipher_info->mode )
672  {
673  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
674  }
675  else
676  {
677  fct_chk( enclen == outlen );
678  }
679 
680  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
681  if( POLARSSL_MODE_CBC == cipher_info->mode )
682  {
683  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
684  }
685  else
686  {
687  fct_chk( outlen == 0 );
688  }
689 
690 
691  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
692 
693  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
694  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
695  FCT_TEST_END();
696 #endif /* POLARSSL_AES_C */
697 
698 #ifdef POLARSSL_AES_C
699 
700  FCT_TEST_BGN(aes_encrypt_and_decrypt_8_bytes)
701  size_t length = 8;
702  unsigned char key[32];
703  unsigned char iv[16];
704 
705  const cipher_info_t *cipher_info;
706  cipher_context_t ctx_dec;
707  cipher_context_t ctx_enc;
708 
709  unsigned char inbuf[64];
710  unsigned char encbuf[64];
711  unsigned char decbuf[64];
712 
713  size_t outlen = 0;
714  size_t enclen = 0;
715 
716  memset( key, 0, 32 );
717  memset( iv , 0, 16 );
718 
719  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
720  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
721 
722  memset( inbuf, 5, 64 );
723  memset( encbuf, 0, 64 );
724  memset( decbuf, 0, 64 );
725 
726  /* Check and get info structures */
728  fct_chk( NULL != cipher_info );
729  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
730 
731  /* Initialise enc and dec contexts */
732  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
733  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
734 
735  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
736  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
737 
738  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
739  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
740 
741  if( POLARSSL_MODE_CBC == cipher_info->mode )
742  {
743  enclen = cipher_get_block_size( &ctx_enc )
744  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
745  }
746  else
747  {
748  enclen = length;
749  }
750 
751  /* encode length number of bytes from inbuf */
752  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
753  if( POLARSSL_MODE_CBC == cipher_info->mode )
754  {
755  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
756  }
757  else
758  {
759  fct_chk( outlen == enclen );
760  }
761 
762  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
763  if( POLARSSL_MODE_CBC == cipher_info->mode )
764  {
765  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
766  }
767  else
768  {
769  fct_chk( outlen == 0 );
770  }
771 
772 
773  /* decode the previously encoded string */
774  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
775  if( POLARSSL_MODE_CBC == cipher_info->mode )
776  {
777  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
778  }
779  else
780  {
781  fct_chk( enclen == outlen );
782  }
783 
784  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
785  if( POLARSSL_MODE_CBC == cipher_info->mode )
786  {
787  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
788  }
789  else
790  {
791  fct_chk( outlen == 0 );
792  }
793 
794 
795  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
796 
797  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
798  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
799  FCT_TEST_END();
800 #endif /* POLARSSL_AES_C */
801 
802 #ifdef POLARSSL_AES_C
803 
804  FCT_TEST_BGN(aes_encrypt_and_decrypt_9_bytes)
805  size_t length = 9;
806  unsigned char key[32];
807  unsigned char iv[16];
808 
809  const cipher_info_t *cipher_info;
810  cipher_context_t ctx_dec;
811  cipher_context_t ctx_enc;
812 
813  unsigned char inbuf[64];
814  unsigned char encbuf[64];
815  unsigned char decbuf[64];
816 
817  size_t outlen = 0;
818  size_t enclen = 0;
819 
820  memset( key, 0, 32 );
821  memset( iv , 0, 16 );
822 
823  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
824  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
825 
826  memset( inbuf, 5, 64 );
827  memset( encbuf, 0, 64 );
828  memset( decbuf, 0, 64 );
829 
830  /* Check and get info structures */
832  fct_chk( NULL != cipher_info );
833  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
834 
835  /* Initialise enc and dec contexts */
836  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
837  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
838 
839  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
840  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
841 
842  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
843  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
844 
845  if( POLARSSL_MODE_CBC == cipher_info->mode )
846  {
847  enclen = cipher_get_block_size( &ctx_enc )
848  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
849  }
850  else
851  {
852  enclen = length;
853  }
854 
855  /* encode length number of bytes from inbuf */
856  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
857  if( POLARSSL_MODE_CBC == cipher_info->mode )
858  {
859  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
860  }
861  else
862  {
863  fct_chk( outlen == enclen );
864  }
865 
866  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
867  if( POLARSSL_MODE_CBC == cipher_info->mode )
868  {
869  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
870  }
871  else
872  {
873  fct_chk( outlen == 0 );
874  }
875 
876 
877  /* decode the previously encoded string */
878  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
879  if( POLARSSL_MODE_CBC == cipher_info->mode )
880  {
881  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
882  }
883  else
884  {
885  fct_chk( enclen == outlen );
886  }
887 
888  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
889  if( POLARSSL_MODE_CBC == cipher_info->mode )
890  {
891  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
892  }
893  else
894  {
895  fct_chk( outlen == 0 );
896  }
897 
898 
899  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
900 
901  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
902  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
903  FCT_TEST_END();
904 #endif /* POLARSSL_AES_C */
905 
906 #ifdef POLARSSL_AES_C
907 
908  FCT_TEST_BGN(aes_encrypt_and_decrypt_15_bytes)
909  size_t length = 15;
910  unsigned char key[32];
911  unsigned char iv[16];
912 
913  const cipher_info_t *cipher_info;
914  cipher_context_t ctx_dec;
915  cipher_context_t ctx_enc;
916 
917  unsigned char inbuf[64];
918  unsigned char encbuf[64];
919  unsigned char decbuf[64];
920 
921  size_t outlen = 0;
922  size_t enclen = 0;
923 
924  memset( key, 0, 32 );
925  memset( iv , 0, 16 );
926 
927  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
928  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
929 
930  memset( inbuf, 5, 64 );
931  memset( encbuf, 0, 64 );
932  memset( decbuf, 0, 64 );
933 
934  /* Check and get info structures */
936  fct_chk( NULL != cipher_info );
937  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
938 
939  /* Initialise enc and dec contexts */
940  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
941  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
942 
943  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
944  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
945 
946  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
947  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
948 
949  if( POLARSSL_MODE_CBC == cipher_info->mode )
950  {
951  enclen = cipher_get_block_size( &ctx_enc )
952  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
953  }
954  else
955  {
956  enclen = length;
957  }
958 
959  /* encode length number of bytes from inbuf */
960  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
961  if( POLARSSL_MODE_CBC == cipher_info->mode )
962  {
963  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
964  }
965  else
966  {
967  fct_chk( outlen == enclen );
968  }
969 
970  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
971  if( POLARSSL_MODE_CBC == cipher_info->mode )
972  {
973  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
974  }
975  else
976  {
977  fct_chk( outlen == 0 );
978  }
979 
980 
981  /* decode the previously encoded string */
982  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
983  if( POLARSSL_MODE_CBC == cipher_info->mode )
984  {
985  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
986  }
987  else
988  {
989  fct_chk( enclen == outlen );
990  }
991 
992  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
993  if( POLARSSL_MODE_CBC == cipher_info->mode )
994  {
995  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
996  }
997  else
998  {
999  fct_chk( outlen == 0 );
1000  }
1001 
1002 
1003  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1004 
1005  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1006  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1007  FCT_TEST_END();
1008 #endif /* POLARSSL_AES_C */
1009 
1010 #ifdef POLARSSL_AES_C
1011 
1012  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes)
1013  size_t length = 16;
1014  unsigned char key[32];
1015  unsigned char iv[16];
1016 
1017  const cipher_info_t *cipher_info;
1018  cipher_context_t ctx_dec;
1019  cipher_context_t ctx_enc;
1020 
1021  unsigned char inbuf[64];
1022  unsigned char encbuf[64];
1023  unsigned char decbuf[64];
1024 
1025  size_t outlen = 0;
1026  size_t enclen = 0;
1027 
1028  memset( key, 0, 32 );
1029  memset( iv , 0, 16 );
1030 
1031  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1032  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1033 
1034  memset( inbuf, 5, 64 );
1035  memset( encbuf, 0, 64 );
1036  memset( decbuf, 0, 64 );
1037 
1038  /* Check and get info structures */
1040  fct_chk( NULL != cipher_info );
1041  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
1042 
1043  /* Initialise enc and dec contexts */
1044  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1045  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1046 
1047  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
1048  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
1049 
1050  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1051  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1052 
1053  if( POLARSSL_MODE_CBC == cipher_info->mode )
1054  {
1055  enclen = cipher_get_block_size( &ctx_enc )
1056  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1057  }
1058  else
1059  {
1060  enclen = length;
1061  }
1062 
1063  /* encode length number of bytes from inbuf */
1064  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1065  if( POLARSSL_MODE_CBC == cipher_info->mode )
1066  {
1067  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1068  }
1069  else
1070  {
1071  fct_chk( outlen == enclen );
1072  }
1073 
1074  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1075  if( POLARSSL_MODE_CBC == cipher_info->mode )
1076  {
1077  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1078  }
1079  else
1080  {
1081  fct_chk( outlen == 0 );
1082  }
1083 
1084 
1085  /* decode the previously encoded string */
1086  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1087  if( POLARSSL_MODE_CBC == cipher_info->mode )
1088  {
1089  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1090  }
1091  else
1092  {
1093  fct_chk( enclen == outlen );
1094  }
1095 
1096  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1097  if( POLARSSL_MODE_CBC == cipher_info->mode )
1098  {
1099  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1100  }
1101  else
1102  {
1103  fct_chk( outlen == 0 );
1104  }
1105 
1106 
1107  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1108 
1109  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1110  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1111  FCT_TEST_END();
1112 #endif /* POLARSSL_AES_C */
1113 
1114 #ifdef POLARSSL_AES_C
1115 
1116  FCT_TEST_BGN(aes_encrypt_and_decrypt_17_bytes)
1117  size_t length = 17;
1118  unsigned char key[32];
1119  unsigned char iv[16];
1120 
1121  const cipher_info_t *cipher_info;
1122  cipher_context_t ctx_dec;
1123  cipher_context_t ctx_enc;
1124 
1125  unsigned char inbuf[64];
1126  unsigned char encbuf[64];
1127  unsigned char decbuf[64];
1128 
1129  size_t outlen = 0;
1130  size_t enclen = 0;
1131 
1132  memset( key, 0, 32 );
1133  memset( iv , 0, 16 );
1134 
1135  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1136  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1137 
1138  memset( inbuf, 5, 64 );
1139  memset( encbuf, 0, 64 );
1140  memset( decbuf, 0, 64 );
1141 
1142  /* Check and get info structures */
1144  fct_chk( NULL != cipher_info );
1145  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
1146 
1147  /* Initialise enc and dec contexts */
1148  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1149  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1150 
1151  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
1152  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
1153 
1154  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1155  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1156 
1157  if( POLARSSL_MODE_CBC == cipher_info->mode )
1158  {
1159  enclen = cipher_get_block_size( &ctx_enc )
1160  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1161  }
1162  else
1163  {
1164  enclen = length;
1165  }
1166 
1167  /* encode length number of bytes from inbuf */
1168  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1169  if( POLARSSL_MODE_CBC == cipher_info->mode )
1170  {
1171  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1172  }
1173  else
1174  {
1175  fct_chk( outlen == enclen );
1176  }
1177 
1178  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1179  if( POLARSSL_MODE_CBC == cipher_info->mode )
1180  {
1181  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1182  }
1183  else
1184  {
1185  fct_chk( outlen == 0 );
1186  }
1187 
1188 
1189  /* decode the previously encoded string */
1190  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1191  if( POLARSSL_MODE_CBC == cipher_info->mode )
1192  {
1193  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1194  }
1195  else
1196  {
1197  fct_chk( enclen == outlen );
1198  }
1199 
1200  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1201  if( POLARSSL_MODE_CBC == cipher_info->mode )
1202  {
1203  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1204  }
1205  else
1206  {
1207  fct_chk( outlen == 0 );
1208  }
1209 
1210 
1211  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1212 
1213  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1214  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1215  FCT_TEST_END();
1216 #endif /* POLARSSL_AES_C */
1217 
1218 #ifdef POLARSSL_AES_C
1219 
1220  FCT_TEST_BGN(aes_encrypt_and_decrypt_31_bytes)
1221  size_t length = 31;
1222  unsigned char key[32];
1223  unsigned char iv[16];
1224 
1225  const cipher_info_t *cipher_info;
1226  cipher_context_t ctx_dec;
1227  cipher_context_t ctx_enc;
1228 
1229  unsigned char inbuf[64];
1230  unsigned char encbuf[64];
1231  unsigned char decbuf[64];
1232 
1233  size_t outlen = 0;
1234  size_t enclen = 0;
1235 
1236  memset( key, 0, 32 );
1237  memset( iv , 0, 16 );
1238 
1239  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1240  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1241 
1242  memset( inbuf, 5, 64 );
1243  memset( encbuf, 0, 64 );
1244  memset( decbuf, 0, 64 );
1245 
1246  /* Check and get info structures */
1248  fct_chk( NULL != cipher_info );
1249  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
1250 
1251  /* Initialise enc and dec contexts */
1252  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1253  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1254 
1255  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
1256  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
1257 
1258  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1259  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1260 
1261  if( POLARSSL_MODE_CBC == cipher_info->mode )
1262  {
1263  enclen = cipher_get_block_size( &ctx_enc )
1264  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1265  }
1266  else
1267  {
1268  enclen = length;
1269  }
1270 
1271  /* encode length number of bytes from inbuf */
1272  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1273  if( POLARSSL_MODE_CBC == cipher_info->mode )
1274  {
1275  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1276  }
1277  else
1278  {
1279  fct_chk( outlen == enclen );
1280  }
1281 
1282  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1283  if( POLARSSL_MODE_CBC == cipher_info->mode )
1284  {
1285  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1286  }
1287  else
1288  {
1289  fct_chk( outlen == 0 );
1290  }
1291 
1292 
1293  /* decode the previously encoded string */
1294  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1295  if( POLARSSL_MODE_CBC == cipher_info->mode )
1296  {
1297  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1298  }
1299  else
1300  {
1301  fct_chk( enclen == outlen );
1302  }
1303 
1304  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1305  if( POLARSSL_MODE_CBC == cipher_info->mode )
1306  {
1307  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1308  }
1309  else
1310  {
1311  fct_chk( outlen == 0 );
1312  }
1313 
1314 
1315  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1316 
1317  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1318  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1319  FCT_TEST_END();
1320 #endif /* POLARSSL_AES_C */
1321 
1322 #ifdef POLARSSL_AES_C
1323 
1324  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes)
1325  size_t length = 32;
1326  unsigned char key[32];
1327  unsigned char iv[16];
1328 
1329  const cipher_info_t *cipher_info;
1330  cipher_context_t ctx_dec;
1331  cipher_context_t ctx_enc;
1332 
1333  unsigned char inbuf[64];
1334  unsigned char encbuf[64];
1335  unsigned char decbuf[64];
1336 
1337  size_t outlen = 0;
1338  size_t enclen = 0;
1339 
1340  memset( key, 0, 32 );
1341  memset( iv , 0, 16 );
1342 
1343  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1344  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1345 
1346  memset( inbuf, 5, 64 );
1347  memset( encbuf, 0, 64 );
1348  memset( decbuf, 0, 64 );
1349 
1350  /* Check and get info structures */
1352  fct_chk( NULL != cipher_info );
1353  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
1354 
1355  /* Initialise enc and dec contexts */
1356  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1357  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1358 
1359  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
1360  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
1361 
1362  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1363  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1364 
1365  if( POLARSSL_MODE_CBC == cipher_info->mode )
1366  {
1367  enclen = cipher_get_block_size( &ctx_enc )
1368  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1369  }
1370  else
1371  {
1372  enclen = length;
1373  }
1374 
1375  /* encode length number of bytes from inbuf */
1376  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1377  if( POLARSSL_MODE_CBC == cipher_info->mode )
1378  {
1379  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1380  }
1381  else
1382  {
1383  fct_chk( outlen == enclen );
1384  }
1385 
1386  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1387  if( POLARSSL_MODE_CBC == cipher_info->mode )
1388  {
1389  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1390  }
1391  else
1392  {
1393  fct_chk( outlen == 0 );
1394  }
1395 
1396 
1397  /* decode the previously encoded string */
1398  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1399  if( POLARSSL_MODE_CBC == cipher_info->mode )
1400  {
1401  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1402  }
1403  else
1404  {
1405  fct_chk( enclen == outlen );
1406  }
1407 
1408  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1409  if( POLARSSL_MODE_CBC == cipher_info->mode )
1410  {
1411  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1412  }
1413  else
1414  {
1415  fct_chk( outlen == 0 );
1416  }
1417 
1418 
1419  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1420 
1421  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1422  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1423  FCT_TEST_END();
1424 #endif /* POLARSSL_AES_C */
1425 
1426 #ifdef POLARSSL_AES_C
1427 
1428  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes)
1429  size_t length = 33;
1430  unsigned char key[32];
1431  unsigned char iv[16];
1432 
1433  const cipher_info_t *cipher_info;
1434  cipher_context_t ctx_dec;
1435  cipher_context_t ctx_enc;
1436 
1437  unsigned char inbuf[64];
1438  unsigned char encbuf[64];
1439  unsigned char decbuf[64];
1440 
1441  size_t outlen = 0;
1442  size_t enclen = 0;
1443 
1444  memset( key, 0, 32 );
1445  memset( iv , 0, 16 );
1446 
1447  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1448  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1449 
1450  memset( inbuf, 5, 64 );
1451  memset( encbuf, 0, 64 );
1452  memset( decbuf, 0, 64 );
1453 
1454  /* Check and get info structures */
1456  fct_chk( NULL != cipher_info );
1457  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
1458 
1459  /* Initialise enc and dec contexts */
1460  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1461  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1462 
1463  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
1464  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
1465 
1466  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1467  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1468 
1469  if( POLARSSL_MODE_CBC == cipher_info->mode )
1470  {
1471  enclen = cipher_get_block_size( &ctx_enc )
1472  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1473  }
1474  else
1475  {
1476  enclen = length;
1477  }
1478 
1479  /* encode length number of bytes from inbuf */
1480  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1481  if( POLARSSL_MODE_CBC == cipher_info->mode )
1482  {
1483  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1484  }
1485  else
1486  {
1487  fct_chk( outlen == enclen );
1488  }
1489 
1490  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1491  if( POLARSSL_MODE_CBC == cipher_info->mode )
1492  {
1493  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1494  }
1495  else
1496  {
1497  fct_chk( outlen == 0 );
1498  }
1499 
1500 
1501  /* decode the previously encoded string */
1502  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1503  if( POLARSSL_MODE_CBC == cipher_info->mode )
1504  {
1505  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1506  }
1507  else
1508  {
1509  fct_chk( enclen == outlen );
1510  }
1511 
1512  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1513  if( POLARSSL_MODE_CBC == cipher_info->mode )
1514  {
1515  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1516  }
1517  else
1518  {
1519  fct_chk( outlen == 0 );
1520  }
1521 
1522 
1523  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1524 
1525  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1526  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1527  FCT_TEST_END();
1528 #endif /* POLARSSL_AES_C */
1529 
1530 #ifdef POLARSSL_AES_C
1531 
1532  FCT_TEST_BGN(aes_encrypt_and_decrypt_47_bytes)
1533  size_t length = 47;
1534  unsigned char key[32];
1535  unsigned char iv[16];
1536 
1537  const cipher_info_t *cipher_info;
1538  cipher_context_t ctx_dec;
1539  cipher_context_t ctx_enc;
1540 
1541  unsigned char inbuf[64];
1542  unsigned char encbuf[64];
1543  unsigned char decbuf[64];
1544 
1545  size_t outlen = 0;
1546  size_t enclen = 0;
1547 
1548  memset( key, 0, 32 );
1549  memset( iv , 0, 16 );
1550 
1551  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1552  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1553 
1554  memset( inbuf, 5, 64 );
1555  memset( encbuf, 0, 64 );
1556  memset( decbuf, 0, 64 );
1557 
1558  /* Check and get info structures */
1560  fct_chk( NULL != cipher_info );
1561  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
1562 
1563  /* Initialise enc and dec contexts */
1564  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1565  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1566 
1567  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
1568  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
1569 
1570  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1571  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1572 
1573  if( POLARSSL_MODE_CBC == cipher_info->mode )
1574  {
1575  enclen = cipher_get_block_size( &ctx_enc )
1576  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1577  }
1578  else
1579  {
1580  enclen = length;
1581  }
1582 
1583  /* encode length number of bytes from inbuf */
1584  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1585  if( POLARSSL_MODE_CBC == cipher_info->mode )
1586  {
1587  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1588  }
1589  else
1590  {
1591  fct_chk( outlen == enclen );
1592  }
1593 
1594  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1595  if( POLARSSL_MODE_CBC == cipher_info->mode )
1596  {
1597  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1598  }
1599  else
1600  {
1601  fct_chk( outlen == 0 );
1602  }
1603 
1604 
1605  /* decode the previously encoded string */
1606  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1607  if( POLARSSL_MODE_CBC == cipher_info->mode )
1608  {
1609  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1610  }
1611  else
1612  {
1613  fct_chk( enclen == outlen );
1614  }
1615 
1616  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1617  if( POLARSSL_MODE_CBC == cipher_info->mode )
1618  {
1619  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1620  }
1621  else
1622  {
1623  fct_chk( outlen == 0 );
1624  }
1625 
1626 
1627  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1628 
1629  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1630  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1631  FCT_TEST_END();
1632 #endif /* POLARSSL_AES_C */
1633 
1634 #ifdef POLARSSL_AES_C
1635 
1636  FCT_TEST_BGN(aes_encrypt_and_decrypt_48_bytes)
1637  size_t length = 48;
1638  unsigned char key[32];
1639  unsigned char iv[16];
1640 
1641  const cipher_info_t *cipher_info;
1642  cipher_context_t ctx_dec;
1643  cipher_context_t ctx_enc;
1644 
1645  unsigned char inbuf[64];
1646  unsigned char encbuf[64];
1647  unsigned char decbuf[64];
1648 
1649  size_t outlen = 0;
1650  size_t enclen = 0;
1651 
1652  memset( key, 0, 32 );
1653  memset( iv , 0, 16 );
1654 
1655  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1656  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1657 
1658  memset( inbuf, 5, 64 );
1659  memset( encbuf, 0, 64 );
1660  memset( decbuf, 0, 64 );
1661 
1662  /* Check and get info structures */
1664  fct_chk( NULL != cipher_info );
1665  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
1666 
1667  /* Initialise enc and dec contexts */
1668  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1669  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1670 
1671  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
1672  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
1673 
1674  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1675  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1676 
1677  if( POLARSSL_MODE_CBC == cipher_info->mode )
1678  {
1679  enclen = cipher_get_block_size( &ctx_enc )
1680  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1681  }
1682  else
1683  {
1684  enclen = length;
1685  }
1686 
1687  /* encode length number of bytes from inbuf */
1688  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1689  if( POLARSSL_MODE_CBC == cipher_info->mode )
1690  {
1691  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1692  }
1693  else
1694  {
1695  fct_chk( outlen == enclen );
1696  }
1697 
1698  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1699  if( POLARSSL_MODE_CBC == cipher_info->mode )
1700  {
1701  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1702  }
1703  else
1704  {
1705  fct_chk( outlen == 0 );
1706  }
1707 
1708 
1709  /* decode the previously encoded string */
1710  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1711  if( POLARSSL_MODE_CBC == cipher_info->mode )
1712  {
1713  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1714  }
1715  else
1716  {
1717  fct_chk( enclen == outlen );
1718  }
1719 
1720  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1721  if( POLARSSL_MODE_CBC == cipher_info->mode )
1722  {
1723  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1724  }
1725  else
1726  {
1727  fct_chk( outlen == 0 );
1728  }
1729 
1730 
1731  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1732 
1733  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1734  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1735  FCT_TEST_END();
1736 #endif /* POLARSSL_AES_C */
1737 
1738 #ifdef POLARSSL_AES_C
1739 
1740  FCT_TEST_BGN(aes_encrypt_and_decrypt_49_bytes)
1741  size_t length = 49;
1742  unsigned char key[32];
1743  unsigned char iv[16];
1744 
1745  const cipher_info_t *cipher_info;
1746  cipher_context_t ctx_dec;
1747  cipher_context_t ctx_enc;
1748 
1749  unsigned char inbuf[64];
1750  unsigned char encbuf[64];
1751  unsigned char decbuf[64];
1752 
1753  size_t outlen = 0;
1754  size_t enclen = 0;
1755 
1756  memset( key, 0, 32 );
1757  memset( iv , 0, 16 );
1758 
1759  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1760  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1761 
1762  memset( inbuf, 5, 64 );
1763  memset( encbuf, 0, 64 );
1764  memset( decbuf, 0, 64 );
1765 
1766  /* Check and get info structures */
1768  fct_chk( NULL != cipher_info );
1769  fct_chk( cipher_info_from_string( "AES-128-CBC" ) == cipher_info );
1770 
1771  /* Initialise enc and dec contexts */
1772  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1773  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1774 
1775  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
1776  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
1777 
1778  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1779  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1780 
1781  if( POLARSSL_MODE_CBC == cipher_info->mode )
1782  {
1783  enclen = cipher_get_block_size( &ctx_enc )
1784  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1785  }
1786  else
1787  {
1788  enclen = length;
1789  }
1790 
1791  /* encode length number of bytes from inbuf */
1792  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1793  if( POLARSSL_MODE_CBC == cipher_info->mode )
1794  {
1795  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1796  }
1797  else
1798  {
1799  fct_chk( outlen == enclen );
1800  }
1801 
1802  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1803  if( POLARSSL_MODE_CBC == cipher_info->mode )
1804  {
1805  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1806  }
1807  else
1808  {
1809  fct_chk( outlen == 0 );
1810  }
1811 
1812 
1813  /* decode the previously encoded string */
1814  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1815  if( POLARSSL_MODE_CBC == cipher_info->mode )
1816  {
1817  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1818  }
1819  else
1820  {
1821  fct_chk( enclen == outlen );
1822  }
1823 
1824  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1825  if( POLARSSL_MODE_CBC == cipher_info->mode )
1826  {
1827  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1828  }
1829  else
1830  {
1831  fct_chk( outlen == 0 );
1832  }
1833 
1834 
1835  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1836 
1837  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1838  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1839  FCT_TEST_END();
1840 #endif /* POLARSSL_AES_C */
1841 
1842 #ifdef POLARSSL_AES_C
1843 
1844  FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes_in_multiple_parts)
1845  size_t first_length = 0;
1846  size_t second_length = 0;
1847  size_t length = first_length + second_length;
1848  unsigned char key[32];
1849  unsigned char iv[16];
1850 
1851  cipher_context_t ctx_dec;
1852  cipher_context_t ctx_enc;
1853  const cipher_info_t *cipher_info;
1854 
1855  unsigned char inbuf[64];
1856  unsigned char encbuf[64];
1857  unsigned char decbuf[64];
1858 
1859  size_t outlen = 0;
1860  size_t totaloutlen = 0;
1861  size_t enclen = 0;
1862 
1863  memset( key, 0, 32 );
1864  memset( iv , 0, 16 );
1865 
1866  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1867  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1868 
1869  memset( inbuf, 5, 64 );
1870  memset( encbuf, 0, 64 );
1871  memset( decbuf, 0, 64 );
1872 
1873  /* Initialise enc and dec contexts */
1875  fct_chk( NULL != cipher_info);
1876 
1877  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1878  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1879 
1880  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
1881  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
1882 
1883  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1884  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1885 
1886  if( POLARSSL_MODE_CBC == cipher_info->mode )
1887  {
1888  enclen = cipher_get_block_size(&ctx_enc )
1889  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1890  }
1891  else
1892  {
1893  enclen = length;
1894  }
1895 
1896  /* encode length number of bytes from inbuf */
1897  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
1898  totaloutlen = outlen;
1899  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
1900  totaloutlen += outlen;
1901  if( POLARSSL_MODE_CBC == cipher_info->mode )
1902  {
1903  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1904  }
1905  else
1906  {
1907  fct_chk( totaloutlen == enclen );
1908  }
1909  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
1910  totaloutlen += outlen;
1911  if( POLARSSL_MODE_CBC == cipher_info->mode )
1912  {
1913  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1914  }
1915  else
1916  {
1917  fct_chk( outlen == 0 );
1918  }
1919 
1920  /* decode the previously encoded string */
1921  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1922  if( POLARSSL_MODE_CBC == cipher_info->mode )
1923  {
1924  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1925  }
1926  else
1927  {
1928  fct_chk( enclen == outlen );
1929  }
1930  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1931  if( POLARSSL_MODE_CBC == cipher_info->mode )
1932  {
1933  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1934  }
1935  else
1936  {
1937  fct_chk( outlen == 0 );
1938  }
1939 
1940 
1941  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1942 
1943  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1944  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1945  FCT_TEST_END();
1946 #endif /* POLARSSL_AES_C */
1947 
1948 #ifdef POLARSSL_AES_C
1949 
1950  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
1951  size_t first_length = 1;
1952  size_t second_length = 0;
1953  size_t length = first_length + second_length;
1954  unsigned char key[32];
1955  unsigned char iv[16];
1956 
1957  cipher_context_t ctx_dec;
1958  cipher_context_t ctx_enc;
1959  const cipher_info_t *cipher_info;
1960 
1961  unsigned char inbuf[64];
1962  unsigned char encbuf[64];
1963  unsigned char decbuf[64];
1964 
1965  size_t outlen = 0;
1966  size_t totaloutlen = 0;
1967  size_t enclen = 0;
1968 
1969  memset( key, 0, 32 );
1970  memset( iv , 0, 16 );
1971 
1972  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1973  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1974 
1975  memset( inbuf, 5, 64 );
1976  memset( encbuf, 0, 64 );
1977  memset( decbuf, 0, 64 );
1978 
1979  /* Initialise enc and dec contexts */
1981  fct_chk( NULL != cipher_info);
1982 
1983  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1984  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1985 
1986  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
1987  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
1988 
1989  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1990  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1991 
1992  if( POLARSSL_MODE_CBC == cipher_info->mode )
1993  {
1994  enclen = cipher_get_block_size(&ctx_enc )
1995  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1996  }
1997  else
1998  {
1999  enclen = length;
2000  }
2001 
2002  /* encode length number of bytes from inbuf */
2003  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2004  totaloutlen = outlen;
2005  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2006  totaloutlen += outlen;
2007  if( POLARSSL_MODE_CBC == cipher_info->mode )
2008  {
2009  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2010  }
2011  else
2012  {
2013  fct_chk( totaloutlen == enclen );
2014  }
2015  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2016  totaloutlen += outlen;
2017  if( POLARSSL_MODE_CBC == cipher_info->mode )
2018  {
2019  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2020  }
2021  else
2022  {
2023  fct_chk( outlen == 0 );
2024  }
2025 
2026  /* decode the previously encoded string */
2027  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2028  if( POLARSSL_MODE_CBC == cipher_info->mode )
2029  {
2030  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2031  }
2032  else
2033  {
2034  fct_chk( enclen == outlen );
2035  }
2036  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2037  if( POLARSSL_MODE_CBC == cipher_info->mode )
2038  {
2039  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2040  }
2041  else
2042  {
2043  fct_chk( outlen == 0 );
2044  }
2045 
2046 
2047  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2048 
2049  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2050  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2051  FCT_TEST_END();
2052 #endif /* POLARSSL_AES_C */
2053 
2054 #ifdef POLARSSL_AES_C
2055 
2056  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
2057  size_t first_length = 0;
2058  size_t second_length = 1;
2059  size_t length = first_length + second_length;
2060  unsigned char key[32];
2061  unsigned char iv[16];
2062 
2063  cipher_context_t ctx_dec;
2064  cipher_context_t ctx_enc;
2065  const cipher_info_t *cipher_info;
2066 
2067  unsigned char inbuf[64];
2068  unsigned char encbuf[64];
2069  unsigned char decbuf[64];
2070 
2071  size_t outlen = 0;
2072  size_t totaloutlen = 0;
2073  size_t enclen = 0;
2074 
2075  memset( key, 0, 32 );
2076  memset( iv , 0, 16 );
2077 
2078  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2079  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2080 
2081  memset( inbuf, 5, 64 );
2082  memset( encbuf, 0, 64 );
2083  memset( decbuf, 0, 64 );
2084 
2085  /* Initialise enc and dec contexts */
2087  fct_chk( NULL != cipher_info);
2088 
2089  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2090  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2091 
2092  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
2093  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
2094 
2095  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2096  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2097 
2098  if( POLARSSL_MODE_CBC == cipher_info->mode )
2099  {
2100  enclen = cipher_get_block_size(&ctx_enc )
2101  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2102  }
2103  else
2104  {
2105  enclen = length;
2106  }
2107 
2108  /* encode length number of bytes from inbuf */
2109  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2110  totaloutlen = outlen;
2111  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2112  totaloutlen += outlen;
2113  if( POLARSSL_MODE_CBC == cipher_info->mode )
2114  {
2115  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2116  }
2117  else
2118  {
2119  fct_chk( totaloutlen == enclen );
2120  }
2121  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2122  totaloutlen += outlen;
2123  if( POLARSSL_MODE_CBC == cipher_info->mode )
2124  {
2125  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2126  }
2127  else
2128  {
2129  fct_chk( outlen == 0 );
2130  }
2131 
2132  /* decode the previously encoded string */
2133  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2134  if( POLARSSL_MODE_CBC == cipher_info->mode )
2135  {
2136  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2137  }
2138  else
2139  {
2140  fct_chk( enclen == outlen );
2141  }
2142  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2143  if( POLARSSL_MODE_CBC == cipher_info->mode )
2144  {
2145  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2146  }
2147  else
2148  {
2149  fct_chk( outlen == 0 );
2150  }
2151 
2152 
2153  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2154 
2155  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2156  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2157  FCT_TEST_END();
2158 #endif /* POLARSSL_AES_C */
2159 
2160 #ifdef POLARSSL_AES_C
2161 
2162  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
2163  size_t first_length = 16;
2164  size_t second_length = 0;
2165  size_t length = first_length + second_length;
2166  unsigned char key[32];
2167  unsigned char iv[16];
2168 
2169  cipher_context_t ctx_dec;
2170  cipher_context_t ctx_enc;
2171  const cipher_info_t *cipher_info;
2172 
2173  unsigned char inbuf[64];
2174  unsigned char encbuf[64];
2175  unsigned char decbuf[64];
2176 
2177  size_t outlen = 0;
2178  size_t totaloutlen = 0;
2179  size_t enclen = 0;
2180 
2181  memset( key, 0, 32 );
2182  memset( iv , 0, 16 );
2183 
2184  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2185  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2186 
2187  memset( inbuf, 5, 64 );
2188  memset( encbuf, 0, 64 );
2189  memset( decbuf, 0, 64 );
2190 
2191  /* Initialise enc and dec contexts */
2193  fct_chk( NULL != cipher_info);
2194 
2195  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2196  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2197 
2198  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
2199  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
2200 
2201  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2202  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2203 
2204  if( POLARSSL_MODE_CBC == cipher_info->mode )
2205  {
2206  enclen = cipher_get_block_size(&ctx_enc )
2207  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2208  }
2209  else
2210  {
2211  enclen = length;
2212  }
2213 
2214  /* encode length number of bytes from inbuf */
2215  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2216  totaloutlen = outlen;
2217  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2218  totaloutlen += outlen;
2219  if( POLARSSL_MODE_CBC == cipher_info->mode )
2220  {
2221  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2222  }
2223  else
2224  {
2225  fct_chk( totaloutlen == enclen );
2226  }
2227  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2228  totaloutlen += outlen;
2229  if( POLARSSL_MODE_CBC == cipher_info->mode )
2230  {
2231  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2232  }
2233  else
2234  {
2235  fct_chk( outlen == 0 );
2236  }
2237 
2238  /* decode the previously encoded string */
2239  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2240  if( POLARSSL_MODE_CBC == cipher_info->mode )
2241  {
2242  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2243  }
2244  else
2245  {
2246  fct_chk( enclen == outlen );
2247  }
2248  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2249  if( POLARSSL_MODE_CBC == cipher_info->mode )
2250  {
2251  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2252  }
2253  else
2254  {
2255  fct_chk( outlen == 0 );
2256  }
2257 
2258 
2259  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2260 
2261  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2262  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2263  FCT_TEST_END();
2264 #endif /* POLARSSL_AES_C */
2265 
2266 #ifdef POLARSSL_AES_C
2267 
2268  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
2269  size_t first_length = 0;
2270  size_t second_length = 16;
2271  size_t length = first_length + second_length;
2272  unsigned char key[32];
2273  unsigned char iv[16];
2274 
2275  cipher_context_t ctx_dec;
2276  cipher_context_t ctx_enc;
2277  const cipher_info_t *cipher_info;
2278 
2279  unsigned char inbuf[64];
2280  unsigned char encbuf[64];
2281  unsigned char decbuf[64];
2282 
2283  size_t outlen = 0;
2284  size_t totaloutlen = 0;
2285  size_t enclen = 0;
2286 
2287  memset( key, 0, 32 );
2288  memset( iv , 0, 16 );
2289 
2290  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2291  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2292 
2293  memset( inbuf, 5, 64 );
2294  memset( encbuf, 0, 64 );
2295  memset( decbuf, 0, 64 );
2296 
2297  /* Initialise enc and dec contexts */
2299  fct_chk( NULL != cipher_info);
2300 
2301  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2302  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2303 
2304  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
2305  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
2306 
2307  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2308  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2309 
2310  if( POLARSSL_MODE_CBC == cipher_info->mode )
2311  {
2312  enclen = cipher_get_block_size(&ctx_enc )
2313  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2314  }
2315  else
2316  {
2317  enclen = length;
2318  }
2319 
2320  /* encode length number of bytes from inbuf */
2321  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2322  totaloutlen = outlen;
2323  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2324  totaloutlen += outlen;
2325  if( POLARSSL_MODE_CBC == cipher_info->mode )
2326  {
2327  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2328  }
2329  else
2330  {
2331  fct_chk( totaloutlen == enclen );
2332  }
2333  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2334  totaloutlen += outlen;
2335  if( POLARSSL_MODE_CBC == cipher_info->mode )
2336  {
2337  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2338  }
2339  else
2340  {
2341  fct_chk( outlen == 0 );
2342  }
2343 
2344  /* decode the previously encoded string */
2345  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2346  if( POLARSSL_MODE_CBC == cipher_info->mode )
2347  {
2348  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2349  }
2350  else
2351  {
2352  fct_chk( enclen == outlen );
2353  }
2354  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2355  if( POLARSSL_MODE_CBC == cipher_info->mode )
2356  {
2357  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2358  }
2359  else
2360  {
2361  fct_chk( outlen == 0 );
2362  }
2363 
2364 
2365  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2366 
2367  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2368  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2369  FCT_TEST_END();
2370 #endif /* POLARSSL_AES_C */
2371 
2372 #ifdef POLARSSL_AES_C
2373 
2374  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
2375  size_t first_length = 1;
2376  size_t second_length = 15;
2377  size_t length = first_length + second_length;
2378  unsigned char key[32];
2379  unsigned char iv[16];
2380 
2381  cipher_context_t ctx_dec;
2382  cipher_context_t ctx_enc;
2383  const cipher_info_t *cipher_info;
2384 
2385  unsigned char inbuf[64];
2386  unsigned char encbuf[64];
2387  unsigned char decbuf[64];
2388 
2389  size_t outlen = 0;
2390  size_t totaloutlen = 0;
2391  size_t enclen = 0;
2392 
2393  memset( key, 0, 32 );
2394  memset( iv , 0, 16 );
2395 
2396  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2397  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2398 
2399  memset( inbuf, 5, 64 );
2400  memset( encbuf, 0, 64 );
2401  memset( decbuf, 0, 64 );
2402 
2403  /* Initialise enc and dec contexts */
2405  fct_chk( NULL != cipher_info);
2406 
2407  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2408  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2409 
2410  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
2411  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
2412 
2413  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2414  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2415 
2416  if( POLARSSL_MODE_CBC == cipher_info->mode )
2417  {
2418  enclen = cipher_get_block_size(&ctx_enc )
2419  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2420  }
2421  else
2422  {
2423  enclen = length;
2424  }
2425 
2426  /* encode length number of bytes from inbuf */
2427  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2428  totaloutlen = outlen;
2429  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2430  totaloutlen += outlen;
2431  if( POLARSSL_MODE_CBC == cipher_info->mode )
2432  {
2433  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2434  }
2435  else
2436  {
2437  fct_chk( totaloutlen == enclen );
2438  }
2439  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2440  totaloutlen += outlen;
2441  if( POLARSSL_MODE_CBC == cipher_info->mode )
2442  {
2443  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2444  }
2445  else
2446  {
2447  fct_chk( outlen == 0 );
2448  }
2449 
2450  /* decode the previously encoded string */
2451  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2452  if( POLARSSL_MODE_CBC == cipher_info->mode )
2453  {
2454  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2455  }
2456  else
2457  {
2458  fct_chk( enclen == outlen );
2459  }
2460  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2461  if( POLARSSL_MODE_CBC == cipher_info->mode )
2462  {
2463  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2464  }
2465  else
2466  {
2467  fct_chk( outlen == 0 );
2468  }
2469 
2470 
2471  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2472 
2473  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2474  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2475  FCT_TEST_END();
2476 #endif /* POLARSSL_AES_C */
2477 
2478 #ifdef POLARSSL_AES_C
2479 
2480  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
2481  size_t first_length = 15;
2482  size_t second_length = 1;
2483  size_t length = first_length + second_length;
2484  unsigned char key[32];
2485  unsigned char iv[16];
2486 
2487  cipher_context_t ctx_dec;
2488  cipher_context_t ctx_enc;
2489  const cipher_info_t *cipher_info;
2490 
2491  unsigned char inbuf[64];
2492  unsigned char encbuf[64];
2493  unsigned char decbuf[64];
2494 
2495  size_t outlen = 0;
2496  size_t totaloutlen = 0;
2497  size_t enclen = 0;
2498 
2499  memset( key, 0, 32 );
2500  memset( iv , 0, 16 );
2501 
2502  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2503  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2504 
2505  memset( inbuf, 5, 64 );
2506  memset( encbuf, 0, 64 );
2507  memset( decbuf, 0, 64 );
2508 
2509  /* Initialise enc and dec contexts */
2511  fct_chk( NULL != cipher_info);
2512 
2513  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2514  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2515 
2516  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
2517  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
2518 
2519  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2520  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2521 
2522  if( POLARSSL_MODE_CBC == cipher_info->mode )
2523  {
2524  enclen = cipher_get_block_size(&ctx_enc )
2525  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2526  }
2527  else
2528  {
2529  enclen = length;
2530  }
2531 
2532  /* encode length number of bytes from inbuf */
2533  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2534  totaloutlen = outlen;
2535  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2536  totaloutlen += outlen;
2537  if( POLARSSL_MODE_CBC == cipher_info->mode )
2538  {
2539  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2540  }
2541  else
2542  {
2543  fct_chk( totaloutlen == enclen );
2544  }
2545  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2546  totaloutlen += outlen;
2547  if( POLARSSL_MODE_CBC == cipher_info->mode )
2548  {
2549  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2550  }
2551  else
2552  {
2553  fct_chk( outlen == 0 );
2554  }
2555 
2556  /* decode the previously encoded string */
2557  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2558  if( POLARSSL_MODE_CBC == cipher_info->mode )
2559  {
2560  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2561  }
2562  else
2563  {
2564  fct_chk( enclen == outlen );
2565  }
2566  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2567  if( POLARSSL_MODE_CBC == cipher_info->mode )
2568  {
2569  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2570  }
2571  else
2572  {
2573  fct_chk( outlen == 0 );
2574  }
2575 
2576 
2577  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2578 
2579  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2580  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2581  FCT_TEST_END();
2582 #endif /* POLARSSL_AES_C */
2583 
2584 #ifdef POLARSSL_AES_C
2585 
2586  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2587  size_t first_length = 15;
2588  size_t second_length = 7;
2589  size_t length = first_length + second_length;
2590  unsigned char key[32];
2591  unsigned char iv[16];
2592 
2593  cipher_context_t ctx_dec;
2594  cipher_context_t ctx_enc;
2595  const cipher_info_t *cipher_info;
2596 
2597  unsigned char inbuf[64];
2598  unsigned char encbuf[64];
2599  unsigned char decbuf[64];
2600 
2601  size_t outlen = 0;
2602  size_t totaloutlen = 0;
2603  size_t enclen = 0;
2604 
2605  memset( key, 0, 32 );
2606  memset( iv , 0, 16 );
2607 
2608  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2609  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2610 
2611  memset( inbuf, 5, 64 );
2612  memset( encbuf, 0, 64 );
2613  memset( decbuf, 0, 64 );
2614 
2615  /* Initialise enc and dec contexts */
2617  fct_chk( NULL != cipher_info);
2618 
2619  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2620  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2621 
2622  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
2623  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
2624 
2625  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2626  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2627 
2628  if( POLARSSL_MODE_CBC == cipher_info->mode )
2629  {
2630  enclen = cipher_get_block_size(&ctx_enc )
2631  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2632  }
2633  else
2634  {
2635  enclen = length;
2636  }
2637 
2638  /* encode length number of bytes from inbuf */
2639  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2640  totaloutlen = outlen;
2641  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2642  totaloutlen += outlen;
2643  if( POLARSSL_MODE_CBC == cipher_info->mode )
2644  {
2645  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2646  }
2647  else
2648  {
2649  fct_chk( totaloutlen == enclen );
2650  }
2651  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2652  totaloutlen += outlen;
2653  if( POLARSSL_MODE_CBC == cipher_info->mode )
2654  {
2655  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2656  }
2657  else
2658  {
2659  fct_chk( outlen == 0 );
2660  }
2661 
2662  /* decode the previously encoded string */
2663  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2664  if( POLARSSL_MODE_CBC == cipher_info->mode )
2665  {
2666  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2667  }
2668  else
2669  {
2670  fct_chk( enclen == outlen );
2671  }
2672  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2673  if( POLARSSL_MODE_CBC == cipher_info->mode )
2674  {
2675  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2676  }
2677  else
2678  {
2679  fct_chk( outlen == 0 );
2680  }
2681 
2682 
2683  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2684 
2685  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2686  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2687  FCT_TEST_END();
2688 #endif /* POLARSSL_AES_C */
2689 
2690 #ifdef POLARSSL_AES_C
2691 
2692  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2693  size_t first_length = 16;
2694  size_t second_length = 6;
2695  size_t length = first_length + second_length;
2696  unsigned char key[32];
2697  unsigned char iv[16];
2698 
2699  cipher_context_t ctx_dec;
2700  cipher_context_t ctx_enc;
2701  const cipher_info_t *cipher_info;
2702 
2703  unsigned char inbuf[64];
2704  unsigned char encbuf[64];
2705  unsigned char decbuf[64];
2706 
2707  size_t outlen = 0;
2708  size_t totaloutlen = 0;
2709  size_t enclen = 0;
2710 
2711  memset( key, 0, 32 );
2712  memset( iv , 0, 16 );
2713 
2714  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2715  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2716 
2717  memset( inbuf, 5, 64 );
2718  memset( encbuf, 0, 64 );
2719  memset( decbuf, 0, 64 );
2720 
2721  /* Initialise enc and dec contexts */
2723  fct_chk( NULL != cipher_info);
2724 
2725  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2726  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2727 
2728  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
2729  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
2730 
2731  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2732  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2733 
2734  if( POLARSSL_MODE_CBC == cipher_info->mode )
2735  {
2736  enclen = cipher_get_block_size(&ctx_enc )
2737  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2738  }
2739  else
2740  {
2741  enclen = length;
2742  }
2743 
2744  /* encode length number of bytes from inbuf */
2745  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2746  totaloutlen = outlen;
2747  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2748  totaloutlen += outlen;
2749  if( POLARSSL_MODE_CBC == cipher_info->mode )
2750  {
2751  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2752  }
2753  else
2754  {
2755  fct_chk( totaloutlen == enclen );
2756  }
2757  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2758  totaloutlen += outlen;
2759  if( POLARSSL_MODE_CBC == cipher_info->mode )
2760  {
2761  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2762  }
2763  else
2764  {
2765  fct_chk( outlen == 0 );
2766  }
2767 
2768  /* decode the previously encoded string */
2769  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2770  if( POLARSSL_MODE_CBC == cipher_info->mode )
2771  {
2772  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2773  }
2774  else
2775  {
2776  fct_chk( enclen == outlen );
2777  }
2778  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2779  if( POLARSSL_MODE_CBC == cipher_info->mode )
2780  {
2781  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2782  }
2783  else
2784  {
2785  fct_chk( outlen == 0 );
2786  }
2787 
2788 
2789  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2790 
2791  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2792  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2793  FCT_TEST_END();
2794 #endif /* POLARSSL_AES_C */
2795 
2796 #ifdef POLARSSL_AES_C
2797 
2798  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2799  size_t first_length = 17;
2800  size_t second_length = 6;
2801  size_t length = first_length + second_length;
2802  unsigned char key[32];
2803  unsigned char iv[16];
2804 
2805  cipher_context_t ctx_dec;
2806  cipher_context_t ctx_enc;
2807  const cipher_info_t *cipher_info;
2808 
2809  unsigned char inbuf[64];
2810  unsigned char encbuf[64];
2811  unsigned char decbuf[64];
2812 
2813  size_t outlen = 0;
2814  size_t totaloutlen = 0;
2815  size_t enclen = 0;
2816 
2817  memset( key, 0, 32 );
2818  memset( iv , 0, 16 );
2819 
2820  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2821  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2822 
2823  memset( inbuf, 5, 64 );
2824  memset( encbuf, 0, 64 );
2825  memset( decbuf, 0, 64 );
2826 
2827  /* Initialise enc and dec contexts */
2829  fct_chk( NULL != cipher_info);
2830 
2831  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2832  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2833 
2834  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
2835  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
2836 
2837  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2838  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2839 
2840  if( POLARSSL_MODE_CBC == cipher_info->mode )
2841  {
2842  enclen = cipher_get_block_size(&ctx_enc )
2843  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2844  }
2845  else
2846  {
2847  enclen = length;
2848  }
2849 
2850  /* encode length number of bytes from inbuf */
2851  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2852  totaloutlen = outlen;
2853  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2854  totaloutlen += outlen;
2855  if( POLARSSL_MODE_CBC == cipher_info->mode )
2856  {
2857  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2858  }
2859  else
2860  {
2861  fct_chk( totaloutlen == enclen );
2862  }
2863  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2864  totaloutlen += outlen;
2865  if( POLARSSL_MODE_CBC == cipher_info->mode )
2866  {
2867  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2868  }
2869  else
2870  {
2871  fct_chk( outlen == 0 );
2872  }
2873 
2874  /* decode the previously encoded string */
2875  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2876  if( POLARSSL_MODE_CBC == cipher_info->mode )
2877  {
2878  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2879  }
2880  else
2881  {
2882  fct_chk( enclen == outlen );
2883  }
2884  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2885  if( POLARSSL_MODE_CBC == cipher_info->mode )
2886  {
2887  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2888  }
2889  else
2890  {
2891  fct_chk( outlen == 0 );
2892  }
2893 
2894 
2895  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2896 
2897  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2898  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2899  FCT_TEST_END();
2900 #endif /* POLARSSL_AES_C */
2901 
2902 #ifdef POLARSSL_AES_C
2903 
2904  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
2905  size_t first_length = 16;
2906  size_t second_length = 16;
2907  size_t length = first_length + second_length;
2908  unsigned char key[32];
2909  unsigned char iv[16];
2910 
2911  cipher_context_t ctx_dec;
2912  cipher_context_t ctx_enc;
2913  const cipher_info_t *cipher_info;
2914 
2915  unsigned char inbuf[64];
2916  unsigned char encbuf[64];
2917  unsigned char decbuf[64];
2918 
2919  size_t outlen = 0;
2920  size_t totaloutlen = 0;
2921  size_t enclen = 0;
2922 
2923  memset( key, 0, 32 );
2924  memset( iv , 0, 16 );
2925 
2926  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2927  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2928 
2929  memset( inbuf, 5, 64 );
2930  memset( encbuf, 0, 64 );
2931  memset( decbuf, 0, 64 );
2932 
2933  /* Initialise enc and dec contexts */
2935  fct_chk( NULL != cipher_info);
2936 
2937  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2938  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2939 
2940  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
2941  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
2942 
2943  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2944  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2945 
2946  if( POLARSSL_MODE_CBC == cipher_info->mode )
2947  {
2948  enclen = cipher_get_block_size(&ctx_enc )
2949  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2950  }
2951  else
2952  {
2953  enclen = length;
2954  }
2955 
2956  /* encode length number of bytes from inbuf */
2957  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2958  totaloutlen = outlen;
2959  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2960  totaloutlen += outlen;
2961  if( POLARSSL_MODE_CBC == cipher_info->mode )
2962  {
2963  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2964  }
2965  else
2966  {
2967  fct_chk( totaloutlen == enclen );
2968  }
2969  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2970  totaloutlen += outlen;
2971  if( POLARSSL_MODE_CBC == cipher_info->mode )
2972  {
2973  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2974  }
2975  else
2976  {
2977  fct_chk( outlen == 0 );
2978  }
2979 
2980  /* decode the previously encoded string */
2981  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2982  if( POLARSSL_MODE_CBC == cipher_info->mode )
2983  {
2984  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2985  }
2986  else
2987  {
2988  fct_chk( enclen == outlen );
2989  }
2990  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2991  if( POLARSSL_MODE_CBC == cipher_info->mode )
2992  {
2993  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2994  }
2995  else
2996  {
2997  fct_chk( outlen == 0 );
2998  }
2999 
3000 
3001  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3002 
3003  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3004  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3005  FCT_TEST_END();
3006 #endif /* POLARSSL_AES_C */
3007 
3008 #ifdef POLARSSL_AES_C
3009 #ifdef POLARSSL_CIPHER_MODE_CFB
3010 
3011  FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes)
3012  size_t length = 0;
3013  unsigned char key[32];
3014  unsigned char iv[16];
3015 
3016  const cipher_info_t *cipher_info;
3017  cipher_context_t ctx_dec;
3018  cipher_context_t ctx_enc;
3019 
3020  unsigned char inbuf[64];
3021  unsigned char encbuf[64];
3022  unsigned char decbuf[64];
3023 
3024  size_t outlen = 0;
3025  size_t enclen = 0;
3026 
3027  memset( key, 0, 32 );
3028  memset( iv , 0, 16 );
3029 
3030  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3031  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3032 
3033  memset( inbuf, 5, 64 );
3034  memset( encbuf, 0, 64 );
3035  memset( decbuf, 0, 64 );
3036 
3037  /* Check and get info structures */
3039  fct_chk( NULL != cipher_info );
3040  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
3041 
3042  /* Initialise enc and dec contexts */
3043  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3044  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3045 
3046  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
3047  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
3048 
3049  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3050  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3051 
3052  if( POLARSSL_MODE_CBC == cipher_info->mode )
3053  {
3054  enclen = cipher_get_block_size( &ctx_enc )
3055  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3056  }
3057  else
3058  {
3059  enclen = length;
3060  }
3061 
3062  /* encode length number of bytes from inbuf */
3063  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3064  if( POLARSSL_MODE_CBC == cipher_info->mode )
3065  {
3066  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3067  }
3068  else
3069  {
3070  fct_chk( outlen == enclen );
3071  }
3072 
3073  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3074  if( POLARSSL_MODE_CBC == cipher_info->mode )
3075  {
3076  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3077  }
3078  else
3079  {
3080  fct_chk( outlen == 0 );
3081  }
3082 
3083 
3084  /* decode the previously encoded string */
3085  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3086  if( POLARSSL_MODE_CBC == cipher_info->mode )
3087  {
3088  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3089  }
3090  else
3091  {
3092  fct_chk( enclen == outlen );
3093  }
3094 
3095  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3096  if( POLARSSL_MODE_CBC == cipher_info->mode )
3097  {
3098  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3099  }
3100  else
3101  {
3102  fct_chk( outlen == 0 );
3103  }
3104 
3105 
3106  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3107 
3108  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3109  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3110  FCT_TEST_END();
3111 #endif /* POLARSSL_AES_C */
3112 #endif /* POLARSSL_CIPHER_MODE_CFB */
3113 
3114 #ifdef POLARSSL_AES_C
3115 #ifdef POLARSSL_CIPHER_MODE_CFB
3116 
3117  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_byte)
3118  size_t length = 1;
3119  unsigned char key[32];
3120  unsigned char iv[16];
3121 
3122  const cipher_info_t *cipher_info;
3123  cipher_context_t ctx_dec;
3124  cipher_context_t ctx_enc;
3125 
3126  unsigned char inbuf[64];
3127  unsigned char encbuf[64];
3128  unsigned char decbuf[64];
3129 
3130  size_t outlen = 0;
3131  size_t enclen = 0;
3132 
3133  memset( key, 0, 32 );
3134  memset( iv , 0, 16 );
3135 
3136  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3137  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3138 
3139  memset( inbuf, 5, 64 );
3140  memset( encbuf, 0, 64 );
3141  memset( decbuf, 0, 64 );
3142 
3143  /* Check and get info structures */
3145  fct_chk( NULL != cipher_info );
3146  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
3147 
3148  /* Initialise enc and dec contexts */
3149  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3150  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3151 
3152  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
3153  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
3154 
3155  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3156  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3157 
3158  if( POLARSSL_MODE_CBC == cipher_info->mode )
3159  {
3160  enclen = cipher_get_block_size( &ctx_enc )
3161  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3162  }
3163  else
3164  {
3165  enclen = length;
3166  }
3167 
3168  /* encode length number of bytes from inbuf */
3169  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3170  if( POLARSSL_MODE_CBC == cipher_info->mode )
3171  {
3172  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3173  }
3174  else
3175  {
3176  fct_chk( outlen == enclen );
3177  }
3178 
3179  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3180  if( POLARSSL_MODE_CBC == cipher_info->mode )
3181  {
3182  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3183  }
3184  else
3185  {
3186  fct_chk( outlen == 0 );
3187  }
3188 
3189 
3190  /* decode the previously encoded string */
3191  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3192  if( POLARSSL_MODE_CBC == cipher_info->mode )
3193  {
3194  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3195  }
3196  else
3197  {
3198  fct_chk( enclen == outlen );
3199  }
3200 
3201  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3202  if( POLARSSL_MODE_CBC == cipher_info->mode )
3203  {
3204  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3205  }
3206  else
3207  {
3208  fct_chk( outlen == 0 );
3209  }
3210 
3211 
3212  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3213 
3214  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3215  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3216  FCT_TEST_END();
3217 #endif /* POLARSSL_AES_C */
3218 #endif /* POLARSSL_CIPHER_MODE_CFB */
3219 
3220 #ifdef POLARSSL_AES_C
3221 #ifdef POLARSSL_CIPHER_MODE_CFB
3222 
3223  FCT_TEST_BGN(aes_encrypt_and_decrypt_2_bytes)
3224  size_t length = 2;
3225  unsigned char key[32];
3226  unsigned char iv[16];
3227 
3228  const cipher_info_t *cipher_info;
3229  cipher_context_t ctx_dec;
3230  cipher_context_t ctx_enc;
3231 
3232  unsigned char inbuf[64];
3233  unsigned char encbuf[64];
3234  unsigned char decbuf[64];
3235 
3236  size_t outlen = 0;
3237  size_t enclen = 0;
3238 
3239  memset( key, 0, 32 );
3240  memset( iv , 0, 16 );
3241 
3242  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3243  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3244 
3245  memset( inbuf, 5, 64 );
3246  memset( encbuf, 0, 64 );
3247  memset( decbuf, 0, 64 );
3248 
3249  /* Check and get info structures */
3251  fct_chk( NULL != cipher_info );
3252  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
3253 
3254  /* Initialise enc and dec contexts */
3255  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3256  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3257 
3258  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
3259  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
3260 
3261  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3262  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3263 
3264  if( POLARSSL_MODE_CBC == cipher_info->mode )
3265  {
3266  enclen = cipher_get_block_size( &ctx_enc )
3267  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3268  }
3269  else
3270  {
3271  enclen = length;
3272  }
3273 
3274  /* encode length number of bytes from inbuf */
3275  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3276  if( POLARSSL_MODE_CBC == cipher_info->mode )
3277  {
3278  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3279  }
3280  else
3281  {
3282  fct_chk( outlen == enclen );
3283  }
3284 
3285  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3286  if( POLARSSL_MODE_CBC == cipher_info->mode )
3287  {
3288  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3289  }
3290  else
3291  {
3292  fct_chk( outlen == 0 );
3293  }
3294 
3295 
3296  /* decode the previously encoded string */
3297  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3298  if( POLARSSL_MODE_CBC == cipher_info->mode )
3299  {
3300  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3301  }
3302  else
3303  {
3304  fct_chk( enclen == outlen );
3305  }
3306 
3307  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3308  if( POLARSSL_MODE_CBC == cipher_info->mode )
3309  {
3310  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3311  }
3312  else
3313  {
3314  fct_chk( outlen == 0 );
3315  }
3316 
3317 
3318  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3319 
3320  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3321  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3322  FCT_TEST_END();
3323 #endif /* POLARSSL_AES_C */
3324 #endif /* POLARSSL_CIPHER_MODE_CFB */
3325 
3326 #ifdef POLARSSL_AES_C
3327 #ifdef POLARSSL_CIPHER_MODE_CFB
3328 
3329  FCT_TEST_BGN(aes_encrypt_and_decrypt_7_bytes)
3330  size_t length = 7;
3331  unsigned char key[32];
3332  unsigned char iv[16];
3333 
3334  const cipher_info_t *cipher_info;
3335  cipher_context_t ctx_dec;
3336  cipher_context_t ctx_enc;
3337 
3338  unsigned char inbuf[64];
3339  unsigned char encbuf[64];
3340  unsigned char decbuf[64];
3341 
3342  size_t outlen = 0;
3343  size_t enclen = 0;
3344 
3345  memset( key, 0, 32 );
3346  memset( iv , 0, 16 );
3347 
3348  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3349  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3350 
3351  memset( inbuf, 5, 64 );
3352  memset( encbuf, 0, 64 );
3353  memset( decbuf, 0, 64 );
3354 
3355  /* Check and get info structures */
3357  fct_chk( NULL != cipher_info );
3358  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
3359 
3360  /* Initialise enc and dec contexts */
3361  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3362  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3363 
3364  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
3365  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
3366 
3367  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3368  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3369 
3370  if( POLARSSL_MODE_CBC == cipher_info->mode )
3371  {
3372  enclen = cipher_get_block_size( &ctx_enc )
3373  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3374  }
3375  else
3376  {
3377  enclen = length;
3378  }
3379 
3380  /* encode length number of bytes from inbuf */
3381  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3382  if( POLARSSL_MODE_CBC == cipher_info->mode )
3383  {
3384  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3385  }
3386  else
3387  {
3388  fct_chk( outlen == enclen );
3389  }
3390 
3391  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3392  if( POLARSSL_MODE_CBC == cipher_info->mode )
3393  {
3394  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3395  }
3396  else
3397  {
3398  fct_chk( outlen == 0 );
3399  }
3400 
3401 
3402  /* decode the previously encoded string */
3403  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3404  if( POLARSSL_MODE_CBC == cipher_info->mode )
3405  {
3406  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3407  }
3408  else
3409  {
3410  fct_chk( enclen == outlen );
3411  }
3412 
3413  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3414  if( POLARSSL_MODE_CBC == cipher_info->mode )
3415  {
3416  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3417  }
3418  else
3419  {
3420  fct_chk( outlen == 0 );
3421  }
3422 
3423 
3424  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3425 
3426  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3427  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3428  FCT_TEST_END();
3429 #endif /* POLARSSL_AES_C */
3430 #endif /* POLARSSL_CIPHER_MODE_CFB */
3431 
3432 #ifdef POLARSSL_AES_C
3433 #ifdef POLARSSL_CIPHER_MODE_CFB
3434 
3435  FCT_TEST_BGN(aes_encrypt_and_decrypt_8_bytes)
3436  size_t length = 8;
3437  unsigned char key[32];
3438  unsigned char iv[16];
3439 
3440  const cipher_info_t *cipher_info;
3441  cipher_context_t ctx_dec;
3442  cipher_context_t ctx_enc;
3443 
3444  unsigned char inbuf[64];
3445  unsigned char encbuf[64];
3446  unsigned char decbuf[64];
3447 
3448  size_t outlen = 0;
3449  size_t enclen = 0;
3450 
3451  memset( key, 0, 32 );
3452  memset( iv , 0, 16 );
3453 
3454  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3455  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3456 
3457  memset( inbuf, 5, 64 );
3458  memset( encbuf, 0, 64 );
3459  memset( decbuf, 0, 64 );
3460 
3461  /* Check and get info structures */
3463  fct_chk( NULL != cipher_info );
3464  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
3465 
3466  /* Initialise enc and dec contexts */
3467  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3468  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3469 
3470  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
3471  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
3472 
3473  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3474  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3475 
3476  if( POLARSSL_MODE_CBC == cipher_info->mode )
3477  {
3478  enclen = cipher_get_block_size( &ctx_enc )
3479  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3480  }
3481  else
3482  {
3483  enclen = length;
3484  }
3485 
3486  /* encode length number of bytes from inbuf */
3487  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3488  if( POLARSSL_MODE_CBC == cipher_info->mode )
3489  {
3490  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3491  }
3492  else
3493  {
3494  fct_chk( outlen == enclen );
3495  }
3496 
3497  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3498  if( POLARSSL_MODE_CBC == cipher_info->mode )
3499  {
3500  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3501  }
3502  else
3503  {
3504  fct_chk( outlen == 0 );
3505  }
3506 
3507 
3508  /* decode the previously encoded string */
3509  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3510  if( POLARSSL_MODE_CBC == cipher_info->mode )
3511  {
3512  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3513  }
3514  else
3515  {
3516  fct_chk( enclen == outlen );
3517  }
3518 
3519  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3520  if( POLARSSL_MODE_CBC == cipher_info->mode )
3521  {
3522  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3523  }
3524  else
3525  {
3526  fct_chk( outlen == 0 );
3527  }
3528 
3529 
3530  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3531 
3532  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3533  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3534  FCT_TEST_END();
3535 #endif /* POLARSSL_AES_C */
3536 #endif /* POLARSSL_CIPHER_MODE_CFB */
3537 
3538 #ifdef POLARSSL_AES_C
3539 #ifdef POLARSSL_CIPHER_MODE_CFB
3540 
3541  FCT_TEST_BGN(aes_encrypt_and_decrypt_9_bytes)
3542  size_t length = 9;
3543  unsigned char key[32];
3544  unsigned char iv[16];
3545 
3546  const cipher_info_t *cipher_info;
3547  cipher_context_t ctx_dec;
3548  cipher_context_t ctx_enc;
3549 
3550  unsigned char inbuf[64];
3551  unsigned char encbuf[64];
3552  unsigned char decbuf[64];
3553 
3554  size_t outlen = 0;
3555  size_t enclen = 0;
3556 
3557  memset( key, 0, 32 );
3558  memset( iv , 0, 16 );
3559 
3560  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3561  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3562 
3563  memset( inbuf, 5, 64 );
3564  memset( encbuf, 0, 64 );
3565  memset( decbuf, 0, 64 );
3566 
3567  /* Check and get info structures */
3569  fct_chk( NULL != cipher_info );
3570  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
3571 
3572  /* Initialise enc and dec contexts */
3573  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3574  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3575 
3576  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
3577  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
3578 
3579  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3580  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3581 
3582  if( POLARSSL_MODE_CBC == cipher_info->mode )
3583  {
3584  enclen = cipher_get_block_size( &ctx_enc )
3585  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3586  }
3587  else
3588  {
3589  enclen = length;
3590  }
3591 
3592  /* encode length number of bytes from inbuf */
3593  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3594  if( POLARSSL_MODE_CBC == cipher_info->mode )
3595  {
3596  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3597  }
3598  else
3599  {
3600  fct_chk( outlen == enclen );
3601  }
3602 
3603  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3604  if( POLARSSL_MODE_CBC == cipher_info->mode )
3605  {
3606  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3607  }
3608  else
3609  {
3610  fct_chk( outlen == 0 );
3611  }
3612 
3613 
3614  /* decode the previously encoded string */
3615  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3616  if( POLARSSL_MODE_CBC == cipher_info->mode )
3617  {
3618  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3619  }
3620  else
3621  {
3622  fct_chk( enclen == outlen );
3623  }
3624 
3625  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3626  if( POLARSSL_MODE_CBC == cipher_info->mode )
3627  {
3628  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3629  }
3630  else
3631  {
3632  fct_chk( outlen == 0 );
3633  }
3634 
3635 
3636  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3637 
3638  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3639  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3640  FCT_TEST_END();
3641 #endif /* POLARSSL_AES_C */
3642 #endif /* POLARSSL_CIPHER_MODE_CFB */
3643 
3644 #ifdef POLARSSL_AES_C
3645 #ifdef POLARSSL_CIPHER_MODE_CFB
3646 
3647  FCT_TEST_BGN(aes_encrypt_and_decrypt_15_bytes)
3648  size_t length = 15;
3649  unsigned char key[32];
3650  unsigned char iv[16];
3651 
3652  const cipher_info_t *cipher_info;
3653  cipher_context_t ctx_dec;
3654  cipher_context_t ctx_enc;
3655 
3656  unsigned char inbuf[64];
3657  unsigned char encbuf[64];
3658  unsigned char decbuf[64];
3659 
3660  size_t outlen = 0;
3661  size_t enclen = 0;
3662 
3663  memset( key, 0, 32 );
3664  memset( iv , 0, 16 );
3665 
3666  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3667  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3668 
3669  memset( inbuf, 5, 64 );
3670  memset( encbuf, 0, 64 );
3671  memset( decbuf, 0, 64 );
3672 
3673  /* Check and get info structures */
3675  fct_chk( NULL != cipher_info );
3676  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
3677 
3678  /* Initialise enc and dec contexts */
3679  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3680  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3681 
3682  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
3683  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
3684 
3685  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3686  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3687 
3688  if( POLARSSL_MODE_CBC == cipher_info->mode )
3689  {
3690  enclen = cipher_get_block_size( &ctx_enc )
3691  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3692  }
3693  else
3694  {
3695  enclen = length;
3696  }
3697 
3698  /* encode length number of bytes from inbuf */
3699  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3700  if( POLARSSL_MODE_CBC == cipher_info->mode )
3701  {
3702  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3703  }
3704  else
3705  {
3706  fct_chk( outlen == enclen );
3707  }
3708 
3709  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3710  if( POLARSSL_MODE_CBC == cipher_info->mode )
3711  {
3712  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3713  }
3714  else
3715  {
3716  fct_chk( outlen == 0 );
3717  }
3718 
3719 
3720  /* decode the previously encoded string */
3721  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3722  if( POLARSSL_MODE_CBC == cipher_info->mode )
3723  {
3724  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3725  }
3726  else
3727  {
3728  fct_chk( enclen == outlen );
3729  }
3730 
3731  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3732  if( POLARSSL_MODE_CBC == cipher_info->mode )
3733  {
3734  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3735  }
3736  else
3737  {
3738  fct_chk( outlen == 0 );
3739  }
3740 
3741 
3742  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3743 
3744  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3745  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3746  FCT_TEST_END();
3747 #endif /* POLARSSL_AES_C */
3748 #endif /* POLARSSL_CIPHER_MODE_CFB */
3749 
3750 #ifdef POLARSSL_AES_C
3751 #ifdef POLARSSL_CIPHER_MODE_CFB
3752 
3753  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes)
3754  size_t length = 16;
3755  unsigned char key[32];
3756  unsigned char iv[16];
3757 
3758  const cipher_info_t *cipher_info;
3759  cipher_context_t ctx_dec;
3760  cipher_context_t ctx_enc;
3761 
3762  unsigned char inbuf[64];
3763  unsigned char encbuf[64];
3764  unsigned char decbuf[64];
3765 
3766  size_t outlen = 0;
3767  size_t enclen = 0;
3768 
3769  memset( key, 0, 32 );
3770  memset( iv , 0, 16 );
3771 
3772  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3773  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3774 
3775  memset( inbuf, 5, 64 );
3776  memset( encbuf, 0, 64 );
3777  memset( decbuf, 0, 64 );
3778 
3779  /* Check and get info structures */
3781  fct_chk( NULL != cipher_info );
3782  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
3783 
3784  /* Initialise enc and dec contexts */
3785  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3786  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3787 
3788  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
3789  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
3790 
3791  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3792  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3793 
3794  if( POLARSSL_MODE_CBC == cipher_info->mode )
3795  {
3796  enclen = cipher_get_block_size( &ctx_enc )
3797  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3798  }
3799  else
3800  {
3801  enclen = length;
3802  }
3803 
3804  /* encode length number of bytes from inbuf */
3805  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3806  if( POLARSSL_MODE_CBC == cipher_info->mode )
3807  {
3808  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3809  }
3810  else
3811  {
3812  fct_chk( outlen == enclen );
3813  }
3814 
3815  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3816  if( POLARSSL_MODE_CBC == cipher_info->mode )
3817  {
3818  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3819  }
3820  else
3821  {
3822  fct_chk( outlen == 0 );
3823  }
3824 
3825 
3826  /* decode the previously encoded string */
3827  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3828  if( POLARSSL_MODE_CBC == cipher_info->mode )
3829  {
3830  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3831  }
3832  else
3833  {
3834  fct_chk( enclen == outlen );
3835  }
3836 
3837  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3838  if( POLARSSL_MODE_CBC == cipher_info->mode )
3839  {
3840  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3841  }
3842  else
3843  {
3844  fct_chk( outlen == 0 );
3845  }
3846 
3847 
3848  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3849 
3850  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3851  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3852  FCT_TEST_END();
3853 #endif /* POLARSSL_AES_C */
3854 #endif /* POLARSSL_CIPHER_MODE_CFB */
3855 
3856 #ifdef POLARSSL_AES_C
3857 #ifdef POLARSSL_CIPHER_MODE_CFB
3858 
3859  FCT_TEST_BGN(aes_encrypt_and_decrypt_17_bytes)
3860  size_t length = 17;
3861  unsigned char key[32];
3862  unsigned char iv[16];
3863 
3864  const cipher_info_t *cipher_info;
3865  cipher_context_t ctx_dec;
3866  cipher_context_t ctx_enc;
3867 
3868  unsigned char inbuf[64];
3869  unsigned char encbuf[64];
3870  unsigned char decbuf[64];
3871 
3872  size_t outlen = 0;
3873  size_t enclen = 0;
3874 
3875  memset( key, 0, 32 );
3876  memset( iv , 0, 16 );
3877 
3878  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3879  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3880 
3881  memset( inbuf, 5, 64 );
3882  memset( encbuf, 0, 64 );
3883  memset( decbuf, 0, 64 );
3884 
3885  /* Check and get info structures */
3887  fct_chk( NULL != cipher_info );
3888  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
3889 
3890  /* Initialise enc and dec contexts */
3891  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3892  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3893 
3894  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
3895  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
3896 
3897  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3898  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3899 
3900  if( POLARSSL_MODE_CBC == cipher_info->mode )
3901  {
3902  enclen = cipher_get_block_size( &ctx_enc )
3903  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3904  }
3905  else
3906  {
3907  enclen = length;
3908  }
3909 
3910  /* encode length number of bytes from inbuf */
3911  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3912  if( POLARSSL_MODE_CBC == cipher_info->mode )
3913  {
3914  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3915  }
3916  else
3917  {
3918  fct_chk( outlen == enclen );
3919  }
3920 
3921  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3922  if( POLARSSL_MODE_CBC == cipher_info->mode )
3923  {
3924  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3925  }
3926  else
3927  {
3928  fct_chk( outlen == 0 );
3929  }
3930 
3931 
3932  /* decode the previously encoded string */
3933  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3934  if( POLARSSL_MODE_CBC == cipher_info->mode )
3935  {
3936  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3937  }
3938  else
3939  {
3940  fct_chk( enclen == outlen );
3941  }
3942 
3943  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3944  if( POLARSSL_MODE_CBC == cipher_info->mode )
3945  {
3946  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3947  }
3948  else
3949  {
3950  fct_chk( outlen == 0 );
3951  }
3952 
3953 
3954  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3955 
3956  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3957  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3958  FCT_TEST_END();
3959 #endif /* POLARSSL_AES_C */
3960 #endif /* POLARSSL_CIPHER_MODE_CFB */
3961 
3962 #ifdef POLARSSL_AES_C
3963 #ifdef POLARSSL_CIPHER_MODE_CFB
3964 
3965  FCT_TEST_BGN(aes_encrypt_and_decrypt_31_bytes)
3966  size_t length = 31;
3967  unsigned char key[32];
3968  unsigned char iv[16];
3969 
3970  const cipher_info_t *cipher_info;
3971  cipher_context_t ctx_dec;
3972  cipher_context_t ctx_enc;
3973 
3974  unsigned char inbuf[64];
3975  unsigned char encbuf[64];
3976  unsigned char decbuf[64];
3977 
3978  size_t outlen = 0;
3979  size_t enclen = 0;
3980 
3981  memset( key, 0, 32 );
3982  memset( iv , 0, 16 );
3983 
3984  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3985  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3986 
3987  memset( inbuf, 5, 64 );
3988  memset( encbuf, 0, 64 );
3989  memset( decbuf, 0, 64 );
3990 
3991  /* Check and get info structures */
3993  fct_chk( NULL != cipher_info );
3994  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
3995 
3996  /* Initialise enc and dec contexts */
3997  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3998  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3999 
4000  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
4001  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
4002 
4003  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4004  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4005 
4006  if( POLARSSL_MODE_CBC == cipher_info->mode )
4007  {
4008  enclen = cipher_get_block_size( &ctx_enc )
4009  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4010  }
4011  else
4012  {
4013  enclen = length;
4014  }
4015 
4016  /* encode length number of bytes from inbuf */
4017  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4018  if( POLARSSL_MODE_CBC == cipher_info->mode )
4019  {
4020  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4021  }
4022  else
4023  {
4024  fct_chk( outlen == enclen );
4025  }
4026 
4027  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4028  if( POLARSSL_MODE_CBC == cipher_info->mode )
4029  {
4030  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4031  }
4032  else
4033  {
4034  fct_chk( outlen == 0 );
4035  }
4036 
4037 
4038  /* decode the previously encoded string */
4039  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4040  if( POLARSSL_MODE_CBC == cipher_info->mode )
4041  {
4042  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4043  }
4044  else
4045  {
4046  fct_chk( enclen == outlen );
4047  }
4048 
4049  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4050  if( POLARSSL_MODE_CBC == cipher_info->mode )
4051  {
4052  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4053  }
4054  else
4055  {
4056  fct_chk( outlen == 0 );
4057  }
4058 
4059 
4060  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4061 
4062  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4063  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4064  FCT_TEST_END();
4065 #endif /* POLARSSL_AES_C */
4066 #endif /* POLARSSL_CIPHER_MODE_CFB */
4067 
4068 #ifdef POLARSSL_AES_C
4069 #ifdef POLARSSL_CIPHER_MODE_CFB
4070 
4071  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes)
4072  size_t length = 32;
4073  unsigned char key[32];
4074  unsigned char iv[16];
4075 
4076  const cipher_info_t *cipher_info;
4077  cipher_context_t ctx_dec;
4078  cipher_context_t ctx_enc;
4079 
4080  unsigned char inbuf[64];
4081  unsigned char encbuf[64];
4082  unsigned char decbuf[64];
4083 
4084  size_t outlen = 0;
4085  size_t enclen = 0;
4086 
4087  memset( key, 0, 32 );
4088  memset( iv , 0, 16 );
4089 
4090  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4091  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4092 
4093  memset( inbuf, 5, 64 );
4094  memset( encbuf, 0, 64 );
4095  memset( decbuf, 0, 64 );
4096 
4097  /* Check and get info structures */
4099  fct_chk( NULL != cipher_info );
4100  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
4101 
4102  /* Initialise enc and dec contexts */
4103  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4104  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4105 
4106  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
4107  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
4108 
4109  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4110  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4111 
4112  if( POLARSSL_MODE_CBC == cipher_info->mode )
4113  {
4114  enclen = cipher_get_block_size( &ctx_enc )
4115  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4116  }
4117  else
4118  {
4119  enclen = length;
4120  }
4121 
4122  /* encode length number of bytes from inbuf */
4123  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4124  if( POLARSSL_MODE_CBC == cipher_info->mode )
4125  {
4126  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4127  }
4128  else
4129  {
4130  fct_chk( outlen == enclen );
4131  }
4132 
4133  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4134  if( POLARSSL_MODE_CBC == cipher_info->mode )
4135  {
4136  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4137  }
4138  else
4139  {
4140  fct_chk( outlen == 0 );
4141  }
4142 
4143 
4144  /* decode the previously encoded string */
4145  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4146  if( POLARSSL_MODE_CBC == cipher_info->mode )
4147  {
4148  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4149  }
4150  else
4151  {
4152  fct_chk( enclen == outlen );
4153  }
4154 
4155  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4156  if( POLARSSL_MODE_CBC == cipher_info->mode )
4157  {
4158  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4159  }
4160  else
4161  {
4162  fct_chk( outlen == 0 );
4163  }
4164 
4165 
4166  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4167 
4168  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4169  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4170  FCT_TEST_END();
4171 #endif /* POLARSSL_AES_C */
4172 #endif /* POLARSSL_CIPHER_MODE_CFB */
4173 
4174 #ifdef POLARSSL_AES_C
4175 #ifdef POLARSSL_CIPHER_MODE_CFB
4176 
4177  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes)
4178  size_t length = 33;
4179  unsigned char key[32];
4180  unsigned char iv[16];
4181 
4182  const cipher_info_t *cipher_info;
4183  cipher_context_t ctx_dec;
4184  cipher_context_t ctx_enc;
4185 
4186  unsigned char inbuf[64];
4187  unsigned char encbuf[64];
4188  unsigned char decbuf[64];
4189 
4190  size_t outlen = 0;
4191  size_t enclen = 0;
4192 
4193  memset( key, 0, 32 );
4194  memset( iv , 0, 16 );
4195 
4196  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4197  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4198 
4199  memset( inbuf, 5, 64 );
4200  memset( encbuf, 0, 64 );
4201  memset( decbuf, 0, 64 );
4202 
4203  /* Check and get info structures */
4205  fct_chk( NULL != cipher_info );
4206  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
4207 
4208  /* Initialise enc and dec contexts */
4209  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4210  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4211 
4212  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
4213  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
4214 
4215  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4216  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4217 
4218  if( POLARSSL_MODE_CBC == cipher_info->mode )
4219  {
4220  enclen = cipher_get_block_size( &ctx_enc )
4221  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4222  }
4223  else
4224  {
4225  enclen = length;
4226  }
4227 
4228  /* encode length number of bytes from inbuf */
4229  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4230  if( POLARSSL_MODE_CBC == cipher_info->mode )
4231  {
4232  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4233  }
4234  else
4235  {
4236  fct_chk( outlen == enclen );
4237  }
4238 
4239  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4240  if( POLARSSL_MODE_CBC == cipher_info->mode )
4241  {
4242  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4243  }
4244  else
4245  {
4246  fct_chk( outlen == 0 );
4247  }
4248 
4249 
4250  /* decode the previously encoded string */
4251  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4252  if( POLARSSL_MODE_CBC == cipher_info->mode )
4253  {
4254  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4255  }
4256  else
4257  {
4258  fct_chk( enclen == outlen );
4259  }
4260 
4261  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4262  if( POLARSSL_MODE_CBC == cipher_info->mode )
4263  {
4264  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4265  }
4266  else
4267  {
4268  fct_chk( outlen == 0 );
4269  }
4270 
4271 
4272  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4273 
4274  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4275  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4276  FCT_TEST_END();
4277 #endif /* POLARSSL_AES_C */
4278 #endif /* POLARSSL_CIPHER_MODE_CFB */
4279 
4280 #ifdef POLARSSL_AES_C
4281 #ifdef POLARSSL_CIPHER_MODE_CFB
4282 
4283  FCT_TEST_BGN(aes_encrypt_and_decrypt_47_bytes)
4284  size_t length = 47;
4285  unsigned char key[32];
4286  unsigned char iv[16];
4287 
4288  const cipher_info_t *cipher_info;
4289  cipher_context_t ctx_dec;
4290  cipher_context_t ctx_enc;
4291 
4292  unsigned char inbuf[64];
4293  unsigned char encbuf[64];
4294  unsigned char decbuf[64];
4295 
4296  size_t outlen = 0;
4297  size_t enclen = 0;
4298 
4299  memset( key, 0, 32 );
4300  memset( iv , 0, 16 );
4301 
4302  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4303  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4304 
4305  memset( inbuf, 5, 64 );
4306  memset( encbuf, 0, 64 );
4307  memset( decbuf, 0, 64 );
4308 
4309  /* Check and get info structures */
4311  fct_chk( NULL != cipher_info );
4312  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
4313 
4314  /* Initialise enc and dec contexts */
4315  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4316  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4317 
4318  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
4319  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
4320 
4321  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4322  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4323 
4324  if( POLARSSL_MODE_CBC == cipher_info->mode )
4325  {
4326  enclen = cipher_get_block_size( &ctx_enc )
4327  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4328  }
4329  else
4330  {
4331  enclen = length;
4332  }
4333 
4334  /* encode length number of bytes from inbuf */
4335  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4336  if( POLARSSL_MODE_CBC == cipher_info->mode )
4337  {
4338  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4339  }
4340  else
4341  {
4342  fct_chk( outlen == enclen );
4343  }
4344 
4345  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4346  if( POLARSSL_MODE_CBC == cipher_info->mode )
4347  {
4348  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4349  }
4350  else
4351  {
4352  fct_chk( outlen == 0 );
4353  }
4354 
4355 
4356  /* decode the previously encoded string */
4357  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4358  if( POLARSSL_MODE_CBC == cipher_info->mode )
4359  {
4360  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4361  }
4362  else
4363  {
4364  fct_chk( enclen == outlen );
4365  }
4366 
4367  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4368  if( POLARSSL_MODE_CBC == cipher_info->mode )
4369  {
4370  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4371  }
4372  else
4373  {
4374  fct_chk( outlen == 0 );
4375  }
4376 
4377 
4378  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4379 
4380  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4381  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4382  FCT_TEST_END();
4383 #endif /* POLARSSL_AES_C */
4384 #endif /* POLARSSL_CIPHER_MODE_CFB */
4385 
4386 #ifdef POLARSSL_AES_C
4387 #ifdef POLARSSL_CIPHER_MODE_CFB
4388 
4389  FCT_TEST_BGN(aes_encrypt_and_decrypt_48_bytes)
4390  size_t length = 48;
4391  unsigned char key[32];
4392  unsigned char iv[16];
4393 
4394  const cipher_info_t *cipher_info;
4395  cipher_context_t ctx_dec;
4396  cipher_context_t ctx_enc;
4397 
4398  unsigned char inbuf[64];
4399  unsigned char encbuf[64];
4400  unsigned char decbuf[64];
4401 
4402  size_t outlen = 0;
4403  size_t enclen = 0;
4404 
4405  memset( key, 0, 32 );
4406  memset( iv , 0, 16 );
4407 
4408  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4409  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4410 
4411  memset( inbuf, 5, 64 );
4412  memset( encbuf, 0, 64 );
4413  memset( decbuf, 0, 64 );
4414 
4415  /* Check and get info structures */
4417  fct_chk( NULL != cipher_info );
4418  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
4419 
4420  /* Initialise enc and dec contexts */
4421  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4422  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4423 
4424  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
4425  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
4426 
4427  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4428  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4429 
4430  if( POLARSSL_MODE_CBC == cipher_info->mode )
4431  {
4432  enclen = cipher_get_block_size( &ctx_enc )
4433  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4434  }
4435  else
4436  {
4437  enclen = length;
4438  }
4439 
4440  /* encode length number of bytes from inbuf */
4441  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4442  if( POLARSSL_MODE_CBC == cipher_info->mode )
4443  {
4444  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4445  }
4446  else
4447  {
4448  fct_chk( outlen == enclen );
4449  }
4450 
4451  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4452  if( POLARSSL_MODE_CBC == cipher_info->mode )
4453  {
4454  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4455  }
4456  else
4457  {
4458  fct_chk( outlen == 0 );
4459  }
4460 
4461 
4462  /* decode the previously encoded string */
4463  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4464  if( POLARSSL_MODE_CBC == cipher_info->mode )
4465  {
4466  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4467  }
4468  else
4469  {
4470  fct_chk( enclen == outlen );
4471  }
4472 
4473  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4474  if( POLARSSL_MODE_CBC == cipher_info->mode )
4475  {
4476  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4477  }
4478  else
4479  {
4480  fct_chk( outlen == 0 );
4481  }
4482 
4483 
4484  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4485 
4486  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4487  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4488  FCT_TEST_END();
4489 #endif /* POLARSSL_AES_C */
4490 #endif /* POLARSSL_CIPHER_MODE_CFB */
4491 
4492 #ifdef POLARSSL_AES_C
4493 #ifdef POLARSSL_CIPHER_MODE_CFB
4494 
4495  FCT_TEST_BGN(aes_encrypt_and_decrypt_49_bytes)
4496  size_t length = 49;
4497  unsigned char key[32];
4498  unsigned char iv[16];
4499 
4500  const cipher_info_t *cipher_info;
4501  cipher_context_t ctx_dec;
4502  cipher_context_t ctx_enc;
4503 
4504  unsigned char inbuf[64];
4505  unsigned char encbuf[64];
4506  unsigned char decbuf[64];
4507 
4508  size_t outlen = 0;
4509  size_t enclen = 0;
4510 
4511  memset( key, 0, 32 );
4512  memset( iv , 0, 16 );
4513 
4514  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4515  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4516 
4517  memset( inbuf, 5, 64 );
4518  memset( encbuf, 0, 64 );
4519  memset( decbuf, 0, 64 );
4520 
4521  /* Check and get info structures */
4523  fct_chk( NULL != cipher_info );
4524  fct_chk( cipher_info_from_string( "AES-128-CFB128" ) == cipher_info );
4525 
4526  /* Initialise enc and dec contexts */
4527  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4528  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4529 
4530  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
4531  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
4532 
4533  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4534  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4535 
4536  if( POLARSSL_MODE_CBC == cipher_info->mode )
4537  {
4538  enclen = cipher_get_block_size( &ctx_enc )
4539  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4540  }
4541  else
4542  {
4543  enclen = length;
4544  }
4545 
4546  /* encode length number of bytes from inbuf */
4547  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4548  if( POLARSSL_MODE_CBC == cipher_info->mode )
4549  {
4550  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4551  }
4552  else
4553  {
4554  fct_chk( outlen == enclen );
4555  }
4556 
4557  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4558  if( POLARSSL_MODE_CBC == cipher_info->mode )
4559  {
4560  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4561  }
4562  else
4563  {
4564  fct_chk( outlen == 0 );
4565  }
4566 
4567 
4568  /* decode the previously encoded string */
4569  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4570  if( POLARSSL_MODE_CBC == cipher_info->mode )
4571  {
4572  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4573  }
4574  else
4575  {
4576  fct_chk( enclen == outlen );
4577  }
4578 
4579  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4580  if( POLARSSL_MODE_CBC == cipher_info->mode )
4581  {
4582  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4583  }
4584  else
4585  {
4586  fct_chk( outlen == 0 );
4587  }
4588 
4589 
4590  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4591 
4592  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4593  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4594  FCT_TEST_END();
4595 #endif /* POLARSSL_AES_C */
4596 #endif /* POLARSSL_CIPHER_MODE_CFB */
4597 
4598 #ifdef POLARSSL_AES_C
4599 #ifdef POLARSSL_CIPHER_MODE_CFB
4600 
4601  FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes_in_multiple_parts)
4602  size_t first_length = 0;
4603  size_t second_length = 0;
4604  size_t length = first_length + second_length;
4605  unsigned char key[32];
4606  unsigned char iv[16];
4607 
4608  cipher_context_t ctx_dec;
4609  cipher_context_t ctx_enc;
4610  const cipher_info_t *cipher_info;
4611 
4612  unsigned char inbuf[64];
4613  unsigned char encbuf[64];
4614  unsigned char decbuf[64];
4615 
4616  size_t outlen = 0;
4617  size_t totaloutlen = 0;
4618  size_t enclen = 0;
4619 
4620  memset( key, 0, 32 );
4621  memset( iv , 0, 16 );
4622 
4623  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4624  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4625 
4626  memset( inbuf, 5, 64 );
4627  memset( encbuf, 0, 64 );
4628  memset( decbuf, 0, 64 );
4629 
4630  /* Initialise enc and dec contexts */
4632  fct_chk( NULL != cipher_info);
4633 
4634  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4635  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4636 
4637  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
4638  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
4639 
4640  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4641  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4642 
4643  if( POLARSSL_MODE_CBC == cipher_info->mode )
4644  {
4645  enclen = cipher_get_block_size(&ctx_enc )
4646  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4647  }
4648  else
4649  {
4650  enclen = length;
4651  }
4652 
4653  /* encode length number of bytes from inbuf */
4654  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4655  totaloutlen = outlen;
4656  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4657  totaloutlen += outlen;
4658  if( POLARSSL_MODE_CBC == cipher_info->mode )
4659  {
4660  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4661  }
4662  else
4663  {
4664  fct_chk( totaloutlen == enclen );
4665  }
4666  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4667  totaloutlen += outlen;
4668  if( POLARSSL_MODE_CBC == cipher_info->mode )
4669  {
4670  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4671  }
4672  else
4673  {
4674  fct_chk( outlen == 0 );
4675  }
4676 
4677  /* decode the previously encoded string */
4678  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4679  if( POLARSSL_MODE_CBC == cipher_info->mode )
4680  {
4681  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4682  }
4683  else
4684  {
4685  fct_chk( enclen == outlen );
4686  }
4687  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4688  if( POLARSSL_MODE_CBC == cipher_info->mode )
4689  {
4690  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4691  }
4692  else
4693  {
4694  fct_chk( outlen == 0 );
4695  }
4696 
4697 
4698  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4699 
4700  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4701  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4702  FCT_TEST_END();
4703 #endif /* POLARSSL_AES_C */
4704 #endif /* POLARSSL_CIPHER_MODE_CFB */
4705 
4706 #ifdef POLARSSL_AES_C
4707 #ifdef POLARSSL_CIPHER_MODE_CFB
4708 
4709  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
4710  size_t first_length = 1;
4711  size_t second_length = 0;
4712  size_t length = first_length + second_length;
4713  unsigned char key[32];
4714  unsigned char iv[16];
4715 
4716  cipher_context_t ctx_dec;
4717  cipher_context_t ctx_enc;
4718  const cipher_info_t *cipher_info;
4719 
4720  unsigned char inbuf[64];
4721  unsigned char encbuf[64];
4722  unsigned char decbuf[64];
4723 
4724  size_t outlen = 0;
4725  size_t totaloutlen = 0;
4726  size_t enclen = 0;
4727 
4728  memset( key, 0, 32 );
4729  memset( iv , 0, 16 );
4730 
4731  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4732  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4733 
4734  memset( inbuf, 5, 64 );
4735  memset( encbuf, 0, 64 );
4736  memset( decbuf, 0, 64 );
4737 
4738  /* Initialise enc and dec contexts */
4740  fct_chk( NULL != cipher_info);
4741 
4742  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4743  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4744 
4745  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
4746  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
4747 
4748  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4749  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4750 
4751  if( POLARSSL_MODE_CBC == cipher_info->mode )
4752  {
4753  enclen = cipher_get_block_size(&ctx_enc )
4754  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4755  }
4756  else
4757  {
4758  enclen = length;
4759  }
4760 
4761  /* encode length number of bytes from inbuf */
4762  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4763  totaloutlen = outlen;
4764  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4765  totaloutlen += outlen;
4766  if( POLARSSL_MODE_CBC == cipher_info->mode )
4767  {
4768  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4769  }
4770  else
4771  {
4772  fct_chk( totaloutlen == enclen );
4773  }
4774  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4775  totaloutlen += outlen;
4776  if( POLARSSL_MODE_CBC == cipher_info->mode )
4777  {
4778  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4779  }
4780  else
4781  {
4782  fct_chk( outlen == 0 );
4783  }
4784 
4785  /* decode the previously encoded string */
4786  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4787  if( POLARSSL_MODE_CBC == cipher_info->mode )
4788  {
4789  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4790  }
4791  else
4792  {
4793  fct_chk( enclen == outlen );
4794  }
4795  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4796  if( POLARSSL_MODE_CBC == cipher_info->mode )
4797  {
4798  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4799  }
4800  else
4801  {
4802  fct_chk( outlen == 0 );
4803  }
4804 
4805 
4806  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4807 
4808  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4809  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4810  FCT_TEST_END();
4811 #endif /* POLARSSL_AES_C */
4812 #endif /* POLARSSL_CIPHER_MODE_CFB */
4813 
4814 #ifdef POLARSSL_AES_C
4815 #ifdef POLARSSL_CIPHER_MODE_CFB
4816 
4817  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
4818  size_t first_length = 0;
4819  size_t second_length = 1;
4820  size_t length = first_length + second_length;
4821  unsigned char key[32];
4822  unsigned char iv[16];
4823 
4824  cipher_context_t ctx_dec;
4825  cipher_context_t ctx_enc;
4826  const cipher_info_t *cipher_info;
4827 
4828  unsigned char inbuf[64];
4829  unsigned char encbuf[64];
4830  unsigned char decbuf[64];
4831 
4832  size_t outlen = 0;
4833  size_t totaloutlen = 0;
4834  size_t enclen = 0;
4835 
4836  memset( key, 0, 32 );
4837  memset( iv , 0, 16 );
4838 
4839  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4840  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4841 
4842  memset( inbuf, 5, 64 );
4843  memset( encbuf, 0, 64 );
4844  memset( decbuf, 0, 64 );
4845 
4846  /* Initialise enc and dec contexts */
4848  fct_chk( NULL != cipher_info);
4849 
4850  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4851  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4852 
4853  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
4854  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
4855 
4856  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4857  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4858 
4859  if( POLARSSL_MODE_CBC == cipher_info->mode )
4860  {
4861  enclen = cipher_get_block_size(&ctx_enc )
4862  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4863  }
4864  else
4865  {
4866  enclen = length;
4867  }
4868 
4869  /* encode length number of bytes from inbuf */
4870  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4871  totaloutlen = outlen;
4872  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4873  totaloutlen += outlen;
4874  if( POLARSSL_MODE_CBC == cipher_info->mode )
4875  {
4876  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4877  }
4878  else
4879  {
4880  fct_chk( totaloutlen == enclen );
4881  }
4882  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4883  totaloutlen += outlen;
4884  if( POLARSSL_MODE_CBC == cipher_info->mode )
4885  {
4886  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4887  }
4888  else
4889  {
4890  fct_chk( outlen == 0 );
4891  }
4892 
4893  /* decode the previously encoded string */
4894  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4895  if( POLARSSL_MODE_CBC == cipher_info->mode )
4896  {
4897  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4898  }
4899  else
4900  {
4901  fct_chk( enclen == outlen );
4902  }
4903  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4904  if( POLARSSL_MODE_CBC == cipher_info->mode )
4905  {
4906  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4907  }
4908  else
4909  {
4910  fct_chk( outlen == 0 );
4911  }
4912 
4913 
4914  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4915 
4916  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4917  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4918  FCT_TEST_END();
4919 #endif /* POLARSSL_AES_C */
4920 #endif /* POLARSSL_CIPHER_MODE_CFB */
4921 
4922 #ifdef POLARSSL_AES_C
4923 #ifdef POLARSSL_CIPHER_MODE_CFB
4924 
4925  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
4926  size_t first_length = 16;
4927  size_t second_length = 0;
4928  size_t length = first_length + second_length;
4929  unsigned char key[32];
4930  unsigned char iv[16];
4931 
4932  cipher_context_t ctx_dec;
4933  cipher_context_t ctx_enc;
4934  const cipher_info_t *cipher_info;
4935 
4936  unsigned char inbuf[64];
4937  unsigned char encbuf[64];
4938  unsigned char decbuf[64];
4939 
4940  size_t outlen = 0;
4941  size_t totaloutlen = 0;
4942  size_t enclen = 0;
4943 
4944  memset( key, 0, 32 );
4945  memset( iv , 0, 16 );
4946 
4947  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4948  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4949 
4950  memset( inbuf, 5, 64 );
4951  memset( encbuf, 0, 64 );
4952  memset( decbuf, 0, 64 );
4953 
4954  /* Initialise enc and dec contexts */
4956  fct_chk( NULL != cipher_info);
4957 
4958  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4959  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4960 
4961  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
4962  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
4963 
4964  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4965  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4966 
4967  if( POLARSSL_MODE_CBC == cipher_info->mode )
4968  {
4969  enclen = cipher_get_block_size(&ctx_enc )
4970  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4971  }
4972  else
4973  {
4974  enclen = length;
4975  }
4976 
4977  /* encode length number of bytes from inbuf */
4978  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4979  totaloutlen = outlen;
4980  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4981  totaloutlen += outlen;
4982  if( POLARSSL_MODE_CBC == cipher_info->mode )
4983  {
4984  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4985  }
4986  else
4987  {
4988  fct_chk( totaloutlen == enclen );
4989  }
4990  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4991  totaloutlen += outlen;
4992  if( POLARSSL_MODE_CBC == cipher_info->mode )
4993  {
4994  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4995  }
4996  else
4997  {
4998  fct_chk( outlen == 0 );
4999  }
5000 
5001  /* decode the previously encoded string */
5002  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5003  if( POLARSSL_MODE_CBC == cipher_info->mode )
5004  {
5005  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5006  }
5007  else
5008  {
5009  fct_chk( enclen == outlen );
5010  }
5011  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5012  if( POLARSSL_MODE_CBC == cipher_info->mode )
5013  {
5014  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5015  }
5016  else
5017  {
5018  fct_chk( outlen == 0 );
5019  }
5020 
5021 
5022  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5023 
5024  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5025  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5026  FCT_TEST_END();
5027 #endif /* POLARSSL_AES_C */
5028 #endif /* POLARSSL_CIPHER_MODE_CFB */
5029 
5030 #ifdef POLARSSL_AES_C
5031 #ifdef POLARSSL_CIPHER_MODE_CFB
5032 
5033  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
5034  size_t first_length = 0;
5035  size_t second_length = 16;
5036  size_t length = first_length + second_length;
5037  unsigned char key[32];
5038  unsigned char iv[16];
5039 
5040  cipher_context_t ctx_dec;
5041  cipher_context_t ctx_enc;
5042  const cipher_info_t *cipher_info;
5043 
5044  unsigned char inbuf[64];
5045  unsigned char encbuf[64];
5046  unsigned char decbuf[64];
5047 
5048  size_t outlen = 0;
5049  size_t totaloutlen = 0;
5050  size_t enclen = 0;
5051 
5052  memset( key, 0, 32 );
5053  memset( iv , 0, 16 );
5054 
5055  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5056  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5057 
5058  memset( inbuf, 5, 64 );
5059  memset( encbuf, 0, 64 );
5060  memset( decbuf, 0, 64 );
5061 
5062  /* Initialise enc and dec contexts */
5064  fct_chk( NULL != cipher_info);
5065 
5066  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5067  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5068 
5069  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
5070  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
5071 
5072  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5073  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5074 
5075  if( POLARSSL_MODE_CBC == cipher_info->mode )
5076  {
5077  enclen = cipher_get_block_size(&ctx_enc )
5078  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5079  }
5080  else
5081  {
5082  enclen = length;
5083  }
5084 
5085  /* encode length number of bytes from inbuf */
5086  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5087  totaloutlen = outlen;
5088  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5089  totaloutlen += outlen;
5090  if( POLARSSL_MODE_CBC == cipher_info->mode )
5091  {
5092  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5093  }
5094  else
5095  {
5096  fct_chk( totaloutlen == enclen );
5097  }
5098  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5099  totaloutlen += outlen;
5100  if( POLARSSL_MODE_CBC == cipher_info->mode )
5101  {
5102  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5103  }
5104  else
5105  {
5106  fct_chk( outlen == 0 );
5107  }
5108 
5109  /* decode the previously encoded string */
5110  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5111  if( POLARSSL_MODE_CBC == cipher_info->mode )
5112  {
5113  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5114  }
5115  else
5116  {
5117  fct_chk( enclen == outlen );
5118  }
5119  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5120  if( POLARSSL_MODE_CBC == cipher_info->mode )
5121  {
5122  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5123  }
5124  else
5125  {
5126  fct_chk( outlen == 0 );
5127  }
5128 
5129 
5130  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5131 
5132  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5133  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5134  FCT_TEST_END();
5135 #endif /* POLARSSL_AES_C */
5136 #endif /* POLARSSL_CIPHER_MODE_CFB */
5137 
5138 #ifdef POLARSSL_AES_C
5139 #ifdef POLARSSL_CIPHER_MODE_CFB
5140 
5141  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
5142  size_t first_length = 1;
5143  size_t second_length = 15;
5144  size_t length = first_length + second_length;
5145  unsigned char key[32];
5146  unsigned char iv[16];
5147 
5148  cipher_context_t ctx_dec;
5149  cipher_context_t ctx_enc;
5150  const cipher_info_t *cipher_info;
5151 
5152  unsigned char inbuf[64];
5153  unsigned char encbuf[64];
5154  unsigned char decbuf[64];
5155 
5156  size_t outlen = 0;
5157  size_t totaloutlen = 0;
5158  size_t enclen = 0;
5159 
5160  memset( key, 0, 32 );
5161  memset( iv , 0, 16 );
5162 
5163  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5164  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5165 
5166  memset( inbuf, 5, 64 );
5167  memset( encbuf, 0, 64 );
5168  memset( decbuf, 0, 64 );
5169 
5170  /* Initialise enc and dec contexts */
5172  fct_chk( NULL != cipher_info);
5173 
5174  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5175  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5176 
5177  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
5178  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
5179 
5180  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5181  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5182 
5183  if( POLARSSL_MODE_CBC == cipher_info->mode )
5184  {
5185  enclen = cipher_get_block_size(&ctx_enc )
5186  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5187  }
5188  else
5189  {
5190  enclen = length;
5191  }
5192 
5193  /* encode length number of bytes from inbuf */
5194  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5195  totaloutlen = outlen;
5196  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5197  totaloutlen += outlen;
5198  if( POLARSSL_MODE_CBC == cipher_info->mode )
5199  {
5200  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5201  }
5202  else
5203  {
5204  fct_chk( totaloutlen == enclen );
5205  }
5206  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5207  totaloutlen += outlen;
5208  if( POLARSSL_MODE_CBC == cipher_info->mode )
5209  {
5210  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5211  }
5212  else
5213  {
5214  fct_chk( outlen == 0 );
5215  }
5216 
5217  /* decode the previously encoded string */
5218  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5219  if( POLARSSL_MODE_CBC == cipher_info->mode )
5220  {
5221  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5222  }
5223  else
5224  {
5225  fct_chk( enclen == outlen );
5226  }
5227  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5228  if( POLARSSL_MODE_CBC == cipher_info->mode )
5229  {
5230  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5231  }
5232  else
5233  {
5234  fct_chk( outlen == 0 );
5235  }
5236 
5237 
5238  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5239 
5240  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5241  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5242  FCT_TEST_END();
5243 #endif /* POLARSSL_AES_C */
5244 #endif /* POLARSSL_CIPHER_MODE_CFB */
5245 
5246 #ifdef POLARSSL_AES_C
5247 #ifdef POLARSSL_CIPHER_MODE_CFB
5248 
5249  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
5250  size_t first_length = 15;
5251  size_t second_length = 1;
5252  size_t length = first_length + second_length;
5253  unsigned char key[32];
5254  unsigned char iv[16];
5255 
5256  cipher_context_t ctx_dec;
5257  cipher_context_t ctx_enc;
5258  const cipher_info_t *cipher_info;
5259 
5260  unsigned char inbuf[64];
5261  unsigned char encbuf[64];
5262  unsigned char decbuf[64];
5263 
5264  size_t outlen = 0;
5265  size_t totaloutlen = 0;
5266  size_t enclen = 0;
5267 
5268  memset( key, 0, 32 );
5269  memset( iv , 0, 16 );
5270 
5271  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5272  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5273 
5274  memset( inbuf, 5, 64 );
5275  memset( encbuf, 0, 64 );
5276  memset( decbuf, 0, 64 );
5277 
5278  /* Initialise enc and dec contexts */
5280  fct_chk( NULL != cipher_info);
5281 
5282  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5283  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5284 
5285  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
5286  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
5287 
5288  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5289  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5290 
5291  if( POLARSSL_MODE_CBC == cipher_info->mode )
5292  {
5293  enclen = cipher_get_block_size(&ctx_enc )
5294  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5295  }
5296  else
5297  {
5298  enclen = length;
5299  }
5300 
5301  /* encode length number of bytes from inbuf */
5302  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5303  totaloutlen = outlen;
5304  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5305  totaloutlen += outlen;
5306  if( POLARSSL_MODE_CBC == cipher_info->mode )
5307  {
5308  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5309  }
5310  else
5311  {
5312  fct_chk( totaloutlen == enclen );
5313  }
5314  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5315  totaloutlen += outlen;
5316  if( POLARSSL_MODE_CBC == cipher_info->mode )
5317  {
5318  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5319  }
5320  else
5321  {
5322  fct_chk( outlen == 0 );
5323  }
5324 
5325  /* decode the previously encoded string */
5326  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5327  if( POLARSSL_MODE_CBC == cipher_info->mode )
5328  {
5329  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5330  }
5331  else
5332  {
5333  fct_chk( enclen == outlen );
5334  }
5335  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5336  if( POLARSSL_MODE_CBC == cipher_info->mode )
5337  {
5338  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5339  }
5340  else
5341  {
5342  fct_chk( outlen == 0 );
5343  }
5344 
5345 
5346  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5347 
5348  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5349  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5350  FCT_TEST_END();
5351 #endif /* POLARSSL_AES_C */
5352 #endif /* POLARSSL_CIPHER_MODE_CFB */
5353 
5354 #ifdef POLARSSL_AES_C
5355 #ifdef POLARSSL_CIPHER_MODE_CFB
5356 
5357  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
5358  size_t first_length = 15;
5359  size_t second_length = 7;
5360  size_t length = first_length + second_length;
5361  unsigned char key[32];
5362  unsigned char iv[16];
5363 
5364  cipher_context_t ctx_dec;
5365  cipher_context_t ctx_enc;
5366  const cipher_info_t *cipher_info;
5367 
5368  unsigned char inbuf[64];
5369  unsigned char encbuf[64];
5370  unsigned char decbuf[64];
5371 
5372  size_t outlen = 0;
5373  size_t totaloutlen = 0;
5374  size_t enclen = 0;
5375 
5376  memset( key, 0, 32 );
5377  memset( iv , 0, 16 );
5378 
5379  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5380  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5381 
5382  memset( inbuf, 5, 64 );
5383  memset( encbuf, 0, 64 );
5384  memset( decbuf, 0, 64 );
5385 
5386  /* Initialise enc and dec contexts */
5388  fct_chk( NULL != cipher_info);
5389 
5390  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5391  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5392 
5393  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
5394  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
5395 
5396  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5397  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5398 
5399  if( POLARSSL_MODE_CBC == cipher_info->mode )
5400  {
5401  enclen = cipher_get_block_size(&ctx_enc )
5402  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5403  }
5404  else
5405  {
5406  enclen = length;
5407  }
5408 
5409  /* encode length number of bytes from inbuf */
5410  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5411  totaloutlen = outlen;
5412  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5413  totaloutlen += outlen;
5414  if( POLARSSL_MODE_CBC == cipher_info->mode )
5415  {
5416  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5417  }
5418  else
5419  {
5420  fct_chk( totaloutlen == enclen );
5421  }
5422  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5423  totaloutlen += outlen;
5424  if( POLARSSL_MODE_CBC == cipher_info->mode )
5425  {
5426  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5427  }
5428  else
5429  {
5430  fct_chk( outlen == 0 );
5431  }
5432 
5433  /* decode the previously encoded string */
5434  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5435  if( POLARSSL_MODE_CBC == cipher_info->mode )
5436  {
5437  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5438  }
5439  else
5440  {
5441  fct_chk( enclen == outlen );
5442  }
5443  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5444  if( POLARSSL_MODE_CBC == cipher_info->mode )
5445  {
5446  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5447  }
5448  else
5449  {
5450  fct_chk( outlen == 0 );
5451  }
5452 
5453 
5454  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5455 
5456  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5457  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5458  FCT_TEST_END();
5459 #endif /* POLARSSL_AES_C */
5460 #endif /* POLARSSL_CIPHER_MODE_CFB */
5461 
5462 #ifdef POLARSSL_AES_C
5463 #ifdef POLARSSL_CIPHER_MODE_CFB
5464 
5465  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
5466  size_t first_length = 16;
5467  size_t second_length = 6;
5468  size_t length = first_length + second_length;
5469  unsigned char key[32];
5470  unsigned char iv[16];
5471 
5472  cipher_context_t ctx_dec;
5473  cipher_context_t ctx_enc;
5474  const cipher_info_t *cipher_info;
5475 
5476  unsigned char inbuf[64];
5477  unsigned char encbuf[64];
5478  unsigned char decbuf[64];
5479 
5480  size_t outlen = 0;
5481  size_t totaloutlen = 0;
5482  size_t enclen = 0;
5483 
5484  memset( key, 0, 32 );
5485  memset( iv , 0, 16 );
5486 
5487  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5488  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5489 
5490  memset( inbuf, 5, 64 );
5491  memset( encbuf, 0, 64 );
5492  memset( decbuf, 0, 64 );
5493 
5494  /* Initialise enc and dec contexts */
5496  fct_chk( NULL != cipher_info);
5497 
5498  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5499  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5500 
5501  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
5502  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
5503 
5504  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5505  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5506 
5507  if( POLARSSL_MODE_CBC == cipher_info->mode )
5508  {
5509  enclen = cipher_get_block_size(&ctx_enc )
5510  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5511  }
5512  else
5513  {
5514  enclen = length;
5515  }
5516 
5517  /* encode length number of bytes from inbuf */
5518  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5519  totaloutlen = outlen;
5520  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5521  totaloutlen += outlen;
5522  if( POLARSSL_MODE_CBC == cipher_info->mode )
5523  {
5524  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5525  }
5526  else
5527  {
5528  fct_chk( totaloutlen == enclen );
5529  }
5530  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5531  totaloutlen += outlen;
5532  if( POLARSSL_MODE_CBC == cipher_info->mode )
5533  {
5534  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5535  }
5536  else
5537  {
5538  fct_chk( outlen == 0 );
5539  }
5540 
5541  /* decode the previously encoded string */
5542  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5543  if( POLARSSL_MODE_CBC == cipher_info->mode )
5544  {
5545  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5546  }
5547  else
5548  {
5549  fct_chk( enclen == outlen );
5550  }
5551  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5552  if( POLARSSL_MODE_CBC == cipher_info->mode )
5553  {
5554  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5555  }
5556  else
5557  {
5558  fct_chk( outlen == 0 );
5559  }
5560 
5561 
5562  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5563 
5564  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5565  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5566  FCT_TEST_END();
5567 #endif /* POLARSSL_AES_C */
5568 #endif /* POLARSSL_CIPHER_MODE_CFB */
5569 
5570 #ifdef POLARSSL_AES_C
5571 #ifdef POLARSSL_CIPHER_MODE_CFB
5572 
5573  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
5574  size_t first_length = 17;
5575  size_t second_length = 6;
5576  size_t length = first_length + second_length;
5577  unsigned char key[32];
5578  unsigned char iv[16];
5579 
5580  cipher_context_t ctx_dec;
5581  cipher_context_t ctx_enc;
5582  const cipher_info_t *cipher_info;
5583 
5584  unsigned char inbuf[64];
5585  unsigned char encbuf[64];
5586  unsigned char decbuf[64];
5587 
5588  size_t outlen = 0;
5589  size_t totaloutlen = 0;
5590  size_t enclen = 0;
5591 
5592  memset( key, 0, 32 );
5593  memset( iv , 0, 16 );
5594 
5595  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5596  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5597 
5598  memset( inbuf, 5, 64 );
5599  memset( encbuf, 0, 64 );
5600  memset( decbuf, 0, 64 );
5601 
5602  /* Initialise enc and dec contexts */
5604  fct_chk( NULL != cipher_info);
5605 
5606  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5607  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5608 
5609  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
5610  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
5611 
5612  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5613  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5614 
5615  if( POLARSSL_MODE_CBC == cipher_info->mode )
5616  {
5617  enclen = cipher_get_block_size(&ctx_enc )
5618  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5619  }
5620  else
5621  {
5622  enclen = length;
5623  }
5624 
5625  /* encode length number of bytes from inbuf */
5626  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5627  totaloutlen = outlen;
5628  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5629  totaloutlen += outlen;
5630  if( POLARSSL_MODE_CBC == cipher_info->mode )
5631  {
5632  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5633  }
5634  else
5635  {
5636  fct_chk( totaloutlen == enclen );
5637  }
5638  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5639  totaloutlen += outlen;
5640  if( POLARSSL_MODE_CBC == cipher_info->mode )
5641  {
5642  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5643  }
5644  else
5645  {
5646  fct_chk( outlen == 0 );
5647  }
5648 
5649  /* decode the previously encoded string */
5650  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5651  if( POLARSSL_MODE_CBC == cipher_info->mode )
5652  {
5653  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5654  }
5655  else
5656  {
5657  fct_chk( enclen == outlen );
5658  }
5659  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5660  if( POLARSSL_MODE_CBC == cipher_info->mode )
5661  {
5662  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5663  }
5664  else
5665  {
5666  fct_chk( outlen == 0 );
5667  }
5668 
5669 
5670  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5671 
5672  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5673  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5674  FCT_TEST_END();
5675 #endif /* POLARSSL_AES_C */
5676 #endif /* POLARSSL_CIPHER_MODE_CFB */
5677 
5678 #ifdef POLARSSL_AES_C
5679 #ifdef POLARSSL_CIPHER_MODE_CFB
5680 
5681  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
5682  size_t first_length = 16;
5683  size_t second_length = 16;
5684  size_t length = first_length + second_length;
5685  unsigned char key[32];
5686  unsigned char iv[16];
5687 
5688  cipher_context_t ctx_dec;
5689  cipher_context_t ctx_enc;
5690  const cipher_info_t *cipher_info;
5691 
5692  unsigned char inbuf[64];
5693  unsigned char encbuf[64];
5694  unsigned char decbuf[64];
5695 
5696  size_t outlen = 0;
5697  size_t totaloutlen = 0;
5698  size_t enclen = 0;
5699 
5700  memset( key, 0, 32 );
5701  memset( iv , 0, 16 );
5702 
5703  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5704  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5705 
5706  memset( inbuf, 5, 64 );
5707  memset( encbuf, 0, 64 );
5708  memset( decbuf, 0, 64 );
5709 
5710  /* Initialise enc and dec contexts */
5712  fct_chk( NULL != cipher_info);
5713 
5714  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5715  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5716 
5717  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
5718  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
5719 
5720  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5721  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5722 
5723  if( POLARSSL_MODE_CBC == cipher_info->mode )
5724  {
5725  enclen = cipher_get_block_size(&ctx_enc )
5726  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5727  }
5728  else
5729  {
5730  enclen = length;
5731  }
5732 
5733  /* encode length number of bytes from inbuf */
5734  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5735  totaloutlen = outlen;
5736  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5737  totaloutlen += outlen;
5738  if( POLARSSL_MODE_CBC == cipher_info->mode )
5739  {
5740  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5741  }
5742  else
5743  {
5744  fct_chk( totaloutlen == enclen );
5745  }
5746  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5747  totaloutlen += outlen;
5748  if( POLARSSL_MODE_CBC == cipher_info->mode )
5749  {
5750  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5751  }
5752  else
5753  {
5754  fct_chk( outlen == 0 );
5755  }
5756 
5757  /* decode the previously encoded string */
5758  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5759  if( POLARSSL_MODE_CBC == cipher_info->mode )
5760  {
5761  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5762  }
5763  else
5764  {
5765  fct_chk( enclen == outlen );
5766  }
5767  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5768  if( POLARSSL_MODE_CBC == cipher_info->mode )
5769  {
5770  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5771  }
5772  else
5773  {
5774  fct_chk( outlen == 0 );
5775  }
5776 
5777 
5778  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5779 
5780  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5781  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5782  FCT_TEST_END();
5783 #endif /* POLARSSL_AES_C */
5784 #endif /* POLARSSL_CIPHER_MODE_CFB */
5785 
5786 #ifdef POLARSSL_AES_C
5787 #ifdef POLARSSL_CIPHER_MODE_CTR
5788 
5789  FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes)
5790  size_t length = 0;
5791  unsigned char key[32];
5792  unsigned char iv[16];
5793 
5794  const cipher_info_t *cipher_info;
5795  cipher_context_t ctx_dec;
5796  cipher_context_t ctx_enc;
5797 
5798  unsigned char inbuf[64];
5799  unsigned char encbuf[64];
5800  unsigned char decbuf[64];
5801 
5802  size_t outlen = 0;
5803  size_t enclen = 0;
5804 
5805  memset( key, 0, 32 );
5806  memset( iv , 0, 16 );
5807 
5808  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5809  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5810 
5811  memset( inbuf, 5, 64 );
5812  memset( encbuf, 0, 64 );
5813  memset( decbuf, 0, 64 );
5814 
5815  /* Check and get info structures */
5817  fct_chk( NULL != cipher_info );
5818  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
5819 
5820  /* Initialise enc and dec contexts */
5821  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5822  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5823 
5824  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
5825  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
5826 
5827  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5828  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5829 
5830  if( POLARSSL_MODE_CBC == cipher_info->mode )
5831  {
5832  enclen = cipher_get_block_size( &ctx_enc )
5833  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5834  }
5835  else
5836  {
5837  enclen = length;
5838  }
5839 
5840  /* encode length number of bytes from inbuf */
5841  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
5842  if( POLARSSL_MODE_CBC == cipher_info->mode )
5843  {
5844  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5845  }
5846  else
5847  {
5848  fct_chk( outlen == enclen );
5849  }
5850 
5851  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
5852  if( POLARSSL_MODE_CBC == cipher_info->mode )
5853  {
5854  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5855  }
5856  else
5857  {
5858  fct_chk( outlen == 0 );
5859  }
5860 
5861 
5862  /* decode the previously encoded string */
5863  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5864  if( POLARSSL_MODE_CBC == cipher_info->mode )
5865  {
5866  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5867  }
5868  else
5869  {
5870  fct_chk( enclen == outlen );
5871  }
5872 
5873  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5874  if( POLARSSL_MODE_CBC == cipher_info->mode )
5875  {
5876  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5877  }
5878  else
5879  {
5880  fct_chk( outlen == 0 );
5881  }
5882 
5883 
5884  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5885 
5886  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5887  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5888  FCT_TEST_END();
5889 #endif /* POLARSSL_AES_C */
5890 #endif /* POLARSSL_CIPHER_MODE_CTR */
5891 
5892 #ifdef POLARSSL_AES_C
5893 #ifdef POLARSSL_CIPHER_MODE_CTR
5894 
5895  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_byte)
5896  size_t length = 1;
5897  unsigned char key[32];
5898  unsigned char iv[16];
5899 
5900  const cipher_info_t *cipher_info;
5901  cipher_context_t ctx_dec;
5902  cipher_context_t ctx_enc;
5903 
5904  unsigned char inbuf[64];
5905  unsigned char encbuf[64];
5906  unsigned char decbuf[64];
5907 
5908  size_t outlen = 0;
5909  size_t enclen = 0;
5910 
5911  memset( key, 0, 32 );
5912  memset( iv , 0, 16 );
5913 
5914  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5915  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5916 
5917  memset( inbuf, 5, 64 );
5918  memset( encbuf, 0, 64 );
5919  memset( decbuf, 0, 64 );
5920 
5921  /* Check and get info structures */
5923  fct_chk( NULL != cipher_info );
5924  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
5925 
5926  /* Initialise enc and dec contexts */
5927  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5928  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5929 
5930  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
5931  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
5932 
5933  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5934  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5935 
5936  if( POLARSSL_MODE_CBC == cipher_info->mode )
5937  {
5938  enclen = cipher_get_block_size( &ctx_enc )
5939  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5940  }
5941  else
5942  {
5943  enclen = length;
5944  }
5945 
5946  /* encode length number of bytes from inbuf */
5947  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
5948  if( POLARSSL_MODE_CBC == cipher_info->mode )
5949  {
5950  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5951  }
5952  else
5953  {
5954  fct_chk( outlen == enclen );
5955  }
5956 
5957  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
5958  if( POLARSSL_MODE_CBC == cipher_info->mode )
5959  {
5960  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5961  }
5962  else
5963  {
5964  fct_chk( outlen == 0 );
5965  }
5966 
5967 
5968  /* decode the previously encoded string */
5969  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5970  if( POLARSSL_MODE_CBC == cipher_info->mode )
5971  {
5972  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5973  }
5974  else
5975  {
5976  fct_chk( enclen == outlen );
5977  }
5978 
5979  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5980  if( POLARSSL_MODE_CBC == cipher_info->mode )
5981  {
5982  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5983  }
5984  else
5985  {
5986  fct_chk( outlen == 0 );
5987  }
5988 
5989 
5990  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5991 
5992  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5993  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5994  FCT_TEST_END();
5995 #endif /* POLARSSL_AES_C */
5996 #endif /* POLARSSL_CIPHER_MODE_CTR */
5997 
5998 #ifdef POLARSSL_AES_C
5999 #ifdef POLARSSL_CIPHER_MODE_CTR
6000 
6001  FCT_TEST_BGN(aes_encrypt_and_decrypt_2_bytes)
6002  size_t length = 2;
6003  unsigned char key[32];
6004  unsigned char iv[16];
6005 
6006  const cipher_info_t *cipher_info;
6007  cipher_context_t ctx_dec;
6008  cipher_context_t ctx_enc;
6009 
6010  unsigned char inbuf[64];
6011  unsigned char encbuf[64];
6012  unsigned char decbuf[64];
6013 
6014  size_t outlen = 0;
6015  size_t enclen = 0;
6016 
6017  memset( key, 0, 32 );
6018  memset( iv , 0, 16 );
6019 
6020  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6021  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6022 
6023  memset( inbuf, 5, 64 );
6024  memset( encbuf, 0, 64 );
6025  memset( decbuf, 0, 64 );
6026 
6027  /* Check and get info structures */
6029  fct_chk( NULL != cipher_info );
6030  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
6031 
6032  /* Initialise enc and dec contexts */
6033  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6034  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6035 
6036  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
6037  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
6038 
6039  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6040  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6041 
6042  if( POLARSSL_MODE_CBC == cipher_info->mode )
6043  {
6044  enclen = cipher_get_block_size( &ctx_enc )
6045  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6046  }
6047  else
6048  {
6049  enclen = length;
6050  }
6051 
6052  /* encode length number of bytes from inbuf */
6053  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6054  if( POLARSSL_MODE_CBC == cipher_info->mode )
6055  {
6056  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6057  }
6058  else
6059  {
6060  fct_chk( outlen == enclen );
6061  }
6062 
6063  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6064  if( POLARSSL_MODE_CBC == cipher_info->mode )
6065  {
6066  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6067  }
6068  else
6069  {
6070  fct_chk( outlen == 0 );
6071  }
6072 
6073 
6074  /* decode the previously encoded string */
6075  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6076  if( POLARSSL_MODE_CBC == cipher_info->mode )
6077  {
6078  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6079  }
6080  else
6081  {
6082  fct_chk( enclen == outlen );
6083  }
6084 
6085  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6086  if( POLARSSL_MODE_CBC == cipher_info->mode )
6087  {
6088  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6089  }
6090  else
6091  {
6092  fct_chk( outlen == 0 );
6093  }
6094 
6095 
6096  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6097 
6098  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6099  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6100  FCT_TEST_END();
6101 #endif /* POLARSSL_AES_C */
6102 #endif /* POLARSSL_CIPHER_MODE_CTR */
6103 
6104 #ifdef POLARSSL_AES_C
6105 #ifdef POLARSSL_CIPHER_MODE_CTR
6106 
6107  FCT_TEST_BGN(aes_encrypt_and_decrypt_7_bytes)
6108  size_t length = 7;
6109  unsigned char key[32];
6110  unsigned char iv[16];
6111 
6112  const cipher_info_t *cipher_info;
6113  cipher_context_t ctx_dec;
6114  cipher_context_t ctx_enc;
6115 
6116  unsigned char inbuf[64];
6117  unsigned char encbuf[64];
6118  unsigned char decbuf[64];
6119 
6120  size_t outlen = 0;
6121  size_t enclen = 0;
6122 
6123  memset( key, 0, 32 );
6124  memset( iv , 0, 16 );
6125 
6126  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6127  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6128 
6129  memset( inbuf, 5, 64 );
6130  memset( encbuf, 0, 64 );
6131  memset( decbuf, 0, 64 );
6132 
6133  /* Check and get info structures */
6135  fct_chk( NULL != cipher_info );
6136  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
6137 
6138  /* Initialise enc and dec contexts */
6139  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6140  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6141 
6142  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
6143  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
6144 
6145  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6146  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6147 
6148  if( POLARSSL_MODE_CBC == cipher_info->mode )
6149  {
6150  enclen = cipher_get_block_size( &ctx_enc )
6151  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6152  }
6153  else
6154  {
6155  enclen = length;
6156  }
6157 
6158  /* encode length number of bytes from inbuf */
6159  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6160  if( POLARSSL_MODE_CBC == cipher_info->mode )
6161  {
6162  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6163  }
6164  else
6165  {
6166  fct_chk( outlen == enclen );
6167  }
6168 
6169  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6170  if( POLARSSL_MODE_CBC == cipher_info->mode )
6171  {
6172  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6173  }
6174  else
6175  {
6176  fct_chk( outlen == 0 );
6177  }
6178 
6179 
6180  /* decode the previously encoded string */
6181  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6182  if( POLARSSL_MODE_CBC == cipher_info->mode )
6183  {
6184  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6185  }
6186  else
6187  {
6188  fct_chk( enclen == outlen );
6189  }
6190 
6191  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6192  if( POLARSSL_MODE_CBC == cipher_info->mode )
6193  {
6194  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6195  }
6196  else
6197  {
6198  fct_chk( outlen == 0 );
6199  }
6200 
6201 
6202  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6203 
6204  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6205  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6206  FCT_TEST_END();
6207 #endif /* POLARSSL_AES_C */
6208 #endif /* POLARSSL_CIPHER_MODE_CTR */
6209 
6210 #ifdef POLARSSL_AES_C
6211 #ifdef POLARSSL_CIPHER_MODE_CTR
6212 
6213  FCT_TEST_BGN(aes_encrypt_and_decrypt_8_bytes)
6214  size_t length = 8;
6215  unsigned char key[32];
6216  unsigned char iv[16];
6217 
6218  const cipher_info_t *cipher_info;
6219  cipher_context_t ctx_dec;
6220  cipher_context_t ctx_enc;
6221 
6222  unsigned char inbuf[64];
6223  unsigned char encbuf[64];
6224  unsigned char decbuf[64];
6225 
6226  size_t outlen = 0;
6227  size_t enclen = 0;
6228 
6229  memset( key, 0, 32 );
6230  memset( iv , 0, 16 );
6231 
6232  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6233  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6234 
6235  memset( inbuf, 5, 64 );
6236  memset( encbuf, 0, 64 );
6237  memset( decbuf, 0, 64 );
6238 
6239  /* Check and get info structures */
6241  fct_chk( NULL != cipher_info );
6242  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
6243 
6244  /* Initialise enc and dec contexts */
6245  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6246  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6247 
6248  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
6249  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
6250 
6251  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6252  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6253 
6254  if( POLARSSL_MODE_CBC == cipher_info->mode )
6255  {
6256  enclen = cipher_get_block_size( &ctx_enc )
6257  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6258  }
6259  else
6260  {
6261  enclen = length;
6262  }
6263 
6264  /* encode length number of bytes from inbuf */
6265  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6266  if( POLARSSL_MODE_CBC == cipher_info->mode )
6267  {
6268  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6269  }
6270  else
6271  {
6272  fct_chk( outlen == enclen );
6273  }
6274 
6275  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6276  if( POLARSSL_MODE_CBC == cipher_info->mode )
6277  {
6278  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6279  }
6280  else
6281  {
6282  fct_chk( outlen == 0 );
6283  }
6284 
6285 
6286  /* decode the previously encoded string */
6287  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6288  if( POLARSSL_MODE_CBC == cipher_info->mode )
6289  {
6290  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6291  }
6292  else
6293  {
6294  fct_chk( enclen == outlen );
6295  }
6296 
6297  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6298  if( POLARSSL_MODE_CBC == cipher_info->mode )
6299  {
6300  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6301  }
6302  else
6303  {
6304  fct_chk( outlen == 0 );
6305  }
6306 
6307 
6308  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6309 
6310  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6311  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6312  FCT_TEST_END();
6313 #endif /* POLARSSL_AES_C */
6314 #endif /* POLARSSL_CIPHER_MODE_CTR */
6315 
6316 #ifdef POLARSSL_AES_C
6317 #ifdef POLARSSL_CIPHER_MODE_CTR
6318 
6319  FCT_TEST_BGN(aes_encrypt_and_decrypt_9_bytes)
6320  size_t length = 9;
6321  unsigned char key[32];
6322  unsigned char iv[16];
6323 
6324  const cipher_info_t *cipher_info;
6325  cipher_context_t ctx_dec;
6326  cipher_context_t ctx_enc;
6327 
6328  unsigned char inbuf[64];
6329  unsigned char encbuf[64];
6330  unsigned char decbuf[64];
6331 
6332  size_t outlen = 0;
6333  size_t enclen = 0;
6334 
6335  memset( key, 0, 32 );
6336  memset( iv , 0, 16 );
6337 
6338  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6339  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6340 
6341  memset( inbuf, 5, 64 );
6342  memset( encbuf, 0, 64 );
6343  memset( decbuf, 0, 64 );
6344 
6345  /* Check and get info structures */
6347  fct_chk( NULL != cipher_info );
6348  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
6349 
6350  /* Initialise enc and dec contexts */
6351  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6352  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6353 
6354  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
6355  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
6356 
6357  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6358  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6359 
6360  if( POLARSSL_MODE_CBC == cipher_info->mode )
6361  {
6362  enclen = cipher_get_block_size( &ctx_enc )
6363  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6364  }
6365  else
6366  {
6367  enclen = length;
6368  }
6369 
6370  /* encode length number of bytes from inbuf */
6371  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6372  if( POLARSSL_MODE_CBC == cipher_info->mode )
6373  {
6374  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6375  }
6376  else
6377  {
6378  fct_chk( outlen == enclen );
6379  }
6380 
6381  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6382  if( POLARSSL_MODE_CBC == cipher_info->mode )
6383  {
6384  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6385  }
6386  else
6387  {
6388  fct_chk( outlen == 0 );
6389  }
6390 
6391 
6392  /* decode the previously encoded string */
6393  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6394  if( POLARSSL_MODE_CBC == cipher_info->mode )
6395  {
6396  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6397  }
6398  else
6399  {
6400  fct_chk( enclen == outlen );
6401  }
6402 
6403  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6404  if( POLARSSL_MODE_CBC == cipher_info->mode )
6405  {
6406  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6407  }
6408  else
6409  {
6410  fct_chk( outlen == 0 );
6411  }
6412 
6413 
6414  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6415 
6416  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6417  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6418  FCT_TEST_END();
6419 #endif /* POLARSSL_AES_C */
6420 #endif /* POLARSSL_CIPHER_MODE_CTR */
6421 
6422 #ifdef POLARSSL_AES_C
6423 #ifdef POLARSSL_CIPHER_MODE_CTR
6424 
6425  FCT_TEST_BGN(aes_encrypt_and_decrypt_15_bytes)
6426  size_t length = 15;
6427  unsigned char key[32];
6428  unsigned char iv[16];
6429 
6430  const cipher_info_t *cipher_info;
6431  cipher_context_t ctx_dec;
6432  cipher_context_t ctx_enc;
6433 
6434  unsigned char inbuf[64];
6435  unsigned char encbuf[64];
6436  unsigned char decbuf[64];
6437 
6438  size_t outlen = 0;
6439  size_t enclen = 0;
6440 
6441  memset( key, 0, 32 );
6442  memset( iv , 0, 16 );
6443 
6444  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6445  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6446 
6447  memset( inbuf, 5, 64 );
6448  memset( encbuf, 0, 64 );
6449  memset( decbuf, 0, 64 );
6450 
6451  /* Check and get info structures */
6453  fct_chk( NULL != cipher_info );
6454  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
6455 
6456  /* Initialise enc and dec contexts */
6457  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6458  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6459 
6460  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
6461  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
6462 
6463  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6464  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6465 
6466  if( POLARSSL_MODE_CBC == cipher_info->mode )
6467  {
6468  enclen = cipher_get_block_size( &ctx_enc )
6469  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6470  }
6471  else
6472  {
6473  enclen = length;
6474  }
6475 
6476  /* encode length number of bytes from inbuf */
6477  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6478  if( POLARSSL_MODE_CBC == cipher_info->mode )
6479  {
6480  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6481  }
6482  else
6483  {
6484  fct_chk( outlen == enclen );
6485  }
6486 
6487  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6488  if( POLARSSL_MODE_CBC == cipher_info->mode )
6489  {
6490  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6491  }
6492  else
6493  {
6494  fct_chk( outlen == 0 );
6495  }
6496 
6497 
6498  /* decode the previously encoded string */
6499  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6500  if( POLARSSL_MODE_CBC == cipher_info->mode )
6501  {
6502  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6503  }
6504  else
6505  {
6506  fct_chk( enclen == outlen );
6507  }
6508 
6509  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6510  if( POLARSSL_MODE_CBC == cipher_info->mode )
6511  {
6512  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6513  }
6514  else
6515  {
6516  fct_chk( outlen == 0 );
6517  }
6518 
6519 
6520  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6521 
6522  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6523  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6524  FCT_TEST_END();
6525 #endif /* POLARSSL_AES_C */
6526 #endif /* POLARSSL_CIPHER_MODE_CTR */
6527 
6528 #ifdef POLARSSL_AES_C
6529 #ifdef POLARSSL_CIPHER_MODE_CTR
6530 
6531  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes)
6532  size_t length = 16;
6533  unsigned char key[32];
6534  unsigned char iv[16];
6535 
6536  const cipher_info_t *cipher_info;
6537  cipher_context_t ctx_dec;
6538  cipher_context_t ctx_enc;
6539 
6540  unsigned char inbuf[64];
6541  unsigned char encbuf[64];
6542  unsigned char decbuf[64];
6543 
6544  size_t outlen = 0;
6545  size_t enclen = 0;
6546 
6547  memset( key, 0, 32 );
6548  memset( iv , 0, 16 );
6549 
6550  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6551  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6552 
6553  memset( inbuf, 5, 64 );
6554  memset( encbuf, 0, 64 );
6555  memset( decbuf, 0, 64 );
6556 
6557  /* Check and get info structures */
6559  fct_chk( NULL != cipher_info );
6560  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
6561 
6562  /* Initialise enc and dec contexts */
6563  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6564  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6565 
6566  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
6567  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
6568 
6569  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6570  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6571 
6572  if( POLARSSL_MODE_CBC == cipher_info->mode )
6573  {
6574  enclen = cipher_get_block_size( &ctx_enc )
6575  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6576  }
6577  else
6578  {
6579  enclen = length;
6580  }
6581 
6582  /* encode length number of bytes from inbuf */
6583  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6584  if( POLARSSL_MODE_CBC == cipher_info->mode )
6585  {
6586  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6587  }
6588  else
6589  {
6590  fct_chk( outlen == enclen );
6591  }
6592 
6593  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6594  if( POLARSSL_MODE_CBC == cipher_info->mode )
6595  {
6596  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6597  }
6598  else
6599  {
6600  fct_chk( outlen == 0 );
6601  }
6602 
6603 
6604  /* decode the previously encoded string */
6605  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6606  if( POLARSSL_MODE_CBC == cipher_info->mode )
6607  {
6608  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6609  }
6610  else
6611  {
6612  fct_chk( enclen == outlen );
6613  }
6614 
6615  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6616  if( POLARSSL_MODE_CBC == cipher_info->mode )
6617  {
6618  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6619  }
6620  else
6621  {
6622  fct_chk( outlen == 0 );
6623  }
6624 
6625 
6626  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6627 
6628  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6629  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6630  FCT_TEST_END();
6631 #endif /* POLARSSL_AES_C */
6632 #endif /* POLARSSL_CIPHER_MODE_CTR */
6633 
6634 #ifdef POLARSSL_AES_C
6635 #ifdef POLARSSL_CIPHER_MODE_CTR
6636 
6637  FCT_TEST_BGN(aes_encrypt_and_decrypt_17_bytes)
6638  size_t length = 17;
6639  unsigned char key[32];
6640  unsigned char iv[16];
6641 
6642  const cipher_info_t *cipher_info;
6643  cipher_context_t ctx_dec;
6644  cipher_context_t ctx_enc;
6645 
6646  unsigned char inbuf[64];
6647  unsigned char encbuf[64];
6648  unsigned char decbuf[64];
6649 
6650  size_t outlen = 0;
6651  size_t enclen = 0;
6652 
6653  memset( key, 0, 32 );
6654  memset( iv , 0, 16 );
6655 
6656  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6657  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6658 
6659  memset( inbuf, 5, 64 );
6660  memset( encbuf, 0, 64 );
6661  memset( decbuf, 0, 64 );
6662 
6663  /* Check and get info structures */
6665  fct_chk( NULL != cipher_info );
6666  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
6667 
6668  /* Initialise enc and dec contexts */
6669  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6670  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6671 
6672  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
6673  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
6674 
6675  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6676  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6677 
6678  if( POLARSSL_MODE_CBC == cipher_info->mode )
6679  {
6680  enclen = cipher_get_block_size( &ctx_enc )
6681  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6682  }
6683  else
6684  {
6685  enclen = length;
6686  }
6687 
6688  /* encode length number of bytes from inbuf */
6689  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6690  if( POLARSSL_MODE_CBC == cipher_info->mode )
6691  {
6692  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6693  }
6694  else
6695  {
6696  fct_chk( outlen == enclen );
6697  }
6698 
6699  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6700  if( POLARSSL_MODE_CBC == cipher_info->mode )
6701  {
6702  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6703  }
6704  else
6705  {
6706  fct_chk( outlen == 0 );
6707  }
6708 
6709 
6710  /* decode the previously encoded string */
6711  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6712  if( POLARSSL_MODE_CBC == cipher_info->mode )
6713  {
6714  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6715  }
6716  else
6717  {
6718  fct_chk( enclen == outlen );
6719  }
6720 
6721  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6722  if( POLARSSL_MODE_CBC == cipher_info->mode )
6723  {
6724  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6725  }
6726  else
6727  {
6728  fct_chk( outlen == 0 );
6729  }
6730 
6731 
6732  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6733 
6734  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6735  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6736  FCT_TEST_END();
6737 #endif /* POLARSSL_AES_C */
6738 #endif /* POLARSSL_CIPHER_MODE_CTR */
6739 
6740 #ifdef POLARSSL_AES_C
6741 #ifdef POLARSSL_CIPHER_MODE_CTR
6742 
6743  FCT_TEST_BGN(aes_encrypt_and_decrypt_31_bytes)
6744  size_t length = 31;
6745  unsigned char key[32];
6746  unsigned char iv[16];
6747 
6748  const cipher_info_t *cipher_info;
6749  cipher_context_t ctx_dec;
6750  cipher_context_t ctx_enc;
6751 
6752  unsigned char inbuf[64];
6753  unsigned char encbuf[64];
6754  unsigned char decbuf[64];
6755 
6756  size_t outlen = 0;
6757  size_t enclen = 0;
6758 
6759  memset( key, 0, 32 );
6760  memset( iv , 0, 16 );
6761 
6762  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6763  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6764 
6765  memset( inbuf, 5, 64 );
6766  memset( encbuf, 0, 64 );
6767  memset( decbuf, 0, 64 );
6768 
6769  /* Check and get info structures */
6771  fct_chk( NULL != cipher_info );
6772  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
6773 
6774  /* Initialise enc and dec contexts */
6775  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6776  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6777 
6778  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
6779  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
6780 
6781  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6782  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6783 
6784  if( POLARSSL_MODE_CBC == cipher_info->mode )
6785  {
6786  enclen = cipher_get_block_size( &ctx_enc )
6787  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6788  }
6789  else
6790  {
6791  enclen = length;
6792  }
6793 
6794  /* encode length number of bytes from inbuf */
6795  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6796  if( POLARSSL_MODE_CBC == cipher_info->mode )
6797  {
6798  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6799  }
6800  else
6801  {
6802  fct_chk( outlen == enclen );
6803  }
6804 
6805  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6806  if( POLARSSL_MODE_CBC == cipher_info->mode )
6807  {
6808  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6809  }
6810  else
6811  {
6812  fct_chk( outlen == 0 );
6813  }
6814 
6815 
6816  /* decode the previously encoded string */
6817  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6818  if( POLARSSL_MODE_CBC == cipher_info->mode )
6819  {
6820  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6821  }
6822  else
6823  {
6824  fct_chk( enclen == outlen );
6825  }
6826 
6827  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6828  if( POLARSSL_MODE_CBC == cipher_info->mode )
6829  {
6830  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6831  }
6832  else
6833  {
6834  fct_chk( outlen == 0 );
6835  }
6836 
6837 
6838  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6839 
6840  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6841  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6842  FCT_TEST_END();
6843 #endif /* POLARSSL_AES_C */
6844 #endif /* POLARSSL_CIPHER_MODE_CTR */
6845 
6846 #ifdef POLARSSL_AES_C
6847 #ifdef POLARSSL_CIPHER_MODE_CTR
6848 
6849  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes)
6850  size_t length = 32;
6851  unsigned char key[32];
6852  unsigned char iv[16];
6853 
6854  const cipher_info_t *cipher_info;
6855  cipher_context_t ctx_dec;
6856  cipher_context_t ctx_enc;
6857 
6858  unsigned char inbuf[64];
6859  unsigned char encbuf[64];
6860  unsigned char decbuf[64];
6861 
6862  size_t outlen = 0;
6863  size_t enclen = 0;
6864 
6865  memset( key, 0, 32 );
6866  memset( iv , 0, 16 );
6867 
6868  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6869  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6870 
6871  memset( inbuf, 5, 64 );
6872  memset( encbuf, 0, 64 );
6873  memset( decbuf, 0, 64 );
6874 
6875  /* Check and get info structures */
6877  fct_chk( NULL != cipher_info );
6878  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
6879 
6880  /* Initialise enc and dec contexts */
6881  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6882  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6883 
6884  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
6885  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
6886 
6887  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6888  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6889 
6890  if( POLARSSL_MODE_CBC == cipher_info->mode )
6891  {
6892  enclen = cipher_get_block_size( &ctx_enc )
6893  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6894  }
6895  else
6896  {
6897  enclen = length;
6898  }
6899 
6900  /* encode length number of bytes from inbuf */
6901  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6902  if( POLARSSL_MODE_CBC == cipher_info->mode )
6903  {
6904  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6905  }
6906  else
6907  {
6908  fct_chk( outlen == enclen );
6909  }
6910 
6911  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6912  if( POLARSSL_MODE_CBC == cipher_info->mode )
6913  {
6914  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6915  }
6916  else
6917  {
6918  fct_chk( outlen == 0 );
6919  }
6920 
6921 
6922  /* decode the previously encoded string */
6923  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6924  if( POLARSSL_MODE_CBC == cipher_info->mode )
6925  {
6926  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6927  }
6928  else
6929  {
6930  fct_chk( enclen == outlen );
6931  }
6932 
6933  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6934  if( POLARSSL_MODE_CBC == cipher_info->mode )
6935  {
6936  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6937  }
6938  else
6939  {
6940  fct_chk( outlen == 0 );
6941  }
6942 
6943 
6944  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6945 
6946  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6947  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6948  FCT_TEST_END();
6949 #endif /* POLARSSL_AES_C */
6950 #endif /* POLARSSL_CIPHER_MODE_CTR */
6951 
6952 #ifdef POLARSSL_AES_C
6953 #ifdef POLARSSL_CIPHER_MODE_CTR
6954 
6955  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes)
6956  size_t length = 33;
6957  unsigned char key[32];
6958  unsigned char iv[16];
6959 
6960  const cipher_info_t *cipher_info;
6961  cipher_context_t ctx_dec;
6962  cipher_context_t ctx_enc;
6963 
6964  unsigned char inbuf[64];
6965  unsigned char encbuf[64];
6966  unsigned char decbuf[64];
6967 
6968  size_t outlen = 0;
6969  size_t enclen = 0;
6970 
6971  memset( key, 0, 32 );
6972  memset( iv , 0, 16 );
6973 
6974  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6975  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6976 
6977  memset( inbuf, 5, 64 );
6978  memset( encbuf, 0, 64 );
6979  memset( decbuf, 0, 64 );
6980 
6981  /* Check and get info structures */
6983  fct_chk( NULL != cipher_info );
6984  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
6985 
6986  /* Initialise enc and dec contexts */
6987  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6988  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6989 
6990  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
6991  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
6992 
6993  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6994  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6995 
6996  if( POLARSSL_MODE_CBC == cipher_info->mode )
6997  {
6998  enclen = cipher_get_block_size( &ctx_enc )
6999  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7000  }
7001  else
7002  {
7003  enclen = length;
7004  }
7005 
7006  /* encode length number of bytes from inbuf */
7007  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
7008  if( POLARSSL_MODE_CBC == cipher_info->mode )
7009  {
7010  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7011  }
7012  else
7013  {
7014  fct_chk( outlen == enclen );
7015  }
7016 
7017  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
7018  if( POLARSSL_MODE_CBC == cipher_info->mode )
7019  {
7020  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7021  }
7022  else
7023  {
7024  fct_chk( outlen == 0 );
7025  }
7026 
7027 
7028  /* decode the previously encoded string */
7029  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7030  if( POLARSSL_MODE_CBC == cipher_info->mode )
7031  {
7032  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7033  }
7034  else
7035  {
7036  fct_chk( enclen == outlen );
7037  }
7038 
7039  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7040  if( POLARSSL_MODE_CBC == cipher_info->mode )
7041  {
7042  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7043  }
7044  else
7045  {
7046  fct_chk( outlen == 0 );
7047  }
7048 
7049 
7050  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7051 
7052  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7053  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7054  FCT_TEST_END();
7055 #endif /* POLARSSL_AES_C */
7056 #endif /* POLARSSL_CIPHER_MODE_CTR */
7057 
7058 #ifdef POLARSSL_AES_C
7059 #ifdef POLARSSL_CIPHER_MODE_CTR
7060 
7061  FCT_TEST_BGN(aes_encrypt_and_decrypt_47_bytes)
7062  size_t length = 47;
7063  unsigned char key[32];
7064  unsigned char iv[16];
7065 
7066  const cipher_info_t *cipher_info;
7067  cipher_context_t ctx_dec;
7068  cipher_context_t ctx_enc;
7069 
7070  unsigned char inbuf[64];
7071  unsigned char encbuf[64];
7072  unsigned char decbuf[64];
7073 
7074  size_t outlen = 0;
7075  size_t enclen = 0;
7076 
7077  memset( key, 0, 32 );
7078  memset( iv , 0, 16 );
7079 
7080  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7081  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7082 
7083  memset( inbuf, 5, 64 );
7084  memset( encbuf, 0, 64 );
7085  memset( decbuf, 0, 64 );
7086 
7087  /* Check and get info structures */
7089  fct_chk( NULL != cipher_info );
7090  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
7091 
7092  /* Initialise enc and dec contexts */
7093  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7094  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7095 
7096  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
7097  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
7098 
7099  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7100  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7101 
7102  if( POLARSSL_MODE_CBC == cipher_info->mode )
7103  {
7104  enclen = cipher_get_block_size( &ctx_enc )
7105  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7106  }
7107  else
7108  {
7109  enclen = length;
7110  }
7111 
7112  /* encode length number of bytes from inbuf */
7113  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
7114  if( POLARSSL_MODE_CBC == cipher_info->mode )
7115  {
7116  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7117  }
7118  else
7119  {
7120  fct_chk( outlen == enclen );
7121  }
7122 
7123  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
7124  if( POLARSSL_MODE_CBC == cipher_info->mode )
7125  {
7126  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7127  }
7128  else
7129  {
7130  fct_chk( outlen == 0 );
7131  }
7132 
7133 
7134  /* decode the previously encoded string */
7135  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7136  if( POLARSSL_MODE_CBC == cipher_info->mode )
7137  {
7138  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7139  }
7140  else
7141  {
7142  fct_chk( enclen == outlen );
7143  }
7144 
7145  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7146  if( POLARSSL_MODE_CBC == cipher_info->mode )
7147  {
7148  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7149  }
7150  else
7151  {
7152  fct_chk( outlen == 0 );
7153  }
7154 
7155 
7156  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7157 
7158  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7159  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7160  FCT_TEST_END();
7161 #endif /* POLARSSL_AES_C */
7162 #endif /* POLARSSL_CIPHER_MODE_CTR */
7163 
7164 #ifdef POLARSSL_AES_C
7165 #ifdef POLARSSL_CIPHER_MODE_CTR
7166 
7167  FCT_TEST_BGN(aes_encrypt_and_decrypt_48_bytes)
7168  size_t length = 48;
7169  unsigned char key[32];
7170  unsigned char iv[16];
7171 
7172  const cipher_info_t *cipher_info;
7173  cipher_context_t ctx_dec;
7174  cipher_context_t ctx_enc;
7175 
7176  unsigned char inbuf[64];
7177  unsigned char encbuf[64];
7178  unsigned char decbuf[64];
7179 
7180  size_t outlen = 0;
7181  size_t enclen = 0;
7182 
7183  memset( key, 0, 32 );
7184  memset( iv , 0, 16 );
7185 
7186  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7187  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7188 
7189  memset( inbuf, 5, 64 );
7190  memset( encbuf, 0, 64 );
7191  memset( decbuf, 0, 64 );
7192 
7193  /* Check and get info structures */
7195  fct_chk( NULL != cipher_info );
7196  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
7197 
7198  /* Initialise enc and dec contexts */
7199  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7200  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7201 
7202  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
7203  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
7204 
7205  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7206  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7207 
7208  if( POLARSSL_MODE_CBC == cipher_info->mode )
7209  {
7210  enclen = cipher_get_block_size( &ctx_enc )
7211  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7212  }
7213  else
7214  {
7215  enclen = length;
7216  }
7217 
7218  /* encode length number of bytes from inbuf */
7219  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
7220  if( POLARSSL_MODE_CBC == cipher_info->mode )
7221  {
7222  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7223  }
7224  else
7225  {
7226  fct_chk( outlen == enclen );
7227  }
7228 
7229  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
7230  if( POLARSSL_MODE_CBC == cipher_info->mode )
7231  {
7232  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7233  }
7234  else
7235  {
7236  fct_chk( outlen == 0 );
7237  }
7238 
7239 
7240  /* decode the previously encoded string */
7241  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7242  if( POLARSSL_MODE_CBC == cipher_info->mode )
7243  {
7244  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7245  }
7246  else
7247  {
7248  fct_chk( enclen == outlen );
7249  }
7250 
7251  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7252  if( POLARSSL_MODE_CBC == cipher_info->mode )
7253  {
7254  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7255  }
7256  else
7257  {
7258  fct_chk( outlen == 0 );
7259  }
7260 
7261 
7262  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7263 
7264  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7265  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7266  FCT_TEST_END();
7267 #endif /* POLARSSL_AES_C */
7268 #endif /* POLARSSL_CIPHER_MODE_CTR */
7269 
7270 #ifdef POLARSSL_AES_C
7271 #ifdef POLARSSL_CIPHER_MODE_CTR
7272 
7273  FCT_TEST_BGN(aes_encrypt_and_decrypt_49_bytes)
7274  size_t length = 49;
7275  unsigned char key[32];
7276  unsigned char iv[16];
7277 
7278  const cipher_info_t *cipher_info;
7279  cipher_context_t ctx_dec;
7280  cipher_context_t ctx_enc;
7281 
7282  unsigned char inbuf[64];
7283  unsigned char encbuf[64];
7284  unsigned char decbuf[64];
7285 
7286  size_t outlen = 0;
7287  size_t enclen = 0;
7288 
7289  memset( key, 0, 32 );
7290  memset( iv , 0, 16 );
7291 
7292  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7293  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7294 
7295  memset( inbuf, 5, 64 );
7296  memset( encbuf, 0, 64 );
7297  memset( decbuf, 0, 64 );
7298 
7299  /* Check and get info structures */
7301  fct_chk( NULL != cipher_info );
7302  fct_chk( cipher_info_from_string( "AES-128-CTR" ) == cipher_info );
7303 
7304  /* Initialise enc and dec contexts */
7305  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7306  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7307 
7308  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
7309  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
7310 
7311  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7312  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7313 
7314  if( POLARSSL_MODE_CBC == cipher_info->mode )
7315  {
7316  enclen = cipher_get_block_size( &ctx_enc )
7317  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7318  }
7319  else
7320  {
7321  enclen = length;
7322  }
7323 
7324  /* encode length number of bytes from inbuf */
7325  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
7326  if( POLARSSL_MODE_CBC == cipher_info->mode )
7327  {
7328  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7329  }
7330  else
7331  {
7332  fct_chk( outlen == enclen );
7333  }
7334 
7335  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
7336  if( POLARSSL_MODE_CBC == cipher_info->mode )
7337  {
7338  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7339  }
7340  else
7341  {
7342  fct_chk( outlen == 0 );
7343  }
7344 
7345 
7346  /* decode the previously encoded string */
7347  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7348  if( POLARSSL_MODE_CBC == cipher_info->mode )
7349  {
7350  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7351  }
7352  else
7353  {
7354  fct_chk( enclen == outlen );
7355  }
7356 
7357  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7358  if( POLARSSL_MODE_CBC == cipher_info->mode )
7359  {
7360  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7361  }
7362  else
7363  {
7364  fct_chk( outlen == 0 );
7365  }
7366 
7367 
7368  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7369 
7370  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7371  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7372  FCT_TEST_END();
7373 #endif /* POLARSSL_AES_C */
7374 #endif /* POLARSSL_CIPHER_MODE_CTR */
7375 
7376 #ifdef POLARSSL_AES_C
7377 #ifdef POLARSSL_CIPHER_MODE_CTR
7378 
7379  FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes_in_multiple_parts)
7380  size_t first_length = 0;
7381  size_t second_length = 0;
7382  size_t length = first_length + second_length;
7383  unsigned char key[32];
7384  unsigned char iv[16];
7385 
7386  cipher_context_t ctx_dec;
7387  cipher_context_t ctx_enc;
7388  const cipher_info_t *cipher_info;
7389 
7390  unsigned char inbuf[64];
7391  unsigned char encbuf[64];
7392  unsigned char decbuf[64];
7393 
7394  size_t outlen = 0;
7395  size_t totaloutlen = 0;
7396  size_t enclen = 0;
7397 
7398  memset( key, 0, 32 );
7399  memset( iv , 0, 16 );
7400 
7401  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7402  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7403 
7404  memset( inbuf, 5, 64 );
7405  memset( encbuf, 0, 64 );
7406  memset( decbuf, 0, 64 );
7407 
7408  /* Initialise enc and dec contexts */
7410  fct_chk( NULL != cipher_info);
7411 
7412  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7413  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7414 
7415  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
7416  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
7417 
7418  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7419  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7420 
7421  if( POLARSSL_MODE_CBC == cipher_info->mode )
7422  {
7423  enclen = cipher_get_block_size(&ctx_enc )
7424  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7425  }
7426  else
7427  {
7428  enclen = length;
7429  }
7430 
7431  /* encode length number of bytes from inbuf */
7432  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7433  totaloutlen = outlen;
7434  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7435  totaloutlen += outlen;
7436  if( POLARSSL_MODE_CBC == cipher_info->mode )
7437  {
7438  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7439  }
7440  else
7441  {
7442  fct_chk( totaloutlen == enclen );
7443  }
7444  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7445  totaloutlen += outlen;
7446  if( POLARSSL_MODE_CBC == cipher_info->mode )
7447  {
7448  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7449  }
7450  else
7451  {
7452  fct_chk( outlen == 0 );
7453  }
7454 
7455  /* decode the previously encoded string */
7456  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7457  if( POLARSSL_MODE_CBC == cipher_info->mode )
7458  {
7459  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7460  }
7461  else
7462  {
7463  fct_chk( enclen == outlen );
7464  }
7465  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7466  if( POLARSSL_MODE_CBC == cipher_info->mode )
7467  {
7468  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7469  }
7470  else
7471  {
7472  fct_chk( outlen == 0 );
7473  }
7474 
7475 
7476  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7477 
7478  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7479  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7480  FCT_TEST_END();
7481 #endif /* POLARSSL_AES_C */
7482 #endif /* POLARSSL_CIPHER_MODE_CTR */
7483 
7484 #ifdef POLARSSL_AES_C
7485 #ifdef POLARSSL_CIPHER_MODE_CTR
7486 
7487  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
7488  size_t first_length = 1;
7489  size_t second_length = 0;
7490  size_t length = first_length + second_length;
7491  unsigned char key[32];
7492  unsigned char iv[16];
7493 
7494  cipher_context_t ctx_dec;
7495  cipher_context_t ctx_enc;
7496  const cipher_info_t *cipher_info;
7497 
7498  unsigned char inbuf[64];
7499  unsigned char encbuf[64];
7500  unsigned char decbuf[64];
7501 
7502  size_t outlen = 0;
7503  size_t totaloutlen = 0;
7504  size_t enclen = 0;
7505 
7506  memset( key, 0, 32 );
7507  memset( iv , 0, 16 );
7508 
7509  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7510  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7511 
7512  memset( inbuf, 5, 64 );
7513  memset( encbuf, 0, 64 );
7514  memset( decbuf, 0, 64 );
7515 
7516  /* Initialise enc and dec contexts */
7518  fct_chk( NULL != cipher_info);
7519 
7520  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7521  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7522 
7523  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
7524  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
7525 
7526  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7527  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7528 
7529  if( POLARSSL_MODE_CBC == cipher_info->mode )
7530  {
7531  enclen = cipher_get_block_size(&ctx_enc )
7532  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7533  }
7534  else
7535  {
7536  enclen = length;
7537  }
7538 
7539  /* encode length number of bytes from inbuf */
7540  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7541  totaloutlen = outlen;
7542  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7543  totaloutlen += outlen;
7544  if( POLARSSL_MODE_CBC == cipher_info->mode )
7545  {
7546  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7547  }
7548  else
7549  {
7550  fct_chk( totaloutlen == enclen );
7551  }
7552  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7553  totaloutlen += outlen;
7554  if( POLARSSL_MODE_CBC == cipher_info->mode )
7555  {
7556  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7557  }
7558  else
7559  {
7560  fct_chk( outlen == 0 );
7561  }
7562 
7563  /* decode the previously encoded string */
7564  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7565  if( POLARSSL_MODE_CBC == cipher_info->mode )
7566  {
7567  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7568  }
7569  else
7570  {
7571  fct_chk( enclen == outlen );
7572  }
7573  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7574  if( POLARSSL_MODE_CBC == cipher_info->mode )
7575  {
7576  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7577  }
7578  else
7579  {
7580  fct_chk( outlen == 0 );
7581  }
7582 
7583 
7584  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7585 
7586  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7587  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7588  FCT_TEST_END();
7589 #endif /* POLARSSL_AES_C */
7590 #endif /* POLARSSL_CIPHER_MODE_CTR */
7591 
7592 #ifdef POLARSSL_AES_C
7593 #ifdef POLARSSL_CIPHER_MODE_CTR
7594 
7595  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
7596  size_t first_length = 0;
7597  size_t second_length = 1;
7598  size_t length = first_length + second_length;
7599  unsigned char key[32];
7600  unsigned char iv[16];
7601 
7602  cipher_context_t ctx_dec;
7603  cipher_context_t ctx_enc;
7604  const cipher_info_t *cipher_info;
7605 
7606  unsigned char inbuf[64];
7607  unsigned char encbuf[64];
7608  unsigned char decbuf[64];
7609 
7610  size_t outlen = 0;
7611  size_t totaloutlen = 0;
7612  size_t enclen = 0;
7613 
7614  memset( key, 0, 32 );
7615  memset( iv , 0, 16 );
7616 
7617  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7618  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7619 
7620  memset( inbuf, 5, 64 );
7621  memset( encbuf, 0, 64 );
7622  memset( decbuf, 0, 64 );
7623 
7624  /* Initialise enc and dec contexts */
7626  fct_chk( NULL != cipher_info);
7627 
7628  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7629  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7630 
7631  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
7632  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
7633 
7634  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7635  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7636 
7637  if( POLARSSL_MODE_CBC == cipher_info->mode )
7638  {
7639  enclen = cipher_get_block_size(&ctx_enc )
7640  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7641  }
7642  else
7643  {
7644  enclen = length;
7645  }
7646 
7647  /* encode length number of bytes from inbuf */
7648  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7649  totaloutlen = outlen;
7650  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7651  totaloutlen += outlen;
7652  if( POLARSSL_MODE_CBC == cipher_info->mode )
7653  {
7654  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7655  }
7656  else
7657  {
7658  fct_chk( totaloutlen == enclen );
7659  }
7660  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7661  totaloutlen += outlen;
7662  if( POLARSSL_MODE_CBC == cipher_info->mode )
7663  {
7664  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7665  }
7666  else
7667  {
7668  fct_chk( outlen == 0 );
7669  }
7670 
7671  /* decode the previously encoded string */
7672  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7673  if( POLARSSL_MODE_CBC == cipher_info->mode )
7674  {
7675  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7676  }
7677  else
7678  {
7679  fct_chk( enclen == outlen );
7680  }
7681  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7682  if( POLARSSL_MODE_CBC == cipher_info->mode )
7683  {
7684  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7685  }
7686  else
7687  {
7688  fct_chk( outlen == 0 );
7689  }
7690 
7691 
7692  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7693 
7694  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7695  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7696  FCT_TEST_END();
7697 #endif /* POLARSSL_AES_C */
7698 #endif /* POLARSSL_CIPHER_MODE_CTR */
7699 
7700 #ifdef POLARSSL_AES_C
7701 #ifdef POLARSSL_CIPHER_MODE_CTR
7702 
7703  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
7704  size_t first_length = 16;
7705  size_t second_length = 0;
7706  size_t length = first_length + second_length;
7707  unsigned char key[32];
7708  unsigned char iv[16];
7709 
7710  cipher_context_t ctx_dec;
7711  cipher_context_t ctx_enc;
7712  const cipher_info_t *cipher_info;
7713 
7714  unsigned char inbuf[64];
7715  unsigned char encbuf[64];
7716  unsigned char decbuf[64];
7717 
7718  size_t outlen = 0;
7719  size_t totaloutlen = 0;
7720  size_t enclen = 0;
7721 
7722  memset( key, 0, 32 );
7723  memset( iv , 0, 16 );
7724 
7725  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7726  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7727 
7728  memset( inbuf, 5, 64 );
7729  memset( encbuf, 0, 64 );
7730  memset( decbuf, 0, 64 );
7731 
7732  /* Initialise enc and dec contexts */
7734  fct_chk( NULL != cipher_info);
7735 
7736  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7737  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7738 
7739  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
7740  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
7741 
7742  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7743  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7744 
7745  if( POLARSSL_MODE_CBC == cipher_info->mode )
7746  {
7747  enclen = cipher_get_block_size(&ctx_enc )
7748  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7749  }
7750  else
7751  {
7752  enclen = length;
7753  }
7754 
7755  /* encode length number of bytes from inbuf */
7756  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7757  totaloutlen = outlen;
7758  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7759  totaloutlen += outlen;
7760  if( POLARSSL_MODE_CBC == cipher_info->mode )
7761  {
7762  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7763  }
7764  else
7765  {
7766  fct_chk( totaloutlen == enclen );
7767  }
7768  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7769  totaloutlen += outlen;
7770  if( POLARSSL_MODE_CBC == cipher_info->mode )
7771  {
7772  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7773  }
7774  else
7775  {
7776  fct_chk( outlen == 0 );
7777  }
7778 
7779  /* decode the previously encoded string */
7780  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7781  if( POLARSSL_MODE_CBC == cipher_info->mode )
7782  {
7783  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7784  }
7785  else
7786  {
7787  fct_chk( enclen == outlen );
7788  }
7789  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7790  if( POLARSSL_MODE_CBC == cipher_info->mode )
7791  {
7792  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7793  }
7794  else
7795  {
7796  fct_chk( outlen == 0 );
7797  }
7798 
7799 
7800  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7801 
7802  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7803  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7804  FCT_TEST_END();
7805 #endif /* POLARSSL_AES_C */
7806 #endif /* POLARSSL_CIPHER_MODE_CTR */
7807 
7808 #ifdef POLARSSL_AES_C
7809 #ifdef POLARSSL_CIPHER_MODE_CTR
7810 
7811  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
7812  size_t first_length = 0;
7813  size_t second_length = 16;
7814  size_t length = first_length + second_length;
7815  unsigned char key[32];
7816  unsigned char iv[16];
7817 
7818  cipher_context_t ctx_dec;
7819  cipher_context_t ctx_enc;
7820  const cipher_info_t *cipher_info;
7821 
7822  unsigned char inbuf[64];
7823  unsigned char encbuf[64];
7824  unsigned char decbuf[64];
7825 
7826  size_t outlen = 0;
7827  size_t totaloutlen = 0;
7828  size_t enclen = 0;
7829 
7830  memset( key, 0, 32 );
7831  memset( iv , 0, 16 );
7832 
7833  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7834  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7835 
7836  memset( inbuf, 5, 64 );
7837  memset( encbuf, 0, 64 );
7838  memset( decbuf, 0, 64 );
7839 
7840  /* Initialise enc and dec contexts */
7842  fct_chk( NULL != cipher_info);
7843 
7844  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7845  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7846 
7847  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
7848  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
7849 
7850  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7851  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7852 
7853  if( POLARSSL_MODE_CBC == cipher_info->mode )
7854  {
7855  enclen = cipher_get_block_size(&ctx_enc )
7856  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7857  }
7858  else
7859  {
7860  enclen = length;
7861  }
7862 
7863  /* encode length number of bytes from inbuf */
7864  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7865  totaloutlen = outlen;
7866  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7867  totaloutlen += outlen;
7868  if( POLARSSL_MODE_CBC == cipher_info->mode )
7869  {
7870  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7871  }
7872  else
7873  {
7874  fct_chk( totaloutlen == enclen );
7875  }
7876  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7877  totaloutlen += outlen;
7878  if( POLARSSL_MODE_CBC == cipher_info->mode )
7879  {
7880  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7881  }
7882  else
7883  {
7884  fct_chk( outlen == 0 );
7885  }
7886 
7887  /* decode the previously encoded string */
7888  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7889  if( POLARSSL_MODE_CBC == cipher_info->mode )
7890  {
7891  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7892  }
7893  else
7894  {
7895  fct_chk( enclen == outlen );
7896  }
7897  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7898  if( POLARSSL_MODE_CBC == cipher_info->mode )
7899  {
7900  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7901  }
7902  else
7903  {
7904  fct_chk( outlen == 0 );
7905  }
7906 
7907 
7908  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7909 
7910  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7911  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7912  FCT_TEST_END();
7913 #endif /* POLARSSL_AES_C */
7914 #endif /* POLARSSL_CIPHER_MODE_CTR */
7915 
7916 #ifdef POLARSSL_AES_C
7917 #ifdef POLARSSL_CIPHER_MODE_CTR
7918 
7919  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
7920  size_t first_length = 1;
7921  size_t second_length = 15;
7922  size_t length = first_length + second_length;
7923  unsigned char key[32];
7924  unsigned char iv[16];
7925 
7926  cipher_context_t ctx_dec;
7927  cipher_context_t ctx_enc;
7928  const cipher_info_t *cipher_info;
7929 
7930  unsigned char inbuf[64];
7931  unsigned char encbuf[64];
7932  unsigned char decbuf[64];
7933 
7934  size_t outlen = 0;
7935  size_t totaloutlen = 0;
7936  size_t enclen = 0;
7937 
7938  memset( key, 0, 32 );
7939  memset( iv , 0, 16 );
7940 
7941  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7942  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7943 
7944  memset( inbuf, 5, 64 );
7945  memset( encbuf, 0, 64 );
7946  memset( decbuf, 0, 64 );
7947 
7948  /* Initialise enc and dec contexts */
7950  fct_chk( NULL != cipher_info);
7951 
7952  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7953  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7954 
7955  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
7956  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
7957 
7958  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7959  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7960 
7961  if( POLARSSL_MODE_CBC == cipher_info->mode )
7962  {
7963  enclen = cipher_get_block_size(&ctx_enc )
7964  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7965  }
7966  else
7967  {
7968  enclen = length;
7969  }
7970 
7971  /* encode length number of bytes from inbuf */
7972  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7973  totaloutlen = outlen;
7974  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7975  totaloutlen += outlen;
7976  if( POLARSSL_MODE_CBC == cipher_info->mode )
7977  {
7978  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7979  }
7980  else
7981  {
7982  fct_chk( totaloutlen == enclen );
7983  }
7984  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7985  totaloutlen += outlen;
7986  if( POLARSSL_MODE_CBC == cipher_info->mode )
7987  {
7988  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7989  }
7990  else
7991  {
7992  fct_chk( outlen == 0 );
7993  }
7994 
7995  /* decode the previously encoded string */
7996  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7997  if( POLARSSL_MODE_CBC == cipher_info->mode )
7998  {
7999  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8000  }
8001  else
8002  {
8003  fct_chk( enclen == outlen );
8004  }
8005  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8006  if( POLARSSL_MODE_CBC == cipher_info->mode )
8007  {
8008  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8009  }
8010  else
8011  {
8012  fct_chk( outlen == 0 );
8013  }
8014 
8015 
8016  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8017 
8018  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8019  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8020  FCT_TEST_END();
8021 #endif /* POLARSSL_AES_C */
8022 #endif /* POLARSSL_CIPHER_MODE_CTR */
8023 
8024 #ifdef POLARSSL_AES_C
8025 #ifdef POLARSSL_CIPHER_MODE_CTR
8026 
8027  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
8028  size_t first_length = 15;
8029  size_t second_length = 1;
8030  size_t length = first_length + second_length;
8031  unsigned char key[32];
8032  unsigned char iv[16];
8033 
8034  cipher_context_t ctx_dec;
8035  cipher_context_t ctx_enc;
8036  const cipher_info_t *cipher_info;
8037 
8038  unsigned char inbuf[64];
8039  unsigned char encbuf[64];
8040  unsigned char decbuf[64];
8041 
8042  size_t outlen = 0;
8043  size_t totaloutlen = 0;
8044  size_t enclen = 0;
8045 
8046  memset( key, 0, 32 );
8047  memset( iv , 0, 16 );
8048 
8049  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8050  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8051 
8052  memset( inbuf, 5, 64 );
8053  memset( encbuf, 0, 64 );
8054  memset( decbuf, 0, 64 );
8055 
8056  /* Initialise enc and dec contexts */
8058  fct_chk( NULL != cipher_info);
8059 
8060  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8061  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8062 
8063  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
8064  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
8065 
8066  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8067  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8068 
8069  if( POLARSSL_MODE_CBC == cipher_info->mode )
8070  {
8071  enclen = cipher_get_block_size(&ctx_enc )
8072  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8073  }
8074  else
8075  {
8076  enclen = length;
8077  }
8078 
8079  /* encode length number of bytes from inbuf */
8080  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8081  totaloutlen = outlen;
8082  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8083  totaloutlen += outlen;
8084  if( POLARSSL_MODE_CBC == cipher_info->mode )
8085  {
8086  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8087  }
8088  else
8089  {
8090  fct_chk( totaloutlen == enclen );
8091  }
8092  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8093  totaloutlen += outlen;
8094  if( POLARSSL_MODE_CBC == cipher_info->mode )
8095  {
8096  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8097  }
8098  else
8099  {
8100  fct_chk( outlen == 0 );
8101  }
8102 
8103  /* decode the previously encoded string */
8104  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8105  if( POLARSSL_MODE_CBC == cipher_info->mode )
8106  {
8107  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8108  }
8109  else
8110  {
8111  fct_chk( enclen == outlen );
8112  }
8113  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8114  if( POLARSSL_MODE_CBC == cipher_info->mode )
8115  {
8116  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8117  }
8118  else
8119  {
8120  fct_chk( outlen == 0 );
8121  }
8122 
8123 
8124  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8125 
8126  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8127  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8128  FCT_TEST_END();
8129 #endif /* POLARSSL_AES_C */
8130 #endif /* POLARSSL_CIPHER_MODE_CTR */
8131 
8132 #ifdef POLARSSL_AES_C
8133 #ifdef POLARSSL_CIPHER_MODE_CTR
8134 
8135  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
8136  size_t first_length = 15;
8137  size_t second_length = 7;
8138  size_t length = first_length + second_length;
8139  unsigned char key[32];
8140  unsigned char iv[16];
8141 
8142  cipher_context_t ctx_dec;
8143  cipher_context_t ctx_enc;
8144  const cipher_info_t *cipher_info;
8145 
8146  unsigned char inbuf[64];
8147  unsigned char encbuf[64];
8148  unsigned char decbuf[64];
8149 
8150  size_t outlen = 0;
8151  size_t totaloutlen = 0;
8152  size_t enclen = 0;
8153 
8154  memset( key, 0, 32 );
8155  memset( iv , 0, 16 );
8156 
8157  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8158  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8159 
8160  memset( inbuf, 5, 64 );
8161  memset( encbuf, 0, 64 );
8162  memset( decbuf, 0, 64 );
8163 
8164  /* Initialise enc and dec contexts */
8166  fct_chk( NULL != cipher_info);
8167 
8168  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8169  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8170 
8171  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
8172  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
8173 
8174  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8175  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8176 
8177  if( POLARSSL_MODE_CBC == cipher_info->mode )
8178  {
8179  enclen = cipher_get_block_size(&ctx_enc )
8180  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8181  }
8182  else
8183  {
8184  enclen = length;
8185  }
8186 
8187  /* encode length number of bytes from inbuf */
8188  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8189  totaloutlen = outlen;
8190  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8191  totaloutlen += outlen;
8192  if( POLARSSL_MODE_CBC == cipher_info->mode )
8193  {
8194  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8195  }
8196  else
8197  {
8198  fct_chk( totaloutlen == enclen );
8199  }
8200  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8201  totaloutlen += outlen;
8202  if( POLARSSL_MODE_CBC == cipher_info->mode )
8203  {
8204  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8205  }
8206  else
8207  {
8208  fct_chk( outlen == 0 );
8209  }
8210 
8211  /* decode the previously encoded string */
8212  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8213  if( POLARSSL_MODE_CBC == cipher_info->mode )
8214  {
8215  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8216  }
8217  else
8218  {
8219  fct_chk( enclen == outlen );
8220  }
8221  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8222  if( POLARSSL_MODE_CBC == cipher_info->mode )
8223  {
8224  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8225  }
8226  else
8227  {
8228  fct_chk( outlen == 0 );
8229  }
8230 
8231 
8232  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8233 
8234  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8235  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8236  FCT_TEST_END();
8237 #endif /* POLARSSL_AES_C */
8238 #endif /* POLARSSL_CIPHER_MODE_CTR */
8239 
8240 #ifdef POLARSSL_AES_C
8241 #ifdef POLARSSL_CIPHER_MODE_CTR
8242 
8243  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
8244  size_t first_length = 16;
8245  size_t second_length = 6;
8246  size_t length = first_length + second_length;
8247  unsigned char key[32];
8248  unsigned char iv[16];
8249 
8250  cipher_context_t ctx_dec;
8251  cipher_context_t ctx_enc;
8252  const cipher_info_t *cipher_info;
8253 
8254  unsigned char inbuf[64];
8255  unsigned char encbuf[64];
8256  unsigned char decbuf[64];
8257 
8258  size_t outlen = 0;
8259  size_t totaloutlen = 0;
8260  size_t enclen = 0;
8261 
8262  memset( key, 0, 32 );
8263  memset( iv , 0, 16 );
8264 
8265  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8266  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8267 
8268  memset( inbuf, 5, 64 );
8269  memset( encbuf, 0, 64 );
8270  memset( decbuf, 0, 64 );
8271 
8272  /* Initialise enc and dec contexts */
8274  fct_chk( NULL != cipher_info);
8275 
8276  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8277  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8278 
8279  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
8280  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
8281 
8282  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8283  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8284 
8285  if( POLARSSL_MODE_CBC == cipher_info->mode )
8286  {
8287  enclen = cipher_get_block_size(&ctx_enc )
8288  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8289  }
8290  else
8291  {
8292  enclen = length;
8293  }
8294 
8295  /* encode length number of bytes from inbuf */
8296  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8297  totaloutlen = outlen;
8298  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8299  totaloutlen += outlen;
8300  if( POLARSSL_MODE_CBC == cipher_info->mode )
8301  {
8302  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8303  }
8304  else
8305  {
8306  fct_chk( totaloutlen == enclen );
8307  }
8308  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8309  totaloutlen += outlen;
8310  if( POLARSSL_MODE_CBC == cipher_info->mode )
8311  {
8312  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8313  }
8314  else
8315  {
8316  fct_chk( outlen == 0 );
8317  }
8318 
8319  /* decode the previously encoded string */
8320  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8321  if( POLARSSL_MODE_CBC == cipher_info->mode )
8322  {
8323  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8324  }
8325  else
8326  {
8327  fct_chk( enclen == outlen );
8328  }
8329  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8330  if( POLARSSL_MODE_CBC == cipher_info->mode )
8331  {
8332  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8333  }
8334  else
8335  {
8336  fct_chk( outlen == 0 );
8337  }
8338 
8339 
8340  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8341 
8342  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8343  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8344  FCT_TEST_END();
8345 #endif /* POLARSSL_AES_C */
8346 #endif /* POLARSSL_CIPHER_MODE_CTR */
8347 
8348 #ifdef POLARSSL_AES_C
8349 #ifdef POLARSSL_CIPHER_MODE_CTR
8350 
8351  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
8352  size_t first_length = 17;
8353  size_t second_length = 6;
8354  size_t length = first_length + second_length;
8355  unsigned char key[32];
8356  unsigned char iv[16];
8357 
8358  cipher_context_t ctx_dec;
8359  cipher_context_t ctx_enc;
8360  const cipher_info_t *cipher_info;
8361 
8362  unsigned char inbuf[64];
8363  unsigned char encbuf[64];
8364  unsigned char decbuf[64];
8365 
8366  size_t outlen = 0;
8367  size_t totaloutlen = 0;
8368  size_t enclen = 0;
8369 
8370  memset( key, 0, 32 );
8371  memset( iv , 0, 16 );
8372 
8373  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8374  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8375 
8376  memset( inbuf, 5, 64 );
8377  memset( encbuf, 0, 64 );
8378  memset( decbuf, 0, 64 );
8379 
8380  /* Initialise enc and dec contexts */
8382  fct_chk( NULL != cipher_info);
8383 
8384  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8385  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8386 
8387  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
8388  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
8389 
8390  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8391  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8392 
8393  if( POLARSSL_MODE_CBC == cipher_info->mode )
8394  {
8395  enclen = cipher_get_block_size(&ctx_enc )
8396  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8397  }
8398  else
8399  {
8400  enclen = length;
8401  }
8402 
8403  /* encode length number of bytes from inbuf */
8404  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8405  totaloutlen = outlen;
8406  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8407  totaloutlen += outlen;
8408  if( POLARSSL_MODE_CBC == cipher_info->mode )
8409  {
8410  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8411  }
8412  else
8413  {
8414  fct_chk( totaloutlen == enclen );
8415  }
8416  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8417  totaloutlen += outlen;
8418  if( POLARSSL_MODE_CBC == cipher_info->mode )
8419  {
8420  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8421  }
8422  else
8423  {
8424  fct_chk( outlen == 0 );
8425  }
8426 
8427  /* decode the previously encoded string */
8428  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8429  if( POLARSSL_MODE_CBC == cipher_info->mode )
8430  {
8431  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8432  }
8433  else
8434  {
8435  fct_chk( enclen == outlen );
8436  }
8437  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8438  if( POLARSSL_MODE_CBC == cipher_info->mode )
8439  {
8440  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8441  }
8442  else
8443  {
8444  fct_chk( outlen == 0 );
8445  }
8446 
8447 
8448  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8449 
8450  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8451  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8452  FCT_TEST_END();
8453 #endif /* POLARSSL_AES_C */
8454 #endif /* POLARSSL_CIPHER_MODE_CTR */
8455 
8456 #ifdef POLARSSL_AES_C
8457 #ifdef POLARSSL_CIPHER_MODE_CTR
8458 
8459  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
8460  size_t first_length = 16;
8461  size_t second_length = 16;
8462  size_t length = first_length + second_length;
8463  unsigned char key[32];
8464  unsigned char iv[16];
8465 
8466  cipher_context_t ctx_dec;
8467  cipher_context_t ctx_enc;
8468  const cipher_info_t *cipher_info;
8469 
8470  unsigned char inbuf[64];
8471  unsigned char encbuf[64];
8472  unsigned char decbuf[64];
8473 
8474  size_t outlen = 0;
8475  size_t totaloutlen = 0;
8476  size_t enclen = 0;
8477 
8478  memset( key, 0, 32 );
8479  memset( iv , 0, 16 );
8480 
8481  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8482  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8483 
8484  memset( inbuf, 5, 64 );
8485  memset( encbuf, 0, 64 );
8486  memset( decbuf, 0, 64 );
8487 
8488  /* Initialise enc and dec contexts */
8490  fct_chk( NULL != cipher_info);
8491 
8492  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8493  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8494 
8495  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
8496  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 128, POLARSSL_ENCRYPT ) );
8497 
8498  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8499  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8500 
8501  if( POLARSSL_MODE_CBC == cipher_info->mode )
8502  {
8503  enclen = cipher_get_block_size(&ctx_enc )
8504  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8505  }
8506  else
8507  {
8508  enclen = length;
8509  }
8510 
8511  /* encode length number of bytes from inbuf */
8512  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8513  totaloutlen = outlen;
8514  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8515  totaloutlen += outlen;
8516  if( POLARSSL_MODE_CBC == cipher_info->mode )
8517  {
8518  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8519  }
8520  else
8521  {
8522  fct_chk( totaloutlen == enclen );
8523  }
8524  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8525  totaloutlen += outlen;
8526  if( POLARSSL_MODE_CBC == cipher_info->mode )
8527  {
8528  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8529  }
8530  else
8531  {
8532  fct_chk( outlen == 0 );
8533  }
8534 
8535  /* decode the previously encoded string */
8536  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8537  if( POLARSSL_MODE_CBC == cipher_info->mode )
8538  {
8539  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8540  }
8541  else
8542  {
8543  fct_chk( enclen == outlen );
8544  }
8545  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8546  if( POLARSSL_MODE_CBC == cipher_info->mode )
8547  {
8548  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8549  }
8550  else
8551  {
8552  fct_chk( outlen == 0 );
8553  }
8554 
8555 
8556  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8557 
8558  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8559  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8560  FCT_TEST_END();
8561 #endif /* POLARSSL_AES_C */
8562 #endif /* POLARSSL_CIPHER_MODE_CTR */
8563 
8564 #ifdef POLARSSL_AES_C
8565 
8566  FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes)
8567  size_t length = 0;
8568  unsigned char key[32];
8569  unsigned char iv[16];
8570 
8571  const cipher_info_t *cipher_info;
8572  cipher_context_t ctx_dec;
8573  cipher_context_t ctx_enc;
8574 
8575  unsigned char inbuf[64];
8576  unsigned char encbuf[64];
8577  unsigned char decbuf[64];
8578 
8579  size_t outlen = 0;
8580  size_t enclen = 0;
8581 
8582  memset( key, 0, 32 );
8583  memset( iv , 0, 16 );
8584 
8585  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8586  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8587 
8588  memset( inbuf, 5, 64 );
8589  memset( encbuf, 0, 64 );
8590  memset( decbuf, 0, 64 );
8591 
8592  /* Check and get info structures */
8594  fct_chk( NULL != cipher_info );
8595  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
8596 
8597  /* Initialise enc and dec contexts */
8598  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8599  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8600 
8601  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
8602  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
8603 
8604  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8605  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8606 
8607  if( POLARSSL_MODE_CBC == cipher_info->mode )
8608  {
8609  enclen = cipher_get_block_size( &ctx_enc )
8610  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8611  }
8612  else
8613  {
8614  enclen = length;
8615  }
8616 
8617  /* encode length number of bytes from inbuf */
8618  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
8619  if( POLARSSL_MODE_CBC == cipher_info->mode )
8620  {
8621  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8622  }
8623  else
8624  {
8625  fct_chk( outlen == enclen );
8626  }
8627 
8628  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
8629  if( POLARSSL_MODE_CBC == cipher_info->mode )
8630  {
8631  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8632  }
8633  else
8634  {
8635  fct_chk( outlen == 0 );
8636  }
8637 
8638 
8639  /* decode the previously encoded string */
8640  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8641  if( POLARSSL_MODE_CBC == cipher_info->mode )
8642  {
8643  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8644  }
8645  else
8646  {
8647  fct_chk( enclen == outlen );
8648  }
8649 
8650  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8651  if( POLARSSL_MODE_CBC == cipher_info->mode )
8652  {
8653  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8654  }
8655  else
8656  {
8657  fct_chk( outlen == 0 );
8658  }
8659 
8660 
8661  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8662 
8663  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8664  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8665  FCT_TEST_END();
8666 #endif /* POLARSSL_AES_C */
8667 
8668 #ifdef POLARSSL_AES_C
8669 
8670  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_byte)
8671  size_t length = 1;
8672  unsigned char key[32];
8673  unsigned char iv[16];
8674 
8675  const cipher_info_t *cipher_info;
8676  cipher_context_t ctx_dec;
8677  cipher_context_t ctx_enc;
8678 
8679  unsigned char inbuf[64];
8680  unsigned char encbuf[64];
8681  unsigned char decbuf[64];
8682 
8683  size_t outlen = 0;
8684  size_t enclen = 0;
8685 
8686  memset( key, 0, 32 );
8687  memset( iv , 0, 16 );
8688 
8689  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8690  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8691 
8692  memset( inbuf, 5, 64 );
8693  memset( encbuf, 0, 64 );
8694  memset( decbuf, 0, 64 );
8695 
8696  /* Check and get info structures */
8698  fct_chk( NULL != cipher_info );
8699  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
8700 
8701  /* Initialise enc and dec contexts */
8702  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8703  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8704 
8705  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
8706  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
8707 
8708  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8709  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8710 
8711  if( POLARSSL_MODE_CBC == cipher_info->mode )
8712  {
8713  enclen = cipher_get_block_size( &ctx_enc )
8714  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8715  }
8716  else
8717  {
8718  enclen = length;
8719  }
8720 
8721  /* encode length number of bytes from inbuf */
8722  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
8723  if( POLARSSL_MODE_CBC == cipher_info->mode )
8724  {
8725  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8726  }
8727  else
8728  {
8729  fct_chk( outlen == enclen );
8730  }
8731 
8732  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
8733  if( POLARSSL_MODE_CBC == cipher_info->mode )
8734  {
8735  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8736  }
8737  else
8738  {
8739  fct_chk( outlen == 0 );
8740  }
8741 
8742 
8743  /* decode the previously encoded string */
8744  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8745  if( POLARSSL_MODE_CBC == cipher_info->mode )
8746  {
8747  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8748  }
8749  else
8750  {
8751  fct_chk( enclen == outlen );
8752  }
8753 
8754  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8755  if( POLARSSL_MODE_CBC == cipher_info->mode )
8756  {
8757  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8758  }
8759  else
8760  {
8761  fct_chk( outlen == 0 );
8762  }
8763 
8764 
8765  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8766 
8767  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8768  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8769  FCT_TEST_END();
8770 #endif /* POLARSSL_AES_C */
8771 
8772 #ifdef POLARSSL_AES_C
8773 
8774  FCT_TEST_BGN(aes_encrypt_and_decrypt_2_bytes)
8775  size_t length = 2;
8776  unsigned char key[32];
8777  unsigned char iv[16];
8778 
8779  const cipher_info_t *cipher_info;
8780  cipher_context_t ctx_dec;
8781  cipher_context_t ctx_enc;
8782 
8783  unsigned char inbuf[64];
8784  unsigned char encbuf[64];
8785  unsigned char decbuf[64];
8786 
8787  size_t outlen = 0;
8788  size_t enclen = 0;
8789 
8790  memset( key, 0, 32 );
8791  memset( iv , 0, 16 );
8792 
8793  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8794  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8795 
8796  memset( inbuf, 5, 64 );
8797  memset( encbuf, 0, 64 );
8798  memset( decbuf, 0, 64 );
8799 
8800  /* Check and get info structures */
8802  fct_chk( NULL != cipher_info );
8803  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
8804 
8805  /* Initialise enc and dec contexts */
8806  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8807  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8808 
8809  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
8810  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
8811 
8812  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8813  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8814 
8815  if( POLARSSL_MODE_CBC == cipher_info->mode )
8816  {
8817  enclen = cipher_get_block_size( &ctx_enc )
8818  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8819  }
8820  else
8821  {
8822  enclen = length;
8823  }
8824 
8825  /* encode length number of bytes from inbuf */
8826  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
8827  if( POLARSSL_MODE_CBC == cipher_info->mode )
8828  {
8829  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8830  }
8831  else
8832  {
8833  fct_chk( outlen == enclen );
8834  }
8835 
8836  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
8837  if( POLARSSL_MODE_CBC == cipher_info->mode )
8838  {
8839  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8840  }
8841  else
8842  {
8843  fct_chk( outlen == 0 );
8844  }
8845 
8846 
8847  /* decode the previously encoded string */
8848  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8849  if( POLARSSL_MODE_CBC == cipher_info->mode )
8850  {
8851  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8852  }
8853  else
8854  {
8855  fct_chk( enclen == outlen );
8856  }
8857 
8858  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8859  if( POLARSSL_MODE_CBC == cipher_info->mode )
8860  {
8861  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8862  }
8863  else
8864  {
8865  fct_chk( outlen == 0 );
8866  }
8867 
8868 
8869  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8870 
8871  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8872  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8873  FCT_TEST_END();
8874 #endif /* POLARSSL_AES_C */
8875 
8876 #ifdef POLARSSL_AES_C
8877 
8878  FCT_TEST_BGN(aes_encrypt_and_decrypt_7_bytes)
8879  size_t length = 7;
8880  unsigned char key[32];
8881  unsigned char iv[16];
8882 
8883  const cipher_info_t *cipher_info;
8884  cipher_context_t ctx_dec;
8885  cipher_context_t ctx_enc;
8886 
8887  unsigned char inbuf[64];
8888  unsigned char encbuf[64];
8889  unsigned char decbuf[64];
8890 
8891  size_t outlen = 0;
8892  size_t enclen = 0;
8893 
8894  memset( key, 0, 32 );
8895  memset( iv , 0, 16 );
8896 
8897  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8898  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8899 
8900  memset( inbuf, 5, 64 );
8901  memset( encbuf, 0, 64 );
8902  memset( decbuf, 0, 64 );
8903 
8904  /* Check and get info structures */
8906  fct_chk( NULL != cipher_info );
8907  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
8908 
8909  /* Initialise enc and dec contexts */
8910  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8911  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8912 
8913  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
8914  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
8915 
8916  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8917  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8918 
8919  if( POLARSSL_MODE_CBC == cipher_info->mode )
8920  {
8921  enclen = cipher_get_block_size( &ctx_enc )
8922  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8923  }
8924  else
8925  {
8926  enclen = length;
8927  }
8928 
8929  /* encode length number of bytes from inbuf */
8930  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
8931  if( POLARSSL_MODE_CBC == cipher_info->mode )
8932  {
8933  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8934  }
8935  else
8936  {
8937  fct_chk( outlen == enclen );
8938  }
8939 
8940  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
8941  if( POLARSSL_MODE_CBC == cipher_info->mode )
8942  {
8943  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8944  }
8945  else
8946  {
8947  fct_chk( outlen == 0 );
8948  }
8949 
8950 
8951  /* decode the previously encoded string */
8952  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8953  if( POLARSSL_MODE_CBC == cipher_info->mode )
8954  {
8955  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8956  }
8957  else
8958  {
8959  fct_chk( enclen == outlen );
8960  }
8961 
8962  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8963  if( POLARSSL_MODE_CBC == cipher_info->mode )
8964  {
8965  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8966  }
8967  else
8968  {
8969  fct_chk( outlen == 0 );
8970  }
8971 
8972 
8973  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8974 
8975  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8976  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8977  FCT_TEST_END();
8978 #endif /* POLARSSL_AES_C */
8979 
8980 #ifdef POLARSSL_AES_C
8981 
8982  FCT_TEST_BGN(aes_encrypt_and_decrypt_8_bytes)
8983  size_t length = 8;
8984  unsigned char key[32];
8985  unsigned char iv[16];
8986 
8987  const cipher_info_t *cipher_info;
8988  cipher_context_t ctx_dec;
8989  cipher_context_t ctx_enc;
8990 
8991  unsigned char inbuf[64];
8992  unsigned char encbuf[64];
8993  unsigned char decbuf[64];
8994 
8995  size_t outlen = 0;
8996  size_t enclen = 0;
8997 
8998  memset( key, 0, 32 );
8999  memset( iv , 0, 16 );
9000 
9001  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
9002  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
9003 
9004  memset( inbuf, 5, 64 );
9005  memset( encbuf, 0, 64 );
9006  memset( decbuf, 0, 64 );
9007 
9008  /* Check and get info structures */
9010  fct_chk( NULL != cipher_info );
9011  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
9012 
9013  /* Initialise enc and dec contexts */
9014  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
9015  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
9016 
9017  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
9018  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
9019 
9020  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
9021  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
9022 
9023  if( POLARSSL_MODE_CBC == cipher_info->mode )
9024  {
9025  enclen = cipher_get_block_size( &ctx_enc )
9026  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
9027  }
9028  else
9029  {
9030  enclen = length;
9031  }
9032 
9033  /* encode length number of bytes from inbuf */
9034  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
9035  if( POLARSSL_MODE_CBC == cipher_info->mode )
9036  {
9037  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
9038  }
9039  else
9040  {
9041  fct_chk( outlen == enclen );
9042  }
9043 
9044  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
9045  if( POLARSSL_MODE_CBC == cipher_info->mode )
9046  {
9047  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
9048  }
9049  else
9050  {
9051  fct_chk( outlen == 0 );
9052  }
9053 
9054 
9055  /* decode the previously encoded string */
9056  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
9057  if( POLARSSL_MODE_CBC == cipher_info->mode )
9058  {
9059  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
9060  }
9061  else
9062  {
9063  fct_chk( enclen == outlen );
9064  }
9065 
9066  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
9067  if( POLARSSL_MODE_CBC == cipher_info->mode )
9068  {
9069  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
9070  }
9071  else
9072  {
9073  fct_chk( outlen == 0 );
9074  }
9075 
9076 
9077  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
9078 
9079  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
9080  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
9081  FCT_TEST_END();
9082 #endif /* POLARSSL_AES_C */
9083 
9084 #ifdef POLARSSL_AES_C
9085 
9086  FCT_TEST_BGN(aes_encrypt_and_decrypt_9_bytes)
9087  size_t length = 9;
9088  unsigned char key[32];
9089  unsigned char iv[16];
9090 
9091  const cipher_info_t *cipher_info;
9092  cipher_context_t ctx_dec;
9093  cipher_context_t ctx_enc;
9094 
9095  unsigned char inbuf[64];
9096  unsigned char encbuf[64];
9097  unsigned char decbuf[64];
9098 
9099  size_t outlen = 0;
9100  size_t enclen = 0;
9101 
9102  memset( key, 0, 32 );
9103  memset( iv , 0, 16 );
9104 
9105  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
9106  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
9107 
9108  memset( inbuf, 5, 64 );
9109  memset( encbuf, 0, 64 );
9110  memset( decbuf, 0, 64 );
9111 
9112  /* Check and get info structures */
9114  fct_chk( NULL != cipher_info );
9115  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
9116 
9117  /* Initialise enc and dec contexts */
9118  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
9119  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
9120 
9121  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
9122  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
9123 
9124  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
9125  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
9126 
9127  if( POLARSSL_MODE_CBC == cipher_info->mode )
9128  {
9129  enclen = cipher_get_block_size( &ctx_enc )
9130  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
9131  }
9132  else
9133  {
9134  enclen = length;
9135  }
9136 
9137  /* encode length number of bytes from inbuf */
9138  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
9139  if( POLARSSL_MODE_CBC == cipher_info->mode )
9140  {
9141  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
9142  }
9143  else
9144  {
9145  fct_chk( outlen == enclen );
9146  }
9147 
9148  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
9149  if( POLARSSL_MODE_CBC == cipher_info->mode )
9150  {
9151  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
9152  }
9153  else
9154  {
9155  fct_chk( outlen == 0 );
9156  }
9157 
9158 
9159  /* decode the previously encoded string */
9160  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
9161  if( POLARSSL_MODE_CBC == cipher_info->mode )
9162  {
9163  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
9164  }
9165  else
9166  {
9167  fct_chk( enclen == outlen );
9168  }
9169 
9170  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
9171  if( POLARSSL_MODE_CBC == cipher_info->mode )
9172  {
9173  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
9174  }
9175  else
9176  {
9177  fct_chk( outlen == 0 );
9178  }
9179 
9180 
9181  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
9182 
9183  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
9184  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
9185  FCT_TEST_END();
9186 #endif /* POLARSSL_AES_C */
9187 
9188 #ifdef POLARSSL_AES_C
9189 
9190  FCT_TEST_BGN(aes_encrypt_and_decrypt_15_bytes)
9191  size_t length = 15;
9192  unsigned char key[32];
9193  unsigned char iv[16];
9194 
9195  const cipher_info_t *cipher_info;
9196  cipher_context_t ctx_dec;
9197  cipher_context_t ctx_enc;
9198 
9199  unsigned char inbuf[64];
9200  unsigned char encbuf[64];
9201  unsigned char decbuf[64];
9202 
9203  size_t outlen = 0;
9204  size_t enclen = 0;
9205 
9206  memset( key, 0, 32 );
9207  memset( iv , 0, 16 );
9208 
9209  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
9210  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
9211 
9212  memset( inbuf, 5, 64 );
9213  memset( encbuf, 0, 64 );
9214  memset( decbuf, 0, 64 );
9215 
9216  /* Check and get info structures */
9218  fct_chk( NULL != cipher_info );
9219  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
9220 
9221  /* Initialise enc and dec contexts */
9222  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
9223  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
9224 
9225  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
9226  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
9227 
9228  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
9229  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
9230 
9231  if( POLARSSL_MODE_CBC == cipher_info->mode )
9232  {
9233  enclen = cipher_get_block_size( &ctx_enc )
9234  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
9235  }
9236  else
9237  {
9238  enclen = length;
9239  }
9240 
9241  /* encode length number of bytes from inbuf */
9242  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
9243  if( POLARSSL_MODE_CBC == cipher_info->mode )
9244  {
9245  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
9246  }
9247  else
9248  {
9249  fct_chk( outlen == enclen );
9250  }
9251 
9252  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
9253  if( POLARSSL_MODE_CBC == cipher_info->mode )
9254  {
9255  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
9256  }
9257  else
9258  {
9259  fct_chk( outlen == 0 );
9260  }
9261 
9262 
9263  /* decode the previously encoded string */
9264  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
9265  if( POLARSSL_MODE_CBC == cipher_info->mode )
9266  {
9267  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
9268  }
9269  else
9270  {
9271  fct_chk( enclen == outlen );
9272  }
9273 
9274  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
9275  if( POLARSSL_MODE_CBC == cipher_info->mode )
9276  {
9277  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
9278  }
9279  else
9280  {
9281  fct_chk( outlen == 0 );
9282  }
9283 
9284 
9285  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
9286 
9287  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
9288  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
9289  FCT_TEST_END();
9290 #endif /* POLARSSL_AES_C */
9291 
9292 #ifdef POLARSSL_AES_C
9293 
9294  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes)
9295  size_t length = 16;
9296  unsigned char key[32];
9297  unsigned char iv[16];
9298 
9299  const cipher_info_t *cipher_info;
9300  cipher_context_t ctx_dec;
9301  cipher_context_t ctx_enc;
9302 
9303  unsigned char inbuf[64];
9304  unsigned char encbuf[64];
9305  unsigned char decbuf[64];
9306 
9307  size_t outlen = 0;
9308  size_t enclen = 0;
9309 
9310  memset( key, 0, 32 );
9311  memset( iv , 0, 16 );
9312 
9313  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
9314  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
9315 
9316  memset( inbuf, 5, 64 );
9317  memset( encbuf, 0, 64 );
9318  memset( decbuf, 0, 64 );
9319 
9320  /* Check and get info structures */
9322  fct_chk( NULL != cipher_info );
9323  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
9324 
9325  /* Initialise enc and dec contexts */
9326  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
9327  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
9328 
9329  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
9330  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
9331 
9332  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
9333  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
9334 
9335  if( POLARSSL_MODE_CBC == cipher_info->mode )
9336  {
9337  enclen = cipher_get_block_size( &ctx_enc )
9338  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
9339  }
9340  else
9341  {
9342  enclen = length;
9343  }
9344 
9345  /* encode length number of bytes from inbuf */
9346  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
9347  if( POLARSSL_MODE_CBC == cipher_info->mode )
9348  {
9349  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
9350  }
9351  else
9352  {
9353  fct_chk( outlen == enclen );
9354  }
9355 
9356  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
9357  if( POLARSSL_MODE_CBC == cipher_info->mode )
9358  {
9359  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
9360  }
9361  else
9362  {
9363  fct_chk( outlen == 0 );
9364  }
9365 
9366 
9367  /* decode the previously encoded string */
9368  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
9369  if( POLARSSL_MODE_CBC == cipher_info->mode )
9370  {
9371  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
9372  }
9373  else
9374  {
9375  fct_chk( enclen == outlen );
9376  }
9377 
9378  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
9379  if( POLARSSL_MODE_CBC == cipher_info->mode )
9380  {
9381  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
9382  }
9383  else
9384  {
9385  fct_chk( outlen == 0 );
9386  }
9387 
9388 
9389  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
9390 
9391  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
9392  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
9393  FCT_TEST_END();
9394 #endif /* POLARSSL_AES_C */
9395 
9396 #ifdef POLARSSL_AES_C
9397 
9398  FCT_TEST_BGN(aes_encrypt_and_decrypt_17_bytes)
9399  size_t length = 17;
9400  unsigned char key[32];
9401  unsigned char iv[16];
9402 
9403  const cipher_info_t *cipher_info;
9404  cipher_context_t ctx_dec;
9405  cipher_context_t ctx_enc;
9406 
9407  unsigned char inbuf[64];
9408  unsigned char encbuf[64];
9409  unsigned char decbuf[64];
9410 
9411  size_t outlen = 0;
9412  size_t enclen = 0;
9413 
9414  memset( key, 0, 32 );
9415  memset( iv , 0, 16 );
9416 
9417  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
9418  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
9419 
9420  memset( inbuf, 5, 64 );
9421  memset( encbuf, 0, 64 );
9422  memset( decbuf, 0, 64 );
9423 
9424  /* Check and get info structures */
9426  fct_chk( NULL != cipher_info );
9427  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
9428 
9429  /* Initialise enc and dec contexts */
9430  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
9431  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
9432 
9433  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
9434  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
9435 
9436  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
9437  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
9438 
9439  if( POLARSSL_MODE_CBC == cipher_info->mode )
9440  {
9441  enclen = cipher_get_block_size( &ctx_enc )
9442  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
9443  }
9444  else
9445  {
9446  enclen = length;
9447  }
9448 
9449  /* encode length number of bytes from inbuf */
9450  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
9451  if( POLARSSL_MODE_CBC == cipher_info->mode )
9452  {
9453  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
9454  }
9455  else
9456  {
9457  fct_chk( outlen == enclen );
9458  }
9459 
9460  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
9461  if( POLARSSL_MODE_CBC == cipher_info->mode )
9462  {
9463  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
9464  }
9465  else
9466  {
9467  fct_chk( outlen == 0 );
9468  }
9469 
9470 
9471  /* decode the previously encoded string */
9472  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
9473  if( POLARSSL_MODE_CBC == cipher_info->mode )
9474  {
9475  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
9476  }
9477  else
9478  {
9479  fct_chk( enclen == outlen );
9480  }
9481 
9482  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
9483  if( POLARSSL_MODE_CBC == cipher_info->mode )
9484  {
9485  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
9486  }
9487  else
9488  {
9489  fct_chk( outlen == 0 );
9490  }
9491 
9492 
9493  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
9494 
9495  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
9496  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
9497  FCT_TEST_END();
9498 #endif /* POLARSSL_AES_C */
9499 
9500 #ifdef POLARSSL_AES_C
9501 
9502  FCT_TEST_BGN(aes_encrypt_and_decrypt_31_bytes)
9503  size_t length = 31;
9504  unsigned char key[32];
9505  unsigned char iv[16];
9506 
9507  const cipher_info_t *cipher_info;
9508  cipher_context_t ctx_dec;
9509  cipher_context_t ctx_enc;
9510 
9511  unsigned char inbuf[64];
9512  unsigned char encbuf[64];
9513  unsigned char decbuf[64];
9514 
9515  size_t outlen = 0;
9516  size_t enclen = 0;
9517 
9518  memset( key, 0, 32 );
9519  memset( iv , 0, 16 );
9520 
9521  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
9522  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
9523 
9524  memset( inbuf, 5, 64 );
9525  memset( encbuf, 0, 64 );
9526  memset( decbuf, 0, 64 );
9527 
9528  /* Check and get info structures */
9530  fct_chk( NULL != cipher_info );
9531  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
9532 
9533  /* Initialise enc and dec contexts */
9534  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
9535  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
9536 
9537  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
9538  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
9539 
9540  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
9541  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
9542 
9543  if( POLARSSL_MODE_CBC == cipher_info->mode )
9544  {
9545  enclen = cipher_get_block_size( &ctx_enc )
9546  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
9547  }
9548  else
9549  {
9550  enclen = length;
9551  }
9552 
9553  /* encode length number of bytes from inbuf */
9554  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
9555  if( POLARSSL_MODE_CBC == cipher_info->mode )
9556  {
9557  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
9558  }
9559  else
9560  {
9561  fct_chk( outlen == enclen );
9562  }
9563 
9564  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
9565  if( POLARSSL_MODE_CBC == cipher_info->mode )
9566  {
9567  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
9568  }
9569  else
9570  {
9571  fct_chk( outlen == 0 );
9572  }
9573 
9574 
9575  /* decode the previously encoded string */
9576  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
9577  if( POLARSSL_MODE_CBC == cipher_info->mode )
9578  {
9579  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
9580  }
9581  else
9582  {
9583  fct_chk( enclen == outlen );
9584  }
9585 
9586  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
9587  if( POLARSSL_MODE_CBC == cipher_info->mode )
9588  {
9589  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
9590  }
9591  else
9592  {
9593  fct_chk( outlen == 0 );
9594  }
9595 
9596 
9597  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
9598 
9599  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
9600  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
9601  FCT_TEST_END();
9602 #endif /* POLARSSL_AES_C */
9603 
9604 #ifdef POLARSSL_AES_C
9605 
9606  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes)
9607  size_t length = 32;
9608  unsigned char key[32];
9609  unsigned char iv[16];
9610 
9611  const cipher_info_t *cipher_info;
9612  cipher_context_t ctx_dec;
9613  cipher_context_t ctx_enc;
9614 
9615  unsigned char inbuf[64];
9616  unsigned char encbuf[64];
9617  unsigned char decbuf[64];
9618 
9619  size_t outlen = 0;
9620  size_t enclen = 0;
9621 
9622  memset( key, 0, 32 );
9623  memset( iv , 0, 16 );
9624 
9625  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
9626  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
9627 
9628  memset( inbuf, 5, 64 );
9629  memset( encbuf, 0, 64 );
9630  memset( decbuf, 0, 64 );
9631 
9632  /* Check and get info structures */
9634  fct_chk( NULL != cipher_info );
9635  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
9636 
9637  /* Initialise enc and dec contexts */
9638  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
9639  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
9640 
9641  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
9642  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
9643 
9644  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
9645  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
9646 
9647  if( POLARSSL_MODE_CBC == cipher_info->mode )
9648  {
9649  enclen = cipher_get_block_size( &ctx_enc )
9650  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
9651  }
9652  else
9653  {
9654  enclen = length;
9655  }
9656 
9657  /* encode length number of bytes from inbuf */
9658  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
9659  if( POLARSSL_MODE_CBC == cipher_info->mode )
9660  {
9661  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
9662  }
9663  else
9664  {
9665  fct_chk( outlen == enclen );
9666  }
9667 
9668  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
9669  if( POLARSSL_MODE_CBC == cipher_info->mode )
9670  {
9671  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
9672  }
9673  else
9674  {
9675  fct_chk( outlen == 0 );
9676  }
9677 
9678 
9679  /* decode the previously encoded string */
9680  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
9681  if( POLARSSL_MODE_CBC == cipher_info->mode )
9682  {
9683  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
9684  }
9685  else
9686  {
9687  fct_chk( enclen == outlen );
9688  }
9689 
9690  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
9691  if( POLARSSL_MODE_CBC == cipher_info->mode )
9692  {
9693  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
9694  }
9695  else
9696  {
9697  fct_chk( outlen == 0 );
9698  }
9699 
9700 
9701  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
9702 
9703  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
9704  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
9705  FCT_TEST_END();
9706 #endif /* POLARSSL_AES_C */
9707 
9708 #ifdef POLARSSL_AES_C
9709 
9710  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes)
9711  size_t length = 33;
9712  unsigned char key[32];
9713  unsigned char iv[16];
9714 
9715  const cipher_info_t *cipher_info;
9716  cipher_context_t ctx_dec;
9717  cipher_context_t ctx_enc;
9718 
9719  unsigned char inbuf[64];
9720  unsigned char encbuf[64];
9721  unsigned char decbuf[64];
9722 
9723  size_t outlen = 0;
9724  size_t enclen = 0;
9725 
9726  memset( key, 0, 32 );
9727  memset( iv , 0, 16 );
9728 
9729  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
9730  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
9731 
9732  memset( inbuf, 5, 64 );
9733  memset( encbuf, 0, 64 );
9734  memset( decbuf, 0, 64 );
9735 
9736  /* Check and get info structures */
9738  fct_chk( NULL != cipher_info );
9739  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
9740 
9741  /* Initialise enc and dec contexts */
9742  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
9743  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
9744 
9745  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
9746  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
9747 
9748  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
9749  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
9750 
9751  if( POLARSSL_MODE_CBC == cipher_info->mode )
9752  {
9753  enclen = cipher_get_block_size( &ctx_enc )
9754  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
9755  }
9756  else
9757  {
9758  enclen = length;
9759  }
9760 
9761  /* encode length number of bytes from inbuf */
9762  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
9763  if( POLARSSL_MODE_CBC == cipher_info->mode )
9764  {
9765  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
9766  }
9767  else
9768  {
9769  fct_chk( outlen == enclen );
9770  }
9771 
9772  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
9773  if( POLARSSL_MODE_CBC == cipher_info->mode )
9774  {
9775  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
9776  }
9777  else
9778  {
9779  fct_chk( outlen == 0 );
9780  }
9781 
9782 
9783  /* decode the previously encoded string */
9784  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
9785  if( POLARSSL_MODE_CBC == cipher_info->mode )
9786  {
9787  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
9788  }
9789  else
9790  {
9791  fct_chk( enclen == outlen );
9792  }
9793 
9794  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
9795  if( POLARSSL_MODE_CBC == cipher_info->mode )
9796  {
9797  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
9798  }
9799  else
9800  {
9801  fct_chk( outlen == 0 );
9802  }
9803 
9804 
9805  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
9806 
9807  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
9808  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
9809  FCT_TEST_END();
9810 #endif /* POLARSSL_AES_C */
9811 
9812 #ifdef POLARSSL_AES_C
9813 
9814  FCT_TEST_BGN(aes_encrypt_and_decrypt_47_bytes)
9815  size_t length = 47;
9816  unsigned char key[32];
9817  unsigned char iv[16];
9818 
9819  const cipher_info_t *cipher_info;
9820  cipher_context_t ctx_dec;
9821  cipher_context_t ctx_enc;
9822 
9823  unsigned char inbuf[64];
9824  unsigned char encbuf[64];
9825  unsigned char decbuf[64];
9826 
9827  size_t outlen = 0;
9828  size_t enclen = 0;
9829 
9830  memset( key, 0, 32 );
9831  memset( iv , 0, 16 );
9832 
9833  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
9834  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
9835 
9836  memset( inbuf, 5, 64 );
9837  memset( encbuf, 0, 64 );
9838  memset( decbuf, 0, 64 );
9839 
9840  /* Check and get info structures */
9842  fct_chk( NULL != cipher_info );
9843  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
9844 
9845  /* Initialise enc and dec contexts */
9846  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
9847  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
9848 
9849  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
9850  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
9851 
9852  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
9853  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
9854 
9855  if( POLARSSL_MODE_CBC == cipher_info->mode )
9856  {
9857  enclen = cipher_get_block_size( &ctx_enc )
9858  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
9859  }
9860  else
9861  {
9862  enclen = length;
9863  }
9864 
9865  /* encode length number of bytes from inbuf */
9866  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
9867  if( POLARSSL_MODE_CBC == cipher_info->mode )
9868  {
9869  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
9870  }
9871  else
9872  {
9873  fct_chk( outlen == enclen );
9874  }
9875 
9876  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
9877  if( POLARSSL_MODE_CBC == cipher_info->mode )
9878  {
9879  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
9880  }
9881  else
9882  {
9883  fct_chk( outlen == 0 );
9884  }
9885 
9886 
9887  /* decode the previously encoded string */
9888  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
9889  if( POLARSSL_MODE_CBC == cipher_info->mode )
9890  {
9891  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
9892  }
9893  else
9894  {
9895  fct_chk( enclen == outlen );
9896  }
9897 
9898  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
9899  if( POLARSSL_MODE_CBC == cipher_info->mode )
9900  {
9901  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
9902  }
9903  else
9904  {
9905  fct_chk( outlen == 0 );
9906  }
9907 
9908 
9909  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
9910 
9911  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
9912  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
9913  FCT_TEST_END();
9914 #endif /* POLARSSL_AES_C */
9915 
9916 #ifdef POLARSSL_AES_C
9917 
9918  FCT_TEST_BGN(aes_encrypt_and_decrypt_48_bytes)
9919  size_t length = 48;
9920  unsigned char key[32];
9921  unsigned char iv[16];
9922 
9923  const cipher_info_t *cipher_info;
9924  cipher_context_t ctx_dec;
9925  cipher_context_t ctx_enc;
9926 
9927  unsigned char inbuf[64];
9928  unsigned char encbuf[64];
9929  unsigned char decbuf[64];
9930 
9931  size_t outlen = 0;
9932  size_t enclen = 0;
9933 
9934  memset( key, 0, 32 );
9935  memset( iv , 0, 16 );
9936 
9937  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
9938  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
9939 
9940  memset( inbuf, 5, 64 );
9941  memset( encbuf, 0, 64 );
9942  memset( decbuf, 0, 64 );
9943 
9944  /* Check and get info structures */
9946  fct_chk( NULL != cipher_info );
9947  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
9948 
9949  /* Initialise enc and dec contexts */
9950  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
9951  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
9952 
9953  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
9954  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
9955 
9956  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
9957  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
9958 
9959  if( POLARSSL_MODE_CBC == cipher_info->mode )
9960  {
9961  enclen = cipher_get_block_size( &ctx_enc )
9962  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
9963  }
9964  else
9965  {
9966  enclen = length;
9967  }
9968 
9969  /* encode length number of bytes from inbuf */
9970  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
9971  if( POLARSSL_MODE_CBC == cipher_info->mode )
9972  {
9973  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
9974  }
9975  else
9976  {
9977  fct_chk( outlen == enclen );
9978  }
9979 
9980  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
9981  if( POLARSSL_MODE_CBC == cipher_info->mode )
9982  {
9983  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
9984  }
9985  else
9986  {
9987  fct_chk( outlen == 0 );
9988  }
9989 
9990 
9991  /* decode the previously encoded string */
9992  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
9993  if( POLARSSL_MODE_CBC == cipher_info->mode )
9994  {
9995  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
9996  }
9997  else
9998  {
9999  fct_chk( enclen == outlen );
10000  }
10001 
10002  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
10003  if( POLARSSL_MODE_CBC == cipher_info->mode )
10004  {
10005  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
10006  }
10007  else
10008  {
10009  fct_chk( outlen == 0 );
10010  }
10011 
10012 
10013  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
10014 
10015  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
10016  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
10017  FCT_TEST_END();
10018 #endif /* POLARSSL_AES_C */
10019 
10020 #ifdef POLARSSL_AES_C
10021 
10022  FCT_TEST_BGN(aes_encrypt_and_decrypt_49_bytes)
10023  size_t length = 49;
10024  unsigned char key[32];
10025  unsigned char iv[16];
10026 
10027  const cipher_info_t *cipher_info;
10028  cipher_context_t ctx_dec;
10029  cipher_context_t ctx_enc;
10030 
10031  unsigned char inbuf[64];
10032  unsigned char encbuf[64];
10033  unsigned char decbuf[64];
10034 
10035  size_t outlen = 0;
10036  size_t enclen = 0;
10037 
10038  memset( key, 0, 32 );
10039  memset( iv , 0, 16 );
10040 
10041  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
10042  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
10043 
10044  memset( inbuf, 5, 64 );
10045  memset( encbuf, 0, 64 );
10046  memset( decbuf, 0, 64 );
10047 
10048  /* Check and get info structures */
10050  fct_chk( NULL != cipher_info );
10051  fct_chk( cipher_info_from_string( "AES-192-CBC" ) == cipher_info );
10052 
10053  /* Initialise enc and dec contexts */
10054  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
10055  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
10056 
10057  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
10058  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
10059 
10060  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
10061  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
10062 
10063  if( POLARSSL_MODE_CBC == cipher_info->mode )
10064  {
10065  enclen = cipher_get_block_size( &ctx_enc )
10066  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
10067  }
10068  else
10069  {
10070  enclen = length;
10071  }
10072 
10073  /* encode length number of bytes from inbuf */
10074  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
10075  if( POLARSSL_MODE_CBC == cipher_info->mode )
10076  {
10077  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
10078  }
10079  else
10080  {
10081  fct_chk( outlen == enclen );
10082  }
10083 
10084  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
10085  if( POLARSSL_MODE_CBC == cipher_info->mode )
10086  {
10087  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
10088  }
10089  else
10090  {
10091  fct_chk( outlen == 0 );
10092  }
10093 
10094 
10095  /* decode the previously encoded string */
10096  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
10097  if( POLARSSL_MODE_CBC == cipher_info->mode )
10098  {
10099  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
10100  }
10101  else
10102  {
10103  fct_chk( enclen == outlen );
10104  }
10105 
10106  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
10107  if( POLARSSL_MODE_CBC == cipher_info->mode )
10108  {
10109  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
10110  }
10111  else
10112  {
10113  fct_chk( outlen == 0 );
10114  }
10115 
10116 
10117  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
10118 
10119  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
10120  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
10121  FCT_TEST_END();
10122 #endif /* POLARSSL_AES_C */
10123 
10124 #ifdef POLARSSL_AES_C
10125 
10126  FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes_in_multiple_parts)
10127  size_t first_length = 0;
10128  size_t second_length = 0;
10129  size_t length = first_length + second_length;
10130  unsigned char key[32];
10131  unsigned char iv[16];
10132 
10133  cipher_context_t ctx_dec;
10134  cipher_context_t ctx_enc;
10135  const cipher_info_t *cipher_info;
10136 
10137  unsigned char inbuf[64];
10138  unsigned char encbuf[64];
10139  unsigned char decbuf[64];
10140 
10141  size_t outlen = 0;
10142  size_t totaloutlen = 0;
10143  size_t enclen = 0;
10144 
10145  memset( key, 0, 32 );
10146  memset( iv , 0, 16 );
10147 
10148  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
10149  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
10150 
10151  memset( inbuf, 5, 64 );
10152  memset( encbuf, 0, 64 );
10153  memset( decbuf, 0, 64 );
10154 
10155  /* Initialise enc and dec contexts */
10157  fct_chk( NULL != cipher_info);
10158 
10159  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
10160  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
10161 
10162  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
10163  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
10164 
10165  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
10166  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
10167 
10168  if( POLARSSL_MODE_CBC == cipher_info->mode )
10169  {
10170  enclen = cipher_get_block_size(&ctx_enc )
10171  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
10172  }
10173  else
10174  {
10175  enclen = length;
10176  }
10177 
10178  /* encode length number of bytes from inbuf */
10179  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
10180  totaloutlen = outlen;
10181  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
10182  totaloutlen += outlen;
10183  if( POLARSSL_MODE_CBC == cipher_info->mode )
10184  {
10185  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
10186  }
10187  else
10188  {
10189  fct_chk( totaloutlen == enclen );
10190  }
10191  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
10192  totaloutlen += outlen;
10193  if( POLARSSL_MODE_CBC == cipher_info->mode )
10194  {
10195  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
10196  }
10197  else
10198  {
10199  fct_chk( outlen == 0 );
10200  }
10201 
10202  /* decode the previously encoded string */
10203  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
10204  if( POLARSSL_MODE_CBC == cipher_info->mode )
10205  {
10206  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
10207  }
10208  else
10209  {
10210  fct_chk( enclen == outlen );
10211  }
10212  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
10213  if( POLARSSL_MODE_CBC == cipher_info->mode )
10214  {
10215  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
10216  }
10217  else
10218  {
10219  fct_chk( outlen == 0 );
10220  }
10221 
10222 
10223  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
10224 
10225  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
10226  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
10227  FCT_TEST_END();
10228 #endif /* POLARSSL_AES_C */
10229 
10230 #ifdef POLARSSL_AES_C
10231 
10232  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
10233  size_t first_length = 1;
10234  size_t second_length = 0;
10235  size_t length = first_length + second_length;
10236  unsigned char key[32];
10237  unsigned char iv[16];
10238 
10239  cipher_context_t ctx_dec;
10240  cipher_context_t ctx_enc;
10241  const cipher_info_t *cipher_info;
10242 
10243  unsigned char inbuf[64];
10244  unsigned char encbuf[64];
10245  unsigned char decbuf[64];
10246 
10247  size_t outlen = 0;
10248  size_t totaloutlen = 0;
10249  size_t enclen = 0;
10250 
10251  memset( key, 0, 32 );
10252  memset( iv , 0, 16 );
10253 
10254  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
10255  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
10256 
10257  memset( inbuf, 5, 64 );
10258  memset( encbuf, 0, 64 );
10259  memset( decbuf, 0, 64 );
10260 
10261  /* Initialise enc and dec contexts */
10263  fct_chk( NULL != cipher_info);
10264 
10265  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
10266  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
10267 
10268  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
10269  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
10270 
10271  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
10272  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
10273 
10274  if( POLARSSL_MODE_CBC == cipher_info->mode )
10275  {
10276  enclen = cipher_get_block_size(&ctx_enc )
10277  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
10278  }
10279  else
10280  {
10281  enclen = length;
10282  }
10283 
10284  /* encode length number of bytes from inbuf */
10285  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
10286  totaloutlen = outlen;
10287  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
10288  totaloutlen += outlen;
10289  if( POLARSSL_MODE_CBC == cipher_info->mode )
10290  {
10291  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
10292  }
10293  else
10294  {
10295  fct_chk( totaloutlen == enclen );
10296  }
10297  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
10298  totaloutlen += outlen;
10299  if( POLARSSL_MODE_CBC == cipher_info->mode )
10300  {
10301  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
10302  }
10303  else
10304  {
10305  fct_chk( outlen == 0 );
10306  }
10307 
10308  /* decode the previously encoded string */
10309  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
10310  if( POLARSSL_MODE_CBC == cipher_info->mode )
10311  {
10312  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
10313  }
10314  else
10315  {
10316  fct_chk( enclen == outlen );
10317  }
10318  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
10319  if( POLARSSL_MODE_CBC == cipher_info->mode )
10320  {
10321  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
10322  }
10323  else
10324  {
10325  fct_chk( outlen == 0 );
10326  }
10327 
10328 
10329  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
10330 
10331  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
10332  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
10333  FCT_TEST_END();
10334 #endif /* POLARSSL_AES_C */
10335 
10336 #ifdef POLARSSL_AES_C
10337 
10338  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
10339  size_t first_length = 0;
10340  size_t second_length = 1;
10341  size_t length = first_length + second_length;
10342  unsigned char key[32];
10343  unsigned char iv[16];
10344 
10345  cipher_context_t ctx_dec;
10346  cipher_context_t ctx_enc;
10347  const cipher_info_t *cipher_info;
10348 
10349  unsigned char inbuf[64];
10350  unsigned char encbuf[64];
10351  unsigned char decbuf[64];
10352 
10353  size_t outlen = 0;
10354  size_t totaloutlen = 0;
10355  size_t enclen = 0;
10356 
10357  memset( key, 0, 32 );
10358  memset( iv , 0, 16 );
10359 
10360  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
10361  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
10362 
10363  memset( inbuf, 5, 64 );
10364  memset( encbuf, 0, 64 );
10365  memset( decbuf, 0, 64 );
10366 
10367  /* Initialise enc and dec contexts */
10369  fct_chk( NULL != cipher_info);
10370 
10371  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
10372  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
10373 
10374  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
10375  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
10376 
10377  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
10378  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
10379 
10380  if( POLARSSL_MODE_CBC == cipher_info->mode )
10381  {
10382  enclen = cipher_get_block_size(&ctx_enc )
10383  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
10384  }
10385  else
10386  {
10387  enclen = length;
10388  }
10389 
10390  /* encode length number of bytes from inbuf */
10391  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
10392  totaloutlen = outlen;
10393  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
10394  totaloutlen += outlen;
10395  if( POLARSSL_MODE_CBC == cipher_info->mode )
10396  {
10397  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
10398  }
10399  else
10400  {
10401  fct_chk( totaloutlen == enclen );
10402  }
10403  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
10404  totaloutlen += outlen;
10405  if( POLARSSL_MODE_CBC == cipher_info->mode )
10406  {
10407  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
10408  }
10409  else
10410  {
10411  fct_chk( outlen == 0 );
10412  }
10413 
10414  /* decode the previously encoded string */
10415  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
10416  if( POLARSSL_MODE_CBC == cipher_info->mode )
10417  {
10418  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
10419  }
10420  else
10421  {
10422  fct_chk( enclen == outlen );
10423  }
10424  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
10425  if( POLARSSL_MODE_CBC == cipher_info->mode )
10426  {
10427  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
10428  }
10429  else
10430  {
10431  fct_chk( outlen == 0 );
10432  }
10433 
10434 
10435  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
10436 
10437  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
10438  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
10439  FCT_TEST_END();
10440 #endif /* POLARSSL_AES_C */
10441 
10442 #ifdef POLARSSL_AES_C
10443 
10444  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
10445  size_t first_length = 16;
10446  size_t second_length = 0;
10447  size_t length = first_length + second_length;
10448  unsigned char key[32];
10449  unsigned char iv[16];
10450 
10451  cipher_context_t ctx_dec;
10452  cipher_context_t ctx_enc;
10453  const cipher_info_t *cipher_info;
10454 
10455  unsigned char inbuf[64];
10456  unsigned char encbuf[64];
10457  unsigned char decbuf[64];
10458 
10459  size_t outlen = 0;
10460  size_t totaloutlen = 0;
10461  size_t enclen = 0;
10462 
10463  memset( key, 0, 32 );
10464  memset( iv , 0, 16 );
10465 
10466  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
10467  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
10468 
10469  memset( inbuf, 5, 64 );
10470  memset( encbuf, 0, 64 );
10471  memset( decbuf, 0, 64 );
10472 
10473  /* Initialise enc and dec contexts */
10475  fct_chk( NULL != cipher_info);
10476 
10477  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
10478  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
10479 
10480  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
10481  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
10482 
10483  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
10484  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
10485 
10486  if( POLARSSL_MODE_CBC == cipher_info->mode )
10487  {
10488  enclen = cipher_get_block_size(&ctx_enc )
10489  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
10490  }
10491  else
10492  {
10493  enclen = length;
10494  }
10495 
10496  /* encode length number of bytes from inbuf */
10497  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
10498  totaloutlen = outlen;
10499  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
10500  totaloutlen += outlen;
10501  if( POLARSSL_MODE_CBC == cipher_info->mode )
10502  {
10503  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
10504  }
10505  else
10506  {
10507  fct_chk( totaloutlen == enclen );
10508  }
10509  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
10510  totaloutlen += outlen;
10511  if( POLARSSL_MODE_CBC == cipher_info->mode )
10512  {
10513  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
10514  }
10515  else
10516  {
10517  fct_chk( outlen == 0 );
10518  }
10519 
10520  /* decode the previously encoded string */
10521  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
10522  if( POLARSSL_MODE_CBC == cipher_info->mode )
10523  {
10524  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
10525  }
10526  else
10527  {
10528  fct_chk( enclen == outlen );
10529  }
10530  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
10531  if( POLARSSL_MODE_CBC == cipher_info->mode )
10532  {
10533  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
10534  }
10535  else
10536  {
10537  fct_chk( outlen == 0 );
10538  }
10539 
10540 
10541  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
10542 
10543  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
10544  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
10545  FCT_TEST_END();
10546 #endif /* POLARSSL_AES_C */
10547 
10548 #ifdef POLARSSL_AES_C
10549 
10550  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
10551  size_t first_length = 0;
10552  size_t second_length = 16;
10553  size_t length = first_length + second_length;
10554  unsigned char key[32];
10555  unsigned char iv[16];
10556 
10557  cipher_context_t ctx_dec;
10558  cipher_context_t ctx_enc;
10559  const cipher_info_t *cipher_info;
10560 
10561  unsigned char inbuf[64];
10562  unsigned char encbuf[64];
10563  unsigned char decbuf[64];
10564 
10565  size_t outlen = 0;
10566  size_t totaloutlen = 0;
10567  size_t enclen = 0;
10568 
10569  memset( key, 0, 32 );
10570  memset( iv , 0, 16 );
10571 
10572  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
10573  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
10574 
10575  memset( inbuf, 5, 64 );
10576  memset( encbuf, 0, 64 );
10577  memset( decbuf, 0, 64 );
10578 
10579  /* Initialise enc and dec contexts */
10581  fct_chk( NULL != cipher_info);
10582 
10583  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
10584  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
10585 
10586  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
10587  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
10588 
10589  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
10590  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
10591 
10592  if( POLARSSL_MODE_CBC == cipher_info->mode )
10593  {
10594  enclen = cipher_get_block_size(&ctx_enc )
10595  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
10596  }
10597  else
10598  {
10599  enclen = length;
10600  }
10601 
10602  /* encode length number of bytes from inbuf */
10603  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
10604  totaloutlen = outlen;
10605  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
10606  totaloutlen += outlen;
10607  if( POLARSSL_MODE_CBC == cipher_info->mode )
10608  {
10609  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
10610  }
10611  else
10612  {
10613  fct_chk( totaloutlen == enclen );
10614  }
10615  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
10616  totaloutlen += outlen;
10617  if( POLARSSL_MODE_CBC == cipher_info->mode )
10618  {
10619  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
10620  }
10621  else
10622  {
10623  fct_chk( outlen == 0 );
10624  }
10625 
10626  /* decode the previously encoded string */
10627  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
10628  if( POLARSSL_MODE_CBC == cipher_info->mode )
10629  {
10630  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
10631  }
10632  else
10633  {
10634  fct_chk( enclen == outlen );
10635  }
10636  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
10637  if( POLARSSL_MODE_CBC == cipher_info->mode )
10638  {
10639  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
10640  }
10641  else
10642  {
10643  fct_chk( outlen == 0 );
10644  }
10645 
10646 
10647  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
10648 
10649  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
10650  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
10651  FCT_TEST_END();
10652 #endif /* POLARSSL_AES_C */
10653 
10654 #ifdef POLARSSL_AES_C
10655 
10656  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
10657  size_t first_length = 1;
10658  size_t second_length = 15;
10659  size_t length = first_length + second_length;
10660  unsigned char key[32];
10661  unsigned char iv[16];
10662 
10663  cipher_context_t ctx_dec;
10664  cipher_context_t ctx_enc;
10665  const cipher_info_t *cipher_info;
10666 
10667  unsigned char inbuf[64];
10668  unsigned char encbuf[64];
10669  unsigned char decbuf[64];
10670 
10671  size_t outlen = 0;
10672  size_t totaloutlen = 0;
10673  size_t enclen = 0;
10674 
10675  memset( key, 0, 32 );
10676  memset( iv , 0, 16 );
10677 
10678  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
10679  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
10680 
10681  memset( inbuf, 5, 64 );
10682  memset( encbuf, 0, 64 );
10683  memset( decbuf, 0, 64 );
10684 
10685  /* Initialise enc and dec contexts */
10687  fct_chk( NULL != cipher_info);
10688 
10689  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
10690  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
10691 
10692  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
10693  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
10694 
10695  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
10696  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
10697 
10698  if( POLARSSL_MODE_CBC == cipher_info->mode )
10699  {
10700  enclen = cipher_get_block_size(&ctx_enc )
10701  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
10702  }
10703  else
10704  {
10705  enclen = length;
10706  }
10707 
10708  /* encode length number of bytes from inbuf */
10709  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
10710  totaloutlen = outlen;
10711  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
10712  totaloutlen += outlen;
10713  if( POLARSSL_MODE_CBC == cipher_info->mode )
10714  {
10715  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
10716  }
10717  else
10718  {
10719  fct_chk( totaloutlen == enclen );
10720  }
10721  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
10722  totaloutlen += outlen;
10723  if( POLARSSL_MODE_CBC == cipher_info->mode )
10724  {
10725  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
10726  }
10727  else
10728  {
10729  fct_chk( outlen == 0 );
10730  }
10731 
10732  /* decode the previously encoded string */
10733  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
10734  if( POLARSSL_MODE_CBC == cipher_info->mode )
10735  {
10736  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
10737  }
10738  else
10739  {
10740  fct_chk( enclen == outlen );
10741  }
10742  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
10743  if( POLARSSL_MODE_CBC == cipher_info->mode )
10744  {
10745  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
10746  }
10747  else
10748  {
10749  fct_chk( outlen == 0 );
10750  }
10751 
10752 
10753  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
10754 
10755  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
10756  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
10757  FCT_TEST_END();
10758 #endif /* POLARSSL_AES_C */
10759 
10760 #ifdef POLARSSL_AES_C
10761 
10762  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
10763  size_t first_length = 15;
10764  size_t second_length = 1;
10765  size_t length = first_length + second_length;
10766  unsigned char key[32];
10767  unsigned char iv[16];
10768 
10769  cipher_context_t ctx_dec;
10770  cipher_context_t ctx_enc;
10771  const cipher_info_t *cipher_info;
10772 
10773  unsigned char inbuf[64];
10774  unsigned char encbuf[64];
10775  unsigned char decbuf[64];
10776 
10777  size_t outlen = 0;
10778  size_t totaloutlen = 0;
10779  size_t enclen = 0;
10780 
10781  memset( key, 0, 32 );
10782  memset( iv , 0, 16 );
10783 
10784  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
10785  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
10786 
10787  memset( inbuf, 5, 64 );
10788  memset( encbuf, 0, 64 );
10789  memset( decbuf, 0, 64 );
10790 
10791  /* Initialise enc and dec contexts */
10793  fct_chk( NULL != cipher_info);
10794 
10795  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
10796  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
10797 
10798  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
10799  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
10800 
10801  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
10802  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
10803 
10804  if( POLARSSL_MODE_CBC == cipher_info->mode )
10805  {
10806  enclen = cipher_get_block_size(&ctx_enc )
10807  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
10808  }
10809  else
10810  {
10811  enclen = length;
10812  }
10813 
10814  /* encode length number of bytes from inbuf */
10815  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
10816  totaloutlen = outlen;
10817  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
10818  totaloutlen += outlen;
10819  if( POLARSSL_MODE_CBC == cipher_info->mode )
10820  {
10821  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
10822  }
10823  else
10824  {
10825  fct_chk( totaloutlen == enclen );
10826  }
10827  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
10828  totaloutlen += outlen;
10829  if( POLARSSL_MODE_CBC == cipher_info->mode )
10830  {
10831  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
10832  }
10833  else
10834  {
10835  fct_chk( outlen == 0 );
10836  }
10837 
10838  /* decode the previously encoded string */
10839  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
10840  if( POLARSSL_MODE_CBC == cipher_info->mode )
10841  {
10842  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
10843  }
10844  else
10845  {
10846  fct_chk( enclen == outlen );
10847  }
10848  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
10849  if( POLARSSL_MODE_CBC == cipher_info->mode )
10850  {
10851  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
10852  }
10853  else
10854  {
10855  fct_chk( outlen == 0 );
10856  }
10857 
10858 
10859  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
10860 
10861  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
10862  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
10863  FCT_TEST_END();
10864 #endif /* POLARSSL_AES_C */
10865 
10866 #ifdef POLARSSL_AES_C
10867 
10868  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
10869  size_t first_length = 15;
10870  size_t second_length = 7;
10871  size_t length = first_length + second_length;
10872  unsigned char key[32];
10873  unsigned char iv[16];
10874 
10875  cipher_context_t ctx_dec;
10876  cipher_context_t ctx_enc;
10877  const cipher_info_t *cipher_info;
10878 
10879  unsigned char inbuf[64];
10880  unsigned char encbuf[64];
10881  unsigned char decbuf[64];
10882 
10883  size_t outlen = 0;
10884  size_t totaloutlen = 0;
10885  size_t enclen = 0;
10886 
10887  memset( key, 0, 32 );
10888  memset( iv , 0, 16 );
10889 
10890  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
10891  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
10892 
10893  memset( inbuf, 5, 64 );
10894  memset( encbuf, 0, 64 );
10895  memset( decbuf, 0, 64 );
10896 
10897  /* Initialise enc and dec contexts */
10899  fct_chk( NULL != cipher_info);
10900 
10901  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
10902  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
10903 
10904  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
10905  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
10906 
10907  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
10908  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
10909 
10910  if( POLARSSL_MODE_CBC == cipher_info->mode )
10911  {
10912  enclen = cipher_get_block_size(&ctx_enc )
10913  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
10914  }
10915  else
10916  {
10917  enclen = length;
10918  }
10919 
10920  /* encode length number of bytes from inbuf */
10921  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
10922  totaloutlen = outlen;
10923  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
10924  totaloutlen += outlen;
10925  if( POLARSSL_MODE_CBC == cipher_info->mode )
10926  {
10927  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
10928  }
10929  else
10930  {
10931  fct_chk( totaloutlen == enclen );
10932  }
10933  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
10934  totaloutlen += outlen;
10935  if( POLARSSL_MODE_CBC == cipher_info->mode )
10936  {
10937  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
10938  }
10939  else
10940  {
10941  fct_chk( outlen == 0 );
10942  }
10943 
10944  /* decode the previously encoded string */
10945  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
10946  if( POLARSSL_MODE_CBC == cipher_info->mode )
10947  {
10948  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
10949  }
10950  else
10951  {
10952  fct_chk( enclen == outlen );
10953  }
10954  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
10955  if( POLARSSL_MODE_CBC == cipher_info->mode )
10956  {
10957  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
10958  }
10959  else
10960  {
10961  fct_chk( outlen == 0 );
10962  }
10963 
10964 
10965  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
10966 
10967  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
10968  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
10969  FCT_TEST_END();
10970 #endif /* POLARSSL_AES_C */
10971 
10972 #ifdef POLARSSL_AES_C
10973 
10974  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
10975  size_t first_length = 16;
10976  size_t second_length = 6;
10977  size_t length = first_length + second_length;
10978  unsigned char key[32];
10979  unsigned char iv[16];
10980 
10981  cipher_context_t ctx_dec;
10982  cipher_context_t ctx_enc;
10983  const cipher_info_t *cipher_info;
10984 
10985  unsigned char inbuf[64];
10986  unsigned char encbuf[64];
10987  unsigned char decbuf[64];
10988 
10989  size_t outlen = 0;
10990  size_t totaloutlen = 0;
10991  size_t enclen = 0;
10992 
10993  memset( key, 0, 32 );
10994  memset( iv , 0, 16 );
10995 
10996  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
10997  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
10998 
10999  memset( inbuf, 5, 64 );
11000  memset( encbuf, 0, 64 );
11001  memset( decbuf, 0, 64 );
11002 
11003  /* Initialise enc and dec contexts */
11005  fct_chk( NULL != cipher_info);
11006 
11007  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
11008  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
11009 
11010  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
11011  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
11012 
11013  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
11014  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
11015 
11016  if( POLARSSL_MODE_CBC == cipher_info->mode )
11017  {
11018  enclen = cipher_get_block_size(&ctx_enc )
11019  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
11020  }
11021  else
11022  {
11023  enclen = length;
11024  }
11025 
11026  /* encode length number of bytes from inbuf */
11027  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
11028  totaloutlen = outlen;
11029  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
11030  totaloutlen += outlen;
11031  if( POLARSSL_MODE_CBC == cipher_info->mode )
11032  {
11033  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
11034  }
11035  else
11036  {
11037  fct_chk( totaloutlen == enclen );
11038  }
11039  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
11040  totaloutlen += outlen;
11041  if( POLARSSL_MODE_CBC == cipher_info->mode )
11042  {
11043  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
11044  }
11045  else
11046  {
11047  fct_chk( outlen == 0 );
11048  }
11049 
11050  /* decode the previously encoded string */
11051  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
11052  if( POLARSSL_MODE_CBC == cipher_info->mode )
11053  {
11054  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
11055  }
11056  else
11057  {
11058  fct_chk( enclen == outlen );
11059  }
11060  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
11061  if( POLARSSL_MODE_CBC == cipher_info->mode )
11062  {
11063  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
11064  }
11065  else
11066  {
11067  fct_chk( outlen == 0 );
11068  }
11069 
11070 
11071  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
11072 
11073  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
11074  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
11075  FCT_TEST_END();
11076 #endif /* POLARSSL_AES_C */
11077 
11078 #ifdef POLARSSL_AES_C
11079 
11080  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
11081  size_t first_length = 17;
11082  size_t second_length = 6;
11083  size_t length = first_length + second_length;
11084  unsigned char key[32];
11085  unsigned char iv[16];
11086 
11087  cipher_context_t ctx_dec;
11088  cipher_context_t ctx_enc;
11089  const cipher_info_t *cipher_info;
11090 
11091  unsigned char inbuf[64];
11092  unsigned char encbuf[64];
11093  unsigned char decbuf[64];
11094 
11095  size_t outlen = 0;
11096  size_t totaloutlen = 0;
11097  size_t enclen = 0;
11098 
11099  memset( key, 0, 32 );
11100  memset( iv , 0, 16 );
11101 
11102  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
11103  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
11104 
11105  memset( inbuf, 5, 64 );
11106  memset( encbuf, 0, 64 );
11107  memset( decbuf, 0, 64 );
11108 
11109  /* Initialise enc and dec contexts */
11111  fct_chk( NULL != cipher_info);
11112 
11113  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
11114  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
11115 
11116  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
11117  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
11118 
11119  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
11120  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
11121 
11122  if( POLARSSL_MODE_CBC == cipher_info->mode )
11123  {
11124  enclen = cipher_get_block_size(&ctx_enc )
11125  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
11126  }
11127  else
11128  {
11129  enclen = length;
11130  }
11131 
11132  /* encode length number of bytes from inbuf */
11133  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
11134  totaloutlen = outlen;
11135  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
11136  totaloutlen += outlen;
11137  if( POLARSSL_MODE_CBC == cipher_info->mode )
11138  {
11139  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
11140  }
11141  else
11142  {
11143  fct_chk( totaloutlen == enclen );
11144  }
11145  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
11146  totaloutlen += outlen;
11147  if( POLARSSL_MODE_CBC == cipher_info->mode )
11148  {
11149  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
11150  }
11151  else
11152  {
11153  fct_chk( outlen == 0 );
11154  }
11155 
11156  /* decode the previously encoded string */
11157  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
11158  if( POLARSSL_MODE_CBC == cipher_info->mode )
11159  {
11160  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
11161  }
11162  else
11163  {
11164  fct_chk( enclen == outlen );
11165  }
11166  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
11167  if( POLARSSL_MODE_CBC == cipher_info->mode )
11168  {
11169  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
11170  }
11171  else
11172  {
11173  fct_chk( outlen == 0 );
11174  }
11175 
11176 
11177  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
11178 
11179  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
11180  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
11181  FCT_TEST_END();
11182 #endif /* POLARSSL_AES_C */
11183 
11184 #ifdef POLARSSL_AES_C
11185 
11186  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
11187  size_t first_length = 16;
11188  size_t second_length = 16;
11189  size_t length = first_length + second_length;
11190  unsigned char key[32];
11191  unsigned char iv[16];
11192 
11193  cipher_context_t ctx_dec;
11194  cipher_context_t ctx_enc;
11195  const cipher_info_t *cipher_info;
11196 
11197  unsigned char inbuf[64];
11198  unsigned char encbuf[64];
11199  unsigned char decbuf[64];
11200 
11201  size_t outlen = 0;
11202  size_t totaloutlen = 0;
11203  size_t enclen = 0;
11204 
11205  memset( key, 0, 32 );
11206  memset( iv , 0, 16 );
11207 
11208  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
11209  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
11210 
11211  memset( inbuf, 5, 64 );
11212  memset( encbuf, 0, 64 );
11213  memset( decbuf, 0, 64 );
11214 
11215  /* Initialise enc and dec contexts */
11217  fct_chk( NULL != cipher_info);
11218 
11219  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
11220  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
11221 
11222  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 192, POLARSSL_DECRYPT ) );
11223  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 192, POLARSSL_ENCRYPT ) );
11224 
11225  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
11226  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
11227 
11228  if( POLARSSL_MODE_CBC == cipher_info->mode )
11229  {
11230  enclen = cipher_get_block_size(&ctx_enc )
11231  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
11232  }
11233  else
11234  {
11235  enclen = length;
11236  }
11237 
11238  /* encode length number of bytes from inbuf */
11239  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
11240  totaloutlen = outlen;
11241  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
11242  totaloutlen += outlen;
11243  if( POLARSSL_MODE_CBC == cipher_info->mode )
11244  {
11245  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
11246  }
11247  else
11248  {
11249  fct_chk( totaloutlen == enclen );
11250  }
11251  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
11252  totaloutlen += outlen;
11253  if( POLARSSL_MODE_CBC == cipher_info->mode )
11254  {
11255  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
11256  }
11257  else
11258  {
11259  fct_chk( outlen == 0 );
11260  }
11261 
11262  /* decode the previously encoded string */
11263  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
11264  if( POLARSSL_MODE_CBC == cipher_info->mode )
11265  {
11266  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
11267  }
11268  else
11269  {
11270  fct_chk( enclen == outlen );
11271  }
11272  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
11273  if( POLARSSL_MODE_CBC == cipher_info->mode )
11274  {
11275  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
11276  }
11277  else
11278  {
11279  fct_chk( outlen == 0 );
11280  }
11281 
11282 
11283  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
11284 
11285  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
11286  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
11287  FCT_TEST_END();
11288 #endif /* POLARSSL_AES_C */
11289 
11290 #ifdef POLARSSL_AES_C
11291 
11292  FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes)
11293  size_t length = 0;
11294  unsigned char key[32];
11295  unsigned char iv[16];
11296 
11297  const cipher_info_t *cipher_info;
11298  cipher_context_t ctx_dec;
11299  cipher_context_t ctx_enc;
11300 
11301  unsigned char inbuf[64];
11302  unsigned char encbuf[64];
11303  unsigned char decbuf[64];
11304 
11305  size_t outlen = 0;
11306  size_t enclen = 0;
11307 
11308  memset( key, 0, 32 );
11309  memset( iv , 0, 16 );
11310 
11311  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
11312  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
11313 
11314  memset( inbuf, 5, 64 );
11315  memset( encbuf, 0, 64 );
11316  memset( decbuf, 0, 64 );
11317 
11318  /* Check and get info structures */
11320  fct_chk( NULL != cipher_info );
11321  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
11322 
11323  /* Initialise enc and dec contexts */
11324  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
11325  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
11326 
11327  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
11328  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
11329 
11330  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
11331  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
11332 
11333  if( POLARSSL_MODE_CBC == cipher_info->mode )
11334  {
11335  enclen = cipher_get_block_size( &ctx_enc )
11336  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
11337  }
11338  else
11339  {
11340  enclen = length;
11341  }
11342 
11343  /* encode length number of bytes from inbuf */
11344  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
11345  if( POLARSSL_MODE_CBC == cipher_info->mode )
11346  {
11347  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
11348  }
11349  else
11350  {
11351  fct_chk( outlen == enclen );
11352  }
11353 
11354  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
11355  if( POLARSSL_MODE_CBC == cipher_info->mode )
11356  {
11357  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
11358  }
11359  else
11360  {
11361  fct_chk( outlen == 0 );
11362  }
11363 
11364 
11365  /* decode the previously encoded string */
11366  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
11367  if( POLARSSL_MODE_CBC == cipher_info->mode )
11368  {
11369  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
11370  }
11371  else
11372  {
11373  fct_chk( enclen == outlen );
11374  }
11375 
11376  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
11377  if( POLARSSL_MODE_CBC == cipher_info->mode )
11378  {
11379  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
11380  }
11381  else
11382  {
11383  fct_chk( outlen == 0 );
11384  }
11385 
11386 
11387  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
11388 
11389  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
11390  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
11391  FCT_TEST_END();
11392 #endif /* POLARSSL_AES_C */
11393 
11394 #ifdef POLARSSL_AES_C
11395 
11396  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_byte)
11397  size_t length = 1;
11398  unsigned char key[32];
11399  unsigned char iv[16];
11400 
11401  const cipher_info_t *cipher_info;
11402  cipher_context_t ctx_dec;
11403  cipher_context_t ctx_enc;
11404 
11405  unsigned char inbuf[64];
11406  unsigned char encbuf[64];
11407  unsigned char decbuf[64];
11408 
11409  size_t outlen = 0;
11410  size_t enclen = 0;
11411 
11412  memset( key, 0, 32 );
11413  memset( iv , 0, 16 );
11414 
11415  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
11416  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
11417 
11418  memset( inbuf, 5, 64 );
11419  memset( encbuf, 0, 64 );
11420  memset( decbuf, 0, 64 );
11421 
11422  /* Check and get info structures */
11424  fct_chk( NULL != cipher_info );
11425  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
11426 
11427  /* Initialise enc and dec contexts */
11428  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
11429  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
11430 
11431  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
11432  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
11433 
11434  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
11435  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
11436 
11437  if( POLARSSL_MODE_CBC == cipher_info->mode )
11438  {
11439  enclen = cipher_get_block_size( &ctx_enc )
11440  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
11441  }
11442  else
11443  {
11444  enclen = length;
11445  }
11446 
11447  /* encode length number of bytes from inbuf */
11448  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
11449  if( POLARSSL_MODE_CBC == cipher_info->mode )
11450  {
11451  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
11452  }
11453  else
11454  {
11455  fct_chk( outlen == enclen );
11456  }
11457 
11458  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
11459  if( POLARSSL_MODE_CBC == cipher_info->mode )
11460  {
11461  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
11462  }
11463  else
11464  {
11465  fct_chk( outlen == 0 );
11466  }
11467 
11468 
11469  /* decode the previously encoded string */
11470  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
11471  if( POLARSSL_MODE_CBC == cipher_info->mode )
11472  {
11473  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
11474  }
11475  else
11476  {
11477  fct_chk( enclen == outlen );
11478  }
11479 
11480  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
11481  if( POLARSSL_MODE_CBC == cipher_info->mode )
11482  {
11483  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
11484  }
11485  else
11486  {
11487  fct_chk( outlen == 0 );
11488  }
11489 
11490 
11491  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
11492 
11493  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
11494  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
11495  FCT_TEST_END();
11496 #endif /* POLARSSL_AES_C */
11497 
11498 #ifdef POLARSSL_AES_C
11499 
11500  FCT_TEST_BGN(aes_encrypt_and_decrypt_2_bytes)
11501  size_t length = 2;
11502  unsigned char key[32];
11503  unsigned char iv[16];
11504 
11505  const cipher_info_t *cipher_info;
11506  cipher_context_t ctx_dec;
11507  cipher_context_t ctx_enc;
11508 
11509  unsigned char inbuf[64];
11510  unsigned char encbuf[64];
11511  unsigned char decbuf[64];
11512 
11513  size_t outlen = 0;
11514  size_t enclen = 0;
11515 
11516  memset( key, 0, 32 );
11517  memset( iv , 0, 16 );
11518 
11519  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
11520  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
11521 
11522  memset( inbuf, 5, 64 );
11523  memset( encbuf, 0, 64 );
11524  memset( decbuf, 0, 64 );
11525 
11526  /* Check and get info structures */
11528  fct_chk( NULL != cipher_info );
11529  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
11530 
11531  /* Initialise enc and dec contexts */
11532  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
11533  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
11534 
11535  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
11536  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
11537 
11538  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
11539  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
11540 
11541  if( POLARSSL_MODE_CBC == cipher_info->mode )
11542  {
11543  enclen = cipher_get_block_size( &ctx_enc )
11544  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
11545  }
11546  else
11547  {
11548  enclen = length;
11549  }
11550 
11551  /* encode length number of bytes from inbuf */
11552  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
11553  if( POLARSSL_MODE_CBC == cipher_info->mode )
11554  {
11555  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
11556  }
11557  else
11558  {
11559  fct_chk( outlen == enclen );
11560  }
11561 
11562  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
11563  if( POLARSSL_MODE_CBC == cipher_info->mode )
11564  {
11565  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
11566  }
11567  else
11568  {
11569  fct_chk( outlen == 0 );
11570  }
11571 
11572 
11573  /* decode the previously encoded string */
11574  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
11575  if( POLARSSL_MODE_CBC == cipher_info->mode )
11576  {
11577  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
11578  }
11579  else
11580  {
11581  fct_chk( enclen == outlen );
11582  }
11583 
11584  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
11585  if( POLARSSL_MODE_CBC == cipher_info->mode )
11586  {
11587  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
11588  }
11589  else
11590  {
11591  fct_chk( outlen == 0 );
11592  }
11593 
11594 
11595  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
11596 
11597  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
11598  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
11599  FCT_TEST_END();
11600 #endif /* POLARSSL_AES_C */
11601 
11602 #ifdef POLARSSL_AES_C
11603 
11604  FCT_TEST_BGN(aes_encrypt_and_decrypt_7_bytes)
11605  size_t length = 7;
11606  unsigned char key[32];
11607  unsigned char iv[16];
11608 
11609  const cipher_info_t *cipher_info;
11610  cipher_context_t ctx_dec;
11611  cipher_context_t ctx_enc;
11612 
11613  unsigned char inbuf[64];
11614  unsigned char encbuf[64];
11615  unsigned char decbuf[64];
11616 
11617  size_t outlen = 0;
11618  size_t enclen = 0;
11619 
11620  memset( key, 0, 32 );
11621  memset( iv , 0, 16 );
11622 
11623  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
11624  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
11625 
11626  memset( inbuf, 5, 64 );
11627  memset( encbuf, 0, 64 );
11628  memset( decbuf, 0, 64 );
11629 
11630  /* Check and get info structures */
11632  fct_chk( NULL != cipher_info );
11633  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
11634 
11635  /* Initialise enc and dec contexts */
11636  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
11637  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
11638 
11639  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
11640  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
11641 
11642  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
11643  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
11644 
11645  if( POLARSSL_MODE_CBC == cipher_info->mode )
11646  {
11647  enclen = cipher_get_block_size( &ctx_enc )
11648  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
11649  }
11650  else
11651  {
11652  enclen = length;
11653  }
11654 
11655  /* encode length number of bytes from inbuf */
11656  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
11657  if( POLARSSL_MODE_CBC == cipher_info->mode )
11658  {
11659  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
11660  }
11661  else
11662  {
11663  fct_chk( outlen == enclen );
11664  }
11665 
11666  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
11667  if( POLARSSL_MODE_CBC == cipher_info->mode )
11668  {
11669  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
11670  }
11671  else
11672  {
11673  fct_chk( outlen == 0 );
11674  }
11675 
11676 
11677  /* decode the previously encoded string */
11678  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
11679  if( POLARSSL_MODE_CBC == cipher_info->mode )
11680  {
11681  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
11682  }
11683  else
11684  {
11685  fct_chk( enclen == outlen );
11686  }
11687 
11688  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
11689  if( POLARSSL_MODE_CBC == cipher_info->mode )
11690  {
11691  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
11692  }
11693  else
11694  {
11695  fct_chk( outlen == 0 );
11696  }
11697 
11698 
11699  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
11700 
11701  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
11702  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
11703  FCT_TEST_END();
11704 #endif /* POLARSSL_AES_C */
11705 
11706 #ifdef POLARSSL_AES_C
11707 
11708  FCT_TEST_BGN(aes_encrypt_and_decrypt_8_bytes)
11709  size_t length = 8;
11710  unsigned char key[32];
11711  unsigned char iv[16];
11712 
11713  const cipher_info_t *cipher_info;
11714  cipher_context_t ctx_dec;
11715  cipher_context_t ctx_enc;
11716 
11717  unsigned char inbuf[64];
11718  unsigned char encbuf[64];
11719  unsigned char decbuf[64];
11720 
11721  size_t outlen = 0;
11722  size_t enclen = 0;
11723 
11724  memset( key, 0, 32 );
11725  memset( iv , 0, 16 );
11726 
11727  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
11728  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
11729 
11730  memset( inbuf, 5, 64 );
11731  memset( encbuf, 0, 64 );
11732  memset( decbuf, 0, 64 );
11733 
11734  /* Check and get info structures */
11736  fct_chk( NULL != cipher_info );
11737  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
11738 
11739  /* Initialise enc and dec contexts */
11740  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
11741  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
11742 
11743  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
11744  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
11745 
11746  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
11747  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
11748 
11749  if( POLARSSL_MODE_CBC == cipher_info->mode )
11750  {
11751  enclen = cipher_get_block_size( &ctx_enc )
11752  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
11753  }
11754  else
11755  {
11756  enclen = length;
11757  }
11758 
11759  /* encode length number of bytes from inbuf */
11760  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
11761  if( POLARSSL_MODE_CBC == cipher_info->mode )
11762  {
11763  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
11764  }
11765  else
11766  {
11767  fct_chk( outlen == enclen );
11768  }
11769 
11770  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
11771  if( POLARSSL_MODE_CBC == cipher_info->mode )
11772  {
11773  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
11774  }
11775  else
11776  {
11777  fct_chk( outlen == 0 );
11778  }
11779 
11780 
11781  /* decode the previously encoded string */
11782  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
11783  if( POLARSSL_MODE_CBC == cipher_info->mode )
11784  {
11785  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
11786  }
11787  else
11788  {
11789  fct_chk( enclen == outlen );
11790  }
11791 
11792  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
11793  if( POLARSSL_MODE_CBC == cipher_info->mode )
11794  {
11795  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
11796  }
11797  else
11798  {
11799  fct_chk( outlen == 0 );
11800  }
11801 
11802 
11803  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
11804 
11805  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
11806  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
11807  FCT_TEST_END();
11808 #endif /* POLARSSL_AES_C */
11809 
11810 #ifdef POLARSSL_AES_C
11811 
11812  FCT_TEST_BGN(aes_encrypt_and_decrypt_9_bytes)
11813  size_t length = 9;
11814  unsigned char key[32];
11815  unsigned char iv[16];
11816 
11817  const cipher_info_t *cipher_info;
11818  cipher_context_t ctx_dec;
11819  cipher_context_t ctx_enc;
11820 
11821  unsigned char inbuf[64];
11822  unsigned char encbuf[64];
11823  unsigned char decbuf[64];
11824 
11825  size_t outlen = 0;
11826  size_t enclen = 0;
11827 
11828  memset( key, 0, 32 );
11829  memset( iv , 0, 16 );
11830 
11831  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
11832  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
11833 
11834  memset( inbuf, 5, 64 );
11835  memset( encbuf, 0, 64 );
11836  memset( decbuf, 0, 64 );
11837 
11838  /* Check and get info structures */
11840  fct_chk( NULL != cipher_info );
11841  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
11842 
11843  /* Initialise enc and dec contexts */
11844  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
11845  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
11846 
11847  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
11848  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
11849 
11850  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
11851  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
11852 
11853  if( POLARSSL_MODE_CBC == cipher_info->mode )
11854  {
11855  enclen = cipher_get_block_size( &ctx_enc )
11856  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
11857  }
11858  else
11859  {
11860  enclen = length;
11861  }
11862 
11863  /* encode length number of bytes from inbuf */
11864  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
11865  if( POLARSSL_MODE_CBC == cipher_info->mode )
11866  {
11867  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
11868  }
11869  else
11870  {
11871  fct_chk( outlen == enclen );
11872  }
11873 
11874  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
11875  if( POLARSSL_MODE_CBC == cipher_info->mode )
11876  {
11877  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
11878  }
11879  else
11880  {
11881  fct_chk( outlen == 0 );
11882  }
11883 
11884 
11885  /* decode the previously encoded string */
11886  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
11887  if( POLARSSL_MODE_CBC == cipher_info->mode )
11888  {
11889  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
11890  }
11891  else
11892  {
11893  fct_chk( enclen == outlen );
11894  }
11895 
11896  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
11897  if( POLARSSL_MODE_CBC == cipher_info->mode )
11898  {
11899  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
11900  }
11901  else
11902  {
11903  fct_chk( outlen == 0 );
11904  }
11905 
11906 
11907  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
11908 
11909  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
11910  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
11911  FCT_TEST_END();
11912 #endif /* POLARSSL_AES_C */
11913 
11914 #ifdef POLARSSL_AES_C
11915 
11916  FCT_TEST_BGN(aes_encrypt_and_decrypt_15_bytes)
11917  size_t length = 15;
11918  unsigned char key[32];
11919  unsigned char iv[16];
11920 
11921  const cipher_info_t *cipher_info;
11922  cipher_context_t ctx_dec;
11923  cipher_context_t ctx_enc;
11924 
11925  unsigned char inbuf[64];
11926  unsigned char encbuf[64];
11927  unsigned char decbuf[64];
11928 
11929  size_t outlen = 0;
11930  size_t enclen = 0;
11931 
11932  memset( key, 0, 32 );
11933  memset( iv , 0, 16 );
11934 
11935  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
11936  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
11937 
11938  memset( inbuf, 5, 64 );
11939  memset( encbuf, 0, 64 );
11940  memset( decbuf, 0, 64 );
11941 
11942  /* Check and get info structures */
11944  fct_chk( NULL != cipher_info );
11945  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
11946 
11947  /* Initialise enc and dec contexts */
11948  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
11949  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
11950 
11951  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
11952  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
11953 
11954  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
11955  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
11956 
11957  if( POLARSSL_MODE_CBC == cipher_info->mode )
11958  {
11959  enclen = cipher_get_block_size( &ctx_enc )
11960  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
11961  }
11962  else
11963  {
11964  enclen = length;
11965  }
11966 
11967  /* encode length number of bytes from inbuf */
11968  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
11969  if( POLARSSL_MODE_CBC == cipher_info->mode )
11970  {
11971  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
11972  }
11973  else
11974  {
11975  fct_chk( outlen == enclen );
11976  }
11977 
11978  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
11979  if( POLARSSL_MODE_CBC == cipher_info->mode )
11980  {
11981  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
11982  }
11983  else
11984  {
11985  fct_chk( outlen == 0 );
11986  }
11987 
11988 
11989  /* decode the previously encoded string */
11990  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
11991  if( POLARSSL_MODE_CBC == cipher_info->mode )
11992  {
11993  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
11994  }
11995  else
11996  {
11997  fct_chk( enclen == outlen );
11998  }
11999 
12000  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
12001  if( POLARSSL_MODE_CBC == cipher_info->mode )
12002  {
12003  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
12004  }
12005  else
12006  {
12007  fct_chk( outlen == 0 );
12008  }
12009 
12010 
12011  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
12012 
12013  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
12014  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
12015  FCT_TEST_END();
12016 #endif /* POLARSSL_AES_C */
12017 
12018 #ifdef POLARSSL_AES_C
12019 
12020  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes)
12021  size_t length = 16;
12022  unsigned char key[32];
12023  unsigned char iv[16];
12024 
12025  const cipher_info_t *cipher_info;
12026  cipher_context_t ctx_dec;
12027  cipher_context_t ctx_enc;
12028 
12029  unsigned char inbuf[64];
12030  unsigned char encbuf[64];
12031  unsigned char decbuf[64];
12032 
12033  size_t outlen = 0;
12034  size_t enclen = 0;
12035 
12036  memset( key, 0, 32 );
12037  memset( iv , 0, 16 );
12038 
12039  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
12040  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
12041 
12042  memset( inbuf, 5, 64 );
12043  memset( encbuf, 0, 64 );
12044  memset( decbuf, 0, 64 );
12045 
12046  /* Check and get info structures */
12048  fct_chk( NULL != cipher_info );
12049  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
12050 
12051  /* Initialise enc and dec contexts */
12052  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
12053  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
12054 
12055  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
12056  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
12057 
12058  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
12059  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
12060 
12061  if( POLARSSL_MODE_CBC == cipher_info->mode )
12062  {
12063  enclen = cipher_get_block_size( &ctx_enc )
12064  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
12065  }
12066  else
12067  {
12068  enclen = length;
12069  }
12070 
12071  /* encode length number of bytes from inbuf */
12072  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
12073  if( POLARSSL_MODE_CBC == cipher_info->mode )
12074  {
12075  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
12076  }
12077  else
12078  {
12079  fct_chk( outlen == enclen );
12080  }
12081 
12082  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
12083  if( POLARSSL_MODE_CBC == cipher_info->mode )
12084  {
12085  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
12086  }
12087  else
12088  {
12089  fct_chk( outlen == 0 );
12090  }
12091 
12092 
12093  /* decode the previously encoded string */
12094  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
12095  if( POLARSSL_MODE_CBC == cipher_info->mode )
12096  {
12097  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
12098  }
12099  else
12100  {
12101  fct_chk( enclen == outlen );
12102  }
12103 
12104  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
12105  if( POLARSSL_MODE_CBC == cipher_info->mode )
12106  {
12107  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
12108  }
12109  else
12110  {
12111  fct_chk( outlen == 0 );
12112  }
12113 
12114 
12115  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
12116 
12117  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
12118  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
12119  FCT_TEST_END();
12120 #endif /* POLARSSL_AES_C */
12121 
12122 #ifdef POLARSSL_AES_C
12123 
12124  FCT_TEST_BGN(aes_encrypt_and_decrypt_17_bytes)
12125  size_t length = 17;
12126  unsigned char key[32];
12127  unsigned char iv[16];
12128 
12129  const cipher_info_t *cipher_info;
12130  cipher_context_t ctx_dec;
12131  cipher_context_t ctx_enc;
12132 
12133  unsigned char inbuf[64];
12134  unsigned char encbuf[64];
12135  unsigned char decbuf[64];
12136 
12137  size_t outlen = 0;
12138  size_t enclen = 0;
12139 
12140  memset( key, 0, 32 );
12141  memset( iv , 0, 16 );
12142 
12143  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
12144  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
12145 
12146  memset( inbuf, 5, 64 );
12147  memset( encbuf, 0, 64 );
12148  memset( decbuf, 0, 64 );
12149 
12150  /* Check and get info structures */
12152  fct_chk( NULL != cipher_info );
12153  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
12154 
12155  /* Initialise enc and dec contexts */
12156  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
12157  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
12158 
12159  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
12160  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
12161 
12162  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
12163  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
12164 
12165  if( POLARSSL_MODE_CBC == cipher_info->mode )
12166  {
12167  enclen = cipher_get_block_size( &ctx_enc )
12168  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
12169  }
12170  else
12171  {
12172  enclen = length;
12173  }
12174 
12175  /* encode length number of bytes from inbuf */
12176  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
12177  if( POLARSSL_MODE_CBC == cipher_info->mode )
12178  {
12179  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
12180  }
12181  else
12182  {
12183  fct_chk( outlen == enclen );
12184  }
12185 
12186  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
12187  if( POLARSSL_MODE_CBC == cipher_info->mode )
12188  {
12189  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
12190  }
12191  else
12192  {
12193  fct_chk( outlen == 0 );
12194  }
12195 
12196 
12197  /* decode the previously encoded string */
12198  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
12199  if( POLARSSL_MODE_CBC == cipher_info->mode )
12200  {
12201  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
12202  }
12203  else
12204  {
12205  fct_chk( enclen == outlen );
12206  }
12207 
12208  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
12209  if( POLARSSL_MODE_CBC == cipher_info->mode )
12210  {
12211  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
12212  }
12213  else
12214  {
12215  fct_chk( outlen == 0 );
12216  }
12217 
12218 
12219  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
12220 
12221  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
12222  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
12223  FCT_TEST_END();
12224 #endif /* POLARSSL_AES_C */
12225 
12226 #ifdef POLARSSL_AES_C
12227 
12228  FCT_TEST_BGN(aes_encrypt_and_decrypt_31_bytes)
12229  size_t length = 31;
12230  unsigned char key[32];
12231  unsigned char iv[16];
12232 
12233  const cipher_info_t *cipher_info;
12234  cipher_context_t ctx_dec;
12235  cipher_context_t ctx_enc;
12236 
12237  unsigned char inbuf[64];
12238  unsigned char encbuf[64];
12239  unsigned char decbuf[64];
12240 
12241  size_t outlen = 0;
12242  size_t enclen = 0;
12243 
12244  memset( key, 0, 32 );
12245  memset( iv , 0, 16 );
12246 
12247  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
12248  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
12249 
12250  memset( inbuf, 5, 64 );
12251  memset( encbuf, 0, 64 );
12252  memset( decbuf, 0, 64 );
12253 
12254  /* Check and get info structures */
12256  fct_chk( NULL != cipher_info );
12257  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
12258 
12259  /* Initialise enc and dec contexts */
12260  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
12261  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
12262 
12263  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
12264  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
12265 
12266  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
12267  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
12268 
12269  if( POLARSSL_MODE_CBC == cipher_info->mode )
12270  {
12271  enclen = cipher_get_block_size( &ctx_enc )
12272  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
12273  }
12274  else
12275  {
12276  enclen = length;
12277  }
12278 
12279  /* encode length number of bytes from inbuf */
12280  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
12281  if( POLARSSL_MODE_CBC == cipher_info->mode )
12282  {
12283  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
12284  }
12285  else
12286  {
12287  fct_chk( outlen == enclen );
12288  }
12289 
12290  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
12291  if( POLARSSL_MODE_CBC == cipher_info->mode )
12292  {
12293  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
12294  }
12295  else
12296  {
12297  fct_chk( outlen == 0 );
12298  }
12299 
12300 
12301  /* decode the previously encoded string */
12302  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
12303  if( POLARSSL_MODE_CBC == cipher_info->mode )
12304  {
12305  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
12306  }
12307  else
12308  {
12309  fct_chk( enclen == outlen );
12310  }
12311 
12312  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
12313  if( POLARSSL_MODE_CBC == cipher_info->mode )
12314  {
12315  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
12316  }
12317  else
12318  {
12319  fct_chk( outlen == 0 );
12320  }
12321 
12322 
12323  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
12324 
12325  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
12326  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
12327  FCT_TEST_END();
12328 #endif /* POLARSSL_AES_C */
12329 
12330 #ifdef POLARSSL_AES_C
12331 
12332  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes)
12333  size_t length = 32;
12334  unsigned char key[32];
12335  unsigned char iv[16];
12336 
12337  const cipher_info_t *cipher_info;
12338  cipher_context_t ctx_dec;
12339  cipher_context_t ctx_enc;
12340 
12341  unsigned char inbuf[64];
12342  unsigned char encbuf[64];
12343  unsigned char decbuf[64];
12344 
12345  size_t outlen = 0;
12346  size_t enclen = 0;
12347 
12348  memset( key, 0, 32 );
12349  memset( iv , 0, 16 );
12350 
12351  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
12352  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
12353 
12354  memset( inbuf, 5, 64 );
12355  memset( encbuf, 0, 64 );
12356  memset( decbuf, 0, 64 );
12357 
12358  /* Check and get info structures */
12360  fct_chk( NULL != cipher_info );
12361  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
12362 
12363  /* Initialise enc and dec contexts */
12364  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
12365  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
12366 
12367  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
12368  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
12369 
12370  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
12371  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
12372 
12373  if( POLARSSL_MODE_CBC == cipher_info->mode )
12374  {
12375  enclen = cipher_get_block_size( &ctx_enc )
12376  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
12377  }
12378  else
12379  {
12380  enclen = length;
12381  }
12382 
12383  /* encode length number of bytes from inbuf */
12384  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
12385  if( POLARSSL_MODE_CBC == cipher_info->mode )
12386  {
12387  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
12388  }
12389  else
12390  {
12391  fct_chk( outlen == enclen );
12392  }
12393 
12394  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
12395  if( POLARSSL_MODE_CBC == cipher_info->mode )
12396  {
12397  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
12398  }
12399  else
12400  {
12401  fct_chk( outlen == 0 );
12402  }
12403 
12404 
12405  /* decode the previously encoded string */
12406  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
12407  if( POLARSSL_MODE_CBC == cipher_info->mode )
12408  {
12409  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
12410  }
12411  else
12412  {
12413  fct_chk( enclen == outlen );
12414  }
12415 
12416  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
12417  if( POLARSSL_MODE_CBC == cipher_info->mode )
12418  {
12419  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
12420  }
12421  else
12422  {
12423  fct_chk( outlen == 0 );
12424  }
12425 
12426 
12427  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
12428 
12429  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
12430  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
12431  FCT_TEST_END();
12432 #endif /* POLARSSL_AES_C */
12433 
12434 #ifdef POLARSSL_AES_C
12435 
12436  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes)
12437  size_t length = 33;
12438  unsigned char key[32];
12439  unsigned char iv[16];
12440 
12441  const cipher_info_t *cipher_info;
12442  cipher_context_t ctx_dec;
12443  cipher_context_t ctx_enc;
12444 
12445  unsigned char inbuf[64];
12446  unsigned char encbuf[64];
12447  unsigned char decbuf[64];
12448 
12449  size_t outlen = 0;
12450  size_t enclen = 0;
12451 
12452  memset( key, 0, 32 );
12453  memset( iv , 0, 16 );
12454 
12455  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
12456  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
12457 
12458  memset( inbuf, 5, 64 );
12459  memset( encbuf, 0, 64 );
12460  memset( decbuf, 0, 64 );
12461 
12462  /* Check and get info structures */
12464  fct_chk( NULL != cipher_info );
12465  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
12466 
12467  /* Initialise enc and dec contexts */
12468  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
12469  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
12470 
12471  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
12472  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
12473 
12474  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
12475  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
12476 
12477  if( POLARSSL_MODE_CBC == cipher_info->mode )
12478  {
12479  enclen = cipher_get_block_size( &ctx_enc )
12480  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
12481  }
12482  else
12483  {
12484  enclen = length;
12485  }
12486 
12487  /* encode length number of bytes from inbuf */
12488  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
12489  if( POLARSSL_MODE_CBC == cipher_info->mode )
12490  {
12491  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
12492  }
12493  else
12494  {
12495  fct_chk( outlen == enclen );
12496  }
12497 
12498  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
12499  if( POLARSSL_MODE_CBC == cipher_info->mode )
12500  {
12501  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
12502  }
12503  else
12504  {
12505  fct_chk( outlen == 0 );
12506  }
12507 
12508 
12509  /* decode the previously encoded string */
12510  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
12511  if( POLARSSL_MODE_CBC == cipher_info->mode )
12512  {
12513  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
12514  }
12515  else
12516  {
12517  fct_chk( enclen == outlen );
12518  }
12519 
12520  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
12521  if( POLARSSL_MODE_CBC == cipher_info->mode )
12522  {
12523  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
12524  }
12525  else
12526  {
12527  fct_chk( outlen == 0 );
12528  }
12529 
12530 
12531  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
12532 
12533  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
12534  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
12535  FCT_TEST_END();
12536 #endif /* POLARSSL_AES_C */
12537 
12538 #ifdef POLARSSL_AES_C
12539 
12540  FCT_TEST_BGN(aes_encrypt_and_decrypt_47_bytes)
12541  size_t length = 47;
12542  unsigned char key[32];
12543  unsigned char iv[16];
12544 
12545  const cipher_info_t *cipher_info;
12546  cipher_context_t ctx_dec;
12547  cipher_context_t ctx_enc;
12548 
12549  unsigned char inbuf[64];
12550  unsigned char encbuf[64];
12551  unsigned char decbuf[64];
12552 
12553  size_t outlen = 0;
12554  size_t enclen = 0;
12555 
12556  memset( key, 0, 32 );
12557  memset( iv , 0, 16 );
12558 
12559  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
12560  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
12561 
12562  memset( inbuf, 5, 64 );
12563  memset( encbuf, 0, 64 );
12564  memset( decbuf, 0, 64 );
12565 
12566  /* Check and get info structures */
12568  fct_chk( NULL != cipher_info );
12569  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
12570 
12571  /* Initialise enc and dec contexts */
12572  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
12573  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
12574 
12575  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
12576  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
12577 
12578  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
12579  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
12580 
12581  if( POLARSSL_MODE_CBC == cipher_info->mode )
12582  {
12583  enclen = cipher_get_block_size( &ctx_enc )
12584  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
12585  }
12586  else
12587  {
12588  enclen = length;
12589  }
12590 
12591  /* encode length number of bytes from inbuf */
12592  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
12593  if( POLARSSL_MODE_CBC == cipher_info->mode )
12594  {
12595  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
12596  }
12597  else
12598  {
12599  fct_chk( outlen == enclen );
12600  }
12601 
12602  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
12603  if( POLARSSL_MODE_CBC == cipher_info->mode )
12604  {
12605  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
12606  }
12607  else
12608  {
12609  fct_chk( outlen == 0 );
12610  }
12611 
12612 
12613  /* decode the previously encoded string */
12614  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
12615  if( POLARSSL_MODE_CBC == cipher_info->mode )
12616  {
12617  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
12618  }
12619  else
12620  {
12621  fct_chk( enclen == outlen );
12622  }
12623 
12624  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
12625  if( POLARSSL_MODE_CBC == cipher_info->mode )
12626  {
12627  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
12628  }
12629  else
12630  {
12631  fct_chk( outlen == 0 );
12632  }
12633 
12634 
12635  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
12636 
12637  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
12638  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
12639  FCT_TEST_END();
12640 #endif /* POLARSSL_AES_C */
12641 
12642 #ifdef POLARSSL_AES_C
12643 
12644  FCT_TEST_BGN(aes_encrypt_and_decrypt_48_bytes)
12645  size_t length = 48;
12646  unsigned char key[32];
12647  unsigned char iv[16];
12648 
12649  const cipher_info_t *cipher_info;
12650  cipher_context_t ctx_dec;
12651  cipher_context_t ctx_enc;
12652 
12653  unsigned char inbuf[64];
12654  unsigned char encbuf[64];
12655  unsigned char decbuf[64];
12656 
12657  size_t outlen = 0;
12658  size_t enclen = 0;
12659 
12660  memset( key, 0, 32 );
12661  memset( iv , 0, 16 );
12662 
12663  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
12664  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
12665 
12666  memset( inbuf, 5, 64 );
12667  memset( encbuf, 0, 64 );
12668  memset( decbuf, 0, 64 );
12669 
12670  /* Check and get info structures */
12672  fct_chk( NULL != cipher_info );
12673  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
12674 
12675  /* Initialise enc and dec contexts */
12676  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
12677  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
12678 
12679  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
12680  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
12681 
12682  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
12683  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
12684 
12685  if( POLARSSL_MODE_CBC == cipher_info->mode )
12686  {
12687  enclen = cipher_get_block_size( &ctx_enc )
12688  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
12689  }
12690  else
12691  {
12692  enclen = length;
12693  }
12694 
12695  /* encode length number of bytes from inbuf */
12696  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
12697  if( POLARSSL_MODE_CBC == cipher_info->mode )
12698  {
12699  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
12700  }
12701  else
12702  {
12703  fct_chk( outlen == enclen );
12704  }
12705 
12706  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
12707  if( POLARSSL_MODE_CBC == cipher_info->mode )
12708  {
12709  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
12710  }
12711  else
12712  {
12713  fct_chk( outlen == 0 );
12714  }
12715 
12716 
12717  /* decode the previously encoded string */
12718  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
12719  if( POLARSSL_MODE_CBC == cipher_info->mode )
12720  {
12721  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
12722  }
12723  else
12724  {
12725  fct_chk( enclen == outlen );
12726  }
12727 
12728  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
12729  if( POLARSSL_MODE_CBC == cipher_info->mode )
12730  {
12731  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
12732  }
12733  else
12734  {
12735  fct_chk( outlen == 0 );
12736  }
12737 
12738 
12739  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
12740 
12741  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
12742  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
12743  FCT_TEST_END();
12744 #endif /* POLARSSL_AES_C */
12745 
12746 #ifdef POLARSSL_AES_C
12747 
12748  FCT_TEST_BGN(aes_encrypt_and_decrypt_49_bytes)
12749  size_t length = 49;
12750  unsigned char key[32];
12751  unsigned char iv[16];
12752 
12753  const cipher_info_t *cipher_info;
12754  cipher_context_t ctx_dec;
12755  cipher_context_t ctx_enc;
12756 
12757  unsigned char inbuf[64];
12758  unsigned char encbuf[64];
12759  unsigned char decbuf[64];
12760 
12761  size_t outlen = 0;
12762  size_t enclen = 0;
12763 
12764  memset( key, 0, 32 );
12765  memset( iv , 0, 16 );
12766 
12767  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
12768  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
12769 
12770  memset( inbuf, 5, 64 );
12771  memset( encbuf, 0, 64 );
12772  memset( decbuf, 0, 64 );
12773 
12774  /* Check and get info structures */
12776  fct_chk( NULL != cipher_info );
12777  fct_chk( cipher_info_from_string( "AES-256-CBC" ) == cipher_info );
12778 
12779  /* Initialise enc and dec contexts */
12780  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
12781  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
12782 
12783  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
12784  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
12785 
12786  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
12787  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
12788 
12789  if( POLARSSL_MODE_CBC == cipher_info->mode )
12790  {
12791  enclen = cipher_get_block_size( &ctx_enc )
12792  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
12793  }
12794  else
12795  {
12796  enclen = length;
12797  }
12798 
12799  /* encode length number of bytes from inbuf */
12800  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
12801  if( POLARSSL_MODE_CBC == cipher_info->mode )
12802  {
12803  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
12804  }
12805  else
12806  {
12807  fct_chk( outlen == enclen );
12808  }
12809 
12810  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
12811  if( POLARSSL_MODE_CBC == cipher_info->mode )
12812  {
12813  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
12814  }
12815  else
12816  {
12817  fct_chk( outlen == 0 );
12818  }
12819 
12820 
12821  /* decode the previously encoded string */
12822  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
12823  if( POLARSSL_MODE_CBC == cipher_info->mode )
12824  {
12825  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
12826  }
12827  else
12828  {
12829  fct_chk( enclen == outlen );
12830  }
12831 
12832  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
12833  if( POLARSSL_MODE_CBC == cipher_info->mode )
12834  {
12835  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
12836  }
12837  else
12838  {
12839  fct_chk( outlen == 0 );
12840  }
12841 
12842 
12843  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
12844 
12845  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
12846  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
12847  FCT_TEST_END();
12848 #endif /* POLARSSL_AES_C */
12849 
12850 #ifdef POLARSSL_AES_C
12851 
12852  FCT_TEST_BGN(aes_encrypt_and_decrypt_0_bytes_in_multiple_parts)
12853  size_t first_length = 0;
12854  size_t second_length = 0;
12855  size_t length = first_length + second_length;
12856  unsigned char key[32];
12857  unsigned char iv[16];
12858 
12859  cipher_context_t ctx_dec;
12860  cipher_context_t ctx_enc;
12861  const cipher_info_t *cipher_info;
12862 
12863  unsigned char inbuf[64];
12864  unsigned char encbuf[64];
12865  unsigned char decbuf[64];
12866 
12867  size_t outlen = 0;
12868  size_t totaloutlen = 0;
12869  size_t enclen = 0;
12870 
12871  memset( key, 0, 32 );
12872  memset( iv , 0, 16 );
12873 
12874  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
12875  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
12876 
12877  memset( inbuf, 5, 64 );
12878  memset( encbuf, 0, 64 );
12879  memset( decbuf, 0, 64 );
12880 
12881  /* Initialise enc and dec contexts */
12883  fct_chk( NULL != cipher_info);
12884 
12885  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
12886  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
12887 
12888  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
12889  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
12890 
12891  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
12892  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
12893 
12894  if( POLARSSL_MODE_CBC == cipher_info->mode )
12895  {
12896  enclen = cipher_get_block_size(&ctx_enc )
12897  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
12898  }
12899  else
12900  {
12901  enclen = length;
12902  }
12903 
12904  /* encode length number of bytes from inbuf */
12905  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
12906  totaloutlen = outlen;
12907  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
12908  totaloutlen += outlen;
12909  if( POLARSSL_MODE_CBC == cipher_info->mode )
12910  {
12911  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
12912  }
12913  else
12914  {
12915  fct_chk( totaloutlen == enclen );
12916  }
12917  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
12918  totaloutlen += outlen;
12919  if( POLARSSL_MODE_CBC == cipher_info->mode )
12920  {
12921  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
12922  }
12923  else
12924  {
12925  fct_chk( outlen == 0 );
12926  }
12927 
12928  /* decode the previously encoded string */
12929  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
12930  if( POLARSSL_MODE_CBC == cipher_info->mode )
12931  {
12932  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
12933  }
12934  else
12935  {
12936  fct_chk( enclen == outlen );
12937  }
12938  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
12939  if( POLARSSL_MODE_CBC == cipher_info->mode )
12940  {
12941  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
12942  }
12943  else
12944  {
12945  fct_chk( outlen == 0 );
12946  }
12947 
12948 
12949  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
12950 
12951  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
12952  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
12953  FCT_TEST_END();
12954 #endif /* POLARSSL_AES_C */
12955 
12956 #ifdef POLARSSL_AES_C
12957 
12958  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
12959  size_t first_length = 1;
12960  size_t second_length = 0;
12961  size_t length = first_length + second_length;
12962  unsigned char key[32];
12963  unsigned char iv[16];
12964 
12965  cipher_context_t ctx_dec;
12966  cipher_context_t ctx_enc;
12967  const cipher_info_t *cipher_info;
12968 
12969  unsigned char inbuf[64];
12970  unsigned char encbuf[64];
12971  unsigned char decbuf[64];
12972 
12973  size_t outlen = 0;
12974  size_t totaloutlen = 0;
12975  size_t enclen = 0;
12976 
12977  memset( key, 0, 32 );
12978  memset( iv , 0, 16 );
12979 
12980  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
12981  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
12982 
12983  memset( inbuf, 5, 64 );
12984  memset( encbuf, 0, 64 );
12985  memset( decbuf, 0, 64 );
12986 
12987  /* Initialise enc and dec contexts */
12989  fct_chk( NULL != cipher_info);
12990 
12991  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
12992  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
12993 
12994  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
12995  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
12996 
12997  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
12998  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
12999 
13000  if( POLARSSL_MODE_CBC == cipher_info->mode )
13001  {
13002  enclen = cipher_get_block_size(&ctx_enc )
13003  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
13004  }
13005  else
13006  {
13007  enclen = length;
13008  }
13009 
13010  /* encode length number of bytes from inbuf */
13011  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
13012  totaloutlen = outlen;
13013  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
13014  totaloutlen += outlen;
13015  if( POLARSSL_MODE_CBC == cipher_info->mode )
13016  {
13017  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
13018  }
13019  else
13020  {
13021  fct_chk( totaloutlen == enclen );
13022  }
13023  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
13024  totaloutlen += outlen;
13025  if( POLARSSL_MODE_CBC == cipher_info->mode )
13026  {
13027  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
13028  }
13029  else
13030  {
13031  fct_chk( outlen == 0 );
13032  }
13033 
13034  /* decode the previously encoded string */
13035  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
13036  if( POLARSSL_MODE_CBC == cipher_info->mode )
13037  {
13038  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
13039  }
13040  else
13041  {
13042  fct_chk( enclen == outlen );
13043  }
13044  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
13045  if( POLARSSL_MODE_CBC == cipher_info->mode )
13046  {
13047  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
13048  }
13049  else
13050  {
13051  fct_chk( outlen == 0 );
13052  }
13053 
13054 
13055  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
13056 
13057  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
13058  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
13059  FCT_TEST_END();
13060 #endif /* POLARSSL_AES_C */
13061 
13062 #ifdef POLARSSL_AES_C
13063 
13064  FCT_TEST_BGN(aes_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
13065  size_t first_length = 0;
13066  size_t second_length = 1;
13067  size_t length = first_length + second_length;
13068  unsigned char key[32];
13069  unsigned char iv[16];
13070 
13071  cipher_context_t ctx_dec;
13072  cipher_context_t ctx_enc;
13073  const cipher_info_t *cipher_info;
13074 
13075  unsigned char inbuf[64];
13076  unsigned char encbuf[64];
13077  unsigned char decbuf[64];
13078 
13079  size_t outlen = 0;
13080  size_t totaloutlen = 0;
13081  size_t enclen = 0;
13082 
13083  memset( key, 0, 32 );
13084  memset( iv , 0, 16 );
13085 
13086  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
13087  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
13088 
13089  memset( inbuf, 5, 64 );
13090  memset( encbuf, 0, 64 );
13091  memset( decbuf, 0, 64 );
13092 
13093  /* Initialise enc and dec contexts */
13095  fct_chk( NULL != cipher_info);
13096 
13097  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
13098  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
13099 
13100  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
13101  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
13102 
13103  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
13104  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
13105 
13106  if( POLARSSL_MODE_CBC == cipher_info->mode )
13107  {
13108  enclen = cipher_get_block_size(&ctx_enc )
13109  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
13110  }
13111  else
13112  {
13113  enclen = length;
13114  }
13115 
13116  /* encode length number of bytes from inbuf */
13117  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
13118  totaloutlen = outlen;
13119  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
13120  totaloutlen += outlen;
13121  if( POLARSSL_MODE_CBC == cipher_info->mode )
13122  {
13123  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
13124  }
13125  else
13126  {
13127  fct_chk( totaloutlen == enclen );
13128  }
13129  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
13130  totaloutlen += outlen;
13131  if( POLARSSL_MODE_CBC == cipher_info->mode )
13132  {
13133  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
13134  }
13135  else
13136  {
13137  fct_chk( outlen == 0 );
13138  }
13139 
13140  /* decode the previously encoded string */
13141  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
13142  if( POLARSSL_MODE_CBC == cipher_info->mode )
13143  {
13144  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
13145  }
13146  else
13147  {
13148  fct_chk( enclen == outlen );
13149  }
13150  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
13151  if( POLARSSL_MODE_CBC == cipher_info->mode )
13152  {
13153  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
13154  }
13155  else
13156  {
13157  fct_chk( outlen == 0 );
13158  }
13159 
13160 
13161  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
13162 
13163  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
13164  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
13165  FCT_TEST_END();
13166 #endif /* POLARSSL_AES_C */
13167 
13168 #ifdef POLARSSL_AES_C
13169 
13170  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
13171  size_t first_length = 16;
13172  size_t second_length = 0;
13173  size_t length = first_length + second_length;
13174  unsigned char key[32];
13175  unsigned char iv[16];
13176 
13177  cipher_context_t ctx_dec;
13178  cipher_context_t ctx_enc;
13179  const cipher_info_t *cipher_info;
13180 
13181  unsigned char inbuf[64];
13182  unsigned char encbuf[64];
13183  unsigned char decbuf[64];
13184 
13185  size_t outlen = 0;
13186  size_t totaloutlen = 0;
13187  size_t enclen = 0;
13188 
13189  memset( key, 0, 32 );
13190  memset( iv , 0, 16 );
13191 
13192  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
13193  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
13194 
13195  memset( inbuf, 5, 64 );
13196  memset( encbuf, 0, 64 );
13197  memset( decbuf, 0, 64 );
13198 
13199  /* Initialise enc and dec contexts */
13201  fct_chk( NULL != cipher_info);
13202 
13203  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
13204  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
13205 
13206  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
13207  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
13208 
13209  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
13210  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
13211 
13212  if( POLARSSL_MODE_CBC == cipher_info->mode )
13213  {
13214  enclen = cipher_get_block_size(&ctx_enc )
13215  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
13216  }
13217  else
13218  {
13219  enclen = length;
13220  }
13221 
13222  /* encode length number of bytes from inbuf */
13223  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
13224  totaloutlen = outlen;
13225  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
13226  totaloutlen += outlen;
13227  if( POLARSSL_MODE_CBC == cipher_info->mode )
13228  {
13229  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
13230  }
13231  else
13232  {
13233  fct_chk( totaloutlen == enclen );
13234  }
13235  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
13236  totaloutlen += outlen;
13237  if( POLARSSL_MODE_CBC == cipher_info->mode )
13238  {
13239  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
13240  }
13241  else
13242  {
13243  fct_chk( outlen == 0 );
13244  }
13245 
13246  /* decode the previously encoded string */
13247  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
13248  if( POLARSSL_MODE_CBC == cipher_info->mode )
13249  {
13250  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
13251  }
13252  else
13253  {
13254  fct_chk( enclen == outlen );
13255  }
13256  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
13257  if( POLARSSL_MODE_CBC == cipher_info->mode )
13258  {
13259  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
13260  }
13261  else
13262  {
13263  fct_chk( outlen == 0 );
13264  }
13265 
13266 
13267  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
13268 
13269  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
13270  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
13271  FCT_TEST_END();
13272 #endif /* POLARSSL_AES_C */
13273 
13274 #ifdef POLARSSL_AES_C
13275 
13276  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
13277  size_t first_length = 0;
13278  size_t second_length = 16;
13279  size_t length = first_length + second_length;
13280  unsigned char key[32];
13281  unsigned char iv[16];
13282 
13283  cipher_context_t ctx_dec;
13284  cipher_context_t ctx_enc;
13285  const cipher_info_t *cipher_info;
13286 
13287  unsigned char inbuf[64];
13288  unsigned char encbuf[64];
13289  unsigned char decbuf[64];
13290 
13291  size_t outlen = 0;
13292  size_t totaloutlen = 0;
13293  size_t enclen = 0;
13294 
13295  memset( key, 0, 32 );
13296  memset( iv , 0, 16 );
13297 
13298  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
13299  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
13300 
13301  memset( inbuf, 5, 64 );
13302  memset( encbuf, 0, 64 );
13303  memset( decbuf, 0, 64 );
13304 
13305  /* Initialise enc and dec contexts */
13307  fct_chk( NULL != cipher_info);
13308 
13309  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
13310  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
13311 
13312  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
13313  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
13314 
13315  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
13316  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
13317 
13318  if( POLARSSL_MODE_CBC == cipher_info->mode )
13319  {
13320  enclen = cipher_get_block_size(&ctx_enc )
13321  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
13322  }
13323  else
13324  {
13325  enclen = length;
13326  }
13327 
13328  /* encode length number of bytes from inbuf */
13329  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
13330  totaloutlen = outlen;
13331  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
13332  totaloutlen += outlen;
13333  if( POLARSSL_MODE_CBC == cipher_info->mode )
13334  {
13335  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
13336  }
13337  else
13338  {
13339  fct_chk( totaloutlen == enclen );
13340  }
13341  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
13342  totaloutlen += outlen;
13343  if( POLARSSL_MODE_CBC == cipher_info->mode )
13344  {
13345  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
13346  }
13347  else
13348  {
13349  fct_chk( outlen == 0 );
13350  }
13351 
13352  /* decode the previously encoded string */
13353  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
13354  if( POLARSSL_MODE_CBC == cipher_info->mode )
13355  {
13356  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
13357  }
13358  else
13359  {
13360  fct_chk( enclen == outlen );
13361  }
13362  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
13363  if( POLARSSL_MODE_CBC == cipher_info->mode )
13364  {
13365  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
13366  }
13367  else
13368  {
13369  fct_chk( outlen == 0 );
13370  }
13371 
13372 
13373  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
13374 
13375  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
13376  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
13377  FCT_TEST_END();
13378 #endif /* POLARSSL_AES_C */
13379 
13380 #ifdef POLARSSL_AES_C
13381 
13382  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
13383  size_t first_length = 1;
13384  size_t second_length = 15;
13385  size_t length = first_length + second_length;
13386  unsigned char key[32];
13387  unsigned char iv[16];
13388 
13389  cipher_context_t ctx_dec;
13390  cipher_context_t ctx_enc;
13391  const cipher_info_t *cipher_info;
13392 
13393  unsigned char inbuf[64];
13394  unsigned char encbuf[64];
13395  unsigned char decbuf[64];
13396 
13397  size_t outlen = 0;
13398  size_t totaloutlen = 0;
13399  size_t enclen = 0;
13400 
13401  memset( key, 0, 32 );
13402  memset( iv , 0, 16 );
13403 
13404  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
13405  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
13406 
13407  memset( inbuf, 5, 64 );
13408  memset( encbuf, 0, 64 );
13409  memset( decbuf, 0, 64 );
13410 
13411  /* Initialise enc and dec contexts */
13413  fct_chk( NULL != cipher_info);
13414 
13415  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
13416  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
13417 
13418  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
13419  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
13420 
13421  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
13422  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
13423 
13424  if( POLARSSL_MODE_CBC == cipher_info->mode )
13425  {
13426  enclen = cipher_get_block_size(&ctx_enc )
13427  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
13428  }
13429  else
13430  {
13431  enclen = length;
13432  }
13433 
13434  /* encode length number of bytes from inbuf */
13435  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
13436  totaloutlen = outlen;
13437  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
13438  totaloutlen += outlen;
13439  if( POLARSSL_MODE_CBC == cipher_info->mode )
13440  {
13441  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
13442  }
13443  else
13444  {
13445  fct_chk( totaloutlen == enclen );
13446  }
13447  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
13448  totaloutlen += outlen;
13449  if( POLARSSL_MODE_CBC == cipher_info->mode )
13450  {
13451  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
13452  }
13453  else
13454  {
13455  fct_chk( outlen == 0 );
13456  }
13457 
13458  /* decode the previously encoded string */
13459  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
13460  if( POLARSSL_MODE_CBC == cipher_info->mode )
13461  {
13462  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
13463  }
13464  else
13465  {
13466  fct_chk( enclen == outlen );
13467  }
13468  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
13469  if( POLARSSL_MODE_CBC == cipher_info->mode )
13470  {
13471  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
13472  }
13473  else
13474  {
13475  fct_chk( outlen == 0 );
13476  }
13477 
13478 
13479  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
13480 
13481  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
13482  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
13483  FCT_TEST_END();
13484 #endif /* POLARSSL_AES_C */
13485 
13486 #ifdef POLARSSL_AES_C
13487 
13488  FCT_TEST_BGN(aes_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
13489  size_t first_length = 15;
13490  size_t second_length = 1;
13491  size_t length = first_length + second_length;
13492  unsigned char key[32];
13493  unsigned char iv[16];
13494 
13495  cipher_context_t ctx_dec;
13496  cipher_context_t ctx_enc;
13497  const cipher_info_t *cipher_info;
13498 
13499  unsigned char inbuf[64];
13500  unsigned char encbuf[64];
13501  unsigned char decbuf[64];
13502 
13503  size_t outlen = 0;
13504  size_t totaloutlen = 0;
13505  size_t enclen = 0;
13506 
13507  memset( key, 0, 32 );
13508  memset( iv , 0, 16 );
13509 
13510  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
13511  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
13512 
13513  memset( inbuf, 5, 64 );
13514  memset( encbuf, 0, 64 );
13515  memset( decbuf, 0, 64 );
13516 
13517  /* Initialise enc and dec contexts */
13519  fct_chk( NULL != cipher_info);
13520 
13521  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
13522  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
13523 
13524  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
13525  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
13526 
13527  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
13528  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
13529 
13530  if( POLARSSL_MODE_CBC == cipher_info->mode )
13531  {
13532  enclen = cipher_get_block_size(&ctx_enc )
13533  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
13534  }
13535  else
13536  {
13537  enclen = length;
13538  }
13539 
13540  /* encode length number of bytes from inbuf */
13541  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
13542  totaloutlen = outlen;
13543  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
13544  totaloutlen += outlen;
13545  if( POLARSSL_MODE_CBC == cipher_info->mode )
13546  {
13547  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
13548  }
13549  else
13550  {
13551  fct_chk( totaloutlen == enclen );
13552  }
13553  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
13554  totaloutlen += outlen;
13555  if( POLARSSL_MODE_CBC == cipher_info->mode )
13556  {
13557  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
13558  }
13559  else
13560  {
13561  fct_chk( outlen == 0 );
13562  }
13563 
13564  /* decode the previously encoded string */
13565  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
13566  if( POLARSSL_MODE_CBC == cipher_info->mode )
13567  {
13568  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
13569  }
13570  else
13571  {
13572  fct_chk( enclen == outlen );
13573  }
13574  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
13575  if( POLARSSL_MODE_CBC == cipher_info->mode )
13576  {
13577  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
13578  }
13579  else
13580  {
13581  fct_chk( outlen == 0 );
13582  }
13583 
13584 
13585  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
13586 
13587  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
13588  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
13589  FCT_TEST_END();
13590 #endif /* POLARSSL_AES_C */
13591 
13592 #ifdef POLARSSL_AES_C
13593 
13594  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
13595  size_t first_length = 15;
13596  size_t second_length = 7;
13597  size_t length = first_length + second_length;
13598  unsigned char key[32];
13599  unsigned char iv[16];
13600 
13601  cipher_context_t ctx_dec;
13602  cipher_context_t ctx_enc;
13603  const cipher_info_t *cipher_info;
13604 
13605  unsigned char inbuf[64];
13606  unsigned char encbuf[64];
13607  unsigned char decbuf[64];
13608 
13609  size_t outlen = 0;
13610  size_t totaloutlen = 0;
13611  size_t enclen = 0;
13612 
13613  memset( key, 0, 32 );
13614  memset( iv , 0, 16 );
13615 
13616  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
13617  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
13618 
13619  memset( inbuf, 5, 64 );
13620  memset( encbuf, 0, 64 );
13621  memset( decbuf, 0, 64 );
13622 
13623  /* Initialise enc and dec contexts */
13625  fct_chk( NULL != cipher_info);
13626 
13627  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
13628  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
13629 
13630  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
13631  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
13632 
13633  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
13634  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
13635 
13636  if( POLARSSL_MODE_CBC == cipher_info->mode )
13637  {
13638  enclen = cipher_get_block_size(&ctx_enc )
13639  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
13640  }
13641  else
13642  {
13643  enclen = length;
13644  }
13645 
13646  /* encode length number of bytes from inbuf */
13647  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
13648  totaloutlen = outlen;
13649  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
13650  totaloutlen += outlen;
13651  if( POLARSSL_MODE_CBC == cipher_info->mode )
13652  {
13653  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
13654  }
13655  else
13656  {
13657  fct_chk( totaloutlen == enclen );
13658  }
13659  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
13660  totaloutlen += outlen;
13661  if( POLARSSL_MODE_CBC == cipher_info->mode )
13662  {
13663  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
13664  }
13665  else
13666  {
13667  fct_chk( outlen == 0 );
13668  }
13669 
13670  /* decode the previously encoded string */
13671  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
13672  if( POLARSSL_MODE_CBC == cipher_info->mode )
13673  {
13674  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
13675  }
13676  else
13677  {
13678  fct_chk( enclen == outlen );
13679  }
13680  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
13681  if( POLARSSL_MODE_CBC == cipher_info->mode )
13682  {
13683  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
13684  }
13685  else
13686  {
13687  fct_chk( outlen == 0 );
13688  }
13689 
13690 
13691  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
13692 
13693  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
13694  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
13695  FCT_TEST_END();
13696 #endif /* POLARSSL_AES_C */
13697 
13698 #ifdef POLARSSL_AES_C
13699 
13700  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
13701  size_t first_length = 16;
13702  size_t second_length = 6;
13703  size_t length = first_length + second_length;
13704  unsigned char key[32];
13705  unsigned char iv[16];
13706 
13707  cipher_context_t ctx_dec;
13708  cipher_context_t ctx_enc;
13709  const cipher_info_t *cipher_info;
13710 
13711  unsigned char inbuf[64];
13712  unsigned char encbuf[64];
13713  unsigned char decbuf[64];
13714 
13715  size_t outlen = 0;
13716  size_t totaloutlen = 0;
13717  size_t enclen = 0;
13718 
13719  memset( key, 0, 32 );
13720  memset( iv , 0, 16 );
13721 
13722  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
13723  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
13724 
13725  memset( inbuf, 5, 64 );
13726  memset( encbuf, 0, 64 );
13727  memset( decbuf, 0, 64 );
13728 
13729  /* Initialise enc and dec contexts */
13731  fct_chk( NULL != cipher_info);
13732 
13733  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
13734  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
13735 
13736  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
13737  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
13738 
13739  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
13740  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
13741 
13742  if( POLARSSL_MODE_CBC == cipher_info->mode )
13743  {
13744  enclen = cipher_get_block_size(&ctx_enc )
13745  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
13746  }
13747  else
13748  {
13749  enclen = length;
13750  }
13751 
13752  /* encode length number of bytes from inbuf */
13753  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
13754  totaloutlen = outlen;
13755  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
13756  totaloutlen += outlen;
13757  if( POLARSSL_MODE_CBC == cipher_info->mode )
13758  {
13759  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
13760  }
13761  else
13762  {
13763  fct_chk( totaloutlen == enclen );
13764  }
13765  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
13766  totaloutlen += outlen;
13767  if( POLARSSL_MODE_CBC == cipher_info->mode )
13768  {
13769  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
13770  }
13771  else
13772  {
13773  fct_chk( outlen == 0 );
13774  }
13775 
13776  /* decode the previously encoded string */
13777  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
13778  if( POLARSSL_MODE_CBC == cipher_info->mode )
13779  {
13780  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
13781  }
13782  else
13783  {
13784  fct_chk( enclen == outlen );
13785  }
13786  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
13787  if( POLARSSL_MODE_CBC == cipher_info->mode )
13788  {
13789  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
13790  }
13791  else
13792  {
13793  fct_chk( outlen == 0 );
13794  }
13795 
13796 
13797  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
13798 
13799  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
13800  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
13801  FCT_TEST_END();
13802 #endif /* POLARSSL_AES_C */
13803 
13804 #ifdef POLARSSL_AES_C
13805 
13806  FCT_TEST_BGN(aes_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
13807  size_t first_length = 17;
13808  size_t second_length = 6;
13809  size_t length = first_length + second_length;
13810  unsigned char key[32];
13811  unsigned char iv[16];
13812 
13813  cipher_context_t ctx_dec;
13814  cipher_context_t ctx_enc;
13815  const cipher_info_t *cipher_info;
13816 
13817  unsigned char inbuf[64];
13818  unsigned char encbuf[64];
13819  unsigned char decbuf[64];
13820 
13821  size_t outlen = 0;
13822  size_t totaloutlen = 0;
13823  size_t enclen = 0;
13824 
13825  memset( key, 0, 32 );
13826  memset( iv , 0, 16 );
13827 
13828  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
13829  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
13830 
13831  memset( inbuf, 5, 64 );
13832  memset( encbuf, 0, 64 );
13833  memset( decbuf, 0, 64 );
13834 
13835  /* Initialise enc and dec contexts */
13837  fct_chk( NULL != cipher_info);
13838 
13839  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
13840  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
13841 
13842  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
13843  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
13844 
13845  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
13846  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
13847 
13848  if( POLARSSL_MODE_CBC == cipher_info->mode )
13849  {
13850  enclen = cipher_get_block_size(&ctx_enc )
13851  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
13852  }
13853  else
13854  {
13855  enclen = length;
13856  }
13857 
13858  /* encode length number of bytes from inbuf */
13859  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
13860  totaloutlen = outlen;
13861  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
13862  totaloutlen += outlen;
13863  if( POLARSSL_MODE_CBC == cipher_info->mode )
13864  {
13865  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
13866  }
13867  else
13868  {
13869  fct_chk( totaloutlen == enclen );
13870  }
13871  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
13872  totaloutlen += outlen;
13873  if( POLARSSL_MODE_CBC == cipher_info->mode )
13874  {
13875  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
13876  }
13877  else
13878  {
13879  fct_chk( outlen == 0 );
13880  }
13881 
13882  /* decode the previously encoded string */
13883  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
13884  if( POLARSSL_MODE_CBC == cipher_info->mode )
13885  {
13886  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
13887  }
13888  else
13889  {
13890  fct_chk( enclen == outlen );
13891  }
13892  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
13893  if( POLARSSL_MODE_CBC == cipher_info->mode )
13894  {
13895  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
13896  }
13897  else
13898  {
13899  fct_chk( outlen == 0 );
13900  }
13901 
13902 
13903  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
13904 
13905  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
13906  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
13907  FCT_TEST_END();
13908 #endif /* POLARSSL_AES_C */
13909 
13910 #ifdef POLARSSL_AES_C
13911 
13912  FCT_TEST_BGN(aes_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
13913  size_t first_length = 16;
13914  size_t second_length = 16;
13915  size_t length = first_length + second_length;
13916  unsigned char key[32];
13917  unsigned char iv[16];
13918 
13919  cipher_context_t ctx_dec;
13920  cipher_context_t ctx_enc;
13921  const cipher_info_t *cipher_info;
13922 
13923  unsigned char inbuf[64];
13924  unsigned char encbuf[64];
13925  unsigned char decbuf[64];
13926 
13927  size_t outlen = 0;
13928  size_t totaloutlen = 0;
13929  size_t enclen = 0;
13930 
13931  memset( key, 0, 32 );
13932  memset( iv , 0, 16 );
13933 
13934  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
13935  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
13936 
13937  memset( inbuf, 5, 64 );
13938  memset( encbuf, 0, 64 );
13939  memset( decbuf, 0, 64 );
13940 
13941  /* Initialise enc and dec contexts */
13943  fct_chk( NULL != cipher_info);
13944 
13945  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
13946  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
13947 
13948  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 256, POLARSSL_DECRYPT ) );
13949  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 256, POLARSSL_ENCRYPT ) );
13950 
13951  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
13952  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
13953 
13954  if( POLARSSL_MODE_CBC == cipher_info->mode )
13955  {
13956  enclen = cipher_get_block_size(&ctx_enc )
13957  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
13958  }
13959  else
13960  {
13961  enclen = length;
13962  }
13963 
13964  /* encode length number of bytes from inbuf */
13965  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
13966  totaloutlen = outlen;
13967  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
13968  totaloutlen += outlen;
13969  if( POLARSSL_MODE_CBC == cipher_info->mode )
13970  {
13971  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
13972  }
13973  else
13974  {
13975  fct_chk( totaloutlen == enclen );
13976  }
13977  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
13978  totaloutlen += outlen;
13979  if( POLARSSL_MODE_CBC == cipher_info->mode )
13980  {
13981  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
13982  }
13983  else
13984  {
13985  fct_chk( outlen == 0 );
13986  }
13987 
13988  /* decode the previously encoded string */
13989  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
13990  if( POLARSSL_MODE_CBC == cipher_info->mode )
13991  {
13992  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
13993  }
13994  else
13995  {
13996  fct_chk( enclen == outlen );
13997  }
13998  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
13999  if( POLARSSL_MODE_CBC == cipher_info->mode )
14000  {
14001  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
14002  }
14003  else
14004  {
14005  fct_chk( outlen == 0 );
14006  }
14007 
14008 
14009  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
14010 
14011  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
14012  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
14013  FCT_TEST_END();
14014 #endif /* POLARSSL_AES_C */
14015 
14016  }
14017  FCT_SUITE_END();
14018 
14019 #endif /* POLARSSL_CIPHER_C */
14020 
14021 }
14022 FCT_END();
14023