blob: c603a1357719651f001f05aabbc614411fe03424 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
56 mbedtls_asn1_buf *salt, int *iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020057{
58 int ret;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020059 unsigned char **p = &params->p;
60 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020061
62 /*
63 * pkcs-12PbeParams ::= SEQUENCE {
64 * salt OCTET STRING,
65 * iterations INTEGER
66 * }
67 *
68 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
70 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
71 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
74 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020075
76 salt->p = *p;
77 *p += salt->len;
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
80 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020081
82 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
84 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020085
86 return( 0 );
87}
88
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020089#define PKCS12_MAX_PWDLEN 128
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020092 const unsigned char *pwd, size_t pwdlen,
93 unsigned char *key, size_t keylen,
94 unsigned char *iv, size_t ivlen )
95{
Nicholas Wilson409401c2016-04-13 11:48:25 +010096 int ret, iterations = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_asn1_buf salt;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020098 size_t i;
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020099 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
100
101 if( pwdlen > PKCS12_MAX_PWDLEN )
102 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200105 memset( &unipwd, 0, sizeof(unipwd) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200106
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200107 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
108 &iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200109 return( ret );
110
Paul Bakker66d5d072014-06-17 16:39:18 +0200111 for( i = 0; i < pwdlen; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200112 unipwd[i * 2 + 1] = pwd[i];
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200115 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200117 {
118 return( ret );
119 }
120
121 if( iv == NULL || ivlen == 0 )
122 return( 0 );
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200125 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200127 {
128 return( ret );
129 }
130 return( 0 );
131}
132
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200133#undef PKCS12_MAX_PWDLEN
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200136 const unsigned char *pwd, size_t pwdlen,
137 const unsigned char *data, size_t len,
138 unsigned char *output )
139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if !defined(MBEDTLS_ARC4_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200141 ((void) pbe_params);
142 ((void) mode);
143 ((void) pwd);
144 ((void) pwdlen);
145 ((void) data);
146 ((void) len);
147 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200149#else
150 int ret;
151 unsigned char key[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_arc4_context ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200153 ((void) mode);
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_arc4_init( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
Paul Bakker38b50d72013-06-24 19:33:27 +0200158 pwd, pwdlen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200159 key, 16, NULL, 0 ) ) != 0 )
160 {
161 return( ret );
162 }
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_arc4_setup( &ctx, key, 16 );
165 if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200166 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200167
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200168exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_zeroize( key, sizeof( key ) );
170 mbedtls_arc4_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200171
172 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#endif /* MBEDTLS_ARC4_C */
Paul Bakker531e2942013-06-24 19:23:12 +0200174}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
177 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
Paul Bakker38b50d72013-06-24 19:33:27 +0200178 const unsigned char *pwd, size_t pwdlen,
179 const unsigned char *data, size_t len,
180 unsigned char *output )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200181{
Paul Bakker38b50d72013-06-24 19:33:27 +0200182 int ret, keylen = 0;
183 unsigned char key[32];
184 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 const mbedtls_cipher_info_t *cipher_info;
186 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker38b50d72013-06-24 19:33:27 +0200187 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
Paul Bakker38b50d72013-06-24 19:33:27 +0200190 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakker38b50d72013-06-24 19:33:27 +0200192
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200193 keylen = cipher_info->key_bitlen / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200194
195 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
196 key, keylen,
197 iv, cipher_info->iv_size ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200198 {
199 return( ret );
200 }
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200203
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200204 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200205 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200208 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200211 goto exit;
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200214 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
Paul Bakker38b50d72013-06-24 19:33:27 +0200217 output, &olen ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200218 {
Paul Bakkerbd552442013-07-03 14:44:40 +0200219 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200220 }
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
223 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200224
Paul Bakkerbd552442013-07-03 14:44:40 +0200225exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_zeroize( key, sizeof( key ) );
227 mbedtls_zeroize( iv, sizeof( iv ) );
228 mbedtls_cipher_free( &cipher_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200229
230 return( ret );
Paul Bakker531e2942013-06-24 19:23:12 +0200231}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200232
233static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
234 const unsigned char *filler, size_t fill_len )
235{
236 unsigned char *p = data;
237 size_t use_len;
238
239 while( data_len > 0 )
240 {
241 use_len = ( data_len > fill_len ) ? fill_len : data_len;
242 memcpy( p, filler, use_len );
243 p += use_len;
244 data_len -= use_len;
245 }
246}
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200249 const unsigned char *pwd, size_t pwdlen,
250 const unsigned char *salt, size_t saltlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_md_type_t md_type, int id, int iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200252{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200253 int ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200254 unsigned int j;
255
256 unsigned char diversifier[128];
257 unsigned char salt_block[128], pwd_block[128], hash_block[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200259 unsigned char *p;
260 unsigned char c;
261
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200262 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 const mbedtls_md_info_t *md_info;
265 mbedtls_md_context_t md_ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200266
267 // This version only allows max of 64 bytes of password or salt
268 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200272 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_md_init( &md_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200278 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200280
281 if( hlen <= 32 )
282 v = 64;
283 else
284 v = 128;
285
286 memset( diversifier, (unsigned char) id, v );
287
288 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
289 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
290
291 p = data;
292 while( datalen > 0 )
293 {
294 // Calculate hash( diversifier || salt_block || pwd_block )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200296 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200299 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200302 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200305 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200308 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200309
310 // Perform remaining ( iterations - 1 ) recursive hash calculations
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200311 for( i = 1; i < (size_t) iterations; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200314 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200315 }
316
317 use_len = ( datalen > hlen ) ? hlen : datalen;
318 memcpy( p, hash_output, use_len );
319 datalen -= use_len;
320 p += use_len;
321
322 if( datalen == 0 )
323 break;
324
325 // Concatenating copies of hash_output into hash_block (B)
326 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
327
328 // B += 1
329 for( i = v; i > 0; i-- )
330 if( ++hash_block[i - 1] != 0 )
331 break;
332
333 // salt_block += B
334 c = 0;
335 for( i = v; i > 0; i-- )
336 {
337 j = salt_block[i - 1] + hash_block[i - 1] + c;
338 c = (unsigned char) (j >> 8);
339 salt_block[i - 1] = j & 0xFF;
340 }
341
342 // pwd_block += B
343 c = 0;
344 for( i = v; i > 0; i-- )
345 {
346 j = pwd_block[i - 1] + hash_block[i - 1] + c;
347 c = (unsigned char) (j >> 8);
348 pwd_block[i - 1] = j & 0xFF;
349 }
350 }
351
Paul Bakkerbd552442013-07-03 14:44:40 +0200352 ret = 0;
353
354exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_zeroize( salt_block, sizeof( salt_block ) );
356 mbedtls_zeroize( pwd_block, sizeof( pwd_block ) );
357 mbedtls_zeroize( hash_block, sizeof( hash_block ) );
358 mbedtls_zeroize( hash_output, sizeof( hash_output ) );
Paul Bakker91c301a2014-06-18 13:59:38 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_md_free( &md_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200361
362 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200363}
364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_PKCS12_C */