Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PKCS#12 Personal Information Exchange Syntax |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 18 | */ |
| 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 Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 26 | #include "common.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PKCS12_C) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/pkcs12.h" |
| 31 | #include "mbedtls/asn1.h" |
| 32 | #include "mbedtls/cipher.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 33 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 34 | #include "mbedtls/error.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 35 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_DES_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 39 | #include "mbedtls/des.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 40 | #endif |
| 41 | |
Hanno Becker | 8a89f9f | 2018-10-12 10:46:32 +0100 | [diff] [blame] | 42 | #if defined(MBEDTLS_ASN1_PARSE_C) |
| 43 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params, |
| 45 | mbedtls_asn1_buf *salt, int *iterations ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 46 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 47 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 48 | unsigned char **p = ¶ms->p; |
| 49 | const unsigned char *end = params->p + params->len; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * pkcs-12PbeParams ::= SEQUENCE { |
| 53 | * salt OCTET STRING, |
| 54 | * iterations INTEGER |
| 55 | * } |
| 56 | * |
| 57 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 59 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, |
| 60 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 63 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret ) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 64 | |
| 65 | salt->p = *p; |
| 66 | *p += salt->len; |
| 67 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 69 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret ) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 70 | |
| 71 | if( *p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 72 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, |
| 73 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 74 | |
| 75 | return( 0 ); |
| 76 | } |
| 77 | |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 78 | #define PKCS12_MAX_PWDLEN 128 |
| 79 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 81 | const unsigned char *pwd, size_t pwdlen, |
| 82 | unsigned char *key, size_t keylen, |
| 83 | unsigned char *iv, size_t ivlen ) |
| 84 | { |
Nicholas Wilson | 409401c | 2016-04-13 11:48:25 +0100 | [diff] [blame] | 85 | int ret, iterations = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | mbedtls_asn1_buf salt; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 87 | size_t i; |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 88 | unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2]; |
| 89 | |
| 90 | if( pwdlen > PKCS12_MAX_PWDLEN ) |
| 91 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 92 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 93 | memset( &salt, 0, sizeof(mbedtls_asn1_buf) ); |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 94 | memset( &unipwd, 0, sizeof(unipwd) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 95 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 96 | if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt, |
| 97 | &iterations ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 98 | return( ret ); |
| 99 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 100 | for( i = 0; i < pwdlen; i++ ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 101 | unipwd[i * 2 + 1] = pwd[i]; |
| 102 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 104 | salt.p, salt.len, md_type, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 106 | { |
| 107 | return( ret ); |
| 108 | } |
| 109 | |
| 110 | if( iv == NULL || ivlen == 0 ) |
| 111 | return( 0 ); |
| 112 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 114 | salt.p, salt.len, md_type, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 116 | { |
| 117 | return( ret ); |
| 118 | } |
| 119 | return( 0 ); |
| 120 | } |
| 121 | |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 122 | #undef PKCS12_MAX_PWDLEN |
| 123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode, |
| 125 | mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 126 | const unsigned char *pwd, size_t pwdlen, |
| 127 | const unsigned char *data, size_t len, |
| 128 | unsigned char *output ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 129 | { |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 130 | int ret, keylen = 0; |
| 131 | unsigned char key[32]; |
| 132 | unsigned char iv[16]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | const mbedtls_cipher_info_t *cipher_info; |
| 134 | mbedtls_cipher_context_t cipher_ctx; |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 135 | size_t olen = 0; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 136 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | cipher_info = mbedtls_cipher_info_from_type( cipher_type ); |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 138 | if( cipher_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 140 | |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 141 | keylen = cipher_info->key_bitlen / 8; |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 142 | |
| 143 | if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen, |
| 144 | key, keylen, |
| 145 | iv, cipher_info->iv_size ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 146 | { |
| 147 | return( ret ); |
| 148 | } |
| 149 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | mbedtls_cipher_init( &cipher_ctx ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 151 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 152 | if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 153 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 154 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 156 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 157 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 159 | goto exit; |
| 160 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 162 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 163 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 165 | output, &olen ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 166 | { |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 167 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 ) |
| 171 | ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 172 | |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 173 | exit: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 174 | mbedtls_platform_zeroize( key, sizeof( key ) ); |
| 175 | mbedtls_platform_zeroize( iv, sizeof( iv ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | mbedtls_cipher_free( &cipher_ctx ); |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 177 | |
| 178 | return( ret ); |
Paul Bakker | 531e294 | 2013-06-24 19:23:12 +0200 | [diff] [blame] | 179 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 180 | |
Hanno Becker | 8a89f9f | 2018-10-12 10:46:32 +0100 | [diff] [blame] | 181 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
| 182 | |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 183 | static void pkcs12_fill_buffer( unsigned char *data, size_t data_len, |
| 184 | const unsigned char *filler, size_t fill_len ) |
| 185 | { |
| 186 | unsigned char *p = data; |
| 187 | size_t use_len; |
| 188 | |
| 189 | while( data_len > 0 ) |
| 190 | { |
| 191 | use_len = ( data_len > fill_len ) ? fill_len : data_len; |
| 192 | memcpy( p, filler, use_len ); |
| 193 | p += use_len; |
| 194 | data_len -= use_len; |
| 195 | } |
| 196 | } |
| 197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 198 | int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 199 | const unsigned char *pwd, size_t pwdlen, |
| 200 | const unsigned char *salt, size_t saltlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | mbedtls_md_type_t md_type, int id, int iterations ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 202 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 203 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 204 | unsigned int j; |
| 205 | |
| 206 | unsigned char diversifier[128]; |
| 207 | unsigned char salt_block[128], pwd_block[128], hash_block[128]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | unsigned char hash_output[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 209 | unsigned char *p; |
| 210 | unsigned char c; |
| 211 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 212 | size_t hlen, use_len, v, i; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 213 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 214 | const mbedtls_md_info_t *md_info; |
| 215 | mbedtls_md_context_t md_ctx; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 216 | |
| 217 | // This version only allows max of 64 bytes of password or salt |
| 218 | if( datalen > 128 || pwdlen > 64 || saltlen > 64 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 220 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | md_info = mbedtls_md_info_from_type( md_type ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 222 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 226 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 228 | return( ret ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | hlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 230 | |
| 231 | if( hlen <= 32 ) |
| 232 | v = 64; |
| 233 | else |
| 234 | v = 128; |
| 235 | |
| 236 | memset( diversifier, (unsigned char) id, v ); |
| 237 | |
| 238 | pkcs12_fill_buffer( salt_block, v, salt, saltlen ); |
| 239 | pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen ); |
| 240 | |
| 241 | p = data; |
| 242 | while( datalen > 0 ) |
| 243 | { |
| 244 | // Calculate hash( diversifier || salt_block || pwd_block ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 246 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 249 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 252 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 255 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 258 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 259 | |
| 260 | // Perform remaining ( iterations - 1 ) recursive hash calculations |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 261 | for( i = 1; i < (size_t) iterations; i++ ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 262 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 264 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | use_len = ( datalen > hlen ) ? hlen : datalen; |
| 268 | memcpy( p, hash_output, use_len ); |
| 269 | datalen -= use_len; |
| 270 | p += use_len; |
| 271 | |
| 272 | if( datalen == 0 ) |
| 273 | break; |
| 274 | |
| 275 | // Concatenating copies of hash_output into hash_block (B) |
| 276 | pkcs12_fill_buffer( hash_block, v, hash_output, hlen ); |
| 277 | |
| 278 | // B += 1 |
| 279 | for( i = v; i > 0; i-- ) |
| 280 | if( ++hash_block[i - 1] != 0 ) |
| 281 | break; |
| 282 | |
| 283 | // salt_block += B |
| 284 | c = 0; |
| 285 | for( i = v; i > 0; i-- ) |
| 286 | { |
| 287 | j = salt_block[i - 1] + hash_block[i - 1] + c; |
| 288 | c = (unsigned char) (j >> 8); |
| 289 | salt_block[i - 1] = j & 0xFF; |
| 290 | } |
| 291 | |
| 292 | // pwd_block += B |
| 293 | c = 0; |
| 294 | for( i = v; i > 0; i-- ) |
| 295 | { |
| 296 | j = pwd_block[i - 1] + hash_block[i - 1] + c; |
| 297 | c = (unsigned char) (j >> 8); |
| 298 | pwd_block[i - 1] = j & 0xFF; |
| 299 | } |
| 300 | } |
| 301 | |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 302 | ret = 0; |
| 303 | |
| 304 | exit: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 305 | mbedtls_platform_zeroize( salt_block, sizeof( salt_block ) ); |
| 306 | mbedtls_platform_zeroize( pwd_block, sizeof( pwd_block ) ); |
| 307 | mbedtls_platform_zeroize( hash_block, sizeof( hash_block ) ); |
| 308 | mbedtls_platform_zeroize( hash_output, sizeof( hash_output ) ); |
Paul Bakker | 91c301a | 2014-06-18 13:59:38 +0200 | [diff] [blame] | 309 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 310 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 311 | |
| 312 | return( ret ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | #endif /* MBEDTLS_PKCS12_C */ |