mbed TLS Build

Dependents:   Encrypt_Decrypt1 mbed_blink_tls encrypt encrypt

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

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