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/numbers.h"
47 #include "internal/evp_int.h"
48 #include "internal/provider_ctx.h"
49 #include "internal/providercommonerr.h"
50 #include "internal/provider_algs.h"
54 EVP_MAC *mac; /* H(x) = HMAC_hash OR H(x) = KMAC */
55 EVP_MD *md; /* H(x) = hash OR when H(x) = HMAC_hash */
56 unsigned char *secret;
62 size_t out_len; /* optional KMAC parameter */
65 #define SSKDF_MAX_INLEN (1<<30)
66 #define SSKDF_KMAC128_DEFAULT_SALT_SIZE (168 - 4)
67 #define SSKDF_KMAC256_DEFAULT_SALT_SIZE (136 - 4)
69 /* KMAC uses a Customisation string of 'KDF' */
70 static const unsigned char kmac_custom_str[] = { 0x4B, 0x44, 0x46 };
72 static OSSL_OP_kdf_newctx_fn sskdf_new;
73 static OSSL_OP_kdf_freectx_fn sskdf_free;
74 static OSSL_OP_kdf_reset_fn sskdf_reset;
75 static OSSL_OP_kdf_derive_fn sskdf_derive;
76 static OSSL_OP_kdf_derive_fn x963kdf_derive;
77 static OSSL_OP_kdf_settable_ctx_params_fn sskdf_settable_ctx_params;
78 static OSSL_OP_kdf_set_ctx_params_fn sskdf_set_ctx_params;
79 static OSSL_OP_kdf_gettable_ctx_params_fn sskdf_gettable_ctx_params;
80 static OSSL_OP_kdf_get_ctx_params_fn sskdf_get_ctx_params;
83 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
84 * Section 4. One-Step Key Derivation using H(x) = hash(x)
85 * Note: X9.63 also uses this code with the only difference being that the
86 * counter is appended to the secret 'z'.
88 * result[i] = Hash(counter || z || info) for One Step OR
89 * result[i] = Hash(z || counter || info) for X9.63.
91 static int SSKDF_hash_kdm(const EVP_MD *kdf_md,
92 const unsigned char *z, size_t z_len,
93 const unsigned char *info, size_t info_len,
94 unsigned int append_ctr,
95 unsigned char *derived_key, size_t derived_key_len)
98 size_t counter, out_len, len = derived_key_len;
100 unsigned char mac[EVP_MAX_MD_SIZE];
101 unsigned char *out = derived_key;
102 EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
104 if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN
105 || derived_key_len > SSKDF_MAX_INLEN
106 || derived_key_len == 0)
109 hlen = EVP_MD_size(kdf_md);
112 out_len = (size_t)hlen;
114 ctx = EVP_MD_CTX_create();
115 ctx_init = EVP_MD_CTX_create();
116 if (ctx == NULL || ctx_init == NULL)
119 if (!EVP_DigestInit(ctx_init, kdf_md))
122 for (counter = 1;; counter++) {
123 c[0] = (unsigned char)((counter >> 24) & 0xff);
124 c[1] = (unsigned char)((counter >> 16) & 0xff);
125 c[2] = (unsigned char)((counter >> 8) & 0xff);
126 c[3] = (unsigned char)(counter & 0xff);
128 if (!(EVP_MD_CTX_copy_ex(ctx, ctx_init)
129 && (append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c)))
130 && EVP_DigestUpdate(ctx, z, z_len)
131 && (!append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c)))
132 && EVP_DigestUpdate(ctx, info, info_len)))
134 if (len >= out_len) {
135 if (!EVP_DigestFinal_ex(ctx, out, NULL))
142 if (!EVP_DigestFinal_ex(ctx, mac, NULL))
144 memcpy(out, mac, len);
150 EVP_MD_CTX_destroy(ctx);
151 EVP_MD_CTX_destroy(ctx_init);
152 OPENSSL_cleanse(mac, sizeof(mac));
156 static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom,
157 size_t custom_len, size_t kmac_out_len,
158 size_t derived_key_len, unsigned char **out)
160 OSSL_PARAM params[2];
162 /* Only KMAC has custom data - so return if not KMAC */
166 params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
167 (void *)custom, custom_len);
168 params[1] = OSSL_PARAM_construct_end();
170 if (!EVP_MAC_CTX_set_params(ctx, params))
173 /* By default only do one iteration if kmac_out_len is not specified */
174 if (kmac_out_len == 0)
175 kmac_out_len = derived_key_len;
176 /* otherwise check the size is valid */
177 else if (!(kmac_out_len == derived_key_len
178 || kmac_out_len == 20
179 || kmac_out_len == 28
180 || kmac_out_len == 32
181 || kmac_out_len == 48
182 || kmac_out_len == 64))
185 params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE,
188 if (EVP_MAC_CTX_set_params(ctx, params) <= 0)
192 * For kmac the output buffer can be larger than EVP_MAX_MD_SIZE: so
193 * alloc a buffer for this case.
195 if (kmac_out_len > EVP_MAX_MD_SIZE) {
196 *out = OPENSSL_zalloc(kmac_out_len);
204 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
205 * Section 4. One-Step Key Derivation using MAC: i.e either
206 * H(x) = HMAC-hash(salt, x) OR
207 * H(x) = KMAC#(salt, x, outbits, CustomString='KDF')
209 static int SSKDF_mac_kdm(EVP_MAC *kdf_mac, const EVP_MD *hmac_md,
210 const unsigned char *kmac_custom,
211 size_t kmac_custom_len, size_t kmac_out_len,
212 const unsigned char *salt, size_t salt_len,
213 const unsigned char *z, size_t z_len,
214 const unsigned char *info, size_t info_len,
215 unsigned char *derived_key, size_t derived_key_len)
218 size_t counter, out_len, len;
220 unsigned char mac_buf[EVP_MAX_MD_SIZE];
221 unsigned char *out = derived_key;
222 EVP_MAC_CTX *ctx = NULL, *ctx_init = NULL;
223 unsigned char *mac = mac_buf, *kmac_buffer = NULL;
224 OSSL_PARAM params[3];
227 if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN
228 || derived_key_len > SSKDF_MAX_INLEN
229 || derived_key_len == 0)
232 ctx_init = EVP_MAC_CTX_new(kdf_mac);
233 if (ctx_init == NULL)
236 if (hmac_md != NULL) {
237 const char *mdname = EVP_MD_name(hmac_md);
239 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
243 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, (void *)salt,
245 params[params_n] = OSSL_PARAM_construct_end();
247 if (!EVP_MAC_CTX_set_params(ctx_init, params))
250 if (!kmac_init(ctx_init, kmac_custom, kmac_custom_len, kmac_out_len,
251 derived_key_len, &kmac_buffer))
253 if (kmac_buffer != NULL)
256 if (!EVP_MAC_init(ctx_init))
259 out_len = EVP_MAC_size(ctx_init); /* output size */
262 len = derived_key_len;
264 for (counter = 1;; counter++) {
265 c[0] = (unsigned char)((counter >> 24) & 0xff);
266 c[1] = (unsigned char)((counter >> 16) & 0xff);
267 c[2] = (unsigned char)((counter >> 8) & 0xff);
268 c[3] = (unsigned char)(counter & 0xff);
270 ctx = EVP_MAC_CTX_dup(ctx_init);
272 && EVP_MAC_update(ctx, c, sizeof(c))
273 && EVP_MAC_update(ctx, z, z_len)
274 && EVP_MAC_update(ctx, info, info_len)))
276 if (len >= out_len) {
277 if (!EVP_MAC_final(ctx, out, NULL, len))
284 if (!EVP_MAC_final(ctx, mac, NULL, len))
286 memcpy(out, mac, len);
289 EVP_MAC_CTX_free(ctx);
294 if (kmac_buffer != NULL)
295 OPENSSL_clear_free(kmac_buffer, kmac_out_len);
297 OPENSSL_cleanse(mac_buf, sizeof(mac_buf));
299 EVP_MAC_CTX_free(ctx);
300 EVP_MAC_CTX_free(ctx_init);
304 static void *sskdf_new(void *provctx)
308 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
309 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
310 ctx->provctx = provctx;
314 static void sskdf_reset(void *vctx)
316 KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
318 EVP_MD_meth_free(ctx->md);
319 EVP_MAC_free(ctx->mac);
320 OPENSSL_clear_free(ctx->secret, ctx->secret_len);
321 OPENSSL_clear_free(ctx->info, ctx->info_len);
322 OPENSSL_clear_free(ctx->salt, ctx->salt_len);
323 memset(ctx, 0, sizeof(*ctx));
326 static void sskdf_free(void *vctx)
328 KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
334 static int sskdf_set_buffer(unsigned char **out, size_t *out_len,
337 if (p->data == NULL || p->data_size == 0)
341 return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
344 static size_t sskdf_size(KDF_SSKDF *ctx)
348 if (ctx->md == NULL) {
349 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
352 len = EVP_MD_size(ctx->md);
353 return (len <= 0) ? 0 : (size_t)len;
356 static int sskdf_derive(void *vctx, unsigned char *key, size_t keylen)
358 KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
360 if (ctx->secret == NULL) {
361 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
365 if (ctx->mac != NULL) {
366 /* H(x) = KMAC or H(x) = HMAC */
368 const unsigned char *custom = NULL;
369 size_t custom_len = 0;
371 int default_salt_len;
374 * TODO(3.0) investigate the necessity to have all these controls.
375 * Why does KMAC require a salt length that's shorter than the MD
378 macname = EVP_MAC_name(ctx->mac);
379 if (strcmp(macname, OSSL_MAC_NAME_HMAC) == 0) {
380 /* H(x) = HMAC(x, salt, hash) */
381 if (ctx->md == NULL) {
382 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
385 default_salt_len = EVP_MD_block_size(ctx->md);
386 if (default_salt_len <= 0)
388 } else if (strcmp(macname, OSSL_MAC_NAME_KMAC128) == 0
389 || strcmp(macname, OSSL_MAC_NAME_KMAC256) == 0) {
390 /* H(x) = KMACzzz(x, salt, custom) */
391 custom = kmac_custom_str;
392 custom_len = sizeof(kmac_custom_str);
393 if (strcmp(macname, OSSL_MAC_NAME_KMAC128) == 0)
394 default_salt_len = SSKDF_KMAC128_DEFAULT_SALT_SIZE;
396 default_salt_len = SSKDF_KMAC256_DEFAULT_SALT_SIZE;
398 ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_MAC_TYPE);
401 /* If no salt is set then use a default_salt of zeros */
402 if (ctx->salt == NULL || ctx->salt_len <= 0) {
403 ctx->salt = OPENSSL_zalloc(default_salt_len);
404 if (ctx->salt == NULL) {
405 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
408 ctx->salt_len = default_salt_len;
410 ret = SSKDF_mac_kdm(ctx->mac, ctx->md,
411 custom, custom_len, ctx->out_len,
412 ctx->salt, ctx->salt_len,
413 ctx->secret, ctx->secret_len,
414 ctx->info, ctx->info_len, key, keylen);
418 if (ctx->md == NULL) {
419 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
422 return SSKDF_hash_kdm(ctx->md, ctx->secret, ctx->secret_len,
423 ctx->info, ctx->info_len, 0, key, keylen);
427 static int x963kdf_derive(void *vctx, unsigned char *key, size_t keylen)
429 KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
431 if (ctx->secret == NULL) {
432 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
436 if (ctx->mac != NULL) {
437 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED);
441 if (ctx->md == NULL) {
442 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
445 return SSKDF_hash_kdm(ctx->md, ctx->secret, ctx->secret_len,
446 ctx->info, ctx->info_len, 1, key, keylen);
450 static int sskdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
453 KDF_SSKDF *ctx = vctx;
457 const char *properties = NULL;
459 /* Grab search properties, should be before the digest and mac lookups */
460 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PROPERTIES))
462 if (p->data_type != OSSL_PARAM_UTF8_STRING)
464 properties = p->data;
466 /* Handle aliasing of digest parameter names */
467 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
468 if (p->data_type != OSSL_PARAM_UTF8_STRING)
470 md = EVP_MD_fetch(PROV_LIBRARY_CONTEXT_OF(ctx->provctx), p->data,
473 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
476 EVP_MD_meth_free(ctx->md);
480 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MAC)) != NULL) {
481 EVP_MAC_free(ctx->mac);
484 mac = EVP_MAC_fetch(PROV_LIBRARY_CONTEXT_OF(ctx->provctx), p->data,
488 EVP_MAC_free(ctx->mac);
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(void)
516 static const OSSL_PARAM known_settable_ctx_params[] = {
517 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
518 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
519 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
520 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
521 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
522 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
523 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
524 OSSL_PARAM_size_t(OSSL_KDF_PARAM_MAC_SIZE, NULL),
527 return known_settable_ctx_params;
530 static int sskdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
532 KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
535 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
536 return OSSL_PARAM_set_size_t(p, sskdf_size(ctx));
540 static const OSSL_PARAM *sskdf_gettable_ctx_params(void)
542 static const OSSL_PARAM known_gettable_ctx_params[] = {
543 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
546 return known_gettable_ctx_params;
549 const OSSL_DISPATCH kdf_sskdf_functions[] = {
550 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))sskdf_new },
551 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))sskdf_free },
552 { OSSL_FUNC_KDF_RESET, (void(*)(void))sskdf_reset },
553 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))sskdf_derive },
554 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
555 (void(*)(void))sskdf_settable_ctx_params },
556 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))sskdf_set_ctx_params },
557 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
558 (void(*)(void))sskdf_gettable_ctx_params },
559 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))sskdf_get_ctx_params },
563 const OSSL_DISPATCH kdf_x963_kdf_functions[] = {
564 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))sskdf_new },
565 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))sskdf_free },
566 { OSSL_FUNC_KDF_RESET, (void(*)(void))sskdf_reset },
567 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x963kdf_derive },
568 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
569 (void(*)(void))sskdf_settable_ctx_params },
570 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))sskdf_set_ctx_params },
571 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
572 (void(*)(void))sskdf_gettable_ctx_params },
573 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))sskdf_get_ctx_params },