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_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_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_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 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 98All these functions are implemented as macros. 99 100A context for HKDF can be obtained by calling: 101 102 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL); 103 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 107The output length of an HKDF expand operation is specified via the length 108parameter to the L<EVP_PKEY_derive(3)> function. 109Since the HKDF output length is variable, passing a B<NULL> buffer as a means 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. 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 124=head1 EXAMPLES 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) 135 /* Error */ 136 if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) 137 /* Error */ 138 if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, "salt", 4) <= 0) 139 /* Error */ 140 if (EVP_PKEY_CTX_set1_hkdf_key(pctx, "secret", 6) <= 0) 141 /* Error */ 142 if (EVP_PKEY_CTX_add1_hkdf_info(pctx, "label", 5) <= 0) 143 /* Error */ 144 if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) 145 /* Error */ 146 147=head1 CONFORMING TO 148 149RFC 5869 150 151=head1 SEE ALSO 152 153L<EVP_PKEY_CTX_new(3)>, 154L<EVP_PKEY_CTX_ctrl_str(3)>, 155L<EVP_PKEY_derive(3)> 156 157=head1 COPYRIGHT 158 159Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved. 160 161Licensed under the OpenSSL license (the "License"). You may not use 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 167