Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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