]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/kdfs/pbkdf2.c
2506a96f7ce8f4b0161f1afd9be1e34d69210d00
[thirdparty/openssl.git] / providers / implementations / kdfs / pbkdf2.c
1 /*
2 * Copyright 2018-2022 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 #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)
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 {
69 KDF_PBKDF2 *ctx;
70
71 if (!ossl_prov_is_running())
72 return NULL;
73
74 ctx = OPENSSL_zalloc(sizeof(*ctx));
75 if (ctx == NULL)
76 return NULL;
77 ctx->provctx = provctx;
78 return ctx;
79 }
80
81 static void *kdf_pbkdf2_new(void *provctx)
82 {
83 KDF_PBKDF2 *ctx = kdf_pbkdf2_new_no_init(provctx);
84
85 if (ctx != NULL)
86 kdf_pbkdf2_init(ctx);
87 return ctx;
88 }
89
90 static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx)
91 {
92 ossl_prov_digest_reset(&ctx->digest);
93 OPENSSL_free(ctx->salt);
94 OPENSSL_clear_free(ctx->pass, ctx->pass_len);
95 memset(ctx, 0, sizeof(*ctx));
96 }
97
98 static void kdf_pbkdf2_free(void *vctx)
99 {
100 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
101
102 if (ctx != NULL) {
103 kdf_pbkdf2_cleanup(ctx);
104 OPENSSL_free(ctx);
105 }
106 }
107
108 static void kdf_pbkdf2_reset(void *vctx)
109 {
110 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
111 void *provctx = ctx->provctx;
112
113 kdf_pbkdf2_cleanup(ctx);
114 ctx->provctx = provctx;
115 kdf_pbkdf2_init(ctx);
116 }
117
118 static void *kdf_pbkdf2_dup(void *vctx)
119 {
120 const KDF_PBKDF2 *src = (const KDF_PBKDF2 *)vctx;
121 KDF_PBKDF2 *dest;
122
123 /* We need a new PBKDF2 object but uninitialised since we're filling it */
124 dest = kdf_pbkdf2_new_no_init(src->provctx);
125 if (dest != NULL) {
126 if (!ossl_prov_memdup(src->salt, src->salt_len,
127 &dest->salt, &dest->salt_len)
128 || !ossl_prov_memdup(src->pass, src->pass_len,
129 &dest->pass, &dest->pass_len)
130 || !ossl_prov_digest_copy(&dest->digest, &src->digest))
131 goto err;
132 dest->iter = src->iter;
133 dest->lower_bound_checks = src->lower_bound_checks;
134 }
135 return dest;
136
137 err:
138 kdf_pbkdf2_free(dest);
139 return NULL;
140 }
141
142 static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx)
143 {
144 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
145 OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
146
147 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
148 SN_sha1, 0);
149 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 ossl_prov_digest_reset(&ctx->digest);
152 ctx->iter = PKCS5_DEFAULT_ITER;
153 ctx->lower_bound_checks = ossl_kdf_pbkdf2_default_checks;
154 }
155
156 static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen,
157 const OSSL_PARAM *p)
158 {
159 OPENSSL_clear_free(*buffer, *buflen);
160 *buffer = NULL;
161 *buflen = 0;
162
163 if (p->data_size == 0) {
164 if ((*buffer = OPENSSL_malloc(1)) == NULL)
165 return 0;
166 } else if (p->data != NULL) {
167 if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
168 return 0;
169 }
170 return 1;
171 }
172
173 static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, size_t keylen,
174 const OSSL_PARAM params[])
175 {
176 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
177 const EVP_MD *md;
178
179 if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params))
180 return 0;
181
182 if (ctx->pass == NULL) {
183 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
184 return 0;
185 }
186
187 if (ctx->salt == NULL) {
188 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
189 return 0;
190 }
191
192 md = ossl_prov_digest_md(&ctx->digest);
193 return pbkdf2_derive((char *)ctx->pass, ctx->pass_len,
194 ctx->salt, ctx->salt_len, ctx->iter,
195 md, key, keylen, ctx->lower_bound_checks);
196 }
197
198 static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
199 {
200 const OSSL_PARAM *p;
201 KDF_PBKDF2 *ctx = vctx;
202 OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
203 int pkcs5;
204 uint64_t iter, min_iter;
205
206 if (params == NULL)
207 return 1;
208
209 if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
210 return 0;
211
212 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS5)) != NULL) {
213 if (!OSSL_PARAM_get_int(p, &pkcs5))
214 return 0;
215 ctx->lower_bound_checks = pkcs5 == 0;
216 }
217
218 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
219 if (!pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p))
220 return 0;
221
222 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) {
223 if (ctx->lower_bound_checks != 0
224 && p->data_size < KDF_PBKDF2_MIN_SALT_LEN) {
225 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
226 return 0;
227 }
228 if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len, p))
229 return 0;
230 }
231
232 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) {
233 if (!OSSL_PARAM_get_uint64(p, &iter))
234 return 0;
235 min_iter = ctx->lower_bound_checks != 0 ? KDF_PBKDF2_MIN_ITERATIONS : 1;
236 if (iter < min_iter) {
237 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
238 return 0;
239 }
240 ctx->iter = iter;
241 }
242 return 1;
243 }
244
245 static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx,
246 ossl_unused void *p_ctx)
247 {
248 static const OSSL_PARAM known_settable_ctx_params[] = {
249 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
250 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
251 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
252 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
253 OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
254 OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS5, NULL),
255 OSSL_PARAM_END
256 };
257 return known_settable_ctx_params;
258 }
259
260 static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[])
261 {
262 OSSL_PARAM *p;
263
264 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
265 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
266 return -2;
267 }
268
269 static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *ctx,
270 ossl_unused void *p_ctx)
271 {
272 static const OSSL_PARAM known_gettable_ctx_params[] = {
273 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
274 OSSL_PARAM_END
275 };
276 return known_gettable_ctx_params;
277 }
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 { 0, NULL }
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 {
309 int ret = 0;
310 unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
311 int cplen, k, tkeylen, mdlen;
312 uint64_t j;
313 unsigned long i = 1;
314 HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
315
316 mdlen = EVP_MD_get_size(digest);
317 if (mdlen <= 0)
318 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 if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) {
325 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
326 return 0;
327 }
328
329 if (lower_bound_checks) {
330 if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) {
331 ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL);
332 return 0;
333 }
334 if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) {
335 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
336 return 0;
337 }
338 if (iter < KDF_PBKDF2_MIN_ITERATIONS) {
339 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
340 return 0;
341 }
342 }
343
344 hctx_tpl = HMAC_CTX_new();
345 if (hctx_tpl == NULL)
346 return 0;
347 p = key;
348 tkeylen = keylen;
349 if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL))
350 goto err;
351 hctx = HMAC_CTX_new();
352 if (hctx == NULL)
353 goto err;
354 while (tkeylen) {
355 if (tkeylen > mdlen)
356 cplen = mdlen;
357 else
358 cplen = tkeylen;
359 /*
360 * We are unlikely to ever use more than 256 blocks (5120 bits!) but
361 * just in case...
362 */
363 itmp[0] = (unsigned char)((i >> 24) & 0xff);
364 itmp[1] = (unsigned char)((i >> 16) & 0xff);
365 itmp[2] = (unsigned char)((i >> 8) & 0xff);
366 itmp[3] = (unsigned char)(i & 0xff);
367 if (!HMAC_CTX_copy(hctx, hctx_tpl))
368 goto err;
369 if (!HMAC_Update(hctx, salt, saltlen)
370 || !HMAC_Update(hctx, itmp, 4)
371 || !HMAC_Final(hctx, digtmp, NULL))
372 goto err;
373 memcpy(p, digtmp, cplen);
374 for (j = 1; j < iter; j++) {
375 if (!HMAC_CTX_copy(hctx, hctx_tpl))
376 goto err;
377 if (!HMAC_Update(hctx, digtmp, mdlen)
378 || !HMAC_Final(hctx, digtmp, NULL))
379 goto err;
380 for (k = 0; k < cplen; k++)
381 p[k] ^= digtmp[k];
382 }
383 tkeylen -= cplen;
384 i++;
385 p += cplen;
386 }
387 ret = 1;
388
389 err:
390 HMAC_CTX_free(hctx);
391 HMAC_CTX_free(hctx_tpl);
392 return ret;
393 }