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