PolarSSL v1.2.8
test_suite_cipher.des.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_DES_C
283 
284  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
320  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
385 
386 #ifdef POLARSSL_DES_C
387 
388  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
424  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
489 
490 #ifdef POLARSSL_DES_C
491 
492  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
528  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
593 
594 #ifdef POLARSSL_DES_C
595 
596  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
632  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
697 
698 #ifdef POLARSSL_DES_C
699 
700  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
736  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
801 
802 #ifdef POLARSSL_DES_C
803 
804  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
840  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
905 
906 #ifdef POLARSSL_DES_C
907 
908  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
944  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
1009 
1010 #ifdef POLARSSL_DES_C
1011 
1012  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
1048  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
1113 
1114 #ifdef POLARSSL_DES_C
1115 
1116  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
1152  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
1217 
1218 #ifdef POLARSSL_DES_C
1219 
1220  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
1256  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
1321 
1322 #ifdef POLARSSL_DES_C
1323 
1324  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
1360  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
1425 
1426 #ifdef POLARSSL_DES_C
1427 
1428  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
1464  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
1529 
1530 #ifdef POLARSSL_DES_C
1531 
1532  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
1568  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
1633 
1634 #ifdef POLARSSL_DES_C
1635 
1636  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
1672  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
1737 
1738 #ifdef POLARSSL_DES_C
1739 
1740  FCT_TEST_BGN(des_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( "DES-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, 56, POLARSSL_DECRYPT ) );
1776  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
1841 
1842 #ifdef POLARSSL_DES_C
1843 
1844  FCT_TEST_BGN(des_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, 56, POLARSSL_DECRYPT ) );
1881  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
1947 
1948 #ifdef POLARSSL_DES_C
1949 
1950  FCT_TEST_BGN(des_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, 56, POLARSSL_DECRYPT ) );
1987  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
2053 
2054 #ifdef POLARSSL_DES_C
2055 
2056  FCT_TEST_BGN(des_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, 56, POLARSSL_DECRYPT ) );
2093  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
2159 
2160 #ifdef POLARSSL_DES_C
2161 
2162  FCT_TEST_BGN(des_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, 56, POLARSSL_DECRYPT ) );
2199  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
2265 
2266 #ifdef POLARSSL_DES_C
2267 
2268  FCT_TEST_BGN(des_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, 56, POLARSSL_DECRYPT ) );
2305  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
2371 
2372 #ifdef POLARSSL_DES_C
2373 
2374  FCT_TEST_BGN(des_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, 56, POLARSSL_DECRYPT ) );
2411  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
2477 
2478 #ifdef POLARSSL_DES_C
2479 
2480  FCT_TEST_BGN(des_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, 56, POLARSSL_DECRYPT ) );
2517  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
2583 
2584 #ifdef POLARSSL_DES_C
2585 
2586  FCT_TEST_BGN(des_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, 56, POLARSSL_DECRYPT ) );
2623  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
2689 
2690 #ifdef POLARSSL_DES_C
2691 
2692  FCT_TEST_BGN(des_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, 56, POLARSSL_DECRYPT ) );
2729  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
2795 
2796 #ifdef POLARSSL_DES_C
2797 
2798  FCT_TEST_BGN(des_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, 56, POLARSSL_DECRYPT ) );
2835  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
2901 
2902 #ifdef POLARSSL_DES_C
2903 
2904  FCT_TEST_BGN(des_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, 56, POLARSSL_DECRYPT ) );
2941  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, 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_DES_C */
3007 
3008 #ifdef POLARSSL_DES_C
3009 
3010  FCT_TEST_BGN(des_encrypt_and_decrypt_0_bytes)
3011  size_t length = 0;
3012  unsigned char key[32];
3013  unsigned char iv[16];
3014 
3015  const cipher_info_t *cipher_info;
3016  cipher_context_t ctx_dec;
3017  cipher_context_t ctx_enc;
3018 
3019  unsigned char inbuf[64];
3020  unsigned char encbuf[64];
3021  unsigned char decbuf[64];
3022 
3023  size_t outlen = 0;
3024  size_t enclen = 0;
3025 
3026  memset( key, 0, 32 );
3027  memset( iv , 0, 16 );
3028 
3029  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3030  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3031 
3032  memset( inbuf, 5, 64 );
3033  memset( encbuf, 0, 64 );
3034  memset( decbuf, 0, 64 );
3035 
3036  /* Check and get info structures */
3038  fct_chk( NULL != cipher_info );
3039  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3040 
3041  /* Initialise enc and dec contexts */
3042  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3043  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3044 
3045  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3046  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3047 
3048  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3049  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3050 
3051  if( POLARSSL_MODE_CBC == cipher_info->mode )
3052  {
3053  enclen = cipher_get_block_size( &ctx_enc )
3054  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3055  }
3056  else
3057  {
3058  enclen = length;
3059  }
3060 
3061  /* encode length number of bytes from inbuf */
3062  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3063  if( POLARSSL_MODE_CBC == cipher_info->mode )
3064  {
3065  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3066  }
3067  else
3068  {
3069  fct_chk( outlen == enclen );
3070  }
3071 
3072  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3073  if( POLARSSL_MODE_CBC == cipher_info->mode )
3074  {
3075  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3076  }
3077  else
3078  {
3079  fct_chk( outlen == 0 );
3080  }
3081 
3082 
3083  /* decode the previously encoded string */
3084  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3085  if( POLARSSL_MODE_CBC == cipher_info->mode )
3086  {
3087  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3088  }
3089  else
3090  {
3091  fct_chk( enclen == outlen );
3092  }
3093 
3094  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3095  if( POLARSSL_MODE_CBC == cipher_info->mode )
3096  {
3097  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3098  }
3099  else
3100  {
3101  fct_chk( outlen == 0 );
3102  }
3103 
3104 
3105  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3106 
3107  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3108  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3109  FCT_TEST_END();
3110 #endif /* POLARSSL_DES_C */
3111 
3112 #ifdef POLARSSL_DES_C
3113 
3114  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_byte)
3115  size_t length = 1;
3116  unsigned char key[32];
3117  unsigned char iv[16];
3118 
3119  const cipher_info_t *cipher_info;
3120  cipher_context_t ctx_dec;
3121  cipher_context_t ctx_enc;
3122 
3123  unsigned char inbuf[64];
3124  unsigned char encbuf[64];
3125  unsigned char decbuf[64];
3126 
3127  size_t outlen = 0;
3128  size_t enclen = 0;
3129 
3130  memset( key, 0, 32 );
3131  memset( iv , 0, 16 );
3132 
3133  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3134  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3135 
3136  memset( inbuf, 5, 64 );
3137  memset( encbuf, 0, 64 );
3138  memset( decbuf, 0, 64 );
3139 
3140  /* Check and get info structures */
3142  fct_chk( NULL != cipher_info );
3143  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3144 
3145  /* Initialise enc and dec contexts */
3146  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3147  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3148 
3149  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3150  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3151 
3152  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3153  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3154 
3155  if( POLARSSL_MODE_CBC == cipher_info->mode )
3156  {
3157  enclen = cipher_get_block_size( &ctx_enc )
3158  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3159  }
3160  else
3161  {
3162  enclen = length;
3163  }
3164 
3165  /* encode length number of bytes from inbuf */
3166  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3167  if( POLARSSL_MODE_CBC == cipher_info->mode )
3168  {
3169  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3170  }
3171  else
3172  {
3173  fct_chk( outlen == enclen );
3174  }
3175 
3176  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3177  if( POLARSSL_MODE_CBC == cipher_info->mode )
3178  {
3179  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3180  }
3181  else
3182  {
3183  fct_chk( outlen == 0 );
3184  }
3185 
3186 
3187  /* decode the previously encoded string */
3188  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3189  if( POLARSSL_MODE_CBC == cipher_info->mode )
3190  {
3191  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3192  }
3193  else
3194  {
3195  fct_chk( enclen == outlen );
3196  }
3197 
3198  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3199  if( POLARSSL_MODE_CBC == cipher_info->mode )
3200  {
3201  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3202  }
3203  else
3204  {
3205  fct_chk( outlen == 0 );
3206  }
3207 
3208 
3209  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3210 
3211  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3212  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3213  FCT_TEST_END();
3214 #endif /* POLARSSL_DES_C */
3215 
3216 #ifdef POLARSSL_DES_C
3217 
3218  FCT_TEST_BGN(des3_encrypt_and_decrypt_2_bytes)
3219  size_t length = 2;
3220  unsigned char key[32];
3221  unsigned char iv[16];
3222 
3223  const cipher_info_t *cipher_info;
3224  cipher_context_t ctx_dec;
3225  cipher_context_t ctx_enc;
3226 
3227  unsigned char inbuf[64];
3228  unsigned char encbuf[64];
3229  unsigned char decbuf[64];
3230 
3231  size_t outlen = 0;
3232  size_t enclen = 0;
3233 
3234  memset( key, 0, 32 );
3235  memset( iv , 0, 16 );
3236 
3237  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3238  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3239 
3240  memset( inbuf, 5, 64 );
3241  memset( encbuf, 0, 64 );
3242  memset( decbuf, 0, 64 );
3243 
3244  /* Check and get info structures */
3246  fct_chk( NULL != cipher_info );
3247  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3248 
3249  /* Initialise enc and dec contexts */
3250  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3251  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3252 
3253  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3254  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3255 
3256  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3257  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3258 
3259  if( POLARSSL_MODE_CBC == cipher_info->mode )
3260  {
3261  enclen = cipher_get_block_size( &ctx_enc )
3262  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3263  }
3264  else
3265  {
3266  enclen = length;
3267  }
3268 
3269  /* encode length number of bytes from inbuf */
3270  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3271  if( POLARSSL_MODE_CBC == cipher_info->mode )
3272  {
3273  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3274  }
3275  else
3276  {
3277  fct_chk( outlen == enclen );
3278  }
3279 
3280  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3281  if( POLARSSL_MODE_CBC == cipher_info->mode )
3282  {
3283  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3284  }
3285  else
3286  {
3287  fct_chk( outlen == 0 );
3288  }
3289 
3290 
3291  /* decode the previously encoded string */
3292  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3293  if( POLARSSL_MODE_CBC == cipher_info->mode )
3294  {
3295  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3296  }
3297  else
3298  {
3299  fct_chk( enclen == outlen );
3300  }
3301 
3302  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3303  if( POLARSSL_MODE_CBC == cipher_info->mode )
3304  {
3305  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3306  }
3307  else
3308  {
3309  fct_chk( outlen == 0 );
3310  }
3311 
3312 
3313  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3314 
3315  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3316  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3317  FCT_TEST_END();
3318 #endif /* POLARSSL_DES_C */
3319 
3320 #ifdef POLARSSL_DES_C
3321 
3322  FCT_TEST_BGN(des3_encrypt_and_decrypt_7_bytes)
3323  size_t length = 7;
3324  unsigned char key[32];
3325  unsigned char iv[16];
3326 
3327  const cipher_info_t *cipher_info;
3328  cipher_context_t ctx_dec;
3329  cipher_context_t ctx_enc;
3330 
3331  unsigned char inbuf[64];
3332  unsigned char encbuf[64];
3333  unsigned char decbuf[64];
3334 
3335  size_t outlen = 0;
3336  size_t enclen = 0;
3337 
3338  memset( key, 0, 32 );
3339  memset( iv , 0, 16 );
3340 
3341  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3342  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3343 
3344  memset( inbuf, 5, 64 );
3345  memset( encbuf, 0, 64 );
3346  memset( decbuf, 0, 64 );
3347 
3348  /* Check and get info structures */
3350  fct_chk( NULL != cipher_info );
3351  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3352 
3353  /* Initialise enc and dec contexts */
3354  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3355  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3356 
3357  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3358  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3359 
3360  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3361  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3362 
3363  if( POLARSSL_MODE_CBC == cipher_info->mode )
3364  {
3365  enclen = cipher_get_block_size( &ctx_enc )
3366  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3367  }
3368  else
3369  {
3370  enclen = length;
3371  }
3372 
3373  /* encode length number of bytes from inbuf */
3374  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3375  if( POLARSSL_MODE_CBC == cipher_info->mode )
3376  {
3377  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3378  }
3379  else
3380  {
3381  fct_chk( outlen == enclen );
3382  }
3383 
3384  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3385  if( POLARSSL_MODE_CBC == cipher_info->mode )
3386  {
3387  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3388  }
3389  else
3390  {
3391  fct_chk( outlen == 0 );
3392  }
3393 
3394 
3395  /* decode the previously encoded string */
3396  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3397  if( POLARSSL_MODE_CBC == cipher_info->mode )
3398  {
3399  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3400  }
3401  else
3402  {
3403  fct_chk( enclen == outlen );
3404  }
3405 
3406  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3407  if( POLARSSL_MODE_CBC == cipher_info->mode )
3408  {
3409  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3410  }
3411  else
3412  {
3413  fct_chk( outlen == 0 );
3414  }
3415 
3416 
3417  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3418 
3419  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3420  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3421  FCT_TEST_END();
3422 #endif /* POLARSSL_DES_C */
3423 
3424 #ifdef POLARSSL_DES_C
3425 
3426  FCT_TEST_BGN(des3_encrypt_and_decrypt_8_bytes)
3427  size_t length = 8;
3428  unsigned char key[32];
3429  unsigned char iv[16];
3430 
3431  const cipher_info_t *cipher_info;
3432  cipher_context_t ctx_dec;
3433  cipher_context_t ctx_enc;
3434 
3435  unsigned char inbuf[64];
3436  unsigned char encbuf[64];
3437  unsigned char decbuf[64];
3438 
3439  size_t outlen = 0;
3440  size_t enclen = 0;
3441 
3442  memset( key, 0, 32 );
3443  memset( iv , 0, 16 );
3444 
3445  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3446  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3447 
3448  memset( inbuf, 5, 64 );
3449  memset( encbuf, 0, 64 );
3450  memset( decbuf, 0, 64 );
3451 
3452  /* Check and get info structures */
3454  fct_chk( NULL != cipher_info );
3455  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3456 
3457  /* Initialise enc and dec contexts */
3458  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3459  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3460 
3461  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3462  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3463 
3464  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3465  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3466 
3467  if( POLARSSL_MODE_CBC == cipher_info->mode )
3468  {
3469  enclen = cipher_get_block_size( &ctx_enc )
3470  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3471  }
3472  else
3473  {
3474  enclen = length;
3475  }
3476 
3477  /* encode length number of bytes from inbuf */
3478  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3479  if( POLARSSL_MODE_CBC == cipher_info->mode )
3480  {
3481  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3482  }
3483  else
3484  {
3485  fct_chk( outlen == enclen );
3486  }
3487 
3488  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3489  if( POLARSSL_MODE_CBC == cipher_info->mode )
3490  {
3491  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3492  }
3493  else
3494  {
3495  fct_chk( outlen == 0 );
3496  }
3497 
3498 
3499  /* decode the previously encoded string */
3500  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3501  if( POLARSSL_MODE_CBC == cipher_info->mode )
3502  {
3503  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3504  }
3505  else
3506  {
3507  fct_chk( enclen == outlen );
3508  }
3509 
3510  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3511  if( POLARSSL_MODE_CBC == cipher_info->mode )
3512  {
3513  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3514  }
3515  else
3516  {
3517  fct_chk( outlen == 0 );
3518  }
3519 
3520 
3521  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3522 
3523  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3524  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3525  FCT_TEST_END();
3526 #endif /* POLARSSL_DES_C */
3527 
3528 #ifdef POLARSSL_DES_C
3529 
3530  FCT_TEST_BGN(des3_encrypt_and_decrypt_9_bytes)
3531  size_t length = 9;
3532  unsigned char key[32];
3533  unsigned char iv[16];
3534 
3535  const cipher_info_t *cipher_info;
3536  cipher_context_t ctx_dec;
3537  cipher_context_t ctx_enc;
3538 
3539  unsigned char inbuf[64];
3540  unsigned char encbuf[64];
3541  unsigned char decbuf[64];
3542 
3543  size_t outlen = 0;
3544  size_t enclen = 0;
3545 
3546  memset( key, 0, 32 );
3547  memset( iv , 0, 16 );
3548 
3549  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3550  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3551 
3552  memset( inbuf, 5, 64 );
3553  memset( encbuf, 0, 64 );
3554  memset( decbuf, 0, 64 );
3555 
3556  /* Check and get info structures */
3558  fct_chk( NULL != cipher_info );
3559  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3560 
3561  /* Initialise enc and dec contexts */
3562  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3563  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3564 
3565  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3566  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3567 
3568  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3569  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3570 
3571  if( POLARSSL_MODE_CBC == cipher_info->mode )
3572  {
3573  enclen = cipher_get_block_size( &ctx_enc )
3574  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3575  }
3576  else
3577  {
3578  enclen = length;
3579  }
3580 
3581  /* encode length number of bytes from inbuf */
3582  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3583  if( POLARSSL_MODE_CBC == cipher_info->mode )
3584  {
3585  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3586  }
3587  else
3588  {
3589  fct_chk( outlen == enclen );
3590  }
3591 
3592  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3593  if( POLARSSL_MODE_CBC == cipher_info->mode )
3594  {
3595  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3596  }
3597  else
3598  {
3599  fct_chk( outlen == 0 );
3600  }
3601 
3602 
3603  /* decode the previously encoded string */
3604  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3605  if( POLARSSL_MODE_CBC == cipher_info->mode )
3606  {
3607  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3608  }
3609  else
3610  {
3611  fct_chk( enclen == outlen );
3612  }
3613 
3614  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3615  if( POLARSSL_MODE_CBC == cipher_info->mode )
3616  {
3617  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3618  }
3619  else
3620  {
3621  fct_chk( outlen == 0 );
3622  }
3623 
3624 
3625  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3626 
3627  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3628  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3629  FCT_TEST_END();
3630 #endif /* POLARSSL_DES_C */
3631 
3632 #ifdef POLARSSL_DES_C
3633 
3634  FCT_TEST_BGN(des3_encrypt_and_decrypt_15_bytes)
3635  size_t length = 15;
3636  unsigned char key[32];
3637  unsigned char iv[16];
3638 
3639  const cipher_info_t *cipher_info;
3640  cipher_context_t ctx_dec;
3641  cipher_context_t ctx_enc;
3642 
3643  unsigned char inbuf[64];
3644  unsigned char encbuf[64];
3645  unsigned char decbuf[64];
3646 
3647  size_t outlen = 0;
3648  size_t enclen = 0;
3649 
3650  memset( key, 0, 32 );
3651  memset( iv , 0, 16 );
3652 
3653  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3654  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3655 
3656  memset( inbuf, 5, 64 );
3657  memset( encbuf, 0, 64 );
3658  memset( decbuf, 0, 64 );
3659 
3660  /* Check and get info structures */
3662  fct_chk( NULL != cipher_info );
3663  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3664 
3665  /* Initialise enc and dec contexts */
3666  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3667  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3668 
3669  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3670  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3671 
3672  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3673  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3674 
3675  if( POLARSSL_MODE_CBC == cipher_info->mode )
3676  {
3677  enclen = cipher_get_block_size( &ctx_enc )
3678  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3679  }
3680  else
3681  {
3682  enclen = length;
3683  }
3684 
3685  /* encode length number of bytes from inbuf */
3686  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3687  if( POLARSSL_MODE_CBC == cipher_info->mode )
3688  {
3689  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3690  }
3691  else
3692  {
3693  fct_chk( outlen == enclen );
3694  }
3695 
3696  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3697  if( POLARSSL_MODE_CBC == cipher_info->mode )
3698  {
3699  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3700  }
3701  else
3702  {
3703  fct_chk( outlen == 0 );
3704  }
3705 
3706 
3707  /* decode the previously encoded string */
3708  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3709  if( POLARSSL_MODE_CBC == cipher_info->mode )
3710  {
3711  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3712  }
3713  else
3714  {
3715  fct_chk( enclen == outlen );
3716  }
3717 
3718  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3719  if( POLARSSL_MODE_CBC == cipher_info->mode )
3720  {
3721  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3722  }
3723  else
3724  {
3725  fct_chk( outlen == 0 );
3726  }
3727 
3728 
3729  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3730 
3731  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3732  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3733  FCT_TEST_END();
3734 #endif /* POLARSSL_DES_C */
3735 
3736 #ifdef POLARSSL_DES_C
3737 
3738  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes)
3739  size_t length = 16;
3740  unsigned char key[32];
3741  unsigned char iv[16];
3742 
3743  const cipher_info_t *cipher_info;
3744  cipher_context_t ctx_dec;
3745  cipher_context_t ctx_enc;
3746 
3747  unsigned char inbuf[64];
3748  unsigned char encbuf[64];
3749  unsigned char decbuf[64];
3750 
3751  size_t outlen = 0;
3752  size_t enclen = 0;
3753 
3754  memset( key, 0, 32 );
3755  memset( iv , 0, 16 );
3756 
3757  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3758  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3759 
3760  memset( inbuf, 5, 64 );
3761  memset( encbuf, 0, 64 );
3762  memset( decbuf, 0, 64 );
3763 
3764  /* Check and get info structures */
3766  fct_chk( NULL != cipher_info );
3767  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3768 
3769  /* Initialise enc and dec contexts */
3770  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3771  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3772 
3773  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3774  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3775 
3776  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3777  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3778 
3779  if( POLARSSL_MODE_CBC == cipher_info->mode )
3780  {
3781  enclen = cipher_get_block_size( &ctx_enc )
3782  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3783  }
3784  else
3785  {
3786  enclen = length;
3787  }
3788 
3789  /* encode length number of bytes from inbuf */
3790  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3791  if( POLARSSL_MODE_CBC == cipher_info->mode )
3792  {
3793  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3794  }
3795  else
3796  {
3797  fct_chk( outlen == enclen );
3798  }
3799 
3800  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3801  if( POLARSSL_MODE_CBC == cipher_info->mode )
3802  {
3803  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3804  }
3805  else
3806  {
3807  fct_chk( outlen == 0 );
3808  }
3809 
3810 
3811  /* decode the previously encoded string */
3812  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3813  if( POLARSSL_MODE_CBC == cipher_info->mode )
3814  {
3815  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3816  }
3817  else
3818  {
3819  fct_chk( enclen == outlen );
3820  }
3821 
3822  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3823  if( POLARSSL_MODE_CBC == cipher_info->mode )
3824  {
3825  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3826  }
3827  else
3828  {
3829  fct_chk( outlen == 0 );
3830  }
3831 
3832 
3833  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3834 
3835  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3836  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3837  FCT_TEST_END();
3838 #endif /* POLARSSL_DES_C */
3839 
3840 #ifdef POLARSSL_DES_C
3841 
3842  FCT_TEST_BGN(des3_encrypt_and_decrypt_17_bytes)
3843  size_t length = 17;
3844  unsigned char key[32];
3845  unsigned char iv[16];
3846 
3847  const cipher_info_t *cipher_info;
3848  cipher_context_t ctx_dec;
3849  cipher_context_t ctx_enc;
3850 
3851  unsigned char inbuf[64];
3852  unsigned char encbuf[64];
3853  unsigned char decbuf[64];
3854 
3855  size_t outlen = 0;
3856  size_t enclen = 0;
3857 
3858  memset( key, 0, 32 );
3859  memset( iv , 0, 16 );
3860 
3861  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3862  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3863 
3864  memset( inbuf, 5, 64 );
3865  memset( encbuf, 0, 64 );
3866  memset( decbuf, 0, 64 );
3867 
3868  /* Check and get info structures */
3870  fct_chk( NULL != cipher_info );
3871  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3872 
3873  /* Initialise enc and dec contexts */
3874  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3875  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3876 
3877  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3878  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3879 
3880  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3881  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3882 
3883  if( POLARSSL_MODE_CBC == cipher_info->mode )
3884  {
3885  enclen = cipher_get_block_size( &ctx_enc )
3886  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3887  }
3888  else
3889  {
3890  enclen = length;
3891  }
3892 
3893  /* encode length number of bytes from inbuf */
3894  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3895  if( POLARSSL_MODE_CBC == cipher_info->mode )
3896  {
3897  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3898  }
3899  else
3900  {
3901  fct_chk( outlen == enclen );
3902  }
3903 
3904  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3905  if( POLARSSL_MODE_CBC == cipher_info->mode )
3906  {
3907  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3908  }
3909  else
3910  {
3911  fct_chk( outlen == 0 );
3912  }
3913 
3914 
3915  /* decode the previously encoded string */
3916  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3917  if( POLARSSL_MODE_CBC == cipher_info->mode )
3918  {
3919  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3920  }
3921  else
3922  {
3923  fct_chk( enclen == outlen );
3924  }
3925 
3926  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3927  if( POLARSSL_MODE_CBC == cipher_info->mode )
3928  {
3929  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3930  }
3931  else
3932  {
3933  fct_chk( outlen == 0 );
3934  }
3935 
3936 
3937  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3938 
3939  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3940  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3941  FCT_TEST_END();
3942 #endif /* POLARSSL_DES_C */
3943 
3944 #ifdef POLARSSL_DES_C
3945 
3946  FCT_TEST_BGN(des3_encrypt_and_decrypt_31_bytes)
3947  size_t length = 31;
3948  unsigned char key[32];
3949  unsigned char iv[16];
3950 
3951  const cipher_info_t *cipher_info;
3952  cipher_context_t ctx_dec;
3953  cipher_context_t ctx_enc;
3954 
3955  unsigned char inbuf[64];
3956  unsigned char encbuf[64];
3957  unsigned char decbuf[64];
3958 
3959  size_t outlen = 0;
3960  size_t enclen = 0;
3961 
3962  memset( key, 0, 32 );
3963  memset( iv , 0, 16 );
3964 
3965  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3966  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3967 
3968  memset( inbuf, 5, 64 );
3969  memset( encbuf, 0, 64 );
3970  memset( decbuf, 0, 64 );
3971 
3972  /* Check and get info structures */
3974  fct_chk( NULL != cipher_info );
3975  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3976 
3977  /* Initialise enc and dec contexts */
3978  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3979  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3980 
3981  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3982  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3983 
3984  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3985  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3986 
3987  if( POLARSSL_MODE_CBC == cipher_info->mode )
3988  {
3989  enclen = cipher_get_block_size( &ctx_enc )
3990  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3991  }
3992  else
3993  {
3994  enclen = length;
3995  }
3996 
3997  /* encode length number of bytes from inbuf */
3998  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3999  if( POLARSSL_MODE_CBC == cipher_info->mode )
4000  {
4001  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4002  }
4003  else
4004  {
4005  fct_chk( outlen == enclen );
4006  }
4007 
4008  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4009  if( POLARSSL_MODE_CBC == cipher_info->mode )
4010  {
4011  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4012  }
4013  else
4014  {
4015  fct_chk( outlen == 0 );
4016  }
4017 
4018 
4019  /* decode the previously encoded string */
4020  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4021  if( POLARSSL_MODE_CBC == cipher_info->mode )
4022  {
4023  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4024  }
4025  else
4026  {
4027  fct_chk( enclen == outlen );
4028  }
4029 
4030  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4031  if( POLARSSL_MODE_CBC == cipher_info->mode )
4032  {
4033  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4034  }
4035  else
4036  {
4037  fct_chk( outlen == 0 );
4038  }
4039 
4040 
4041  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4042 
4043  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4044  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4045  FCT_TEST_END();
4046 #endif /* POLARSSL_DES_C */
4047 
4048 #ifdef POLARSSL_DES_C
4049 
4050  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes)
4051  size_t length = 32;
4052  unsigned char key[32];
4053  unsigned char iv[16];
4054 
4055  const cipher_info_t *cipher_info;
4056  cipher_context_t ctx_dec;
4057  cipher_context_t ctx_enc;
4058 
4059  unsigned char inbuf[64];
4060  unsigned char encbuf[64];
4061  unsigned char decbuf[64];
4062 
4063  size_t outlen = 0;
4064  size_t enclen = 0;
4065 
4066  memset( key, 0, 32 );
4067  memset( iv , 0, 16 );
4068 
4069  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4070  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4071 
4072  memset( inbuf, 5, 64 );
4073  memset( encbuf, 0, 64 );
4074  memset( decbuf, 0, 64 );
4075 
4076  /* Check and get info structures */
4078  fct_chk( NULL != cipher_info );
4079  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
4080 
4081  /* Initialise enc and dec contexts */
4082  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4083  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4084 
4085  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4086  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4087 
4088  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4089  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4090 
4091  if( POLARSSL_MODE_CBC == cipher_info->mode )
4092  {
4093  enclen = cipher_get_block_size( &ctx_enc )
4094  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4095  }
4096  else
4097  {
4098  enclen = length;
4099  }
4100 
4101  /* encode length number of bytes from inbuf */
4102  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4103  if( POLARSSL_MODE_CBC == cipher_info->mode )
4104  {
4105  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4106  }
4107  else
4108  {
4109  fct_chk( outlen == enclen );
4110  }
4111 
4112  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4113  if( POLARSSL_MODE_CBC == cipher_info->mode )
4114  {
4115  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4116  }
4117  else
4118  {
4119  fct_chk( outlen == 0 );
4120  }
4121 
4122 
4123  /* decode the previously encoded string */
4124  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4125  if( POLARSSL_MODE_CBC == cipher_info->mode )
4126  {
4127  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4128  }
4129  else
4130  {
4131  fct_chk( enclen == outlen );
4132  }
4133 
4134  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4135  if( POLARSSL_MODE_CBC == cipher_info->mode )
4136  {
4137  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4138  }
4139  else
4140  {
4141  fct_chk( outlen == 0 );
4142  }
4143 
4144 
4145  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4146 
4147  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4148  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4149  FCT_TEST_END();
4150 #endif /* POLARSSL_DES_C */
4151 
4152 #ifdef POLARSSL_DES_C
4153 
4154  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes)
4155  size_t length = 33;
4156  unsigned char key[32];
4157  unsigned char iv[16];
4158 
4159  const cipher_info_t *cipher_info;
4160  cipher_context_t ctx_dec;
4161  cipher_context_t ctx_enc;
4162 
4163  unsigned char inbuf[64];
4164  unsigned char encbuf[64];
4165  unsigned char decbuf[64];
4166 
4167  size_t outlen = 0;
4168  size_t enclen = 0;
4169 
4170  memset( key, 0, 32 );
4171  memset( iv , 0, 16 );
4172 
4173  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4174  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4175 
4176  memset( inbuf, 5, 64 );
4177  memset( encbuf, 0, 64 );
4178  memset( decbuf, 0, 64 );
4179 
4180  /* Check and get info structures */
4182  fct_chk( NULL != cipher_info );
4183  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
4184 
4185  /* Initialise enc and dec contexts */
4186  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4187  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4188 
4189  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4190  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4191 
4192  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4193  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4194 
4195  if( POLARSSL_MODE_CBC == cipher_info->mode )
4196  {
4197  enclen = cipher_get_block_size( &ctx_enc )
4198  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4199  }
4200  else
4201  {
4202  enclen = length;
4203  }
4204 
4205  /* encode length number of bytes from inbuf */
4206  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4207  if( POLARSSL_MODE_CBC == cipher_info->mode )
4208  {
4209  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4210  }
4211  else
4212  {
4213  fct_chk( outlen == enclen );
4214  }
4215 
4216  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4217  if( POLARSSL_MODE_CBC == cipher_info->mode )
4218  {
4219  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4220  }
4221  else
4222  {
4223  fct_chk( outlen == 0 );
4224  }
4225 
4226 
4227  /* decode the previously encoded string */
4228  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4229  if( POLARSSL_MODE_CBC == cipher_info->mode )
4230  {
4231  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4232  }
4233  else
4234  {
4235  fct_chk( enclen == outlen );
4236  }
4237 
4238  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4239  if( POLARSSL_MODE_CBC == cipher_info->mode )
4240  {
4241  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4242  }
4243  else
4244  {
4245  fct_chk( outlen == 0 );
4246  }
4247 
4248 
4249  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4250 
4251  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4252  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4253  FCT_TEST_END();
4254 #endif /* POLARSSL_DES_C */
4255 
4256 #ifdef POLARSSL_DES_C
4257 
4258  FCT_TEST_BGN(des3_encrypt_and_decrypt_47_bytes)
4259  size_t length = 47;
4260  unsigned char key[32];
4261  unsigned char iv[16];
4262 
4263  const cipher_info_t *cipher_info;
4264  cipher_context_t ctx_dec;
4265  cipher_context_t ctx_enc;
4266 
4267  unsigned char inbuf[64];
4268  unsigned char encbuf[64];
4269  unsigned char decbuf[64];
4270 
4271  size_t outlen = 0;
4272  size_t enclen = 0;
4273 
4274  memset( key, 0, 32 );
4275  memset( iv , 0, 16 );
4276 
4277  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4278  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4279 
4280  memset( inbuf, 5, 64 );
4281  memset( encbuf, 0, 64 );
4282  memset( decbuf, 0, 64 );
4283 
4284  /* Check and get info structures */
4286  fct_chk( NULL != cipher_info );
4287  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
4288 
4289  /* Initialise enc and dec contexts */
4290  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4291  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4292 
4293  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4294  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4295 
4296  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4297  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4298 
4299  if( POLARSSL_MODE_CBC == cipher_info->mode )
4300  {
4301  enclen = cipher_get_block_size( &ctx_enc )
4302  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4303  }
4304  else
4305  {
4306  enclen = length;
4307  }
4308 
4309  /* encode length number of bytes from inbuf */
4310  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4311  if( POLARSSL_MODE_CBC == cipher_info->mode )
4312  {
4313  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4314  }
4315  else
4316  {
4317  fct_chk( outlen == enclen );
4318  }
4319 
4320  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4321  if( POLARSSL_MODE_CBC == cipher_info->mode )
4322  {
4323  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4324  }
4325  else
4326  {
4327  fct_chk( outlen == 0 );
4328  }
4329 
4330 
4331  /* decode the previously encoded string */
4332  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4333  if( POLARSSL_MODE_CBC == cipher_info->mode )
4334  {
4335  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4336  }
4337  else
4338  {
4339  fct_chk( enclen == outlen );
4340  }
4341 
4342  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4343  if( POLARSSL_MODE_CBC == cipher_info->mode )
4344  {
4345  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4346  }
4347  else
4348  {
4349  fct_chk( outlen == 0 );
4350  }
4351 
4352 
4353  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4354 
4355  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4356  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4357  FCT_TEST_END();
4358 #endif /* POLARSSL_DES_C */
4359 
4360 #ifdef POLARSSL_DES_C
4361 
4362  FCT_TEST_BGN(des3_encrypt_and_decrypt_48_bytes)
4363  size_t length = 48;
4364  unsigned char key[32];
4365  unsigned char iv[16];
4366 
4367  const cipher_info_t *cipher_info;
4368  cipher_context_t ctx_dec;
4369  cipher_context_t ctx_enc;
4370 
4371  unsigned char inbuf[64];
4372  unsigned char encbuf[64];
4373  unsigned char decbuf[64];
4374 
4375  size_t outlen = 0;
4376  size_t enclen = 0;
4377 
4378  memset( key, 0, 32 );
4379  memset( iv , 0, 16 );
4380 
4381  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4382  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4383 
4384  memset( inbuf, 5, 64 );
4385  memset( encbuf, 0, 64 );
4386  memset( decbuf, 0, 64 );
4387 
4388  /* Check and get info structures */
4390  fct_chk( NULL != cipher_info );
4391  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
4392 
4393  /* Initialise enc and dec contexts */
4394  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4395  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4396 
4397  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4398  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4399 
4400  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4401  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4402 
4403  if( POLARSSL_MODE_CBC == cipher_info->mode )
4404  {
4405  enclen = cipher_get_block_size( &ctx_enc )
4406  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4407  }
4408  else
4409  {
4410  enclen = length;
4411  }
4412 
4413  /* encode length number of bytes from inbuf */
4414  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4415  if( POLARSSL_MODE_CBC == cipher_info->mode )
4416  {
4417  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4418  }
4419  else
4420  {
4421  fct_chk( outlen == enclen );
4422  }
4423 
4424  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4425  if( POLARSSL_MODE_CBC == cipher_info->mode )
4426  {
4427  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4428  }
4429  else
4430  {
4431  fct_chk( outlen == 0 );
4432  }
4433 
4434 
4435  /* decode the previously encoded string */
4436  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4437  if( POLARSSL_MODE_CBC == cipher_info->mode )
4438  {
4439  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4440  }
4441  else
4442  {
4443  fct_chk( enclen == outlen );
4444  }
4445 
4446  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4447  if( POLARSSL_MODE_CBC == cipher_info->mode )
4448  {
4449  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4450  }
4451  else
4452  {
4453  fct_chk( outlen == 0 );
4454  }
4455 
4456 
4457  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4458 
4459  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4460  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4461  FCT_TEST_END();
4462 #endif /* POLARSSL_DES_C */
4463 
4464 #ifdef POLARSSL_DES_C
4465 
4466  FCT_TEST_BGN(des3_encrypt_and_decrypt_49_bytes)
4467  size_t length = 49;
4468  unsigned char key[32];
4469  unsigned char iv[16];
4470 
4471  const cipher_info_t *cipher_info;
4472  cipher_context_t ctx_dec;
4473  cipher_context_t ctx_enc;
4474 
4475  unsigned char inbuf[64];
4476  unsigned char encbuf[64];
4477  unsigned char decbuf[64];
4478 
4479  size_t outlen = 0;
4480  size_t enclen = 0;
4481 
4482  memset( key, 0, 32 );
4483  memset( iv , 0, 16 );
4484 
4485  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4486  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4487 
4488  memset( inbuf, 5, 64 );
4489  memset( encbuf, 0, 64 );
4490  memset( decbuf, 0, 64 );
4491 
4492  /* Check and get info structures */
4494  fct_chk( NULL != cipher_info );
4495  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
4496 
4497  /* Initialise enc and dec contexts */
4498  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4499  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4500 
4501  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4502  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4503 
4504  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4505  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4506 
4507  if( POLARSSL_MODE_CBC == cipher_info->mode )
4508  {
4509  enclen = cipher_get_block_size( &ctx_enc )
4510  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4511  }
4512  else
4513  {
4514  enclen = length;
4515  }
4516 
4517  /* encode length number of bytes from inbuf */
4518  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4519  if( POLARSSL_MODE_CBC == cipher_info->mode )
4520  {
4521  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4522  }
4523  else
4524  {
4525  fct_chk( outlen == enclen );
4526  }
4527 
4528  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4529  if( POLARSSL_MODE_CBC == cipher_info->mode )
4530  {
4531  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4532  }
4533  else
4534  {
4535  fct_chk( outlen == 0 );
4536  }
4537 
4538 
4539  /* decode the previously encoded string */
4540  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4541  if( POLARSSL_MODE_CBC == cipher_info->mode )
4542  {
4543  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4544  }
4545  else
4546  {
4547  fct_chk( enclen == outlen );
4548  }
4549 
4550  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4551  if( POLARSSL_MODE_CBC == cipher_info->mode )
4552  {
4553  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4554  }
4555  else
4556  {
4557  fct_chk( outlen == 0 );
4558  }
4559 
4560 
4561  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4562 
4563  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4564  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4565  FCT_TEST_END();
4566 #endif /* POLARSSL_DES_C */
4567 
4568 #ifdef POLARSSL_DES_C
4569 
4570  FCT_TEST_BGN(des3_encrypt_and_decrypt_0_bytes_in_multiple_parts)
4571  size_t first_length = 0;
4572  size_t second_length = 0;
4573  size_t length = first_length + second_length;
4574  unsigned char key[32];
4575  unsigned char iv[16];
4576 
4577  cipher_context_t ctx_dec;
4578  cipher_context_t ctx_enc;
4579  const cipher_info_t *cipher_info;
4580 
4581  unsigned char inbuf[64];
4582  unsigned char encbuf[64];
4583  unsigned char decbuf[64];
4584 
4585  size_t outlen = 0;
4586  size_t totaloutlen = 0;
4587  size_t enclen = 0;
4588 
4589  memset( key, 0, 32 );
4590  memset( iv , 0, 16 );
4591 
4592  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4593  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4594 
4595  memset( inbuf, 5, 64 );
4596  memset( encbuf, 0, 64 );
4597  memset( decbuf, 0, 64 );
4598 
4599  /* Initialise enc and dec contexts */
4601  fct_chk( NULL != cipher_info);
4602 
4603  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4604  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4605 
4606  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4607  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4608 
4609  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4610  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4611 
4612  if( POLARSSL_MODE_CBC == cipher_info->mode )
4613  {
4614  enclen = cipher_get_block_size(&ctx_enc )
4615  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4616  }
4617  else
4618  {
4619  enclen = length;
4620  }
4621 
4622  /* encode length number of bytes from inbuf */
4623  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4624  totaloutlen = outlen;
4625  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4626  totaloutlen += outlen;
4627  if( POLARSSL_MODE_CBC == cipher_info->mode )
4628  {
4629  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4630  }
4631  else
4632  {
4633  fct_chk( totaloutlen == enclen );
4634  }
4635  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4636  totaloutlen += outlen;
4637  if( POLARSSL_MODE_CBC == cipher_info->mode )
4638  {
4639  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4640  }
4641  else
4642  {
4643  fct_chk( outlen == 0 );
4644  }
4645 
4646  /* decode the previously encoded string */
4647  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4648  if( POLARSSL_MODE_CBC == cipher_info->mode )
4649  {
4650  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4651  }
4652  else
4653  {
4654  fct_chk( enclen == outlen );
4655  }
4656  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4657  if( POLARSSL_MODE_CBC == cipher_info->mode )
4658  {
4659  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4660  }
4661  else
4662  {
4663  fct_chk( outlen == 0 );
4664  }
4665 
4666 
4667  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4668 
4669  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4670  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4671  FCT_TEST_END();
4672 #endif /* POLARSSL_DES_C */
4673 
4674 #ifdef POLARSSL_DES_C
4675 
4676  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
4677  size_t first_length = 1;
4678  size_t second_length = 0;
4679  size_t length = first_length + second_length;
4680  unsigned char key[32];
4681  unsigned char iv[16];
4682 
4683  cipher_context_t ctx_dec;
4684  cipher_context_t ctx_enc;
4685  const cipher_info_t *cipher_info;
4686 
4687  unsigned char inbuf[64];
4688  unsigned char encbuf[64];
4689  unsigned char decbuf[64];
4690 
4691  size_t outlen = 0;
4692  size_t totaloutlen = 0;
4693  size_t enclen = 0;
4694 
4695  memset( key, 0, 32 );
4696  memset( iv , 0, 16 );
4697 
4698  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4699  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4700 
4701  memset( inbuf, 5, 64 );
4702  memset( encbuf, 0, 64 );
4703  memset( decbuf, 0, 64 );
4704 
4705  /* Initialise enc and dec contexts */
4707  fct_chk( NULL != cipher_info);
4708 
4709  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4710  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4711 
4712  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4713  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4714 
4715  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4716  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4717 
4718  if( POLARSSL_MODE_CBC == cipher_info->mode )
4719  {
4720  enclen = cipher_get_block_size(&ctx_enc )
4721  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4722  }
4723  else
4724  {
4725  enclen = length;
4726  }
4727 
4728  /* encode length number of bytes from inbuf */
4729  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4730  totaloutlen = outlen;
4731  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4732  totaloutlen += outlen;
4733  if( POLARSSL_MODE_CBC == cipher_info->mode )
4734  {
4735  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4736  }
4737  else
4738  {
4739  fct_chk( totaloutlen == enclen );
4740  }
4741  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4742  totaloutlen += outlen;
4743  if( POLARSSL_MODE_CBC == cipher_info->mode )
4744  {
4745  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4746  }
4747  else
4748  {
4749  fct_chk( outlen == 0 );
4750  }
4751 
4752  /* decode the previously encoded string */
4753  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4754  if( POLARSSL_MODE_CBC == cipher_info->mode )
4755  {
4756  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4757  }
4758  else
4759  {
4760  fct_chk( enclen == outlen );
4761  }
4762  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4763  if( POLARSSL_MODE_CBC == cipher_info->mode )
4764  {
4765  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4766  }
4767  else
4768  {
4769  fct_chk( outlen == 0 );
4770  }
4771 
4772 
4773  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4774 
4775  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4776  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4777  FCT_TEST_END();
4778 #endif /* POLARSSL_DES_C */
4779 
4780 #ifdef POLARSSL_DES_C
4781 
4782  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
4783  size_t first_length = 0;
4784  size_t second_length = 1;
4785  size_t length = first_length + second_length;
4786  unsigned char key[32];
4787  unsigned char iv[16];
4788 
4789  cipher_context_t ctx_dec;
4790  cipher_context_t ctx_enc;
4791  const cipher_info_t *cipher_info;
4792 
4793  unsigned char inbuf[64];
4794  unsigned char encbuf[64];
4795  unsigned char decbuf[64];
4796 
4797  size_t outlen = 0;
4798  size_t totaloutlen = 0;
4799  size_t enclen = 0;
4800 
4801  memset( key, 0, 32 );
4802  memset( iv , 0, 16 );
4803 
4804  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4805  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4806 
4807  memset( inbuf, 5, 64 );
4808  memset( encbuf, 0, 64 );
4809  memset( decbuf, 0, 64 );
4810 
4811  /* Initialise enc and dec contexts */
4813  fct_chk( NULL != cipher_info);
4814 
4815  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4816  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4817 
4818  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4819  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4820 
4821  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4822  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4823 
4824  if( POLARSSL_MODE_CBC == cipher_info->mode )
4825  {
4826  enclen = cipher_get_block_size(&ctx_enc )
4827  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4828  }
4829  else
4830  {
4831  enclen = length;
4832  }
4833 
4834  /* encode length number of bytes from inbuf */
4835  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4836  totaloutlen = outlen;
4837  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4838  totaloutlen += outlen;
4839  if( POLARSSL_MODE_CBC == cipher_info->mode )
4840  {
4841  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4842  }
4843  else
4844  {
4845  fct_chk( totaloutlen == enclen );
4846  }
4847  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4848  totaloutlen += outlen;
4849  if( POLARSSL_MODE_CBC == cipher_info->mode )
4850  {
4851  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4852  }
4853  else
4854  {
4855  fct_chk( outlen == 0 );
4856  }
4857 
4858  /* decode the previously encoded string */
4859  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4860  if( POLARSSL_MODE_CBC == cipher_info->mode )
4861  {
4862  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4863  }
4864  else
4865  {
4866  fct_chk( enclen == outlen );
4867  }
4868  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4869  if( POLARSSL_MODE_CBC == cipher_info->mode )
4870  {
4871  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4872  }
4873  else
4874  {
4875  fct_chk( outlen == 0 );
4876  }
4877 
4878 
4879  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4880 
4881  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4882  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4883  FCT_TEST_END();
4884 #endif /* POLARSSL_DES_C */
4885 
4886 #ifdef POLARSSL_DES_C
4887 
4888  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
4889  size_t first_length = 16;
4890  size_t second_length = 0;
4891  size_t length = first_length + second_length;
4892  unsigned char key[32];
4893  unsigned char iv[16];
4894 
4895  cipher_context_t ctx_dec;
4896  cipher_context_t ctx_enc;
4897  const cipher_info_t *cipher_info;
4898 
4899  unsigned char inbuf[64];
4900  unsigned char encbuf[64];
4901  unsigned char decbuf[64];
4902 
4903  size_t outlen = 0;
4904  size_t totaloutlen = 0;
4905  size_t enclen = 0;
4906 
4907  memset( key, 0, 32 );
4908  memset( iv , 0, 16 );
4909 
4910  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4911  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4912 
4913  memset( inbuf, 5, 64 );
4914  memset( encbuf, 0, 64 );
4915  memset( decbuf, 0, 64 );
4916 
4917  /* Initialise enc and dec contexts */
4919  fct_chk( NULL != cipher_info);
4920 
4921  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4922  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4923 
4924  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4925  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4926 
4927  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4928  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4929 
4930  if( POLARSSL_MODE_CBC == cipher_info->mode )
4931  {
4932  enclen = cipher_get_block_size(&ctx_enc )
4933  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4934  }
4935  else
4936  {
4937  enclen = length;
4938  }
4939 
4940  /* encode length number of bytes from inbuf */
4941  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4942  totaloutlen = outlen;
4943  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4944  totaloutlen += outlen;
4945  if( POLARSSL_MODE_CBC == cipher_info->mode )
4946  {
4947  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4948  }
4949  else
4950  {
4951  fct_chk( totaloutlen == enclen );
4952  }
4953  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4954  totaloutlen += outlen;
4955  if( POLARSSL_MODE_CBC == cipher_info->mode )
4956  {
4957  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4958  }
4959  else
4960  {
4961  fct_chk( outlen == 0 );
4962  }
4963 
4964  /* decode the previously encoded string */
4965  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4966  if( POLARSSL_MODE_CBC == cipher_info->mode )
4967  {
4968  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4969  }
4970  else
4971  {
4972  fct_chk( enclen == outlen );
4973  }
4974  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4975  if( POLARSSL_MODE_CBC == cipher_info->mode )
4976  {
4977  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4978  }
4979  else
4980  {
4981  fct_chk( outlen == 0 );
4982  }
4983 
4984 
4985  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4986 
4987  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4988  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4989  FCT_TEST_END();
4990 #endif /* POLARSSL_DES_C */
4991 
4992 #ifdef POLARSSL_DES_C
4993 
4994  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
4995  size_t first_length = 0;
4996  size_t second_length = 16;
4997  size_t length = first_length + second_length;
4998  unsigned char key[32];
4999  unsigned char iv[16];
5000 
5001  cipher_context_t ctx_dec;
5002  cipher_context_t ctx_enc;
5003  const cipher_info_t *cipher_info;
5004 
5005  unsigned char inbuf[64];
5006  unsigned char encbuf[64];
5007  unsigned char decbuf[64];
5008 
5009  size_t outlen = 0;
5010  size_t totaloutlen = 0;
5011  size_t enclen = 0;
5012 
5013  memset( key, 0, 32 );
5014  memset( iv , 0, 16 );
5015 
5016  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5017  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5018 
5019  memset( inbuf, 5, 64 );
5020  memset( encbuf, 0, 64 );
5021  memset( decbuf, 0, 64 );
5022 
5023  /* Initialise enc and dec contexts */
5025  fct_chk( NULL != cipher_info);
5026 
5027  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5028  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5029 
5030  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5031  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5032 
5033  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5034  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5035 
5036  if( POLARSSL_MODE_CBC == cipher_info->mode )
5037  {
5038  enclen = cipher_get_block_size(&ctx_enc )
5039  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5040  }
5041  else
5042  {
5043  enclen = length;
5044  }
5045 
5046  /* encode length number of bytes from inbuf */
5047  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5048  totaloutlen = outlen;
5049  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5050  totaloutlen += outlen;
5051  if( POLARSSL_MODE_CBC == cipher_info->mode )
5052  {
5053  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5054  }
5055  else
5056  {
5057  fct_chk( totaloutlen == enclen );
5058  }
5059  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5060  totaloutlen += outlen;
5061  if( POLARSSL_MODE_CBC == cipher_info->mode )
5062  {
5063  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5064  }
5065  else
5066  {
5067  fct_chk( outlen == 0 );
5068  }
5069 
5070  /* decode the previously encoded string */
5071  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5072  if( POLARSSL_MODE_CBC == cipher_info->mode )
5073  {
5074  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5075  }
5076  else
5077  {
5078  fct_chk( enclen == outlen );
5079  }
5080  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5081  if( POLARSSL_MODE_CBC == cipher_info->mode )
5082  {
5083  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5084  }
5085  else
5086  {
5087  fct_chk( outlen == 0 );
5088  }
5089 
5090 
5091  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5092 
5093  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5094  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5095  FCT_TEST_END();
5096 #endif /* POLARSSL_DES_C */
5097 
5098 #ifdef POLARSSL_DES_C
5099 
5100  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
5101  size_t first_length = 1;
5102  size_t second_length = 15;
5103  size_t length = first_length + second_length;
5104  unsigned char key[32];
5105  unsigned char iv[16];
5106 
5107  cipher_context_t ctx_dec;
5108  cipher_context_t ctx_enc;
5109  const cipher_info_t *cipher_info;
5110 
5111  unsigned char inbuf[64];
5112  unsigned char encbuf[64];
5113  unsigned char decbuf[64];
5114 
5115  size_t outlen = 0;
5116  size_t totaloutlen = 0;
5117  size_t enclen = 0;
5118 
5119  memset( key, 0, 32 );
5120  memset( iv , 0, 16 );
5121 
5122  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5123  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5124 
5125  memset( inbuf, 5, 64 );
5126  memset( encbuf, 0, 64 );
5127  memset( decbuf, 0, 64 );
5128 
5129  /* Initialise enc and dec contexts */
5131  fct_chk( NULL != cipher_info);
5132 
5133  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5134  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5135 
5136  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5137  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5138 
5139  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5140  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5141 
5142  if( POLARSSL_MODE_CBC == cipher_info->mode )
5143  {
5144  enclen = cipher_get_block_size(&ctx_enc )
5145  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5146  }
5147  else
5148  {
5149  enclen = length;
5150  }
5151 
5152  /* encode length number of bytes from inbuf */
5153  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5154  totaloutlen = outlen;
5155  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5156  totaloutlen += outlen;
5157  if( POLARSSL_MODE_CBC == cipher_info->mode )
5158  {
5159  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5160  }
5161  else
5162  {
5163  fct_chk( totaloutlen == enclen );
5164  }
5165  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5166  totaloutlen += outlen;
5167  if( POLARSSL_MODE_CBC == cipher_info->mode )
5168  {
5169  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5170  }
5171  else
5172  {
5173  fct_chk( outlen == 0 );
5174  }
5175 
5176  /* decode the previously encoded string */
5177  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5178  if( POLARSSL_MODE_CBC == cipher_info->mode )
5179  {
5180  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5181  }
5182  else
5183  {
5184  fct_chk( enclen == outlen );
5185  }
5186  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5187  if( POLARSSL_MODE_CBC == cipher_info->mode )
5188  {
5189  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5190  }
5191  else
5192  {
5193  fct_chk( outlen == 0 );
5194  }
5195 
5196 
5197  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5198 
5199  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5200  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5201  FCT_TEST_END();
5202 #endif /* POLARSSL_DES_C */
5203 
5204 #ifdef POLARSSL_DES_C
5205 
5206  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
5207  size_t first_length = 15;
5208  size_t second_length = 1;
5209  size_t length = first_length + second_length;
5210  unsigned char key[32];
5211  unsigned char iv[16];
5212 
5213  cipher_context_t ctx_dec;
5214  cipher_context_t ctx_enc;
5215  const cipher_info_t *cipher_info;
5216 
5217  unsigned char inbuf[64];
5218  unsigned char encbuf[64];
5219  unsigned char decbuf[64];
5220 
5221  size_t outlen = 0;
5222  size_t totaloutlen = 0;
5223  size_t enclen = 0;
5224 
5225  memset( key, 0, 32 );
5226  memset( iv , 0, 16 );
5227 
5228  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5229  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5230 
5231  memset( inbuf, 5, 64 );
5232  memset( encbuf, 0, 64 );
5233  memset( decbuf, 0, 64 );
5234 
5235  /* Initialise enc and dec contexts */
5237  fct_chk( NULL != cipher_info);
5238 
5239  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5240  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5241 
5242  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5243  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5244 
5245  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5246  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5247 
5248  if( POLARSSL_MODE_CBC == cipher_info->mode )
5249  {
5250  enclen = cipher_get_block_size(&ctx_enc )
5251  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5252  }
5253  else
5254  {
5255  enclen = length;
5256  }
5257 
5258  /* encode length number of bytes from inbuf */
5259  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5260  totaloutlen = outlen;
5261  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5262  totaloutlen += outlen;
5263  if( POLARSSL_MODE_CBC == cipher_info->mode )
5264  {
5265  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5266  }
5267  else
5268  {
5269  fct_chk( totaloutlen == enclen );
5270  }
5271  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5272  totaloutlen += outlen;
5273  if( POLARSSL_MODE_CBC == cipher_info->mode )
5274  {
5275  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5276  }
5277  else
5278  {
5279  fct_chk( outlen == 0 );
5280  }
5281 
5282  /* decode the previously encoded string */
5283  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5284  if( POLARSSL_MODE_CBC == cipher_info->mode )
5285  {
5286  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5287  }
5288  else
5289  {
5290  fct_chk( enclen == outlen );
5291  }
5292  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5293  if( POLARSSL_MODE_CBC == cipher_info->mode )
5294  {
5295  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5296  }
5297  else
5298  {
5299  fct_chk( outlen == 0 );
5300  }
5301 
5302 
5303  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5304 
5305  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5306  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5307  FCT_TEST_END();
5308 #endif /* POLARSSL_DES_C */
5309 
5310 #ifdef POLARSSL_DES_C
5311 
5312  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
5313  size_t first_length = 15;
5314  size_t second_length = 7;
5315  size_t length = first_length + second_length;
5316  unsigned char key[32];
5317  unsigned char iv[16];
5318 
5319  cipher_context_t ctx_dec;
5320  cipher_context_t ctx_enc;
5321  const cipher_info_t *cipher_info;
5322 
5323  unsigned char inbuf[64];
5324  unsigned char encbuf[64];
5325  unsigned char decbuf[64];
5326 
5327  size_t outlen = 0;
5328  size_t totaloutlen = 0;
5329  size_t enclen = 0;
5330 
5331  memset( key, 0, 32 );
5332  memset( iv , 0, 16 );
5333 
5334  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5335  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5336 
5337  memset( inbuf, 5, 64 );
5338  memset( encbuf, 0, 64 );
5339  memset( decbuf, 0, 64 );
5340 
5341  /* Initialise enc and dec contexts */
5343  fct_chk( NULL != cipher_info);
5344 
5345  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5346  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5347 
5348  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5349  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5350 
5351  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5352  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5353 
5354  if( POLARSSL_MODE_CBC == cipher_info->mode )
5355  {
5356  enclen = cipher_get_block_size(&ctx_enc )
5357  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5358  }
5359  else
5360  {
5361  enclen = length;
5362  }
5363 
5364  /* encode length number of bytes from inbuf */
5365  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5366  totaloutlen = outlen;
5367  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5368  totaloutlen += outlen;
5369  if( POLARSSL_MODE_CBC == cipher_info->mode )
5370  {
5371  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5372  }
5373  else
5374  {
5375  fct_chk( totaloutlen == enclen );
5376  }
5377  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5378  totaloutlen += outlen;
5379  if( POLARSSL_MODE_CBC == cipher_info->mode )
5380  {
5381  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5382  }
5383  else
5384  {
5385  fct_chk( outlen == 0 );
5386  }
5387 
5388  /* decode the previously encoded string */
5389  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5390  if( POLARSSL_MODE_CBC == cipher_info->mode )
5391  {
5392  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5393  }
5394  else
5395  {
5396  fct_chk( enclen == outlen );
5397  }
5398  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5399  if( POLARSSL_MODE_CBC == cipher_info->mode )
5400  {
5401  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5402  }
5403  else
5404  {
5405  fct_chk( outlen == 0 );
5406  }
5407 
5408 
5409  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5410 
5411  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5412  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5413  FCT_TEST_END();
5414 #endif /* POLARSSL_DES_C */
5415 
5416 #ifdef POLARSSL_DES_C
5417 
5418  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
5419  size_t first_length = 16;
5420  size_t second_length = 6;
5421  size_t length = first_length + second_length;
5422  unsigned char key[32];
5423  unsigned char iv[16];
5424 
5425  cipher_context_t ctx_dec;
5426  cipher_context_t ctx_enc;
5427  const cipher_info_t *cipher_info;
5428 
5429  unsigned char inbuf[64];
5430  unsigned char encbuf[64];
5431  unsigned char decbuf[64];
5432 
5433  size_t outlen = 0;
5434  size_t totaloutlen = 0;
5435  size_t enclen = 0;
5436 
5437  memset( key, 0, 32 );
5438  memset( iv , 0, 16 );
5439 
5440  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5441  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5442 
5443  memset( inbuf, 5, 64 );
5444  memset( encbuf, 0, 64 );
5445  memset( decbuf, 0, 64 );
5446 
5447  /* Initialise enc and dec contexts */
5449  fct_chk( NULL != cipher_info);
5450 
5451  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5452  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5453 
5454  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5455  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5456 
5457  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5458  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5459 
5460  if( POLARSSL_MODE_CBC == cipher_info->mode )
5461  {
5462  enclen = cipher_get_block_size(&ctx_enc )
5463  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5464  }
5465  else
5466  {
5467  enclen = length;
5468  }
5469 
5470  /* encode length number of bytes from inbuf */
5471  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5472  totaloutlen = outlen;
5473  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5474  totaloutlen += outlen;
5475  if( POLARSSL_MODE_CBC == cipher_info->mode )
5476  {
5477  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5478  }
5479  else
5480  {
5481  fct_chk( totaloutlen == enclen );
5482  }
5483  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5484  totaloutlen += outlen;
5485  if( POLARSSL_MODE_CBC == cipher_info->mode )
5486  {
5487  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5488  }
5489  else
5490  {
5491  fct_chk( outlen == 0 );
5492  }
5493 
5494  /* decode the previously encoded string */
5495  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5496  if( POLARSSL_MODE_CBC == cipher_info->mode )
5497  {
5498  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5499  }
5500  else
5501  {
5502  fct_chk( enclen == outlen );
5503  }
5504  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5505  if( POLARSSL_MODE_CBC == cipher_info->mode )
5506  {
5507  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5508  }
5509  else
5510  {
5511  fct_chk( outlen == 0 );
5512  }
5513 
5514 
5515  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5516 
5517  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5518  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5519  FCT_TEST_END();
5520 #endif /* POLARSSL_DES_C */
5521 
5522 #ifdef POLARSSL_DES_C
5523 
5524  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
5525  size_t first_length = 17;
5526  size_t second_length = 6;
5527  size_t length = first_length + second_length;
5528  unsigned char key[32];
5529  unsigned char iv[16];
5530 
5531  cipher_context_t ctx_dec;
5532  cipher_context_t ctx_enc;
5533  const cipher_info_t *cipher_info;
5534 
5535  unsigned char inbuf[64];
5536  unsigned char encbuf[64];
5537  unsigned char decbuf[64];
5538 
5539  size_t outlen = 0;
5540  size_t totaloutlen = 0;
5541  size_t enclen = 0;
5542 
5543  memset( key, 0, 32 );
5544  memset( iv , 0, 16 );
5545 
5546  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5547  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5548 
5549  memset( inbuf, 5, 64 );
5550  memset( encbuf, 0, 64 );
5551  memset( decbuf, 0, 64 );
5552 
5553  /* Initialise enc and dec contexts */
5555  fct_chk( NULL != cipher_info);
5556 
5557  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5558  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5559 
5560  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5561  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5562 
5563  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5564  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5565 
5566  if( POLARSSL_MODE_CBC == cipher_info->mode )
5567  {
5568  enclen = cipher_get_block_size(&ctx_enc )
5569  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5570  }
5571  else
5572  {
5573  enclen = length;
5574  }
5575 
5576  /* encode length number of bytes from inbuf */
5577  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5578  totaloutlen = outlen;
5579  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5580  totaloutlen += outlen;
5581  if( POLARSSL_MODE_CBC == cipher_info->mode )
5582  {
5583  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5584  }
5585  else
5586  {
5587  fct_chk( totaloutlen == enclen );
5588  }
5589  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5590  totaloutlen += outlen;
5591  if( POLARSSL_MODE_CBC == cipher_info->mode )
5592  {
5593  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5594  }
5595  else
5596  {
5597  fct_chk( outlen == 0 );
5598  }
5599 
5600  /* decode the previously encoded string */
5601  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5602  if( POLARSSL_MODE_CBC == cipher_info->mode )
5603  {
5604  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5605  }
5606  else
5607  {
5608  fct_chk( enclen == outlen );
5609  }
5610  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5611  if( POLARSSL_MODE_CBC == cipher_info->mode )
5612  {
5613  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5614  }
5615  else
5616  {
5617  fct_chk( outlen == 0 );
5618  }
5619 
5620 
5621  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5622 
5623  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5624  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5625  FCT_TEST_END();
5626 #endif /* POLARSSL_DES_C */
5627 
5628 #ifdef POLARSSL_DES_C
5629 
5630  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
5631  size_t first_length = 16;
5632  size_t second_length = 16;
5633  size_t length = first_length + second_length;
5634  unsigned char key[32];
5635  unsigned char iv[16];
5636 
5637  cipher_context_t ctx_dec;
5638  cipher_context_t ctx_enc;
5639  const cipher_info_t *cipher_info;
5640 
5641  unsigned char inbuf[64];
5642  unsigned char encbuf[64];
5643  unsigned char decbuf[64];
5644 
5645  size_t outlen = 0;
5646  size_t totaloutlen = 0;
5647  size_t enclen = 0;
5648 
5649  memset( key, 0, 32 );
5650  memset( iv , 0, 16 );
5651 
5652  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5653  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5654 
5655  memset( inbuf, 5, 64 );
5656  memset( encbuf, 0, 64 );
5657  memset( decbuf, 0, 64 );
5658 
5659  /* Initialise enc and dec contexts */
5661  fct_chk( NULL != cipher_info);
5662 
5663  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5664  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5665 
5666  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5667  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5668 
5669  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5670  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5671 
5672  if( POLARSSL_MODE_CBC == cipher_info->mode )
5673  {
5674  enclen = cipher_get_block_size(&ctx_enc )
5675  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5676  }
5677  else
5678  {
5679  enclen = length;
5680  }
5681 
5682  /* encode length number of bytes from inbuf */
5683  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5684  totaloutlen = outlen;
5685  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5686  totaloutlen += outlen;
5687  if( POLARSSL_MODE_CBC == cipher_info->mode )
5688  {
5689  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5690  }
5691  else
5692  {
5693  fct_chk( totaloutlen == enclen );
5694  }
5695  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5696  totaloutlen += outlen;
5697  if( POLARSSL_MODE_CBC == cipher_info->mode )
5698  {
5699  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5700  }
5701  else
5702  {
5703  fct_chk( outlen == 0 );
5704  }
5705 
5706  /* decode the previously encoded string */
5707  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5708  if( POLARSSL_MODE_CBC == cipher_info->mode )
5709  {
5710  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5711  }
5712  else
5713  {
5714  fct_chk( enclen == outlen );
5715  }
5716  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5717  if( POLARSSL_MODE_CBC == cipher_info->mode )
5718  {
5719  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5720  }
5721  else
5722  {
5723  fct_chk( outlen == 0 );
5724  }
5725 
5726 
5727  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5728 
5729  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5730  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5731  FCT_TEST_END();
5732 #endif /* POLARSSL_DES_C */
5733 
5734 #ifdef POLARSSL_DES_C
5735 
5736  FCT_TEST_BGN(des3_encrypt_and_decrypt_0_bytes)
5737  size_t length = 0;
5738  unsigned char key[32];
5739  unsigned char iv[16];
5740 
5741  const cipher_info_t *cipher_info;
5742  cipher_context_t ctx_dec;
5743  cipher_context_t ctx_enc;
5744 
5745  unsigned char inbuf[64];
5746  unsigned char encbuf[64];
5747  unsigned char decbuf[64];
5748 
5749  size_t outlen = 0;
5750  size_t enclen = 0;
5751 
5752  memset( key, 0, 32 );
5753  memset( iv , 0, 16 );
5754 
5755  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5756  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5757 
5758  memset( inbuf, 5, 64 );
5759  memset( encbuf, 0, 64 );
5760  memset( decbuf, 0, 64 );
5761 
5762  /* Check and get info structures */
5764  fct_chk( NULL != cipher_info );
5765  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
5766 
5767  /* Initialise enc and dec contexts */
5768  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5769  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5770 
5771  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
5772  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
5773 
5774  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5775  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5776 
5777  if( POLARSSL_MODE_CBC == cipher_info->mode )
5778  {
5779  enclen = cipher_get_block_size( &ctx_enc )
5780  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5781  }
5782  else
5783  {
5784  enclen = length;
5785  }
5786 
5787  /* encode length number of bytes from inbuf */
5788  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
5789  if( POLARSSL_MODE_CBC == cipher_info->mode )
5790  {
5791  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5792  }
5793  else
5794  {
5795  fct_chk( outlen == enclen );
5796  }
5797 
5798  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
5799  if( POLARSSL_MODE_CBC == cipher_info->mode )
5800  {
5801  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5802  }
5803  else
5804  {
5805  fct_chk( outlen == 0 );
5806  }
5807 
5808 
5809  /* decode the previously encoded string */
5810  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5811  if( POLARSSL_MODE_CBC == cipher_info->mode )
5812  {
5813  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5814  }
5815  else
5816  {
5817  fct_chk( enclen == outlen );
5818  }
5819 
5820  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5821  if( POLARSSL_MODE_CBC == cipher_info->mode )
5822  {
5823  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5824  }
5825  else
5826  {
5827  fct_chk( outlen == 0 );
5828  }
5829 
5830 
5831  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5832 
5833  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5834  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5835  FCT_TEST_END();
5836 #endif /* POLARSSL_DES_C */
5837 
5838 #ifdef POLARSSL_DES_C
5839 
5840  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_byte)
5841  size_t length = 1;
5842  unsigned char key[32];
5843  unsigned char iv[16];
5844 
5845  const cipher_info_t *cipher_info;
5846  cipher_context_t ctx_dec;
5847  cipher_context_t ctx_enc;
5848 
5849  unsigned char inbuf[64];
5850  unsigned char encbuf[64];
5851  unsigned char decbuf[64];
5852 
5853  size_t outlen = 0;
5854  size_t enclen = 0;
5855 
5856  memset( key, 0, 32 );
5857  memset( iv , 0, 16 );
5858 
5859  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5860  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5861 
5862  memset( inbuf, 5, 64 );
5863  memset( encbuf, 0, 64 );
5864  memset( decbuf, 0, 64 );
5865 
5866  /* Check and get info structures */
5868  fct_chk( NULL != cipher_info );
5869  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
5870 
5871  /* Initialise enc and dec contexts */
5872  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5873  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5874 
5875  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
5876  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
5877 
5878  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5879  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5880 
5881  if( POLARSSL_MODE_CBC == cipher_info->mode )
5882  {
5883  enclen = cipher_get_block_size( &ctx_enc )
5884  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5885  }
5886  else
5887  {
5888  enclen = length;
5889  }
5890 
5891  /* encode length number of bytes from inbuf */
5892  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
5893  if( POLARSSL_MODE_CBC == cipher_info->mode )
5894  {
5895  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5896  }
5897  else
5898  {
5899  fct_chk( outlen == enclen );
5900  }
5901 
5902  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
5903  if( POLARSSL_MODE_CBC == cipher_info->mode )
5904  {
5905  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5906  }
5907  else
5908  {
5909  fct_chk( outlen == 0 );
5910  }
5911 
5912 
5913  /* decode the previously encoded string */
5914  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5915  if( POLARSSL_MODE_CBC == cipher_info->mode )
5916  {
5917  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5918  }
5919  else
5920  {
5921  fct_chk( enclen == outlen );
5922  }
5923 
5924  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5925  if( POLARSSL_MODE_CBC == cipher_info->mode )
5926  {
5927  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5928  }
5929  else
5930  {
5931  fct_chk( outlen == 0 );
5932  }
5933 
5934 
5935  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5936 
5937  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5938  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5939  FCT_TEST_END();
5940 #endif /* POLARSSL_DES_C */
5941 
5942 #ifdef POLARSSL_DES_C
5943 
5944  FCT_TEST_BGN(des3_encrypt_and_decrypt_2_bytes)
5945  size_t length = 2;
5946  unsigned char key[32];
5947  unsigned char iv[16];
5948 
5949  const cipher_info_t *cipher_info;
5950  cipher_context_t ctx_dec;
5951  cipher_context_t ctx_enc;
5952 
5953  unsigned char inbuf[64];
5954  unsigned char encbuf[64];
5955  unsigned char decbuf[64];
5956 
5957  size_t outlen = 0;
5958  size_t enclen = 0;
5959 
5960  memset( key, 0, 32 );
5961  memset( iv , 0, 16 );
5962 
5963  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5964  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5965 
5966  memset( inbuf, 5, 64 );
5967  memset( encbuf, 0, 64 );
5968  memset( decbuf, 0, 64 );
5969 
5970  /* Check and get info structures */
5972  fct_chk( NULL != cipher_info );
5973  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
5974 
5975  /* Initialise enc and dec contexts */
5976  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5977  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5978 
5979  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
5980  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
5981 
5982  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5983  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5984 
5985  if( POLARSSL_MODE_CBC == cipher_info->mode )
5986  {
5987  enclen = cipher_get_block_size( &ctx_enc )
5988  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5989  }
5990  else
5991  {
5992  enclen = length;
5993  }
5994 
5995  /* encode length number of bytes from inbuf */
5996  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
5997  if( POLARSSL_MODE_CBC == cipher_info->mode )
5998  {
5999  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6000  }
6001  else
6002  {
6003  fct_chk( outlen == enclen );
6004  }
6005 
6006  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6007  if( POLARSSL_MODE_CBC == cipher_info->mode )
6008  {
6009  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6010  }
6011  else
6012  {
6013  fct_chk( outlen == 0 );
6014  }
6015 
6016 
6017  /* decode the previously encoded string */
6018  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6019  if( POLARSSL_MODE_CBC == cipher_info->mode )
6020  {
6021  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6022  }
6023  else
6024  {
6025  fct_chk( enclen == outlen );
6026  }
6027 
6028  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6029  if( POLARSSL_MODE_CBC == cipher_info->mode )
6030  {
6031  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6032  }
6033  else
6034  {
6035  fct_chk( outlen == 0 );
6036  }
6037 
6038 
6039  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6040 
6041  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6042  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6043  FCT_TEST_END();
6044 #endif /* POLARSSL_DES_C */
6045 
6046 #ifdef POLARSSL_DES_C
6047 
6048  FCT_TEST_BGN(des3_encrypt_and_decrypt_7_bytes)
6049  size_t length = 7;
6050  unsigned char key[32];
6051  unsigned char iv[16];
6052 
6053  const cipher_info_t *cipher_info;
6054  cipher_context_t ctx_dec;
6055  cipher_context_t ctx_enc;
6056 
6057  unsigned char inbuf[64];
6058  unsigned char encbuf[64];
6059  unsigned char decbuf[64];
6060 
6061  size_t outlen = 0;
6062  size_t enclen = 0;
6063 
6064  memset( key, 0, 32 );
6065  memset( iv , 0, 16 );
6066 
6067  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6068  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6069 
6070  memset( inbuf, 5, 64 );
6071  memset( encbuf, 0, 64 );
6072  memset( decbuf, 0, 64 );
6073 
6074  /* Check and get info structures */
6076  fct_chk( NULL != cipher_info );
6077  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6078 
6079  /* Initialise enc and dec contexts */
6080  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6081  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6082 
6083  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6084  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6085 
6086  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6087  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6088 
6089  if( POLARSSL_MODE_CBC == cipher_info->mode )
6090  {
6091  enclen = cipher_get_block_size( &ctx_enc )
6092  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6093  }
6094  else
6095  {
6096  enclen = length;
6097  }
6098 
6099  /* encode length number of bytes from inbuf */
6100  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6101  if( POLARSSL_MODE_CBC == cipher_info->mode )
6102  {
6103  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6104  }
6105  else
6106  {
6107  fct_chk( outlen == enclen );
6108  }
6109 
6110  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6111  if( POLARSSL_MODE_CBC == cipher_info->mode )
6112  {
6113  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6114  }
6115  else
6116  {
6117  fct_chk( outlen == 0 );
6118  }
6119 
6120 
6121  /* decode the previously encoded string */
6122  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6123  if( POLARSSL_MODE_CBC == cipher_info->mode )
6124  {
6125  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6126  }
6127  else
6128  {
6129  fct_chk( enclen == outlen );
6130  }
6131 
6132  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6133  if( POLARSSL_MODE_CBC == cipher_info->mode )
6134  {
6135  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6136  }
6137  else
6138  {
6139  fct_chk( outlen == 0 );
6140  }
6141 
6142 
6143  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6144 
6145  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6146  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6147  FCT_TEST_END();
6148 #endif /* POLARSSL_DES_C */
6149 
6150 #ifdef POLARSSL_DES_C
6151 
6152  FCT_TEST_BGN(des3_encrypt_and_decrypt_8_bytes)
6153  size_t length = 8;
6154  unsigned char key[32];
6155  unsigned char iv[16];
6156 
6157  const cipher_info_t *cipher_info;
6158  cipher_context_t ctx_dec;
6159  cipher_context_t ctx_enc;
6160 
6161  unsigned char inbuf[64];
6162  unsigned char encbuf[64];
6163  unsigned char decbuf[64];
6164 
6165  size_t outlen = 0;
6166  size_t enclen = 0;
6167 
6168  memset( key, 0, 32 );
6169  memset( iv , 0, 16 );
6170 
6171  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6172  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6173 
6174  memset( inbuf, 5, 64 );
6175  memset( encbuf, 0, 64 );
6176  memset( decbuf, 0, 64 );
6177 
6178  /* Check and get info structures */
6180  fct_chk( NULL != cipher_info );
6181  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6182 
6183  /* Initialise enc and dec contexts */
6184  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6185  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6186 
6187  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6188  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6189 
6190  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6191  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6192 
6193  if( POLARSSL_MODE_CBC == cipher_info->mode )
6194  {
6195  enclen = cipher_get_block_size( &ctx_enc )
6196  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6197  }
6198  else
6199  {
6200  enclen = length;
6201  }
6202 
6203  /* encode length number of bytes from inbuf */
6204  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6205  if( POLARSSL_MODE_CBC == cipher_info->mode )
6206  {
6207  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6208  }
6209  else
6210  {
6211  fct_chk( outlen == enclen );
6212  }
6213 
6214  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6215  if( POLARSSL_MODE_CBC == cipher_info->mode )
6216  {
6217  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6218  }
6219  else
6220  {
6221  fct_chk( outlen == 0 );
6222  }
6223 
6224 
6225  /* decode the previously encoded string */
6226  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6227  if( POLARSSL_MODE_CBC == cipher_info->mode )
6228  {
6229  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6230  }
6231  else
6232  {
6233  fct_chk( enclen == outlen );
6234  }
6235 
6236  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6237  if( POLARSSL_MODE_CBC == cipher_info->mode )
6238  {
6239  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6240  }
6241  else
6242  {
6243  fct_chk( outlen == 0 );
6244  }
6245 
6246 
6247  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6248 
6249  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6250  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6251  FCT_TEST_END();
6252 #endif /* POLARSSL_DES_C */
6253 
6254 #ifdef POLARSSL_DES_C
6255 
6256  FCT_TEST_BGN(des3_encrypt_and_decrypt_9_bytes)
6257  size_t length = 9;
6258  unsigned char key[32];
6259  unsigned char iv[16];
6260 
6261  const cipher_info_t *cipher_info;
6262  cipher_context_t ctx_dec;
6263  cipher_context_t ctx_enc;
6264 
6265  unsigned char inbuf[64];
6266  unsigned char encbuf[64];
6267  unsigned char decbuf[64];
6268 
6269  size_t outlen = 0;
6270  size_t enclen = 0;
6271 
6272  memset( key, 0, 32 );
6273  memset( iv , 0, 16 );
6274 
6275  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6276  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6277 
6278  memset( inbuf, 5, 64 );
6279  memset( encbuf, 0, 64 );
6280  memset( decbuf, 0, 64 );
6281 
6282  /* Check and get info structures */
6284  fct_chk( NULL != cipher_info );
6285  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6286 
6287  /* Initialise enc and dec contexts */
6288  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6289  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6290 
6291  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6292  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6293 
6294  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6295  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6296 
6297  if( POLARSSL_MODE_CBC == cipher_info->mode )
6298  {
6299  enclen = cipher_get_block_size( &ctx_enc )
6300  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6301  }
6302  else
6303  {
6304  enclen = length;
6305  }
6306 
6307  /* encode length number of bytes from inbuf */
6308  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6309  if( POLARSSL_MODE_CBC == cipher_info->mode )
6310  {
6311  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6312  }
6313  else
6314  {
6315  fct_chk( outlen == enclen );
6316  }
6317 
6318  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6319  if( POLARSSL_MODE_CBC == cipher_info->mode )
6320  {
6321  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6322  }
6323  else
6324  {
6325  fct_chk( outlen == 0 );
6326  }
6327 
6328 
6329  /* decode the previously encoded string */
6330  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6331  if( POLARSSL_MODE_CBC == cipher_info->mode )
6332  {
6333  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6334  }
6335  else
6336  {
6337  fct_chk( enclen == outlen );
6338  }
6339 
6340  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6341  if( POLARSSL_MODE_CBC == cipher_info->mode )
6342  {
6343  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6344  }
6345  else
6346  {
6347  fct_chk( outlen == 0 );
6348  }
6349 
6350 
6351  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6352 
6353  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6354  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6355  FCT_TEST_END();
6356 #endif /* POLARSSL_DES_C */
6357 
6358 #ifdef POLARSSL_DES_C
6359 
6360  FCT_TEST_BGN(des3_encrypt_and_decrypt_15_bytes)
6361  size_t length = 15;
6362  unsigned char key[32];
6363  unsigned char iv[16];
6364 
6365  const cipher_info_t *cipher_info;
6366  cipher_context_t ctx_dec;
6367  cipher_context_t ctx_enc;
6368 
6369  unsigned char inbuf[64];
6370  unsigned char encbuf[64];
6371  unsigned char decbuf[64];
6372 
6373  size_t outlen = 0;
6374  size_t enclen = 0;
6375 
6376  memset( key, 0, 32 );
6377  memset( iv , 0, 16 );
6378 
6379  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6380  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6381 
6382  memset( inbuf, 5, 64 );
6383  memset( encbuf, 0, 64 );
6384  memset( decbuf, 0, 64 );
6385 
6386  /* Check and get info structures */
6388  fct_chk( NULL != cipher_info );
6389  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6390 
6391  /* Initialise enc and dec contexts */
6392  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6393  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6394 
6395  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6396  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6397 
6398  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6399  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6400 
6401  if( POLARSSL_MODE_CBC == cipher_info->mode )
6402  {
6403  enclen = cipher_get_block_size( &ctx_enc )
6404  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6405  }
6406  else
6407  {
6408  enclen = length;
6409  }
6410 
6411  /* encode length number of bytes from inbuf */
6412  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6413  if( POLARSSL_MODE_CBC == cipher_info->mode )
6414  {
6415  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6416  }
6417  else
6418  {
6419  fct_chk( outlen == enclen );
6420  }
6421 
6422  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6423  if( POLARSSL_MODE_CBC == cipher_info->mode )
6424  {
6425  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6426  }
6427  else
6428  {
6429  fct_chk( outlen == 0 );
6430  }
6431 
6432 
6433  /* decode the previously encoded string */
6434  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6435  if( POLARSSL_MODE_CBC == cipher_info->mode )
6436  {
6437  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6438  }
6439  else
6440  {
6441  fct_chk( enclen == outlen );
6442  }
6443 
6444  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6445  if( POLARSSL_MODE_CBC == cipher_info->mode )
6446  {
6447  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6448  }
6449  else
6450  {
6451  fct_chk( outlen == 0 );
6452  }
6453 
6454 
6455  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6456 
6457  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6458  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6459  FCT_TEST_END();
6460 #endif /* POLARSSL_DES_C */
6461 
6462 #ifdef POLARSSL_DES_C
6463 
6464  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes)
6465  size_t length = 16;
6466  unsigned char key[32];
6467  unsigned char iv[16];
6468 
6469  const cipher_info_t *cipher_info;
6470  cipher_context_t ctx_dec;
6471  cipher_context_t ctx_enc;
6472 
6473  unsigned char inbuf[64];
6474  unsigned char encbuf[64];
6475  unsigned char decbuf[64];
6476 
6477  size_t outlen = 0;
6478  size_t enclen = 0;
6479 
6480  memset( key, 0, 32 );
6481  memset( iv , 0, 16 );
6482 
6483  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6484  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6485 
6486  memset( inbuf, 5, 64 );
6487  memset( encbuf, 0, 64 );
6488  memset( decbuf, 0, 64 );
6489 
6490  /* Check and get info structures */
6492  fct_chk( NULL != cipher_info );
6493  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6494 
6495  /* Initialise enc and dec contexts */
6496  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6497  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6498 
6499  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6500  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6501 
6502  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6503  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6504 
6505  if( POLARSSL_MODE_CBC == cipher_info->mode )
6506  {
6507  enclen = cipher_get_block_size( &ctx_enc )
6508  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6509  }
6510  else
6511  {
6512  enclen = length;
6513  }
6514 
6515  /* encode length number of bytes from inbuf */
6516  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6517  if( POLARSSL_MODE_CBC == cipher_info->mode )
6518  {
6519  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6520  }
6521  else
6522  {
6523  fct_chk( outlen == enclen );
6524  }
6525 
6526  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6527  if( POLARSSL_MODE_CBC == cipher_info->mode )
6528  {
6529  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6530  }
6531  else
6532  {
6533  fct_chk( outlen == 0 );
6534  }
6535 
6536 
6537  /* decode the previously encoded string */
6538  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6539  if( POLARSSL_MODE_CBC == cipher_info->mode )
6540  {
6541  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6542  }
6543  else
6544  {
6545  fct_chk( enclen == outlen );
6546  }
6547 
6548  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6549  if( POLARSSL_MODE_CBC == cipher_info->mode )
6550  {
6551  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6552  }
6553  else
6554  {
6555  fct_chk( outlen == 0 );
6556  }
6557 
6558 
6559  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6560 
6561  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6562  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6563  FCT_TEST_END();
6564 #endif /* POLARSSL_DES_C */
6565 
6566 #ifdef POLARSSL_DES_C
6567 
6568  FCT_TEST_BGN(des3_encrypt_and_decrypt_17_bytes)
6569  size_t length = 17;
6570  unsigned char key[32];
6571  unsigned char iv[16];
6572 
6573  const cipher_info_t *cipher_info;
6574  cipher_context_t ctx_dec;
6575  cipher_context_t ctx_enc;
6576 
6577  unsigned char inbuf[64];
6578  unsigned char encbuf[64];
6579  unsigned char decbuf[64];
6580 
6581  size_t outlen = 0;
6582  size_t enclen = 0;
6583 
6584  memset( key, 0, 32 );
6585  memset( iv , 0, 16 );
6586 
6587  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6588  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6589 
6590  memset( inbuf, 5, 64 );
6591  memset( encbuf, 0, 64 );
6592  memset( decbuf, 0, 64 );
6593 
6594  /* Check and get info structures */
6596  fct_chk( NULL != cipher_info );
6597  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6598 
6599  /* Initialise enc and dec contexts */
6600  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6601  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6602 
6603  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6604  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6605 
6606  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6607  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6608 
6609  if( POLARSSL_MODE_CBC == cipher_info->mode )
6610  {
6611  enclen = cipher_get_block_size( &ctx_enc )
6612  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6613  }
6614  else
6615  {
6616  enclen = length;
6617  }
6618 
6619  /* encode length number of bytes from inbuf */
6620  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6621  if( POLARSSL_MODE_CBC == cipher_info->mode )
6622  {
6623  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6624  }
6625  else
6626  {
6627  fct_chk( outlen == enclen );
6628  }
6629 
6630  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6631  if( POLARSSL_MODE_CBC == cipher_info->mode )
6632  {
6633  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6634  }
6635  else
6636  {
6637  fct_chk( outlen == 0 );
6638  }
6639 
6640 
6641  /* decode the previously encoded string */
6642  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6643  if( POLARSSL_MODE_CBC == cipher_info->mode )
6644  {
6645  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6646  }
6647  else
6648  {
6649  fct_chk( enclen == outlen );
6650  }
6651 
6652  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6653  if( POLARSSL_MODE_CBC == cipher_info->mode )
6654  {
6655  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6656  }
6657  else
6658  {
6659  fct_chk( outlen == 0 );
6660  }
6661 
6662 
6663  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6664 
6665  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6666  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6667  FCT_TEST_END();
6668 #endif /* POLARSSL_DES_C */
6669 
6670 #ifdef POLARSSL_DES_C
6671 
6672  FCT_TEST_BGN(des3_encrypt_and_decrypt_31_bytes)
6673  size_t length = 31;
6674  unsigned char key[32];
6675  unsigned char iv[16];
6676 
6677  const cipher_info_t *cipher_info;
6678  cipher_context_t ctx_dec;
6679  cipher_context_t ctx_enc;
6680 
6681  unsigned char inbuf[64];
6682  unsigned char encbuf[64];
6683  unsigned char decbuf[64];
6684 
6685  size_t outlen = 0;
6686  size_t enclen = 0;
6687 
6688  memset( key, 0, 32 );
6689  memset( iv , 0, 16 );
6690 
6691  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6692  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6693 
6694  memset( inbuf, 5, 64 );
6695  memset( encbuf, 0, 64 );
6696  memset( decbuf, 0, 64 );
6697 
6698  /* Check and get info structures */
6700  fct_chk( NULL != cipher_info );
6701  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6702 
6703  /* Initialise enc and dec contexts */
6704  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6705  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6706 
6707  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6708  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6709 
6710  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6711  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6712 
6713  if( POLARSSL_MODE_CBC == cipher_info->mode )
6714  {
6715  enclen = cipher_get_block_size( &ctx_enc )
6716  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6717  }
6718  else
6719  {
6720  enclen = length;
6721  }
6722 
6723  /* encode length number of bytes from inbuf */
6724  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6725  if( POLARSSL_MODE_CBC == cipher_info->mode )
6726  {
6727  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6728  }
6729  else
6730  {
6731  fct_chk( outlen == enclen );
6732  }
6733 
6734  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6735  if( POLARSSL_MODE_CBC == cipher_info->mode )
6736  {
6737  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6738  }
6739  else
6740  {
6741  fct_chk( outlen == 0 );
6742  }
6743 
6744 
6745  /* decode the previously encoded string */
6746  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6747  if( POLARSSL_MODE_CBC == cipher_info->mode )
6748  {
6749  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6750  }
6751  else
6752  {
6753  fct_chk( enclen == outlen );
6754  }
6755 
6756  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6757  if( POLARSSL_MODE_CBC == cipher_info->mode )
6758  {
6759  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6760  }
6761  else
6762  {
6763  fct_chk( outlen == 0 );
6764  }
6765 
6766 
6767  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6768 
6769  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6770  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6771  FCT_TEST_END();
6772 #endif /* POLARSSL_DES_C */
6773 
6774 #ifdef POLARSSL_DES_C
6775 
6776  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes)
6777  size_t length = 32;
6778  unsigned char key[32];
6779  unsigned char iv[16];
6780 
6781  const cipher_info_t *cipher_info;
6782  cipher_context_t ctx_dec;
6783  cipher_context_t ctx_enc;
6784 
6785  unsigned char inbuf[64];
6786  unsigned char encbuf[64];
6787  unsigned char decbuf[64];
6788 
6789  size_t outlen = 0;
6790  size_t enclen = 0;
6791 
6792  memset( key, 0, 32 );
6793  memset( iv , 0, 16 );
6794 
6795  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6796  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6797 
6798  memset( inbuf, 5, 64 );
6799  memset( encbuf, 0, 64 );
6800  memset( decbuf, 0, 64 );
6801 
6802  /* Check and get info structures */
6804  fct_chk( NULL != cipher_info );
6805  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6806 
6807  /* Initialise enc and dec contexts */
6808  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6809  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6810 
6811  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6812  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6813 
6814  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6815  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6816 
6817  if( POLARSSL_MODE_CBC == cipher_info->mode )
6818  {
6819  enclen = cipher_get_block_size( &ctx_enc )
6820  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6821  }
6822  else
6823  {
6824  enclen = length;
6825  }
6826 
6827  /* encode length number of bytes from inbuf */
6828  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6829  if( POLARSSL_MODE_CBC == cipher_info->mode )
6830  {
6831  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6832  }
6833  else
6834  {
6835  fct_chk( outlen == enclen );
6836  }
6837 
6838  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6839  if( POLARSSL_MODE_CBC == cipher_info->mode )
6840  {
6841  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6842  }
6843  else
6844  {
6845  fct_chk( outlen == 0 );
6846  }
6847 
6848 
6849  /* decode the previously encoded string */
6850  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6851  if( POLARSSL_MODE_CBC == cipher_info->mode )
6852  {
6853  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6854  }
6855  else
6856  {
6857  fct_chk( enclen == outlen );
6858  }
6859 
6860  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6861  if( POLARSSL_MODE_CBC == cipher_info->mode )
6862  {
6863  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6864  }
6865  else
6866  {
6867  fct_chk( outlen == 0 );
6868  }
6869 
6870 
6871  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6872 
6873  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6874  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6875  FCT_TEST_END();
6876 #endif /* POLARSSL_DES_C */
6877 
6878 #ifdef POLARSSL_DES_C
6879 
6880  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes)
6881  size_t length = 33;
6882  unsigned char key[32];
6883  unsigned char iv[16];
6884 
6885  const cipher_info_t *cipher_info;
6886  cipher_context_t ctx_dec;
6887  cipher_context_t ctx_enc;
6888 
6889  unsigned char inbuf[64];
6890  unsigned char encbuf[64];
6891  unsigned char decbuf[64];
6892 
6893  size_t outlen = 0;
6894  size_t enclen = 0;
6895 
6896  memset( key, 0, 32 );
6897  memset( iv , 0, 16 );
6898 
6899  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6900  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6901 
6902  memset( inbuf, 5, 64 );
6903  memset( encbuf, 0, 64 );
6904  memset( decbuf, 0, 64 );
6905 
6906  /* Check and get info structures */
6908  fct_chk( NULL != cipher_info );
6909  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6910 
6911  /* Initialise enc and dec contexts */
6912  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6913  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6914 
6915  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6916  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6917 
6918  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6919  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6920 
6921  if( POLARSSL_MODE_CBC == cipher_info->mode )
6922  {
6923  enclen = cipher_get_block_size( &ctx_enc )
6924  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6925  }
6926  else
6927  {
6928  enclen = length;
6929  }
6930 
6931  /* encode length number of bytes from inbuf */
6932  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6933  if( POLARSSL_MODE_CBC == cipher_info->mode )
6934  {
6935  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6936  }
6937  else
6938  {
6939  fct_chk( outlen == enclen );
6940  }
6941 
6942  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6943  if( POLARSSL_MODE_CBC == cipher_info->mode )
6944  {
6945  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6946  }
6947  else
6948  {
6949  fct_chk( outlen == 0 );
6950  }
6951 
6952 
6953  /* decode the previously encoded string */
6954  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6955  if( POLARSSL_MODE_CBC == cipher_info->mode )
6956  {
6957  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6958  }
6959  else
6960  {
6961  fct_chk( enclen == outlen );
6962  }
6963 
6964  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6965  if( POLARSSL_MODE_CBC == cipher_info->mode )
6966  {
6967  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6968  }
6969  else
6970  {
6971  fct_chk( outlen == 0 );
6972  }
6973 
6974 
6975  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6976 
6977  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6978  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6979  FCT_TEST_END();
6980 #endif /* POLARSSL_DES_C */
6981 
6982 #ifdef POLARSSL_DES_C
6983 
6984  FCT_TEST_BGN(des3_encrypt_and_decrypt_47_bytes)
6985  size_t length = 47;
6986  unsigned char key[32];
6987  unsigned char iv[16];
6988 
6989  const cipher_info_t *cipher_info;
6990  cipher_context_t ctx_dec;
6991  cipher_context_t ctx_enc;
6992 
6993  unsigned char inbuf[64];
6994  unsigned char encbuf[64];
6995  unsigned char decbuf[64];
6996 
6997  size_t outlen = 0;
6998  size_t enclen = 0;
6999 
7000  memset( key, 0, 32 );
7001  memset( iv , 0, 16 );
7002 
7003  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7004  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7005 
7006  memset( inbuf, 5, 64 );
7007  memset( encbuf, 0, 64 );
7008  memset( decbuf, 0, 64 );
7009 
7010  /* Check and get info structures */
7012  fct_chk( NULL != cipher_info );
7013  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
7014 
7015  /* Initialise enc and dec contexts */
7016  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7017  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7018 
7019  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7020  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7021 
7022  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7023  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7024 
7025  if( POLARSSL_MODE_CBC == cipher_info->mode )
7026  {
7027  enclen = cipher_get_block_size( &ctx_enc )
7028  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7029  }
7030  else
7031  {
7032  enclen = length;
7033  }
7034 
7035  /* encode length number of bytes from inbuf */
7036  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
7037  if( POLARSSL_MODE_CBC == cipher_info->mode )
7038  {
7039  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7040  }
7041  else
7042  {
7043  fct_chk( outlen == enclen );
7044  }
7045 
7046  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
7047  if( POLARSSL_MODE_CBC == cipher_info->mode )
7048  {
7049  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7050  }
7051  else
7052  {
7053  fct_chk( outlen == 0 );
7054  }
7055 
7056 
7057  /* decode the previously encoded string */
7058  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7059  if( POLARSSL_MODE_CBC == cipher_info->mode )
7060  {
7061  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7062  }
7063  else
7064  {
7065  fct_chk( enclen == outlen );
7066  }
7067 
7068  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7069  if( POLARSSL_MODE_CBC == cipher_info->mode )
7070  {
7071  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7072  }
7073  else
7074  {
7075  fct_chk( outlen == 0 );
7076  }
7077 
7078 
7079  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7080 
7081  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7082  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7083  FCT_TEST_END();
7084 #endif /* POLARSSL_DES_C */
7085 
7086 #ifdef POLARSSL_DES_C
7087 
7088  FCT_TEST_BGN(des3_encrypt_and_decrypt_48_bytes)
7089  size_t length = 48;
7090  unsigned char key[32];
7091  unsigned char iv[16];
7092 
7093  const cipher_info_t *cipher_info;
7094  cipher_context_t ctx_dec;
7095  cipher_context_t ctx_enc;
7096 
7097  unsigned char inbuf[64];
7098  unsigned char encbuf[64];
7099  unsigned char decbuf[64];
7100 
7101  size_t outlen = 0;
7102  size_t enclen = 0;
7103 
7104  memset( key, 0, 32 );
7105  memset( iv , 0, 16 );
7106 
7107  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7108  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7109 
7110  memset( inbuf, 5, 64 );
7111  memset( encbuf, 0, 64 );
7112  memset( decbuf, 0, 64 );
7113 
7114  /* Check and get info structures */
7116  fct_chk( NULL != cipher_info );
7117  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
7118 
7119  /* Initialise enc and dec contexts */
7120  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7121  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7122 
7123  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7124  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7125 
7126  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7127  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7128 
7129  if( POLARSSL_MODE_CBC == cipher_info->mode )
7130  {
7131  enclen = cipher_get_block_size( &ctx_enc )
7132  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7133  }
7134  else
7135  {
7136  enclen = length;
7137  }
7138 
7139  /* encode length number of bytes from inbuf */
7140  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
7141  if( POLARSSL_MODE_CBC == cipher_info->mode )
7142  {
7143  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7144  }
7145  else
7146  {
7147  fct_chk( outlen == enclen );
7148  }
7149 
7150  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
7151  if( POLARSSL_MODE_CBC == cipher_info->mode )
7152  {
7153  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7154  }
7155  else
7156  {
7157  fct_chk( outlen == 0 );
7158  }
7159 
7160 
7161  /* decode the previously encoded string */
7162  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7163  if( POLARSSL_MODE_CBC == cipher_info->mode )
7164  {
7165  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7166  }
7167  else
7168  {
7169  fct_chk( enclen == outlen );
7170  }
7171 
7172  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7173  if( POLARSSL_MODE_CBC == cipher_info->mode )
7174  {
7175  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7176  }
7177  else
7178  {
7179  fct_chk( outlen == 0 );
7180  }
7181 
7182 
7183  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7184 
7185  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7186  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7187  FCT_TEST_END();
7188 #endif /* POLARSSL_DES_C */
7189 
7190 #ifdef POLARSSL_DES_C
7191 
7192  FCT_TEST_BGN(des3_encrypt_and_decrypt_49_bytes)
7193  size_t length = 49;
7194  unsigned char key[32];
7195  unsigned char iv[16];
7196 
7197  const cipher_info_t *cipher_info;
7198  cipher_context_t ctx_dec;
7199  cipher_context_t ctx_enc;
7200 
7201  unsigned char inbuf[64];
7202  unsigned char encbuf[64];
7203  unsigned char decbuf[64];
7204 
7205  size_t outlen = 0;
7206  size_t enclen = 0;
7207 
7208  memset( key, 0, 32 );
7209  memset( iv , 0, 16 );
7210 
7211  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7212  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7213 
7214  memset( inbuf, 5, 64 );
7215  memset( encbuf, 0, 64 );
7216  memset( decbuf, 0, 64 );
7217 
7218  /* Check and get info structures */
7220  fct_chk( NULL != cipher_info );
7221  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
7222 
7223  /* Initialise enc and dec contexts */
7224  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7225  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7226 
7227  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7228  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7229 
7230  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7231  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7232 
7233  if( POLARSSL_MODE_CBC == cipher_info->mode )
7234  {
7235  enclen = cipher_get_block_size( &ctx_enc )
7236  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7237  }
7238  else
7239  {
7240  enclen = length;
7241  }
7242 
7243  /* encode length number of bytes from inbuf */
7244  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
7245  if( POLARSSL_MODE_CBC == cipher_info->mode )
7246  {
7247  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7248  }
7249  else
7250  {
7251  fct_chk( outlen == enclen );
7252  }
7253 
7254  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
7255  if( POLARSSL_MODE_CBC == cipher_info->mode )
7256  {
7257  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7258  }
7259  else
7260  {
7261  fct_chk( outlen == 0 );
7262  }
7263 
7264 
7265  /* decode the previously encoded string */
7266  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7267  if( POLARSSL_MODE_CBC == cipher_info->mode )
7268  {
7269  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7270  }
7271  else
7272  {
7273  fct_chk( enclen == outlen );
7274  }
7275 
7276  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7277  if( POLARSSL_MODE_CBC == cipher_info->mode )
7278  {
7279  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7280  }
7281  else
7282  {
7283  fct_chk( outlen == 0 );
7284  }
7285 
7286 
7287  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7288 
7289  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7290  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7291  FCT_TEST_END();
7292 #endif /* POLARSSL_DES_C */
7293 
7294 #ifdef POLARSSL_DES_C
7295 
7296  FCT_TEST_BGN(des3_encrypt_and_decrypt_0_bytes_in_multiple_parts)
7297  size_t first_length = 0;
7298  size_t second_length = 0;
7299  size_t length = first_length + second_length;
7300  unsigned char key[32];
7301  unsigned char iv[16];
7302 
7303  cipher_context_t ctx_dec;
7304  cipher_context_t ctx_enc;
7305  const cipher_info_t *cipher_info;
7306 
7307  unsigned char inbuf[64];
7308  unsigned char encbuf[64];
7309  unsigned char decbuf[64];
7310 
7311  size_t outlen = 0;
7312  size_t totaloutlen = 0;
7313  size_t enclen = 0;
7314 
7315  memset( key, 0, 32 );
7316  memset( iv , 0, 16 );
7317 
7318  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7319  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7320 
7321  memset( inbuf, 5, 64 );
7322  memset( encbuf, 0, 64 );
7323  memset( decbuf, 0, 64 );
7324 
7325  /* Initialise enc and dec contexts */
7327  fct_chk( NULL != cipher_info);
7328 
7329  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7330  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7331 
7332  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7333  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7334 
7335  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7336  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7337 
7338  if( POLARSSL_MODE_CBC == cipher_info->mode )
7339  {
7340  enclen = cipher_get_block_size(&ctx_enc )
7341  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7342  }
7343  else
7344  {
7345  enclen = length;
7346  }
7347 
7348  /* encode length number of bytes from inbuf */
7349  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7350  totaloutlen = outlen;
7351  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7352  totaloutlen += outlen;
7353  if( POLARSSL_MODE_CBC == cipher_info->mode )
7354  {
7355  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7356  }
7357  else
7358  {
7359  fct_chk( totaloutlen == enclen );
7360  }
7361  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7362  totaloutlen += outlen;
7363  if( POLARSSL_MODE_CBC == cipher_info->mode )
7364  {
7365  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7366  }
7367  else
7368  {
7369  fct_chk( outlen == 0 );
7370  }
7371 
7372  /* decode the previously encoded string */
7373  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7374  if( POLARSSL_MODE_CBC == cipher_info->mode )
7375  {
7376  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7377  }
7378  else
7379  {
7380  fct_chk( enclen == outlen );
7381  }
7382  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7383  if( POLARSSL_MODE_CBC == cipher_info->mode )
7384  {
7385  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7386  }
7387  else
7388  {
7389  fct_chk( outlen == 0 );
7390  }
7391 
7392 
7393  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7394 
7395  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7396  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7397  FCT_TEST_END();
7398 #endif /* POLARSSL_DES_C */
7399 
7400 #ifdef POLARSSL_DES_C
7401 
7402  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
7403  size_t first_length = 1;
7404  size_t second_length = 0;
7405  size_t length = first_length + second_length;
7406  unsigned char key[32];
7407  unsigned char iv[16];
7408 
7409  cipher_context_t ctx_dec;
7410  cipher_context_t ctx_enc;
7411  const cipher_info_t *cipher_info;
7412 
7413  unsigned char inbuf[64];
7414  unsigned char encbuf[64];
7415  unsigned char decbuf[64];
7416 
7417  size_t outlen = 0;
7418  size_t totaloutlen = 0;
7419  size_t enclen = 0;
7420 
7421  memset( key, 0, 32 );
7422  memset( iv , 0, 16 );
7423 
7424  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7425  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7426 
7427  memset( inbuf, 5, 64 );
7428  memset( encbuf, 0, 64 );
7429  memset( decbuf, 0, 64 );
7430 
7431  /* Initialise enc and dec contexts */
7433  fct_chk( NULL != cipher_info);
7434 
7435  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7436  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7437 
7438  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7439  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7440 
7441  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7442  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7443 
7444  if( POLARSSL_MODE_CBC == cipher_info->mode )
7445  {
7446  enclen = cipher_get_block_size(&ctx_enc )
7447  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7448  }
7449  else
7450  {
7451  enclen = length;
7452  }
7453 
7454  /* encode length number of bytes from inbuf */
7455  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7456  totaloutlen = outlen;
7457  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7458  totaloutlen += outlen;
7459  if( POLARSSL_MODE_CBC == cipher_info->mode )
7460  {
7461  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7462  }
7463  else
7464  {
7465  fct_chk( totaloutlen == enclen );
7466  }
7467  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7468  totaloutlen += outlen;
7469  if( POLARSSL_MODE_CBC == cipher_info->mode )
7470  {
7471  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7472  }
7473  else
7474  {
7475  fct_chk( outlen == 0 );
7476  }
7477 
7478  /* decode the previously encoded string */
7479  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7480  if( POLARSSL_MODE_CBC == cipher_info->mode )
7481  {
7482  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7483  }
7484  else
7485  {
7486  fct_chk( enclen == outlen );
7487  }
7488  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7489  if( POLARSSL_MODE_CBC == cipher_info->mode )
7490  {
7491  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7492  }
7493  else
7494  {
7495  fct_chk( outlen == 0 );
7496  }
7497 
7498 
7499  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7500 
7501  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7502  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7503  FCT_TEST_END();
7504 #endif /* POLARSSL_DES_C */
7505 
7506 #ifdef POLARSSL_DES_C
7507 
7508  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
7509  size_t first_length = 0;
7510  size_t second_length = 1;
7511  size_t length = first_length + second_length;
7512  unsigned char key[32];
7513  unsigned char iv[16];
7514 
7515  cipher_context_t ctx_dec;
7516  cipher_context_t ctx_enc;
7517  const cipher_info_t *cipher_info;
7518 
7519  unsigned char inbuf[64];
7520  unsigned char encbuf[64];
7521  unsigned char decbuf[64];
7522 
7523  size_t outlen = 0;
7524  size_t totaloutlen = 0;
7525  size_t enclen = 0;
7526 
7527  memset( key, 0, 32 );
7528  memset( iv , 0, 16 );
7529 
7530  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7531  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7532 
7533  memset( inbuf, 5, 64 );
7534  memset( encbuf, 0, 64 );
7535  memset( decbuf, 0, 64 );
7536 
7537  /* Initialise enc and dec contexts */
7539  fct_chk( NULL != cipher_info);
7540 
7541  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7542  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7543 
7544  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7545  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7546 
7547  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7548  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7549 
7550  if( POLARSSL_MODE_CBC == cipher_info->mode )
7551  {
7552  enclen = cipher_get_block_size(&ctx_enc )
7553  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7554  }
7555  else
7556  {
7557  enclen = length;
7558  }
7559 
7560  /* encode length number of bytes from inbuf */
7561  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7562  totaloutlen = outlen;
7563  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7564  totaloutlen += outlen;
7565  if( POLARSSL_MODE_CBC == cipher_info->mode )
7566  {
7567  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7568  }
7569  else
7570  {
7571  fct_chk( totaloutlen == enclen );
7572  }
7573  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7574  totaloutlen += outlen;
7575  if( POLARSSL_MODE_CBC == cipher_info->mode )
7576  {
7577  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7578  }
7579  else
7580  {
7581  fct_chk( outlen == 0 );
7582  }
7583 
7584  /* decode the previously encoded string */
7585  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7586  if( POLARSSL_MODE_CBC == cipher_info->mode )
7587  {
7588  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7589  }
7590  else
7591  {
7592  fct_chk( enclen == outlen );
7593  }
7594  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7595  if( POLARSSL_MODE_CBC == cipher_info->mode )
7596  {
7597  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7598  }
7599  else
7600  {
7601  fct_chk( outlen == 0 );
7602  }
7603 
7604 
7605  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7606 
7607  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7608  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7609  FCT_TEST_END();
7610 #endif /* POLARSSL_DES_C */
7611 
7612 #ifdef POLARSSL_DES_C
7613 
7614  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
7615  size_t first_length = 16;
7616  size_t second_length = 0;
7617  size_t length = first_length + second_length;
7618  unsigned char key[32];
7619  unsigned char iv[16];
7620 
7621  cipher_context_t ctx_dec;
7622  cipher_context_t ctx_enc;
7623  const cipher_info_t *cipher_info;
7624 
7625  unsigned char inbuf[64];
7626  unsigned char encbuf[64];
7627  unsigned char decbuf[64];
7628 
7629  size_t outlen = 0;
7630  size_t totaloutlen = 0;
7631  size_t enclen = 0;
7632 
7633  memset( key, 0, 32 );
7634  memset( iv , 0, 16 );
7635 
7636  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7637  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7638 
7639  memset( inbuf, 5, 64 );
7640  memset( encbuf, 0, 64 );
7641  memset( decbuf, 0, 64 );
7642 
7643  /* Initialise enc and dec contexts */
7645  fct_chk( NULL != cipher_info);
7646 
7647  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7648  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7649 
7650  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7651  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7652 
7653  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7654  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7655 
7656  if( POLARSSL_MODE_CBC == cipher_info->mode )
7657  {
7658  enclen = cipher_get_block_size(&ctx_enc )
7659  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7660  }
7661  else
7662  {
7663  enclen = length;
7664  }
7665 
7666  /* encode length number of bytes from inbuf */
7667  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7668  totaloutlen = outlen;
7669  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7670  totaloutlen += outlen;
7671  if( POLARSSL_MODE_CBC == cipher_info->mode )
7672  {
7673  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7674  }
7675  else
7676  {
7677  fct_chk( totaloutlen == enclen );
7678  }
7679  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7680  totaloutlen += outlen;
7681  if( POLARSSL_MODE_CBC == cipher_info->mode )
7682  {
7683  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7684  }
7685  else
7686  {
7687  fct_chk( outlen == 0 );
7688  }
7689 
7690  /* decode the previously encoded string */
7691  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7692  if( POLARSSL_MODE_CBC == cipher_info->mode )
7693  {
7694  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7695  }
7696  else
7697  {
7698  fct_chk( enclen == outlen );
7699  }
7700  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7701  if( POLARSSL_MODE_CBC == cipher_info->mode )
7702  {
7703  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7704  }
7705  else
7706  {
7707  fct_chk( outlen == 0 );
7708  }
7709 
7710 
7711  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7712 
7713  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7714  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7715  FCT_TEST_END();
7716 #endif /* POLARSSL_DES_C */
7717 
7718 #ifdef POLARSSL_DES_C
7719 
7720  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
7721  size_t first_length = 0;
7722  size_t second_length = 16;
7723  size_t length = first_length + second_length;
7724  unsigned char key[32];
7725  unsigned char iv[16];
7726 
7727  cipher_context_t ctx_dec;
7728  cipher_context_t ctx_enc;
7729  const cipher_info_t *cipher_info;
7730 
7731  unsigned char inbuf[64];
7732  unsigned char encbuf[64];
7733  unsigned char decbuf[64];
7734 
7735  size_t outlen = 0;
7736  size_t totaloutlen = 0;
7737  size_t enclen = 0;
7738 
7739  memset( key, 0, 32 );
7740  memset( iv , 0, 16 );
7741 
7742  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7743  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7744 
7745  memset( inbuf, 5, 64 );
7746  memset( encbuf, 0, 64 );
7747  memset( decbuf, 0, 64 );
7748 
7749  /* Initialise enc and dec contexts */
7751  fct_chk( NULL != cipher_info);
7752 
7753  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7754  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7755 
7756  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7757  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7758 
7759  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7760  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7761 
7762  if( POLARSSL_MODE_CBC == cipher_info->mode )
7763  {
7764  enclen = cipher_get_block_size(&ctx_enc )
7765  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7766  }
7767  else
7768  {
7769  enclen = length;
7770  }
7771 
7772  /* encode length number of bytes from inbuf */
7773  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7774  totaloutlen = outlen;
7775  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7776  totaloutlen += outlen;
7777  if( POLARSSL_MODE_CBC == cipher_info->mode )
7778  {
7779  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7780  }
7781  else
7782  {
7783  fct_chk( totaloutlen == enclen );
7784  }
7785  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7786  totaloutlen += outlen;
7787  if( POLARSSL_MODE_CBC == cipher_info->mode )
7788  {
7789  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7790  }
7791  else
7792  {
7793  fct_chk( outlen == 0 );
7794  }
7795 
7796  /* decode the previously encoded string */
7797  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7798  if( POLARSSL_MODE_CBC == cipher_info->mode )
7799  {
7800  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7801  }
7802  else
7803  {
7804  fct_chk( enclen == outlen );
7805  }
7806  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7807  if( POLARSSL_MODE_CBC == cipher_info->mode )
7808  {
7809  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7810  }
7811  else
7812  {
7813  fct_chk( outlen == 0 );
7814  }
7815 
7816 
7817  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7818 
7819  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7820  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7821  FCT_TEST_END();
7822 #endif /* POLARSSL_DES_C */
7823 
7824 #ifdef POLARSSL_DES_C
7825 
7826  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
7827  size_t first_length = 1;
7828  size_t second_length = 15;
7829  size_t length = first_length + second_length;
7830  unsigned char key[32];
7831  unsigned char iv[16];
7832 
7833  cipher_context_t ctx_dec;
7834  cipher_context_t ctx_enc;
7835  const cipher_info_t *cipher_info;
7836 
7837  unsigned char inbuf[64];
7838  unsigned char encbuf[64];
7839  unsigned char decbuf[64];
7840 
7841  size_t outlen = 0;
7842  size_t totaloutlen = 0;
7843  size_t enclen = 0;
7844 
7845  memset( key, 0, 32 );
7846  memset( iv , 0, 16 );
7847 
7848  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7849  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7850 
7851  memset( inbuf, 5, 64 );
7852  memset( encbuf, 0, 64 );
7853  memset( decbuf, 0, 64 );
7854 
7855  /* Initialise enc and dec contexts */
7857  fct_chk( NULL != cipher_info);
7858 
7859  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7860  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7861 
7862  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7863  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7864 
7865  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7866  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7867 
7868  if( POLARSSL_MODE_CBC == cipher_info->mode )
7869  {
7870  enclen = cipher_get_block_size(&ctx_enc )
7871  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7872  }
7873  else
7874  {
7875  enclen = length;
7876  }
7877 
7878  /* encode length number of bytes from inbuf */
7879  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7880  totaloutlen = outlen;
7881  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7882  totaloutlen += outlen;
7883  if( POLARSSL_MODE_CBC == cipher_info->mode )
7884  {
7885  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7886  }
7887  else
7888  {
7889  fct_chk( totaloutlen == enclen );
7890  }
7891  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7892  totaloutlen += outlen;
7893  if( POLARSSL_MODE_CBC == cipher_info->mode )
7894  {
7895  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7896  }
7897  else
7898  {
7899  fct_chk( outlen == 0 );
7900  }
7901 
7902  /* decode the previously encoded string */
7903  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7904  if( POLARSSL_MODE_CBC == cipher_info->mode )
7905  {
7906  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7907  }
7908  else
7909  {
7910  fct_chk( enclen == outlen );
7911  }
7912  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7913  if( POLARSSL_MODE_CBC == cipher_info->mode )
7914  {
7915  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7916  }
7917  else
7918  {
7919  fct_chk( outlen == 0 );
7920  }
7921 
7922 
7923  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7924 
7925  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7926  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7927  FCT_TEST_END();
7928 #endif /* POLARSSL_DES_C */
7929 
7930 #ifdef POLARSSL_DES_C
7931 
7932  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
7933  size_t first_length = 15;
7934  size_t second_length = 1;
7935  size_t length = first_length + second_length;
7936  unsigned char key[32];
7937  unsigned char iv[16];
7938 
7939  cipher_context_t ctx_dec;
7940  cipher_context_t ctx_enc;
7941  const cipher_info_t *cipher_info;
7942 
7943  unsigned char inbuf[64];
7944  unsigned char encbuf[64];
7945  unsigned char decbuf[64];
7946 
7947  size_t outlen = 0;
7948  size_t totaloutlen = 0;
7949  size_t enclen = 0;
7950 
7951  memset( key, 0, 32 );
7952  memset( iv , 0, 16 );
7953 
7954  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7955  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7956 
7957  memset( inbuf, 5, 64 );
7958  memset( encbuf, 0, 64 );
7959  memset( decbuf, 0, 64 );
7960 
7961  /* Initialise enc and dec contexts */
7963  fct_chk( NULL != cipher_info);
7964 
7965  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7966  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7967 
7968  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7969  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7970 
7971  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7972  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7973 
7974  if( POLARSSL_MODE_CBC == cipher_info->mode )
7975  {
7976  enclen = cipher_get_block_size(&ctx_enc )
7977  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7978  }
7979  else
7980  {
7981  enclen = length;
7982  }
7983 
7984  /* encode length number of bytes from inbuf */
7985  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7986  totaloutlen = outlen;
7987  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7988  totaloutlen += outlen;
7989  if( POLARSSL_MODE_CBC == cipher_info->mode )
7990  {
7991  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7992  }
7993  else
7994  {
7995  fct_chk( totaloutlen == enclen );
7996  }
7997  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7998  totaloutlen += outlen;
7999  if( POLARSSL_MODE_CBC == cipher_info->mode )
8000  {
8001  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8002  }
8003  else
8004  {
8005  fct_chk( outlen == 0 );
8006  }
8007 
8008  /* decode the previously encoded string */
8009  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8010  if( POLARSSL_MODE_CBC == cipher_info->mode )
8011  {
8012  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8013  }
8014  else
8015  {
8016  fct_chk( enclen == outlen );
8017  }
8018  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8019  if( POLARSSL_MODE_CBC == cipher_info->mode )
8020  {
8021  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8022  }
8023  else
8024  {
8025  fct_chk( outlen == 0 );
8026  }
8027 
8028 
8029  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8030 
8031  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8032  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8033  FCT_TEST_END();
8034 #endif /* POLARSSL_DES_C */
8035 
8036 #ifdef POLARSSL_DES_C
8037 
8038  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
8039  size_t first_length = 15;
8040  size_t second_length = 7;
8041  size_t length = first_length + second_length;
8042  unsigned char key[32];
8043  unsigned char iv[16];
8044 
8045  cipher_context_t ctx_dec;
8046  cipher_context_t ctx_enc;
8047  const cipher_info_t *cipher_info;
8048 
8049  unsigned char inbuf[64];
8050  unsigned char encbuf[64];
8051  unsigned char decbuf[64];
8052 
8053  size_t outlen = 0;
8054  size_t totaloutlen = 0;
8055  size_t enclen = 0;
8056 
8057  memset( key, 0, 32 );
8058  memset( iv , 0, 16 );
8059 
8060  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8061  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8062 
8063  memset( inbuf, 5, 64 );
8064  memset( encbuf, 0, 64 );
8065  memset( decbuf, 0, 64 );
8066 
8067  /* Initialise enc and dec contexts */
8069  fct_chk( NULL != cipher_info);
8070 
8071  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8072  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8073 
8074  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
8075  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
8076 
8077  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8078  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8079 
8080  if( POLARSSL_MODE_CBC == cipher_info->mode )
8081  {
8082  enclen = cipher_get_block_size(&ctx_enc )
8083  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8084  }
8085  else
8086  {
8087  enclen = length;
8088  }
8089 
8090  /* encode length number of bytes from inbuf */
8091  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8092  totaloutlen = outlen;
8093  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8094  totaloutlen += outlen;
8095  if( POLARSSL_MODE_CBC == cipher_info->mode )
8096  {
8097  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8098  }
8099  else
8100  {
8101  fct_chk( totaloutlen == enclen );
8102  }
8103  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8104  totaloutlen += outlen;
8105  if( POLARSSL_MODE_CBC == cipher_info->mode )
8106  {
8107  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8108  }
8109  else
8110  {
8111  fct_chk( outlen == 0 );
8112  }
8113 
8114  /* decode the previously encoded string */
8115  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8116  if( POLARSSL_MODE_CBC == cipher_info->mode )
8117  {
8118  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8119  }
8120  else
8121  {
8122  fct_chk( enclen == outlen );
8123  }
8124  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8125  if( POLARSSL_MODE_CBC == cipher_info->mode )
8126  {
8127  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8128  }
8129  else
8130  {
8131  fct_chk( outlen == 0 );
8132  }
8133 
8134 
8135  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8136 
8137  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8138  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8139  FCT_TEST_END();
8140 #endif /* POLARSSL_DES_C */
8141 
8142 #ifdef POLARSSL_DES_C
8143 
8144  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
8145  size_t first_length = 16;
8146  size_t second_length = 6;
8147  size_t length = first_length + second_length;
8148  unsigned char key[32];
8149  unsigned char iv[16];
8150 
8151  cipher_context_t ctx_dec;
8152  cipher_context_t ctx_enc;
8153  const cipher_info_t *cipher_info;
8154 
8155  unsigned char inbuf[64];
8156  unsigned char encbuf[64];
8157  unsigned char decbuf[64];
8158 
8159  size_t outlen = 0;
8160  size_t totaloutlen = 0;
8161  size_t enclen = 0;
8162 
8163  memset( key, 0, 32 );
8164  memset( iv , 0, 16 );
8165 
8166  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8167  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8168 
8169  memset( inbuf, 5, 64 );
8170  memset( encbuf, 0, 64 );
8171  memset( decbuf, 0, 64 );
8172 
8173  /* Initialise enc and dec contexts */
8175  fct_chk( NULL != cipher_info);
8176 
8177  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8178  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8179 
8180  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
8181  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
8182 
8183  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8184  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8185 
8186  if( POLARSSL_MODE_CBC == cipher_info->mode )
8187  {
8188  enclen = cipher_get_block_size(&ctx_enc )
8189  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8190  }
8191  else
8192  {
8193  enclen = length;
8194  }
8195 
8196  /* encode length number of bytes from inbuf */
8197  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8198  totaloutlen = outlen;
8199  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8200  totaloutlen += outlen;
8201  if( POLARSSL_MODE_CBC == cipher_info->mode )
8202  {
8203  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8204  }
8205  else
8206  {
8207  fct_chk( totaloutlen == enclen );
8208  }
8209  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8210  totaloutlen += outlen;
8211  if( POLARSSL_MODE_CBC == cipher_info->mode )
8212  {
8213  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8214  }
8215  else
8216  {
8217  fct_chk( outlen == 0 );
8218  }
8219 
8220  /* decode the previously encoded string */
8221  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8222  if( POLARSSL_MODE_CBC == cipher_info->mode )
8223  {
8224  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8225  }
8226  else
8227  {
8228  fct_chk( enclen == outlen );
8229  }
8230  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8231  if( POLARSSL_MODE_CBC == cipher_info->mode )
8232  {
8233  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8234  }
8235  else
8236  {
8237  fct_chk( outlen == 0 );
8238  }
8239 
8240 
8241  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8242 
8243  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8244  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8245  FCT_TEST_END();
8246 #endif /* POLARSSL_DES_C */
8247 
8248 #ifdef POLARSSL_DES_C
8249 
8250  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
8251  size_t first_length = 17;
8252  size_t second_length = 6;
8253  size_t length = first_length + second_length;
8254  unsigned char key[32];
8255  unsigned char iv[16];
8256 
8257  cipher_context_t ctx_dec;
8258  cipher_context_t ctx_enc;
8259  const cipher_info_t *cipher_info;
8260 
8261  unsigned char inbuf[64];
8262  unsigned char encbuf[64];
8263  unsigned char decbuf[64];
8264 
8265  size_t outlen = 0;
8266  size_t totaloutlen = 0;
8267  size_t enclen = 0;
8268 
8269  memset( key, 0, 32 );
8270  memset( iv , 0, 16 );
8271 
8272  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8273  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8274 
8275  memset( inbuf, 5, 64 );
8276  memset( encbuf, 0, 64 );
8277  memset( decbuf, 0, 64 );
8278 
8279  /* Initialise enc and dec contexts */
8281  fct_chk( NULL != cipher_info);
8282 
8283  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8284  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8285 
8286  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
8287  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
8288 
8289  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8290  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8291 
8292  if( POLARSSL_MODE_CBC == cipher_info->mode )
8293  {
8294  enclen = cipher_get_block_size(&ctx_enc )
8295  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8296  }
8297  else
8298  {
8299  enclen = length;
8300  }
8301 
8302  /* encode length number of bytes from inbuf */
8303  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8304  totaloutlen = outlen;
8305  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8306  totaloutlen += outlen;
8307  if( POLARSSL_MODE_CBC == cipher_info->mode )
8308  {
8309  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8310  }
8311  else
8312  {
8313  fct_chk( totaloutlen == enclen );
8314  }
8315  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8316  totaloutlen += outlen;
8317  if( POLARSSL_MODE_CBC == cipher_info->mode )
8318  {
8319  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8320  }
8321  else
8322  {
8323  fct_chk( outlen == 0 );
8324  }
8325 
8326  /* decode the previously encoded string */
8327  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8328  if( POLARSSL_MODE_CBC == cipher_info->mode )
8329  {
8330  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8331  }
8332  else
8333  {
8334  fct_chk( enclen == outlen );
8335  }
8336  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8337  if( POLARSSL_MODE_CBC == cipher_info->mode )
8338  {
8339  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8340  }
8341  else
8342  {
8343  fct_chk( outlen == 0 );
8344  }
8345 
8346 
8347  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8348 
8349  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8350  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8351  FCT_TEST_END();
8352 #endif /* POLARSSL_DES_C */
8353 
8354 #ifdef POLARSSL_DES_C
8355 
8356  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
8357  size_t first_length = 16;
8358  size_t second_length = 16;
8359  size_t length = first_length + second_length;
8360  unsigned char key[32];
8361  unsigned char iv[16];
8362 
8363  cipher_context_t ctx_dec;
8364  cipher_context_t ctx_enc;
8365  const cipher_info_t *cipher_info;
8366 
8367  unsigned char inbuf[64];
8368  unsigned char encbuf[64];
8369  unsigned char decbuf[64];
8370 
8371  size_t outlen = 0;
8372  size_t totaloutlen = 0;
8373  size_t enclen = 0;
8374 
8375  memset( key, 0, 32 );
8376  memset( iv , 0, 16 );
8377 
8378  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8379  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8380 
8381  memset( inbuf, 5, 64 );
8382  memset( encbuf, 0, 64 );
8383  memset( decbuf, 0, 64 );
8384 
8385  /* Initialise enc and dec contexts */
8387  fct_chk( NULL != cipher_info);
8388 
8389  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8390  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8391 
8392  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
8393  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
8394 
8395  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8396  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8397 
8398  if( POLARSSL_MODE_CBC == cipher_info->mode )
8399  {
8400  enclen = cipher_get_block_size(&ctx_enc )
8401  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8402  }
8403  else
8404  {
8405  enclen = length;
8406  }
8407 
8408  /* encode length number of bytes from inbuf */
8409  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8410  totaloutlen = outlen;
8411  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8412  totaloutlen += outlen;
8413  if( POLARSSL_MODE_CBC == cipher_info->mode )
8414  {
8415  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8416  }
8417  else
8418  {
8419  fct_chk( totaloutlen == enclen );
8420  }
8421  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8422  totaloutlen += outlen;
8423  if( POLARSSL_MODE_CBC == cipher_info->mode )
8424  {
8425  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8426  }
8427  else
8428  {
8429  fct_chk( outlen == 0 );
8430  }
8431 
8432  /* decode the previously encoded string */
8433  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8434  if( POLARSSL_MODE_CBC == cipher_info->mode )
8435  {
8436  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8437  }
8438  else
8439  {
8440  fct_chk( enclen == outlen );
8441  }
8442  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8443  if( POLARSSL_MODE_CBC == cipher_info->mode )
8444  {
8445  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8446  }
8447  else
8448  {
8449  fct_chk( outlen == 0 );
8450  }
8451 
8452 
8453  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8454 
8455  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8456  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8457  FCT_TEST_END();
8458 #endif /* POLARSSL_DES_C */
8459 
8460  }
8461  FCT_SUITE_END();
8462 
8463 #endif /* POLARSSL_CIPHER_C */
8464 
8465 }
8466 FCT_END();
8467