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           Sets the expected tag to "taglen" bytes from "tag".  The tag length
345           can only be set before specifying an IV.  "taglen" must be between
346           1 and 16 inclusive.
347
348           For GCM, this call is only valid when decrypting data.
349
350           For OCB, this call is valid when decrypting data to set the
351           expected tag, and before encryption to set the desired tag length.
352
353           In OCB mode, calling this before encryption with "tag" set to
354           "NULL" sets the tag length.  If this is not called prior to
355           encryption, a default tag length is used.
356
357           For OCB AES, the default tag length is 16 (i.e. 128 bits).  It is
358           also the maximum tag length for OCB.
359
360   CCM Mode
361       The EVP interface for CCM mode is similar to that of the GCM mode but
362       with a few additional requirements and different ctrl values.
363
364       For CCM mode, the total plaintext or ciphertext length MUST be passed
365       to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with
366       the output and input parameters (in and out) set to NULL and the length
367       passed in the inl parameter.
368
369       The following ctrls are supported in CCM mode.
370
371       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
372           This call is made to set the expected CCM tag value when decrypting
373           or the length of the tag (with the "tag" parameter set to NULL)
374           when encrypting.  The tag length is often referred to as M. If not
375           set a default value is used (12 for AES). When decrypting, the tag
376           needs to be set before passing in data to be decrypted, but as in
377           GCM and OCB mode, it can be set after passing additional
378           authenticated data (see "AEAD Interface").
379
380       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, ivlen, NULL)
381           Sets the CCM L value. If not set a default is used (8 for AES).
382
383       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
384           Sets the CCM nonce (IV) length. This call can only be made before
385           specifying a nonce value. The nonce length is given by 15 - L so it
386           is 7 by default for AES.
387
388   ChaCha20-Poly1305
389       The following ctrls are supported for the ChaCha20-Poly1305 AEAD
390       algorithm.
391
392       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
393           Sets the nonce length. This call can only be made before specifying
394           the nonce.  If not called a default nonce length of 12 (i.e. 96
395           bits) is used. The maximum nonce length is 12 bytes (i.e. 96-bits).
396           If a nonce of less than 12 bytes is set then the nonce is
397           automatically padded with leading 0 bytes to make it 12 bytes in
398           length.
399
400       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)
401           Writes "taglen" bytes of the tag value to the buffer indicated by
402           "tag".  This call can only be made when encrypting data and after
403           all data has been processed (e.g. after an EVP_EncryptFinal()
404           call).
405
406           "taglen" specified here must be 16 (POLY1305_BLOCK_SIZE, i.e.
407           128-bits) or less.
408
409       EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
410           Sets the expected tag to "taglen" bytes from "tag".  The tag length
411           can only be set before specifying an IV.  "taglen" must be between
412           1 and 16 (POLY1305_BLOCK_SIZE) inclusive.  This call is only valid
413           when decrypting data.
414

NOTES

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

BUGS

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

EXAMPLES

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

SEE ALSO

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

HISTORY

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