BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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