PolarSSL v1.3.1
test_suite_aes.cfb.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_AES_C
4 
5 #include <polarssl/aes.h>
6 #endif /* POLARSSL_AES_C */
7 
8 
9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
10 #include "polarssl/memory.h"
11 #endif
12 
13 #ifdef _MSC_VER
14 #include <basetsd.h>
15 typedef UINT32 uint32_t;
16 #else
17 #include <inttypes.h>
18 #endif
19 
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 /*
25  * 32-bit integer manipulation macros (big endian)
26  */
27 #ifndef GET_UINT32_BE
28 #define GET_UINT32_BE(n,b,i) \
29 { \
30  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
31  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
32  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
33  | ( (uint32_t) (b)[(i) + 3] ); \
34 }
35 #endif
36 
37 #ifndef PUT_UINT32_BE
38 #define PUT_UINT32_BE(n,b,i) \
39 { \
40  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
41  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
42  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
43  (b)[(i) + 3] = (unsigned char) ( (n) ); \
44 }
45 #endif
46 
47 static int unhexify(unsigned char *obuf, const char *ibuf)
48 {
49  unsigned char c, c2;
50  int len = strlen(ibuf) / 2;
51  assert(!(strlen(ibuf) %1)); // must be even number of bytes
52 
53  while (*ibuf != 0)
54  {
55  c = *ibuf++;
56  if( c >= '0' && c <= '9' )
57  c -= '0';
58  else if( c >= 'a' && c <= 'f' )
59  c -= 'a' - 10;
60  else if( c >= 'A' && c <= 'F' )
61  c -= 'A' - 10;
62  else
63  assert( 0 );
64 
65  c2 = *ibuf++;
66  if( c2 >= '0' && c2 <= '9' )
67  c2 -= '0';
68  else if( c2 >= 'a' && c2 <= 'f' )
69  c2 -= 'a' - 10;
70  else if( c2 >= 'A' && c2 <= 'F' )
71  c2 -= 'A' - 10;
72  else
73  assert( 0 );
74 
75  *obuf++ = ( c << 4 ) | c2;
76  }
77 
78  return len;
79 }
80 
81 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
82 {
83  unsigned char l, h;
84 
85  while (len != 0)
86  {
87  h = (*ibuf) / 16;
88  l = (*ibuf) % 16;
89 
90  if( h < 10 )
91  *obuf++ = '0' + h;
92  else
93  *obuf++ = 'a' + h - 10;
94 
95  if( l < 10 )
96  *obuf++ = '0' + l;
97  else
98  *obuf++ = 'a' + l - 10;
99 
100  ++ibuf;
101  len--;
102  }
103 }
104 
114 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
115 {
116  size_t i;
117 
118  if( rng_state != NULL )
119  rng_state = NULL;
120 
121  for( i = 0; i < len; ++i )
122  output[i] = rand();
123 
124  return( 0 );
125 }
126 
132 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
133 {
134  if( rng_state != NULL )
135  rng_state = NULL;
136 
137  memset( output, 0, len );
138 
139  return( 0 );
140 }
141 
142 typedef struct
143 {
144  unsigned char *buf;
145  size_t length;
146 } rnd_buf_info;
147 
159 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
160 {
161  rnd_buf_info *info = (rnd_buf_info *) rng_state;
162  size_t use_len;
163 
164  if( rng_state == NULL )
165  return( rnd_std_rand( NULL, output, len ) );
166 
167  use_len = len;
168  if( len > info->length )
169  use_len = info->length;
170 
171  if( use_len )
172  {
173  memcpy( output, info->buf, use_len );
174  info->buf += use_len;
175  info->length -= use_len;
176  }
177 
178  if( len - use_len > 0 )
179  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
180 
181  return( 0 );
182 }
183 
191 typedef struct
192 {
193  uint32_t key[16];
194  uint32_t v0, v1;
196 
205 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
206 {
207  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
208  uint32_t i, *k, sum, delta=0x9E3779B9;
209  unsigned char result[4];
210 
211  if( rng_state == NULL )
212  return( rnd_std_rand( NULL, output, len ) );
213 
214  k = info->key;
215 
216  while( len > 0 )
217  {
218  size_t use_len = ( len > 4 ) ? 4 : len;
219  sum = 0;
220 
221  for( i = 0; i < 32; i++ )
222  {
223  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
224  sum += delta;
225  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
226  }
227 
228  PUT_UINT32_BE( info->v0, result, 0 );
229  memcpy( output, result, use_len );
230  len -= use_len;
231  }
232 
233  return( 0 );
234 }
235 
245 static int not_rnd( void *in, unsigned char *out, size_t len )
246 {
247  unsigned char *obuf;
248  const char *ibuf = in;
249  unsigned char c, c2;
250  assert( len == strlen(ibuf) / 2 );
251  assert(!(strlen(ibuf) %1)); // must be even number of bytes
252 
253  obuf = out + (len - 1); // sic
254  while (*ibuf != 0)
255  {
256  c = *ibuf++;
257  if( c >= '0' && c <= '9' )
258  c -= '0';
259  else if( c >= 'a' && c <= 'f' )
260  c -= 'a' - 10;
261  else if( c >= 'A' && c <= 'F' )
262  c -= 'A' - 10;
263  else
264  assert( 0 );
265 
266  c2 = *ibuf++;
267  if( c2 >= '0' && c2 <= '9' )
268  c2 -= '0';
269  else if( c2 >= 'a' && c2 <= 'f' )
270  c2 -= 'a' - 10;
271  else if( c2 >= 'A' && c2 <= 'F' )
272  c2 -= 'A' - 10;
273  else
274  assert( 0 );
275 
276  *obuf-- = ( c << 4 ) | c2; // sic
277  }
278 
279  return( 0 );
280 }
281 
282 
283 #include <stdio.h>
284 #include <string.h>
285 
286 static int test_errors = 0;
287 
288 #ifdef POLARSSL_AES_C
289 
290 #define TEST_SUITE_ACTIVE
291 
292 static int test_assert( int correct, char *test )
293 {
294  if( correct )
295  return( 0 );
296 
297  test_errors++;
298  if( test_errors == 1 )
299  printf( "FAILED\n" );
300  printf( " %s\n", test );
301 
302  return( 1 );
303 }
304 
305 #define TEST_ASSERT( TEST ) \
306  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
307  if( test_errors) return; \
308  } while (0)
309 
310 int verify_string( char **str )
311 {
312  if( (*str)[0] != '"' ||
313  (*str)[strlen( *str ) - 1] != '"' )
314  {
315  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
316  return( -1 );
317  }
318 
319  (*str)++;
320  (*str)[strlen( *str ) - 1] = '\0';
321 
322  return( 0 );
323 }
324 
325 int verify_int( char *str, int *value )
326 {
327  size_t i;
328  int minus = 0;
329  int digits = 1;
330  int hex = 0;
331 
332  for( i = 0; i < strlen( str ); i++ )
333  {
334  if( i == 0 && str[i] == '-' )
335  {
336  minus = 1;
337  continue;
338  }
339 
340  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
341  str[i - 1] == '0' && str[i] == 'x' )
342  {
343  hex = 1;
344  continue;
345  }
346 
347  if( str[i] < '0' || str[i] > '9' )
348  {
349  digits = 0;
350  break;
351  }
352  }
353 
354  if( digits )
355  {
356  if( hex )
357  *value = strtol( str, NULL, 16 );
358  else
359  *value = strtol( str, NULL, 10 );
360 
361  return( 0 );
362  }
363 
364 
365 
366  printf( "Expected integer for parameter and got: %s\n", str );
367  return( -1 );
368 }
369 
370 void test_suite_aes_encrypt_ecb( char *hex_key_string, char *hex_src_string,
371  char *hex_dst_string, int setkey_result )
372 {
373  unsigned char key_str[100];
374  unsigned char src_str[100];
375  unsigned char dst_str[100];
376  unsigned char output[100];
377  aes_context ctx;
378  int key_len;
379 
380  memset(key_str, 0x00, 100);
381  memset(src_str, 0x00, 100);
382  memset(dst_str, 0x00, 100);
383  memset(output, 0x00, 100);
384 
385  key_len = unhexify( key_str, hex_key_string );
386  unhexify( src_str, hex_src_string );
387 
388  TEST_ASSERT( aes_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result );
389  if( setkey_result == 0 )
390  {
391  TEST_ASSERT( aes_crypt_ecb( &ctx, AES_ENCRYPT, src_str, output ) == 0 );
392  hexify( dst_str, output, 16 );
393 
394  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
395  }
396 }
397 
398 void test_suite_aes_decrypt_ecb( char *hex_key_string, char *hex_src_string,
399  char *hex_dst_string, int setkey_result )
400 {
401  unsigned char key_str[100];
402  unsigned char src_str[100];
403  unsigned char dst_str[100];
404  unsigned char output[100];
405  aes_context ctx;
406  int key_len;
407 
408  memset(key_str, 0x00, 100);
409  memset(src_str, 0x00, 100);
410  memset(dst_str, 0x00, 100);
411  memset(output, 0x00, 100);
412 
413  key_len = unhexify( key_str, hex_key_string );
414  unhexify( src_str, hex_src_string );
415 
416  TEST_ASSERT( aes_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result );
417  if( setkey_result == 0 )
418  {
419  TEST_ASSERT( aes_crypt_ecb( &ctx, AES_DECRYPT, src_str, output ) == 0 );
420  hexify( dst_str, output, 16 );
421 
422  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
423  }
424 }
425 
426 #ifdef POLARSSL_CIPHER_MODE_CBC
427 void test_suite_aes_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
428  char *hex_src_string, char *hex_dst_string,
429  int cbc_result )
430 {
431  unsigned char key_str[100];
432  unsigned char iv_str[100];
433  unsigned char src_str[100];
434  unsigned char dst_str[100];
435  unsigned char output[100];
436  aes_context ctx;
437  int key_len, data_len;
438 
439  memset(key_str, 0x00, 100);
440  memset(iv_str, 0x00, 100);
441  memset(src_str, 0x00, 100);
442  memset(dst_str, 0x00, 100);
443  memset(output, 0x00, 100);
444 
445  key_len = unhexify( key_str, hex_key_string );
446  unhexify( iv_str, hex_iv_string );
447  data_len = unhexify( src_str, hex_src_string );
448 
449  aes_setkey_enc( &ctx, key_str, key_len * 8 );
450  TEST_ASSERT( aes_crypt_cbc( &ctx, AES_ENCRYPT, data_len, iv_str, src_str, output ) == cbc_result );
451  if( cbc_result == 0 )
452  {
453  hexify( dst_str, output, data_len );
454 
455  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
456  }
457 }
458 #endif /* POLARSSL_CIPHER_MODE_CBC */
459 
460 #ifdef POLARSSL_CIPHER_MODE_CBC
461 void test_suite_aes_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
462  char *hex_src_string, char *hex_dst_string,
463  int cbc_result )
464 {
465  unsigned char key_str[100];
466  unsigned char iv_str[100];
467  unsigned char src_str[100];
468  unsigned char dst_str[100];
469  unsigned char output[100];
470  aes_context ctx;
471  int key_len, data_len;
472 
473  memset(key_str, 0x00, 100);
474  memset(iv_str, 0x00, 100);
475  memset(src_str, 0x00, 100);
476  memset(dst_str, 0x00, 100);
477  memset(output, 0x00, 100);
478 
479  key_len = unhexify( key_str, hex_key_string );
480  unhexify( iv_str, hex_iv_string );
481  data_len = unhexify( src_str, hex_src_string );
482 
483  aes_setkey_dec( &ctx, key_str, key_len * 8 );
484  TEST_ASSERT( aes_crypt_cbc( &ctx, AES_DECRYPT, data_len, iv_str, src_str, output ) == cbc_result );
485  if( cbc_result == 0)
486  {
487  hexify( dst_str, output, data_len );
488 
489  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
490  }
491 }
492 #endif /* POLARSSL_CIPHER_MODE_CBC */
493 
494 #ifdef POLARSSL_CIPHER_MODE_CFB
495 void test_suite_aes_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
496  char *hex_src_string, char *hex_dst_string )
497 {
498  unsigned char key_str[100];
499  unsigned char iv_str[100];
500  unsigned char src_str[100];
501  unsigned char dst_str[100];
502  unsigned char output[100];
503  aes_context ctx;
504  size_t iv_offset = 0;
505  int key_len;
506 
507  memset(key_str, 0x00, 100);
508  memset(iv_str, 0x00, 100);
509  memset(src_str, 0x00, 100);
510  memset(dst_str, 0x00, 100);
511  memset(output, 0x00, 100);
512 
513  key_len = unhexify( key_str, hex_key_string );
514  unhexify( iv_str, hex_iv_string );
515  unhexify( src_str, hex_src_string );
516 
517  aes_setkey_enc( &ctx, key_str, key_len * 8 );
518  TEST_ASSERT( aes_crypt_cfb128( &ctx, AES_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
519  hexify( dst_str, output, 16 );
520 
521  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
522 }
523 #endif /* POLARSSL_CIPHER_MODE_CFB */
524 
525 #ifdef POLARSSL_CIPHER_MODE_CFB
526 void test_suite_aes_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
527  char *hex_src_string, char *hex_dst_string )
528 {
529  unsigned char key_str[100];
530  unsigned char iv_str[100];
531  unsigned char src_str[100];
532  unsigned char dst_str[100];
533  unsigned char output[100];
534  aes_context ctx;
535  size_t iv_offset = 0;
536  int key_len;
537 
538  memset(key_str, 0x00, 100);
539  memset(iv_str, 0x00, 100);
540  memset(src_str, 0x00, 100);
541  memset(dst_str, 0x00, 100);
542  memset(output, 0x00, 100);
543 
544  key_len = unhexify( key_str, hex_key_string );
545  unhexify( iv_str, hex_iv_string );
546  unhexify( src_str, hex_src_string );
547 
548  aes_setkey_enc( &ctx, key_str, key_len * 8 );
549  TEST_ASSERT( aes_crypt_cfb128( &ctx, AES_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
550  hexify( dst_str, output, 16 );
551 
552  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
553 }
554 #endif /* POLARSSL_CIPHER_MODE_CFB */
555 
556 #ifdef POLARSSL_SELF_TEST
557 void test_suite_aes_selftest()
558 {
559  TEST_ASSERT( aes_self_test( 0 ) == 0 );
560 }
561 #endif /* POLARSSL_SELF_TEST */
562 
563 
564 #endif /* POLARSSL_AES_C */
565 
566 
567 int dep_check( char *str )
568 {
569  if( str == NULL )
570  return( 1 );
571 
572  if( strcmp( str, "POLARSSL_CIPHER_MODE_CFB" ) == 0 )
573  {
574 #if defined(POLARSSL_CIPHER_MODE_CFB)
575  return( 0 );
576 #else
577  return( 1 );
578 #endif
579  }
580 
581 
582  return( 1 );
583 }
584 
585 int dispatch_test(int cnt, char *params[50])
586 {
587  int ret;
588  ((void) cnt);
589  ((void) params);
590 
591 #if defined(TEST_SUITE_ACTIVE)
592  if( strcmp( params[0], "aes_encrypt_ecb" ) == 0 )
593  {
594 
595  char *param1 = params[1];
596  char *param2 = params[2];
597  char *param3 = params[3];
598  int param4;
599 
600  if( cnt != 5 )
601  {
602  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
603  return( 2 );
604  }
605 
606  if( verify_string( &param1 ) != 0 ) return( 2 );
607  if( verify_string( &param2 ) != 0 ) return( 2 );
608  if( verify_string( &param3 ) != 0 ) return( 2 );
609  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
610 
611  test_suite_aes_encrypt_ecb( param1, param2, param3, param4 );
612  return ( 0 );
613 
614  return ( 3 );
615  }
616  else
617  if( strcmp( params[0], "aes_decrypt_ecb" ) == 0 )
618  {
619 
620  char *param1 = params[1];
621  char *param2 = params[2];
622  char *param3 = params[3];
623  int param4;
624 
625  if( cnt != 5 )
626  {
627  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
628  return( 2 );
629  }
630 
631  if( verify_string( &param1 ) != 0 ) return( 2 );
632  if( verify_string( &param2 ) != 0 ) return( 2 );
633  if( verify_string( &param3 ) != 0 ) return( 2 );
634  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
635 
636  test_suite_aes_decrypt_ecb( param1, param2, param3, param4 );
637  return ( 0 );
638 
639  return ( 3 );
640  }
641  else
642  if( strcmp( params[0], "aes_encrypt_cbc" ) == 0 )
643  {
644  #ifdef POLARSSL_CIPHER_MODE_CBC
645 
646  char *param1 = params[1];
647  char *param2 = params[2];
648  char *param3 = params[3];
649  char *param4 = params[4];
650  int param5;
651 
652  if( cnt != 6 )
653  {
654  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
655  return( 2 );
656  }
657 
658  if( verify_string( &param1 ) != 0 ) return( 2 );
659  if( verify_string( &param2 ) != 0 ) return( 2 );
660  if( verify_string( &param3 ) != 0 ) return( 2 );
661  if( verify_string( &param4 ) != 0 ) return( 2 );
662  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
663 
664  test_suite_aes_encrypt_cbc( param1, param2, param3, param4, param5 );
665  return ( 0 );
666  #endif /* POLARSSL_CIPHER_MODE_CBC */
667 
668  return ( 3 );
669  }
670  else
671  if( strcmp( params[0], "aes_decrypt_cbc" ) == 0 )
672  {
673  #ifdef POLARSSL_CIPHER_MODE_CBC
674 
675  char *param1 = params[1];
676  char *param2 = params[2];
677  char *param3 = params[3];
678  char *param4 = params[4];
679  int param5;
680 
681  if( cnt != 6 )
682  {
683  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
684  return( 2 );
685  }
686 
687  if( verify_string( &param1 ) != 0 ) return( 2 );
688  if( verify_string( &param2 ) != 0 ) return( 2 );
689  if( verify_string( &param3 ) != 0 ) return( 2 );
690  if( verify_string( &param4 ) != 0 ) return( 2 );
691  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
692 
693  test_suite_aes_decrypt_cbc( param1, param2, param3, param4, param5 );
694  return ( 0 );
695  #endif /* POLARSSL_CIPHER_MODE_CBC */
696 
697  return ( 3 );
698  }
699  else
700  if( strcmp( params[0], "aes_encrypt_cfb128" ) == 0 )
701  {
702  #ifdef POLARSSL_CIPHER_MODE_CFB
703 
704  char *param1 = params[1];
705  char *param2 = params[2];
706  char *param3 = params[3];
707  char *param4 = params[4];
708 
709  if( cnt != 5 )
710  {
711  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
712  return( 2 );
713  }
714 
715  if( verify_string( &param1 ) != 0 ) return( 2 );
716  if( verify_string( &param2 ) != 0 ) return( 2 );
717  if( verify_string( &param3 ) != 0 ) return( 2 );
718  if( verify_string( &param4 ) != 0 ) return( 2 );
719 
720  test_suite_aes_encrypt_cfb128( param1, param2, param3, param4 );
721  return ( 0 );
722  #endif /* POLARSSL_CIPHER_MODE_CFB */
723 
724  return ( 3 );
725  }
726  else
727  if( strcmp( params[0], "aes_decrypt_cfb128" ) == 0 )
728  {
729  #ifdef POLARSSL_CIPHER_MODE_CFB
730 
731  char *param1 = params[1];
732  char *param2 = params[2];
733  char *param3 = params[3];
734  char *param4 = params[4];
735 
736  if( cnt != 5 )
737  {
738  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
739  return( 2 );
740  }
741 
742  if( verify_string( &param1 ) != 0 ) return( 2 );
743  if( verify_string( &param2 ) != 0 ) return( 2 );
744  if( verify_string( &param3 ) != 0 ) return( 2 );
745  if( verify_string( &param4 ) != 0 ) return( 2 );
746 
747  test_suite_aes_decrypt_cfb128( param1, param2, param3, param4 );
748  return ( 0 );
749  #endif /* POLARSSL_CIPHER_MODE_CFB */
750 
751  return ( 3 );
752  }
753  else
754  if( strcmp( params[0], "aes_selftest" ) == 0 )
755  {
756  #ifdef POLARSSL_SELF_TEST
757 
758 
759  if( cnt != 1 )
760  {
761  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
762  return( 2 );
763  }
764 
765 
766  test_suite_aes_selftest( );
767  return ( 0 );
768  #endif /* POLARSSL_SELF_TEST */
769 
770  return ( 3 );
771  }
772  else
773 
774  {
775  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
776  fflush( stdout );
777  return( 1 );
778  }
779 #else
780  return( 3 );
781 #endif
782  return( ret );
783 }
784 
785 int get_line( FILE *f, char *buf, size_t len )
786 {
787  char *ret;
788 
789  ret = fgets( buf, len, f );
790  if( ret == NULL )
791  return( -1 );
792 
793  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
794  buf[strlen(buf) - 1] = '\0';
795  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
796  buf[strlen(buf) - 1] = '\0';
797 
798  return( 0 );
799 }
800 
801 int parse_arguments( char *buf, size_t len, char *params[50] )
802 {
803  int cnt = 0, i;
804  char *cur = buf;
805  char *p = buf, *q;
806 
807  params[cnt++] = cur;
808 
809  while( *p != '\0' && p < buf + len )
810  {
811  if( *p == '\\' )
812  {
813  *p++;
814  *p++;
815  continue;
816  }
817  if( *p == ':' )
818  {
819  if( p + 1 < buf + len )
820  {
821  cur = p + 1;
822  params[cnt++] = cur;
823  }
824  *p = '\0';
825  }
826 
827  *p++;
828  }
829 
830  // Replace newlines, question marks and colons in strings
831  for( i = 0; i < cnt; i++ )
832  {
833  p = params[i];
834  q = params[i];
835 
836  while( *p != '\0' )
837  {
838  if( *p == '\\' && *(p + 1) == 'n' )
839  {
840  p += 2;
841  *(q++) = '\n';
842  }
843  else if( *p == '\\' && *(p + 1) == ':' )
844  {
845  p += 2;
846  *(q++) = ':';
847  }
848  else if( *p == '\\' && *(p + 1) == '?' )
849  {
850  p += 2;
851  *(q++) = '?';
852  }
853  else
854  *(q++) = *(p++);
855  }
856  *q = '\0';
857  }
858 
859  return( cnt );
860 }
861 
862 int main()
863 {
864  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
865  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_aes.cfb.data";
866  FILE *file;
867  char buf[5000];
868  char *params[50];
869 
870 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
871  unsigned char alloc_buf[1000000];
872  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
873 #endif
874 
875  file = fopen( filename, "r" );
876  if( file == NULL )
877  {
878  fprintf( stderr, "Failed to open\n" );
879  return( 1 );
880  }
881 
882  while( !feof( file ) )
883  {
884  int skip = 0;
885 
886  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
887  break;
888  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
889  fprintf( stdout, " " );
890  for( i = strlen( buf ) + 1; i < 67; i++ )
891  fprintf( stdout, "." );
892  fprintf( stdout, " " );
893  fflush( stdout );
894 
895  total_tests++;
896 
897  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
898  break;
899  cnt = parse_arguments( buf, strlen(buf), params );
900 
901  if( strcmp( params[0], "depends_on" ) == 0 )
902  {
903  for( i = 1; i < cnt; i++ )
904  if( dep_check( params[i] ) != 0 )
905  skip = 1;
906 
907  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
908  break;
909  cnt = parse_arguments( buf, strlen(buf), params );
910  }
911 
912  if( skip == 0 )
913  {
914  test_errors = 0;
915  ret = dispatch_test( cnt, params );
916  }
917 
918  if( skip == 1 || ret == 3 )
919  {
920  total_skipped++;
921  fprintf( stdout, "----\n" );
922  fflush( stdout );
923  }
924  else if( ret == 0 && test_errors == 0 )
925  {
926  fprintf( stdout, "PASS\n" );
927  fflush( stdout );
928  }
929  else if( ret == 2 )
930  {
931  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
932  fclose(file);
933  exit( 2 );
934  }
935  else
936  total_errors++;
937 
938  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
939  break;
940  if( strlen(buf) != 0 )
941  {
942  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
943  return( 1 );
944  }
945  }
946  fclose(file);
947 
948  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
949  if( total_errors == 0 )
950  fprintf( stdout, "PASSED" );
951  else
952  fprintf( stdout, "FAILED" );
953 
954  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
955  total_tests - total_errors, total_tests, total_skipped );
956 
957 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
958 #if defined(POLARSSL_MEMORY_DEBUG)
959  memory_buffer_alloc_status();
960 #endif
961  memory_buffer_alloc_free();
962 #endif
963 
964  return( total_errors != 0 );
965 }
966 
967