1EVP_PKEY_CTX_SET_HKDF_MD(3) OpenSSL EVP_PKEY_CTX_SET_HKDF_MD(3)
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_hkdf_mode - HMAC-based Extract-and-Expand key derivation
9 algorithm
10
12 #include <openssl/kdf.h>
13
14 int EVP_PKEY_CTX_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_hkdf_mode() sets the mode for the HKDF operation. There
36 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 All these functions are implemented as macros.
90
91 A context for HKDF can be obtained by calling:
92
93 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
94
95 The total length of the info buffer cannot exceed 1024 bytes in length:
96 this should be more than enough for any normal use of HKDF.
97
98 The output length of an HKDF expand operation is specified via the
99 length parameter to the EVP_PKEY_derive(3) function. Since the HKDF
100 output length is variable, passing a NULL buffer as a means to obtain
101 the requisite length is not meaningful with HKDF in any mode that
102 performs an expand operation. Instead, the caller must allocate a
103 buffer of the desired length, and pass that buffer to
104 EVP_PKEY_derive(3) along with (a pointer initialized to) the desired
105 length. Passing a NULL buffer to obtain the length is allowed when
106 using EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY.
107
108 Optimised versions of HKDF can be implemented in an ENGINE.
109
111 All these functions return 1 for success and 0 or a negative value for
112 failure. In particular a return value of -2 indicates the operation is
113 not supported by the public key algorithm.
114
116 This example derives 10 bytes using SHA-256 with the secret key
117 "secret", salt value "salt" and info value "label":
118
119 EVP_PKEY_CTX *pctx;
120 unsigned char out[10];
121 size_t outlen = sizeof(out);
122 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
123
124 if (EVP_PKEY_derive_init(pctx) <= 0)
125 /* Error */
126 if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0)
127 /* Error */
128 if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, "salt", 4) <= 0)
129 /* Error */
130 if (EVP_PKEY_CTX_set1_hkdf_key(pctx, "secret", 6) <= 0)
131 /* Error */
132 if (EVP_PKEY_CTX_add1_hkdf_info(pctx, "label", 5) <= 0)
133 /* Error */
134 if (EVP_PKEY_derive(pctx, out, &outlen) <= 0)
135 /* Error */
136
138 RFC 5869
139
141 EVP_PKEY_CTX_new(3), EVP_PKEY_CTX_ctrl_str(3), EVP_PKEY_derive(3)
142
144 Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
145
146 Licensed under the OpenSSL license (the "License"). You may not use
147 this file except in compliance with the License. You can obtain a copy
148 in the file LICENSE in the source distribution or at
149 <https://www.openssl.org/source/license.html>.
150
151
152
1531.1.1i 2021-01-26 EVP_PKEY_CTX_SET_HKDF_MD(3)