mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

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