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