/src/mbedtls/library/pkcs5.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * \file pkcs5.c |
3 | | * |
4 | | * \brief PKCS#5 functions |
5 | | * |
6 | | * \author Mathias Olsson <mathias@kompetensum.com> |
7 | | * |
8 | | * Copyright The Mbed TLS Contributors |
9 | | * 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. |
22 | | */ |
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 | | |
30 | | #include "common.h" |
31 | | |
32 | | #if defined(MBEDTLS_PKCS5_C) |
33 | | |
34 | | #include "mbedtls/pkcs5.h" |
35 | | #include "mbedtls/error.h" |
36 | | |
37 | | #if defined(MBEDTLS_ASN1_PARSE_C) |
38 | | #include "mbedtls/asn1.h" |
39 | | #include "mbedtls/cipher.h" |
40 | | #include "mbedtls/oid.h" |
41 | | #endif /* MBEDTLS_ASN1_PARSE_C */ |
42 | | |
43 | | #include <string.h> |
44 | | |
45 | | #include "mbedtls/platform.h" |
46 | | |
47 | | #include "hash_info.h" |
48 | | #include "mbedtls/psa_util.h" |
49 | | |
50 | | #if defined(MBEDTLS_ASN1_PARSE_C) |
51 | | static int pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf *params, |
52 | | mbedtls_asn1_buf *salt, int *iterations, |
53 | | int *keylen, mbedtls_md_type_t *md_type) |
54 | 0 | { |
55 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
56 | 0 | mbedtls_asn1_buf prf_alg_oid; |
57 | 0 | unsigned char *p = params->p; |
58 | 0 | const unsigned char *end = params->p + params->len; |
59 | |
|
60 | 0 | if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
61 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, |
62 | 0 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
63 | 0 | } |
64 | | /* |
65 | | * PBKDF2-params ::= SEQUENCE { |
66 | | * salt OCTET STRING, |
67 | | * iterationCount INTEGER, |
68 | | * keyLength INTEGER OPTIONAL |
69 | | * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1 |
70 | | * } |
71 | | * |
72 | | */ |
73 | 0 | if ((ret = mbedtls_asn1_get_tag(&p, end, &salt->len, |
74 | 0 | MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
75 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret); |
76 | 0 | } |
77 | | |
78 | 0 | salt->p = p; |
79 | 0 | p += salt->len; |
80 | |
|
81 | 0 | if ((ret = mbedtls_asn1_get_int(&p, end, iterations)) != 0) { |
82 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret); |
83 | 0 | } |
84 | | |
85 | 0 | if (p == end) { |
86 | 0 | return 0; |
87 | 0 | } |
88 | | |
89 | 0 | if ((ret = mbedtls_asn1_get_int(&p, end, keylen)) != 0) { |
90 | 0 | if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
91 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret); |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | 0 | if (p == end) { |
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | 0 | if ((ret = mbedtls_asn1_get_alg_null(&p, end, &prf_alg_oid)) != 0) { |
100 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret); |
101 | 0 | } |
102 | | |
103 | 0 | if (mbedtls_oid_get_md_hmac(&prf_alg_oid, md_type) != 0) { |
104 | 0 | return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE; |
105 | 0 | } |
106 | | |
107 | 0 | if (p != end) { |
108 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, |
109 | 0 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
110 | 0 | } |
111 | | |
112 | 0 | return 0; |
113 | 0 | } |
114 | | |
115 | | int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode, |
116 | | const unsigned char *pwd, size_t pwdlen, |
117 | | const unsigned char *data, size_t datalen, |
118 | | unsigned char *output) |
119 | 0 | { |
120 | 0 | int ret, iterations = 0, keylen = 0; |
121 | 0 | unsigned char *p, *end; |
122 | 0 | mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params; |
123 | 0 | mbedtls_asn1_buf salt; |
124 | 0 | mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1; |
125 | 0 | unsigned char key[32], iv[32]; |
126 | 0 | size_t olen = 0; |
127 | 0 | const mbedtls_cipher_info_t *cipher_info; |
128 | 0 | mbedtls_cipher_type_t cipher_alg; |
129 | 0 | mbedtls_cipher_context_t cipher_ctx; |
130 | |
|
131 | 0 | p = pbe_params->p; |
132 | 0 | end = p + pbe_params->len; |
133 | | |
134 | | /* |
135 | | * PBES2-params ::= SEQUENCE { |
136 | | * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}}, |
137 | | * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}} |
138 | | * } |
139 | | */ |
140 | 0 | if (pbe_params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
141 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, |
142 | 0 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
143 | 0 | } |
144 | | |
145 | 0 | if ((ret = mbedtls_asn1_get_alg(&p, end, &kdf_alg_oid, |
146 | 0 | &kdf_alg_params)) != 0) { |
147 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret); |
148 | 0 | } |
149 | | |
150 | | // Only PBKDF2 supported at the moment |
151 | | // |
152 | 0 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid) != 0) { |
153 | 0 | return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE; |
154 | 0 | } |
155 | | |
156 | 0 | if ((ret = pkcs5_parse_pbkdf2_params(&kdf_alg_params, |
157 | 0 | &salt, &iterations, &keylen, |
158 | 0 | &md_type)) != 0) { |
159 | 0 | return ret; |
160 | 0 | } |
161 | | |
162 | 0 | if ((ret = mbedtls_asn1_get_alg(&p, end, &enc_scheme_oid, |
163 | 0 | &enc_scheme_params)) != 0) { |
164 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret); |
165 | 0 | } |
166 | | |
167 | 0 | if (mbedtls_oid_get_cipher_alg(&enc_scheme_oid, &cipher_alg) != 0) { |
168 | 0 | return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE; |
169 | 0 | } |
170 | | |
171 | 0 | cipher_info = mbedtls_cipher_info_from_type(cipher_alg); |
172 | 0 | if (cipher_info == NULL) { |
173 | 0 | return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE; |
174 | 0 | } |
175 | | |
176 | | /* |
177 | | * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored |
178 | | * since it is optional and we don't know if it was set or not |
179 | | */ |
180 | 0 | keylen = cipher_info->key_bitlen / 8; |
181 | |
|
182 | 0 | if (enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING || |
183 | 0 | enc_scheme_params.len != cipher_info->iv_size) { |
184 | 0 | return MBEDTLS_ERR_PKCS5_INVALID_FORMAT; |
185 | 0 | } |
186 | | |
187 | 0 | mbedtls_cipher_init(&cipher_ctx); |
188 | |
|
189 | 0 | memcpy(iv, enc_scheme_params.p, enc_scheme_params.len); |
190 | |
|
191 | 0 | if ((ret = mbedtls_pkcs5_pbkdf2_hmac_ext(md_type, pwd, pwdlen, salt.p, |
192 | 0 | salt.len, iterations, keylen, |
193 | 0 | key)) != 0) { |
194 | 0 | goto exit; |
195 | 0 | } |
196 | | |
197 | 0 | if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) { |
198 | 0 | goto exit; |
199 | 0 | } |
200 | | |
201 | 0 | if ((ret = mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen, |
202 | 0 | (mbedtls_operation_t) mode)) != 0) { |
203 | 0 | goto exit; |
204 | 0 | } |
205 | | |
206 | 0 | if ((ret = mbedtls_cipher_crypt(&cipher_ctx, iv, enc_scheme_params.len, |
207 | 0 | data, datalen, output, &olen)) != 0) { |
208 | 0 | ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH; |
209 | 0 | } |
210 | |
|
211 | 0 | exit: |
212 | 0 | mbedtls_cipher_free(&cipher_ctx); |
213 | |
|
214 | 0 | return ret; |
215 | 0 | } |
216 | | #endif /* MBEDTLS_ASN1_PARSE_C */ |
217 | | |
218 | | #if defined(MBEDTLS_MD_C) |
219 | | static int pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx, |
220 | | const unsigned char *password, |
221 | | size_t plen, const unsigned char *salt, size_t slen, |
222 | | unsigned int iteration_count, |
223 | | uint32_t key_length, unsigned char *output) |
224 | 0 | { |
225 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
226 | 0 | unsigned int i; |
227 | 0 | unsigned char md1[MBEDTLS_MD_MAX_SIZE]; |
228 | 0 | unsigned char work[MBEDTLS_MD_MAX_SIZE]; |
229 | 0 | unsigned char md_size = mbedtls_md_get_size(ctx->md_info); |
230 | 0 | size_t use_len; |
231 | 0 | unsigned char *out_p = output; |
232 | 0 | unsigned char counter[4]; |
233 | |
|
234 | 0 | memset(counter, 0, 4); |
235 | 0 | counter[3] = 1; |
236 | |
|
237 | | #if UINT_MAX > 0xFFFFFFFF |
238 | | if (iteration_count > 0xFFFFFFFF) { |
239 | | return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA; |
240 | | } |
241 | | #endif |
242 | |
|
243 | 0 | if ((ret = mbedtls_md_hmac_starts(ctx, password, plen)) != 0) { |
244 | 0 | return ret; |
245 | 0 | } |
246 | 0 | while (key_length) { |
247 | | // U1 ends up in work |
248 | | // |
249 | 0 | if ((ret = mbedtls_md_hmac_update(ctx, salt, slen)) != 0) { |
250 | 0 | goto cleanup; |
251 | 0 | } |
252 | | |
253 | 0 | if ((ret = mbedtls_md_hmac_update(ctx, counter, 4)) != 0) { |
254 | 0 | goto cleanup; |
255 | 0 | } |
256 | | |
257 | 0 | if ((ret = mbedtls_md_hmac_finish(ctx, work)) != 0) { |
258 | 0 | goto cleanup; |
259 | 0 | } |
260 | | |
261 | 0 | if ((ret = mbedtls_md_hmac_reset(ctx)) != 0) { |
262 | 0 | goto cleanup; |
263 | 0 | } |
264 | | |
265 | 0 | memcpy(md1, work, md_size); |
266 | |
|
267 | 0 | for (i = 1; i < iteration_count; i++) { |
268 | | // U2 ends up in md1 |
269 | | // |
270 | 0 | if ((ret = mbedtls_md_hmac_update(ctx, md1, md_size)) != 0) { |
271 | 0 | goto cleanup; |
272 | 0 | } |
273 | | |
274 | 0 | if ((ret = mbedtls_md_hmac_finish(ctx, md1)) != 0) { |
275 | 0 | goto cleanup; |
276 | 0 | } |
277 | | |
278 | 0 | if ((ret = mbedtls_md_hmac_reset(ctx)) != 0) { |
279 | 0 | goto cleanup; |
280 | 0 | } |
281 | | |
282 | | // U1 xor U2 |
283 | | // |
284 | 0 | mbedtls_xor(work, work, md1, md_size); |
285 | 0 | } |
286 | | |
287 | 0 | use_len = (key_length < md_size) ? key_length : md_size; |
288 | 0 | memcpy(out_p, work, use_len); |
289 | |
|
290 | 0 | key_length -= (uint32_t) use_len; |
291 | 0 | out_p += use_len; |
292 | |
|
293 | 0 | for (i = 4; i > 0; i--) { |
294 | 0 | if (++counter[i - 1] != 0) { |
295 | 0 | break; |
296 | 0 | } |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | 0 | cleanup: |
301 | | /* Zeroise buffers to clear sensitive data from memory. */ |
302 | 0 | mbedtls_platform_zeroize(work, MBEDTLS_MD_MAX_SIZE); |
303 | 0 | mbedtls_platform_zeroize(md1, MBEDTLS_MD_MAX_SIZE); |
304 | |
|
305 | 0 | return ret; |
306 | 0 | } |
307 | | |
308 | | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
309 | | int mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx, |
310 | | const unsigned char *password, |
311 | | size_t plen, const unsigned char *salt, size_t slen, |
312 | | unsigned int iteration_count, |
313 | | uint32_t key_length, unsigned char *output) |
314 | 0 | { |
315 | 0 | return pkcs5_pbkdf2_hmac(ctx, password, plen, salt, slen, iteration_count, |
316 | 0 | key_length, output); |
317 | 0 | } |
318 | | #endif |
319 | | #endif /* MBEDTLS_MD_C */ |
320 | | |
321 | | int mbedtls_pkcs5_pbkdf2_hmac_ext(mbedtls_md_type_t md_alg, |
322 | | const unsigned char *password, |
323 | | size_t plen, const unsigned char *salt, size_t slen, |
324 | | unsigned int iteration_count, |
325 | | uint32_t key_length, unsigned char *output) |
326 | 0 | { |
327 | 0 | #if defined(MBEDTLS_MD_C) |
328 | 0 | mbedtls_md_context_t md_ctx; |
329 | 0 | const mbedtls_md_info_t *md_info = NULL; |
330 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
331 | |
|
332 | 0 | md_info = mbedtls_md_info_from_type(md_alg); |
333 | 0 | if (md_info == NULL) { |
334 | 0 | return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE; |
335 | 0 | } |
336 | | |
337 | 0 | mbedtls_md_init(&md_ctx); |
338 | |
|
339 | 0 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) { |
340 | 0 | goto exit; |
341 | 0 | } |
342 | 0 | ret = pkcs5_pbkdf2_hmac(&md_ctx, password, plen, salt, slen, |
343 | 0 | iteration_count, key_length, output); |
344 | 0 | exit: |
345 | 0 | mbedtls_md_free(&md_ctx); |
346 | 0 | return ret; |
347 | | #else |
348 | | unsigned int i; |
349 | | unsigned char md1[PSA_HASH_MAX_SIZE]; |
350 | | unsigned char work[PSA_HASH_MAX_SIZE]; |
351 | | const unsigned char md_size = mbedtls_hash_info_get_size(md_alg); |
352 | | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
353 | | |
354 | | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
355 | | psa_status_t status_destruction = PSA_ERROR_CORRUPTION_DETECTED; |
356 | | size_t use_len, out_len; |
357 | | unsigned char *out_p = output; |
358 | | unsigned char counter[4]; |
359 | | mbedtls_svc_key_id_t psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT; |
360 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
361 | | const psa_algorithm_t alg = PSA_ALG_HMAC(mbedtls_hash_info_psa_from_md(md_alg)); |
362 | | const size_t out_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_HMAC, 0, alg); |
363 | | |
364 | | memset(counter, 0, sizeof(counter)); |
365 | | counter[3] = 1; |
366 | | |
367 | | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
368 | | psa_set_key_algorithm(&attributes, alg); |
369 | | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
370 | | |
371 | | if (key_length == 0) { |
372 | | return 0; |
373 | | } |
374 | | if ((status = psa_import_key(&attributes, |
375 | | password, plen, |
376 | | &psa_hmac_key)) != PSA_SUCCESS) { |
377 | | return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA; |
378 | | } |
379 | | |
380 | | #if UINT_MAX > 0xFFFFFFFF |
381 | | if (iteration_count > 0xFFFFFFFF) { |
382 | | return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA; |
383 | | } |
384 | | #endif |
385 | | |
386 | | while (key_length) { |
387 | | status = psa_mac_sign_setup(&operation, psa_hmac_key, |
388 | | PSA_ALG_HMAC(alg)); |
389 | | if (status != PSA_SUCCESS) { |
390 | | goto cleanup; |
391 | | } |
392 | | // U1 ends up in work |
393 | | if ((status = psa_mac_update(&operation, salt, slen)) != PSA_SUCCESS) { |
394 | | goto cleanup; |
395 | | } |
396 | | |
397 | | if ((status = psa_mac_update(&operation, counter, sizeof(counter))) != PSA_SUCCESS) { |
398 | | goto cleanup; |
399 | | } |
400 | | |
401 | | if ((status = psa_mac_sign_finish(&operation, work, out_size, &out_len)) |
402 | | != PSA_SUCCESS) { |
403 | | goto cleanup; |
404 | | } |
405 | | |
406 | | memcpy(md1, work, out_len); |
407 | | |
408 | | for (i = 1; i < iteration_count; i++) { |
409 | | // U2 ends up in md1 |
410 | | // |
411 | | status = psa_mac_sign_setup(&operation, psa_hmac_key, |
412 | | PSA_ALG_HMAC(alg)); |
413 | | if (status != PSA_SUCCESS) { |
414 | | goto cleanup; |
415 | | } |
416 | | if ((status = psa_mac_update(&operation, md1, md_size)) != PSA_SUCCESS) { |
417 | | goto cleanup; |
418 | | } |
419 | | if ((status = |
420 | | psa_mac_sign_finish(&operation, md1, out_size, &out_len)) != PSA_SUCCESS) { |
421 | | goto cleanup; |
422 | | } |
423 | | |
424 | | // U1 xor U2 |
425 | | // |
426 | | mbedtls_xor(work, work, md1, md_size); |
427 | | } |
428 | | |
429 | | use_len = (key_length < md_size) ? key_length : md_size; |
430 | | memcpy(out_p, work, use_len); |
431 | | |
432 | | key_length -= (uint32_t) use_len; |
433 | | out_p += use_len; |
434 | | |
435 | | for (i = 4; i > 0; i--) { |
436 | | if (++counter[i - 1] != 0) { |
437 | | break; |
438 | | } |
439 | | } |
440 | | } |
441 | | |
442 | | cleanup: |
443 | | /* Zeroise buffers to clear sensitive data from memory. */ |
444 | | mbedtls_platform_zeroize(work, PSA_HASH_MAX_SIZE); |
445 | | mbedtls_platform_zeroize(md1, PSA_HASH_MAX_SIZE); |
446 | | status_destruction = psa_destroy_key(psa_hmac_key); |
447 | | if (status == PSA_SUCCESS && status_destruction != PSA_SUCCESS) { |
448 | | status = status_destruction; |
449 | | } |
450 | | status_destruction = psa_mac_abort(&operation); |
451 | | if (status == PSA_SUCCESS && status_destruction != PSA_SUCCESS) { |
452 | | status = status_destruction; |
453 | | } |
454 | | |
455 | | return mbedtls_md_error_from_psa(status); |
456 | | #endif /* !MBEDTLS_MD_C */ |
457 | 0 | } |
458 | | |
459 | | #if defined(MBEDTLS_SELF_TEST) |
460 | | |
461 | | #if !defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA) |
462 | | int mbedtls_pkcs5_self_test(int verbose) |
463 | 0 | { |
464 | 0 | if (verbose != 0) { |
465 | 0 | mbedtls_printf(" PBKDF2 (SHA1): skipped\n\n"); |
466 | 0 | } |
467 | |
|
468 | 0 | return 0; |
469 | 0 | } |
470 | | #else |
471 | | |
472 | | #define MAX_TESTS 6 |
473 | | |
474 | | static const size_t plen_test_data[MAX_TESTS] = |
475 | | { 8, 8, 8, 24, 9 }; |
476 | | |
477 | | static const unsigned char password_test_data[MAX_TESTS][32] = |
478 | | { |
479 | | "password", |
480 | | "password", |
481 | | "password", |
482 | | "passwordPASSWORDpassword", |
483 | | "pass\0word", |
484 | | }; |
485 | | |
486 | | static const size_t slen_test_data[MAX_TESTS] = |
487 | | { 4, 4, 4, 36, 5 }; |
488 | | |
489 | | static const unsigned char salt_test_data[MAX_TESTS][40] = |
490 | | { |
491 | | "salt", |
492 | | "salt", |
493 | | "salt", |
494 | | "saltSALTsaltSALTsaltSALTsaltSALTsalt", |
495 | | "sa\0lt", |
496 | | }; |
497 | | |
498 | | static const uint32_t it_cnt_test_data[MAX_TESTS] = |
499 | | { 1, 2, 4096, 4096, 4096 }; |
500 | | |
501 | | static const uint32_t key_len_test_data[MAX_TESTS] = |
502 | | { 20, 20, 20, 25, 16 }; |
503 | | |
504 | | static const unsigned char result_key_test_data[MAX_TESTS][32] = |
505 | | { |
506 | | { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, |
507 | | 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, |
508 | | 0x2f, 0xe0, 0x37, 0xa6 }, |
509 | | { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, |
510 | | 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, |
511 | | 0xd8, 0xde, 0x89, 0x57 }, |
512 | | { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, |
513 | | 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, |
514 | | 0x65, 0xa4, 0x29, 0xc1 }, |
515 | | { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b, |
516 | | 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a, |
517 | | 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70, |
518 | | 0x38 }, |
519 | | { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, |
520 | | 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 }, |
521 | | }; |
522 | | |
523 | | int mbedtls_pkcs5_self_test(int verbose) |
524 | | { |
525 | | int ret, i; |
526 | | unsigned char key[64]; |
527 | | |
528 | | for (i = 0; i < MAX_TESTS; i++) { |
529 | | if (verbose != 0) { |
530 | | mbedtls_printf(" PBKDF2 (SHA1) #%d: ", i); |
531 | | } |
532 | | |
533 | | ret = mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA1, password_test_data[i], |
534 | | plen_test_data[i], salt_test_data[i], |
535 | | slen_test_data[i], it_cnt_test_data[i], |
536 | | key_len_test_data[i], key); |
537 | | if (ret != 0 || |
538 | | memcmp(result_key_test_data[i], key, key_len_test_data[i]) != 0) { |
539 | | if (verbose != 0) { |
540 | | mbedtls_printf("failed\n"); |
541 | | } |
542 | | |
543 | | ret = 1; |
544 | | goto exit; |
545 | | } |
546 | | |
547 | | if (verbose != 0) { |
548 | | mbedtls_printf("passed\n"); |
549 | | } |
550 | | } |
551 | | |
552 | | if (verbose != 0) { |
553 | | mbedtls_printf("\n"); |
554 | | } |
555 | | |
556 | | exit: |
557 | | return ret; |
558 | | } |
559 | | #endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA */ |
560 | | |
561 | | #endif /* MBEDTLS_SELF_TEST */ |
562 | | |
563 | | #endif /* MBEDTLS_PKCS5_C */ |