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