Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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