PolarSSL v1.3.9
test_suite_cipher.des.c
Go to the documentation of this file.
1 #if !defined(POLARSSL_CONFIG_FILE)
2 #include <polarssl/config.h>
3 #else
4 #include POLARSSL_CONFIG_FILE
5 #endif
6 
7 #ifdef POLARSSL_CIPHER_C
8 
9 #include <polarssl/cipher.h>
10 
11 #if defined(POLARSSL_GCM_C)
12 #include <polarssl/gcm.h>
13 #endif
14 #endif /* POLARSSL_CIPHER_C */
15 
16 
17 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
18 #include "polarssl/memory.h"
19 #endif
20 
21 #if defined(POLARSSL_PLATFORM_C)
22 #include "polarssl/platform.h"
23 #else
24 #define polarssl_malloc malloc
25 #define polarssl_free free
26 #endif
27 
28 #ifdef _MSC_VER
29 #include <basetsd.h>
30 typedef UINT32 uint32_t;
31 #else
32 #include <inttypes.h>
33 #endif
34 
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 /*
40  * 32-bit integer manipulation macros (big endian)
41  */
42 #ifndef GET_UINT32_BE
43 #define GET_UINT32_BE(n,b,i) \
44 { \
45  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
46  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
47  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
48  | ( (uint32_t) (b)[(i) + 3] ); \
49 }
50 #endif
51 
52 #ifndef PUT_UINT32_BE
53 #define PUT_UINT32_BE(n,b,i) \
54 { \
55  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
56  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
57  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
58  (b)[(i) + 3] = (unsigned char) ( (n) ); \
59 }
60 #endif
61 
62 static int unhexify(unsigned char *obuf, const char *ibuf)
63 {
64  unsigned char c, c2;
65  int len = strlen(ibuf) / 2;
66  assert(!(strlen(ibuf) %1)); // must be even number of bytes
67 
68  while (*ibuf != 0)
69  {
70  c = *ibuf++;
71  if( c >= '0' && c <= '9' )
72  c -= '0';
73  else if( c >= 'a' && c <= 'f' )
74  c -= 'a' - 10;
75  else if( c >= 'A' && c <= 'F' )
76  c -= 'A' - 10;
77  else
78  assert( 0 );
79 
80  c2 = *ibuf++;
81  if( c2 >= '0' && c2 <= '9' )
82  c2 -= '0';
83  else if( c2 >= 'a' && c2 <= 'f' )
84  c2 -= 'a' - 10;
85  else if( c2 >= 'A' && c2 <= 'F' )
86  c2 -= 'A' - 10;
87  else
88  assert( 0 );
89 
90  *obuf++ = ( c << 4 ) | c2;
91  }
92 
93  return len;
94 }
95 
96 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
97 {
98  unsigned char l, h;
99 
100  while (len != 0)
101  {
102  h = (*ibuf) / 16;
103  l = (*ibuf) % 16;
104 
105  if( h < 10 )
106  *obuf++ = '0' + h;
107  else
108  *obuf++ = 'a' + h - 10;
109 
110  if( l < 10 )
111  *obuf++ = '0' + l;
112  else
113  *obuf++ = 'a' + l - 10;
114 
115  ++ibuf;
116  len--;
117  }
118 }
119 
127 static unsigned char *zero_alloc( size_t len )
128 {
129  void *p;
130  size_t actual_len = len != 0 ? len : 1;
131 
132  p = polarssl_malloc( actual_len );
133  assert( p != NULL );
134 
135  memset( p, 0x00, actual_len );
136 
137  return( p );
138 }
139 
150 static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
151 {
152  unsigned char *obuf;
153 
154  *olen = strlen(ibuf) / 2;
155 
156  if( *olen == 0 )
157  return( zero_alloc( *olen ) );
158 
159  obuf = polarssl_malloc( *olen );
160  assert( obuf != NULL );
161 
162  (void) unhexify( obuf, ibuf );
163 
164  return( obuf );
165 }
166 
176 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
177 {
178 #if !defined(__OpenBSD__)
179  size_t i;
180 
181  if( rng_state != NULL )
182  rng_state = NULL;
183 
184  for( i = 0; i < len; ++i )
185  output[i] = rand();
186 #else
187  if( rng_state != NULL )
188  rng_state = NULL;
189 
190  arc4random_buf( output, len );
191 #endif /* !OpenBSD */
192 
193  return( 0 );
194 }
195 
201 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
202 {
203  if( rng_state != NULL )
204  rng_state = NULL;
205 
206  memset( output, 0, len );
207 
208  return( 0 );
209 }
210 
211 typedef struct
212 {
213  unsigned char *buf;
214  size_t length;
215 } rnd_buf_info;
216 
228 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
229 {
230  rnd_buf_info *info = (rnd_buf_info *) rng_state;
231  size_t use_len;
232 
233  if( rng_state == NULL )
234  return( rnd_std_rand( NULL, output, len ) );
235 
236  use_len = len;
237  if( len > info->length )
238  use_len = info->length;
239 
240  if( use_len )
241  {
242  memcpy( output, info->buf, use_len );
243  info->buf += use_len;
244  info->length -= use_len;
245  }
246 
247  if( len - use_len > 0 )
248  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
249 
250  return( 0 );
251 }
252 
260 typedef struct
261 {
262  uint32_t key[16];
263  uint32_t v0, v1;
265 
274 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
275 {
276  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
277  uint32_t i, *k, sum, delta=0x9E3779B9;
278  unsigned char result[4], *out = output;
279 
280  if( rng_state == NULL )
281  return( rnd_std_rand( NULL, output, len ) );
282 
283  k = info->key;
284 
285  while( len > 0 )
286  {
287  size_t use_len = ( len > 4 ) ? 4 : len;
288  sum = 0;
289 
290  for( i = 0; i < 32; i++ )
291  {
292  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
293  sum += delta;
294  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
295  }
296 
297  PUT_UINT32_BE( info->v0, result, 0 );
298  memcpy( out, result, use_len );
299  len -= use_len;
300  out += 4;
301  }
302 
303  return( 0 );
304 }
305 
306 
307 #include <stdio.h>
308 #include <string.h>
309 
310 #if defined(POLARSSL_PLATFORM_C)
311 #include "polarssl/platform.h"
312 #else
313 #define polarssl_printf printf
314 #define polarssl_malloc malloc
315 #define polarssl_free free
316 #endif
317 
318 static int test_errors = 0;
319 
320 #ifdef POLARSSL_CIPHER_C
321 
322 #define TEST_SUITE_ACTIVE
323 
324 static int test_assert( int correct, const char *test )
325 {
326  if( correct )
327  return( 0 );
328 
329  test_errors++;
330  if( test_errors == 1 )
331  printf( "FAILED\n" );
332  printf( " %s\n", test );
333 
334  return( 1 );
335 }
336 
337 #define TEST_ASSERT( TEST ) \
338  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
339  if( test_errors) goto exit; \
340  } while (0)
341 
342 int verify_string( char **str )
343 {
344  if( (*str)[0] != '"' ||
345  (*str)[strlen( *str ) - 1] != '"' )
346  {
347  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
348  return( -1 );
349  }
350 
351  (*str)++;
352  (*str)[strlen( *str ) - 1] = '\0';
353 
354  return( 0 );
355 }
356 
357 int verify_int( char *str, int *value )
358 {
359  size_t i;
360  int minus = 0;
361  int digits = 1;
362  int hex = 0;
363 
364  for( i = 0; i < strlen( str ); i++ )
365  {
366  if( i == 0 && str[i] == '-' )
367  {
368  minus = 1;
369  continue;
370  }
371 
372  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
373  str[i - 1] == '0' && str[i] == 'x' )
374  {
375  hex = 1;
376  continue;
377  }
378 
379  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
380  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
381  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
382  {
383  digits = 0;
384  break;
385  }
386  }
387 
388  if( digits )
389  {
390  if( hex )
391  *value = strtol( str, NULL, 16 );
392  else
393  *value = strtol( str, NULL, 10 );
394 
395  return( 0 );
396  }
397 
398  if( strcmp( str, "-1" ) == 0 )
399  {
400  *value = ( -1 );
401  return( 0 );
402  }
403  if( strcmp( str, "POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED" ) == 0 )
404  {
406  return( 0 );
407  }
408  if( strcmp( str, "POLARSSL_ENCRYPT" ) == 0 )
409  {
410  *value = ( POLARSSL_ENCRYPT );
411  return( 0 );
412  }
413  if( strcmp( str, "POLARSSL_CIPHER_DES_EDE_CBC" ) == 0 )
414  {
415  *value = ( POLARSSL_CIPHER_DES_EDE_CBC );
416  return( 0 );
417  }
418  if( strcmp( str, "POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
419  {
420  *value = ( POLARSSL_PADDING_ZEROS_AND_LEN );
421  return( 0 );
422  }
423  if( strcmp( str, "POLARSSL_CIPHER_DES_EDE3_CBC" ) == 0 )
424  {
425  *value = ( POLARSSL_CIPHER_DES_EDE3_CBC );
426  return( 0 );
427  }
428  if( strcmp( str, "POLARSSL_DECRYPT" ) == 0 )
429  {
430  *value = ( POLARSSL_DECRYPT );
431  return( 0 );
432  }
433  if( strcmp( str, "POLARSSL_CIPHER_DES_CBC" ) == 0 )
434  {
435  *value = ( POLARSSL_CIPHER_DES_CBC );
436  return( 0 );
437  }
438  if( strcmp( str, "POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
439  {
440  *value = ( POLARSSL_PADDING_ONE_AND_ZEROS );
441  return( 0 );
442  }
443  if( strcmp( str, "POLARSSL_CIPHER_DES_ECB" ) == 0 )
444  {
445  *value = ( POLARSSL_CIPHER_DES_ECB );
446  return( 0 );
447  }
448  if( strcmp( str, "POLARSSL_PADDING_ZEROS" ) == 0 )
449  {
450  *value = ( POLARSSL_PADDING_ZEROS );
451  return( 0 );
452  }
453  if( strcmp( str, "POLARSSL_PADDING_NONE" ) == 0 )
454  {
455  *value = ( POLARSSL_PADDING_NONE );
456  return( 0 );
457  }
458  if( strcmp( str, "POLARSSL_CIPHER_DES_EDE_ECB" ) == 0 )
459  {
460  *value = ( POLARSSL_CIPHER_DES_EDE_ECB );
461  return( 0 );
462  }
463 
464 
465  printf( "Expected integer for parameter and got: %s\n", str );
466  return( -1 );
467 }
468 
469 void test_suite_cipher_list( )
470 {
471  const int *cipher_type;
472 
473  for( cipher_type = cipher_list(); *cipher_type != 0; cipher_type++ )
474  TEST_ASSERT( cipher_info_from_type( *cipher_type ) != NULL );
475 
476 exit:
477  return;
478 }
479 
480 void test_suite_cipher_null_args( )
481 {
482  cipher_context_t ctx;
483  const cipher_info_t *info = cipher_info_from_type( *( cipher_list() ) );
484  unsigned char buf[1] = { 0 };
485  size_t olen;
486 
487  cipher_init( &ctx );
488 
489  TEST_ASSERT( cipher_get_block_size( NULL ) == 0 );
490  TEST_ASSERT( cipher_get_block_size( &ctx ) == 0 );
491 
494 
495  TEST_ASSERT( cipher_get_iv_size( NULL ) == 0 );
496  TEST_ASSERT( cipher_get_iv_size( &ctx ) == 0 );
497 
498  TEST_ASSERT( cipher_info_from_string( NULL ) == NULL );
499 
500  TEST_ASSERT( cipher_init_ctx( &ctx, NULL )
502  TEST_ASSERT( cipher_init_ctx( NULL, info )
504 
505  TEST_ASSERT( cipher_setkey( NULL, buf, 0, POLARSSL_ENCRYPT )
507  TEST_ASSERT( cipher_setkey( &ctx, buf, 0, POLARSSL_ENCRYPT )
509 
510  TEST_ASSERT( cipher_set_iv( NULL, buf, 0 )
512  TEST_ASSERT( cipher_set_iv( &ctx, buf, 0 )
514 
517 
518 #if defined(POLARSSL_GCM_C)
519  TEST_ASSERT( cipher_update_ad( NULL, buf, 0 )
521  TEST_ASSERT( cipher_update_ad( &ctx, buf, 0 )
523 #endif
524 
525  TEST_ASSERT( cipher_update( NULL, buf, 0, buf, &olen )
527  TEST_ASSERT( cipher_update( &ctx, buf, 0, buf, &olen )
529 
530  TEST_ASSERT( cipher_finish( NULL, buf, &olen )
532  TEST_ASSERT( cipher_finish( &ctx, buf, &olen )
534 
535 #if defined(POLARSSL_GCM_C)
536  TEST_ASSERT( cipher_write_tag( NULL, buf, olen )
538  TEST_ASSERT( cipher_write_tag( &ctx, buf, olen )
540 
541  TEST_ASSERT( cipher_check_tag( NULL, buf, olen )
543  TEST_ASSERT( cipher_check_tag( &ctx, buf, olen )
545 #endif
546 
547 exit:
548  return;
549 }
550 
551 void test_suite_enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
552  int length_val, int pad_mode )
553 {
554  size_t length = length_val, outlen, total_len, i;
555  unsigned char key[32];
556  unsigned char iv[16];
557  unsigned char ad[13];
558  unsigned char tag[16];
559  unsigned char inbuf[64];
560  unsigned char encbuf[64];
561  unsigned char decbuf[64];
562 
563  const cipher_info_t *cipher_info;
564  cipher_context_t ctx_dec;
565  cipher_context_t ctx_enc;
566 
567  /*
568  * Prepare contexts
569  */
570  cipher_init( &ctx_dec );
571  cipher_init( &ctx_enc );
572 
573  memset( key, 0x2a, sizeof( key ) );
574 
575  /* Check and get info structures */
576  cipher_info = cipher_info_from_type( cipher_id );
577  TEST_ASSERT( NULL != cipher_info );
578  TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
579 
580  /* Initialise enc and dec contexts */
581  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
582  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
583 
584  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
585  TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
586 
587 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
588  if( -1 != pad_mode )
589  {
590  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
591  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
592  }
593 #else
594  (void) pad_mode;
595 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
596 
597  /*
598  * Do a few encode/decode cycles
599  */
600  for( i = 0; i < 3; i++ )
601  {
602  memset( iv , 0x00 + i, sizeof( iv ) );
603  memset( ad, 0x10 + i, sizeof( ad ) );
604  memset( inbuf, 0x20 + i, sizeof( inbuf ) );
605 
606  memset( encbuf, 0, sizeof( encbuf ) );
607  memset( decbuf, 0, sizeof( decbuf ) );
608  memset( tag, 0, sizeof( tag ) );
609 
610  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
611  TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
612 
613  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
614  TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
615 
616 #if defined(POLARSSL_GCM_C)
617  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
618  TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
619 #endif
620 
621  /* encode length number of bytes from inbuf */
622  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
623  total_len = outlen;
624 
625  TEST_ASSERT( total_len == length ||
626  ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
627  total_len < length &&
628  total_len + cipher_get_block_size( &ctx_enc ) > length ) );
629 
630  TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
631  total_len += outlen;
632 
633 #if defined(POLARSSL_GCM_C)
634  TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
635 #endif
636 
637  TEST_ASSERT( total_len == length ||
638  ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
639  total_len > length &&
640  total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
641 
642  /* decode the previously encoded string */
643  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
644  total_len = outlen;
645 
646  TEST_ASSERT( total_len == length ||
647  ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
648  total_len < length &&
649  total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
650 
651  TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
652  total_len += outlen;
653 
654 #if defined(POLARSSL_GCM_C)
655  TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
656 #endif
657 
658  /* check result */
659  TEST_ASSERT( total_len == length );
660  TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
661  }
662 
663  /*
664  * Done
665  */
666 exit:
667  cipher_free( &ctx_dec );
668  cipher_free( &ctx_enc );
669 }
670 
671 void test_suite_enc_fail( int cipher_id, int pad_mode, int key_len,
672  int length_val, int ret )
673 {
674  size_t length = length_val;
675  unsigned char key[32];
676  unsigned char iv[16];
677 
678  const cipher_info_t *cipher_info;
679  cipher_context_t ctx;
680 
681  unsigned char inbuf[64];
682  unsigned char encbuf[64];
683 
684  size_t outlen = 0;
685 
686  memset( key, 0, 32 );
687  memset( iv , 0, 16 );
688 
689  cipher_init( &ctx );
690 
691  memset( inbuf, 5, 64 );
692  memset( encbuf, 0, 64 );
693 
694  /* Check and get info structures */
695  cipher_info = cipher_info_from_type( cipher_id );
696  TEST_ASSERT( NULL != cipher_info );
697 
698  /* Initialise context */
699  TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
700  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
701 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
702  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
703 #else
704  (void) pad_mode;
705 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
706  TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
707  TEST_ASSERT( 0 == cipher_reset( &ctx ) );
708 #if defined(POLARSSL_GCM_C)
709  TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
710 #endif
711 
712  /* encode length number of bytes from inbuf */
713  TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
714  TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
715 
716  /* done */
717 exit:
718  cipher_free( &ctx );
719 }
720 
721 void test_suite_dec_empty_buf()
722 {
723  unsigned char key[32];
724  unsigned char iv[16];
725 
726  cipher_context_t ctx_dec;
727  const cipher_info_t *cipher_info;
728 
729  unsigned char encbuf[64];
730  unsigned char decbuf[64];
731 
732  size_t outlen = 0;
733 
734  memset( key, 0, 32 );
735  memset( iv , 0, 16 );
736 
737  cipher_init( &ctx_dec );
738 
739  memset( encbuf, 0, 64 );
740  memset( decbuf, 0, 64 );
741 
742  /* Initialise context */
744  TEST_ASSERT( NULL != cipher_info);
745 
746  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
747 
748  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
749 
750  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
751 
752  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
753 
754 #if defined(POLARSSL_GCM_C)
755  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
756 #endif
757 
758  /* decode 0-byte string */
759  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
760  TEST_ASSERT( 0 == outlen );
762  &ctx_dec, decbuf + outlen, &outlen ) );
763  TEST_ASSERT( 0 == outlen );
764 
765 exit:
766  cipher_free( &ctx_dec );
767 }
768 
769 void test_suite_enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
770  int second_length_val )
771 {
772  size_t first_length = first_length_val;
773  size_t second_length = second_length_val;
774  size_t length = first_length + second_length;
775  unsigned char key[32];
776  unsigned char iv[16];
777 
778  cipher_context_t ctx_dec;
779  cipher_context_t ctx_enc;
780  const cipher_info_t *cipher_info;
781 
782  unsigned char inbuf[64];
783  unsigned char encbuf[64];
784  unsigned char decbuf[64];
785 
786  size_t outlen = 0;
787  size_t totaloutlen = 0;
788 
789  memset( key, 0, 32 );
790  memset( iv , 0, 16 );
791 
792  cipher_init( &ctx_dec );
793  cipher_init( &ctx_enc );
794 
795  memset( inbuf, 5, 64 );
796  memset( encbuf, 0, 64 );
797  memset( decbuf, 0, 64 );
798 
799  /* Initialise enc and dec contexts */
800  cipher_info = cipher_info_from_type( cipher_id );
801  TEST_ASSERT( NULL != cipher_info);
802 
803  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
804  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
805 
806  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
807  TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
808 
809  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
810  TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
811 
812  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
813  TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
814 
815 #if defined(POLARSSL_GCM_C)
816  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
817  TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
818 #endif
819 
820  /* encode length number of bytes from inbuf */
821  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
822  totaloutlen = outlen;
823  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
824  totaloutlen += outlen;
825  TEST_ASSERT( totaloutlen == length ||
826  ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
827  totaloutlen < length &&
828  totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
829 
830  TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
831  totaloutlen += outlen;
832  TEST_ASSERT( totaloutlen == length ||
833  ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
834  totaloutlen > length &&
835  totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
836 
837  /* decode the previously encoded string */
838  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
839  totaloutlen = outlen;
840 
841  TEST_ASSERT( totaloutlen == length ||
842  ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
843  totaloutlen < length &&
844  totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
845 
846  TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
847  totaloutlen += outlen;
848 
849  TEST_ASSERT( totaloutlen == length );
850 
851  TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
852 
853 exit:
854  cipher_free( &ctx_dec );
855  cipher_free( &ctx_enc );
856 }
857 
858 void test_suite_decrypt_test_vec( int cipher_id, int pad_mode,
859  char *hex_key, char *hex_iv,
860  char *hex_cipher, char *hex_clear,
861  char *hex_ad, char *hex_tag,
862  int finish_result, int tag_result )
863 {
864  unsigned char key[50];
865  unsigned char iv[50];
866  unsigned char cipher[200];
867  unsigned char clear[200];
868  unsigned char ad[200];
869  unsigned char tag[20];
870  size_t key_len, iv_len, cipher_len, clear_len;
871 #if defined(POLARSSL_GCM_C)
872  size_t ad_len, tag_len;
873 #endif
874  cipher_context_t ctx;
875  unsigned char output[200];
876  size_t outlen, total_len;
877 
878  cipher_init( &ctx );
879 
880  memset( key, 0x00, sizeof( key ) );
881  memset( iv, 0x00, sizeof( iv ) );
882  memset( cipher, 0x00, sizeof( cipher ) );
883  memset( clear, 0x00, sizeof( clear ) );
884  memset( ad, 0x00, sizeof( ad ) );
885  memset( tag, 0x00, sizeof( tag ) );
886  memset( output, 0x00, sizeof( output ) );
887 
888  key_len = unhexify( key, hex_key );
889  iv_len = unhexify( iv, hex_iv );
890  cipher_len = unhexify( cipher, hex_cipher );
891  clear_len = unhexify( clear, hex_clear );
892 #if defined(POLARSSL_GCM_C)
893  ad_len = unhexify( ad, hex_ad );
894  tag_len = unhexify( tag, hex_tag );
895 #else
896  ((void) hex_ad);
897  ((void) hex_tag);
898 #endif
899 
900  /* Prepare context */
901  TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
902  cipher_info_from_type( cipher_id ) ) );
903  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
904 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
905  if( pad_mode != -1 )
906  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
907 #else
908  (void) pad_mode;
909 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
910  TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
911  TEST_ASSERT( 0 == cipher_reset( &ctx ) );
912 #if defined(POLARSSL_GCM_C)
913  TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
914 #endif
915 
916  /* decode buffer and check tag */
917  total_len = 0;
918  TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
919  total_len += outlen;
920  TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
921  &outlen ) );
922  total_len += outlen;
923 #if defined(POLARSSL_GCM_C)
924  TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
925 #endif
926 
927  /* check plaintext only if everything went fine */
928  if( 0 == finish_result && 0 == tag_result )
929  {
930  TEST_ASSERT( total_len == clear_len );
931  TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
932  }
933 
934 exit:
935  cipher_free( &ctx );
936 }
937 
938 #ifdef POLARSSL_CIPHER_MODE_AEAD
939 void test_suite_auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
940  char *hex_ad, char *hex_cipher,
941  char *hex_tag, char *hex_clear )
942 {
943  int ret;
944  unsigned char key[50];
945  unsigned char iv[50];
946  unsigned char cipher[200];
947  unsigned char clear[200];
948  unsigned char ad[200];
949  unsigned char tag[20];
950  unsigned char my_tag[20];
951  size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
952  cipher_context_t ctx;
953  unsigned char output[200];
954  size_t outlen;
955 
956  cipher_init( &ctx );
957 
958  memset( key, 0x00, sizeof( key ) );
959  memset( iv, 0x00, sizeof( iv ) );
960  memset( cipher, 0x00, sizeof( cipher ) );
961  memset( clear, 0x00, sizeof( clear ) );
962  memset( ad, 0x00, sizeof( ad ) );
963  memset( tag, 0x00, sizeof( tag ) );
964  memset( my_tag, 0xFF, sizeof( my_tag ) );
965  memset( output, 0xFF, sizeof( output ) );
966 
967  key_len = unhexify( key, hex_key );
968  iv_len = unhexify( iv, hex_iv );
969  cipher_len = unhexify( cipher, hex_cipher );
970  ad_len = unhexify( ad, hex_ad );
971  tag_len = unhexify( tag, hex_tag );
972 
973  /* Prepare context */
974  TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
975  cipher_info_from_type( cipher_id ) ) );
976  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
977 
978  /* decode buffer and check tag */
979  ret = cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
980  cipher, cipher_len, output, &outlen,
981  tag, tag_len );
982 
983  /* make sure we didn't overwrite */
984  TEST_ASSERT( output[outlen + 0] == 0xFF );
985  TEST_ASSERT( output[outlen + 1] == 0xFF );
986 
987  /* make sure the message is rejected if it should be */
988  if( strcmp( hex_clear, "FAIL" ) == 0 )
989  {
991  goto exit;
992  }
993 
994  /* otherwise, make sure it was decrypted properly */
995  TEST_ASSERT( ret == 0 );
996 
997  clear_len = unhexify( clear, hex_clear );
998  TEST_ASSERT( outlen == clear_len );
999  TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
1000 
1001  /* then encrypt the clear and make sure we get the same ciphertext and tag */
1002  memset( output, 0xFF, sizeof( output ) );
1003  outlen = 0;
1004 
1005  ret = cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
1006  clear, clear_len, output, &outlen,
1007  my_tag, tag_len );
1008  TEST_ASSERT( ret == 0 );
1009 
1010  TEST_ASSERT( outlen == clear_len );
1011  TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
1012  TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
1013 
1014  /* make sure we didn't overwrite */
1015  TEST_ASSERT( output[outlen + 0] == 0xFF );
1016  TEST_ASSERT( output[outlen + 1] == 0xFF );
1017  TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
1018  TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
1019 
1020 
1021 exit:
1022  cipher_free( &ctx );
1023 }
1024 #endif /* POLARSSL_CIPHER_MODE_AEAD */
1025 
1026 void test_suite_test_vec_ecb( int cipher_id, int operation, char *hex_key,
1027  char *hex_input, char *hex_result,
1028  int finish_result )
1029 {
1030  unsigned char key[50];
1031  unsigned char input[16];
1032  unsigned char result[16];
1033  size_t key_len;
1034  cipher_context_t ctx;
1035  unsigned char output[32];
1036  size_t outlen;
1037 
1038  cipher_init( &ctx );
1039 
1040  memset( key, 0x00, sizeof( key ) );
1041  memset( input, 0x00, sizeof( input ) );
1042  memset( result, 0x00, sizeof( result ) );
1043  memset( output, 0x00, sizeof( output ) );
1044 
1045  /* Prepare context */
1046  TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
1047  cipher_info_from_type( cipher_id ) ) );
1048 
1049  key_len = unhexify( key, hex_key );
1050  TEST_ASSERT( unhexify( input, hex_input ) ==
1051  (int) cipher_get_block_size( &ctx ) );
1052  TEST_ASSERT( unhexify( result, hex_result ) ==
1053  (int) cipher_get_block_size( &ctx ) );
1054 
1055  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
1056 
1057  TEST_ASSERT( 0 == cipher_update( &ctx, input,
1058  cipher_get_block_size( &ctx ),
1059  output, &outlen ) );
1060  TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
1061  TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
1062  &outlen ) );
1063  TEST_ASSERT( 0 == outlen );
1064 
1065  /* check plaintext only if everything went fine */
1066  if( 0 == finish_result )
1067  TEST_ASSERT( 0 == memcmp( output, result,
1068  cipher_get_block_size( &ctx ) ) );
1069 
1070 exit:
1071  cipher_free( &ctx );
1072 }
1073 
1074 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1075 void test_suite_set_padding( int cipher_id, int pad_mode, int ret )
1076 {
1077  const cipher_info_t *cipher_info;
1078  cipher_context_t ctx;
1079 
1080  cipher_init( &ctx );
1081 
1082  cipher_info = cipher_info_from_type( cipher_id );
1083  TEST_ASSERT( NULL != cipher_info );
1084  TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
1085 
1086  TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
1087 
1088 exit:
1089  cipher_free( &ctx );
1090 }
1091 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1092 
1093 #ifdef POLARSSL_CIPHER_MODE_CBC
1094 void test_suite_check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
1095 {
1096  cipher_info_t cipher_info;
1097  cipher_context_t ctx;
1098  unsigned char input[16];
1099  size_t ilen, dlen;
1100 
1101  /* build a fake context just for getting access to get_padding */
1102  cipher_init( &ctx );
1103  cipher_info.mode = POLARSSL_MODE_CBC;
1104  ctx.cipher_info = &cipher_info;
1105 
1106  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
1107 
1108  ilen = unhexify( input, input_str );
1109 
1110  TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
1111  if( 0 == ret )
1112  TEST_ASSERT( dlen == (size_t) dlen_check );
1113 
1114 exit:
1115  return;
1116 }
1117 #endif /* POLARSSL_CIPHER_MODE_CBC */
1118 
1119 #ifdef POLARSSL_SELF_TEST
1120 void test_suite_cipher_selftest()
1121 {
1122  TEST_ASSERT( cipher_self_test( 0 ) == 0 );
1123 
1124 exit:
1125  return;
1126 }
1127 #endif /* POLARSSL_SELF_TEST */
1128 
1129 
1130 #endif /* POLARSSL_CIPHER_C */
1131 
1132 
1133 int dep_check( char *str )
1134 {
1135  if( str == NULL )
1136  return( 1 );
1137 
1138  if( strcmp( str, "POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
1139  {
1140 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
1141  return( 0 );
1142 #else
1143  return( 1 );
1144 #endif
1145  }
1146  if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
1147  {
1148 #if defined(POLARSSL_CIPHER_MODE_CBC)
1149  return( 0 );
1150 #else
1151  return( 1 );
1152 #endif
1153  }
1154  if( strcmp( str, "POLARSSL_DES_C" ) == 0 )
1155  {
1156 #if defined(POLARSSL_DES_C)
1157  return( 0 );
1158 #else
1159  return( 1 );
1160 #endif
1161  }
1162 
1163 
1164  return( 1 );
1165 }
1166 
1167 int dispatch_test(int cnt, char *params[50])
1168 {
1169  int ret;
1170  ((void) cnt);
1171  ((void) params);
1172 
1173 #if defined(TEST_SUITE_ACTIVE)
1174  if( strcmp( params[0], "cipher_list" ) == 0 )
1175  {
1176 
1177 
1178  if( cnt != 1 )
1179  {
1180  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1181  return( 2 );
1182  }
1183 
1184 
1185  test_suite_cipher_list( );
1186  return ( 0 );
1187 
1188  return ( 3 );
1189  }
1190  else
1191  if( strcmp( params[0], "cipher_null_args" ) == 0 )
1192  {
1193 
1194 
1195  if( cnt != 1 )
1196  {
1197  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1198  return( 2 );
1199  }
1200 
1201 
1202  test_suite_cipher_null_args( );
1203  return ( 0 );
1204 
1205  return ( 3 );
1206  }
1207  else
1208  if( strcmp( params[0], "enc_dec_buf" ) == 0 )
1209  {
1210 
1211  int param1;
1212  char *param2 = params[2];
1213  int param3;
1214  int param4;
1215  int param5;
1216 
1217  if( cnt != 6 )
1218  {
1219  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1220  return( 2 );
1221  }
1222 
1223  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1224  if( verify_string( &param2 ) != 0 ) return( 2 );
1225  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1226  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1227  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1228 
1229  test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
1230  return ( 0 );
1231 
1232  return ( 3 );
1233  }
1234  else
1235  if( strcmp( params[0], "enc_fail" ) == 0 )
1236  {
1237 
1238  int param1;
1239  int param2;
1240  int param3;
1241  int param4;
1242  int param5;
1243 
1244  if( cnt != 6 )
1245  {
1246  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1247  return( 2 );
1248  }
1249 
1250  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1251  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1252  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1253  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1254  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1255 
1256  test_suite_enc_fail( param1, param2, param3, param4, param5 );
1257  return ( 0 );
1258 
1259  return ( 3 );
1260  }
1261  else
1262  if( strcmp( params[0], "dec_empty_buf" ) == 0 )
1263  {
1264 
1265 
1266  if( cnt != 1 )
1267  {
1268  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1269  return( 2 );
1270  }
1271 
1272 
1273  test_suite_dec_empty_buf( );
1274  return ( 0 );
1275 
1276  return ( 3 );
1277  }
1278  else
1279  if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
1280  {
1281 
1282  int param1;
1283  int param2;
1284  int param3;
1285  int param4;
1286 
1287  if( cnt != 5 )
1288  {
1289  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1290  return( 2 );
1291  }
1292 
1293  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1294  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1295  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1296  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1297 
1298  test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1299  return ( 0 );
1300 
1301  return ( 3 );
1302  }
1303  else
1304  if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
1305  {
1306 
1307  int param1;
1308  int param2;
1309  char *param3 = params[3];
1310  char *param4 = params[4];
1311  char *param5 = params[5];
1312  char *param6 = params[6];
1313  char *param7 = params[7];
1314  char *param8 = params[8];
1315  int param9;
1316  int param10;
1317 
1318  if( cnt != 11 )
1319  {
1320  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1321  return( 2 );
1322  }
1323 
1324  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1325  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1326  if( verify_string( &param3 ) != 0 ) return( 2 );
1327  if( verify_string( &param4 ) != 0 ) return( 2 );
1328  if( verify_string( &param5 ) != 0 ) return( 2 );
1329  if( verify_string( &param6 ) != 0 ) return( 2 );
1330  if( verify_string( &param7 ) != 0 ) return( 2 );
1331  if( verify_string( &param8 ) != 0 ) return( 2 );
1332  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1333  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1334 
1335  test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1336  return ( 0 );
1337 
1338  return ( 3 );
1339  }
1340  else
1341  if( strcmp( params[0], "auth_crypt_tv" ) == 0 )
1342  {
1343  #ifdef POLARSSL_CIPHER_MODE_AEAD
1344 
1345  int param1;
1346  char *param2 = params[2];
1347  char *param3 = params[3];
1348  char *param4 = params[4];
1349  char *param5 = params[5];
1350  char *param6 = params[6];
1351  char *param7 = params[7];
1352 
1353  if( cnt != 8 )
1354  {
1355  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1356  return( 2 );
1357  }
1358 
1359  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1360  if( verify_string( &param2 ) != 0 ) return( 2 );
1361  if( verify_string( &param3 ) != 0 ) return( 2 );
1362  if( verify_string( &param4 ) != 0 ) return( 2 );
1363  if( verify_string( &param5 ) != 0 ) return( 2 );
1364  if( verify_string( &param6 ) != 0 ) return( 2 );
1365  if( verify_string( &param7 ) != 0 ) return( 2 );
1366 
1367  test_suite_auth_crypt_tv( param1, param2, param3, param4, param5, param6, param7 );
1368  return ( 0 );
1369  #endif /* POLARSSL_CIPHER_MODE_AEAD */
1370 
1371  return ( 3 );
1372  }
1373  else
1374  if( strcmp( params[0], "test_vec_ecb" ) == 0 )
1375  {
1376 
1377  int param1;
1378  int param2;
1379  char *param3 = params[3];
1380  char *param4 = params[4];
1381  char *param5 = params[5];
1382  int param6;
1383 
1384  if( cnt != 7 )
1385  {
1386  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1387  return( 2 );
1388  }
1389 
1390  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1391  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1392  if( verify_string( &param3 ) != 0 ) return( 2 );
1393  if( verify_string( &param4 ) != 0 ) return( 2 );
1394  if( verify_string( &param5 ) != 0 ) return( 2 );
1395  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1396 
1397  test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1398  return ( 0 );
1399 
1400  return ( 3 );
1401  }
1402  else
1403  if( strcmp( params[0], "set_padding" ) == 0 )
1404  {
1405  #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1406 
1407  int param1;
1408  int param2;
1409  int param3;
1410 
1411  if( cnt != 4 )
1412  {
1413  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1414  return( 2 );
1415  }
1416 
1417  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1418  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1419  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1420 
1421  test_suite_set_padding( param1, param2, param3 );
1422  return ( 0 );
1423  #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1424 
1425  return ( 3 );
1426  }
1427  else
1428  if( strcmp( params[0], "check_padding" ) == 0 )
1429  {
1430  #ifdef POLARSSL_CIPHER_MODE_CBC
1431 
1432  int param1;
1433  char *param2 = params[2];
1434  int param3;
1435  int param4;
1436 
1437  if( cnt != 5 )
1438  {
1439  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1440  return( 2 );
1441  }
1442 
1443  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1444  if( verify_string( &param2 ) != 0 ) return( 2 );
1445  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1446  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1447 
1448  test_suite_check_padding( param1, param2, param3, param4 );
1449  return ( 0 );
1450  #endif /* POLARSSL_CIPHER_MODE_CBC */
1451 
1452  return ( 3 );
1453  }
1454  else
1455  if( strcmp( params[0], "cipher_selftest" ) == 0 )
1456  {
1457  #ifdef POLARSSL_SELF_TEST
1458 
1459 
1460  if( cnt != 1 )
1461  {
1462  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1463  return( 2 );
1464  }
1465 
1466 
1467  test_suite_cipher_selftest( );
1468  return ( 0 );
1469  #endif /* POLARSSL_SELF_TEST */
1470 
1471  return ( 3 );
1472  }
1473  else
1474 
1475  {
1476  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1477  fflush( stdout );
1478  return( 1 );
1479  }
1480 #else
1481  return( 3 );
1482 #endif
1483  return( ret );
1484 }
1485 
1486 int get_line( FILE *f, char *buf, size_t len )
1487 {
1488  char *ret;
1489 
1490  ret = fgets( buf, len, f );
1491  if( ret == NULL )
1492  return( -1 );
1493 
1494  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1495  buf[strlen(buf) - 1] = '\0';
1496  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1497  buf[strlen(buf) - 1] = '\0';
1498 
1499  return( 0 );
1500 }
1501 
1502 int parse_arguments( char *buf, size_t len, char *params[50] )
1503 {
1504  int cnt = 0, i;
1505  char *cur = buf;
1506  char *p = buf, *q;
1507 
1508  params[cnt++] = cur;
1509 
1510  while( *p != '\0' && p < buf + len )
1511  {
1512  if( *p == '\\' )
1513  {
1514  p++;
1515  p++;
1516  continue;
1517  }
1518  if( *p == ':' )
1519  {
1520  if( p + 1 < buf + len )
1521  {
1522  cur = p + 1;
1523  params[cnt++] = cur;
1524  }
1525  *p = '\0';
1526  }
1527 
1528  p++;
1529  }
1530 
1531  // Replace newlines, question marks and colons in strings
1532  for( i = 0; i < cnt; i++ )
1533  {
1534  p = params[i];
1535  q = params[i];
1536 
1537  while( *p != '\0' )
1538  {
1539  if( *p == '\\' && *(p + 1) == 'n' )
1540  {
1541  p += 2;
1542  *(q++) = '\n';
1543  }
1544  else if( *p == '\\' && *(p + 1) == ':' )
1545  {
1546  p += 2;
1547  *(q++) = ':';
1548  }
1549  else if( *p == '\\' && *(p + 1) == '?' )
1550  {
1551  p += 2;
1552  *(q++) = '?';
1553  }
1554  else
1555  *(q++) = *(p++);
1556  }
1557  *q = '\0';
1558  }
1559 
1560  return( cnt );
1561 }
1562 
1563 int main()
1564 {
1565  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1566  const char *filename = "/root/rpmbuild/BUILD/polarssl-1.3.9/tests/suites/test_suite_cipher.des.data";
1567  FILE *file;
1568  char buf[5000];
1569  char *params[50];
1570 
1571 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1572  unsigned char alloc_buf[1000000];
1573  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1574 #endif
1575 
1576  file = fopen( filename, "r" );
1577  if( file == NULL )
1578  {
1579  fprintf( stderr, "Failed to open\n" );
1580  return( 1 );
1581  }
1582 
1583  while( !feof( file ) )
1584  {
1585  int skip = 0;
1586 
1587  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1588  break;
1589  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1590  fprintf( stdout, " " );
1591  for( i = strlen( buf ) + 1; i < 67; i++ )
1592  fprintf( stdout, "." );
1593  fprintf( stdout, " " );
1594  fflush( stdout );
1595 
1596  total_tests++;
1597 
1598  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1599  break;
1600  cnt = parse_arguments( buf, strlen(buf), params );
1601 
1602  if( strcmp( params[0], "depends_on" ) == 0 )
1603  {
1604  for( i = 1; i < cnt; i++ )
1605  if( dep_check( params[i] ) != 0 )
1606  skip = 1;
1607 
1608  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1609  break;
1610  cnt = parse_arguments( buf, strlen(buf), params );
1611  }
1612 
1613  if( skip == 0 )
1614  {
1615  test_errors = 0;
1616  ret = dispatch_test( cnt, params );
1617  }
1618 
1619  if( skip == 1 || ret == 3 )
1620  {
1621  total_skipped++;
1622  fprintf( stdout, "----\n" );
1623  fflush( stdout );
1624  }
1625  else if( ret == 0 && test_errors == 0 )
1626  {
1627  fprintf( stdout, "PASS\n" );
1628  fflush( stdout );
1629  }
1630  else if( ret == 2 )
1631  {
1632  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1633  fclose(file);
1634  exit( 2 );
1635  }
1636  else
1637  total_errors++;
1638 
1639  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1640  break;
1641  if( strlen(buf) != 0 )
1642  {
1643  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1644  return( 1 );
1645  }
1646  }
1647  fclose(file);
1648 
1649  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1650  if( total_errors == 0 )
1651  fprintf( stdout, "PASSED" );
1652  else
1653  fprintf( stdout, "FAILED" );
1654 
1655  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1656  total_tests - total_errors, total_tests, total_skipped );
1657 
1658 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1659 #if defined(POLARSSL_MEMORY_DEBUG)
1660  memory_buffer_alloc_status();
1661 #endif
1663 #endif
1664 
1665  return( total_errors != 0 );
1666 }
1667 
1668 
#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Bad input parameters to function.
Definition: cipher.h:58
static int test_errors
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
Memory allocation layer (Deprecated to platform layer)
static int cipher_get_iv_size(const cipher_context_t *ctx)
Returns the size of the cipher&#39;s IV/NONCE in bytes.
Definition: cipher.h:418
Generic cipher context.
Definition: cipher.h:258
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
static int unhexify(unsigned char *obuf, const char *ibuf)
Info structure for the pseudo random function.
void cipher_init(cipher_context_t *ctx)
Initialize a cipher_context (as NONE)
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
static cipher_mode_t cipher_get_cipher_mode(const cipher_context_t *ctx)
Returns the mode of operation for the cipher.
Definition: cipher.h:401
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:226
zero padding (not reversible!)
Definition: cipher.h:150
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:384
const cipher_info_t * cipher_info_from_string(const char *cipher_name)
Returns the cipher information structure associated with the given cipher name.
int main()
Configuration options (set of defines)
int dispatch_test(int cnt, char *params[50])
PolarSSL Platform abstraction layer.
#define PUT_UINT32_BE(n, b, i)
#define polarssl_malloc
static int test_assert(int correct, const char *test)
ISO/IEC 7816-4 padding.
Definition: cipher.h:148
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
int(* get_padding)(unsigned char *input, size_t ilen, size_t *data_len)
Definition: cipher.h:270
const cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:260
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 parse_arguments(char *buf, size_t len, char *params[50])
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_auth_encrypt(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len, const unsigned char *ad, size_t ad_len, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, unsigned char *tag, size_t tag_len)
Generic autenticated encryption (AEAD ciphers).
int cipher_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
int cipher_auth_decrypt(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len, const unsigned char *ad, size_t ad_len, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, const unsigned char *tag, size_t tag_len)
Generic autenticated decryption (AEAD ciphers).
int dep_check(char *str)
#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
Decryption of block requires a full block.
Definition: cipher.h:61
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
#define TEST_ASSERT(TEST)
Generic cipher wrapper.
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
void cipher_free(cipher_context_t *ctx)
Free and clear the cipher-specific context of ctx.
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:231
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.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
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)
never pad (full blocks only)
Definition: cipher.h:151
Galois/Counter mode for 128-bit block ciphers.
unsigned char * buf
const int * cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
ANSI X.923 padding.
Definition: cipher.h:149
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int get_line(FILE *f, char *buf, size_t len)
cipher_type_t cipher
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.
#define POLARSSL_ERR_CIPHER_AUTH_FAILED
Authentication failed (for AEAD modes).
Definition: cipher.h:62