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