PolarSSL v1.3.2
test_suite_mdx.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 
4 #include <polarssl/md2.h>
5 #include <polarssl/md4.h>
6 #include <polarssl/md5.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_MD2_C
366 void test_suite_md2_text( char *text_src_string, char *hex_hash_string )
367 {
368  unsigned char src_str[1000];
369  unsigned char hash_str[1000];
370  unsigned char output[33];
371 
372  memset(src_str, 0x00, 1000);
373  memset(hash_str, 0x00, 1000);
374  memset(output, 0x00, 33);
375 
376  strcpy( (char *) src_str, text_src_string );
377 
378  md2( src_str, strlen( (char *) src_str ), output );
379  hexify( hash_str, output, 16 );
380 
381  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
382 }
383 #endif /* POLARSSL_MD2_C */
384 
385 #ifdef POLARSSL_MD4_C
386 void test_suite_md4_text( char *text_src_string, char *hex_hash_string )
387 {
388  unsigned char src_str[1000];
389  unsigned char hash_str[1000];
390  unsigned char output[33];
391 
392  memset(src_str, 0x00, 1000);
393  memset(hash_str, 0x00, 1000);
394  memset(output, 0x00, 33);
395 
396  strcpy( (char *) src_str, text_src_string );
397 
398  md4( src_str, strlen( (char *) src_str ), output );
399  hexify( hash_str, output, 16 );
400 
401  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
402 }
403 #endif /* POLARSSL_MD4_C */
404 
405 #ifdef POLARSSL_MD5_C
406 void test_suite_md5_text( char *text_src_string, char *hex_hash_string )
407 {
408  unsigned char src_str[1000];
409  unsigned char hash_str[1000];
410  unsigned char output[33];
411 
412  memset(src_str, 0x00, 1000);
413  memset(hash_str, 0x00, 1000);
414  memset(output, 0x00, 33);
415 
416  strcpy( (char *) src_str, text_src_string );
417 
418  md5( src_str, strlen( (char *) src_str ), output );
419  hexify( hash_str, output, 16 );
420 
421  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
422 }
423 #endif /* POLARSSL_MD5_C */
424 
425 #ifdef POLARSSL_MD2_C
426 void test_suite_md2_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
427  char *hex_hash_string )
428 {
429  unsigned char src_str[10000];
430  unsigned char key_str[10000];
431  unsigned char hash_str[10000];
432  unsigned char output[33];
433  int key_len, src_len;
434 
435  memset(src_str, 0x00, 10000);
436  memset(key_str, 0x00, 10000);
437  memset(hash_str, 0x00, 10000);
438  memset(output, 0x00, 33);
439 
440  key_len = unhexify( key_str, hex_key_string );
441  src_len = unhexify( src_str, hex_src_string );
442 
443  md2_hmac( key_str, key_len, src_str, src_len, output );
444  hexify( hash_str, output, 16 );
445 
446  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
447 }
448 #endif /* POLARSSL_MD2_C */
449 
450 #ifdef POLARSSL_MD4_C
451 void test_suite_md4_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
452  char *hex_hash_string )
453 {
454  unsigned char src_str[10000];
455  unsigned char key_str[10000];
456  unsigned char hash_str[10000];
457  unsigned char output[33];
458  int key_len, src_len;
459 
460  memset(src_str, 0x00, 10000);
461  memset(key_str, 0x00, 10000);
462  memset(hash_str, 0x00, 10000);
463  memset(output, 0x00, 33);
464 
465  key_len = unhexify( key_str, hex_key_string );
466  src_len = unhexify( src_str, hex_src_string );
467 
468  md4_hmac( key_str, key_len, src_str, src_len, output );
469  hexify( hash_str, output, 16 );
470 
471  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
472 }
473 #endif /* POLARSSL_MD4_C */
474 
475 #ifdef POLARSSL_MD5_C
476 void test_suite_md5_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
477  char *hex_hash_string )
478 {
479  unsigned char src_str[10000];
480  unsigned char key_str[10000];
481  unsigned char hash_str[10000];
482  unsigned char output[33];
483  int key_len, src_len;
484 
485  memset(src_str, 0x00, 10000);
486  memset(key_str, 0x00, 10000);
487  memset(hash_str, 0x00, 10000);
488  memset(output, 0x00, 33);
489 
490  key_len = unhexify( key_str, hex_key_string );
491  src_len = unhexify( src_str, hex_src_string );
492 
493  md5_hmac( key_str, key_len, src_str, src_len, output );
494  hexify( hash_str, output, 16 );
495 
496  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
497 }
498 #endif /* POLARSSL_MD5_C */
499 
500 #ifdef POLARSSL_MD2_C
501 #ifdef POLARSSL_FS_IO
502 void test_suite_md2_file( char *filename, char *hex_hash_string )
503 {
504  unsigned char hash_str[65];
505  unsigned char output[33];
506 
507  memset(hash_str, 0x00, 65);
508  memset(output, 0x00, 33);
509 
510  md2_file( filename, output);
511  hexify( hash_str, output, 16 );
512 
513  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
514 }
515 #endif /* POLARSSL_MD2_C */
516 #endif /* POLARSSL_FS_IO */
517 
518 #ifdef POLARSSL_MD4_C
519 #ifdef POLARSSL_FS_IO
520 void test_suite_md4_file( char *filename, char *hex_hash_string )
521 {
522  unsigned char hash_str[65];
523  unsigned char output[33];
524 
525  memset(hash_str, 0x00, 65);
526  memset(output, 0x00, 33);
527 
528  md4_file( filename, output);
529  hexify( hash_str, output, 16 );
530 
531  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
532 }
533 #endif /* POLARSSL_MD4_C */
534 #endif /* POLARSSL_FS_IO */
535 
536 #ifdef POLARSSL_MD5_C
537 #ifdef POLARSSL_FS_IO
538 void test_suite_md5_file( char *filename, char *hex_hash_string )
539 {
540  unsigned char hash_str[65];
541  unsigned char output[33];
542 
543  memset(hash_str, 0x00, 65);
544  memset(output, 0x00, 33);
545 
546  md5_file( filename, output);
547  hexify( hash_str, output, 16 );
548 
549  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
550 }
551 #endif /* POLARSSL_MD5_C */
552 #endif /* POLARSSL_FS_IO */
553 
554 #ifdef POLARSSL_MD2_C
555 #ifdef POLARSSL_SELF_TEST
556 void test_suite_md2_selftest()
557 {
558  TEST_ASSERT( md2_self_test( 0 ) == 0 );
559 }
560 #endif /* POLARSSL_MD2_C */
561 #endif /* POLARSSL_SELF_TEST */
562 
563 #ifdef POLARSSL_MD4_C
564 #ifdef POLARSSL_SELF_TEST
565 void test_suite_md4_selftest()
566 {
567  TEST_ASSERT( md4_self_test( 0 ) == 0 );
568 }
569 #endif /* POLARSSL_MD4_C */
570 #endif /* POLARSSL_SELF_TEST */
571 
572 #ifdef POLARSSL_MD5_C
573 #ifdef POLARSSL_SELF_TEST
574 void test_suite_md5_selftest()
575 {
576  TEST_ASSERT( md5_self_test( 0 ) == 0 );
577 }
578 #endif /* POLARSSL_MD5_C */
579 #endif /* POLARSSL_SELF_TEST */
580 
581 
582 
583 
584 int dep_check( char *str )
585 {
586  if( str == NULL )
587  return( 1 );
588 
589  if( strcmp( str, "POLARSSL_MD4_C" ) == 0 )
590  {
591 #if defined(POLARSSL_MD4_C)
592  return( 0 );
593 #else
594  return( 1 );
595 #endif
596  }
597  if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
598  {
599 #if defined(POLARSSL_MD5_C)
600  return( 0 );
601 #else
602  return( 1 );
603 #endif
604  }
605  if( strcmp( str, "POLARSSL_MD2_C" ) == 0 )
606  {
607 #if defined(POLARSSL_MD2_C)
608  return( 0 );
609 #else
610  return( 1 );
611 #endif
612  }
613  if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
614  {
615 #if defined(POLARSSL_SELF_TEST)
616  return( 0 );
617 #else
618  return( 1 );
619 #endif
620  }
621 
622 
623  return( 1 );
624 }
625 
626 int dispatch_test(int cnt, char *params[50])
627 {
628  int ret;
629  ((void) cnt);
630  ((void) params);
631 
632 #if defined(TEST_SUITE_ACTIVE)
633  if( strcmp( params[0], "md2_text" ) == 0 )
634  {
635  #ifdef POLARSSL_MD2_C
636 
637  char *param1 = params[1];
638  char *param2 = params[2];
639 
640  if( cnt != 3 )
641  {
642  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
643  return( 2 );
644  }
645 
646  if( verify_string( &param1 ) != 0 ) return( 2 );
647  if( verify_string( &param2 ) != 0 ) return( 2 );
648 
649  test_suite_md2_text( param1, param2 );
650  return ( 0 );
651  #endif /* POLARSSL_MD2_C */
652 
653  return ( 3 );
654  }
655  else
656  if( strcmp( params[0], "md4_text" ) == 0 )
657  {
658  #ifdef POLARSSL_MD4_C
659 
660  char *param1 = params[1];
661  char *param2 = params[2];
662 
663  if( cnt != 3 )
664  {
665  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
666  return( 2 );
667  }
668 
669  if( verify_string( &param1 ) != 0 ) return( 2 );
670  if( verify_string( &param2 ) != 0 ) return( 2 );
671 
672  test_suite_md4_text( param1, param2 );
673  return ( 0 );
674  #endif /* POLARSSL_MD4_C */
675 
676  return ( 3 );
677  }
678  else
679  if( strcmp( params[0], "md5_text" ) == 0 )
680  {
681  #ifdef POLARSSL_MD5_C
682 
683  char *param1 = params[1];
684  char *param2 = params[2];
685 
686  if( cnt != 3 )
687  {
688  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
689  return( 2 );
690  }
691 
692  if( verify_string( &param1 ) != 0 ) return( 2 );
693  if( verify_string( &param2 ) != 0 ) return( 2 );
694 
695  test_suite_md5_text( param1, param2 );
696  return ( 0 );
697  #endif /* POLARSSL_MD5_C */
698 
699  return ( 3 );
700  }
701  else
702  if( strcmp( params[0], "md2_hmac" ) == 0 )
703  {
704  #ifdef POLARSSL_MD2_C
705 
706  int param1;
707  char *param2 = params[2];
708  char *param3 = params[3];
709  char *param4 = params[4];
710 
711  if( cnt != 5 )
712  {
713  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
714  return( 2 );
715  }
716 
717  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
718  if( verify_string( &param2 ) != 0 ) return( 2 );
719  if( verify_string( &param3 ) != 0 ) return( 2 );
720  if( verify_string( &param4 ) != 0 ) return( 2 );
721 
722  test_suite_md2_hmac( param1, param2, param3, param4 );
723  return ( 0 );
724  #endif /* POLARSSL_MD2_C */
725 
726  return ( 3 );
727  }
728  else
729  if( strcmp( params[0], "md4_hmac" ) == 0 )
730  {
731  #ifdef POLARSSL_MD4_C
732 
733  int param1;
734  char *param2 = params[2];
735  char *param3 = params[3];
736  char *param4 = params[4];
737 
738  if( cnt != 5 )
739  {
740  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
741  return( 2 );
742  }
743 
744  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
745  if( verify_string( &param2 ) != 0 ) return( 2 );
746  if( verify_string( &param3 ) != 0 ) return( 2 );
747  if( verify_string( &param4 ) != 0 ) return( 2 );
748 
749  test_suite_md4_hmac( param1, param2, param3, param4 );
750  return ( 0 );
751  #endif /* POLARSSL_MD4_C */
752 
753  return ( 3 );
754  }
755  else
756  if( strcmp( params[0], "md5_hmac" ) == 0 )
757  {
758  #ifdef POLARSSL_MD5_C
759 
760  int param1;
761  char *param2 = params[2];
762  char *param3 = params[3];
763  char *param4 = params[4];
764 
765  if( cnt != 5 )
766  {
767  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
768  return( 2 );
769  }
770 
771  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
772  if( verify_string( &param2 ) != 0 ) return( 2 );
773  if( verify_string( &param3 ) != 0 ) return( 2 );
774  if( verify_string( &param4 ) != 0 ) return( 2 );
775 
776  test_suite_md5_hmac( param1, param2, param3, param4 );
777  return ( 0 );
778  #endif /* POLARSSL_MD5_C */
779 
780  return ( 3 );
781  }
782  else
783  if( strcmp( params[0], "md2_file" ) == 0 )
784  {
785  #ifdef POLARSSL_MD2_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_md2_file( param1, param2 );
801  return ( 0 );
802  #endif /* POLARSSL_MD2_C */
803  #endif /* POLARSSL_FS_IO */
804 
805  return ( 3 );
806  }
807  else
808  if( strcmp( params[0], "md4_file" ) == 0 )
809  {
810  #ifdef POLARSSL_MD4_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_md4_file( param1, param2 );
826  return ( 0 );
827  #endif /* POLARSSL_MD4_C */
828  #endif /* POLARSSL_FS_IO */
829 
830  return ( 3 );
831  }
832  else
833  if( strcmp( params[0], "md5_file" ) == 0 )
834  {
835  #ifdef POLARSSL_MD5_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_md5_file( param1, param2 );
851  return ( 0 );
852  #endif /* POLARSSL_MD5_C */
853  #endif /* POLARSSL_FS_IO */
854 
855  return ( 3 );
856  }
857  else
858  if( strcmp( params[0], "md2_selftest" ) == 0 )
859  {
860  #ifdef POLARSSL_MD2_C
861  #ifdef POLARSSL_SELF_TEST
862 
863 
864  if( cnt != 1 )
865  {
866  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
867  return( 2 );
868  }
869 
870 
871  test_suite_md2_selftest( );
872  return ( 0 );
873  #endif /* POLARSSL_MD2_C */
874  #endif /* POLARSSL_SELF_TEST */
875 
876  return ( 3 );
877  }
878  else
879  if( strcmp( params[0], "md4_selftest" ) == 0 )
880  {
881  #ifdef POLARSSL_MD4_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_md4_selftest( );
893  return ( 0 );
894  #endif /* POLARSSL_MD4_C */
895  #endif /* POLARSSL_SELF_TEST */
896 
897  return ( 3 );
898  }
899  else
900  if( strcmp( params[0], "md5_selftest" ) == 0 )
901  {
902  #ifdef POLARSSL_MD5_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_md5_selftest( );
914  return ( 0 );
915  #endif /* POLARSSL_MD5_C */
916  #endif /* POLARSSL_SELF_TEST */
917 
918  return ( 3 );
919  }
920  else
921 
922  {
923  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
924  fflush( stdout );
925  return( 1 );
926  }
927 #else
928  return( 3 );
929 #endif
930  return( ret );
931 }
932 
933 int get_line( FILE *f, char *buf, size_t len )
934 {
935  char *ret;
936 
937  ret = fgets( buf, len, f );
938  if( ret == NULL )
939  return( -1 );
940 
941  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
942  buf[strlen(buf) - 1] = '\0';
943  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
944  buf[strlen(buf) - 1] = '\0';
945 
946  return( 0 );
947 }
948 
949 int parse_arguments( char *buf, size_t len, char *params[50] )
950 {
951  int cnt = 0, i;
952  char *cur = buf;
953  char *p = buf, *q;
954 
955  params[cnt++] = cur;
956 
957  while( *p != '\0' && p < buf + len )
958  {
959  if( *p == '\\' )
960  {
961  *p++;
962  *p++;
963  continue;
964  }
965  if( *p == ':' )
966  {
967  if( p + 1 < buf + len )
968  {
969  cur = p + 1;
970  params[cnt++] = cur;
971  }
972  *p = '\0';
973  }
974 
975  *p++;
976  }
977 
978  // Replace newlines, question marks and colons in strings
979  for( i = 0; i < cnt; i++ )
980  {
981  p = params[i];
982  q = params[i];
983 
984  while( *p != '\0' )
985  {
986  if( *p == '\\' && *(p + 1) == 'n' )
987  {
988  p += 2;
989  *(q++) = '\n';
990  }
991  else if( *p == '\\' && *(p + 1) == ':' )
992  {
993  p += 2;
994  *(q++) = ':';
995  }
996  else if( *p == '\\' && *(p + 1) == '?' )
997  {
998  p += 2;
999  *(q++) = '?';
1000  }
1001  else
1002  *(q++) = *(p++);
1003  }
1004  *q = '\0';
1005  }
1006 
1007  return( cnt );
1008 }
1009 
1010 int main()
1011 {
1012  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1013  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_mdx.data";
1014  FILE *file;
1015  char buf[5000];
1016  char *params[50];
1017 
1018 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1019  unsigned char alloc_buf[1000000];
1020  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1021 #endif
1022 
1023  file = fopen( filename, "r" );
1024  if( file == NULL )
1025  {
1026  fprintf( stderr, "Failed to open\n" );
1027  return( 1 );
1028  }
1029 
1030  while( !feof( file ) )
1031  {
1032  int skip = 0;
1033 
1034  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1035  break;
1036  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1037  fprintf( stdout, " " );
1038  for( i = strlen( buf ) + 1; i < 67; i++ )
1039  fprintf( stdout, "." );
1040  fprintf( stdout, " " );
1041  fflush( stdout );
1042 
1043  total_tests++;
1044 
1045  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1046  break;
1047  cnt = parse_arguments( buf, strlen(buf), params );
1048 
1049  if( strcmp( params[0], "depends_on" ) == 0 )
1050  {
1051  for( i = 1; i < cnt; i++ )
1052  if( dep_check( params[i] ) != 0 )
1053  skip = 1;
1054 
1055  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1056  break;
1057  cnt = parse_arguments( buf, strlen(buf), params );
1058  }
1059 
1060  if( skip == 0 )
1061  {
1062  test_errors = 0;
1063  ret = dispatch_test( cnt, params );
1064  }
1065 
1066  if( skip == 1 || ret == 3 )
1067  {
1068  total_skipped++;
1069  fprintf( stdout, "----\n" );
1070  fflush( stdout );
1071  }
1072  else if( ret == 0 && test_errors == 0 )
1073  {
1074  fprintf( stdout, "PASS\n" );
1075  fflush( stdout );
1076  }
1077  else if( ret == 2 )
1078  {
1079  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1080  fclose(file);
1081  exit( 2 );
1082  }
1083  else
1084  total_errors++;
1085 
1086  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1087  break;
1088  if( strlen(buf) != 0 )
1089  {
1090  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1091  return( 1 );
1092  }
1093  }
1094  fclose(file);
1095 
1096  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1097  if( total_errors == 0 )
1098  fprintf( stdout, "PASSED" );
1099  else
1100  fprintf( stdout, "FAILED" );
1101 
1102  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1103  total_tests - total_errors, total_tests, total_skipped );
1104 
1105 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1106 #if defined(POLARSSL_MEMORY_DEBUG)
1107  memory_buffer_alloc_status();
1108 #endif
1109  memory_buffer_alloc_free();
1110 #endif
1111 
1112  return( total_errors != 0 );
1113 }
1114 
1115 
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
Memory allocation layer.
uint32_t t_uint
Definition: bignum.h:149
Info structure for the pseudo random function.
int s
Definition: bignum.h:173
int md4_self_test(int verbose)
Checkup routine.
void md4_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[16])
Output = HMAC-MD4( hmac key, input buffer )
Configuration options (set of defines)
void md2(const unsigned char *input, size_t ilen, unsigned char output[16])
Output = MD2( input buffer )
MPI structure.
Definition: bignum.h:171
void md5_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[16])
Output = HMAC-MD5( hmac key, input buffer )
void md4(const unsigned char *input, size_t ilen, unsigned char output[16])
Output = MD4( input buffer )
int main(int argc, char *argv[])
Multi-precision integer library.
int dep_check(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 md2_file(const char *path, unsigned char output[16])
Output = MD2( file contents )
int md5_self_test(int verbose)
Checkup routine.
int md5_file(const char *path, unsigned char output[16])
Output = MD5( file contents )
static int unhexify(unsigned char *obuf, const char *ibuf)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int parse_arguments(char *buf, size_t len, char *params[50])
static int test_assert(int correct, char *test)
static int test_errors
int md2_self_test(int verbose)
Checkup routine.
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)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int dispatch_test(int cnt, char *params[50])
size_t n
Definition: bignum.h:174
void md2_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[16])
Output = HMAC-MD2( hmac key, input buffer )
unsigned char * buf
#define TEST_ASSERT(TEST)
int verify_int(char *str, int *value)
MD4 message digest algorithm (hash function)
int md4_file(const char *path, unsigned char output[16])
Output = MD4( file contents )
MD5 message digest algorithm (hash function)
void md5(const unsigned char *input, size_t ilen, unsigned char output[16])
Output = MD5( input buffer )
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
#define PUT_UINT32_BE(n, b, i)
MD2 message digest algorithm (hash function)
int get_line(FILE *f, char *buf, size_t len)