mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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