1EVP_ENCRYPTINIT(3)                  OpenSSL                 EVP_ENCRYPTINIT(3)
2
3
4

NAME

6       EVP_CIPHER_CTX_new, EVP_CIPHER_CTX_reset, EVP_CIPHER_CTX_free,
7       EVP_EncryptInit_ex, EVP_EncryptUpdate, EVP_EncryptFinal_ex,
8       EVP_DecryptInit_ex, EVP_DecryptUpdate, EVP_DecryptFinal_ex,
9       EVP_CipherInit_ex, EVP_CipherUpdate, EVP_CipherFinal_ex,
10       EVP_CIPHER_CTX_set_key_length, EVP_CIPHER_CTX_ctrl, EVP_EncryptInit,
11       EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal, EVP_CipherInit,
12       EVP_CipherFinal, EVP_get_cipherbyname, EVP_get_cipherbynid,
13       EVP_get_cipherbyobj, EVP_CIPHER_nid, EVP_CIPHER_block_size,
14       EVP_CIPHER_key_length, EVP_CIPHER_iv_length, EVP_CIPHER_flags,
15       EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher,
16       EVP_CIPHER_CTX_nid, EVP_CIPHER_CTX_block_size,
17       EVP_CIPHER_CTX_key_length, EVP_CIPHER_CTX_iv_length,
18       EVP_CIPHER_CTX_get_app_data, EVP_CIPHER_CTX_set_app_data,
19       EVP_CIPHER_CTX_type, EVP_CIPHER_CTX_flags, EVP_CIPHER_CTX_mode,
20       EVP_CIPHER_param_to_asn1, EVP_CIPHER_asn1_to_param,
21       EVP_CIPHER_CTX_set_padding, EVP_enc_null - EVP cipher routines
22

SYNOPSIS

24        #include <openssl/evp.h>
25
26        EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void);
27        int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx);
28        void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx);
29
30        int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
31                               ENGINE *impl, const unsigned char *key, const unsigned char *iv);
32        int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
33                              int *outl, const unsigned char *in, int inl);
34        int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
35
36        int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
37                               ENGINE *impl, const unsigned char *key, const unsigned char *iv);
38        int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
39                              int *outl, const unsigned char *in, int inl);
40        int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
41
42        int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
43                              ENGINE *impl, const unsigned char *key, const unsigned char *iv, int enc);
44        int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
45                             int *outl, const unsigned char *in, int inl);
46        int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
47
48        int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
49                            const unsigned char *key, const unsigned char *iv);
50        int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
51
52        int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
53                            const unsigned char *key, const unsigned char *iv);
54        int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
55
56        int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
57                           const unsigned char *key, const unsigned char *iv, int enc);
58        int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
59
60        int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
61        int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
62        int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
63        int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key);
64
65        const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
66        const EVP_CIPHER *EVP_get_cipherbynid(int nid);
67        const EVP_CIPHER *EVP_get_cipherbyobj(const ASN1_OBJECT *a);
68
69        int EVP_CIPHER_nid(const EVP_CIPHER *e);
70        int EVP_CIPHER_block_size(const EVP_CIPHER *e);
71        int EVP_CIPHER_key_length(const EVP_CIPHER *e);
72        int EVP_CIPHER_iv_length(const EVP_CIPHER *e);
73        unsigned long EVP_CIPHER_flags(const EVP_CIPHER *e);
74        unsigned long EVP_CIPHER_mode(const EVP_CIPHER *e);
75        int EVP_CIPHER_type(const EVP_CIPHER *ctx);
76
77        const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx);
78        int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx);
79        int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx);
80        int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx);
81        int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx);
82        void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx);
83        void EVP_CIPHER_CTX_set_app_data(const EVP_CIPHER_CTX *ctx, void *data);
84        int EVP_CIPHER_CTX_type(const EVP_CIPHER_CTX *ctx);
85        int EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx);
86
87        int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
88        int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
89

DESCRIPTION

