PolarSSL v1.3.1
test_suite_cipher.aes.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_CIPHER_C
4 
5 #include <polarssl/cipher.h>
6 
7 #if defined(POLARSSL_GCM_C)
8 #include <polarssl/gcm.h>
9 #endif
10 #endif /* POLARSSL_CIPHER_C */
11 
12 
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14 #include "polarssl/memory.h"
15 #endif
16 
17 #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_PADDING_ZEROS" ) == 0 )
369  {
370  *value = ( POLARSSL_PADDING_ZEROS );
371  return( 0 );
372  }
373  if( strcmp( str, "POLARSSL_CIPHER_AES_256_CFB128" ) == 0 )
374  {
375  *value = ( POLARSSL_CIPHER_AES_256_CFB128 );
376  return( 0 );
377  }
378  if( strcmp( str, "POLARSSL_CIPHER_AES_128_ECB" ) == 0 )
379  {
380  *value = ( POLARSSL_CIPHER_AES_128_ECB );
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_AES_192_ECB" ) == 0 )
389  {
390  *value = ( POLARSSL_CIPHER_AES_192_ECB );
391  return( 0 );
392  }
393  if( strcmp( str, "POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED" ) == 0 )
394  {
396  return( 0 );
397  }
398  if( strcmp( str, "POLARSSL_CIPHER_AES_256_CBC" ) == 0 )
399  {
400  *value = ( POLARSSL_CIPHER_AES_256_CBC );
401  return( 0 );
402  }
403  if( strcmp( str, "POLARSSL_CIPHER_AES_192_CFB128" ) == 0 )
404  {
405  *value = ( POLARSSL_CIPHER_AES_192_CFB128 );
406  return( 0 );
407  }
408  if( strcmp( str, "POLARSSL_DECRYPT" ) == 0 )
409  {
410  *value = ( POLARSSL_DECRYPT );
411  return( 0 );
412  }
413  if( strcmp( str, "POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
414  {
415  *value = ( POLARSSL_PADDING_ONE_AND_ZEROS );
416  return( 0 );
417  }
418  if( strcmp( str, "POLARSSL_ERR_CIPHER_INVALID_PADDING" ) == 0 )
419  {
421  return( 0 );
422  }
423  if( strcmp( str, "POLARSSL_CIPHER_AES_256_ECB" ) == 0 )
424  {
425  *value = ( POLARSSL_CIPHER_AES_256_ECB );
426  return( 0 );
427  }
428  if( strcmp( str, "POLARSSL_CIPHER_AES_128_CBC" ) == 0 )
429  {
430  *value = ( POLARSSL_CIPHER_AES_128_CBC );
431  return( 0 );
432  }
433  if( strcmp( str, "POLARSSL_CIPHER_AES_128_CFB128" ) == 0 )
434  {
435  *value = ( POLARSSL_CIPHER_AES_128_CFB128 );
436  return( 0 );
437  }
438  if( strcmp( str, "POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
439  {
440  *value = ( POLARSSL_PADDING_ZEROS_AND_LEN );
441  return( 0 );
442  }
443  if( strcmp( str, "POLARSSL_CIPHER_AES_192_CBC" ) == 0 )
444  {
445  *value = ( POLARSSL_CIPHER_AES_192_CBC );
446  return( 0 );
447  }
448  if( strcmp( str, "POLARSSL_ENCRYPT" ) == 0 )
449  {
450  *value = ( POLARSSL_ENCRYPT );
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, "-1" ) == 0 )
459  {
460  *value = ( -1 );
461  return( 0 );
462  }
463  if( strcmp( str, "POLARSSL_PADDING_PKCS7" ) == 0 )
464  {
465  *value = ( POLARSSL_PADDING_PKCS7 );
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_CIPHER_MODE_CBC" ) == 0 )
987  {
988 #if defined(POLARSSL_CIPHER_MODE_CBC)
989  return( 0 );
990 #else
991  return( 1 );
992 #endif
993  }
994  if( strcmp( str, "POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
995  {
996 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
997  return( 0 );
998 #else
999  return( 1 );
1000 #endif
1001  }
1002  if( strcmp( str, "POLARSSL_CIPHER_MODE_CFB" ) == 0 )
1003  {
1004 #if defined(POLARSSL_CIPHER_MODE_CFB)
1005  return( 0 );
1006 #else
1007  return( 1 );
1008 #endif
1009  }
1010  if( strcmp( str, "POLARSSL_CIPHER_MODE_CTR" ) == 0 )
1011  {
1012 #if defined(POLARSSL_CIPHER_MODE_CTR)
1013  return( 0 );
1014 #else
1015  return( 1 );
1016 #endif
1017  }
1018 
1019 
1020  return( 1 );
1021 }
1022 
1023 int dispatch_test(int cnt, char *params[50])
1024 {
1025  int ret;
1026  ((void) cnt);
1027  ((void) params);
1028 
1029 #if defined(TEST_SUITE_ACTIVE)
1030  if( strcmp( params[0], "enc_dec_buf" ) == 0 )
1031  {
1032 
1033  int param1;
1034  char *param2 = params[2];
1035  int param3;
1036  int param4;
1037  int param5;
1038 
1039  if( cnt != 6 )
1040  {
1041  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1042  return( 2 );
1043  }
1044 
1045  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1046  if( verify_string( &param2 ) != 0 ) return( 2 );
1047  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1048  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1049  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1050 
1051  test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
1052  return ( 0 );
1053 
1054  return ( 3 );
1055  }
1056  else
1057  if( strcmp( params[0], "enc_fail" ) == 0 )
1058  {
1059 
1060  int param1;
1061  int param2;
1062  int param3;
1063  int param4;
1064  int param5;
1065 
1066  if( cnt != 6 )
1067  {
1068  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1069  return( 2 );
1070  }
1071 
1072  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1073  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1074  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1075  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1076  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1077 
1078  test_suite_enc_fail( param1, param2, param3, param4, param5 );
1079  return ( 0 );
1080 
1081  return ( 3 );
1082  }
1083  else
1084  if( strcmp( params[0], "dec_empty_buf" ) == 0 )
1085  {
1086 
1087 
1088  if( cnt != 1 )
1089  {
1090  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1091  return( 2 );
1092  }
1093 
1094 
1095  test_suite_dec_empty_buf( );
1096  return ( 0 );
1097 
1098  return ( 3 );
1099  }
1100  else
1101  if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
1102  {
1103 
1104  int param1;
1105  int param2;
1106  int param3;
1107  int param4;
1108 
1109  if( cnt != 5 )
1110  {
1111  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1112  return( 2 );
1113  }
1114 
1115  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1116  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1117  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1118  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1119 
1120  test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1121  return ( 0 );
1122 
1123  return ( 3 );
1124  }
1125  else
1126  if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
1127  {
1128 
1129  int param1;
1130  int param2;
1131  char *param3 = params[3];
1132  char *param4 = params[4];
1133  char *param5 = params[5];
1134  char *param6 = params[6];
1135  char *param7 = params[7];
1136  char *param8 = params[8];
1137  int param9;
1138  int param10;
1139 
1140  if( cnt != 11 )
1141  {
1142  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1143  return( 2 );
1144  }
1145 
1146  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1147  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1148  if( verify_string( &param3 ) != 0 ) return( 2 );
1149  if( verify_string( &param4 ) != 0 ) return( 2 );
1150  if( verify_string( &param5 ) != 0 ) return( 2 );
1151  if( verify_string( &param6 ) != 0 ) return( 2 );
1152  if( verify_string( &param7 ) != 0 ) return( 2 );
1153  if( verify_string( &param8 ) != 0 ) return( 2 );
1154  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1155  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1156 
1157  test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1158  return ( 0 );
1159 
1160  return ( 3 );
1161  }
1162  else
1163  if( strcmp( params[0], "test_vec_ecb" ) == 0 )
1164  {
1165 
1166  int param1;
1167  int param2;
1168  char *param3 = params[3];
1169  char *param4 = params[4];
1170  char *param5 = params[5];
1171  int param6;
1172 
1173  if( cnt != 7 )
1174  {
1175  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
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_string( &param3 ) != 0 ) return( 2 );
1182  if( verify_string( &param4 ) != 0 ) return( 2 );
1183  if( verify_string( &param5 ) != 0 ) return( 2 );
1184  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1185 
1186  test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1187  return ( 0 );
1188 
1189  return ( 3 );
1190  }
1191  else
1192  if( strcmp( params[0], "set_padding" ) == 0 )
1193  {
1194  #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1195 
1196  int param1;
1197  int param2;
1198  int param3;
1199 
1200  if( cnt != 4 )
1201  {
1202  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1203  return( 2 );
1204  }
1205 
1206  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1207  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1208  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1209 
1210  test_suite_set_padding( param1, param2, param3 );
1211  return ( 0 );
1212  #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1213 
1214  return ( 3 );
1215  }
1216  else
1217  if( strcmp( params[0], "check_padding" ) == 0 )
1218  {
1219  #ifdef POLARSSL_CIPHER_MODE_CBC
1220 
1221  int param1;
1222  char *param2 = params[2];
1223  int param3;
1224  int param4;
1225 
1226  if( cnt != 5 )
1227  {
1228  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1229  return( 2 );
1230  }
1231 
1232  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1233  if( verify_string( &param2 ) != 0 ) return( 2 );
1234  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1235  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1236 
1237  test_suite_check_padding( param1, param2, param3, param4 );
1238  return ( 0 );
1239  #endif /* POLARSSL_CIPHER_MODE_CBC */
1240 
1241  return ( 3 );
1242  }
1243  else
1244  if( strcmp( params[0], "cipher_selftest" ) == 0 )
1245  {
1246  #ifdef POLARSSL_SELF_TEST
1247 
1248 
1249  if( cnt != 1 )
1250  {
1251  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1252  return( 2 );
1253  }
1254 
1255 
1256  test_suite_cipher_selftest( );
1257  return ( 0 );
1258  #endif /* POLARSSL_SELF_TEST */
1259 
1260  return ( 3 );
1261  }
1262  else
1263 
1264  {
1265  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1266  fflush( stdout );
1267  return( 1 );
1268  }
1269 #else
1270  return( 3 );
1271 #endif
1272  return( ret );
1273 }
1274 
1275 int get_line( FILE *f, char *buf, size_t len )
1276 {
1277  char *ret;
1278 
1279  ret = fgets( buf, len, f );
1280  if( ret == NULL )
1281  return( -1 );
1282 
1283  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1284  buf[strlen(buf) - 1] = '\0';
1285  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1286  buf[strlen(buf) - 1] = '\0';
1287 
1288  return( 0 );
1289 }
1290 
1291 int parse_arguments( char *buf, size_t len, char *params[50] )
1292 {
1293  int cnt = 0, i;
1294  char *cur = buf;
1295  char *p = buf, *q;
1296 
1297  params[cnt++] = cur;
1298 
1299  while( *p != '\0' && p < buf + len )
1300  {
1301  if( *p == '\\' )
1302  {
1303  *p++;
1304  *p++;
1305  continue;
1306  }
1307  if( *p == ':' )
1308  {
1309  if( p + 1 < buf + len )
1310  {
1311  cur = p + 1;
1312  params[cnt++] = cur;
1313  }
1314  *p = '\0';
1315  }
1316 
1317  *p++;
1318  }
1319 
1320  // Replace newlines, question marks and colons in strings
1321  for( i = 0; i < cnt; i++ )
1322  {
1323  p = params[i];
1324  q = params[i];
1325 
1326  while( *p != '\0' )
1327  {
1328  if( *p == '\\' && *(p + 1) == 'n' )
1329  {
1330  p += 2;
1331  *(q++) = '\n';
1332  }
1333  else if( *p == '\\' && *(p + 1) == ':' )
1334  {
1335  p += 2;
1336  *(q++) = ':';
1337  }
1338  else if( *p == '\\' && *(p + 1) == '?' )
1339  {
1340  p += 2;
1341  *(q++) = '?';
1342  }
1343  else
1344  *(q++) = *(p++);
1345  }
1346  *q = '\0';
1347  }
1348 
1349  return( cnt );
1350 }
1351 
1352 int main()
1353 {
1354  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1355  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_cipher.aes.data";
1356  FILE *file;
1357  char buf[5000];
1358  char *params[50];
1359 
1360 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1361  unsigned char alloc_buf[1000000];
1362  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1363 #endif
1364 
1365  file = fopen( filename, "r" );
1366  if( file == NULL )
1367  {
1368  fprintf( stderr, "Failed to open\n" );
1369  return( 1 );
1370  }
1371 
1372  while( !feof( file ) )
1373  {
1374  int skip = 0;
1375 
1376  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1377  break;
1378  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1379  fprintf( stdout, " " );
1380  for( i = strlen( buf ) + 1; i < 67; i++ )
1381  fprintf( stdout, "." );
1382  fprintf( stdout, " " );
1383  fflush( stdout );
1384 
1385  total_tests++;
1386 
1387  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1388  break;
1389  cnt = parse_arguments( buf, strlen(buf), params );
1390 
1391  if( strcmp( params[0], "depends_on" ) == 0 )
1392  {
1393  for( i = 1; i < cnt; i++ )
1394  if( dep_check( params[i] ) != 0 )
1395  skip = 1;
1396 
1397  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1398  break;
1399  cnt = parse_arguments( buf, strlen(buf), params );
1400  }
1401 
1402  if( skip == 0 )
1403  {
1404  test_errors = 0;
1405  ret = dispatch_test( cnt, params );
1406  }
1407 
1408  if( skip == 1 || ret == 3 )
1409  {
1410  total_skipped++;
1411  fprintf( stdout, "----\n" );
1412  fflush( stdout );
1413  }
1414  else if( ret == 0 && test_errors == 0 )
1415  {
1416  fprintf( stdout, "PASS\n" );
1417  fflush( stdout );
1418  }
1419  else if( ret == 2 )
1420  {
1421  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1422  fclose(file);
1423  exit( 2 );
1424  }
1425  else
1426  total_errors++;
1427 
1428  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1429  break;
1430  if( strlen(buf) != 0 )
1431  {
1432  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1433  return( 1 );
1434  }
1435  }
1436  fclose(file);
1437 
1438  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1439  if( total_errors == 0 )
1440  fprintf( stdout, "PASSED" );
1441  else
1442  fprintf( stdout, "FAILED" );
1443 
1444  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1445  total_tests - total_errors, total_tests, total_skipped );
1446 
1447 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1448 #if defined(POLARSSL_MEMORY_DEBUG)
1449  memory_buffer_alloc_status();
1450 #endif
1451  memory_buffer_alloc_free();
1452 #endif
1453 
1454  return( total_errors != 0 );
1455 }
1456 
1457