5 EVP_PKEY_HKDF, EVP_PKEY_CTX_set_hkdf_md, EVP_PKEY_CTX_set1_hkdf_salt,
6 EVP_PKEY_CTX_set1_hkdf_key, EVP_PKEY_CTX_add1_hkdf_info -
7 HMAC-based Extract-and-Expand key derivation algorithm
11 #include <openssl/kdf.h>
13 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *pctx, const EVP_MD *md);
15 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *pctx, unsigned char *salt,
18 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *pctx, unsigned char *key,
21 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *pctx, unsigned char *info,
26 The EVP_PKEY_HKDF algorithm implements the HKDF key derivation function.
27 HKDF follows the "extract-then-expand" paradigm, where the KDF logically
28 consists of two modules. The first stage takes the input keying material
29 and "extracts" from it a fixed-length pseudorandom key K. The second stage
30 "expands" the key K into several additional pseudorandom keys (the output
33 EVP_PKEY_set_hkdf_md() sets the message digest associated with the HKDF.
35 EVP_PKEY_CTX_set1_hkdf_salt() sets the salt to B<saltlen> bytes of the
36 buffer B<salt>. Any existing value is replaced.
38 EVP_PKEY_CTX_set_hkdf_key() sets the key to B<keylen> bytes of the buffer
39 B<key>. Any existing value is replaced.
41 EVP_PKEY_CTX_add1_hkdf_info() sets the info value to B<infolen> bytes of the
42 buffer B<info>. If a value is already set, it is appended to the existing
47 HKDF also supports string based control operations via
48 L<EVP_PKEY_CTX_ctrl_str(3)>.
49 The B<type> parameter "md" uses the supplied B<value> as the name of the digest
51 The B<type> parameters "salt", "key" and "info" use the supplied B<value>
52 parameter as a B<seed>, B<key> or B<info> value.
53 The names "hexsalt", "hexkey" and "hexinfo" are similar except they take a hex
54 string which is converted to binary.
58 All these functions are implemented as macros.
60 A context for HKDF can be obtained by calling:
62 EVP_PKEY_CTX *pctx = EVP_PKEY_new_id(EVP_PKEY_HKDF, NULL);
64 The digest, key, salt and info values must be set before a key is derived or
67 The total length of the info buffer cannot exceed 1024 bytes in length: this
68 should be more than enough for any normal use of HKDF.
70 The output length of the KDF is specified via the length parameter to the
71 L<EVP_PKEY_derive(3)> function.
72 Since the HKDF output length is variable, passing a B<NULL> buffer as a means
73 to obtain the requisite length is not meaningful with HKDF.
74 Instead, the caller must allocate a buffer of the desired length, and pass that
75 buffer to L<EVP_PKEY_derive(3)> along with (a pointer initialized to) the
78 Optimised versions of HKDF can be implemented in an ENGINE.
82 All these functions return 1 for success and 0 or a negative value for failure.
83 In particular a return value of -2 indicates the operation is not supported by
84 the public key algorithm.
88 This example derives 10 bytes using SHA-256 with the secret key "secret",
89 salt value "salt" and info value "label":
92 unsigned char out[10];
93 size_t outlen = sizeof(out);
94 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
96 if (EVP_PKEY_derive_init(pctx) <= 0)
98 if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0)
100 if (EVP_PKEY_CTX_set1_salt(pctx, "salt", 4) <= 0)
102 if (EVP_PKEY_CTX_set1_key(pctx, "secret", 6) <= 0)
104 if (EVP_PKEY_CTX_add1_hkdf_info(pctx, "label", 6) <= 0)
106 if (EVP_PKEY_derive(pctx, out, &outlen) <= 0)
115 L<EVP_PKEY_CTX_new(3)>,
116 L<EVP_PKEY_CTX_ctrl_str(3)>,
117 L<EVP_PKEY_derive(3)>