2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2019 Red Hat, Inc.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 * This implements https://csrc.nist.gov/publications/detail/sp/800-108/final
13 * section 5.1 ("counter mode") and section 5.2 ("feedback mode") in both HMAC
14 * and CMAC. That document does not name the KDFs it defines; the name is
16 * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Key-Derivation
18 * Note that section 5.3 ("double-pipeline mode") is not implemented, though
19 * it would be possible to do so in the future.
21 * These versions all assume the counter is used. It would be relatively
22 * straightforward to expose a configuration handle should the need arise.
24 * Variable names attempt to match those of SP800-108.
31 #include <openssl/core_names.h>
32 #include <openssl/evp.h>
33 #include <openssl/hmac.h>
34 #include <openssl/kdf.h>
35 #include <openssl/params.h>
37 #include "internal/cryptlib.h"
38 #include "crypto/evp.h"
39 #include "internal/numbers.h"
40 #include "prov/implementations.h"
41 #include "prov/provider_ctx.h"
42 #include "prov/provider_util.h"
43 #include "prov/providercommonerr.h"
47 #define MIN(a, b) ((a) < (b)) ? (a) : (b)
54 /* Our context structure. */
58 EVP_MAC_CTX *ctx_init;
60 /* Names are lowercased versions of those found in SP800-108. */
65 unsigned char *context;
71 /* Definitions needed for typechecking. */
72 static OSSL_OP_kdf_newctx_fn kbkdf_new;
73 static OSSL_OP_kdf_freectx_fn kbkdf_free;
74 static OSSL_OP_kdf_reset_fn kbkdf_reset;
75 static OSSL_OP_kdf_derive_fn kbkdf_derive;
76 static OSSL_OP_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params;
77 static OSSL_OP_kdf_set_ctx_params_fn kbkdf_set_ctx_params;
79 /* Not all platforms have htobe32(). */
80 static uint32_t be32(uint32_t host)
88 if (!is_endian.little)
91 big |= (host & 0xff000000) >> 24;
92 big |= (host & 0x00ff0000) >> 8;
93 big |= (host & 0x0000ff00) << 8;
94 big |= (host & 0x000000ff) << 24;
98 static void *kbkdf_new(void *provctx)
102 ctx = OPENSSL_zalloc(sizeof(*ctx));
104 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
108 ctx->provctx = provctx;
112 static void kbkdf_free(void *vctx)
114 KBKDF *ctx = (KBKDF *)vctx;
120 static void kbkdf_reset(void *vctx)
122 KBKDF *ctx = (KBKDF *)vctx;
124 EVP_MAC_CTX_free(ctx->ctx_init);
125 OPENSSL_clear_free(ctx->context, ctx->context_len);
126 OPENSSL_clear_free(ctx->label, ctx->label_len);
127 OPENSSL_clear_free(ctx->ki, ctx->ki_len);
128 OPENSSL_clear_free(ctx->iv, ctx->iv_len);
129 memset(ctx, 0, sizeof(*ctx));
132 /* SP800-108 section 5.1 or section 5.2 depending on mode. */
133 static int derive(EVP_MAC_CTX *ctx_init, kbkdf_mode mode, unsigned char *iv,
134 size_t iv_len, unsigned char *label, size_t label_len,
135 unsigned char *context, size_t context_len,
136 unsigned char *k_i, size_t h, uint32_t l, unsigned char *ko,
140 EVP_MAC_CTX *ctx = NULL;
141 size_t written = 0, to_write, k_i_len = iv_len;
142 const unsigned char zero = 0;
145 /* Setup K(0) for feedback mode. */
147 memcpy(k_i, iv, iv_len);
149 for (counter = 1; written < ko_len; counter++) {
152 ctx = EVP_MAC_CTX_dup(ctx_init);
156 /* Perform feedback, if appropriate. */
157 if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len))
160 if (!EVP_MAC_update(ctx, (unsigned char *)&i, 4)
161 || !EVP_MAC_update(ctx, label, label_len)
162 || !EVP_MAC_update(ctx, &zero, 1)
163 || !EVP_MAC_update(ctx, context, context_len)
164 || !EVP_MAC_update(ctx, (unsigned char *)&l, 4)
165 || !EVP_MAC_final(ctx, k_i, NULL, h))
168 to_write = ko_len - written;
169 memcpy(ko + written, k_i, MIN(to_write, h));
173 EVP_MAC_CTX_free(ctx);
179 EVP_MAC_CTX_free(ctx);
183 static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen)
185 KBKDF *ctx = (KBKDF *)vctx;
187 unsigned char *k_i = NULL;
188 uint32_t l = be32(keylen * 8);
191 /* label, context, and iv are permitted to be empty. Check everything
193 if (ctx->ctx_init == NULL) {
194 if (ctx->ki_len == 0 || ctx->ki == NULL) {
195 ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
198 /* Could either be missing MAC or missing message digest or missing
199 * cipher - arbitrarily, I pick this one. */
200 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
204 h = EVP_MAC_size(ctx->ctx_init);
207 if (ctx->iv_len != 0 && ctx->iv_len != h) {
208 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
212 k_i = OPENSSL_zalloc(h);
216 ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label,
217 ctx->label_len, ctx->context, ctx->context_len, k_i, h, l,
221 OPENSSL_cleanse(key, keylen);
222 OPENSSL_clear_free(k_i, h);
226 static int kbkdf_set_buffer(unsigned char **out, size_t *out_len,
229 if (p->data == NULL || p->data_size == 0)
232 OPENSSL_clear_free(*out, *out_len);
234 return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
237 static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
239 KBKDF *ctx = (KBKDF *)vctx;
240 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
242 OSSL_PARAM mparams[2];
244 if (!ossl_prov_macctx_load_from_params(&ctx->ctx_init, params, NULL,
247 else if (ctx->ctx_init != NULL
248 && !EVP_MAC_is_a(EVP_MAC_CTX_mac(ctx->ctx_init),
250 && !EVP_MAC_is_a(EVP_MAC_CTX_mac(ctx->ctx_init),
251 OSSL_MAC_NAME_CMAC)) {
252 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);
256 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MODE);
257 if (p != NULL && strncasecmp("counter", p->data, p->data_size) == 0) {
260 && strncasecmp("feedback", p->data, p->data_size) == 0) {
261 ctx->mode = FEEDBACK;
262 } else if (p != NULL) {
263 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
267 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY);
268 if (p != NULL && !kbkdf_set_buffer(&ctx->ki, &ctx->ki_len, p))
271 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT);
272 if (p != NULL && !kbkdf_set_buffer(&ctx->label, &ctx->label_len, p))
275 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_INFO);
276 if (p != NULL && !kbkdf_set_buffer(&ctx->context, &ctx->context_len, p))
279 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED);
280 if (p != NULL && !kbkdf_set_buffer(&ctx->iv, &ctx->iv_len, p))
283 /* Set up digest context, if we can. */
284 if (ctx->ctx_init != NULL && ctx->ki_len != 0) {
285 mparams[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
286 ctx->ki, ctx->ki_len);
287 mparams[1] = OSSL_PARAM_construct_end();
289 if (!EVP_MAC_CTX_set_params(ctx->ctx_init, mparams)
290 || !EVP_MAC_init(ctx->ctx_init))
297 static const OSSL_PARAM *kbkdf_settable_ctx_params(void)
299 static const OSSL_PARAM known_settable_ctx_params[] = {
300 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
301 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
302 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
303 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
304 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
305 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),
306 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
307 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0),
309 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
312 return known_settable_ctx_params;
315 static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
319 p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE);
323 /* KBKDF can produce results as large as you like. */
324 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
327 static const OSSL_PARAM *kbkdf_gettable_ctx_params(void)
329 static const OSSL_PARAM known_gettable_ctx_params[] =
330 { OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), OSSL_PARAM_END };
331 return known_gettable_ctx_params;
334 const OSSL_DISPATCH kdf_kbkdf_functions[] = {
335 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kbkdf_new },
336 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kbkdf_free },
337 { OSSL_FUNC_KDF_RESET, (void(*)(void))kbkdf_reset },
338 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kbkdf_derive },
339 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
340 (void(*)(void))kbkdf_settable_ctx_params },
341 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kbkdf_set_ctx_params },
342 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
343 (void(*)(void))kbkdf_gettable_ctx_params },
344 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kbkdf_get_ctx_params },