1e1051a39Sopenharmony_ci=pod
2e1051a39Sopenharmony_ci
3e1051a39Sopenharmony_ci=head1 NAME
4e1051a39Sopenharmony_ci
5e1051a39Sopenharmony_ciprovider-keyexch - The keyexch library E<lt>-E<gt> provider functions
6e1051a39Sopenharmony_ci
7e1051a39Sopenharmony_ci=head1 SYNOPSIS
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ci=for openssl multiple includes
10e1051a39Sopenharmony_ci
11e1051a39Sopenharmony_ci #include <openssl/core_dispatch.h>
12e1051a39Sopenharmony_ci #include <openssl/core_names.h>
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ci /*
15e1051a39Sopenharmony_ci  * None of these are actual functions, but are displayed like this for
16e1051a39Sopenharmony_ci  * the function signatures for functions that are offered as function
17e1051a39Sopenharmony_ci  * pointers in OSSL_DISPATCH arrays.
18e1051a39Sopenharmony_ci  */
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ci /* Context management */
21e1051a39Sopenharmony_ci void *OSSL_FUNC_keyexch_newctx(void *provctx);
22e1051a39Sopenharmony_ci void OSSL_FUNC_keyexch_freectx(void *ctx);
23e1051a39Sopenharmony_ci void *OSSL_FUNC_keyexch_dupctx(void *ctx);
24e1051a39Sopenharmony_ci
25e1051a39Sopenharmony_ci /* Shared secret derivation */
26e1051a39Sopenharmony_ci int OSSL_FUNC_keyexch_init(void *ctx, void *provkey,
27e1051a39Sopenharmony_ci                            const OSSL_PARAM params[]);
28e1051a39Sopenharmony_ci int OSSL_FUNC_keyexch_set_peer(void *ctx, void *provkey);
29e1051a39Sopenharmony_ci int OSSL_FUNC_keyexch_derive(void *ctx, unsigned char *secret, size_t *secretlen,
30e1051a39Sopenharmony_ci                              size_t outlen);
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci /* Key Exchange parameters */
33e1051a39Sopenharmony_ci int OSSL_FUNC_keyexch_set_ctx_params(void *ctx, const OSSL_PARAM params[]);
34e1051a39Sopenharmony_ci const OSSL_PARAM *OSSL_FUNC_keyexch_settable_ctx_params(void *ctx,
35e1051a39Sopenharmony_ci                                                         void *provctx);
36e1051a39Sopenharmony_ci int OSSL_FUNC_keyexch_get_ctx_params(void *ctx, OSSL_PARAM params[]);
37e1051a39Sopenharmony_ci const OSSL_PARAM *OSSL_FUNC_keyexch_gettable_ctx_params(void *ctx,
38e1051a39Sopenharmony_ci                                                         void *provctx);
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_ci=head1 DESCRIPTION
41e1051a39Sopenharmony_ci
42e1051a39Sopenharmony_ciThis documentation is primarily aimed at provider authors. See L<provider(7)>
43e1051a39Sopenharmony_cifor further information.
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_ciThe key exchange (OSSL_OP_KEYEXCH) operation enables providers to implement key
46e1051a39Sopenharmony_ciexchange algorithms and make them available to applications via
47e1051a39Sopenharmony_ciL<EVP_PKEY_derive(3)> and
48e1051a39Sopenharmony_ciother related functions).
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_ciAll "functions" mentioned here are passed as function pointers between
51e1051a39Sopenharmony_ciF<libcrypto> and the provider in L<OSSL_DISPATCH(3)> arrays via
52e1051a39Sopenharmony_ciL<OSSL_ALGORITHM(3)> arrays that are returned by the provider's
53e1051a39Sopenharmony_ciprovider_query_operation() function
54e1051a39Sopenharmony_ci(see L<provider-base(7)/Provider Functions>).
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ciAll these "functions" have a corresponding function type definition
57e1051a39Sopenharmony_cinamed B<OSSL_FUNC_{name}_fn>, and a helper function to retrieve the
58e1051a39Sopenharmony_cifunction pointer from an L<OSSL_DISPATCH(3)> element named
59e1051a39Sopenharmony_ciB<OSSL_FUNC_{name}>.
60e1051a39Sopenharmony_ciFor example, the "function" OSSL_FUNC_keyexch_newctx() has these:
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_ci typedef void *(OSSL_FUNC_keyexch_newctx_fn)(void *provctx);
63e1051a39Sopenharmony_ci static ossl_inline OSSL_FUNC_keyexch_newctx_fn
64e1051a39Sopenharmony_ci     OSSL_FUNC_keyexch_newctx(const OSSL_DISPATCH *opf);
65e1051a39Sopenharmony_ci
66e1051a39Sopenharmony_ciL<OSSL_DISPATCH(3)> arrays are indexed by numbers that are provided as
67e1051a39Sopenharmony_cimacros in L<openssl-core_dispatch.h(7)>, as follows:
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci OSSL_FUNC_keyexch_newctx                OSSL_FUNC_KEYEXCH_NEWCTX
70e1051a39Sopenharmony_ci OSSL_FUNC_keyexch_freectx               OSSL_FUNC_KEYEXCH_FREECTX
71e1051a39Sopenharmony_ci OSSL_FUNC_keyexch_dupctx                OSSL_FUNC_KEYEXCH_DUPCTX
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_ci OSSL_FUNC_keyexch_init                  OSSL_FUNC_KEYEXCH_INIT
74e1051a39Sopenharmony_ci OSSL_FUNC_keyexch_set_peer              OSSL_FUNC_KEYEXCH_SET_PEER
75e1051a39Sopenharmony_ci OSSL_FUNC_keyexch_derive                OSSL_FUNC_KEYEXCH_DERIVE
76e1051a39Sopenharmony_ci
77e1051a39Sopenharmony_ci OSSL_FUNC_keyexch_set_ctx_params        OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS
78e1051a39Sopenharmony_ci OSSL_FUNC_keyexch_settable_ctx_params   OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS
79e1051a39Sopenharmony_ci OSSL_FUNC_keyexch_get_ctx_params        OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS
80e1051a39Sopenharmony_ci OSSL_FUNC_keyexch_gettable_ctx_params   OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ciA key exchange algorithm implementation may not implement all of these functions.
83e1051a39Sopenharmony_ciIn order to be a consistent set of functions a provider must implement
84e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_newctx, OSSL_FUNC_keyexch_freectx, OSSL_FUNC_keyexch_init and OSSL_FUNC_keyexch_derive.
85e1051a39Sopenharmony_ciAll other functions are optional.
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_ciA key exchange algorithm must also implement some mechanism for generating,
88e1051a39Sopenharmony_ciloading or importing keys via the key management (OSSL_OP_KEYMGMT) operation.
89e1051a39Sopenharmony_ciSee L<provider-keymgmt(7)> for further details.
90e1051a39Sopenharmony_ci
91e1051a39Sopenharmony_ci=head2 Context Management Functions
92e1051a39Sopenharmony_ci
93e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_newctx() should create and return a pointer to a provider side
94e1051a39Sopenharmony_cistructure for holding context information during a key exchange operation.
95e1051a39Sopenharmony_ciA pointer to this context will be passed back in a number of the other key
96e1051a39Sopenharmony_ciexchange operation function calls.
97e1051a39Sopenharmony_ciThe parameter I<provctx> is the provider context generated during provider
98e1051a39Sopenharmony_ciinitialisation (see L<provider(7)>).
99e1051a39Sopenharmony_ci
100e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_freectx() is passed a pointer to the provider side key exchange
101e1051a39Sopenharmony_cicontext in the I<ctx> parameter.
102e1051a39Sopenharmony_ciThis function should free any resources associated with that context.
103e1051a39Sopenharmony_ci
104e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_dupctx() should duplicate the provider side key exchange context in
105e1051a39Sopenharmony_cithe I<ctx> parameter and return the duplicate copy.
106e1051a39Sopenharmony_ci
107e1051a39Sopenharmony_ci=head2 Shared Secret Derivation Functions
108e1051a39Sopenharmony_ci
109e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_init() initialises a key exchange operation given a provider side key
110e1051a39Sopenharmony_ciexchange context in the I<ctx> parameter, and a pointer to a provider key object
111e1051a39Sopenharmony_ciin the I<provkey> parameter.
112e1051a39Sopenharmony_ciThe I<params>, if not NULL, should be set on the context in a manner similar to
113e1051a39Sopenharmony_ciusing OSSL_FUNC_keyexch_set_params().
114e1051a39Sopenharmony_ciThe key object should have been previously
115e1051a39Sopenharmony_cigenerated, loaded or imported into the provider using the key management
116e1051a39Sopenharmony_ci(OSSL_OP_KEYMGMT) operation (see provider-keymgmt(7)>.
117e1051a39Sopenharmony_ci
118e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_set_peer() is called to supply the peer's public key (in the
119e1051a39Sopenharmony_ciI<provkey> parameter) to be used when deriving the shared secret.
120e1051a39Sopenharmony_ciIt is also passed a previously initialised key exchange context in the I<ctx>
121e1051a39Sopenharmony_ciparameter.
122e1051a39Sopenharmony_ciThe key object should have been previously generated, loaded or imported into
123e1051a39Sopenharmony_cithe provider using the key management (OSSL_OP_KEYMGMT) operation (see
124e1051a39Sopenharmony_ciprovider-keymgmt(7)>.
125e1051a39Sopenharmony_ci
126e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_derive() performs the actual key exchange itself by deriving a shared
127e1051a39Sopenharmony_cisecret.
128e1051a39Sopenharmony_ciA previously initialised key exchange context is passed in the I<ctx>
129e1051a39Sopenharmony_ciparameter.
130e1051a39Sopenharmony_ciThe derived secret should be written to the location I<secret> which should not
131e1051a39Sopenharmony_ciexceed I<outlen> bytes.
132e1051a39Sopenharmony_ciThe length of the shared secret should be written to I<*secretlen>.
133e1051a39Sopenharmony_ciIf I<secret> is NULL then the maximum length of the shared secret should be
134e1051a39Sopenharmony_ciwritten to I<*secretlen>.
135e1051a39Sopenharmony_ci
136e1051a39Sopenharmony_ci=head2 Key Exchange Parameters Functions
137e1051a39Sopenharmony_ci
138e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_set_ctx_params() sets key exchange parameters associated with the
139e1051a39Sopenharmony_cigiven provider side key exchange context I<ctx> to I<params>,
140e1051a39Sopenharmony_cisee L</Common Key Exchange parameters>.
141e1051a39Sopenharmony_ciAny parameter settings are additional to any that were previously set.
142e1051a39Sopenharmony_ciPassing NULL for I<params> should return true.
143e1051a39Sopenharmony_ci
144e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_get_ctx_params() gets key exchange parameters associated with the
145e1051a39Sopenharmony_cigiven provider side key exchange context I<ctx> into I<params>,
146e1051a39Sopenharmony_cisee L</Common Key Exchange parameters>.
147e1051a39Sopenharmony_ciPassing NULL for I<params> should return true.
148e1051a39Sopenharmony_ci
149e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_settable_ctx_params() yields a constant L<OSSL_PARAM(3)> array that
150e1051a39Sopenharmony_cidescribes the settable parameters, i.e. parameters that can be used with
151e1051a39Sopenharmony_ciOP_signature_set_ctx_params().
152e1051a39Sopenharmony_ciIf OSSL_FUNC_keyexch_settable_ctx_params() is present, OSSL_FUNC_keyexch_set_ctx_params() must
153e1051a39Sopenharmony_cialso be present, and vice versa.
154e1051a39Sopenharmony_ciSimilarly, OSSL_FUNC_keyexch_gettable_ctx_params() yields a constant L<OSSL_PARAM(3)>
155e1051a39Sopenharmony_ciarray that describes the gettable parameters, i.e. parameters that can be
156e1051a39Sopenharmony_cihandled by OP_signature_get_ctx_params().
157e1051a39Sopenharmony_ciIf OSSL_FUNC_keyexch_gettable_ctx_params() is present, OSSL_FUNC_keyexch_get_ctx_params() must
158e1051a39Sopenharmony_cialso be present, and vice versa.
159e1051a39Sopenharmony_ci
160e1051a39Sopenharmony_ciNotice that not all settable parameters are also gettable, and vice versa.
161e1051a39Sopenharmony_ci
162e1051a39Sopenharmony_ci=head2 Common Key Exchange parameters
163e1051a39Sopenharmony_ci
164e1051a39Sopenharmony_ciSee L<OSSL_PARAM(3)> for further details on the parameters structure used by
165e1051a39Sopenharmony_cithe OSSL_FUNC_keyexch_set_ctx_params() and OSSL_FUNC_keyexch_get_ctx_params() functions.
166e1051a39Sopenharmony_ci
167e1051a39Sopenharmony_ciCommon parameters currently recognised by built-in key exchange algorithms are
168e1051a39Sopenharmony_cias follows.
169e1051a39Sopenharmony_ci
170e1051a39Sopenharmony_ci=over 4
171e1051a39Sopenharmony_ci
172e1051a39Sopenharmony_ci=item "kdf-type" (B<OSSL_EXCHANGE_PARAM_KDF_TYPE>) <UTF8 string>
173e1051a39Sopenharmony_ci
174e1051a39Sopenharmony_ciSets or gets the Key Derivation Function type to apply within the associated key
175e1051a39Sopenharmony_ciexchange ctx.
176e1051a39Sopenharmony_ci
177e1051a39Sopenharmony_ci=item "kdf-digest" (B<OSSL_EXCHANGE_PARAM_KDF_DIGEST>) <UTF8 string>
178e1051a39Sopenharmony_ci
179e1051a39Sopenharmony_ciSets or gets the Digest algorithm to be used as part of the Key Derivation Function
180e1051a39Sopenharmony_ciassociated with the given key exchange ctx.
181e1051a39Sopenharmony_ci
182e1051a39Sopenharmony_ci=item "kdf-digest-props" (B<OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS>) <UTF8 string>
183e1051a39Sopenharmony_ci
184e1051a39Sopenharmony_ciSets properties to be used upon look up of the implementation for the selected
185e1051a39Sopenharmony_ciDigest algorithm for the Key Derivation Function associated with the given key
186e1051a39Sopenharmony_ciexchange ctx.
187e1051a39Sopenharmony_ci
188e1051a39Sopenharmony_ci=item "kdf-outlen" (B<OSSL_EXCHANGE_PARAM_KDF_OUTLEN>) <unsigned integer>
189e1051a39Sopenharmony_ci
190e1051a39Sopenharmony_ciSets or gets the desired size for the output of the chosen Key Derivation Function
191e1051a39Sopenharmony_ciassociated with the given key exchange ctx.
192e1051a39Sopenharmony_ciThe length of the "kdf-outlen" parameter should not exceed that of a B<size_t>.
193e1051a39Sopenharmony_ci
194e1051a39Sopenharmony_ci=item "kdf-ukm" (B<OSSL_EXCHANGE_PARAM_KDF_UKM>) <octet string>
195e1051a39Sopenharmony_ci
196e1051a39Sopenharmony_ciSets the User Key Material to be used as part of the selected Key Derivation
197e1051a39Sopenharmony_ciFunction associated with the given key exchange ctx.
198e1051a39Sopenharmony_ci
199e1051a39Sopenharmony_ci=item "kdf-ukm" (B<OSSL_EXCHANGE_PARAM_KDF_UKM>) <octet string ptr>
200e1051a39Sopenharmony_ci
201e1051a39Sopenharmony_ciGets a pointer to the User Key Material to be used as part of the selected
202e1051a39Sopenharmony_ciKey Derivation Function associated with the given key exchange ctx. Providers
203e1051a39Sopenharmony_ciusually do not need to support this gettable parameter as its sole purpose
204e1051a39Sopenharmony_ciis to support functionality of the deprecated EVP_PKEY_CTX_get0_ecdh_kdf_ukm()
205e1051a39Sopenharmony_ciand EVP_PKEY_CTX_get0_dh_kdf_ukm() functions.
206e1051a39Sopenharmony_ci
207e1051a39Sopenharmony_ci=back
208e1051a39Sopenharmony_ci
209e1051a39Sopenharmony_ci=head1 RETURN VALUES
210e1051a39Sopenharmony_ci
211e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_newctx() and OSSL_FUNC_keyexch_dupctx() should return the newly created
212e1051a39Sopenharmony_ciprovider side key exchange context, or NULL on failure.
213e1051a39Sopenharmony_ci
214e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_init(), OSSL_FUNC_keyexch_set_peer(), OSSL_FUNC_keyexch_derive(),
215e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_set_params(), and OSSL_FUNC_keyexch_get_params() should return 1 for success
216e1051a39Sopenharmony_cior 0 on error.
217e1051a39Sopenharmony_ci
218e1051a39Sopenharmony_ciOSSL_FUNC_keyexch_settable_ctx_params() and OSSL_FUNC_keyexch_gettable_ctx_params() should
219e1051a39Sopenharmony_cialways return a constant L<OSSL_PARAM(3)> array.
220e1051a39Sopenharmony_ci
221e1051a39Sopenharmony_ci=head1 SEE ALSO
222e1051a39Sopenharmony_ci
223e1051a39Sopenharmony_ciL<provider(7)>
224e1051a39Sopenharmony_ci
225e1051a39Sopenharmony_ci=head1 HISTORY
226e1051a39Sopenharmony_ci
227e1051a39Sopenharmony_ciThe provider KEYEXCH interface was introduced in OpenSSL 3.0.
228e1051a39Sopenharmony_ci
229e1051a39Sopenharmony_ci=head1 COPYRIGHT
230e1051a39Sopenharmony_ci
231e1051a39Sopenharmony_ciCopyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
232e1051a39Sopenharmony_ci
233e1051a39Sopenharmony_ciLicensed under the Apache License 2.0 (the "License").  You may not use
234e1051a39Sopenharmony_cithis file except in compliance with the License.  You can obtain a copy
235e1051a39Sopenharmony_ciin the file LICENSE in the source distribution or at
236e1051a39Sopenharmony_ciL<https://www.openssl.org/source/license.html>.
237e1051a39Sopenharmony_ci
238e1051a39Sopenharmony_ci=cut
239