91       The EVP cipher routines are a high-level interface to certain symmetric
92       ciphers.
93
94       EVP_CIPHER_CTX_new() creates a cipher context.
95
96       EVP_CIPHER_CTX_free() clears all information from a cipher context and
97       free up any allocated memory associate with it, including ctx itself.
98       This function should be called after all operations using a cipher are
99       complete so sensitive information does not remain in memory.
100
101       EVP_EncryptInit_ex() sets up cipher context ctx for encryption with
102       cipher type from ENGINE impl. ctx must be created before calling this
103       function. type is normally supplied by a function such as
104       EVP_aes_256_cbc(). If impl is NULL then the default implementation is
105       used. key is the symmetric key to use and iv is the IV to use (if
106       necessary), the actual number of bytes used for the key and IV depends
107       on the cipher. It is possible to set all parameters to NULL except type
108       in an initial call and supply the remaining parameters in subsequent
109       calls, all of which have type set to NULL. This is done when the
110       default cipher parameters are not appropriate.
111
112       EVP_EncryptUpdate() encrypts inl bytes from the buffer in and writes
113       the encrypted version to out. This function can be called multiple
114       times to encrypt successive blocks of data. The amount of data written
115       depends on the block alignment of the encrypted data.  For most ciphers
116       and modes, the amount of data written can be anything from zero bytes
117       to (inl + cipher_block_size - 1) bytes.  For wrap cipher modes, the
118       amount of data written can be anything from zero bytes to (inl +
119       cipher_block_size) bytes.  For stream ciphers, the amount of data
120       written can be anything from zero bytes to inl bytes.  Thus, out should
121       contain sufficient room for the operation being performed.  The actual
122       number of bytes written is placed in outl. It also checks if in and out
123       are partially overlapping, and if they are 0 is returned to indicate
124       failure.
125
126       If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts
127       the "final" data, that is any data that remains in a partial block.  It
128       uses standard block padding (aka PKCS padding) as described in the
129       NOTES section, below. The encrypted final data is written to out which
130       should have sufficient space for one cipher block. The number of bytes
131       written is placed in outl. After this function is called the encryption
132       operation is finished and no further calls to EVP_EncryptUpdate()
133       should be made.
134
135       If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any
136       more data and it will return an error if any data remains in a partial
137       block: that is if the total data length is not a multiple of the block
138       size.
139
140       EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are
141       the corresponding decryption operations. EVP_DecryptFinal() will return
142       an error code if padding is enabled and the final block is not
143       correctly formatted. The parameters and restrictions are identical to
144       the encryption operations except that if padding is enabled the
145       decrypted data buffer out passed to EVP_DecryptUpdate() should have
146       sufficient room for (inl + cipher_block_size) bytes unless the cipher
147       block size is 1 in which case inl bytes is sufficient.
148
149       EVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex() are
150       functions that can be used for decryption or encryption. The operation
151       performed depends on the value of the enc parameter. It should be set
152       to 1 for encryption, 0 for decryption and -1 to leave the value
153       unchanged (the actual value of 'enc' being supplied in a previous
154       call).
155
156       EVP_CIPHER_CTX_reset() clears all information from a cipher context and
157       free up any allocated memory associate with it, except the ctx itself.
158       This function should be called anytime ctx is to be reused for another
159       EVP_CipherInit() / EVP_CipherUpdate() / EVP_CipherFinal() series of
160       calls.
161
162       EVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit() behave in a
163       similar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex() and
164       EVP_CipherInit_ex() except they always use the default cipher
165       implementation.
166
167       EVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal() are
168       identical to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and
169       EVP_CipherFinal_ex(). In previous releases they also cleaned up the
170       ctx, but this is no longer done and EVP_CIPHER_CTX_clean() must be
171       called to free any context resources.
172
173       EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
174       return an EVP_CIPHER structure when passed a cipher name, a NID or an
175       ASN1_OBJECT structure.
176
177       EVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return the NID of a cipher
178       when passed an EVP_CIPHER or EVP_CIPHER_CTX structure.  The actual NID
179       value is an internal value which may not have a corresponding OBJECT
180       IDENTIFIER.
181
182       EVP_CIPHER_CTX_set_padding() enables or disables padding. This function
183       should be called after the context is set up for encryption or
184       decryption with EVP_EncryptInit_ex(), EVP_DecryptInit_ex() or
185       EVP_CipherInit_ex(). By default encryption operations are padded using
186       standard block padding and the padding is checked and removed when
187       decrypting. If the pad parameter is zero then no padding is performed,
188       the total amount of data encrypted or decrypted must then be a multiple
189       of the block size or an error will occur.
190
191       EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
192       length of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX
193       structure. The constant EVP_MAX_KEY_LENGTH is the maximum key length
194       for all ciphers. Note: although EVP_CIPHER_key_length() is fixed for a
195       given cipher, the value of EVP_CIPHER_CTX_key_length() may be different
196       for variable key length ciphers.
197
198       EVP_CIPHER_CTX_set_key_length() sets the key length of the cipher ctx.
199       If the cipher is a fixed length cipher then attempting to set the key
200       length to any value other than the fixed value is an error.
201
202       EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
203       length of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX.  It
204       will return zero if the cipher does not use an IV.  The constant
205       EVP_MAX_IV_LENGTH is the maximum IV length for all ciphers.
206
207       EVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the
208       block size of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX
209       structure. The constant EVP_MAX_BLOCK_LENGTH is also the maximum block
210       length for all ciphers.
211
212       EVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the type of the
213       passed cipher or context. This "type" is the actual NID of the cipher
214       OBJECT IDENTIFIER as such it ignores the cipher parameters and 40 bit
215       RC2 and 128 bit RC2 have the same NID. If the cipher does not have an
216       object identifier or does not have ASN1 support this function will
217       return NID_undef.
218
219       EVP_CIPHER_CTX_cipher() returns the EVP_CIPHER structure when passed an
220       EVP_CIPHER_CTX structure.
221
222       EVP_CIPHER_mode() and EVP_CIPHER_CTX_mode() return the block cipher
223       mode: EVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE,
224       EVP_CIPH_OFB_MODE, EVP_CIPH_CTR_MODE, EVP_CIPH_GCM_MODE,
225       EVP_CIPH_CCM_MODE, EVP_CIPH_XTS_MODE, EVP_CIPH_WRAP_MODE or
226       EVP_CIPH_OCB_MODE. If the cipher is a stream cipher then
227       EVP_CIPH_STREAM_CIPHER is returned.
228
229       EVP_CIPHER_param_to_asn1() sets the AlgorithmIdentifier "parameter"
230       based on the passed cipher. This will typically include any parameters
231       and an IV. The cipher IV (if any) must be set when this call is made.
232       This call should be made before the cipher is actually "used" (before
233       any EVP_EncryptUpdate(), EVP_DecryptUpdate() calls for example). This
234       function may fail if the cipher does not have any ASN1 support.
235
236       EVP_CIPHER_asn1_to_param() sets the cipher parameters based on an ASN1
237       AlgorithmIdentifier "parameter". The precise effect depends on the
238       cipher In the case of RC2, for example, it will set the IV and
239       effective key length.  This function should be called after the base
240       cipher type is set but before the key is set. For example
241       EVP_CipherInit() will be called with the IV and key set to NULL,
242       EVP_CIPHER_asn1_to_param() will be called and finally EVP_CipherInit()
243       again with all parameters except the key set to NULL. It is possible
244       for this function to fail if the cipher does not have any ASN1 support
245       or the parameters cannot be set (for example the RC2 effective key
246       length is not supported.
247
248       EVP_CIPHER_CTX_ctrl() allows various cipher specific parameters to be
249       determined and set.
250
251       EVP_CIPHER_CTX_rand_key() generates a random key of the appropriate
252       length based on the cipher context. The EVP_CIPHER can provide its own
253       random key generation routine to support keys of a specific form. Key
254       must point to a buffer at least as big as the value returned by
255       EVP_CIPHER_CTX_key_length().
256

RETURN VALUES

258       EVP_CIPHER_CTX_new() returns a pointer to a newly created
259       EVP_CIPHER_CTX for success and NULL for failure.
260
261       EVP_EncryptInit_ex(), EVP_EncryptUpdate() and EVP_EncryptFinal_ex()
262       return 1 for success and 0 for failure.
263
264       EVP_DecryptInit_ex() and EVP_DecryptUpdate() return 1 for success and 0
265       for failure.  EVP_DecryptFinal_ex() returns 0 if the decrypt failed or
266       1 for success.
267
268       EVP_CipherInit_ex() and EVP_CipherUpdate() return 1 for success and 0
269       for failure.  EVP_CipherFinal_ex() returns 0 for a decryption failure
270       or 1 for success.
271
272       EVP_CIPHER_CTX_reset() returns 1 for success and 0 for failure.
273
274       EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
275       return an EVP_CIPHER structure or NULL on error.
276
277       EVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return a NID.
278
279       EVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the
280       block size.
281
282       EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
283       length.
284
285       EVP_CIPHER_CTX_set_padding() always returns 1.
286
287       EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
288       length or zero if the cipher does not use an IV.
289
290       EVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the NID of the
291       cipher's OBJECT IDENTIFIER or NID_undef if it has no defined OBJECT
292       IDENTIFIER.
293
294       EVP_CIPHER_CTX_cipher() returns an EVP_CIPHER structure.
295
296       EVP_CIPHER_param_to_asn1() and EVP_CIPHER_asn1_to_param() return
297       greater than zero for success and zero or a negative number on failure.
298
299       EVP_CIPHER_CTX_rand_key() returns 1 for success.
300

CIPHER LISTING

302       All algorithms have a fixed key length unless otherwise stated.
303
304       Refer to "SEE ALSO" for the full list of ciphers available through the
305       EVP interface.
306
307       EVP_enc_null()
308           Null cipher: does nothing.
309

AEAD Interface

311       The EVP interface for Authenticated Encryption with Associated Data
312       (AEAD) modes are subtly altered and several additional ctrl operations
313       are supported depending on the mode specified.
314
315       To specify additional authenticated data (AAD), a call to
316       EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() should
317       be made with the output parameter out set to NULL.
318
319       When decrypting, the return value of EVP_DecryptFinal() or
320       EVP_CipherFinal() indicates whether the operation was successful. If it
321       does not indicate success, the authentication operation has failed and
322       any output data MUST NOT be used as it is corrupted.
323
324   GCM and OCB Modes
325       The following ctrls are supported in GCM and OCB modes.
326
327       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
328           Sets the IV length. This call can only be made before specifying an
329           IV. If not called a default IV length is used.
330
331           For GCM AES and OCB AES the default is 12 (i.e. 96 bits). For OCB
332           mode the maximum is 15.
333
334       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)
335           Writes "taglen" bytes of the tag value to the buffer indicated by
336           "tag".  This call can only be made when encrypting data and after
337           all data has been processed (e.g. after an EVP_EncryptFinal()
338           call).
339
340           For OCB, "taglen" must either be 16 or the value previously set via
341           EVP_CTRL_AEAD_SET_TAG.
342
343       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
344           When decrypting, this call sets the expected tag to "taglen" bytes
345           from "tag".  "taglen" must be between 1 and 16 inclusive.  The tag
346           must be set prior to any call to EVP_DecryptFinal() or
347           EVP_DecryptFinal_ex().
348
349           For GCM, this call is only valid when decrypting data.
350
351           For OCB, this call is valid when decrypting data to set the
352           expected tag, and when encrypting to set the desired tag length.
353
354           In OCB mode, calling this when encrypting with "tag" set to "NULL"
355           sets the tag length. The tag length can only be set before
356           specifying an IV. If this is not called prior to setting the IV
357           during encryption, then a default tag length is used.
358
359           For OCB AES, the default tag length is 16 (i.e. 128 bits).  It is
360           also the maximum tag length for OCB.
361
362   CCM Mode
363       The EVP interface for CCM mode is similar to that of the GCM mode but
364       with a few additional requirements and different ctrl values.
365
366       For CCM mode, the total plaintext or ciphertext length MUST be passed
367       to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with
368       the output and input parameters (in and out) set to NULL and the length
369       passed in the inl parameter.
370
371       The following ctrls are supported in CCM mode.
372
373       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
374           This call is made to set the expected CCM tag value when decrypting
375           or the length of the tag (with the "tag" parameter set to NULL)
376           when encrypting.  The tag length is often referred to as M. If not
377           set a default value is used (12 for AES). When decrypting, the tag
378           needs to be set before passing in data to be decrypted, but as in
379           GCM and OCB mode, it can be set after passing additional
380           authenticated data (see "AEAD Interface").
381
382       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, ivlen, NULL)
383           Sets the CCM L value. If not set a default is used (8 for AES).
384
385       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
386           Sets the CCM nonce (IV) length. This call can only be made before
387           specifying a nonce value. The nonce length is given by 15 - L so it
388           is 7 by default for AES.
389
390   ChaCha20-Poly1305
391       The following ctrls are supported for the ChaCha20-Poly1305 AEAD
392       algorithm.
393
394       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
395           Sets the nonce length. This call can only be made before specifying
396           the nonce.  If not called a default nonce length of 12 (i.e. 96
397           bits) is used. The maximum nonce length is 12 bytes (i.e. 96-bits).
398           If a nonce of less than 12 bytes is set then the nonce is
399           automatically padded with leading 0 bytes to make it 12 bytes in
400           length.
401
402       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)
403           Writes "taglen" bytes of the tag value to the buffer indicated by
404           "tag".  This call can only be made when encrypting data and after
405           all data has been processed (e.g. after an EVP_EncryptFinal()
406           call).
407
408           "taglen" specified here must be 16 (POLY1305_BLOCK_SIZE, i.e.
409           128-bits) or less.
410
411       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
412           Sets the expected tag to "taglen" bytes from "tag".  The tag length
413           can only be set before specifying an IV.  "taglen" must be between
414           1 and 16 (POLY1305_BLOCK_SIZE) inclusive.  This call is only valid
415           when decrypting data.
416

