blob: ec8dc662dedf75ae0bdc9e856f7b38e007f2c071 [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
Waleed Elmelegyb66cb652023-08-01 14:56:30 +010047#if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
48int mbedtls_pkcs5_pbes2_ext(const mbedtls_asn1_buf *pbe_params, int mode,
49 const unsigned char *pwd, size_t pwdlen,
50 const unsigned char *data, size_t datalen,
51 unsigned char *output, size_t output_size,
52 size_t *output_len);
53#endif
54
Hanno Becker1ea604d2018-10-12 10:57:33 +010055#if defined(MBEDTLS_ASN1_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056static int pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf *params,
57 mbedtls_asn1_buf *salt, int *iterations,
58 int *keylen, mbedtls_md_type_t *md_type)
Paul Bakker28144de2013-06-24 19:28:55 +020059{
Janos Follath24eed8d2019-11-22 13:21:35 +000060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_asn1_buf prf_alg_oid;
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020062 unsigned char *p = params->p;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020063 const unsigned char *end = params->p + params->len;
Paul Bakker28144de2013-06-24 19:28:55 +020064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
66 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
67 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
68 }
Paul Bakker28144de2013-06-24 19:28:55 +020069 /*
70 * PBKDF2-params ::= SEQUENCE {
71 * salt OCTET STRING,
72 * iterationCount INTEGER,
73 * keyLength INTEGER OPTIONAL
74 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
75 * }
76 *
77 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078 if ((ret = mbedtls_asn1_get_tag(&p, end, &salt->len,
79 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
80 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
81 }
Paul Bakker28144de2013-06-24 19:28:55 +020082
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020083 salt->p = p;
84 p += salt->len;
Paul Bakker28144de2013-06-24 19:28:55 +020085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 if ((ret = mbedtls_asn1_get_int(&p, end, iterations)) != 0) {
87 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
Paul Bakker28144de2013-06-24 19:28:55 +020088 }
89
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 if (p == end) {
91 return 0;
92 }
Paul Bakker28144de2013-06-24 19:28:55 +020093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 if ((ret = mbedtls_asn1_get_int(&p, end, keylen)) != 0) {
95 if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
96 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
97 }
98 }
Paul Bakker28144de2013-06-24 19:28:55 +020099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 if (p == end) {
101 return 0;
102 }
Paul Bakker28144de2013-06-24 19:28:55 +0200103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 if ((ret = mbedtls_asn1_get_alg_null(&p, end, &prf_alg_oid)) != 0) {
105 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
106 }
Paul Bakker28144de2013-06-24 19:28:55 +0200107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 if (mbedtls_oid_get_md_hmac(&prf_alg_oid, md_type) != 0) {
109 return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
110 }
111
112 if (p != end) {
113 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
114 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
115 }
116
117 return 0;
Paul Bakker28144de2013-06-24 19:28:55 +0200118}
119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
121 const unsigned char *pwd, size_t pwdlen,
122 const unsigned char *data, size_t datalen,
123 unsigned char *output)
Paul Bakker28144de2013-06-24 19:28:55 +0200124{
Waleed Elmelegyb66cb652023-08-01 14:56:30 +0100125 size_t output_len = 0;
126
127 /* We assume caller of the function is providing a big enough output buffer
128 * so we pass output_size as SIZE_MAX to pass checks, However, no gurantees
129 * for the output size actually being correct.
130 */
131 return mbedtls_pkcs5_pbes2_ext(pbe_params, mode, pwd, pwdlen, data,
132 datalen, output, SIZE_MAX, &output_len);
133}
134
135int mbedtls_pkcs5_pbes2_ext(const mbedtls_asn1_buf *pbe_params, int mode,
136 const unsigned char *pwd, size_t pwdlen,
137 const unsigned char *data, size_t datalen,
138 unsigned char *output, size_t output_size,
139 size_t *output_len)
140{
Paul Bakker28144de2013-06-24 19:28:55 +0200141 int ret, iterations = 0, keylen = 0;
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200142 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
144 mbedtls_asn1_buf salt;
145 mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
Paul Bakker28144de2013-06-24 19:28:55 +0200146 unsigned char key[32], iv[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 const mbedtls_md_info_t *md_info;
148 const mbedtls_cipher_info_t *cipher_info;
149 mbedtls_md_context_t md_ctx;
150 mbedtls_cipher_type_t cipher_alg;
151 mbedtls_cipher_context_t cipher_ctx;
Waleed Elmelegyb66cb652023-08-01 14:56:30 +0100152 unsigned int padlen = 0;
Paul Bakker28144de2013-06-24 19:28:55 +0200153
154 p = pbe_params->p;
155 end = p + pbe_params->len;
156
157 /*
158 * PBES2-params ::= SEQUENCE {
159 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
160 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
161 * }
162 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 if (pbe_params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
164 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
165 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
166 }
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 if ((ret = mbedtls_asn1_get_alg(&p, end, &kdf_alg_oid,
169 &kdf_alg_params)) != 0) {
170 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
171 }
Paul Bakker28144de2013-06-24 19:28:55 +0200172
173 // Only PBKDF2 supported at the moment
174 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid) != 0) {
176 return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
Paul Bakker28144de2013-06-24 19:28:55 +0200177 }
178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 if ((ret = pkcs5_parse_pbkdf2_params(&kdf_alg_params,
180 &salt, &iterations, &keylen,
181 &md_type)) != 0) {
182 return ret;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200183 }
Paul Bakker28144de2013-06-24 19:28:55 +0200184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 md_info = mbedtls_md_info_from_type(md_type);
186 if (md_info == NULL) {
187 return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
188 }
Paul Bakker28144de2013-06-24 19:28:55 +0200189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 if ((ret = mbedtls_asn1_get_alg(&p, end, &enc_scheme_oid,
191 &enc_scheme_params)) != 0) {
192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
193 }
194
195 if (mbedtls_oid_get_cipher_alg(&enc_scheme_oid, &cipher_alg) != 0) {
196 return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
197 }
198
199 cipher_info = mbedtls_cipher_info_from_type(cipher_alg);
200 if (cipher_info == NULL) {
201 return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
202 }
Paul Bakker28144de2013-06-24 19:28:55 +0200203
Manuel Pégourié-Gonnard66aca932014-06-12 13:14:55 +0200204 /*
205 * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
206 * since it is optional and we don't know if it was set or not
207 */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200208 keylen = cipher_info->key_bitlen / 8;
Paul Bakker28144de2013-06-24 19:28:55 +0200209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 if (enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
211 enc_scheme_params.len != cipher_info->iv_size) {
212 return MBEDTLS_ERR_PKCS5_INVALID_FORMAT;
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200213 }
Paul Bakker28144de2013-06-24 19:28:55 +0200214
Waleed Elmelegyb66cb652023-08-01 14:56:30 +0100215 if (mode == MBEDTLS_PKCS5_DECRYPT) {
216 if (output_size < datalen) {
217 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
218 }
219 }
220
221 if (mode == MBEDTLS_PKCS5_ENCRYPT) {
222 padlen = cipher_info->block_size - (datalen % cipher_info->block_size);
223 if (output_size < (datalen + padlen)) {
224 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
225 }
226 }
227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 mbedtls_md_init(&md_ctx);
Waleed Elmelegyb66cb652023-08-01 14:56:30 +0100229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 mbedtls_cipher_init(&cipher_ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 memcpy(iv, enc_scheme_params.p, enc_scheme_params.len);
Paul Bakker28144de2013-06-24 19:28:55 +0200233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Paul Bakker46320832013-07-03 14:01:52 +0200235 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200236 }
237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 if ((ret = mbedtls_pkcs5_pbkdf2_hmac(&md_ctx, pwd, pwdlen, salt.p, salt.len,
239 iterations, keylen, key)) != 0) {
Paul Bakker46320832013-07-03 14:01:52 +0200240 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 }
Paul Bakker46320832013-07-03 14:01:52 +0200242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
Paul Bakker46320832013-07-03 14:01:52 +0200244 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 }
Paul Bakker28144de2013-06-24 19:28:55 +0200246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 if ((ret = mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
248 (mbedtls_operation_t) mode)) != 0) {
249 goto exit;
250 }
251
Waleed Elmelegya2307352023-07-21 16:20:17 +0100252#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Waleed Elmelegy412629c2023-07-19 14:01:35 +0100253 /* PKCS5 uses CBC with PKCS7 padding (which is the same as
254 * "PKCS5 padding" except that it's typically only called PKCS5
255 * with 64-bit-block ciphers).
256 */
257 mbedtls_cipher_padding_t padding = MBEDTLS_PADDING_PKCS7;
258#if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
259 /* For historical reasons, when decrypting, this function works when
260 * decrypting even when support for PKCS7 padding is disabled. In this
261 * case, it ignores the padding, and so will never report a
262 * password mismatch.
263 */
Waleed Elmelegya2307352023-07-21 16:20:17 +0100264 if (mode == MBEDTLS_DECRYPT) {
Waleed Elmelegy412629c2023-07-19 14:01:35 +0100265 padding = MBEDTLS_PADDING_NONE;
Waleed Elmelegya2307352023-07-21 16:20:17 +0100266 }
Waleed Elmelegy412629c2023-07-19 14:01:35 +0100267#endif
268 if ((ret = mbedtls_cipher_set_padding_mode(&cipher_ctx, padding)) != 0) {
269 goto exit;
270 }
Waleed Elmelegya2307352023-07-21 16:20:17 +0100271#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 if ((ret = mbedtls_cipher_crypt(&cipher_ctx, iv, enc_scheme_params.len,
Waleed Elmelegyb66cb652023-08-01 14:56:30 +0100273 data, datalen, output, output_len)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 }
Paul Bakker28144de2013-06-24 19:28:55 +0200276
Paul Bakker46320832013-07-03 14:01:52 +0200277exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 mbedtls_md_free(&md_ctx);
279 mbedtls_cipher_free(&cipher_ctx);
Paul Bakker46320832013-07-03 14:01:52 +0200280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 return ret;
Paul Bakker28144de2013-06-24 19:28:55 +0200282}
Marcos Del Sol Vives8a0dfac2016-11-06 12:22:25 +0100283#endif /* MBEDTLS_ASN1_PARSE_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285int mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx,
286 const unsigned char *password,
287 size_t plen, const unsigned char *salt, size_t slen,
288 unsigned int iteration_count,
289 uint32_t key_length, unsigned char *output)
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200290{
gabor-mezei-armb8513fa2020-08-24 09:53:04 +0200291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
292 int j;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200293 unsigned int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 unsigned char md1[MBEDTLS_MD_MAX_SIZE];
295 unsigned char work[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 unsigned char md_size = mbedtls_md_get_size(ctx->md_info);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200297 size_t use_len;
298 unsigned char *out_p = output;
299 unsigned char counter[4];
300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 memset(counter, 0, 4);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200302 counter[3] = 1;
303
Azim Khan45b79cf2018-05-23 16:55:16 +0100304#if UINT_MAX > 0xFFFFFFFF
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 if (iteration_count > 0xFFFFFFFF) {
306 return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
307 }
Azim Khan45b79cf2018-05-23 16:55:16 +0100308#endif
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 if ((ret = mbedtls_md_hmac_starts(ctx, password, plen)) != 0) {
311 return ret;
312 }
313 while (key_length) {
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200314 // U1 ends up in work
315 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 if ((ret = mbedtls_md_hmac_update(ctx, salt, slen)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200317 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 if ((ret = mbedtls_md_hmac_update(ctx, counter, 4)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200321 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 if ((ret = mbedtls_md_hmac_finish(ctx, work)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200325 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 if ((ret = mbedtls_md_hmac_reset(ctx)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200329 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 }
Jack Lloyd71657492019-09-23 19:15:54 -0400331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 memcpy(md1, work, md_size);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 for (i = 1; i < iteration_count; i++) {
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200335 // U2 ends up in md1
336 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 if ((ret = mbedtls_md_hmac_update(ctx, md1, md_size)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200338 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 if ((ret = mbedtls_md_hmac_finish(ctx, md1)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200342 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 if ((ret = mbedtls_md_hmac_reset(ctx)) != 0) {
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200346 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 }
Jack Lloyd71657492019-09-23 19:15:54 -0400348
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200349 // U1 xor U2
350 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 for (j = 0; j < md_size; j++) {
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200352 work[j] ^= md1[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200354 }
355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 use_len = (key_length < md_size) ? key_length : md_size;
357 memcpy(out_p, work, use_len);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200358
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200359 key_length -= (uint32_t) use_len;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200360 out_p += use_len;
361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 for (i = 4; i > 0; i--) {
363 if (++counter[i - 1] != 0) {
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200364 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 }
366 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200367 }
368
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200369cleanup:
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200370 /* Zeroise buffers to clear sensitive data from memory. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 mbedtls_platform_zeroize(work, MBEDTLS_MD_MAX_SIZE);
372 mbedtls_platform_zeroize(md1, MBEDTLS_MD_MAX_SIZE);
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 return ret;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200375}
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_SELF_TEST)
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#if !defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380int mbedtls_pkcs5_self_test(int verbose)
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200381{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 if (verbose != 0) {
383 mbedtls_printf(" PBKDF2 (SHA1): skipped\n\n");
384 }
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 return 0;
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200387}
388#else
389
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200390#define MAX_TESTS 6
391
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100392static const size_t plen_test_data[MAX_TESTS] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393{ 8, 8, 8, 24, 9 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200394
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100395static const unsigned char password_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200396{
397 "password",
398 "password",
399 "password",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200400 "passwordPASSWORDpassword",
401 "pass\0word",
402};
403
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100404static const size_t slen_test_data[MAX_TESTS] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405{ 4, 4, 4, 36, 5 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200406
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100407static const unsigned char salt_test_data[MAX_TESTS][40] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200408{
409 "salt",
410 "salt",
411 "salt",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200412 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
413 "sa\0lt",
414};
415
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100416static const uint32_t it_cnt_test_data[MAX_TESTS] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417{ 1, 2, 4096, 4096, 4096 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200418
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100419static const uint32_t key_len_test_data[MAX_TESTS] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420{ 20, 20, 20, 25, 16 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200421
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100422static const unsigned char result_key_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200423{
424 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
425 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
426 0x2f, 0xe0, 0x37, 0xa6 },
427 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
428 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
429 0xd8, 0xde, 0x89, 0x57 },
430 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
431 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
432 0x65, 0xa4, 0x29, 0xc1 },
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200433 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
434 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
435 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
436 0x38 },
437 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
438 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
439};
440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441int mbedtls_pkcs5_self_test(int verbose)
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_md_context_t sha1_ctx;
444 const mbedtls_md_info_t *info_sha1;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200445 int ret, i;
446 unsigned char key[64];
447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 mbedtls_md_init(&sha1_ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 info_sha1 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
451 if (info_sha1 == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200452 ret = 1;
453 goto exit;
454 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 if ((ret = mbedtls_md_setup(&sha1_ctx, info_sha1, 1)) != 0) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200457 ret = 1;
458 goto exit;
459 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 for (i = 0; i < MAX_TESTS; i++) {
462 if (verbose != 0) {
463 mbedtls_printf(" PBKDF2 (SHA1) #%d: ", i);
464 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 ret = mbedtls_pkcs5_pbkdf2_hmac(&sha1_ctx, password_test_data[i],
467 plen_test_data[i], salt_test_data[i],
468 slen_test_data[i], it_cnt_test_data[i],
469 key_len_test_data[i], key);
470 if (ret != 0 ||
471 memcmp(result_key_test_data[i], key, key_len_test_data[i]) != 0) {
472 if (verbose != 0) {
473 mbedtls_printf("failed\n");
474 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200475
Paul Bakker84bbeb52014-07-01 14:53:22 +0200476 ret = 1;
477 goto exit;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200478 }
479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 if (verbose != 0) {
481 mbedtls_printf("passed\n");
482 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200483 }
484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 if (verbose != 0) {
486 mbedtls_printf("\n");
487 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200488
Paul Bakker84bbeb52014-07-01 14:53:22 +0200489exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 mbedtls_md_free(&sha1_ctx);
Paul Bakkerf8634852013-07-03 13:31:52 +0200491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492 return ret;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200493}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#endif /* MBEDTLS_SHA1_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#endif /* MBEDTLS_SELF_TEST */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#endif /* MBEDTLS_PKCS5_C */