2 * Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
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
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"
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)
32 static OSSL_OP_kdf_newctx_fn kdf_pbkdf2_new;
33 static OSSL_OP_kdf_freectx_fn kdf_pbkdf2_free;
34 static OSSL_OP_kdf_reset_fn kdf_pbkdf2_reset;
35 static OSSL_OP_kdf_derive_fn kdf_pbkdf2_derive;
36 static OSSL_OP_kdf_settable_ctx_params_fn kdf_pbkdf2_settable_ctx_params;
37 static OSSL_OP_kdf_set_ctx_params_fn kdf_pbkdf2_set_ctx_params;
39 static 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);
52 int lower_bound_checks;
55 static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx);
57 static void *kdf_pbkdf2_new(void *provctx)
61 ctx = OPENSSL_zalloc(sizeof(*ctx));
63 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
66 ctx->provctx = provctx;
71 static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx)
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));
79 static void kdf_pbkdf2_free(void *vctx)
81 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
84 kdf_pbkdf2_cleanup(ctx);
89 static void kdf_pbkdf2_reset(void *vctx)
91 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
93 kdf_pbkdf2_cleanup(ctx);
97 static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx)
99 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
100 OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
102 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
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;
111 static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen,
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);
120 } else if (p->data != NULL) {
122 if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
128 static int kdf_pbkdf2_derive(void *vctx, unsigned char *key,
131 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
132 const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
134 if (ctx->pass == NULL) {
135 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
139 if (ctx->salt == NULL) {
140 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
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);
149 static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
152 KDF_PBKDF2 *ctx = vctx;
153 OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
155 uint64_t iter, min_iter;
157 if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
160 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS5)) != NULL) {
161 if (!OSSL_PARAM_get_int(p, &pkcs5))
163 ctx->lower_bound_checks = pkcs5 == 0;
166 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
167 if (!pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p))
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);
176 if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len,p))
180 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) {
181 if (!OSSL_PARAM_get_uint64(p, &iter))
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);
193 static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(void)
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),
204 return known_settable_ctx_params;
207 static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[])
211 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
212 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
216 static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(void)
218 static const OSSL_PARAM known_gettable_ctx_params[] = {
219 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
222 return known_gettable_ctx_params;
225 const 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 },
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.
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.
249 static 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)
255 unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
256 int cplen, k, tkeylen, mdlen;
259 HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
261 mdlen = EVP_MD_size(digest);
266 * This check should always be done because keylen / mdlen >= (2^32 - 1)
267 * results in an overflow of the loop counter 'i'.
269 if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) {
270 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LEN);
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);
279 if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) {
280 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
283 if (iter < KDF_PBKDF2_MIN_ITERATIONS) {
284 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
289 hctx_tpl = HMAC_CTX_new();
290 if (hctx_tpl == NULL)
294 if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL))
296 hctx = HMAC_CTX_new();
305 * We are unlikely to ever use more than 256 blocks (5120 bits!) but
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))
314 if (!HMAC_Update(hctx, salt, saltlen)
315 || !HMAC_Update(hctx, itmp, 4)
316 || !HMAC_Final(hctx, digtmp, NULL))
318 memcpy(p, digtmp, cplen);
319 for (j = 1; j < iter; j++) {
320 if (!HMAC_CTX_copy(hctx, hctx_tpl))
322 if (!HMAC_Update(hctx, digtmp, mdlen)
323 || !HMAC_Final(hctx, digtmp, NULL))
325 for (k = 0; k < cplen; k++)
336 HMAC_CTX_free(hctx_tpl);