2 * Copyright 2019-2021 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>
36 #include <openssl/proverr.h>
38 #include "internal/cryptlib.h"
39 #include "crypto/evp.h"
40 #include "internal/numbers.h"
41 #include "internal/endian.h"
42 #include "prov/implementations.h"
43 #include "prov/provider_ctx.h"
44 #include "prov/provider_util.h"
45 #include "prov/providercommon.h"
49 #define MIN(a, b) ((a) < (b)) ? (a) : (b)
56 /* Our context structure. */
60 EVP_MAC_CTX
*ctx_init
;
62 /* Names are lowercased versions of those found in SP800-108. */
67 unsigned char *context
;
75 /* Definitions needed for typechecking. */
76 static OSSL_FUNC_kdf_newctx_fn kbkdf_new
;
77 static OSSL_FUNC_kdf_freectx_fn kbkdf_free
;
78 static OSSL_FUNC_kdf_reset_fn kbkdf_reset
;
79 static OSSL_FUNC_kdf_derive_fn kbkdf_derive
;
80 static OSSL_FUNC_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params
;
81 static OSSL_FUNC_kdf_set_ctx_params_fn kbkdf_set_ctx_params
;
82 static OSSL_FUNC_kdf_gettable_ctx_params_fn kbkdf_gettable_ctx_params
;
83 static OSSL_FUNC_kdf_get_ctx_params_fn kbkdf_get_ctx_params
;
85 /* Not all platforms have htobe32(). */
86 static uint32_t be32(uint32_t host
)
91 if (!IS_LITTLE_ENDIAN
)
94 big
|= (host
& 0xff000000) >> 24;
95 big
|= (host
& 0x00ff0000) >> 8;
96 big
|= (host
& 0x0000ff00) << 8;
97 big
|= (host
& 0x000000ff) << 24;
101 static void init(KBKDF
*ctx
)
104 ctx
->use_separator
= 1;
107 static void *kbkdf_new(void *provctx
)
111 if (!ossl_prov_is_running())
114 ctx
= OPENSSL_zalloc(sizeof(*ctx
));
116 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
120 ctx
->provctx
= provctx
;
125 static void kbkdf_free(void *vctx
)
127 KBKDF
*ctx
= (KBKDF
*)vctx
;
135 static void kbkdf_reset(void *vctx
)
137 KBKDF
*ctx
= (KBKDF
*)vctx
;
138 void *provctx
= ctx
->provctx
;
140 EVP_MAC_CTX_free(ctx
->ctx_init
);
141 OPENSSL_clear_free(ctx
->context
, ctx
->context_len
);
142 OPENSSL_clear_free(ctx
->label
, ctx
->label_len
);
143 OPENSSL_clear_free(ctx
->ki
, ctx
->ki_len
);
144 OPENSSL_clear_free(ctx
->iv
, ctx
->iv_len
);
145 memset(ctx
, 0, sizeof(*ctx
));
146 ctx
->provctx
= provctx
;
150 /* SP800-108 section 5.1 or section 5.2 depending on mode. */
151 static int derive(EVP_MAC_CTX
*ctx_init
, kbkdf_mode mode
, unsigned char *iv
,
152 size_t iv_len
, unsigned char *label
, size_t label_len
,
153 unsigned char *context
, size_t context_len
,
154 unsigned char *k_i
, size_t h
, uint32_t l
, int has_separator
,
155 unsigned char *ko
, size_t ko_len
)
158 EVP_MAC_CTX
*ctx
= NULL
;
159 size_t written
= 0, to_write
, k_i_len
= iv_len
;
160 const unsigned char zero
= 0;
164 * The fixed input data is a concatenation of a Label,
165 * a separation indicator 0x00, the Context, and L.
166 * One or more of these fixed input data fields may be omitted.
168 * has_separator == 0 means that the separator is omitted.
169 * Passing a value of l == 0 means that L is omitted.
170 * The Context and L are omitted automatically if a NULL buffer is passed.
172 int has_l
= (l
!= 0);
174 /* Setup K(0) for feedback mode. */
176 memcpy(k_i
, iv
, iv_len
);
178 for (counter
= 1; written
< ko_len
; counter
++) {
181 ctx
= EVP_MAC_CTX_dup(ctx_init
);
185 /* Perform feedback, if appropriate. */
186 if (mode
== FEEDBACK
&& !EVP_MAC_update(ctx
, k_i
, k_i_len
))
189 if (!EVP_MAC_update(ctx
, (unsigned char *)&i
, 4)
190 || !EVP_MAC_update(ctx
, label
, label_len
)
191 || (has_separator
&& !EVP_MAC_update(ctx
, &zero
, 1))
192 || !EVP_MAC_update(ctx
, context
, context_len
)
193 || (has_l
&& !EVP_MAC_update(ctx
, (unsigned char *)&l
, 4))
194 || !EVP_MAC_final(ctx
, k_i
, NULL
, h
))
197 to_write
= ko_len
- written
;
198 memcpy(ko
+ written
, k_i
, MIN(to_write
, h
));
202 EVP_MAC_CTX_free(ctx
);
208 EVP_MAC_CTX_free(ctx
);
212 static int kbkdf_derive(void *vctx
, unsigned char *key
, size_t keylen
)
214 KBKDF
*ctx
= (KBKDF
*)vctx
;
216 unsigned char *k_i
= NULL
;
220 if (!ossl_prov_is_running())
223 /* label, context, and iv are permitted to be empty. Check everything
225 if (ctx
->ctx_init
== NULL
) {
226 if (ctx
->ki_len
== 0 || ctx
->ki
== NULL
) {
227 ERR_raise(ERR_LIB_PROV
, PROV_R_NO_KEY_SET
);
230 /* Could either be missing MAC or missing message digest or missing
231 * cipher - arbitrarily, I pick this one. */
232 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_MAC
);
236 /* Fail if the output length is zero */
238 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY_LENGTH
);
242 h
= EVP_MAC_CTX_get_mac_size(ctx
->ctx_init
);
245 if (ctx
->iv_len
!= 0 && ctx
->iv_len
!= h
) {
246 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_SEED_LENGTH
);
251 l
= be32(keylen
* 8);
253 k_i
= OPENSSL_zalloc(h
);
257 ret
= derive(ctx
->ctx_init
, ctx
->mode
, ctx
->iv
, ctx
->iv_len
, ctx
->label
,
258 ctx
->label_len
, ctx
->context
, ctx
->context_len
, k_i
, h
, l
,
259 ctx
->use_separator
, key
, keylen
);
262 OPENSSL_cleanse(key
, keylen
);
263 OPENSSL_clear_free(k_i
, h
);
267 static int kbkdf_set_buffer(unsigned char **out
, size_t *out_len
,
270 if (p
->data
== NULL
|| p
->data_size
== 0)
273 OPENSSL_clear_free(*out
, *out_len
);
275 return OSSL_PARAM_get_octet_string(p
, (void **)out
, 0, out_len
);
278 static int kbkdf_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
280 KBKDF
*ctx
= (KBKDF
*)vctx
;
281 OSSL_LIB_CTX
*libctx
= PROV_LIBCTX_OF(ctx
->provctx
);
283 OSSL_PARAM mparams
[2];
285 if (!ossl_prov_macctx_load_from_params(&ctx
->ctx_init
, params
, NULL
,
288 else if (ctx
->ctx_init
!= NULL
289 && !EVP_MAC_is_a(EVP_MAC_CTX_mac(ctx
->ctx_init
),
291 && !EVP_MAC_is_a(EVP_MAC_CTX_mac(ctx
->ctx_init
),
292 OSSL_MAC_NAME_CMAC
)) {
293 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_MAC
);
297 p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_MODE
);
298 if (p
!= NULL
&& strncasecmp("counter", p
->data
, p
->data_size
) == 0) {
301 && strncasecmp("feedback", p
->data
, p
->data_size
) == 0) {
302 ctx
->mode
= FEEDBACK
;
303 } else if (p
!= NULL
) {
304 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_MODE
);
308 p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_KEY
);
309 if (p
!= NULL
&& !kbkdf_set_buffer(&ctx
->ki
, &ctx
->ki_len
, p
))
312 p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_SALT
);
313 if (p
!= NULL
&& !kbkdf_set_buffer(&ctx
->label
, &ctx
->label_len
, p
))
316 p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_INFO
);
317 if (p
!= NULL
&& !kbkdf_set_buffer(&ctx
->context
, &ctx
->context_len
, p
))
320 p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_SEED
);
321 if (p
!= NULL
&& !kbkdf_set_buffer(&ctx
->iv
, &ctx
->iv_len
, p
))
324 p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_KBKDF_USE_L
);
325 if (p
!= NULL
&& !OSSL_PARAM_get_int(p
, &ctx
->use_l
))
328 p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR
);
329 if (p
!= NULL
&& !OSSL_PARAM_get_int(p
, &ctx
->use_separator
))
332 /* Set up digest context, if we can. */
333 if (ctx
->ctx_init
!= NULL
&& ctx
->ki_len
!= 0) {
334 mparams
[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
335 ctx
->ki
, ctx
->ki_len
);
336 mparams
[1] = OSSL_PARAM_construct_end();
338 if (!EVP_MAC_CTX_set_params(ctx
->ctx_init
, mparams
)
339 || !EVP_MAC_init(ctx
->ctx_init
))
346 static const OSSL_PARAM
*kbkdf_settable_ctx_params(ossl_unused
void *provctx
)
348 static const OSSL_PARAM known_settable_ctx_params
[] = {
349 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO
, NULL
, 0),
350 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT
, NULL
, 0),
351 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY
, NULL
, 0),
352 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED
, NULL
, 0),
353 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST
, NULL
, 0),
354 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER
, NULL
, 0),
355 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC
, NULL
, 0),
356 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE
, NULL
, 0),
357 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES
, NULL
, 0),
358 OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_L
, NULL
),
359 OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR
, NULL
),
362 return known_settable_ctx_params
;
365 static int kbkdf_get_ctx_params(void *vctx
, OSSL_PARAM params
[])
369 p
= OSSL_PARAM_locate(params
, OSSL_KDF_PARAM_SIZE
);
373 /* KBKDF can produce results as large as you like. */
374 return OSSL_PARAM_set_size_t(p
, SIZE_MAX
);
377 static const OSSL_PARAM
*kbkdf_gettable_ctx_params(ossl_unused
void *provctx
)
379 static const OSSL_PARAM known_gettable_ctx_params
[] =
380 { OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE
, NULL
), OSSL_PARAM_END
};
381 return known_gettable_ctx_params
;
384 const OSSL_DISPATCH ossl_kdf_kbkdf_functions
[] = {
385 { OSSL_FUNC_KDF_NEWCTX
, (void(*)(void))kbkdf_new
},
386 { OSSL_FUNC_KDF_FREECTX
, (void(*)(void))kbkdf_free
},
387 { OSSL_FUNC_KDF_RESET
, (void(*)(void))kbkdf_reset
},
388 { OSSL_FUNC_KDF_DERIVE
, (void(*)(void))kbkdf_derive
},
389 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS
,
390 (void(*)(void))kbkdf_settable_ctx_params
},
391 { OSSL_FUNC_KDF_SET_CTX_PARAMS
, (void(*)(void))kbkdf_set_ctx_params
},
392 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS
,
393 (void(*)(void))kbkdf_gettable_ctx_params
},
394 { OSSL_FUNC_KDF_GET_CTX_PARAMS
, (void(*)(void))kbkdf_get_ctx_params
},