PolarSSL v1.2.5
test_suite_cipher.null.c
Go to the documentation of this file.
1 #include "fct.h"
2 
3 #include <polarssl/cipher.h>
4 
5 #include <polarssl/config.h>
6 
7 #ifdef _MSC_VER
8 #include <basetsd.h>
9 typedef UINT32 uint32_t;
10 #else
11 #include <inttypes.h>
12 #endif
13 
14 /*
15  * 32-bit integer manipulation macros (big endian)
16  */
17 #ifndef GET_UINT32_BE
18 #define GET_UINT32_BE(n,b,i) \
19 { \
20  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
21  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
22  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
23  | ( (uint32_t) (b)[(i) + 3] ); \
24 }
25 #endif
26 
27 #ifndef PUT_UINT32_BE
28 #define PUT_UINT32_BE(n,b,i) \
29 { \
30  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
31  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
32  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
33  (b)[(i) + 3] = (unsigned char) ( (n) ); \
34 }
35 #endif
36 
37 int unhexify(unsigned char *obuf, const char *ibuf)
38 {
39  unsigned char c, c2;
40  int len = strlen(ibuf) / 2;
41  assert(!(strlen(ibuf) %1)); // must be even number of bytes
42 
43  while (*ibuf != 0)
44  {
45  c = *ibuf++;
46  if( c >= '0' && c <= '9' )
47  c -= '0';
48  else if( c >= 'a' && c <= 'f' )
49  c -= 'a' - 10;
50  else if( c >= 'A' && c <= 'F' )
51  c -= 'A' - 10;
52  else
53  assert( 0 );
54 
55  c2 = *ibuf++;
56  if( c2 >= '0' && c2 <= '9' )
57  c2 -= '0';
58  else if( c2 >= 'a' && c2 <= 'f' )
59  c2 -= 'a' - 10;
60  else if( c2 >= 'A' && c2 <= 'F' )
61  c2 -= 'A' - 10;
62  else
63  assert( 0 );
64 
65  *obuf++ = ( c << 4 ) | c2;
66  }
67 
68  return len;
69 }
70 
71 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
72 {
73  unsigned char l, h;
74 
75  while (len != 0)
76  {
77  h = (*ibuf) / 16;
78  l = (*ibuf) % 16;
79 
80  if( h < 10 )
81  *obuf++ = '0' + h;
82  else
83  *obuf++ = 'a' + h - 10;
84 
85  if( l < 10 )
86  *obuf++ = '0' + l;
87  else
88  *obuf++ = 'a' + l - 10;
89 
90  ++ibuf;
91  len--;
92  }
93 }
94 
104 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
105 {
106  size_t i;
107 
108  if( rng_state != NULL )
109  rng_state = NULL;
110 
111  for( i = 0; i < len; ++i )
112  output[i] = rand();
113 
114  return( 0 );
115 }
116 
122 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
123 {
124  if( rng_state != NULL )
125  rng_state = NULL;
126 
127  memset( output, 0, len );
128 
129  return( 0 );
130 }
131 
132 typedef struct
133 {
134  unsigned char *buf;
135  size_t length;
136 } rnd_buf_info;
137 
149 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
150 {
151  rnd_buf_info *info = (rnd_buf_info *) rng_state;
152  size_t use_len;
153 
154  if( rng_state == NULL )
155  return( rnd_std_rand( NULL, output, len ) );
156 
157  use_len = len;
158  if( len > info->length )
159  use_len = info->length;
160 
161  if( use_len )
162  {
163  memcpy( output, info->buf, use_len );
164  info->buf += use_len;
165  info->length -= use_len;
166  }
167 
168  if( len - use_len > 0 )
169  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
170 
171  return( 0 );
172 }
173 
181 typedef struct
182 {
183  uint32_t key[16];
184  uint32_t v0, v1;
186 
195 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
196 {
197  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
198  uint32_t i, *k, sum, delta=0x9E3779B9;
199  unsigned char result[4];
200 
201  if( rng_state == NULL )
202  return( rnd_std_rand( NULL, output, len ) );
203 
204  k = info->key;
205 
206  while( len > 0 )
207  {
208  size_t use_len = ( len > 4 ) ? 4 : len;
209  sum = 0;
210 
211  for( i = 0; i < 32; i++ )
212  {
213  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
214  sum += delta;
215  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
216  }
217 
218  PUT_UINT32_BE( info->v0, result, 0 );
219  memcpy( output, result, use_len );
220  len -= use_len;
221  }
222 
223  return( 0 );
224 }
225 
226 
228 {
229 #ifdef POLARSSL_CIPHER_C
230 
231 
232  FCT_SUITE_BGN(test_suite_cipher)
233  {
234 #ifdef POLARSSL_SELF_TEST
235 
236  FCT_TEST_BGN(cipher_selftest)
237  {
238  fct_chk( cipher_self_test( 0 ) == 0 );
239  }
240  FCT_TEST_END();
241 #endif /* POLARSSL_SELF_TEST */
242 
243 
244  FCT_TEST_BGN(decrypt_empty_buffer)
245  unsigned char key[32];
246  unsigned char iv[16];
247 
248  cipher_context_t ctx_dec;
249  const cipher_info_t *cipher_info;
250 
251  unsigned char encbuf[64];
252  unsigned char decbuf[64];
253 
254  size_t outlen = 0;
255 
256  memset( key, 0, 32 );
257  memset( iv , 0, 16 );
258 
259  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
260 
261  memset( encbuf, 0, 64 );
262  memset( decbuf, 0, 64 );
263 
264  /* Initialise enc and dec contexts */
266  fct_chk( NULL != cipher_info);
267 
268  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
269 
270  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
271 
272  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
273 
274  /* decode 0-byte string */
275  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
276  fct_chk( 0 == outlen );
277  fct_chk( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
278  fct_chk( 0 == outlen );
279 
280  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
281  FCT_TEST_END();
282 
283 #ifdef POLARSSL_CIPHER_NULL_CIPHER
284 
285  FCT_TEST_BGN(null_encrypt_and_decrypt_0_bytes)
286  size_t length = 0;
287  unsigned char key[32];
288  unsigned char iv[16];
289 
290  const cipher_info_t *cipher_info;
291  cipher_context_t ctx_dec;
292  cipher_context_t ctx_enc;
293 
294  unsigned char inbuf[64];
295  unsigned char encbuf[64];
296  unsigned char decbuf[64];
297 
298  size_t outlen = 0;
299  size_t enclen = 0;
300 
301  memset( key, 0, 32 );
302  memset( iv , 0, 16 );
303 
304  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
305  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
306 
307  memset( inbuf, 5, 64 );
308  memset( encbuf, 0, 64 );
309  memset( decbuf, 0, 64 );
310 
311  /* Check and get info structures */
313  fct_chk( NULL != cipher_info );
314  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
315 
316  /* Initialise enc and dec contexts */
317  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
318  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
319 
320  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
321  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
322 
323  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
324  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
325 
326  if( POLARSSL_MODE_CBC == cipher_info->mode )
327  {
328  enclen = cipher_get_block_size( &ctx_enc )
329  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
330  }
331  else
332  {
333  enclen = length;
334  }
335 
336  /* encode length number of bytes from inbuf */
337  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
338  if( POLARSSL_MODE_CBC == cipher_info->mode )
339  {
340  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
341  }
342  else
343  {
344  fct_chk( outlen == enclen );
345  }
346 
347  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
348  if( POLARSSL_MODE_CBC == cipher_info->mode )
349  {
350  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
351  }
352  else
353  {
354  fct_chk( outlen == 0 );
355  }
356 
357 
358  /* decode the previously encoded string */
359  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
360  if( POLARSSL_MODE_CBC == cipher_info->mode )
361  {
362  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
363  }
364  else
365  {
366  fct_chk( enclen == outlen );
367  }
368 
369  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
370  if( POLARSSL_MODE_CBC == cipher_info->mode )
371  {
372  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
373  }
374  else
375  {
376  fct_chk( outlen == 0 );
377  }
378 
379 
380  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
381 
382  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
383  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
384  FCT_TEST_END();
385 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
386 
387 #ifdef POLARSSL_CIPHER_NULL_CIPHER
388 
389  FCT_TEST_BGN(null_encrypt_and_decrypt_1_bytes)
390  size_t length = 1;
391  unsigned char key[32];
392  unsigned char iv[16];
393 
394  const cipher_info_t *cipher_info;
395  cipher_context_t ctx_dec;
396  cipher_context_t ctx_enc;
397 
398  unsigned char inbuf[64];
399  unsigned char encbuf[64];
400  unsigned char decbuf[64];
401 
402  size_t outlen = 0;
403  size_t enclen = 0;
404 
405  memset( key, 0, 32 );
406  memset( iv , 0, 16 );
407 
408  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
409  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
410 
411  memset( inbuf, 5, 64 );
412  memset( encbuf, 0, 64 );
413  memset( decbuf, 0, 64 );
414 
415  /* Check and get info structures */
417  fct_chk( NULL != cipher_info );
418  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
419 
420  /* Initialise enc and dec contexts */
421  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
422  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
423 
424  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
425  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
426 
427  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
428  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
429 
430  if( POLARSSL_MODE_CBC == cipher_info->mode )
431  {
432  enclen = cipher_get_block_size( &ctx_enc )
433  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
434  }
435  else
436  {
437  enclen = length;
438  }
439 
440  /* encode length number of bytes from inbuf */
441  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
442  if( POLARSSL_MODE_CBC == cipher_info->mode )
443  {
444  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
445  }
446  else
447  {
448  fct_chk( outlen == enclen );
449  }
450 
451  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
452  if( POLARSSL_MODE_CBC == cipher_info->mode )
453  {
454  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
455  }
456  else
457  {
458  fct_chk( outlen == 0 );
459  }
460 
461 
462  /* decode the previously encoded string */
463  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
464  if( POLARSSL_MODE_CBC == cipher_info->mode )
465  {
466  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
467  }
468  else
469  {
470  fct_chk( enclen == outlen );
471  }
472 
473  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
474  if( POLARSSL_MODE_CBC == cipher_info->mode )
475  {
476  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
477  }
478  else
479  {
480  fct_chk( outlen == 0 );
481  }
482 
483 
484  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
485 
486  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
487  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
488  FCT_TEST_END();
489 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
490 
491 #ifdef POLARSSL_CIPHER_NULL_CIPHER
492 
493  FCT_TEST_BGN(null_encrypt_and_decrypt_2_bytes)
494  size_t length = 2;
495  unsigned char key[32];
496  unsigned char iv[16];
497 
498  const cipher_info_t *cipher_info;
499  cipher_context_t ctx_dec;
500  cipher_context_t ctx_enc;
501 
502  unsigned char inbuf[64];
503  unsigned char encbuf[64];
504  unsigned char decbuf[64];
505 
506  size_t outlen = 0;
507  size_t enclen = 0;
508 
509  memset( key, 0, 32 );
510  memset( iv , 0, 16 );
511 
512  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
513  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
514 
515  memset( inbuf, 5, 64 );
516  memset( encbuf, 0, 64 );
517  memset( decbuf, 0, 64 );
518 
519  /* Check and get info structures */
521  fct_chk( NULL != cipher_info );
522  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
523 
524  /* Initialise enc and dec contexts */
525  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
526  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
527 
528  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
529  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
530 
531  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
532  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
533 
534  if( POLARSSL_MODE_CBC == cipher_info->mode )
535  {
536  enclen = cipher_get_block_size( &ctx_enc )
537  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
538  }
539  else
540  {
541  enclen = length;
542  }
543 
544  /* encode length number of bytes from inbuf */
545  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
546  if( POLARSSL_MODE_CBC == cipher_info->mode )
547  {
548  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
549  }
550  else
551  {
552  fct_chk( outlen == enclen );
553  }
554 
555  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
556  if( POLARSSL_MODE_CBC == cipher_info->mode )
557  {
558  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
559  }
560  else
561  {
562  fct_chk( outlen == 0 );
563  }
564 
565 
566  /* decode the previously encoded string */
567  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
568  if( POLARSSL_MODE_CBC == cipher_info->mode )
569  {
570  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
571  }
572  else
573  {
574  fct_chk( enclen == outlen );
575  }
576 
577  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
578  if( POLARSSL_MODE_CBC == cipher_info->mode )
579  {
580  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
581  }
582  else
583  {
584  fct_chk( outlen == 0 );
585  }
586 
587 
588  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
589 
590  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
591  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
592  FCT_TEST_END();
593 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
594 
595 #ifdef POLARSSL_CIPHER_NULL_CIPHER
596 
597  FCT_TEST_BGN(null_encrypt_and_decrypt_7_bytes)
598  size_t length = 7;
599  unsigned char key[32];
600  unsigned char iv[16];
601 
602  const cipher_info_t *cipher_info;
603  cipher_context_t ctx_dec;
604  cipher_context_t ctx_enc;
605 
606  unsigned char inbuf[64];
607  unsigned char encbuf[64];
608  unsigned char decbuf[64];
609 
610  size_t outlen = 0;
611  size_t enclen = 0;
612 
613  memset( key, 0, 32 );
614  memset( iv , 0, 16 );
615 
616  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
617  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
618 
619  memset( inbuf, 5, 64 );
620  memset( encbuf, 0, 64 );
621  memset( decbuf, 0, 64 );
622 
623  /* Check and get info structures */
625  fct_chk( NULL != cipher_info );
626  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
627 
628  /* Initialise enc and dec contexts */
629  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
630  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
631 
632  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
633  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
634 
635  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
636  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
637 
638  if( POLARSSL_MODE_CBC == cipher_info->mode )
639  {
640  enclen = cipher_get_block_size( &ctx_enc )
641  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
642  }
643  else
644  {
645  enclen = length;
646  }
647 
648  /* encode length number of bytes from inbuf */
649  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
650  if( POLARSSL_MODE_CBC == cipher_info->mode )
651  {
652  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
653  }
654  else
655  {
656  fct_chk( outlen == enclen );
657  }
658 
659  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
660  if( POLARSSL_MODE_CBC == cipher_info->mode )
661  {
662  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
663  }
664  else
665  {
666  fct_chk( outlen == 0 );
667  }
668 
669 
670  /* decode the previously encoded string */
671  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
672  if( POLARSSL_MODE_CBC == cipher_info->mode )
673  {
674  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
675  }
676  else
677  {
678  fct_chk( enclen == outlen );
679  }
680 
681  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
682  if( POLARSSL_MODE_CBC == cipher_info->mode )
683  {
684  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
685  }
686  else
687  {
688  fct_chk( outlen == 0 );
689  }
690 
691 
692  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
693 
694  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
695  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
696  FCT_TEST_END();
697 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
698 
699 #ifdef POLARSSL_CIPHER_NULL_CIPHER
700 
701  FCT_TEST_BGN(null_encrypt_and_decrypt_8_bytes)
702  size_t length = 8;
703  unsigned char key[32];
704  unsigned char iv[16];
705 
706  const cipher_info_t *cipher_info;
707  cipher_context_t ctx_dec;
708  cipher_context_t ctx_enc;
709 
710  unsigned char inbuf[64];
711  unsigned char encbuf[64];
712  unsigned char decbuf[64];
713 
714  size_t outlen = 0;
715  size_t enclen = 0;
716 
717  memset( key, 0, 32 );
718  memset( iv , 0, 16 );
719 
720  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
721  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
722 
723  memset( inbuf, 5, 64 );
724  memset( encbuf, 0, 64 );
725  memset( decbuf, 0, 64 );
726 
727  /* Check and get info structures */
729  fct_chk( NULL != cipher_info );
730  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
731 
732  /* Initialise enc and dec contexts */
733  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
734  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
735 
736  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
737  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
738 
739  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
740  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
741 
742  if( POLARSSL_MODE_CBC == cipher_info->mode )
743  {
744  enclen = cipher_get_block_size( &ctx_enc )
745  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
746  }
747  else
748  {
749  enclen = length;
750  }
751 
752  /* encode length number of bytes from inbuf */
753  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
754  if( POLARSSL_MODE_CBC == cipher_info->mode )
755  {
756  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
757  }
758  else
759  {
760  fct_chk( outlen == enclen );
761  }
762 
763  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
764  if( POLARSSL_MODE_CBC == cipher_info->mode )
765  {
766  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
767  }
768  else
769  {
770  fct_chk( outlen == 0 );
771  }
772 
773 
774  /* decode the previously encoded string */
775  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
776  if( POLARSSL_MODE_CBC == cipher_info->mode )
777  {
778  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
779  }
780  else
781  {
782  fct_chk( enclen == outlen );
783  }
784 
785  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
786  if( POLARSSL_MODE_CBC == cipher_info->mode )
787  {
788  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
789  }
790  else
791  {
792  fct_chk( outlen == 0 );
793  }
794 
795 
796  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
797 
798  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
799  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
800  FCT_TEST_END();
801 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
802 
803 #ifdef POLARSSL_CIPHER_NULL_CIPHER
804 
805  FCT_TEST_BGN(null_encrypt_and_decrypt_9_bytes)
806  size_t length = 9;
807  unsigned char key[32];
808  unsigned char iv[16];
809 
810  const cipher_info_t *cipher_info;
811  cipher_context_t ctx_dec;
812  cipher_context_t ctx_enc;
813 
814  unsigned char inbuf[64];
815  unsigned char encbuf[64];
816  unsigned char decbuf[64];
817 
818  size_t outlen = 0;
819  size_t enclen = 0;
820 
821  memset( key, 0, 32 );
822  memset( iv , 0, 16 );
823 
824  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
825  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
826 
827  memset( inbuf, 5, 64 );
828  memset( encbuf, 0, 64 );
829  memset( decbuf, 0, 64 );
830 
831  /* Check and get info structures */
833  fct_chk( NULL != cipher_info );
834  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
835 
836  /* Initialise enc and dec contexts */
837  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
838  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
839 
840  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
841  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
842 
843  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
844  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
845 
846  if( POLARSSL_MODE_CBC == cipher_info->mode )
847  {
848  enclen = cipher_get_block_size( &ctx_enc )
849  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
850  }
851  else
852  {
853  enclen = length;
854  }
855 
856  /* encode length number of bytes from inbuf */
857  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
858  if( POLARSSL_MODE_CBC == cipher_info->mode )
859  {
860  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
861  }
862  else
863  {
864  fct_chk( outlen == enclen );
865  }
866 
867  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
868  if( POLARSSL_MODE_CBC == cipher_info->mode )
869  {
870  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
871  }
872  else
873  {
874  fct_chk( outlen == 0 );
875  }
876 
877 
878  /* decode the previously encoded string */
879  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
880  if( POLARSSL_MODE_CBC == cipher_info->mode )
881  {
882  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
883  }
884  else
885  {
886  fct_chk( enclen == outlen );
887  }
888 
889  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
890  if( POLARSSL_MODE_CBC == cipher_info->mode )
891  {
892  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
893  }
894  else
895  {
896  fct_chk( outlen == 0 );
897  }
898 
899 
900  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
901 
902  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
903  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
904  FCT_TEST_END();
905 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
906 
907 #ifdef POLARSSL_CIPHER_NULL_CIPHER
908 
909  FCT_TEST_BGN(null_encrypt_and_decrypt_15_bytes)
910  size_t length = 15;
911  unsigned char key[32];
912  unsigned char iv[16];
913 
914  const cipher_info_t *cipher_info;
915  cipher_context_t ctx_dec;
916  cipher_context_t ctx_enc;
917 
918  unsigned char inbuf[64];
919  unsigned char encbuf[64];
920  unsigned char decbuf[64];
921 
922  size_t outlen = 0;
923  size_t enclen = 0;
924 
925  memset( key, 0, 32 );
926  memset( iv , 0, 16 );
927 
928  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
929  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
930 
931  memset( inbuf, 5, 64 );
932  memset( encbuf, 0, 64 );
933  memset( decbuf, 0, 64 );
934 
935  /* Check and get info structures */
937  fct_chk( NULL != cipher_info );
938  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
939 
940  /* Initialise enc and dec contexts */
941  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
942  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
943 
944  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
945  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
946 
947  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
948  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
949 
950  if( POLARSSL_MODE_CBC == cipher_info->mode )
951  {
952  enclen = cipher_get_block_size( &ctx_enc )
953  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
954  }
955  else
956  {
957  enclen = length;
958  }
959 
960  /* encode length number of bytes from inbuf */
961  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
962  if( POLARSSL_MODE_CBC == cipher_info->mode )
963  {
964  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
965  }
966  else
967  {
968  fct_chk( outlen == enclen );
969  }
970 
971  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
972  if( POLARSSL_MODE_CBC == cipher_info->mode )
973  {
974  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
975  }
976  else
977  {
978  fct_chk( outlen == 0 );
979  }
980 
981 
982  /* decode the previously encoded string */
983  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
984  if( POLARSSL_MODE_CBC == cipher_info->mode )
985  {
986  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
987  }
988  else
989  {
990  fct_chk( enclen == outlen );
991  }
992 
993  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
994  if( POLARSSL_MODE_CBC == cipher_info->mode )
995  {
996  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
997  }
998  else
999  {
1000  fct_chk( outlen == 0 );
1001  }
1002 
1003 
1004  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1005 
1006  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1007  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1008  FCT_TEST_END();
1009 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1010 
1011 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1012 
1013  FCT_TEST_BGN(null_encrypt_and_decrypt_16_bytes)
1014  size_t length = 16;
1015  unsigned char key[32];
1016  unsigned char iv[16];
1017 
1018  const cipher_info_t *cipher_info;
1019  cipher_context_t ctx_dec;
1020  cipher_context_t ctx_enc;
1021 
1022  unsigned char inbuf[64];
1023  unsigned char encbuf[64];
1024  unsigned char decbuf[64];
1025 
1026  size_t outlen = 0;
1027  size_t enclen = 0;
1028 
1029  memset( key, 0, 32 );
1030  memset( iv , 0, 16 );
1031 
1032  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1033  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1034 
1035  memset( inbuf, 5, 64 );
1036  memset( encbuf, 0, 64 );
1037  memset( decbuf, 0, 64 );
1038 
1039  /* Check and get info structures */
1041  fct_chk( NULL != cipher_info );
1042  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
1043 
1044  /* Initialise enc and dec contexts */
1045  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1046  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1047 
1048  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1049  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1050 
1051  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1052  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1053 
1054  if( POLARSSL_MODE_CBC == cipher_info->mode )
1055  {
1056  enclen = cipher_get_block_size( &ctx_enc )
1057  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1058  }
1059  else
1060  {
1061  enclen = length;
1062  }
1063 
1064  /* encode length number of bytes from inbuf */
1065  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1066  if( POLARSSL_MODE_CBC == cipher_info->mode )
1067  {
1068  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1069  }
1070  else
1071  {
1072  fct_chk( outlen == enclen );
1073  }
1074 
1075  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1076  if( POLARSSL_MODE_CBC == cipher_info->mode )
1077  {
1078  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1079  }
1080  else
1081  {
1082  fct_chk( outlen == 0 );
1083  }
1084 
1085 
1086  /* decode the previously encoded string */
1087  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1088  if( POLARSSL_MODE_CBC == cipher_info->mode )
1089  {
1090  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1091  }
1092  else
1093  {
1094  fct_chk( enclen == outlen );
1095  }
1096 
1097  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1098  if( POLARSSL_MODE_CBC == cipher_info->mode )
1099  {
1100  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1101  }
1102  else
1103  {
1104  fct_chk( outlen == 0 );
1105  }
1106 
1107 
1108  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1109 
1110  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1111  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1112  FCT_TEST_END();
1113 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1114 
1115 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1116 
1117  FCT_TEST_BGN(null_encrypt_and_decrypt_31_bytes)
1118  size_t length = 31;
1119  unsigned char key[32];
1120  unsigned char iv[16];
1121 
1122  const cipher_info_t *cipher_info;
1123  cipher_context_t ctx_dec;
1124  cipher_context_t ctx_enc;
1125 
1126  unsigned char inbuf[64];
1127  unsigned char encbuf[64];
1128  unsigned char decbuf[64];
1129 
1130  size_t outlen = 0;
1131  size_t enclen = 0;
1132 
1133  memset( key, 0, 32 );
1134  memset( iv , 0, 16 );
1135 
1136  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1137  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1138 
1139  memset( inbuf, 5, 64 );
1140  memset( encbuf, 0, 64 );
1141  memset( decbuf, 0, 64 );
1142 
1143  /* Check and get info structures */
1145  fct_chk( NULL != cipher_info );
1146  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
1147 
1148  /* Initialise enc and dec contexts */
1149  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1150  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1151 
1152  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1153  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1154 
1155  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1156  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1157 
1158  if( POLARSSL_MODE_CBC == cipher_info->mode )
1159  {
1160  enclen = cipher_get_block_size( &ctx_enc )
1161  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1162  }
1163  else
1164  {
1165  enclen = length;
1166  }
1167 
1168  /* encode length number of bytes from inbuf */
1169  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1170  if( POLARSSL_MODE_CBC == cipher_info->mode )
1171  {
1172  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1173  }
1174  else
1175  {
1176  fct_chk( outlen == enclen );
1177  }
1178 
1179  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1180  if( POLARSSL_MODE_CBC == cipher_info->mode )
1181  {
1182  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1183  }
1184  else
1185  {
1186  fct_chk( outlen == 0 );
1187  }
1188 
1189 
1190  /* decode the previously encoded string */
1191  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1192  if( POLARSSL_MODE_CBC == cipher_info->mode )
1193  {
1194  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1195  }
1196  else
1197  {
1198  fct_chk( enclen == outlen );
1199  }
1200 
1201  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1202  if( POLARSSL_MODE_CBC == cipher_info->mode )
1203  {
1204  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1205  }
1206  else
1207  {
1208  fct_chk( outlen == 0 );
1209  }
1210 
1211 
1212  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1213 
1214  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1215  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1216  FCT_TEST_END();
1217 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1218 
1219 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1220 
1221  FCT_TEST_BGN(null_encrypt_and_decrypt_32_bytes)
1222  size_t length = 32;
1223  unsigned char key[32];
1224  unsigned char iv[16];
1225 
1226  const cipher_info_t *cipher_info;
1227  cipher_context_t ctx_dec;
1228  cipher_context_t ctx_enc;
1229 
1230  unsigned char inbuf[64];
1231  unsigned char encbuf[64];
1232  unsigned char decbuf[64];
1233 
1234  size_t outlen = 0;
1235  size_t enclen = 0;
1236 
1237  memset( key, 0, 32 );
1238  memset( iv , 0, 16 );
1239 
1240  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1241  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1242 
1243  memset( inbuf, 5, 64 );
1244  memset( encbuf, 0, 64 );
1245  memset( decbuf, 0, 64 );
1246 
1247  /* Check and get info structures */
1249  fct_chk( NULL != cipher_info );
1250  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
1251 
1252  /* Initialise enc and dec contexts */
1253  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1254  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1255 
1256  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1257  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1258 
1259  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1260  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1261 
1262  if( POLARSSL_MODE_CBC == cipher_info->mode )
1263  {
1264  enclen = cipher_get_block_size( &ctx_enc )
1265  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1266  }
1267  else
1268  {
1269  enclen = length;
1270  }
1271 
1272  /* encode length number of bytes from inbuf */
1273  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1274  if( POLARSSL_MODE_CBC == cipher_info->mode )
1275  {
1276  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1277  }
1278  else
1279  {
1280  fct_chk( outlen == enclen );
1281  }
1282 
1283  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1284  if( POLARSSL_MODE_CBC == cipher_info->mode )
1285  {
1286  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1287  }
1288  else
1289  {
1290  fct_chk( outlen == 0 );
1291  }
1292 
1293 
1294  /* decode the previously encoded string */
1295  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1296  if( POLARSSL_MODE_CBC == cipher_info->mode )
1297  {
1298  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1299  }
1300  else
1301  {
1302  fct_chk( enclen == outlen );
1303  }
1304 
1305  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1306  if( POLARSSL_MODE_CBC == cipher_info->mode )
1307  {
1308  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1309  }
1310  else
1311  {
1312  fct_chk( outlen == 0 );
1313  }
1314 
1315 
1316  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1317 
1318  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1319  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1320  FCT_TEST_END();
1321 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1322 
1323 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1324 
1325  FCT_TEST_BGN(null_encrypt_and_decrypt_33_bytes)
1326  size_t length = 33;
1327  unsigned char key[32];
1328  unsigned char iv[16];
1329 
1330  const cipher_info_t *cipher_info;
1331  cipher_context_t ctx_dec;
1332  cipher_context_t ctx_enc;
1333 
1334  unsigned char inbuf[64];
1335  unsigned char encbuf[64];
1336  unsigned char decbuf[64];
1337 
1338  size_t outlen = 0;
1339  size_t enclen = 0;
1340 
1341  memset( key, 0, 32 );
1342  memset( iv , 0, 16 );
1343 
1344  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1345  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1346 
1347  memset( inbuf, 5, 64 );
1348  memset( encbuf, 0, 64 );
1349  memset( decbuf, 0, 64 );
1350 
1351  /* Check and get info structures */
1353  fct_chk( NULL != cipher_info );
1354  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
1355 
1356  /* Initialise enc and dec contexts */
1357  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1358  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1359 
1360  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1361  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1362 
1363  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1364  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1365 
1366  if( POLARSSL_MODE_CBC == cipher_info->mode )
1367  {
1368  enclen = cipher_get_block_size( &ctx_enc )
1369  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1370  }
1371  else
1372  {
1373  enclen = length;
1374  }
1375 
1376  /* encode length number of bytes from inbuf */
1377  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1378  if( POLARSSL_MODE_CBC == cipher_info->mode )
1379  {
1380  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1381  }
1382  else
1383  {
1384  fct_chk( outlen == enclen );
1385  }
1386 
1387  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1388  if( POLARSSL_MODE_CBC == cipher_info->mode )
1389  {
1390  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1391  }
1392  else
1393  {
1394  fct_chk( outlen == 0 );
1395  }
1396 
1397 
1398  /* decode the previously encoded string */
1399  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1400  if( POLARSSL_MODE_CBC == cipher_info->mode )
1401  {
1402  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1403  }
1404  else
1405  {
1406  fct_chk( enclen == outlen );
1407  }
1408 
1409  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1410  if( POLARSSL_MODE_CBC == cipher_info->mode )
1411  {
1412  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1413  }
1414  else
1415  {
1416  fct_chk( outlen == 0 );
1417  }
1418 
1419 
1420  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1421 
1422  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1423  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1424  FCT_TEST_END();
1425 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1426 
1427 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1428 
1429  FCT_TEST_BGN(null_encrypt_and_decrypt_47_bytes)
1430  size_t length = 47;
1431  unsigned char key[32];
1432  unsigned char iv[16];
1433 
1434  const cipher_info_t *cipher_info;
1435  cipher_context_t ctx_dec;
1436  cipher_context_t ctx_enc;
1437 
1438  unsigned char inbuf[64];
1439  unsigned char encbuf[64];
1440  unsigned char decbuf[64];
1441 
1442  size_t outlen = 0;
1443  size_t enclen = 0;
1444 
1445  memset( key, 0, 32 );
1446  memset( iv , 0, 16 );
1447 
1448  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1449  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1450 
1451  memset( inbuf, 5, 64 );
1452  memset( encbuf, 0, 64 );
1453  memset( decbuf, 0, 64 );
1454 
1455  /* Check and get info structures */
1457  fct_chk( NULL != cipher_info );
1458  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
1459 
1460  /* Initialise enc and dec contexts */
1461  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1462  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1463 
1464  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1465  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1466 
1467  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1468  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1469 
1470  if( POLARSSL_MODE_CBC == cipher_info->mode )
1471  {
1472  enclen = cipher_get_block_size( &ctx_enc )
1473  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1474  }
1475  else
1476  {
1477  enclen = length;
1478  }
1479 
1480  /* encode length number of bytes from inbuf */
1481  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1482  if( POLARSSL_MODE_CBC == cipher_info->mode )
1483  {
1484  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1485  }
1486  else
1487  {
1488  fct_chk( outlen == enclen );
1489  }
1490 
1491  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1492  if( POLARSSL_MODE_CBC == cipher_info->mode )
1493  {
1494  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1495  }
1496  else
1497  {
1498  fct_chk( outlen == 0 );
1499  }
1500 
1501 
1502  /* decode the previously encoded string */
1503  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1504  if( POLARSSL_MODE_CBC == cipher_info->mode )
1505  {
1506  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1507  }
1508  else
1509  {
1510  fct_chk( enclen == outlen );
1511  }
1512 
1513  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1514  if( POLARSSL_MODE_CBC == cipher_info->mode )
1515  {
1516  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1517  }
1518  else
1519  {
1520  fct_chk( outlen == 0 );
1521  }
1522 
1523 
1524  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1525 
1526  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1527  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1528  FCT_TEST_END();
1529 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1530 
1531 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1532 
1533  FCT_TEST_BGN(null_encrypt_and_decrypt_48_bytes)
1534  size_t length = 48;
1535  unsigned char key[32];
1536  unsigned char iv[16];
1537 
1538  const cipher_info_t *cipher_info;
1539  cipher_context_t ctx_dec;
1540  cipher_context_t ctx_enc;
1541 
1542  unsigned char inbuf[64];
1543  unsigned char encbuf[64];
1544  unsigned char decbuf[64];
1545 
1546  size_t outlen = 0;
1547  size_t enclen = 0;
1548 
1549  memset( key, 0, 32 );
1550  memset( iv , 0, 16 );
1551 
1552  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1553  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1554 
1555  memset( inbuf, 5, 64 );
1556  memset( encbuf, 0, 64 );
1557  memset( decbuf, 0, 64 );
1558 
1559  /* Check and get info structures */
1561  fct_chk( NULL != cipher_info );
1562  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
1563 
1564  /* Initialise enc and dec contexts */
1565  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1566  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1567 
1568  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1569  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1570 
1571  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1572  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1573 
1574  if( POLARSSL_MODE_CBC == cipher_info->mode )
1575  {
1576  enclen = cipher_get_block_size( &ctx_enc )
1577  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1578  }
1579  else
1580  {
1581  enclen = length;
1582  }
1583 
1584  /* encode length number of bytes from inbuf */
1585  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1586  if( POLARSSL_MODE_CBC == cipher_info->mode )
1587  {
1588  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1589  }
1590  else
1591  {
1592  fct_chk( outlen == enclen );
1593  }
1594 
1595  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1596  if( POLARSSL_MODE_CBC == cipher_info->mode )
1597  {
1598  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1599  }
1600  else
1601  {
1602  fct_chk( outlen == 0 );
1603  }
1604 
1605 
1606  /* decode the previously encoded string */
1607  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1608  if( POLARSSL_MODE_CBC == cipher_info->mode )
1609  {
1610  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1611  }
1612  else
1613  {
1614  fct_chk( enclen == outlen );
1615  }
1616 
1617  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1618  if( POLARSSL_MODE_CBC == cipher_info->mode )
1619  {
1620  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1621  }
1622  else
1623  {
1624  fct_chk( outlen == 0 );
1625  }
1626 
1627 
1628  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1629 
1630  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1631  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1632  FCT_TEST_END();
1633 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1634 
1635 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1636 
1637  FCT_TEST_BGN(null_encrypt_and_decrypt_49_bytes)
1638  size_t length = 49;
1639  unsigned char key[32];
1640  unsigned char iv[16];
1641 
1642  const cipher_info_t *cipher_info;
1643  cipher_context_t ctx_dec;
1644  cipher_context_t ctx_enc;
1645 
1646  unsigned char inbuf[64];
1647  unsigned char encbuf[64];
1648  unsigned char decbuf[64];
1649 
1650  size_t outlen = 0;
1651  size_t enclen = 0;
1652 
1653  memset( key, 0, 32 );
1654  memset( iv , 0, 16 );
1655 
1656  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1657  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1658 
1659  memset( inbuf, 5, 64 );
1660  memset( encbuf, 0, 64 );
1661  memset( decbuf, 0, 64 );
1662 
1663  /* Check and get info structures */
1665  fct_chk( NULL != cipher_info );
1666  fct_chk( cipher_info_from_string( "NULL" ) == cipher_info );
1667 
1668  /* Initialise enc and dec contexts */
1669  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1670  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1671 
1672  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1673  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1674 
1675  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1676  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1677 
1678  if( POLARSSL_MODE_CBC == cipher_info->mode )
1679  {
1680  enclen = cipher_get_block_size( &ctx_enc )
1681  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1682  }
1683  else
1684  {
1685  enclen = length;
1686  }
1687 
1688  /* encode length number of bytes from inbuf */
1689  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
1690  if( POLARSSL_MODE_CBC == cipher_info->mode )
1691  {
1692  fct_chk( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1693  }
1694  else
1695  {
1696  fct_chk( outlen == enclen );
1697  }
1698 
1699  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
1700  if( POLARSSL_MODE_CBC == cipher_info->mode )
1701  {
1702  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1703  }
1704  else
1705  {
1706  fct_chk( outlen == 0 );
1707  }
1708 
1709 
1710  /* decode the previously encoded string */
1711  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1712  if( POLARSSL_MODE_CBC == cipher_info->mode )
1713  {
1714  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1715  }
1716  else
1717  {
1718  fct_chk( enclen == outlen );
1719  }
1720 
1721  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1722  if( POLARSSL_MODE_CBC == cipher_info->mode )
1723  {
1724  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1725  }
1726  else
1727  {
1728  fct_chk( outlen == 0 );
1729  }
1730 
1731 
1732  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1733 
1734  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1735  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1736  FCT_TEST_END();
1737 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1738 
1739 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1740 
1741  FCT_TEST_BGN(null_encrypt_and_decrypt_1_bytes_in_multiple_parts_1)
1742  size_t first_length = 1;
1743  size_t second_length = 0;
1744  size_t length = first_length + second_length;
1745  unsigned char key[32];
1746  unsigned char iv[16];
1747 
1748  cipher_context_t ctx_dec;
1749  cipher_context_t ctx_enc;
1750  const cipher_info_t *cipher_info;
1751 
1752  unsigned char inbuf[64];
1753  unsigned char encbuf[64];
1754  unsigned char decbuf[64];
1755 
1756  size_t outlen = 0;
1757  size_t totaloutlen = 0;
1758  size_t enclen = 0;
1759 
1760  memset( key, 0, 32 );
1761  memset( iv , 0, 16 );
1762 
1763  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1764  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1765 
1766  memset( inbuf, 5, 64 );
1767  memset( encbuf, 0, 64 );
1768  memset( decbuf, 0, 64 );
1769 
1770  /* Initialise enc and dec contexts */
1772  fct_chk( NULL != cipher_info);
1773 
1774  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1775  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1776 
1777  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1778  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1779 
1780  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1781  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1782 
1783  if( POLARSSL_MODE_CBC == cipher_info->mode )
1784  {
1785  enclen = cipher_get_block_size(&ctx_enc )
1786  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1787  }
1788  else
1789  {
1790  enclen = length;
1791  }
1792 
1793  /* encode length number of bytes from inbuf */
1794  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
1795  totaloutlen = outlen;
1796  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
1797  totaloutlen += outlen;
1798  if( POLARSSL_MODE_CBC == cipher_info->mode )
1799  {
1800  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1801  }
1802  else
1803  {
1804  fct_chk( totaloutlen == enclen );
1805  }
1806  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
1807  totaloutlen += outlen;
1808  if( POLARSSL_MODE_CBC == cipher_info->mode )
1809  {
1810  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1811  }
1812  else
1813  {
1814  fct_chk( outlen == 0 );
1815  }
1816 
1817  /* decode the previously encoded string */
1818  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1819  if( POLARSSL_MODE_CBC == cipher_info->mode )
1820  {
1821  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1822  }
1823  else
1824  {
1825  fct_chk( enclen == outlen );
1826  }
1827  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1828  if( POLARSSL_MODE_CBC == cipher_info->mode )
1829  {
1830  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1831  }
1832  else
1833  {
1834  fct_chk( outlen == 0 );
1835  }
1836 
1837 
1838  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1839 
1840  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1841  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1842  FCT_TEST_END();
1843 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1844 
1845 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1846 
1847  FCT_TEST_BGN(null_encrypt_and_decrypt_1_bytes_in_multiple_parts_2)
1848  size_t first_length = 0;
1849  size_t second_length = 1;
1850  size_t length = first_length + second_length;
1851  unsigned char key[32];
1852  unsigned char iv[16];
1853 
1854  cipher_context_t ctx_dec;
1855  cipher_context_t ctx_enc;
1856  const cipher_info_t *cipher_info;
1857 
1858  unsigned char inbuf[64];
1859  unsigned char encbuf[64];
1860  unsigned char decbuf[64];
1861 
1862  size_t outlen = 0;
1863  size_t totaloutlen = 0;
1864  size_t enclen = 0;
1865 
1866  memset( key, 0, 32 );
1867  memset( iv , 0, 16 );
1868 
1869  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1870  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1871 
1872  memset( inbuf, 5, 64 );
1873  memset( encbuf, 0, 64 );
1874  memset( decbuf, 0, 64 );
1875 
1876  /* Initialise enc and dec contexts */
1878  fct_chk( NULL != cipher_info);
1879 
1880  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1881  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1882 
1883  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1884  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1885 
1886  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1887  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1888 
1889  if( POLARSSL_MODE_CBC == cipher_info->mode )
1890  {
1891  enclen = cipher_get_block_size(&ctx_enc )
1892  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1893  }
1894  else
1895  {
1896  enclen = length;
1897  }
1898 
1899  /* encode length number of bytes from inbuf */
1900  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
1901  totaloutlen = outlen;
1902  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
1903  totaloutlen += outlen;
1904  if( POLARSSL_MODE_CBC == cipher_info->mode )
1905  {
1906  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
1907  }
1908  else
1909  {
1910  fct_chk( totaloutlen == enclen );
1911  }
1912  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
1913  totaloutlen += outlen;
1914  if( POLARSSL_MODE_CBC == cipher_info->mode )
1915  {
1916  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
1917  }
1918  else
1919  {
1920  fct_chk( outlen == 0 );
1921  }
1922 
1923  /* decode the previously encoded string */
1924  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
1925  if( POLARSSL_MODE_CBC == cipher_info->mode )
1926  {
1927  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
1928  }
1929  else
1930  {
1931  fct_chk( enclen == outlen );
1932  }
1933  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
1934  if( POLARSSL_MODE_CBC == cipher_info->mode )
1935  {
1936  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
1937  }
1938  else
1939  {
1940  fct_chk( outlen == 0 );
1941  }
1942 
1943 
1944  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
1945 
1946  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
1947  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
1948  FCT_TEST_END();
1949 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1950 
1951 #ifdef POLARSSL_CIPHER_NULL_CIPHER
1952 
1953  FCT_TEST_BGN(null_encrypt_and_decrypt_16_bytes_in_multiple_parts_1)
1954  size_t first_length = 16;
1955  size_t second_length = 0;
1956  size_t length = first_length + second_length;
1957  unsigned char key[32];
1958  unsigned char iv[16];
1959 
1960  cipher_context_t ctx_dec;
1961  cipher_context_t ctx_enc;
1962  const cipher_info_t *cipher_info;
1963 
1964  unsigned char inbuf[64];
1965  unsigned char encbuf[64];
1966  unsigned char decbuf[64];
1967 
1968  size_t outlen = 0;
1969  size_t totaloutlen = 0;
1970  size_t enclen = 0;
1971 
1972  memset( key, 0, 32 );
1973  memset( iv , 0, 16 );
1974 
1975  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
1976  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
1977 
1978  memset( inbuf, 5, 64 );
1979  memset( encbuf, 0, 64 );
1980  memset( decbuf, 0, 64 );
1981 
1982  /* Initialise enc and dec contexts */
1984  fct_chk( NULL != cipher_info);
1985 
1986  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
1987  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
1988 
1989  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
1990  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
1991 
1992  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
1993  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
1994 
1995  if( POLARSSL_MODE_CBC == cipher_info->mode )
1996  {
1997  enclen = cipher_get_block_size(&ctx_enc )
1998  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
1999  }
2000  else
2001  {
2002  enclen = length;
2003  }
2004 
2005  /* encode length number of bytes from inbuf */
2006  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2007  totaloutlen = outlen;
2008  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2009  totaloutlen += outlen;
2010  if( POLARSSL_MODE_CBC == cipher_info->mode )
2011  {
2012  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2013  }
2014  else
2015  {
2016  fct_chk( totaloutlen == enclen );
2017  }
2018  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2019  totaloutlen += outlen;
2020  if( POLARSSL_MODE_CBC == cipher_info->mode )
2021  {
2022  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2023  }
2024  else
2025  {
2026  fct_chk( outlen == 0 );
2027  }
2028 
2029  /* decode the previously encoded string */
2030  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2031  if( POLARSSL_MODE_CBC == cipher_info->mode )
2032  {
2033  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2034  }
2035  else
2036  {
2037  fct_chk( enclen == outlen );
2038  }
2039  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2040  if( POLARSSL_MODE_CBC == cipher_info->mode )
2041  {
2042  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2043  }
2044  else
2045  {
2046  fct_chk( outlen == 0 );
2047  }
2048 
2049 
2050  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2051 
2052  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2053  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2054  FCT_TEST_END();
2055 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2056 
2057 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2058 
2059  FCT_TEST_BGN(null_encrypt_and_decrypt_16_bytes_in_multiple_parts_2)
2060  size_t first_length = 0;
2061  size_t second_length = 16;
2062  size_t length = first_length + second_length;
2063  unsigned char key[32];
2064  unsigned char iv[16];
2065 
2066  cipher_context_t ctx_dec;
2067  cipher_context_t ctx_enc;
2068  const cipher_info_t *cipher_info;
2069 
2070  unsigned char inbuf[64];
2071  unsigned char encbuf[64];
2072  unsigned char decbuf[64];
2073 
2074  size_t outlen = 0;
2075  size_t totaloutlen = 0;
2076  size_t enclen = 0;
2077 
2078  memset( key, 0, 32 );
2079  memset( iv , 0, 16 );
2080 
2081  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2082  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2083 
2084  memset( inbuf, 5, 64 );
2085  memset( encbuf, 0, 64 );
2086  memset( decbuf, 0, 64 );
2087 
2088  /* Initialise enc and dec contexts */
2090  fct_chk( NULL != cipher_info);
2091 
2092  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2093  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2094 
2095  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2096  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2097 
2098  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2099  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2100 
2101  if( POLARSSL_MODE_CBC == cipher_info->mode )
2102  {
2103  enclen = cipher_get_block_size(&ctx_enc )
2104  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2105  }
2106  else
2107  {
2108  enclen = length;
2109  }
2110 
2111  /* encode length number of bytes from inbuf */
2112  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2113  totaloutlen = outlen;
2114  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2115  totaloutlen += outlen;
2116  if( POLARSSL_MODE_CBC == cipher_info->mode )
2117  {
2118  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2119  }
2120  else
2121  {
2122  fct_chk( totaloutlen == enclen );
2123  }
2124  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2125  totaloutlen += outlen;
2126  if( POLARSSL_MODE_CBC == cipher_info->mode )
2127  {
2128  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2129  }
2130  else
2131  {
2132  fct_chk( outlen == 0 );
2133  }
2134 
2135  /* decode the previously encoded string */
2136  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2137  if( POLARSSL_MODE_CBC == cipher_info->mode )
2138  {
2139  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2140  }
2141  else
2142  {
2143  fct_chk( enclen == outlen );
2144  }
2145  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2146  if( POLARSSL_MODE_CBC == cipher_info->mode )
2147  {
2148  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2149  }
2150  else
2151  {
2152  fct_chk( outlen == 0 );
2153  }
2154 
2155 
2156  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2157 
2158  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2159  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2160  FCT_TEST_END();
2161 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2162 
2163 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2164 
2165  FCT_TEST_BGN(null_encrypt_and_decrypt_16_bytes_in_multiple_parts_3)
2166  size_t first_length = 1;
2167  size_t second_length = 15;
2168  size_t length = first_length + second_length;
2169  unsigned char key[32];
2170  unsigned char iv[16];
2171 
2172  cipher_context_t ctx_dec;
2173  cipher_context_t ctx_enc;
2174  const cipher_info_t *cipher_info;
2175 
2176  unsigned char inbuf[64];
2177  unsigned char encbuf[64];
2178  unsigned char decbuf[64];
2179 
2180  size_t outlen = 0;
2181  size_t totaloutlen = 0;
2182  size_t enclen = 0;
2183 
2184  memset( key, 0, 32 );
2185  memset( iv , 0, 16 );
2186 
2187  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2188  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2189 
2190  memset( inbuf, 5, 64 );
2191  memset( encbuf, 0, 64 );
2192  memset( decbuf, 0, 64 );
2193 
2194  /* Initialise enc and dec contexts */
2196  fct_chk( NULL != cipher_info);
2197 
2198  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2199  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2200 
2201  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2202  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2203 
2204  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2205  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2206 
2207  if( POLARSSL_MODE_CBC == cipher_info->mode )
2208  {
2209  enclen = cipher_get_block_size(&ctx_enc )
2210  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2211  }
2212  else
2213  {
2214  enclen = length;
2215  }
2216 
2217  /* encode length number of bytes from inbuf */
2218  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2219  totaloutlen = outlen;
2220  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2221  totaloutlen += outlen;
2222  if( POLARSSL_MODE_CBC == cipher_info->mode )
2223  {
2224  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2225  }
2226  else
2227  {
2228  fct_chk( totaloutlen == enclen );
2229  }
2230  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2231  totaloutlen += outlen;
2232  if( POLARSSL_MODE_CBC == cipher_info->mode )
2233  {
2234  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2235  }
2236  else
2237  {
2238  fct_chk( outlen == 0 );
2239  }
2240 
2241  /* decode the previously encoded string */
2242  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2243  if( POLARSSL_MODE_CBC == cipher_info->mode )
2244  {
2245  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2246  }
2247  else
2248  {
2249  fct_chk( enclen == outlen );
2250  }
2251  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2252  if( POLARSSL_MODE_CBC == cipher_info->mode )
2253  {
2254  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2255  }
2256  else
2257  {
2258  fct_chk( outlen == 0 );
2259  }
2260 
2261 
2262  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2263 
2264  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2265  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2266  FCT_TEST_END();
2267 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2268 
2269 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2270 
2271  FCT_TEST_BGN(null_encrypt_and_decrypt_16_bytes_in_multiple_parts_4)
2272  size_t first_length = 15;
2273  size_t second_length = 1;
2274  size_t length = first_length + second_length;
2275  unsigned char key[32];
2276  unsigned char iv[16];
2277 
2278  cipher_context_t ctx_dec;
2279  cipher_context_t ctx_enc;
2280  const cipher_info_t *cipher_info;
2281 
2282  unsigned char inbuf[64];
2283  unsigned char encbuf[64];
2284  unsigned char decbuf[64];
2285 
2286  size_t outlen = 0;
2287  size_t totaloutlen = 0;
2288  size_t enclen = 0;
2289 
2290  memset( key, 0, 32 );
2291  memset( iv , 0, 16 );
2292 
2293  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2294  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2295 
2296  memset( inbuf, 5, 64 );
2297  memset( encbuf, 0, 64 );
2298  memset( decbuf, 0, 64 );
2299 
2300  /* Initialise enc and dec contexts */
2302  fct_chk( NULL != cipher_info);
2303 
2304  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2305  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2306 
2307  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2308  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2309 
2310  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2311  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2312 
2313  if( POLARSSL_MODE_CBC == cipher_info->mode )
2314  {
2315  enclen = cipher_get_block_size(&ctx_enc )
2316  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2317  }
2318  else
2319  {
2320  enclen = length;
2321  }
2322 
2323  /* encode length number of bytes from inbuf */
2324  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2325  totaloutlen = outlen;
2326  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2327  totaloutlen += outlen;
2328  if( POLARSSL_MODE_CBC == cipher_info->mode )
2329  {
2330  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2331  }
2332  else
2333  {
2334  fct_chk( totaloutlen == enclen );
2335  }
2336  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2337  totaloutlen += outlen;
2338  if( POLARSSL_MODE_CBC == cipher_info->mode )
2339  {
2340  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2341  }
2342  else
2343  {
2344  fct_chk( outlen == 0 );
2345  }
2346 
2347  /* decode the previously encoded string */
2348  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2349  if( POLARSSL_MODE_CBC == cipher_info->mode )
2350  {
2351  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2352  }
2353  else
2354  {
2355  fct_chk( enclen == outlen );
2356  }
2357  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2358  if( POLARSSL_MODE_CBC == cipher_info->mode )
2359  {
2360  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2361  }
2362  else
2363  {
2364  fct_chk( outlen == 0 );
2365  }
2366 
2367 
2368  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2369 
2370  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2371  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2372  FCT_TEST_END();
2373 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2374 
2375 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2376 
2377  FCT_TEST_BGN(null_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2378  size_t first_length = 15;
2379  size_t second_length = 7;
2380  size_t length = first_length + second_length;
2381  unsigned char key[32];
2382  unsigned char iv[16];
2383 
2384  cipher_context_t ctx_dec;
2385  cipher_context_t ctx_enc;
2386  const cipher_info_t *cipher_info;
2387 
2388  unsigned char inbuf[64];
2389  unsigned char encbuf[64];
2390  unsigned char decbuf[64];
2391 
2392  size_t outlen = 0;
2393  size_t totaloutlen = 0;
2394  size_t enclen = 0;
2395 
2396  memset( key, 0, 32 );
2397  memset( iv , 0, 16 );
2398 
2399  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2400  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2401 
2402  memset( inbuf, 5, 64 );
2403  memset( encbuf, 0, 64 );
2404  memset( decbuf, 0, 64 );
2405 
2406  /* Initialise enc and dec contexts */
2408  fct_chk( NULL != cipher_info);
2409 
2410  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2411  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2412 
2413  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2414  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2415 
2416  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2417  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2418 
2419  if( POLARSSL_MODE_CBC == cipher_info->mode )
2420  {
2421  enclen = cipher_get_block_size(&ctx_enc )
2422  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2423  }
2424  else
2425  {
2426  enclen = length;
2427  }
2428 
2429  /* encode length number of bytes from inbuf */
2430  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2431  totaloutlen = outlen;
2432  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2433  totaloutlen += outlen;
2434  if( POLARSSL_MODE_CBC == cipher_info->mode )
2435  {
2436  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2437  }
2438  else
2439  {
2440  fct_chk( totaloutlen == enclen );
2441  }
2442  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2443  totaloutlen += outlen;
2444  if( POLARSSL_MODE_CBC == cipher_info->mode )
2445  {
2446  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2447  }
2448  else
2449  {
2450  fct_chk( outlen == 0 );
2451  }
2452 
2453  /* decode the previously encoded string */
2454  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2455  if( POLARSSL_MODE_CBC == cipher_info->mode )
2456  {
2457  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2458  }
2459  else
2460  {
2461  fct_chk( enclen == outlen );
2462  }
2463  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2464  if( POLARSSL_MODE_CBC == cipher_info->mode )
2465  {
2466  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2467  }
2468  else
2469  {
2470  fct_chk( outlen == 0 );
2471  }
2472 
2473 
2474  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2475 
2476  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2477  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2478  FCT_TEST_END();
2479 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2480 
2481 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2482 
2483  FCT_TEST_BGN(null_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2484  size_t first_length = 16;
2485  size_t second_length = 6;
2486  size_t length = first_length + second_length;
2487  unsigned char key[32];
2488  unsigned char iv[16];
2489 
2490  cipher_context_t ctx_dec;
2491  cipher_context_t ctx_enc;
2492  const cipher_info_t *cipher_info;
2493 
2494  unsigned char inbuf[64];
2495  unsigned char encbuf[64];
2496  unsigned char decbuf[64];
2497 
2498  size_t outlen = 0;
2499  size_t totaloutlen = 0;
2500  size_t enclen = 0;
2501 
2502  memset( key, 0, 32 );
2503  memset( iv , 0, 16 );
2504 
2505  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2506  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2507 
2508  memset( inbuf, 5, 64 );
2509  memset( encbuf, 0, 64 );
2510  memset( decbuf, 0, 64 );
2511 
2512  /* Initialise enc and dec contexts */
2514  fct_chk( NULL != cipher_info);
2515 
2516  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2517  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2518 
2519  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2520  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2521 
2522  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2523  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2524 
2525  if( POLARSSL_MODE_CBC == cipher_info->mode )
2526  {
2527  enclen = cipher_get_block_size(&ctx_enc )
2528  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2529  }
2530  else
2531  {
2532  enclen = length;
2533  }
2534 
2535  /* encode length number of bytes from inbuf */
2536  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2537  totaloutlen = outlen;
2538  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2539  totaloutlen += outlen;
2540  if( POLARSSL_MODE_CBC == cipher_info->mode )
2541  {
2542  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2543  }
2544  else
2545  {
2546  fct_chk( totaloutlen == enclen );
2547  }
2548  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2549  totaloutlen += outlen;
2550  if( POLARSSL_MODE_CBC == cipher_info->mode )
2551  {
2552  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2553  }
2554  else
2555  {
2556  fct_chk( outlen == 0 );
2557  }
2558 
2559  /* decode the previously encoded string */
2560  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2561  if( POLARSSL_MODE_CBC == cipher_info->mode )
2562  {
2563  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2564  }
2565  else
2566  {
2567  fct_chk( enclen == outlen );
2568  }
2569  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2570  if( POLARSSL_MODE_CBC == cipher_info->mode )
2571  {
2572  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2573  }
2574  else
2575  {
2576  fct_chk( outlen == 0 );
2577  }
2578 
2579 
2580  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2581 
2582  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2583  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2584  FCT_TEST_END();
2585 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2586 
2587 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2588 
2589  FCT_TEST_BGN(null_encrypt_and_decrypt_22_bytes_in_multiple_parts_1)
2590  size_t first_length = 17;
2591  size_t second_length = 6;
2592  size_t length = first_length + second_length;
2593  unsigned char key[32];
2594  unsigned char iv[16];
2595 
2596  cipher_context_t ctx_dec;
2597  cipher_context_t ctx_enc;
2598  const cipher_info_t *cipher_info;
2599 
2600  unsigned char inbuf[64];
2601  unsigned char encbuf[64];
2602  unsigned char decbuf[64];
2603 
2604  size_t outlen = 0;
2605  size_t totaloutlen = 0;
2606  size_t enclen = 0;
2607 
2608  memset( key, 0, 32 );
2609  memset( iv , 0, 16 );
2610 
2611  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2612  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2613 
2614  memset( inbuf, 5, 64 );
2615  memset( encbuf, 0, 64 );
2616  memset( decbuf, 0, 64 );
2617 
2618  /* Initialise enc and dec contexts */
2620  fct_chk( NULL != cipher_info);
2621 
2622  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2623  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2624 
2625  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2626  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2627 
2628  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2629  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2630 
2631  if( POLARSSL_MODE_CBC == cipher_info->mode )
2632  {
2633  enclen = cipher_get_block_size(&ctx_enc )
2634  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2635  }
2636  else
2637  {
2638  enclen = length;
2639  }
2640 
2641  /* encode length number of bytes from inbuf */
2642  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2643  totaloutlen = outlen;
2644  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2645  totaloutlen += outlen;
2646  if( POLARSSL_MODE_CBC == cipher_info->mode )
2647  {
2648  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2649  }
2650  else
2651  {
2652  fct_chk( totaloutlen == enclen );
2653  }
2654  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2655  totaloutlen += outlen;
2656  if( POLARSSL_MODE_CBC == cipher_info->mode )
2657  {
2658  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2659  }
2660  else
2661  {
2662  fct_chk( outlen == 0 );
2663  }
2664 
2665  /* decode the previously encoded string */
2666  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2667  if( POLARSSL_MODE_CBC == cipher_info->mode )
2668  {
2669  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2670  }
2671  else
2672  {
2673  fct_chk( enclen == outlen );
2674  }
2675  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2676  if( POLARSSL_MODE_CBC == cipher_info->mode )
2677  {
2678  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2679  }
2680  else
2681  {
2682  fct_chk( outlen == 0 );
2683  }
2684 
2685 
2686  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2687 
2688  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2689  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2690  FCT_TEST_END();
2691 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2692 
2693 #ifdef POLARSSL_CIPHER_NULL_CIPHER
2694 
2695  FCT_TEST_BGN(null_encrypt_and_decrypt_32_bytes_in_multiple_parts_1)
2696  size_t first_length = 16;
2697  size_t second_length = 16;
2698  size_t length = first_length + second_length;
2699  unsigned char key[32];
2700  unsigned char iv[16];
2701 
2702  cipher_context_t ctx_dec;
2703  cipher_context_t ctx_enc;
2704  const cipher_info_t *cipher_info;
2705 
2706  unsigned char inbuf[64];
2707  unsigned char encbuf[64];
2708  unsigned char decbuf[64];
2709 
2710  size_t outlen = 0;
2711  size_t totaloutlen = 0;
2712  size_t enclen = 0;
2713 
2714  memset( key, 0, 32 );
2715  memset( iv , 0, 16 );
2716 
2717  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
2718  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
2719 
2720  memset( inbuf, 5, 64 );
2721  memset( encbuf, 0, 64 );
2722  memset( decbuf, 0, 64 );
2723 
2724  /* Initialise enc and dec contexts */
2726  fct_chk( NULL != cipher_info);
2727 
2728  fct_chk( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
2729  fct_chk( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
2730 
2731  fct_chk( 0 == cipher_setkey( &ctx_dec, key, 0, POLARSSL_DECRYPT ) );
2732  fct_chk( 0 == cipher_setkey( &ctx_enc, key, 0, POLARSSL_ENCRYPT ) );
2733 
2734  fct_chk( 0 == cipher_reset( &ctx_dec, iv ) );
2735  fct_chk( 0 == cipher_reset( &ctx_enc, iv ) );
2736 
2737  if( POLARSSL_MODE_CBC == cipher_info->mode )
2738  {
2739  enclen = cipher_get_block_size(&ctx_enc )
2740  * ( 1 + length / cipher_get_block_size( &ctx_enc ) );
2741  }
2742  else
2743  {
2744  enclen = length;
2745  }
2746 
2747  /* encode length number of bytes from inbuf */
2748  fct_chk( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
2749  totaloutlen = outlen;
2750  fct_chk( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
2751  totaloutlen += outlen;
2752  if( POLARSSL_MODE_CBC == cipher_info->mode )
2753  {
2754  fct_chk( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
2755  }
2756  else
2757  {
2758  fct_chk( totaloutlen == enclen );
2759  }
2760  fct_chk( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
2761  totaloutlen += outlen;
2762  if( POLARSSL_MODE_CBC == cipher_info->mode )
2763  {
2764  fct_chk( outlen == cipher_get_block_size ( &ctx_enc ) );
2765  }
2766  else
2767  {
2768  fct_chk( outlen == 0 );
2769  }
2770 
2771  /* decode the previously encoded string */
2772  fct_chk( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
2773  if( POLARSSL_MODE_CBC == cipher_info->mode )
2774  {
2775  fct_chk( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
2776  }
2777  else
2778  {
2779  fct_chk( enclen == outlen );
2780  }
2781  fct_chk( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
2782  if( POLARSSL_MODE_CBC == cipher_info->mode )
2783  {
2784  fct_chk( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
2785  }
2786  else
2787  {
2788  fct_chk( outlen == 0 );
2789  }
2790 
2791 
2792  fct_chk( 0 == memcmp(inbuf, decbuf, length) );
2793 
2794  fct_chk( 0 == cipher_free_ctx( &ctx_dec ) );
2795  fct_chk( 0 == cipher_free_ctx( &ctx_enc ) );
2796  FCT_TEST_END();
2797 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
2798 
2799  }
2800  FCT_SUITE_END();
2801 
2802 #endif /* POLARSSL_CIPHER_C */
2803 
2804 }
2805 FCT_END();
2806