2 * Copyright 2019 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 "internal/cryptlib.h"
46 #include "internal/evp_int.h"
47 #include "kdf_local.h"
49 struct evp_kdf_impl_st {
50 EVP_MAC *mac; /* H(x) = HMAC_hash OR H(x) = KMAC */
51 const EVP_MD *md; /* H(x) = hash OR when H(x) = HMAC_hash */
52 unsigned char *secret;
58 size_t out_len; /* optional KMAC parameter */
61 #define SSKDF_MAX_INLEN (1<<30)
62 #define SSKDF_KMAC128_DEFAULT_SALT_SIZE (168 - 4)
63 #define SSKDF_KMAC256_DEFAULT_SALT_SIZE (136 - 4)
65 /* KMAC uses a Customisation string of 'KDF' */
66 static const unsigned char kmac_custom_str[] = { 0x4B, 0x44, 0x46 };
69 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
70 * Section 4. One-Step Key Derivation using H(x) = hash(x)
71 * Note: X9.63 also uses this code with the only difference being that the
72 * counter is appended to the secret 'z'.
74 * result[i] = Hash(counter || z || info) for One Step OR
75 * result[i] = Hash(z || counter || info) for X9.63.
77 static int SSKDF_hash_kdm(const EVP_MD *kdf_md,
78 const unsigned char *z, size_t z_len,
79 const unsigned char *info, size_t info_len,
80 unsigned int append_ctr,
81 unsigned char *derived_key, size_t derived_key_len)
84 size_t counter, out_len, len = derived_key_len;
86 unsigned char mac[EVP_MAX_MD_SIZE];
87 unsigned char *out = derived_key;
88 EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
90 if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN
91 || derived_key_len > SSKDF_MAX_INLEN
92 || derived_key_len == 0)
95 hlen = EVP_MD_size(kdf_md);
98 out_len = (size_t)hlen;
100 ctx = EVP_MD_CTX_create();
101 ctx_init = EVP_MD_CTX_create();
102 if (ctx == NULL || ctx_init == NULL)
105 if (!EVP_DigestInit(ctx_init, kdf_md))
108 for (counter = 1;; counter++) {
109 c[0] = (unsigned char)((counter >> 24) & 0xff);
110 c[1] = (unsigned char)((counter >> 16) & 0xff);
111 c[2] = (unsigned char)((counter >> 8) & 0xff);
112 c[3] = (unsigned char)(counter & 0xff);
114 if (!(EVP_MD_CTX_copy_ex(ctx, ctx_init)
115 && (append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c)))
116 && EVP_DigestUpdate(ctx, z, z_len)
117 && (!append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c)))
118 && EVP_DigestUpdate(ctx, info, info_len)))
120 if (len >= out_len) {
121 if (!EVP_DigestFinal_ex(ctx, out, NULL))
128 if (!EVP_DigestFinal_ex(ctx, mac, NULL))
130 memcpy(out, mac, len);
136 EVP_MD_CTX_destroy(ctx);
137 EVP_MD_CTX_destroy(ctx_init);
138 OPENSSL_cleanse(mac, sizeof(mac));
142 static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom,
143 size_t custom_len, size_t kmac_out_len,
144 size_t derived_key_len, unsigned char **out)
146 OSSL_PARAM params[2];
148 /* Only KMAC has custom data - so return if not KMAC */
152 params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
153 (void *)custom, custom_len);
154 params[1] = OSSL_PARAM_construct_end();
156 if (!EVP_MAC_CTX_set_params(ctx, params))
159 /* By default only do one iteration if kmac_out_len is not specified */
160 if (kmac_out_len == 0)
161 kmac_out_len = derived_key_len;
162 /* otherwise check the size is valid */
163 else if (!(kmac_out_len == derived_key_len
164 || kmac_out_len == 20
165 || kmac_out_len == 28
166 || kmac_out_len == 32
167 || kmac_out_len == 48
168 || kmac_out_len == 64))
171 params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_OUTLEN,
174 if (EVP_MAC_CTX_set_params(ctx, params) <= 0)
178 * For kmac the output buffer can be larger than EVP_MAX_MD_SIZE: so
179 * alloc a buffer for this case.
181 if (kmac_out_len > EVP_MAX_MD_SIZE) {
182 *out = OPENSSL_zalloc(kmac_out_len);
190 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
191 * Section 4. One-Step Key Derivation using MAC: i.e either
192 * H(x) = HMAC-hash(salt, x) OR
193 * H(x) = KMAC#(salt, x, outbits, CustomString='KDF')
195 static int SSKDF_mac_kdm(EVP_MAC *kdf_mac, const EVP_MD *hmac_md,
196 const unsigned char *kmac_custom,
197 size_t kmac_custom_len, size_t kmac_out_len,
198 const unsigned char *salt, size_t salt_len,
199 const unsigned char *z, size_t z_len,
200 const unsigned char *info, size_t info_len,
201 unsigned char *derived_key, size_t derived_key_len)
204 size_t counter, out_len, len;
206 unsigned char mac_buf[EVP_MAX_MD_SIZE];
207 unsigned char *out = derived_key;
208 EVP_MAC_CTX *ctx = NULL, *ctx_init = NULL;
209 unsigned char *mac = mac_buf, *kmac_buffer = NULL;
210 OSSL_PARAM params[3];
213 if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN
214 || derived_key_len > SSKDF_MAX_INLEN
215 || derived_key_len == 0)
218 ctx_init = EVP_MAC_CTX_new(kdf_mac);
219 if (ctx_init == NULL)
222 if (hmac_md != NULL) {
223 const char *mdname = EVP_MD_name(hmac_md);
225 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ALGORITHM,
230 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, (void *)salt,
232 params[params_n] = OSSL_PARAM_construct_end();
234 if (!EVP_MAC_CTX_set_params(ctx_init, params))
237 if (!kmac_init(ctx_init, kmac_custom, kmac_custom_len, kmac_out_len,
238 derived_key_len, &kmac_buffer))
240 if (kmac_buffer != NULL)
243 if (!EVP_MAC_init(ctx_init))
246 out_len = EVP_MAC_size(ctx_init); /* output size */
249 len = derived_key_len;
251 for (counter = 1;; counter++) {
252 c[0] = (unsigned char)((counter >> 24) & 0xff);
253 c[1] = (unsigned char)((counter >> 16) & 0xff);
254 c[2] = (unsigned char)((counter >> 8) & 0xff);
255 c[3] = (unsigned char)(counter & 0xff);
257 ctx = EVP_MAC_CTX_dup(ctx_init);
259 && EVP_MAC_update(ctx, c, sizeof(c))
260 && EVP_MAC_update(ctx, z, z_len)
261 && EVP_MAC_update(ctx, info, info_len)))
263 if (len >= out_len) {
264 if (!EVP_MAC_final(ctx, out, NULL, len))
271 if (!EVP_MAC_final(ctx, mac, NULL, len))
273 memcpy(out, mac, len);
276 EVP_MAC_CTX_free(ctx);
281 if (kmac_buffer != NULL)
282 OPENSSL_clear_free(kmac_buffer, kmac_out_len);
284 OPENSSL_cleanse(mac_buf, sizeof(mac_buf));
286 EVP_MAC_CTX_free(ctx);
287 EVP_MAC_CTX_free(ctx_init);
291 static EVP_KDF_IMPL *sskdf_new(void)
295 if ((impl = OPENSSL_zalloc(sizeof(*impl))) == NULL)
296 KDFerr(KDF_F_SSKDF_NEW, ERR_R_MALLOC_FAILURE);
300 static void sskdf_reset(EVP_KDF_IMPL *impl)
302 OPENSSL_clear_free(impl->secret, impl->secret_len);
303 OPENSSL_clear_free(impl->info, impl->info_len);
304 OPENSSL_clear_free(impl->salt, impl->salt_len);
305 EVP_MAC_free(impl->mac);
306 #if 0 /* TODO(3.0) When we switch to fetched MDs */
307 EVP_MD_meth_free(impl->md);
309 memset(impl, 0, sizeof(*impl));
312 static void sskdf_free(EVP_KDF_IMPL *impl)
318 static int sskdf_set_buffer(va_list args, unsigned char **out, size_t *out_len)
320 const unsigned char *p;
323 p = va_arg(args, const unsigned char *);
324 len = va_arg(args, size_t);
325 if (len == 0 || p == NULL)
329 *out = OPENSSL_memdup(p, len);
337 static int sskdf_ctrl(EVP_KDF_IMPL *impl, int cmd, va_list args)
342 case EVP_KDF_CTRL_SET_KEY:
343 return sskdf_set_buffer(args, &impl->secret, &impl->secret_len);
345 case EVP_KDF_CTRL_SET_SSKDF_INFO:
346 return sskdf_set_buffer(args, &impl->info, &impl->info_len);
348 case EVP_KDF_CTRL_SET_MD:
349 md = va_arg(args, const EVP_MD *);
353 #if 0 /* TODO(3.0) When we switch to fetched MDs */
354 EVP_MD_meth_free(impl->md);
359 case EVP_KDF_CTRL_SET_MAC:
364 name = va_arg(args, const char *);
368 EVP_MAC_free(impl->mac);
372 * TODO(3.0) add support for OPENSSL_CTX and properties in KDFs
374 mac = EVP_MAC_fetch(NULL, name, NULL);
381 case EVP_KDF_CTRL_SET_SALT:
382 return sskdf_set_buffer(args, &impl->salt, &impl->salt_len);
384 case EVP_KDF_CTRL_SET_MAC_SIZE:
385 impl->out_len = va_arg(args, size_t);
393 static int sskdf_ctrl_str(EVP_KDF_IMPL *impl, const char *type,
396 if (strcmp(type, "secret") == 0 || strcmp(type, "key") == 0)
397 return kdf_str2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_KEY,
400 if (strcmp(type, "hexsecret") == 0 || strcmp(type, "hexkey") == 0)
401 return kdf_hex2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_KEY,
404 if (strcmp(type, "info") == 0)
405 return kdf_str2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_SSKDF_INFO,
408 if (strcmp(type, "hexinfo") == 0)
409 return kdf_hex2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_SSKDF_INFO,
412 if (strcmp(type, "digest") == 0)
413 return kdf_md2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_MD, value);
415 if (strcmp(type, "mac") == 0)
416 return kdf_str2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_MAC, value);
418 if (strcmp(type, "salt") == 0)
419 return kdf_str2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_SALT, value);
421 if (strcmp(type, "hexsalt") == 0)
422 return kdf_hex2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_SALT, value);
425 if (strcmp(type, "maclen") == 0) {
426 int val = atoi(value);
428 KDFerr(KDF_F_SSKDF_CTRL_STR, KDF_R_VALUE_ERROR);
431 return call_ctrl(sskdf_ctrl, impl, EVP_KDF_CTRL_SET_MAC_SIZE,
437 static size_t sskdf_size(EVP_KDF_IMPL *impl)
441 if (impl->md == NULL) {
442 KDFerr(KDF_F_SSKDF_SIZE, KDF_R_MISSING_MESSAGE_DIGEST);
445 len = EVP_MD_size(impl->md);
446 return (len <= 0) ? 0 : (size_t)len;
449 static int sskdf_derive(EVP_KDF_IMPL *impl, unsigned char *key, size_t keylen)
451 if (impl->secret == NULL) {
452 KDFerr(KDF_F_SSKDF_DERIVE, KDF_R_MISSING_SECRET);
456 if (impl->mac != NULL) {
457 /* H(x) = KMAC or H(x) = HMAC */
459 const unsigned char *custom = NULL;
460 size_t custom_len = 0;
462 int default_salt_len;
465 * TODO(3.0) investigate the necessity to have all these controls.
466 * Why does KMAC require a salt length that's shorter than the MD
469 macname = EVP_MAC_name(impl->mac);
470 if (strcmp(macname, "HMAC") == 0) {
471 /* H(x) = HMAC(x, salt, hash) */
472 if (impl->md == NULL) {
473 KDFerr(KDF_F_SSKDF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
476 default_salt_len = EVP_MD_block_size(impl->md);
477 if (default_salt_len <= 0)
479 } else if (strcmp(macname, "KMAC128") == 0
480 || strcmp(macname, "KMAC256") == 0) {
481 /* H(x) = KMACzzz(x, salt, custom) */
482 custom = kmac_custom_str;
483 custom_len = sizeof(kmac_custom_str);
484 if (strcmp(macname, "KMAC128") == 0)
485 default_salt_len = SSKDF_KMAC128_DEFAULT_SALT_SIZE;
487 default_salt_len = SSKDF_KMAC256_DEFAULT_SALT_SIZE;
489 KDFerr(KDF_F_SSKDF_DERIVE, KDF_R_UNSUPPORTED_MAC_TYPE);
492 /* If no salt is set then use a default_salt of zeros */
493 if (impl->salt == NULL || impl->salt_len <= 0) {
494 impl->salt = OPENSSL_zalloc(default_salt_len);
495 if (impl->salt == NULL) {
496 KDFerr(KDF_F_SSKDF_DERIVE, ERR_R_MALLOC_FAILURE);
499 impl->salt_len = default_salt_len;
501 ret = SSKDF_mac_kdm(impl->mac, impl->md,
502 custom, custom_len, impl->out_len,
503 impl->salt, impl->salt_len,
504 impl->secret, impl->secret_len,
505 impl->info, impl->info_len, key, keylen);
509 if (impl->md == NULL) {
510 KDFerr(KDF_F_SSKDF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
513 return SSKDF_hash_kdm(impl->md, impl->secret, impl->secret_len,
514 impl->info, impl->info_len, 0, key, keylen);
518 static int x963kdf_derive(EVP_KDF_IMPL *impl, unsigned char *key, size_t keylen)
520 if (impl->secret == NULL) {
521 KDFerr(KDF_F_X963KDF_DERIVE, KDF_R_MISSING_SECRET);
525 if (impl->mac != NULL) {
526 KDFerr(KDF_F_X963KDF_DERIVE, KDF_R_NOT_SUPPORTED);
530 if (impl->md == NULL) {
531 KDFerr(KDF_F_X963KDF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
534 return SSKDF_hash_kdm(impl->md, impl->secret, impl->secret_len,
535 impl->info, impl->info_len, 1, key, keylen);
539 const EVP_KDF ss_kdf_meth = {
550 const EVP_KDF x963_kdf_meth = {