PolarSSL v1.3.2
test_suite_pbkdf2.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_PBKDF2_C
4 
5 #include <polarssl/pbkdf2.h>
6 #endif /* POLARSSL_PBKDF2_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_PBKDF2_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, char *hex_salt_string,
372  int it_cnt, int key_len, char *result_key_string )
373 {
374  unsigned char pw_str[100];
375  unsigned char salt_str[100];
376  unsigned char dst_str[100];
377 
378  md_context_t ctx;
379  const md_info_t *info;
380 
381  int pw_len, salt_len;
382  unsigned char key[100];
383 
384  memset(pw_str, 0x00, 100);
385  memset(salt_str, 0x00, 100);
386  memset(dst_str, 0x00, 100);
387 
388  pw_len = unhexify( pw_str, hex_password_string );
389  salt_len = unhexify( salt_str, hex_salt_string );
390 
391 
392  info = md_info_from_type( hash );
393  TEST_ASSERT( info != NULL );
394  if( info == NULL )
395  return;
396  TEST_ASSERT( md_init_ctx( &ctx, info ) == 0 );
397  TEST_ASSERT( pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len,
398  it_cnt, key_len, key ) == 0 );
399  TEST_ASSERT( md_free_ctx( &ctx ) == 0 );
400 
401  hexify( dst_str, key, key_len );
402  TEST_ASSERT( strcmp( (char *) dst_str, result_key_string ) == 0 );
403 }
404 
405 
406 #endif /* POLARSSL_PBKDF2_C */
407 
408 
409 int dep_check( char *str )
410 {
411  if( str == NULL )
412  return( 1 );
413 
414  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
415  {
416 #if defined(POLARSSL_SHA1_C)
417  return( 0 );
418 #else
419  return( 1 );
420 #endif
421  }
422 
423 
424  return( 1 );
425 }
426 
427 int dispatch_test(int cnt, char *params[50])
428 {
429  int ret;
430  ((void) cnt);
431  ((void) params);
432 
433 #if defined(TEST_SUITE_ACTIVE)
434  if( strcmp( params[0], "pbkdf2_hmac" ) == 0 )
435  {
436 
437  int param1;
438  char *param2 = params[2];
439  char *param3 = params[3];
440  int param4;
441  int param5;
442  char *param6 = params[6];
443 
444  if( cnt != 7 )
445  {
446  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
447  return( 2 );
448  }
449 
450  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
451  if( verify_string( &param2 ) != 0 ) return( 2 );
452  if( verify_string( &param3 ) != 0 ) return( 2 );
453  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
454  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
455  if( verify_string( &param6 ) != 0 ) return( 2 );
456 
457  test_suite_pbkdf2_hmac( param1, param2, param3, param4, param5, param6 );
458  return ( 0 );
459 
460  return ( 3 );
461  }
462  else
463 
464  {
465  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
466  fflush( stdout );
467  return( 1 );
468  }
469 #else
470  return( 3 );
471 #endif
472  return( ret );
473 }
474 
475 int get_line( FILE *f, char *buf, size_t len )
476 {
477  char *ret;
478 
479  ret = fgets( buf, len, f );
480  if( ret == NULL )
481  return( -1 );
482 
483  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
484  buf[strlen(buf) - 1] = '\0';
485  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
486  buf[strlen(buf) - 1] = '\0';
487 
488  return( 0 );
489 }
490 
491 int parse_arguments( char *buf, size_t len, char *params[50] )
492 {
493  int cnt = 0, i;
494  char *cur = buf;
495  char *p = buf, *q;
496 
497  params[cnt++] = cur;
498 
499  while( *p != '\0' && p < buf + len )
500  {
501  if( *p == '\\' )
502  {
503  *p++;
504  *p++;
505  continue;
506  }
507  if( *p == ':' )
508  {
509  if( p + 1 < buf + len )
510  {
511  cur = p + 1;
512  params[cnt++] = cur;
513  }
514  *p = '\0';
515  }
516 
517  *p++;
518  }
519 
520  // Replace newlines, question marks and colons in strings
521  for( i = 0; i < cnt; i++ )
522  {
523  p = params[i];
524  q = params[i];
525 
526  while( *p != '\0' )
527  {
528  if( *p == '\\' && *(p + 1) == 'n' )
529  {
530  p += 2;
531  *(q++) = '\n';
532  }
533  else if( *p == '\\' && *(p + 1) == ':' )
534  {
535  p += 2;
536  *(q++) = ':';
537  }
538  else if( *p == '\\' && *(p + 1) == '?' )
539  {
540  p += 2;
541  *(q++) = '?';
542  }
543  else
544  *(q++) = *(p++);
545  }
546  *q = '\0';
547  }
548 
549  return( cnt );
550 }
551 
552 int main()
553 {
554  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
555  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_pbkdf2.data";
556  FILE *file;
557  char buf[5000];
558  char *params[50];
559 
560 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
561  unsigned char alloc_buf[1000000];
562  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
563 #endif
564 
565  file = fopen( filename, "r" );
566  if( file == NULL )
567  {
568  fprintf( stderr, "Failed to open\n" );
569  return( 1 );
570  }
571 
572  while( !feof( file ) )
573  {
574  int skip = 0;
575 
576  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
577  break;
578  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
579  fprintf( stdout, " " );
580  for( i = strlen( buf ) + 1; i < 67; i++ )
581  fprintf( stdout, "." );
582  fprintf( stdout, " " );
583  fflush( stdout );
584 
585  total_tests++;
586 
587  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
588  break;
589  cnt = parse_arguments( buf, strlen(buf), params );
590 
591  if( strcmp( params[0], "depends_on" ) == 0 )
592  {
593  for( i = 1; i < cnt; i++ )
594  if( dep_check( params[i] ) != 0 )
595  skip = 1;
596 
597  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
598  break;
599  cnt = parse_arguments( buf, strlen(buf), params );
600  }
601 
602  if( skip == 0 )
603  {
604  test_errors = 0;
605  ret = dispatch_test( cnt, params );
606  }
607 
608  if( skip == 1 || ret == 3 )
609  {
610  total_skipped++;
611  fprintf( stdout, "----\n" );
612  fflush( stdout );
613  }
614  else if( ret == 0 && test_errors == 0 )
615  {
616  fprintf( stdout, "PASS\n" );
617  fflush( stdout );
618  }
619  else if( ret == 2 )
620  {
621  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
622  fclose(file);
623  exit( 2 );
624  }
625  else
626  total_errors++;
627 
628  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
629  break;
630  if( strlen(buf) != 0 )
631  {
632  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
633  return( 1 );
634  }
635  }
636  fclose(file);
637 
638  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
639  if( total_errors == 0 )
640  fprintf( stdout, "PASSED" );
641  else
642  fprintf( stdout, "FAILED" );
643 
644  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
645  total_tests - total_errors, total_tests, total_skipped );
646 
647 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
648 #if defined(POLARSSL_MEMORY_DEBUG)
649  memory_buffer_alloc_status();
650 #endif
651  memory_buffer_alloc_free();
652 #endif
653 
654  return( total_errors != 0 );
655 }
656 
657 
Password-Based Key Derivation Function 2 (from PKCS#5) DEPRECATED: use pkcs5.h instead.
Memory allocation layer.
uint32_t t_uint
Definition: bignum.h:149
Info structure for the pseudo random function.
int s
Definition: bignum.h:173
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 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)
MPI structure.
Definition: bignum.h:171
static int test_assert(int correct, char *test)
static int test_errors
int 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 DEPRECATED: Use pkcs5_pbkdf2_hmac() instead!
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.
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])
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
t_uint * p
Definition: bignum.h:175
#define PUT_UINT32_BE(n, b, i)
int verify_string(char **str)
int dispatch_test(int cnt, char *params[50])
size_t n
Definition: bignum.h:174
unsigned char * buf
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
static int unhexify(unsigned char *obuf, const char *ibuf)
int verify_int(char *str, int *value)
int md_free_ctx(md_context_t *ctx)
Free the message-specific context of ctx.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
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
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)