1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
7 #ifdef POLARSSL_PKCS5_C
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
17 #if defined(POLARSSL_PLATFORM_C)
20 #define polarssl_malloc malloc
21 #define polarssl_free free
26 typedef UINT32 uint32_t;
39 #define GET_UINT32_BE(n,b,i) \
41 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
42 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
43 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
44 | ( (uint32_t) (b)[(i) + 3] ); \
49 #define PUT_UINT32_BE(n,b,i) \
51 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
52 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
53 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
54 (b)[(i) + 3] = (unsigned char) ( (n) ); \
58 static int unhexify(
unsigned char *obuf,
const char *ibuf)
61 int len = strlen(ibuf) / 2;
62 assert(!(strlen(ibuf) %1));
67 if( c >=
'0' && c <=
'9' )
69 else if( c >=
'a' && c <=
'f' )
71 else if( c >=
'A' && c <=
'F' )
77 if( c2 >=
'0' && c2 <=
'9' )
79 else if( c2 >=
'a' && c2 <=
'f' )
81 else if( c2 >=
'A' && c2 <=
'F' )
86 *obuf++ = ( c << 4 ) | c2;
92 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
104 *obuf++ =
'a' + h - 10;
109 *obuf++ =
'a' + l - 10;
126 size_t actual_len = len != 0 ? len : 1;
131 memset( p, 0x00, actual_len );
150 *olen = strlen(ibuf) / 2;
156 assert( obuf != NULL );
172 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
174 #if !defined(__OpenBSD__)
177 if( rng_state != NULL )
180 for( i = 0; i < len; ++i )
183 if( rng_state != NULL )
186 arc4random_buf( output, len );
197 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
199 if( rng_state != NULL )
202 memset( output, 0, len );
229 if( rng_state == NULL )
238 memcpy( output, info->
buf, use_len );
239 info->
buf += use_len;
243 if( len - use_len > 0 )
244 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
273 uint32_t i, *k, sum, delta=0x9E3779B9;
274 unsigned char result[4], *out = output;
276 if( rng_state == NULL )
283 size_t use_len = ( len > 4 ) ? 4 : len;
286 for( i = 0; i < 32; i++ )
288 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
290 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
294 memcpy( out, result, use_len );
306 #if defined(POLARSSL_PLATFORM_C)
309 #define polarssl_printf printf
310 #define polarssl_malloc malloc
311 #define polarssl_free free
316 #ifdef POLARSSL_PKCS5_C
318 #define TEST_SUITE_ACTIVE
320 static int test_assert(
int correct,
const char *test )
327 printf(
"FAILED\n" );
328 printf(
" %s\n", test );
333 #define TEST_ASSERT( TEST ) \
334 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
335 if( test_errors) goto exit; \
340 if( (*str)[0] !=
'"' ||
341 (*str)[strlen( *str ) - 1] !=
'"' )
343 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
348 (*str)[strlen( *str ) - 1] =
'\0';
360 for( i = 0; i < strlen( str ); i++ )
362 if( i == 0 && str[i] ==
'-' )
368 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
369 str[i - 1] ==
'0' && str[i] ==
'x' )
375 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
376 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
377 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
387 *value = strtol( str, NULL, 16 );
389 *value = strtol( str, NULL, 10 );
394 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
399 if( strcmp( str,
"POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH" ) == 0 )
404 if( strcmp( str,
"POLARSSL_ERR_PKCS5_INVALID_FORMAT" ) == 0 )
409 if( strcmp( str,
"POLARSSL_ERR_PKCS5_INVALID_FORMAT + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
414 if( strcmp( str,
"ASN1_SEQUENCE" ) == 0 )
419 if( strcmp( str,
"POLARSSL_ERR_PKCS5_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
424 if( strcmp( str,
"POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE" ) == 0 )
429 if( strcmp( str,
"POLARSSL_ERR_PKCS5_INVALID_FORMAT + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
434 if( strcmp( str,
"ASN1_CONSTRUCTED | ASN1_SEQUENCE" ) == 0 )
441 printf(
"Expected integer for parameter and got: %s\n", str );
445 void test_suite_pbkdf2_hmac(
int hash,
char *hex_password_string,
446 char *hex_salt_string,
int it_cnt,
int key_len,
447 char *result_key_string )
449 unsigned char pw_str[100];
450 unsigned char salt_str[100];
451 unsigned char dst_str[100];
456 int pw_len, salt_len;
457 unsigned char key[100];
461 memset(pw_str, 0x00, 100);
462 memset(salt_str, 0x00, 100);
463 memset(dst_str, 0x00, 100);
465 pw_len =
unhexify( pw_str, hex_password_string );
466 salt_len =
unhexify( salt_str, hex_salt_string );
475 it_cnt, key_len, key ) == 0 );
477 hexify( dst_str, key, key_len );
478 TEST_ASSERT( strcmp( (
char *) dst_str, result_key_string ) == 0 );
484 void test_suite_pkcs5_pbes2(
int params_tag,
char *params_hex,
char *pw_hex,
485 char *data_hex,
int ref_ret,
char *ref_out_hex )
489 unsigned char *my_out = NULL, *ref_out = NULL, *data = NULL, *pw = NULL;
490 size_t ref_out_len, data_len, pw_len;
492 params.
tag = params_tag;
501 pw, pw_len, data, data_len, my_out );
505 TEST_ASSERT( memcmp( my_out, ref_out, ref_out_len ) == 0 );
524 if( strcmp( str,
"POLARSSL_DES_C" ) == 0 )
526 #if defined(POLARSSL_DES_C)
532 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
534 #if defined(POLARSSL_SHA1_C)
551 #if defined(TEST_SUITE_ACTIVE)
552 if( strcmp( params[0],
"pbkdf2_hmac" ) == 0 )
556 char *param2 = params[2];
557 char *param3 = params[3];
560 char *param6 = params[6];
564 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
568 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
571 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
572 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
575 test_suite_pbkdf2_hmac( param1, param2, param3, param4, param5, param6 );
581 if( strcmp( params[0],
"pkcs5_pbes2" ) == 0 )
585 char *param2 = params[2];
586 char *param3 = params[3];
587 char *param4 = params[4];
589 char *param6 = params[6];
593 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
597 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
601 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
604 test_suite_pkcs5_pbes2( param1, param2, param3, param4, param5, param6 );
612 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
626 ret = fgets( buf, len, f );
630 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
631 buf[strlen(buf) - 1] =
'\0';
632 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
633 buf[strlen(buf) - 1] =
'\0';
646 while( *p !=
'\0' && p < buf + len )
656 if( p + 1 < buf + len )
668 for( i = 0; i < cnt; i++ )
675 if( *p ==
'\\' && *(p + 1) ==
'n' )
680 else if( *p ==
'\\' && *(p + 1) ==
':' )
685 else if( *p ==
'\\' && *(p + 1) ==
'?' )
701 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
702 const char *filename =
"/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_pkcs5.data";
707 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
708 unsigned char alloc_buf[1000000];
712 file = fopen( filename,
"r" );
715 fprintf( stderr,
"Failed to open\n" );
719 while( !feof( file ) )
723 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
725 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
726 fprintf( stdout,
" " );
727 for( i = strlen( buf ) + 1; i < 67; i++ )
728 fprintf( stdout,
"." );
729 fprintf( stdout,
" " );
734 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
738 if( strcmp( params[0],
"depends_on" ) == 0 )
740 for( i = 1; i < cnt; i++ )
744 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
755 if( skip == 1 || ret == 3 )
758 fprintf( stdout,
"----\n" );
763 fprintf( stdout,
"PASS\n" );
768 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
775 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
777 if( strlen(buf) != 0 )
779 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
785 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
786 if( total_errors == 0 )
787 fprintf( stdout,
"PASSED" );
789 fprintf( stdout,
"FAILED" );
791 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
792 total_tests - total_errors, total_tests, total_skipped );
794 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
795 #if defined(POLARSSL_MEMORY_DEBUG)
796 memory_buffer_alloc_status();
801 return( total_errors != 0 );
#define POLARSSL_ERR_PKCS5_INVALID_FORMAT
Unexpected ASN.1 data.
Memory allocation layer (Deprecated to platform layer)
Info structure for the pseudo random function.
#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH
Actual length differs from expected length.
void md_init(md_context_t *ctx)
Initialize a md_context (as NONE)
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
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.
#define POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE
Requested encryption or digest alg not available.
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 rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
static int test_assert(int correct, const char *test)
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 unhexify(unsigned char *obuf, const char *ibuf)
int parse_arguments(char *buf, size_t len, char *params[50])
#define PUT_UINT32_BE(n, b, i)
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
#define POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH
Given private key password does not allow for correct decryption.
int get_line(FILE *f, char *buf, size_t len)
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
void md_free(md_context_t *ctx)
Free and clear the message-specific context of ctx.
int pkcs5_pbes2(asn1_buf *pbe_params, int mode, const unsigned char *pwd, size_t pwdlen, const unsigned char *data, size_t datalen, unsigned char *output)
PKCS#5 PBES2 function.
unsigned char * p
ASN1 data, e.g.
int dispatch_test(int cnt, char *params[50])
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
#define POLARSSL_ERR_ASN1_OUT_OF_DATA
Out of data when parsing an ASN1 data structure.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Type-length-value structure that allows for ASN1 using DER.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
size_t len
ASN1 length, e.g.
int verify_string(char **str)
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().
Message digest information.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG
ASN1 tag was of an unexpected value.
Generic message digest context.