---
name: "libressl-EVP_PKEY_CTX_set_hkdf_md"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-EVP_PKEY_CTX_set_hkdf_md"
---

# libressl-EVP\_PKEY\_CTX\_set\_hkdf\_md(3)

HMAC-based Extract-and-Expand key derivation algorithm

## 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\_hkdf\_mode** – HMAC-based Extract-and-Expand key derivation algorithm

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#include \<openssl/evp.h\>**

**#include \<openssl/kdf.h\>**

*int*\

**EVP\_PKEY\_CTX\_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

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).

**EVP\_PKEY\_CTX\_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. 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.

- 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.

**EVP\_PKEY\_CTX\_set\_hkdf\_md**() sets the message digest associated with the HKDF.

**EVP\_PKEY\_CTX\_set1\_hkdf\_salt**() sets the salt to *saltlen* bytes of the buffer *salt*. Any existing value is replaced.

**EVP\_PKEY\_CTX\_set1\_hkdf\_key**() sets the key to *keylen* bytes of the buffer *key*. Any existing value is replaced.

**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.

## 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" accepts "EXTRACT\_AND\_EXPAND", "EXTRACT\_ONLY" and "EXPAND\_ONLY" as *value* to determine the mode to use. The *type* parameters "salt", "key" and "info" use the supplied *value* parameter as a seed, key, or info. The names "hexsalt", "hexkey" and "hexinfo" are similar except they take a hex string which is converted to binary.

## NOTES

All these functions are implemented as macros.

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 1024 bytes in length: this should be more than enough for any normal use of HKDF.

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.

## 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.

## 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);
if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)) == NULL)
	/* Error */
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 */
```

## SEE ALSO

EVP\_PKEY\_CTX\_ctrl\_str(3), EVP\_PKEY\_CTX\_new(3), EVP\_PKEY\_derive(3)

## STANDARDS

RFC 5869: HMAC-based Extract-and-Expand Key Derivation Function (HKDF)

## HISTORY

**EVP\_PKEY\_CTX\_set\_hkdf\_md**(), **EVP\_PKEY\_CTX\_set1\_hkdf\_salt**(), **EVP\_PKEY\_CTX\_set1\_hkdf\_key**(), and **EVP\_PKEY\_CTX\_add1\_hkdf\_info**() first appeared in OpenSSL 1.1.0 and **EVP\_PKEY\_CTX\_hkdf\_mode**() in OpenSSL 1.1.1. These functions have been available since OpenBSD 7.2.
