EVP_PKEY_CTX_SET_HKDF_MD(3SSL)

✨ 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 manual page EVP_PKEY_CTX_SET_HKDF_MD(3SSL) β€” yes it's from the OpenSSL family, section 3SSL, and it's literally the whole HKDF key-derivation glow-up. let's get into it πŸ¦‹


🌸 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);

πŸ’… DESCRIPTION πŸ’…

so the EVP_PKEY_HKDF algorithm is the one that implements the HKDF key derivation function. and HKDF is obsessed with the "extract-then-expand" paradigm β€” meaning the KDF is logically two modules, like two besties doing two jobs πŸ’–

stage one: it takes your input keying material and "extracts" from it a fixed-length pseudorandom key <italic>K*. slay.

stage two: it "expands" that key <italic>K* into a bunch more pseudorandom keys β€” that's the output of the KDF. main character expansion energy, periodt.

EVP_PKEY_CTX_set_hkdf_mode() sets the mode for the HKDF operation. there are three modes currently defined, and honestly they each have their own vibe:

  • EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND β€” this is the default mode, the it-girl. calling EVP_PKEY_derive(3) on an EVP_PKEY_CTX set up for HKDF will do an extract followed by an expand, all in one go. the derived key you get back is the result after the expand operation. the intermediate fixed-length pseudorandom key <italic>K* is not returned, no cap. in this mode the digest, key, salt and info values must be set before a key is derived, or an error occurs. set them or perish, fr fr.
  • EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY β€” in this mode, calling EVP_PKEY_derive(3) will just do the extract operation. the value returned is that intermediate fixed-length pseudorandom key <italic>K*. just the one job, and she does it well. 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 do the expand operation. the input key should be set to the intermediate fixed-length pseudorandom key <italic>K* returned from a previous extract operation β€” like, you gotta bring your own <italic>K* from last time. the digest, key and info values must be set before a key is derived, or an error occurs.

EVP_PKEY_CTX_set_hkdf_md() sets the message digest associated with the HKDF. that's it, that's the whole job πŸ’…

EVP_PKEY_CTX_set1_hkdf_salt() sets the salt to saltlen bytes of the buffer salt. any existing value? replaced. gone. bye 🫢

EVP_PKEY_CTX_set1_hkdf_key() sets the key to keylen bytes of the buffer key. any existing value is also replaced, no survivors.

EVP_PKEY_CTX_add1_hkdf_info() sets the info value to infolen bytes of the buffer info. but here's the ✨ twist ✨ β€” if a value is already set, it is appended to the existing value. not replaced. she adds, she doesn't erase. iconic behavior honestly.


πŸŽ€ STRING CTRLS πŸŽ€

HKDF also supports string based control operations via EVP_PKEY_CTX_ctrl_str(3). here's the lineup:

  • 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. hex girlies rise up 🌈

πŸ¦‹ NOTES πŸ¦‹

a context for HKDF can be obtained by calling this cutie:

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 β€” but ngl that should be more than enough for any normal use of HKDF. we are not stressing about it.

the output length of an HKDF expand operation is specified via the length parameter to the EVP_PKEY_derive(3) function. and since the HKDF output length is variable, passing a NULL buffer as a way 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. do the work, bestie.

passing a NULL buffer to obtain the length is allowed when using EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY. so like, only in that one specific case. don't get it twisted πŸ’€

🌟 RETURN VALUES 🌟

all these functions return 1 for success and 0 or a negative value for failure. and specifically, a return value of -2 indicates the operation is not supported by the public key algorithm. rude, but okay.


πŸ’– 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 her go. every single if with her little /* Error */ backup plan. we love a prepared queen. πŸ‘‘


🌈 CONFORMING TO 🌈

RFC 5869 β€” she's standards-compliant, obviously.


🫢 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 of the century honestly.


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!! πŸ˜­πŸ’– this was OpenSSL 4.0.3, dated 2026-09-29, from the EVP_PKEY_CTX_SET_HKDF_MD(3SSL) manual. go derive something gorgeous. mwah πŸŽ€βœ¨

πŸ“¦ where this page comes from