PolarSSL v1.3.9
test_suite_shax.c
Go to the documentation of this file.
1 #if !defined(POLARSSL_CONFIG_FILE)
2 #include <polarssl/config.h>
3 #else
4 #include POLARSSL_CONFIG_FILE
5 #endif
6 
7 
8 #include <polarssl/sha1.h>
9 #include <polarssl/sha256.h>
10 #include <polarssl/sha512.h>
11 
12 
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14 #include "polarssl/memory.h"
15 #endif
16 
17 #if defined(POLARSSL_PLATFORM_C)
18 #include "polarssl/platform.h"
19 #else
20 #define polarssl_malloc malloc
21 #define polarssl_free free
22 #endif
23 
24 #ifdef _MSC_VER
25 #include <basetsd.h>
26 typedef UINT32 uint32_t;
27 #else
28 #include <inttypes.h>
29 #endif
30 
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 /*
36  * 32-bit integer manipulation macros (big endian)
37  */
38 #ifndef GET_UINT32_BE
39 #define GET_UINT32_BE(n,b,i) \
40 { \
41  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
42  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
43  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
44  | ( (uint32_t) (b)[(i) + 3] ); \
45 }
46 #endif
47 
48 #ifndef PUT_UINT32_BE
49 #define PUT_UINT32_BE(n,b,i) \
50 { \
51  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
52  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
53  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
54  (b)[(i) + 3] = (unsigned char) ( (n) ); \
55 }
56 #endif
57 
58 static int unhexify(unsigned char *obuf, const char *ibuf)
59 {
60  unsigned char c, c2;
61  int len = strlen(ibuf) / 2;
62  assert(!(strlen(ibuf) %1)); // must be even number of bytes
63 
64  while (*ibuf != 0)
65  {
66  c = *ibuf++;
67  if( c >= '0' && c <= '9' )
68  c -= '0';
69  else if( c >= 'a' && c <= 'f' )
70  c -= 'a' - 10;
71  else if( c >= 'A' && c <= 'F' )
72  c -= 'A' - 10;
73  else
74  assert( 0 );
75 
76  c2 = *ibuf++;
77  if( c2 >= '0' && c2 <= '9' )
78  c2 -= '0';
79  else if( c2 >= 'a' && c2 <= 'f' )
80  c2 -= 'a' - 10;
81  else if( c2 >= 'A' && c2 <= 'F' )
82  c2 -= 'A' - 10;
83  else
84  assert( 0 );
85 
86  *obuf++ = ( c << 4 ) | c2;
87  }
88 
89  return len;
90 }
91 
92 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
93 {
94  unsigned char l, h;
95 
96  while (len != 0)
97  {
98  h = (*ibuf) / 16;
99  l = (*ibuf) % 16;
100 
101  if( h < 10 )
102  *obuf++ = '0' + h;
103  else
104  *obuf++ = 'a' + h - 10;
105 
106  if( l < 10 )
107  *obuf++ = '0' + l;
108  else
109  *obuf++ = 'a' + l - 10;
110 
111  ++ibuf;
112  len--;
113  }
114 }
115 
123 static unsigned char *zero_alloc( size_t len )
124 {
125  void *p;
126  size_t actual_len = len != 0 ? len : 1;
127 
128  p = polarssl_malloc( actual_len );
129  assert( p != NULL );
130 
131  memset( p, 0x00, actual_len );
132 
133  return( p );
134 }
135 
146 static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
147 {
148  unsigned char *obuf;
149 
150  *olen = strlen(ibuf) / 2;
151 
152  if( *olen == 0 )
153  return( zero_alloc( *olen ) );
154 
155  obuf = polarssl_malloc( *olen );
156  assert( obuf != NULL );
157 
158  (void) unhexify( obuf, ibuf );
159 
160  return( obuf );
161 }
162 
172 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
173 {
174 #if !defined(__OpenBSD__)
175  size_t i;
176 
177  if( rng_state != NULL )
178  rng_state = NULL;
179 
180  for( i = 0; i < len; ++i )
181  output[i] = rand();
182 #else
183  if( rng_state != NULL )
184  rng_state = NULL;
185 
186  arc4random_buf( output, len );
187 #endif /* !OpenBSD */
188 
189  return( 0 );
190 }
191 
197 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
198 {
199  if( rng_state != NULL )
200  rng_state = NULL;
201 
202  memset( output, 0, len );
203 
204  return( 0 );
205 }
206 
207 typedef struct
208 {
209  unsigned char *buf;
210  size_t length;
211 } rnd_buf_info;
212 
224 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
225 {
226  rnd_buf_info *info = (rnd_buf_info *) rng_state;
227  size_t use_len;
228 
229  if( rng_state == NULL )
230  return( rnd_std_rand( NULL, output, len ) );
231 
232  use_len = len;
233  if( len > info->length )
234  use_len = info->length;
235 
236  if( use_len )
237  {
238  memcpy( output, info->buf, use_len );
239  info->buf += use_len;
240  info->length -= use_len;
241  }
242 
243  if( len - use_len > 0 )
244  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
245 
246  return( 0 );
247 }
248 
256 typedef struct
257 {
258  uint32_t key[16];
259  uint32_t v0, v1;
261 
270 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
271 {
272  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
273  uint32_t i, *k, sum, delta=0x9E3779B9;
274  unsigned char result[4], *out = output;
275 
276  if( rng_state == NULL )
277  return( rnd_std_rand( NULL, output, len ) );
278 
279  k = info->key;
280 
281  while( len > 0 )
282  {
283  size_t use_len = ( len > 4 ) ? 4 : len;
284  sum = 0;
285 
286  for( i = 0; i < 32; i++ )
287  {
288  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
289  sum += delta;
290  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
291  }
292 
293  PUT_UINT32_BE( info->v0, result, 0 );
294  memcpy( out, result, use_len );
295  len -= use_len;
296  out += 4;
297  }
298 
299  return( 0 );
300 }
301 
302 
303 #include <stdio.h>
304 #include <string.h>
305 
306 #if defined(POLARSSL_PLATFORM_C)
307 #include "polarssl/platform.h"
308 #else
309 #define polarssl_printf printf
310 #define polarssl_malloc malloc
311 #define polarssl_free free
312 #endif
313 
314 static int test_errors = 0;
315 
316 
317 #define TEST_SUITE_ACTIVE
318 
319 static int test_assert( int correct, const char *test )
320 {
321  if( correct )
322  return( 0 );
323 
324  test_errors++;
325  if( test_errors == 1 )
326  printf( "FAILED\n" );
327  printf( " %s\n", test );
328 
329  return( 1 );
330 }
331 
332 #define TEST_ASSERT( TEST ) \
333  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
334  if( test_errors) goto exit; \
335  } while (0)
336 
337 int verify_string( char **str )
338 {
339  if( (*str)[0] != '"' ||
340  (*str)[strlen( *str ) - 1] != '"' )
341  {
342  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
343  return( -1 );
344  }
345 
346  (*str)++;
347  (*str)[strlen( *str ) - 1] = '\0';
348 
349  return( 0 );
350 }
351 
352 int verify_int( char *str, int *value )
353 {
354  size_t i;
355  int minus = 0;
356  int digits = 1;
357  int hex = 0;
358 
359  for( i = 0; i < strlen( str ); i++ )
360  {
361  if( i == 0 && str[i] == '-' )
362  {
363  minus = 1;
364  continue;
365  }
366 
367  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
368  str[i - 1] == '0' && str[i] == 'x' )
369  {
370  hex = 1;
371  continue;
372  }
373 
374  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
375  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
376  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
377  {
378  digits = 0;
379  break;
380  }
381  }
382 
383  if( digits )
384  {
385  if( hex )
386  *value = strtol( str, NULL, 16 );
387  else
388  *value = strtol( str, NULL, 10 );
389 
390  return( 0 );
391  }
392 
393 
394 
395  printf( "Expected integer for parameter and got: %s\n", str );
396  return( -1 );
397 }
398 
399 #ifdef POLARSSL_SHA1_C
400 void test_suite_sha1( char *hex_src_string, char *hex_hash_string )
401 {
402  unsigned char src_str[10000];
403  unsigned char hash_str[10000];
404  unsigned char output[41];
405  int src_len;
406 
407  memset(src_str, 0x00, 10000);
408  memset(hash_str, 0x00, 10000);
409  memset(output, 0x00, 41);
410 
411  src_len = unhexify( src_str, hex_src_string );
412 
413  sha1( src_str, src_len, output );
414  hexify( hash_str, output, 20 );
415 
416  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
417 
418 exit:
419  return;
420 }
421 #endif /* POLARSSL_SHA1_C */
422 
423 #ifdef POLARSSL_SHA256_C
424 void test_suite_sha224(char *hex_src_string, char *hex_hash_string )
425 {
426  unsigned char src_str[10000];
427  unsigned char hash_str[10000];
428  unsigned char output[57];
429  int src_len;
430 
431  memset(src_str, 0x00, 10000);
432  memset(hash_str, 0x00, 10000);
433  memset(output, 0x00, 57);
434 
435  src_len = unhexify( src_str, hex_src_string );
436 
437  sha256( src_str, src_len, output, 1 );
438  hexify( hash_str, output, 28 );
439 
440  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
441 
442 exit:
443  return;
444 }
445 #endif /* POLARSSL_SHA256_C */
446 
447 #ifdef POLARSSL_SHA256_C
448 void test_suite_sha256(char *hex_src_string, char *hex_hash_string )
449 {
450  unsigned char src_str[10000];
451  unsigned char hash_str[10000];
452  unsigned char output[65];
453  int src_len;
454 
455  memset(src_str, 0x00, 10000);
456  memset(hash_str, 0x00, 10000);
457  memset(output, 0x00, 65);
458 
459  src_len = unhexify( src_str, hex_src_string );
460 
461  sha256( src_str, src_len, output, 0 );
462  hexify( hash_str, output, 32 );
463 
464  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
465 
466 exit:
467  return;
468 }
469 #endif /* POLARSSL_SHA256_C */
470 
471 #ifdef POLARSSL_SHA512_C
472 void test_suite_sha384(char *hex_src_string, char *hex_hash_string )
473 {
474  unsigned char src_str[10000];
475  unsigned char hash_str[10000];
476  unsigned char output[97];
477  int src_len;
478 
479  memset(src_str, 0x00, 10000);
480  memset(hash_str, 0x00, 10000);
481  memset(output, 0x00, 97);
482 
483  src_len = unhexify( src_str, hex_src_string );
484 
485  sha512( src_str, src_len, output, 1 );
486  hexify( hash_str, output, 48 );
487 
488  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
489 
490 exit:
491  return;
492 }
493 #endif /* POLARSSL_SHA512_C */
494 
495 #ifdef POLARSSL_SHA512_C
496 void test_suite_sha512(char *hex_src_string, char *hex_hash_string )
497 {
498  unsigned char src_str[10000];
499  unsigned char hash_str[10000];
500  unsigned char output[129];
501  int src_len;
502 
503  memset(src_str, 0x00, 10000);
504  memset(hash_str, 0x00, 10000);
505  memset(output, 0x00, 129);
506 
507  src_len = unhexify( src_str, hex_src_string );
508 
509  sha512( src_str, src_len, output, 0);
510  hexify( hash_str, output, 64 );
511 
512  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
513 
514 exit:
515  return;
516 }
517 #endif /* POLARSSL_SHA512_C */
518 
519 #ifdef POLARSSL_SHA1_C
520 #ifdef POLARSSL_FS_IO
521 void test_suite_sha1_file( char *filename, char *hex_hash_string )
522 {
523  unsigned char hash_str[41];
524  unsigned char output[21];
525 
526  memset(hash_str, 0x00, 41);
527  memset(output, 0x00, 21);
528 
529  sha1_file( filename, output);
530  hexify( hash_str, output, 20 );
531 
532  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
533 
534 exit:
535  return;
536 }
537 #endif /* POLARSSL_SHA1_C */
538 #endif /* POLARSSL_FS_IO */
539 
540 #ifdef POLARSSL_SHA256_C
541 #ifdef POLARSSL_FS_IO
542 void test_suite_sha224_file( char *filename, char *hex_hash_string )
543 {
544  unsigned char hash_str[57];
545  unsigned char output[29];
546 
547  memset(hash_str, 0x00, 57);
548  memset(output, 0x00, 29);
549 
550  sha256_file( filename, output, 1);
551  hexify( hash_str, output, 28 );
552 
553  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
554 
555 exit:
556  return;
557 }
558 #endif /* POLARSSL_SHA256_C */
559 #endif /* POLARSSL_FS_IO */
560 
561 #ifdef POLARSSL_SHA256_C
562 #ifdef POLARSSL_FS_IO
563 void test_suite_sha256_file( char *filename, char *hex_hash_string )
564 {
565  unsigned char hash_str[65];
566  unsigned char output[33];
567 
568  memset(hash_str, 0x00, 65);
569  memset(output, 0x00, 33);
570 
571  sha256_file( filename, output, 0);
572  hexify( hash_str, output, 32 );
573 
574  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
575 
576 exit:
577  return;
578 }
579 #endif /* POLARSSL_SHA256_C */
580 #endif /* POLARSSL_FS_IO */
581 
582 #ifdef POLARSSL_SHA512_C
583 #ifdef POLARSSL_FS_IO
584 void test_suite_sha384_file( char *filename, char *hex_hash_string )
585 {
586  unsigned char hash_str[97];
587  unsigned char output[49];
588 
589  memset(hash_str, 0x00, 97);
590  memset(output, 0x00, 49);
591 
592  sha512_file( filename, output, 1);
593  hexify( hash_str, output, 48 );
594 
595  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
596 
597 exit:
598  return;
599 }
600 #endif /* POLARSSL_SHA512_C */
601 #endif /* POLARSSL_FS_IO */
602 
603 #ifdef POLARSSL_SHA512_C
604 #ifdef POLARSSL_FS_IO
605 void test_suite_sha512_file( char *filename, char *hex_hash_string )
606 {
607  unsigned char hash_str[129];
608  unsigned char output[65];
609 
610  memset(hash_str, 0x00, 129);
611  memset(output, 0x00, 65);
612 
613  sha512_file( filename, output, 0);
614  hexify( hash_str, output, 64 );
615 
616  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
617 
618 exit:
619  return;
620 }
621 #endif /* POLARSSL_SHA512_C */
622 #endif /* POLARSSL_FS_IO */
623 
624 #ifdef POLARSSL_SHA1_C
625 #ifdef POLARSSL_SELF_TEST
626 void test_suite_sha1_selftest()
627 {
628  TEST_ASSERT( sha1_self_test( 0 ) == 0 );
629 
630 exit:
631  return;
632 }
633 #endif /* POLARSSL_SHA1_C */
634 #endif /* POLARSSL_SELF_TEST */
635 
636 #ifdef POLARSSL_SHA256_C
637 #ifdef POLARSSL_SELF_TEST
638 void test_suite_sha256_selftest()
639 {
640  TEST_ASSERT( sha256_self_test( 0 ) == 0 );
641 
642 exit:
643  return;
644 }
645 #endif /* POLARSSL_SHA256_C */
646 #endif /* POLARSSL_SELF_TEST */
647 
648 #ifdef POLARSSL_SHA512_C
649 #ifdef POLARSSL_SELF_TEST
650 void test_suite_sha512_selftest()
651 {
652  TEST_ASSERT( sha512_self_test( 0 ) == 0 );
653 
654 exit:
655  return;
656 }
657 #endif /* POLARSSL_SHA512_C */
658 #endif /* POLARSSL_SELF_TEST */
659 
660 
661 
662 
663 int dep_check( char *str )
664 {
665  if( str == NULL )
666  return( 1 );
667 
668  if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
669  {
670 #if defined(POLARSSL_SHA512_C)
671  return( 0 );
672 #else
673  return( 1 );
674 #endif
675  }
676  if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
677  {
678 #if defined(POLARSSL_SELF_TEST)
679  return( 0 );
680 #else
681  return( 1 );
682 #endif
683  }
684  if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
685  {
686 #if defined(POLARSSL_SHA256_C)
687  return( 0 );
688 #else
689  return( 1 );
690 #endif
691  }
692  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
693  {
694 #if defined(POLARSSL_SHA1_C)
695  return( 0 );
696 #else
697  return( 1 );
698 #endif
699  }
700 
701 
702  return( 1 );
703 }
704 
705 int dispatch_test(int cnt, char *params[50])
706 {
707  int ret;
708  ((void) cnt);
709  ((void) params);
710 
711 #if defined(TEST_SUITE_ACTIVE)
712  if( strcmp( params[0], "sha1" ) == 0 )
713  {
714  #ifdef POLARSSL_SHA1_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_sha1( param1, param2 );
729  return ( 0 );
730  #endif /* POLARSSL_SHA1_C */
731 
732  return ( 3 );
733  }
734  else
735  if( strcmp( params[0], "sha224" ) == 0 )
736  {
737  #ifdef POLARSSL_SHA256_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_sha224( param1, param2 );
752  return ( 0 );
753  #endif /* POLARSSL_SHA256_C */
754 
755  return ( 3 );
756  }
757  else
758  if( strcmp( params[0], "sha256" ) == 0 )
759  {
760  #ifdef POLARSSL_SHA256_C
761 
762  char *param1 = params[1];
763  char *param2 = params[2];
764 
765  if( cnt != 3 )
766  {
767  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
768  return( 2 );
769  }
770 
771  if( verify_string( &param1 ) != 0 ) return( 2 );
772  if( verify_string( &param2 ) != 0 ) return( 2 );
773 
774  test_suite_sha256( param1, param2 );
775  return ( 0 );
776  #endif /* POLARSSL_SHA256_C */
777 
778  return ( 3 );
779  }
780  else
781  if( strcmp( params[0], "sha384" ) == 0 )
782  {
783  #ifdef POLARSSL_SHA512_C
784 
785  char *param1 = params[1];
786  char *param2 = params[2];
787 
788  if( cnt != 3 )
789  {
790  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
791  return( 2 );
792  }
793 
794  if( verify_string( &param1 ) != 0 ) return( 2 );
795  if( verify_string( &param2 ) != 0 ) return( 2 );
796 
797  test_suite_sha384( param1, param2 );
798  return ( 0 );
799  #endif /* POLARSSL_SHA512_C */
800 
801  return ( 3 );
802  }
803  else
804  if( strcmp( params[0], "sha512" ) == 0 )
805  {
806  #ifdef POLARSSL_SHA512_C
807 
808  char *param1 = params[1];
809  char *param2 = params[2];
810 
811  if( cnt != 3 )
812  {
813  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
814  return( 2 );
815  }
816 
817  if( verify_string( &param1 ) != 0 ) return( 2 );
818  if( verify_string( &param2 ) != 0 ) return( 2 );
819 
820  test_suite_sha512( param1, param2 );
821  return ( 0 );
822  #endif /* POLARSSL_SHA512_C */
823 
824  return ( 3 );
825  }
826  else
827  if( strcmp( params[0], "sha1_file" ) == 0 )
828  {
829  #ifdef POLARSSL_SHA1_C
830  #ifdef POLARSSL_FS_IO
831 
832  char *param1 = params[1];
833  char *param2 = params[2];
834 
835  if( cnt != 3 )
836  {
837  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
838  return( 2 );
839  }
840 
841  if( verify_string( &param1 ) != 0 ) return( 2 );
842  if( verify_string( &param2 ) != 0 ) return( 2 );
843 
844  test_suite_sha1_file( param1, param2 );
845  return ( 0 );
846  #endif /* POLARSSL_SHA1_C */
847  #endif /* POLARSSL_FS_IO */
848 
849  return ( 3 );
850  }
851  else
852  if( strcmp( params[0], "sha224_file" ) == 0 )
853  {
854  #ifdef POLARSSL_SHA256_C
855  #ifdef POLARSSL_FS_IO
856 
857  char *param1 = params[1];
858  char *param2 = params[2];
859 
860  if( cnt != 3 )
861  {
862  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
863  return( 2 );
864  }
865 
866  if( verify_string( &param1 ) != 0 ) return( 2 );
867  if( verify_string( &param2 ) != 0 ) return( 2 );
868 
869  test_suite_sha224_file( param1, param2 );
870  return ( 0 );
871  #endif /* POLARSSL_SHA256_C */
872  #endif /* POLARSSL_FS_IO */
873 
874  return ( 3 );
875  }
876  else
877  if( strcmp( params[0], "sha256_file" ) == 0 )
878  {
879  #ifdef POLARSSL_SHA256_C
880  #ifdef POLARSSL_FS_IO
881 
882  char *param1 = params[1];
883  char *param2 = params[2];
884 
885  if( cnt != 3 )
886  {
887  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
888  return( 2 );
889  }
890 
891  if( verify_string( &param1 ) != 0 ) return( 2 );
892  if( verify_string( &param2 ) != 0 ) return( 2 );
893 
894  test_suite_sha256_file( param1, param2 );
895  return ( 0 );
896  #endif /* POLARSSL_SHA256_C */
897  #endif /* POLARSSL_FS_IO */
898 
899  return ( 3 );
900  }
901  else
902  if( strcmp( params[0], "sha384_file" ) == 0 )
903  {
904  #ifdef POLARSSL_SHA512_C
905  #ifdef POLARSSL_FS_IO
906 
907  char *param1 = params[1];
908  char *param2 = params[2];
909 
910  if( cnt != 3 )
911  {
912  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
913  return( 2 );
914  }
915 
916  if( verify_string( &param1 ) != 0 ) return( 2 );
917  if( verify_string( &param2 ) != 0 ) return( 2 );
918 
919  test_suite_sha384_file( param1, param2 );
920  return ( 0 );
921  #endif /* POLARSSL_SHA512_C */
922  #endif /* POLARSSL_FS_IO */
923 
924  return ( 3 );
925  }
926  else
927  if( strcmp( params[0], "sha512_file" ) == 0 )
928  {
929  #ifdef POLARSSL_SHA512_C
930  #ifdef POLARSSL_FS_IO
931 
932  char *param1 = params[1];
933  char *param2 = params[2];
934 
935  if( cnt != 3 )
936  {
937  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
938  return( 2 );
939  }
940 
941  if( verify_string( &param1 ) != 0 ) return( 2 );
942  if( verify_string( &param2 ) != 0 ) return( 2 );
943 
944  test_suite_sha512_file( param1, param2 );
945  return ( 0 );
946  #endif /* POLARSSL_SHA512_C */
947  #endif /* POLARSSL_FS_IO */
948 
949  return ( 3 );
950  }
951  else
952  if( strcmp( params[0], "sha1_selftest" ) == 0 )
953  {
954  #ifdef POLARSSL_SHA1_C
955  #ifdef POLARSSL_SELF_TEST
956 
957 
958  if( cnt != 1 )
959  {
960  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
961  return( 2 );
962  }
963 
964 
965  test_suite_sha1_selftest( );
966  return ( 0 );
967  #endif /* POLARSSL_SHA1_C */
968  #endif /* POLARSSL_SELF_TEST */
969 
970  return ( 3 );
971  }
972  else
973  if( strcmp( params[0], "sha256_selftest" ) == 0 )
974  {
975  #ifdef POLARSSL_SHA256_C
976  #ifdef POLARSSL_SELF_TEST
977 
978 
979  if( cnt != 1 )
980  {
981  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
982  return( 2 );
983  }
984 
985 
986  test_suite_sha256_selftest( );
987  return ( 0 );
988  #endif /* POLARSSL_SHA256_C */
989  #endif /* POLARSSL_SELF_TEST */
990 
991  return ( 3 );
992  }
993  else
994  if( strcmp( params[0], "sha512_selftest" ) == 0 )
995  {
996  #ifdef POLARSSL_SHA512_C
997  #ifdef POLARSSL_SELF_TEST
998 
999 
1000  if( cnt != 1 )
1001  {
1002  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1003  return( 2 );
1004  }
1005 
1006 
1007  test_suite_sha512_selftest( );
1008  return ( 0 );
1009  #endif /* POLARSSL_SHA512_C */
1010  #endif /* POLARSSL_SELF_TEST */
1011 
1012  return ( 3 );
1013  }
1014  else
1015 
1016  {
1017  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1018  fflush( stdout );
1019  return( 1 );
1020  }
1021 #else
1022  return( 3 );
1023 #endif
1024  return( ret );
1025 }
1026 
1027 int get_line( FILE *f, char *buf, size_t len )
1028 {
1029  char *ret;
1030 
1031  ret = fgets( buf, len, f );
1032  if( ret == NULL )
1033  return( -1 );
1034 
1035  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1036  buf[strlen(buf) - 1] = '\0';
1037  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1038  buf[strlen(buf) - 1] = '\0';
1039 
1040  return( 0 );
1041 }
1042 
1043 int parse_arguments( char *buf, size_t len, char *params[50] )
1044 {
1045  int cnt = 0, i;
1046  char *cur = buf;
1047  char *p = buf, *q;
1048 
1049  params[cnt++] = cur;
1050 
1051  while( *p != '\0' && p < buf + len )
1052  {
1053  if( *p == '\\' )
1054  {
1055  p++;
1056  p++;
1057  continue;
1058  }
1059  if( *p == ':' )
1060  {
1061  if( p + 1 < buf + len )
1062  {
1063  cur = p + 1;
1064  params[cnt++] = cur;
1065  }
1066  *p = '\0';
1067  }
1068 
1069  p++;
1070  }
1071 
1072  // Replace newlines, question marks and colons in strings
1073  for( i = 0; i < cnt; i++ )
1074  {
1075  p = params[i];
1076  q = params[i];
1077 
1078  while( *p != '\0' )
1079  {
1080  if( *p == '\\' && *(p + 1) == 'n' )
1081  {
1082  p += 2;
1083  *(q++) = '\n';
1084  }
1085  else if( *p == '\\' && *(p + 1) == ':' )
1086  {
1087  p += 2;
1088  *(q++) = ':';
1089  }
1090  else if( *p == '\\' && *(p + 1) == '?' )
1091  {
1092  p += 2;
1093  *(q++) = '?';
1094  }
1095  else
1096  *(q++) = *(p++);
1097  }
1098  *q = '\0';
1099  }
1100 
1101  return( cnt );
1102 }
1103 
1104 int main()
1105 {
1106  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1107  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.9/tests/suites/test_suite_shax.data";
1108  FILE *file;
1109  char buf[5000];
1110  char *params[50];
1111 
1112 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1113  unsigned char alloc_buf[1000000];
1114  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1115 #endif
1116 
1117  file = fopen( filename, "r" );
1118  if( file == NULL )
1119  {
1120  fprintf( stderr, "Failed to open\n" );
1121  return( 1 );
1122  }
1123 
1124  while( !feof( file ) )
1125  {
1126  int skip = 0;
1127 
1128  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1129  break;
1130  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1131  fprintf( stdout, " " );
1132  for( i = strlen( buf ) + 1; i < 67; i++ )
1133  fprintf( stdout, "." );
1134  fprintf( stdout, " " );
1135  fflush( stdout );
1136 
1137  total_tests++;
1138 
1139  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1140  break;
1141  cnt = parse_arguments( buf, strlen(buf), params );
1142 
1143  if( strcmp( params[0], "depends_on" ) == 0 )
1144  {
1145  for( i = 1; i < cnt; i++ )
1146  if( dep_check( params[i] ) != 0 )
1147  skip = 1;
1148 
1149  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1150  break;
1151  cnt = parse_arguments( buf, strlen(buf), params );
1152  }
1153 
1154  if( skip == 0 )
1155  {
1156  test_errors = 0;
1157  ret = dispatch_test( cnt, params );
1158  }
1159 
1160  if( skip == 1 || ret == 3 )
1161  {
1162  total_skipped++;
1163  fprintf( stdout, "----\n" );
1164  fflush( stdout );
1165  }
1166  else if( ret == 0 && test_errors == 0 )
1167  {
1168  fprintf( stdout, "PASS\n" );
1169  fflush( stdout );
1170  }
1171  else if( ret == 2 )
1172  {
1173  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1174  fclose(file);
1175  exit( 2 );
1176  }
1177  else
1178  total_errors++;
1179 
1180  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1181  break;
1182  if( strlen(buf) != 0 )
1183  {
1184  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1185  return( 1 );
1186  }
1187  }
1188  fclose(file);
1189 
1190  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1191  if( total_errors == 0 )
1192  fprintf( stdout, "PASSED" );
1193  else
1194  fprintf( stdout, "FAILED" );
1195 
1196  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1197  total_tests - total_errors, total_tests, total_skipped );
1198 
1199 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1200 #if defined(POLARSSL_MEMORY_DEBUG)
1201  memory_buffer_alloc_status();
1202 #endif
1204 #endif
1205 
1206  return( total_errors != 0 );
1207 }
1208 
1209 
int sha1_self_test(int verbose)
Checkup routine.
Memory allocation layer (Deprecated to platform layer)
int sha256_file(const char *path, unsigned char output[32], int is224)
Output = SHA-256( file contents )
void sha256(const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
Output = SHA-256( input buffer )
Info structure for the pseudo random function.
void sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
Output = SHA-1( input buffer )
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int verify_string(char **str)
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
int dep_check(char *str)
int dispatch_test(int cnt, char *params[50])
Configuration options (set of defines)
#define polarssl_malloc
PolarSSL Platform abstraction layer.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
#define TEST_ASSERT(TEST)
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
static int test_assert(int correct, const char *test)
int sha256_self_test(int verbose)
Checkup routine.
static int unhexify(unsigned char *obuf, const char *ibuf)
#define PUT_UINT32_BE(n, b, i)
int sha1_file(const char *path, unsigned char output[20])
Output = SHA-1( file contents )
int sha512_self_test(int verbose)
Checkup routine.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
void sha512(const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
Output = SHA-512( input buffer )
int sha512_file(const char *path, unsigned char output[64], int is384)
Output = SHA-512( file contents )
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int parse_arguments(char *buf, size_t len, char *params[50])
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int verify_int(char *str, int *value)
SHA-1 cryptographic hash function.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
unsigned char * buf
SHA-384 and SHA-512 cryptographic hash function.
int main()
static int test_errors
SHA-224 and SHA-256 cryptographic hash function.
int get_line(FILE *f, char *buf, size_t len)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.