blob: f471b6378b79a9981fc2e5d70632defc984460da [file] [log] [blame]
Paul Bakkerb0c19a42013-06-24 19:26:38 +02001/**
2 * \file pkcs5.c
3 *
4 * \brief PKCS#5 functions
5 *
6 * \author Mathias Olsson <mathias@kompetensum.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakkerb0c19a42013-06-24 19:26:38 +020022 */
23/*
24 * PKCS#5 includes PBKDF2 and more
25 *
26 * http://tools.ietf.org/html/rfc2898 (Specification)
27 * http://tools.ietf.org/html/rfc6070 (Test vectors)
28 */
29
Gilles Peskinedb09ef62020-06-03 01:43:33 +020030#include "common.h"
Paul Bakkerb0c19a42013-06-24 19:26:38 +020031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_PKCS5_C)
Paul Bakkerb0c19a42013-06-24 19:26:38 +020033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/pkcs5.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000035#include "mbedtls/error.h"
Marcos Del Sol Vives8a0dfac2016-11-06 12:22:25 +010036
37#if defined(MBEDTLS_ASN1_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/asn1.h"
39#include "mbedtls/cipher.h"
40#include "mbedtls/oid.h"
Andres Amaya Garciaaf9a4862018-03-27 20:53:07 +010041#endif /* MBEDTLS_ASN1_PARSE_C */
42
43#include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000044
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010046
Andrzej Kurekdd36c762022-08-31 13:29:38 -040047#include "hash_info.h"
48#include "mbedtls/psa_util.h"
49
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050050#if !defined(MBEDTLS_MD_C)
51#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
52 psa_to_md_errors, \
53 psa_generic_status_to_mbedtls)
54#endif
55
Hanno Becker1ea604d2018-10-12 10:57:33 +010056#if defined(MBEDTLS_ASN1_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010057static int pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf *params,
58 mbedtls_asn1_buf *salt, int *iterations,
59 int *keylen, mbedtls_md_type_t *md_type)
Paul Bakker28144de2013-06-24 19:28:55 +020060{
Janos Follath24eed8d2019-11-22 13:21:35 +000061 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_asn1_buf prf_alg_oid;
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020063 unsigned char *p = params->p;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020064 const unsigned char *end = params->p + params->len;
Paul Bakker28144de2013-06-24 19:28:55 +020065
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
67 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
68 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
69 }
Paul Bakker28144de2013-06-24 19:28:55 +020070 /*
71 * PBKDF2-params ::= SEQUENCE {
72 * salt OCTET STRING,
73 * iterationCount INTEGER,
74 * keyLength INTEGER OPTIONAL
75 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
76 * }
77 *
78 */
Gilles Peskine449bd832023-01-11 14:50:10 +010079 if ((ret = mbedtls_asn1_get_tag(&p, end, &salt->len,
80 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
81 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
82 }
Paul Bakker28144de2013-06-24 19:28:55 +020083
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020084 salt->p = p;
85 p += salt->len;
Paul Bakker28144de2013-06-24 19:28:55 +020086
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if ((ret = mbedtls_asn1_get_int(&p, end, iterations)) != 0) {
88 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
Paul Bakker28144de2013-06-24 19:28:55 +020089 }
90
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (p == end) {
92 return 0;
93 }
Paul Bakker28144de2013-06-24 19:28:55 +020094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if ((ret = mbedtls_asn1_get_int(&p, end, keylen)) != 0) {
96 if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
97 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
98 }
99 }
Paul Bakker28144de2013-06-24 19:28:55 +0200100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (p == end) {
102 return 0;
103 }
Paul Bakker28144de2013-06-24 19:28:55 +0200104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if ((ret = mbedtls_asn1_get_alg_null(&p, end, &prf_alg_oid)) != 0) {
106 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
107 }
Paul Bakker28144de2013-06-24 19:28:55 +0200108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (mbedtls_oid_get_md_hmac(&prf_alg_oid, md_type) != 0) {
110 return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
111 }
112
113 if (p != end) {
114 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
115 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
116 }
117
118 return 0;
Paul Bakker28144de2013-06-24 19:28:55 +0200119}
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
122 const unsigned char *pwd, size_t pwdlen,
123 const unsigned char *data, size_t datalen,
124 unsigned char *output)
Paul Bakker28144de2013-06-24 19:28:55 +0200125{
126 int ret, iterations = 0, keylen = 0;
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200127 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
129 mbedtls_asn1_buf salt;
130 mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
Paul Bakker28144de2013-06-24 19:28:55 +0200131 unsigned char key[32], iv[32];
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200132 size_t olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_cipher_type_t cipher_alg;
135 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker28144de2013-06-24 19:28:55 +0200136
137 p = pbe_params->p;
138 end = p + pbe_params->len;
139
140 /*
141 * PBES2-params ::= SEQUENCE {
142 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
143 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
144 * }
145 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if (pbe_params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
147 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
148 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
149 }
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if ((ret = mbedtls_asn1_get_alg(&p, end, &kdf_alg_oid,
152 &kdf_alg_params)) != 0) {
153 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
154 }
Paul Bakker28144de2013-06-24 19:28:55 +0200155
156 // Only PBKDF2 supported at the moment
157 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid) != 0) {
159 return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
Paul Bakker28144de2013-06-24 19:28:55 +0200160 }
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if ((ret = pkcs5_parse_pbkdf2_params(&kdf_alg_params,
163 &salt, &iterations, &keylen,
164 &md_type)) != 0) {
165 return ret;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200166 }
Paul Bakker28144de2013-06-24 19:28:55 +0200167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 if ((ret = mbedtls_asn1_get_alg(&p, end, &enc_scheme_oid,
169 &enc_scheme_params)) != 0) {
170 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
171 }
Paul Bakker28144de2013-06-24 19:28:55 +0200172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (mbedtls_oid_get_cipher_alg(&enc_scheme_oid, &cipher_alg) != 0) {
174 return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
175 }
176
177 cipher_info = mbedtls_cipher_info_from_type(cipher_alg);
178 if (cipher_info == NULL) {
179 return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
180 }
Paul Bakker28144de2013-06-24 19:28:55 +0200181
Manuel Pégourié-Gonnard66aca932014-06-12 13:14:55 +0200182 /*
183 * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
184 * since it is optional and we don't know if it was set or not
185 */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200186 keylen = cipher_info->key_bitlen / 8;
Paul Bakker28144de2013-06-24 19:28:55 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
189 enc_scheme_params.len != cipher_info->iv_size) {
190 return MBEDTLS_ERR_PKCS5_INVALID_FORMAT;
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200191 }
Paul Bakker28144de2013-06-24 19:28:55 +0200192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_cipher_init(&cipher_ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 memcpy(iv, enc_scheme_params.p, enc_scheme_params.len);
Paul Bakker28144de2013-06-24 19:28:55 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if ((ret = mbedtls_pkcs5_pbkdf2_hmac_ext(md_type, pwd, pwdlen, salt.p,
198 salt.len, iterations, keylen,
199 key)) != 0) {
Paul Bakker46320832013-07-03 14:01:52 +0200200 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200201 }
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
Paul Bakker46320832013-07-03 14:01:52 +0200204 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 }
Paul Bakker46320832013-07-03 14:01:52 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if ((ret = mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
208 (mbedtls_operation_t) mode)) != 0) {
Paul Bakker46320832013-07-03 14:01:52 +0200209 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 }
Paul Bakker28144de2013-06-24 19:28:55 +0200211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if ((ret = mbedtls_cipher_crypt(&cipher_ctx, iv, enc_scheme_params.len,
213 data, datalen, output, &olen)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 }
Paul Bakker28144de2013-06-24 19:28:55 +0200216
Paul Bakker46320832013-07-03 14:01:52 +0200217exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 mbedtls_cipher_free(&cipher_ctx);
Paul Bakker46320832013-07-03 14:01:52 +0200219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return ret;
Paul Bakker28144de2013-06-24 19:28:55 +0200221}
Marcos Del Sol Vives8a0dfac2016-11-06 12:22:25 +0100222#endif /* MBEDTLS_ASN1_PARSE_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200223
Andrzej Kurekf0004712022-08-31 19:10:42 -0400224#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225static int pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx,
226 const unsigned char *password,
227 size_t plen, const unsigned char *salt, size_t slen,
228 unsigned int iteration_count,
229 uint32_t key_length, unsigned char *output)
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200230{
gabor-mezei-armb8513fa2020-08-24 09:53:04 +0200231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200232 unsigned int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 unsigned char md1[MBEDTLS_MD_MAX_SIZE];
234 unsigned char work[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 unsigned char md_size = mbedtls_md_get_size(ctx->md_info);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200236 size_t use_len;
237 unsigned char *out_p = output;
238 unsigned char counter[4];
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 memset(counter, 0, 4);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200241 counter[3] = 1;
242
Azim Khan45b79cf2018-05-23 16:55:16 +0100243#if UINT_MAX > 0xFFFFFFFF
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (iteration_count > 0xFFFFFFFF) {
245 return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
246 }
Azim Khan45b79cf2018-05-23 16:55:16 +0100247#endif
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if ((ret = mbedtls_md_hmac_starts(ctx, password, plen)) != 0) {
250 return ret;
251 }
252 while (key_length) {
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200253 // U1 ends up in work
254 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_md_hmac_update(ctx, salt, slen)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200256 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if ((ret = mbedtls_md_hmac_update(ctx, counter, 4)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200260 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if ((ret = mbedtls_md_hmac_finish(ctx, work)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200264 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if ((ret = mbedtls_md_hmac_reset(ctx)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200268 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 }
Jack Lloyd71657492019-09-23 19:15:54 -0400270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 memcpy(md1, work, md_size);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 for (i = 1; i < iteration_count; i++) {
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200274 // U2 ends up in md1
275 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if ((ret = mbedtls_md_hmac_update(ctx, md1, md_size)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200277 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if ((ret = mbedtls_md_hmac_finish(ctx, md1)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200281 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if ((ret = mbedtls_md_hmac_reset(ctx)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200285 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 }
Jack Lloyd71657492019-09-23 19:15:54 -0400287
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200288 // U1 xor U2
289 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 mbedtls_xor(work, work, md1, md_size);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200291 }
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 use_len = (key_length < md_size) ? key_length : md_size;
294 memcpy(out_p, work, use_len);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200295
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200296 key_length -= (uint32_t) use_len;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200297 out_p += use_len;
298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 for (i = 4; i > 0; i--) {
300 if (++counter[i - 1] != 0) {
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200301 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 }
303 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200304 }
305
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200306cleanup:
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200307 /* Zeroise buffers to clear sensitive data from memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 mbedtls_platform_zeroize(work, MBEDTLS_MD_MAX_SIZE);
309 mbedtls_platform_zeroize(md1, MBEDTLS_MD_MAX_SIZE);
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return ret;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200312}
Andrzej Kurek3d0dfb92022-09-01 05:16:48 -0400313
314#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100315int mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx,
316 const unsigned char *password,
317 size_t plen, const unsigned char *salt, size_t slen,
318 unsigned int iteration_count,
319 uint32_t key_length, unsigned char *output)
Andrzej Kurek3d0dfb92022-09-01 05:16:48 -0400320{
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return pkcs5_pbkdf2_hmac(ctx, password, plen, salt, slen, iteration_count,
322 key_length, output);
Andrzej Kurek3d0dfb92022-09-01 05:16:48 -0400323}
324#endif
Andrzej Kurekf0004712022-08-31 19:10:42 -0400325#endif /* MBEDTLS_MD_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327int mbedtls_pkcs5_pbkdf2_hmac_ext(mbedtls_md_type_t md_alg,
328 const unsigned char *password,
329 size_t plen, const unsigned char *salt, size_t slen,
330 unsigned int iteration_count,
331 uint32_t key_length, unsigned char *output)
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400332{
333#if defined(MBEDTLS_MD_C)
334 mbedtls_md_context_t md_ctx;
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400335 const mbedtls_md_info_t *md_info = NULL;
336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 md_info = mbedtls_md_info_from_type(md_alg);
339 if (md_info == NULL) {
340 return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
341 }
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 mbedtls_md_init(&md_ctx);
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400346 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 }
348 ret = pkcs5_pbkdf2_hmac(&md_ctx, password, plen, salt, slen,
349 iteration_count, key_length, output);
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400350exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 mbedtls_md_free(&md_ctx);
352 return ret;
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400353#else
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400354 unsigned int i;
355 unsigned char md1[PSA_HASH_MAX_SIZE];
356 unsigned char work[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 const unsigned char md_size = mbedtls_hash_info_get_size(md_alg);
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400358 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
359
360 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek216baca2022-09-01 12:59:05 -0400361 psa_status_t status_destruction = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400362 size_t use_len, out_len;
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400363 unsigned char *out_p = output;
364 unsigned char counter[4];
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400365 mbedtls_svc_key_id_t psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400366 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 const psa_algorithm_t alg = PSA_ALG_HMAC(mbedtls_hash_info_psa_from_md(md_alg));
368 const size_t out_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_HMAC, 0, alg);
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 memset(counter, 0, sizeof(counter));
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400371 counter[3] = 1;
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
374 psa_set_key_algorithm(&attributes, alg);
375 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (key_length == 0) {
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400378 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 }
380 if ((status = psa_import_key(&attributes,
381 password, plen,
382 &psa_hmac_key)) != PSA_SUCCESS) {
Andrzej Kurek216baca2022-09-01 12:59:05 -0400383 return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400384 }
385
386#if UINT_MAX > 0xFFFFFFFF
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (iteration_count > 0xFFFFFFFF) {
388 return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
389 }
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400390#endif
391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 while (key_length) {
393 status = psa_mac_sign_setup(&operation, psa_hmac_key,
394 PSA_ALG_HMAC(alg));
395 if (status != PSA_SUCCESS) {
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400396 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 }
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400398 // U1 ends up in work
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if ((status = psa_mac_update(&operation, salt, slen)) != PSA_SUCCESS) {
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400400 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 }
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if ((status = psa_mac_update(&operation, counter, sizeof(counter))) != PSA_SUCCESS) {
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400404 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 }
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if ((status = psa_mac_sign_finish(&operation, work, out_size, &out_len))
408 != PSA_SUCCESS) {
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400409 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 }
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 memcpy(md1, work, out_len);
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 for (i = 1; i < iteration_count; i++) {
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400415 // U2 ends up in md1
416 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 status = psa_mac_sign_setup(&operation, psa_hmac_key,
418 PSA_ALG_HMAC(alg));
419 if (status != PSA_SUCCESS) {
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400420 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 }
422 if ((status = psa_mac_update(&operation, md1, md_size)) != PSA_SUCCESS) {
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400423 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 }
425 if ((status =
426 psa_mac_sign_finish(&operation, md1, out_size, &out_len)) != PSA_SUCCESS) {
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400427 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 }
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400429
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400430 // U1 xor U2
431 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 mbedtls_xor(work, work, md1, md_size);
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400433 }
434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 use_len = (key_length < md_size) ? key_length : md_size;
436 memcpy(out_p, work, use_len);
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400437
438 key_length -= (uint32_t) use_len;
439 out_p += use_len;
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 for (i = 4; i > 0; i--) {
442 if (++counter[i - 1] != 0) {
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400443 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 }
445 }
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400446 }
447
448cleanup:
449 /* Zeroise buffers to clear sensitive data from memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 mbedtls_platform_zeroize(work, PSA_HASH_MAX_SIZE);
451 mbedtls_platform_zeroize(md1, PSA_HASH_MAX_SIZE);
452 status_destruction = psa_destroy_key(psa_hmac_key);
453 if (status == PSA_SUCCESS && status_destruction != PSA_SUCCESS) {
Andrzej Kurek216baca2022-09-01 12:59:05 -0400454 status = status_destruction;
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 }
456 status_destruction = psa_mac_abort(&operation);
457 if (status == PSA_SUCCESS && status_destruction != PSA_SUCCESS) {
Andrzej Kurek216baca2022-09-01 12:59:05 -0400458 status = status_destruction;
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 }
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400460
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500461 return PSA_TO_MBEDTLS_ERR(status);
Andrzej Kurekf0004712022-08-31 19:10:42 -0400462#endif /* !MBEDTLS_MD_C */
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400463}
464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#if defined(MBEDTLS_SELF_TEST)
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200466
Andrzej Kureked98e952022-08-31 14:57:11 -0400467#if !defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100468int mbedtls_pkcs5_self_test(int verbose)
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200469{
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (verbose != 0) {
471 mbedtls_printf(" PBKDF2 (SHA1): skipped\n\n");
472 }
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return 0;
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200475}
476#else
477
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200478#define MAX_TESTS 6
479
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100480static const size_t plen_test_data[MAX_TESTS] =
Gilles Peskine449bd832023-01-11 14:50:10 +0100481{ 8, 8, 8, 24, 9 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200482
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100483static const unsigned char password_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200484{
485 "password",
486 "password",
487 "password",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200488 "passwordPASSWORDpassword",
489 "pass\0word",
490};
491
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100492static const size_t slen_test_data[MAX_TESTS] =
Gilles Peskine449bd832023-01-11 14:50:10 +0100493{ 4, 4, 4, 36, 5 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200494
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100495static const unsigned char salt_test_data[MAX_TESTS][40] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200496{
497 "salt",
498 "salt",
499 "salt",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200500 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
501 "sa\0lt",
502};
503
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100504static const uint32_t it_cnt_test_data[MAX_TESTS] =
Gilles Peskine449bd832023-01-11 14:50:10 +0100505{ 1, 2, 4096, 4096, 4096 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200506
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100507static const uint32_t key_len_test_data[MAX_TESTS] =
Gilles Peskine449bd832023-01-11 14:50:10 +0100508{ 20, 20, 20, 25, 16 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200509
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100510static const unsigned char result_key_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200511{
512 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
513 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
514 0x2f, 0xe0, 0x37, 0xa6 },
515 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
516 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
517 0xd8, 0xde, 0x89, 0x57 },
518 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
519 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
520 0x65, 0xa4, 0x29, 0xc1 },
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200521 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
522 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
523 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
524 0x38 },
525 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
526 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
527};
528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529int mbedtls_pkcs5_self_test(int verbose)
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200530{
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200531 int ret, i;
532 unsigned char key[64];
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 for (i = 0; i < MAX_TESTS; i++) {
535 if (verbose != 0) {
536 mbedtls_printf(" PBKDF2 (SHA1) #%d: ", i);
537 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 ret = mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA1, password_test_data[i],
540 plen_test_data[i], salt_test_data[i],
541 slen_test_data[i], it_cnt_test_data[i],
542 key_len_test_data[i], key);
543 if (ret != 0 ||
544 memcmp(result_key_test_data[i], key, key_len_test_data[i]) != 0) {
545 if (verbose != 0) {
546 mbedtls_printf("failed\n");
547 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200548
Paul Bakker84bbeb52014-07-01 14:53:22 +0200549 ret = 1;
550 goto exit;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200551 }
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (verbose != 0) {
554 mbedtls_printf("passed\n");
555 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200556 }
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (verbose != 0) {
559 mbedtls_printf("\n");
560 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200561
Paul Bakker84bbeb52014-07-01 14:53:22 +0200562exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return ret;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200564}
Andrzej Kureked98e952022-08-31 14:57:11 -0400565#endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#endif /* MBEDTLS_SELF_TEST */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#endif /* MBEDTLS_PKCS5_C */