PolarSSL v1.3.2
test_suite_cipher.gcm.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_AES_256_GCM" ) == 0 )
365  {
366  *value = ( POLARSSL_CIPHER_AES_256_GCM );
367  return( 0 );
368  }
369  if( strcmp( str, "POLARSSL_CIPHER_AES_192_GCM" ) == 0 )
370  {
371  *value = ( POLARSSL_CIPHER_AES_192_GCM );
372  return( 0 );
373  }
374  if( strcmp( str, "POLARSSL_ERR_CIPHER_AUTH_FAILED" ) == 0 )
375  {
376  *value = ( POLARSSL_ERR_CIPHER_AUTH_FAILED );
377  return( 0 );
378  }
379  if( strcmp( str, "POLARSSL_CIPHER_AES_128_GCM" ) == 0 )
380  {
381  *value = ( POLARSSL_CIPHER_AES_128_GCM );
382  return( 0 );
383  }
384  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_256_GCM" ) == 0 )
385  {
387  return( 0 );
388  }
389  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_192_GCM" ) == 0 )
390  {
392  return( 0 );
393  }
394  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_GCM" ) == 0 )
395  {
397  return( 0 );
398  }
399  if( strcmp( str, "-1" ) == 0 )
400  {
401  *value = ( -1 );
402  return( 0 );
403  }
404 
405 
406  printf( "Expected integer for parameter and got: %s\n", str );
407  return( -1 );
408 }
409 
410 void test_suite_enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
411  int length_val, int pad_mode )
412 {
413  size_t length = length_val, outlen, total_len, i;
414  unsigned char key[32];
415  unsigned char iv[16];
416  unsigned char ad[13];
417  unsigned char tag[16];
418  unsigned char inbuf[64];
419  unsigned char encbuf[64];
420  unsigned char decbuf[64];
421 
422  const cipher_info_t *cipher_info;
423  cipher_context_t ctx_dec;
424  cipher_context_t ctx_enc;
425 
426  /*
427  * Prepare contexts
428  */
429  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
430  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
431 
432  memset( key, 0x2a, sizeof( key ) );
433 
434  /* Check and get info structures */
435  cipher_info = cipher_info_from_type( cipher_id );
436  TEST_ASSERT( NULL != cipher_info );
437  TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
438 
439  /* Initialise enc and dec contexts */
440  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
441  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
442 
443  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
444  TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
445 
446 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
447  if( -1 != pad_mode )
448  {
449  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
450  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
451  }
452 #else
453  (void) pad_mode;
454 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
455 
456  /*
457  * Do a few encode/decode cycles
458  */
459  for( i = 0; i < 3; i++ )
460  {
461  memset( iv , 0x00 + i, sizeof( iv ) );
462  memset( ad, 0x10 + i, sizeof( ad ) );
463  memset( inbuf, 0x20 + i, sizeof( inbuf ) );
464 
465  memset( encbuf, 0, sizeof( encbuf ) );
466  memset( decbuf, 0, sizeof( decbuf ) );
467  memset( tag, 0, sizeof( tag ) );
468 
469  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
470  TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
471 
472  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
473  TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
474 
475 #if defined(POLARSSL_CIPHER_MODE_AEAD)
476  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
477  TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
478 #endif /* POLARSSL_CIPHER_MODE_AEAD */
479 
480  /* encode length number of bytes from inbuf */
481  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
482  total_len = outlen;
483 
484  TEST_ASSERT( total_len == length ||
485  ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
486  total_len < length &&
487  total_len + cipher_get_block_size( &ctx_enc ) > length ) );
488 
489  TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
490  total_len += outlen;
491 
492 #if defined(POLARSSL_CIPHER_MODE_AEAD)
493  TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
494 #endif /* POLARSSL_CIPHER_MODE_AEAD */
495 
496  TEST_ASSERT( total_len == length ||
497  ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
498  total_len > length &&
499  total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
500 
501  /* decode the previously encoded string */
502  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
503  total_len = outlen;
504 
505  TEST_ASSERT( total_len == length ||
506  ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
507  total_len < length &&
508  total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
509 
510  TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
511  total_len += outlen;
512 
513 #if defined(POLARSSL_CIPHER_MODE_AEAD)
514  TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
515 #endif /* POLARSSL_CIPHER_MODE_AEAD */
516 
517  /* check result */
518  TEST_ASSERT( total_len == length );
519  TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
520  }
521 
522  /*
523  * Done
524  */
525  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
526  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
527 }
528 
529 void test_suite_enc_fail( int cipher_id, int pad_mode, int key_len,
530  int length_val, int ret )
531 {
532  size_t length = length_val;
533  unsigned char key[32];
534  unsigned char iv[16];
535 
536  const cipher_info_t *cipher_info;
537  cipher_context_t ctx;
538 
539  unsigned char inbuf[64];
540  unsigned char encbuf[64];
541 
542  size_t outlen = 0;
543 
544  memset( key, 0, 32 );
545  memset( iv , 0, 16 );
546 
547  memset( &ctx, 0, sizeof( ctx ) );
548 
549  memset( inbuf, 5, 64 );
550  memset( encbuf, 0, 64 );
551 
552  /* Check and get info structures */
553  cipher_info = cipher_info_from_type( cipher_id );
554  TEST_ASSERT( NULL != cipher_info );
555 
556  /* Initialise context */
557  TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
558  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
559 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
560  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
561 #else
562  (void) pad_mode;
563 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
564  TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
565  TEST_ASSERT( 0 == cipher_reset( &ctx ) );
566 #if defined(POLARSSL_CIPHER_MODE_AEAD)
567  TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
568 #endif /* POLARSSL_CIPHER_MODE_AEAD */
569 
570  /* encode length number of bytes from inbuf */
571  TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
572  TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
573 
574  /* done */
575  TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
576 }
577 
578 void test_suite_dec_empty_buf()
579 {
580  unsigned char key[32];
581  unsigned char iv[16];
582 
583  cipher_context_t ctx_dec;
584  const cipher_info_t *cipher_info;
585 
586  unsigned char encbuf[64];
587  unsigned char decbuf[64];
588 
589  size_t outlen = 0;
590 
591  memset( key, 0, 32 );
592  memset( iv , 0, 16 );
593 
594  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
595 
596  memset( encbuf, 0, 64 );
597  memset( decbuf, 0, 64 );
598 
599  /* Initialise context */
601  TEST_ASSERT( NULL != cipher_info);
602 
603  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
604 
605  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
606 
607  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
608 
609  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
610 
611 #if defined(POLARSSL_CIPHER_MODE_AEAD)
612  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
613 #endif /* POLARSSL_CIPHER_MODE_AEAD */
614 
615  /* decode 0-byte string */
616  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
617  TEST_ASSERT( 0 == outlen );
619  &ctx_dec, decbuf + outlen, &outlen ) );
620  TEST_ASSERT( 0 == outlen );
621 
622  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
623 }
624 
625 void test_suite_enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
626  int second_length_val )
627 {
628  size_t first_length = first_length_val;
629  size_t second_length = second_length_val;
630  size_t length = first_length + second_length;
631  unsigned char key[32];
632  unsigned char iv[16];
633 
634  cipher_context_t ctx_dec;
635  cipher_context_t ctx_enc;
636  const cipher_info_t *cipher_info;
637 
638  unsigned char inbuf[64];
639  unsigned char encbuf[64];
640  unsigned char decbuf[64];
641 
642  size_t outlen = 0;
643  size_t totaloutlen = 0;
644 
645  memset( key, 0, 32 );
646  memset( iv , 0, 16 );
647 
648  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
649  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
650 
651  memset( inbuf, 5, 64 );
652  memset( encbuf, 0, 64 );
653  memset( decbuf, 0, 64 );
654 
655  /* Initialise enc and dec contexts */
656  cipher_info = cipher_info_from_type( cipher_id );
657  TEST_ASSERT( NULL != cipher_info);
658 
659  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
660  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
661 
662  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
663  TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
664 
665  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
666  TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
667 
668  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
669  TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
670 
671 #if defined(POLARSSL_CIPHER_MODE_AEAD)
672  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
673  TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
674 #endif /* POLARSSL_CIPHER_MODE_AEAD */
675 
676  /* encode length number of bytes from inbuf */
677  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
678  totaloutlen = outlen;
679  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
680  totaloutlen += outlen;
681  TEST_ASSERT( totaloutlen == length ||
682  ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
683  totaloutlen < length &&
684  totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
685 
686  TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
687  totaloutlen += outlen;
688  TEST_ASSERT( totaloutlen == length ||
689  ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
690  totaloutlen > length &&
691  totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
692 
693  /* decode the previously encoded string */
694  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
695  totaloutlen = outlen;
696 
697  TEST_ASSERT( totaloutlen == length ||
698  ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
699  totaloutlen < length &&
700  totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
701 
702  TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
703  totaloutlen += outlen;
704 
705  TEST_ASSERT( totaloutlen == length );
706 
707  TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
708 
709  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
710  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
711 }
712 
713 void test_suite_decrypt_test_vec( int cipher_id, int pad_mode,
714  char *hex_key, char *hex_iv,
715  char *hex_cipher, char *hex_clear,
716  char *hex_ad, char *hex_tag,
717  int finish_result, int tag_result )
718 {
719  unsigned char key[50];
720  unsigned char iv[50];
721  unsigned char cipher[200];
722  unsigned char clear[200];
723  unsigned char ad[200];
724  unsigned char tag[20];
725  size_t key_len, iv_len, cipher_len, clear_len;
726 #if defined(POLARSSL_CIPHER_MODE_AEAD)
727  size_t ad_len, tag_len;
728 #endif
729  cipher_context_t ctx;
730  unsigned char output[200];
731  size_t outlen, total_len;
732 
733  memset( key, 0x00, sizeof( key ) );
734  memset( iv, 0x00, sizeof( iv ) );
735  memset( cipher, 0x00, sizeof( cipher ) );
736  memset( clear, 0x00, sizeof( clear ) );
737  memset( ad, 0x00, sizeof( ad ) );
738  memset( tag, 0x00, sizeof( tag ) );
739  memset( output, 0x00, sizeof( output ) );
740 
741  key_len = unhexify( key, hex_key );
742  iv_len = unhexify( iv, hex_iv );
743  cipher_len = unhexify( cipher, hex_cipher );
744  clear_len = unhexify( clear, hex_clear );
745 #if defined(POLARSSL_CIPHER_MODE_AEAD)
746  ad_len = unhexify( ad, hex_ad );
747  tag_len = unhexify( tag, hex_tag );
748 #else
749  ((void) hex_ad);
750  ((void) hex_tag);
751 #endif
752 
753  /* Prepare context */
754  TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
755  cipher_info_from_type( cipher_id ) ) );
756  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
757 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
758  if( pad_mode != -1 )
759  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
760 #else
761  (void) pad_mode;
762 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
763  TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
764  TEST_ASSERT( 0 == cipher_reset( &ctx ) );
765 #if defined(POLARSSL_CIPHER_MODE_AEAD)
766  TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
767 #endif /* POLARSSL_CIPHER_MODE_AEAD */
768 
769  /* decode buffer and check tag */
770  total_len = 0;
771  TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
772  total_len += outlen;
773  TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
774  &outlen ) );
775  total_len += outlen;
776 #if defined(POLARSSL_CIPHER_MODE_AEAD)
777  TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
778 #endif /* POLARSSL_CIPHER_MODE_AEAD */
779 
780  /* check plaintext only if everything went fine */
781  if( 0 == finish_result && 0 == tag_result )
782  {
783  TEST_ASSERT( total_len == clear_len );
784  TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
785  }
786 
787  cipher_free_ctx( &ctx );
788 }
789 
790 void test_suite_test_vec_ecb( int cipher_id, int operation, char *hex_key,
791  char *hex_input, char *hex_result,
792  int finish_result )
793 {
794  unsigned char key[50];
795  unsigned char input[16];
796  unsigned char result[16];
797  size_t key_len;
798  cipher_context_t ctx;
799  unsigned char output[32];
800  size_t outlen;
801 
802  memset( key, 0x00, sizeof( key ) );
803  memset( input, 0x00, sizeof( input ) );
804  memset( result, 0x00, sizeof( result ) );
805  memset( output, 0x00, sizeof( output ) );
806 
807  /* Prepare context */
808  TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
809  cipher_info_from_type( cipher_id ) ) );
810 
811  key_len = unhexify( key, hex_key );
812  TEST_ASSERT( unhexify( input, hex_input ) ==
813  (int) cipher_get_block_size( &ctx ) );
814  TEST_ASSERT( unhexify( result, hex_result ) ==
815  (int) cipher_get_block_size( &ctx ) );
816 
817  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
818 
819  TEST_ASSERT( 0 == cipher_update( &ctx, input,
820  cipher_get_block_size( &ctx ),
821  output, &outlen ) );
822  TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
823  TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
824  &outlen ) );
825  TEST_ASSERT( 0 == outlen );
826 
827  /* check plaintext only if everything went fine */
828  if( 0 == finish_result )
829  TEST_ASSERT( 0 == memcmp( output, result,
830  cipher_get_block_size( &ctx ) ) );
831 
832  cipher_free_ctx( &ctx );
833 }
834 
835 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
836 void test_suite_set_padding( int cipher_id, int pad_mode, int ret )
837 {
838  const cipher_info_t *cipher_info;
839  cipher_context_t ctx;
840 
841  cipher_info = cipher_info_from_type( cipher_id );
842  TEST_ASSERT( NULL != cipher_info );
843  TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
844 
845  TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
846 
847  TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
848 }
849 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
850 
851 #ifdef POLARSSL_CIPHER_MODE_CBC
852 void test_suite_check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
853 {
854  cipher_info_t cipher_info;
855  cipher_context_t ctx;
856  unsigned char input[16];
857  size_t ilen, dlen;
858 
859  /* build a fake context just for getting access to get_padding */
860  memset( &ctx, 0, sizeof( ctx ) );
861  cipher_info.mode = POLARSSL_MODE_CBC;
862  ctx.cipher_info = &cipher_info;
863 
864  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
865 
866  ilen = unhexify( input, input_str );
867 
868  TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
869  if( 0 == ret )
870  TEST_ASSERT( dlen == (size_t) dlen_check );
871 }
872 #endif /* POLARSSL_CIPHER_MODE_CBC */
873 
874 #ifdef POLARSSL_SELF_TEST
875 void test_suite_cipher_selftest()
876 {
877  TEST_ASSERT( cipher_self_test( 0 ) == 0 );
878 }
879 #endif /* POLARSSL_SELF_TEST */
880 
881 
882 #endif /* POLARSSL_CIPHER_C */
883 
884 
885 int dep_check( char *str )
886 {
887  if( str == NULL )
888  return( 1 );
889 
890  if( strcmp( str, "POLARSSL_CAMELLIA_C" ) == 0 )
891  {
892 #if defined(POLARSSL_CAMELLIA_C)
893  return( 0 );
894 #else
895  return( 1 );
896 #endif
897  }
898  if( strcmp( str, "POLARSSL_GCM_C" ) == 0 )
899  {
900 #if defined(POLARSSL_GCM_C)
901  return( 0 );
902 #else
903  return( 1 );
904 #endif
905  }
906  if( strcmp( str, "POLARSSL_AES_C" ) == 0 )
907  {
908 #if defined(POLARSSL_AES_C)
909  return( 0 );
910 #else
911  return( 1 );
912 #endif
913  }
914 
915 
916  return( 1 );
917 }
918 
919 int dispatch_test(int cnt, char *params[50])
920 {
921  int ret;
922  ((void) cnt);
923  ((void) params);
924 
925 #if defined(TEST_SUITE_ACTIVE)
926  if( strcmp( params[0], "enc_dec_buf" ) == 0 )
927  {
928 
929  int param1;
930  char *param2 = params[2];
931  int param3;
932  int param4;
933  int param5;
934 
935  if( cnt != 6 )
936  {
937  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
938  return( 2 );
939  }
940 
941  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
942  if( verify_string( &param2 ) != 0 ) return( 2 );
943  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
944  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
945  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
946 
947  test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
948  return ( 0 );
949 
950  return ( 3 );
951  }
952  else
953  if( strcmp( params[0], "enc_fail" ) == 0 )
954  {
955 
956  int param1;
957  int param2;
958  int param3;
959  int param4;
960  int param5;
961 
962  if( cnt != 6 )
963  {
964  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
965  return( 2 );
966  }
967 
968  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
969  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
970  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
971  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
972  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
973 
974  test_suite_enc_fail( param1, param2, param3, param4, param5 );
975  return ( 0 );
976 
977  return ( 3 );
978  }
979  else
980  if( strcmp( params[0], "dec_empty_buf" ) == 0 )
981  {
982 
983 
984  if( cnt != 1 )
985  {
986  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
987  return( 2 );
988  }
989 
990 
991  test_suite_dec_empty_buf( );
992  return ( 0 );
993 
994  return ( 3 );
995  }
996  else
997  if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
998  {
999 
1000  int param1;
1001  int param2;
1002  int param3;
1003  int param4;
1004 
1005  if( cnt != 5 )
1006  {
1007  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1008  return( 2 );
1009  }
1010 
1011  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1012  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1013  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1014  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1015 
1016  test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1017  return ( 0 );
1018 
1019  return ( 3 );
1020  }
1021  else
1022  if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
1023  {
1024 
1025  int param1;
1026  int param2;
1027  char *param3 = params[3];
1028  char *param4 = params[4];
1029  char *param5 = params[5];
1030  char *param6 = params[6];
1031  char *param7 = params[7];
1032  char *param8 = params[8];
1033  int param9;
1034  int param10;
1035 
1036  if( cnt != 11 )
1037  {
1038  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
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_string( &param3 ) != 0 ) return( 2 );
1045  if( verify_string( &param4 ) != 0 ) return( 2 );
1046  if( verify_string( &param5 ) != 0 ) return( 2 );
1047  if( verify_string( &param6 ) != 0 ) return( 2 );
1048  if( verify_string( &param7 ) != 0 ) return( 2 );
1049  if( verify_string( &param8 ) != 0 ) return( 2 );
1050  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1051  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1052 
1053  test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1054  return ( 0 );
1055 
1056  return ( 3 );
1057  }
1058  else
1059  if( strcmp( params[0], "test_vec_ecb" ) == 0 )
1060  {
1061 
1062  int param1;
1063  int param2;
1064  char *param3 = params[3];
1065  char *param4 = params[4];
1066  char *param5 = params[5];
1067  int param6;
1068 
1069  if( cnt != 7 )
1070  {
1071  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1072  return( 2 );
1073  }
1074 
1075  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1076  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1077  if( verify_string( &param3 ) != 0 ) return( 2 );
1078  if( verify_string( &param4 ) != 0 ) return( 2 );
1079  if( verify_string( &param5 ) != 0 ) return( 2 );
1080  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1081 
1082  test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1083  return ( 0 );
1084 
1085  return ( 3 );
1086  }
1087  else
1088  if( strcmp( params[0], "set_padding" ) == 0 )
1089  {
1090  #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1091 
1092  int param1;
1093  int param2;
1094  int param3;
1095 
1096  if( cnt != 4 )
1097  {
1098  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1099  return( 2 );
1100  }
1101 
1102  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1103  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1104  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1105 
1106  test_suite_set_padding( param1, param2, param3 );
1107  return ( 0 );
1108  #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1109 
1110  return ( 3 );
1111  }
1112  else
1113  if( strcmp( params[0], "check_padding" ) == 0 )
1114  {
1115  #ifdef POLARSSL_CIPHER_MODE_CBC
1116 
1117  int param1;
1118  char *param2 = params[2];
1119  int param3;
1120  int param4;
1121 
1122  if( cnt != 5 )
1123  {
1124  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1125  return( 2 );
1126  }
1127 
1128  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1129  if( verify_string( &param2 ) != 0 ) return( 2 );
1130  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1131  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1132 
1133  test_suite_check_padding( param1, param2, param3, param4 );
1134  return ( 0 );
1135  #endif /* POLARSSL_CIPHER_MODE_CBC */
1136 
1137  return ( 3 );
1138  }
1139  else
1140  if( strcmp( params[0], "cipher_selftest" ) == 0 )
1141  {
1142  #ifdef POLARSSL_SELF_TEST
1143 
1144 
1145  if( cnt != 1 )
1146  {
1147  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1148  return( 2 );
1149  }
1150 
1151 
1152  test_suite_cipher_selftest( );
1153  return ( 0 );
1154  #endif /* POLARSSL_SELF_TEST */
1155 
1156  return ( 3 );
1157  }
1158  else
1159 
1160  {
1161  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1162  fflush( stdout );
1163  return( 1 );
1164  }
1165 #else
1166  return( 3 );
1167 #endif
1168  return( ret );
1169 }
1170 
1171 int get_line( FILE *f, char *buf, size_t len )
1172 {
1173  char *ret;
1174 
1175  ret = fgets( buf, len, f );
1176  if( ret == NULL )
1177  return( -1 );
1178 
1179  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1180  buf[strlen(buf) - 1] = '\0';
1181  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1182  buf[strlen(buf) - 1] = '\0';
1183 
1184  return( 0 );
1185 }
1186 
1187 int parse_arguments( char *buf, size_t len, char *params[50] )
1188 {
1189  int cnt = 0, i;
1190  char *cur = buf;
1191  char *p = buf, *q;
1192 
1193  params[cnt++] = cur;
1194 
1195  while( *p != '\0' && p < buf + len )
1196  {
1197  if( *p == '\\' )
1198  {
1199  *p++;
1200  *p++;
1201  continue;
1202  }
1203  if( *p == ':' )
1204  {
1205  if( p + 1 < buf + len )
1206  {
1207  cur = p + 1;
1208  params[cnt++] = cur;
1209  }
1210  *p = '\0';
1211  }
1212 
1213  *p++;
1214  }
1215 
1216  // Replace newlines, question marks and colons in strings
1217  for( i = 0; i < cnt; i++ )
1218  {
1219  p = params[i];
1220  q = params[i];
1221 
1222  while( *p != '\0' )
1223  {
1224  if( *p == '\\' && *(p + 1) == 'n' )
1225  {
1226  p += 2;
1227  *(q++) = '\n';
1228  }
1229  else if( *p == '\\' && *(p + 1) == ':' )
1230  {
1231  p += 2;
1232  *(q++) = ':';
1233  }
1234  else if( *p == '\\' && *(p + 1) == '?' )
1235  {
1236  p += 2;
1237  *(q++) = '?';
1238  }
1239  else
1240  *(q++) = *(p++);
1241  }
1242  *q = '\0';
1243  }
1244 
1245  return( cnt );
1246 }
1247 
1248 int main()
1249 {
1250  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1251  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_cipher.gcm.data";
1252  FILE *file;
1253  char buf[5000];
1254  char *params[50];
1255 
1256 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1257  unsigned char alloc_buf[1000000];
1258  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1259 #endif
1260 
1261  file = fopen( filename, "r" );
1262  if( file == NULL )
1263  {
1264  fprintf( stderr, "Failed to open\n" );
1265  return( 1 );
1266  }
1267 
1268  while( !feof( file ) )
1269  {
1270  int skip = 0;
1271 
1272  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1273  break;
1274  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1275  fprintf( stdout, " " );
1276  for( i = strlen( buf ) + 1; i < 67; i++ )
1277  fprintf( stdout, "." );
1278  fprintf( stdout, " " );
1279  fflush( stdout );
1280 
1281  total_tests++;
1282 
1283  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1284  break;
1285  cnt = parse_arguments( buf, strlen(buf), params );
1286 
1287  if( strcmp( params[0], "depends_on" ) == 0 )
1288  {
1289  for( i = 1; i < cnt; i++ )
1290  if( dep_check( params[i] ) != 0 )
1291  skip = 1;
1292 
1293  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1294  break;
1295  cnt = parse_arguments( buf, strlen(buf), params );
1296  }
1297 
1298  if( skip == 0 )
1299  {
1300  test_errors = 0;
1301  ret = dispatch_test( cnt, params );
1302  }
1303 
1304  if( skip == 1 || ret == 3 )
1305  {
1306  total_skipped++;
1307  fprintf( stdout, "----\n" );
1308  fflush( stdout );
1309  }
1310  else if( ret == 0 && test_errors == 0 )
1311  {
1312  fprintf( stdout, "PASS\n" );
1313  fflush( stdout );
1314  }
1315  else if( ret == 2 )
1316  {
1317  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1318  fclose(file);
1319  exit( 2 );
1320  }
1321  else
1322  total_errors++;
1323 
1324  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1325  break;
1326  if( strlen(buf) != 0 )
1327  {
1328  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1329  return( 1 );
1330  }
1331  }
1332  fclose(file);
1333 
1334  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1335  if( total_errors == 0 )
1336  fprintf( stdout, "PASSED" );
1337  else
1338  fprintf( stdout, "FAILED" );
1339 
1340  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1341  total_tests - total_errors, total_tests, total_skipped );
1342 
1343 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1344 #if defined(POLARSSL_MEMORY_DEBUG)
1345  memory_buffer_alloc_status();
1346 #endif
1347  memory_buffer_alloc_free();
1348 #endif
1349 
1350  return( total_errors != 0 );
1351 }
1352 
1353 
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
Memory allocation layer.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
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
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_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
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
#define PUT_UINT32_BE(n, b, i)
static int unhexify(unsigned char *obuf, const char *ibuf)
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 test_errors
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
Decryption of block requires a full block.
Definition: cipher.h:57
Generic cipher wrapper.
int parse_arguments(char *buf, size_t len, char *params[50])
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)
int dispatch_test(int cnt, char *params[50])
size_t n
Definition: bignum.h:174
Galois/Counter mode for 128-bit block ciphers.
unsigned char * buf
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int verify_int(char *str, int *value)
int cipher_self_test(int verbose)
Checkup routine.
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 cipher_check_tag(cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers.
#define POLARSSL_ERR_CIPHER_AUTH_FAILED
Authentication failed (for AEAD modes).
Definition: cipher.h:58
int get_line(FILE *f, char *buf, size_t len)