EVP_KDF-HKDF(7ossl)

πŸ“¦
version 15.1 Β· 3 versions
, in freebsd 15.1

πŸ’… NAME πŸ’…

ok so bestie, gather round: EVP_KDF-HKDF is the HKDF EVP_KDF implementation β€” literally the whole manual page for it, from the OpenSSL manual set (that's 7ossl for the section, iykyk). πŸŽ€


✨ DESCRIPTION ✨

So this is all about computing the HKDF KDF through the EVP_KDF API. πŸ¦‹

The EVP_KDF-HKDF algorithm implements the HKDF key derivation function. HKDF follows the "extract-then-expand" paradigm, and the way that works is the KDF logically consists of two modules:

  1. The first stage takes the input keying material and "extracts" from it a fixed-length pseudorandom key K.
  2. The second stage "expands" the key K into several additional pseudorandom keys (the output of the KDF).

The output is considered to be keying material. periodt. πŸ’œ

🌸 Identity 🌸

"HKDF" is the name for this implementation β€” that's the icon, that's the name, no cap. You can use it with the EVP_KDF_fetch() function. ⭐

🫢 Supported parameters 🫢

The supported parameters are, and honestly? iconic lineup:

  • "properties" (OSSL_KDF_PARAM_PROPERTIES) <UTF8 string>
  • "digest" (OSSL_KDF_PARAM_DIGEST) <UTF8 string>
  • "key" (OSSL_KDF_PARAM_KEY) <octet string>
  • "salt" (OSSL_KDF_PARAM_SALT) <octet string>

These parameters work as described in "PARAMETERS" in EVP_KDF(3). 😌

  • "info" (OSSL_KDF_PARAM_INFO) <octet string> β€” this parameter sets the info value. The length of the context info buffer cannot exceed 1024 bytes; this should be more than enough for any normal use of HKDF. (like 1024 is already so much, don't be greedy babe πŸ’€)
  • "mode" (OSSL_KDF_PARAM_MODE) <UTF8 string> or <integer> β€” this parameter sets the mode for the HKDF operation. There are three modes that are currently defined:
    • "EXTRACT_AND_EXPAND" or EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND β€” this is the default mode. Calling EVP_KDF_derive(3) on an EVP_KDF_CTX set up for HKDF will perform an extract followed by an expand operation in one go. The derived key returned will be the result after the expand operation. The intermediate fixed-length pseudorandom key K is not returned. In this mode the digest, key, salt and info values must be set before a key is derived otherwise an error will occur. ⚠️
    • "EXTRACT_ONLY" or EVP_KDF_HKDF_MODE_EXTRACT_ONLY β€” in this mode calling EVP_KDF_derive(3) will just perform the extract operation. The value returned will be the intermediate fixed-length pseudorandom key K. The keylen parameter must match the size of K, which can be looked up by calling EVP_KDF_CTX_get_kdf_size() after setting the mode and digest. The digest, key and salt values must be set before a key is derived otherwise an error will occur. ⚠️
    • "EXPAND_ONLY" or EVP_KDF_HKDF_MODE_EXPAND_ONLY β€” in this mode calling EVP_KDF_derive(3) will just perform the expand operation. The input key should be set to the intermediate fixed-length pseudorandom key K returned from a previous extract operation. The digest, key and info values must be set before a key is derived otherwise an error will occur. ⚠️

The OpenSSL FIPS provider also supports the following parameters, bestie, pay attention 🧚:

  • "fips-indicator" (OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR) <integer> β€” a getter that returns 1 if the operation is FIPS approved, or 0 otherwise. This may be used after calling EVP_KDF_derive. It returns 0 if "key-check" is set to 0 and the check fails.
  • "key-check" (OSSL_KDF_PARAM_FIPS_KEY_CHECK) <integer> β€” the default value of 1 causes an error during EVP_KDF_CTX_set_params() if the length of used key-derivation key (OSSL_KDF_PARAM_KEY) is shorter than 112 bits. Setting this to zero will ignore the error and set the approved "fips-indicator" to 0. This option breaks FIPS compliance if it causes the approved "fips-indicator" to return 0. 🫣

πŸŽ€ NOTES πŸŽ€

A context for HKDF can be obtained by calling β€” look at her go:

 EVP_KDF *kdf = EVP_KDF_fetch(NULL, "HKDF", NULL);
 EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf);

The output length of an HKDF expand operation is specified via the keylen parameter to the EVP_KDF_derive(3) function. When using EVP_KDF_HKDF_MODE_EXTRACT_ONLY the keylen parameter must equal the size of the intermediate fixed-length pseudorandom key otherwise an error will occur. For that mode, the fixed output size can be looked up by calling EVP_KDF_CTX_get_kdf_size() after setting the mode and digest on the EVP_KDF_CTX. πŸ”


🌈 EXAMPLES 🌈

This example derives 10 bytes using SHA-256 with the secret key "secret", salt value "salt" and info value "label" β€” main character example fr fr:

 EVP_KDF *kdf;
 EVP_KDF_CTX *kctx;
 unsigned char out[10];
 OSSL_PARAM params[5], *p = params;

 kdf = EVP_KDF_fetch(NULL, "HKDF", NULL);
 kctx = EVP_KDF_CTX_new(kdf);
 EVP_KDF_free(kdf);

 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
                                         SN_sha256, strlen(SN_sha256));
 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
                                          "secret", (size_t)6);
 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
                                          "label", (size_t)5);
 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
                                          "salt", (size_t)4);
 *p = OSSL_PARAM_construct_end();
 if (EVP_KDF_derive(kctx, out, sizeof(out), params) <= 0) {
     error("EVP_KDF_derive");
 }

 EVP_KDF_CTX_free(kctx);

she ate that ngl πŸ’…


πŸ‘‘ CONFORMING TO πŸ‘‘

RFC 5869 β€” yes, we stan a standards-compliant queen. ⭐


πŸ¦‹ SEE ALSO πŸ¦‹

EVP_KDF(3), EVP_KDF_CTX_new(3), EVP_KDF_CTX_free(3), EVP_KDF_CTX_get_kdf_size(3), EVP_KDF_CTX_set_params(3), EVP_KDF_derive(3), "PARAMETERS" in EVP_KDF(3), EVP_KDF-TLS13_KDF(7)

like go read them, they're all besties of this page 🫢


πŸ’œ HISTORY πŸ’œ

This functionality was added in OpenSSL 3.0. vintage but still slaying πŸ’…


Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


ok that's the whole thing bestie, ily, go derive some keys responsibly πŸ“βœ¨ β€” signed, sealed, delivered from page version 3.5.6, dated 2026-04-07, in the EVP_KDF-HKDF(7ossl) era πŸŽ€πŸ’–

πŸ“¦ where this page comes from