1=pod 2 3=head1 NAME 4 5EVP_PKEY_CTX_set_hkdf_md, EVP_PKEY_CTX_set1_hkdf_salt, 6EVP_PKEY_CTX_set1_hkdf_key, EVP_PKEY_CTX_add1_hkdf_info, 7EVP_PKEY_CTX_set_hkdf_mode - 8HMAC-based Extract-and-Expand key derivation algorithm 9 10=head1 SYNOPSIS 11 12 #include <openssl/kdf.h> 13 14 int EVP_PKEY_CTX_set_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 29The EVP_PKEY_HKDF algorithm implements the HKDF key derivation function. 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 36EVP_PKEY_CTX_set_hkdf_mode() sets the mode for the HKDF operation. There 37are three modes 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 71EVP_PKEY_CTX_set_hkdf_md() sets the message digest associated with the HKDF. 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 76EVP_PKEY_CTX_set1_hkdf_key() sets the key to B<keylen> bytes of the buffer 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 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. 89The B<type> parameter "mode" uses the values "EXTRACT_AND_EXPAND", 90"EXTRACT_ONLY" and "EXPAND_ONLY" to determine the mode to use. 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 96=head1 NOTES 97 98A context for HKDF can be obtained by calling: 99 100 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL); 101 102The total length of the info buffer cannot exceed 1024 bytes in length: this 103should be more than enough for any normal use of HKDF. 104 105The output length of an HKDF expand operation is specified via the length 106parameter to the L<EVP_PKEY_derive(3)> function. 107Since the HKDF output length is variable, passing a B<NULL> buffer as a means 108to obtain the requisite length is not meaningful with HKDF in any mode that 109performs an expand operation. Instead, the caller must allocate a buffer of the 110desired length, and pass that buffer to L<EVP_PKEY_derive(3)> along with (a 111pointer initialized to) the desired length. Passing a B<NULL> buffer to obtain 112the length is allowed when using EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY. 113 114Optimised versions of HKDF can be implemented in an ENGINE. 115 116=head1 RETURN VALUES 117 118All these functions return 1 for success and 0 or a negative value for failure. 119In particular a return value of -2 indicates the operation is not supported by 120the public key algorithm. 121 122=head1 EXAMPLES 123 124This example derives 10 bytes using SHA-256 with the secret key "secret", 125salt 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 147RFC 5869 148 149=head1 SEE ALSO 150 151L<EVP_PKEY_CTX_new(3)>, 152L<EVP_PKEY_CTX_ctrl_str(3)>, 153L<EVP_PKEY_derive(3)> 154 155=head1 HISTORY 156 157All of the functions described here were converted from macros to functions in 158OpenSSL 3.0. 159 160=head1 COPYRIGHT 161 162Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. 163 164Licensed under the Apache License 2.0 (the "License"). You may not use 165this file except in compliance with the License. You can obtain a copy 166in the file LICENSE in the source distribution or at 167L<https://www.openssl.org/source/license.html>. 168 169=cut 170