PolarSSL v1.3.1
test_suite_ctr_drbg.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_CTR_DRBG_C
4 
5 #include <polarssl/ctr_drbg.h>
6 
7 int test_offset;
8 int entropy_func( void *data, unsigned char *buf, size_t len )
9 {
10  unsigned char *p = (unsigned char *) data;
11  memcpy( buf, p + test_offset, len );
12  test_offset += 32;
13  return( 0 );
14 }
15 #endif /* POLARSSL_CTR_DRBG_C */
16 
17 
18 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
19 #include "polarssl/memory.h"
20 #endif
21 
22 #ifdef _MSC_VER
23 #include <basetsd.h>
24 typedef UINT32 uint32_t;
25 #else
26 #include <inttypes.h>
27 #endif
28 
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 /*
34  * 32-bit integer manipulation macros (big endian)
35  */
36 #ifndef GET_UINT32_BE
37 #define GET_UINT32_BE(n,b,i) \
38 { \
39  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
40  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
41  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
42  | ( (uint32_t) (b)[(i) + 3] ); \
43 }
44 #endif
45 
46 #ifndef PUT_UINT32_BE
47 #define PUT_UINT32_BE(n,b,i) \
48 { \
49  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
50  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
51  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
52  (b)[(i) + 3] = (unsigned char) ( (n) ); \
53 }
54 #endif
55 
56 static int unhexify(unsigned char *obuf, const char *ibuf)
57 {
58  unsigned char c, c2;
59  int len = strlen(ibuf) / 2;
60  assert(!(strlen(ibuf) %1)); // must be even number of bytes
61 
62  while (*ibuf != 0)
63  {
64  c = *ibuf++;
65  if( c >= '0' && c <= '9' )
66  c -= '0';
67  else if( c >= 'a' && c <= 'f' )
68  c -= 'a' - 10;
69  else if( c >= 'A' && c <= 'F' )
70  c -= 'A' - 10;
71  else
72  assert( 0 );
73 
74  c2 = *ibuf++;
75  if( c2 >= '0' && c2 <= '9' )
76  c2 -= '0';
77  else if( c2 >= 'a' && c2 <= 'f' )
78  c2 -= 'a' - 10;
79  else if( c2 >= 'A' && c2 <= 'F' )
80  c2 -= 'A' - 10;
81  else
82  assert( 0 );
83 
84  *obuf++ = ( c << 4 ) | c2;
85  }
86 
87  return len;
88 }
89 
90 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
91 {
92  unsigned char l, h;
93 
94  while (len != 0)
95  {
96  h = (*ibuf) / 16;
97  l = (*ibuf) % 16;
98 
99  if( h < 10 )
100  *obuf++ = '0' + h;
101  else
102  *obuf++ = 'a' + h - 10;
103 
104  if( l < 10 )
105  *obuf++ = '0' + l;
106  else
107  *obuf++ = 'a' + l - 10;
108 
109  ++ibuf;
110  len--;
111  }
112 }
113 
123 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
124 {
125  size_t i;
126 
127  if( rng_state != NULL )
128  rng_state = NULL;
129 
130  for( i = 0; i < len; ++i )
131  output[i] = rand();
132 
133  return( 0 );
134 }
135 
141 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
142 {
143  if( rng_state != NULL )
144  rng_state = NULL;
145 
146  memset( output, 0, len );
147 
148  return( 0 );
149 }
150 
151 typedef struct
152 {
153  unsigned char *buf;
154  size_t length;
155 } rnd_buf_info;
156 
168 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
169 {
170  rnd_buf_info *info = (rnd_buf_info *) rng_state;
171  size_t use_len;
172 
173  if( rng_state == NULL )
174  return( rnd_std_rand( NULL, output, len ) );
175 
176  use_len = len;
177  if( len > info->length )
178  use_len = info->length;
179 
180  if( use_len )
181  {
182  memcpy( output, info->buf, use_len );
183  info->buf += use_len;
184  info->length -= use_len;
185  }
186 
187  if( len - use_len > 0 )
188  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
189 
190  return( 0 );
191 }
192 
200 typedef struct
201 {
202  uint32_t key[16];
203  uint32_t v0, v1;
205 
214 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
215 {
216  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
217  uint32_t i, *k, sum, delta=0x9E3779B9;
218  unsigned char result[4];
219 
220  if( rng_state == NULL )
221  return( rnd_std_rand( NULL, output, len ) );
222 
223  k = info->key;
224 
225  while( len > 0 )
226  {
227  size_t use_len = ( len > 4 ) ? 4 : len;
228  sum = 0;
229 
230  for( i = 0; i < 32; i++ )
231  {
232  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
233  sum += delta;
234  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
235  }
236 
237  PUT_UINT32_BE( info->v0, result, 0 );
238  memcpy( output, result, use_len );
239  len -= use_len;
240  }
241 
242  return( 0 );
243 }
244 
254 static int not_rnd( void *in, unsigned char *out, size_t len )
255 {
256  unsigned char *obuf;
257  const char *ibuf = in;
258  unsigned char c, c2;
259  assert( len == strlen(ibuf) / 2 );
260  assert(!(strlen(ibuf) %1)); // must be even number of bytes
261 
262  obuf = out + (len - 1); // sic
263  while (*ibuf != 0)
264  {
265  c = *ibuf++;
266  if( c >= '0' && c <= '9' )
267  c -= '0';
268  else if( c >= 'a' && c <= 'f' )
269  c -= 'a' - 10;
270  else if( c >= 'A' && c <= 'F' )
271  c -= 'A' - 10;
272  else
273  assert( 0 );
274 
275  c2 = *ibuf++;
276  if( c2 >= '0' && c2 <= '9' )
277  c2 -= '0';
278  else if( c2 >= 'a' && c2 <= 'f' )
279  c2 -= 'a' - 10;
280  else if( c2 >= 'A' && c2 <= 'F' )
281  c2 -= 'A' - 10;
282  else
283  assert( 0 );
284 
285  *obuf-- = ( c << 4 ) | c2; // sic
286  }
287 
288  return( 0 );
289 }
290 
291 
292 #include <stdio.h>
293 #include <string.h>
294 
295 static int test_errors = 0;
296 
297 #ifdef POLARSSL_CTR_DRBG_C
298 
299 #define TEST_SUITE_ACTIVE
300 
301 static int test_assert( int correct, char *test )
302 {
303  if( correct )
304  return( 0 );
305 
306  test_errors++;
307  if( test_errors == 1 )
308  printf( "FAILED\n" );
309  printf( " %s\n", test );
310 
311  return( 1 );
312 }
313 
314 #define TEST_ASSERT( TEST ) \
315  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
316  if( test_errors) return; \
317  } while (0)
318 
319 int verify_string( char **str )
320 {
321  if( (*str)[0] != '"' ||
322  (*str)[strlen( *str ) - 1] != '"' )
323  {
324  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
325  return( -1 );
326  }
327 
328  (*str)++;
329  (*str)[strlen( *str ) - 1] = '\0';
330 
331  return( 0 );
332 }
333 
334 int verify_int( char *str, int *value )
335 {
336  size_t i;
337  int minus = 0;
338  int digits = 1;
339  int hex = 0;
340 
341  for( i = 0; i < strlen( str ); i++ )
342  {
343  if( i == 0 && str[i] == '-' )
344  {
345  minus = 1;
346  continue;
347  }
348 
349  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
350  str[i - 1] == '0' && str[i] == 'x' )
351  {
352  hex = 1;
353  continue;
354  }
355 
356  if( str[i] < '0' || str[i] > '9' )
357  {
358  digits = 0;
359  break;
360  }
361  }
362 
363  if( digits )
364  {
365  if( hex )
366  *value = strtol( str, NULL, 16 );
367  else
368  *value = strtol( str, NULL, 10 );
369 
370  return( 0 );
371  }
372 
373 
374 
375  printf( "Expected integer for parameter and got: %s\n", str );
376  return( -1 );
377 }
378 
379 void test_suite_ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
380  char *add1_string, char *add2_string,
381  char *result_str )
382 {
383  unsigned char entropy[512];
384  unsigned char add_init[512];
385  unsigned char add1[512];
386  unsigned char add2[512];
387  ctr_drbg_context ctx;
388  unsigned char buf[512];
389  unsigned char output_str[512];
390  int add_init_len, add1_len, add2_len;
391 
392  memset( output_str, 0, 512 );
393 
394  unhexify( entropy, entropy_string );
395  add_init_len = unhexify( add_init, add_init_string );
396  add1_len = unhexify( add1, add1_string );
397  add2_len = unhexify( add2, add2_string );
398 
399  test_offset = 0;
400  TEST_ASSERT( ctr_drbg_init_entropy_len( &ctx, entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
402 
403  TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
404  TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
405  hexify( output_str, buf, 16 );
406  TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
407 }
408 
409 void test_suite_ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
410  char *add1_string, char *add_reseed_string,
411  char *add2_string, char *result_str )
412 {
413  unsigned char entropy[512];
414  unsigned char add_init[512];
415  unsigned char add1[512];
416  unsigned char add_reseed[512];
417  unsigned char add2[512];
418  ctr_drbg_context ctx;
419  unsigned char buf[512];
420  unsigned char output_str[512];
421  int add_init_len, add1_len, add_reseed_len, add2_len;
422 
423  memset( output_str, 0, 512 );
424 
425  unhexify( entropy, entropy_string );
426  add_init_len = unhexify( add_init, add_init_string );
427  add1_len = unhexify( add1, add1_string );
428  add_reseed_len = unhexify( add_reseed, add_reseed_string );
429  add2_len = unhexify( add2, add2_string );
430 
431  test_offset = 0;
432  TEST_ASSERT( ctr_drbg_init_entropy_len( &ctx, entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
433 
434  TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
435  TEST_ASSERT( ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
436  TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
437  hexify( output_str, buf, 16 );
438  TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
439 }
440 
441 
442 #endif /* POLARSSL_CTR_DRBG_C */
443 
444 
445 int dep_check( char *str )
446 {
447  if( str == NULL )
448  return( 1 );
449 
450 
451 
452  return( 1 );
453 }
454 
455 int dispatch_test(int cnt, char *params[50])
456 {
457  int ret;
458  ((void) cnt);
459  ((void) params);
460 
461 #if defined(TEST_SUITE_ACTIVE)
462  if( strcmp( params[0], "ctr_drbg_validate_pr" ) == 0 )
463  {
464 
465  char *param1 = params[1];
466  char *param2 = params[2];
467  char *param3 = params[3];
468  char *param4 = params[4];
469  char *param5 = params[5];
470 
471  if( cnt != 6 )
472  {
473  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
474  return( 2 );
475  }
476 
477  if( verify_string( &param1 ) != 0 ) return( 2 );
478  if( verify_string( &param2 ) != 0 ) return( 2 );
479  if( verify_string( &param3 ) != 0 ) return( 2 );
480  if( verify_string( &param4 ) != 0 ) return( 2 );
481  if( verify_string( &param5 ) != 0 ) return( 2 );
482 
483  test_suite_ctr_drbg_validate_pr( param1, param2, param3, param4, param5 );
484  return ( 0 );
485 
486  return ( 3 );
487  }
488  else
489  if( strcmp( params[0], "ctr_drbg_validate_nopr" ) == 0 )
490  {
491 
492  char *param1 = params[1];
493  char *param2 = params[2];
494  char *param3 = params[3];
495  char *param4 = params[4];
496  char *param5 = params[5];
497  char *param6 = params[6];
498 
499  if( cnt != 7 )
500  {
501  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
502  return( 2 );
503  }
504 
505  if( verify_string( &param1 ) != 0 ) return( 2 );
506  if( verify_string( &param2 ) != 0 ) return( 2 );
507  if( verify_string( &param3 ) != 0 ) return( 2 );
508  if( verify_string( &param4 ) != 0 ) return( 2 );
509  if( verify_string( &param5 ) != 0 ) return( 2 );
510  if( verify_string( &param6 ) != 0 ) return( 2 );
511 
512  test_suite_ctr_drbg_validate_nopr( param1, param2, param3, param4, param5, param6 );
513  return ( 0 );
514 
515  return ( 3 );
516  }
517  else
518 
519  {
520  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
521  fflush( stdout );
522  return( 1 );
523  }
524 #else
525  return( 3 );
526 #endif
527  return( ret );
528 }
529 
530 int get_line( FILE *f, char *buf, size_t len )
531 {
532  char *ret;
533 
534  ret = fgets( buf, len, f );
535  if( ret == NULL )
536  return( -1 );
537 
538  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
539  buf[strlen(buf) - 1] = '\0';
540  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
541  buf[strlen(buf) - 1] = '\0';
542 
543  return( 0 );
544 }
545 
546 int parse_arguments( char *buf, size_t len, char *params[50] )
547 {
548  int cnt = 0, i;
549  char *cur = buf;
550  char *p = buf, *q;
551 
552  params[cnt++] = cur;
553 
554  while( *p != '\0' && p < buf + len )
555  {
556  if( *p == '\\' )
557  {
558  *p++;
559  *p++;
560  continue;
561  }
562  if( *p == ':' )
563  {
564  if( p + 1 < buf + len )
565  {
566  cur = p + 1;
567  params[cnt++] = cur;
568  }
569  *p = '\0';
570  }
571 
572  *p++;
573  }
574 
575  // Replace newlines, question marks and colons in strings
576  for( i = 0; i < cnt; i++ )
577  {
578  p = params[i];
579  q = params[i];
580 
581  while( *p != '\0' )
582  {
583  if( *p == '\\' && *(p + 1) == 'n' )
584  {
585  p += 2;
586  *(q++) = '\n';
587  }
588  else if( *p == '\\' && *(p + 1) == ':' )
589  {
590  p += 2;
591  *(q++) = ':';
592  }
593  else if( *p == '\\' && *(p + 1) == '?' )
594  {
595  p += 2;
596  *(q++) = '?';
597  }
598  else
599  *(q++) = *(p++);
600  }
601  *q = '\0';
602  }
603 
604  return( cnt );
605 }
606 
607 int main()
608 {
609  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
610  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_ctr_drbg.data";
611  FILE *file;
612  char buf[5000];
613  char *params[50];
614 
615 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
616  unsigned char alloc_buf[1000000];
617  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
618 #endif
619 
620  file = fopen( filename, "r" );
621  if( file == NULL )
622  {
623  fprintf( stderr, "Failed to open\n" );
624  return( 1 );
625  }
626 
627  while( !feof( file ) )
628  {
629  int skip = 0;
630 
631  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
632  break;
633  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
634  fprintf( stdout, " " );
635  for( i = strlen( buf ) + 1; i < 67; i++ )
636  fprintf( stdout, "." );
637  fprintf( stdout, " " );
638  fflush( stdout );
639 
640  total_tests++;
641 
642  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
643  break;
644  cnt = parse_arguments( buf, strlen(buf), params );
645 
646  if( strcmp( params[0], "depends_on" ) == 0 )
647  {
648  for( i = 1; i < cnt; i++ )
649  if( dep_check( params[i] ) != 0 )
650  skip = 1;
651 
652  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
653  break;
654  cnt = parse_arguments( buf, strlen(buf), params );
655  }
656 
657  if( skip == 0 )
658  {
659  test_errors = 0;
660  ret = dispatch_test( cnt, params );
661  }
662 
663  if( skip == 1 || ret == 3 )
664  {
665  total_skipped++;
666  fprintf( stdout, "----\n" );
667  fflush( stdout );
668  }
669  else if( ret == 0 && test_errors == 0 )
670  {
671  fprintf( stdout, "PASS\n" );
672  fflush( stdout );
673  }
674  else if( ret == 2 )
675  {
676  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
677  fclose(file);
678  exit( 2 );
679  }
680  else
681  total_errors++;
682 
683  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
684  break;
685  if( strlen(buf) != 0 )
686  {
687  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
688  return( 1 );
689  }
690  }
691  fclose(file);
692 
693  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
694  if( total_errors == 0 )
695  fprintf( stdout, "PASSED" );
696  else
697  fprintf( stdout, "FAILED" );
698 
699  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
700  total_tests - total_errors, total_tests, total_skipped );
701 
702 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
703 #if defined(POLARSSL_MEMORY_DEBUG)
704  memory_buffer_alloc_status();
705 #endif
706  memory_buffer_alloc_free();
707 #endif
708 
709  return( total_errors != 0 );
710 }
711 
712