EVP_PKEY_CTX_SET_HKDF_MD(3ossl)
π 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 π
okay bestie so this is the 3ossl manpage for OpenSSL's HKDF controls, and honestly? it's giving crypto-girlie main character energy. we're talking the HMAC-based Extract-and-Expand key derivation algorithm β say that five times fast, i dare you π«Ά
β¨ 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);
like, five functions, all vibes, all returning int π
π¦ DESCRIPTION π¦
so the EVP_PKEY_HKDF algorithm is literally the HKDF key derivation function, no cap. HKDF follows the "extract-then-expand" paradigm, which means the KDF logically consists of two modules, like two besties doing their own thing:
- the first stage takes the input keying material and "extracts" from it a fixed-length pseudorandom key
Kπ₯ - the second stage "expands" the key
Kinto several additional pseudorandom keys (aka the output of the KDF) β¨
πΈ the actual functions
EVP_PKEY_CTX_set_hkdf_mode()β sets the mode for the HKDF operation. there are three modes currently defined, and yes you need to know all of them:
β EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND
this is the default mode, periodt. calling EVP_PKEY_derive(3) on an EVP_PKEY_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 or an error occurs. no skipping steps bestie.
β EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY
in this mode calling EVP_PKEY_derive(3) will just perform the extract operation. the value returned will be the intermediate fixed-length pseudorandom key K π
the digest, key and salt values must be set before a key is derived or an error occurs.
β EVP_PKEY_HKDEF_MODE_EXPAND_ONLY
in this mode calling EVP_PKEY_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 or an error occurs.
π the setters
EVP_PKEY_CTX_set_hkdf_md()β sets the message digest associated with the HKDF. that's it, that's the whole thing β¨EVP_PKEY_CTX_set1_hkdf_salt()β sets the salt tosaltlenbytes of the buffersalt. any existing value is replaced (sorry to the old salt, it had a good run) π§EVP_PKEY_CTX_set1_hkdf_key()β sets the key tokeylenbytes of the bufferkey. any existing value is replaced πEVP_PKEY_CTX_add1_hkdf_info()β sets the info value toinfolenbytes of the bufferinfo. if a value is already set, it is appended to the existing value (note: appended, not replaced β icon behaviour) π
π STRING CTRLS π
HKDF also supports string based control operations via EVP_PKEY_CTX_ctrl_str(3). here's the roster:
- 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 π’
π 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. like genuinely, 2048 is plenty, breathe π«Ά
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 β
optimised versions of HKDF can be implemented in an ENGINE. fancy.
β 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 if you see -2, that's the "not my department" of return codes π
π 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 */
look at that flawless setup-then-derive pipeline. ate and left no crumbs π₯
π CONFORMING TO π
π SEE ALSO π
EVP_PKEY_CTX_new(3), EVP_PKEY_CTX_ctrl_str(3), EVP_PKEY_derive(3)
πΈ HISTORY πΈ
all of the functions described here were converted from macros to functions in OpenSSL 3.0. glow-up era β¨
π 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>.
okay that's the whole page bestie, go forth and derive keys responsibly πβ¨ this was lovingly delivered to you by the 3.5.0 docs, dated 2026-09-25 β mwah, see you in the next man page ππ«Ά
π¦ where this page comes from
- opensuse-leap 16.0: openssl-3-doc 3.5.0-160000.11.1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms