blob: 16a15cb63e928f2683374bd8f51e3dc6af8ede30 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PKCS12_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/pkcs12.h"
37#include "mbedtls/asn1.h"
38#include "mbedtls/cipher.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/arc4.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020045#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/des.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020049#endif
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
52 mbedtls_asn1_buf *salt, int *iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020053{
54 int ret;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020055 unsigned char **p = &params->p;
56 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020057
58 /*
59 * pkcs-12PbeParams ::= SEQUENCE {
60 * salt OCTET STRING,
61 * iterations INTEGER
62 * }
63 *
64 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
66 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
67 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
70 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020071
72 salt->p = *p;
73 *p += salt->len;
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
76 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020077
78 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
80 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020081
82 return( 0 );
83}
84
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020085#define PKCS12_MAX_PWDLEN 128
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020088 const unsigned char *pwd, size_t pwdlen,
89 unsigned char *key, size_t keylen,
90 unsigned char *iv, size_t ivlen )
91{
Nicholas Wilson409401c2016-04-13 11:48:25 +010092 int ret, iterations = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_asn1_buf salt;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020094 size_t i;
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020095 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
96
97 if( pwdlen > PKCS12_MAX_PWDLEN )
98 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200101 memset( &unipwd, 0, sizeof(unipwd) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200102
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200103 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
104 &iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200105 return( ret );
106
Paul Bakker66d5d072014-06-17 16:39:18 +0200107 for( i = 0; i < pwdlen; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200108 unipwd[i * 2 + 1] = pwd[i];
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200111 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200113 {
114 return( ret );
115 }
116
117 if( iv == NULL || ivlen == 0 )
118 return( 0 );
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200121 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200123 {
124 return( ret );
125 }
126 return( 0 );
127}
128
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200129#undef PKCS12_MAX_PWDLEN
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200132 const unsigned char *pwd, size_t pwdlen,
133 const unsigned char *data, size_t len,
134 unsigned char *output )
135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if !defined(MBEDTLS_ARC4_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200137 ((void) pbe_params);
138 ((void) mode);
139 ((void) pwd);
140 ((void) pwdlen);
141 ((void) data);
142 ((void) len);
143 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200145#else
146 int ret;
147 unsigned char key[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_arc4_context ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200149 ((void) mode);
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_arc4_init( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
Paul Bakker38b50d72013-06-24 19:33:27 +0200154 pwd, pwdlen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200155 key, 16, NULL, 0 ) ) != 0 )
156 {
157 return( ret );
158 }
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_arc4_setup( &ctx, key, 16 );
161 if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200162 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200163
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200164exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500165 mbedtls_platform_zeroize( key, sizeof( key ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_arc4_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200167
168 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_ARC4_C */
Paul Bakker531e2942013-06-24 19:23:12 +0200170}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
173 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
Paul Bakker38b50d72013-06-24 19:33:27 +0200174 const unsigned char *pwd, size_t pwdlen,
175 const unsigned char *data, size_t len,
176 unsigned char *output )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200177{
Paul Bakker38b50d72013-06-24 19:33:27 +0200178 int ret, keylen = 0;
179 unsigned char key[32];
180 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 const mbedtls_cipher_info_t *cipher_info;
182 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker38b50d72013-06-24 19:33:27 +0200183 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
Paul Bakker38b50d72013-06-24 19:33:27 +0200186 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakker38b50d72013-06-24 19:33:27 +0200188
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200189 keylen = cipher_info->key_bitlen / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200190
191 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
192 key, keylen,
193 iv, cipher_info->iv_size ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200194 {
195 return( ret );
196 }
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200199
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200200 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200201 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200204 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200207 goto exit;
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200210 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
Paul Bakker38b50d72013-06-24 19:33:27 +0200213 output, &olen ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200214 {
Paul Bakkerbd552442013-07-03 14:44:40 +0200215 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200216 }
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
219 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200220
Paul Bakkerbd552442013-07-03 14:44:40 +0200221exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500222 mbedtls_platform_zeroize( key, sizeof( key ) );
223 mbedtls_platform_zeroize( iv, sizeof( iv ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_cipher_free( &cipher_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200225
226 return( ret );
Paul Bakker531e2942013-06-24 19:23:12 +0200227}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200228
229static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
230 const unsigned char *filler, size_t fill_len )
231{
232 unsigned char *p = data;
233 size_t use_len;
234
235 while( data_len > 0 )
236 {
237 use_len = ( data_len > fill_len ) ? fill_len : data_len;
238 memcpy( p, filler, use_len );
239 p += use_len;
240 data_len -= use_len;
241 }
242}
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200245 const unsigned char *pwd, size_t pwdlen,
246 const unsigned char *salt, size_t saltlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_md_type_t md_type, int id, int iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200248{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200249 int ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200250 unsigned int j;
251
252 unsigned char diversifier[128];
253 unsigned char salt_block[128], pwd_block[128], hash_block[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200255 unsigned char *p;
256 unsigned char c;
257
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200258 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 const mbedtls_md_info_t *md_info;
261 mbedtls_md_context_t md_ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200262
263 // This version only allows max of 64 bytes of password or salt
264 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200268 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_md_init( &md_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200274 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200276
277 if( hlen <= 32 )
278 v = 64;
279 else
280 v = 128;
281
282 memset( diversifier, (unsigned char) id, v );
283
284 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
285 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
286
287 p = data;
288 while( datalen > 0 )
289 {
290 // Calculate hash( diversifier || salt_block || pwd_block )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200292 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200295 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200298 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200301 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200304 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200305
306 // Perform remaining ( iterations - 1 ) recursive hash calculations
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200307 for( i = 1; i < (size_t) iterations; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200310 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200311 }
312
313 use_len = ( datalen > hlen ) ? hlen : datalen;
314 memcpy( p, hash_output, use_len );
315 datalen -= use_len;
316 p += use_len;
317
318 if( datalen == 0 )
319 break;
320
321 // Concatenating copies of hash_output into hash_block (B)
322 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
323
324 // B += 1
325 for( i = v; i > 0; i-- )
326 if( ++hash_block[i - 1] != 0 )
327 break;
328
329 // salt_block += B
330 c = 0;
331 for( i = v; i > 0; i-- )
332 {
333 j = salt_block[i - 1] + hash_block[i - 1] + c;
334 c = (unsigned char) (j >> 8);
335 salt_block[i - 1] = j & 0xFF;
336 }
337
338 // pwd_block += B
339 c = 0;
340 for( i = v; i > 0; i-- )
341 {
342 j = pwd_block[i - 1] + hash_block[i - 1] + c;
343 c = (unsigned char) (j >> 8);
344 pwd_block[i - 1] = j & 0xFF;
345 }
346 }
347
Paul Bakkerbd552442013-07-03 14:44:40 +0200348 ret = 0;
349
350exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500351 mbedtls_platform_zeroize( salt_block, sizeof( salt_block ) );
352 mbedtls_platform_zeroize( pwd_block, sizeof( pwd_block ) );
353 mbedtls_platform_zeroize( hash_block, sizeof( hash_block ) );
354 mbedtls_platform_zeroize( hash_output, sizeof( hash_output ) );
Paul Bakker91c301a2014-06-18 13:59:38 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_md_free( &md_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200357
358 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200359}
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#endif /* MBEDTLS_PKCS12_C */