]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/EVP_PKEY_CTX_set_hkdf_md.pod
Remove an unnecessary call to BN_CTX_free.
[thirdparty/openssl.git] / doc / man3 / EVP_PKEY_CTX_set_hkdf_md.pod
CommitLineData
aacfb134
AG
1=pod
2
3=head1 NAME
4
c952780c 5EVP_PKEY_CTX_set_hkdf_md, EVP_PKEY_CTX_set1_hkdf_salt,
327c1627
MC
6EVP_PKEY_CTX_set1_hkdf_key, EVP_PKEY_CTX_add1_hkdf_info,
7EVP_PKEY_CTX_hkdf_mode -
aacfb134
AG
8HMAC-based Extract-and-Expand key derivation algorithm
9
10=head1 SYNOPSIS
11
12 #include <openssl/kdf.h>
13
327c1627
MC
14 int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX *pctx, int mode);
15
aacfb134
AG
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
27=head1 DESCRIPTION
28
f04abe7d 29The EVP_PKEY_HKDF algorithm implements the HKDF key derivation function.
aacfb134
AG
30HKDF follows the "extract-then-expand" paradigm, where the KDF logically
31consists of two modules. The first stage takes the input keying material
32and "extracts" from it a fixed-length pseudorandom key K. The second stage
33"expands" the key K into several additional pseudorandom keys (the output
34of the KDF).
35
327c1627
MC
36EVP_PKEY_CTX_hkdf_mode() sets the mode for the HKDF operation. There are three
37modes that are currently defined:
38
39=over 4
40
41=item EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND
42
43This is the default mode. Calling L<EVP_PKEY_derive(3)> on an EVP_PKEY_CTX set
44up for HKDF will perform an extract followed by an expand operation in one go.
45The derived key returned will be the result after the expand operation. The
46intermediate fixed-length pseudorandom key K is not returned.
47
48In this mode the digest, key, salt and info values must be set before a key is
49derived or an error occurs.
50
51=item EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY
52
53In this mode calling L<EVP_PKEY_derive(3)> will just perform the extract
54operation. The value returned will be the intermediate fixed-length pseudorandom
55key K.
56
57The digest, key and salt values must be set before a key is derived or an
58error occurs.
59
60=item EVP_PKEY_HKDEF_MODE_EXPAND_ONLY
61
62In this mode calling L<EVP_PKEY_derive(3)> will just perform the expand
63operation. The input key should be set to the intermediate fixed-length
64pseudorandom key K returned from a previous extract operation.
65
66The digest, key and info values must be set before a key is derived or an
67error occurs.
68
69=back
70
d474100a 71EVP_PKEY_CTX_set_hkdf_md() sets the message digest associated with the HKDF.
aacfb134
AG
72
73EVP_PKEY_CTX_set1_hkdf_salt() sets the salt to B<saltlen> bytes of the
74buffer B<salt>. Any existing value is replaced.
75
d474100a 76EVP_PKEY_CTX_set1_hkdf_key() sets the key to B<keylen> bytes of the buffer
aacfb134
AG
77B<key>. Any existing value is replaced.
78
79EVP_PKEY_CTX_add1_hkdf_info() sets the info value to B<infolen> bytes of the
80buffer B<info>. If a value is already set, it is appended to the existing
81value.
82
f04abe7d
VD
83=head1 STRING CTRLS
84
85HKDF also supports string based control operations via
86L<EVP_PKEY_CTX_ctrl_str(3)>.
87The B<type> parameter "md" uses the supplied B<value> as the name of the digest
88algorithm to use.
327c1627
MC
89The B<type> parameter "mode" uses the values "EXTRACT_AND_EXPAND",
90"EXTRACT_ONLY" and "EXPAND_ONLY" to determine the mode to use.
f04abe7d
VD
91The B<type> parameters "salt", "key" and "info" use the supplied B<value>
92parameter as a B<seed>, B<key> or B<info> value.
93The names "hexsalt", "hexkey" and "hexinfo" are similar except they take a hex
94string which is converted to binary.
95
aacfb134
AG
96=head1 NOTES
97
98All these functions are implemented as macros.
99
100A context for HKDF can be obtained by calling:
101
c16ab9dc 102 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
aacfb134 103
aacfb134
AG
104The total length of the info buffer cannot exceed 1024 bytes in length: this
105should be more than enough for any normal use of HKDF.
106
327c1627
MC
107The output length of an HKDF expand operation is specified via the length
108parameter to the L<EVP_PKEY_derive(3)> function.
f04abe7d 109Since the HKDF output length is variable, passing a B<NULL> buffer as a means
327c1627
MC
110to obtain the requisite length is not meaningful with HKDF in any mode that
111performs an expand operation. Instead, the caller must allocate a buffer of the
112desired length, and pass that buffer to L<EVP_PKEY_derive(3)> along with (a
113pointer initialized to) the desired length. Passing a B<NULL> buffer to obtain
114the length is allowed when using EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY.
aacfb134
AG
115
116Optimised versions of HKDF can be implemented in an ENGINE.
117
118=head1 RETURN VALUES
119
120All these functions return 1 for success and 0 or a negative value for failure.
121In particular a return value of -2 indicates the operation is not supported by
122the public key algorithm.
123
cda77422 124=head1 EXAMPLES
aacfb134
AG
125
126This example derives 10 bytes using SHA-256 with the secret key "secret",
127salt value "salt" and info value "label":
128
129 EVP_PKEY_CTX *pctx;
130 unsigned char out[10];
131 size_t outlen = sizeof(out);
132 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
133
134 if (EVP_PKEY_derive_init(pctx) <= 0)
2947af32 135 /* Error */
aacfb134 136 if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0)
2947af32 137 /* Error */
d2139cf8 138 if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, "salt", 4) <= 0)
2947af32 139 /* Error */
d2139cf8 140 if (EVP_PKEY_CTX_set1_hkdf_key(pctx, "secret", 6) <= 0)
2947af32 141 /* Error */
c67a2f80 142 if (EVP_PKEY_CTX_add1_hkdf_info(pctx, "label", 5) <= 0)
2947af32 143 /* Error */
aacfb134 144 if (EVP_PKEY_derive(pctx, out, &outlen) <= 0)
2947af32 145 /* Error */
aacfb134
AG
146
147=head1 CONFORMING TO
148
149RFC 5869
150
151=head1 SEE ALSO
152
153L<EVP_PKEY_CTX_new(3)>,
f04abe7d
VD
154L<EVP_PKEY_CTX_ctrl_str(3)>,
155L<EVP_PKEY_derive(3)>
aacfb134 156
e2f92610
RS
157=head1 COPYRIGHT
158
83cf7abf 159Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
e2f92610 160
4746f25a 161Licensed under the Apache License 2.0 (the "License"). You may not use
e2f92610
RS
162this file except in compliance with the License. You can obtain a copy
163in the file LICENSE in the source distribution or at
164L<https://www.openssl.org/source/license.html>.
165
166=cut