blob: 5e8b2879a06b586781e34254977048ba7b969787 [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"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020039
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/arc4.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/des.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020048#endif
49
Paul Bakker91c301a2014-06-18 13:59:38 +020050/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker91c301a2014-06-18 13:59:38 +020052 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
53}
54
Hanno Beckerd30cd342018-10-12 10:46:32 +010055#if defined(MBEDTLS_ASN1_PARSE_C)
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
58 mbedtls_asn1_buf *salt, int *iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020059{
60 int ret;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020061 unsigned char **p = &params->p;
62 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020063
64 /*
65 * pkcs-12PbeParams ::= SEQUENCE {
66 * salt OCTET STRING,
67 * iterations INTEGER
68 * }
69 *
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
72 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
73 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
76 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020077
78 salt->p = *p;
79 *p += salt->len;
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
82 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020083
84 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
86 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020087
88 return( 0 );
89}
90
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020091#define PKCS12_MAX_PWDLEN 128
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020094 const unsigned char *pwd, size_t pwdlen,
95 unsigned char *key, size_t keylen,
96 unsigned char *iv, size_t ivlen )
97{
Nicholas Wilson409401c2016-04-13 11:48:25 +010098 int ret, iterations = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_asn1_buf salt;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200100 size_t i;
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200101 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
102
103 if( pwdlen > PKCS12_MAX_PWDLEN )
104 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200107 memset( &unipwd, 0, sizeof(unipwd) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200108
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200109 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
110 &iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200111 return( ret );
112
Paul Bakker66d5d072014-06-17 16:39:18 +0200113 for( i = 0; i < pwdlen; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200114 unipwd[i * 2 + 1] = pwd[i];
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200117 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200119 {
120 return( ret );
121 }
122
123 if( iv == NULL || ivlen == 0 )
124 return( 0 );
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200127 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200129 {
130 return( ret );
131 }
132 return( 0 );
133}
134
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200135#undef PKCS12_MAX_PWDLEN
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200138 const unsigned char *pwd, size_t pwdlen,
139 const unsigned char *data, size_t len,
140 unsigned char *output )
141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if !defined(MBEDTLS_ARC4_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200143 ((void) pbe_params);
144 ((void) mode);
145 ((void) pwd);
146 ((void) pwdlen);
147 ((void) data);
148 ((void) len);
149 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200151#else
152 int ret;
153 unsigned char key[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_arc4_context ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200155 ((void) mode);
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_arc4_init( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
Paul Bakker38b50d72013-06-24 19:33:27 +0200160 pwd, pwdlen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200161 key, 16, NULL, 0 ) ) != 0 )
162 {
163 return( ret );
164 }
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_arc4_setup( &ctx, key, 16 );
167 if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200168 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200169
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200170exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_zeroize( key, sizeof( key ) );
172 mbedtls_arc4_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200173
174 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_ARC4_C */
Paul Bakker531e2942013-06-24 19:23:12 +0200176}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
179 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
Paul Bakker38b50d72013-06-24 19:33:27 +0200180 const unsigned char *pwd, size_t pwdlen,
181 const unsigned char *data, size_t len,
182 unsigned char *output )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200183{
Paul Bakker38b50d72013-06-24 19:33:27 +0200184 int ret, keylen = 0;
185 unsigned char key[32];
186 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 const mbedtls_cipher_info_t *cipher_info;
188 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker38b50d72013-06-24 19:33:27 +0200189 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
Paul Bakker38b50d72013-06-24 19:33:27 +0200192 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakker38b50d72013-06-24 19:33:27 +0200194
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200195 keylen = cipher_info->key_bitlen / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200196
197 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
198 key, keylen,
199 iv, cipher_info->iv_size ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200200 {
201 return( ret );
202 }
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200205
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200206 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200207 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 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_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200213 goto exit;
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200216 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
Paul Bakker38b50d72013-06-24 19:33:27 +0200219 output, &olen ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200220 {
Paul Bakkerbd552442013-07-03 14:44:40 +0200221 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200222 }
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
225 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200226
Paul Bakkerbd552442013-07-03 14:44:40 +0200227exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_zeroize( key, sizeof( key ) );
229 mbedtls_zeroize( iv, sizeof( iv ) );
230 mbedtls_cipher_free( &cipher_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200231
232 return( ret );
Paul Bakker531e2942013-06-24 19:23:12 +0200233}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200234
Hanno Beckerd30cd342018-10-12 10:46:32 +0100235#endif /* MBEDTLS_ASN1_PARSE_C */
236
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200237static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
238 const unsigned char *filler, size_t fill_len )
239{
240 unsigned char *p = data;
241 size_t use_len;
242
243 while( data_len > 0 )
244 {
245 use_len = ( data_len > fill_len ) ? fill_len : data_len;
246 memcpy( p, filler, use_len );
247 p += use_len;
248 data_len -= use_len;
249 }
250}
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200253 const unsigned char *pwd, size_t pwdlen,
254 const unsigned char *salt, size_t saltlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_md_type_t md_type, int id, int iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200256{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200257 int ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200258 unsigned int j;
259
260 unsigned char diversifier[128];
261 unsigned char salt_block[128], pwd_block[128], hash_block[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200263 unsigned char *p;
264 unsigned char c;
265
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200266 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 const mbedtls_md_info_t *md_info;
269 mbedtls_md_context_t md_ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200270
271 // This version only allows max of 64 bytes of password or salt
272 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200276 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_md_init( &md_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200282 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200284
285 if( hlen <= 32 )
286 v = 64;
287 else
288 v = 128;
289
290 memset( diversifier, (unsigned char) id, v );
291
292 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
293 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
294
295 p = data;
296 while( datalen > 0 )
297 {
298 // Calculate hash( diversifier || salt_block || pwd_block )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200300 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200303 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200306 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200309 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200312 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200313
314 // Perform remaining ( iterations - 1 ) recursive hash calculations
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200315 for( i = 1; i < (size_t) iterations; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200318 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200319 }
320
321 use_len = ( datalen > hlen ) ? hlen : datalen;
322 memcpy( p, hash_output, use_len );
323 datalen -= use_len;
324 p += use_len;
325
326 if( datalen == 0 )
327 break;
328
329 // Concatenating copies of hash_output into hash_block (B)
330 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
331
332 // B += 1
333 for( i = v; i > 0; i-- )
334 if( ++hash_block[i - 1] != 0 )
335 break;
336
337 // salt_block += B
338 c = 0;
339 for( i = v; i > 0; i-- )
340 {
341 j = salt_block[i - 1] + hash_block[i - 1] + c;
342 c = (unsigned char) (j >> 8);
343 salt_block[i - 1] = j & 0xFF;
344 }
345
346 // pwd_block += B
347 c = 0;
348 for( i = v; i > 0; i-- )
349 {
350 j = pwd_block[i - 1] + hash_block[i - 1] + c;
351 c = (unsigned char) (j >> 8);
352 pwd_block[i - 1] = j & 0xFF;
353 }
354 }
355
Paul Bakkerbd552442013-07-03 14:44:40 +0200356 ret = 0;
357
358exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_zeroize( salt_block, sizeof( salt_block ) );
360 mbedtls_zeroize( pwd_block, sizeof( pwd_block ) );
361 mbedtls_zeroize( hash_block, sizeof( hash_block ) );
362 mbedtls_zeroize( hash_output, sizeof( hash_output ) );
Paul Bakker91c301a2014-06-18 13:59:38 +0200363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_md_free( &md_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200365
366 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200367}
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_PKCS12_C */