xref: /freebsd/crypto/openssl/doc/man7/provider-cipher.pod (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1b077aed3SPierre Pronchery=pod
2b077aed3SPierre Pronchery
3b077aed3SPierre Pronchery=head1 NAME
4b077aed3SPierre Pronchery
5b077aed3SPierre Proncheryprovider-cipher - The cipher library E<lt>-E<gt> provider functions
6b077aed3SPierre Pronchery
7b077aed3SPierre Pronchery=head1 SYNOPSIS
8b077aed3SPierre Pronchery
9b077aed3SPierre Pronchery=for openssl multiple includes
10b077aed3SPierre Pronchery
11b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
12b077aed3SPierre Pronchery #include <openssl/core_names.h>
13b077aed3SPierre Pronchery
14b077aed3SPierre Pronchery /*
15b077aed3SPierre Pronchery  * None of these are actual functions, but are displayed like this for
16b077aed3SPierre Pronchery  * the function signatures for functions that are offered as function
17b077aed3SPierre Pronchery  * pointers in OSSL_DISPATCH arrays.
18b077aed3SPierre Pronchery  */
19b077aed3SPierre Pronchery
20b077aed3SPierre Pronchery /* Context management */
21b077aed3SPierre Pronchery void *OSSL_FUNC_cipher_newctx(void *provctx);
22b077aed3SPierre Pronchery void OSSL_FUNC_cipher_freectx(void *cctx);
23b077aed3SPierre Pronchery void *OSSL_FUNC_cipher_dupctx(void *cctx);
24b077aed3SPierre Pronchery
25b077aed3SPierre Pronchery /* Encryption/decryption */
26b077aed3SPierre Pronchery int OSSL_FUNC_cipher_encrypt_init(void *cctx, const unsigned char *key,
27b077aed3SPierre Pronchery                                   size_t keylen, const unsigned char *iv,
28b077aed3SPierre Pronchery                                   size_t ivlen, const OSSL_PARAM params[]);
29b077aed3SPierre Pronchery int OSSL_FUNC_cipher_decrypt_init(void *cctx, const unsigned char *key,
30b077aed3SPierre Pronchery                                   size_t keylen, const unsigned char *iv,
31b077aed3SPierre Pronchery                                   size_t ivlen, const OSSL_PARAM params[]);
32b077aed3SPierre Pronchery int OSSL_FUNC_cipher_update(void *cctx, unsigned char *out, size_t *outl,
33b077aed3SPierre Pronchery                             size_t outsize, const unsigned char *in, size_t inl);
34b077aed3SPierre Pronchery int OSSL_FUNC_cipher_final(void *cctx, unsigned char *out, size_t *outl,
35b077aed3SPierre Pronchery                            size_t outsize);
36b077aed3SPierre Pronchery int OSSL_FUNC_cipher_cipher(void *cctx, unsigned char *out, size_t *outl,
37b077aed3SPierre Pronchery                             size_t outsize, const unsigned char *in, size_t inl);
38b077aed3SPierre Pronchery
39b077aed3SPierre Pronchery /* Cipher parameter descriptors */
40b077aed3SPierre Pronchery const OSSL_PARAM *OSSL_FUNC_cipher_gettable_params(void *provctx);
41b077aed3SPierre Pronchery
42b077aed3SPierre Pronchery /* Cipher operation parameter descriptors */
43b077aed3SPierre Pronchery const OSSL_PARAM *OSSL_FUNC_cipher_gettable_ctx_params(void *cctx,
44b077aed3SPierre Pronchery                                                        void *provctx);
45b077aed3SPierre Pronchery const OSSL_PARAM *OSSL_FUNC_cipher_settable_ctx_params(void *cctx,
46b077aed3SPierre Pronchery                                                        void *provctx);
47b077aed3SPierre Pronchery
48b077aed3SPierre Pronchery /* Cipher parameters */
49b077aed3SPierre Pronchery int OSSL_FUNC_cipher_get_params(OSSL_PARAM params[]);
50b077aed3SPierre Pronchery
51b077aed3SPierre Pronchery /* Cipher operation parameters */
52b077aed3SPierre Pronchery int OSSL_FUNC_cipher_get_ctx_params(void *cctx, OSSL_PARAM params[]);
53b077aed3SPierre Pronchery int OSSL_FUNC_cipher_set_ctx_params(void *cctx, const OSSL_PARAM params[]);
54b077aed3SPierre Pronchery
55b077aed3SPierre Pronchery=head1 DESCRIPTION
56b077aed3SPierre Pronchery
57b077aed3SPierre ProncheryThis documentation is primarily aimed at provider authors. See L<provider(7)>
58b077aed3SPierre Proncheryfor further information.
59b077aed3SPierre Pronchery
60b077aed3SPierre ProncheryThe CIPHER operation enables providers to implement cipher algorithms and make
61b077aed3SPierre Proncherythem available to applications via the API functions L<EVP_EncryptInit_ex(3)>,
62b077aed3SPierre ProncheryL<EVP_EncryptUpdate(3)> and L<EVP_EncryptFinal(3)> (as well as the decrypt
63b077aed3SPierre Proncheryequivalents and other related functions).
64b077aed3SPierre Pronchery
65b077aed3SPierre ProncheryAll "functions" mentioned here are passed as function pointers between
66b077aed3SPierre ProncheryF<libcrypto> and the provider in L<OSSL_DISPATCH(3)> arrays via
67b077aed3SPierre ProncheryL<OSSL_ALGORITHM(3)> arrays that are returned by the provider's
68b077aed3SPierre Proncheryprovider_query_operation() function
69b077aed3SPierre Pronchery(see L<provider-base(7)/Provider Functions>).
70b077aed3SPierre Pronchery
71b077aed3SPierre ProncheryAll these "functions" have a corresponding function type definition
72b077aed3SPierre Proncherynamed B<OSSL_FUNC_{name}_fn>, and a helper function to retrieve the
73b077aed3SPierre Proncheryfunction pointer from an L<OSSL_DISPATCH(3)> element named
74b077aed3SPierre ProncheryB<OSSL_FUNC_{name}>.
75b077aed3SPierre ProncheryFor example, the "function" OSSL_FUNC_cipher_newctx() has these:
76b077aed3SPierre Pronchery
77b077aed3SPierre Pronchery typedef void *(OSSL_FUNC_cipher_newctx_fn)(void *provctx);
78b077aed3SPierre Pronchery static ossl_inline OSSL_FUNC_cipher_newctx_fn
79b077aed3SPierre Pronchery     OSSL_FUNC_cipher_newctx(const OSSL_DISPATCH *opf);
80b077aed3SPierre Pronchery
81b077aed3SPierre ProncheryL<OSSL_DISPATCH(3)> arrays are indexed by numbers that are provided as
82b077aed3SPierre Proncherymacros in L<openssl-core_dispatch.h(7)>, as follows:
83b077aed3SPierre Pronchery
84b077aed3SPierre Pronchery OSSL_FUNC_cipher_newctx               OSSL_FUNC_CIPHER_NEWCTX
85b077aed3SPierre Pronchery OSSL_FUNC_cipher_freectx              OSSL_FUNC_CIPHER_FREECTX
86b077aed3SPierre Pronchery OSSL_FUNC_cipher_dupctx               OSSL_FUNC_CIPHER_DUPCTX
87b077aed3SPierre Pronchery
88b077aed3SPierre Pronchery OSSL_FUNC_cipher_encrypt_init         OSSL_FUNC_CIPHER_ENCRYPT_INIT
89b077aed3SPierre Pronchery OSSL_FUNC_cipher_decrypt_init         OSSL_FUNC_CIPHER_DECRYPT_INIT
90b077aed3SPierre Pronchery OSSL_FUNC_cipher_update               OSSL_FUNC_CIPHER_UPDATE
91b077aed3SPierre Pronchery OSSL_FUNC_cipher_final                OSSL_FUNC_CIPHER_FINAL
92b077aed3SPierre Pronchery OSSL_FUNC_cipher_cipher               OSSL_FUNC_CIPHER_CIPHER
93b077aed3SPierre Pronchery
94b077aed3SPierre Pronchery OSSL_FUNC_cipher_get_params           OSSL_FUNC_CIPHER_GET_PARAMS
95b077aed3SPierre Pronchery OSSL_FUNC_cipher_get_ctx_params       OSSL_FUNC_CIPHER_GET_CTX_PARAMS
96b077aed3SPierre Pronchery OSSL_FUNC_cipher_set_ctx_params       OSSL_FUNC_CIPHER_SET_CTX_PARAMS
97b077aed3SPierre Pronchery
98b077aed3SPierre Pronchery OSSL_FUNC_cipher_gettable_params      OSSL_FUNC_CIPHER_GETTABLE_PARAMS
99b077aed3SPierre Pronchery OSSL_FUNC_cipher_gettable_ctx_params  OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS
100b077aed3SPierre Pronchery OSSL_FUNC_cipher_settable_ctx_params  OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS
101b077aed3SPierre Pronchery
102b077aed3SPierre ProncheryA cipher algorithm implementation may not implement all of these functions.
103b077aed3SPierre ProncheryIn order to be a consistent set of functions there must at least be a complete
104b077aed3SPierre Proncheryset of "encrypt" functions, or a complete set of "decrypt" functions, or a
105b077aed3SPierre Proncherysingle "cipher" function.
106b077aed3SPierre ProncheryIn all cases both the OSSL_FUNC_cipher_newctx and OSSL_FUNC_cipher_freectx functions must be
107b077aed3SPierre Proncherypresent.
108b077aed3SPierre ProncheryAll other functions are optional.
109b077aed3SPierre Pronchery
110b077aed3SPierre Pronchery=head2 Context Management Functions
111b077aed3SPierre Pronchery
112b077aed3SPierre ProncheryOSSL_FUNC_cipher_newctx() should create and return a pointer to a provider side
113b077aed3SPierre Proncherystructure for holding context information during a cipher operation.
114b077aed3SPierre ProncheryA pointer to this context will be passed back in a number of the other cipher
115b077aed3SPierre Proncheryoperation function calls.
116b077aed3SPierre ProncheryThe parameter I<provctx> is the provider context generated during provider
117b077aed3SPierre Proncheryinitialisation (see L<provider(7)>).
118b077aed3SPierre Pronchery
119b077aed3SPierre ProncheryOSSL_FUNC_cipher_freectx() is passed a pointer to the provider side cipher context in
120b077aed3SPierre Proncherythe I<cctx> parameter.
121b077aed3SPierre ProncheryThis function should free any resources associated with that context.
122b077aed3SPierre Pronchery
123b077aed3SPierre ProncheryOSSL_FUNC_cipher_dupctx() should duplicate the provider side cipher context in the
124b077aed3SPierre ProncheryI<cctx> parameter and return the duplicate copy.
125b077aed3SPierre Pronchery
126b077aed3SPierre Pronchery=head2 Encryption/Decryption Functions
127b077aed3SPierre Pronchery
128b077aed3SPierre ProncheryOSSL_FUNC_cipher_encrypt_init() initialises a cipher operation for encryption given a
129b077aed3SPierre Proncherynewly created provider side cipher context in the I<cctx> parameter.
130b077aed3SPierre ProncheryThe key to be used is given in I<key> which is I<keylen> bytes long.
131b077aed3SPierre ProncheryThe IV to be used is given in I<iv> which is I<ivlen> bytes long.
132b077aed3SPierre ProncheryThe I<params>, if not NULL, should be set on the context in a manner similar to
133b077aed3SPierre Proncheryusing OSSL_FUNC_cipher_set_ctx_params().
134b077aed3SPierre Pronchery
135b077aed3SPierre ProncheryOSSL_FUNC_cipher_decrypt_init() is the same as OSSL_FUNC_cipher_encrypt_init() except that it
136b077aed3SPierre Proncheryinitialises the context for a decryption operation.
137b077aed3SPierre Pronchery
138b077aed3SPierre ProncheryOSSL_FUNC_cipher_update() is called to supply data to be encrypted/decrypted as part of
139b077aed3SPierre Proncherya previously initialised cipher operation.
140b077aed3SPierre ProncheryThe I<cctx> parameter contains a pointer to a previously initialised provider
141b077aed3SPierre Proncheryside context.
142b077aed3SPierre ProncheryOSSL_FUNC_cipher_update() should encrypt/decrypt I<inl> bytes of data at the location
143b077aed3SPierre Proncherypointed to by I<in>.
144b077aed3SPierre ProncheryThe encrypted data should be stored in I<out> and the amount of data written to
145b077aed3SPierre ProncheryI<*outl> which should not exceed I<outsize> bytes.
146b077aed3SPierre ProncheryOSSL_FUNC_cipher_update() may be called multiple times for a single cipher operation.
147b077aed3SPierre ProncheryIt is the responsibility of the cipher implementation to handle input lengths
148b077aed3SPierre Proncherythat are not multiples of the block length.
149b077aed3SPierre ProncheryIn such cases a cipher implementation will typically cache partial blocks of
150b077aed3SPierre Proncheryinput data until a complete block is obtained.
151*e0c4386eSCy SchubertThe pointers I<out> and I<in> may point to the same location, in which
152*e0c4386eSCy Schubertcase the encryption must be done in-place. If I<out> and I<in> point to different
153*e0c4386eSCy Schubertlocations, the requirements of L<EVP_EncryptUpdate(3)> and L<EVP_DecryptUpdate(3)>
154*e0c4386eSCy Schubertguarantee that the two buffers are disjoint.
155*e0c4386eSCy SchubertSimilarly, the requirements of L<EVP_EncryptUpdate(3)> and L<EVP_DecryptUpdate(3)>
156*e0c4386eSCy Schubertensure that the buffer pointed to by I<out> contains sufficient room for the
157*e0c4386eSCy Schubertoperation being performed.
158b077aed3SPierre Pronchery
159b077aed3SPierre ProncheryOSSL_FUNC_cipher_final() completes an encryption or decryption started through previous
160b077aed3SPierre ProncheryOSSL_FUNC_cipher_encrypt_init() or OSSL_FUNC_cipher_decrypt_init(), and OSSL_FUNC_cipher_update()
161b077aed3SPierre Proncherycalls.
162b077aed3SPierre ProncheryThe I<cctx> parameter contains a pointer to the provider side context.
163b077aed3SPierre ProncheryAny final encryption/decryption output should be written to I<out> and the
164b077aed3SPierre Proncheryamount of data written to I<*outl> which should not exceed I<outsize> bytes.
165b077aed3SPierre ProncheryThe same expectations apply to I<outsize> as documented for
166b077aed3SPierre ProncheryL<EVP_EncryptFinal(3)> and L<EVP_DecryptFinal(3)>.
167b077aed3SPierre Pronchery
168b077aed3SPierre ProncheryOSSL_FUNC_cipher_cipher() performs encryption/decryption using the provider side cipher
169b077aed3SPierre Proncherycontext in the I<cctx> parameter that should have been previously initialised via
170b077aed3SPierre Proncherya call to OSSL_FUNC_cipher_encrypt_init() or OSSL_FUNC_cipher_decrypt_init().
171b077aed3SPierre ProncheryThis should call the raw underlying cipher function without any padding.
172b077aed3SPierre ProncheryThis will be invoked in the provider as a result of the application calling
173b077aed3SPierre ProncheryL<EVP_Cipher(3)>.
174b077aed3SPierre ProncheryThe application is responsible for ensuring that the input is a multiple of the
175b077aed3SPierre Proncheryblock length.
176b077aed3SPierre ProncheryThe data to be encrypted/decrypted will be in I<in>, and it will be I<inl> bytes
177b077aed3SPierre Proncheryin length.
178b077aed3SPierre ProncheryThe output from the encryption/decryption should be stored in I<out> and the
179b077aed3SPierre Proncheryamount of data stored should be put in I<*outl> which should be no more than
180b077aed3SPierre ProncheryI<outsize> bytes.
181b077aed3SPierre Pronchery
182b077aed3SPierre Pronchery=head2 Cipher Parameters
183b077aed3SPierre Pronchery
184b077aed3SPierre ProncherySee L<OSSL_PARAM(3)> for further details on the parameters structure used by
185b077aed3SPierre Proncherythese functions.
186b077aed3SPierre Pronchery
187b077aed3SPierre ProncheryOSSL_FUNC_cipher_get_params() gets details of the algorithm implementation
188b077aed3SPierre Proncheryand stores them in I<params>.
189b077aed3SPierre Pronchery
190b077aed3SPierre ProncheryOSSL_FUNC_cipher_set_ctx_params() sets cipher operation parameters for the
191b077aed3SPierre Proncheryprovider side cipher context I<cctx> to I<params>.
192b077aed3SPierre ProncheryAny parameter settings are additional to any that were previously set.
193b077aed3SPierre ProncheryPassing NULL for I<params> should return true.
194b077aed3SPierre Pronchery
195b077aed3SPierre ProncheryOSSL_FUNC_cipher_get_ctx_params() gets cipher operation details details from
196b077aed3SPierre Proncherythe given provider side cipher context I<cctx> and stores them in I<params>.
197b077aed3SPierre ProncheryPassing NULL for I<params> should return true.
198b077aed3SPierre Pronchery
199b077aed3SPierre ProncheryOSSL_FUNC_cipher_gettable_params(), OSSL_FUNC_cipher_gettable_ctx_params(),
200b077aed3SPierre Proncheryand OSSL_FUNC_cipher_settable_ctx_params() all return constant L<OSSL_PARAM(3)>
201b077aed3SPierre Proncheryarrays as descriptors of the parameters that OSSL_FUNC_cipher_get_params(),
202b077aed3SPierre ProncheryOSSL_FUNC_cipher_get_ctx_params(), and OSSL_FUNC_cipher_set_ctx_params()
203b077aed3SPierre Proncherycan handle, respectively.  OSSL_FUNC_cipher_gettable_ctx_params() and
204b077aed3SPierre ProncheryOSSL_FUNC_cipher_settable_ctx_params() will return the parameters associated
205b077aed3SPierre Proncherywith the provider side context I<cctx> in its current state if it is
206b077aed3SPierre Proncherynot NULL.  Otherwise, they return the parameters associated with the
207b077aed3SPierre Proncheryprovider side algorithm I<provctx>.
208b077aed3SPierre Pronchery
209b077aed3SPierre ProncheryParameters currently recognised by built-in ciphers are listed in
210b077aed3SPierre ProncheryL<EVP_EncryptInit(3)/PARAMETERS>.
211b077aed3SPierre ProncheryNot all parameters are relevant to, or are understood by all ciphers.
212b077aed3SPierre Pronchery
213b077aed3SPierre Pronchery=head1 RETURN VALUES
214b077aed3SPierre Pronchery
215b077aed3SPierre ProncheryOSSL_FUNC_cipher_newctx() and OSSL_FUNC_cipher_dupctx() should return the newly created
216b077aed3SPierre Proncheryprovider side cipher context, or NULL on failure.
217b077aed3SPierre Pronchery
218b077aed3SPierre ProncheryOSSL_FUNC_cipher_encrypt_init(), OSSL_FUNC_cipher_decrypt_init(), OSSL_FUNC_cipher_update(),
219b077aed3SPierre ProncheryOSSL_FUNC_cipher_final(), OSSL_FUNC_cipher_cipher(), OSSL_FUNC_cipher_get_params(),
220b077aed3SPierre ProncheryOSSL_FUNC_cipher_get_ctx_params() and OSSL_FUNC_cipher_set_ctx_params() should return 1 for
221b077aed3SPierre Proncherysuccess or 0 on error.
222b077aed3SPierre Pronchery
223b077aed3SPierre ProncheryOSSL_FUNC_cipher_gettable_params(), OSSL_FUNC_cipher_gettable_ctx_params() and
224b077aed3SPierre ProncheryOSSL_FUNC_cipher_settable_ctx_params() should return a constant L<OSSL_PARAM(3)>
225b077aed3SPierre Proncheryarray, or NULL if none is offered.
226b077aed3SPierre Pronchery
227b077aed3SPierre Pronchery=head1 SEE ALSO
228b077aed3SPierre Pronchery
229b077aed3SPierre ProncheryL<provider(7)>, L<OSSL_PROVIDER-FIPS(7)>, L<OSSL_PROVIDER-default(7)>,
230b077aed3SPierre ProncheryL<OSSL_PROVIDER-legacy(7)>,
231b077aed3SPierre ProncheryL<EVP_CIPHER-AES(7)>, L<EVP_CIPHER-ARIA(7)>, L<EVP_CIPHER-BLOWFISH(7)>,
232b077aed3SPierre ProncheryL<EVP_CIPHER-CAMELLIA(7)>, L<EVP_CIPHER-CAST(7)>, L<EVP_CIPHER-CHACHA(7)>,
233b077aed3SPierre ProncheryL<EVP_CIPHER-DES(7)>, L<EVP_CIPHER-IDEA(7)>, L<EVP_CIPHER-RC2(7)>,
234b077aed3SPierre ProncheryL<EVP_CIPHER-RC4(7)>, L<EVP_CIPHER-RC5(7)>, L<EVP_CIPHER-SEED(7)>,
235b077aed3SPierre ProncheryL<EVP_CIPHER-SM4(7)>, L<EVP_CIPHER-NULL(7)>,
236b077aed3SPierre ProncheryL<life_cycle-cipher(7)>, L<EVP_EncryptInit(3)>
237b077aed3SPierre Pronchery
238b077aed3SPierre Pronchery=head1 HISTORY
239b077aed3SPierre Pronchery
240b077aed3SPierre ProncheryThe provider CIPHER interface was introduced in OpenSSL 3.0.
241b077aed3SPierre Pronchery
242b077aed3SPierre Pronchery=head1 COPYRIGHT
243b077aed3SPierre Pronchery
244b077aed3SPierre ProncheryCopyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
245b077aed3SPierre Pronchery
246b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
247b077aed3SPierre Proncherythis file except in compliance with the License.  You can obtain a copy
248b077aed3SPierre Proncheryin the file LICENSE in the source distribution or at
249b077aed3SPierre ProncheryL<https://www.openssl.org/source/license.html>.
250b077aed3SPierre Pronchery
251b077aed3SPierre Pronchery=cut
252