PolarSSL v1.3.2
test_suite_pkcs5.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_PKCS5_C
4 
5 #include <polarssl/pkcs5.h>
6 #endif /* POLARSSL_PKCS5_C */
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 #ifdef POLARSSL_PKCS5_C
285 
286 #define TEST_SUITE_ACTIVE
287 
288 static int test_assert( int correct, char *test )
289 {
290  if( correct )
291  return( 0 );
292 
293  test_errors++;
294  if( test_errors == 1 )
295  printf( "FAILED\n" );
296  printf( " %s\n", test );
297 
298  return( 1 );
299 }
300 
301 #define TEST_ASSERT( TEST ) \
302  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
303  if( test_errors) return; \
304  } while (0)
305 
306 int verify_string( char **str )
307 {
308  if( (*str)[0] != '"' ||
309  (*str)[strlen( *str ) - 1] != '"' )
310  {
311  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
312  return( -1 );
313  }
314 
315  (*str)++;
316  (*str)[strlen( *str ) - 1] = '\0';
317 
318  return( 0 );
319 }
320 
321 int verify_int( char *str, int *value )
322 {
323  size_t i;
324  int minus = 0;
325  int digits = 1;
326  int hex = 0;
327 
328  for( i = 0; i < strlen( str ); i++ )
329  {
330  if( i == 0 && str[i] == '-' )
331  {
332  minus = 1;
333  continue;
334  }
335 
336  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
337  str[i - 1] == '0' && str[i] == 'x' )
338  {
339  hex = 1;
340  continue;
341  }
342 
343  if( str[i] < '0' || str[i] > '9' )
344  {
345  digits = 0;
346  break;
347  }
348  }
349 
350  if( digits )
351  {
352  if( hex )
353  *value = strtol( str, NULL, 16 );
354  else
355  *value = strtol( str, NULL, 10 );
356 
357  return( 0 );
358  }
359 
360  if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
361  {
362  *value = ( POLARSSL_MD_SHA1 );
363  return( 0 );
364  }
365 
366 
367  printf( "Expected integer for parameter and got: %s\n", str );
368  return( -1 );
369 }
370 
371 void test_suite_pbkdf2_hmac( int hash, char *hex_password_string,
372  char *hex_salt_string, int it_cnt, int key_len,
373  char *result_key_string )
374 {
375  unsigned char pw_str[100];
376  unsigned char salt_str[100];
377  unsigned char dst_str[100];
378 
379  md_context_t ctx;
380  const md_info_t *info;
381 
382  int pw_len, salt_len;
383  unsigned char key[100];
384 
385  memset(pw_str, 0x00, 100);
386  memset(salt_str, 0x00, 100);
387  memset(dst_str, 0x00, 100);
388 
389  pw_len = unhexify( pw_str, hex_password_string );
390  salt_len = unhexify( salt_str, hex_salt_string );
391 
392 
393  info = md_info_from_type( hash );
394  TEST_ASSERT( info != NULL );
395  if( info == NULL )
396  return;
397  TEST_ASSERT( md_init_ctx( &ctx, info ) == 0 );
398  TEST_ASSERT( pkcs5_pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len,
399  it_cnt, key_len, key ) == 0 );
400  TEST_ASSERT( md_free_ctx( &ctx ) == 0 );
401 
402  hexify( dst_str, key, key_len );
403  TEST_ASSERT( strcmp( (char *) dst_str, result_key_string ) == 0 );
404 }
405 
406 
407 #endif /* POLARSSL_PKCS5_C */
408 
409 
410 int dep_check( char *str )
411 {
412  if( str == NULL )
413  return( 1 );
414 
415  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
416  {
417 #if defined(POLARSSL_SHA1_C)
418  return( 0 );
419 #else
420  return( 1 );
421 #endif
422  }
423 
424 
425  return( 1 );
426 }
427 
428 int dispatch_test(int cnt, char *params[50])
429 {
430  int ret;
431  ((void) cnt);
432  ((void) params);
433 
434 #if defined(TEST_SUITE_ACTIVE)
435  if( strcmp( params[0], "pbkdf2_hmac" ) == 0 )
436  {
437 
438  int param1;
439  char *param2 = params[2];
440  char *param3 = params[3];
441  int param4;
442  int param5;
443  char *param6 = params[6];
444 
445  if( cnt != 7 )
446  {
447  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
448  return( 2 );
449  }
450 
451  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
452  if( verify_string( &param2 ) != 0 ) return( 2 );
453  if( verify_string( &param3 ) != 0 ) return( 2 );
454  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
455  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
456  if( verify_string( &param6 ) != 0 ) return( 2 );
457 
458  test_suite_pbkdf2_hmac( param1, param2, param3, param4, param5, param6 );
459  return ( 0 );
460 
461  return ( 3 );
462  }
463  else
464 
465  {
466  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
467  fflush( stdout );
468  return( 1 );
469  }
470 #else
471  return( 3 );
472 #endif
473  return( ret );
474 }
475 
476 int get_line( FILE *f, char *buf, size_t len )
477 {
478  char *ret;
479 
480  ret = fgets( buf, len, f );
481  if( ret == NULL )
482  return( -1 );
483 
484  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
485  buf[strlen(buf) - 1] = '\0';
486  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
487  buf[strlen(buf) - 1] = '\0';
488 
489  return( 0 );
490 }
491 
492 int parse_arguments( char *buf, size_t len, char *params[50] )
493 {
494  int cnt = 0, i;
495  char *cur = buf;
496  char *p = buf, *q;
497 
498  params[cnt++] = cur;
499 
500  while( *p != '\0' && p < buf + len )
501  {
502  if( *p == '\\' )
503  {
504  *p++;
505  *p++;
506  continue;
507  }
508  if( *p == ':' )
509  {
510  if( p + 1 < buf + len )
511  {
512  cur = p + 1;
513  params[cnt++] = cur;
514  }
515  *p = '\0';
516  }
517 
518  *p++;
519  }
520 
521  // Replace newlines, question marks and colons in strings
522  for( i = 0; i < cnt; i++ )
523  {
524  p = params[i];
525  q = params[i];
526 
527  while( *p != '\0' )
528  {
529  if( *p == '\\' && *(p + 1) == 'n' )
530  {
531  p += 2;
532  *(q++) = '\n';
533  }
534  else if( *p == '\\' && *(p + 1) == ':' )
535  {
536  p += 2;
537  *(q++) = ':';
538  }
539  else if( *p == '\\' && *(p + 1) == '?' )
540  {
541  p += 2;
542  *(q++) = '?';
543  }
544  else
545  *(q++) = *(p++);
546  }
547  *q = '\0';
548  }
549 
550  return( cnt );
551 }
552 
553 int main()
554 {
555  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
556  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_pkcs5.data";
557  FILE *file;
558  char buf[5000];
559  char *params[50];
560 
561 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
562  unsigned char alloc_buf[1000000];
563  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
564 #endif
565 
566  file = fopen( filename, "r" );
567  if( file == NULL )
568  {
569  fprintf( stderr, "Failed to open\n" );
570  return( 1 );
571  }
572 
573  while( !feof( file ) )
574  {
575  int skip = 0;
576 
577  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
578  break;
579  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
580  fprintf( stdout, " " );
581  for( i = strlen( buf ) + 1; i < 67; i++ )
582  fprintf( stdout, "." );
583  fprintf( stdout, " " );
584  fflush( stdout );
585 
586  total_tests++;
587 
588  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
589  break;
590  cnt = parse_arguments( buf, strlen(buf), params );
591 
592  if( strcmp( params[0], "depends_on" ) == 0 )
593  {
594  for( i = 1; i < cnt; i++ )
595  if( dep_check( params[i] ) != 0 )
596  skip = 1;
597 
598  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
599  break;
600  cnt = parse_arguments( buf, strlen(buf), params );
601  }
602 
603  if( skip == 0 )
604  {
605  test_errors = 0;
606  ret = dispatch_test( cnt, params );
607  }
608 
609  if( skip == 1 || ret == 3 )
610  {
611  total_skipped++;
612  fprintf( stdout, "----\n" );
613  fflush( stdout );
614  }
615  else if( ret == 0 && test_errors == 0 )
616  {
617  fprintf( stdout, "PASS\n" );
618  fflush( stdout );
619  }
620  else if( ret == 2 )
621  {
622  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
623  fclose(file);
624  exit( 2 );
625  }
626  else
627  total_errors++;
628 
629  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
630  break;
631  if( strlen(buf) != 0 )
632  {
633  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
634  return( 1 );
635  }
636  }
637  fclose(file);
638 
639  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
640  if( total_errors == 0 )
641  fprintf( stdout, "PASSED" );
642  else
643  fprintf( stdout, "FAILED" );
644 
645  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
646  total_tests - total_errors, total_tests, total_skipped );
647 
648 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
649 #if defined(POLARSSL_MEMORY_DEBUG)
650  memory_buffer_alloc_status();
651 #endif
652  memory_buffer_alloc_free();
653 #endif
654 
655  return( total_errors != 0 );
656 }
657 
658 
Memory allocation layer.
uint32_t t_uint
Definition: bignum.h:149
PKCS#5 functions.
Info structure for the pseudo random function.
int s
Definition: bignum.h:173
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.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
Configuration options (set of defines)
static int test_errors
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
MPI structure.
Definition: bignum.h:171
int pkcs5_pbkdf2_hmac(md_context_t *ctx, const unsigned char *password, size_t plen, const unsigned char *salt, size_t slen, unsigned int iteration_count, uint32_t key_length, unsigned char *output)
PKCS#5 PBKDF2 using HMAC.
static int test_assert(int correct, char *test)
static int unhexify(unsigned char *obuf, const char *ibuf)
#define PUT_UINT32_BE(n, b, i)
int main(int argc, char *argv[])
Multi-precision integer library.
int dep_check(char *str)
#define TEST_ASSERT(TEST)
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
int parse_arguments(char *buf, size_t len, char *params[50])
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
t_uint * p
Definition: bignum.h:175
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 verify_string(char **str)
int dispatch_test(int cnt, char *params[50])
size_t n
Definition: bignum.h:174
unsigned char * buf
int verify_int(char *str, int *value)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int md_free_ctx(md_context_t *ctx)
Free the message-specific context of ctx.
Message digest information.
Definition: md.h:73
int get_line(FILE *f, char *buf, size_t len)
Generic message digest context.
Definition: md.h:129