EVP_PKEY_CTX_SET_HKDF_MD(3ossl)
β¨ EVP_PKEY_CTX_set_hkdf_md β the HKDF glow-up π
okay bestie, welcome to the EVP_PKEY_CTX_SET_HKDF_MD(3ossl) page, aka the OpenSSL manual for the HMAC-based Extract-and-Expand key derivation algorithm. yes that's a mouthful. yes it's iconic. let's get into it π«Ά
πΈ NAME πΈ
EVP_PKEY_CTX_set_hkdf_md, EVP_PKEY_CTX_set1_hkdf_salt, EVP_PKEY_CTX_set1_hkdf_key, EVP_PKEY_CTX_add1_hkdf_info, EVP_PKEY_CTX_set_hkdf_mode β HMAC-based Extract-and-Expand key derivation algorithm. literally the whole squad, all five of them, one page π
π SYNOPSIS π
#include <openssl/kdf.h>
int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *pctx, int mode);
int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *pctx, const EVP_MD *md);
int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *pctx, unsigned char *salt,
int saltlen);
int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *pctx, unsigned char *key,
int keylen);
int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *pctx, unsigned char *info,
int infolen);
no cap, that's the whole vibe right there β
π¦ DESCRIPTION π¦
The EVP_PKEY_HKDF algorithm implements the HKDF key derivation function. HKDF follows the "extract-then-expand" paradigm, where the KDF logically consists of two modules. The first stage takes the input keying material and "extracts" from it a fixed-length pseudorandom key K. The second stage "expands" the key K into several additional pseudorandom keys (the output of the KDF). two modules, one glow-up, main character energy honestly π
EVP_PKEY_CTX_set_hkdf_mode() sets the mode for the HKDF operation. There are three modes that are currently defined:
EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPANDβ This is the default mode. CallingEVP_PKEY_derive(3)on anEVP_PKEY_CTXset 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 keyKis not returned.
In this mode the digest, key, salt and info values must be set before a key is derived, or an error occurs. no skipping steps bestie, the algorithm WILL clock you π
EVP_PKEY_HKDEF_MODE_EXTRACT_ONLYβ In this mode callingEVP_PKEY_derive(3)will just perform the extract operation. The value returned will be the intermediate fixed-length pseudorandom keyK.
The digest, key and salt values must be set before a key is derived, or an error occurs. notice there's no info here, and that's on purpose π
EVP_PKEY_HKDEF_MODE_EXPAND_ONLYβ In this mode callingEVP_PKEY_derive(3)will just perform the expand operation. The input key should be set to the intermediate fixed-length pseudorandom keyKreturned from a previous extract operation. it's giving round two, the sequel π
The digest, key and info values must be set before a key is derived, or an error occurs. again: salt is NOT in this list. do not come for me, I'm just the messenger π
EVP_PKEY_CTX_set_hkdf_md() sets the message digest associated with the HKDF. it's the md. it's set. obsessed π
EVP_PKEY_CTX_set1_hkdf_salt() sets the salt to saltlen bytes of the buffer salt. Any existing value is replaced. (the set1 is doing exactly what you think β it's a full replacement, no mercy, out with the old β¨)
EVP_PKEY_CTX_set1_hkdf_key() sets the key to keylen bytes of the buffer key. Any existing value is replaced. same energy, full swap π
EVP_PKEY_CTX_add1_hkdf_info() sets the info value to infolen bytes of the buffer info. If a value is already set, it is appended to the existing value. see the add in the name? that's the whole personality. it accumulates π₯
π§ STRING CTRLS π§
HKDF also supports string based control operations via EVP_PKEY_CTX_ctrl_str(3).
- The type parameter
"md"uses the supplied value as the name of the digest algorithm to use. - The type parameter
"mode"uses the values"EXTRACT_AND_EXPAND","EXTRACT_ONLY"and"EXPAND_ONLY"to determine the mode to use. - The type parameters
"salt","key"and"info"use the supplied value parameter as a seed, key or info value. - The names
"hexsalt","hexkey"and"hexinfo"are similar except they take a hex string which is converted to binary.
so yes, you can do the whole thing with strings if that's your moment π β¨
π NOTES π
A context for HKDF can be obtained by calling:
EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
The total length of the info buffer cannot exceed 2048 bytes in length: this should be more than enough for any normal use of HKDF. 2048. that's the limit. do not test it, bestie, I'm begging π«Ά
The output length of an HKDF expand operation is specified via the length parameter to the EVP_PKEY_derive(3) function. Since the HKDF output length is variable, passing a NULL buffer as a means to obtain the requisite length is not meaningful with HKDF in any mode that performs an expand operation. Instead, the caller must allocate a buffer of the desired length, and pass that buffer to EVP_PKEY_derive(3) along with (a pointer initialized to) the desired length. Passing a NULL buffer to obtain the length is allowed when using EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY. so the NULL-buffer trick is only a thing in that one mode, okay? everything else you gotta bring your own buffer π
Optimised versions of HKDF can be implemented in an ENGINE. slay, go off π
β RETURN VALUES β
All these functions return 1 for success and 0 or a negative value for failure. In particular a return value of -2 indicates the operation is not supported by the public key algorithm. so like: 1 = we ate, 0/negative = we did not eat, -2 = that algorithm said "not my department" π
π EXAMPLES π
This example derives 10 bytes using SHA-256 with the secret key "secret", salt value "salt" and info value "label":
EVP_PKEY_CTX *pctx;
unsigned char out[10];
size_t outlen = sizeof(out);
pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
if (EVP_PKEY_derive_init(pctx) <= 0)
/* Error */
if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0)
/* Error */
if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, "salt", 4) <= 0)
/* Error */
if (EVP_PKEY_CTX_set1_hkdf_key(pctx, "secret", 6) <= 0)
/* Error */
if (EVP_PKEY_CTX_add1_hkdf_info(pctx, "label", 5) <= 0)
/* Error */
if (EVP_PKEY_derive(pctx, out, &outlen) <= 0)
/* Error */
10 bytes, SHA-256, salt, secret, label, done. chef's kiss, no notes π₯
π CONFORMING TO π
RFC 5869 β we're standards compliant AND cute, a dual threat π
π SEE ALSO π
EVP_PKEY_CTX_new(3), EVP_PKEY_CTX_ctrl_str(3), EVP_PKEY_derive(3)
go read those too, they're the supporting cast and they understood the assignment π«Ά
πΈ HISTORY πΈ
All of the functions described here were converted from macros to functions in OpenSSL 3.0. glow-up of the century, honestly β¨
π COPYRIGHT π
Copyright 2016-2021 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>.
and that's the whole page, bestie π mwah π this was OpenSSL 3.5.9, dated 2026-09-29, and you read all of it. so proud. now go set your HKDF params before you derive, or the error is coming for you fr fr π
β¨
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms