xref: /openssl/doc/man7/provider-kem.pod (revision 7ed6de99)
1=pod
2
3=head1 NAME
4
5provider-kem - The kem library E<lt>-E<gt> provider functions
6
7=head1 SYNOPSIS
8
9=for openssl multiple includes
10
11 #include <openssl/core_dispatch.h>
12 #include <openssl/core_names.h>
13
14 /*
15  * None of these are actual functions, but are displayed like this for
16  * the function signatures for functions that are offered as function
17  * pointers in OSSL_DISPATCH arrays.
18  */
19
20 /* Context management */
21 void *OSSL_FUNC_kem_newctx(void *provctx);
22 void OSSL_FUNC_kem_freectx(void *ctx);
23 void *OSSL_FUNC_kem_dupctx(void *ctx);
24
25 /* Encapsulation */
26 int OSSL_FUNC_kem_encapsulate_init(void *ctx, void *provkey,
27                                    const OSSL_PARAM params[]);
28 int OSSL_FUNC_kem_auth_encapsulate_init(void *ctx, void *provkey,
29                                         void *provauthkey,
30                                         const OSSL_PARAM params[]);
31 int OSSL_FUNC_kem_encapsulate(void *ctx, unsigned char *out, size_t *outlen,
32                               unsigned char *secret, size_t *secretlen);
33
34 /* Decapsulation */
35 int OSSL_FUNC_kem_decapsulate_init(void *ctx, void *provkey);
36 int OSSL_FUNC_kem_auth_decapsulate_init(void *ctx, void *provkey,
37                                         void *provauthkey,
38                                         const OSSL_PARAM params[]);
39 int OSSL_FUNC_kem_decapsulate(void *ctx, unsigned char *out, size_t *outlen,
40                               const unsigned char *in, size_t inlen);
41
42 /* KEM parameters */
43 int OSSL_FUNC_kem_get_ctx_params(void *ctx, OSSL_PARAM params[]);
44 const OSSL_PARAM *OSSL_FUNC_kem_gettable_ctx_params(void *ctx, void *provctx);
45 int OSSL_FUNC_kem_set_ctx_params(void *ctx, const OSSL_PARAM params[]);
46 const OSSL_PARAM *OSSL_FUNC_kem_settable_ctx_params(void *ctx, void *provctx);
47
48=head1 DESCRIPTION
49
50This documentation is primarily aimed at provider authors. See L<provider(7)>
51for further information.
52
53The asymmetric kem (OSSL_OP_KEM) operation enables providers to
54implement asymmetric kem algorithms and make them available to applications
55via the API functions L<EVP_PKEY_encapsulate(3)>,
56L<EVP_PKEY_decapsulate(3)> and other related functions.
57
58All "functions" mentioned here are passed as function pointers between
59F<libcrypto> and the provider in L<OSSL_DISPATCH(3)> arrays via
60L<OSSL_ALGORITHM(3)> arrays that are returned by the provider's
61provider_query_operation() function
62(see L<provider-base(7)/Provider Functions>).
63
64All these "functions" have a corresponding function type definition
65named B<OSSL_FUNC_{name}_fn>, and a helper function to retrieve the
66function pointer from an L<OSSL_DISPATCH(3)> element named
67B<OSSL_FUNC_{name}>.
68For example, the "function" OSSL_FUNC_kem_newctx() has these:
69
70 typedef void *(OSSL_FUNC_kem_newctx_fn)(void *provctx);
71 static ossl_inline OSSL_FUNC_kem_newctx_fn
72     OSSL_FUNC_kem_newctx(const OSSL_DISPATCH *opf);
73
74L<OSSL_DISPATCH(3)> arrays are indexed by numbers that are provided as
75macros in L<openssl-core_dispatch.h(7)>, as follows:
76
77 OSSL_FUNC_kem_newctx                OSSL_FUNC_KEM_NEWCTX
78 OSSL_FUNC_kem_freectx               OSSL_FUNC_KEM_FREECTX
79 OSSL_FUNC_kem_dupctx                OSSL_FUNC_KEM_DUPCTX
80
81 OSSL_FUNC_kem_encapsulate_init      OSSL_FUNC_KEM_ENCAPSULATE_INIT
82 OSSL_FUNC_kem_auth_encapsulate_init OSSL_FUNC_KEM_AUTH_ENCAPSULATE_INIT
83 OSSL_FUNC_kem_encapsulate           OSSL_FUNC_KEM_ENCAPSULATE
84
85 OSSL_FUNC_kem_decapsulate_init      OSSL_FUNC_KEM_DECAPSULATE_INIT
86 OSSL_FUNC_kem_auth_decapsulate_init OSSL_FUNC_KEM_AUTH_DECAPSULATE_INIT
87 OSSL_FUNC_kem_decapsulate           OSSL_FUNC_KEM_DECAPSULATE
88
89 OSSL_FUNC_kem_get_ctx_params        OSSL_FUNC_KEM_GET_CTX_PARAMS
90 OSSL_FUNC_kem_gettable_ctx_params   OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS
91 OSSL_FUNC_kem_set_ctx_params        OSSL_FUNC_KEM_SET_CTX_PARAMS
92 OSSL_FUNC_kem_settable_ctx_params   OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS
93
94An asymmetric kem algorithm implementation may not implement all of these
95functions.
96In order to be a consistent set of functions a provider must implement
97OSSL_FUNC_kem_newctx and OSSL_FUNC_kem_freectx.
98It must also implement both of OSSL_FUNC_kem_encapsulate_init and
99OSSL_FUNC_kem_encapsulate, or both of OSSL_FUNC_kem_decapsulate_init and
100OSSL_FUNC_kem_decapsulate.
101OSSL_FUNC_kem_auth_encapsulate_init is optional but if it is present then so
102must OSSL_FUNC_kem_auth_decapsulate_init.
103OSSL_FUNC_kem_get_ctx_params is optional but if it is present then so must
104OSSL_FUNC_kem_gettable_ctx_params.
105Similarly, OSSL_FUNC_kem_set_ctx_params is optional but if it is present then
106OSSL_FUNC_kem_settable_ctx_params must also be present.
107
108An asymmetric kem algorithm must also implement some mechanism for generating,
109loading or importing keys via the key management (OSSL_OP_KEYMGMT) operation.
110See L<provider-keymgmt(7)> for further details.
111
112=head2 Context Management Functions
113
114OSSL_FUNC_kem_newctx() should create and return a pointer to a provider side
115structure for holding context information during an asymmetric kem operation.
116A pointer to this context will be passed back in a number of the other
117asymmetric kem operation function calls.
118The parameter I<provctx> is the provider context generated during provider
119initialisation (see L<provider(7)>).
120
121OSSL_FUNC_kem_freectx() is passed a pointer to the provider side asymmetric
122kem context in the I<ctx> parameter.
123This function should free any resources associated with that context.
124
125OSSL_FUNC_kem_dupctx() should duplicate the provider side asymmetric kem
126context in the I<ctx> parameter and return the duplicate copy.
127
128=head2 Asymmetric Key Encapsulation Functions
129
130OSSL_FUNC_kem_encapsulate_init() initialises a context for an asymmetric
131encapsulation given a provider side asymmetric kem context in the I<ctx>
132parameter, a pointer to a provider key object in the I<provkey> parameter and
133the I<name> of the algorithm.
134The I<params>, if not NULL, should be set on the context in a manner similar to
135using OSSL_FUNC_kem_set_ctx_params().
136The key object should have been previously generated, loaded or imported into
137the provider using the key management (OSSL_OP_KEYMGMT) operation (see
138provider-keymgmt(7)>.
139
140OSSL_FUNC_kem_auth_encapsulate_init() is similar to
141OSSL_FUNC_kem_encapsulate_init(), but also passes an additional authentication
142key I<provauthkey> which cannot be NULL.
143
144OSSL_FUNC_kem_encapsulate() performs the actual encapsulation itself.
145A previously initialised asymmetric kem context is passed in the I<ctx>
146parameter.
147Unless I<out> is NULL, the data to be encapsulated is internally generated,
148and returned into the buffer pointed to by the I<secret> parameter and the
149encapsulated data should also be written to the location pointed to by the
150I<out> parameter. The length of the encapsulated data should be written to
151I<*outlen> and the length of the generated secret should be written to
152I<*secretlen>.
153
154If I<out> is NULL then the maximum length of the encapsulated data should be
155written to I<*outlen>, and the maximum length of the generated secret should be
156written to I<*secretlen>.
157
158=head2 Decapsulation Functions
159
160OSSL_FUNC_kem_decapsulate_init() initialises a context for an asymmetric
161decapsulation given a provider side asymmetric kem context in the I<ctx>
162parameter, a pointer to a provider key object in the I<provkey> parameter, and
163a I<name> of the algorithm.
164The key object should have been previously generated, loaded or imported into
165the provider using the key management (OSSL_OP_KEYMGMT) operation (see
166provider-keymgmt(7)>.
167
168OSSL_FUNC_kem_auth_decapsulate_init() is similar to
169OSSL_FUNC_kem_decapsulate_init(), but also passes an additional authentication
170key I<provauthkey> which cannot be NULL.
171
172OSSL_FUNC_kem_decapsulate() performs the actual decapsulation itself.
173A previously initialised asymmetric kem context is passed in the I<ctx>
174parameter.
175The data to be decapsulated is pointed to by the I<in> parameter which is I<inlen>
176bytes long.
177Unless I<out> is NULL, the decapsulated data should be written to the location
178pointed to by the I<out> parameter.
179The length of the decapsulated data should be written to I<*outlen>.
180If I<out> is NULL then the maximum length of the decapsulated data should be
181written to I<*outlen>.
182
183=head2 Asymmetric Key Encapsulation Parameters
184
185See L<OSSL_PARAM(3)> for further details on the parameters structure used by
186the OSSL_FUNC_kem_get_ctx_params() and OSSL_FUNC_kem_set_ctx_params()
187functions.
188
189The OpenSSL FIPS provider also supports the following parameters:
190
191=over 4
192
193=item "fips-indicator" (B<OSSL_KEM_PARAM_FIPS_APPROVED_INDICATOR>) <integer>
194
195A getter that returns 1 if the operation is FIPS approved, or 0 otherwise.
196This may be used after calling either OSSL_FUNC_kem_encapsulate() or
197OSSL_FUNC_kem_decapsulate(). It may return 0 if the "key-check" is set to 0.
198
199=item "key-check" (B<OSSL_KEM_PARAM_FIPS_KEY_CHECK>) <integer>
200
201If required this parameter should be set using OSSL_FUNC_kem_encapsulate_init()
202or OSSL_FUNC_kem_decapsulate_init().
203The default value of 1 causes an error during the init if the key is not FIPS
204approved (e.g. The key has a security strength of less than 112 bits). Setting
205this to 0 will ignore the error and set the approved "fips-indicator" to 0.
206This option breaks FIPS compliance if it causes the approved "fips-indicator"
207to return 0.
208
209=back
210
211=head2 Asymmetric Key Encapsulation Parameter Functions
212
213OSSL_FUNC_kem_get_ctx_params() gets asymmetric KEM parameters associated
214with the given provider side asymmetric kem context I<ctx> and stores them in
215I<params>.
216Passing NULL for I<params> should return true.
217
218OSSL_FUNC_kem_set_ctx_params() sets the asymmetric KEM parameters associated
219with the given provider side asymmetric kem context I<ctx> to I<params>.
220Any parameter settings are additional to any that were previously set.
221Passing NULL for I<params> should return true.
222
223No parameters are currently recognised by built-in asymmetric kem algorithms.
224
225OSSL_FUNC_kem_gettable_ctx_params() and OSSL_FUNC_kem_settable_ctx_params()
226get a constant L<OSSL_PARAM(3)> array that describes the gettable and settable
227parameters, i.e. parameters that can be used with OSSL_FUNC_kem_get_ctx_params()
228and OSSL_FUNC_kem_set_ctx_params() respectively.
229
230=head1 RETURN VALUES
231
232OSSL_FUNC_kem_newctx() and OSSL_FUNC_kem_dupctx() should return the newly
233created provider side asymmetric kem context, or NULL on failure.
234
235All other functions should return 1 for success or 0 on error.
236
237=head1 SEE ALSO
238
239L<provider(7)>
240
241=head1 HISTORY
242
243The provider KEM interface was introduced in OpenSSL 3.0.
244
245OSSL_FUNC_kem_auth_encapsulate_init() and OSSL_FUNC_kem_auth_decapsulate_init()
246were added in OpenSSL 3.2.
247
248The Asymmetric Key Encapsulation Parameters "fips-indicator" and "key-check"
249were added in OpenSSL 3.4.
250
251=head1 COPYRIGHT
252
253Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
254
255Licensed under the Apache License 2.0 (the "License").  You may not use
256this file except in compliance with the License.  You can obtain a copy
257in the file LICENSE in the source distribution or at
258L<https://www.openssl.org/source/license.html>.
259
260=cut
261