/src/openssl36/providers/implementations/kdfs/pbkdf2.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | /* clang-format off */ |
10 | | |
11 | | /* clang-format on */ |
12 | | |
13 | | /* |
14 | | * HMAC low level APIs are deprecated for public use, but still ok for internal |
15 | | * use. |
16 | | */ |
17 | | #include "internal/deprecated.h" |
18 | | |
19 | | #include <stdlib.h> |
20 | | #include <stdarg.h> |
21 | | #include <string.h> |
22 | | #include <openssl/hmac.h> |
23 | | #include <openssl/evp.h> |
24 | | #include <openssl/kdf.h> |
25 | | #include <openssl/core_names.h> |
26 | | #include <openssl/proverr.h> |
27 | | #include "internal/cryptlib.h" |
28 | | #include "internal/numbers.h" |
29 | | #include "crypto/evp.h" |
30 | | #include "prov/provider_ctx.h" |
31 | | #include "prov/providercommon.h" |
32 | | #include "prov/implementations.h" |
33 | | #include "prov/provider_util.h" |
34 | | #include "prov/securitycheck.h" |
35 | | |
36 | | /* Constants specified in SP800-132 */ |
37 | 6 | #define KDF_PBKDF2_MIN_KEY_LEN_BITS 112 |
38 | 115 | #define KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO 0xFFFFFFFF |
39 | 2 | #define KDF_PBKDF2_MIN_ITERATIONS 1000 |
40 | 6 | #define KDF_PBKDF2_MIN_SALT_LEN (128 / 8) |
41 | | |
42 | | static OSSL_FUNC_kdf_newctx_fn kdf_pbkdf2_new; |
43 | | static OSSL_FUNC_kdf_dupctx_fn kdf_pbkdf2_dup; |
44 | | static OSSL_FUNC_kdf_freectx_fn kdf_pbkdf2_free; |
45 | | static OSSL_FUNC_kdf_reset_fn kdf_pbkdf2_reset; |
46 | | static OSSL_FUNC_kdf_derive_fn kdf_pbkdf2_derive; |
47 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pbkdf2_settable_ctx_params; |
48 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pbkdf2_set_ctx_params; |
49 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pbkdf2_gettable_ctx_params; |
50 | | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pbkdf2_get_ctx_params; |
51 | | |
52 | | typedef struct { |
53 | | void *provctx; |
54 | | unsigned char *pass; |
55 | | size_t pass_len; |
56 | | unsigned char *salt; |
57 | | size_t salt_len; |
58 | | uint64_t iter; |
59 | | PROV_DIGEST digest; |
60 | | int lower_bound_checks; |
61 | | OSSL_FIPS_IND_DECLARE |
62 | | } KDF_PBKDF2; |
63 | | |
64 | | static int pbkdf2_derive(KDF_PBKDF2 *ctx, const char *pass, size_t passlen, |
65 | | const unsigned char *salt, int saltlen, uint64_t iter, |
66 | | const EVP_MD *digest, unsigned char *key, |
67 | | size_t keylen, int lower_bound_checks); |
68 | | |
69 | | static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx); |
70 | | |
71 | | static void *kdf_pbkdf2_new_no_init(void *provctx) |
72 | 286 | { |
73 | 286 | KDF_PBKDF2 *ctx; |
74 | | |
75 | 286 | if (!ossl_prov_is_running()) |
76 | 0 | return NULL; |
77 | | |
78 | 286 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
79 | 286 | if (ctx == NULL) |
80 | 0 | return NULL; |
81 | 286 | ctx->provctx = provctx; |
82 | 286 | OSSL_FIPS_IND_INIT(ctx); |
83 | 286 | return ctx; |
84 | 286 | } |
85 | | |
86 | | static void *kdf_pbkdf2_new(void *provctx) |
87 | 286 | { |
88 | 286 | KDF_PBKDF2 *ctx = kdf_pbkdf2_new_no_init(provctx); |
89 | | |
90 | 286 | if (ctx != NULL) |
91 | 286 | kdf_pbkdf2_init(ctx); |
92 | 286 | return ctx; |
93 | 286 | } |
94 | | |
95 | | static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx) |
96 | 286 | { |
97 | 286 | ossl_prov_digest_reset(&ctx->digest); |
98 | | #ifdef OPENSSL_PEDANTIC_ZEROIZATION |
99 | | OPENSSL_clear_free(ctx->salt, ctx->salt_len); |
100 | | #else |
101 | 286 | OPENSSL_free(ctx->salt); |
102 | 286 | #endif |
103 | 286 | OPENSSL_clear_free(ctx->pass, ctx->pass_len); |
104 | 286 | memset(ctx, 0, sizeof(*ctx)); |
105 | 286 | } |
106 | | |
107 | | static void kdf_pbkdf2_free(void *vctx) |
108 | 286 | { |
109 | 286 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; |
110 | | |
111 | 286 | if (ctx != NULL) { |
112 | 286 | kdf_pbkdf2_cleanup(ctx); |
113 | 286 | OPENSSL_free(ctx); |
114 | 286 | } |
115 | 286 | } |
116 | | |
117 | | static void kdf_pbkdf2_reset(void *vctx) |
118 | 0 | { |
119 | 0 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; |
120 | 0 | void *provctx = ctx->provctx; |
121 | |
|
122 | 0 | kdf_pbkdf2_cleanup(ctx); |
123 | 0 | ctx->provctx = provctx; |
124 | 0 | kdf_pbkdf2_init(ctx); |
125 | 0 | } |
126 | | |
127 | | static void *kdf_pbkdf2_dup(void *vctx) |
128 | 0 | { |
129 | 0 | const KDF_PBKDF2 *src = (const KDF_PBKDF2 *)vctx; |
130 | 0 | KDF_PBKDF2 *dest; |
131 | | |
132 | | /* We need a new PBKDF2 object but uninitialised since we're filling it */ |
133 | 0 | dest = kdf_pbkdf2_new_no_init(src->provctx); |
134 | 0 | if (dest != NULL) { |
135 | 0 | if (!ossl_prov_memdup(src->salt, src->salt_len, |
136 | 0 | &dest->salt, &dest->salt_len) |
137 | 0 | || !ossl_prov_memdup(src->pass, src->pass_len, |
138 | 0 | &dest->pass, &dest->pass_len) |
139 | 0 | || !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
140 | 0 | goto err; |
141 | 0 | dest->iter = src->iter; |
142 | 0 | dest->lower_bound_checks = src->lower_bound_checks; |
143 | 0 | OSSL_FIPS_IND_COPY(dest, src) |
144 | 0 | } |
145 | 0 | return dest; |
146 | | |
147 | 0 | err: |
148 | 0 | kdf_pbkdf2_free(dest); |
149 | 0 | return NULL; |
150 | 0 | } |
151 | | |
152 | | static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx) |
153 | 286 | { |
154 | 286 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
155 | 286 | OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx); |
156 | | |
157 | 286 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, |
158 | 286 | SN_sha1, 0); |
159 | 286 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx)) |
160 | | /* This is an error, but there is no way to indicate such directly */ |
161 | 0 | ossl_prov_digest_reset(&ctx->digest); |
162 | 286 | ctx->iter = PKCS5_DEFAULT_ITER; |
163 | | #ifdef FIPS_MODULE |
164 | | ctx->lower_bound_checks = 1; |
165 | | #else |
166 | 286 | ctx->lower_bound_checks = 0; |
167 | 286 | #endif |
168 | 286 | } |
169 | | |
170 | | static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen, |
171 | | const OSSL_PARAM *p) |
172 | 495 | { |
173 | 495 | OPENSSL_clear_free(*buffer, *buflen); |
174 | 495 | *buffer = NULL; |
175 | 495 | *buflen = 0; |
176 | | |
177 | 495 | if (p->data_size == 0) { |
178 | 153 | if ((*buffer = OPENSSL_malloc(1)) == NULL) |
179 | 0 | return 0; |
180 | 342 | } else if (p->data != NULL) { |
181 | 342 | if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen)) |
182 | 0 | return 0; |
183 | 342 | } |
184 | 495 | return 1; |
185 | 495 | } |
186 | | |
187 | | static int pbkdf2_lower_bound_check_passed(int saltlen, uint64_t iter, |
188 | | size_t keylen, int *error, |
189 | | const char **desc) |
190 | 6 | { |
191 | 6 | if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) { |
192 | 0 | *error = PROV_R_KEY_SIZE_TOO_SMALL; |
193 | 0 | if (desc != NULL) |
194 | 0 | *desc = "Key size"; |
195 | 0 | return 0; |
196 | 0 | } |
197 | 6 | if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) { |
198 | 4 | *error = PROV_R_INVALID_SALT_LENGTH; |
199 | 4 | if (desc != NULL) |
200 | 0 | *desc = "Salt size"; |
201 | 4 | return 0; |
202 | 4 | } |
203 | 2 | if (iter < KDF_PBKDF2_MIN_ITERATIONS) { |
204 | 1 | *error = PROV_R_INVALID_ITERATION_COUNT; |
205 | 1 | if (desc != NULL) |
206 | 0 | *desc = "Iteration count"; |
207 | 1 | return 0; |
208 | 1 | } |
209 | | |
210 | 1 | return 1; |
211 | 2 | } |
212 | | |
213 | | #ifdef FIPS_MODULE |
214 | | static int fips_lower_bound_check_passed(KDF_PBKDF2 *ctx, int saltlen, |
215 | | uint64_t iter, size_t keylen) |
216 | | { |
217 | | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
218 | | int error = 0; |
219 | | const char *desc = NULL; |
220 | | int approved = pbkdf2_lower_bound_check_passed(saltlen, iter, keylen, |
221 | | &error, &desc); |
222 | | |
223 | | if (!approved) { |
224 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, libctx, |
225 | | "PBKDF2", desc, |
226 | | ossl_fips_config_pbkdf2_lower_bound_check)) { |
227 | | ERR_raise(ERR_LIB_PROV, error); |
228 | | return 0; |
229 | | } |
230 | | } |
231 | | return 1; |
232 | | } |
233 | | #endif |
234 | | |
235 | | static int lower_bound_check_passed(KDF_PBKDF2 *ctx, int saltlen, uint64_t iter, |
236 | | size_t keylen, int lower_bound_checks) |
237 | 361 | { |
238 | | #ifdef FIPS_MODULE |
239 | | if (!fips_lower_bound_check_passed(ctx, saltlen, iter, keylen)) |
240 | | return 0; |
241 | | #else |
242 | 361 | if (lower_bound_checks) { |
243 | 12 | int error = 0; |
244 | 12 | int passed = pbkdf2_lower_bound_check_passed(saltlen, iter, keylen, |
245 | 12 | &error, NULL); |
246 | | |
247 | 12 | if (!passed) { |
248 | 10 | ERR_raise(ERR_LIB_PROV, error); |
249 | 10 | return 0; |
250 | 10 | } |
251 | 349 | } else if (iter < 1) { |
252 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT); |
253 | 0 | return 0; |
254 | 0 | } |
255 | 351 | #endif |
256 | | |
257 | 351 | return 1; |
258 | 361 | } |
259 | | |
260 | | static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, size_t keylen, |
261 | | const OSSL_PARAM params[]) |
262 | 229 | { |
263 | 229 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; |
264 | 229 | const EVP_MD *md; |
265 | | |
266 | 229 | if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params)) |
267 | 0 | return 0; |
268 | | |
269 | 229 | if (ctx->pass == NULL) { |
270 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); |
271 | 0 | return 0; |
272 | 0 | } |
273 | | |
274 | 229 | if (ctx->salt == NULL) { |
275 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT); |
276 | 0 | return 0; |
277 | 0 | } |
278 | | |
279 | 229 | md = ossl_prov_digest_md(&ctx->digest); |
280 | 229 | return pbkdf2_derive(ctx, (char *)ctx->pass, ctx->pass_len, |
281 | 229 | ctx->salt, (int)ctx->salt_len, ctx->iter, |
282 | 229 | md, key, keylen, ctx->lower_bound_checks); |
283 | 229 | } |
284 | | |
285 | | /* clang-format off */ |
286 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
287 | | #ifndef pbkdf2_set_ctx_params_list |
288 | | static const OSSL_PARAM pbkdf2_set_ctx_params_list[] = { |
289 | | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), |
290 | | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), |
291 | | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0), |
292 | | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0), |
293 | | OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL), |
294 | | OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS5, NULL), |
295 | | OSSL_PARAM_END |
296 | | }; |
297 | | #endif |
298 | | |
299 | | #ifndef pbkdf2_set_ctx_params_st |
300 | | struct pbkdf2_set_ctx_params_st { |
301 | | OSSL_PARAM *digest; |
302 | | OSSL_PARAM *engine; |
303 | | OSSL_PARAM *iter; |
304 | | OSSL_PARAM *pkcs5; |
305 | | OSSL_PARAM *propq; |
306 | | OSSL_PARAM *pw; |
307 | | OSSL_PARAM *salt; |
308 | | }; |
309 | | #endif |
310 | | |
311 | | #ifndef pbkdf2_set_ctx_params_decoder |
312 | | static int pbkdf2_set_ctx_params_decoder |
313 | | (const OSSL_PARAM *p, struct pbkdf2_set_ctx_params_st *r) |
314 | 141 | { |
315 | 141 | const char *s; |
316 | | |
317 | 141 | memset(r, 0, sizeof(*r)); |
318 | 141 | if (p != NULL) |
319 | 518 | for (; (s = p->key) != NULL; p++) |
320 | 444 | switch(s[0]) { |
321 | 0 | default: |
322 | 0 | break; |
323 | 74 | case 'd': |
324 | 74 | if (ossl_likely(strcmp("igest", s + 1) == 0)) { |
325 | | /* OSSL_KDF_PARAM_DIGEST */ |
326 | 74 | if (ossl_unlikely(r->digest != NULL)) { |
327 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
328 | 0 | "param %s is repeated", s); |
329 | 0 | return 0; |
330 | 0 | } |
331 | 74 | r->digest = (OSSL_PARAM *)p; |
332 | 74 | } |
333 | 74 | break; |
334 | 74 | case 'e': |
335 | 0 | if (ossl_likely(strcmp("ngine", s + 1) == 0)) { |
336 | | /* OSSL_ALG_PARAM_ENGINE */ |
337 | 0 | if (ossl_unlikely(r->engine != NULL)) { |
338 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
339 | 0 | "param %s is repeated", s); |
340 | 0 | return 0; |
341 | 0 | } |
342 | 0 | r->engine = (OSSL_PARAM *)p; |
343 | 0 | } |
344 | 0 | break; |
345 | 74 | case 'i': |
346 | 74 | if (ossl_likely(strcmp("ter", s + 1) == 0)) { |
347 | | /* OSSL_KDF_PARAM_ITER */ |
348 | 74 | if (ossl_unlikely(r->iter != NULL)) { |
349 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
350 | 0 | "param %s is repeated", s); |
351 | 0 | return 0; |
352 | 0 | } |
353 | 74 | r->iter = (OSSL_PARAM *)p; |
354 | 74 | } |
355 | 74 | break; |
356 | 222 | case 'p': |
357 | 222 | switch(s[1]) { |
358 | 0 | default: |
359 | 0 | break; |
360 | 74 | case 'a': |
361 | 74 | if (ossl_likely(strcmp("ss", s + 2) == 0)) { |
362 | | /* OSSL_KDF_PARAM_PASSWORD */ |
363 | 74 | if (ossl_unlikely(r->pw != NULL)) { |
364 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
365 | 0 | "param %s is repeated", s); |
366 | 0 | return 0; |
367 | 0 | } |
368 | 74 | r->pw = (OSSL_PARAM *)p; |
369 | 74 | } |
370 | 74 | break; |
371 | 74 | case 'k': |
372 | 74 | if (ossl_likely(strcmp("cs5", s + 2) == 0)) { |
373 | | /* OSSL_KDF_PARAM_PKCS5 */ |
374 | 74 | if (ossl_unlikely(r->pkcs5 != NULL)) { |
375 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
376 | 0 | "param %s is repeated", s); |
377 | 0 | return 0; |
378 | 0 | } |
379 | 74 | r->pkcs5 = (OSSL_PARAM *)p; |
380 | 74 | } |
381 | 74 | break; |
382 | 74 | case 'r': |
383 | 74 | if (ossl_likely(strcmp("operties", s + 2) == 0)) { |
384 | | /* OSSL_KDF_PARAM_PROPERTIES */ |
385 | 74 | if (ossl_unlikely(r->propq != NULL)) { |
386 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
387 | 0 | "param %s is repeated", s); |
388 | 0 | return 0; |
389 | 0 | } |
390 | 74 | r->propq = (OSSL_PARAM *)p; |
391 | 74 | } |
392 | 222 | } |
393 | 222 | break; |
394 | 222 | case 's': |
395 | 74 | if (ossl_likely(strcmp("alt", s + 1) == 0)) { |
396 | | /* OSSL_KDF_PARAM_SALT */ |
397 | 74 | if (ossl_unlikely(r->salt != NULL)) { |
398 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
399 | 0 | "param %s is repeated", s); |
400 | 0 | return 0; |
401 | 0 | } |
402 | 74 | r->salt = (OSSL_PARAM *)p; |
403 | 74 | } |
404 | 444 | } |
405 | 141 | return 1; |
406 | 141 | } |
407 | | #endif |
408 | | /* End of machine generated */ |
409 | | /* clang-format on */ |
410 | | |
411 | | static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
412 | 141 | { |
413 | 141 | struct pbkdf2_set_ctx_params_st p; |
414 | 141 | KDF_PBKDF2 *ctx = vctx; |
415 | 141 | OSSL_LIB_CTX *provctx; |
416 | 141 | int pkcs5; |
417 | 141 | uint64_t iter; |
418 | 141 | const EVP_MD *md; |
419 | | |
420 | 141 | if (ctx == NULL || !pbkdf2_set_ctx_params_decoder(params, &p)) |
421 | 0 | return 0; |
422 | | |
423 | 141 | provctx = PROV_LIBCTX_OF(ctx->provctx); |
424 | | |
425 | 141 | if (p.digest != NULL) { |
426 | 74 | if (!ossl_prov_digest_load(&ctx->digest, p.digest, |
427 | 74 | p.propq, p.engine, provctx)) |
428 | 1 | return 0; |
429 | 73 | md = ossl_prov_digest_md(&ctx->digest); |
430 | 73 | if (EVP_MD_xof(md)) { |
431 | 1 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
432 | 1 | return 0; |
433 | 1 | } |
434 | 73 | } |
435 | | |
436 | 139 | if (p.pkcs5 != NULL) { |
437 | 72 | if (!OSSL_PARAM_get_int(p.pkcs5, &pkcs5)) |
438 | 0 | return 0; |
439 | 72 | ctx->lower_bound_checks = pkcs5 == 0; |
440 | | #ifdef FIPS_MODULE |
441 | | ossl_FIPS_IND_set_settable(OSSL_FIPS_IND_GET(ctx), |
442 | | OSSL_FIPS_IND_SETTABLE0, |
443 | | ctx->lower_bound_checks); |
444 | | #endif |
445 | 72 | } |
446 | | |
447 | 139 | if (p.pw != NULL && !pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p.pw)) |
448 | 0 | return 0; |
449 | | |
450 | 139 | if (p.salt != NULL) { |
451 | 72 | if (!lower_bound_check_passed(ctx, (int)p.salt->data_size, UINT64_MAX, SIZE_MAX, |
452 | 72 | ctx->lower_bound_checks)) |
453 | 4 | return 0; |
454 | 68 | if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len, p.salt)) |
455 | 0 | return 0; |
456 | 68 | } |
457 | | |
458 | 135 | if (p.iter != NULL) { |
459 | 68 | if (!OSSL_PARAM_get_uint64(p.iter, &iter)) |
460 | 0 | return 0; |
461 | 68 | if (!lower_bound_check_passed(ctx, INT_MAX, iter, SIZE_MAX, |
462 | 68 | ctx->lower_bound_checks)) |
463 | 1 | return 0; |
464 | 67 | ctx->iter = iter; |
465 | 67 | } |
466 | 134 | return 1; |
467 | 135 | } |
468 | | |
469 | | static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx, |
470 | | ossl_unused void *p_ctx) |
471 | 286 | { |
472 | 286 | return pbkdf2_set_ctx_params_list; |
473 | 286 | } |
474 | | |
475 | | /* clang-format off */ |
476 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
477 | | #ifndef pbkdf2_get_ctx_params_list |
478 | | static const OSSL_PARAM pbkdf2_get_ctx_params_list[] = { |
479 | | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
480 | | # if defined(FIPS_MODULE) |
481 | | OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR, NULL), |
482 | | # endif |
483 | | OSSL_PARAM_END |
484 | | }; |
485 | | #endif |
486 | | |
487 | | #ifndef pbkdf2_get_ctx_params_st |
488 | | struct pbkdf2_get_ctx_params_st { |
489 | | # if defined(FIPS_MODULE) |
490 | | OSSL_PARAM *ind; |
491 | | # endif |
492 | | OSSL_PARAM *size; |
493 | | }; |
494 | | #endif |
495 | | |
496 | | #ifndef pbkdf2_get_ctx_params_decoder |
497 | | static int pbkdf2_get_ctx_params_decoder |
498 | | (const OSSL_PARAM *p, struct pbkdf2_get_ctx_params_st *r) |
499 | 0 | { |
500 | 0 | const char *s; |
501 | |
|
502 | 0 | memset(r, 0, sizeof(*r)); |
503 | 0 | if (p != NULL) |
504 | 0 | for (; (s = p->key) != NULL; p++) |
505 | 0 | switch(s[0]) { |
506 | 0 | default: |
507 | 0 | break; |
508 | 0 | case 'f': |
509 | | # if defined(FIPS_MODULE) |
510 | | if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) { |
511 | | /* OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR */ |
512 | | if (ossl_unlikely(r->ind != NULL)) { |
513 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
514 | | "param %s is repeated", s); |
515 | | return 0; |
516 | | } |
517 | | r->ind = (OSSL_PARAM *)p; |
518 | | } |
519 | | # endif |
520 | 0 | break; |
521 | 0 | case 's': |
522 | 0 | if (ossl_likely(strcmp("ize", s + 1) == 0)) { |
523 | | /* OSSL_KDF_PARAM_SIZE */ |
524 | 0 | if (ossl_unlikely(r->size != NULL)) { |
525 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
526 | 0 | "param %s is repeated", s); |
527 | 0 | return 0; |
528 | 0 | } |
529 | 0 | r->size = (OSSL_PARAM *)p; |
530 | 0 | } |
531 | 0 | } |
532 | 0 | return 1; |
533 | 0 | } |
534 | | #endif |
535 | | /* End of machine generated */ |
536 | | /* clang-format on */ |
537 | | |
538 | | static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
539 | 0 | { |
540 | 0 | KDF_PBKDF2 *ctx = vctx; |
541 | 0 | struct pbkdf2_get_ctx_params_st p; |
542 | |
|
543 | 0 | if (ctx == NULL || !pbkdf2_get_ctx_params_decoder(params, &p)) |
544 | 0 | return 0; |
545 | | |
546 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX)) |
547 | 0 | return 0; |
548 | | |
549 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind)) |
550 | 0 | return 0; |
551 | 0 | return 1; |
552 | 0 | } |
553 | | |
554 | | static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *ctx, |
555 | | ossl_unused void *p_ctx) |
556 | 0 | { |
557 | 0 | return pbkdf2_get_ctx_params_list; |
558 | 0 | } |
559 | | |
560 | | const OSSL_DISPATCH ossl_kdf_pbkdf2_functions[] = { |
561 | | { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_pbkdf2_new }, |
562 | | { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_pbkdf2_dup }, |
563 | | { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_pbkdf2_free }, |
564 | | { OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_pbkdf2_reset }, |
565 | | { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_pbkdf2_derive }, |
566 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
567 | | (void (*)(void))kdf_pbkdf2_settable_ctx_params }, |
568 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))kdf_pbkdf2_set_ctx_params }, |
569 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
570 | | (void (*)(void))kdf_pbkdf2_gettable_ctx_params }, |
571 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))kdf_pbkdf2_get_ctx_params }, |
572 | | OSSL_DISPATCH_END |
573 | | }; |
574 | | |
575 | | /* |
576 | | * This is an implementation of PKCS#5 v2.0 password based encryption key |
577 | | * derivation function PBKDF2. SHA1 version verified against test vectors |
578 | | * posted by Peter Gutmann to the PKCS-TNG mailing list. |
579 | | * |
580 | | * The constraints specified by SP800-132 have been added i.e. |
581 | | * - Check the range of the key length. |
582 | | * - Minimum iteration count of 1000. |
583 | | * - Randomly-generated portion of the salt shall be at least 128 bits. |
584 | | */ |
585 | | static int pbkdf2_derive(KDF_PBKDF2 *ctx, const char *pass, size_t passlen, |
586 | | const unsigned char *salt, int saltlen, uint64_t iter, |
587 | | const EVP_MD *digest, unsigned char *key, |
588 | | size_t keylen, int lower_bound_checks) |
589 | 117 | { |
590 | 117 | int ret = 0; |
591 | 117 | unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4]; |
592 | 117 | int cplen, k, tkeylen, mdlen; |
593 | 117 | uint64_t j; |
594 | 117 | unsigned long i = 1; |
595 | 117 | HMAC_CTX *hctx_tpl = NULL, *hctx = NULL; |
596 | | |
597 | 117 | mdlen = EVP_MD_get_size(digest); |
598 | 117 | if (mdlen <= 0) |
599 | 2 | return 0; |
600 | | |
601 | | /* |
602 | | * This check should always be done because keylen / mdlen >= (2^32 - 1) |
603 | | * results in an overflow of the loop counter 'i'. |
604 | | */ |
605 | 115 | if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) { |
606 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
607 | 0 | return 0; |
608 | 0 | } |
609 | | |
610 | 115 | if (!lower_bound_check_passed(ctx, saltlen, iter, keylen, lower_bound_checks)) |
611 | 0 | return 0; |
612 | | |
613 | 115 | hctx_tpl = HMAC_CTX_new(); |
614 | 115 | if (hctx_tpl == NULL) |
615 | 0 | return 0; |
616 | 115 | p = key; |
617 | 115 | tkeylen = (int)keylen; |
618 | 115 | if (!HMAC_Init_ex(hctx_tpl, pass, (int)passlen, digest, NULL)) |
619 | 0 | goto err; |
620 | 115 | hctx = HMAC_CTX_new(); |
621 | 115 | if (hctx == NULL) |
622 | 0 | goto err; |
623 | 272 | while (tkeylen) { |
624 | 157 | if (tkeylen > mdlen) |
625 | 42 | cplen = mdlen; |
626 | 115 | else |
627 | 115 | cplen = tkeylen; |
628 | | /* |
629 | | * We are unlikely to ever use more than 256 blocks (5120 bits!) but |
630 | | * just in case... |
631 | | */ |
632 | 157 | itmp[0] = (unsigned char)((i >> 24) & 0xff); |
633 | 157 | itmp[1] = (unsigned char)((i >> 16) & 0xff); |
634 | 157 | itmp[2] = (unsigned char)((i >> 8) & 0xff); |
635 | 157 | itmp[3] = (unsigned char)(i & 0xff); |
636 | 157 | if (!HMAC_CTX_copy(hctx, hctx_tpl)) |
637 | 0 | goto err; |
638 | 157 | if (!HMAC_Update(hctx, salt, saltlen) |
639 | 157 | || !HMAC_Update(hctx, itmp, 4) |
640 | 157 | || !HMAC_Final(hctx, digtmp, NULL)) |
641 | 0 | goto err; |
642 | 157 | memcpy(p, digtmp, cplen); |
643 | 157 | for (j = 1; j < iter; j++) { |
644 | 0 | if (!HMAC_CTX_copy(hctx, hctx_tpl)) |
645 | 0 | goto err; |
646 | 0 | if (!HMAC_Update(hctx, digtmp, mdlen) |
647 | 0 | || !HMAC_Final(hctx, digtmp, NULL)) |
648 | 0 | goto err; |
649 | 0 | for (k = 0; k < cplen; k++) |
650 | 0 | p[k] ^= digtmp[k]; |
651 | 0 | } |
652 | 157 | tkeylen -= cplen; |
653 | 157 | i++; |
654 | 157 | p += cplen; |
655 | 157 | } |
656 | 115 | ret = 1; |
657 | | |
658 | 115 | err: |
659 | 115 | HMAC_CTX_free(hctx); |
660 | 115 | HMAC_CTX_free(hctx_tpl); |
661 | 115 | return ret; |
662 | 115 | } |