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