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