blob: 3d23d5e354923cd01d69a479fcf572d80af540a6 [file] [log] [blame]
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02001/*
2 * PKCS#12 Personal Information Exchange Syntax
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-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útif744bd72020-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"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050064#include "mbedtls/platform_util.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020065
Rich Evans00ab4702015-02-06 13:43:58 +000066#include <string.h>
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/arc4.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020070#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/des.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020074#endif
75
Hanno Becker8a89f9f2018-10-12 10:46:32 +010076#if defined(MBEDTLS_ASN1_PARSE_C)
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
79 mbedtls_asn1_buf *salt, int *iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020080{
81 int ret;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020082 unsigned char **p = &params->p;
83 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020084
85 /*
86 * pkcs-12PbeParams ::= SEQUENCE {
87 * salt OCTET STRING,
88 * iterations INTEGER
89 * }
90 *
91 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
93 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
94 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
97 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020098
99 salt->p = *p;
100 *p += salt->len;
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
103 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200104
105 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
107 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200108
109 return( 0 );
110}
111
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200112#define PKCS12_MAX_PWDLEN 128
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200115 const unsigned char *pwd, size_t pwdlen,
116 unsigned char *key, size_t keylen,
117 unsigned char *iv, size_t ivlen )
118{
Nicholas Wilson409401c2016-04-13 11:48:25 +0100119 int ret, iterations = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_asn1_buf salt;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200121 size_t i;
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200122 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
123
124 if( pwdlen > PKCS12_MAX_PWDLEN )
125 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200128 memset( &unipwd, 0, sizeof(unipwd) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200129
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200130 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
131 &iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200132 return( ret );
133
Paul Bakker66d5d072014-06-17 16:39:18 +0200134 for( i = 0; i < pwdlen; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200135 unipwd[i * 2 + 1] = pwd[i];
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200138 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200140 {
141 return( ret );
142 }
143
144 if( iv == NULL || ivlen == 0 )
145 return( 0 );
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200148 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200150 {
151 return( ret );
152 }
153 return( 0 );
154}
155
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200156#undef PKCS12_MAX_PWDLEN
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200159 const unsigned char *pwd, size_t pwdlen,
160 const unsigned char *data, size_t len,
161 unsigned char *output )
162{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if !defined(MBEDTLS_ARC4_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200164 ((void) pbe_params);
165 ((void) mode);
166 ((void) pwd);
167 ((void) pwdlen);
168 ((void) data);
169 ((void) len);
170 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200172#else
173 int ret;
174 unsigned char key[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_arc4_context ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200176 ((void) mode);
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_arc4_init( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
Paul Bakker38b50d72013-06-24 19:33:27 +0200181 pwd, pwdlen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200182 key, 16, NULL, 0 ) ) != 0 )
183 {
184 return( ret );
185 }
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_arc4_setup( &ctx, key, 16 );
188 if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200189 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200190
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200191exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500192 mbedtls_platform_zeroize( key, sizeof( key ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_arc4_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200194
195 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#endif /* MBEDTLS_ARC4_C */
Paul Bakker531e2942013-06-24 19:23:12 +0200197}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
200 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
Paul Bakker38b50d72013-06-24 19:33:27 +0200201 const unsigned char *pwd, size_t pwdlen,
202 const unsigned char *data, size_t len,
203 unsigned char *output )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200204{
Paul Bakker38b50d72013-06-24 19:33:27 +0200205 int ret, keylen = 0;
206 unsigned char key[32];
207 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 const mbedtls_cipher_info_t *cipher_info;
209 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker38b50d72013-06-24 19:33:27 +0200210 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
Paul Bakker38b50d72013-06-24 19:33:27 +0200213 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakker38b50d72013-06-24 19:33:27 +0200215
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200216 keylen = cipher_info->key_bitlen / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200217
218 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
219 key, keylen,
220 iv, cipher_info->iv_size ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200221 {
222 return( ret );
223 }
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200226
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200227 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200228 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200231 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200234 goto exit;
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200237 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
Paul Bakker38b50d72013-06-24 19:33:27 +0200240 output, &olen ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200241 {
Paul Bakkerbd552442013-07-03 14:44:40 +0200242 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200243 }
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
246 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200247
Paul Bakkerbd552442013-07-03 14:44:40 +0200248exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500249 mbedtls_platform_zeroize( key, sizeof( key ) );
250 mbedtls_platform_zeroize( iv, sizeof( iv ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_cipher_free( &cipher_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200252
253 return( ret );
Paul Bakker531e2942013-06-24 19:23:12 +0200254}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200255
Hanno Becker8a89f9f2018-10-12 10:46:32 +0100256#endif /* MBEDTLS_ASN1_PARSE_C */
257
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200258static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
259 const unsigned char *filler, size_t fill_len )
260{
261 unsigned char *p = data;
262 size_t use_len;
263
264 while( data_len > 0 )
265 {
266 use_len = ( data_len > fill_len ) ? fill_len : data_len;
267 memcpy( p, filler, use_len );
268 p += use_len;
269 data_len -= use_len;
270 }
271}
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200274 const unsigned char *pwd, size_t pwdlen,
275 const unsigned char *salt, size_t saltlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_md_type_t md_type, int id, int iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200277{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200278 int ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200279 unsigned int j;
280
281 unsigned char diversifier[128];
282 unsigned char salt_block[128], pwd_block[128], hash_block[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200284 unsigned char *p;
285 unsigned char c;
286
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200287 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 const mbedtls_md_info_t *md_info;
290 mbedtls_md_context_t md_ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200291
292 // This version only allows max of 64 bytes of password or salt
293 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200297 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_md_init( &md_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200303 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200305
306 if( hlen <= 32 )
307 v = 64;
308 else
309 v = 128;
310
311 memset( diversifier, (unsigned char) id, v );
312
313 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
314 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
315
316 p = data;
317 while( datalen > 0 )
318 {
319 // Calculate hash( diversifier || salt_block || pwd_block )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200321 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200324 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200327 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200330 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200333 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200334
335 // Perform remaining ( iterations - 1 ) recursive hash calculations
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200336 for( i = 1; i < (size_t) iterations; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200339 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200340 }
341
342 use_len = ( datalen > hlen ) ? hlen : datalen;
343 memcpy( p, hash_output, use_len );
344 datalen -= use_len;
345 p += use_len;
346
347 if( datalen == 0 )
348 break;
349
350 // Concatenating copies of hash_output into hash_block (B)
351 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
352
353 // B += 1
354 for( i = v; i > 0; i-- )
355 if( ++hash_block[i - 1] != 0 )
356 break;
357
358 // salt_block += B
359 c = 0;
360 for( i = v; i > 0; i-- )
361 {
362 j = salt_block[i - 1] + hash_block[i - 1] + c;
363 c = (unsigned char) (j >> 8);
364 salt_block[i - 1] = j & 0xFF;
365 }
366
367 // pwd_block += B
368 c = 0;
369 for( i = v; i > 0; i-- )
370 {
371 j = pwd_block[i - 1] + hash_block[i - 1] + c;
372 c = (unsigned char) (j >> 8);
373 pwd_block[i - 1] = j & 0xFF;
374 }
375 }
376
Paul Bakkerbd552442013-07-03 14:44:40 +0200377 ret = 0;
378
379exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500380 mbedtls_platform_zeroize( salt_block, sizeof( salt_block ) );
381 mbedtls_platform_zeroize( pwd_block, sizeof( pwd_block ) );
382 mbedtls_platform_zeroize( hash_block, sizeof( hash_block ) );
383 mbedtls_platform_zeroize( hash_output, sizeof( hash_output ) );
Paul Bakker91c301a2014-06-18 13:59:38 +0200384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_md_free( &md_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200386
387 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200388}
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#endif /* MBEDTLS_PKCS12_C */