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