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") in HMAC only. That document does not name the
14 * KDFs it defines; the name is derived from
15 * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Key-Derivation
17 * Note that sections 5.2 ("feedback mode") and 5.3 ("double-pipeline mode")
18 * are not implemented, though it would be possible to do so in the future.
19 * CMAC mode is also not implemented; some plumbing would be required.
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 "internal/provider_ctx.h"
42 #include "internal/provider_util.h"
43 #include "internal/providercommonerr.h"
47 #define MIN(a, b) ((a) < (b)) ? (a) : (b)
49 /* Our context structure. */
52 EVP_MAC_CTX *ctx_init;
54 /* Names are lowercased versions of those found in SP800-108. */
59 unsigned char *context;
63 /* Definitions needed for typechecking. */
64 static OSSL_OP_kdf_newctx_fn kbkdf_new;
65 static OSSL_OP_kdf_freectx_fn kbkdf_free;
66 static OSSL_OP_kdf_reset_fn kbkdf_reset;
67 static OSSL_OP_kdf_derive_fn kbkdf_derive;
68 static OSSL_OP_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params;
69 static OSSL_OP_kdf_set_ctx_params_fn kbkdf_set_ctx_params;
71 /* Not all platforms have htobe32(). */
72 static uint32_t be32(uint32_t host)
80 if (!is_endian.little)
83 big |= (host & 0xff000000) >> 24;
84 big |= (host & 0x00ff0000) >> 8;
85 big |= (host & 0x0000ff00) << 8;
86 big |= (host & 0x000000ff) << 24;
90 static void *kbkdf_new(void *provctx)
94 ctx = OPENSSL_zalloc(sizeof(*ctx));
96 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
100 ctx->provctx = provctx;
104 static void kbkdf_free(void *vctx)
106 KBKDF *ctx = (KBKDF *)vctx;
112 static void kbkdf_reset(void *vctx)
114 KBKDF *ctx = (KBKDF *)vctx;
116 EVP_MAC_CTX_free(ctx->ctx_init);
117 OPENSSL_clear_free(ctx->context, ctx->context_len);
118 OPENSSL_clear_free(ctx->label, ctx->label_len);
119 OPENSSL_clear_free(ctx->ki, ctx->ki_len);
120 memset(ctx, 0, sizeof(*ctx));
123 /* SP800-108 section 5.1. */
124 static int kbkdf_derive_counter(EVP_MAC_CTX *ctx_init,
125 unsigned char *label, size_t label_len,
126 unsigned char *context, size_t context_len,
127 unsigned char *k_i, size_t h, uint32_t l,
128 unsigned char *ko, size_t ko_len)
131 EVP_MAC_CTX *ctx = NULL;
132 size_t written = 0, to_write;
133 const unsigned char zero = 0;
136 for (counter = 1; written < ko_len; counter++) {
139 ctx = EVP_MAC_CTX_dup(ctx_init);
143 if (!EVP_MAC_update(ctx, (unsigned char *)&i, 4)
144 || !EVP_MAC_update(ctx, label, label_len)
145 || !EVP_MAC_update(ctx, &zero, 1)
146 || !EVP_MAC_update(ctx, context, context_len)
147 || !EVP_MAC_update(ctx, (unsigned char *)&l, 4)
148 || !EVP_MAC_final(ctx, k_i, NULL, h))
151 to_write = ko_len - written;
152 memcpy(ko + written, k_i, MIN(to_write, h));
155 EVP_MAC_CTX_free(ctx);
161 EVP_MAC_CTX_free(ctx);
165 static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen)
167 KBKDF *ctx = (KBKDF *)vctx;
169 unsigned char *k_i = NULL;
170 uint32_t l = be32(keylen * 8);
173 /* Label and Context are permitted to be empty. Check everything else. */
174 if (ctx->ctx_init == NULL) {
175 if (ctx->ki_len == 0 || ctx->ki == NULL) {
176 ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
179 /* Could either be missing MAC or missing message digest -
180 * arbitrarily, I pick this one. */
181 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
185 h = EVP_MAC_size(ctx->ctx_init);
189 k_i = OPENSSL_zalloc(h);
193 ret = kbkdf_derive_counter(
194 ctx->ctx_init, ctx->label, ctx->label_len, ctx->context,
195 ctx->context_len, k_i, h, l, key, keylen);
198 OPENSSL_cleanse(key, keylen);
199 OPENSSL_clear_free(k_i, h);
203 static int kbkdf_set_buffer(unsigned char **out, size_t *out_len,
206 if (p->data == NULL || p->data_size == 0)
209 OPENSSL_clear_free(*out, *out_len);
211 return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
214 static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
216 KBKDF *ctx = (KBKDF *)vctx;
217 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
219 OSSL_PARAM mparams[2];
221 if (!ossl_prov_macctx_load_from_params(&ctx->ctx_init, params, NULL,
224 else if (ctx->ctx_init != NULL
225 && !EVP_MAC_is_a(EVP_MAC_CTX_mac(ctx->ctx_init),
226 OSSL_MAC_NAME_HMAC)) {
227 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);
231 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY);
232 if (p != NULL && !kbkdf_set_buffer(&ctx->ki, &ctx->ki_len, p))
235 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT);
236 if (p != NULL && !kbkdf_set_buffer(&ctx->label, &ctx->label_len, p))
239 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_INFO);
240 if (p != NULL && !kbkdf_set_buffer(&ctx->context, &ctx->context_len, p))
243 /* Set up digest context, if we can. */
244 if (ctx->ctx_init != NULL && ctx->ki_len != 0) {
245 mparams[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
246 ctx->ki, ctx->ki_len);
247 mparams[1] = OSSL_PARAM_construct_end();
249 if (!EVP_MAC_CTX_set_params(ctx->ctx_init, mparams)
250 || !EVP_MAC_init(ctx->ctx_init))
257 static const OSSL_PARAM *kbkdf_settable_ctx_params(void)
259 static const OSSL_PARAM known_settable_ctx_params[] = {
260 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
261 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
262 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
263 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
264 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
266 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
269 return known_settable_ctx_params;
272 static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
276 p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE);
280 /* KBKDF can produce results as large as you like. */
281 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
284 static const OSSL_PARAM *kbkdf_gettable_ctx_params(void)
286 static const OSSL_PARAM known_gettable_ctx_params[] =
287 { OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), OSSL_PARAM_END };
288 return known_gettable_ctx_params;
291 const OSSL_DISPATCH kdf_kbkdf_functions[] = {
292 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kbkdf_new },
293 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kbkdf_free },
294 { OSSL_FUNC_KDF_RESET, (void(*)(void))kbkdf_reset },
295 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kbkdf_derive },
296 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
297 (void(*)(void))kbkdf_settable_ctx_params },
298 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kbkdf_set_ctx_params },
299 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
300 (void(*)(void))kbkdf_gettable_ctx_params },
301 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kbkdf_get_ctx_params },