5 EVP_KDF-HKDF - The HKDF EVP_KDF implementation
9 Support for computing the B<HKDF> KDF through the B<EVP_KDF> API.
11 The EVP_KDF-HKDF algorithm implements the HKDF key derivation function.
12 HKDF follows the "extract-then-expand" paradigm, where the KDF logically
13 consists of two modules. The first stage takes the input keying material
14 and "extracts" from it a fixed-length pseudorandom key K. The second stage
15 "expands" the key K into several additional pseudorandom keys (the output
20 "HKDF" is the name for this implementation; it
21 can be used with the EVP_KDF_fetch() function.
23 =head2 Supported parameters
25 The supported parameters are:
29 =item B<OSSL_KDF_PARAM_PROPERTIES> ("properties") <UTF8 string>
31 =item B<OSSL_KDF_PARAM_DIGEST> ("digest") <UTF8 string>
33 =item B<OSSL_KDF_PARAM_KEY> ("key") <octet string>
35 =item B<OSSL_KDF_PARAM_SALT> ("salt") <octet string>
37 These parameters work as described in L<EVP_KDF(3)/PARAMETERS>.
39 =item B<OSSL_KDF_PARAM_INFO> ("info") <octet string>
41 This parameter sets the info value.
42 The length of the context info buffer cannot exceed 1024 bytes;
43 this should be more than enough for any normal use of HKDF.
45 =item B<OSSL_KDF_PARAM_MODE> ("mode") <UTF8 string> or <integer>
47 This parameter sets the mode for the HKDF operation.
48 There are three modes that are currently defined:
52 =item B<EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND> "EXTRACT_AND_EXPAND"
54 This is the default mode. Calling L<EVP_KDF-derive(3)> on an EVP_KDF_CTX set
55 up for HKDF will perform an extract followed by an expand operation in one go.
56 The derived key returned will be the result after the expand operation. The
57 intermediate fixed-length pseudorandom key K is not returned.
59 In this mode the digest, key, salt and info values must be set before a key is
60 derived otherwise an error will occur.
62 =item B<EVP_KDF_HKDF_MODE_EXTRACT_ONLY> "EXTRACT_ONLY"
64 In this mode calling L<EVP_KDF-derive(3)> will just perform the extract
65 operation. The value returned will be the intermediate fixed-length pseudorandom
66 key K. The C<keylen> parameter must match the size of K, which can be looked
67 up by calling EVP_KDF_size() after setting the mode and digest.
69 The digest, key and salt values must be set before a key is derived otherwise
72 =item B<EVP_KDF_HKDF_MODE_EXPAND_ONLY> "EXPAND_ONLY"
74 In this mode calling L<EVP_KDF-derive(3)> will just perform the expand
75 operation. The input key should be set to the intermediate fixed-length
76 pseudorandom key K returned from a previous extract operation.
78 The digest, key and info values must be set before a key is derived otherwise
87 A context for HKDF can be obtained by calling:
89 EVP_KDF *kdf = EVP_KDF_fetch(NULL, "HKDF", NULL);
90 EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf);
92 The output length of an HKDF expand operation is specified via the C<keylen>
93 parameter to the L<EVP_KDF-derive(3)> function. When using
94 EVP_KDF_HKDF_MODE_EXTRACT_ONLY the C<keylen> parameter must equal the size of
95 the intermediate fixed-length pseudorandom key otherwise an error will occur.
96 For that mode, the fixed output size can be looked up by calling EVP_KDF_size()
97 after setting the mode and digest on the C<EVP_KDF_CTX>.
101 This example derives 10 bytes using SHA-256 with the secret key "secret",
102 salt value "salt" and info value "label":
106 unsigned char out[10];
107 OSSL_PARAM params[5], *p = params;
109 kdf = EVP_KDF_fetch(NULL, "HKDF", NULL);
110 kctx = EVP_KDF_CTX_new(kdf);
113 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
114 SN_sha256, strlen(SN_sha256));
115 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
116 "secret", (size_t)6);
117 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
119 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
121 *p = OSSL_PARAM_construct_end();
122 if (EVP_KDF_CTX_set_params(kctx, params) <= 0) {
123 error("EVP_KDF_CTX_set_params");
125 if (EVP_KDF_derive(kctx, out, sizeof(out)) <= 0) {
126 error("EVP_KDF_derive");
129 EVP_KDF_CTX_free(kctx);
138 L<EVP_KDF_CTX_new(3)>,
139 L<EVP_KDF_CTX_free(3)>,
141 L<EVP_KDF_CTX_set_params(3)>,
142 L<EVP_KDF_derive(3)>,
143 L<EVP_KDF(3)/PARAMETERS>
147 Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
149 Licensed under the Apache License 2.0 (the "License"). You may not use
150 this file except in compliance with the License. You can obtain a copy
151 in the file LICENSE in the source distribution or at
152 L<https://www.openssl.org/source/license.html>.