Reformat param description lines
[openssl.git] / doc / man7 / provider-cipher.pod
1 =pod
2
3 =head1 NAME
4
5 provider-cipher - The cipher library E<lt>-E<gt> provider functions
6
7 =head1 SYNOPSIS
8
9 =for comment multiple includes
10
11  #include <openssl/core_numbers.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 *OP_cipher_newctx(void *provctx);
22  void OP_cipher_freectx(void *cctx);
23  void *OP_cipher_dupctx(void *cctx);
24
25  /* Encryption/decryption */
26  int OP_cipher_encrypt_init(void *cctx, const unsigned char *key,
27                             size_t keylen, const unsigned char *iv,
28                             size_t ivlen);
29  int OP_cipher_decrypt_init(void *cctx, const unsigned char *key,
30                             size_t keylen, const unsigned char *iv,
31                             size_t ivlen);
32  int OP_cipher_update(void *cctx, unsigned char *out, size_t *outl,
33                       size_t outsize, const unsigned char *in, size_t inl);
34  int OP_cipher_final(void *cctx, unsigned char *out, size_t *outl,
35                      size_t outsize);
36  int OP_cipher_cipher(void *cctx, unsigned char *out, size_t *outl,
37                       size_t outsize, const unsigned char *in, size_t inl);
38
39  /* Cipher parameter descriptors */
40  const OSSL_PARAM *OP_cipher_gettable_params(void);
41
42  /* Cipher operation parameter descriptors */
43  const OSSL_PARAM *OP_cipher_gettable_ctx_params(void);
44  const OSSL_PARAM *OP_cipher_settable_ctx_params(void);
45
46  /* Cipher parameters */
47  int OP_cipher_get_params(OSSL_PARAM params[]);
48
49  /* Cipher operation parameters */
50  int OP_cipher_get_ctx_params(void *cctx, OSSL_PARAM params[]);
51  int OP_cipher_set_ctx_params(void *cctx, const OSSL_PARAM params[]);
52
53 =head1 DESCRIPTION
54
55 This documentation is primarily aimed at provider authors. See L<provider(7)>
56 for further information.
57
58 The CIPHER operation enables providers to implement cipher algorithms and make
59 them available to applications via the API functions L<EVP_EncryptInit_ex(3)>,
60 L<EVP_EncryptUpdate(3)> and L<EVP_EncryptFinal(3)> (as well as the decrypt
61 equivalents and other related functions).
62
63 All "functions" mentioned here are passed as function pointers between
64 F<libcrypto> and the provider in B<OSSL_DISPATCH> arrays via
65 B<OSSL_ALGORITHM> arrays that are returned by the provider's
66 provider_query_operation() function
67 (see L<provider-base(7)/Provider Functions>).
68
69 All these "functions" have a corresponding function type definition
70 named B<OSSL_{name}_fn>, and a helper function to retrieve the
71 function pointer from an B<OSSL_DISPATCH> element named
72 B<OSSL_get_{name}>.
73 For example, the "function" OP_cipher_newctx() has these:
74
75  typedef void *(OSSL_OP_cipher_newctx_fn)(void *provctx);
76  static ossl_inline OSSL_OP_cipher_newctx_fn
77      OSSL_get_OP_cipher_newctx(const OSSL_DISPATCH *opf);
78
79 B<OSSL_DISPATCH> arrays are indexed by numbers that are provided as
80 macros in L<openssl-core_numbers.h(7)>, as follows:
81
82  OP_cipher_newctx               OSSL_FUNC_CIPHER_NEWCTX
83  OP_cipher_freectx              OSSL_FUNC_CIPHER_FREECTX
84  OP_cipher_dupctx               OSSL_FUNC_CIPHER_DUPCTX
85
86  OP_cipher_encrypt_init         OSSL_FUNC_CIPHER_ENCRYPT_INIT
87  OP_cipher_decrypt_init         OSSL_FUNC_CIPHER_DECRYPT_INIT
88  OP_cipher_update               OSSL_FUNC_CIPHER_UPDATE
89  OP_cipher_final                OSSL_FUNC_CIPHER_FINAL
90  OP_cipher_cipher               OSSL_FUNC_CIPHER_CIPHER
91
92  OP_cipher_get_params           OSSL_FUNC_CIPHER_GET_PARAMS
93  OP_cipher_get_ctx_params       OSSL_FUNC_CIPHER_GET_CTX_PARAMS
94  OP_cipher_set_ctx_params       OSSL_FUNC_CIPHER_SET_CTX_PARAMS
95
96  OP_cipher_gettable_params      OSSL_FUNC_CIPHER_GETTABLE_PARAMS
97  OP_cipher_gettable_ctx_params  OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS
98  OP_cipher_settable_ctx_params  OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS
99
100 A cipher algorithm implementation may not implement all of these functions.
101 In order to be a consistent set of functions there must at least be a complete
102 set of "encrypt" functions, or a complete set of "decrypt" functions, or a
103 single "cipher" function.
104 In all cases both the OP_cipher_newctx and OP_cipher_freectx functions must be
105 present.
106 All other functions are optional.
107
108 =head2 Context Management Functions
109
110 OP_cipher_newctx() should create and return a pointer to a provider side
111 structure for holding context information during a cipher operation.
112 A pointer to this context will be passed back in a number of the other cipher
113 operation function calls.
114 The parameter B<provctx> is the provider context generated during provider
115 initialisation (see L<provider(3)>).
116
117 OP_cipher_freectx() is passed a pointer to the provider side cipher context in
118 the B<cctx> parameter.
119 This function should free any resources associated with that context.
120
121 OP_cipher_dupctx() should duplicate the provider side cipher context in the
122 B<cctx> parameter and return the duplicate copy.
123
124 =head2 Encryption/Decryption Functions
125
126 OP_cipher_encrypt_init() initialises a cipher operation for encryption given a
127 newly created provider side cipher context in the B<cctx> parameter.
128 The key to be used is given in B<key> which is B<keylen> bytes long.
129 The IV to be used is given in B<iv> which is B<ivlen> bytes long.
130
131 OP_cipher_decrypt_init() is the same as OP_cipher_encrypt_init() except that it
132 initialises the context for a decryption operation.
133
134 OP_cipher_update() is called to supply data to be encrypted/decrypted as part of
135 a previously initialised cipher operation.
136 The B<cctx> parameter contains a pointer to a previously initialised provider
137 side context.
138 OP_cipher_update() should encrypt/decrypt B<inl> bytes of data at the location
139 pointed to by B<in>.
140 The encrypted data should be stored in B<out> and the amount of data written to
141 B<*outl> which should not exceed B<outsize> bytes.
142 OP_cipher_update() may be called multiple times for a single cipher operation.
143 It is the responsibility of the cipher implementation to handle input lengths
144 that are not multiples of the block length.
145 In such cases a cipher implementation will typically cache partial blocks of
146 input data until a complete block is obtained.
147 B<out> may be the same location as B<in> but it should not partially overlap.
148 The same expectations apply to B<outsize> as documented for
149 L<EVP_EncryptUpdate(3)> and L<EVP_DecryptUpdate(3)>.
150
151 OP_cipher_final() completes an encryption or decryption started through previous
152 OP_cipher_encrypt_init() or OP_cipher_decrypt_init(), and OP_cipher_update()
153 calls.
154 The B<cctx> parameter contains a pointer to the provider side context.
155 Any final encryption/decryption output should be written to B<out> and the
156 amount of data written to B<*outl> which should not exceed B<outsize> bytes.
157 The same expectations apply to B<outsize> as documented for
158 L<EVP_EncryptFinal(3)> and L<EVP_DecryptFinal(3)>.
159
160 OP_cipher_cipher() performs encryption/decryption using the provider side cipher
161 context in the B<cctx> parameter that should have been previously initialised via
162 a call to OP_cipher_encrypt_init() or OP_cipher_decrypt_init.
163 This should call the raw underlying cipher function without any padding.
164 This will be invoked in the provider as a result of the application calling
165 L<EVP_Cipher(3)>.
166 The application is responsible for ensuring that the input is a multiple of the
167 block length.
168 The data to be encrypted/decrypted will be in B<in>, and it will be B<inl> bytes
169 in length.
170 The output from the encryption/decryption should be stored in B<out> and the
171 amount of data stored should be put in B<*outl> which should be no more than
172 B<outsize> bytes.
173
174 =head2 Cipher Parameters
175
176 See L<OSSL_PARAM(3)> for further details on the parameters structure used by
177 these functions.
178
179 OP_cipher_get_params() gets details of the algorithm implementation
180 and stores them in B<params>.
181
182 OP_cipher_set_ctx_params() sets cipher operation parameters for the
183 provider side cipher context B<cctx> to B<params>.
184 Any parameter settings are additional to any that were previously set.
185
186 OP_cipher_get_ctx_params() gets cipher operation details details from
187 the given provider side cipher context B<cctx> and stores them in B<params>.
188
189 OP_cipher_gettable_params(), OP_cipher_gettable_ctx_params(), and
190 OP_cipher_settable_ctx_params() all return constant B<OSSL_PARAM> arrays
191 as descriptors of the parameters that OP_cipher_get_params(),
192 OP_cipher_get_ctx_params(), and OP_cipher_set_ctx_params() can handle,
193 respectively.
194
195 Parameters currently recognised by built-in ciphers are as follows. Not all
196 parameters are relevant to, or are understood by all ciphers:
197
198 =over 4
199
200 =item "padding" (B<OSSL_CIPHER_PARAM_PADDING>) <unsigned integer>
201
202 Sets the padding mode for the associated cipher ctx.
203 Setting a value of 1 will turn padding on.
204 Setting a value of 0 will turn padding off.
205
206 =item "mode" (B<OSSL_CIPHER_PARAM_MODE>) <unsigned integer>
207
208 Gets the mode for the associated cipher algorithm.
209 See L<EVP_CIPHER_mode(3)> for a list of valid modes.
210
211 =item "blocksize" (B<OSSL_CIPHER_PARAM_BLOCK_SIZE>) <unsigned integer>
212
213 Gets the block size for the associated cipher algorithm.
214 The block size should be 1 for stream ciphers.
215 Note that the block size for a cipher may be different to the block size for
216 the underlying encryption/decryption primitive.
217 For example AES in CTR mode has a block size of 1 (because it operates like a
218 stream cipher), even though AES has a block size of 16.
219 The length of the "blocksize" parameter should not exceed that of a B<size_t>.
220
221 =item "flags" (B<OSSL_CIPHER_PARAM_FLAGS>) <unsigned integer>
222
223 Gets any flags for the associated cipher algorithm.
224 See L<EVP_CIPHER_meth_set_flags(3)> for a list of currently defined cipher
225 flags.
226 The length of the "flags" parameter should equal that of an
227 B<unsigned long int>.
228
229 =item "keylen" (B<OSSL_CIPHER_PARAM_KEYLEN>) <unsigned integer>
230
231 Gets the key length for the associated cipher algorithm.
232 This can also be used to get or set the key length for the associated cipher
233 ctx.
234 The length of the "keylen" parameter should not exceed that of a B<size_t>.
235
236 =item "ivlen" (B<OSSL_CIPHER_PARAM_IVLEN>) <unsigned integer>
237
238 Gets the IV length for the associated cipher algorithm.
239 The length of the "ivlen" parameter should not exceed that of a B<size_t>.
240
241 =item "iv" (B<OSSL_CIPHER_PARAM_IV>) <octet string OR octet ptr>
242
243 Gets the IV for the associated cipher ctx.
244
245 =item "num" (B<OSSL_CIPHER_PARAM_NUM>) <unsigned integer>
246
247 Gets or sets the cipher specific "num" parameter for the associated cipher ctx.
248 Built-in ciphers typically use this to track how much of the current underlying
249 block has been "used" already.
250
251 =item "tag" (B<OSSL_CIPHER_PARAM_AEAD_TAG>) <octet string>
252
253 Gets or sets the AEAD tag for the associated cipher ctx.
254 See L<EVP_EncryptInit(3)/AEAD Interface>.
255
256 =item "taglen" (B<OSSL_CIPHER_PARAM_AEAD_TAGLEN>) <unsigned integer>
257
258 Gets the tag length to be used for an AEAD cipher for the associated cipher ctx.
259 It returns a default value if it has not been set.
260 The length of the "taglen" parameter should not exceed that of a B<size_t>.
261
262 =item "tlsaad" (B<OSSL_CIPHER_PARAM_AEAD_TLS1_AAD>) <octet string>
263
264 =for comment TODO(3.0): Consider changing this interface so that all ciphers
265 use the standard AEAD interface - rather than having this special purpose
266 interface for TLS
267
268 Sets TLSv1.2 AAD information for the associated cipher ctx.
269 TLSv1.2 AAD information is always 13 bytes in length and is as defined for the
270 "additional_data" field described in section 6.2.3.3 of RFC5246.
271
272 =item "tlsaadpad" (B<OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD>) <unsigned integer>
273
274 Gets the length of the tag that will be added to a TLS record for the AEAD
275 tag for the associated cipher ctx.
276 The length of the "tlsaadpad" parameter should not exceed that of a B<size_t>.
277
278 =item "tlsivfixed" (B<OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED>) <octet string>
279
280 =for comment TODO(3.0): This interface needs completely redesigning!
281
282 Sets the fixed portion of an IV for an AEAD cipher used in a TLS record
283 encryption/ decryption for the associated cipher ctx.
284 TLS record encryption/decryption always occurs "in place" so that the input and
285 output buffers are always the same memory location.
286 AEAD IVs in TLSv1.2 consist of an implicit "fixed" part and an explicit part
287 that varies with every record.
288 Setting a TLS fixed IV changes a cipher to encrypt/decrypt TLS records.
289 TLS records are encrypted/decrypted using a single OP_cipher_cipher call per
290 record.
291 For a record decryption the first bytes of the input buffer will be the explict
292 part of the IV and the final bytes of the input buffer will be the AEAD tag.
293 The length of the explicit part of the IV and the tag length will depend on the
294 cipher in use and will be defined in the RFC for the relevant ciphersuite.
295 In order to allow for "in place" decryption the plaintext output should be
296 written to the same location in the output buffer that the ciphertext payload
297 was read from, i.e. immediately after the explicit IV.
298
299 When encrypting a record the first bytes of the input buffer will be empty to
300 allow space for the explicit IV, as will the final bytes where the tag will
301 be written.
302 The length of the input buffer will include the length of the explicit IV, the
303 payload, and the tag bytes.
304 The cipher implementation should generate the explicit IV and write it to the
305 beginning of the output buffer, do "in place" encryption of the payload and
306 write that to the output buffer, and finally add the tag onto the end of the
307 output buffer.
308
309 Whether encrypting or decrypting the value written to B<*outl> in the
310 OP_cipher_cipher call should be the length of the payload excluding the explicit
311 IV length and the tag length.
312
313 =item "ivlen" (B<OSSL_CIPHER_PARAM_AEAD_IVLEN>) <unsigned integer>
314
315 Sets the IV length to be used for an AEAD cipher for the associated cipher ctx.
316 The length of the "ivlen" parameter should not exceed that of a B<size_t>.
317
318 =item "randkey" (B<OSSL_CIPHER_PARAM_RANDOM_KEY>) <octet string>
319
320 Gets a implementation specific randomly generated key for the associated
321 cipher ctx. This is currently only supported by 3DES (which sets the key to
322 odd parity).
323
324 =back
325
326 =head1 RETURN VALUES
327
328 OP_cipher_newctx() and OP_cipher_dupctx() should return the newly created
329 provider side cipher context, or NULL on failure.
330
331 OP_cipher_encrypt_init(), OP_cipher_decrypt_init(), OP_cipher_update(),
332 OP_cipher_final(), OP_cipher_cipher(), OP_cipher_get_params(),
333 OP_cipher_get_ctx_params() and OP_cipher_set_ctx_params() should return 1 for
334 success or 0 on error.
335
336 OP_cipher_gettable_params(), OP_cipher_gettable_ctx_params() and
337 OP_cipher_settable_ctx_params() should return a constant B<OSSL_PARAM>
338 array, or NULL if none is offered.
339
340 =head1 SEE ALSO
341
342 L<provider(7)>
343
344 =head1 HISTORY
345
346 The provider CIPHER interface was introduced in OpenSSL 3.0.
347
348 =head1 COPYRIGHT
349
350 Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
351
352 Licensed under the Apache License 2.0 (the "License").  You may not use
353 this file except in compliance with the License.  You can obtain a copy
354 in the file LICENSE in the source distribution or at
355 L<https://www.openssl.org/source/license.html>.
356
357 =cut