PolarSSL v1.2.8
test_suite_cipher.null.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_CIPHER_NULL_CIPHER
283 
284  FCT_TEST_BGN(null_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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
320  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
385 
386 #ifdef POLARSSL_CIPHER_NULL_CIPHER
387 
388  FCT_TEST_BGN(null_encrypt_and_decrypt_1_bytes)
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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
424  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
489 
490 #ifdef POLARSSL_CIPHER_NULL_CIPHER
491 
492  FCT_TEST_BGN(null_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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
528  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
593 
594 #ifdef POLARSSL_CIPHER_NULL_CIPHER
595 
596  FCT_TEST_BGN(null_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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
632  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
697 
698 #ifdef POLARSSL_CIPHER_NULL_CIPHER
699 
700  FCT_TEST_BGN(null_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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
736  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
801 
802 #ifdef POLARSSL_CIPHER_NULL_CIPHER
803 
804  FCT_TEST_BGN(null_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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
840  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
905 
906 #ifdef POLARSSL_CIPHER_NULL_CIPHER
907 
908  FCT_TEST_BGN(null_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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
944  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
1009 
1010 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1011 
1012  FCT_TEST_BGN(null_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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
1048  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
1113 
1114 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1115 
1116  FCT_TEST_BGN(null_encrypt_and_decrypt_31_bytes)
1117  size_t length = 31;
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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
1152  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
1217 
1218 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1219 
1220  FCT_TEST_BGN(null_encrypt_and_decrypt_32_bytes)
1221  size_t length = 32;
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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
1256  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
1321 
1322 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1323 
1324  FCT_TEST_BGN(null_encrypt_and_decrypt_33_bytes)
1325  size_t length = 33;
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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
1360  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
1425 
1426 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1427 
1428  FCT_TEST_BGN(null_encrypt_and_decrypt_47_bytes)
1429  size_t length = 47;
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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
1464  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
1529 
1530 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1531 
1532  FCT_TEST_BGN(null_encrypt_and_decrypt_48_bytes)
1533  size_t length = 48;
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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
1568  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
1633 
1634 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1635 
1636  FCT_TEST_BGN(null_encrypt_and_decrypt_49_bytes)
1637  size_t length = 49;
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( "NULL" ) == 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, 0, POLARSSL_DECRYPT ) );
1672  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, 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_CIPHER_NULL_CIPHER */
1737 
1738 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1739 
1740  FCT_TEST_BGN(null_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
1741  size_t first_length = 1;
1742  size_t second_length = 0;
1743  size_t length = first_length + second_length;
1744  unsigned char key[32];
1745  unsigned char iv[16];
1746 
1747  cipher_context_t ctx_dec;
1748  cipher_context_t ctx_enc;
1749  const cipher_info_t *cipher_info;
1750 
1751  unsigned char inbuf[64];
1752  unsigned char encbuf[64];
1753  unsigned char decbuf[64];
1754 
1755  size_t outlen = 0;
1756  size_t totaloutlen = 0;
1757  size_t enclen = 0;
1758 
1759  memset( key, 0, 32 );
1760  memset( iv , 0, 16 );
1761 
1762  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1763  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1764 
1765  memset( inbuf, 5, 64 );
1766  memset( encbuf, 0, 64 );
1767  memset( decbuf, 0, 64 );
1768 
1769  /* Initialise enc and dec contexts */
1771  fct_chk( NULL != cipher_info);
1772 
1773  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1774  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1775 
1776  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1777  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1778 
1779  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1780  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1781 
1782  if( POLARSSL_MODE_CBC == cipher_info->mode )
1783  {
1784  enclen = cipher_get_block_size(&ctx_enc )
1785  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1786  }
1787  else
1788  {
1789  enclen = length;
1790  }
1791 
1792  /* encode length number of bytes from inbuf */
1793  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
1794  totaloutlen = outlen;
1795  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
1796  totaloutlen += outlen;
1797  if( POLARSSL_MODE_CBC == cipher_info->mode )
1798  {
1799  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1800  }
1801  else
1802  {
1803  fct_chk( totaloutlen == enclen );
1804  }
1805  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
1806  totaloutlen += outlen;
1807  if( POLARSSL_MODE_CBC == cipher_info->mode )
1808  {
1809  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1810  }
1811  else
1812  {
1813  fct_chk( outlen == 0 );
1814  }
1815 
1816  /* decode the previously encoded string */
1817  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1818  if( POLARSSL_MODE_CBC == cipher_info->mode )
1819  {
1820  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1821  }
1822  else
1823  {
1824  fct_chk( enclen == outlen );
1825  }
1826  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1827  if( POLARSSL_MODE_CBC == cipher_info->mode )
1828  {
1829  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1830  }
1831  else
1832  {
1833  fct_chk( outlen == 0 );
1834  }
1835 
1836 
1837  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1838 
1839  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1840  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1841  FCT_TEST_END();
1842 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1843 
1844 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1845 
1846  FCT_TEST_BGN(null_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
1847  size_t first_length = 0;
1848  size_t second_length = 1;
1849  size_t length = first_length + second_length;
1850  unsigned char key[32];
1851  unsigned char iv[16];
1852 
1853  cipher_context_t ctx_dec;
1854  cipher_context_t ctx_enc;
1855  const cipher_info_t *cipher_info;
1856 
1857  unsigned char inbuf[64];
1858  unsigned char encbuf[64];
1859  unsigned char decbuf[64];
1860 
1861  size_t outlen = 0;
1862  size_t totaloutlen = 0;
1863  size_t enclen = 0;
1864 
1865  memset( key, 0, 32 );
1866  memset( iv , 0, 16 );
1867 
1868  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1869  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1870 
1871  memset( inbuf, 5, 64 );
1872  memset( encbuf, 0, 64 );
1873  memset( decbuf, 0, 64 );
1874 
1875  /* Initialise enc and dec contexts */
1877  fct_chk( NULL != cipher_info);
1878 
1879  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1880  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1881 
1882  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1883  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1884 
1885  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1886  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1887 
1888  if( POLARSSL_MODE_CBC == cipher_info->mode )
1889  {
1890  enclen = cipher_get_block_size(&ctx_enc )
1891  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1892  }
1893  else
1894  {
1895  enclen = length;
1896  }
1897 
1898  /* encode length number of bytes from inbuf */
1899  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
1900  totaloutlen = outlen;
1901  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
1902  totaloutlen += outlen;
1903  if( POLARSSL_MODE_CBC == cipher_info->mode )
1904  {
1905  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1906  }
1907  else
1908  {
1909  fct_chk( totaloutlen == enclen );
1910  }
1911  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
1912  totaloutlen += outlen;
1913  if( POLARSSL_MODE_CBC == cipher_info->mode )
1914  {
1915  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1916  }
1917  else
1918  {
1919  fct_chk( outlen == 0 );
1920  }
1921 
1922  /* decode the previously encoded string */
1923  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1924  if( POLARSSL_MODE_CBC == cipher_info->mode )
1925  {
1926  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1927  }
1928  else
1929  {
1930  fct_chk( enclen == outlen );
1931  }
1932  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1933  if( POLARSSL_MODE_CBC == cipher_info->mode )
1934  {
1935  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1936  }
1937  else
1938  {
1939  fct_chk( outlen == 0 );
1940  }
1941 
1942 
1943  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1944 
1945  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1946  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1947  FCT_TEST_END();
1948 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1949 
1950 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1951 
1952  FCT_TEST_BGN(null_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
1953  size_t first_length = 16;
1954  size_t second_length = 0;
1955  size_t length = first_length + second_length;
1956  unsigned char key[32];
1957  unsigned char iv[16];
1958 
1959  cipher_context_t ctx_dec;
1960  cipher_context_t ctx_enc;
1961  const cipher_info_t *cipher_info;
1962 
1963  unsigned char inbuf[64];
1964  unsigned char encbuf[64];
1965  unsigned char decbuf[64];
1966 
1967  size_t outlen = 0;
1968  size_t totaloutlen = 0;
1969  size_t enclen = 0;
1970 
1971  memset( key, 0, 32 );
1972  memset( iv , 0, 16 );
1973 
1974  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1975  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1976 
1977  memset( inbuf, 5, 64 );
1978  memset( encbuf, 0, 64 );
1979  memset( decbuf, 0, 64 );
1980 
1981  /* Initialise enc and dec contexts */
1983  fct_chk( NULL != cipher_info);
1984 
1985  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1986  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1987 
1988  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1989  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1990 
1991  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1992  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1993 
1994  if( POLARSSL_MODE_CBC == cipher_info->mode )
1995  {
1996  enclen = cipher_get_block_size(&ctx_enc )
1997  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1998  }
1999  else
2000  {
2001  enclen = length;
2002  }
2003 
2004  /* encode length number of bytes from inbuf */
2005  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2006  totaloutlen = outlen;
2007  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2008  totaloutlen += outlen;
2009  if( POLARSSL_MODE_CBC == cipher_info->mode )
2010  {
2011  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2012  }
2013  else
2014  {
2015  fct_chk( totaloutlen == enclen );
2016  }
2017  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2018  totaloutlen += outlen;
2019  if( POLARSSL_MODE_CBC == cipher_info->mode )
2020  {
2021  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2022  }
2023  else
2024  {
2025  fct_chk( outlen == 0 );
2026  }
2027 
2028  /* decode the previously encoded string */
2029  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2030  if( POLARSSL_MODE_CBC == cipher_info->mode )
2031  {
2032  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2033  }
2034  else
2035  {
2036  fct_chk( enclen == outlen );
2037  }
2038  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2039  if( POLARSSL_MODE_CBC == cipher_info->mode )
2040  {
2041  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2042  }
2043  else
2044  {
2045  fct_chk( outlen == 0 );
2046  }
2047 
2048 
2049  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2050 
2051  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2052  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2053  FCT_TEST_END();
2054 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2055 
2056 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2057 
2058  FCT_TEST_BGN(null_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
2059  size_t first_length = 0;
2060  size_t second_length = 16;
2061  size_t length = first_length + second_length;
2062  unsigned char key[32];
2063  unsigned char iv[16];
2064 
2065  cipher_context_t ctx_dec;
2066  cipher_context_t ctx_enc;
2067  const cipher_info_t *cipher_info;
2068 
2069  unsigned char inbuf[64];
2070  unsigned char encbuf[64];
2071  unsigned char decbuf[64];
2072 
2073  size_t outlen = 0;
2074  size_t totaloutlen = 0;
2075  size_t enclen = 0;
2076 
2077  memset( key, 0, 32 );
2078  memset( iv , 0, 16 );
2079 
2080  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2081  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2082 
2083  memset( inbuf, 5, 64 );
2084  memset( encbuf, 0, 64 );
2085  memset( decbuf, 0, 64 );
2086 
2087  /* Initialise enc and dec contexts */
2089  fct_chk( NULL != cipher_info);
2090 
2091  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2092  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2093 
2094  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2095  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2096 
2097  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2098  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2099 
2100  if( POLARSSL_MODE_CBC == cipher_info->mode )
2101  {
2102  enclen = cipher_get_block_size(&ctx_enc )
2103  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2104  }
2105  else
2106  {
2107  enclen = length;
2108  }
2109 
2110  /* encode length number of bytes from inbuf */
2111  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2112  totaloutlen = outlen;
2113  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2114  totaloutlen += outlen;
2115  if( POLARSSL_MODE_CBC == cipher_info->mode )
2116  {
2117  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2118  }
2119  else
2120  {
2121  fct_chk( totaloutlen == enclen );
2122  }
2123  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2124  totaloutlen += outlen;
2125  if( POLARSSL_MODE_CBC == cipher_info->mode )
2126  {
2127  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2128  }
2129  else
2130  {
2131  fct_chk( outlen == 0 );
2132  }
2133 
2134  /* decode the previously encoded string */
2135  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2136  if( POLARSSL_MODE_CBC == cipher_info->mode )
2137  {
2138  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2139  }
2140  else
2141  {
2142  fct_chk( enclen == outlen );
2143  }
2144  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2145  if( POLARSSL_MODE_CBC == cipher_info->mode )
2146  {
2147  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2148  }
2149  else
2150  {
2151  fct_chk( outlen == 0 );
2152  }
2153 
2154 
2155  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2156 
2157  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2158  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2159  FCT_TEST_END();
2160 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2161 
2162 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2163 
2164  FCT_TEST_BGN(null_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
2165  size_t first_length = 1;
2166  size_t second_length = 15;
2167  size_t length = first_length + second_length;
2168  unsigned char key[32];
2169  unsigned char iv[16];
2170 
2171  cipher_context_t ctx_dec;
2172  cipher_context_t ctx_enc;
2173  const cipher_info_t *cipher_info;
2174 
2175  unsigned char inbuf[64];
2176  unsigned char encbuf[64];
2177  unsigned char decbuf[64];
2178 
2179  size_t outlen = 0;
2180  size_t totaloutlen = 0;
2181  size_t enclen = 0;
2182 
2183  memset( key, 0, 32 );
2184  memset( iv , 0, 16 );
2185 
2186  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2187  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2188 
2189  memset( inbuf, 5, 64 );
2190  memset( encbuf, 0, 64 );
2191  memset( decbuf, 0, 64 );
2192 
2193  /* Initialise enc and dec contexts */
2195  fct_chk( NULL != cipher_info);
2196 
2197  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2198  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2199 
2200  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2201  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2202 
2203  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2204  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2205 
2206  if( POLARSSL_MODE_CBC == cipher_info->mode )
2207  {
2208  enclen = cipher_get_block_size(&ctx_enc )
2209  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2210  }
2211  else
2212  {
2213  enclen = length;
2214  }
2215 
2216  /* encode length number of bytes from inbuf */
2217  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2218  totaloutlen = outlen;
2219  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2220  totaloutlen += outlen;
2221  if( POLARSSL_MODE_CBC == cipher_info->mode )
2222  {
2223  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2224  }
2225  else
2226  {
2227  fct_chk( totaloutlen == enclen );
2228  }
2229  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2230  totaloutlen += outlen;
2231  if( POLARSSL_MODE_CBC == cipher_info->mode )
2232  {
2233  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2234  }
2235  else
2236  {
2237  fct_chk( outlen == 0 );
2238  }
2239 
2240  /* decode the previously encoded string */
2241  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2242  if( POLARSSL_MODE_CBC == cipher_info->mode )
2243  {
2244  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2245  }
2246  else
2247  {
2248  fct_chk( enclen == outlen );
2249  }
2250  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2251  if( POLARSSL_MODE_CBC == cipher_info->mode )
2252  {
2253  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2254  }
2255  else
2256  {
2257  fct_chk( outlen == 0 );
2258  }
2259 
2260 
2261  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2262 
2263  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2264  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2265  FCT_TEST_END();
2266 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2267 
2268 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2269 
2270  FCT_TEST_BGN(null_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
2271  size_t first_length = 15;
2272  size_t second_length = 1;
2273  size_t length = first_length + second_length;
2274  unsigned char key[32];
2275  unsigned char iv[16];
2276 
2277  cipher_context_t ctx_dec;
2278  cipher_context_t ctx_enc;
2279  const cipher_info_t *cipher_info;
2280 
2281  unsigned char inbuf[64];
2282  unsigned char encbuf[64];
2283  unsigned char decbuf[64];
2284 
2285  size_t outlen = 0;
2286  size_t totaloutlen = 0;
2287  size_t enclen = 0;
2288 
2289  memset( key, 0, 32 );
2290  memset( iv , 0, 16 );
2291 
2292  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2293  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2294 
2295  memset( inbuf, 5, 64 );
2296  memset( encbuf, 0, 64 );
2297  memset( decbuf, 0, 64 );
2298 
2299  /* Initialise enc and dec contexts */
2301  fct_chk( NULL != cipher_info);
2302 
2303  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2304  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2305 
2306  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2307  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2308 
2309  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2310  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2311 
2312  if( POLARSSL_MODE_CBC == cipher_info->mode )
2313  {
2314  enclen = cipher_get_block_size(&ctx_enc )
2315  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2316  }
2317  else
2318  {
2319  enclen = length;
2320  }
2321 
2322  /* encode length number of bytes from inbuf */
2323  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2324  totaloutlen = outlen;
2325  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2326  totaloutlen += outlen;
2327  if( POLARSSL_MODE_CBC == cipher_info->mode )
2328  {
2329  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2330  }
2331  else
2332  {
2333  fct_chk( totaloutlen == enclen );
2334  }
2335  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2336  totaloutlen += outlen;
2337  if( POLARSSL_MODE_CBC == cipher_info->mode )
2338  {
2339  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2340  }
2341  else
2342  {
2343  fct_chk( outlen == 0 );
2344  }
2345 
2346  /* decode the previously encoded string */
2347  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2348  if( POLARSSL_MODE_CBC == cipher_info->mode )
2349  {
2350  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2351  }
2352  else
2353  {
2354  fct_chk( enclen == outlen );
2355  }
2356  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2357  if( POLARSSL_MODE_CBC == cipher_info->mode )
2358  {
2359  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2360  }
2361  else
2362  {
2363  fct_chk( outlen == 0 );
2364  }
2365 
2366 
2367  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2368 
2369  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2370  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2371  FCT_TEST_END();
2372 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2373 
2374 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2375 
2376  FCT_TEST_BGN(null_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2377  size_t first_length = 15;
2378  size_t second_length = 7;
2379  size_t length = first_length + second_length;
2380  unsigned char key[32];
2381  unsigned char iv[16];
2382 
2383  cipher_context_t ctx_dec;
2384  cipher_context_t ctx_enc;
2385  const cipher_info_t *cipher_info;
2386 
2387  unsigned char inbuf[64];
2388  unsigned char encbuf[64];
2389  unsigned char decbuf[64];
2390 
2391  size_t outlen = 0;
2392  size_t totaloutlen = 0;
2393  size_t enclen = 0;
2394 
2395  memset( key, 0, 32 );
2396  memset( iv , 0, 16 );
2397 
2398  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2399  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2400 
2401  memset( inbuf, 5, 64 );
2402  memset( encbuf, 0, 64 );
2403  memset( decbuf, 0, 64 );
2404 
2405  /* Initialise enc and dec contexts */
2407  fct_chk( NULL != cipher_info);
2408 
2409  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2410  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2411 
2412  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2413  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2414 
2415  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2416  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2417 
2418  if( POLARSSL_MODE_CBC == cipher_info->mode )
2419  {
2420  enclen = cipher_get_block_size(&ctx_enc )
2421  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2422  }
2423  else
2424  {
2425  enclen = length;
2426  }
2427 
2428  /* encode length number of bytes from inbuf */
2429  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2430  totaloutlen = outlen;
2431  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2432  totaloutlen += outlen;
2433  if( POLARSSL_MODE_CBC == cipher_info->mode )
2434  {
2435  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2436  }
2437  else
2438  {
2439  fct_chk( totaloutlen == enclen );
2440  }
2441  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2442  totaloutlen += outlen;
2443  if( POLARSSL_MODE_CBC == cipher_info->mode )
2444  {
2445  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2446  }
2447  else
2448  {
2449  fct_chk( outlen == 0 );
2450  }
2451 
2452  /* decode the previously encoded string */
2453  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2454  if( POLARSSL_MODE_CBC == cipher_info->mode )
2455  {
2456  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2457  }
2458  else
2459  {
2460  fct_chk( enclen == outlen );
2461  }
2462  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2463  if( POLARSSL_MODE_CBC == cipher_info->mode )
2464  {
2465  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2466  }
2467  else
2468  {
2469  fct_chk( outlen == 0 );
2470  }
2471 
2472 
2473  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2474 
2475  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2476  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2477  FCT_TEST_END();
2478 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2479 
2480 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2481 
2482  FCT_TEST_BGN(null_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2483  size_t first_length = 16;
2484  size_t second_length = 6;
2485  size_t length = first_length + second_length;
2486  unsigned char key[32];
2487  unsigned char iv[16];
2488 
2489  cipher_context_t ctx_dec;
2490  cipher_context_t ctx_enc;
2491  const cipher_info_t *cipher_info;
2492 
2493  unsigned char inbuf[64];
2494  unsigned char encbuf[64];
2495  unsigned char decbuf[64];
2496 
2497  size_t outlen = 0;
2498  size_t totaloutlen = 0;
2499  size_t enclen = 0;
2500 
2501  memset( key, 0, 32 );
2502  memset( iv , 0, 16 );
2503 
2504  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2505  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2506 
2507  memset( inbuf, 5, 64 );
2508  memset( encbuf, 0, 64 );
2509  memset( decbuf, 0, 64 );
2510 
2511  /* Initialise enc and dec contexts */
2513  fct_chk( NULL != cipher_info);
2514 
2515  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2516  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2517 
2518  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2519  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2520 
2521  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2522  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2523 
2524  if( POLARSSL_MODE_CBC == cipher_info->mode )
2525  {
2526  enclen = cipher_get_block_size(&ctx_enc )
2527  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2528  }
2529  else
2530  {
2531  enclen = length;
2532  }
2533 
2534  /* encode length number of bytes from inbuf */
2535  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2536  totaloutlen = outlen;
2537  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2538  totaloutlen += outlen;
2539  if( POLARSSL_MODE_CBC == cipher_info->mode )
2540  {
2541  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2542  }
2543  else
2544  {
2545  fct_chk( totaloutlen == enclen );
2546  }
2547  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2548  totaloutlen += outlen;
2549  if( POLARSSL_MODE_CBC == cipher_info->mode )
2550  {
2551  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2552  }
2553  else
2554  {
2555  fct_chk( outlen == 0 );
2556  }
2557 
2558  /* decode the previously encoded string */
2559  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2560  if( POLARSSL_MODE_CBC == cipher_info->mode )
2561  {
2562  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2563  }
2564  else
2565  {
2566  fct_chk( enclen == outlen );
2567  }
2568  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2569  if( POLARSSL_MODE_CBC == cipher_info->mode )
2570  {
2571  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2572  }
2573  else
2574  {
2575  fct_chk( outlen == 0 );
2576  }
2577 
2578 
2579  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2580 
2581  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2582  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2583  FCT_TEST_END();
2584 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2585 
2586 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2587 
2588  FCT_TEST_BGN(null_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2589  size_t first_length = 17;
2590  size_t second_length = 6;
2591  size_t length = first_length + second_length;
2592  unsigned char key[32];
2593  unsigned char iv[16];
2594 
2595  cipher_context_t ctx_dec;
2596  cipher_context_t ctx_enc;
2597  const cipher_info_t *cipher_info;
2598 
2599  unsigned char inbuf[64];
2600  unsigned char encbuf[64];
2601  unsigned char decbuf[64];
2602 
2603  size_t outlen = 0;
2604  size_t totaloutlen = 0;
2605  size_t enclen = 0;
2606 
2607  memset( key, 0, 32 );
2608  memset( iv , 0, 16 );
2609 
2610  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2611  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2612 
2613  memset( inbuf, 5, 64 );
2614  memset( encbuf, 0, 64 );
2615  memset( decbuf, 0, 64 );
2616 
2617  /* Initialise enc and dec contexts */
2619  fct_chk( NULL != cipher_info);
2620 
2621  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2622  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2623 
2624  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2625  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2626 
2627  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2628  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2629 
2630  if( POLARSSL_MODE_CBC == cipher_info->mode )
2631  {
2632  enclen = cipher_get_block_size(&ctx_enc )
2633  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2634  }
2635  else
2636  {
2637  enclen = length;
2638  }
2639 
2640  /* encode length number of bytes from inbuf */
2641  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2642  totaloutlen = outlen;
2643  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2644  totaloutlen += outlen;
2645  if( POLARSSL_MODE_CBC == cipher_info->mode )
2646  {
2647  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2648  }
2649  else
2650  {
2651  fct_chk( totaloutlen == enclen );
2652  }
2653  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2654  totaloutlen += outlen;
2655  if( POLARSSL_MODE_CBC == cipher_info->mode )
2656  {
2657  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2658  }
2659  else
2660  {
2661  fct_chk( outlen == 0 );
2662  }
2663 
2664  /* decode the previously encoded string */
2665  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2666  if( POLARSSL_MODE_CBC == cipher_info->mode )
2667  {
2668  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2669  }
2670  else
2671  {
2672  fct_chk( enclen == outlen );
2673  }
2674  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2675  if( POLARSSL_MODE_CBC == cipher_info->mode )
2676  {
2677  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2678  }
2679  else
2680  {
2681  fct_chk( outlen == 0 );
2682  }
2683 
2684 
2685  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2686 
2687  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2688  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2689  FCT_TEST_END();
2690 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2691 
2692 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2693 
2694  FCT_TEST_BGN(null_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
2695  size_t first_length = 16;
2696  size_t second_length = 16;
2697  size_t length = first_length + second_length;
2698  unsigned char key[32];
2699  unsigned char iv[16];
2700 
2701  cipher_context_t ctx_dec;
2702  cipher_context_t ctx_enc;
2703  const cipher_info_t *cipher_info;
2704 
2705  unsigned char inbuf[64];
2706  unsigned char encbuf[64];
2707  unsigned char decbuf[64];
2708 
2709  size_t outlen = 0;
2710  size_t totaloutlen = 0;
2711  size_t enclen = 0;
2712 
2713  memset( key, 0, 32 );
2714  memset( iv , 0, 16 );
2715 
2716  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2717  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2718 
2719  memset( inbuf, 5, 64 );
2720  memset( encbuf, 0, 64 );
2721  memset( decbuf, 0, 64 );
2722 
2723  /* Initialise enc and dec contexts */
2725  fct_chk( NULL != cipher_info);
2726 
2727  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2728  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2729 
2730  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2731  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2732 
2733  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2734  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2735 
2736  if( POLARSSL_MODE_CBC == cipher_info->mode )
2737  {
2738  enclen = cipher_get_block_size(&ctx_enc )
2739  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2740  }
2741  else
2742  {
2743  enclen = length;
2744  }
2745 
2746  /* encode length number of bytes from inbuf */
2747  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2748  totaloutlen = outlen;
2749  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2750  totaloutlen += outlen;
2751  if( POLARSSL_MODE_CBC == cipher_info->mode )
2752  {
2753  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2754  }
2755  else
2756  {
2757  fct_chk( totaloutlen == enclen );
2758  }
2759  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2760  totaloutlen += outlen;
2761  if( POLARSSL_MODE_CBC == cipher_info->mode )
2762  {
2763  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2764  }
2765  else
2766  {
2767  fct_chk( outlen == 0 );
2768  }
2769 
2770  /* decode the previously encoded string */
2771  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2772  if( POLARSSL_MODE_CBC == cipher_info->mode )
2773  {
2774  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2775  }
2776  else
2777  {
2778  fct_chk( enclen == outlen );
2779  }
2780  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2781  if( POLARSSL_MODE_CBC == cipher_info->mode )
2782  {
2783  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2784  }
2785  else
2786  {
2787  fct_chk( outlen == 0 );
2788  }
2789 
2790 
2791  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2792 
2793  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2794  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2795  FCT_TEST_END();
2796 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2797 
2798  }
2799  FCT_SUITE_END();
2800 
2801 #endif /* POLARSSL_CIPHER_C */
2802 
2803 }
2804 FCT_END();
2805