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
kdf_pbkdf2_new_no_init(void * provctx)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 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
77 return NULL;
78 }
79 ctx->provctx = provctx;
80 return ctx;
81 }
82
kdf_pbkdf2_new(void * provctx)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);
89 return ctx;
90 }
91
kdf_pbkdf2_cleanup(KDF_PBKDF2 * ctx)92 static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx)
93 {
94 ossl_prov_digest_reset(&ctx->digest);
95 OPENSSL_free(ctx->salt);
96 OPENSSL_clear_free(ctx->pass, ctx->pass_len);
97 memset(ctx, 0, sizeof(*ctx));
98 }
99
kdf_pbkdf2_free(void * vctx)100 static void kdf_pbkdf2_free(void *vctx)
101 {
102 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
103
104 if (ctx != NULL) {
105 kdf_pbkdf2_cleanup(ctx);
106 OPENSSL_free(ctx);
107 }
108 }
109
kdf_pbkdf2_reset(void * vctx)110 static void kdf_pbkdf2_reset(void *vctx)
111 {
112 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
113 void *provctx = ctx->provctx;
114
115 kdf_pbkdf2_cleanup(ctx);
116 ctx->provctx = provctx;
117 kdf_pbkdf2_init(ctx);
118 }
119
kdf_pbkdf2_dup(void * vctx)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
kdf_pbkdf2_init(KDF_PBKDF2 * ctx)144 static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx)
145 {
146 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
147 OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
148
149 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
150 SN_sha1, 0);
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);
154 ctx->iter = PKCS5_DEFAULT_ITER;
155 ctx->lower_bound_checks = ossl_kdf_pbkdf2_default_checks;
156 }
157
pbkdf2_set_membuf(unsigned char ** buffer,size_t * buflen,const OSSL_PARAM * p)158 static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen,
159 const OSSL_PARAM *p)
160 {
161 OPENSSL_clear_free(*buffer, *buflen);
162 *buffer = NULL;
163 *buflen = 0;
164
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) {
171 if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
172 return 0;
173 }
174 return 1;
175 }
176
kdf_pbkdf2_derive(void * vctx,unsigned char * key,size_t keylen,const OSSL_PARAM params[])177 static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, size_t keylen,
178 const OSSL_PARAM params[])
179 {
180 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
181 const EVP_MD *md;
182
183 if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params))
184 return 0;
185
186 if (ctx->pass == NULL) {
187 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
188 return 0;
189 }
190
191 if (ctx->salt == NULL) {
192 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
193 return 0;
194 }
195
196 md = ossl_prov_digest_md(&ctx->digest);
197 return pbkdf2_derive((char *)ctx->pass, ctx->pass_len,
198 ctx->salt, ctx->salt_len, ctx->iter,
199 md, key, keylen, ctx->lower_bound_checks);
200 }
201
kdf_pbkdf2_set_ctx_params(void * vctx,const OSSL_PARAM params[])202 static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
203 {
204 const OSSL_PARAM *p;
205 KDF_PBKDF2 *ctx = vctx;
206 OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
207 int pkcs5;
208 uint64_t iter, min_iter;
209
210 if (params == NULL)
211 return 1;
212
213 if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
214 return 0;
215
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;
220 }
221
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;
225
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 }
232 if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len, p))
233 return 0;
234 }
235
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 }
248
kdf_pbkdf2_settable_ctx_params(ossl_unused void * ctx,ossl_unused void * p_ctx)249 static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx,
250 ossl_unused void *p_ctx)
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 }
263
kdf_pbkdf2_get_ctx_params(void * vctx,OSSL_PARAM params[])264 static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[])
265 {
266 OSSL_PARAM *p;
267
268 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
269 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
270 return -2;
271 }
272
kdf_pbkdf2_gettable_ctx_params(ossl_unused void * ctx,ossl_unused void * p_ctx)273 static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *ctx,
274 ossl_unused void *p_ctx)
275 {
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;
281 }
282
283 const OSSL_DISPATCH ossl_kdf_pbkdf2_functions[] = {
284 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new },
285 { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pbkdf2_dup },
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 }
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.
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.
307 */
pbkdf2_derive(const char * pass,size_t passlen,const unsigned char * salt,int saltlen,uint64_t iter,const EVP_MD * digest,unsigned char * key,size_t keylen,int lower_bound_checks)308 static int pbkdf2_derive(const char *pass, size_t passlen,
309 const unsigned char *salt, int saltlen, uint64_t iter,
310 const EVP_MD *digest, unsigned char *key,
311 size_t keylen, int lower_bound_checks)
312 {
313 int ret = 0;
314 unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
315 int cplen, k, tkeylen, mdlen;
316 uint64_t j;
317 unsigned long i = 1;
318 HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
319
320 mdlen = EVP_MD_get_size(digest);
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) {
329 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
330 return 0;
331 }
332
333 if (lower_bound_checks) {
334 if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) {
335 ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL);
336 return 0;
337 }
338 if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) {
339 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
340 return 0;
341 }
342 if (iter < KDF_PBKDF2_MIN_ITERATIONS) {
343 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
344 return 0;
345 }
346 }
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 }
398