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