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