PolarSSL v1.3.2
test_suite_cipher.camellia.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_CIPHER_C
4 
5 #include <polarssl/cipher.h>
6 
7 #if defined(POLARSSL_GCM_C)
8 #include <polarssl/gcm.h>
9 #endif
10 #endif /* POLARSSL_CIPHER_C */
11 
12 
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14 #include "polarssl/memory.h"
15 #endif
16 
17 #if defined(WANT_NOT_RND_MPI)
18 #if defined(POLARSSL_BIGNUM_C)
19 #include "polarssl/bignum.h"
20 #else
21 #error "not_rnd_mpi() need bignum.c"
22 #endif
23 #endif
24 
25 #ifdef _MSC_VER
26 #include <basetsd.h>
27 typedef UINT32 uint32_t;
28 #else
29 #include <inttypes.h>
30 #endif
31 
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 /*
37  * 32-bit integer manipulation macros (big endian)
38  */
39 #ifndef GET_UINT32_BE
40 #define GET_UINT32_BE(n,b,i) \
41 { \
42  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
43  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
44  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
45  | ( (uint32_t) (b)[(i) + 3] ); \
46 }
47 #endif
48 
49 #ifndef PUT_UINT32_BE
50 #define PUT_UINT32_BE(n,b,i) \
51 { \
52  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
53  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
54  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
55  (b)[(i) + 3] = (unsigned char) ( (n) ); \
56 }
57 #endif
58 
59 static int unhexify(unsigned char *obuf, const char *ibuf)
60 {
61  unsigned char c, c2;
62  int len = strlen(ibuf) / 2;
63  assert(!(strlen(ibuf) %1)); // must be even number of bytes
64 
65  while (*ibuf != 0)
66  {
67  c = *ibuf++;
68  if( c >= '0' && c <= '9' )
69  c -= '0';
70  else if( c >= 'a' && c <= 'f' )
71  c -= 'a' - 10;
72  else if( c >= 'A' && c <= 'F' )
73  c -= 'A' - 10;
74  else
75  assert( 0 );
76 
77  c2 = *ibuf++;
78  if( c2 >= '0' && c2 <= '9' )
79  c2 -= '0';
80  else if( c2 >= 'a' && c2 <= 'f' )
81  c2 -= 'a' - 10;
82  else if( c2 >= 'A' && c2 <= 'F' )
83  c2 -= 'A' - 10;
84  else
85  assert( 0 );
86 
87  *obuf++ = ( c << 4 ) | c2;
88  }
89 
90  return len;
91 }
92 
93 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
94 {
95  unsigned char l, h;
96 
97  while (len != 0)
98  {
99  h = (*ibuf) / 16;
100  l = (*ibuf) % 16;
101 
102  if( h < 10 )
103  *obuf++ = '0' + h;
104  else
105  *obuf++ = 'a' + h - 10;
106 
107  if( l < 10 )
108  *obuf++ = '0' + l;
109  else
110  *obuf++ = 'a' + l - 10;
111 
112  ++ibuf;
113  len--;
114  }
115 }
116 
126 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
127 {
128  size_t i;
129 
130  if( rng_state != NULL )
131  rng_state = NULL;
132 
133  for( i = 0; i < len; ++i )
134  output[i] = rand();
135 
136  return( 0 );
137 }
138 
144 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
145 {
146  if( rng_state != NULL )
147  rng_state = NULL;
148 
149  memset( output, 0, len );
150 
151  return( 0 );
152 }
153 
154 typedef struct
155 {
156  unsigned char *buf;
157  size_t length;
158 } rnd_buf_info;
159 
171 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
172 {
173  rnd_buf_info *info = (rnd_buf_info *) rng_state;
174  size_t use_len;
175 
176  if( rng_state == NULL )
177  return( rnd_std_rand( NULL, output, len ) );
178 
179  use_len = len;
180  if( len > info->length )
181  use_len = info->length;
182 
183  if( use_len )
184  {
185  memcpy( output, info->buf, use_len );
186  info->buf += use_len;
187  info->length -= use_len;
188  }
189 
190  if( len - use_len > 0 )
191  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
192 
193  return( 0 );
194 }
195 
203 typedef struct
204 {
205  uint32_t key[16];
206  uint32_t v0, v1;
208 
217 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
218 {
219  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
220  uint32_t i, *k, sum, delta=0x9E3779B9;
221  unsigned char result[4];
222 
223  if( rng_state == NULL )
224  return( rnd_std_rand( NULL, output, len ) );
225 
226  k = info->key;
227 
228  while( len > 0 )
229  {
230  size_t use_len = ( len > 4 ) ? 4 : len;
231  sum = 0;
232 
233  for( i = 0; i < 32; i++ )
234  {
235  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
236  sum += delta;
237  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
238  }
239 
240  PUT_UINT32_BE( info->v0, result, 0 );
241  memcpy( output, result, use_len );
242  len -= use_len;
243  }
244 
245  return( 0 );
246 }
247 
248 #if defined(WANT_NOT_RND_MPI)
249 
257 #define ciL (sizeof(t_uint)) /* chars in limb */
258 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
259 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
260 {
261  char *str = (char *) in;
262  mpi X;
263 
264  /*
265  * The 'in' pointer we get is from an MPI prepared by mpi_fill_random(),
266  * just reconstruct the rest in order to be able to call mpi_read_string()
267  */
268  X.s = 1;
269  X.p = (t_uint *) out;
270  X.n = CHARS_TO_LIMBS( len );
271 
272  /*
273  * If str is too long, mpi_read_string() will try to allocate a new buffer
274  * for X.p, which we want to avoid at all costs.
275  */
276  assert( strlen( str ) / 2 == len );
277 
278  return( mpi_read_string( &X, 16, str ) );
279 }
280 #endif /* WANT_NOT_RND_MPI */
281 
282 
283 #include <stdio.h>
284 #include <string.h>
285 
286 static int test_errors = 0;
287 
288 #ifdef POLARSSL_CIPHER_C
289 
290 #define TEST_SUITE_ACTIVE
291 
292 static int test_assert( int correct, char *test )
293 {
294  if( correct )
295  return( 0 );
296 
297  test_errors++;
298  if( test_errors == 1 )
299  printf( "FAILED\n" );
300  printf( " %s\n", test );
301 
302  return( 1 );
303 }
304 
305 #define TEST_ASSERT( TEST ) \
306  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
307  if( test_errors) return; \
308  } while (0)
309 
310 int verify_string( char **str )
311 {
312  if( (*str)[0] != '"' ||
313  (*str)[strlen( *str ) - 1] != '"' )
314  {
315  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
316  return( -1 );
317  }
318 
319  (*str)++;
320  (*str)[strlen( *str ) - 1] = '\0';
321 
322  return( 0 );
323 }
324 
325 int verify_int( char *str, int *value )
326 {
327  size_t i;
328  int minus = 0;
329  int digits = 1;
330  int hex = 0;
331 
332  for( i = 0; i < strlen( str ); i++ )
333  {
334  if( i == 0 && str[i] == '-' )
335  {
336  minus = 1;
337  continue;
338  }
339 
340  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
341  str[i - 1] == '0' && str[i] == 'x' )
342  {
343  hex = 1;
344  continue;
345  }
346 
347  if( str[i] < '0' || str[i] > '9' )
348  {
349  digits = 0;
350  break;
351  }
352  }
353 
354  if( digits )
355  {
356  if( hex )
357  *value = strtol( str, NULL, 16 );
358  else
359  *value = strtol( str, NULL, 10 );
360 
361  return( 0 );
362  }
363 
364  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_256_CBC" ) == 0 )
365  {
367  return( 0 );
368  }
369  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_CFB128" ) == 0 )
370  {
372  return( 0 );
373  }
374  if( strcmp( str, "POLARSSL_PADDING_ZEROS" ) == 0 )
375  {
376  *value = ( POLARSSL_PADDING_ZEROS );
377  return( 0 );
378  }
379  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_192_CBC" ) == 0 )
380  {
382  return( 0 );
383  }
384  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_CBC" ) == 0 )
385  {
387  return( 0 );
388  }
389  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_CTR" ) == 0 )
390  {
392  return( 0 );
393  }
394  if( strcmp( str, "POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED" ) == 0 )
395  {
397  return( 0 );
398  }
399  if( strcmp( str, "POLARSSL_PADDING_NONE" ) == 0 )
400  {
401  *value = ( POLARSSL_PADDING_NONE );
402  return( 0 );
403  }
404  if( strcmp( str, "POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
405  {
406  *value = ( POLARSSL_PADDING_ZEROS_AND_LEN );
407  return( 0 );
408  }
409  if( strcmp( str, "-1" ) == 0 )
410  {
411  *value = ( -1 );
412  return( 0 );
413  }
414  if( strcmp( str, "POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
415  {
416  *value = ( POLARSSL_PADDING_ONE_AND_ZEROS );
417  return( 0 );
418  }
419 
420 
421  printf( "Expected integer for parameter and got: %s\n", str );
422  return( -1 );
423 }
424 
425 void test_suite_enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
426  int length_val, int pad_mode )
427 {
428  size_t length = length_val, outlen, total_len, i;
429  unsigned char key[32];
430  unsigned char iv[16];
431  unsigned char ad[13];
432  unsigned char tag[16];
433  unsigned char inbuf[64];
434  unsigned char encbuf[64];
435  unsigned char decbuf[64];
436 
437  const cipher_info_t *cipher_info;
438  cipher_context_t ctx_dec;
439  cipher_context_t ctx_enc;
440 
441  /*
442  * Prepare contexts
443  */
444  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
445  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
446 
447  memset( key, 0x2a, sizeof( key ) );
448 
449  /* Check and get info structures */
450  cipher_info = cipher_info_from_type( cipher_id );
451  TEST_ASSERT( NULL != cipher_info );
452  TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
453 
454  /* Initialise enc and dec contexts */
455  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
456  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
457 
458  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
459  TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
460 
461 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
462  if( -1 != pad_mode )
463  {
464  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
465  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
466  }
467 #else
468  (void) pad_mode;
469 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
470 
471  /*
472  * Do a few encode/decode cycles
473  */
474  for( i = 0; i < 3; i++ )
475  {
476  memset( iv , 0x00 + i, sizeof( iv ) );
477  memset( ad, 0x10 + i, sizeof( ad ) );
478  memset( inbuf, 0x20 + i, sizeof( inbuf ) );
479 
480  memset( encbuf, 0, sizeof( encbuf ) );
481  memset( decbuf, 0, sizeof( decbuf ) );
482  memset( tag, 0, sizeof( tag ) );
483 
484  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
485  TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
486 
487  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
488  TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
489 
490 #if defined(POLARSSL_CIPHER_MODE_AEAD)
491  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
492  TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
493 #endif /* POLARSSL_CIPHER_MODE_AEAD */
494 
495  /* encode length number of bytes from inbuf */
496  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
497  total_len = outlen;
498 
499  TEST_ASSERT( total_len == length ||
500  ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
501  total_len < length &&
502  total_len + cipher_get_block_size( &ctx_enc ) > length ) );
503 
504  TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
505  total_len += outlen;
506 
507 #if defined(POLARSSL_CIPHER_MODE_AEAD)
508  TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
509 #endif /* POLARSSL_CIPHER_MODE_AEAD */
510 
511  TEST_ASSERT( total_len == length ||
512  ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
513  total_len > length &&
514  total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
515 
516  /* decode the previously encoded string */
517  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
518  total_len = outlen;
519 
520  TEST_ASSERT( total_len == length ||
521  ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
522  total_len < length &&
523  total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
524 
525  TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
526  total_len += outlen;
527 
528 #if defined(POLARSSL_CIPHER_MODE_AEAD)
529  TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
530 #endif /* POLARSSL_CIPHER_MODE_AEAD */
531 
532  /* check result */
533  TEST_ASSERT( total_len == length );
534  TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
535  }
536 
537  /*
538  * Done
539  */
540  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
541  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
542 }
543 
544 void test_suite_enc_fail( int cipher_id, int pad_mode, int key_len,
545  int length_val, int ret )
546 {
547  size_t length = length_val;
548  unsigned char key[32];
549  unsigned char iv[16];
550 
551  const cipher_info_t *cipher_info;
552  cipher_context_t ctx;
553 
554  unsigned char inbuf[64];
555  unsigned char encbuf[64];
556 
557  size_t outlen = 0;
558 
559  memset( key, 0, 32 );
560  memset( iv , 0, 16 );
561 
562  memset( &ctx, 0, sizeof( ctx ) );
563 
564  memset( inbuf, 5, 64 );
565  memset( encbuf, 0, 64 );
566 
567  /* Check and get info structures */
568  cipher_info = cipher_info_from_type( cipher_id );
569  TEST_ASSERT( NULL != cipher_info );
570 
571  /* Initialise context */
572  TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
573  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
574 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
575  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
576 #else
577  (void) pad_mode;
578 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
579  TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
580  TEST_ASSERT( 0 == cipher_reset( &ctx ) );
581 #if defined(POLARSSL_CIPHER_MODE_AEAD)
582  TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
583 #endif /* POLARSSL_CIPHER_MODE_AEAD */
584 
585  /* encode length number of bytes from inbuf */
586  TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
587  TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
588 
589  /* done */
590  TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
591 }
592 
593 void test_suite_dec_empty_buf()
594 {
595  unsigned char key[32];
596  unsigned char iv[16];
597 
598  cipher_context_t ctx_dec;
599  const cipher_info_t *cipher_info;
600 
601  unsigned char encbuf[64];
602  unsigned char decbuf[64];
603 
604  size_t outlen = 0;
605 
606  memset( key, 0, 32 );
607  memset( iv , 0, 16 );
608 
609  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
610 
611  memset( encbuf, 0, 64 );
612  memset( decbuf, 0, 64 );
613 
614  /* Initialise context */
616  TEST_ASSERT( NULL != cipher_info);
617 
618  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
619 
620  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
621 
622  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
623 
624  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
625 
626 #if defined(POLARSSL_CIPHER_MODE_AEAD)
627  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
628 #endif /* POLARSSL_CIPHER_MODE_AEAD */
629 
630  /* decode 0-byte string */
631  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
632  TEST_ASSERT( 0 == outlen );
634  &ctx_dec, decbuf + outlen, &outlen ) );
635  TEST_ASSERT( 0 == outlen );
636 
637  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
638 }
639 
640 void test_suite_enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
641  int second_length_val )
642 {
643  size_t first_length = first_length_val;
644  size_t second_length = second_length_val;
645  size_t length = first_length + second_length;
646  unsigned char key[32];
647  unsigned char iv[16];
648 
649  cipher_context_t ctx_dec;
650  cipher_context_t ctx_enc;
651  const cipher_info_t *cipher_info;
652 
653  unsigned char inbuf[64];
654  unsigned char encbuf[64];
655  unsigned char decbuf[64];
656 
657  size_t outlen = 0;
658  size_t totaloutlen = 0;
659 
660  memset( key, 0, 32 );
661  memset( iv , 0, 16 );
662 
663  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
664  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
665 
666  memset( inbuf, 5, 64 );
667  memset( encbuf, 0, 64 );
668  memset( decbuf, 0, 64 );
669 
670  /* Initialise enc and dec contexts */
671  cipher_info = cipher_info_from_type( cipher_id );
672  TEST_ASSERT( NULL != cipher_info);
673 
674  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
675  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
676 
677  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
678  TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
679 
680  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
681  TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
682 
683  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
684  TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
685 
686 #if defined(POLARSSL_CIPHER_MODE_AEAD)
687  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
688  TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
689 #endif /* POLARSSL_CIPHER_MODE_AEAD */
690 
691  /* encode length number of bytes from inbuf */
692  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
693  totaloutlen = outlen;
694  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
695  totaloutlen += outlen;
696  TEST_ASSERT( totaloutlen == length ||
697  ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
698  totaloutlen < length &&
699  totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
700 
701  TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
702  totaloutlen += outlen;
703  TEST_ASSERT( totaloutlen == length ||
704  ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
705  totaloutlen > length &&
706  totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
707 
708  /* decode the previously encoded string */
709  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
710  totaloutlen = outlen;
711 
712  TEST_ASSERT( totaloutlen == length ||
713  ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
714  totaloutlen < length &&
715  totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
716 
717  TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
718  totaloutlen += outlen;
719 
720  TEST_ASSERT( totaloutlen == length );
721 
722  TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
723 
724  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
725  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
726 }
727 
728 void test_suite_decrypt_test_vec( int cipher_id, int pad_mode,
729  char *hex_key, char *hex_iv,
730  char *hex_cipher, char *hex_clear,
731  char *hex_ad, char *hex_tag,
732  int finish_result, int tag_result )
733 {
734  unsigned char key[50];
735  unsigned char iv[50];
736  unsigned char cipher[200];
737  unsigned char clear[200];
738  unsigned char ad[200];
739  unsigned char tag[20];
740  size_t key_len, iv_len, cipher_len, clear_len;
741 #if defined(POLARSSL_CIPHER_MODE_AEAD)
742  size_t ad_len, tag_len;
743 #endif
744  cipher_context_t ctx;
745  unsigned char output[200];
746  size_t outlen, total_len;
747 
748  memset( key, 0x00, sizeof( key ) );
749  memset( iv, 0x00, sizeof( iv ) );
750  memset( cipher, 0x00, sizeof( cipher ) );
751  memset( clear, 0x00, sizeof( clear ) );
752  memset( ad, 0x00, sizeof( ad ) );
753  memset( tag, 0x00, sizeof( tag ) );
754  memset( output, 0x00, sizeof( output ) );
755 
756  key_len = unhexify( key, hex_key );
757  iv_len = unhexify( iv, hex_iv );
758  cipher_len = unhexify( cipher, hex_cipher );
759  clear_len = unhexify( clear, hex_clear );
760 #if defined(POLARSSL_CIPHER_MODE_AEAD)
761  ad_len = unhexify( ad, hex_ad );
762  tag_len = unhexify( tag, hex_tag );
763 #else
764  ((void) hex_ad);
765  ((void) hex_tag);
766 #endif
767 
768  /* Prepare context */
769  TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
770  cipher_info_from_type( cipher_id ) ) );
771  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
772 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
773  if( pad_mode != -1 )
774  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
775 #else
776  (void) pad_mode;
777 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
778  TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
779  TEST_ASSERT( 0 == cipher_reset( &ctx ) );
780 #if defined(POLARSSL_CIPHER_MODE_AEAD)
781  TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
782 #endif /* POLARSSL_CIPHER_MODE_AEAD */
783 
784  /* decode buffer and check tag */
785  total_len = 0;
786  TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
787  total_len += outlen;
788  TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
789  &outlen ) );
790  total_len += outlen;
791 #if defined(POLARSSL_CIPHER_MODE_AEAD)
792  TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
793 #endif /* POLARSSL_CIPHER_MODE_AEAD */
794 
795  /* check plaintext only if everything went fine */
796  if( 0 == finish_result && 0 == tag_result )
797  {
798  TEST_ASSERT( total_len == clear_len );
799  TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
800  }
801 
802  cipher_free_ctx( &ctx );
803 }
804 
805 void test_suite_test_vec_ecb( int cipher_id, int operation, char *hex_key,
806  char *hex_input, char *hex_result,
807  int finish_result )
808 {
809  unsigned char key[50];
810  unsigned char input[16];
811  unsigned char result[16];
812  size_t key_len;
813  cipher_context_t ctx;
814  unsigned char output[32];
815  size_t outlen;
816 
817  memset( key, 0x00, sizeof( key ) );
818  memset( input, 0x00, sizeof( input ) );
819  memset( result, 0x00, sizeof( result ) );
820  memset( output, 0x00, sizeof( output ) );
821 
822  /* Prepare context */
823  TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
824  cipher_info_from_type( cipher_id ) ) );
825 
826  key_len = unhexify( key, hex_key );
827  TEST_ASSERT( unhexify( input, hex_input ) ==
828  (int) cipher_get_block_size( &ctx ) );
829  TEST_ASSERT( unhexify( result, hex_result ) ==
830  (int) cipher_get_block_size( &ctx ) );
831 
832  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
833 
834  TEST_ASSERT( 0 == cipher_update( &ctx, input,
835  cipher_get_block_size( &ctx ),
836  output, &outlen ) );
837  TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
838  TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
839  &outlen ) );
840  TEST_ASSERT( 0 == outlen );
841 
842  /* check plaintext only if everything went fine */
843  if( 0 == finish_result )
844  TEST_ASSERT( 0 == memcmp( output, result,
845  cipher_get_block_size( &ctx ) ) );
846 
847  cipher_free_ctx( &ctx );
848 }
849 
850 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
851 void test_suite_set_padding( int cipher_id, int pad_mode, int ret )
852 {
853  const cipher_info_t *cipher_info;
854  cipher_context_t ctx;
855 
856  cipher_info = cipher_info_from_type( cipher_id );
857  TEST_ASSERT( NULL != cipher_info );
858  TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
859 
860  TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
861 
862  TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
863 }
864 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
865 
866 #ifdef POLARSSL_CIPHER_MODE_CBC
867 void test_suite_check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
868 {
869  cipher_info_t cipher_info;
870  cipher_context_t ctx;
871  unsigned char input[16];
872  size_t ilen, dlen;
873 
874  /* build a fake context just for getting access to get_padding */
875  memset( &ctx, 0, sizeof( ctx ) );
876  cipher_info.mode = POLARSSL_MODE_CBC;
877  ctx.cipher_info = &cipher_info;
878 
879  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
880 
881  ilen = unhexify( input, input_str );
882 
883  TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
884  if( 0 == ret )
885  TEST_ASSERT( dlen == (size_t) dlen_check );
886 }
887 #endif /* POLARSSL_CIPHER_MODE_CBC */
888 
889 #ifdef POLARSSL_SELF_TEST
890 void test_suite_cipher_selftest()
891 {
892  TEST_ASSERT( cipher_self_test( 0 ) == 0 );
893 }
894 #endif /* POLARSSL_SELF_TEST */
895 
896 
897 #endif /* POLARSSL_CIPHER_C */
898 
899 
900 int dep_check( char *str )
901 {
902  if( str == NULL )
903  return( 1 );
904 
905  if( strcmp( str, "POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
906  {
907 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
908  return( 0 );
909 #else
910  return( 1 );
911 #endif
912  }
913  if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
914  {
915 #if defined(POLARSSL_CIPHER_MODE_CBC)
916  return( 0 );
917 #else
918  return( 1 );
919 #endif
920  }
921  if( strcmp( str, "POLARSSL_CIPHER_MODE_CTR" ) == 0 )
922  {
923 #if defined(POLARSSL_CIPHER_MODE_CTR)
924  return( 0 );
925 #else
926  return( 1 );
927 #endif
928  }
929  if( strcmp( str, "POLARSSL_CAMELLIA_C" ) == 0 )
930  {
931 #if defined(POLARSSL_CAMELLIA_C)
932  return( 0 );
933 #else
934  return( 1 );
935 #endif
936  }
937  if( strcmp( str, "POLARSSL_CIPHER_MODE_CFB" ) == 0 )
938  {
939 #if defined(POLARSSL_CIPHER_MODE_CFB)
940  return( 0 );
941 #else
942  return( 1 );
943 #endif
944  }
945 
946 
947  return( 1 );
948 }
949 
950 int dispatch_test(int cnt, char *params[50])
951 {
952  int ret;
953  ((void) cnt);
954  ((void) params);
955 
956 #if defined(TEST_SUITE_ACTIVE)
957  if( strcmp( params[0], "enc_dec_buf" ) == 0 )
958  {
959 
960  int param1;
961  char *param2 = params[2];
962  int param3;
963  int param4;
964  int param5;
965 
966  if( cnt != 6 )
967  {
968  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
969  return( 2 );
970  }
971 
972  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
973  if( verify_string( &param2 ) != 0 ) return( 2 );
974  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
975  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
976  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
977 
978  test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
979  return ( 0 );
980 
981  return ( 3 );
982  }
983  else
984  if( strcmp( params[0], "enc_fail" ) == 0 )
985  {
986 
987  int param1;
988  int param2;
989  int param3;
990  int param4;
991  int param5;
992 
993  if( cnt != 6 )
994  {
995  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
996  return( 2 );
997  }
998 
999  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1000  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1001  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1002  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1003  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1004 
1005  test_suite_enc_fail( param1, param2, param3, param4, param5 );
1006  return ( 0 );
1007 
1008  return ( 3 );
1009  }
1010  else
1011  if( strcmp( params[0], "dec_empty_buf" ) == 0 )
1012  {
1013 
1014 
1015  if( cnt != 1 )
1016  {
1017  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1018  return( 2 );
1019  }
1020 
1021 
1022  test_suite_dec_empty_buf( );
1023  return ( 0 );
1024 
1025  return ( 3 );
1026  }
1027  else
1028  if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
1029  {
1030 
1031  int param1;
1032  int param2;
1033  int param3;
1034  int param4;
1035 
1036  if( cnt != 5 )
1037  {
1038  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1039  return( 2 );
1040  }
1041 
1042  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1043  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1044  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1045  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1046 
1047  test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1048  return ( 0 );
1049 
1050  return ( 3 );
1051  }
1052  else
1053  if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
1054  {
1055 
1056  int param1;
1057  int param2;
1058  char *param3 = params[3];
1059  char *param4 = params[4];
1060  char *param5 = params[5];
1061  char *param6 = params[6];
1062  char *param7 = params[7];
1063  char *param8 = params[8];
1064  int param9;
1065  int param10;
1066 
1067  if( cnt != 11 )
1068  {
1069  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1070  return( 2 );
1071  }
1072 
1073  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1074  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1075  if( verify_string( &param3 ) != 0 ) return( 2 );
1076  if( verify_string( &param4 ) != 0 ) return( 2 );
1077  if( verify_string( &param5 ) != 0 ) return( 2 );
1078  if( verify_string( &param6 ) != 0 ) return( 2 );
1079  if( verify_string( &param7 ) != 0 ) return( 2 );
1080  if( verify_string( &param8 ) != 0 ) return( 2 );
1081  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1082  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1083 
1084  test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1085  return ( 0 );
1086 
1087  return ( 3 );
1088  }
1089  else
1090  if( strcmp( params[0], "test_vec_ecb" ) == 0 )
1091  {
1092 
1093  int param1;
1094  int param2;
1095  char *param3 = params[3];
1096  char *param4 = params[4];
1097  char *param5 = params[5];
1098  int param6;
1099 
1100  if( cnt != 7 )
1101  {
1102  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1103  return( 2 );
1104  }
1105 
1106  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1107  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1108  if( verify_string( &param3 ) != 0 ) return( 2 );
1109  if( verify_string( &param4 ) != 0 ) return( 2 );
1110  if( verify_string( &param5 ) != 0 ) return( 2 );
1111  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1112 
1113  test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1114  return ( 0 );
1115 
1116  return ( 3 );
1117  }
1118  else
1119  if( strcmp( params[0], "set_padding" ) == 0 )
1120  {
1121  #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1122 
1123  int param1;
1124  int param2;
1125  int param3;
1126 
1127  if( cnt != 4 )
1128  {
1129  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1130  return( 2 );
1131  }
1132 
1133  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1134  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1135  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1136 
1137  test_suite_set_padding( param1, param2, param3 );
1138  return ( 0 );
1139  #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1140 
1141  return ( 3 );
1142  }
1143  else
1144  if( strcmp( params[0], "check_padding" ) == 0 )
1145  {
1146  #ifdef POLARSSL_CIPHER_MODE_CBC
1147 
1148  int param1;
1149  char *param2 = params[2];
1150  int param3;
1151  int param4;
1152 
1153  if( cnt != 5 )
1154  {
1155  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1156  return( 2 );
1157  }
1158 
1159  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1160  if( verify_string( &param2 ) != 0 ) return( 2 );
1161  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1162  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1163 
1164  test_suite_check_padding( param1, param2, param3, param4 );
1165  return ( 0 );
1166  #endif /* POLARSSL_CIPHER_MODE_CBC */
1167 
1168  return ( 3 );
1169  }
1170  else
1171  if( strcmp( params[0], "cipher_selftest" ) == 0 )
1172  {
1173  #ifdef POLARSSL_SELF_TEST
1174 
1175 
1176  if( cnt != 1 )
1177  {
1178  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1179  return( 2 );
1180  }
1181 
1182 
1183  test_suite_cipher_selftest( );
1184  return ( 0 );
1185  #endif /* POLARSSL_SELF_TEST */
1186 
1187  return ( 3 );
1188  }
1189  else
1190 
1191  {
1192  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1193  fflush( stdout );
1194  return( 1 );
1195  }
1196 #else
1197  return( 3 );
1198 #endif
1199  return( ret );
1200 }
1201 
1202 int get_line( FILE *f, char *buf, size_t len )
1203 {
1204  char *ret;
1205 
1206  ret = fgets( buf, len, f );
1207  if( ret == NULL )
1208  return( -1 );
1209 
1210  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1211  buf[strlen(buf) - 1] = '\0';
1212  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1213  buf[strlen(buf) - 1] = '\0';
1214 
1215  return( 0 );
1216 }
1217 
1218 int parse_arguments( char *buf, size_t len, char *params[50] )
1219 {
1220  int cnt = 0, i;
1221  char *cur = buf;
1222  char *p = buf, *q;
1223 
1224  params[cnt++] = cur;
1225 
1226  while( *p != '\0' && p < buf + len )
1227  {
1228  if( *p == '\\' )
1229  {
1230  *p++;
1231  *p++;
1232  continue;
1233  }
1234  if( *p == ':' )
1235  {
1236  if( p + 1 < buf + len )
1237  {
1238  cur = p + 1;
1239  params[cnt++] = cur;
1240  }
1241  *p = '\0';
1242  }
1243 
1244  *p++;
1245  }
1246 
1247  // Replace newlines, question marks and colons in strings
1248  for( i = 0; i < cnt; i++ )
1249  {
1250  p = params[i];
1251  q = params[i];
1252 
1253  while( *p != '\0' )
1254  {
1255  if( *p == '\\' && *(p + 1) == 'n' )
1256  {
1257  p += 2;
1258  *(q++) = '\n';
1259  }
1260  else if( *p == '\\' && *(p + 1) == ':' )
1261  {
1262  p += 2;
1263  *(q++) = ':';
1264  }
1265  else if( *p == '\\' && *(p + 1) == '?' )
1266  {
1267  p += 2;
1268  *(q++) = '?';
1269  }
1270  else
1271  *(q++) = *(p++);
1272  }
1273  *q = '\0';
1274  }
1275 
1276  return( cnt );
1277 }
1278 
1279 int main()
1280 {
1281  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1282  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_cipher.camellia.data";
1283  FILE *file;
1284  char buf[5000];
1285  char *params[50];
1286 
1287 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1288  unsigned char alloc_buf[1000000];
1289  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1290 #endif
1291 
1292  file = fopen( filename, "r" );
1293  if( file == NULL )
1294  {
1295  fprintf( stderr, "Failed to open\n" );
1296  return( 1 );
1297  }
1298 
1299  while( !feof( file ) )
1300  {
1301  int skip = 0;
1302 
1303  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1304  break;
1305  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1306  fprintf( stdout, " " );
1307  for( i = strlen( buf ) + 1; i < 67; i++ )
1308  fprintf( stdout, "." );
1309  fprintf( stdout, " " );
1310  fflush( stdout );
1311 
1312  total_tests++;
1313 
1314  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1315  break;
1316  cnt = parse_arguments( buf, strlen(buf), params );
1317 
1318  if( strcmp( params[0], "depends_on" ) == 0 )
1319  {
1320  for( i = 1; i < cnt; i++ )
1321  if( dep_check( params[i] ) != 0 )
1322  skip = 1;
1323 
1324  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1325  break;
1326  cnt = parse_arguments( buf, strlen(buf), params );
1327  }
1328 
1329  if( skip == 0 )
1330  {
1331  test_errors = 0;
1332  ret = dispatch_test( cnt, params );
1333  }
1334 
1335  if( skip == 1 || ret == 3 )
1336  {
1337  total_skipped++;
1338  fprintf( stdout, "----\n" );
1339  fflush( stdout );
1340  }
1341  else if( ret == 0 && test_errors == 0 )
1342  {
1343  fprintf( stdout, "PASS\n" );
1344  fflush( stdout );
1345  }
1346  else if( ret == 2 )
1347  {
1348  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1349  fclose(file);
1350  exit( 2 );
1351  }
1352  else
1353  total_errors++;
1354 
1355  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1356  break;
1357  if( strlen(buf) != 0 )
1358  {
1359  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1360  return( 1 );
1361  }
1362  }
1363  fclose(file);
1364 
1365  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1366  if( total_errors == 0 )
1367  fprintf( stdout, "PASSED" );
1368  else
1369  fprintf( stdout, "FAILED" );
1370 
1371  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1372  total_tests - total_errors, total_tests, total_skipped );
1373 
1374 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1375 #if defined(POLARSSL_MEMORY_DEBUG)
1376  memory_buffer_alloc_status();
1377 #endif
1378  memory_buffer_alloc_free();
1379 #endif
1380 
1381  return( total_errors != 0 );
1382 }
1383 
1384 
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
Memory allocation layer.
Generic cipher context.
Definition: cipher.h:239
uint32_t t_uint
Definition: bignum.h:149
Info structure for the pseudo random function.
int cipher_write_tag(cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
Write tag for AEAD ciphers.
int s
Definition: bignum.h:173
Cipher information.
Definition: cipher.h:207
zero padding (not reversible!)
Definition: cipher.h:136
const cipher_info_t * cipher_info_from_type(const cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
static unsigned int cipher_get_block_size(const cipher_context_t *ctx)
Returns the block size of the given cipher.
Definition: cipher.h:348
const cipher_info_t * cipher_info_from_string(const char *cipher_name)
Returns the cipher information structure associated with the given cipher name.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int(* get_padding)(unsigned char *input, size_t ilen, size_t *data_len)
Definition: cipher.h:251
Configuration options (set of defines)
MPI structure.
Definition: bignum.h:171
ISO/IEC 7816-4 padding.
Definition: cipher.h:134
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
Multi-precision integer library.
int dep_check(char *str)
const cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:241
#define TEST_ASSERT(TEST)
int cipher_free_ctx(cipher_context_t *ctx)
Free the cipher-specific context of ctx.
int cipher_update_ad(cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
Add additional data (for AEAD ciphers).
int cipher_set_iv(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
int cipher_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
static int unhexify(unsigned char *obuf, const char *ibuf)
static int test_errors
#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
Decryption of block requires a full block.
Definition: cipher.h:57
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
Generic cipher wrapper.
int parse_arguments(char *buf, size_t len, char *params[50])
#define PUT_UINT32_BE(n, b, i)
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
int cipher_set_padding_mode(cipher_context_t *ctx, cipher_padding_t mode)
Set padding mode, for cipher modes that use padding.
cipher_mode_t mode
Cipher mode (e.g.
Definition: cipher.h:212
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
int cipher_init_ctx(cipher_context_t *ctx, const cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
int cipher_setkey(cipher_context_t *ctx, const unsigned char *key, int key_length, const operation_t operation)
Set the key to use with the given context.
t_uint * p
Definition: bignum.h:175
int verify_string(char **str)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int dispatch_test(int cnt, char *params[50])
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
never pad (full blocks only)
Definition: cipher.h:137
size_t n
Definition: bignum.h:174
Galois/Counter mode for 128-bit block ciphers.
unsigned char * buf
ANSI X.923 padding.
Definition: cipher.h:135
int verify_int(char *str, int *value)
int cipher_self_test(int verbose)
Checkup routine.
int cipher_check_tag(cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers.
int get_line(FILE *f, char *buf, size_t len)