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