The WDCInterface is is a drop-in replacement for an EthernetInterface class that allows the user to connect to the Internet with a Wistron NeWeb Corporation (WNC) M14A2A Series data module using the standard network Socket API's. This interface class is used in the AT&T Cellular IoT Starter Kit which is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).

Dependencies:   WncControllerK64F

Dependents:   WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more

See the WNCInterface README in the Wiki tab for detailed information on this library.

Committer:
JMF
Date:
Tue Nov 01 14:22:56 2016 +0000
Revision:
12:0071cb144c7a
Adding mbedtls files

Who changed what in which revision?

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