/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 | 25 | #define KDF_PBKDF2_MIN_KEY_LEN_BITS 112 |
36 | 55 | #define KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO 0xFFFFFFFF |
37 | 2 | #define KDF_PBKDF2_MIN_ITERATIONS 1000 |
38 | 25 | #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 | 236 | { |
71 | 236 | KDF_PBKDF2 *ctx; |
72 | | |
73 | 236 | if (!ossl_prov_is_running()) |
74 | 0 | return NULL; |
75 | | |
76 | 236 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
77 | 236 | if (ctx == NULL) |
78 | 0 | return NULL; |
79 | 236 | ctx->provctx = provctx; |
80 | 236 | OSSL_FIPS_IND_INIT(ctx); |
81 | 236 | return ctx; |
82 | 236 | } |
83 | | |
84 | | static void *kdf_pbkdf2_new(void *provctx) |
85 | 236 | { |
86 | 236 | KDF_PBKDF2 *ctx = kdf_pbkdf2_new_no_init(provctx); |
87 | | |
88 | 236 | if (ctx != NULL) |
89 | 236 | kdf_pbkdf2_init(ctx); |
90 | 236 | return ctx; |
91 | 236 | } |
92 | | |
93 | | static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx) |
94 | 236 | { |
95 | 236 | ossl_prov_digest_reset(&ctx->digest); |
96 | | #ifdef OPENSSL_PEDANTIC_ZEROIZATION |
97 | | OPENSSL_clear_free(ctx->salt, ctx->salt_len); |
98 | | #else |
99 | 236 | OPENSSL_free(ctx->salt); |
100 | 236 | #endif |
101 | 236 | OPENSSL_clear_free(ctx->pass, ctx->pass_len); |
102 | 236 | memset(ctx, 0, sizeof(*ctx)); |
103 | 236 | } |
104 | | |
105 | | static void kdf_pbkdf2_free(void *vctx) |
106 | 236 | { |
107 | 236 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; |
108 | | |
109 | 236 | if (ctx != NULL) { |
110 | 236 | kdf_pbkdf2_cleanup(ctx); |
111 | 236 | OPENSSL_free(ctx); |
112 | 236 | } |
113 | 236 | } |
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 | 236 | { |
152 | 236 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
153 | 236 | OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx); |
154 | | |
155 | 236 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, |
156 | 236 | SN_sha1, 0); |
157 | 236 | 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 | 236 | ctx->iter = PKCS5_DEFAULT_ITER; |
161 | | #ifdef FIPS_MODULE |
162 | | ctx->lower_bound_checks = 1; |
163 | | #else |
164 | 236 | ctx->lower_bound_checks = 0; |
165 | 236 | #endif |
166 | 236 | } |
167 | | |
168 | | static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen, |
169 | | const OSSL_PARAM *p) |
170 | 382 | { |
171 | 382 | OPENSSL_clear_free(*buffer, *buflen); |
172 | 382 | *buffer = NULL; |
173 | 382 | *buflen = 0; |
174 | | |
175 | 382 | if (p->data_size == 0) { |
176 | 119 | if ((*buffer = OPENSSL_malloc(1)) == NULL) |
177 | 0 | return 0; |
178 | 263 | } else if (p->data != NULL) { |
179 | 263 | if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen)) |
180 | 0 | return 0; |
181 | 263 | } |
182 | 382 | return 1; |
183 | 382 | } |
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 | 25 | { |
189 | 25 | 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 | 25 | if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) { |
196 | 23 | *error = PROV_R_INVALID_SALT_LENGTH; |
197 | 23 | if (desc != NULL) |
198 | 0 | *desc = "Salt size"; |
199 | 23 | return 0; |
200 | 23 | } |
201 | 2 | if (iter < KDF_PBKDF2_MIN_ITERATIONS) { |
202 | 1 | *error = PROV_R_INVALID_ITERATION_COUNT; |
203 | 1 | if (desc != NULL) |
204 | 0 | *desc = "Iteration count"; |
205 | 1 | return 0; |
206 | 1 | } |
207 | | |
208 | 1 | return 1; |
209 | 2 | } |
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 | 192 | { |
236 | | #ifdef FIPS_MODULE |
237 | | if (!fips_lower_bound_check_passed(ctx, saltlen, iter, keylen)) |
238 | | return 0; |
239 | | #else |
240 | 192 | if (lower_bound_checks) { |
241 | 25 | int error = 0; |
242 | 25 | int passed = pbkdf2_lower_bound_check_passed(saltlen, iter, keylen, |
243 | 25 | &error, NULL); |
244 | | |
245 | 25 | if (!passed) { |
246 | 24 | ERR_raise(ERR_LIB_PROV, error); |
247 | 24 | return 0; |
248 | 24 | } |
249 | 167 | } else if (iter < 1) { |
250 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT); |
251 | 0 | return 0; |
252 | 0 | } |
253 | 168 | #endif |
254 | | |
255 | 168 | return 1; |
256 | 192 | } |
257 | | |
258 | | static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, size_t keylen, |
259 | | const OSSL_PARAM params[]) |
260 | 166 | { |
261 | 166 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; |
262 | 166 | const EVP_MD *md; |
263 | | |
264 | 166 | if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params)) |
265 | 0 | return 0; |
266 | | |
267 | 166 | if (ctx->pass == NULL) { |
268 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); |
269 | 0 | return 0; |
270 | 0 | } |
271 | | |
272 | 166 | if (ctx->salt == NULL) { |
273 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT); |
274 | 0 | return 0; |
275 | 0 | } |
276 | | |
277 | 166 | md = ossl_prov_digest_md(&ctx->digest); |
278 | 166 | return pbkdf2_derive(ctx, (char *)ctx->pass, ctx->pass_len, |
279 | 166 | ctx->salt, (int)ctx->salt_len, ctx->iter, |
280 | 166 | md, key, keylen, ctx->lower_bound_checks); |
281 | 166 | } |
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 | 147 | { |
312 | 147 | const char *s; |
313 | | |
314 | 147 | memset(r, 0, sizeof(*r)); |
315 | 147 | if (p != NULL) |
316 | 637 | for (; (s = p->key) != NULL; p++) |
317 | 546 | switch(s[0]) { |
318 | 0 | default: |
319 | 0 | break; |
320 | 91 | case 'd': |
321 | 91 | if (ossl_likely(strcmp("igest", s + 1) == 0)) { |
322 | 91 | if (ossl_unlikely(r->digest != NULL)) { |
323 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
324 | 0 | "param %s is repeated", s); |
325 | 0 | return 0; |
326 | 0 | } |
327 | 91 | r->digest = (OSSL_PARAM *)p; |
328 | 91 | } |
329 | 91 | break; |
330 | 91 | case 'e': |
331 | 0 | if (ossl_likely(strcmp("ngine", s + 1) == 0)) { |
332 | 0 | if (ossl_unlikely(r->engine != NULL)) { |
333 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
334 | 0 | "param %s is repeated", s); |
335 | 0 | return 0; |
336 | 0 | } |
337 | 0 | r->engine = (OSSL_PARAM *)p; |
338 | 0 | } |
339 | 0 | break; |
340 | 91 | case 'i': |
341 | 91 | if (ossl_likely(strcmp("ter", s + 1) == 0)) { |
342 | 91 | if (ossl_unlikely(r->iter != NULL)) { |
343 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
344 | 0 | "param %s is repeated", s); |
345 | 0 | return 0; |
346 | 0 | } |
347 | 91 | r->iter = (OSSL_PARAM *)p; |
348 | 91 | } |
349 | 91 | break; |
350 | 273 | case 'p': |
351 | 273 | switch(s[1]) { |
352 | 0 | default: |
353 | 0 | break; |
354 | 91 | case 'a': |
355 | 91 | if (ossl_likely(strcmp("ss", s + 2) == 0)) { |
356 | 91 | if (ossl_unlikely(r->pw != NULL)) { |
357 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
358 | 0 | "param %s is repeated", s); |
359 | 0 | return 0; |
360 | 0 | } |
361 | 91 | r->pw = (OSSL_PARAM *)p; |
362 | 91 | } |
363 | 91 | break; |
364 | 91 | case 'k': |
365 | 91 | if (ossl_likely(strcmp("cs5", s + 2) == 0)) { |
366 | 91 | if (ossl_unlikely(r->pkcs5 != NULL)) { |
367 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
368 | 0 | "param %s is repeated", s); |
369 | 0 | return 0; |
370 | 0 | } |
371 | 91 | r->pkcs5 = (OSSL_PARAM *)p; |
372 | 91 | } |
373 | 91 | break; |
374 | 91 | case 'r': |
375 | 91 | if (ossl_likely(strcmp("operties", s + 2) == 0)) { |
376 | 91 | if (ossl_unlikely(r->propq != NULL)) { |
377 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
378 | 0 | "param %s is repeated", s); |
379 | 0 | return 0; |
380 | 0 | } |
381 | 91 | r->propq = (OSSL_PARAM *)p; |
382 | 91 | } |
383 | 273 | } |
384 | 273 | break; |
385 | 273 | case 's': |
386 | 91 | if (ossl_likely(strcmp("alt", s + 1) == 0)) { |
387 | 91 | if (ossl_unlikely(r->salt != NULL)) { |
388 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
389 | 0 | "param %s is repeated", s); |
390 | 0 | return 0; |
391 | 0 | } |
392 | 91 | r->salt = (OSSL_PARAM *)p; |
393 | 91 | } |
394 | 546 | } |
395 | 147 | return 1; |
396 | 147 | } |
397 | | #endif |
398 | | /* End of machine generated */ |
399 | | |
400 | | static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
401 | 147 | { |
402 | 147 | struct pbkdf2_set_ctx_params_st p; |
403 | 147 | KDF_PBKDF2 *ctx = vctx; |
404 | 147 | OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx); |
405 | 147 | int pkcs5; |
406 | 147 | uint64_t iter; |
407 | 147 | const EVP_MD *md; |
408 | | |
409 | 147 | if (!pbkdf2_set_ctx_params_decoder(params, &p)) |
410 | 0 | return 0; |
411 | | |
412 | 147 | if (p.digest != NULL) { |
413 | 91 | if (!ossl_prov_digest_load(&ctx->digest, p.digest, |
414 | 91 | p.propq, p.engine, provctx)) |
415 | 10 | return 0; |
416 | 81 | md = ossl_prov_digest_md(&ctx->digest); |
417 | 81 | if (EVP_MD_xof(md)) { |
418 | 1 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
419 | 1 | return 0; |
420 | 1 | } |
421 | 81 | } |
422 | | |
423 | 136 | if (p.pkcs5 != NULL) { |
424 | 80 | if (!OSSL_PARAM_get_int(p.pkcs5, &pkcs5)) |
425 | 0 | return 0; |
426 | 80 | ctx->lower_bound_checks = pkcs5 == 0; |
427 | | #ifdef FIPS_MODULE |
428 | | ossl_FIPS_IND_set_settable(OSSL_FIPS_IND_GET(ctx), |
429 | | OSSL_FIPS_IND_SETTABLE0, |
430 | | ctx->lower_bound_checks); |
431 | | #endif |
432 | 80 | } |
433 | | |
434 | 136 | if (p.pw != NULL && !pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p.pw)) |
435 | 0 | return 0; |
436 | | |
437 | 136 | if (p.salt != NULL) { |
438 | 80 | if (!lower_bound_check_passed(ctx, (int)p.salt->data_size, UINT64_MAX, SIZE_MAX, |
439 | 80 | ctx->lower_bound_checks)) |
440 | 23 | return 0; |
441 | 57 | if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len, p.salt)) |
442 | 0 | return 0; |
443 | 57 | } |
444 | | |
445 | 113 | if (p.iter != NULL) { |
446 | 57 | if (!OSSL_PARAM_get_uint64(p.iter, &iter)) |
447 | 0 | return 0; |
448 | 57 | if (!lower_bound_check_passed(ctx, INT_MAX, iter, SIZE_MAX, |
449 | 57 | ctx->lower_bound_checks)) |
450 | 1 | return 0; |
451 | 56 | ctx->iter = iter; |
452 | 56 | } |
453 | 112 | return 1; |
454 | 113 | } |
455 | | |
456 | | static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx, |
457 | | ossl_unused void *p_ctx) |
458 | 236 | { |
459 | 236 | return pbkdf2_set_ctx_params_list; |
460 | 236 | } |
461 | | |
462 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
463 | | #ifndef pbkdf2_get_ctx_params_list |
464 | | static const OSSL_PARAM pbkdf2_get_ctx_params_list[] = { |
465 | | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
466 | | # if defined(FIPS_MODULE) |
467 | | OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR, NULL), |
468 | | # endif |
469 | | OSSL_PARAM_END |
470 | | }; |
471 | | #endif |
472 | | |
473 | | #ifndef pbkdf2_get_ctx_params_st |
474 | | struct pbkdf2_get_ctx_params_st { |
475 | | # if defined(FIPS_MODULE) |
476 | | OSSL_PARAM *ind; |
477 | | # endif |
478 | | OSSL_PARAM *size; |
479 | | }; |
480 | | #endif |
481 | | |
482 | | #ifndef pbkdf2_get_ctx_params_decoder |
483 | | static int pbkdf2_get_ctx_params_decoder |
484 | | (const OSSL_PARAM *p, struct pbkdf2_get_ctx_params_st *r) |
485 | 0 | { |
486 | 0 | const char *s; |
487 | |
|
488 | 0 | memset(r, 0, sizeof(*r)); |
489 | 0 | if (p != NULL) |
490 | 0 | for (; (s = p->key) != NULL; p++) |
491 | 0 | switch(s[0]) { |
492 | 0 | default: |
493 | 0 | break; |
494 | 0 | case 'f': |
495 | | # if defined(FIPS_MODULE) |
496 | | if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) { |
497 | | if (ossl_unlikely(r->ind != NULL)) { |
498 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
499 | | "param %s is repeated", s); |
500 | | return 0; |
501 | | } |
502 | | r->ind = (OSSL_PARAM *)p; |
503 | | } |
504 | | # endif |
505 | 0 | break; |
506 | 0 | case 's': |
507 | 0 | if (ossl_likely(strcmp("ize", s + 1) == 0)) { |
508 | 0 | if (ossl_unlikely(r->size != NULL)) { |
509 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
510 | 0 | "param %s is repeated", s); |
511 | 0 | return 0; |
512 | 0 | } |
513 | 0 | r->size = (OSSL_PARAM *)p; |
514 | 0 | } |
515 | 0 | } |
516 | 0 | return 1; |
517 | 0 | } |
518 | | #endif |
519 | | /* End of machine generated */ |
520 | | |
521 | | static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
522 | 0 | { |
523 | 0 | KDF_PBKDF2 *ctx = vctx; |
524 | 0 | struct pbkdf2_get_ctx_params_st p; |
525 | |
|
526 | 0 | if (ctx == NULL || !pbkdf2_get_ctx_params_decoder(params, &p)) |
527 | 0 | return 0; |
528 | | |
529 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX)) |
530 | 0 | return 0; |
531 | | |
532 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind)) |
533 | 0 | return 0; |
534 | 0 | return 1; |
535 | 0 | } |
536 | | |
537 | | static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *ctx, |
538 | | ossl_unused void *p_ctx) |
539 | 0 | { |
540 | 0 | return pbkdf2_get_ctx_params_list; |
541 | 0 | } |
542 | | |
543 | | const OSSL_DISPATCH ossl_kdf_pbkdf2_functions[] = { |
544 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new }, |
545 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pbkdf2_dup }, |
546 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf2_free }, |
547 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf2_reset }, |
548 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf2_derive }, |
549 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
550 | | (void(*)(void))kdf_pbkdf2_settable_ctx_params }, |
551 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_set_ctx_params }, |
552 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
553 | | (void(*)(void))kdf_pbkdf2_gettable_ctx_params }, |
554 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_get_ctx_params }, |
555 | | OSSL_DISPATCH_END |
556 | | }; |
557 | | |
558 | | /* |
559 | | * This is an implementation of PKCS#5 v2.0 password based encryption key |
560 | | * derivation function PBKDF2. SHA1 version verified against test vectors |
561 | | * posted by Peter Gutmann to the PKCS-TNG mailing list. |
562 | | * |
563 | | * The constraints specified by SP800-132 have been added i.e. |
564 | | * - Check the range of the key length. |
565 | | * - Minimum iteration count of 1000. |
566 | | * - Randomly-generated portion of the salt shall be at least 128 bits. |
567 | | */ |
568 | | static int pbkdf2_derive(KDF_PBKDF2 *ctx, const char *pass, size_t passlen, |
569 | | const unsigned char *salt, int saltlen, uint64_t iter, |
570 | | const EVP_MD *digest, unsigned char *key, |
571 | | size_t keylen, int lower_bound_checks) |
572 | 56 | { |
573 | 56 | int ret = 0; |
574 | 56 | unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4]; |
575 | 56 | int cplen, k, tkeylen, mdlen; |
576 | 56 | uint64_t j; |
577 | 56 | unsigned long i = 1; |
578 | 56 | HMAC_CTX *hctx_tpl = NULL, *hctx = NULL; |
579 | | |
580 | 56 | mdlen = EVP_MD_get_size(digest); |
581 | 56 | if (mdlen <= 0) |
582 | 1 | return 0; |
583 | | |
584 | | /* |
585 | | * This check should always be done because keylen / mdlen >= (2^32 - 1) |
586 | | * results in an overflow of the loop counter 'i'. |
587 | | */ |
588 | 55 | if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) { |
589 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
590 | 0 | return 0; |
591 | 0 | } |
592 | | |
593 | 55 | if (!lower_bound_check_passed(ctx, saltlen, iter, keylen, lower_bound_checks)) |
594 | 0 | return 0; |
595 | | |
596 | 55 | hctx_tpl = HMAC_CTX_new(); |
597 | 55 | if (hctx_tpl == NULL) |
598 | 0 | return 0; |
599 | 55 | p = key; |
600 | 55 | tkeylen = (int)keylen; |
601 | 55 | if (!HMAC_Init_ex(hctx_tpl, pass, (int)passlen, digest, NULL)) |
602 | 0 | goto err; |
603 | 55 | hctx = HMAC_CTX_new(); |
604 | 55 | if (hctx == NULL) |
605 | 0 | goto err; |
606 | 134 | while (tkeylen) { |
607 | 79 | if (tkeylen > mdlen) |
608 | 24 | cplen = mdlen; |
609 | 55 | else |
610 | 55 | cplen = tkeylen; |
611 | | /* |
612 | | * We are unlikely to ever use more than 256 blocks (5120 bits!) but |
613 | | * just in case... |
614 | | */ |
615 | 79 | itmp[0] = (unsigned char)((i >> 24) & 0xff); |
616 | 79 | itmp[1] = (unsigned char)((i >> 16) & 0xff); |
617 | 79 | itmp[2] = (unsigned char)((i >> 8) & 0xff); |
618 | 79 | itmp[3] = (unsigned char)(i & 0xff); |
619 | 79 | if (!HMAC_CTX_copy(hctx, hctx_tpl)) |
620 | 0 | goto err; |
621 | 79 | if (!HMAC_Update(hctx, salt, saltlen) |
622 | 79 | || !HMAC_Update(hctx, itmp, 4) |
623 | 79 | || !HMAC_Final(hctx, digtmp, NULL)) |
624 | 0 | goto err; |
625 | 79 | memcpy(p, digtmp, cplen); |
626 | 79 | for (j = 1; j < iter; j++) { |
627 | 0 | if (!HMAC_CTX_copy(hctx, hctx_tpl)) |
628 | 0 | goto err; |
629 | 0 | if (!HMAC_Update(hctx, digtmp, mdlen) |
630 | 0 | || !HMAC_Final(hctx, digtmp, NULL)) |
631 | 0 | goto err; |
632 | 0 | for (k = 0; k < cplen; k++) |
633 | 0 | p[k] ^= digtmp[k]; |
634 | 0 | } |
635 | 79 | tkeylen -= cplen; |
636 | 79 | i++; |
637 | 79 | p += cplen; |
638 | 79 | } |
639 | 55 | ret = 1; |
640 | | |
641 | 55 | err: |
642 | 55 | HMAC_CTX_free(hctx); |
643 | 55 | HMAC_CTX_free(hctx_tpl); |
644 | 55 | return ret; |
645 | 55 | } |