Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /*
marcozecchini 0:9fca2b23d0ba 2 * PKCS#12 Personal Information Exchange Syntax
marcozecchini 0:9fca2b23d0ba 3 *
marcozecchini 0:9fca2b23d0ba 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
marcozecchini 0:9fca2b23d0ba 5 * SPDX-License-Identifier: Apache-2.0
marcozecchini 0:9fca2b23d0ba 6 *
marcozecchini 0:9fca2b23d0ba 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
marcozecchini 0:9fca2b23d0ba 8 * not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 9 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 10 *
marcozecchini 0:9fca2b23d0ba 11 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 12 *
marcozecchini 0:9fca2b23d0ba 13 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
marcozecchini 0:9fca2b23d0ba 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 16 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 17 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 18 *
marcozecchini 0:9fca2b23d0ba 19 * This file is part of mbed TLS (https://tls.mbed.org)
marcozecchini 0:9fca2b23d0ba 20 */
marcozecchini 0:9fca2b23d0ba 21 /*
marcozecchini 0:9fca2b23d0ba 22 * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
marcozecchini 0:9fca2b23d0ba 23 *
marcozecchini 0:9fca2b23d0ba 24 * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
marcozecchini 0:9fca2b23d0ba 25 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
marcozecchini 0:9fca2b23d0ba 26 */
marcozecchini 0:9fca2b23d0ba 27
marcozecchini 0:9fca2b23d0ba 28 #if !defined(MBEDTLS_CONFIG_FILE)
marcozecchini 0:9fca2b23d0ba 29 #include "mbedtls/config.h"
marcozecchini 0:9fca2b23d0ba 30 #else
marcozecchini 0:9fca2b23d0ba 31 #include MBEDTLS_CONFIG_FILE
marcozecchini 0:9fca2b23d0ba 32 #endif
marcozecchini 0:9fca2b23d0ba 33
marcozecchini 0:9fca2b23d0ba 34 #if defined(MBEDTLS_PKCS12_C)
marcozecchini 0:9fca2b23d0ba 35
marcozecchini 0:9fca2b23d0ba 36 #include "mbedtls/pkcs12.h"
marcozecchini 0:9fca2b23d0ba 37 #include "mbedtls/asn1.h"
marcozecchini 0:9fca2b23d0ba 38 #include "mbedtls/cipher.h"
marcozecchini 0:9fca2b23d0ba 39
marcozecchini 0:9fca2b23d0ba 40 #include <string.h>
marcozecchini 0:9fca2b23d0ba 41
marcozecchini 0:9fca2b23d0ba 42 #if defined(MBEDTLS_ARC4_C)
marcozecchini 0:9fca2b23d0ba 43 #include "mbedtls/arc4.h"
marcozecchini 0:9fca2b23d0ba 44 #endif
marcozecchini 0:9fca2b23d0ba 45
marcozecchini 0:9fca2b23d0ba 46 #if defined(MBEDTLS_DES_C)
marcozecchini 0:9fca2b23d0ba 47 #include "mbedtls/des.h"
marcozecchini 0:9fca2b23d0ba 48 #endif
marcozecchini 0:9fca2b23d0ba 49
marcozecchini 0:9fca2b23d0ba 50 /* Implementation that should never be optimized out by the compiler */
marcozecchini 0:9fca2b23d0ba 51 static void mbedtls_zeroize( void *v, size_t n ) {
marcozecchini 0:9fca2b23d0ba 52 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
marcozecchini 0:9fca2b23d0ba 53 }
marcozecchini 0:9fca2b23d0ba 54
marcozecchini 0:9fca2b23d0ba 55 static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
marcozecchini 0:9fca2b23d0ba 56 mbedtls_asn1_buf *salt, int *iterations )
marcozecchini 0:9fca2b23d0ba 57 {
marcozecchini 0:9fca2b23d0ba 58 int ret;
marcozecchini 0:9fca2b23d0ba 59 unsigned char **p = &params->p;
marcozecchini 0:9fca2b23d0ba 60 const unsigned char *end = params->p + params->len;
marcozecchini 0:9fca2b23d0ba 61
marcozecchini 0:9fca2b23d0ba 62 /*
marcozecchini 0:9fca2b23d0ba 63 * pkcs-12PbeParams ::= SEQUENCE {
marcozecchini 0:9fca2b23d0ba 64 * salt OCTET STRING,
marcozecchini 0:9fca2b23d0ba 65 * iterations INTEGER
marcozecchini 0:9fca2b23d0ba 66 * }
marcozecchini 0:9fca2b23d0ba 67 *
marcozecchini 0:9fca2b23d0ba 68 */
marcozecchini 0:9fca2b23d0ba 69 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
marcozecchini 0:9fca2b23d0ba 70 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
marcozecchini 0:9fca2b23d0ba 71 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
marcozecchini 0:9fca2b23d0ba 72
marcozecchini 0:9fca2b23d0ba 73 if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 74 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
marcozecchini 0:9fca2b23d0ba 75
marcozecchini 0:9fca2b23d0ba 76 salt->p = *p;
marcozecchini 0:9fca2b23d0ba 77 *p += salt->len;
marcozecchini 0:9fca2b23d0ba 78
marcozecchini 0:9fca2b23d0ba 79 if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 80 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
marcozecchini 0:9fca2b23d0ba 81
marcozecchini 0:9fca2b23d0ba 82 if( *p != end )
marcozecchini 0:9fca2b23d0ba 83 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
marcozecchini 0:9fca2b23d0ba 84 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
marcozecchini 0:9fca2b23d0ba 85
marcozecchini 0:9fca2b23d0ba 86 return( 0 );
marcozecchini 0:9fca2b23d0ba 87 }
marcozecchini 0:9fca2b23d0ba 88
marcozecchini 0:9fca2b23d0ba 89 #define PKCS12_MAX_PWDLEN 128
marcozecchini 0:9fca2b23d0ba 90
marcozecchini 0:9fca2b23d0ba 91 static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
marcozecchini 0:9fca2b23d0ba 92 const unsigned char *pwd, size_t pwdlen,
marcozecchini 0:9fca2b23d0ba 93 unsigned char *key, size_t keylen,
marcozecchini 0:9fca2b23d0ba 94 unsigned char *iv, size_t ivlen )
marcozecchini 0:9fca2b23d0ba 95 {
marcozecchini 0:9fca2b23d0ba 96 int ret, iterations = 0;
marcozecchini 0:9fca2b23d0ba 97 mbedtls_asn1_buf salt;
marcozecchini 0:9fca2b23d0ba 98 size_t i;
marcozecchini 0:9fca2b23d0ba 99 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
marcozecchini 0:9fca2b23d0ba 100
marcozecchini 0:9fca2b23d0ba 101 if( pwdlen > PKCS12_MAX_PWDLEN )
marcozecchini 0:9fca2b23d0ba 102 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
marcozecchini 0:9fca2b23d0ba 103
marcozecchini 0:9fca2b23d0ba 104 memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
marcozecchini 0:9fca2b23d0ba 105 memset( &unipwd, 0, sizeof(unipwd) );
marcozecchini 0:9fca2b23d0ba 106
marcozecchini 0:9fca2b23d0ba 107 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
marcozecchini 0:9fca2b23d0ba 108 &iterations ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 109 return( ret );
marcozecchini 0:9fca2b23d0ba 110
marcozecchini 0:9fca2b23d0ba 111 for( i = 0; i < pwdlen; i++ )
marcozecchini 0:9fca2b23d0ba 112 unipwd[i * 2 + 1] = pwd[i];
marcozecchini 0:9fca2b23d0ba 113
marcozecchini 0:9fca2b23d0ba 114 if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
marcozecchini 0:9fca2b23d0ba 115 salt.p, salt.len, md_type,
marcozecchini 0:9fca2b23d0ba 116 MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 117 {
marcozecchini 0:9fca2b23d0ba 118 return( ret );
marcozecchini 0:9fca2b23d0ba 119 }
marcozecchini 0:9fca2b23d0ba 120
marcozecchini 0:9fca2b23d0ba 121 if( iv == NULL || ivlen == 0 )
marcozecchini 0:9fca2b23d0ba 122 return( 0 );
marcozecchini 0:9fca2b23d0ba 123
marcozecchini 0:9fca2b23d0ba 124 if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
marcozecchini 0:9fca2b23d0ba 125 salt.p, salt.len, md_type,
marcozecchini 0:9fca2b23d0ba 126 MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 127 {
marcozecchini 0:9fca2b23d0ba 128 return( ret );
marcozecchini 0:9fca2b23d0ba 129 }
marcozecchini 0:9fca2b23d0ba 130 return( 0 );
marcozecchini 0:9fca2b23d0ba 131 }
marcozecchini 0:9fca2b23d0ba 132
marcozecchini 0:9fca2b23d0ba 133 #undef PKCS12_MAX_PWDLEN
marcozecchini 0:9fca2b23d0ba 134
marcozecchini 0:9fca2b23d0ba 135 int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
marcozecchini 0:9fca2b23d0ba 136 const unsigned char *pwd, size_t pwdlen,
marcozecchini 0:9fca2b23d0ba 137 const unsigned char *data, size_t len,
marcozecchini 0:9fca2b23d0ba 138 unsigned char *output )
marcozecchini 0:9fca2b23d0ba 139 {
marcozecchini 0:9fca2b23d0ba 140 #if !defined(MBEDTLS_ARC4_C)
marcozecchini 0:9fca2b23d0ba 141 ((void) pbe_params);
marcozecchini 0:9fca2b23d0ba 142 ((void) mode);
marcozecchini 0:9fca2b23d0ba 143 ((void) pwd);
marcozecchini 0:9fca2b23d0ba 144 ((void) pwdlen);
marcozecchini 0:9fca2b23d0ba 145 ((void) data);
marcozecchini 0:9fca2b23d0ba 146 ((void) len);
marcozecchini 0:9fca2b23d0ba 147 ((void) output);
marcozecchini 0:9fca2b23d0ba 148 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
marcozecchini 0:9fca2b23d0ba 149 #else
marcozecchini 0:9fca2b23d0ba 150 int ret;
marcozecchini 0:9fca2b23d0ba 151 unsigned char key[16];
marcozecchini 0:9fca2b23d0ba 152 mbedtls_arc4_context ctx;
marcozecchini 0:9fca2b23d0ba 153 ((void) mode);
marcozecchini 0:9fca2b23d0ba 154
marcozecchini 0:9fca2b23d0ba 155 mbedtls_arc4_init( &ctx );
marcozecchini 0:9fca2b23d0ba 156
marcozecchini 0:9fca2b23d0ba 157 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
marcozecchini 0:9fca2b23d0ba 158 pwd, pwdlen,
marcozecchini 0:9fca2b23d0ba 159 key, 16, NULL, 0 ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 160 {
marcozecchini 0:9fca2b23d0ba 161 return( ret );
marcozecchini 0:9fca2b23d0ba 162 }
marcozecchini 0:9fca2b23d0ba 163
marcozecchini 0:9fca2b23d0ba 164 mbedtls_arc4_setup( &ctx, key, 16 );
marcozecchini 0:9fca2b23d0ba 165 if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 166 goto exit;
marcozecchini 0:9fca2b23d0ba 167
marcozecchini 0:9fca2b23d0ba 168 exit:
marcozecchini 0:9fca2b23d0ba 169 mbedtls_zeroize( key, sizeof( key ) );
marcozecchini 0:9fca2b23d0ba 170 mbedtls_arc4_free( &ctx );
marcozecchini 0:9fca2b23d0ba 171
marcozecchini 0:9fca2b23d0ba 172 return( ret );
marcozecchini 0:9fca2b23d0ba 173 #endif /* MBEDTLS_ARC4_C */
marcozecchini 0:9fca2b23d0ba 174 }
marcozecchini 0:9fca2b23d0ba 175
marcozecchini 0:9fca2b23d0ba 176 int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
marcozecchini 0:9fca2b23d0ba 177 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
marcozecchini 0:9fca2b23d0ba 178 const unsigned char *pwd, size_t pwdlen,
marcozecchini 0:9fca2b23d0ba 179 const unsigned char *data, size_t len,
marcozecchini 0:9fca2b23d0ba 180 unsigned char *output )
marcozecchini 0:9fca2b23d0ba 181 {
marcozecchini 0:9fca2b23d0ba 182 int ret, keylen = 0;
marcozecchini 0:9fca2b23d0ba 183 unsigned char key[32];
marcozecchini 0:9fca2b23d0ba 184 unsigned char iv[16];
marcozecchini 0:9fca2b23d0ba 185 const mbedtls_cipher_info_t *cipher_info;
marcozecchini 0:9fca2b23d0ba 186 mbedtls_cipher_context_t cipher_ctx;
marcozecchini 0:9fca2b23d0ba 187 size_t olen = 0;
marcozecchini 0:9fca2b23d0ba 188
marcozecchini 0:9fca2b23d0ba 189 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
marcozecchini 0:9fca2b23d0ba 190 if( cipher_info == NULL )
marcozecchini 0:9fca2b23d0ba 191 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
marcozecchini 0:9fca2b23d0ba 192
marcozecchini 0:9fca2b23d0ba 193 keylen = cipher_info->key_bitlen / 8;
marcozecchini 0:9fca2b23d0ba 194
marcozecchini 0:9fca2b23d0ba 195 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
marcozecchini 0:9fca2b23d0ba 196 key, keylen,
marcozecchini 0:9fca2b23d0ba 197 iv, cipher_info->iv_size ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 198 {
marcozecchini 0:9fca2b23d0ba 199 return( ret );
marcozecchini 0:9fca2b23d0ba 200 }
marcozecchini 0:9fca2b23d0ba 201
marcozecchini 0:9fca2b23d0ba 202 mbedtls_cipher_init( &cipher_ctx );
marcozecchini 0:9fca2b23d0ba 203
marcozecchini 0:9fca2b23d0ba 204 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 205 goto exit;
marcozecchini 0:9fca2b23d0ba 206
marcozecchini 0:9fca2b23d0ba 207 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 208 goto exit;
marcozecchini 0:9fca2b23d0ba 209
marcozecchini 0:9fca2b23d0ba 210 if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 211 goto exit;
marcozecchini 0:9fca2b23d0ba 212
marcozecchini 0:9fca2b23d0ba 213 if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 214 goto exit;
marcozecchini 0:9fca2b23d0ba 215
marcozecchini 0:9fca2b23d0ba 216 if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
marcozecchini 0:9fca2b23d0ba 217 output, &olen ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 218 {
marcozecchini 0:9fca2b23d0ba 219 goto exit;
marcozecchini 0:9fca2b23d0ba 220 }
marcozecchini 0:9fca2b23d0ba 221
marcozecchini 0:9fca2b23d0ba 222 if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 223 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
marcozecchini 0:9fca2b23d0ba 224
marcozecchini 0:9fca2b23d0ba 225 exit:
marcozecchini 0:9fca2b23d0ba 226 mbedtls_zeroize( key, sizeof( key ) );
marcozecchini 0:9fca2b23d0ba 227 mbedtls_zeroize( iv, sizeof( iv ) );
marcozecchini 0:9fca2b23d0ba 228 mbedtls_cipher_free( &cipher_ctx );
marcozecchini 0:9fca2b23d0ba 229
marcozecchini 0:9fca2b23d0ba 230 return( ret );
marcozecchini 0:9fca2b23d0ba 231 }
marcozecchini 0:9fca2b23d0ba 232
marcozecchini 0:9fca2b23d0ba 233 static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
marcozecchini 0:9fca2b23d0ba 234 const unsigned char *filler, size_t fill_len )
marcozecchini 0:9fca2b23d0ba 235 {
marcozecchini 0:9fca2b23d0ba 236 unsigned char *p = data;
marcozecchini 0:9fca2b23d0ba 237 size_t use_len;
marcozecchini 0:9fca2b23d0ba 238
marcozecchini 0:9fca2b23d0ba 239 while( data_len > 0 )
marcozecchini 0:9fca2b23d0ba 240 {
marcozecchini 0:9fca2b23d0ba 241 use_len = ( data_len > fill_len ) ? fill_len : data_len;
marcozecchini 0:9fca2b23d0ba 242 memcpy( p, filler, use_len );
marcozecchini 0:9fca2b23d0ba 243 p += use_len;
marcozecchini 0:9fca2b23d0ba 244 data_len -= use_len;
marcozecchini 0:9fca2b23d0ba 245 }
marcozecchini 0:9fca2b23d0ba 246 }
marcozecchini 0:9fca2b23d0ba 247
marcozecchini 0:9fca2b23d0ba 248 int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
marcozecchini 0:9fca2b23d0ba 249 const unsigned char *pwd, size_t pwdlen,
marcozecchini 0:9fca2b23d0ba 250 const unsigned char *salt, size_t saltlen,
marcozecchini 0:9fca2b23d0ba 251 mbedtls_md_type_t md_type, int id, int iterations )
marcozecchini 0:9fca2b23d0ba 252 {
marcozecchini 0:9fca2b23d0ba 253 int ret;
marcozecchini 0:9fca2b23d0ba 254 unsigned int j;
marcozecchini 0:9fca2b23d0ba 255
marcozecchini 0:9fca2b23d0ba 256 unsigned char diversifier[128];
marcozecchini 0:9fca2b23d0ba 257 unsigned char salt_block[128], pwd_block[128], hash_block[128];
marcozecchini 0:9fca2b23d0ba 258 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
marcozecchini 0:9fca2b23d0ba 259 unsigned char *p;
marcozecchini 0:9fca2b23d0ba 260 unsigned char c;
marcozecchini 0:9fca2b23d0ba 261
marcozecchini 0:9fca2b23d0ba 262 size_t hlen, use_len, v, i;
marcozecchini 0:9fca2b23d0ba 263
marcozecchini 0:9fca2b23d0ba 264 const mbedtls_md_info_t *md_info;
marcozecchini 0:9fca2b23d0ba 265 mbedtls_md_context_t md_ctx;
marcozecchini 0:9fca2b23d0ba 266
marcozecchini 0:9fca2b23d0ba 267 // This version only allows max of 64 bytes of password or salt
marcozecchini 0:9fca2b23d0ba 268 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
marcozecchini 0:9fca2b23d0ba 269 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
marcozecchini 0:9fca2b23d0ba 270
marcozecchini 0:9fca2b23d0ba 271 md_info = mbedtls_md_info_from_type( md_type );
marcozecchini 0:9fca2b23d0ba 272 if( md_info == NULL )
marcozecchini 0:9fca2b23d0ba 273 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
marcozecchini 0:9fca2b23d0ba 274
marcozecchini 0:9fca2b23d0ba 275 mbedtls_md_init( &md_ctx );
marcozecchini 0:9fca2b23d0ba 276
marcozecchini 0:9fca2b23d0ba 277 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 278 return( ret );
marcozecchini 0:9fca2b23d0ba 279 hlen = mbedtls_md_get_size( md_info );
marcozecchini 0:9fca2b23d0ba 280
marcozecchini 0:9fca2b23d0ba 281 if( hlen <= 32 )
marcozecchini 0:9fca2b23d0ba 282 v = 64;
marcozecchini 0:9fca2b23d0ba 283 else
marcozecchini 0:9fca2b23d0ba 284 v = 128;
marcozecchini 0:9fca2b23d0ba 285
marcozecchini 0:9fca2b23d0ba 286 memset( diversifier, (unsigned char) id, v );
marcozecchini 0:9fca2b23d0ba 287
marcozecchini 0:9fca2b23d0ba 288 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
marcozecchini 0:9fca2b23d0ba 289 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
marcozecchini 0:9fca2b23d0ba 290
marcozecchini 0:9fca2b23d0ba 291 p = data;
marcozecchini 0:9fca2b23d0ba 292 while( datalen > 0 )
marcozecchini 0:9fca2b23d0ba 293 {
marcozecchini 0:9fca2b23d0ba 294 // Calculate hash( diversifier || salt_block || pwd_block )
marcozecchini 0:9fca2b23d0ba 295 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 296 goto exit;
marcozecchini 0:9fca2b23d0ba 297
marcozecchini 0:9fca2b23d0ba 298 if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 299 goto exit;
marcozecchini 0:9fca2b23d0ba 300
marcozecchini 0:9fca2b23d0ba 301 if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 302 goto exit;
marcozecchini 0:9fca2b23d0ba 303
marcozecchini 0:9fca2b23d0ba 304 if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 305 goto exit;
marcozecchini 0:9fca2b23d0ba 306
marcozecchini 0:9fca2b23d0ba 307 if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 308 goto exit;
marcozecchini 0:9fca2b23d0ba 309
marcozecchini 0:9fca2b23d0ba 310 // Perform remaining ( iterations - 1 ) recursive hash calculations
marcozecchini 0:9fca2b23d0ba 311 for( i = 1; i < (size_t) iterations; i++ )
marcozecchini 0:9fca2b23d0ba 312 {
marcozecchini 0:9fca2b23d0ba 313 if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 314 goto exit;
marcozecchini 0:9fca2b23d0ba 315 }
marcozecchini 0:9fca2b23d0ba 316
marcozecchini 0:9fca2b23d0ba 317 use_len = ( datalen > hlen ) ? hlen : datalen;
marcozecchini 0:9fca2b23d0ba 318 memcpy( p, hash_output, use_len );
marcozecchini 0:9fca2b23d0ba 319 datalen -= use_len;
marcozecchini 0:9fca2b23d0ba 320 p += use_len;
marcozecchini 0:9fca2b23d0ba 321
marcozecchini 0:9fca2b23d0ba 322 if( datalen == 0 )
marcozecchini 0:9fca2b23d0ba 323 break;
marcozecchini 0:9fca2b23d0ba 324
marcozecchini 0:9fca2b23d0ba 325 // Concatenating copies of hash_output into hash_block (B)
marcozecchini 0:9fca2b23d0ba 326 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
marcozecchini 0:9fca2b23d0ba 327
marcozecchini 0:9fca2b23d0ba 328 // B += 1
marcozecchini 0:9fca2b23d0ba 329 for( i = v; i > 0; i-- )
marcozecchini 0:9fca2b23d0ba 330 if( ++hash_block[i - 1] != 0 )
marcozecchini 0:9fca2b23d0ba 331 break;
marcozecchini 0:9fca2b23d0ba 332
marcozecchini 0:9fca2b23d0ba 333 // salt_block += B
marcozecchini 0:9fca2b23d0ba 334 c = 0;
marcozecchini 0:9fca2b23d0ba 335 for( i = v; i > 0; i-- )
marcozecchini 0:9fca2b23d0ba 336 {
marcozecchini 0:9fca2b23d0ba 337 j = salt_block[i - 1] + hash_block[i - 1] + c;
marcozecchini 0:9fca2b23d0ba 338 c = (unsigned char) (j >> 8);
marcozecchini 0:9fca2b23d0ba 339 salt_block[i - 1] = j & 0xFF;
marcozecchini 0:9fca2b23d0ba 340 }
marcozecchini 0:9fca2b23d0ba 341
marcozecchini 0:9fca2b23d0ba 342 // pwd_block += B
marcozecchini 0:9fca2b23d0ba 343 c = 0;
marcozecchini 0:9fca2b23d0ba 344 for( i = v; i > 0; i-- )
marcozecchini 0:9fca2b23d0ba 345 {
marcozecchini 0:9fca2b23d0ba 346 j = pwd_block[i - 1] + hash_block[i - 1] + c;
marcozecchini 0:9fca2b23d0ba 347 c = (unsigned char) (j >> 8);
marcozecchini 0:9fca2b23d0ba 348 pwd_block[i - 1] = j & 0xFF;
marcozecchini 0:9fca2b23d0ba 349 }
marcozecchini 0:9fca2b23d0ba 350 }
marcozecchini 0:9fca2b23d0ba 351
marcozecchini 0:9fca2b23d0ba 352 ret = 0;
marcozecchini 0:9fca2b23d0ba 353
marcozecchini 0:9fca2b23d0ba 354 exit:
marcozecchini 0:9fca2b23d0ba 355 mbedtls_zeroize( salt_block, sizeof( salt_block ) );
marcozecchini 0:9fca2b23d0ba 356 mbedtls_zeroize( pwd_block, sizeof( pwd_block ) );
marcozecchini 0:9fca2b23d0ba 357 mbedtls_zeroize( hash_block, sizeof( hash_block ) );
marcozecchini 0:9fca2b23d0ba 358 mbedtls_zeroize( hash_output, sizeof( hash_output ) );
marcozecchini 0:9fca2b23d0ba 359
marcozecchini 0:9fca2b23d0ba 360 mbedtls_md_free( &md_ctx );
marcozecchini 0:9fca2b23d0ba 361
marcozecchini 0:9fca2b23d0ba 362 return( ret );
marcozecchini 0:9fca2b23d0ba 363 }
marcozecchini 0:9fca2b23d0ba 364
marcozecchini 0:9fca2b23d0ba 365 #endif /* MBEDTLS_PKCS12_C */