2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pkcs5.h"
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_PKCS5_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void pbkdf2_hmac( int hash, char *hex_password_string,
|
|
|
|
char *hex_salt_string, int it_cnt, int key_len,
|
|
|
|
char *result_key_string )
|
2013-06-24 19:26:38 +02:00
|
|
|
{
|
|
|
|
unsigned char pw_str[100];
|
|
|
|
unsigned char salt_str[100];
|
2018-01-31 16:45:09 +01:00
|
|
|
unsigned char dst_str[200];
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_context_t ctx;
|
|
|
|
const mbedtls_md_info_t *info;
|
2013-06-24 19:26:38 +02:00
|
|
|
|
|
|
|
int pw_len, salt_len;
|
|
|
|
unsigned char key[100];
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_init( &ctx );
|
2014-07-01 15:45:49 +02:00
|
|
|
|
2018-01-31 16:45:09 +01:00
|
|
|
memset(pw_str, 0x00, sizeof(pw_str));
|
|
|
|
memset(salt_str, 0x00, sizeof(salt_str));
|
|
|
|
memset(dst_str, 0x00, sizeof(dst_str));
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
pw_len = unhexify( pw_str, hex_password_string );
|
|
|
|
salt_len = unhexify( salt_str, hex_salt_string );
|
2013-06-24 19:26:38 +02:00
|
|
|
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
info = mbedtls_md_info_from_type( hash );
|
2013-06-24 19:26:38 +02:00
|
|
|
TEST_ASSERT( info != NULL );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_md_setup( &ctx, info, 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pkcs5_pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len,
|
2013-08-20 11:48:36 +02:00
|
|
|
it_cnt, key_len, key ) == 0 );
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
hexify( dst_str, key, key_len );
|
|
|
|
TEST_ASSERT( strcmp( (char *) dst_str, result_key_string ) == 0 );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_free( &ctx );
|
2013-06-24 19:26:38 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2014-06-12 13:14:55 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_pkcs5_pbes2( int params_tag, char *params_hex, char *pw_hex,
|
2014-06-12 13:14:55 +02:00
|
|
|
char *data_hex, int ref_ret, char *ref_out_hex )
|
|
|
|
{
|
|
|
|
int my_ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_buf params;
|
2014-07-10 15:26:12 +02:00
|
|
|
unsigned char *my_out = NULL, *ref_out = NULL, *data = NULL, *pw = NULL;
|
2014-06-12 13:14:55 +02:00
|
|
|
size_t ref_out_len, data_len, pw_len;
|
|
|
|
|
|
|
|
params.tag = params_tag;
|
|
|
|
params.p = unhexify_alloc( params_hex, ¶ms.len );
|
|
|
|
|
|
|
|
data = unhexify_alloc( data_hex, &data_len );
|
|
|
|
pw = unhexify_alloc( pw_hex, &pw_len );
|
|
|
|
ref_out = unhexify_alloc( ref_out_hex, &ref_out_len );
|
2014-06-13 21:09:26 +02:00
|
|
|
my_out = zero_alloc( ref_out_len );
|
2014-06-12 13:14:55 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
my_ret = mbedtls_pkcs5_pbes2( ¶ms, MBEDTLS_PKCS5_DECRYPT,
|
2014-06-12 13:14:55 +02:00
|
|
|
pw, pw_len, data, data_len, my_out );
|
|
|
|
TEST_ASSERT( my_ret == ref_ret );
|
|
|
|
|
|
|
|
if( ref_ret == 0 )
|
|
|
|
TEST_ASSERT( memcmp( my_out, ref_out, ref_out_len ) == 0 );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( params.p );
|
|
|
|
mbedtls_free( data );
|
|
|
|
mbedtls_free( pw );
|
|
|
|
mbedtls_free( ref_out );
|
|
|
|
mbedtls_free( my_out );
|
2014-06-12 13:14:55 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2016-07-19 15:54:57 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
|
|
|
void pkcs5_selftest( )
|
|
|
|
{
|
2016-09-09 10:10:28 +02:00
|
|
|
TEST_ASSERT( mbedtls_pkcs5_self_test( 1 ) == 0 );
|
2016-07-19 15:54:57 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|