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