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