blob: 9ee63c2d323a2888dff3fc59f16ba9feb01ae87a [file] [log] [blame]
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02001/*
2 * PKCS#12 Personal Information Exchange Syntax
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020045 */
46/*
47 * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
48 *
49 * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
50 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
51 */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PKCS12_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020060
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/pkcs12.h"
62#include "mbedtls/asn1.h"
63#include "mbedtls/cipher.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020064
Rich Evans00ab4702015-02-06 13:43:58 +000065#include <string.h>
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/arc4.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020069#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000072#include "mbedtls/des.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020073#endif
74
Paul Bakker91c301a2014-06-18 13:59:38 +020075/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker91c301a2014-06-18 13:59:38 +020077 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
78}
79
Hanno Beckerd30cd342018-10-12 10:46:32 +010080#if defined(MBEDTLS_ASN1_PARSE_C)
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
83 mbedtls_asn1_buf *salt, int *iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020084{
85 int ret;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020086 unsigned char **p = &params->p;
87 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020088
89 /*
90 * pkcs-12PbeParams ::= SEQUENCE {
91 * salt OCTET STRING,
92 * iterations INTEGER
93 * }
94 *
95 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
97 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
98 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
101 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200102
103 salt->p = *p;
104 *p += salt->len;
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
107 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200108
109 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
111 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200112
113 return( 0 );
114}
115
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200116#define PKCS12_MAX_PWDLEN 128
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200119 const unsigned char *pwd, size_t pwdlen,
120 unsigned char *key, size_t keylen,
121 unsigned char *iv, size_t ivlen )
122{
Nicholas Wilson409401c2016-04-13 11:48:25 +0100123 int ret, iterations = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_asn1_buf salt;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200125 size_t i;
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200126 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
127
128 if( pwdlen > PKCS12_MAX_PWDLEN )
129 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200132 memset( &unipwd, 0, sizeof(unipwd) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200133
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200134 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
135 &iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200136 return( ret );
137
Paul Bakker66d5d072014-06-17 16:39:18 +0200138 for( i = 0; i < pwdlen; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200139 unipwd[i * 2 + 1] = pwd[i];
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200142 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200144 {
145 return( ret );
146 }
147
148 if( iv == NULL || ivlen == 0 )
149 return( 0 );
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200152 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200154 {
155 return( ret );
156 }
157 return( 0 );
158}
159
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200160#undef PKCS12_MAX_PWDLEN
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200163 const unsigned char *pwd, size_t pwdlen,
164 const unsigned char *data, size_t len,
165 unsigned char *output )
166{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if !defined(MBEDTLS_ARC4_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200168 ((void) pbe_params);
169 ((void) mode);
170 ((void) pwd);
171 ((void) pwdlen);
172 ((void) data);
173 ((void) len);
174 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200176#else
177 int ret;
178 unsigned char key[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_arc4_context ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200180 ((void) mode);
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_arc4_init( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
Paul Bakker38b50d72013-06-24 19:33:27 +0200185 pwd, pwdlen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200186 key, 16, NULL, 0 ) ) != 0 )
187 {
188 return( ret );
189 }
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_arc4_setup( &ctx, key, 16 );
192 if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200193 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200194
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200195exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_zeroize( key, sizeof( key ) );
197 mbedtls_arc4_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200198
199 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#endif /* MBEDTLS_ARC4_C */
Paul Bakker531e2942013-06-24 19:23:12 +0200201}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
204 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
Paul Bakker38b50d72013-06-24 19:33:27 +0200205 const unsigned char *pwd, size_t pwdlen,
206 const unsigned char *data, size_t len,
207 unsigned char *output )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200208{
Paul Bakker38b50d72013-06-24 19:33:27 +0200209 int ret, keylen = 0;
210 unsigned char key[32];
211 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 const mbedtls_cipher_info_t *cipher_info;
213 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker38b50d72013-06-24 19:33:27 +0200214 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
Paul Bakker38b50d72013-06-24 19:33:27 +0200217 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakker38b50d72013-06-24 19:33:27 +0200219
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200220 keylen = cipher_info->key_bitlen / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200221
222 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
223 key, keylen,
224 iv, cipher_info->iv_size ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200225 {
226 return( ret );
227 }
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200230
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200231 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200232 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200235 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200238 goto exit;
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200241 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
Paul Bakker38b50d72013-06-24 19:33:27 +0200244 output, &olen ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200245 {
Paul Bakkerbd552442013-07-03 14:44:40 +0200246 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200247 }
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
250 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200251
Paul Bakkerbd552442013-07-03 14:44:40 +0200252exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_zeroize( key, sizeof( key ) );
254 mbedtls_zeroize( iv, sizeof( iv ) );
255 mbedtls_cipher_free( &cipher_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200256
257 return( ret );
Paul Bakker531e2942013-06-24 19:23:12 +0200258}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200259
Hanno Beckerd30cd342018-10-12 10:46:32 +0100260#endif /* MBEDTLS_ASN1_PARSE_C */
261
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200262static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
263 const unsigned char *filler, size_t fill_len )
264{
265 unsigned char *p = data;
266 size_t use_len;
267
268 while( data_len > 0 )
269 {
270 use_len = ( data_len > fill_len ) ? fill_len : data_len;
271 memcpy( p, filler, use_len );
272 p += use_len;
273 data_len -= use_len;
274 }
275}
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200278 const unsigned char *pwd, size_t pwdlen,
279 const unsigned char *salt, size_t saltlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_md_type_t md_type, int id, int iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200281{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200282 int ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200283 unsigned int j;
284
285 unsigned char diversifier[128];
286 unsigned char salt_block[128], pwd_block[128], hash_block[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200288 unsigned char *p;
289 unsigned char c;
290
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200291 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 const mbedtls_md_info_t *md_info;
294 mbedtls_md_context_t md_ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200295
296 // This version only allows max of 64 bytes of password or salt
297 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200301 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_md_init( &md_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200307 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200309
310 if( hlen <= 32 )
311 v = 64;
312 else
313 v = 128;
314
315 memset( diversifier, (unsigned char) id, v );
316
317 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
318 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
319
320 p = data;
321 while( datalen > 0 )
322 {
323 // Calculate hash( diversifier || salt_block || pwd_block )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200325 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200328 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200331 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200334 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200337 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200338
339 // Perform remaining ( iterations - 1 ) recursive hash calculations
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200340 for( i = 1; i < (size_t) iterations; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200343 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200344 }
345
346 use_len = ( datalen > hlen ) ? hlen : datalen;
347 memcpy( p, hash_output, use_len );
348 datalen -= use_len;
349 p += use_len;
350
351 if( datalen == 0 )
352 break;
353
354 // Concatenating copies of hash_output into hash_block (B)
355 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
356
357 // B += 1
358 for( i = v; i > 0; i-- )
359 if( ++hash_block[i - 1] != 0 )
360 break;
361
362 // salt_block += B
363 c = 0;
364 for( i = v; i > 0; i-- )
365 {
366 j = salt_block[i - 1] + hash_block[i - 1] + c;
367 c = (unsigned char) (j >> 8);
368 salt_block[i - 1] = j & 0xFF;
369 }
370
371 // pwd_block += B
372 c = 0;
373 for( i = v; i > 0; i-- )
374 {
375 j = pwd_block[i - 1] + hash_block[i - 1] + c;
376 c = (unsigned char) (j >> 8);
377 pwd_block[i - 1] = j & 0xFF;
378 }
379 }
380
Paul Bakkerbd552442013-07-03 14:44:40 +0200381 ret = 0;
382
383exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_zeroize( salt_block, sizeof( salt_block ) );
385 mbedtls_zeroize( pwd_block, sizeof( pwd_block ) );
386 mbedtls_zeroize( hash_block, sizeof( hash_block ) );
387 mbedtls_zeroize( hash_output, sizeof( hash_output ) );
Paul Bakker91c301a2014-06-18 13:59:38 +0200388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_md_free( &md_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200390
391 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200392}
393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394#endif /* MBEDTLS_PKCS12_C */