PolarSSL v1.3.1
test_suite_shax.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 
4 #include <polarssl/sha1.h>
5 #include <polarssl/sha256.h>
6 #include <polarssl/sha512.h>
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 
289 #define TEST_SUITE_ACTIVE
290 
291 static int test_assert( int correct, char *test )
292 {
293  if( correct )
294  return( 0 );
295 
296  test_errors++;
297  if( test_errors == 1 )
298  printf( "FAILED\n" );
299  printf( " %s\n", test );
300 
301  return( 1 );
302 }
303 
304 #define TEST_ASSERT( TEST ) \
305  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
306  if( test_errors) return; \
307  } while (0)
308 
309 int verify_string( char **str )
310 {
311  if( (*str)[0] != '"' ||
312  (*str)[strlen( *str ) - 1] != '"' )
313  {
314  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
315  return( -1 );
316  }
317 
318  (*str)++;
319  (*str)[strlen( *str ) - 1] = '\0';
320 
321  return( 0 );
322 }
323 
324 int verify_int( char *str, int *value )
325 {
326  size_t i;
327  int minus = 0;
328  int digits = 1;
329  int hex = 0;
330 
331  for( i = 0; i < strlen( str ); i++ )
332  {
333  if( i == 0 && str[i] == '-' )
334  {
335  minus = 1;
336  continue;
337  }
338 
339  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
340  str[i - 1] == '0' && str[i] == 'x' )
341  {
342  hex = 1;
343  continue;
344  }
345 
346  if( str[i] < '0' || str[i] > '9' )
347  {
348  digits = 0;
349  break;
350  }
351  }
352 
353  if( digits )
354  {
355  if( hex )
356  *value = strtol( str, NULL, 16 );
357  else
358  *value = strtol( str, NULL, 10 );
359 
360  return( 0 );
361  }
362 
363 
364 
365  printf( "Expected integer for parameter and got: %s\n", str );
366  return( -1 );
367 }
368 
369 #ifdef POLARSSL_SHA1_C
370 void test_suite_sha1( char *hex_src_string, char *hex_hash_string )
371 {
372  unsigned char src_str[10000];
373  unsigned char hash_str[10000];
374  unsigned char output[41];
375  int src_len;
376 
377  memset(src_str, 0x00, 10000);
378  memset(hash_str, 0x00, 10000);
379  memset(output, 0x00, 41);
380 
381  src_len = unhexify( src_str, hex_src_string );
382 
383  sha1( src_str, src_len, output );
384  hexify( hash_str, output, 20 );
385 
386  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
387 }
388 #endif /* POLARSSL_SHA1_C */
389 
390 #ifdef POLARSSL_SHA256_C
391 void test_suite_sha224(char *hex_src_string, char *hex_hash_string )
392 {
393  unsigned char src_str[10000];
394  unsigned char hash_str[10000];
395  unsigned char output[57];
396  int src_len;
397 
398  memset(src_str, 0x00, 10000);
399  memset(hash_str, 0x00, 10000);
400  memset(output, 0x00, 57);
401 
402  src_len = unhexify( src_str, hex_src_string );
403 
404  sha256( src_str, src_len, output, 1 );
405  hexify( hash_str, output, 28 );
406 
407  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
408 }
409 #endif /* POLARSSL_SHA256_C */
410 
411 #ifdef POLARSSL_SHA256_C
412 void test_suite_sha256(char *hex_src_string, char *hex_hash_string )
413 {
414  unsigned char src_str[10000];
415  unsigned char hash_str[10000];
416  unsigned char output[65];
417  int src_len;
418 
419  memset(src_str, 0x00, 10000);
420  memset(hash_str, 0x00, 10000);
421  memset(output, 0x00, 65);
422 
423  src_len = unhexify( src_str, hex_src_string );
424 
425  sha256( src_str, src_len, output, 0 );
426  hexify( hash_str, output, 32 );
427 
428  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
429 }
430 #endif /* POLARSSL_SHA256_C */
431 
432 #ifdef POLARSSL_SHA512_C
433 void test_suite_sha384(char *hex_src_string, char *hex_hash_string )
434 {
435  unsigned char src_str[10000];
436  unsigned char hash_str[10000];
437  unsigned char output[97];
438  int src_len;
439 
440  memset(src_str, 0x00, 10000);
441  memset(hash_str, 0x00, 10000);
442  memset(output, 0x00, 97);
443 
444  src_len = unhexify( src_str, hex_src_string );
445 
446  sha512( src_str, src_len, output, 1 );
447  hexify( hash_str, output, 48 );
448 
449  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
450 }
451 #endif /* POLARSSL_SHA512_C */
452 
453 #ifdef POLARSSL_SHA512_C
454 void test_suite_sha512(char *hex_src_string, char *hex_hash_string )
455 {
456  unsigned char src_str[10000];
457  unsigned char hash_str[10000];
458  unsigned char output[129];
459  int src_len;
460 
461  memset(src_str, 0x00, 10000);
462  memset(hash_str, 0x00, 10000);
463  memset(output, 0x00, 129);
464 
465  src_len = unhexify( src_str, hex_src_string );
466 
467  sha512( src_str, src_len, output, 0);
468  hexify( hash_str, output, 64 );
469 
470  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
471 }
472 #endif /* POLARSSL_SHA512_C */
473 
474 #ifdef POLARSSL_SHA1_C
475 #ifdef POLARSSL_FS_IO
476 void test_suite_sha1_file( char *filename, char *hex_hash_string )
477 {
478  unsigned char hash_str[41];
479  unsigned char output[21];
480 
481  memset(hash_str, 0x00, 41);
482  memset(output, 0x00, 21);
483 
484  sha1_file( filename, output);
485  hexify( hash_str, output, 20 );
486 
487  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
488 }
489 #endif /* POLARSSL_SHA1_C */
490 #endif /* POLARSSL_FS_IO */
491 
492 #ifdef POLARSSL_SHA256_C
493 #ifdef POLARSSL_FS_IO
494 void test_suite_sha224_file( char *filename, char *hex_hash_string )
495 {
496  unsigned char hash_str[57];
497  unsigned char output[29];
498 
499  memset(hash_str, 0x00, 57);
500  memset(output, 0x00, 29);
501 
502  sha256_file( filename, output, 1);
503  hexify( hash_str, output, 28 );
504 
505  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
506 }
507 #endif /* POLARSSL_SHA256_C */
508 #endif /* POLARSSL_FS_IO */
509 
510 #ifdef POLARSSL_SHA256_C
511 #ifdef POLARSSL_FS_IO
512 void test_suite_sha256_file( char *filename, char *hex_hash_string )
513 {
514  unsigned char hash_str[65];
515  unsigned char output[33];
516 
517  memset(hash_str, 0x00, 65);
518  memset(output, 0x00, 33);
519 
520  sha256_file( filename, output, 0);
521  hexify( hash_str, output, 32 );
522 
523  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
524 }
525 #endif /* POLARSSL_SHA256_C */
526 #endif /* POLARSSL_FS_IO */
527 
528 #ifdef POLARSSL_SHA512_C
529 #ifdef POLARSSL_FS_IO
530 void test_suite_sha384_file( char *filename, char *hex_hash_string )
531 {
532  unsigned char hash_str[97];
533  unsigned char output[49];
534 
535  memset(hash_str, 0x00, 97);
536  memset(output, 0x00, 49);
537 
538  sha512_file( filename, output, 1);
539  hexify( hash_str, output, 48 );
540 
541  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
542 }
543 #endif /* POLARSSL_SHA512_C */
544 #endif /* POLARSSL_FS_IO */
545 
546 #ifdef POLARSSL_SHA512_C
547 #ifdef POLARSSL_FS_IO
548 void test_suite_sha512_file( char *filename, char *hex_hash_string )
549 {
550  unsigned char hash_str[129];
551  unsigned char output[65];
552 
553  memset(hash_str, 0x00, 129);
554  memset(output, 0x00, 65);
555 
556  sha512_file( filename, output, 0);
557  hexify( hash_str, output, 64 );
558 
559  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
560 }
561 #endif /* POLARSSL_SHA512_C */
562 #endif /* POLARSSL_FS_IO */
563 
564 #ifdef POLARSSL_SHA1_C
565 #ifdef POLARSSL_SELF_TEST
566 void test_suite_sha1_selftest()
567 {
568  TEST_ASSERT( sha1_self_test( 0 ) == 0 );
569 }
570 #endif /* POLARSSL_SHA1_C */
571 #endif /* POLARSSL_SELF_TEST */
572 
573 #ifdef POLARSSL_SHA256_C
574 #ifdef POLARSSL_SELF_TEST
575 void test_suite_sha256_selftest()
576 {
577  TEST_ASSERT( sha256_self_test( 0 ) == 0 );
578 }
579 #endif /* POLARSSL_SHA256_C */
580 #endif /* POLARSSL_SELF_TEST */
581 
582 #ifdef POLARSSL_SHA512_C
583 #ifdef POLARSSL_SELF_TEST
584 void test_suite_sha512_selftest()
585 {
586  TEST_ASSERT( sha512_self_test( 0 ) == 0 );
587 }
588 #endif /* POLARSSL_SHA512_C */
589 #endif /* POLARSSL_SELF_TEST */
590 
591 
592 
593 
594 int dep_check( char *str )
595 {
596  if( str == NULL )
597  return( 1 );
598 
599  if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
600  {
601 #if defined(POLARSSL_SELF_TEST)
602  return( 0 );
603 #else
604  return( 1 );
605 #endif
606  }
607  if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
608  {
609 #if defined(POLARSSL_SHA256_C)
610  return( 0 );
611 #else
612  return( 1 );
613 #endif
614  }
615  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
616  {
617 #if defined(POLARSSL_SHA1_C)
618  return( 0 );
619 #else
620  return( 1 );
621 #endif
622  }
623  if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
624  {
625 #if defined(POLARSSL_SHA512_C)
626  return( 0 );
627 #else
628  return( 1 );
629 #endif
630  }
631 
632 
633  return( 1 );
634 }
635 
636 int dispatch_test(int cnt, char *params[50])
637 {
638  int ret;
639  ((void) cnt);
640  ((void) params);
641 
642 #if defined(TEST_SUITE_ACTIVE)
643  if( strcmp( params[0], "sha1" ) == 0 )
644  {
645  #ifdef POLARSSL_SHA1_C
646 
647  char *param1 = params[1];
648  char *param2 = params[2];
649 
650  if( cnt != 3 )
651  {
652  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
653  return( 2 );
654  }
655 
656  if( verify_string( &param1 ) != 0 ) return( 2 );
657  if( verify_string( &param2 ) != 0 ) return( 2 );
658 
659  test_suite_sha1( param1, param2 );
660  return ( 0 );
661  #endif /* POLARSSL_SHA1_C */
662 
663  return ( 3 );
664  }
665  else
666  if( strcmp( params[0], "sha224" ) == 0 )
667  {
668  #ifdef POLARSSL_SHA256_C
669 
670  char *param1 = params[1];
671  char *param2 = params[2];
672 
673  if( cnt != 3 )
674  {
675  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
676  return( 2 );
677  }
678 
679  if( verify_string( &param1 ) != 0 ) return( 2 );
680  if( verify_string( &param2 ) != 0 ) return( 2 );
681 
682  test_suite_sha224( param1, param2 );
683  return ( 0 );
684  #endif /* POLARSSL_SHA256_C */
685 
686  return ( 3 );
687  }
688  else
689  if( strcmp( params[0], "sha256" ) == 0 )
690  {
691  #ifdef POLARSSL_SHA256_C
692 
693  char *param1 = params[1];
694  char *param2 = params[2];
695 
696  if( cnt != 3 )
697  {
698  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
699  return( 2 );
700  }
701 
702  if( verify_string( &param1 ) != 0 ) return( 2 );
703  if( verify_string( &param2 ) != 0 ) return( 2 );
704 
705  test_suite_sha256( param1, param2 );
706  return ( 0 );
707  #endif /* POLARSSL_SHA256_C */
708 
709  return ( 3 );
710  }
711  else
712  if( strcmp( params[0], "sha384" ) == 0 )
713  {
714  #ifdef POLARSSL_SHA512_C
715 
716  char *param1 = params[1];
717  char *param2 = params[2];
718 
719  if( cnt != 3 )
720  {
721  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
722  return( 2 );
723  }
724 
725  if( verify_string( &param1 ) != 0 ) return( 2 );
726  if( verify_string( &param2 ) != 0 ) return( 2 );
727 
728  test_suite_sha384( param1, param2 );
729  return ( 0 );
730  #endif /* POLARSSL_SHA512_C */
731 
732  return ( 3 );
733  }
734  else
735  if( strcmp( params[0], "sha512" ) == 0 )
736  {
737  #ifdef POLARSSL_SHA512_C
738 
739  char *param1 = params[1];
740  char *param2 = params[2];
741 
742  if( cnt != 3 )
743  {
744  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
745  return( 2 );
746  }
747 
748  if( verify_string( &param1 ) != 0 ) return( 2 );
749  if( verify_string( &param2 ) != 0 ) return( 2 );
750 
751  test_suite_sha512( param1, param2 );
752  return ( 0 );
753  #endif /* POLARSSL_SHA512_C */
754 
755  return ( 3 );
756  }
757  else
758  if( strcmp( params[0], "sha1_file" ) == 0 )
759  {
760  #ifdef POLARSSL_SHA1_C
761  #ifdef POLARSSL_FS_IO
762 
763  char *param1 = params[1];
764  char *param2 = params[2];
765 
766  if( cnt != 3 )
767  {
768  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
769  return( 2 );
770  }
771 
772  if( verify_string( &param1 ) != 0 ) return( 2 );
773  if( verify_string( &param2 ) != 0 ) return( 2 );
774 
775  test_suite_sha1_file( param1, param2 );
776  return ( 0 );
777  #endif /* POLARSSL_SHA1_C */
778  #endif /* POLARSSL_FS_IO */
779 
780  return ( 3 );
781  }
782  else
783  if( strcmp( params[0], "sha224_file" ) == 0 )
784  {
785  #ifdef POLARSSL_SHA256_C
786  #ifdef POLARSSL_FS_IO
787 
788  char *param1 = params[1];
789  char *param2 = params[2];
790 
791  if( cnt != 3 )
792  {
793  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
794  return( 2 );
795  }
796 
797  if( verify_string( &param1 ) != 0 ) return( 2 );
798  if( verify_string( &param2 ) != 0 ) return( 2 );
799 
800  test_suite_sha224_file( param1, param2 );
801  return ( 0 );
802  #endif /* POLARSSL_SHA256_C */
803  #endif /* POLARSSL_FS_IO */
804 
805  return ( 3 );
806  }
807  else
808  if( strcmp( params[0], "sha256_file" ) == 0 )
809  {
810  #ifdef POLARSSL_SHA256_C
811  #ifdef POLARSSL_FS_IO
812 
813  char *param1 = params[1];
814  char *param2 = params[2];
815 
816  if( cnt != 3 )
817  {
818  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
819  return( 2 );
820  }
821 
822  if( verify_string( &param1 ) != 0 ) return( 2 );
823  if( verify_string( &param2 ) != 0 ) return( 2 );
824 
825  test_suite_sha256_file( param1, param2 );
826  return ( 0 );
827  #endif /* POLARSSL_SHA256_C */
828  #endif /* POLARSSL_FS_IO */
829 
830  return ( 3 );
831  }
832  else
833  if( strcmp( params[0], "sha384_file" ) == 0 )
834  {
835  #ifdef POLARSSL_SHA512_C
836  #ifdef POLARSSL_FS_IO
837 
838  char *param1 = params[1];
839  char *param2 = params[2];
840 
841  if( cnt != 3 )
842  {
843  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
844  return( 2 );
845  }
846 
847  if( verify_string( &param1 ) != 0 ) return( 2 );
848  if( verify_string( &param2 ) != 0 ) return( 2 );
849 
850  test_suite_sha384_file( param1, param2 );
851  return ( 0 );
852  #endif /* POLARSSL_SHA512_C */
853  #endif /* POLARSSL_FS_IO */
854 
855  return ( 3 );
856  }
857  else
858  if( strcmp( params[0], "sha512_file" ) == 0 )
859  {
860  #ifdef POLARSSL_SHA512_C
861  #ifdef POLARSSL_FS_IO
862 
863  char *param1 = params[1];
864  char *param2 = params[2];
865 
866  if( cnt != 3 )
867  {
868  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
869  return( 2 );
870  }
871 
872  if( verify_string( &param1 ) != 0 ) return( 2 );
873  if( verify_string( &param2 ) != 0 ) return( 2 );
874 
875  test_suite_sha512_file( param1, param2 );
876  return ( 0 );
877  #endif /* POLARSSL_SHA512_C */
878  #endif /* POLARSSL_FS_IO */
879 
880  return ( 3 );
881  }
882  else
883  if( strcmp( params[0], "sha1_selftest" ) == 0 )
884  {
885  #ifdef POLARSSL_SHA1_C
886  #ifdef POLARSSL_SELF_TEST
887 
888 
889  if( cnt != 1 )
890  {
891  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
892  return( 2 );
893  }
894 
895 
896  test_suite_sha1_selftest( );
897  return ( 0 );
898  #endif /* POLARSSL_SHA1_C */
899  #endif /* POLARSSL_SELF_TEST */
900 
901  return ( 3 );
902  }
903  else
904  if( strcmp( params[0], "sha256_selftest" ) == 0 )
905  {
906  #ifdef POLARSSL_SHA256_C
907  #ifdef POLARSSL_SELF_TEST
908 
909 
910  if( cnt != 1 )
911  {
912  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
913  return( 2 );
914  }
915 
916 
917  test_suite_sha256_selftest( );
918  return ( 0 );
919  #endif /* POLARSSL_SHA256_C */
920  #endif /* POLARSSL_SELF_TEST */
921 
922  return ( 3 );
923  }
924  else
925  if( strcmp( params[0], "sha512_selftest" ) == 0 )
926  {
927  #ifdef POLARSSL_SHA512_C
928  #ifdef POLARSSL_SELF_TEST
929 
930 
931  if( cnt != 1 )
932  {
933  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
934  return( 2 );
935  }
936 
937 
938  test_suite_sha512_selftest( );
939  return ( 0 );
940  #endif /* POLARSSL_SHA512_C */
941  #endif /* POLARSSL_SELF_TEST */
942 
943  return ( 3 );
944  }
945  else
946 
947  {
948  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
949  fflush( stdout );
950  return( 1 );
951  }
952 #else
953  return( 3 );
954 #endif
955  return( ret );
956 }
957 
958 int get_line( FILE *f, char *buf, size_t len )
959 {
960  char *ret;
961 
962  ret = fgets( buf, len, f );
963  if( ret == NULL )
964  return( -1 );
965 
966  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
967  buf[strlen(buf) - 1] = '\0';
968  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
969  buf[strlen(buf) - 1] = '\0';
970 
971  return( 0 );
972 }
973 
974 int parse_arguments( char *buf, size_t len, char *params[50] )
975 {
976  int cnt = 0, i;
977  char *cur = buf;
978  char *p = buf, *q;
979 
980  params[cnt++] = cur;
981 
982  while( *p != '\0' && p < buf + len )
983  {
984  if( *p == '\\' )
985  {
986  *p++;
987  *p++;
988  continue;
989  }
990  if( *p == ':' )
991  {
992  if( p + 1 < buf + len )
993  {
994  cur = p + 1;
995  params[cnt++] = cur;
996  }
997  *p = '\0';
998  }
999 
1000  *p++;
1001  }
1002 
1003  // Replace newlines, question marks and colons in strings
1004  for( i = 0; i < cnt; i++ )
1005  {
1006  p = params[i];
1007  q = params[i];
1008 
1009  while( *p != '\0' )
1010  {
1011  if( *p == '\\' && *(p + 1) == 'n' )
1012  {
1013  p += 2;
1014  *(q++) = '\n';
1015  }
1016  else if( *p == '\\' && *(p + 1) == ':' )
1017  {
1018  p += 2;
1019  *(q++) = ':';
1020  }
1021  else if( *p == '\\' && *(p + 1) == '?' )
1022  {
1023  p += 2;
1024  *(q++) = '?';
1025  }
1026  else
1027  *(q++) = *(p++);
1028  }
1029  *q = '\0';
1030  }
1031 
1032  return( cnt );
1033 }
1034 
1035 int main()
1036 {
1037  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1038  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_shax.data";
1039  FILE *file;
1040  char buf[5000];
1041  char *params[50];
1042 
1043 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1044  unsigned char alloc_buf[1000000];
1045  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1046 #endif
1047 
1048  file = fopen( filename, "r" );
1049  if( file == NULL )
1050  {
1051  fprintf( stderr, "Failed to open\n" );
1052  return( 1 );
1053  }
1054 
1055  while( !feof( file ) )
1056  {
1057  int skip = 0;
1058 
1059  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1060  break;
1061  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1062  fprintf( stdout, " " );
1063  for( i = strlen( buf ) + 1; i < 67; i++ )
1064  fprintf( stdout, "." );
1065  fprintf( stdout, " " );
1066  fflush( stdout );
1067 
1068  total_tests++;
1069 
1070  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1071  break;
1072  cnt = parse_arguments( buf, strlen(buf), params );
1073 
1074  if( strcmp( params[0], "depends_on" ) == 0 )
1075  {
1076  for( i = 1; i < cnt; i++ )
1077  if( dep_check( params[i] ) != 0 )
1078  skip = 1;
1079 
1080  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1081  break;
1082  cnt = parse_arguments( buf, strlen(buf), params );
1083  }
1084 
1085  if( skip == 0 )
1086  {
1087  test_errors = 0;
1088  ret = dispatch_test( cnt, params );
1089  }
1090 
1091  if( skip == 1 || ret == 3 )
1092  {
1093  total_skipped++;
1094  fprintf( stdout, "----\n" );
1095  fflush( stdout );
1096  }
1097  else if( ret == 0 && test_errors == 0 )
1098  {
1099  fprintf( stdout, "PASS\n" );
1100  fflush( stdout );
1101  }
1102  else if( ret == 2 )
1103  {
1104  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1105  fclose(file);
1106  exit( 2 );
1107  }
1108  else
1109  total_errors++;
1110 
1111  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1112  break;
1113  if( strlen(buf) != 0 )
1114  {
1115  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1116  return( 1 );
1117  }
1118  }
1119  fclose(file);
1120 
1121  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1122  if( total_errors == 0 )
1123  fprintf( stdout, "PASSED" );
1124  else
1125  fprintf( stdout, "FAILED" );
1126 
1127  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1128  total_tests - total_errors, total_tests, total_skipped );
1129 
1130 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1131 #if defined(POLARSSL_MEMORY_DEBUG)
1132  memory_buffer_alloc_status();
1133 #endif
1134  memory_buffer_alloc_free();
1135 #endif
1136 
1137  return( total_errors != 0 );
1138 }
1139 
1140