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