blob: b26f5669fc7582231b9b4d5b4aa99fc858bb72ad [file] [log] [blame]
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02001/*
2 * PKCS#12 Personal Information Exchange Syntax
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020020 */
21/*
22 * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
23 *
24 * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
25 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
26 */
27
Gilles Peskinedb09ef62020-06-03 01:43:33 +020028#include "common.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PKCS12_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/pkcs12.h"
33#include "mbedtls/asn1.h"
34#include "mbedtls/cipher.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050035#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000036#include "mbedtls/error.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/arc4.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020042#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/des.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020046#endif
47
Hanno Becker8a89f9f2018-10-12 10:46:32 +010048#if defined(MBEDTLS_ASN1_PARSE_C)
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
51 mbedtls_asn1_buf *salt, int *iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020052{
Janos Follath24eed8d2019-11-22 13:21:35 +000053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020054 unsigned char **p = &params->p;
55 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020056
57 /*
58 * pkcs-12PbeParams ::= SEQUENCE {
59 * salt OCTET STRING,
60 * iterations INTEGER
61 * }
62 *
63 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
65 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
66 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
69 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020070
71 salt->p = *p;
72 *p += salt->len;
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
75 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020076
77 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
79 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020080
81 return( 0 );
82}
83
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020084#define PKCS12_MAX_PWDLEN 128
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020087 const unsigned char *pwd, size_t pwdlen,
88 unsigned char *key, size_t keylen,
89 unsigned char *iv, size_t ivlen )
90{
Nicholas Wilson409401c2016-04-13 11:48:25 +010091 int ret, iterations = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_asn1_buf salt;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020093 size_t i;
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020094 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
95
96 if( pwdlen > PKCS12_MAX_PWDLEN )
97 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200100 memset( &unipwd, 0, sizeof(unipwd) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200101
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200102 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
103 &iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200104 return( ret );
105
Paul Bakker66d5d072014-06-17 16:39:18 +0200106 for( i = 0; i < pwdlen; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200107 unipwd[i * 2 + 1] = pwd[i];
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200110 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200112 {
113 return( ret );
114 }
115
116 if( iv == NULL || ivlen == 0 )
117 return( 0 );
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200120 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200122 {
123 return( ret );
124 }
125 return( 0 );
126}
127
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200128#undef PKCS12_MAX_PWDLEN
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200131 const unsigned char *pwd, size_t pwdlen,
132 const unsigned char *data, size_t len,
133 unsigned char *output )
134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if !defined(MBEDTLS_ARC4_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200136 ((void) pbe_params);
137 ((void) mode);
138 ((void) pwd);
139 ((void) pwdlen);
140 ((void) data);
141 ((void) len);
142 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200144#else
Janos Follath24eed8d2019-11-22 13:21:35 +0000145 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200146 unsigned char key[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_arc4_context ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200148 ((void) mode);
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_arc4_init( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
Paul Bakker38b50d72013-06-24 19:33:27 +0200153 pwd, pwdlen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200154 key, 16, NULL, 0 ) ) != 0 )
155 {
156 return( ret );
157 }
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_arc4_setup( &ctx, key, 16 );
160 if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200161 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200162
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200163exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500164 mbedtls_platform_zeroize( key, sizeof( key ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_arc4_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200166
167 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_ARC4_C */
Paul Bakker531e2942013-06-24 19:23:12 +0200169}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
172 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
Paul Bakker38b50d72013-06-24 19:33:27 +0200173 const unsigned char *pwd, size_t pwdlen,
174 const unsigned char *data, size_t len,
175 unsigned char *output )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200176{
Paul Bakker38b50d72013-06-24 19:33:27 +0200177 int ret, keylen = 0;
178 unsigned char key[32];
179 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 const mbedtls_cipher_info_t *cipher_info;
181 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker38b50d72013-06-24 19:33:27 +0200182 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
Paul Bakker38b50d72013-06-24 19:33:27 +0200185 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakker38b50d72013-06-24 19:33:27 +0200187
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200188 keylen = cipher_info->key_bitlen / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200189
190 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
191 key, keylen,
192 iv, cipher_info->iv_size ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200193 {
194 return( ret );
195 }
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200198
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200199 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200200 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200203 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200206 goto exit;
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200209 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
Paul Bakker38b50d72013-06-24 19:33:27 +0200212 output, &olen ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200213 {
Paul Bakkerbd552442013-07-03 14:44:40 +0200214 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200215 }
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
218 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200219
Paul Bakkerbd552442013-07-03 14:44:40 +0200220exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500221 mbedtls_platform_zeroize( key, sizeof( key ) );
222 mbedtls_platform_zeroize( iv, sizeof( iv ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_cipher_free( &cipher_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200224
225 return( ret );
Paul Bakker531e2942013-06-24 19:23:12 +0200226}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200227
Hanno Becker8a89f9f2018-10-12 10:46:32 +0100228#endif /* MBEDTLS_ASN1_PARSE_C */
229
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200230static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
231 const unsigned char *filler, size_t fill_len )
232{
233 unsigned char *p = data;
234 size_t use_len;
235
236 while( data_len > 0 )
237 {
238 use_len = ( data_len > fill_len ) ? fill_len : data_len;
239 memcpy( p, filler, use_len );
240 p += use_len;
241 data_len -= use_len;
242 }
243}
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200246 const unsigned char *pwd, size_t pwdlen,
247 const unsigned char *salt, size_t saltlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_md_type_t md_type, int id, int iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200249{
Janos Follath24eed8d2019-11-22 13:21:35 +0000250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200251 unsigned int j;
252
253 unsigned char diversifier[128];
254 unsigned char salt_block[128], pwd_block[128], hash_block[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200256 unsigned char *p;
257 unsigned char c;
258
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200259 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 const mbedtls_md_info_t *md_info;
262 mbedtls_md_context_t md_ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200263
264 // This version only allows max of 64 bytes of password or salt
265 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200269 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_md_init( &md_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200275 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200277
278 if( hlen <= 32 )
279 v = 64;
280 else
281 v = 128;
282
283 memset( diversifier, (unsigned char) id, v );
284
285 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
286 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
287
288 p = data;
289 while( datalen > 0 )
290 {
291 // Calculate hash( diversifier || salt_block || pwd_block )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200293 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200296 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200299 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200302 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200305 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200306
307 // Perform remaining ( iterations - 1 ) recursive hash calculations
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200308 for( i = 1; i < (size_t) iterations; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200311 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200312 }
313
314 use_len = ( datalen > hlen ) ? hlen : datalen;
315 memcpy( p, hash_output, use_len );
316 datalen -= use_len;
317 p += use_len;
318
319 if( datalen == 0 )
320 break;
321
322 // Concatenating copies of hash_output into hash_block (B)
323 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
324
325 // B += 1
326 for( i = v; i > 0; i-- )
327 if( ++hash_block[i - 1] != 0 )
328 break;
329
330 // salt_block += B
331 c = 0;
332 for( i = v; i > 0; i-- )
333 {
334 j = salt_block[i - 1] + hash_block[i - 1] + c;
335 c = (unsigned char) (j >> 8);
336 salt_block[i - 1] = j & 0xFF;
337 }
338
339 // pwd_block += B
340 c = 0;
341 for( i = v; i > 0; i-- )
342 {
343 j = pwd_block[i - 1] + hash_block[i - 1] + c;
344 c = (unsigned char) (j >> 8);
345 pwd_block[i - 1] = j & 0xFF;
346 }
347 }
348
Paul Bakkerbd552442013-07-03 14:44:40 +0200349 ret = 0;
350
351exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500352 mbedtls_platform_zeroize( salt_block, sizeof( salt_block ) );
353 mbedtls_platform_zeroize( pwd_block, sizeof( pwd_block ) );
354 mbedtls_platform_zeroize( hash_block, sizeof( hash_block ) );
355 mbedtls_platform_zeroize( hash_output, sizeof( hash_output ) );
Paul Bakker91c301a2014-06-18 13:59:38 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_md_free( &md_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200358
359 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200360}
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#endif /* MBEDTLS_PKCS12_C */