2 * Copyright 2018-2020 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
11 * HMAC low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
19 #include <openssl/hmac.h>
20 #include <openssl/evp.h>
21 #include <openssl/kdf.h>
22 #include <openssl/core_names.h>
23 #include "internal/cryptlib.h"
24 #include "internal/numbers.h"
25 #include "crypto/evp.h"
26 #include "prov/provider_ctx.h"
27 #include "prov/providercommon.h"
28 #include "prov/providercommonerr.h"
29 #include "prov/implementations.h"
30 #include "prov/provider_util.h"
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)
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;
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);
61 int lower_bound_checks;
64 static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx);
66 static void *kdf_pbkdf2_new(void *provctx)
70 if (!ossl_prov_is_running())
73 ctx = OPENSSL_zalloc(sizeof(*ctx));
75 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
78 ctx->provctx = provctx;
83 static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx)
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));
91 static void kdf_pbkdf2_free(void *vctx)
93 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
96 kdf_pbkdf2_cleanup(ctx);
101 static void kdf_pbkdf2_reset(void *vctx)
103 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
104 void *provctx = ctx->provctx;
106 kdf_pbkdf2_cleanup(ctx);
107 ctx->provctx = provctx;
108 kdf_pbkdf2_init(ctx);
111 static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx)
113 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
114 OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
116 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
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 = kdf_pbkdf2_default_checks;
125 static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen,
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);
134 } else if (p->data != NULL) {
136 if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
142 static int kdf_pbkdf2_derive(void *vctx, unsigned char *key,
145 KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
148 if (!ossl_prov_is_running())
151 if (ctx->pass == NULL) {
152 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
156 if (ctx->salt == NULL) {
157 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
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);
167 static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
170 KDF_PBKDF2 *ctx = vctx;
171 OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
173 uint64_t iter, min_iter;
175 if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
178 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS5)) != NULL) {
179 if (!OSSL_PARAM_get_int(p, &pkcs5))
181 ctx->lower_bound_checks = pkcs5 == 0;
184 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
185 if (!pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, 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);
194 if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len,p))
198 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) {
199 if (!OSSL_PARAM_get_uint64(p, &iter))
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);
211 static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *p_ctx)
213 static const OSSL_PARAM known_settable_ctx_params[] = {
214 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
215 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
216 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
217 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
218 OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
219 OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS5, NULL),
222 return known_settable_ctx_params;
225 static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[])
229 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
230 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
234 static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *p_ctx)
236 static const OSSL_PARAM known_gettable_ctx_params[] = {
237 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
240 return known_gettable_ctx_params;
243 const OSSL_DISPATCH kdf_pbkdf2_functions[] = {
244 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new },
245 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf2_free },
246 { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf2_reset },
247 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf2_derive },
248 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
249 (void(*)(void))kdf_pbkdf2_settable_ctx_params },
250 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_set_ctx_params },
251 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
252 (void(*)(void))kdf_pbkdf2_gettable_ctx_params },
253 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_get_ctx_params },
258 * This is an implementation of PKCS#5 v2.0 password based encryption key
259 * derivation function PBKDF2. SHA1 version verified against test vectors
260 * posted by Peter Gutmann to the PKCS-TNG mailing list.
262 * The constraints specified by SP800-132 have been added i.e.
263 * - Check the range of the key length.
264 * - Minimum iteration count of 1000.
265 * - Randomly-generated portion of the salt shall be at least 128 bits.
267 static int pbkdf2_derive(const char *pass, size_t passlen,
268 const unsigned char *salt, int saltlen, uint64_t iter,
269 const EVP_MD *digest, unsigned char *key,
270 size_t keylen, int lower_bound_checks)
273 unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
274 int cplen, k, tkeylen, mdlen;
277 HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
279 mdlen = EVP_MD_size(digest);
284 * This check should always be done because keylen / mdlen >= (2^32 - 1)
285 * results in an overflow of the loop counter 'i'.
287 if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) {
288 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LEN);
292 if (lower_bound_checks) {
293 if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) {
294 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LEN);
297 if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) {
298 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
301 if (iter < KDF_PBKDF2_MIN_ITERATIONS) {
302 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
307 hctx_tpl = HMAC_CTX_new();
308 if (hctx_tpl == NULL)
312 if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL))
314 hctx = HMAC_CTX_new();
323 * We are unlikely to ever use more than 256 blocks (5120 bits!) but
326 itmp[0] = (unsigned char)((i >> 24) & 0xff);
327 itmp[1] = (unsigned char)((i >> 16) & 0xff);
328 itmp[2] = (unsigned char)((i >> 8) & 0xff);
329 itmp[3] = (unsigned char)(i & 0xff);
330 if (!HMAC_CTX_copy(hctx, hctx_tpl))
332 if (!HMAC_Update(hctx, salt, saltlen)
333 || !HMAC_Update(hctx, itmp, 4)
334 || !HMAC_Final(hctx, digtmp, NULL))
336 memcpy(p, digtmp, cplen);
337 for (j = 1; j < iter; j++) {
338 if (!HMAC_CTX_copy(hctx, hctx_tpl))
340 if (!HMAC_Update(hctx, digtmp, mdlen)
341 || !HMAC_Final(hctx, digtmp, NULL))
343 for (k = 0; k < cplen; k++)
354 HMAC_CTX_free(hctx_tpl);