3 #ifdef POLARSSL_PBKDF2_C
9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
15 typedef UINT32 uint32_t;
28 #define GET_UINT32_BE(n,b,i) \
30 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
31 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
32 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
33 | ( (uint32_t) (b)[(i) + 3] ); \
38 #define PUT_UINT32_BE(n,b,i) \
40 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
41 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
42 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
43 (b)[(i) + 3] = (unsigned char) ( (n) ); \
47 static int unhexify(
unsigned char *obuf,
const char *ibuf)
50 int len = strlen(ibuf) / 2;
51 assert(!(strlen(ibuf) %1));
56 if( c >=
'0' && c <=
'9' )
58 else if( c >=
'a' && c <=
'f' )
60 else if( c >=
'A' && c <=
'F' )
66 if( c2 >=
'0' && c2 <=
'9' )
68 else if( c2 >=
'a' && c2 <=
'f' )
70 else if( c2 >=
'A' && c2 <=
'F' )
75 *obuf++ = ( c << 4 ) | c2;
81 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
93 *obuf++ =
'a' + h - 10;
98 *obuf++ =
'a' + l - 10;
114 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
118 if( rng_state != NULL )
121 for( i = 0; i < len; ++i )
132 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
134 if( rng_state != NULL )
137 memset( output, 0, len );
164 if( rng_state == NULL )
173 memcpy( output, info->
buf, use_len );
174 info->
buf += use_len;
178 if( len - use_len > 0 )
179 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
208 uint32_t i, *k, sum, delta=0x9E3779B9;
209 unsigned char result[4];
211 if( rng_state == NULL )
218 size_t use_len = ( len > 4 ) ? 4 : len;
221 for( i = 0; i < 32; i++ )
223 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
225 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
229 memcpy( output, result, use_len );
245 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
248 const char *ibuf = in;
250 assert( len == strlen(ibuf) / 2 );
251 assert(!(strlen(ibuf) %1));
253 obuf = out + (len - 1);
257 if( c >=
'0' && c <=
'9' )
259 else if( c >=
'a' && c <=
'f' )
261 else if( c >=
'A' && c <=
'F' )
267 if( c2 >=
'0' && c2 <=
'9' )
269 else if( c2 >=
'a' && c2 <=
'f' )
271 else if( c2 >=
'A' && c2 <=
'F' )
276 *obuf-- = ( c << 4 ) | c2;
288 #ifdef POLARSSL_PBKDF2_C
290 #define TEST_SUITE_ACTIVE
299 printf(
"FAILED\n" );
300 printf(
" %s\n", test );
305 #define TEST_ASSERT( TEST ) \
306 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
307 if( test_errors) return; \
312 if( (*str)[0] !=
'"' ||
313 (*str)[strlen( *str ) - 1] !=
'"' )
315 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
320 (*str)[strlen( *str ) - 1] =
'\0';
332 for( i = 0; i < strlen( str ); i++ )
334 if( i == 0 && str[i] ==
'-' )
340 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
341 str[i - 1] ==
'0' && str[i] ==
'x' )
347 if( str[i] <
'0' || str[i] >
'9' )
357 *value = strtol( str, NULL, 16 );
359 *value = strtol( str, NULL, 10 );
364 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
371 printf(
"Expected integer for parameter and got: %s\n", str );
375 void test_suite_pbkdf2_hmac(
int hash,
char *hex_password_string,
char *hex_salt_string,
376 int it_cnt,
int key_len,
char *result_key_string )
378 unsigned char pw_str[100];
379 unsigned char salt_str[100];
380 unsigned char dst_str[100];
385 int pw_len, salt_len;
386 unsigned char key[100];
388 memset(pw_str, 0x00, 100);
389 memset(salt_str, 0x00, 100);
390 memset(dst_str, 0x00, 100);
392 pw_len =
unhexify( pw_str, hex_password_string );
393 salt_len =
unhexify( salt_str, hex_salt_string );
402 it_cnt, key_len, key ) == 0 );
405 hexify( dst_str, key, key_len );
406 TEST_ASSERT( strcmp( (
char *) dst_str, result_key_string ) == 0 );
418 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
420 #if defined(POLARSSL_SHA1_C)
437 #if defined(TEST_SUITE_ACTIVE)
438 if( strcmp( params[0],
"pbkdf2_hmac" ) == 0 )
442 char *param2 = params[2];
443 char *param3 = params[3];
446 char *param6 = params[6];
450 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
454 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
457 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
458 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
461 test_suite_pbkdf2_hmac( param1, param2, param3, param4, param5, param6 );
469 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
483 ret = fgets( buf, len, f );
487 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
488 buf[strlen(buf) - 1] =
'\0';
489 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
490 buf[strlen(buf) - 1] =
'\0';
503 while( *p !=
'\0' && p < buf + len )
513 if( p + 1 < buf + len )
525 for( i = 0; i < cnt; i++ )
532 if( *p ==
'\\' && *(p + 1) ==
'n' )
537 else if( *p ==
'\\' && *(p + 1) ==
':' )
542 else if( *p ==
'\\' && *(p + 1) ==
'?' )
558 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
559 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_pbkdf2.data";
564 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
565 unsigned char alloc_buf[1000000];
566 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
569 file = fopen( filename,
"r" );
572 fprintf( stderr,
"Failed to open\n" );
576 while( !feof( file ) )
580 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
582 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
583 fprintf( stdout,
" " );
584 for( i = strlen( buf ) + 1; i < 67; i++ )
585 fprintf( stdout,
"." );
586 fprintf( stdout,
" " );
591 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
595 if( strcmp( params[0],
"depends_on" ) == 0 )
597 for( i = 1; i < cnt; i++ )
601 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
612 if( skip == 1 || ret == 3 )
615 fprintf( stdout,
"----\n" );
620 fprintf( stdout,
"PASS\n" );
625 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
632 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
634 if( strlen(buf) != 0 )
636 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
642 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
643 if( total_errors == 0 )
644 fprintf( stdout,
"PASSED" );
646 fprintf( stdout,
"FAILED" );
648 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
649 total_tests - total_errors, total_tests, total_skipped );
651 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
652 #if defined(POLARSSL_MEMORY_DEBUG)
653 memory_buffer_alloc_status();
655 memory_buffer_alloc_free();
658 return( total_errors != 0 );