1EVP_PKEY_CTX_SET_HKDF_MD(3ossl) OpenSSL EVP_PKEY_CTX_SET_HKDF_MD(3ossl)
2
3
4
6 EVP_PKEY_CTX_set_hkdf_md, EVP_PKEY_CTX_set1_hkdf_salt,
7 EVP_PKEY_CTX_set1_hkdf_key, EVP_PKEY_CTX_add1_hkdf_info,
8 EVP_PKEY_CTX_set_hkdf_mode - HMAC-based Extract-and-Expand key
9 derivation algorithm
10
12 #include <openssl/kdf.h>
13
14 int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *pctx, int mode);
15
16 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *pctx, const EVP_MD *md);
17
18 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *pctx, unsigned char *salt,
19 int saltlen);
20
21 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *pctx, unsigned char *key,
22 int keylen);
23
24 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *pctx, unsigned char *info,
25 int infolen);
26
28 The EVP_PKEY_HKDF algorithm implements the HKDF key derivation
29 function. HKDF follows the "extract-then-expand" paradigm, where the
30 KDF logically consists of two modules. The first stage takes the input
31 keying material and "extracts" from it a fixed-length pseudorandom key
32 K. The second stage "expands" the key K into several additional
33 pseudorandom keys (the output of the KDF).
34
35 EVP_PKEY_CTX_set_hkdf_mode() sets the mode for the HKDF operation.
36 There are three modes that are currently defined:
37
38 EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND
39 This is the default mode. Calling EVP_PKEY_derive(3) on an
40 EVP_PKEY_CTX set up for HKDF will perform an extract followed by an
41 expand operation in one go. The derived key returned will be the
42 result after the expand operation. The intermediate fixed-length
43 pseudorandom key K is not returned.
44
45 In this mode the digest, key, salt and info values must be set
46 before a key is derived or an error occurs.
47
48 EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY
49 In this mode calling EVP_PKEY_derive(3) will just perform the
50 extract operation. The value returned will be the intermediate
51 fixed-length pseudorandom key K.
52
53 The digest, key and salt values must be set before a key is derived
54 or an error occurs.
55
56 EVP_PKEY_HKDEF_MODE_EXPAND_ONLY
57 In this mode calling EVP_PKEY_derive(3) will just perform the
58 expand operation. The input key should be set to the intermediate
59 fixed-length pseudorandom key K returned from a previous extract
60 operation.
61
62 The digest, key and info values must be set before a key is derived
63 or an error occurs.
64
65 EVP_PKEY_CTX_set_hkdf_md() sets the message digest associated with the
66 HKDF.
67
68 EVP_PKEY_CTX_set1_hkdf_salt() sets the salt to saltlen bytes of the
69 buffer salt. Any existing value is replaced.
70
71 EVP_PKEY_CTX_set1_hkdf_key() sets the key to keylen bytes of the buffer
72 key. Any existing value is replaced.
73
74 EVP_PKEY_CTX_add1_hkdf_info() sets the info value to infolen bytes of
75 the buffer info. If a value is already set, it is appended to the
76 existing value.
77
79 HKDF also supports string based control operations via
80 EVP_PKEY_CTX_ctrl_str(3). The type parameter "md" uses the supplied
81 value as the name of the digest algorithm to use. The type parameter
82 "mode" uses the values "EXTRACT_AND_EXPAND", "EXTRACT_ONLY" and
83 "EXPAND_ONLY" to determine the mode to use. The type parameters
84 "salt", "key" and "info" use the supplied value parameter as a seed,
85 key or info value. The names "hexsalt", "hexkey" and "hexinfo" are
86 similar except they take a hex string which is converted to binary.
87
89 A context for HKDF can be obtained by calling:
90
91 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
92
93 The total length of the info buffer cannot exceed 1024 bytes in length:
94 this should be more than enough for any normal use of HKDF.
95
96 The output length of an HKDF expand operation is specified via the
97 length parameter to the EVP_PKEY_derive(3) function. Since the HKDF
98 output length is variable, passing a NULL buffer as a means to obtain
99 the requisite length is not meaningful with HKDF in any mode that
100 performs an expand operation. Instead, the caller must allocate a
101 buffer of the desired length, and pass that buffer to
102 EVP_PKEY_derive(3) along with (a pointer initialized to) the desired
103 length. Passing a NULL buffer to obtain the length is allowed when
104 using EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY.
105
106 Optimised versions of HKDF can be implemented in an ENGINE.
107
109 All these functions return 1 for success and 0 or a negative value for
110 failure. In particular a return value of -2 indicates the operation is
111 not supported by the public key algorithm.
112
114 This example derives 10 bytes using SHA-256 with the secret key
115 "secret", salt value "salt" and info value "label":
116
117 EVP_PKEY_CTX *pctx;
118 unsigned char out[10];
119 size_t outlen = sizeof(out);
120 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
121
122 if (EVP_PKEY_derive_init(pctx) <= 0)
123 /* Error */
124 if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0)
125 /* Error */
126 if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, "salt", 4) <= 0)
127 /* Error */
128 if (EVP_PKEY_CTX_set1_hkdf_key(pctx, "secret", 6) <= 0)
129 /* Error */
130 if (EVP_PKEY_CTX_add1_hkdf_info(pctx, "label", 5) <= 0)
131 /* Error */
132 if (EVP_PKEY_derive(pctx, out, &outlen) <= 0)
133 /* Error */
134
136 RFC 5869
137
139 EVP_PKEY_CTX_new(3), EVP_PKEY_CTX_ctrl_str(3), EVP_PKEY_derive(3)
140
142 All of the functions described here were converted from macros to
143 functions in OpenSSL 3.0.
144
146 Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
147
148 Licensed under the Apache License 2.0 (the "License"). You may not use
149 this file except in compliance with the License. You can obtain a copy
150 in the file LICENSE in the source distribution or at
151 <https://www.openssl.org/source/license.html>.
152
153
154
1553.0.5 2022-11-01 EVP_PKEY_CTX_SET_HKDF_MD(3ossl)