PolarSSL v1.2.5
test_suite_cipher.des.c
Go to the documentation of this file.
1 #include "fct.h"
2 
3 #include <polarssl/cipher.h>
4 
5 #include <polarssl/config.h>
6 
7 #ifdef _MSC_VER
8 #include <basetsd.h>
9 typedef UINT32 uint32_t;
10 #else
11 #include <inttypes.h>
12 #endif
13 
14 /*
15  * 32-bit integer manipulation macros (big endian)
16  */
17 #ifndef GET_UINT32_BE
18 #define GET_UINT32_BE(n,b,i) \
19 { \
20  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
21  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
22  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
23  | ( (uint32_t) (b)[(i) + 3] ); \
24 }
25 #endif
26 
27 #ifndef PUT_UINT32_BE
28 #define PUT_UINT32_BE(n,b,i) \
29 { \
30  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
31  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
32  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
33  (b)[(i) + 3] = (unsigned char) ( (n) ); \
34 }
35 #endif
36 
37 int unhexify(unsigned char *obuf, const char *ibuf)
38 {
39  unsigned char c, c2;
40  int len = strlen(ibuf) / 2;
41  assert(!(strlen(ibuf) %1)); // must be even number of bytes
42 
43  while (*ibuf != 0)
44  {
45  c = *ibuf++;
46  if( c >= '0' && c <= '9' )
47  c -= '0';
48  else if( c >= 'a' && c <= 'f' )
49  c -= 'a' - 10;
50  else if( c >= 'A' && c <= 'F' )
51  c -= 'A' - 10;
52  else
53  assert( 0 );
54 
55  c2 = *ibuf++;
56  if( c2 >= '0' && c2 <= '9' )
57  c2 -= '0';
58  else if( c2 >= 'a' && c2 <= 'f' )
59  c2 -= 'a' - 10;
60  else if( c2 >= 'A' && c2 <= 'F' )
61  c2 -= 'A' - 10;
62  else
63  assert( 0 );
64 
65  *obuf++ = ( c << 4 ) | c2;
66  }
67 
68  return len;
69 }
70 
71 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
72 {
73  unsigned char l, h;
74 
75  while (len != 0)
76  {
77  h = (*ibuf) / 16;
78  l = (*ibuf) % 16;
79 
80  if( h < 10 )
81  *obuf++ = '0' + h;
82  else
83  *obuf++ = 'a' + h - 10;
84 
85  if( l < 10 )
86  *obuf++ = '0' + l;
87  else
88  *obuf++ = 'a' + l - 10;
89 
90  ++ibuf;
91  len--;
92  }
93 }
94 
104 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
105 {
106  size_t i;
107 
108  if( rng_state != NULL )
109  rng_state = NULL;
110 
111  for( i = 0; i < len; ++i )
112  output[i] = rand();
113 
114  return( 0 );
115 }
116 
122 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
123 {
124  if( rng_state != NULL )
125  rng_state = NULL;
126 
127  memset( output, 0, len );
128 
129  return( 0 );
130 }
131 
132 typedef struct
133 {
134  unsigned char *buf;
135  size_t length;
136 } rnd_buf_info;
137 
149 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
150 {
151  rnd_buf_info *info = (rnd_buf_info *) rng_state;
152  size_t use_len;
153 
154  if( rng_state == NULL )
155  return( rnd_std_rand( NULL, output, len ) );
156 
157  use_len = len;
158  if( len > info->length )
159  use_len = info->length;
160 
161  if( use_len )
162  {
163  memcpy( output, info->buf, use_len );
164  info->buf += use_len;
165  info->length -= use_len;
166  }
167 
168  if( len - use_len > 0 )
169  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
170 
171  return( 0 );
172 }
173 
181 typedef struct
182 {
183  uint32_t key[16];
184  uint32_t v0, v1;
186 
195 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
196 {
197  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
198  uint32_t i, *k, sum, delta=0x9E3779B9;
199  unsigned char result[4];
200 
201  if( rng_state == NULL )
202  return( rnd_std_rand( NULL, output, len ) );
203 
204  k = info->key;
205 
206  while( len > 0 )
207  {
208  size_t use_len = ( len > 4 ) ? 4 : len;
209  sum = 0;
210 
211  for( i = 0; i < 32; i++ )
212  {
213  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
214  sum += delta;
215  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
216  }
217 
218  PUT_UINT32_BE( info->v0, result, 0 );
219  memcpy( output, result, use_len );
220  len -= use_len;
221  }
222 
223  return( 0 );
224 }
225 
226 
228 {
229 #ifdef POLARSSL_CIPHER_C
230 
231 
232  FCT_SUITE_BGN(test_suite_cipher)
233  {
234 #ifdef POLARSSL_SELF_TEST
235 
236  FCT_TEST_BGN(cipher_selftest)
237  {
238  fct_chk( cipher_self_test( 0 ) == 0 );
239  }
240  FCT_TEST_END();
241 #endif /* POLARSSL_SELF_TEST */
242 
243 
244  FCT_TEST_BGN(decrypt_empty_buffer)
245  unsigned char key[32];
246  unsigned char iv[16];
247 
248  cipher_context_t ctx_dec;
249  const cipher_info_t *cipher_info;
250 
251  unsigned char encbuf[64];
252  unsigned char decbuf[64];
253 
254  size_t outlen = 0;
255 
256  memset( key, 0, 32 );
257  memset( iv , 0, 16 );
258 
259  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
260 
261  memset( encbuf, 0, 64 );
262  memset( decbuf, 0, 64 );
263 
264  /* Initialise enc and dec contexts */
266  fct_chk( NULL != cipher_info);
267 
268  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
269 
270  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
271 
272  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
273 
274  /* decode 0-byte string */
275  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
276  fct_chk( 0 == outlen );
277  fct_chk( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
278  fct_chk( 0 == outlen );
279 
280  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
281  FCT_TEST_END();
282 
283 #ifdef POLARSSL_DES_C
284 
285  FCT_TEST_BGN(des_encrypt_and_decrypt_0_bytes)
286  size_t length = 0;
287  unsigned char key[32];
288  unsigned char iv[16];
289 
290  const cipher_info_t *cipher_info;
291  cipher_context_t ctx_dec;
292  cipher_context_t ctx_enc;
293 
294  unsigned char inbuf[64];
295  unsigned char encbuf[64];
296  unsigned char decbuf[64];
297 
298  size_t outlen = 0;
299  size_t enclen = 0;
300 
301  memset( key, 0, 32 );
302  memset( iv , 0, 16 );
303 
304  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
305  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
306 
307  memset( inbuf, 5, 64 );
308  memset( encbuf, 0, 64 );
309  memset( decbuf, 0, 64 );
310 
311  /* Check and get info structures */
313  fct_chk( NULL != cipher_info );
314  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
315 
316  /* Initialise enc and dec contexts */
317  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
318  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
319 
320  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
321  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
322 
323  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
324  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
325 
326  if( POLARSSL_MODE_CBC == cipher_info->mode )
327  {
328  enclen = cipher_get_block_size( &ctx_enc )
329  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
330  }
331  else
332  {
333  enclen = length;
334  }
335 
336  /* encode length number of bytes from inbuf */
337  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
338  if( POLARSSL_MODE_CBC == cipher_info->mode )
339  {
340  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
341  }
342  else
343  {
344  fct_chk( outlen == enclen );
345  }
346 
347  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
348  if( POLARSSL_MODE_CBC == cipher_info->mode )
349  {
350  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
351  }
352  else
353  {
354  fct_chk( outlen == 0 );
355  }
356 
357 
358  /* decode the previously encoded string */
359  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
360  if( POLARSSL_MODE_CBC == cipher_info->mode )
361  {
362  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
363  }
364  else
365  {
366  fct_chk( enclen == outlen );
367  }
368 
369  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
370  if( POLARSSL_MODE_CBC == cipher_info->mode )
371  {
372  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
373  }
374  else
375  {
376  fct_chk( outlen == 0 );
377  }
378 
379 
380  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
381 
382  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
383  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
384  FCT_TEST_END();
385 #endif /* POLARSSL_DES_C */
386 
387 #ifdef POLARSSL_DES_C
388 
389  FCT_TEST_BGN(des_encrypt_and_decrypt_1_byte)
390  size_t length = 1;
391  unsigned char key[32];
392  unsigned char iv[16];
393 
394  const cipher_info_t *cipher_info;
395  cipher_context_t ctx_dec;
396  cipher_context_t ctx_enc;
397 
398  unsigned char inbuf[64];
399  unsigned char encbuf[64];
400  unsigned char decbuf[64];
401 
402  size_t outlen = 0;
403  size_t enclen = 0;
404 
405  memset( key, 0, 32 );
406  memset( iv , 0, 16 );
407 
408  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
409  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
410 
411  memset( inbuf, 5, 64 );
412  memset( encbuf, 0, 64 );
413  memset( decbuf, 0, 64 );
414 
415  /* Check and get info structures */
417  fct_chk( NULL != cipher_info );
418  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
419 
420  /* Initialise enc and dec contexts */
421  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
422  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
423 
424  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
425  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
426 
427  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
428  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
429 
430  if( POLARSSL_MODE_CBC == cipher_info->mode )
431  {
432  enclen = cipher_get_block_size( &ctx_enc )
433  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
434  }
435  else
436  {
437  enclen = length;
438  }
439 
440  /* encode length number of bytes from inbuf */
441  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
442  if( POLARSSL_MODE_CBC == cipher_info->mode )
443  {
444  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
445  }
446  else
447  {
448  fct_chk( outlen == enclen );
449  }
450 
451  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
452  if( POLARSSL_MODE_CBC == cipher_info->mode )
453  {
454  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
455  }
456  else
457  {
458  fct_chk( outlen == 0 );
459  }
460 
461 
462  /* decode the previously encoded string */
463  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
464  if( POLARSSL_MODE_CBC == cipher_info->mode )
465  {
466  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
467  }
468  else
469  {
470  fct_chk( enclen == outlen );
471  }
472 
473  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
474  if( POLARSSL_MODE_CBC == cipher_info->mode )
475  {
476  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
477  }
478  else
479  {
480  fct_chk( outlen == 0 );
481  }
482 
483 
484  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
485 
486  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
487  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
488  FCT_TEST_END();
489 #endif /* POLARSSL_DES_C */
490 
491 #ifdef POLARSSL_DES_C
492 
493  FCT_TEST_BGN(des_encrypt_and_decrypt_2_bytes)
494  size_t length = 2;
495  unsigned char key[32];
496  unsigned char iv[16];
497 
498  const cipher_info_t *cipher_info;
499  cipher_context_t ctx_dec;
500  cipher_context_t ctx_enc;
501 
502  unsigned char inbuf[64];
503  unsigned char encbuf[64];
504  unsigned char decbuf[64];
505 
506  size_t outlen = 0;
507  size_t enclen = 0;
508 
509  memset( key, 0, 32 );
510  memset( iv , 0, 16 );
511 
512  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
513  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
514 
515  memset( inbuf, 5, 64 );
516  memset( encbuf, 0, 64 );
517  memset( decbuf, 0, 64 );
518 
519  /* Check and get info structures */
521  fct_chk( NULL != cipher_info );
522  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
523 
524  /* Initialise enc and dec contexts */
525  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
526  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
527 
528  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
529  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
530 
531  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
532  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
533 
534  if( POLARSSL_MODE_CBC == cipher_info->mode )
535  {
536  enclen = cipher_get_block_size( &ctx_enc )
537  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
538  }
539  else
540  {
541  enclen = length;
542  }
543 
544  /* encode length number of bytes from inbuf */
545  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
546  if( POLARSSL_MODE_CBC == cipher_info->mode )
547  {
548  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
549  }
550  else
551  {
552  fct_chk( outlen == enclen );
553  }
554 
555  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
556  if( POLARSSL_MODE_CBC == cipher_info->mode )
557  {
558  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
559  }
560  else
561  {
562  fct_chk( outlen == 0 );
563  }
564 
565 
566  /* decode the previously encoded string */
567  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
568  if( POLARSSL_MODE_CBC == cipher_info->mode )
569  {
570  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
571  }
572  else
573  {
574  fct_chk( enclen == outlen );
575  }
576 
577  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
578  if( POLARSSL_MODE_CBC == cipher_info->mode )
579  {
580  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
581  }
582  else
583  {
584  fct_chk( outlen == 0 );
585  }
586 
587 
588  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
589 
590  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
591  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
592  FCT_TEST_END();
593 #endif /* POLARSSL_DES_C */
594 
595 #ifdef POLARSSL_DES_C
596 
597  FCT_TEST_BGN(des_encrypt_and_decrypt_7_bytes)
598  size_t length = 7;
599  unsigned char key[32];
600  unsigned char iv[16];
601 
602  const cipher_info_t *cipher_info;
603  cipher_context_t ctx_dec;
604  cipher_context_t ctx_enc;
605 
606  unsigned char inbuf[64];
607  unsigned char encbuf[64];
608  unsigned char decbuf[64];
609 
610  size_t outlen = 0;
611  size_t enclen = 0;
612 
613  memset( key, 0, 32 );
614  memset( iv , 0, 16 );
615 
616  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
617  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
618 
619  memset( inbuf, 5, 64 );
620  memset( encbuf, 0, 64 );
621  memset( decbuf, 0, 64 );
622 
623  /* Check and get info structures */
625  fct_chk( NULL != cipher_info );
626  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
627 
628  /* Initialise enc and dec contexts */
629  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
630  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
631 
632  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
633  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
634 
635  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
636  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
637 
638  if( POLARSSL_MODE_CBC == cipher_info->mode )
639  {
640  enclen = cipher_get_block_size( &ctx_enc )
641  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
642  }
643  else
644  {
645  enclen = length;
646  }
647 
648  /* encode length number of bytes from inbuf */
649  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
650  if( POLARSSL_MODE_CBC == cipher_info->mode )
651  {
652  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
653  }
654  else
655  {
656  fct_chk( outlen == enclen );
657  }
658 
659  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
660  if( POLARSSL_MODE_CBC == cipher_info->mode )
661  {
662  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
663  }
664  else
665  {
666  fct_chk( outlen == 0 );
667  }
668 
669 
670  /* decode the previously encoded string */
671  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
672  if( POLARSSL_MODE_CBC == cipher_info->mode )
673  {
674  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
675  }
676  else
677  {
678  fct_chk( enclen == outlen );
679  }
680 
681  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
682  if( POLARSSL_MODE_CBC == cipher_info->mode )
683  {
684  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
685  }
686  else
687  {
688  fct_chk( outlen == 0 );
689  }
690 
691 
692  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
693 
694  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
695  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
696  FCT_TEST_END();
697 #endif /* POLARSSL_DES_C */
698 
699 #ifdef POLARSSL_DES_C
700 
701  FCT_TEST_BGN(des_encrypt_and_decrypt_8_bytes)
702  size_t length = 8;
703  unsigned char key[32];
704  unsigned char iv[16];
705 
706  const cipher_info_t *cipher_info;
707  cipher_context_t ctx_dec;
708  cipher_context_t ctx_enc;
709 
710  unsigned char inbuf[64];
711  unsigned char encbuf[64];
712  unsigned char decbuf[64];
713 
714  size_t outlen = 0;
715  size_t enclen = 0;
716 
717  memset( key, 0, 32 );
718  memset( iv , 0, 16 );
719 
720  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
721  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
722 
723  memset( inbuf, 5, 64 );
724  memset( encbuf, 0, 64 );
725  memset( decbuf, 0, 64 );
726 
727  /* Check and get info structures */
729  fct_chk( NULL != cipher_info );
730  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
731 
732  /* Initialise enc and dec contexts */
733  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
734  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
735 
736  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
737  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
738 
739  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
740  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
741 
742  if( POLARSSL_MODE_CBC == cipher_info->mode )
743  {
744  enclen = cipher_get_block_size( &ctx_enc )
745  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
746  }
747  else
748  {
749  enclen = length;
750  }
751 
752  /* encode length number of bytes from inbuf */
753  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
754  if( POLARSSL_MODE_CBC == cipher_info->mode )
755  {
756  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
757  }
758  else
759  {
760  fct_chk( outlen == enclen );
761  }
762 
763  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
764  if( POLARSSL_MODE_CBC == cipher_info->mode )
765  {
766  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
767  }
768  else
769  {
770  fct_chk( outlen == 0 );
771  }
772 
773 
774  /* decode the previously encoded string */
775  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
776  if( POLARSSL_MODE_CBC == cipher_info->mode )
777  {
778  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
779  }
780  else
781  {
782  fct_chk( enclen == outlen );
783  }
784 
785  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
786  if( POLARSSL_MODE_CBC == cipher_info->mode )
787  {
788  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
789  }
790  else
791  {
792  fct_chk( outlen == 0 );
793  }
794 
795 
796  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
797 
798  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
799  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
800  FCT_TEST_END();
801 #endif /* POLARSSL_DES_C */
802 
803 #ifdef POLARSSL_DES_C
804 
805  FCT_TEST_BGN(des_encrypt_and_decrypt_9_bytes)
806  size_t length = 9;
807  unsigned char key[32];
808  unsigned char iv[16];
809 
810  const cipher_info_t *cipher_info;
811  cipher_context_t ctx_dec;
812  cipher_context_t ctx_enc;
813 
814  unsigned char inbuf[64];
815  unsigned char encbuf[64];
816  unsigned char decbuf[64];
817 
818  size_t outlen = 0;
819  size_t enclen = 0;
820 
821  memset( key, 0, 32 );
822  memset( iv , 0, 16 );
823 
824  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
825  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
826 
827  memset( inbuf, 5, 64 );
828  memset( encbuf, 0, 64 );
829  memset( decbuf, 0, 64 );
830 
831  /* Check and get info structures */
833  fct_chk( NULL != cipher_info );
834  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
835 
836  /* Initialise enc and dec contexts */
837  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
838  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
839 
840  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
841  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
842 
843  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
844  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
845 
846  if( POLARSSL_MODE_CBC == cipher_info->mode )
847  {
848  enclen = cipher_get_block_size( &ctx_enc )
849  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
850  }
851  else
852  {
853  enclen = length;
854  }
855 
856  /* encode length number of bytes from inbuf */
857  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
858  if( POLARSSL_MODE_CBC == cipher_info->mode )
859  {
860  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
861  }
862  else
863  {
864  fct_chk( outlen == enclen );
865  }
866 
867  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
868  if( POLARSSL_MODE_CBC == cipher_info->mode )
869  {
870  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
871  }
872  else
873  {
874  fct_chk( outlen == 0 );
875  }
876 
877 
878  /* decode the previously encoded string */
879  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
880  if( POLARSSL_MODE_CBC == cipher_info->mode )
881  {
882  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
883  }
884  else
885  {
886  fct_chk( enclen == outlen );
887  }
888 
889  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
890  if( POLARSSL_MODE_CBC == cipher_info->mode )
891  {
892  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
893  }
894  else
895  {
896  fct_chk( outlen == 0 );
897  }
898 
899 
900  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
901 
902  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
903  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
904  FCT_TEST_END();
905 #endif /* POLARSSL_DES_C */
906 
907 #ifdef POLARSSL_DES_C
908 
909  FCT_TEST_BGN(des_encrypt_and_decrypt_15_bytes)
910  size_t length = 15;
911  unsigned char key[32];
912  unsigned char iv[16];
913 
914  const cipher_info_t *cipher_info;
915  cipher_context_t ctx_dec;
916  cipher_context_t ctx_enc;
917 
918  unsigned char inbuf[64];
919  unsigned char encbuf[64];
920  unsigned char decbuf[64];
921 
922  size_t outlen = 0;
923  size_t enclen = 0;
924 
925  memset( key, 0, 32 );
926  memset( iv , 0, 16 );
927 
928  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
929  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
930 
931  memset( inbuf, 5, 64 );
932  memset( encbuf, 0, 64 );
933  memset( decbuf, 0, 64 );
934 
935  /* Check and get info structures */
937  fct_chk( NULL != cipher_info );
938  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
939 
940  /* Initialise enc and dec contexts */
941  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
942  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
943 
944  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
945  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
946 
947  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
948  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
949 
950  if( POLARSSL_MODE_CBC == cipher_info->mode )
951  {
952  enclen = cipher_get_block_size( &ctx_enc )
953  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
954  }
955  else
956  {
957  enclen = length;
958  }
959 
960  /* encode length number of bytes from inbuf */
961  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
962  if( POLARSSL_MODE_CBC == cipher_info->mode )
963  {
964  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
965  }
966  else
967  {
968  fct_chk( outlen == enclen );
969  }
970 
971  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
972  if( POLARSSL_MODE_CBC == cipher_info->mode )
973  {
974  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
975  }
976  else
977  {
978  fct_chk( outlen == 0 );
979  }
980 
981 
982  /* decode the previously encoded string */
983  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
984  if( POLARSSL_MODE_CBC == cipher_info->mode )
985  {
986  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
987  }
988  else
989  {
990  fct_chk( enclen == outlen );
991  }
992 
993  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
994  if( POLARSSL_MODE_CBC == cipher_info->mode )
995  {
996  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
997  }
998  else
999  {
1000  fct_chk( outlen == 0 );
1001  }
1002 
1003 
1004  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1005 
1006  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1007  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1008  FCT_TEST_END();
1009 #endif /* POLARSSL_DES_C */
1010 
1011 #ifdef POLARSSL_DES_C
1012 
1013  FCT_TEST_BGN(des_encrypt_and_decrypt_16_bytes)
1014  size_t length = 16;
1015  unsigned char key[32];
1016  unsigned char iv[16];
1017 
1018  const cipher_info_t *cipher_info;
1019  cipher_context_t ctx_dec;
1020  cipher_context_t ctx_enc;
1021 
1022  unsigned char inbuf[64];
1023  unsigned char encbuf[64];
1024  unsigned char decbuf[64];
1025 
1026  size_t outlen = 0;
1027  size_t enclen = 0;
1028 
1029  memset( key, 0, 32 );
1030  memset( iv , 0, 16 );
1031 
1032  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1033  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1034 
1035  memset( inbuf, 5, 64 );
1036  memset( encbuf, 0, 64 );
1037  memset( decbuf, 0, 64 );
1038 
1039  /* Check and get info structures */
1041  fct_chk( NULL != cipher_info );
1042  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
1043 
1044  /* Initialise enc and dec contexts */
1045  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1046  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1047 
1048  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
1049  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
1050 
1051  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1052  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1053 
1054  if( POLARSSL_MODE_CBC == cipher_info->mode )
1055  {
1056  enclen = cipher_get_block_size( &ctx_enc )
1057  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1058  }
1059  else
1060  {
1061  enclen = length;
1062  }
1063 
1064  /* encode length number of bytes from inbuf */
1065  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1066  if( POLARSSL_MODE_CBC == cipher_info->mode )
1067  {
1068  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1069  }
1070  else
1071  {
1072  fct_chk( outlen == enclen );
1073  }
1074 
1075  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1076  if( POLARSSL_MODE_CBC == cipher_info->mode )
1077  {
1078  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1079  }
1080  else
1081  {
1082  fct_chk( outlen == 0 );
1083  }
1084 
1085 
1086  /* decode the previously encoded string */
1087  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1088  if( POLARSSL_MODE_CBC == cipher_info->mode )
1089  {
1090  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1091  }
1092  else
1093  {
1094  fct_chk( enclen == outlen );
1095  }
1096 
1097  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1098  if( POLARSSL_MODE_CBC == cipher_info->mode )
1099  {
1100  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1101  }
1102  else
1103  {
1104  fct_chk( outlen == 0 );
1105  }
1106 
1107 
1108  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1109 
1110  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1111  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1112  FCT_TEST_END();
1113 #endif /* POLARSSL_DES_C */
1114 
1115 #ifdef POLARSSL_DES_C
1116 
1117  FCT_TEST_BGN(des_encrypt_and_decrypt_17_bytes)
1118  size_t length = 17;
1119  unsigned char key[32];
1120  unsigned char iv[16];
1121 
1122  const cipher_info_t *cipher_info;
1123  cipher_context_t ctx_dec;
1124  cipher_context_t ctx_enc;
1125 
1126  unsigned char inbuf[64];
1127  unsigned char encbuf[64];
1128  unsigned char decbuf[64];
1129 
1130  size_t outlen = 0;
1131  size_t enclen = 0;
1132 
1133  memset( key, 0, 32 );
1134  memset( iv , 0, 16 );
1135 
1136  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1137  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1138 
1139  memset( inbuf, 5, 64 );
1140  memset( encbuf, 0, 64 );
1141  memset( decbuf, 0, 64 );
1142 
1143  /* Check and get info structures */
1145  fct_chk( NULL != cipher_info );
1146  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
1147 
1148  /* Initialise enc and dec contexts */
1149  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1150  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1151 
1152  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
1153  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
1154 
1155  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1156  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1157 
1158  if( POLARSSL_MODE_CBC == cipher_info->mode )
1159  {
1160  enclen = cipher_get_block_size( &ctx_enc )
1161  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1162  }
1163  else
1164  {
1165  enclen = length;
1166  }
1167 
1168  /* encode length number of bytes from inbuf */
1169  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1170  if( POLARSSL_MODE_CBC == cipher_info->mode )
1171  {
1172  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1173  }
1174  else
1175  {
1176  fct_chk( outlen == enclen );
1177  }
1178 
1179  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1180  if( POLARSSL_MODE_CBC == cipher_info->mode )
1181  {
1182  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1183  }
1184  else
1185  {
1186  fct_chk( outlen == 0 );
1187  }
1188 
1189 
1190  /* decode the previously encoded string */
1191  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1192  if( POLARSSL_MODE_CBC == cipher_info->mode )
1193  {
1194  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1195  }
1196  else
1197  {
1198  fct_chk( enclen == outlen );
1199  }
1200 
1201  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1202  if( POLARSSL_MODE_CBC == cipher_info->mode )
1203  {
1204  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1205  }
1206  else
1207  {
1208  fct_chk( outlen == 0 );
1209  }
1210 
1211 
1212  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1213 
1214  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1215  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1216  FCT_TEST_END();
1217 #endif /* POLARSSL_DES_C */
1218 
1219 #ifdef POLARSSL_DES_C
1220 
1221  FCT_TEST_BGN(des_encrypt_and_decrypt_31_bytes)
1222  size_t length = 31;
1223  unsigned char key[32];
1224  unsigned char iv[16];
1225 
1226  const cipher_info_t *cipher_info;
1227  cipher_context_t ctx_dec;
1228  cipher_context_t ctx_enc;
1229 
1230  unsigned char inbuf[64];
1231  unsigned char encbuf[64];
1232  unsigned char decbuf[64];
1233 
1234  size_t outlen = 0;
1235  size_t enclen = 0;
1236 
1237  memset( key, 0, 32 );
1238  memset( iv , 0, 16 );
1239 
1240  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1241  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1242 
1243  memset( inbuf, 5, 64 );
1244  memset( encbuf, 0, 64 );
1245  memset( decbuf, 0, 64 );
1246 
1247  /* Check and get info structures */
1249  fct_chk( NULL != cipher_info );
1250  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
1251 
1252  /* Initialise enc and dec contexts */
1253  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1254  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1255 
1256  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
1257  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
1258 
1259  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1260  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1261 
1262  if( POLARSSL_MODE_CBC == cipher_info->mode )
1263  {
1264  enclen = cipher_get_block_size( &ctx_enc )
1265  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1266  }
1267  else
1268  {
1269  enclen = length;
1270  }
1271 
1272  /* encode length number of bytes from inbuf */
1273  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1274  if( POLARSSL_MODE_CBC == cipher_info->mode )
1275  {
1276  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1277  }
1278  else
1279  {
1280  fct_chk( outlen == enclen );
1281  }
1282 
1283  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1284  if( POLARSSL_MODE_CBC == cipher_info->mode )
1285  {
1286  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1287  }
1288  else
1289  {
1290  fct_chk( outlen == 0 );
1291  }
1292 
1293 
1294  /* decode the previously encoded string */
1295  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1296  if( POLARSSL_MODE_CBC == cipher_info->mode )
1297  {
1298  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1299  }
1300  else
1301  {
1302  fct_chk( enclen == outlen );
1303  }
1304 
1305  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1306  if( POLARSSL_MODE_CBC == cipher_info->mode )
1307  {
1308  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1309  }
1310  else
1311  {
1312  fct_chk( outlen == 0 );
1313  }
1314 
1315 
1316  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1317 
1318  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1319  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1320  FCT_TEST_END();
1321 #endif /* POLARSSL_DES_C */
1322 
1323 #ifdef POLARSSL_DES_C
1324 
1325  FCT_TEST_BGN(des_encrypt_and_decrypt_32_bytes)
1326  size_t length = 32;
1327  unsigned char key[32];
1328  unsigned char iv[16];
1329 
1330  const cipher_info_t *cipher_info;
1331  cipher_context_t ctx_dec;
1332  cipher_context_t ctx_enc;
1333 
1334  unsigned char inbuf[64];
1335  unsigned char encbuf[64];
1336  unsigned char decbuf[64];
1337 
1338  size_t outlen = 0;
1339  size_t enclen = 0;
1340 
1341  memset( key, 0, 32 );
1342  memset( iv , 0, 16 );
1343 
1344  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1345  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1346 
1347  memset( inbuf, 5, 64 );
1348  memset( encbuf, 0, 64 );
1349  memset( decbuf, 0, 64 );
1350 
1351  /* Check and get info structures */
1353  fct_chk( NULL != cipher_info );
1354  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
1355 
1356  /* Initialise enc and dec contexts */
1357  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1358  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1359 
1360  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
1361  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
1362 
1363  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1364  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1365 
1366  if( POLARSSL_MODE_CBC == cipher_info->mode )
1367  {
1368  enclen = cipher_get_block_size( &ctx_enc )
1369  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1370  }
1371  else
1372  {
1373  enclen = length;
1374  }
1375 
1376  /* encode length number of bytes from inbuf */
1377  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1378  if( POLARSSL_MODE_CBC == cipher_info->mode )
1379  {
1380  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1381  }
1382  else
1383  {
1384  fct_chk( outlen == enclen );
1385  }
1386 
1387  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1388  if( POLARSSL_MODE_CBC == cipher_info->mode )
1389  {
1390  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1391  }
1392  else
1393  {
1394  fct_chk( outlen == 0 );
1395  }
1396 
1397 
1398  /* decode the previously encoded string */
1399  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1400  if( POLARSSL_MODE_CBC == cipher_info->mode )
1401  {
1402  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1403  }
1404  else
1405  {
1406  fct_chk( enclen == outlen );
1407  }
1408 
1409  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1410  if( POLARSSL_MODE_CBC == cipher_info->mode )
1411  {
1412  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1413  }
1414  else
1415  {
1416  fct_chk( outlen == 0 );
1417  }
1418 
1419 
1420  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1421 
1422  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1423  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1424  FCT_TEST_END();
1425 #endif /* POLARSSL_DES_C */
1426 
1427 #ifdef POLARSSL_DES_C
1428 
1429  FCT_TEST_BGN(des_encrypt_and_decrypt_32_bytes)
1430  size_t length = 33;
1431  unsigned char key[32];
1432  unsigned char iv[16];
1433 
1434  const cipher_info_t *cipher_info;
1435  cipher_context_t ctx_dec;
1436  cipher_context_t ctx_enc;
1437 
1438  unsigned char inbuf[64];
1439  unsigned char encbuf[64];
1440  unsigned char decbuf[64];
1441 
1442  size_t outlen = 0;
1443  size_t enclen = 0;
1444 
1445  memset( key, 0, 32 );
1446  memset( iv , 0, 16 );
1447 
1448  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1449  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1450 
1451  memset( inbuf, 5, 64 );
1452  memset( encbuf, 0, 64 );
1453  memset( decbuf, 0, 64 );
1454 
1455  /* Check and get info structures */
1457  fct_chk( NULL != cipher_info );
1458  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
1459 
1460  /* Initialise enc and dec contexts */
1461  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1462  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1463 
1464  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
1465  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
1466 
1467  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1468  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1469 
1470  if( POLARSSL_MODE_CBC == cipher_info->mode )
1471  {
1472  enclen = cipher_get_block_size( &ctx_enc )
1473  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1474  }
1475  else
1476  {
1477  enclen = length;
1478  }
1479 
1480  /* encode length number of bytes from inbuf */
1481  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1482  if( POLARSSL_MODE_CBC == cipher_info->mode )
1483  {
1484  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1485  }
1486  else
1487  {
1488  fct_chk( outlen == enclen );
1489  }
1490 
1491  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1492  if( POLARSSL_MODE_CBC == cipher_info->mode )
1493  {
1494  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1495  }
1496  else
1497  {
1498  fct_chk( outlen == 0 );
1499  }
1500 
1501 
1502  /* decode the previously encoded string */
1503  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1504  if( POLARSSL_MODE_CBC == cipher_info->mode )
1505  {
1506  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1507  }
1508  else
1509  {
1510  fct_chk( enclen == outlen );
1511  }
1512 
1513  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1514  if( POLARSSL_MODE_CBC == cipher_info->mode )
1515  {
1516  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1517  }
1518  else
1519  {
1520  fct_chk( outlen == 0 );
1521  }
1522 
1523 
1524  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1525 
1526  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1527  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1528  FCT_TEST_END();
1529 #endif /* POLARSSL_DES_C */
1530 
1531 #ifdef POLARSSL_DES_C
1532 
1533  FCT_TEST_BGN(des_encrypt_and_decrypt_47_bytes)
1534  size_t length = 47;
1535  unsigned char key[32];
1536  unsigned char iv[16];
1537 
1538  const cipher_info_t *cipher_info;
1539  cipher_context_t ctx_dec;
1540  cipher_context_t ctx_enc;
1541 
1542  unsigned char inbuf[64];
1543  unsigned char encbuf[64];
1544  unsigned char decbuf[64];
1545 
1546  size_t outlen = 0;
1547  size_t enclen = 0;
1548 
1549  memset( key, 0, 32 );
1550  memset( iv , 0, 16 );
1551 
1552  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1553  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1554 
1555  memset( inbuf, 5, 64 );
1556  memset( encbuf, 0, 64 );
1557  memset( decbuf, 0, 64 );
1558 
1559  /* Check and get info structures */
1561  fct_chk( NULL != cipher_info );
1562  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
1563 
1564  /* Initialise enc and dec contexts */
1565  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1566  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1567 
1568  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
1569  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
1570 
1571  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1572  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1573 
1574  if( POLARSSL_MODE_CBC == cipher_info->mode )
1575  {
1576  enclen = cipher_get_block_size( &ctx_enc )
1577  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1578  }
1579  else
1580  {
1581  enclen = length;
1582  }
1583 
1584  /* encode length number of bytes from inbuf */
1585  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1586  if( POLARSSL_MODE_CBC == cipher_info->mode )
1587  {
1588  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1589  }
1590  else
1591  {
1592  fct_chk( outlen == enclen );
1593  }
1594 
1595  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1596  if( POLARSSL_MODE_CBC == cipher_info->mode )
1597  {
1598  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1599  }
1600  else
1601  {
1602  fct_chk( outlen == 0 );
1603  }
1604 
1605 
1606  /* decode the previously encoded string */
1607  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1608  if( POLARSSL_MODE_CBC == cipher_info->mode )
1609  {
1610  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1611  }
1612  else
1613  {
1614  fct_chk( enclen == outlen );
1615  }
1616 
1617  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1618  if( POLARSSL_MODE_CBC == cipher_info->mode )
1619  {
1620  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1621  }
1622  else
1623  {
1624  fct_chk( outlen == 0 );
1625  }
1626 
1627 
1628  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1629 
1630  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1631  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1632  FCT_TEST_END();
1633 #endif /* POLARSSL_DES_C */
1634 
1635 #ifdef POLARSSL_DES_C
1636 
1637  FCT_TEST_BGN(des_encrypt_and_decrypt_48_bytes)
1638  size_t length = 48;
1639  unsigned char key[32];
1640  unsigned char iv[16];
1641 
1642  const cipher_info_t *cipher_info;
1643  cipher_context_t ctx_dec;
1644  cipher_context_t ctx_enc;
1645 
1646  unsigned char inbuf[64];
1647  unsigned char encbuf[64];
1648  unsigned char decbuf[64];
1649 
1650  size_t outlen = 0;
1651  size_t enclen = 0;
1652 
1653  memset( key, 0, 32 );
1654  memset( iv , 0, 16 );
1655 
1656  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1657  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1658 
1659  memset( inbuf, 5, 64 );
1660  memset( encbuf, 0, 64 );
1661  memset( decbuf, 0, 64 );
1662 
1663  /* Check and get info structures */
1665  fct_chk( NULL != cipher_info );
1666  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
1667 
1668  /* Initialise enc and dec contexts */
1669  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1670  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1671 
1672  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
1673  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
1674 
1675  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1676  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1677 
1678  if( POLARSSL_MODE_CBC == cipher_info->mode )
1679  {
1680  enclen = cipher_get_block_size( &ctx_enc )
1681  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1682  }
1683  else
1684  {
1685  enclen = length;
1686  }
1687 
1688  /* encode length number of bytes from inbuf */
1689  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1690  if( POLARSSL_MODE_CBC == cipher_info->mode )
1691  {
1692  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1693  }
1694  else
1695  {
1696  fct_chk( outlen == enclen );
1697  }
1698 
1699  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1700  if( POLARSSL_MODE_CBC == cipher_info->mode )
1701  {
1702  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1703  }
1704  else
1705  {
1706  fct_chk( outlen == 0 );
1707  }
1708 
1709 
1710  /* decode the previously encoded string */
1711  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1712  if( POLARSSL_MODE_CBC == cipher_info->mode )
1713  {
1714  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1715  }
1716  else
1717  {
1718  fct_chk( enclen == outlen );
1719  }
1720 
1721  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1722  if( POLARSSL_MODE_CBC == cipher_info->mode )
1723  {
1724  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1725  }
1726  else
1727  {
1728  fct_chk( outlen == 0 );
1729  }
1730 
1731 
1732  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1733 
1734  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1735  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1736  FCT_TEST_END();
1737 #endif /* POLARSSL_DES_C */
1738 
1739 #ifdef POLARSSL_DES_C
1740 
1741  FCT_TEST_BGN(des_encrypt_and_decrypt_49_bytes)
1742  size_t length = 49;
1743  unsigned char key[32];
1744  unsigned char iv[16];
1745 
1746  const cipher_info_t *cipher_info;
1747  cipher_context_t ctx_dec;
1748  cipher_context_t ctx_enc;
1749 
1750  unsigned char inbuf[64];
1751  unsigned char encbuf[64];
1752  unsigned char decbuf[64];
1753 
1754  size_t outlen = 0;
1755  size_t enclen = 0;
1756 
1757  memset( key, 0, 32 );
1758  memset( iv , 0, 16 );
1759 
1760  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1761  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1762 
1763  memset( inbuf, 5, 64 );
1764  memset( encbuf, 0, 64 );
1765  memset( decbuf, 0, 64 );
1766 
1767  /* Check and get info structures */
1769  fct_chk( NULL != cipher_info );
1770  fct_chk( cipher_info_from_string( "DES-CBC" ) == cipher_info );
1771 
1772  /* Initialise enc and dec contexts */
1773  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1774  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1775 
1776  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
1777  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
1778 
1779  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1780  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1781 
1782  if( POLARSSL_MODE_CBC == cipher_info->mode )
1783  {
1784  enclen = cipher_get_block_size( &ctx_enc )
1785  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1786  }
1787  else
1788  {
1789  enclen = length;
1790  }
1791 
1792  /* encode length number of bytes from inbuf */
1793  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1794  if( POLARSSL_MODE_CBC == cipher_info->mode )
1795  {
1796  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1797  }
1798  else
1799  {
1800  fct_chk( outlen == enclen );
1801  }
1802 
1803  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1804  if( POLARSSL_MODE_CBC == cipher_info->mode )
1805  {
1806  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1807  }
1808  else
1809  {
1810  fct_chk( outlen == 0 );
1811  }
1812 
1813 
1814  /* decode the previously encoded string */
1815  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1816  if( POLARSSL_MODE_CBC == cipher_info->mode )
1817  {
1818  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1819  }
1820  else
1821  {
1822  fct_chk( enclen == outlen );
1823  }
1824 
1825  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1826  if( POLARSSL_MODE_CBC == cipher_info->mode )
1827  {
1828  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1829  }
1830  else
1831  {
1832  fct_chk( outlen == 0 );
1833  }
1834 
1835 
1836  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1837 
1838  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1839  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1840  FCT_TEST_END();
1841 #endif /* POLARSSL_DES_C */
1842 
1843 #ifdef POLARSSL_DES_C
1844 
1845  FCT_TEST_BGN(des_encrypt_and_decrypt_0_bytes_in_multiple_parts)
1846  size_t first_length = 0;
1847  size_t second_length = 0;
1848  size_t length = first_length + second_length;
1849  unsigned char key[32];
1850  unsigned char iv[16];
1851 
1852  cipher_context_t ctx_dec;
1853  cipher_context_t ctx_enc;
1854  const cipher_info_t *cipher_info;
1855 
1856  unsigned char inbuf[64];
1857  unsigned char encbuf[64];
1858  unsigned char decbuf[64];
1859 
1860  size_t outlen = 0;
1861  size_t totaloutlen = 0;
1862  size_t enclen = 0;
1863 
1864  memset( key, 0, 32 );
1865  memset( iv , 0, 16 );
1866 
1867  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1868  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1869 
1870  memset( inbuf, 5, 64 );
1871  memset( encbuf, 0, 64 );
1872  memset( decbuf, 0, 64 );
1873 
1874  /* Initialise enc and dec contexts */
1876  fct_chk( NULL != cipher_info);
1877 
1878  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1879  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1880 
1881  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
1882  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
1883 
1884  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1885  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1886 
1887  if( POLARSSL_MODE_CBC == cipher_info->mode )
1888  {
1889  enclen = cipher_get_block_size(&ctx_enc )
1890  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1891  }
1892  else
1893  {
1894  enclen = length;
1895  }
1896 
1897  /* encode length number of bytes from inbuf */
1898  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
1899  totaloutlen = outlen;
1900  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
1901  totaloutlen += outlen;
1902  if( POLARSSL_MODE_CBC == cipher_info->mode )
1903  {
1904  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1905  }
1906  else
1907  {
1908  fct_chk( totaloutlen == enclen );
1909  }
1910  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
1911  totaloutlen += outlen;
1912  if( POLARSSL_MODE_CBC == cipher_info->mode )
1913  {
1914  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1915  }
1916  else
1917  {
1918  fct_chk( outlen == 0 );
1919  }
1920 
1921  /* decode the previously encoded string */
1922  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1923  if( POLARSSL_MODE_CBC == cipher_info->mode )
1924  {
1925  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1926  }
1927  else
1928  {
1929  fct_chk( enclen == outlen );
1930  }
1931  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1932  if( POLARSSL_MODE_CBC == cipher_info->mode )
1933  {
1934  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1935  }
1936  else
1937  {
1938  fct_chk( outlen == 0 );
1939  }
1940 
1941 
1942  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1943 
1944  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1945  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1946  FCT_TEST_END();
1947 #endif /* POLARSSL_DES_C */
1948 
1949 #ifdef POLARSSL_DES_C
1950 
1951  FCT_TEST_BGN(des_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
1952  size_t first_length = 1;
1953  size_t second_length = 0;
1954  size_t length = first_length + second_length;
1955  unsigned char key[32];
1956  unsigned char iv[16];
1957 
1958  cipher_context_t ctx_dec;
1959  cipher_context_t ctx_enc;
1960  const cipher_info_t *cipher_info;
1961 
1962  unsigned char inbuf[64];
1963  unsigned char encbuf[64];
1964  unsigned char decbuf[64];
1965 
1966  size_t outlen = 0;
1967  size_t totaloutlen = 0;
1968  size_t enclen = 0;
1969 
1970  memset( key, 0, 32 );
1971  memset( iv , 0, 16 );
1972 
1973  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1974  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1975 
1976  memset( inbuf, 5, 64 );
1977  memset( encbuf, 0, 64 );
1978  memset( decbuf, 0, 64 );
1979 
1980  /* Initialise enc and dec contexts */
1982  fct_chk( NULL != cipher_info);
1983 
1984  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1985  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1986 
1987  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
1988  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
1989 
1990  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1991  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1992 
1993  if( POLARSSL_MODE_CBC == cipher_info->mode )
1994  {
1995  enclen = cipher_get_block_size(&ctx_enc )
1996  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1997  }
1998  else
1999  {
2000  enclen = length;
2001  }
2002 
2003  /* encode length number of bytes from inbuf */
2004  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2005  totaloutlen = outlen;
2006  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2007  totaloutlen += outlen;
2008  if( POLARSSL_MODE_CBC == cipher_info->mode )
2009  {
2010  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2011  }
2012  else
2013  {
2014  fct_chk( totaloutlen == enclen );
2015  }
2016  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2017  totaloutlen += outlen;
2018  if( POLARSSL_MODE_CBC == cipher_info->mode )
2019  {
2020  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2021  }
2022  else
2023  {
2024  fct_chk( outlen == 0 );
2025  }
2026 
2027  /* decode the previously encoded string */
2028  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2029  if( POLARSSL_MODE_CBC == cipher_info->mode )
2030  {
2031  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2032  }
2033  else
2034  {
2035  fct_chk( enclen == outlen );
2036  }
2037  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2038  if( POLARSSL_MODE_CBC == cipher_info->mode )
2039  {
2040  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2041  }
2042  else
2043  {
2044  fct_chk( outlen == 0 );
2045  }
2046 
2047 
2048  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2049 
2050  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2051  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2052  FCT_TEST_END();
2053 #endif /* POLARSSL_DES_C */
2054 
2055 #ifdef POLARSSL_DES_C
2056 
2057  FCT_TEST_BGN(des_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
2058  size_t first_length = 0;
2059  size_t second_length = 1;
2060  size_t length = first_length + second_length;
2061  unsigned char key[32];
2062  unsigned char iv[16];
2063 
2064  cipher_context_t ctx_dec;
2065  cipher_context_t ctx_enc;
2066  const cipher_info_t *cipher_info;
2067 
2068  unsigned char inbuf[64];
2069  unsigned char encbuf[64];
2070  unsigned char decbuf[64];
2071 
2072  size_t outlen = 0;
2073  size_t totaloutlen = 0;
2074  size_t enclen = 0;
2075 
2076  memset( key, 0, 32 );
2077  memset( iv , 0, 16 );
2078 
2079  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2080  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2081 
2082  memset( inbuf, 5, 64 );
2083  memset( encbuf, 0, 64 );
2084  memset( decbuf, 0, 64 );
2085 
2086  /* Initialise enc and dec contexts */
2088  fct_chk( NULL != cipher_info);
2089 
2090  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2091  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2092 
2093  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
2094  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
2095 
2096  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2097  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2098 
2099  if( POLARSSL_MODE_CBC == cipher_info->mode )
2100  {
2101  enclen = cipher_get_block_size(&ctx_enc )
2102  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2103  }
2104  else
2105  {
2106  enclen = length;
2107  }
2108 
2109  /* encode length number of bytes from inbuf */
2110  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2111  totaloutlen = outlen;
2112  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2113  totaloutlen += outlen;
2114  if( POLARSSL_MODE_CBC == cipher_info->mode )
2115  {
2116  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2117  }
2118  else
2119  {
2120  fct_chk( totaloutlen == enclen );
2121  }
2122  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2123  totaloutlen += outlen;
2124  if( POLARSSL_MODE_CBC == cipher_info->mode )
2125  {
2126  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2127  }
2128  else
2129  {
2130  fct_chk( outlen == 0 );
2131  }
2132 
2133  /* decode the previously encoded string */
2134  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2135  if( POLARSSL_MODE_CBC == cipher_info->mode )
2136  {
2137  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2138  }
2139  else
2140  {
2141  fct_chk( enclen == outlen );
2142  }
2143  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2144  if( POLARSSL_MODE_CBC == cipher_info->mode )
2145  {
2146  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2147  }
2148  else
2149  {
2150  fct_chk( outlen == 0 );
2151  }
2152 
2153 
2154  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2155 
2156  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2157  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2158  FCT_TEST_END();
2159 #endif /* POLARSSL_DES_C */
2160 
2161 #ifdef POLARSSL_DES_C
2162 
2163  FCT_TEST_BGN(des_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
2164  size_t first_length = 16;
2165  size_t second_length = 0;
2166  size_t length = first_length + second_length;
2167  unsigned char key[32];
2168  unsigned char iv[16];
2169 
2170  cipher_context_t ctx_dec;
2171  cipher_context_t ctx_enc;
2172  const cipher_info_t *cipher_info;
2173 
2174  unsigned char inbuf[64];
2175  unsigned char encbuf[64];
2176  unsigned char decbuf[64];
2177 
2178  size_t outlen = 0;
2179  size_t totaloutlen = 0;
2180  size_t enclen = 0;
2181 
2182  memset( key, 0, 32 );
2183  memset( iv , 0, 16 );
2184 
2185  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2186  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2187 
2188  memset( inbuf, 5, 64 );
2189  memset( encbuf, 0, 64 );
2190  memset( decbuf, 0, 64 );
2191 
2192  /* Initialise enc and dec contexts */
2194  fct_chk( NULL != cipher_info);
2195 
2196  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2197  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2198 
2199  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
2200  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
2201 
2202  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2203  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2204 
2205  if( POLARSSL_MODE_CBC == cipher_info->mode )
2206  {
2207  enclen = cipher_get_block_size(&ctx_enc )
2208  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2209  }
2210  else
2211  {
2212  enclen = length;
2213  }
2214 
2215  /* encode length number of bytes from inbuf */
2216  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2217  totaloutlen = outlen;
2218  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2219  totaloutlen += outlen;
2220  if( POLARSSL_MODE_CBC == cipher_info->mode )
2221  {
2222  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2223  }
2224  else
2225  {
2226  fct_chk( totaloutlen == enclen );
2227  }
2228  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2229  totaloutlen += outlen;
2230  if( POLARSSL_MODE_CBC == cipher_info->mode )
2231  {
2232  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2233  }
2234  else
2235  {
2236  fct_chk( outlen == 0 );
2237  }
2238 
2239  /* decode the previously encoded string */
2240  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2241  if( POLARSSL_MODE_CBC == cipher_info->mode )
2242  {
2243  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2244  }
2245  else
2246  {
2247  fct_chk( enclen == outlen );
2248  }
2249  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2250  if( POLARSSL_MODE_CBC == cipher_info->mode )
2251  {
2252  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2253  }
2254  else
2255  {
2256  fct_chk( outlen == 0 );
2257  }
2258 
2259 
2260  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2261 
2262  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2263  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2264  FCT_TEST_END();
2265 #endif /* POLARSSL_DES_C */
2266 
2267 #ifdef POLARSSL_DES_C
2268 
2269  FCT_TEST_BGN(des_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
2270  size_t first_length = 0;
2271  size_t second_length = 16;
2272  size_t length = first_length + second_length;
2273  unsigned char key[32];
2274  unsigned char iv[16];
2275 
2276  cipher_context_t ctx_dec;
2277  cipher_context_t ctx_enc;
2278  const cipher_info_t *cipher_info;
2279 
2280  unsigned char inbuf[64];
2281  unsigned char encbuf[64];
2282  unsigned char decbuf[64];
2283 
2284  size_t outlen = 0;
2285  size_t totaloutlen = 0;
2286  size_t enclen = 0;
2287 
2288  memset( key, 0, 32 );
2289  memset( iv , 0, 16 );
2290 
2291  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2292  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2293 
2294  memset( inbuf, 5, 64 );
2295  memset( encbuf, 0, 64 );
2296  memset( decbuf, 0, 64 );
2297 
2298  /* Initialise enc and dec contexts */
2300  fct_chk( NULL != cipher_info);
2301 
2302  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2303  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2304 
2305  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
2306  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
2307 
2308  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2309  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2310 
2311  if( POLARSSL_MODE_CBC == cipher_info->mode )
2312  {
2313  enclen = cipher_get_block_size(&ctx_enc )
2314  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2315  }
2316  else
2317  {
2318  enclen = length;
2319  }
2320 
2321  /* encode length number of bytes from inbuf */
2322  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2323  totaloutlen = outlen;
2324  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2325  totaloutlen += outlen;
2326  if( POLARSSL_MODE_CBC == cipher_info->mode )
2327  {
2328  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2329  }
2330  else
2331  {
2332  fct_chk( totaloutlen == enclen );
2333  }
2334  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2335  totaloutlen += outlen;
2336  if( POLARSSL_MODE_CBC == cipher_info->mode )
2337  {
2338  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2339  }
2340  else
2341  {
2342  fct_chk( outlen == 0 );
2343  }
2344 
2345  /* decode the previously encoded string */
2346  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2347  if( POLARSSL_MODE_CBC == cipher_info->mode )
2348  {
2349  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2350  }
2351  else
2352  {
2353  fct_chk( enclen == outlen );
2354  }
2355  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2356  if( POLARSSL_MODE_CBC == cipher_info->mode )
2357  {
2358  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2359  }
2360  else
2361  {
2362  fct_chk( outlen == 0 );
2363  }
2364 
2365 
2366  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2367 
2368  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2369  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2370  FCT_TEST_END();
2371 #endif /* POLARSSL_DES_C */
2372 
2373 #ifdef POLARSSL_DES_C
2374 
2375  FCT_TEST_BGN(des_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
2376  size_t first_length = 1;
2377  size_t second_length = 15;
2378  size_t length = first_length + second_length;
2379  unsigned char key[32];
2380  unsigned char iv[16];
2381 
2382  cipher_context_t ctx_dec;
2383  cipher_context_t ctx_enc;
2384  const cipher_info_t *cipher_info;
2385 
2386  unsigned char inbuf[64];
2387  unsigned char encbuf[64];
2388  unsigned char decbuf[64];
2389 
2390  size_t outlen = 0;
2391  size_t totaloutlen = 0;
2392  size_t enclen = 0;
2393 
2394  memset( key, 0, 32 );
2395  memset( iv , 0, 16 );
2396 
2397  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2398  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2399 
2400  memset( inbuf, 5, 64 );
2401  memset( encbuf, 0, 64 );
2402  memset( decbuf, 0, 64 );
2403 
2404  /* Initialise enc and dec contexts */
2406  fct_chk( NULL != cipher_info);
2407 
2408  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2409  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2410 
2411  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
2412  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
2413 
2414  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2415  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2416 
2417  if( POLARSSL_MODE_CBC == cipher_info->mode )
2418  {
2419  enclen = cipher_get_block_size(&ctx_enc )
2420  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2421  }
2422  else
2423  {
2424  enclen = length;
2425  }
2426 
2427  /* encode length number of bytes from inbuf */
2428  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2429  totaloutlen = outlen;
2430  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2431  totaloutlen += outlen;
2432  if( POLARSSL_MODE_CBC == cipher_info->mode )
2433  {
2434  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2435  }
2436  else
2437  {
2438  fct_chk( totaloutlen == enclen );
2439  }
2440  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2441  totaloutlen += outlen;
2442  if( POLARSSL_MODE_CBC == cipher_info->mode )
2443  {
2444  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2445  }
2446  else
2447  {
2448  fct_chk( outlen == 0 );
2449  }
2450 
2451  /* decode the previously encoded string */
2452  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2453  if( POLARSSL_MODE_CBC == cipher_info->mode )
2454  {
2455  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2456  }
2457  else
2458  {
2459  fct_chk( enclen == outlen );
2460  }
2461  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2462  if( POLARSSL_MODE_CBC == cipher_info->mode )
2463  {
2464  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2465  }
2466  else
2467  {
2468  fct_chk( outlen == 0 );
2469  }
2470 
2471 
2472  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2473 
2474  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2475  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2476  FCT_TEST_END();
2477 #endif /* POLARSSL_DES_C */
2478 
2479 #ifdef POLARSSL_DES_C
2480 
2481  FCT_TEST_BGN(des_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
2482  size_t first_length = 15;
2483  size_t second_length = 1;
2484  size_t length = first_length + second_length;
2485  unsigned char key[32];
2486  unsigned char iv[16];
2487 
2488  cipher_context_t ctx_dec;
2489  cipher_context_t ctx_enc;
2490  const cipher_info_t *cipher_info;
2491 
2492  unsigned char inbuf[64];
2493  unsigned char encbuf[64];
2494  unsigned char decbuf[64];
2495 
2496  size_t outlen = 0;
2497  size_t totaloutlen = 0;
2498  size_t enclen = 0;
2499 
2500  memset( key, 0, 32 );
2501  memset( iv , 0, 16 );
2502 
2503  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2504  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2505 
2506  memset( inbuf, 5, 64 );
2507  memset( encbuf, 0, 64 );
2508  memset( decbuf, 0, 64 );
2509 
2510  /* Initialise enc and dec contexts */
2512  fct_chk( NULL != cipher_info);
2513 
2514  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2515  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2516 
2517  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
2518  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
2519 
2520  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2521  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2522 
2523  if( POLARSSL_MODE_CBC == cipher_info->mode )
2524  {
2525  enclen = cipher_get_block_size(&ctx_enc )
2526  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2527  }
2528  else
2529  {
2530  enclen = length;
2531  }
2532 
2533  /* encode length number of bytes from inbuf */
2534  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2535  totaloutlen = outlen;
2536  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2537  totaloutlen += outlen;
2538  if( POLARSSL_MODE_CBC == cipher_info->mode )
2539  {
2540  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2541  }
2542  else
2543  {
2544  fct_chk( totaloutlen == enclen );
2545  }
2546  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2547  totaloutlen += outlen;
2548  if( POLARSSL_MODE_CBC == cipher_info->mode )
2549  {
2550  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2551  }
2552  else
2553  {
2554  fct_chk( outlen == 0 );
2555  }
2556 
2557  /* decode the previously encoded string */
2558  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2559  if( POLARSSL_MODE_CBC == cipher_info->mode )
2560  {
2561  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2562  }
2563  else
2564  {
2565  fct_chk( enclen == outlen );
2566  }
2567  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2568  if( POLARSSL_MODE_CBC == cipher_info->mode )
2569  {
2570  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2571  }
2572  else
2573  {
2574  fct_chk( outlen == 0 );
2575  }
2576 
2577 
2578  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2579 
2580  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2581  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2582  FCT_TEST_END();
2583 #endif /* POLARSSL_DES_C */
2584 
2585 #ifdef POLARSSL_DES_C
2586 
2587  FCT_TEST_BGN(des_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2588  size_t first_length = 15;
2589  size_t second_length = 7;
2590  size_t length = first_length + second_length;
2591  unsigned char key[32];
2592  unsigned char iv[16];
2593 
2594  cipher_context_t ctx_dec;
2595  cipher_context_t ctx_enc;
2596  const cipher_info_t *cipher_info;
2597 
2598  unsigned char inbuf[64];
2599  unsigned char encbuf[64];
2600  unsigned char decbuf[64];
2601 
2602  size_t outlen = 0;
2603  size_t totaloutlen = 0;
2604  size_t enclen = 0;
2605 
2606  memset( key, 0, 32 );
2607  memset( iv , 0, 16 );
2608 
2609  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2610  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2611 
2612  memset( inbuf, 5, 64 );
2613  memset( encbuf, 0, 64 );
2614  memset( decbuf, 0, 64 );
2615 
2616  /* Initialise enc and dec contexts */
2618  fct_chk( NULL != cipher_info);
2619 
2620  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2621  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2622 
2623  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
2624  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
2625 
2626  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2627  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2628 
2629  if( POLARSSL_MODE_CBC == cipher_info->mode )
2630  {
2631  enclen = cipher_get_block_size(&ctx_enc )
2632  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2633  }
2634  else
2635  {
2636  enclen = length;
2637  }
2638 
2639  /* encode length number of bytes from inbuf */
2640  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2641  totaloutlen = outlen;
2642  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2643  totaloutlen += outlen;
2644  if( POLARSSL_MODE_CBC == cipher_info->mode )
2645  {
2646  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2647  }
2648  else
2649  {
2650  fct_chk( totaloutlen == enclen );
2651  }
2652  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2653  totaloutlen += outlen;
2654  if( POLARSSL_MODE_CBC == cipher_info->mode )
2655  {
2656  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2657  }
2658  else
2659  {
2660  fct_chk( outlen == 0 );
2661  }
2662 
2663  /* decode the previously encoded string */
2664  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2665  if( POLARSSL_MODE_CBC == cipher_info->mode )
2666  {
2667  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2668  }
2669  else
2670  {
2671  fct_chk( enclen == outlen );
2672  }
2673  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2674  if( POLARSSL_MODE_CBC == cipher_info->mode )
2675  {
2676  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2677  }
2678  else
2679  {
2680  fct_chk( outlen == 0 );
2681  }
2682 
2683 
2684  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2685 
2686  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2687  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2688  FCT_TEST_END();
2689 #endif /* POLARSSL_DES_C */
2690 
2691 #ifdef POLARSSL_DES_C
2692 
2693  FCT_TEST_BGN(des_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2694  size_t first_length = 16;
2695  size_t second_length = 6;
2696  size_t length = first_length + second_length;
2697  unsigned char key[32];
2698  unsigned char iv[16];
2699 
2700  cipher_context_t ctx_dec;
2701  cipher_context_t ctx_enc;
2702  const cipher_info_t *cipher_info;
2703 
2704  unsigned char inbuf[64];
2705  unsigned char encbuf[64];
2706  unsigned char decbuf[64];
2707 
2708  size_t outlen = 0;
2709  size_t totaloutlen = 0;
2710  size_t enclen = 0;
2711 
2712  memset( key, 0, 32 );
2713  memset( iv , 0, 16 );
2714 
2715  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2716  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2717 
2718  memset( inbuf, 5, 64 );
2719  memset( encbuf, 0, 64 );
2720  memset( decbuf, 0, 64 );
2721 
2722  /* Initialise enc and dec contexts */
2724  fct_chk( NULL != cipher_info);
2725 
2726  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2727  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2728 
2729  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
2730  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
2731 
2732  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2733  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2734 
2735  if( POLARSSL_MODE_CBC == cipher_info->mode )
2736  {
2737  enclen = cipher_get_block_size(&ctx_enc )
2738  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2739  }
2740  else
2741  {
2742  enclen = length;
2743  }
2744 
2745  /* encode length number of bytes from inbuf */
2746  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2747  totaloutlen = outlen;
2748  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2749  totaloutlen += outlen;
2750  if( POLARSSL_MODE_CBC == cipher_info->mode )
2751  {
2752  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2753  }
2754  else
2755  {
2756  fct_chk( totaloutlen == enclen );
2757  }
2758  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2759  totaloutlen += outlen;
2760  if( POLARSSL_MODE_CBC == cipher_info->mode )
2761  {
2762  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2763  }
2764  else
2765  {
2766  fct_chk( outlen == 0 );
2767  }
2768 
2769  /* decode the previously encoded string */
2770  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2771  if( POLARSSL_MODE_CBC == cipher_info->mode )
2772  {
2773  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2774  }
2775  else
2776  {
2777  fct_chk( enclen == outlen );
2778  }
2779  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2780  if( POLARSSL_MODE_CBC == cipher_info->mode )
2781  {
2782  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2783  }
2784  else
2785  {
2786  fct_chk( outlen == 0 );
2787  }
2788 
2789 
2790  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2791 
2792  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2793  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2794  FCT_TEST_END();
2795 #endif /* POLARSSL_DES_C */
2796 
2797 #ifdef POLARSSL_DES_C
2798 
2799  FCT_TEST_BGN(des_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2800  size_t first_length = 17;
2801  size_t second_length = 6;
2802  size_t length = first_length + second_length;
2803  unsigned char key[32];
2804  unsigned char iv[16];
2805 
2806  cipher_context_t ctx_dec;
2807  cipher_context_t ctx_enc;
2808  const cipher_info_t *cipher_info;
2809 
2810  unsigned char inbuf[64];
2811  unsigned char encbuf[64];
2812  unsigned char decbuf[64];
2813 
2814  size_t outlen = 0;
2815  size_t totaloutlen = 0;
2816  size_t enclen = 0;
2817 
2818  memset( key, 0, 32 );
2819  memset( iv , 0, 16 );
2820 
2821  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2822  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2823 
2824  memset( inbuf, 5, 64 );
2825  memset( encbuf, 0, 64 );
2826  memset( decbuf, 0, 64 );
2827 
2828  /* Initialise enc and dec contexts */
2830  fct_chk( NULL != cipher_info);
2831 
2832  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2833  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2834 
2835  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
2836  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
2837 
2838  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2839  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2840 
2841  if( POLARSSL_MODE_CBC == cipher_info->mode )
2842  {
2843  enclen = cipher_get_block_size(&ctx_enc )
2844  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2845  }
2846  else
2847  {
2848  enclen = length;
2849  }
2850 
2851  /* encode length number of bytes from inbuf */
2852  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2853  totaloutlen = outlen;
2854  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2855  totaloutlen += outlen;
2856  if( POLARSSL_MODE_CBC == cipher_info->mode )
2857  {
2858  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2859  }
2860  else
2861  {
2862  fct_chk( totaloutlen == enclen );
2863  }
2864  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2865  totaloutlen += outlen;
2866  if( POLARSSL_MODE_CBC == cipher_info->mode )
2867  {
2868  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2869  }
2870  else
2871  {
2872  fct_chk( outlen == 0 );
2873  }
2874 
2875  /* decode the previously encoded string */
2876  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2877  if( POLARSSL_MODE_CBC == cipher_info->mode )
2878  {
2879  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2880  }
2881  else
2882  {
2883  fct_chk( enclen == outlen );
2884  }
2885  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2886  if( POLARSSL_MODE_CBC == cipher_info->mode )
2887  {
2888  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2889  }
2890  else
2891  {
2892  fct_chk( outlen == 0 );
2893  }
2894 
2895 
2896  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2897 
2898  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2899  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2900  FCT_TEST_END();
2901 #endif /* POLARSSL_DES_C */
2902 
2903 #ifdef POLARSSL_DES_C
2904 
2905  FCT_TEST_BGN(des_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
2906  size_t first_length = 16;
2907  size_t second_length = 16;
2908  size_t length = first_length + second_length;
2909  unsigned char key[32];
2910  unsigned char iv[16];
2911 
2912  cipher_context_t ctx_dec;
2913  cipher_context_t ctx_enc;
2914  const cipher_info_t *cipher_info;
2915 
2916  unsigned char inbuf[64];
2917  unsigned char encbuf[64];
2918  unsigned char decbuf[64];
2919 
2920  size_t outlen = 0;
2921  size_t totaloutlen = 0;
2922  size_t enclen = 0;
2923 
2924  memset( key, 0, 32 );
2925  memset( iv , 0, 16 );
2926 
2927  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2928  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2929 
2930  memset( inbuf, 5, 64 );
2931  memset( encbuf, 0, 64 );
2932  memset( decbuf, 0, 64 );
2933 
2934  /* Initialise enc and dec contexts */
2936  fct_chk( NULL != cipher_info);
2937 
2938  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2939  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2940 
2941  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 56, POLARSSL_DECRYPT ) );
2942  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 56, POLARSSL_ENCRYPT ) );
2943 
2944  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2945  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2946 
2947  if( POLARSSL_MODE_CBC == cipher_info->mode )
2948  {
2949  enclen = cipher_get_block_size(&ctx_enc )
2950  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2951  }
2952  else
2953  {
2954  enclen = length;
2955  }
2956 
2957  /* encode length number of bytes from inbuf */
2958  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2959  totaloutlen = outlen;
2960  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2961  totaloutlen += outlen;
2962  if( POLARSSL_MODE_CBC == cipher_info->mode )
2963  {
2964  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2965  }
2966  else
2967  {
2968  fct_chk( totaloutlen == enclen );
2969  }
2970  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2971  totaloutlen += outlen;
2972  if( POLARSSL_MODE_CBC == cipher_info->mode )
2973  {
2974  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2975  }
2976  else
2977  {
2978  fct_chk( outlen == 0 );
2979  }
2980 
2981  /* decode the previously encoded string */
2982  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2983  if( POLARSSL_MODE_CBC == cipher_info->mode )
2984  {
2985  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2986  }
2987  else
2988  {
2989  fct_chk( enclen == outlen );
2990  }
2991  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2992  if( POLARSSL_MODE_CBC == cipher_info->mode )
2993  {
2994  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2995  }
2996  else
2997  {
2998  fct_chk( outlen == 0 );
2999  }
3000 
3001 
3002  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3003 
3004  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3005  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3006  FCT_TEST_END();
3007 #endif /* POLARSSL_DES_C */
3008 
3009 #ifdef POLARSSL_DES_C
3010 
3011  FCT_TEST_BGN(des_encrypt_and_decrypt_0_bytes)
3012  size_t length = 0;
3013  unsigned char key[32];
3014  unsigned char iv[16];
3015 
3016  const cipher_info_t *cipher_info;
3017  cipher_context_t ctx_dec;
3018  cipher_context_t ctx_enc;
3019 
3020  unsigned char inbuf[64];
3021  unsigned char encbuf[64];
3022  unsigned char decbuf[64];
3023 
3024  size_t outlen = 0;
3025  size_t enclen = 0;
3026 
3027  memset( key, 0, 32 );
3028  memset( iv , 0, 16 );
3029 
3030  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3031  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3032 
3033  memset( inbuf, 5, 64 );
3034  memset( encbuf, 0, 64 );
3035  memset( decbuf, 0, 64 );
3036 
3037  /* Check and get info structures */
3039  fct_chk( NULL != cipher_info );
3040  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3041 
3042  /* Initialise enc and dec contexts */
3043  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3044  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3045 
3046  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3047  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3048 
3049  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3050  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3051 
3052  if( POLARSSL_MODE_CBC == cipher_info->mode )
3053  {
3054  enclen = cipher_get_block_size( &ctx_enc )
3055  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3056  }
3057  else
3058  {
3059  enclen = length;
3060  }
3061 
3062  /* encode length number of bytes from inbuf */
3063  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3064  if( POLARSSL_MODE_CBC == cipher_info->mode )
3065  {
3066  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3067  }
3068  else
3069  {
3070  fct_chk( outlen == enclen );
3071  }
3072 
3073  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3074  if( POLARSSL_MODE_CBC == cipher_info->mode )
3075  {
3076  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3077  }
3078  else
3079  {
3080  fct_chk( outlen == 0 );
3081  }
3082 
3083 
3084  /* decode the previously encoded string */
3085  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3086  if( POLARSSL_MODE_CBC == cipher_info->mode )
3087  {
3088  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3089  }
3090  else
3091  {
3092  fct_chk( enclen == outlen );
3093  }
3094 
3095  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3096  if( POLARSSL_MODE_CBC == cipher_info->mode )
3097  {
3098  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3099  }
3100  else
3101  {
3102  fct_chk( outlen == 0 );
3103  }
3104 
3105 
3106  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3107 
3108  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3109  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3110  FCT_TEST_END();
3111 #endif /* POLARSSL_DES_C */
3112 
3113 #ifdef POLARSSL_DES_C
3114 
3115  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_byte)
3116  size_t length = 1;
3117  unsigned char key[32];
3118  unsigned char iv[16];
3119 
3120  const cipher_info_t *cipher_info;
3121  cipher_context_t ctx_dec;
3122  cipher_context_t ctx_enc;
3123 
3124  unsigned char inbuf[64];
3125  unsigned char encbuf[64];
3126  unsigned char decbuf[64];
3127 
3128  size_t outlen = 0;
3129  size_t enclen = 0;
3130 
3131  memset( key, 0, 32 );
3132  memset( iv , 0, 16 );
3133 
3134  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3135  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3136 
3137  memset( inbuf, 5, 64 );
3138  memset( encbuf, 0, 64 );
3139  memset( decbuf, 0, 64 );
3140 
3141  /* Check and get info structures */
3143  fct_chk( NULL != cipher_info );
3144  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3145 
3146  /* Initialise enc and dec contexts */
3147  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3148  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3149 
3150  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3151  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3152 
3153  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3154  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3155 
3156  if( POLARSSL_MODE_CBC == cipher_info->mode )
3157  {
3158  enclen = cipher_get_block_size( &ctx_enc )
3159  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3160  }
3161  else
3162  {
3163  enclen = length;
3164  }
3165 
3166  /* encode length number of bytes from inbuf */
3167  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3168  if( POLARSSL_MODE_CBC == cipher_info->mode )
3169  {
3170  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3171  }
3172  else
3173  {
3174  fct_chk( outlen == enclen );
3175  }
3176 
3177  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3178  if( POLARSSL_MODE_CBC == cipher_info->mode )
3179  {
3180  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3181  }
3182  else
3183  {
3184  fct_chk( outlen == 0 );
3185  }
3186 
3187 
3188  /* decode the previously encoded string */
3189  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3190  if( POLARSSL_MODE_CBC == cipher_info->mode )
3191  {
3192  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3193  }
3194  else
3195  {
3196  fct_chk( enclen == outlen );
3197  }
3198 
3199  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3200  if( POLARSSL_MODE_CBC == cipher_info->mode )
3201  {
3202  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3203  }
3204  else
3205  {
3206  fct_chk( outlen == 0 );
3207  }
3208 
3209 
3210  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3211 
3212  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3213  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3214  FCT_TEST_END();
3215 #endif /* POLARSSL_DES_C */
3216 
3217 #ifdef POLARSSL_DES_C
3218 
3219  FCT_TEST_BGN(des3_encrypt_and_decrypt_2_bytes)
3220  size_t length = 2;
3221  unsigned char key[32];
3222  unsigned char iv[16];
3223 
3224  const cipher_info_t *cipher_info;
3225  cipher_context_t ctx_dec;
3226  cipher_context_t ctx_enc;
3227 
3228  unsigned char inbuf[64];
3229  unsigned char encbuf[64];
3230  unsigned char decbuf[64];
3231 
3232  size_t outlen = 0;
3233  size_t enclen = 0;
3234 
3235  memset( key, 0, 32 );
3236  memset( iv , 0, 16 );
3237 
3238  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3239  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3240 
3241  memset( inbuf, 5, 64 );
3242  memset( encbuf, 0, 64 );
3243  memset( decbuf, 0, 64 );
3244 
3245  /* Check and get info structures */
3247  fct_chk( NULL != cipher_info );
3248  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3249 
3250  /* Initialise enc and dec contexts */
3251  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3252  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3253 
3254  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3255  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3256 
3257  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3258  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3259 
3260  if( POLARSSL_MODE_CBC == cipher_info->mode )
3261  {
3262  enclen = cipher_get_block_size( &ctx_enc )
3263  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3264  }
3265  else
3266  {
3267  enclen = length;
3268  }
3269 
3270  /* encode length number of bytes from inbuf */
3271  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3272  if( POLARSSL_MODE_CBC == cipher_info->mode )
3273  {
3274  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3275  }
3276  else
3277  {
3278  fct_chk( outlen == enclen );
3279  }
3280 
3281  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3282  if( POLARSSL_MODE_CBC == cipher_info->mode )
3283  {
3284  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3285  }
3286  else
3287  {
3288  fct_chk( outlen == 0 );
3289  }
3290 
3291 
3292  /* decode the previously encoded string */
3293  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3294  if( POLARSSL_MODE_CBC == cipher_info->mode )
3295  {
3296  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3297  }
3298  else
3299  {
3300  fct_chk( enclen == outlen );
3301  }
3302 
3303  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3304  if( POLARSSL_MODE_CBC == cipher_info->mode )
3305  {
3306  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3307  }
3308  else
3309  {
3310  fct_chk( outlen == 0 );
3311  }
3312 
3313 
3314  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3315 
3316  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3317  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3318  FCT_TEST_END();
3319 #endif /* POLARSSL_DES_C */
3320 
3321 #ifdef POLARSSL_DES_C
3322 
3323  FCT_TEST_BGN(des3_encrypt_and_decrypt_7_bytes)
3324  size_t length = 7;
3325  unsigned char key[32];
3326  unsigned char iv[16];
3327 
3328  const cipher_info_t *cipher_info;
3329  cipher_context_t ctx_dec;
3330  cipher_context_t ctx_enc;
3331 
3332  unsigned char inbuf[64];
3333  unsigned char encbuf[64];
3334  unsigned char decbuf[64];
3335 
3336  size_t outlen = 0;
3337  size_t enclen = 0;
3338 
3339  memset( key, 0, 32 );
3340  memset( iv , 0, 16 );
3341 
3342  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3343  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3344 
3345  memset( inbuf, 5, 64 );
3346  memset( encbuf, 0, 64 );
3347  memset( decbuf, 0, 64 );
3348 
3349  /* Check and get info structures */
3351  fct_chk( NULL != cipher_info );
3352  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3353 
3354  /* Initialise enc and dec contexts */
3355  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3356  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3357 
3358  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3359  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3360 
3361  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3362  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3363 
3364  if( POLARSSL_MODE_CBC == cipher_info->mode )
3365  {
3366  enclen = cipher_get_block_size( &ctx_enc )
3367  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3368  }
3369  else
3370  {
3371  enclen = length;
3372  }
3373 
3374  /* encode length number of bytes from inbuf */
3375  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3376  if( POLARSSL_MODE_CBC == cipher_info->mode )
3377  {
3378  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3379  }
3380  else
3381  {
3382  fct_chk( outlen == enclen );
3383  }
3384 
3385  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3386  if( POLARSSL_MODE_CBC == cipher_info->mode )
3387  {
3388  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3389  }
3390  else
3391  {
3392  fct_chk( outlen == 0 );
3393  }
3394 
3395 
3396  /* decode the previously encoded string */
3397  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3398  if( POLARSSL_MODE_CBC == cipher_info->mode )
3399  {
3400  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3401  }
3402  else
3403  {
3404  fct_chk( enclen == outlen );
3405  }
3406 
3407  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3408  if( POLARSSL_MODE_CBC == cipher_info->mode )
3409  {
3410  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3411  }
3412  else
3413  {
3414  fct_chk( outlen == 0 );
3415  }
3416 
3417 
3418  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3419 
3420  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3421  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3422  FCT_TEST_END();
3423 #endif /* POLARSSL_DES_C */
3424 
3425 #ifdef POLARSSL_DES_C
3426 
3427  FCT_TEST_BGN(des3_encrypt_and_decrypt_8_bytes)
3428  size_t length = 8;
3429  unsigned char key[32];
3430  unsigned char iv[16];
3431 
3432  const cipher_info_t *cipher_info;
3433  cipher_context_t ctx_dec;
3434  cipher_context_t ctx_enc;
3435 
3436  unsigned char inbuf[64];
3437  unsigned char encbuf[64];
3438  unsigned char decbuf[64];
3439 
3440  size_t outlen = 0;
3441  size_t enclen = 0;
3442 
3443  memset( key, 0, 32 );
3444  memset( iv , 0, 16 );
3445 
3446  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3447  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3448 
3449  memset( inbuf, 5, 64 );
3450  memset( encbuf, 0, 64 );
3451  memset( decbuf, 0, 64 );
3452 
3453  /* Check and get info structures */
3455  fct_chk( NULL != cipher_info );
3456  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3457 
3458  /* Initialise enc and dec contexts */
3459  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3460  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3461 
3462  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3463  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3464 
3465  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3466  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3467 
3468  if( POLARSSL_MODE_CBC == cipher_info->mode )
3469  {
3470  enclen = cipher_get_block_size( &ctx_enc )
3471  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3472  }
3473  else
3474  {
3475  enclen = length;
3476  }
3477 
3478  /* encode length number of bytes from inbuf */
3479  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3480  if( POLARSSL_MODE_CBC == cipher_info->mode )
3481  {
3482  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3483  }
3484  else
3485  {
3486  fct_chk( outlen == enclen );
3487  }
3488 
3489  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3490  if( POLARSSL_MODE_CBC == cipher_info->mode )
3491  {
3492  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3493  }
3494  else
3495  {
3496  fct_chk( outlen == 0 );
3497  }
3498 
3499 
3500  /* decode the previously encoded string */
3501  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3502  if( POLARSSL_MODE_CBC == cipher_info->mode )
3503  {
3504  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3505  }
3506  else
3507  {
3508  fct_chk( enclen == outlen );
3509  }
3510 
3511  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3512  if( POLARSSL_MODE_CBC == cipher_info->mode )
3513  {
3514  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3515  }
3516  else
3517  {
3518  fct_chk( outlen == 0 );
3519  }
3520 
3521 
3522  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3523 
3524  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3525  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3526  FCT_TEST_END();
3527 #endif /* POLARSSL_DES_C */
3528 
3529 #ifdef POLARSSL_DES_C
3530 
3531  FCT_TEST_BGN(des3_encrypt_and_decrypt_9_bytes)
3532  size_t length = 9;
3533  unsigned char key[32];
3534  unsigned char iv[16];
3535 
3536  const cipher_info_t *cipher_info;
3537  cipher_context_t ctx_dec;
3538  cipher_context_t ctx_enc;
3539 
3540  unsigned char inbuf[64];
3541  unsigned char encbuf[64];
3542  unsigned char decbuf[64];
3543 
3544  size_t outlen = 0;
3545  size_t enclen = 0;
3546 
3547  memset( key, 0, 32 );
3548  memset( iv , 0, 16 );
3549 
3550  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3551  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3552 
3553  memset( inbuf, 5, 64 );
3554  memset( encbuf, 0, 64 );
3555  memset( decbuf, 0, 64 );
3556 
3557  /* Check and get info structures */
3559  fct_chk( NULL != cipher_info );
3560  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3561 
3562  /* Initialise enc and dec contexts */
3563  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3564  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3565 
3566  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3567  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3568 
3569  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3570  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3571 
3572  if( POLARSSL_MODE_CBC == cipher_info->mode )
3573  {
3574  enclen = cipher_get_block_size( &ctx_enc )
3575  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3576  }
3577  else
3578  {
3579  enclen = length;
3580  }
3581 
3582  /* encode length number of bytes from inbuf */
3583  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3584  if( POLARSSL_MODE_CBC == cipher_info->mode )
3585  {
3586  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3587  }
3588  else
3589  {
3590  fct_chk( outlen == enclen );
3591  }
3592 
3593  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3594  if( POLARSSL_MODE_CBC == cipher_info->mode )
3595  {
3596  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3597  }
3598  else
3599  {
3600  fct_chk( outlen == 0 );
3601  }
3602 
3603 
3604  /* decode the previously encoded string */
3605  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3606  if( POLARSSL_MODE_CBC == cipher_info->mode )
3607  {
3608  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3609  }
3610  else
3611  {
3612  fct_chk( enclen == outlen );
3613  }
3614 
3615  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3616  if( POLARSSL_MODE_CBC == cipher_info->mode )
3617  {
3618  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3619  }
3620  else
3621  {
3622  fct_chk( outlen == 0 );
3623  }
3624 
3625 
3626  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3627 
3628  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3629  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3630  FCT_TEST_END();
3631 #endif /* POLARSSL_DES_C */
3632 
3633 #ifdef POLARSSL_DES_C
3634 
3635  FCT_TEST_BGN(des3_encrypt_and_decrypt_15_bytes)
3636  size_t length = 15;
3637  unsigned char key[32];
3638  unsigned char iv[16];
3639 
3640  const cipher_info_t *cipher_info;
3641  cipher_context_t ctx_dec;
3642  cipher_context_t ctx_enc;
3643 
3644  unsigned char inbuf[64];
3645  unsigned char encbuf[64];
3646  unsigned char decbuf[64];
3647 
3648  size_t outlen = 0;
3649  size_t enclen = 0;
3650 
3651  memset( key, 0, 32 );
3652  memset( iv , 0, 16 );
3653 
3654  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3655  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3656 
3657  memset( inbuf, 5, 64 );
3658  memset( encbuf, 0, 64 );
3659  memset( decbuf, 0, 64 );
3660 
3661  /* Check and get info structures */
3663  fct_chk( NULL != cipher_info );
3664  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3665 
3666  /* Initialise enc and dec contexts */
3667  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3668  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3669 
3670  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3671  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3672 
3673  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3674  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3675 
3676  if( POLARSSL_MODE_CBC == cipher_info->mode )
3677  {
3678  enclen = cipher_get_block_size( &ctx_enc )
3679  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3680  }
3681  else
3682  {
3683  enclen = length;
3684  }
3685 
3686  /* encode length number of bytes from inbuf */
3687  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3688  if( POLARSSL_MODE_CBC == cipher_info->mode )
3689  {
3690  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3691  }
3692  else
3693  {
3694  fct_chk( outlen == enclen );
3695  }
3696 
3697  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3698  if( POLARSSL_MODE_CBC == cipher_info->mode )
3699  {
3700  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3701  }
3702  else
3703  {
3704  fct_chk( outlen == 0 );
3705  }
3706 
3707 
3708  /* decode the previously encoded string */
3709  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3710  if( POLARSSL_MODE_CBC == cipher_info->mode )
3711  {
3712  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3713  }
3714  else
3715  {
3716  fct_chk( enclen == outlen );
3717  }
3718 
3719  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3720  if( POLARSSL_MODE_CBC == cipher_info->mode )
3721  {
3722  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3723  }
3724  else
3725  {
3726  fct_chk( outlen == 0 );
3727  }
3728 
3729 
3730  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3731 
3732  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3733  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3734  FCT_TEST_END();
3735 #endif /* POLARSSL_DES_C */
3736 
3737 #ifdef POLARSSL_DES_C
3738 
3739  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes)
3740  size_t length = 16;
3741  unsigned char key[32];
3742  unsigned char iv[16];
3743 
3744  const cipher_info_t *cipher_info;
3745  cipher_context_t ctx_dec;
3746  cipher_context_t ctx_enc;
3747 
3748  unsigned char inbuf[64];
3749  unsigned char encbuf[64];
3750  unsigned char decbuf[64];
3751 
3752  size_t outlen = 0;
3753  size_t enclen = 0;
3754 
3755  memset( key, 0, 32 );
3756  memset( iv , 0, 16 );
3757 
3758  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3759  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3760 
3761  memset( inbuf, 5, 64 );
3762  memset( encbuf, 0, 64 );
3763  memset( decbuf, 0, 64 );
3764 
3765  /* Check and get info structures */
3767  fct_chk( NULL != cipher_info );
3768  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3769 
3770  /* Initialise enc and dec contexts */
3771  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3772  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3773 
3774  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3775  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3776 
3777  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3778  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3779 
3780  if( POLARSSL_MODE_CBC == cipher_info->mode )
3781  {
3782  enclen = cipher_get_block_size( &ctx_enc )
3783  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3784  }
3785  else
3786  {
3787  enclen = length;
3788  }
3789 
3790  /* encode length number of bytes from inbuf */
3791  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3792  if( POLARSSL_MODE_CBC == cipher_info->mode )
3793  {
3794  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3795  }
3796  else
3797  {
3798  fct_chk( outlen == enclen );
3799  }
3800 
3801  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3802  if( POLARSSL_MODE_CBC == cipher_info->mode )
3803  {
3804  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3805  }
3806  else
3807  {
3808  fct_chk( outlen == 0 );
3809  }
3810 
3811 
3812  /* decode the previously encoded string */
3813  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3814  if( POLARSSL_MODE_CBC == cipher_info->mode )
3815  {
3816  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3817  }
3818  else
3819  {
3820  fct_chk( enclen == outlen );
3821  }
3822 
3823  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3824  if( POLARSSL_MODE_CBC == cipher_info->mode )
3825  {
3826  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3827  }
3828  else
3829  {
3830  fct_chk( outlen == 0 );
3831  }
3832 
3833 
3834  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3835 
3836  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3837  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3838  FCT_TEST_END();
3839 #endif /* POLARSSL_DES_C */
3840 
3841 #ifdef POLARSSL_DES_C
3842 
3843  FCT_TEST_BGN(des3_encrypt_and_decrypt_17_bytes)
3844  size_t length = 17;
3845  unsigned char key[32];
3846  unsigned char iv[16];
3847 
3848  const cipher_info_t *cipher_info;
3849  cipher_context_t ctx_dec;
3850  cipher_context_t ctx_enc;
3851 
3852  unsigned char inbuf[64];
3853  unsigned char encbuf[64];
3854  unsigned char decbuf[64];
3855 
3856  size_t outlen = 0;
3857  size_t enclen = 0;
3858 
3859  memset( key, 0, 32 );
3860  memset( iv , 0, 16 );
3861 
3862  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3863  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3864 
3865  memset( inbuf, 5, 64 );
3866  memset( encbuf, 0, 64 );
3867  memset( decbuf, 0, 64 );
3868 
3869  /* Check and get info structures */
3871  fct_chk( NULL != cipher_info );
3872  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3873 
3874  /* Initialise enc and dec contexts */
3875  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3876  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3877 
3878  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3879  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3880 
3881  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3882  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3883 
3884  if( POLARSSL_MODE_CBC == cipher_info->mode )
3885  {
3886  enclen = cipher_get_block_size( &ctx_enc )
3887  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3888  }
3889  else
3890  {
3891  enclen = length;
3892  }
3893 
3894  /* encode length number of bytes from inbuf */
3895  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
3896  if( POLARSSL_MODE_CBC == cipher_info->mode )
3897  {
3898  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
3899  }
3900  else
3901  {
3902  fct_chk( outlen == enclen );
3903  }
3904 
3905  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
3906  if( POLARSSL_MODE_CBC == cipher_info->mode )
3907  {
3908  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
3909  }
3910  else
3911  {
3912  fct_chk( outlen == 0 );
3913  }
3914 
3915 
3916  /* decode the previously encoded string */
3917  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
3918  if( POLARSSL_MODE_CBC == cipher_info->mode )
3919  {
3920  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
3921  }
3922  else
3923  {
3924  fct_chk( enclen == outlen );
3925  }
3926 
3927  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
3928  if( POLARSSL_MODE_CBC == cipher_info->mode )
3929  {
3930  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
3931  }
3932  else
3933  {
3934  fct_chk( outlen == 0 );
3935  }
3936 
3937 
3938  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
3939 
3940  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
3941  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
3942  FCT_TEST_END();
3943 #endif /* POLARSSL_DES_C */
3944 
3945 #ifdef POLARSSL_DES_C
3946 
3947  FCT_TEST_BGN(des3_encrypt_and_decrypt_31_bytes)
3948  size_t length = 31;
3949  unsigned char key[32];
3950  unsigned char iv[16];
3951 
3952  const cipher_info_t *cipher_info;
3953  cipher_context_t ctx_dec;
3954  cipher_context_t ctx_enc;
3955 
3956  unsigned char inbuf[64];
3957  unsigned char encbuf[64];
3958  unsigned char decbuf[64];
3959 
3960  size_t outlen = 0;
3961  size_t enclen = 0;
3962 
3963  memset( key, 0, 32 );
3964  memset( iv , 0, 16 );
3965 
3966  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
3967  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
3968 
3969  memset( inbuf, 5, 64 );
3970  memset( encbuf, 0, 64 );
3971  memset( decbuf, 0, 64 );
3972 
3973  /* Check and get info structures */
3975  fct_chk( NULL != cipher_info );
3976  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
3977 
3978  /* Initialise enc and dec contexts */
3979  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
3980  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
3981 
3982  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
3983  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
3984 
3985  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
3986  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
3987 
3988  if( POLARSSL_MODE_CBC == cipher_info->mode )
3989  {
3990  enclen = cipher_get_block_size( &ctx_enc )
3991  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
3992  }
3993  else
3994  {
3995  enclen = length;
3996  }
3997 
3998  /* encode length number of bytes from inbuf */
3999  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4000  if( POLARSSL_MODE_CBC == cipher_info->mode )
4001  {
4002  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4003  }
4004  else
4005  {
4006  fct_chk( outlen == enclen );
4007  }
4008 
4009  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4010  if( POLARSSL_MODE_CBC == cipher_info->mode )
4011  {
4012  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4013  }
4014  else
4015  {
4016  fct_chk( outlen == 0 );
4017  }
4018 
4019 
4020  /* decode the previously encoded string */
4021  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4022  if( POLARSSL_MODE_CBC == cipher_info->mode )
4023  {
4024  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4025  }
4026  else
4027  {
4028  fct_chk( enclen == outlen );
4029  }
4030 
4031  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4032  if( POLARSSL_MODE_CBC == cipher_info->mode )
4033  {
4034  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4035  }
4036  else
4037  {
4038  fct_chk( outlen == 0 );
4039  }
4040 
4041 
4042  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4043 
4044  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4045  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4046  FCT_TEST_END();
4047 #endif /* POLARSSL_DES_C */
4048 
4049 #ifdef POLARSSL_DES_C
4050 
4051  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes)
4052  size_t length = 32;
4053  unsigned char key[32];
4054  unsigned char iv[16];
4055 
4056  const cipher_info_t *cipher_info;
4057  cipher_context_t ctx_dec;
4058  cipher_context_t ctx_enc;
4059 
4060  unsigned char inbuf[64];
4061  unsigned char encbuf[64];
4062  unsigned char decbuf[64];
4063 
4064  size_t outlen = 0;
4065  size_t enclen = 0;
4066 
4067  memset( key, 0, 32 );
4068  memset( iv , 0, 16 );
4069 
4070  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4071  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4072 
4073  memset( inbuf, 5, 64 );
4074  memset( encbuf, 0, 64 );
4075  memset( decbuf, 0, 64 );
4076 
4077  /* Check and get info structures */
4079  fct_chk( NULL != cipher_info );
4080  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
4081 
4082  /* Initialise enc and dec contexts */
4083  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4084  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4085 
4086  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4087  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4088 
4089  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4090  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4091 
4092  if( POLARSSL_MODE_CBC == cipher_info->mode )
4093  {
4094  enclen = cipher_get_block_size( &ctx_enc )
4095  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4096  }
4097  else
4098  {
4099  enclen = length;
4100  }
4101 
4102  /* encode length number of bytes from inbuf */
4103  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4104  if( POLARSSL_MODE_CBC == cipher_info->mode )
4105  {
4106  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4107  }
4108  else
4109  {
4110  fct_chk( outlen == enclen );
4111  }
4112 
4113  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4114  if( POLARSSL_MODE_CBC == cipher_info->mode )
4115  {
4116  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4117  }
4118  else
4119  {
4120  fct_chk( outlen == 0 );
4121  }
4122 
4123 
4124  /* decode the previously encoded string */
4125  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4126  if( POLARSSL_MODE_CBC == cipher_info->mode )
4127  {
4128  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4129  }
4130  else
4131  {
4132  fct_chk( enclen == outlen );
4133  }
4134 
4135  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4136  if( POLARSSL_MODE_CBC == cipher_info->mode )
4137  {
4138  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4139  }
4140  else
4141  {
4142  fct_chk( outlen == 0 );
4143  }
4144 
4145 
4146  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4147 
4148  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4149  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4150  FCT_TEST_END();
4151 #endif /* POLARSSL_DES_C */
4152 
4153 #ifdef POLARSSL_DES_C
4154 
4155  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes)
4156  size_t length = 33;
4157  unsigned char key[32];
4158  unsigned char iv[16];
4159 
4160  const cipher_info_t *cipher_info;
4161  cipher_context_t ctx_dec;
4162  cipher_context_t ctx_enc;
4163 
4164  unsigned char inbuf[64];
4165  unsigned char encbuf[64];
4166  unsigned char decbuf[64];
4167 
4168  size_t outlen = 0;
4169  size_t enclen = 0;
4170 
4171  memset( key, 0, 32 );
4172  memset( iv , 0, 16 );
4173 
4174  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4175  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4176 
4177  memset( inbuf, 5, 64 );
4178  memset( encbuf, 0, 64 );
4179  memset( decbuf, 0, 64 );
4180 
4181  /* Check and get info structures */
4183  fct_chk( NULL != cipher_info );
4184  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
4185 
4186  /* Initialise enc and dec contexts */
4187  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4188  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4189 
4190  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4191  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4192 
4193  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4194  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4195 
4196  if( POLARSSL_MODE_CBC == cipher_info->mode )
4197  {
4198  enclen = cipher_get_block_size( &ctx_enc )
4199  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4200  }
4201  else
4202  {
4203  enclen = length;
4204  }
4205 
4206  /* encode length number of bytes from inbuf */
4207  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4208  if( POLARSSL_MODE_CBC == cipher_info->mode )
4209  {
4210  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4211  }
4212  else
4213  {
4214  fct_chk( outlen == enclen );
4215  }
4216 
4217  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4218  if( POLARSSL_MODE_CBC == cipher_info->mode )
4219  {
4220  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4221  }
4222  else
4223  {
4224  fct_chk( outlen == 0 );
4225  }
4226 
4227 
4228  /* decode the previously encoded string */
4229  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4230  if( POLARSSL_MODE_CBC == cipher_info->mode )
4231  {
4232  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4233  }
4234  else
4235  {
4236  fct_chk( enclen == outlen );
4237  }
4238 
4239  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4240  if( POLARSSL_MODE_CBC == cipher_info->mode )
4241  {
4242  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4243  }
4244  else
4245  {
4246  fct_chk( outlen == 0 );
4247  }
4248 
4249 
4250  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4251 
4252  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4253  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4254  FCT_TEST_END();
4255 #endif /* POLARSSL_DES_C */
4256 
4257 #ifdef POLARSSL_DES_C
4258 
4259  FCT_TEST_BGN(des3_encrypt_and_decrypt_47_bytes)
4260  size_t length = 47;
4261  unsigned char key[32];
4262  unsigned char iv[16];
4263 
4264  const cipher_info_t *cipher_info;
4265  cipher_context_t ctx_dec;
4266  cipher_context_t ctx_enc;
4267 
4268  unsigned char inbuf[64];
4269  unsigned char encbuf[64];
4270  unsigned char decbuf[64];
4271 
4272  size_t outlen = 0;
4273  size_t enclen = 0;
4274 
4275  memset( key, 0, 32 );
4276  memset( iv , 0, 16 );
4277 
4278  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4279  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4280 
4281  memset( inbuf, 5, 64 );
4282  memset( encbuf, 0, 64 );
4283  memset( decbuf, 0, 64 );
4284 
4285  /* Check and get info structures */
4287  fct_chk( NULL != cipher_info );
4288  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
4289 
4290  /* Initialise enc and dec contexts */
4291  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4292  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4293 
4294  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4295  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4296 
4297  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4298  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4299 
4300  if( POLARSSL_MODE_CBC == cipher_info->mode )
4301  {
4302  enclen = cipher_get_block_size( &ctx_enc )
4303  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4304  }
4305  else
4306  {
4307  enclen = length;
4308  }
4309 
4310  /* encode length number of bytes from inbuf */
4311  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4312  if( POLARSSL_MODE_CBC == cipher_info->mode )
4313  {
4314  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4315  }
4316  else
4317  {
4318  fct_chk( outlen == enclen );
4319  }
4320 
4321  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4322  if( POLARSSL_MODE_CBC == cipher_info->mode )
4323  {
4324  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4325  }
4326  else
4327  {
4328  fct_chk( outlen == 0 );
4329  }
4330 
4331 
4332  /* decode the previously encoded string */
4333  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4334  if( POLARSSL_MODE_CBC == cipher_info->mode )
4335  {
4336  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4337  }
4338  else
4339  {
4340  fct_chk( enclen == outlen );
4341  }
4342 
4343  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4344  if( POLARSSL_MODE_CBC == cipher_info->mode )
4345  {
4346  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4347  }
4348  else
4349  {
4350  fct_chk( outlen == 0 );
4351  }
4352 
4353 
4354  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4355 
4356  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4357  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4358  FCT_TEST_END();
4359 #endif /* POLARSSL_DES_C */
4360 
4361 #ifdef POLARSSL_DES_C
4362 
4363  FCT_TEST_BGN(des3_encrypt_and_decrypt_48_bytes)
4364  size_t length = 48;
4365  unsigned char key[32];
4366  unsigned char iv[16];
4367 
4368  const cipher_info_t *cipher_info;
4369  cipher_context_t ctx_dec;
4370  cipher_context_t ctx_enc;
4371 
4372  unsigned char inbuf[64];
4373  unsigned char encbuf[64];
4374  unsigned char decbuf[64];
4375 
4376  size_t outlen = 0;
4377  size_t enclen = 0;
4378 
4379  memset( key, 0, 32 );
4380  memset( iv , 0, 16 );
4381 
4382  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4383  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4384 
4385  memset( inbuf, 5, 64 );
4386  memset( encbuf, 0, 64 );
4387  memset( decbuf, 0, 64 );
4388 
4389  /* Check and get info structures */
4391  fct_chk( NULL != cipher_info );
4392  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
4393 
4394  /* Initialise enc and dec contexts */
4395  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4396  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4397 
4398  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4399  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4400 
4401  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4402  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4403 
4404  if( POLARSSL_MODE_CBC == cipher_info->mode )
4405  {
4406  enclen = cipher_get_block_size( &ctx_enc )
4407  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4408  }
4409  else
4410  {
4411  enclen = length;
4412  }
4413 
4414  /* encode length number of bytes from inbuf */
4415  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4416  if( POLARSSL_MODE_CBC == cipher_info->mode )
4417  {
4418  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4419  }
4420  else
4421  {
4422  fct_chk( outlen == enclen );
4423  }
4424 
4425  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4426  if( POLARSSL_MODE_CBC == cipher_info->mode )
4427  {
4428  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4429  }
4430  else
4431  {
4432  fct_chk( outlen == 0 );
4433  }
4434 
4435 
4436  /* decode the previously encoded string */
4437  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4438  if( POLARSSL_MODE_CBC == cipher_info->mode )
4439  {
4440  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4441  }
4442  else
4443  {
4444  fct_chk( enclen == outlen );
4445  }
4446 
4447  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4448  if( POLARSSL_MODE_CBC == cipher_info->mode )
4449  {
4450  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4451  }
4452  else
4453  {
4454  fct_chk( outlen == 0 );
4455  }
4456 
4457 
4458  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4459 
4460  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4461  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4462  FCT_TEST_END();
4463 #endif /* POLARSSL_DES_C */
4464 
4465 #ifdef POLARSSL_DES_C
4466 
4467  FCT_TEST_BGN(des3_encrypt_and_decrypt_49_bytes)
4468  size_t length = 49;
4469  unsigned char key[32];
4470  unsigned char iv[16];
4471 
4472  const cipher_info_t *cipher_info;
4473  cipher_context_t ctx_dec;
4474  cipher_context_t ctx_enc;
4475 
4476  unsigned char inbuf[64];
4477  unsigned char encbuf[64];
4478  unsigned char decbuf[64];
4479 
4480  size_t outlen = 0;
4481  size_t enclen = 0;
4482 
4483  memset( key, 0, 32 );
4484  memset( iv , 0, 16 );
4485 
4486  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4487  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4488 
4489  memset( inbuf, 5, 64 );
4490  memset( encbuf, 0, 64 );
4491  memset( decbuf, 0, 64 );
4492 
4493  /* Check and get info structures */
4495  fct_chk( NULL != cipher_info );
4496  fct_chk( cipher_info_from_string( "DES-EDE-CBC" ) == cipher_info );
4497 
4498  /* Initialise enc and dec contexts */
4499  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4500  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4501 
4502  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4503  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4504 
4505  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4506  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4507 
4508  if( POLARSSL_MODE_CBC == cipher_info->mode )
4509  {
4510  enclen = cipher_get_block_size( &ctx_enc )
4511  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4512  }
4513  else
4514  {
4515  enclen = length;
4516  }
4517 
4518  /* encode length number of bytes from inbuf */
4519  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
4520  if( POLARSSL_MODE_CBC == cipher_info->mode )
4521  {
4522  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4523  }
4524  else
4525  {
4526  fct_chk( outlen == enclen );
4527  }
4528 
4529  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
4530  if( POLARSSL_MODE_CBC == cipher_info->mode )
4531  {
4532  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4533  }
4534  else
4535  {
4536  fct_chk( outlen == 0 );
4537  }
4538 
4539 
4540  /* decode the previously encoded string */
4541  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4542  if( POLARSSL_MODE_CBC == cipher_info->mode )
4543  {
4544  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4545  }
4546  else
4547  {
4548  fct_chk( enclen == outlen );
4549  }
4550 
4551  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4552  if( POLARSSL_MODE_CBC == cipher_info->mode )
4553  {
4554  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4555  }
4556  else
4557  {
4558  fct_chk( outlen == 0 );
4559  }
4560 
4561 
4562  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4563 
4564  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4565  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4566  FCT_TEST_END();
4567 #endif /* POLARSSL_DES_C */
4568 
4569 #ifdef POLARSSL_DES_C
4570 
4571  FCT_TEST_BGN(des3_encrypt_and_decrypt_0_bytes_in_multiple_parts)
4572  size_t first_length = 0;
4573  size_t second_length = 0;
4574  size_t length = first_length + second_length;
4575  unsigned char key[32];
4576  unsigned char iv[16];
4577 
4578  cipher_context_t ctx_dec;
4579  cipher_context_t ctx_enc;
4580  const cipher_info_t *cipher_info;
4581 
4582  unsigned char inbuf[64];
4583  unsigned char encbuf[64];
4584  unsigned char decbuf[64];
4585 
4586  size_t outlen = 0;
4587  size_t totaloutlen = 0;
4588  size_t enclen = 0;
4589 
4590  memset( key, 0, 32 );
4591  memset( iv , 0, 16 );
4592 
4593  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4594  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4595 
4596  memset( inbuf, 5, 64 );
4597  memset( encbuf, 0, 64 );
4598  memset( decbuf, 0, 64 );
4599 
4600  /* Initialise enc and dec contexts */
4602  fct_chk( NULL != cipher_info);
4603 
4604  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4605  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4606 
4607  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4608  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4609 
4610  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4611  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4612 
4613  if( POLARSSL_MODE_CBC == cipher_info->mode )
4614  {
4615  enclen = cipher_get_block_size(&ctx_enc )
4616  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4617  }
4618  else
4619  {
4620  enclen = length;
4621  }
4622 
4623  /* encode length number of bytes from inbuf */
4624  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4625  totaloutlen = outlen;
4626  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4627  totaloutlen += outlen;
4628  if( POLARSSL_MODE_CBC == cipher_info->mode )
4629  {
4630  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4631  }
4632  else
4633  {
4634  fct_chk( totaloutlen == enclen );
4635  }
4636  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4637  totaloutlen += outlen;
4638  if( POLARSSL_MODE_CBC == cipher_info->mode )
4639  {
4640  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4641  }
4642  else
4643  {
4644  fct_chk( outlen == 0 );
4645  }
4646 
4647  /* decode the previously encoded string */
4648  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4649  if( POLARSSL_MODE_CBC == cipher_info->mode )
4650  {
4651  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4652  }
4653  else
4654  {
4655  fct_chk( enclen == outlen );
4656  }
4657  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4658  if( POLARSSL_MODE_CBC == cipher_info->mode )
4659  {
4660  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4661  }
4662  else
4663  {
4664  fct_chk( outlen == 0 );
4665  }
4666 
4667 
4668  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4669 
4670  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4671  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4672  FCT_TEST_END();
4673 #endif /* POLARSSL_DES_C */
4674 
4675 #ifdef POLARSSL_DES_C
4676 
4677  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
4678  size_t first_length = 1;
4679  size_t second_length = 0;
4680  size_t length = first_length + second_length;
4681  unsigned char key[32];
4682  unsigned char iv[16];
4683 
4684  cipher_context_t ctx_dec;
4685  cipher_context_t ctx_enc;
4686  const cipher_info_t *cipher_info;
4687 
4688  unsigned char inbuf[64];
4689  unsigned char encbuf[64];
4690  unsigned char decbuf[64];
4691 
4692  size_t outlen = 0;
4693  size_t totaloutlen = 0;
4694  size_t enclen = 0;
4695 
4696  memset( key, 0, 32 );
4697  memset( iv , 0, 16 );
4698 
4699  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4700  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4701 
4702  memset( inbuf, 5, 64 );
4703  memset( encbuf, 0, 64 );
4704  memset( decbuf, 0, 64 );
4705 
4706  /* Initialise enc and dec contexts */
4708  fct_chk( NULL != cipher_info);
4709 
4710  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4711  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4712 
4713  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4714  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4715 
4716  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4717  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4718 
4719  if( POLARSSL_MODE_CBC == cipher_info->mode )
4720  {
4721  enclen = cipher_get_block_size(&ctx_enc )
4722  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4723  }
4724  else
4725  {
4726  enclen = length;
4727  }
4728 
4729  /* encode length number of bytes from inbuf */
4730  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4731  totaloutlen = outlen;
4732  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4733  totaloutlen += outlen;
4734  if( POLARSSL_MODE_CBC == cipher_info->mode )
4735  {
4736  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4737  }
4738  else
4739  {
4740  fct_chk( totaloutlen == enclen );
4741  }
4742  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4743  totaloutlen += outlen;
4744  if( POLARSSL_MODE_CBC == cipher_info->mode )
4745  {
4746  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4747  }
4748  else
4749  {
4750  fct_chk( outlen == 0 );
4751  }
4752 
4753  /* decode the previously encoded string */
4754  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4755  if( POLARSSL_MODE_CBC == cipher_info->mode )
4756  {
4757  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4758  }
4759  else
4760  {
4761  fct_chk( enclen == outlen );
4762  }
4763  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4764  if( POLARSSL_MODE_CBC == cipher_info->mode )
4765  {
4766  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4767  }
4768  else
4769  {
4770  fct_chk( outlen == 0 );
4771  }
4772 
4773 
4774  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4775 
4776  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4777  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4778  FCT_TEST_END();
4779 #endif /* POLARSSL_DES_C */
4780 
4781 #ifdef POLARSSL_DES_C
4782 
4783  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
4784  size_t first_length = 0;
4785  size_t second_length = 1;
4786  size_t length = first_length + second_length;
4787  unsigned char key[32];
4788  unsigned char iv[16];
4789 
4790  cipher_context_t ctx_dec;
4791  cipher_context_t ctx_enc;
4792  const cipher_info_t *cipher_info;
4793 
4794  unsigned char inbuf[64];
4795  unsigned char encbuf[64];
4796  unsigned char decbuf[64];
4797 
4798  size_t outlen = 0;
4799  size_t totaloutlen = 0;
4800  size_t enclen = 0;
4801 
4802  memset( key, 0, 32 );
4803  memset( iv , 0, 16 );
4804 
4805  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4806  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4807 
4808  memset( inbuf, 5, 64 );
4809  memset( encbuf, 0, 64 );
4810  memset( decbuf, 0, 64 );
4811 
4812  /* Initialise enc and dec contexts */
4814  fct_chk( NULL != cipher_info);
4815 
4816  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4817  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4818 
4819  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4820  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4821 
4822  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4823  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4824 
4825  if( POLARSSL_MODE_CBC == cipher_info->mode )
4826  {
4827  enclen = cipher_get_block_size(&ctx_enc )
4828  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4829  }
4830  else
4831  {
4832  enclen = length;
4833  }
4834 
4835  /* encode length number of bytes from inbuf */
4836  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4837  totaloutlen = outlen;
4838  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4839  totaloutlen += outlen;
4840  if( POLARSSL_MODE_CBC == cipher_info->mode )
4841  {
4842  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4843  }
4844  else
4845  {
4846  fct_chk( totaloutlen == enclen );
4847  }
4848  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4849  totaloutlen += outlen;
4850  if( POLARSSL_MODE_CBC == cipher_info->mode )
4851  {
4852  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4853  }
4854  else
4855  {
4856  fct_chk( outlen == 0 );
4857  }
4858 
4859  /* decode the previously encoded string */
4860  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4861  if( POLARSSL_MODE_CBC == cipher_info->mode )
4862  {
4863  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4864  }
4865  else
4866  {
4867  fct_chk( enclen == outlen );
4868  }
4869  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4870  if( POLARSSL_MODE_CBC == cipher_info->mode )
4871  {
4872  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4873  }
4874  else
4875  {
4876  fct_chk( outlen == 0 );
4877  }
4878 
4879 
4880  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4881 
4882  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4883  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4884  FCT_TEST_END();
4885 #endif /* POLARSSL_DES_C */
4886 
4887 #ifdef POLARSSL_DES_C
4888 
4889  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
4890  size_t first_length = 16;
4891  size_t second_length = 0;
4892  size_t length = first_length + second_length;
4893  unsigned char key[32];
4894  unsigned char iv[16];
4895 
4896  cipher_context_t ctx_dec;
4897  cipher_context_t ctx_enc;
4898  const cipher_info_t *cipher_info;
4899 
4900  unsigned char inbuf[64];
4901  unsigned char encbuf[64];
4902  unsigned char decbuf[64];
4903 
4904  size_t outlen = 0;
4905  size_t totaloutlen = 0;
4906  size_t enclen = 0;
4907 
4908  memset( key, 0, 32 );
4909  memset( iv , 0, 16 );
4910 
4911  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
4912  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
4913 
4914  memset( inbuf, 5, 64 );
4915  memset( encbuf, 0, 64 );
4916  memset( decbuf, 0, 64 );
4917 
4918  /* Initialise enc and dec contexts */
4920  fct_chk( NULL != cipher_info);
4921 
4922  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
4923  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
4924 
4925  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
4926  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
4927 
4928  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
4929  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
4930 
4931  if( POLARSSL_MODE_CBC == cipher_info->mode )
4932  {
4933  enclen = cipher_get_block_size(&ctx_enc )
4934  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
4935  }
4936  else
4937  {
4938  enclen = length;
4939  }
4940 
4941  /* encode length number of bytes from inbuf */
4942  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
4943  totaloutlen = outlen;
4944  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
4945  totaloutlen += outlen;
4946  if( POLARSSL_MODE_CBC == cipher_info->mode )
4947  {
4948  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
4949  }
4950  else
4951  {
4952  fct_chk( totaloutlen == enclen );
4953  }
4954  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
4955  totaloutlen += outlen;
4956  if( POLARSSL_MODE_CBC == cipher_info->mode )
4957  {
4958  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
4959  }
4960  else
4961  {
4962  fct_chk( outlen == 0 );
4963  }
4964 
4965  /* decode the previously encoded string */
4966  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
4967  if( POLARSSL_MODE_CBC == cipher_info->mode )
4968  {
4969  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
4970  }
4971  else
4972  {
4973  fct_chk( enclen == outlen );
4974  }
4975  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
4976  if( POLARSSL_MODE_CBC == cipher_info->mode )
4977  {
4978  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
4979  }
4980  else
4981  {
4982  fct_chk( outlen == 0 );
4983  }
4984 
4985 
4986  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
4987 
4988  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
4989  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
4990  FCT_TEST_END();
4991 #endif /* POLARSSL_DES_C */
4992 
4993 #ifdef POLARSSL_DES_C
4994 
4995  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
4996  size_t first_length = 0;
4997  size_t second_length = 16;
4998  size_t length = first_length + second_length;
4999  unsigned char key[32];
5000  unsigned char iv[16];
5001 
5002  cipher_context_t ctx_dec;
5003  cipher_context_t ctx_enc;
5004  const cipher_info_t *cipher_info;
5005 
5006  unsigned char inbuf[64];
5007  unsigned char encbuf[64];
5008  unsigned char decbuf[64];
5009 
5010  size_t outlen = 0;
5011  size_t totaloutlen = 0;
5012  size_t enclen = 0;
5013 
5014  memset( key, 0, 32 );
5015  memset( iv , 0, 16 );
5016 
5017  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5018  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5019 
5020  memset( inbuf, 5, 64 );
5021  memset( encbuf, 0, 64 );
5022  memset( decbuf, 0, 64 );
5023 
5024  /* Initialise enc and dec contexts */
5026  fct_chk( NULL != cipher_info);
5027 
5028  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5029  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5030 
5031  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5032  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5033 
5034  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5035  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5036 
5037  if( POLARSSL_MODE_CBC == cipher_info->mode )
5038  {
5039  enclen = cipher_get_block_size(&ctx_enc )
5040  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5041  }
5042  else
5043  {
5044  enclen = length;
5045  }
5046 
5047  /* encode length number of bytes from inbuf */
5048  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5049  totaloutlen = outlen;
5050  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5051  totaloutlen += outlen;
5052  if( POLARSSL_MODE_CBC == cipher_info->mode )
5053  {
5054  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5055  }
5056  else
5057  {
5058  fct_chk( totaloutlen == enclen );
5059  }
5060  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5061  totaloutlen += outlen;
5062  if( POLARSSL_MODE_CBC == cipher_info->mode )
5063  {
5064  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5065  }
5066  else
5067  {
5068  fct_chk( outlen == 0 );
5069  }
5070 
5071  /* decode the previously encoded string */
5072  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5073  if( POLARSSL_MODE_CBC == cipher_info->mode )
5074  {
5075  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5076  }
5077  else
5078  {
5079  fct_chk( enclen == outlen );
5080  }
5081  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5082  if( POLARSSL_MODE_CBC == cipher_info->mode )
5083  {
5084  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5085  }
5086  else
5087  {
5088  fct_chk( outlen == 0 );
5089  }
5090 
5091 
5092  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5093 
5094  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5095  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5096  FCT_TEST_END();
5097 #endif /* POLARSSL_DES_C */
5098 
5099 #ifdef POLARSSL_DES_C
5100 
5101  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
5102  size_t first_length = 1;
5103  size_t second_length = 15;
5104  size_t length = first_length + second_length;
5105  unsigned char key[32];
5106  unsigned char iv[16];
5107 
5108  cipher_context_t ctx_dec;
5109  cipher_context_t ctx_enc;
5110  const cipher_info_t *cipher_info;
5111 
5112  unsigned char inbuf[64];
5113  unsigned char encbuf[64];
5114  unsigned char decbuf[64];
5115 
5116  size_t outlen = 0;
5117  size_t totaloutlen = 0;
5118  size_t enclen = 0;
5119 
5120  memset( key, 0, 32 );
5121  memset( iv , 0, 16 );
5122 
5123  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5124  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5125 
5126  memset( inbuf, 5, 64 );
5127  memset( encbuf, 0, 64 );
5128  memset( decbuf, 0, 64 );
5129 
5130  /* Initialise enc and dec contexts */
5132  fct_chk( NULL != cipher_info);
5133 
5134  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5135  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5136 
5137  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5138  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5139 
5140  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5141  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5142 
5143  if( POLARSSL_MODE_CBC == cipher_info->mode )
5144  {
5145  enclen = cipher_get_block_size(&ctx_enc )
5146  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5147  }
5148  else
5149  {
5150  enclen = length;
5151  }
5152 
5153  /* encode length number of bytes from inbuf */
5154  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5155  totaloutlen = outlen;
5156  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5157  totaloutlen += outlen;
5158  if( POLARSSL_MODE_CBC == cipher_info->mode )
5159  {
5160  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5161  }
5162  else
5163  {
5164  fct_chk( totaloutlen == enclen );
5165  }
5166  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5167  totaloutlen += outlen;
5168  if( POLARSSL_MODE_CBC == cipher_info->mode )
5169  {
5170  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5171  }
5172  else
5173  {
5174  fct_chk( outlen == 0 );
5175  }
5176 
5177  /* decode the previously encoded string */
5178  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5179  if( POLARSSL_MODE_CBC == cipher_info->mode )
5180  {
5181  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5182  }
5183  else
5184  {
5185  fct_chk( enclen == outlen );
5186  }
5187  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5188  if( POLARSSL_MODE_CBC == cipher_info->mode )
5189  {
5190  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5191  }
5192  else
5193  {
5194  fct_chk( outlen == 0 );
5195  }
5196 
5197 
5198  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5199 
5200  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5201  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5202  FCT_TEST_END();
5203 #endif /* POLARSSL_DES_C */
5204 
5205 #ifdef POLARSSL_DES_C
5206 
5207  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
5208  size_t first_length = 15;
5209  size_t second_length = 1;
5210  size_t length = first_length + second_length;
5211  unsigned char key[32];
5212  unsigned char iv[16];
5213 
5214  cipher_context_t ctx_dec;
5215  cipher_context_t ctx_enc;
5216  const cipher_info_t *cipher_info;
5217 
5218  unsigned char inbuf[64];
5219  unsigned char encbuf[64];
5220  unsigned char decbuf[64];
5221 
5222  size_t outlen = 0;
5223  size_t totaloutlen = 0;
5224  size_t enclen = 0;
5225 
5226  memset( key, 0, 32 );
5227  memset( iv , 0, 16 );
5228 
5229  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5230  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5231 
5232  memset( inbuf, 5, 64 );
5233  memset( encbuf, 0, 64 );
5234  memset( decbuf, 0, 64 );
5235 
5236  /* Initialise enc and dec contexts */
5238  fct_chk( NULL != cipher_info);
5239 
5240  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5241  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5242 
5243  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5244  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5245 
5246  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5247  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5248 
5249  if( POLARSSL_MODE_CBC == cipher_info->mode )
5250  {
5251  enclen = cipher_get_block_size(&ctx_enc )
5252  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5253  }
5254  else
5255  {
5256  enclen = length;
5257  }
5258 
5259  /* encode length number of bytes from inbuf */
5260  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5261  totaloutlen = outlen;
5262  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5263  totaloutlen += outlen;
5264  if( POLARSSL_MODE_CBC == cipher_info->mode )
5265  {
5266  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5267  }
5268  else
5269  {
5270  fct_chk( totaloutlen == enclen );
5271  }
5272  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5273  totaloutlen += outlen;
5274  if( POLARSSL_MODE_CBC == cipher_info->mode )
5275  {
5276  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5277  }
5278  else
5279  {
5280  fct_chk( outlen == 0 );
5281  }
5282 
5283  /* decode the previously encoded string */
5284  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5285  if( POLARSSL_MODE_CBC == cipher_info->mode )
5286  {
5287  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5288  }
5289  else
5290  {
5291  fct_chk( enclen == outlen );
5292  }
5293  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5294  if( POLARSSL_MODE_CBC == cipher_info->mode )
5295  {
5296  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5297  }
5298  else
5299  {
5300  fct_chk( outlen == 0 );
5301  }
5302 
5303 
5304  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5305 
5306  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5307  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5308  FCT_TEST_END();
5309 #endif /* POLARSSL_DES_C */
5310 
5311 #ifdef POLARSSL_DES_C
5312 
5313  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
5314  size_t first_length = 15;
5315  size_t second_length = 7;
5316  size_t length = first_length + second_length;
5317  unsigned char key[32];
5318  unsigned char iv[16];
5319 
5320  cipher_context_t ctx_dec;
5321  cipher_context_t ctx_enc;
5322  const cipher_info_t *cipher_info;
5323 
5324  unsigned char inbuf[64];
5325  unsigned char encbuf[64];
5326  unsigned char decbuf[64];
5327 
5328  size_t outlen = 0;
5329  size_t totaloutlen = 0;
5330  size_t enclen = 0;
5331 
5332  memset( key, 0, 32 );
5333  memset( iv , 0, 16 );
5334 
5335  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5336  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5337 
5338  memset( inbuf, 5, 64 );
5339  memset( encbuf, 0, 64 );
5340  memset( decbuf, 0, 64 );
5341 
5342  /* Initialise enc and dec contexts */
5344  fct_chk( NULL != cipher_info);
5345 
5346  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5347  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5348 
5349  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5350  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5351 
5352  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5353  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5354 
5355  if( POLARSSL_MODE_CBC == cipher_info->mode )
5356  {
5357  enclen = cipher_get_block_size(&ctx_enc )
5358  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5359  }
5360  else
5361  {
5362  enclen = length;
5363  }
5364 
5365  /* encode length number of bytes from inbuf */
5366  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5367  totaloutlen = outlen;
5368  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5369  totaloutlen += outlen;
5370  if( POLARSSL_MODE_CBC == cipher_info->mode )
5371  {
5372  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5373  }
5374  else
5375  {
5376  fct_chk( totaloutlen == enclen );
5377  }
5378  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5379  totaloutlen += outlen;
5380  if( POLARSSL_MODE_CBC == cipher_info->mode )
5381  {
5382  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5383  }
5384  else
5385  {
5386  fct_chk( outlen == 0 );
5387  }
5388 
5389  /* decode the previously encoded string */
5390  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5391  if( POLARSSL_MODE_CBC == cipher_info->mode )
5392  {
5393  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5394  }
5395  else
5396  {
5397  fct_chk( enclen == outlen );
5398  }
5399  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5400  if( POLARSSL_MODE_CBC == cipher_info->mode )
5401  {
5402  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5403  }
5404  else
5405  {
5406  fct_chk( outlen == 0 );
5407  }
5408 
5409 
5410  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5411 
5412  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5413  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5414  FCT_TEST_END();
5415 #endif /* POLARSSL_DES_C */
5416 
5417 #ifdef POLARSSL_DES_C
5418 
5419  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
5420  size_t first_length = 16;
5421  size_t second_length = 6;
5422  size_t length = first_length + second_length;
5423  unsigned char key[32];
5424  unsigned char iv[16];
5425 
5426  cipher_context_t ctx_dec;
5427  cipher_context_t ctx_enc;
5428  const cipher_info_t *cipher_info;
5429 
5430  unsigned char inbuf[64];
5431  unsigned char encbuf[64];
5432  unsigned char decbuf[64];
5433 
5434  size_t outlen = 0;
5435  size_t totaloutlen = 0;
5436  size_t enclen = 0;
5437 
5438  memset( key, 0, 32 );
5439  memset( iv , 0, 16 );
5440 
5441  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5442  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5443 
5444  memset( inbuf, 5, 64 );
5445  memset( encbuf, 0, 64 );
5446  memset( decbuf, 0, 64 );
5447 
5448  /* Initialise enc and dec contexts */
5450  fct_chk( NULL != cipher_info);
5451 
5452  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5453  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5454 
5455  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5456  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5457 
5458  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5459  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5460 
5461  if( POLARSSL_MODE_CBC == cipher_info->mode )
5462  {
5463  enclen = cipher_get_block_size(&ctx_enc )
5464  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5465  }
5466  else
5467  {
5468  enclen = length;
5469  }
5470 
5471  /* encode length number of bytes from inbuf */
5472  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5473  totaloutlen = outlen;
5474  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5475  totaloutlen += outlen;
5476  if( POLARSSL_MODE_CBC == cipher_info->mode )
5477  {
5478  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5479  }
5480  else
5481  {
5482  fct_chk( totaloutlen == enclen );
5483  }
5484  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5485  totaloutlen += outlen;
5486  if( POLARSSL_MODE_CBC == cipher_info->mode )
5487  {
5488  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5489  }
5490  else
5491  {
5492  fct_chk( outlen == 0 );
5493  }
5494 
5495  /* decode the previously encoded string */
5496  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5497  if( POLARSSL_MODE_CBC == cipher_info->mode )
5498  {
5499  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5500  }
5501  else
5502  {
5503  fct_chk( enclen == outlen );
5504  }
5505  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5506  if( POLARSSL_MODE_CBC == cipher_info->mode )
5507  {
5508  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5509  }
5510  else
5511  {
5512  fct_chk( outlen == 0 );
5513  }
5514 
5515 
5516  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5517 
5518  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5519  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5520  FCT_TEST_END();
5521 #endif /* POLARSSL_DES_C */
5522 
5523 #ifdef POLARSSL_DES_C
5524 
5525  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
5526  size_t first_length = 17;
5527  size_t second_length = 6;
5528  size_t length = first_length + second_length;
5529  unsigned char key[32];
5530  unsigned char iv[16];
5531 
5532  cipher_context_t ctx_dec;
5533  cipher_context_t ctx_enc;
5534  const cipher_info_t *cipher_info;
5535 
5536  unsigned char inbuf[64];
5537  unsigned char encbuf[64];
5538  unsigned char decbuf[64];
5539 
5540  size_t outlen = 0;
5541  size_t totaloutlen = 0;
5542  size_t enclen = 0;
5543 
5544  memset( key, 0, 32 );
5545  memset( iv , 0, 16 );
5546 
5547  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5548  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5549 
5550  memset( inbuf, 5, 64 );
5551  memset( encbuf, 0, 64 );
5552  memset( decbuf, 0, 64 );
5553 
5554  /* Initialise enc and dec contexts */
5556  fct_chk( NULL != cipher_info);
5557 
5558  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5559  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5560 
5561  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5562  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5563 
5564  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5565  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5566 
5567  if( POLARSSL_MODE_CBC == cipher_info->mode )
5568  {
5569  enclen = cipher_get_block_size(&ctx_enc )
5570  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5571  }
5572  else
5573  {
5574  enclen = length;
5575  }
5576 
5577  /* encode length number of bytes from inbuf */
5578  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5579  totaloutlen = outlen;
5580  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5581  totaloutlen += outlen;
5582  if( POLARSSL_MODE_CBC == cipher_info->mode )
5583  {
5584  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5585  }
5586  else
5587  {
5588  fct_chk( totaloutlen == enclen );
5589  }
5590  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5591  totaloutlen += outlen;
5592  if( POLARSSL_MODE_CBC == cipher_info->mode )
5593  {
5594  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5595  }
5596  else
5597  {
5598  fct_chk( outlen == 0 );
5599  }
5600 
5601  /* decode the previously encoded string */
5602  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5603  if( POLARSSL_MODE_CBC == cipher_info->mode )
5604  {
5605  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5606  }
5607  else
5608  {
5609  fct_chk( enclen == outlen );
5610  }
5611  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5612  if( POLARSSL_MODE_CBC == cipher_info->mode )
5613  {
5614  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5615  }
5616  else
5617  {
5618  fct_chk( outlen == 0 );
5619  }
5620 
5621 
5622  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5623 
5624  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5625  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5626  FCT_TEST_END();
5627 #endif /* POLARSSL_DES_C */
5628 
5629 #ifdef POLARSSL_DES_C
5630 
5631  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
5632  size_t first_length = 16;
5633  size_t second_length = 16;
5634  size_t length = first_length + second_length;
5635  unsigned char key[32];
5636  unsigned char iv[16];
5637 
5638  cipher_context_t ctx_dec;
5639  cipher_context_t ctx_enc;
5640  const cipher_info_t *cipher_info;
5641 
5642  unsigned char inbuf[64];
5643  unsigned char encbuf[64];
5644  unsigned char decbuf[64];
5645 
5646  size_t outlen = 0;
5647  size_t totaloutlen = 0;
5648  size_t enclen = 0;
5649 
5650  memset( key, 0, 32 );
5651  memset( iv , 0, 16 );
5652 
5653  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5654  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5655 
5656  memset( inbuf, 5, 64 );
5657  memset( encbuf, 0, 64 );
5658  memset( decbuf, 0, 64 );
5659 
5660  /* Initialise enc and dec contexts */
5662  fct_chk( NULL != cipher_info);
5663 
5664  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5665  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5666 
5667  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 112, POLARSSL_DECRYPT ) );
5668  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 112, POLARSSL_ENCRYPT ) );
5669 
5670  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5671  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5672 
5673  if( POLARSSL_MODE_CBC == cipher_info->mode )
5674  {
5675  enclen = cipher_get_block_size(&ctx_enc )
5676  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5677  }
5678  else
5679  {
5680  enclen = length;
5681  }
5682 
5683  /* encode length number of bytes from inbuf */
5684  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
5685  totaloutlen = outlen;
5686  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
5687  totaloutlen += outlen;
5688  if( POLARSSL_MODE_CBC == cipher_info->mode )
5689  {
5690  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5691  }
5692  else
5693  {
5694  fct_chk( totaloutlen == enclen );
5695  }
5696  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
5697  totaloutlen += outlen;
5698  if( POLARSSL_MODE_CBC == cipher_info->mode )
5699  {
5700  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5701  }
5702  else
5703  {
5704  fct_chk( outlen == 0 );
5705  }
5706 
5707  /* decode the previously encoded string */
5708  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5709  if( POLARSSL_MODE_CBC == cipher_info->mode )
5710  {
5711  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5712  }
5713  else
5714  {
5715  fct_chk( enclen == outlen );
5716  }
5717  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5718  if( POLARSSL_MODE_CBC == cipher_info->mode )
5719  {
5720  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5721  }
5722  else
5723  {
5724  fct_chk( outlen == 0 );
5725  }
5726 
5727 
5728  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5729 
5730  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5731  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5732  FCT_TEST_END();
5733 #endif /* POLARSSL_DES_C */
5734 
5735 #ifdef POLARSSL_DES_C
5736 
5737  FCT_TEST_BGN(des3_encrypt_and_decrypt_0_bytes)
5738  size_t length = 0;
5739  unsigned char key[32];
5740  unsigned char iv[16];
5741 
5742  const cipher_info_t *cipher_info;
5743  cipher_context_t ctx_dec;
5744  cipher_context_t ctx_enc;
5745 
5746  unsigned char inbuf[64];
5747  unsigned char encbuf[64];
5748  unsigned char decbuf[64];
5749 
5750  size_t outlen = 0;
5751  size_t enclen = 0;
5752 
5753  memset( key, 0, 32 );
5754  memset( iv , 0, 16 );
5755 
5756  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5757  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5758 
5759  memset( inbuf, 5, 64 );
5760  memset( encbuf, 0, 64 );
5761  memset( decbuf, 0, 64 );
5762 
5763  /* Check and get info structures */
5765  fct_chk( NULL != cipher_info );
5766  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
5767 
5768  /* Initialise enc and dec contexts */
5769  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5770  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5771 
5772  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
5773  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
5774 
5775  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5776  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5777 
5778  if( POLARSSL_MODE_CBC == cipher_info->mode )
5779  {
5780  enclen = cipher_get_block_size( &ctx_enc )
5781  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5782  }
5783  else
5784  {
5785  enclen = length;
5786  }
5787 
5788  /* encode length number of bytes from inbuf */
5789  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
5790  if( POLARSSL_MODE_CBC == cipher_info->mode )
5791  {
5792  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5793  }
5794  else
5795  {
5796  fct_chk( outlen == enclen );
5797  }
5798 
5799  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
5800  if( POLARSSL_MODE_CBC == cipher_info->mode )
5801  {
5802  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5803  }
5804  else
5805  {
5806  fct_chk( outlen == 0 );
5807  }
5808 
5809 
5810  /* decode the previously encoded string */
5811  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5812  if( POLARSSL_MODE_CBC == cipher_info->mode )
5813  {
5814  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5815  }
5816  else
5817  {
5818  fct_chk( enclen == outlen );
5819  }
5820 
5821  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5822  if( POLARSSL_MODE_CBC == cipher_info->mode )
5823  {
5824  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5825  }
5826  else
5827  {
5828  fct_chk( outlen == 0 );
5829  }
5830 
5831 
5832  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5833 
5834  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5835  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5836  FCT_TEST_END();
5837 #endif /* POLARSSL_DES_C */
5838 
5839 #ifdef POLARSSL_DES_C
5840 
5841  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_byte)
5842  size_t length = 1;
5843  unsigned char key[32];
5844  unsigned char iv[16];
5845 
5846  const cipher_info_t *cipher_info;
5847  cipher_context_t ctx_dec;
5848  cipher_context_t ctx_enc;
5849 
5850  unsigned char inbuf[64];
5851  unsigned char encbuf[64];
5852  unsigned char decbuf[64];
5853 
5854  size_t outlen = 0;
5855  size_t enclen = 0;
5856 
5857  memset( key, 0, 32 );
5858  memset( iv , 0, 16 );
5859 
5860  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5861  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5862 
5863  memset( inbuf, 5, 64 );
5864  memset( encbuf, 0, 64 );
5865  memset( decbuf, 0, 64 );
5866 
5867  /* Check and get info structures */
5869  fct_chk( NULL != cipher_info );
5870  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
5871 
5872  /* Initialise enc and dec contexts */
5873  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5874  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5875 
5876  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
5877  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
5878 
5879  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5880  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5881 
5882  if( POLARSSL_MODE_CBC == cipher_info->mode )
5883  {
5884  enclen = cipher_get_block_size( &ctx_enc )
5885  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5886  }
5887  else
5888  {
5889  enclen = length;
5890  }
5891 
5892  /* encode length number of bytes from inbuf */
5893  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
5894  if( POLARSSL_MODE_CBC == cipher_info->mode )
5895  {
5896  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
5897  }
5898  else
5899  {
5900  fct_chk( outlen == enclen );
5901  }
5902 
5903  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
5904  if( POLARSSL_MODE_CBC == cipher_info->mode )
5905  {
5906  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
5907  }
5908  else
5909  {
5910  fct_chk( outlen == 0 );
5911  }
5912 
5913 
5914  /* decode the previously encoded string */
5915  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
5916  if( POLARSSL_MODE_CBC == cipher_info->mode )
5917  {
5918  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
5919  }
5920  else
5921  {
5922  fct_chk( enclen == outlen );
5923  }
5924 
5925  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
5926  if( POLARSSL_MODE_CBC == cipher_info->mode )
5927  {
5928  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
5929  }
5930  else
5931  {
5932  fct_chk( outlen == 0 );
5933  }
5934 
5935 
5936  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
5937 
5938  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
5939  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
5940  FCT_TEST_END();
5941 #endif /* POLARSSL_DES_C */
5942 
5943 #ifdef POLARSSL_DES_C
5944 
5945  FCT_TEST_BGN(des3_encrypt_and_decrypt_2_bytes)
5946  size_t length = 2;
5947  unsigned char key[32];
5948  unsigned char iv[16];
5949 
5950  const cipher_info_t *cipher_info;
5951  cipher_context_t ctx_dec;
5952  cipher_context_t ctx_enc;
5953 
5954  unsigned char inbuf[64];
5955  unsigned char encbuf[64];
5956  unsigned char decbuf[64];
5957 
5958  size_t outlen = 0;
5959  size_t enclen = 0;
5960 
5961  memset( key, 0, 32 );
5962  memset( iv , 0, 16 );
5963 
5964  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
5965  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
5966 
5967  memset( inbuf, 5, 64 );
5968  memset( encbuf, 0, 64 );
5969  memset( decbuf, 0, 64 );
5970 
5971  /* Check and get info structures */
5973  fct_chk( NULL != cipher_info );
5974  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
5975 
5976  /* Initialise enc and dec contexts */
5977  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
5978  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
5979 
5980  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
5981  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
5982 
5983  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
5984  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
5985 
5986  if( POLARSSL_MODE_CBC == cipher_info->mode )
5987  {
5988  enclen = cipher_get_block_size( &ctx_enc )
5989  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
5990  }
5991  else
5992  {
5993  enclen = length;
5994  }
5995 
5996  /* encode length number of bytes from inbuf */
5997  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
5998  if( POLARSSL_MODE_CBC == cipher_info->mode )
5999  {
6000  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6001  }
6002  else
6003  {
6004  fct_chk( outlen == enclen );
6005  }
6006 
6007  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6008  if( POLARSSL_MODE_CBC == cipher_info->mode )
6009  {
6010  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6011  }
6012  else
6013  {
6014  fct_chk( outlen == 0 );
6015  }
6016 
6017 
6018  /* decode the previously encoded string */
6019  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6020  if( POLARSSL_MODE_CBC == cipher_info->mode )
6021  {
6022  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6023  }
6024  else
6025  {
6026  fct_chk( enclen == outlen );
6027  }
6028 
6029  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6030  if( POLARSSL_MODE_CBC == cipher_info->mode )
6031  {
6032  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6033  }
6034  else
6035  {
6036  fct_chk( outlen == 0 );
6037  }
6038 
6039 
6040  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6041 
6042  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6043  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6044  FCT_TEST_END();
6045 #endif /* POLARSSL_DES_C */
6046 
6047 #ifdef POLARSSL_DES_C
6048 
6049  FCT_TEST_BGN(des3_encrypt_and_decrypt_7_bytes)
6050  size_t length = 7;
6051  unsigned char key[32];
6052  unsigned char iv[16];
6053 
6054  const cipher_info_t *cipher_info;
6055  cipher_context_t ctx_dec;
6056  cipher_context_t ctx_enc;
6057 
6058  unsigned char inbuf[64];
6059  unsigned char encbuf[64];
6060  unsigned char decbuf[64];
6061 
6062  size_t outlen = 0;
6063  size_t enclen = 0;
6064 
6065  memset( key, 0, 32 );
6066  memset( iv , 0, 16 );
6067 
6068  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6069  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6070 
6071  memset( inbuf, 5, 64 );
6072  memset( encbuf, 0, 64 );
6073  memset( decbuf, 0, 64 );
6074 
6075  /* Check and get info structures */
6077  fct_chk( NULL != cipher_info );
6078  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6079 
6080  /* Initialise enc and dec contexts */
6081  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6082  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6083 
6084  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6085  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6086 
6087  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6088  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6089 
6090  if( POLARSSL_MODE_CBC == cipher_info->mode )
6091  {
6092  enclen = cipher_get_block_size( &ctx_enc )
6093  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6094  }
6095  else
6096  {
6097  enclen = length;
6098  }
6099 
6100  /* encode length number of bytes from inbuf */
6101  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6102  if( POLARSSL_MODE_CBC == cipher_info->mode )
6103  {
6104  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6105  }
6106  else
6107  {
6108  fct_chk( outlen == enclen );
6109  }
6110 
6111  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6112  if( POLARSSL_MODE_CBC == cipher_info->mode )
6113  {
6114  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6115  }
6116  else
6117  {
6118  fct_chk( outlen == 0 );
6119  }
6120 
6121 
6122  /* decode the previously encoded string */
6123  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6124  if( POLARSSL_MODE_CBC == cipher_info->mode )
6125  {
6126  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6127  }
6128  else
6129  {
6130  fct_chk( enclen == outlen );
6131  }
6132 
6133  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6134  if( POLARSSL_MODE_CBC == cipher_info->mode )
6135  {
6136  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6137  }
6138  else
6139  {
6140  fct_chk( outlen == 0 );
6141  }
6142 
6143 
6144  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6145 
6146  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6147  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6148  FCT_TEST_END();
6149 #endif /* POLARSSL_DES_C */
6150 
6151 #ifdef POLARSSL_DES_C
6152 
6153  FCT_TEST_BGN(des3_encrypt_and_decrypt_8_bytes)
6154  size_t length = 8;
6155  unsigned char key[32];
6156  unsigned char iv[16];
6157 
6158  const cipher_info_t *cipher_info;
6159  cipher_context_t ctx_dec;
6160  cipher_context_t ctx_enc;
6161 
6162  unsigned char inbuf[64];
6163  unsigned char encbuf[64];
6164  unsigned char decbuf[64];
6165 
6166  size_t outlen = 0;
6167  size_t enclen = 0;
6168 
6169  memset( key, 0, 32 );
6170  memset( iv , 0, 16 );
6171 
6172  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6173  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6174 
6175  memset( inbuf, 5, 64 );
6176  memset( encbuf, 0, 64 );
6177  memset( decbuf, 0, 64 );
6178 
6179  /* Check and get info structures */
6181  fct_chk( NULL != cipher_info );
6182  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6183 
6184  /* Initialise enc and dec contexts */
6185  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6186  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6187 
6188  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6189  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6190 
6191  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6192  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6193 
6194  if( POLARSSL_MODE_CBC == cipher_info->mode )
6195  {
6196  enclen = cipher_get_block_size( &ctx_enc )
6197  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6198  }
6199  else
6200  {
6201  enclen = length;
6202  }
6203 
6204  /* encode length number of bytes from inbuf */
6205  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6206  if( POLARSSL_MODE_CBC == cipher_info->mode )
6207  {
6208  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6209  }
6210  else
6211  {
6212  fct_chk( outlen == enclen );
6213  }
6214 
6215  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6216  if( POLARSSL_MODE_CBC == cipher_info->mode )
6217  {
6218  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6219  }
6220  else
6221  {
6222  fct_chk( outlen == 0 );
6223  }
6224 
6225 
6226  /* decode the previously encoded string */
6227  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6228  if( POLARSSL_MODE_CBC == cipher_info->mode )
6229  {
6230  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6231  }
6232  else
6233  {
6234  fct_chk( enclen == outlen );
6235  }
6236 
6237  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6238  if( POLARSSL_MODE_CBC == cipher_info->mode )
6239  {
6240  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6241  }
6242  else
6243  {
6244  fct_chk( outlen == 0 );
6245  }
6246 
6247 
6248  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6249 
6250  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6251  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6252  FCT_TEST_END();
6253 #endif /* POLARSSL_DES_C */
6254 
6255 #ifdef POLARSSL_DES_C
6256 
6257  FCT_TEST_BGN(des3_encrypt_and_decrypt_9_bytes)
6258  size_t length = 9;
6259  unsigned char key[32];
6260  unsigned char iv[16];
6261 
6262  const cipher_info_t *cipher_info;
6263  cipher_context_t ctx_dec;
6264  cipher_context_t ctx_enc;
6265 
6266  unsigned char inbuf[64];
6267  unsigned char encbuf[64];
6268  unsigned char decbuf[64];
6269 
6270  size_t outlen = 0;
6271  size_t enclen = 0;
6272 
6273  memset( key, 0, 32 );
6274  memset( iv , 0, 16 );
6275 
6276  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6277  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6278 
6279  memset( inbuf, 5, 64 );
6280  memset( encbuf, 0, 64 );
6281  memset( decbuf, 0, 64 );
6282 
6283  /* Check and get info structures */
6285  fct_chk( NULL != cipher_info );
6286  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6287 
6288  /* Initialise enc and dec contexts */
6289  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6290  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6291 
6292  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6293  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6294 
6295  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6296  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6297 
6298  if( POLARSSL_MODE_CBC == cipher_info->mode )
6299  {
6300  enclen = cipher_get_block_size( &ctx_enc )
6301  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6302  }
6303  else
6304  {
6305  enclen = length;
6306  }
6307 
6308  /* encode length number of bytes from inbuf */
6309  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6310  if( POLARSSL_MODE_CBC == cipher_info->mode )
6311  {
6312  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6313  }
6314  else
6315  {
6316  fct_chk( outlen == enclen );
6317  }
6318 
6319  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6320  if( POLARSSL_MODE_CBC == cipher_info->mode )
6321  {
6322  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6323  }
6324  else
6325  {
6326  fct_chk( outlen == 0 );
6327  }
6328 
6329 
6330  /* decode the previously encoded string */
6331  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6332  if( POLARSSL_MODE_CBC == cipher_info->mode )
6333  {
6334  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6335  }
6336  else
6337  {
6338  fct_chk( enclen == outlen );
6339  }
6340 
6341  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6342  if( POLARSSL_MODE_CBC == cipher_info->mode )
6343  {
6344  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6345  }
6346  else
6347  {
6348  fct_chk( outlen == 0 );
6349  }
6350 
6351 
6352  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6353 
6354  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6355  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6356  FCT_TEST_END();
6357 #endif /* POLARSSL_DES_C */
6358 
6359 #ifdef POLARSSL_DES_C
6360 
6361  FCT_TEST_BGN(des3_encrypt_and_decrypt_15_bytes)
6362  size_t length = 15;
6363  unsigned char key[32];
6364  unsigned char iv[16];
6365 
6366  const cipher_info_t *cipher_info;
6367  cipher_context_t ctx_dec;
6368  cipher_context_t ctx_enc;
6369 
6370  unsigned char inbuf[64];
6371  unsigned char encbuf[64];
6372  unsigned char decbuf[64];
6373 
6374  size_t outlen = 0;
6375  size_t enclen = 0;
6376 
6377  memset( key, 0, 32 );
6378  memset( iv , 0, 16 );
6379 
6380  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6381  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6382 
6383  memset( inbuf, 5, 64 );
6384  memset( encbuf, 0, 64 );
6385  memset( decbuf, 0, 64 );
6386 
6387  /* Check and get info structures */
6389  fct_chk( NULL != cipher_info );
6390  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6391 
6392  /* Initialise enc and dec contexts */
6393  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6394  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6395 
6396  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6397  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6398 
6399  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6400  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6401 
6402  if( POLARSSL_MODE_CBC == cipher_info->mode )
6403  {
6404  enclen = cipher_get_block_size( &ctx_enc )
6405  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6406  }
6407  else
6408  {
6409  enclen = length;
6410  }
6411 
6412  /* encode length number of bytes from inbuf */
6413  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6414  if( POLARSSL_MODE_CBC == cipher_info->mode )
6415  {
6416  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6417  }
6418  else
6419  {
6420  fct_chk( outlen == enclen );
6421  }
6422 
6423  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6424  if( POLARSSL_MODE_CBC == cipher_info->mode )
6425  {
6426  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6427  }
6428  else
6429  {
6430  fct_chk( outlen == 0 );
6431  }
6432 
6433 
6434  /* decode the previously encoded string */
6435  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6436  if( POLARSSL_MODE_CBC == cipher_info->mode )
6437  {
6438  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6439  }
6440  else
6441  {
6442  fct_chk( enclen == outlen );
6443  }
6444 
6445  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6446  if( POLARSSL_MODE_CBC == cipher_info->mode )
6447  {
6448  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6449  }
6450  else
6451  {
6452  fct_chk( outlen == 0 );
6453  }
6454 
6455 
6456  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6457 
6458  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6459  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6460  FCT_TEST_END();
6461 #endif /* POLARSSL_DES_C */
6462 
6463 #ifdef POLARSSL_DES_C
6464 
6465  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes)
6466  size_t length = 16;
6467  unsigned char key[32];
6468  unsigned char iv[16];
6469 
6470  const cipher_info_t *cipher_info;
6471  cipher_context_t ctx_dec;
6472  cipher_context_t ctx_enc;
6473 
6474  unsigned char inbuf[64];
6475  unsigned char encbuf[64];
6476  unsigned char decbuf[64];
6477 
6478  size_t outlen = 0;
6479  size_t enclen = 0;
6480 
6481  memset( key, 0, 32 );
6482  memset( iv , 0, 16 );
6483 
6484  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6485  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6486 
6487  memset( inbuf, 5, 64 );
6488  memset( encbuf, 0, 64 );
6489  memset( decbuf, 0, 64 );
6490 
6491  /* Check and get info structures */
6493  fct_chk( NULL != cipher_info );
6494  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6495 
6496  /* Initialise enc and dec contexts */
6497  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6498  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6499 
6500  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6501  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6502 
6503  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6504  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6505 
6506  if( POLARSSL_MODE_CBC == cipher_info->mode )
6507  {
6508  enclen = cipher_get_block_size( &ctx_enc )
6509  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6510  }
6511  else
6512  {
6513  enclen = length;
6514  }
6515 
6516  /* encode length number of bytes from inbuf */
6517  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6518  if( POLARSSL_MODE_CBC == cipher_info->mode )
6519  {
6520  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6521  }
6522  else
6523  {
6524  fct_chk( outlen == enclen );
6525  }
6526 
6527  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6528  if( POLARSSL_MODE_CBC == cipher_info->mode )
6529  {
6530  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6531  }
6532  else
6533  {
6534  fct_chk( outlen == 0 );
6535  }
6536 
6537 
6538  /* decode the previously encoded string */
6539  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6540  if( POLARSSL_MODE_CBC == cipher_info->mode )
6541  {
6542  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6543  }
6544  else
6545  {
6546  fct_chk( enclen == outlen );
6547  }
6548 
6549  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6550  if( POLARSSL_MODE_CBC == cipher_info->mode )
6551  {
6552  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6553  }
6554  else
6555  {
6556  fct_chk( outlen == 0 );
6557  }
6558 
6559 
6560  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6561 
6562  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6563  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6564  FCT_TEST_END();
6565 #endif /* POLARSSL_DES_C */
6566 
6567 #ifdef POLARSSL_DES_C
6568 
6569  FCT_TEST_BGN(des3_encrypt_and_decrypt_17_bytes)
6570  size_t length = 17;
6571  unsigned char key[32];
6572  unsigned char iv[16];
6573 
6574  const cipher_info_t *cipher_info;
6575  cipher_context_t ctx_dec;
6576  cipher_context_t ctx_enc;
6577 
6578  unsigned char inbuf[64];
6579  unsigned char encbuf[64];
6580  unsigned char decbuf[64];
6581 
6582  size_t outlen = 0;
6583  size_t enclen = 0;
6584 
6585  memset( key, 0, 32 );
6586  memset( iv , 0, 16 );
6587 
6588  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6589  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6590 
6591  memset( inbuf, 5, 64 );
6592  memset( encbuf, 0, 64 );
6593  memset( decbuf, 0, 64 );
6594 
6595  /* Check and get info structures */
6597  fct_chk( NULL != cipher_info );
6598  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6599 
6600  /* Initialise enc and dec contexts */
6601  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6602  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6603 
6604  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6605  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6606 
6607  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6608  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6609 
6610  if( POLARSSL_MODE_CBC == cipher_info->mode )
6611  {
6612  enclen = cipher_get_block_size( &ctx_enc )
6613  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6614  }
6615  else
6616  {
6617  enclen = length;
6618  }
6619 
6620  /* encode length number of bytes from inbuf */
6621  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6622  if( POLARSSL_MODE_CBC == cipher_info->mode )
6623  {
6624  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6625  }
6626  else
6627  {
6628  fct_chk( outlen == enclen );
6629  }
6630 
6631  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6632  if( POLARSSL_MODE_CBC == cipher_info->mode )
6633  {
6634  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6635  }
6636  else
6637  {
6638  fct_chk( outlen == 0 );
6639  }
6640 
6641 
6642  /* decode the previously encoded string */
6643  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6644  if( POLARSSL_MODE_CBC == cipher_info->mode )
6645  {
6646  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6647  }
6648  else
6649  {
6650  fct_chk( enclen == outlen );
6651  }
6652 
6653  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6654  if( POLARSSL_MODE_CBC == cipher_info->mode )
6655  {
6656  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6657  }
6658  else
6659  {
6660  fct_chk( outlen == 0 );
6661  }
6662 
6663 
6664  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6665 
6666  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6667  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6668  FCT_TEST_END();
6669 #endif /* POLARSSL_DES_C */
6670 
6671 #ifdef POLARSSL_DES_C
6672 
6673  FCT_TEST_BGN(des3_encrypt_and_decrypt_31_bytes)
6674  size_t length = 31;
6675  unsigned char key[32];
6676  unsigned char iv[16];
6677 
6678  const cipher_info_t *cipher_info;
6679  cipher_context_t ctx_dec;
6680  cipher_context_t ctx_enc;
6681 
6682  unsigned char inbuf[64];
6683  unsigned char encbuf[64];
6684  unsigned char decbuf[64];
6685 
6686  size_t outlen = 0;
6687  size_t enclen = 0;
6688 
6689  memset( key, 0, 32 );
6690  memset( iv , 0, 16 );
6691 
6692  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6693  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6694 
6695  memset( inbuf, 5, 64 );
6696  memset( encbuf, 0, 64 );
6697  memset( decbuf, 0, 64 );
6698 
6699  /* Check and get info structures */
6701  fct_chk( NULL != cipher_info );
6702  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6703 
6704  /* Initialise enc and dec contexts */
6705  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6706  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6707 
6708  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6709  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6710 
6711  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6712  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6713 
6714  if( POLARSSL_MODE_CBC == cipher_info->mode )
6715  {
6716  enclen = cipher_get_block_size( &ctx_enc )
6717  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6718  }
6719  else
6720  {
6721  enclen = length;
6722  }
6723 
6724  /* encode length number of bytes from inbuf */
6725  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6726  if( POLARSSL_MODE_CBC == cipher_info->mode )
6727  {
6728  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6729  }
6730  else
6731  {
6732  fct_chk( outlen == enclen );
6733  }
6734 
6735  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6736  if( POLARSSL_MODE_CBC == cipher_info->mode )
6737  {
6738  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6739  }
6740  else
6741  {
6742  fct_chk( outlen == 0 );
6743  }
6744 
6745 
6746  /* decode the previously encoded string */
6747  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6748  if( POLARSSL_MODE_CBC == cipher_info->mode )
6749  {
6750  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6751  }
6752  else
6753  {
6754  fct_chk( enclen == outlen );
6755  }
6756 
6757  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6758  if( POLARSSL_MODE_CBC == cipher_info->mode )
6759  {
6760  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6761  }
6762  else
6763  {
6764  fct_chk( outlen == 0 );
6765  }
6766 
6767 
6768  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6769 
6770  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6771  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6772  FCT_TEST_END();
6773 #endif /* POLARSSL_DES_C */
6774 
6775 #ifdef POLARSSL_DES_C
6776 
6777  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes)
6778  size_t length = 32;
6779  unsigned char key[32];
6780  unsigned char iv[16];
6781 
6782  const cipher_info_t *cipher_info;
6783  cipher_context_t ctx_dec;
6784  cipher_context_t ctx_enc;
6785 
6786  unsigned char inbuf[64];
6787  unsigned char encbuf[64];
6788  unsigned char decbuf[64];
6789 
6790  size_t outlen = 0;
6791  size_t enclen = 0;
6792 
6793  memset( key, 0, 32 );
6794  memset( iv , 0, 16 );
6795 
6796  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6797  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6798 
6799  memset( inbuf, 5, 64 );
6800  memset( encbuf, 0, 64 );
6801  memset( decbuf, 0, 64 );
6802 
6803  /* Check and get info structures */
6805  fct_chk( NULL != cipher_info );
6806  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6807 
6808  /* Initialise enc and dec contexts */
6809  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6810  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6811 
6812  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6813  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6814 
6815  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6816  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6817 
6818  if( POLARSSL_MODE_CBC == cipher_info->mode )
6819  {
6820  enclen = cipher_get_block_size( &ctx_enc )
6821  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6822  }
6823  else
6824  {
6825  enclen = length;
6826  }
6827 
6828  /* encode length number of bytes from inbuf */
6829  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6830  if( POLARSSL_MODE_CBC == cipher_info->mode )
6831  {
6832  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6833  }
6834  else
6835  {
6836  fct_chk( outlen == enclen );
6837  }
6838 
6839  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6840  if( POLARSSL_MODE_CBC == cipher_info->mode )
6841  {
6842  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6843  }
6844  else
6845  {
6846  fct_chk( outlen == 0 );
6847  }
6848 
6849 
6850  /* decode the previously encoded string */
6851  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6852  if( POLARSSL_MODE_CBC == cipher_info->mode )
6853  {
6854  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6855  }
6856  else
6857  {
6858  fct_chk( enclen == outlen );
6859  }
6860 
6861  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6862  if( POLARSSL_MODE_CBC == cipher_info->mode )
6863  {
6864  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6865  }
6866  else
6867  {
6868  fct_chk( outlen == 0 );
6869  }
6870 
6871 
6872  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6873 
6874  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6875  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6876  FCT_TEST_END();
6877 #endif /* POLARSSL_DES_C */
6878 
6879 #ifdef POLARSSL_DES_C
6880 
6881  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes)
6882  size_t length = 33;
6883  unsigned char key[32];
6884  unsigned char iv[16];
6885 
6886  const cipher_info_t *cipher_info;
6887  cipher_context_t ctx_dec;
6888  cipher_context_t ctx_enc;
6889 
6890  unsigned char inbuf[64];
6891  unsigned char encbuf[64];
6892  unsigned char decbuf[64];
6893 
6894  size_t outlen = 0;
6895  size_t enclen = 0;
6896 
6897  memset( key, 0, 32 );
6898  memset( iv , 0, 16 );
6899 
6900  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
6901  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
6902 
6903  memset( inbuf, 5, 64 );
6904  memset( encbuf, 0, 64 );
6905  memset( decbuf, 0, 64 );
6906 
6907  /* Check and get info structures */
6909  fct_chk( NULL != cipher_info );
6910  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
6911 
6912  /* Initialise enc and dec contexts */
6913  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
6914  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
6915 
6916  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
6917  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
6918 
6919  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
6920  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
6921 
6922  if( POLARSSL_MODE_CBC == cipher_info->mode )
6923  {
6924  enclen = cipher_get_block_size( &ctx_enc )
6925  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
6926  }
6927  else
6928  {
6929  enclen = length;
6930  }
6931 
6932  /* encode length number of bytes from inbuf */
6933  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
6934  if( POLARSSL_MODE_CBC == cipher_info->mode )
6935  {
6936  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
6937  }
6938  else
6939  {
6940  fct_chk( outlen == enclen );
6941  }
6942 
6943  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
6944  if( POLARSSL_MODE_CBC == cipher_info->mode )
6945  {
6946  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
6947  }
6948  else
6949  {
6950  fct_chk( outlen == 0 );
6951  }
6952 
6953 
6954  /* decode the previously encoded string */
6955  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
6956  if( POLARSSL_MODE_CBC == cipher_info->mode )
6957  {
6958  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
6959  }
6960  else
6961  {
6962  fct_chk( enclen == outlen );
6963  }
6964 
6965  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
6966  if( POLARSSL_MODE_CBC == cipher_info->mode )
6967  {
6968  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
6969  }
6970  else
6971  {
6972  fct_chk( outlen == 0 );
6973  }
6974 
6975 
6976  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
6977 
6978  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
6979  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
6980  FCT_TEST_END();
6981 #endif /* POLARSSL_DES_C */
6982 
6983 #ifdef POLARSSL_DES_C
6984 
6985  FCT_TEST_BGN(des3_encrypt_and_decrypt_47_bytes)
6986  size_t length = 47;
6987  unsigned char key[32];
6988  unsigned char iv[16];
6989 
6990  const cipher_info_t *cipher_info;
6991  cipher_context_t ctx_dec;
6992  cipher_context_t ctx_enc;
6993 
6994  unsigned char inbuf[64];
6995  unsigned char encbuf[64];
6996  unsigned char decbuf[64];
6997 
6998  size_t outlen = 0;
6999  size_t enclen = 0;
7000 
7001  memset( key, 0, 32 );
7002  memset( iv , 0, 16 );
7003 
7004  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7005  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7006 
7007  memset( inbuf, 5, 64 );
7008  memset( encbuf, 0, 64 );
7009  memset( decbuf, 0, 64 );
7010 
7011  /* Check and get info structures */
7013  fct_chk( NULL != cipher_info );
7014  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
7015 
7016  /* Initialise enc and dec contexts */
7017  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7018  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7019 
7020  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7021  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7022 
7023  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7024  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7025 
7026  if( POLARSSL_MODE_CBC == cipher_info->mode )
7027  {
7028  enclen = cipher_get_block_size( &ctx_enc )
7029  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7030  }
7031  else
7032  {
7033  enclen = length;
7034  }
7035 
7036  /* encode length number of bytes from inbuf */
7037  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
7038  if( POLARSSL_MODE_CBC == cipher_info->mode )
7039  {
7040  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7041  }
7042  else
7043  {
7044  fct_chk( outlen == enclen );
7045  }
7046 
7047  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
7048  if( POLARSSL_MODE_CBC == cipher_info->mode )
7049  {
7050  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7051  }
7052  else
7053  {
7054  fct_chk( outlen == 0 );
7055  }
7056 
7057 
7058  /* decode the previously encoded string */
7059  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7060  if( POLARSSL_MODE_CBC == cipher_info->mode )
7061  {
7062  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7063  }
7064  else
7065  {
7066  fct_chk( enclen == outlen );
7067  }
7068 
7069  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7070  if( POLARSSL_MODE_CBC == cipher_info->mode )
7071  {
7072  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7073  }
7074  else
7075  {
7076  fct_chk( outlen == 0 );
7077  }
7078 
7079 
7080  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7081 
7082  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7083  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7084  FCT_TEST_END();
7085 #endif /* POLARSSL_DES_C */
7086 
7087 #ifdef POLARSSL_DES_C
7088 
7089  FCT_TEST_BGN(des3_encrypt_and_decrypt_48_bytes)
7090  size_t length = 48;
7091  unsigned char key[32];
7092  unsigned char iv[16];
7093 
7094  const cipher_info_t *cipher_info;
7095  cipher_context_t ctx_dec;
7096  cipher_context_t ctx_enc;
7097 
7098  unsigned char inbuf[64];
7099  unsigned char encbuf[64];
7100  unsigned char decbuf[64];
7101 
7102  size_t outlen = 0;
7103  size_t enclen = 0;
7104 
7105  memset( key, 0, 32 );
7106  memset( iv , 0, 16 );
7107 
7108  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7109  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7110 
7111  memset( inbuf, 5, 64 );
7112  memset( encbuf, 0, 64 );
7113  memset( decbuf, 0, 64 );
7114 
7115  /* Check and get info structures */
7117  fct_chk( NULL != cipher_info );
7118  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
7119 
7120  /* Initialise enc and dec contexts */
7121  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7122  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7123 
7124  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7125  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7126 
7127  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7128  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7129 
7130  if( POLARSSL_MODE_CBC == cipher_info->mode )
7131  {
7132  enclen = cipher_get_block_size( &ctx_enc )
7133  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7134  }
7135  else
7136  {
7137  enclen = length;
7138  }
7139 
7140  /* encode length number of bytes from inbuf */
7141  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
7142  if( POLARSSL_MODE_CBC == cipher_info->mode )
7143  {
7144  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7145  }
7146  else
7147  {
7148  fct_chk( outlen == enclen );
7149  }
7150 
7151  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
7152  if( POLARSSL_MODE_CBC == cipher_info->mode )
7153  {
7154  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7155  }
7156  else
7157  {
7158  fct_chk( outlen == 0 );
7159  }
7160 
7161 
7162  /* decode the previously encoded string */
7163  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7164  if( POLARSSL_MODE_CBC == cipher_info->mode )
7165  {
7166  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7167  }
7168  else
7169  {
7170  fct_chk( enclen == outlen );
7171  }
7172 
7173  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7174  if( POLARSSL_MODE_CBC == cipher_info->mode )
7175  {
7176  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7177  }
7178  else
7179  {
7180  fct_chk( outlen == 0 );
7181  }
7182 
7183 
7184  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7185 
7186  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7187  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7188  FCT_TEST_END();
7189 #endif /* POLARSSL_DES_C */
7190 
7191 #ifdef POLARSSL_DES_C
7192 
7193  FCT_TEST_BGN(des3_encrypt_and_decrypt_49_bytes)
7194  size_t length = 49;
7195  unsigned char key[32];
7196  unsigned char iv[16];
7197 
7198  const cipher_info_t *cipher_info;
7199  cipher_context_t ctx_dec;
7200  cipher_context_t ctx_enc;
7201 
7202  unsigned char inbuf[64];
7203  unsigned char encbuf[64];
7204  unsigned char decbuf[64];
7205 
7206  size_t outlen = 0;
7207  size_t enclen = 0;
7208 
7209  memset( key, 0, 32 );
7210  memset( iv , 0, 16 );
7211 
7212  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7213  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7214 
7215  memset( inbuf, 5, 64 );
7216  memset( encbuf, 0, 64 );
7217  memset( decbuf, 0, 64 );
7218 
7219  /* Check and get info structures */
7221  fct_chk( NULL != cipher_info );
7222  fct_chk( cipher_info_from_string( "DES-EDE3-CBC" ) == cipher_info );
7223 
7224  /* Initialise enc and dec contexts */
7225  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7226  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7227 
7228  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7229  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7230 
7231  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7232  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7233 
7234  if( POLARSSL_MODE_CBC == cipher_info->mode )
7235  {
7236  enclen = cipher_get_block_size( &ctx_enc )
7237  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7238  }
7239  else
7240  {
7241  enclen = length;
7242  }
7243 
7244  /* encode length number of bytes from inbuf */
7245  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
7246  if( POLARSSL_MODE_CBC == cipher_info->mode )
7247  {
7248  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7249  }
7250  else
7251  {
7252  fct_chk( outlen == enclen );
7253  }
7254 
7255  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
7256  if( POLARSSL_MODE_CBC == cipher_info->mode )
7257  {
7258  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7259  }
7260  else
7261  {
7262  fct_chk( outlen == 0 );
7263  }
7264 
7265 
7266  /* decode the previously encoded string */
7267  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7268  if( POLARSSL_MODE_CBC == cipher_info->mode )
7269  {
7270  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7271  }
7272  else
7273  {
7274  fct_chk( enclen == outlen );
7275  }
7276 
7277  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7278  if( POLARSSL_MODE_CBC == cipher_info->mode )
7279  {
7280  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7281  }
7282  else
7283  {
7284  fct_chk( outlen == 0 );
7285  }
7286 
7287 
7288  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7289 
7290  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7291  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7292  FCT_TEST_END();
7293 #endif /* POLARSSL_DES_C */
7294 
7295 #ifdef POLARSSL_DES_C
7296 
7297  FCT_TEST_BGN(des3_encrypt_and_decrypt_0_bytes_in_multiple_parts)
7298  size_t first_length = 0;
7299  size_t second_length = 0;
7300  size_t length = first_length + second_length;
7301  unsigned char key[32];
7302  unsigned char iv[16];
7303 
7304  cipher_context_t ctx_dec;
7305  cipher_context_t ctx_enc;
7306  const cipher_info_t *cipher_info;
7307 
7308  unsigned char inbuf[64];
7309  unsigned char encbuf[64];
7310  unsigned char decbuf[64];
7311 
7312  size_t outlen = 0;
7313  size_t totaloutlen = 0;
7314  size_t enclen = 0;
7315 
7316  memset( key, 0, 32 );
7317  memset( iv , 0, 16 );
7318 
7319  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7320  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7321 
7322  memset( inbuf, 5, 64 );
7323  memset( encbuf, 0, 64 );
7324  memset( decbuf, 0, 64 );
7325 
7326  /* Initialise enc and dec contexts */
7328  fct_chk( NULL != cipher_info);
7329 
7330  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7331  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7332 
7333  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7334  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7335 
7336  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7337  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7338 
7339  if( POLARSSL_MODE_CBC == cipher_info->mode )
7340  {
7341  enclen = cipher_get_block_size(&ctx_enc )
7342  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7343  }
7344  else
7345  {
7346  enclen = length;
7347  }
7348 
7349  /* encode length number of bytes from inbuf */
7350  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7351  totaloutlen = outlen;
7352  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7353  totaloutlen += outlen;
7354  if( POLARSSL_MODE_CBC == cipher_info->mode )
7355  {
7356  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7357  }
7358  else
7359  {
7360  fct_chk( totaloutlen == enclen );
7361  }
7362  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7363  totaloutlen += outlen;
7364  if( POLARSSL_MODE_CBC == cipher_info->mode )
7365  {
7366  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7367  }
7368  else
7369  {
7370  fct_chk( outlen == 0 );
7371  }
7372 
7373  /* decode the previously encoded string */
7374  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7375  if( POLARSSL_MODE_CBC == cipher_info->mode )
7376  {
7377  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7378  }
7379  else
7380  {
7381  fct_chk( enclen == outlen );
7382  }
7383  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7384  if( POLARSSL_MODE_CBC == cipher_info->mode )
7385  {
7386  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7387  }
7388  else
7389  {
7390  fct_chk( outlen == 0 );
7391  }
7392 
7393 
7394  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7395 
7396  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7397  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7398  FCT_TEST_END();
7399 #endif /* POLARSSL_DES_C */
7400 
7401 #ifdef POLARSSL_DES_C
7402 
7403  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
7404  size_t first_length = 1;
7405  size_t second_length = 0;
7406  size_t length = first_length + second_length;
7407  unsigned char key[32];
7408  unsigned char iv[16];
7409 
7410  cipher_context_t ctx_dec;
7411  cipher_context_t ctx_enc;
7412  const cipher_info_t *cipher_info;
7413 
7414  unsigned char inbuf[64];
7415  unsigned char encbuf[64];
7416  unsigned char decbuf[64];
7417 
7418  size_t outlen = 0;
7419  size_t totaloutlen = 0;
7420  size_t enclen = 0;
7421 
7422  memset( key, 0, 32 );
7423  memset( iv , 0, 16 );
7424 
7425  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7426  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7427 
7428  memset( inbuf, 5, 64 );
7429  memset( encbuf, 0, 64 );
7430  memset( decbuf, 0, 64 );
7431 
7432  /* Initialise enc and dec contexts */
7434  fct_chk( NULL != cipher_info);
7435 
7436  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7437  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7438 
7439  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7440  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7441 
7442  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7443  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7444 
7445  if( POLARSSL_MODE_CBC == cipher_info->mode )
7446  {
7447  enclen = cipher_get_block_size(&ctx_enc )
7448  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7449  }
7450  else
7451  {
7452  enclen = length;
7453  }
7454 
7455  /* encode length number of bytes from inbuf */
7456  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7457  totaloutlen = outlen;
7458  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7459  totaloutlen += outlen;
7460  if( POLARSSL_MODE_CBC == cipher_info->mode )
7461  {
7462  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7463  }
7464  else
7465  {
7466  fct_chk( totaloutlen == enclen );
7467  }
7468  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7469  totaloutlen += outlen;
7470  if( POLARSSL_MODE_CBC == cipher_info->mode )
7471  {
7472  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7473  }
7474  else
7475  {
7476  fct_chk( outlen == 0 );
7477  }
7478 
7479  /* decode the previously encoded string */
7480  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7481  if( POLARSSL_MODE_CBC == cipher_info->mode )
7482  {
7483  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7484  }
7485  else
7486  {
7487  fct_chk( enclen == outlen );
7488  }
7489  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7490  if( POLARSSL_MODE_CBC == cipher_info->mode )
7491  {
7492  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7493  }
7494  else
7495  {
7496  fct_chk( outlen == 0 );
7497  }
7498 
7499 
7500  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7501 
7502  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7503  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7504  FCT_TEST_END();
7505 #endif /* POLARSSL_DES_C */
7506 
7507 #ifdef POLARSSL_DES_C
7508 
7509  FCT_TEST_BGN(des3_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
7510  size_t first_length = 0;
7511  size_t second_length = 1;
7512  size_t length = first_length + second_length;
7513  unsigned char key[32];
7514  unsigned char iv[16];
7515 
7516  cipher_context_t ctx_dec;
7517  cipher_context_t ctx_enc;
7518  const cipher_info_t *cipher_info;
7519 
7520  unsigned char inbuf[64];
7521  unsigned char encbuf[64];
7522  unsigned char decbuf[64];
7523 
7524  size_t outlen = 0;
7525  size_t totaloutlen = 0;
7526  size_t enclen = 0;
7527 
7528  memset( key, 0, 32 );
7529  memset( iv , 0, 16 );
7530 
7531  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7532  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7533 
7534  memset( inbuf, 5, 64 );
7535  memset( encbuf, 0, 64 );
7536  memset( decbuf, 0, 64 );
7537 
7538  /* Initialise enc and dec contexts */
7540  fct_chk( NULL != cipher_info);
7541 
7542  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7543  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7544 
7545  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7546  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7547 
7548  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7549  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7550 
7551  if( POLARSSL_MODE_CBC == cipher_info->mode )
7552  {
7553  enclen = cipher_get_block_size(&ctx_enc )
7554  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7555  }
7556  else
7557  {
7558  enclen = length;
7559  }
7560 
7561  /* encode length number of bytes from inbuf */
7562  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7563  totaloutlen = outlen;
7564  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7565  totaloutlen += outlen;
7566  if( POLARSSL_MODE_CBC == cipher_info->mode )
7567  {
7568  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7569  }
7570  else
7571  {
7572  fct_chk( totaloutlen == enclen );
7573  }
7574  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7575  totaloutlen += outlen;
7576  if( POLARSSL_MODE_CBC == cipher_info->mode )
7577  {
7578  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7579  }
7580  else
7581  {
7582  fct_chk( outlen == 0 );
7583  }
7584 
7585  /* decode the previously encoded string */
7586  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7587  if( POLARSSL_MODE_CBC == cipher_info->mode )
7588  {
7589  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7590  }
7591  else
7592  {
7593  fct_chk( enclen == outlen );
7594  }
7595  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7596  if( POLARSSL_MODE_CBC == cipher_info->mode )
7597  {
7598  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7599  }
7600  else
7601  {
7602  fct_chk( outlen == 0 );
7603  }
7604 
7605 
7606  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7607 
7608  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7609  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7610  FCT_TEST_END();
7611 #endif /* POLARSSL_DES_C */
7612 
7613 #ifdef POLARSSL_DES_C
7614 
7615  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
7616  size_t first_length = 16;
7617  size_t second_length = 0;
7618  size_t length = first_length + second_length;
7619  unsigned char key[32];
7620  unsigned char iv[16];
7621 
7622  cipher_context_t ctx_dec;
7623  cipher_context_t ctx_enc;
7624  const cipher_info_t *cipher_info;
7625 
7626  unsigned char inbuf[64];
7627  unsigned char encbuf[64];
7628  unsigned char decbuf[64];
7629 
7630  size_t outlen = 0;
7631  size_t totaloutlen = 0;
7632  size_t enclen = 0;
7633 
7634  memset( key, 0, 32 );
7635  memset( iv , 0, 16 );
7636 
7637  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7638  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7639 
7640  memset( inbuf, 5, 64 );
7641  memset( encbuf, 0, 64 );
7642  memset( decbuf, 0, 64 );
7643 
7644  /* Initialise enc and dec contexts */
7646  fct_chk( NULL != cipher_info);
7647 
7648  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7649  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7650 
7651  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7652  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7653 
7654  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7655  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7656 
7657  if( POLARSSL_MODE_CBC == cipher_info->mode )
7658  {
7659  enclen = cipher_get_block_size(&ctx_enc )
7660  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7661  }
7662  else
7663  {
7664  enclen = length;
7665  }
7666 
7667  /* encode length number of bytes from inbuf */
7668  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7669  totaloutlen = outlen;
7670  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7671  totaloutlen += outlen;
7672  if( POLARSSL_MODE_CBC == cipher_info->mode )
7673  {
7674  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7675  }
7676  else
7677  {
7678  fct_chk( totaloutlen == enclen );
7679  }
7680  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7681  totaloutlen += outlen;
7682  if( POLARSSL_MODE_CBC == cipher_info->mode )
7683  {
7684  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7685  }
7686  else
7687  {
7688  fct_chk( outlen == 0 );
7689  }
7690 
7691  /* decode the previously encoded string */
7692  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7693  if( POLARSSL_MODE_CBC == cipher_info->mode )
7694  {
7695  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7696  }
7697  else
7698  {
7699  fct_chk( enclen == outlen );
7700  }
7701  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7702  if( POLARSSL_MODE_CBC == cipher_info->mode )
7703  {
7704  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7705  }
7706  else
7707  {
7708  fct_chk( outlen == 0 );
7709  }
7710 
7711 
7712  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7713 
7714  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7715  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7716  FCT_TEST_END();
7717 #endif /* POLARSSL_DES_C */
7718 
7719 #ifdef POLARSSL_DES_C
7720 
7721  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
7722  size_t first_length = 0;
7723  size_t second_length = 16;
7724  size_t length = first_length + second_length;
7725  unsigned char key[32];
7726  unsigned char iv[16];
7727 
7728  cipher_context_t ctx_dec;
7729  cipher_context_t ctx_enc;
7730  const cipher_info_t *cipher_info;
7731 
7732  unsigned char inbuf[64];
7733  unsigned char encbuf[64];
7734  unsigned char decbuf[64];
7735 
7736  size_t outlen = 0;
7737  size_t totaloutlen = 0;
7738  size_t enclen = 0;
7739 
7740  memset( key, 0, 32 );
7741  memset( iv , 0, 16 );
7742 
7743  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7744  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7745 
7746  memset( inbuf, 5, 64 );
7747  memset( encbuf, 0, 64 );
7748  memset( decbuf, 0, 64 );
7749 
7750  /* Initialise enc and dec contexts */
7752  fct_chk( NULL != cipher_info);
7753 
7754  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7755  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7756 
7757  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7758  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7759 
7760  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7761  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7762 
7763  if( POLARSSL_MODE_CBC == cipher_info->mode )
7764  {
7765  enclen = cipher_get_block_size(&ctx_enc )
7766  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7767  }
7768  else
7769  {
7770  enclen = length;
7771  }
7772 
7773  /* encode length number of bytes from inbuf */
7774  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7775  totaloutlen = outlen;
7776  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7777  totaloutlen += outlen;
7778  if( POLARSSL_MODE_CBC == cipher_info->mode )
7779  {
7780  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7781  }
7782  else
7783  {
7784  fct_chk( totaloutlen == enclen );
7785  }
7786  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7787  totaloutlen += outlen;
7788  if( POLARSSL_MODE_CBC == cipher_info->mode )
7789  {
7790  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7791  }
7792  else
7793  {
7794  fct_chk( outlen == 0 );
7795  }
7796 
7797  /* decode the previously encoded string */
7798  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7799  if( POLARSSL_MODE_CBC == cipher_info->mode )
7800  {
7801  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7802  }
7803  else
7804  {
7805  fct_chk( enclen == outlen );
7806  }
7807  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7808  if( POLARSSL_MODE_CBC == cipher_info->mode )
7809  {
7810  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7811  }
7812  else
7813  {
7814  fct_chk( outlen == 0 );
7815  }
7816 
7817 
7818  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7819 
7820  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7821  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7822  FCT_TEST_END();
7823 #endif /* POLARSSL_DES_C */
7824 
7825 #ifdef POLARSSL_DES_C
7826 
7827  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
7828  size_t first_length = 1;
7829  size_t second_length = 15;
7830  size_t length = first_length + second_length;
7831  unsigned char key[32];
7832  unsigned char iv[16];
7833 
7834  cipher_context_t ctx_dec;
7835  cipher_context_t ctx_enc;
7836  const cipher_info_t *cipher_info;
7837 
7838  unsigned char inbuf[64];
7839  unsigned char encbuf[64];
7840  unsigned char decbuf[64];
7841 
7842  size_t outlen = 0;
7843  size_t totaloutlen = 0;
7844  size_t enclen = 0;
7845 
7846  memset( key, 0, 32 );
7847  memset( iv , 0, 16 );
7848 
7849  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7850  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7851 
7852  memset( inbuf, 5, 64 );
7853  memset( encbuf, 0, 64 );
7854  memset( decbuf, 0, 64 );
7855 
7856  /* Initialise enc and dec contexts */
7858  fct_chk( NULL != cipher_info);
7859 
7860  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7861  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7862 
7863  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7864  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7865 
7866  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7867  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7868 
7869  if( POLARSSL_MODE_CBC == cipher_info->mode )
7870  {
7871  enclen = cipher_get_block_size(&ctx_enc )
7872  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7873  }
7874  else
7875  {
7876  enclen = length;
7877  }
7878 
7879  /* encode length number of bytes from inbuf */
7880  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7881  totaloutlen = outlen;
7882  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7883  totaloutlen += outlen;
7884  if( POLARSSL_MODE_CBC == cipher_info->mode )
7885  {
7886  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7887  }
7888  else
7889  {
7890  fct_chk( totaloutlen == enclen );
7891  }
7892  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7893  totaloutlen += outlen;
7894  if( POLARSSL_MODE_CBC == cipher_info->mode )
7895  {
7896  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
7897  }
7898  else
7899  {
7900  fct_chk( outlen == 0 );
7901  }
7902 
7903  /* decode the previously encoded string */
7904  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
7905  if( POLARSSL_MODE_CBC == cipher_info->mode )
7906  {
7907  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
7908  }
7909  else
7910  {
7911  fct_chk( enclen == outlen );
7912  }
7913  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
7914  if( POLARSSL_MODE_CBC == cipher_info->mode )
7915  {
7916  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
7917  }
7918  else
7919  {
7920  fct_chk( outlen == 0 );
7921  }
7922 
7923 
7924  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
7925 
7926  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
7927  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
7928  FCT_TEST_END();
7929 #endif /* POLARSSL_DES_C */
7930 
7931 #ifdef POLARSSL_DES_C
7932 
7933  FCT_TEST_BGN(des3_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
7934  size_t first_length = 15;
7935  size_t second_length = 1;
7936  size_t length = first_length + second_length;
7937  unsigned char key[32];
7938  unsigned char iv[16];
7939 
7940  cipher_context_t ctx_dec;
7941  cipher_context_t ctx_enc;
7942  const cipher_info_t *cipher_info;
7943 
7944  unsigned char inbuf[64];
7945  unsigned char encbuf[64];
7946  unsigned char decbuf[64];
7947 
7948  size_t outlen = 0;
7949  size_t totaloutlen = 0;
7950  size_t enclen = 0;
7951 
7952  memset( key, 0, 32 );
7953  memset( iv , 0, 16 );
7954 
7955  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
7956  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
7957 
7958  memset( inbuf, 5, 64 );
7959  memset( encbuf, 0, 64 );
7960  memset( decbuf, 0, 64 );
7961 
7962  /* Initialise enc and dec contexts */
7964  fct_chk( NULL != cipher_info);
7965 
7966  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
7967  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
7968 
7969  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
7970  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
7971 
7972  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
7973  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
7974 
7975  if( POLARSSL_MODE_CBC == cipher_info->mode )
7976  {
7977  enclen = cipher_get_block_size(&ctx_enc )
7978  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
7979  }
7980  else
7981  {
7982  enclen = length;
7983  }
7984 
7985  /* encode length number of bytes from inbuf */
7986  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
7987  totaloutlen = outlen;
7988  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
7989  totaloutlen += outlen;
7990  if( POLARSSL_MODE_CBC == cipher_info->mode )
7991  {
7992  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
7993  }
7994  else
7995  {
7996  fct_chk( totaloutlen == enclen );
7997  }
7998  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
7999  totaloutlen += outlen;
8000  if( POLARSSL_MODE_CBC == cipher_info->mode )
8001  {
8002  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8003  }
8004  else
8005  {
8006  fct_chk( outlen == 0 );
8007  }
8008 
8009  /* decode the previously encoded string */
8010  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8011  if( POLARSSL_MODE_CBC == cipher_info->mode )
8012  {
8013  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8014  }
8015  else
8016  {
8017  fct_chk( enclen == outlen );
8018  }
8019  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8020  if( POLARSSL_MODE_CBC == cipher_info->mode )
8021  {
8022  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8023  }
8024  else
8025  {
8026  fct_chk( outlen == 0 );
8027  }
8028 
8029 
8030  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8031 
8032  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8033  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8034  FCT_TEST_END();
8035 #endif /* POLARSSL_DES_C */
8036 
8037 #ifdef POLARSSL_DES_C
8038 
8039  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
8040  size_t first_length = 15;
8041  size_t second_length = 7;
8042  size_t length = first_length + second_length;
8043  unsigned char key[32];
8044  unsigned char iv[16];
8045 
8046  cipher_context_t ctx_dec;
8047  cipher_context_t ctx_enc;
8048  const cipher_info_t *cipher_info;
8049 
8050  unsigned char inbuf[64];
8051  unsigned char encbuf[64];
8052  unsigned char decbuf[64];
8053 
8054  size_t outlen = 0;
8055  size_t totaloutlen = 0;
8056  size_t enclen = 0;
8057 
8058  memset( key, 0, 32 );
8059  memset( iv , 0, 16 );
8060 
8061  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8062  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8063 
8064  memset( inbuf, 5, 64 );
8065  memset( encbuf, 0, 64 );
8066  memset( decbuf, 0, 64 );
8067 
8068  /* Initialise enc and dec contexts */
8070  fct_chk( NULL != cipher_info);
8071 
8072  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8073  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8074 
8075  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
8076  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
8077 
8078  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8079  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8080 
8081  if( POLARSSL_MODE_CBC == cipher_info->mode )
8082  {
8083  enclen = cipher_get_block_size(&ctx_enc )
8084  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8085  }
8086  else
8087  {
8088  enclen = length;
8089  }
8090 
8091  /* encode length number of bytes from inbuf */
8092  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8093  totaloutlen = outlen;
8094  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8095  totaloutlen += outlen;
8096  if( POLARSSL_MODE_CBC == cipher_info->mode )
8097  {
8098  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8099  }
8100  else
8101  {
8102  fct_chk( totaloutlen == enclen );
8103  }
8104  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8105  totaloutlen += outlen;
8106  if( POLARSSL_MODE_CBC == cipher_info->mode )
8107  {
8108  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8109  }
8110  else
8111  {
8112  fct_chk( outlen == 0 );
8113  }
8114 
8115  /* decode the previously encoded string */
8116  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8117  if( POLARSSL_MODE_CBC == cipher_info->mode )
8118  {
8119  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8120  }
8121  else
8122  {
8123  fct_chk( enclen == outlen );
8124  }
8125  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8126  if( POLARSSL_MODE_CBC == cipher_info->mode )
8127  {
8128  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8129  }
8130  else
8131  {
8132  fct_chk( outlen == 0 );
8133  }
8134 
8135 
8136  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8137 
8138  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8139  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8140  FCT_TEST_END();
8141 #endif /* POLARSSL_DES_C */
8142 
8143 #ifdef POLARSSL_DES_C
8144 
8145  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
8146  size_t first_length = 16;
8147  size_t second_length = 6;
8148  size_t length = first_length + second_length;
8149  unsigned char key[32];
8150  unsigned char iv[16];
8151 
8152  cipher_context_t ctx_dec;
8153  cipher_context_t ctx_enc;
8154  const cipher_info_t *cipher_info;
8155 
8156  unsigned char inbuf[64];
8157  unsigned char encbuf[64];
8158  unsigned char decbuf[64];
8159 
8160  size_t outlen = 0;
8161  size_t totaloutlen = 0;
8162  size_t enclen = 0;
8163 
8164  memset( key, 0, 32 );
8165  memset( iv , 0, 16 );
8166 
8167  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8168  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8169 
8170  memset( inbuf, 5, 64 );
8171  memset( encbuf, 0, 64 );
8172  memset( decbuf, 0, 64 );
8173 
8174  /* Initialise enc and dec contexts */
8176  fct_chk( NULL != cipher_info);
8177 
8178  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8179  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8180 
8181  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
8182  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
8183 
8184  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8185  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8186 
8187  if( POLARSSL_MODE_CBC == cipher_info->mode )
8188  {
8189  enclen = cipher_get_block_size(&ctx_enc )
8190  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8191  }
8192  else
8193  {
8194  enclen = length;
8195  }
8196 
8197  /* encode length number of bytes from inbuf */
8198  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8199  totaloutlen = outlen;
8200  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8201  totaloutlen += outlen;
8202  if( POLARSSL_MODE_CBC == cipher_info->mode )
8203  {
8204  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8205  }
8206  else
8207  {
8208  fct_chk( totaloutlen == enclen );
8209  }
8210  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8211  totaloutlen += outlen;
8212  if( POLARSSL_MODE_CBC == cipher_info->mode )
8213  {
8214  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8215  }
8216  else
8217  {
8218  fct_chk( outlen == 0 );
8219  }
8220 
8221  /* decode the previously encoded string */
8222  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8223  if( POLARSSL_MODE_CBC == cipher_info->mode )
8224  {
8225  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8226  }
8227  else
8228  {
8229  fct_chk( enclen == outlen );
8230  }
8231  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8232  if( POLARSSL_MODE_CBC == cipher_info->mode )
8233  {
8234  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8235  }
8236  else
8237  {
8238  fct_chk( outlen == 0 );
8239  }
8240 
8241 
8242  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8243 
8244  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8245  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8246  FCT_TEST_END();
8247 #endif /* POLARSSL_DES_C */
8248 
8249 #ifdef POLARSSL_DES_C
8250 
8251  FCT_TEST_BGN(des3_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
8252  size_t first_length = 17;
8253  size_t second_length = 6;
8254  size_t length = first_length + second_length;
8255  unsigned char key[32];
8256  unsigned char iv[16];
8257 
8258  cipher_context_t ctx_dec;
8259  cipher_context_t ctx_enc;
8260  const cipher_info_t *cipher_info;
8261 
8262  unsigned char inbuf[64];
8263  unsigned char encbuf[64];
8264  unsigned char decbuf[64];
8265 
8266  size_t outlen = 0;
8267  size_t totaloutlen = 0;
8268  size_t enclen = 0;
8269 
8270  memset( key, 0, 32 );
8271  memset( iv , 0, 16 );
8272 
8273  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8274  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8275 
8276  memset( inbuf, 5, 64 );
8277  memset( encbuf, 0, 64 );
8278  memset( decbuf, 0, 64 );
8279 
8280  /* Initialise enc and dec contexts */
8282  fct_chk( NULL != cipher_info);
8283 
8284  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8285  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8286 
8287  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
8288  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
8289 
8290  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8291  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8292 
8293  if( POLARSSL_MODE_CBC == cipher_info->mode )
8294  {
8295  enclen = cipher_get_block_size(&ctx_enc )
8296  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8297  }
8298  else
8299  {
8300  enclen = length;
8301  }
8302 
8303  /* encode length number of bytes from inbuf */
8304  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8305  totaloutlen = outlen;
8306  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8307  totaloutlen += outlen;
8308  if( POLARSSL_MODE_CBC == cipher_info->mode )
8309  {
8310  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8311  }
8312  else
8313  {
8314  fct_chk( totaloutlen == enclen );
8315  }
8316  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8317  totaloutlen += outlen;
8318  if( POLARSSL_MODE_CBC == cipher_info->mode )
8319  {
8320  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8321  }
8322  else
8323  {
8324  fct_chk( outlen == 0 );
8325  }
8326 
8327  /* decode the previously encoded string */
8328  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8329  if( POLARSSL_MODE_CBC == cipher_info->mode )
8330  {
8331  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8332  }
8333  else
8334  {
8335  fct_chk( enclen == outlen );
8336  }
8337  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8338  if( POLARSSL_MODE_CBC == cipher_info->mode )
8339  {
8340  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8341  }
8342  else
8343  {
8344  fct_chk( outlen == 0 );
8345  }
8346 
8347 
8348  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8349 
8350  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8351  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8352  FCT_TEST_END();
8353 #endif /* POLARSSL_DES_C */
8354 
8355 #ifdef POLARSSL_DES_C
8356 
8357  FCT_TEST_BGN(des3_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
8358  size_t first_length = 16;
8359  size_t second_length = 16;
8360  size_t length = first_length + second_length;
8361  unsigned char key[32];
8362  unsigned char iv[16];
8363 
8364  cipher_context_t ctx_dec;
8365  cipher_context_t ctx_enc;
8366  const cipher_info_t *cipher_info;
8367 
8368  unsigned char inbuf[64];
8369  unsigned char encbuf[64];
8370  unsigned char decbuf[64];
8371 
8372  size_t outlen = 0;
8373  size_t totaloutlen = 0;
8374  size_t enclen = 0;
8375 
8376  memset( key, 0, 32 );
8377  memset( iv , 0, 16 );
8378 
8379  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
8380  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
8381 
8382  memset( inbuf, 5, 64 );
8383  memset( encbuf, 0, 64 );
8384  memset( decbuf, 0, 64 );
8385 
8386  /* Initialise enc and dec contexts */
8388  fct_chk( NULL != cipher_info);
8389 
8390  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
8391  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
8392 
8393  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 168, POLARSSL_DECRYPT ) );
8394  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 168, POLARSSL_ENCRYPT ) );
8395 
8396  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
8397  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
8398 
8399  if( POLARSSL_MODE_CBC == cipher_info->mode )
8400  {
8401  enclen = cipher_get_block_size(&ctx_enc )
8402  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
8403  }
8404  else
8405  {
8406  enclen = length;
8407  }
8408 
8409  /* encode length number of bytes from inbuf */
8410  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
8411  totaloutlen = outlen;
8412  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
8413  totaloutlen += outlen;
8414  if( POLARSSL_MODE_CBC == cipher_info->mode )
8415  {
8416  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
8417  }
8418  else
8419  {
8420  fct_chk( totaloutlen == enclen );
8421  }
8422  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
8423  totaloutlen += outlen;
8424  if( POLARSSL_MODE_CBC == cipher_info->mode )
8425  {
8426  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
8427  }
8428  else
8429  {
8430  fct_chk( outlen == 0 );
8431  }
8432 
8433  /* decode the previously encoded string */
8434  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
8435  if( POLARSSL_MODE_CBC == cipher_info->mode )
8436  {
8437  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
8438  }
8439  else
8440  {
8441  fct_chk( enclen == outlen );
8442  }
8443  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
8444  if( POLARSSL_MODE_CBC == cipher_info->mode )
8445  {
8446  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
8447  }
8448  else
8449  {
8450  fct_chk( outlen == 0 );
8451  }
8452 
8453 
8454  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
8455 
8456  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
8457  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
8458  FCT_TEST_END();
8459 #endif /* POLARSSL_DES_C */
8460 
8461  }
8462  FCT_SUITE_END();
8463 
8464 #endif /* POLARSSL_CIPHER_C */
8465 
8466 }
8467 FCT_END();
8468