PolarSSL v1.3.9
test_suite_ctr_drbg.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 #ifdef POLARSSL_CTR_DRBG_C
8 
9 #include <polarssl/ctr_drbg.h>
10 
11 int test_offset_idx;
12 int entropy_func( void *data, unsigned char *buf, size_t len )
13 {
14  const unsigned char *p = (unsigned char *) data;
15  memcpy( buf, p + test_offset_idx, len );
16  test_offset_idx += len;
17  return( 0 );
18 }
19 #endif /* POLARSSL_CTR_DRBG_C */
20 
21 
22 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
23 #include "polarssl/memory.h"
24 #endif
25 
26 #if defined(POLARSSL_PLATFORM_C)
27 #include "polarssl/platform.h"
28 #else
29 #define polarssl_malloc malloc
30 #define polarssl_free free
31 #endif
32 
33 #ifdef _MSC_VER
34 #include <basetsd.h>
35 typedef UINT32 uint32_t;
36 #else
37 #include <inttypes.h>
38 #endif
39 
40 #include <assert.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 /*
45  * 32-bit integer manipulation macros (big endian)
46  */
47 #ifndef GET_UINT32_BE
48 #define GET_UINT32_BE(n,b,i) \
49 { \
50  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
51  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
52  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
53  | ( (uint32_t) (b)[(i) + 3] ); \
54 }
55 #endif
56 
57 #ifndef PUT_UINT32_BE
58 #define PUT_UINT32_BE(n,b,i) \
59 { \
60  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
61  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
62  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
63  (b)[(i) + 3] = (unsigned char) ( (n) ); \
64 }
65 #endif
66 
67 static int unhexify(unsigned char *obuf, const char *ibuf)
68 {
69  unsigned char c, c2;
70  int len = strlen(ibuf) / 2;
71  assert(!(strlen(ibuf) %1)); // must be even number of bytes
72 
73  while (*ibuf != 0)
74  {
75  c = *ibuf++;
76  if( c >= '0' && c <= '9' )
77  c -= '0';
78  else if( c >= 'a' && c <= 'f' )
79  c -= 'a' - 10;
80  else if( c >= 'A' && c <= 'F' )
81  c -= 'A' - 10;
82  else
83  assert( 0 );
84 
85  c2 = *ibuf++;
86  if( c2 >= '0' && c2 <= '9' )
87  c2 -= '0';
88  else if( c2 >= 'a' && c2 <= 'f' )
89  c2 -= 'a' - 10;
90  else if( c2 >= 'A' && c2 <= 'F' )
91  c2 -= 'A' - 10;
92  else
93  assert( 0 );
94 
95  *obuf++ = ( c << 4 ) | c2;
96  }
97 
98  return len;
99 }
100 
101 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
102 {
103  unsigned char l, h;
104 
105  while (len != 0)
106  {
107  h = (*ibuf) / 16;
108  l = (*ibuf) % 16;
109 
110  if( h < 10 )
111  *obuf++ = '0' + h;
112  else
113  *obuf++ = 'a' + h - 10;
114 
115  if( l < 10 )
116  *obuf++ = '0' + l;
117  else
118  *obuf++ = 'a' + l - 10;
119 
120  ++ibuf;
121  len--;
122  }
123 }
124 
132 static unsigned char *zero_alloc( size_t len )
133 {
134  void *p;
135  size_t actual_len = len != 0 ? len : 1;
136 
137  p = polarssl_malloc( actual_len );
138  assert( p != NULL );
139 
140  memset( p, 0x00, actual_len );
141 
142  return( p );
143 }
144 
155 static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
156 {
157  unsigned char *obuf;
158 
159  *olen = strlen(ibuf) / 2;
160 
161  if( *olen == 0 )
162  return( zero_alloc( *olen ) );
163 
164  obuf = polarssl_malloc( *olen );
165  assert( obuf != NULL );
166 
167  (void) unhexify( obuf, ibuf );
168 
169  return( obuf );
170 }
171 
181 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
182 {
183 #if !defined(__OpenBSD__)
184  size_t i;
185 
186  if( rng_state != NULL )
187  rng_state = NULL;
188 
189  for( i = 0; i < len; ++i )
190  output[i] = rand();
191 #else
192  if( rng_state != NULL )
193  rng_state = NULL;
194 
195  arc4random_buf( output, len );
196 #endif /* !OpenBSD */
197 
198  return( 0 );
199 }
200 
206 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
207 {
208  if( rng_state != NULL )
209  rng_state = NULL;
210 
211  memset( output, 0, len );
212 
213  return( 0 );
214 }
215 
216 typedef struct
217 {
218  unsigned char *buf;
219  size_t length;
220 } rnd_buf_info;
221 
233 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
234 {
235  rnd_buf_info *info = (rnd_buf_info *) rng_state;
236  size_t use_len;
237 
238  if( rng_state == NULL )
239  return( rnd_std_rand( NULL, output, len ) );
240 
241  use_len = len;
242  if( len > info->length )
243  use_len = info->length;
244 
245  if( use_len )
246  {
247  memcpy( output, info->buf, use_len );
248  info->buf += use_len;
249  info->length -= use_len;
250  }
251 
252  if( len - use_len > 0 )
253  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
254 
255  return( 0 );
256 }
257 
265 typedef struct
266 {
267  uint32_t key[16];
268  uint32_t v0, v1;
270 
279 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
280 {
281  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
282  uint32_t i, *k, sum, delta=0x9E3779B9;
283  unsigned char result[4], *out = output;
284 
285  if( rng_state == NULL )
286  return( rnd_std_rand( NULL, output, len ) );
287 
288  k = info->key;
289 
290  while( len > 0 )
291  {
292  size_t use_len = ( len > 4 ) ? 4 : len;
293  sum = 0;
294 
295  for( i = 0; i < 32; i++ )
296  {
297  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
298  sum += delta;
299  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
300  }
301 
302  PUT_UINT32_BE( info->v0, result, 0 );
303  memcpy( out, result, use_len );
304  len -= use_len;
305  out += 4;
306  }
307 
308  return( 0 );
309 }
310 
311 
312 #include <stdio.h>
313 #include <string.h>
314 
315 #if defined(POLARSSL_PLATFORM_C)
316 #include "polarssl/platform.h"
317 #else
318 #define polarssl_printf printf
319 #define polarssl_malloc malloc
320 #define polarssl_free free
321 #endif
322 
323 static int test_errors = 0;
324 
325 #ifdef POLARSSL_CTR_DRBG_C
326 
327 #define TEST_SUITE_ACTIVE
328 
329 static int test_assert( int correct, const char *test )
330 {
331  if( correct )
332  return( 0 );
333 
334  test_errors++;
335  if( test_errors == 1 )
336  printf( "FAILED\n" );
337  printf( " %s\n", test );
338 
339  return( 1 );
340 }
341 
342 #define TEST_ASSERT( TEST ) \
343  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
344  if( test_errors) goto exit; \
345  } while (0)
346 
347 int verify_string( char **str )
348 {
349  if( (*str)[0] != '"' ||
350  (*str)[strlen( *str ) - 1] != '"' )
351  {
352  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
353  return( -1 );
354  }
355 
356  (*str)++;
357  (*str)[strlen( *str ) - 1] = '\0';
358 
359  return( 0 );
360 }
361 
362 int verify_int( char *str, int *value )
363 {
364  size_t i;
365  int minus = 0;
366  int digits = 1;
367  int hex = 0;
368 
369  for( i = 0; i < strlen( str ); i++ )
370  {
371  if( i == 0 && str[i] == '-' )
372  {
373  minus = 1;
374  continue;
375  }
376 
377  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
378  str[i - 1] == '0' && str[i] == 'x' )
379  {
380  hex = 1;
381  continue;
382  }
383 
384  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
385  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
386  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
387  {
388  digits = 0;
389  break;
390  }
391  }
392 
393  if( digits )
394  {
395  if( hex )
396  *value = strtol( str, NULL, 16 );
397  else
398  *value = strtol( str, NULL, 10 );
399 
400  return( 0 );
401  }
402 
403 #ifdef POLARSSL_FS_IO
404  if( strcmp( str, "POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR" ) == 0 )
405  {
407  return( 0 );
408  }
409 #endif // POLARSSL_FS_IO
410 
411 
412  printf( "Expected integer for parameter and got: %s\n", str );
413  return( -1 );
414 }
415 
416 void test_suite_ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
417  char *add1_string, char *add2_string,
418  char *result_str )
419 {
420  unsigned char entropy[512];
421  unsigned char add_init[512];
422  unsigned char add1[512];
423  unsigned char add2[512];
424  ctr_drbg_context ctx;
425  unsigned char buf[512];
426  unsigned char output_str[512];
427  int add_init_len, add1_len, add2_len;
428 
429  memset( output_str, 0, 512 );
430 
431  unhexify( entropy, entropy_string );
432  add_init_len = unhexify( add_init, add_init_string );
433  add1_len = unhexify( add1, add1_string );
434  add2_len = unhexify( add2, add2_string );
435 
436  test_offset_idx = 0;
437  TEST_ASSERT( ctr_drbg_init_entropy_len( &ctx, entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
439 
440  TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
441  TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
442  hexify( output_str, buf, 16 );
443  TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
444 
445 exit:
446  ctr_drbg_free( &ctx );
447 }
448 
449 void test_suite_ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
450  char *add1_string, char *add_reseed_string,
451  char *add2_string, char *result_str )
452 {
453  unsigned char entropy[512];
454  unsigned char add_init[512];
455  unsigned char add1[512];
456  unsigned char add_reseed[512];
457  unsigned char add2[512];
458  ctr_drbg_context ctx;
459  unsigned char buf[512];
460  unsigned char output_str[512];
461  int add_init_len, add1_len, add_reseed_len, add2_len;
462 
463  memset( output_str, 0, 512 );
464 
465  unhexify( entropy, entropy_string );
466  add_init_len = unhexify( add_init, add_init_string );
467  add1_len = unhexify( add1, add1_string );
468  add_reseed_len = unhexify( add_reseed, add_reseed_string );
469  add2_len = unhexify( add2, add2_string );
470 
471  test_offset_idx = 0;
472  TEST_ASSERT( ctr_drbg_init_entropy_len( &ctx, entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
473 
474  TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
475  TEST_ASSERT( ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
476  TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
477  hexify( output_str, buf, 16 );
478  TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
479 
480 exit:
481  ctr_drbg_free( &ctx );
482 }
483 
484 void test_suite_ctr_drbg_entropy_usage( )
485 {
486  unsigned char out[16];
487  unsigned char add[16];
488  unsigned char entropy[1024];
489  ctr_drbg_context ctx;
490  size_t i, reps = 10;
491  int last_idx;
492 
493  test_offset_idx = 0;
494  memset( entropy, 0, sizeof( entropy ) );
495  memset( out, 0, sizeof( out ) );
496  memset( add, 0, sizeof( add ) );
497 
498  /* Init must use entropy */
499  last_idx = test_offset_idx;
500  TEST_ASSERT( ctr_drbg_init( &ctx, entropy_func, entropy, NULL, 0 ) == 0 );
501  TEST_ASSERT( last_idx < test_offset_idx );
502 
503  /* By default, PR is off and reseed_interval is large,
504  * so the next few calls should not use entropy */
505  last_idx = test_offset_idx;
506  for( i = 0; i < reps; i++ )
507  {
508  TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
509  TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
510  add, sizeof( add ) ) == 0 );
511  }
512  TEST_ASSERT( last_idx == test_offset_idx );
513 
514  /* While at it, make sure we didn't write past the requested length */
515  TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
516  TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
517  TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
518  TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
519 
520  /* Set reseed_interval to the number of calls done,
521  * so the next call should reseed */
522  ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
523  TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
524  TEST_ASSERT( last_idx < test_offset_idx );
525 
526  /* The new few calls should not reseed */
527  last_idx = test_offset_idx;
528  for( i = 0; i < reps / 2; i++ )
529  {
530  TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
531  TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
532  add, sizeof( add ) ) == 0 );
533  }
534  TEST_ASSERT( last_idx == test_offset_idx );
535 
536  /* Now enable PR, so the next few calls should all reseed */
538  TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
539  TEST_ASSERT( last_idx < test_offset_idx );
540 
541  /* Finally, check setting entropy_len */
542  ctr_drbg_set_entropy_len( &ctx, 42 );
543  last_idx = test_offset_idx;
544  TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
545  TEST_ASSERT( test_offset_idx - last_idx == 42 );
546 
547  ctr_drbg_set_entropy_len( &ctx, 13 );
548  last_idx = test_offset_idx;
549  TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
550  TEST_ASSERT( test_offset_idx - last_idx == 13 );
551 
552 exit:
553  ctr_drbg_free( &ctx );
554 }
555 
556 #ifdef POLARSSL_FS_IO
557 void test_suite_ctr_drbg_seed_file( char *path, int ret )
558 {
559  ctr_drbg_context ctx;
560 
561  TEST_ASSERT( ctr_drbg_init( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
562  TEST_ASSERT( ctr_drbg_write_seed_file( &ctx, path ) == ret );
563  TEST_ASSERT( ctr_drbg_update_seed_file( &ctx, path ) == ret );
564 
565 exit:
566  ctr_drbg_free( &ctx );
567 }
568 #endif /* POLARSSL_FS_IO */
569 
570 #ifdef POLARSSL_SELF_TEST
571 void test_suite_ctr_drbg_selftest( )
572 {
573  TEST_ASSERT( ctr_drbg_self_test( 0 ) == 0 );
574 
575 exit:
576  return;
577 }
578 #endif /* POLARSSL_SELF_TEST */
579 
580 
581 #endif /* POLARSSL_CTR_DRBG_C */
582 
583 
584 int dep_check( char *str )
585 {
586  if( str == NULL )
587  return( 1 );
588 
589 
590 
591  return( 1 );
592 }
593 
594 int dispatch_test(int cnt, char *params[50])
595 {
596  int ret;
597  ((void) cnt);
598  ((void) params);
599 
600 #if defined(TEST_SUITE_ACTIVE)
601  if( strcmp( params[0], "ctr_drbg_validate_pr" ) == 0 )
602  {
603 
604  char *param1 = params[1];
605  char *param2 = params[2];
606  char *param3 = params[3];
607  char *param4 = params[4];
608  char *param5 = params[5];
609 
610  if( cnt != 6 )
611  {
612  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
613  return( 2 );
614  }
615 
616  if( verify_string( &param1 ) != 0 ) return( 2 );
617  if( verify_string( &param2 ) != 0 ) return( 2 );
618  if( verify_string( &param3 ) != 0 ) return( 2 );
619  if( verify_string( &param4 ) != 0 ) return( 2 );
620  if( verify_string( &param5 ) != 0 ) return( 2 );
621 
622  test_suite_ctr_drbg_validate_pr( param1, param2, param3, param4, param5 );
623  return ( 0 );
624 
625  return ( 3 );
626  }
627  else
628  if( strcmp( params[0], "ctr_drbg_validate_nopr" ) == 0 )
629  {
630 
631  char *param1 = params[1];
632  char *param2 = params[2];
633  char *param3 = params[3];
634  char *param4 = params[4];
635  char *param5 = params[5];
636  char *param6 = params[6];
637 
638  if( cnt != 7 )
639  {
640  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
641  return( 2 );
642  }
643 
644  if( verify_string( &param1 ) != 0 ) return( 2 );
645  if( verify_string( &param2 ) != 0 ) return( 2 );
646  if( verify_string( &param3 ) != 0 ) return( 2 );
647  if( verify_string( &param4 ) != 0 ) return( 2 );
648  if( verify_string( &param5 ) != 0 ) return( 2 );
649  if( verify_string( &param6 ) != 0 ) return( 2 );
650 
651  test_suite_ctr_drbg_validate_nopr( param1, param2, param3, param4, param5, param6 );
652  return ( 0 );
653 
654  return ( 3 );
655  }
656  else
657  if( strcmp( params[0], "ctr_drbg_entropy_usage" ) == 0 )
658  {
659 
660 
661  if( cnt != 1 )
662  {
663  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
664  return( 2 );
665  }
666 
667 
668  test_suite_ctr_drbg_entropy_usage( );
669  return ( 0 );
670 
671  return ( 3 );
672  }
673  else
674  if( strcmp( params[0], "ctr_drbg_seed_file" ) == 0 )
675  {
676  #ifdef POLARSSL_FS_IO
677 
678  char *param1 = params[1];
679  int param2;
680 
681  if( cnt != 3 )
682  {
683  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
684  return( 2 );
685  }
686 
687  if( verify_string( &param1 ) != 0 ) return( 2 );
688  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
689 
690  test_suite_ctr_drbg_seed_file( param1, param2 );
691  return ( 0 );
692  #endif /* POLARSSL_FS_IO */
693 
694  return ( 3 );
695  }
696  else
697  if( strcmp( params[0], "ctr_drbg_selftest" ) == 0 )
698  {
699  #ifdef POLARSSL_SELF_TEST
700 
701 
702  if( cnt != 1 )
703  {
704  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
705  return( 2 );
706  }
707 
708 
709  test_suite_ctr_drbg_selftest( );
710  return ( 0 );
711  #endif /* POLARSSL_SELF_TEST */
712 
713  return ( 3 );
714  }
715  else
716 
717  {
718  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
719  fflush( stdout );
720  return( 1 );
721  }
722 #else
723  return( 3 );
724 #endif
725  return( ret );
726 }
727 
728 int get_line( FILE *f, char *buf, size_t len )
729 {
730  char *ret;
731 
732  ret = fgets( buf, len, f );
733  if( ret == NULL )
734  return( -1 );
735 
736  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
737  buf[strlen(buf) - 1] = '\0';
738  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
739  buf[strlen(buf) - 1] = '\0';
740 
741  return( 0 );
742 }
743 
744 int parse_arguments( char *buf, size_t len, char *params[50] )
745 {
746  int cnt = 0, i;
747  char *cur = buf;
748  char *p = buf, *q;
749 
750  params[cnt++] = cur;
751 
752  while( *p != '\0' && p < buf + len )
753  {
754  if( *p == '\\' )
755  {
756  p++;
757  p++;
758  continue;
759  }
760  if( *p == ':' )
761  {
762  if( p + 1 < buf + len )
763  {
764  cur = p + 1;
765  params[cnt++] = cur;
766  }
767  *p = '\0';
768  }
769 
770  p++;
771  }
772 
773  // Replace newlines, question marks and colons in strings
774  for( i = 0; i < cnt; i++ )
775  {
776  p = params[i];
777  q = params[i];
778 
779  while( *p != '\0' )
780  {
781  if( *p == '\\' && *(p + 1) == 'n' )
782  {
783  p += 2;
784  *(q++) = '\n';
785  }
786  else if( *p == '\\' && *(p + 1) == ':' )
787  {
788  p += 2;
789  *(q++) = ':';
790  }
791  else if( *p == '\\' && *(p + 1) == '?' )
792  {
793  p += 2;
794  *(q++) = '?';
795  }
796  else
797  *(q++) = *(p++);
798  }
799  *q = '\0';
800  }
801 
802  return( cnt );
803 }
804 
805 int main()
806 {
807  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
808  const char *filename = "/tmp/B.1c06a028-9709-4951-a65b-b24142b09746/BUILD/polarssl-1.3.9/tests/suites/test_suite_ctr_drbg.data";
809  FILE *file;
810  char buf[5000];
811  char *params[50];
812 
813 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
814  unsigned char alloc_buf[1000000];
815  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
816 #endif
817 
818  file = fopen( filename, "r" );
819  if( file == NULL )
820  {
821  fprintf( stderr, "Failed to open\n" );
822  return( 1 );
823  }
824 
825  while( !feof( file ) )
826  {
827  int skip = 0;
828 
829  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
830  break;
831  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
832  fprintf( stdout, " " );
833  for( i = strlen( buf ) + 1; i < 67; i++ )
834  fprintf( stdout, "." );
835  fprintf( stdout, " " );
836  fflush( stdout );
837 
838  total_tests++;
839 
840  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
841  break;
842  cnt = parse_arguments( buf, strlen(buf), params );
843 
844  if( strcmp( params[0], "depends_on" ) == 0 )
845  {
846  for( i = 1; i < cnt; i++ )
847  if( dep_check( params[i] ) != 0 )
848  skip = 1;
849 
850  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
851  break;
852  cnt = parse_arguments( buf, strlen(buf), params );
853  }
854 
855  if( skip == 0 )
856  {
857  test_errors = 0;
858  ret = dispatch_test( cnt, params );
859  }
860 
861  if( skip == 1 || ret == 3 )
862  {
863  total_skipped++;
864  fprintf( stdout, "----\n" );
865  fflush( stdout );
866  }
867  else if( ret == 0 && test_errors == 0 )
868  {
869  fprintf( stdout, "PASS\n" );
870  fflush( stdout );
871  }
872  else if( ret == 2 )
873  {
874  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
875  fclose(file);
876  exit( 2 );
877  }
878  else
879  total_errors++;
880 
881  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
882  break;
883  if( strlen(buf) != 0 )
884  {
885  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
886  return( 1 );
887  }
888  }
889  fclose(file);
890 
891  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
892  if( total_errors == 0 )
893  fprintf( stdout, "PASSED" );
894  else
895  fprintf( stdout, "FAILED" );
896 
897  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
898  total_tests - total_errors, total_tests, total_skipped );
899 
900 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
901 #if defined(POLARSSL_MEMORY_DEBUG)
902  memory_buffer_alloc_status();
903 #endif
905 #endif
906 
907  return( total_errors != 0 );
908 }
909 
910 
int ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_len)
CTR_DRBG generate random.
void ctr_drbg_set_prediction_resistance(ctr_drbg_context *ctx, int resistance)
Enable / disable prediction resistance (Default: Off)
#define CTR_DRBG_PR_ON
Prediction resistance enabled.
Definition: ctr_drbg.h:80
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
Memory allocation layer (Deprecated to platform layer)
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int get_line(FILE *f, char *buf, size_t len)
Configuration options (set of defines)
void ctr_drbg_free(ctr_drbg_context *ctx)
Clear CTR_CRBG context data.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
PolarSSL Platform abstraction layer.
int ctr_drbg_init_entropy_len(ctr_drbg_context *, int(*)(void *, unsigned char *, size_t), void *, const unsigned char *, size_t, size_t)
static int test_assert(int correct, const char *test)
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
int ctr_drbg_random_with_add(void *p_rng, unsigned char *output, size_t output_len, const unsigned char *additional, size_t add_len)
CTR_DRBG generate random with additional update input.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int dispatch_test(int cnt, char *params[50])
#define POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR
Read/write error in file.
Definition: ctr_drbg.h:37
#define polarssl_malloc
static int test_errors
int ctr_drbg_reseed(ctr_drbg_context *ctx, const unsigned char *additional, size_t len)
CTR_DRBG reseeding (extracts data from entropy source)
CTR_DRBG context structure.
Definition: ctr_drbg.h:89
int verify_string(char **str)
int dep_check(char *str)
void ctr_drbg_set_reseed_interval(ctr_drbg_context *ctx, int interval)
Set the reseed interval (Default: CTR_DRBG_RESEED_INTERVAL)
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 ctr_drbg_update_seed_file(ctr_drbg_context *ctx, const char *path)
Read and update a seed file.
unsigned char * buf
static int unhexify(unsigned char *obuf, const char *ibuf)
void ctr_drbg_set_entropy_len(ctr_drbg_context *ctx, size_t len)
Set the amount of entropy grabbed on each (re)seed (Default: CTR_DRBG_ENTROPY_LEN) ...
int ctr_drbg_self_test(int verbose)
Checkup routine.
int parse_arguments(char *buf, size_t len, char *params[50])
int ctr_drbg_init(ctr_drbg_context *ctx, int(*f_entropy)(void *, unsigned char *, size_t), void *p_entropy, const unsigned char *custom, size_t len)
CTR_DRBG initialization.
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 ctr_drbg_write_seed_file(ctr_drbg_context *ctx, const char *path)
Write a seed file.
int verify_int(char *str, int *value)
int main()
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int entropy_func(void *data, unsigned char *output, size_t len)
Retrieve entropy from the accumulator (Maximum length: ENTROPY_BLOCK_SIZE) (Thread-safe if POLARSSL_T...
#define PUT_UINT32_BE(n, b, i)
CTR_DRBG based on AES-256 (NIST SP 800-90)