1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1 christos EVP_KDF, EVP_KDF_fetch, EVP_KDF_free, EVP_KDF_up_ref, 6 1.1 christos EVP_KDF_CTX, EVP_KDF_CTX_new, EVP_KDF_CTX_free, EVP_KDF_CTX_dup, 7 1.1 christos EVP_KDF_CTX_reset, EVP_KDF_derive, 8 1.1 christos EVP_KDF_CTX_get_kdf_size, 9 1.1 christos EVP_KDF_get0_provider, EVP_KDF_CTX_kdf, EVP_KDF_is_a, 10 1.1 christos EVP_KDF_get0_name, EVP_KDF_names_do_all, EVP_KDF_get0_description, 11 1.1 christos EVP_KDF_CTX_get_params, EVP_KDF_CTX_set_params, EVP_KDF_do_all_provided, 12 1.1 christos EVP_KDF_get_params, EVP_KDF_gettable_params, 13 1.1 christos EVP_KDF_gettable_ctx_params, EVP_KDF_settable_ctx_params, 14 1.1 christos EVP_KDF_CTX_gettable_params, EVP_KDF_CTX_settable_params - EVP KDF routines 15 1.1 christos 16 1.1 christos =head1 SYNOPSIS 17 1.1 christos 18 1.1 christos #include <openssl/kdf.h> 19 1.1 christos 20 1.1 christos typedef struct evp_kdf_st EVP_KDF; 21 1.1 christos typedef struct evp_kdf_ctx_st EVP_KDF_CTX; 22 1.1 christos 23 1.1.1.3 christos EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf); 24 1.1 christos const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx); 25 1.1 christos void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx); 26 1.1 christos EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src); 27 1.1 christos void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx); 28 1.1 christos size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx); 29 1.1 christos int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen, 30 1.1 christos const OSSL_PARAM params[]); 31 1.1 christos int EVP_KDF_up_ref(EVP_KDF *kdf); 32 1.1 christos void EVP_KDF_free(EVP_KDF *kdf); 33 1.1 christos EVP_KDF *EVP_KDF_fetch(OSSL_LIB_CTX *libctx, const char *algorithm, 34 1.1 christos const char *properties); 35 1.1 christos int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name); 36 1.1 christos const char *EVP_KDF_get0_name(const EVP_KDF *kdf); 37 1.1 christos const char *EVP_KDF_get0_description(const EVP_KDF *kdf); 38 1.1 christos const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf); 39 1.1 christos void EVP_KDF_do_all_provided(OSSL_LIB_CTX *libctx, 40 1.1 christos void (*fn)(EVP_KDF *kdf, void *arg), 41 1.1 christos void *arg); 42 1.1 christos int EVP_KDF_names_do_all(const EVP_KDF *kdf, 43 1.1 christos void (*fn)(const char *name, void *data), 44 1.1 christos void *data); 45 1.1 christos int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[]); 46 1.1 christos int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[]); 47 1.1 christos int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[]); 48 1.1 christos const OSSL_PARAM *EVP_KDF_gettable_params(const EVP_KDF *kdf); 49 1.1 christos const OSSL_PARAM *EVP_KDF_gettable_ctx_params(const EVP_KDF *kdf); 50 1.1 christos const OSSL_PARAM *EVP_KDF_settable_ctx_params(const EVP_KDF *kdf); 51 1.1 christos const OSSL_PARAM *EVP_KDF_CTX_gettable_params(const EVP_KDF *kdf); 52 1.1 christos const OSSL_PARAM *EVP_KDF_CTX_settable_params(const EVP_KDF *kdf); 53 1.1 christos const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf); 54 1.1 christos 55 1.1 christos =head1 DESCRIPTION 56 1.1 christos 57 1.1 christos The EVP KDF routines are a high-level interface to Key Derivation Function 58 1.1 christos algorithms and should be used instead of algorithm-specific functions. 59 1.1 christos 60 1.1 christos After creating a B<EVP_KDF_CTX> for the required algorithm using 61 1.1 christos EVP_KDF_CTX_new(), inputs to the algorithm are supplied either by 62 1.1 christos passing them as part of the EVP_KDF_derive() call or using calls 63 1.1 christos to EVP_KDF_CTX_set_params() before calling EVP_KDF_derive() to derive 64 1.1 christos the key. 65 1.1 christos 66 1.1 christos =head2 Types 67 1.1 christos 68 1.1 christos B<EVP_KDF> is a type that holds the implementation of a KDF. 69 1.1 christos 70 1.1 christos B<EVP_KDF_CTX> is a context type that holds the algorithm inputs. 71 1.1 christos 72 1.1 christos =head2 Algorithm implementation fetching 73 1.1 christos 74 1.1 christos EVP_KDF_fetch() fetches an implementation of a KDF I<algorithm>, given 75 1.1 christos a library context I<libctx> and a set of I<properties>. 76 1.1 christos See L<crypto(7)/ALGORITHM FETCHING> for further information. 77 1.1 christos 78 1.1 christos See L<OSSL_PROVIDER-default(7)/Key Derivation Function (KDF)> for the lists of 79 1.1 christos algorithms supported by the default provider. 80 1.1 christos 81 1.1 christos The returned value must eventually be freed with 82 1.1 christos L<EVP_KDF_free(3)>. 83 1.1 christos 84 1.1 christos EVP_KDF_up_ref() increments the reference count of an already fetched 85 1.1 christos KDF. 86 1.1 christos 87 1.1 christos EVP_KDF_free() frees a fetched algorithm. 88 1.1 christos NULL is a valid parameter, for which this function is a no-op. 89 1.1 christos 90 1.1 christos =head2 Context manipulation functions 91 1.1 christos 92 1.1 christos EVP_KDF_CTX_new() creates a new context for the KDF implementation I<kdf>. 93 1.1 christos 94 1.1 christos EVP_KDF_CTX_free() frees up the context I<ctx>. If I<ctx> is NULL, nothing 95 1.1 christos is done. 96 1.1 christos 97 1.1 christos EVP_KDF_CTX_kdf() returns the B<EVP_KDF> associated with the context 98 1.1 christos I<ctx>. 99 1.1 christos 100 1.1 christos =head2 Computing functions 101 1.1 christos 102 1.1 christos EVP_KDF_CTX_reset() resets the context to the default state as if the context 103 1.1 christos had just been created. 104 1.1 christos 105 1.1 christos EVP_KDF_derive() processes any parameters in I<Params> and then derives 106 1.1 christos I<keylen> bytes of key material and places it in the I<key> buffer. 107 1.1 christos If the algorithm produces a fixed amount of output then an error will 108 1.1 christos occur unless the I<keylen> parameter is equal to that output size, 109 1.1 christos as returned by EVP_KDF_CTX_get_kdf_size(). 110 1.1 christos 111 1.1 christos EVP_KDF_get_params() retrieves details about the implementation 112 1.1 christos I<kdf>. 113 1.1 christos The set of parameters given with I<params> determine exactly what 114 1.1 christos parameters should be retrieved. 115 1.1 christos Note that a parameter that is unknown in the underlying context is 116 1.1 christos simply ignored. 117 1.1 christos 118 1.1 christos EVP_KDF_CTX_get_params() retrieves chosen parameters, given the 119 1.1 christos context I<ctx> and its underlying context. 120 1.1 christos The set of parameters given with I<params> determine exactly what 121 1.1 christos parameters should be retrieved. 122 1.1 christos Note that a parameter that is unknown in the underlying context is 123 1.1 christos simply ignored. 124 1.1 christos 125 1.1 christos EVP_KDF_CTX_set_params() passes chosen parameters to the underlying 126 1.1 christos context, given a context I<ctx>. 127 1.1 christos The set of parameters given with I<params> determine exactly what 128 1.1 christos parameters are passed down. 129 1.1 christos Note that a parameter that is unknown in the underlying context is 130 1.1 christos simply ignored. 131 1.1 christos Also, what happens when a needed parameter isn't passed down is 132 1.1 christos defined by the implementation. 133 1.1 christos 134 1.1 christos EVP_KDF_gettable_params() returns an L<OSSL_PARAM(3)> array that describes 135 1.1 christos the retrievable and settable parameters. EVP_KDF_gettable_params() 136 1.1 christos returns parameters that can be used with EVP_KDF_get_params(). 137 1.1 christos 138 1.1 christos EVP_KDF_gettable_ctx_params() and EVP_KDF_CTX_gettable_params() 139 1.1 christos return constant L<OSSL_PARAM(3)> arrays that describe the retrievable 140 1.1 christos parameters that can be used with EVP_KDF_CTX_get_params(). 141 1.1 christos EVP_KDF_gettable_ctx_params() returns the parameters that can be retrieved 142 1.1 christos from the algorithm, whereas EVP_KDF_CTX_gettable_params() returns 143 1.1 christos the parameters that can be retrieved in the context's current state. 144 1.1 christos 145 1.1 christos EVP_KDF_settable_ctx_params() and EVP_KDF_CTX_settable_params() return 146 1.1 christos constant L<OSSL_PARAM(3)> arrays that describe the settable parameters that 147 1.1 christos can be used with EVP_KDF_CTX_set_params(). EVP_KDF_settable_ctx_params() 148 1.1 christos returns the parameters that can be retrieved from the algorithm, 149 1.1 christos whereas EVP_KDF_CTX_settable_params() returns the parameters that can 150 1.1 christos be retrieved in the context's current state. 151 1.1 christos 152 1.1 christos =head2 Information functions 153 1.1 christos 154 1.1 christos EVP_KDF_CTX_get_kdf_size() returns the output size if the algorithm produces a fixed amount 155 1.1 christos of output and B<SIZE_MAX> otherwise. If an error occurs then 0 is returned. 156 1.1 christos For some algorithms an error may result if input parameters necessary to 157 1.1 christos calculate a fixed output size have not yet been supplied. 158 1.1 christos 159 1.1 christos EVP_KDF_is_a() returns 1 if I<kdf> is an implementation of an 160 1.1 christos algorithm that's identifiable with I<name>, otherwise 0. 161 1.1 christos 162 1.1 christos EVP_KDF_get0_provider() returns the provider that holds the implementation 163 1.1 christos of the given I<kdf>. 164 1.1 christos 165 1.1 christos EVP_KDF_do_all_provided() traverses all KDF implemented by all activated 166 1.1 christos providers in the given library context I<libctx>, and for each of the 167 1.1 christos implementations, calls the given function I<fn> with the implementation method 168 1.1 christos and the given I<arg> as argument. 169 1.1 christos 170 1.1 christos EVP_KDF_get0_name() return the name of the given KDF. For fetched KDFs 171 1.1 christos with multiple names, only one of them is returned; it's 172 1.1 christos recommended to use EVP_KDF_names_do_all() instead. 173 1.1 christos 174 1.1 christos EVP_KDF_names_do_all() traverses all names for I<kdf>, and calls 175 1.1 christos I<fn> with each name and I<data>. 176 1.1 christos 177 1.1 christos EVP_KDF_get0_description() returns a description of the I<kdf>, meant for 178 1.1 christos display and human consumption. The description is at the discretion of 179 1.1 christos the I<kdf> implementation. 180 1.1 christos 181 1.1 christos =head1 PARAMETERS 182 1.1 christos 183 1.1 christos The standard parameter names are: 184 1.1 christos 185 1.1 christos =over 4 186 1.1 christos 187 1.1 christos =item "pass" (B<OSSL_KDF_PARAM_PASSWORD>) <octet string> 188 1.1 christos 189 1.1 christos Some KDF implementations require a password. 190 1.1 christos For those KDF implementations that support it, this parameter sets the password. 191 1.1 christos 192 1.1 christos =item "salt" (B<OSSL_KDF_PARAM_SALT>) <octet string> 193 1.1 christos 194 1.1.1.2 christos Some KDF implementations can take a non-secret unique cryptographic salt. 195 1.1 christos For those KDF implementations that support it, this parameter sets the salt. 196 1.1 christos 197 1.1 christos The default value, if any, is implementation dependent. 198 1.1 christos 199 1.1 christos =item "iter" (B<OSSL_KDF_PARAM_ITER>) <unsigned integer> 200 1.1 christos 201 1.1 christos Some KDF implementations require an iteration count. 202 1.1 christos For those KDF implementations that support it, this parameter sets the 203 1.1 christos iteration count. 204 1.1 christos 205 1.1 christos The default value, if any, is implementation dependent. 206 1.1 christos 207 1.1 christos =item "properties" (B<OSSL_KDF_PARAM_PROPERTIES>) <UTF8 string> 208 1.1 christos 209 1.1 christos =item "mac" (B<OSSL_KDF_PARAM_MAC>) <UTF8 string> 210 1.1 christos 211 1.1 christos =item "digest" (B<OSSL_KDF_PARAM_DIGEST>) <UTF8 string> 212 1.1 christos 213 1.1 christos =item "cipher" (B<OSSL_KDF_PARAM_CIPHER>) <UTF8 string> 214 1.1 christos 215 1.1 christos For KDF implementations that use an underlying computation MAC, digest or 216 1.1 christos cipher, these parameters set what the algorithm should be. 217 1.1 christos 218 1.1 christos The value is always the name of the intended algorithm, 219 1.1 christos or the properties. 220 1.1 christos 221 1.1 christos Note that not all algorithms may support all possible underlying 222 1.1 christos implementations. 223 1.1 christos 224 1.1 christos =item "key" (B<OSSL_KDF_PARAM_KEY>) <octet string> 225 1.1 christos 226 1.1 christos Some KDF implementations require a key. 227 1.1 christos For those KDF implementations that support it, this octet string parameter 228 1.1 christos sets the key. 229 1.1 christos 230 1.1.1.2 christos =item "info" (B<OSSL_KDF_PARAM_INFO>) <octet string> 231 1.1.1.2 christos 232 1.1.1.2 christos Some KDF implementations, such as L<EVP_KDF-HKDF(7)>, take an 'info' parameter 233 1.1.1.2 christos for binding the derived key material 234 1.1.1.2 christos to application- and context-specific information. 235 1.1.1.2 christos This parameter sets the info, fixed info, other info or shared info argument. 236 1.1.1.2 christos You can specify this parameter multiple times, and each instance will 237 1.1.1.2 christos be concatenated to form the final value. 238 1.1.1.2 christos 239 1.1 christos =item "maclen" (B<OSSL_KDF_PARAM_MAC_SIZE>) <unsigned integer> 240 1.1 christos 241 1.1 christos Used by implementations that use a MAC with a variable output size (KMAC). 242 1.1 christos For those KDF implementations that support it, this parameter 243 1.1 christos sets the MAC output size. 244 1.1 christos 245 1.1 christos The default value, if any, is implementation dependent. 246 1.1 christos The length must never exceed what can be given with a B<size_t>. 247 1.1 christos 248 1.1 christos =item "maxmem_bytes" (B<OSSL_KDF_PARAM_SCRYPT_MAXMEM>) <unsigned integer> 249 1.1 christos 250 1.1 christos Memory-hard password-based KDF algorithms, such as scrypt, use an amount of 251 1.1 christos memory that depends on the load factors provided as input. 252 1.1 christos For those KDF implementations that support it, this B<uint64_t> parameter sets 253 1.1 christos an upper limit on the amount of memory that may be consumed while performing 254 1.1 christos a key derivation. 255 1.1 christos If this memory usage limit is exceeded because the load factors are chosen 256 1.1 christos too high, the key derivation will fail. 257 1.1 christos 258 1.1 christos The default value is implementation dependent. 259 1.1 christos The memory size must never exceed what can be given with a B<size_t>. 260 1.1 christos 261 1.1 christos =back 262 1.1 christos 263 1.1 christos =head1 RETURN VALUES 264 1.1 christos 265 1.1 christos EVP_KDF_fetch() returns a pointer to a newly fetched B<EVP_KDF>, or 266 1.1 christos NULL if allocation failed. 267 1.1 christos 268 1.1 christos EVP_KDF_get0_provider() returns a pointer to the provider for the KDF, or 269 1.1 christos NULL on error. 270 1.1 christos 271 1.1 christos EVP_KDF_up_ref() returns 1 on success, 0 on error. 272 1.1 christos 273 1.1 christos EVP_KDF_CTX_new() returns either the newly allocated 274 1.1 christos B<EVP_KDF_CTX> structure or NULL if an error occurred. 275 1.1 christos 276 1.1 christos EVP_KDF_CTX_free() and EVP_KDF_CTX_reset() do not return a value. 277 1.1 christos 278 1.1 christos EVP_KDF_CTX_get_kdf_size() returns the output size. B<SIZE_MAX> is returned to indicate 279 1.1 christos that the algorithm produces a variable amount of output; 0 to indicate failure. 280 1.1 christos 281 1.1 christos EVP_KDF_get0_name() returns the name of the KDF, or NULL on error. 282 1.1 christos 283 1.1 christos EVP_KDF_names_do_all() returns 1 if the callback was called for all names. A 284 1.1 christos return value of 0 means that the callback was not called for any names. 285 1.1 christos 286 1.1 christos The remaining functions return 1 for success and 0 or a negative value for 287 1.1 christos failure. In particular, a return value of -2 indicates the operation is not 288 1.1 christos supported by the KDF algorithm. 289 1.1 christos 290 1.1 christos =head1 NOTES 291 1.1 christos 292 1.1 christos The KDF life-cycle is described in L<life_cycle-kdf(7)>. In the future, 293 1.1 christos the transitions described there will be enforced. When this is done, it will 294 1.1 christos not be considered a breaking change to the API. 295 1.1 christos 296 1.1 christos =head1 SEE ALSO 297 1.1 christos 298 1.1 christos L<OSSL_PROVIDER-default(7)/Key Derivation Function (KDF)>, 299 1.1 christos L<life_cycle-kdf(7)>. 300 1.1 christos 301 1.1 christos =head1 HISTORY 302 1.1 christos 303 1.1 christos This functionality was added in OpenSSL 3.0. 304 1.1 christos 305 1.1 christos =head1 COPYRIGHT 306 1.1 christos 307 1.1.1.3 christos Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved. 308 1.1 christos 309 1.1 christos Licensed under the Apache License 2.0 (the "License"). You may not use 310 1.1 christos this file except in compliance with the License. You can obtain a copy 311 1.1 christos in the file LICENSE in the source distribution or at 312 1.1 christos L<https://www.openssl.org/source/license.html>. 313 1.1 christos 314 1.1 christos =cut 315