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