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