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