/src/openssl32/providers/implementations/kdfs/pbkdf2.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018-2023 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 | | * HMAC low level APIs are deprecated for public use, but still ok for internal |
12 | | * use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <stdlib.h> |
17 | | #include <stdarg.h> |
18 | | #include <string.h> |
19 | | #include <openssl/hmac.h> |
20 | | #include <openssl/evp.h> |
21 | | #include <openssl/kdf.h> |
22 | | #include <openssl/core_names.h> |
23 | | #include <openssl/proverr.h> |
24 | | #include "internal/cryptlib.h" |
25 | | #include "internal/numbers.h" |
26 | | #include "crypto/evp.h" |
27 | | #include "prov/provider_ctx.h" |
28 | | #include "prov/providercommon.h" |
29 | | #include "prov/implementations.h" |
30 | | #include "prov/provider_util.h" |
31 | | #include "pbkdf2.h" |
32 | | |
33 | | /* Constants specified in SP800-132 */ |
34 | 0 | #define KDF_PBKDF2_MIN_KEY_LEN_BITS 112 |
35 | 0 | #define KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO 0xFFFFFFFF |
36 | 0 | #define KDF_PBKDF2_MIN_ITERATIONS 1000 |
37 | 0 | #define KDF_PBKDF2_MIN_SALT_LEN (128 / 8) |
38 | | |
39 | | static OSSL_FUNC_kdf_newctx_fn kdf_pbkdf2_new; |
40 | | static OSSL_FUNC_kdf_dupctx_fn kdf_pbkdf2_dup; |
41 | | static OSSL_FUNC_kdf_freectx_fn kdf_pbkdf2_free; |
42 | | static OSSL_FUNC_kdf_reset_fn kdf_pbkdf2_reset; |
43 | | static OSSL_FUNC_kdf_derive_fn kdf_pbkdf2_derive; |
44 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pbkdf2_settable_ctx_params; |
45 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pbkdf2_set_ctx_params; |
46 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pbkdf2_gettable_ctx_params; |
47 | | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pbkdf2_get_ctx_params; |
48 | | |
49 | | static int pbkdf2_derive(const char *pass, size_t passlen, |
50 | | const unsigned char *salt, int saltlen, uint64_t iter, |
51 | | const EVP_MD *digest, unsigned char *key, |
52 | | size_t keylen, int extra_checks); |
53 | | |
54 | | typedef struct { |
55 | | void *provctx; |
56 | | unsigned char *pass; |
57 | | size_t pass_len; |
58 | | unsigned char *salt; |
59 | | size_t salt_len; |
60 | | uint64_t iter; |
61 | | PROV_DIGEST digest; |
62 | | int lower_bound_checks; |
63 | | } KDF_PBKDF2; |
64 | | |
65 | | static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx); |
66 | | |
67 | | static void *kdf_pbkdf2_new_no_init(void *provctx) |
68 | 67 | { |
69 | 67 | KDF_PBKDF2 *ctx; |
70 | | |
71 | 67 | if (!ossl_prov_is_running()) |
72 | 0 | return NULL; |
73 | | |
74 | 67 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
75 | 67 | if (ctx == NULL) |
76 | 0 | return NULL; |
77 | 67 | ctx->provctx = provctx; |
78 | 67 | return ctx; |
79 | 67 | } |
80 | | |
81 | | static void *kdf_pbkdf2_new(void *provctx) |
82 | 67 | { |
83 | 67 | KDF_PBKDF2 *ctx = kdf_pbkdf2_new_no_init(provctx); |
84 | | |
85 | 67 | if (ctx != NULL) |
86 | 67 | kdf_pbkdf2_init(ctx); |
87 | 67 | return ctx; |
88 | 67 | } |
89 | | |
90 | | static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx) |
91 | 67 | { |
92 | 67 | ossl_prov_digest_reset(&ctx->digest); |
93 | 67 | OPENSSL_free(ctx->salt); |
94 | 67 | OPENSSL_clear_free(ctx->pass, ctx->pass_len); |
95 | 67 | memset(ctx, 0, sizeof(*ctx)); |
96 | 67 | } |
97 | | |
98 | | static void kdf_pbkdf2_free(void *vctx) |
99 | 67 | { |
100 | 67 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; |
101 | | |
102 | 67 | if (ctx != NULL) { |
103 | 67 | kdf_pbkdf2_cleanup(ctx); |
104 | 67 | OPENSSL_free(ctx); |
105 | 67 | } |
106 | 67 | } |
107 | | |
108 | | static void kdf_pbkdf2_reset(void *vctx) |
109 | 0 | { |
110 | 0 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; |
111 | 0 | void *provctx = ctx->provctx; |
112 | |
|
113 | 0 | kdf_pbkdf2_cleanup(ctx); |
114 | 0 | ctx->provctx = provctx; |
115 | 0 | kdf_pbkdf2_init(ctx); |
116 | 0 | } |
117 | | |
118 | | static void *kdf_pbkdf2_dup(void *vctx) |
119 | 0 | { |
120 | 0 | const KDF_PBKDF2 *src = (const KDF_PBKDF2 *)vctx; |
121 | 0 | KDF_PBKDF2 *dest; |
122 | | |
123 | | /* We need a new PBKDF2 object but uninitialised since we're filling it */ |
124 | 0 | dest = kdf_pbkdf2_new_no_init(src->provctx); |
125 | 0 | if (dest != NULL) { |
126 | 0 | if (!ossl_prov_memdup(src->salt, src->salt_len, |
127 | 0 | &dest->salt, &dest->salt_len) |
128 | 0 | || !ossl_prov_memdup(src->pass, src->pass_len, |
129 | 0 | &dest->pass, &dest->pass_len) |
130 | 0 | || !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
131 | 0 | goto err; |
132 | 0 | dest->iter = src->iter; |
133 | 0 | dest->lower_bound_checks = src->lower_bound_checks; |
134 | 0 | } |
135 | 0 | return dest; |
136 | | |
137 | 0 | err: |
138 | 0 | kdf_pbkdf2_free(dest); |
139 | 0 | return NULL; |
140 | 0 | } |
141 | | |
142 | | static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx) |
143 | 67 | { |
144 | 67 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
145 | 67 | OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx); |
146 | | |
147 | 67 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, |
148 | 67 | SN_sha1, 0); |
149 | 67 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx)) |
150 | | /* This is an error, but there is no way to indicate such directly */ |
151 | 0 | ossl_prov_digest_reset(&ctx->digest); |
152 | 67 | ctx->iter = PKCS5_DEFAULT_ITER; |
153 | 67 | ctx->lower_bound_checks = ossl_kdf_pbkdf2_default_checks; |
154 | 67 | } |
155 | | |
156 | | static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen, |
157 | | const OSSL_PARAM *p) |
158 | 115 | { |
159 | 115 | OPENSSL_clear_free(*buffer, *buflen); |
160 | 115 | *buffer = NULL; |
161 | 115 | *buflen = 0; |
162 | | |
163 | 115 | if (p->data_size == 0) { |
164 | 59 | if ((*buffer = OPENSSL_malloc(1)) == NULL) |
165 | 0 | return 0; |
166 | 59 | } else if (p->data != NULL) { |
167 | 56 | if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen)) |
168 | 0 | return 0; |
169 | 56 | } |
170 | 115 | return 1; |
171 | 115 | } |
172 | | |
173 | | static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, size_t keylen, |
174 | | const OSSL_PARAM params[]) |
175 | 49 | { |
176 | 49 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; |
177 | 49 | const EVP_MD *md; |
178 | | |
179 | 49 | if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params)) |
180 | 0 | return 0; |
181 | | |
182 | 49 | if (ctx->pass == NULL) { |
183 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); |
184 | 0 | return 0; |
185 | 0 | } |
186 | | |
187 | 49 | if (ctx->salt == NULL) { |
188 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT); |
189 | 0 | return 0; |
190 | 0 | } |
191 | | |
192 | 49 | md = ossl_prov_digest_md(&ctx->digest); |
193 | 49 | return pbkdf2_derive((char *)ctx->pass, ctx->pass_len, |
194 | 49 | ctx->salt, ctx->salt_len, ctx->iter, |
195 | 49 | md, key, keylen, ctx->lower_bound_checks); |
196 | 49 | } |
197 | | |
198 | | static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
199 | 0 | { |
200 | 0 | const OSSL_PARAM *p; |
201 | 0 | KDF_PBKDF2 *ctx = vctx; |
202 | 0 | OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx); |
203 | 0 | int pkcs5; |
204 | 0 | uint64_t iter, min_iter; |
205 | |
|
206 | 0 | if (params == NULL) |
207 | 0 | return 1; |
208 | | |
209 | 0 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx)) |
210 | 0 | return 0; |
211 | | |
212 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS5)) != NULL) { |
213 | 0 | if (!OSSL_PARAM_get_int(p, &pkcs5)) |
214 | 0 | return 0; |
215 | 0 | ctx->lower_bound_checks = pkcs5 == 0; |
216 | 0 | } |
217 | | |
218 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL) |
219 | 0 | if (!pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p)) |
220 | 0 | return 0; |
221 | | |
222 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) { |
223 | 0 | if (ctx->lower_bound_checks != 0 |
224 | 0 | && p->data_size < KDF_PBKDF2_MIN_SALT_LEN) { |
225 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH); |
226 | 0 | return 0; |
227 | 0 | } |
228 | 0 | if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len, p)) |
229 | 0 | return 0; |
230 | 0 | } |
231 | | |
232 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) { |
233 | 0 | if (!OSSL_PARAM_get_uint64(p, &iter)) |
234 | 0 | return 0; |
235 | 0 | min_iter = ctx->lower_bound_checks != 0 ? KDF_PBKDF2_MIN_ITERATIONS : 1; |
236 | 0 | if (iter < min_iter) { |
237 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT); |
238 | 0 | return 0; |
239 | 0 | } |
240 | 0 | ctx->iter = iter; |
241 | 0 | } |
242 | 0 | return 1; |
243 | 0 | } |
244 | | |
245 | | static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx, |
246 | | ossl_unused void *p_ctx) |
247 | 67 | { |
248 | 67 | static const OSSL_PARAM known_settable_ctx_params[] = { |
249 | 67 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), |
250 | 67 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), |
251 | 67 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0), |
252 | 67 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0), |
253 | 67 | OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL), |
254 | 67 | OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS5, NULL), |
255 | 67 | OSSL_PARAM_END |
256 | 67 | }; |
257 | 67 | return known_settable_ctx_params; |
258 | 67 | } |
259 | | |
260 | | static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
261 | 0 | { |
262 | 0 | OSSL_PARAM *p; |
263 | |
|
264 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) |
265 | 0 | return OSSL_PARAM_set_size_t(p, SIZE_MAX); |
266 | 0 | return -2; |
267 | 0 | } |
268 | | |
269 | | static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *ctx, |
270 | | ossl_unused void *p_ctx) |
271 | 0 | { |
272 | 0 | static const OSSL_PARAM known_gettable_ctx_params[] = { |
273 | 0 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
274 | 0 | OSSL_PARAM_END |
275 | 0 | }; |
276 | 0 | return known_gettable_ctx_params; |
277 | 0 | } |
278 | | |
279 | | const OSSL_DISPATCH ossl_kdf_pbkdf2_functions[] = { |
280 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new }, |
281 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pbkdf2_dup }, |
282 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf2_free }, |
283 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf2_reset }, |
284 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf2_derive }, |
285 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
286 | | (void(*)(void))kdf_pbkdf2_settable_ctx_params }, |
287 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_set_ctx_params }, |
288 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
289 | | (void(*)(void))kdf_pbkdf2_gettable_ctx_params }, |
290 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_get_ctx_params }, |
291 | | OSSL_DISPATCH_END |
292 | | }; |
293 | | |
294 | | /* |
295 | | * This is an implementation of PKCS#5 v2.0 password based encryption key |
296 | | * derivation function PBKDF2. SHA1 version verified against test vectors |
297 | | * posted by Peter Gutmann to the PKCS-TNG mailing list. |
298 | | * |
299 | | * The constraints specified by SP800-132 have been added i.e. |
300 | | * - Check the range of the key length. |
301 | | * - Minimum iteration count of 1000. |
302 | | * - Randomly-generated portion of the salt shall be at least 128 bits. |
303 | | */ |
304 | | static int pbkdf2_derive(const char *pass, size_t passlen, |
305 | | const unsigned char *salt, int saltlen, uint64_t iter, |
306 | | const EVP_MD *digest, unsigned char *key, |
307 | | size_t keylen, int lower_bound_checks) |
308 | 0 | { |
309 | 0 | int ret = 0; |
310 | 0 | unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4]; |
311 | 0 | int cplen, k, tkeylen, mdlen; |
312 | 0 | uint64_t j; |
313 | 0 | unsigned long i = 1; |
314 | 0 | HMAC_CTX *hctx_tpl = NULL, *hctx = NULL; |
315 | |
|
316 | 0 | mdlen = EVP_MD_get_size(digest); |
317 | 0 | if (mdlen <= 0) |
318 | 0 | return 0; |
319 | | |
320 | | /* |
321 | | * This check should always be done because keylen / mdlen >= (2^32 - 1) |
322 | | * results in an overflow of the loop counter 'i'. |
323 | | */ |
324 | 0 | if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) { |
325 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
326 | 0 | return 0; |
327 | 0 | } |
328 | | |
329 | 0 | if (lower_bound_checks) { |
330 | 0 | if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) { |
331 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL); |
332 | 0 | return 0; |
333 | 0 | } |
334 | 0 | if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) { |
335 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH); |
336 | 0 | return 0; |
337 | 0 | } |
338 | 0 | if (iter < KDF_PBKDF2_MIN_ITERATIONS) { |
339 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT); |
340 | 0 | return 0; |
341 | 0 | } |
342 | 0 | } |
343 | | |
344 | 0 | hctx_tpl = HMAC_CTX_new(); |
345 | 0 | if (hctx_tpl == NULL) |
346 | 0 | return 0; |
347 | 0 | p = key; |
348 | 0 | tkeylen = keylen; |
349 | 0 | if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL)) |
350 | 0 | goto err; |
351 | 0 | hctx = HMAC_CTX_new(); |
352 | 0 | if (hctx == NULL) |
353 | 0 | goto err; |
354 | 0 | while (tkeylen) { |
355 | 0 | if (tkeylen > mdlen) |
356 | 0 | cplen = mdlen; |
357 | 0 | else |
358 | 0 | cplen = tkeylen; |
359 | | /* |
360 | | * We are unlikely to ever use more than 256 blocks (5120 bits!) but |
361 | | * just in case... |
362 | | */ |
363 | 0 | itmp[0] = (unsigned char)((i >> 24) & 0xff); |
364 | 0 | itmp[1] = (unsigned char)((i >> 16) & 0xff); |
365 | 0 | itmp[2] = (unsigned char)((i >> 8) & 0xff); |
366 | 0 | itmp[3] = (unsigned char)(i & 0xff); |
367 | 0 | if (!HMAC_CTX_copy(hctx, hctx_tpl)) |
368 | 0 | goto err; |
369 | 0 | if (!HMAC_Update(hctx, salt, saltlen) |
370 | 0 | || !HMAC_Update(hctx, itmp, 4) |
371 | 0 | || !HMAC_Final(hctx, digtmp, NULL)) |
372 | 0 | goto err; |
373 | 0 | memcpy(p, digtmp, cplen); |
374 | 0 | for (j = 1; j < iter; j++) { |
375 | 0 | if (!HMAC_CTX_copy(hctx, hctx_tpl)) |
376 | 0 | goto err; |
377 | 0 | if (!HMAC_Update(hctx, digtmp, mdlen) |
378 | 0 | || !HMAC_Final(hctx, digtmp, NULL)) |
379 | 0 | goto err; |
380 | 0 | for (k = 0; k < cplen; k++) |
381 | 0 | p[k] ^= digtmp[k]; |
382 | 0 | } |
383 | 0 | tkeylen -= cplen; |
384 | 0 | i++; |
385 | 0 | p += cplen; |
386 | 0 | } |
387 | 0 | ret = 1; |
388 | |
|
389 | 0 | err: |
390 | 0 | HMAC_CTX_free(hctx); |
391 | 0 | HMAC_CTX_free(hctx_tpl); |
392 | 0 | return ret; |
393 | 0 | } |