xref: /freebsd/crypto/openssl/doc/man7/EVP_KDF-HKDF.pod (revision 0d0c8621fd181e507f0fb50ffcca606faf66a8c2)
1=pod
2
3=head1 NAME
4
5EVP_KDF-HKDF - The HKDF EVP_KDF implementation
6
7=head1 DESCRIPTION
8
9Support for computing the B<HKDF> KDF through the B<EVP_KDF> API.
10
11The EVP_KDF-HKDF algorithm implements the HKDF key derivation function.
12HKDF follows the "extract-then-expand" paradigm, where the KDF logically
13consists of two modules. The first stage takes the input keying material
14and "extracts" from it a fixed-length pseudorandom key K. The second stage
15"expands" the key K into several additional pseudorandom keys (the output
16of the KDF).
17
18The output is considered to be keying material.
19
20=head2 Identity
21
22"HKDF" is the name for this implementation; it
23can be used with the EVP_KDF_fetch() function.
24
25=head2 Supported parameters
26
27The supported parameters are:
28
29=over 4
30
31=item "properties" (B<OSSL_KDF_PARAM_PROPERTIES>) <UTF8 string>
32
33=item "digest" (B<OSSL_KDF_PARAM_DIGEST>) <UTF8 string>
34
35=item "key" (B<OSSL_KDF_PARAM_KEY>) <octet string>
36
37=item "salt" (B<OSSL_KDF_PARAM_SALT>) <octet string>
38
39These parameters work as described in L<EVP_KDF(3)/PARAMETERS>.
40
41=item "info" (B<OSSL_KDF_PARAM_INFO>) <octet string>
42
43This parameter sets the info value.
44The length of the context info buffer cannot exceed 1024 bytes;
45this should be more than enough for any normal use of HKDF.
46
47=item "mode" (B<OSSL_KDF_PARAM_MODE>) <UTF8 string> or <integer>
48
49This parameter sets the mode for the HKDF operation.
50There are three modes that are currently defined:
51
52=over 4
53
54=item "EXTRACT_AND_EXPAND" or B<EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND>
55
56This is the default mode.  Calling L<EVP_KDF_derive(3)> on an EVP_KDF_CTX set
57up for HKDF will perform an extract followed by an expand operation in one go.
58The derived key returned will be the result after the expand operation. The
59intermediate fixed-length pseudorandom key K is not returned.
60
61In this mode the digest, key, salt and info values must be set before a key is
62derived otherwise an error will occur.
63
64=item "EXTRACT_ONLY" or B<EVP_KDF_HKDF_MODE_EXTRACT_ONLY>
65
66In this mode calling L<EVP_KDF_derive(3)> will just perform the extract
67operation. The value returned will be the intermediate fixed-length pseudorandom
68key K.  The I<keylen> parameter must match the size of K, which can be looked
69up by calling EVP_KDF_CTX_get_kdf_size() after setting the mode and digest.
70
71The digest, key and salt values must be set before a key is derived otherwise
72an error will occur.
73
74=item "EXPAND_ONLY" or B<EVP_KDF_HKDF_MODE_EXPAND_ONLY>
75
76In this mode calling L<EVP_KDF_derive(3)> will just perform the expand
77operation. The input key should be set to the intermediate fixed-length
78pseudorandom key K returned from a previous extract operation.
79
80The digest, key and info values must be set before a key is derived otherwise
81an error will occur.
82
83=back
84
85=back
86
87=head1 NOTES
88
89A context for HKDF can be obtained by calling:
90
91 EVP_KDF *kdf = EVP_KDF_fetch(NULL, "HKDF", NULL);
92 EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf);
93
94The output length of an HKDF expand operation is specified via the I<keylen>
95parameter to the L<EVP_KDF_derive(3)> function.  When using
96EVP_KDF_HKDF_MODE_EXTRACT_ONLY the I<keylen> parameter must equal the size of
97the intermediate fixed-length pseudorandom key otherwise an error will occur.
98For that mode, the fixed output size can be looked up by calling EVP_KDF_CTX_get_kdf_size()
99after setting the mode and digest on the B<EVP_KDF_CTX>.
100
101=head1 EXAMPLES
102
103This example derives 10 bytes using SHA-256 with the secret key "secret",
104salt value "salt" and info value "label":
105
106 EVP_KDF *kdf;
107 EVP_KDF_CTX *kctx;
108 unsigned char out[10];
109 OSSL_PARAM params[5], *p = params;
110
111 kdf = EVP_KDF_fetch(NULL, "HKDF", NULL);
112 kctx = EVP_KDF_CTX_new(kdf);
113 EVP_KDF_free(kdf);
114
115 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
116                                         SN_sha256, strlen(SN_sha256));
117 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
118                                          "secret", (size_t)6);
119 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
120                                          "label", (size_t)5);
121 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
122                                          "salt", (size_t)4);
123 *p = OSSL_PARAM_construct_end();
124 if (EVP_KDF_derive(kctx, out, sizeof(out), params) <= 0) {
125     error("EVP_KDF_derive");
126 }
127
128 EVP_KDF_CTX_free(kctx);
129
130=head1 CONFORMING TO
131
132RFC 5869
133
134=head1 SEE ALSO
135
136L<EVP_KDF(3)>,
137L<EVP_KDF_CTX_new(3)>,
138L<EVP_KDF_CTX_free(3)>,
139L<EVP_KDF_CTX_get_kdf_size(3)>,
140L<EVP_KDF_CTX_set_params(3)>,
141L<EVP_KDF_derive(3)>,
142L<EVP_KDF(3)/PARAMETERS>,
143L<EVP_KDF-TLS13_KDF(7)>
144
145=head1 HISTORY
146
147This functionality was added in OpenSSL 3.0.
148
149=head1 COPYRIGHT
150
151Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
152
153Licensed under the Apache License 2.0 (the "License").  You may not use
154this file except in compliance with the License.  You can obtain a copy
155in the file LICENSE in the source distribution or at
156L<https://www.openssl.org/source/license.html>.
157
158=cut
159