NOTES

418       Where possible the EVP interface to symmetric ciphers should be used in
419       preference to the low-level interfaces. This is because the code then
420       becomes transparent to the cipher used and much more flexible.
421       Additionally, the EVP interface will ensure the use of platform
422       specific cryptographic acceleration such as AES-NI (the low-level
423       interfaces do not provide the guarantee).
424
425       PKCS padding works by adding n padding bytes of value n to make the
426       total length of the encrypted data a multiple of the block size.
427       Padding is always added so if the data is already a multiple of the
428       block size n will equal the block size. For example if the block size
429       is 8 and 11 bytes are to be encrypted then 5 padding bytes of value 5
430       will be added.
431
432       When decrypting the final block is checked to see if it has the correct
433       form.
434
435       Although the decryption operation can produce an error if padding is
436       enabled, it is not a strong test that the input data or key is correct.
437       A random block has better than 1 in 256 chance of being of the correct
438       format and problems with the input data earlier on will not produce a
439       final decrypt error.
440
441       If padding is disabled then the decryption operation will always
442       succeed if the total amount of data decrypted is a multiple of the
443       block size.
444
445       The functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(),
446       EVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained
447       for compatibility with existing code. New code should use
448       EVP_EncryptInit_ex(), EVP_EncryptFinal_ex(), EVP_DecryptInit_ex(),
449       EVP_DecryptFinal_ex(), EVP_CipherInit_ex() and EVP_CipherFinal_ex()
450       because they can reuse an existing context without allocating and
451       freeing it up on each call.
452
453       There are some differences between functions EVP_CipherInit() and
454       EVP_CipherInit_ex(), significant in some circumstances.
455       EVP_CipherInit() fills the passed context object with zeros.  As a
456       consequence, EVP_CipherInit() does not allow step-by-step
457       initialization of the ctx when the key and iv are passed in separate
458       calls. It also means that the flags set for the CTX are removed, and it
459       is especially important for the EVP_CIPHER_CTX_FLAG_WRAP_ALLOW flag
460       treated specially in EVP_CipherInit_ex().
461
462       EVP_get_cipherbynid(), and EVP_get_cipherbyobj() are implemented as
463       macros.
464

