blob: a90d1f90ce5c6c44e6a6182257f524753b207928 [file] [log] [blame]
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02001/*
2 * PKCS#12 Personal Information Exchange Syntax
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19/*
20 * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
21 *
22 * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
23 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
24 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PKCS12_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pkcs12.h"
31#include "mbedtls/asn1.h"
32#include "mbedtls/cipher.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000034#include "mbedtls/error.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020035
Rich Evans00ab4702015-02-06 13:43:58 +000036#include <string.h>
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/des.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020040#endif
41
Hanno Becker8a89f9f2018-10-12 10:46:32 +010042#if defined(MBEDTLS_ASN1_PARSE_C)
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
45 mbedtls_asn1_buf *salt, int *iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020046{
Janos Follath24eed8d2019-11-22 13:21:35 +000047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020048 unsigned char **p = &params->p;
49 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020050
51 /*
52 * pkcs-12PbeParams ::= SEQUENCE {
53 * salt OCTET STRING,
54 * iterations INTEGER
55 * }
56 *
57 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +010059 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
60 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010063 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020064
65 salt->p = *p;
66 *p += salt->len;
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010069 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020070
71 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +010072 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
73 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020074
75 return( 0 );
76}
77
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020078#define PKCS12_MAX_PWDLEN 128
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020081 const unsigned char *pwd, size_t pwdlen,
82 unsigned char *key, size_t keylen,
83 unsigned char *iv, size_t ivlen )
84{
Nicholas Wilson409401c2016-04-13 11:48:25 +010085 int ret, iterations = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_asn1_buf salt;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020087 size_t i;
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020088 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
89
90 if( pwdlen > PKCS12_MAX_PWDLEN )
91 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker66d5d072014-06-17 16:39:18 +020094 memset( &unipwd, 0, sizeof(unipwd) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020095
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020096 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
97 &iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020098 return( ret );
99
Paul Bakker66d5d072014-06-17 16:39:18 +0200100 for( i = 0; i < pwdlen; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200101 unipwd[i * 2 + 1] = pwd[i];
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200104 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200106 {
107 return( ret );
108 }
109
110 if( iv == NULL || ivlen == 0 )
111 return( 0 );
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200114 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200116 {
117 return( ret );
118 }
119 return( 0 );
120}
121
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200122#undef PKCS12_MAX_PWDLEN
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
125 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
Paul Bakker38b50d72013-06-24 19:33:27 +0200126 const unsigned char *pwd, size_t pwdlen,
127 const unsigned char *data, size_t len,
128 unsigned char *output )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200129{
Paul Bakker38b50d72013-06-24 19:33:27 +0200130 int ret, keylen = 0;
131 unsigned char key[32];
132 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 const mbedtls_cipher_info_t *cipher_info;
134 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker38b50d72013-06-24 19:33:27 +0200135 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200136
Paul Elliott853c0da2021-11-11 19:00:38 +0000137 if( pwd == NULL && pwdlen != 0 )
138 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
Paul Bakker38b50d72013-06-24 19:33:27 +0200141 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakker38b50d72013-06-24 19:33:27 +0200143
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200144 keylen = cipher_info->key_bitlen / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200145
146 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
147 key, keylen,
148 iv, cipher_info->iv_size ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200149 {
150 return( ret );
151 }
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200154
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200155 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200156 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200159 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200162 goto exit;
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200165 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
Paul Bakker38b50d72013-06-24 19:33:27 +0200168 output, &olen ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200169 {
Paul Bakkerbd552442013-07-03 14:44:40 +0200170 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200171 }
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
174 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200175
Paul Bakkerbd552442013-07-03 14:44:40 +0200176exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500177 mbedtls_platform_zeroize( key, sizeof( key ) );
178 mbedtls_platform_zeroize( iv, sizeof( iv ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_cipher_free( &cipher_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200180
181 return( ret );
Paul Bakker531e2942013-06-24 19:23:12 +0200182}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200183
Hanno Becker8a89f9f2018-10-12 10:46:32 +0100184#endif /* MBEDTLS_ASN1_PARSE_C */
185
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200186static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
187 const unsigned char *filler, size_t fill_len )
188{
189 unsigned char *p = data;
190 size_t use_len;
191
Paul Elliott853c0da2021-11-11 19:00:38 +0000192 if( filler != NULL && fill_len != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200193 {
Paul Elliott853c0da2021-11-11 19:00:38 +0000194 while( data_len > 0 )
195 {
196 use_len = ( data_len > fill_len ) ? fill_len : data_len;
197 memcpy( p, filler, use_len );
198 p += use_len;
199 data_len -= use_len;
200 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200201 }
Paul Elliott7298bef2021-12-02 17:51:34 +0000202 else
203 {
204 /* If either of the above are not true then clearly there is nothing
205 * that this function can do. The function should *not* be called
206 * under either of those circumstances, as you could end up with an
207 * incorrect output but for safety's sake, leaving the check in as
208 * otherwise we could end up with memory corruption.*/
209 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200210}
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200213 const unsigned char *pwd, size_t pwdlen,
214 const unsigned char *salt, size_t saltlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_md_type_t md_type, int id, int iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200216{
Janos Follath24eed8d2019-11-22 13:21:35 +0000217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200218 unsigned int j;
219
220 unsigned char diversifier[128];
221 unsigned char salt_block[128], pwd_block[128], hash_block[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200223 unsigned char *p;
224 unsigned char c;
Paul Elliott4086bdb2021-11-18 14:02:21 +0000225 int use_password = 0;
226 int use_salt = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200227
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200228 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 const mbedtls_md_info_t *md_info;
231 mbedtls_md_context_t md_ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200232
233 // This version only allows max of 64 bytes of password or salt
234 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200236
Paul Elliott853c0da2021-11-11 19:00:38 +0000237 if( pwd == NULL && pwdlen != 0 )
238 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
239
Paul Elliott4086bdb2021-11-18 14:02:21 +0000240 if( salt == NULL && saltlen != 0 )
241 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
242
243 use_password = ( pwd && pwdlen != 0 );
244 use_salt = ( salt && saltlen != 0 );
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200247 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_md_init( &md_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200253 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200255
256 if( hlen <= 32 )
257 v = 64;
258 else
259 v = 128;
260
261 memset( diversifier, (unsigned char) id, v );
262
Paul Elliott4086bdb2021-11-18 14:02:21 +0000263 if( use_salt != 0 )
264 {
265 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
266 }
267
268 if( use_password != 0 )
269 {
270 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
271 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200272
273 p = data;
274 while( datalen > 0 )
275 {
276 // Calculate hash( diversifier || salt_block || pwd_block )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200278 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200281 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200282
Paul Elliott4086bdb2021-11-18 14:02:21 +0000283 if( use_salt != 0 )
284 {
285 if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v )) != 0 )
286 goto exit;
287 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200288
Paul Elliott4086bdb2021-11-18 14:02:21 +0000289 if( use_password != 0)
290 {
291 if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v )) != 0 )
292 goto exit;
293 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200296 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200297
298 // Perform remaining ( iterations - 1 ) recursive hash calculations
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200299 for( i = 1; i < (size_t) iterations; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200302 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200303 }
304
305 use_len = ( datalen > hlen ) ? hlen : datalen;
306 memcpy( p, hash_output, use_len );
307 datalen -= use_len;
308 p += use_len;
309
310 if( datalen == 0 )
311 break;
312
313 // Concatenating copies of hash_output into hash_block (B)
314 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
315
316 // B += 1
317 for( i = v; i > 0; i-- )
318 if( ++hash_block[i - 1] != 0 )
319 break;
320
Paul Elliott4086bdb2021-11-18 14:02:21 +0000321 if( use_salt != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200322 {
Paul Elliott4086bdb2021-11-18 14:02:21 +0000323 // salt_block += B
324 c = 0;
325 for( i = v; i > 0; i-- )
326 {
327 j = salt_block[i - 1] + hash_block[i - 1] + c;
328 c = MBEDTLS_BYTE_1( j );
329 salt_block[i - 1] = MBEDTLS_BYTE_0( j );
330 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200331 }
332
Paul Elliott4086bdb2021-11-18 14:02:21 +0000333 if( use_password != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200334 {
Paul Elliott4086bdb2021-11-18 14:02:21 +0000335 // pwd_block += B
336 c = 0;
337 for( i = v; i > 0; i-- )
338 {
339 j = pwd_block[i - 1] + hash_block[i - 1] + c;
340 c = MBEDTLS_BYTE_1( j );
341 pwd_block[i - 1] = MBEDTLS_BYTE_0( j );
342 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200343 }
344 }
345
Paul Bakkerbd552442013-07-03 14:44:40 +0200346 ret = 0;
347
348exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500349 mbedtls_platform_zeroize( salt_block, sizeof( salt_block ) );
350 mbedtls_platform_zeroize( pwd_block, sizeof( pwd_block ) );
351 mbedtls_platform_zeroize( hash_block, sizeof( hash_block ) );
352 mbedtls_platform_zeroize( hash_output, sizeof( hash_output ) );
Paul Bakker91c301a2014-06-18 13:59:38 +0200353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_md_free( &md_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200355
356 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200357}
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#endif /* MBEDTLS_PKCS12_C */