3 #ifdef POLARSSL_PKCS5_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_PKCS5_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,
376 char *hex_salt_string,
int it_cnt,
int key_len,
377 char *result_key_string )
379 unsigned char pw_str[100];
380 unsigned char salt_str[100];
381 unsigned char dst_str[100];
386 int pw_len, salt_len;
387 unsigned char key[100];
389 memset(pw_str, 0x00, 100);
390 memset(salt_str, 0x00, 100);
391 memset(dst_str, 0x00, 100);
393 pw_len =
unhexify( pw_str, hex_password_string );
394 salt_len =
unhexify( salt_str, hex_salt_string );
403 it_cnt, key_len, key ) == 0 );
406 hexify( dst_str, key, key_len );
407 TEST_ASSERT( strcmp( (
char *) dst_str, result_key_string ) == 0 );
419 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
421 #if defined(POLARSSL_SHA1_C)
438 #if defined(TEST_SUITE_ACTIVE)
439 if( strcmp( params[0],
"pbkdf2_hmac" ) == 0 )
443 char *param2 = params[2];
444 char *param3 = params[3];
447 char *param6 = params[6];
451 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
455 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
458 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
459 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
462 test_suite_pbkdf2_hmac( param1, param2, param3, param4, param5, param6 );
470 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
484 ret = fgets( buf, len, f );
488 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
489 buf[strlen(buf) - 1] =
'\0';
490 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
491 buf[strlen(buf) - 1] =
'\0';
504 while( *p !=
'\0' && p < buf + len )
514 if( p + 1 < buf + len )
526 for( i = 0; i < cnt; i++ )
533 if( *p ==
'\\' && *(p + 1) ==
'n' )
538 else if( *p ==
'\\' && *(p + 1) ==
':' )
543 else if( *p ==
'\\' && *(p + 1) ==
'?' )
559 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
560 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_pkcs5.data";
565 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
566 unsigned char alloc_buf[1000000];
567 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
570 file = fopen( filename,
"r" );
573 fprintf( stderr,
"Failed to open\n" );
577 while( !feof( file ) )
581 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
583 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
584 fprintf( stdout,
" " );
585 for( i = strlen( buf ) + 1; i < 67; i++ )
586 fprintf( stdout,
"." );
587 fprintf( stdout,
" " );
592 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
596 if( strcmp( params[0],
"depends_on" ) == 0 )
598 for( i = 1; i < cnt; i++ )
602 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
613 if( skip == 1 || ret == 3 )
616 fprintf( stdout,
"----\n" );
621 fprintf( stdout,
"PASS\n" );
626 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
633 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
635 if( strlen(buf) != 0 )
637 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
643 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
644 if( total_errors == 0 )
645 fprintf( stdout,
"PASSED" );
647 fprintf( stdout,
"FAILED" );
649 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
650 total_tests - total_errors, total_tests, total_skipped );
652 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
653 #if defined(POLARSSL_MEMORY_DEBUG)
654 memory_buffer_alloc_status();
656 memory_buffer_alloc_free();
659 return( total_errors != 0 );