2 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
15 * The Single Step KDF algorithm is given by:
17 * Result(0) = empty bit string (i.e., the null string).
18 * For i = 1 to reps, do the following:
19 * Increment counter by 1.
20 * Result(i) = Result(i - 1) || H(counter || Z || FixedInfo).
21 * DKM = LeftmostBits(Result(reps), L))
24 * Z is a shared secret required to produce the derived key material.
25 * counter is a 4 byte buffer.
26 * FixedInfo is a bit string containing context specific data.
27 * DKM is the output derived key material.
28 * L is the required size of the DKM.
29 * reps = [L / H_outputBits]
30 * H(x) is the auxiliary function that can be either a hash, HMAC or KMAC.
31 * H_outputBits is the length of the output of the auxiliary function H(x).
33 * Currently there is not a comprehensive list of test vectors for this
34 * algorithm, especially for H(x) = HMAC and H(x) = KMAC.
35 * Test vectors for H(x) = Hash are indirectly used by CAVS KAS tests.
40 #include <openssl/hmac.h>
41 #include <openssl/evp.h>
42 #include <openssl/kdf.h>
43 #include <openssl/core_names.h>
44 #include <openssl/params.h>
45 #include <openssl/proverr.h>
46 #include "internal/cryptlib.h"
47 #include "internal/numbers.h"
48 #include "crypto/evp.h"
49 #include "prov/provider_ctx.h"
50 #include "prov/providercommon.h"
51 #include "prov/implementations.h"
52 #include "prov/provider_util.h"
56 EVP_MAC_CTX
*macctx
; /* H(x) = HMAC_hash OR H(x) = KMAC */
57 PROV_DIGEST digest
; /* H(x) = hash(x) */
58 unsigned char *secret
;
64 size_t out_len
; /* optional KMAC parameter */
67 #define SSKDF_MAX_INLEN (1<<30)
68 #define SSKDF_KMAC128_DEFAULT_SALT_SIZE (168 - 4)
69 #define SSKDF_KMAC256_DEFAULT_SALT_SIZE (136 - 4)
71 /* KMAC uses a Customisation string of 'KDF' */
72 static const unsigned char kmac_custom_str
[] = { 0x4B, 0x44, 0x46 };
74 static OSSL_FUNC_kdf_newctx_fn sskdf_new
;
75 static OSSL_FUNC_kdf_dupctx_fn sskdf_dup
;
76 static OSSL_FUNC_kdf_freectx_fn sskdf_free
;
77 static OSSL_FUNC_kdf_reset_fn sskdf_reset
;
78 static OSSL_FUNC_kdf_derive_fn sskdf_derive
;
79 static OSSL_FUNC_kdf_derive_fn x963kdf_derive
;
80 static OSSL_FUNC_kdf_settable_ctx_params_fn sskdf_settable_ctx_params
;
81 static OSSL_FUNC_kdf_set_ctx_params_fn sskdf_set_ctx_params
;
82 static OSSL_FUNC_kdf_gettable_ctx_params_fn sskdf_gettable_ctx_params
;
83 static OSSL_FUNC_kdf_get_ctx_params_fn sskdf_get_ctx_params
;
86 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
87 * Section 4. One-Step Key Derivation using H(x) = hash(x)
88 * Note: X9.63 also uses this code with the only difference being that the
89 * counter is appended to the secret 'z'.
91 * result[i] = Hash(counter || z || info) for One Step OR
92 * result[i] = Hash(z || counter || info) for X9.63.
94 static int SSKDF_hash_kdm(const EVP_MD
*kdf_md
,
95 const unsigned char *z
, size_t z_len
,
96 const unsigned char *info
, size_t info_len
,
97 unsigned int append_ctr
,
98 unsigned char *derived_key
, size_t derived_key_len
)
101 size_t counter
, out_len
, len
= derived_key_len
;
103 unsigned char mac
[EVP_MAX_MD_SIZE
];
104 unsigned char *out
= derived_key
;
105 EVP_MD_CTX
*ctx
= NULL
, *ctx_init
= NULL
;
107 if (z_len
> SSKDF_MAX_INLEN
|| info_len
> SSKDF_MAX_INLEN
108 || derived_key_len
> SSKDF_MAX_INLEN
109 || derived_key_len
== 0)
112 hlen
= EVP_MD_get_size(kdf_md
);
115 out_len
= (size_t)hlen
;
117 ctx
= EVP_MD_CTX_create();
118 ctx_init
= EVP_MD_CTX_create();
119 if (ctx
== NULL
|| ctx_init
== NULL
)
122 if (!EVP_DigestInit(ctx_init
, kdf_md
))
125 for (counter
= 1;; counter
++) {
126 c
[0] = (unsigned char)((counter
>> 24) & 0xff);
127 c
[1] = (unsigned char)((counter
>> 16) & 0xff);
128 c
[2] = (unsigned char)((counter
>> 8) & 0xff);
129 c
[3] = (unsigned char)(counter
& 0xff);
131 if (!(EVP_MD_CTX_copy_ex(ctx
, ctx_init
)
132 && (append_ctr
|| EVP_DigestUpdate(ctx
, c
, sizeof(c
)))
133 && EVP_DigestUpdate(ctx
, z
, z_len
)
134 && (!append_ctr
|| EVP_DigestUpdate(ctx
, c
, sizeof(c
)))
135 && EVP_DigestUpdate(ctx
, info
, info_len
)))
137 if (len
>= out_len
) {
138 if (!EVP_DigestFinal_ex(ctx
, out
, NULL
))
145 if (!EVP_DigestFinal_ex(ctx
, mac
, NULL
))
147 memcpy(out
, mac
, len
);
153 EVP_MD_CTX_destroy(ctx
);
154 EVP_MD_CTX_destroy(ctx_init
);
155 OPENSSL_cleanse(mac
, sizeof(mac
));
159 static int kmac_init(EVP_MAC_CTX
*ctx
, const unsigned char *custom
,
160 size_t custom_len
, size_t kmac_out_len
,
161 size_t derived_key_len
, unsigned char **out
)
163 OSSL_PARAM params
[2];
165 /* Only KMAC has custom data - so return if not KMAC */
169 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM
,
170 (void *)custom
, custom_len
);
171 params
[1] = OSSL_PARAM_construct_end();
173 if (!EVP_MAC_CTX_set_params(ctx
, params
))
176 /* By default only do one iteration if kmac_out_len is not specified */
177 if (kmac_out_len
== 0)
178 kmac_out_len
= derived_key_len
;
179 /* otherwise check the size is valid */
180 else if (!(kmac_out_len
== derived_key_len
181 || kmac_out_len
== 20
182 || kmac_out_len
== 28
183 || kmac_out_len
== 32
184 || kmac_out_len
== 48
185 || kmac_out_len
== 64))
188 params
[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE
,
191 if (EVP_MAC_CTX_set_params(ctx
, params
) <= 0)
195 * For kmac the output buffer can be larger than EVP_MAX_MD_SIZE: so
196 * alloc a buffer for this case.
198 if (kmac_out_len
> EVP_MAX_MD_SIZE
) {
199 *out
= OPENSSL_zalloc(kmac_out_len
);
207 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
208 * Section 4. One-Step Key Derivation using MAC: i.e either
209 * H(x) = HMAC-hash(salt, x) OR
210 * H(x) = KMAC#(salt, x, outbits, CustomString='KDF')
212 static int SSKDF_mac_kdm(EVP_MAC_CTX
*ctx_init
,
213 const unsigned char *kmac_custom
,
214 size_t kmac_custom_len
, size_t kmac_out_len
,
215 const unsigned char *salt
, size_t salt_len
,
216 const unsigned char *z
, size_t z_len
,
217 const unsigned char *info
, size_t info_len
,
218 unsigned char *derived_key
, size_t derived_key_len
)
221 size_t counter
, out_len
, len
;
223 unsigned char mac_buf
[EVP_MAX_MD_SIZE
];
224 unsigned char *out
= derived_key
;
225 EVP_MAC_CTX
*ctx
= NULL
;
226 unsigned char *mac
= mac_buf
, *kmac_buffer
= NULL
;
228 if (z_len
> SSKDF_MAX_INLEN
|| info_len
> SSKDF_MAX_INLEN
229 || derived_key_len
> SSKDF_MAX_INLEN
230 || derived_key_len
== 0)
233 if (!kmac_init(ctx_init
, kmac_custom
, kmac_custom_len
, kmac_out_len
,
234 derived_key_len
, &kmac_buffer
))
236 if (kmac_buffer
!= NULL
)
239 if (!EVP_MAC_init(ctx_init
, salt
, salt_len
, NULL
))
242 out_len
= EVP_MAC_CTX_get_mac_size(ctx_init
); /* output size */
243 if (out_len
<= 0 || (mac
== mac_buf
&& out_len
> sizeof(mac_buf
)))
245 len
= derived_key_len
;
247 for (counter
= 1;; counter
++) {
248 c
[0] = (unsigned char)((counter
>> 24) & 0xff);
249 c
[1] = (unsigned char)((counter
>> 16) & 0xff);
250 c
[2] = (unsigned char)((counter
>> 8) & 0xff);
251 c
[3] = (unsigned char)(counter
& 0xff);
253 ctx
= EVP_MAC_CTX_dup(ctx_init
);
255 && EVP_MAC_update(ctx
, c
, sizeof(c
))
256 && EVP_MAC_update(ctx
, z
, z_len
)
257 && EVP_MAC_update(ctx
, info
, info_len
)))
259 if (len
>= out_len
) {
260 if (!EVP_MAC_final(ctx
, out
, NULL
, len
))
267 if (!EVP_MAC_final(ctx
, mac
, NULL
, out_len
))
269 memcpy(out
, mac
, len
);
272 EVP_MAC_CTX_free(ctx
);
277 if (kmac_buffer
!= NULL
)
278 OPENSSL_clear_free(kmac_buffer
, kmac_out_len
);
280 OPENSSL_cleanse(mac_buf
, sizeof(mac_buf
));
282 EVP_MAC_CTX_free(ctx
);
286 static void *sskdf_new(void *provctx
)
290 if (!ossl_prov_is_running())
293 if ((ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
)
294 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
295 ctx
->provctx
= provctx
;
299 static void sskdf_reset(void *vctx
)
301 KDF_SSKDF
*ctx
= (KDF_SSKDF
*)vctx
;
302 void *provctx
= ctx
->provctx
;
304 EVP_MAC_CTX_free(ctx
->macctx
);
305 ossl_prov_digest_reset(&ctx
->digest
);
306 OPENSSL_clear_free(ctx
->secret
, ctx
->secret_len
);
307 OPENSSL_clear_free(ctx
->info
, ctx
->info_len
);
308 OPENSSL_clear_free(ctx
->salt
, ctx
->salt_len
);
309 memset(ctx
, 0, sizeof(*ctx
));
310 ctx
->provctx
= provctx
;
313 static void sskdf_free(void *vctx
)
315 KDF_SSKDF
*ctx
= (KDF_SSKDF
*)vctx
;
323 static void *sskdf_dup(void *vctx
)
325 const KDF_SSKDF
*src
= (const KDF_SSKDF
*)vctx
;
328 dest
= sskdf_new(src
->provctx
);
330 if (src
->macctx
!= NULL
) {
331 dest
->macctx
= EVP_MAC_CTX_dup(src
->macctx
);
332 if (dest
->macctx
== NULL
)
335 if (!ossl_prov_memdup(src
->info
, src
->info_len
,
336 &dest
->info
, &dest
->info_len
)
337 || !ossl_prov_memdup(src
->salt
, src
->salt_len
,
338 &dest
->salt
, &dest
->salt_len
)
339 || !ossl_prov_memdup(src
->secret
, src
->secret_len
,
340 &dest
->secret
, &dest
->secret_len
)
341 || !ossl_prov_digest_copy(&dest
->digest
, &src
->digest
))
343 dest
->out_len
= src
->out_len
;
352 static int sskdf_set_buffer(unsigned char **out
, size_t *out_len
,
355 if (p
->data
== NULL
|| p
->data_size
== 0)
359 return OSSL_PARAM_get_octet_string(p
, (void **)out
, 0, out_len
);
362 static size_t sskdf_size(KDF_SSKDF
*ctx
)
365 const EVP_MD
*md
= ossl_prov_digest_md(&ctx
->digest
);
368 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_MESSAGE_DIGEST
);
371 len
= EVP_MD_get_size(md
);
372 return (len
<= 0) ? 0 : (size_t)len
;
375 static int sskdf_derive(void *vctx
, unsigned char *key
, size_t keylen
,
376 const OSSL_PARAM params
[])
378 KDF_SSKDF
*ctx
= (KDF_SSKDF
*)vctx
;
381 if (!ossl_prov_is_running() || !sskdf_set_ctx_params(ctx
, params
))
383 if (ctx
->secret
== NULL
) {
384 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_SECRET
);
387 md
= ossl_prov_digest_md(&ctx
->digest
);
389 if (ctx
->macctx
!= NULL
) {
390 /* H(x) = KMAC or H(x) = HMAC */
392 const unsigned char *custom
= NULL
;
393 size_t custom_len
= 0;
394 int default_salt_len
;
395 EVP_MAC
*mac
= EVP_MAC_CTX_get0_mac(ctx
->macctx
);
397 if (EVP_MAC_is_a(mac
, OSSL_MAC_NAME_HMAC
)) {
398 /* H(x) = HMAC(x, salt, hash) */
400 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_MESSAGE_DIGEST
);
403 default_salt_len
= EVP_MD_get_size(md
);
404 if (default_salt_len
<= 0)
406 } else if (EVP_MAC_is_a(mac
, OSSL_MAC_NAME_KMAC128
)
407 || EVP_MAC_is_a(mac
, OSSL_MAC_NAME_KMAC256
)) {
408 /* H(x) = KMACzzz(x, salt, custom) */
409 custom
= kmac_custom_str
;
410 custom_len
= sizeof(kmac_custom_str
);
411 if (EVP_MAC_is_a(mac
, OSSL_MAC_NAME_KMAC128
))
412 default_salt_len
= SSKDF_KMAC128_DEFAULT_SALT_SIZE
;
414 default_salt_len
= SSKDF_KMAC256_DEFAULT_SALT_SIZE
;
416 ERR_raise(ERR_LIB_PROV
, PROV_R_UNSUPPORTED_MAC_TYPE
);
419 /* If no salt is set then use a default_salt of zeros */
420 if (ctx
->salt
== NULL
|| ctx
->salt_len
<= 0) {
421 ctx
->salt
= OPENSSL_zalloc(default_salt_len
);
422 if (ctx
->salt
== NULL
) {
423 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
426 ctx
->salt_len
= default_salt_len
;
428 ret
= SSKDF_mac_kdm(ctx
->macctx
,
429 custom
, custom_len
, ctx
->out_len
,
430 ctx
->salt
, ctx
->salt_len
,
431 ctx
->secret
, ctx
->secret_len
,
432 ctx
->info
, ctx
->info_len
, key
, keylen
);
437 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_MESSAGE_DIGEST
);
440 return SSKDF_hash_kdm(md
, ctx
->secret
, ctx
->secret_len
,
441 ctx
->info
, ctx
->info_len
, 0, key
, keylen
);
445 static int x963kdf_derive(void *vctx
, unsigned char *key
, size_t keylen
,
446 const OSSL_PARAM params
[])
448 KDF_SSKDF
*ctx
= (KDF_SSKDF
*)vctx
;
451 if (!ossl_prov_is_running() || !sskdf_set_ctx_params(ctx
, params
))
454 if (ctx
->secret
== NULL
) {
455 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_SECRET
);
459 if (ctx
->macctx
!= NULL
) {
460 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_SUPPORTED
);
465 md
= ossl_prov_digest_md(&ctx
->digest
);
467 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_MESSAGE_DIGEST
);
471 return SSKDF_hash_kdm(md
, ctx
->secret
, ctx
->secret_len
,
472 ctx
->info
, ctx
->info_len
, 1, key
, keylen
);
475 static int sskdf_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
478 KDF_SSKDF
*ctx
= vctx
;
479 OSSL_LIB_CTX
*libctx
= PROV_LIBCTX_OF(ctx
->provctx
);
485 if (!ossl_prov_digest_load_from_params(&ctx
->digest
, params
, libctx
))
488 if (!ossl_prov_macctx_load_from_params(&ctx
->macctx
, params
,
489 NULL
, NULL
, NULL
, libctx
))
492 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_SECRET
)) != NULL
493 || (p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_KEY
)) != NULL
)
494 if (!sskdf_set_buffer(&ctx
->secret
, &ctx
->secret_len
, p
))
497 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_INFO
)) != NULL
)
498 if (!sskdf_set_buffer(&ctx
->info
, &ctx
->info_len
, p
))
501 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_SALT
)) != NULL
)
502 if (!sskdf_set_buffer(&ctx
->salt
, &ctx
->salt_len
, p
))
505 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_MAC_SIZE
))
507 if (!OSSL_PARAM_get_size_t(p
, &sz
) || sz
== 0)
514 static const OSSL_PARAM
*sskdf_settable_ctx_params(ossl_unused
void *ctx
,
515 ossl_unused
void *provctx
)
517 static const OSSL_PARAM known_settable_ctx_params
[] = {
518 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET
, NULL
, 0),
519 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY
, NULL
, 0),
520 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO
, NULL
, 0),
521 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES
, NULL
, 0),
522 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST
, NULL
, 0),
523 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC
, NULL
, 0),
524 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT
, NULL
, 0),
525 OSSL_PARAM_size_t(OSSL_KDF_PARAM_MAC_SIZE
, NULL
),
528 return known_settable_ctx_params
;
531 static int sskdf_get_ctx_params(void *vctx
, OSSL_PARAM params
[])
533 KDF_SSKDF
*ctx
= (KDF_SSKDF
*)vctx
;
536 if ((p
= OSSL_PARAM_locate(params
, OSSL_KDF_PARAM_SIZE
)) != NULL
)
537 return OSSL_PARAM_set_size_t(p
, sskdf_size(ctx
));
541 static const OSSL_PARAM
*sskdf_gettable_ctx_params(ossl_unused
void *ctx
,
542 ossl_unused
void *provctx
)
544 static const OSSL_PARAM known_gettable_ctx_params
[] = {
545 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE
, NULL
),
548 return known_gettable_ctx_params
;
551 const OSSL_DISPATCH ossl_kdf_sskdf_functions
[] = {
552 { OSSL_FUNC_KDF_NEWCTX
, (void(*)(void))sskdf_new
},
553 { OSSL_FUNC_KDF_DUPCTX
, (void(*)(void))sskdf_dup
},
554 { OSSL_FUNC_KDF_FREECTX
, (void(*)(void))sskdf_free
},
555 { OSSL_FUNC_KDF_RESET
, (void(*)(void))sskdf_reset
},
556 { OSSL_FUNC_KDF_DERIVE
, (void(*)(void))sskdf_derive
},
557 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS
,
558 (void(*)(void))sskdf_settable_ctx_params
},
559 { OSSL_FUNC_KDF_SET_CTX_PARAMS
, (void(*)(void))sskdf_set_ctx_params
},
560 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS
,
561 (void(*)(void))sskdf_gettable_ctx_params
},
562 { OSSL_FUNC_KDF_GET_CTX_PARAMS
, (void(*)(void))sskdf_get_ctx_params
},
566 const OSSL_DISPATCH ossl_kdf_x963_kdf_functions
[] = {
567 { OSSL_FUNC_KDF_NEWCTX
, (void(*)(void))sskdf_new
},
568 { OSSL_FUNC_KDF_DUPCTX
, (void(*)(void))sskdf_dup
},
569 { OSSL_FUNC_KDF_FREECTX
, (void(*)(void))sskdf_free
},
570 { OSSL_FUNC_KDF_RESET
, (void(*)(void))sskdf_reset
},
571 { OSSL_FUNC_KDF_DERIVE
, (void(*)(void))x963kdf_derive
},
572 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS
,
573 (void(*)(void))sskdf_settable_ctx_params
},
574 { OSSL_FUNC_KDF_SET_CTX_PARAMS
, (void(*)(void))sskdf_set_ctx_params
},
575 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS
,
576 (void(*)(void))sskdf_gettable_ctx_params
},
577 { OSSL_FUNC_KDF_GET_CTX_PARAMS
, (void(*)(void))sskdf_get_ctx_params
},