BUGS

466       EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal
467       ciphers with default key lengths. If custom ciphers exceed these values
468       the results are unpredictable. This is because it has become standard
469       practice to define a generic key as a fixed unsigned char array
470       containing EVP_MAX_KEY_LENGTH bytes.
471
472       The ASN1 code is incomplete (and sometimes inaccurate) it has only been
473       tested for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC
474       mode.
475

EXAMPLES

477       Encrypt a string using IDEA:
478
479        int do_crypt(char *outfile)
480        {
481            unsigned char outbuf[1024];
482            int outlen, tmplen;
483            /*
484             * Bogus key and IV: we'd normally set these from
485             * another source.
486             */
487            unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
488            unsigned char iv[] = {1,2,3,4,5,6,7,8};
489            char intext[] = "Some Crypto Text";
490            EVP_CIPHER_CTX *ctx;
491            FILE *out;
492
493            ctx = EVP_CIPHER_CTX_new();
494            EVP_EncryptInit_ex(ctx, EVP_idea_cbc(), NULL, key, iv);
495
496            if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) {
497                /* Error */
498                EVP_CIPHER_CTX_free(ctx);
499                return 0;
500            }
501            /*
502             * Buffer passed to EVP_EncryptFinal() must be after data just
503             * encrypted to avoid overwriting it.
504             */
505            if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) {
506                /* Error */
507                EVP_CIPHER_CTX_free(ctx);
508                return 0;
509            }
510            outlen += tmplen;
511            EVP_CIPHER_CTX_free(ctx);
512            /*
513             * Need binary mode for fopen because encrypted data is
514             * binary data. Also cannot use strlen() on it because
515             * it won't be NUL terminated and may contain embedded
516             * NULs.
517             */
518            out = fopen(outfile, "wb");
519            if (out == NULL) {
520                /* Error */
521                return 0;
522            }
523            fwrite(outbuf, 1, outlen, out);
524            fclose(out);
525            return 1;
526        }
527
528       The ciphertext from the above example can be decrypted using the
529       openssl utility with the command line (shown on two lines for clarity):
530
531        openssl idea -d \
532            -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 <filename
533
534       General encryption and decryption function example using FILE I/O and
535       AES128 with a 128-bit key:
536
537        int do_crypt(FILE *in, FILE *out, int do_encrypt)
538        {
539            /* Allow enough space in output buffer for additional block */
540            unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
541            int inlen, outlen;
542            EVP_CIPHER_CTX *ctx;
543            /*
544             * Bogus key and IV: we'd normally set these from
545             * another source.
546             */
547            unsigned char key[] = "0123456789abcdeF";
548            unsigned char iv[] = "1234567887654321";
549
550            /* Don't set key or IV right away; we want to check lengths */
551            ctx = EVP_CIPHER_CTX_new();
552            EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, NULL, NULL,
553                              do_encrypt);
554            OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16);
555            OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == 16);
556
557            /* Now we can set key and IV */
558            EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt);
559
560            for (;;) {
561                inlen = fread(inbuf, 1, 1024, in);
562                if (inlen <= 0)
563                    break;
564                if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
565                    /* Error */
566                    EVP_CIPHER_CTX_free(ctx);
567                    return 0;
568                }
569                fwrite(outbuf, 1, outlen, out);
570            }
571            if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
572                /* Error */
573                EVP_CIPHER_CTX_free(ctx);
574                return 0;
575            }
576            fwrite(outbuf, 1, outlen, out);
577
578            EVP_CIPHER_CTX_free(ctx);
579            return 1;
580        }
581

SEE ALSO

583       evp(7)
584
585       Supported ciphers are listed in:
586
587       EVP_aes(3), EVP_aria(3), EVP_bf(3), EVP_camellia(3), EVP_cast5(3),
588       EVP_chacha20(3), EVP_des(3), EVP_desx(3), EVP_idea(3), EVP_rc2(3),
589       EVP_rc4(3), EVP_rc5(3), EVP_seed(3), EVP_sm4(3)
590

HISTORY

592       Support for OCB mode was added in OpenSSL 1.1.0.
593
594       EVP_CIPHER_CTX was made opaque in OpenSSL 1.1.0.  As a result,
595       EVP_CIPHER_CTX_reset() appeared and EVP_CIPHER_CTX_cleanup()
596       disappeared.  EVP_CIPHER_CTX_init() remains as an alias for
597       EVP_CIPHER_CTX_reset().
598
600       Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
601
602       Licensed under the OpenSSL license (the "License").  You may not use
603       this file except in compliance with the License.  You can obtain a copy
604       in the file LICENSE in the source distribution or at
605       <https://www.openssl.org/source/license.html>.
606
607
608
6091.1.1q                            2022-07-21                EVP_ENCRYPTINIT(3)
Impressum