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