1/* BEGIN_HEADER */
2#include "mbedtls/pkcs5.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_PKCS5_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void pbkdf2_hmac( int hash, char *hex_password_string,
12                  char *hex_salt_string, int it_cnt, int key_len,
13                  char *result_key_string )
14{
15    unsigned char pw_str[100];
16    unsigned char salt_str[100];
17    unsigned char dst_str[100];
18
19    mbedtls_md_context_t ctx;
20    const mbedtls_md_info_t *info;
21
22    int pw_len, salt_len;
23    unsigned char key[100];
24
25    mbedtls_md_init( &ctx );
26
27    memset(pw_str, 0x00, 100);
28    memset(salt_str, 0x00, 100);
29    memset(dst_str, 0x00, 100);
30
31    pw_len = unhexify( pw_str, hex_password_string );
32    salt_len = unhexify( salt_str, hex_salt_string );
33
34
35    info = mbedtls_md_info_from_type( hash );
36    TEST_ASSERT( info != NULL );
37    TEST_ASSERT( mbedtls_md_setup( &ctx, info, 1 ) == 0 );
38    TEST_ASSERT( mbedtls_pkcs5_pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len,
39                                     it_cnt, key_len, key ) == 0 );
40
41    hexify( dst_str, key, key_len );
42    TEST_ASSERT( strcmp( (char *) dst_str, result_key_string ) == 0 );
43
44exit:
45    mbedtls_md_free( &ctx );
46}
47/* END_CASE */
48
49/* BEGIN_CASE */
50void mbedtls_pkcs5_pbes2( int params_tag, char *params_hex, char *pw_hex,
51                  char *data_hex, int ref_ret, char *ref_out_hex )
52{
53    int my_ret;
54    mbedtls_asn1_buf params;
55    unsigned char *my_out = NULL, *ref_out = NULL, *data = NULL, *pw = NULL;
56    size_t ref_out_len, data_len, pw_len;
57
58    params.tag = params_tag;
59    params.p = unhexify_alloc( params_hex, &params.len );
60
61    data = unhexify_alloc( data_hex, &data_len );
62    pw = unhexify_alloc( pw_hex, &pw_len );
63    ref_out = unhexify_alloc( ref_out_hex, &ref_out_len );
64    my_out = zero_alloc( ref_out_len );
65
66    my_ret = mbedtls_pkcs5_pbes2( &params, MBEDTLS_PKCS5_DECRYPT,
67                          pw, pw_len, data, data_len, my_out );
68    TEST_ASSERT( my_ret == ref_ret );
69
70    if( ref_ret == 0 )
71        TEST_ASSERT( memcmp( my_out, ref_out, ref_out_len ) == 0 );
72
73exit:
74    mbedtls_free( params.p );
75    mbedtls_free( data );
76    mbedtls_free( pw );
77    mbedtls_free( ref_out );
78    mbedtls_free( my_out );
79}
80/* END_CASE */
81
82/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
83void pkcs5_selftest( )
84{
85    TEST_ASSERT( mbedtls_pkcs5_self_test( 1 ) == 0 );
86}
87/* END_CASE */
88