1EVP_ENCRYPTINIT(3) OpenSSL EVP_ENCRYPTINIT(3)
2
3
4
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
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
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: as a result the
116 amount of data written may be anything from zero bytes to (inl +
117 cipher_block_size - 1) so out should contain sufficient room. The
118 actual number of bytes written is placed in outl. It also checks if in
119 and out are partially overlapping, and if they are 0 is returned to
120 indicate failure.
121
122 If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts
123 the "final" data, that is any data that remains in a partial block. It
124 uses standard block padding (aka PKCS padding) as described in the
125 NOTES section, below. The encrypted final data is written to out which
126 should have sufficient space for one cipher block. The number of bytes
127 written is placed in outl. After this function is called the encryption
128 operation is finished and no further calls to EVP_EncryptUpdate()
129 should be made.
130
131 If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any
132 more data and it will return an error if any data remains in a partial
133 block: that is if the total data length is not a multiple of the block
134 size.
135
136 EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are
137 the corresponding decryption operations. EVP_DecryptFinal() will return
138 an error code if padding is enabled and the final block is not
139 correctly formatted. The parameters and restrictions are identical to
140 the encryption operations except that if padding is enabled the
141 decrypted data buffer out passed to EVP_DecryptUpdate() should have
142 sufficient room for (inl + cipher_block_size) bytes unless the cipher
143 block size is 1 in which case inl bytes is sufficient.
144
145 EVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex() are
146 functions that can be used for decryption or encryption. The operation
147 performed depends on the value of the enc parameter. It should be set
148 to 1 for encryption, 0 for decryption and -1 to leave the value
149 unchanged (the actual value of 'enc' being supplied in a previous
150 call).
151
152 EVP_CIPHER_CTX_reset() clears all information from a cipher context and
153 free up any allocated memory associate with it, except the ctx itself.
154 This function should be called anytime ctx is to be reused for another
155 EVP_CipherInit() / EVP_CipherUpdate() / EVP_CipherFinal() series of
156 calls.
157
158 EVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit() behave in a
159 similar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex() and
160 EVP_CipherInit_ex() except they always use the default cipher
161 implementation.
162
163 EVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal() are
164 identical to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and
165 EVP_CipherFinal_ex(). In previous releases they also cleaned up the
166 ctx, but this is no longer done and EVP_CIPHER_CTX_clean() must be
167 called to free any context resources.
168
169 EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
170 return an EVP_CIPHER structure when passed a cipher name, a NID or an
171 ASN1_OBJECT structure.
172
173 EVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return the NID of a cipher
174 when passed an EVP_CIPHER or EVP_CIPHER_CTX structure. The actual NID
175 value is an internal value which may not have a corresponding OBJECT
176 IDENTIFIER.
177
178 EVP_CIPHER_CTX_set_padding() enables or disables padding. This function
179 should be called after the context is set up for encryption or
180 decryption with EVP_EncryptInit_ex(), EVP_DecryptInit_ex() or
181 EVP_CipherInit_ex(). By default encryption operations are padded using
182 standard block padding and the padding is checked and removed when
183 decrypting. If the pad parameter is zero then no padding is performed,
184 the total amount of data encrypted or decrypted must then be a multiple
185 of the block size or an error will occur.
186
187 EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
188 length of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX
189 structure. The constant EVP_MAX_KEY_LENGTH is the maximum key length
190 for all ciphers. Note: although EVP_CIPHER_key_length() is fixed for a
191 given cipher, the value of EVP_CIPHER_CTX_key_length() may be different
192 for variable key length ciphers.
193
194 EVP_CIPHER_CTX_set_key_length() sets the key length of the cipher ctx.
195 If the cipher is a fixed length cipher then attempting to set the key
196 length to any value other than the fixed value is an error.
197
198 EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
199 length of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX. It
200 will return zero if the cipher does not use an IV. The constant
201 EVP_MAX_IV_LENGTH is the maximum IV length for all ciphers.
202
203 EVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the
204 block size of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX
205 structure. The constant EVP_MAX_BLOCK_LENGTH is also the maximum block
206 length for all ciphers.
207
208 EVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the type of the
209 passed cipher or context. This "type" is the actual NID of the cipher
210 OBJECT IDENTIFIER as such it ignores the cipher parameters and 40 bit
211 RC2 and 128 bit RC2 have the same NID. If the cipher does not have an
212 object identifier or does not have ASN1 support this function will
213 return NID_undef.
214
215 EVP_CIPHER_CTX_cipher() returns the EVP_CIPHER structure when passed an
216 EVP_CIPHER_CTX structure.
217
218 EVP_CIPHER_mode() and EVP_CIPHER_CTX_mode() return the block cipher
219 mode: EVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE,
220 EVP_CIPH_OFB_MODE, EVP_CIPH_CTR_MODE, EVP_CIPH_GCM_MODE,
221 EVP_CIPH_CCM_MODE, EVP_CIPH_XTS_MODE, EVP_CIPH_WRAP_MODE or
222 EVP_CIPH_OCB_MODE. If the cipher is a stream cipher then
223 EVP_CIPH_STREAM_CIPHER is returned.
224
225 EVP_CIPHER_param_to_asn1() sets the AlgorithmIdentifier "parameter"
226 based on the passed cipher. This will typically include any parameters
227 and an IV. The cipher IV (if any) must be set when this call is made.
228 This call should be made before the cipher is actually "used" (before
229 any EVP_EncryptUpdate(), EVP_DecryptUpdate() calls for example). This
230 function may fail if the cipher does not have any ASN1 support.
231
232 EVP_CIPHER_asn1_to_param() sets the cipher parameters based on an ASN1
233 AlgorithmIdentifier "parameter". The precise effect depends on the
234 cipher In the case of RC2, for example, it will set the IV and
235 effective key length. This function should be called after the base
236 cipher type is set but before the key is set. For example
237 EVP_CipherInit() will be called with the IV and key set to NULL,
238 EVP_CIPHER_asn1_to_param() will be called and finally EVP_CipherInit()
239 again with all parameters except the key set to NULL. It is possible
240 for this function to fail if the cipher does not have any ASN1 support
241 or the parameters cannot be set (for example the RC2 effective key
242 length is not supported.
243
244 EVP_CIPHER_CTX_ctrl() allows various cipher specific parameters to be
245 determined and set.
246
247 EVP_CIPHER_CTX_rand_key() generates a random key of the appropriate
248 length based on the cipher context. The EVP_CIPHER can provide its own
249 random key generation routine to support keys of a specific form. Key
250 must point to a buffer at least as big as the value returned by
251 EVP_CIPHER_CTX_key_length().
252
254 EVP_CIPHER_CTX_new() returns a pointer to a newly created
255 EVP_CIPHER_CTX for success and NULL for failure.
256
257 EVP_EncryptInit_ex(), EVP_EncryptUpdate() and EVP_EncryptFinal_ex()
258 return 1 for success and 0 for failure.
259
260 EVP_DecryptInit_ex() and EVP_DecryptUpdate() return 1 for success and 0
261 for failure. EVP_DecryptFinal_ex() returns 0 if the decrypt failed or
262 1 for success.
263
264 EVP_CipherInit_ex() and EVP_CipherUpdate() return 1 for success and 0
265 for failure. EVP_CipherFinal_ex() returns 0 for a decryption failure
266 or 1 for success.
267
268 EVP_CIPHER_CTX_reset() returns 1 for success and 0 for failure.
269
270 EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
271 return an EVP_CIPHER structure or NULL on error.
272
273 EVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return a NID.
274
275 EVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the
276 block size.
277
278 EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
279 length.
280
281 EVP_CIPHER_CTX_set_padding() always returns 1.
282
283 EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
284 length or zero if the cipher does not use an IV.
285
286 EVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the NID of the
287 cipher's OBJECT IDENTIFIER or NID_undef if it has no defined OBJECT
288 IDENTIFIER.
289
290 EVP_CIPHER_CTX_cipher() returns an EVP_CIPHER structure.
291
292 EVP_CIPHER_param_to_asn1() and EVP_CIPHER_asn1_to_param() return
293 greater than zero for success and zero or a negative number on failure.
294
295 EVP_CIPHER_CTX_rand_key() returns 1 for success.
296
298 All algorithms have a fixed key length unless otherwise stated.
299
300 Refer to "SEE ALSO" for the full list of ciphers available through the
301 EVP interface.
302
303 EVP_enc_null()
304 Null cipher: does nothing.
305
307 The EVP interface for Authenticated Encryption with Associated Data
308 (AEAD) modes are subtly altered and several additional ctrl operations
309 are supported depending on the mode specified.
310
311 To specify additional authenticated data (AAD), a call to
312 EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() should
313 be made with the output parameter out set to NULL.
314
315 When decrypting, the return value of EVP_DecryptFinal() or
316 EVP_CipherFinal() indicates whether the operation was successful. If it
317 does not indicate success, the authentication operation has failed and
318 any output data MUST NOT be used as it is corrupted.
319
320 GCM and OCB Modes
321 The following ctrls are supported in GCM and OCB modes.
322
323 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
324 Sets the IV length. This call can only be made before specifying an
325 IV. If not called a default IV length is used.
326
327 For GCM AES and OCB AES the default is 12 (i.e. 96 bits). For OCB
328 mode the maximum is 15.
329
330 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)
331 Writes "taglen" bytes of the tag value to the buffer indicated by
332 "tag". This call can only be made when encrypting data and after
333 all data has been processed (e.g. after an EVP_EncryptFinal()
334 call).
335
336 For OCB, "taglen" must either be 16 or the value previously set via
337 EVP_CTRL_AEAD_SET_TAG.
338
339 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
340 Sets the expected tag to "taglen" bytes from "tag". The tag length
341 can only be set before specifying an IV. "taglen" must be between
342 1 and 16 inclusive.
343
344 For GCM, this call is only valid when decrypting data.
345
346 For OCB, this call is valid when decrypting data to set the
347 expected tag, and before encryption to set the desired tag length.
348
349 In OCB mode, calling this before encryption with "tag" set to
350 "NULL" sets the tag length. If this is not called prior to
351 encryption, a default tag length is used.
352
353 For OCB AES, the default tag length is 16 (i.e. 128 bits). It is
354 also the maximum tag length for OCB.
355
356 CCM Mode
357 The EVP interface for CCM mode is similar to that of the GCM mode but
358 with a few additional requirements and different ctrl values.
359
360 For CCM mode, the total plaintext or ciphertext length MUST be passed
361 to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with
362 the output and input parameters (in and out) set to NULL and the length
363 passed in the inl parameter.
364
365 The following ctrls are supported in CCM mode.
366
367 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
368 This call is made to set the expected CCM tag value when decrypting
369 or the length of the tag (with the "tag" parameter set to NULL)
370 when encrypting. The tag length is often referred to as M. If not
371 set a default value is used (12 for AES). When decrypting, the tag
372 needs to be set before passing in data to be decrypted, but as in
373 GCM and OCB mode, it can be set after passing additional
374 authenticated data (see "AEAD Interface").
375
376 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, ivlen, NULL)
377 Sets the CCM L value. If not set a default is used (8 for AES).
378
379 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
380 Sets the CCM nonce (IV) length. This call can only be made before
381 specifying an nonce value. The nonce length is given by 15 - L so
382 it is 7 by default for AES.
383
384 ChaCha20-Poly1305
385 The following ctrls are supported for the ChaCha20-Poly1305 AEAD
386 algorithm.
387
388 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
389 Sets the nonce length. This call can only be made before specifying
390 the nonce. If not called a default nonce length of 12 (i.e. 96
391 bits) is used. The maximum nonce length is 12 bytes (i.e. 96-bits).
392 If a nonce of less than 12 bytes is set then the nonce is
393 automatically padded with leading 0 bytes to make it 12 bytes in
394 length.
395
396 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)
397 Writes "taglen" bytes of the tag value to the buffer indicated by
398 "tag". This call can only be made when encrypting data and after
399 all data has been processed (e.g. after an EVP_EncryptFinal()
400 call).
401
402 "taglen" specified here must be 16 (POLY1305_BLOCK_SIZE, i.e.
403 128-bits) or less.
404
405 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
406 Sets the expected tag to "taglen" bytes from "tag". The tag length
407 can only be set before specifying an IV. "taglen" must be between
408 1 and 16 (POLY1305_BLOCK_SIZE) inclusive. This call is only valid
409 when decrypting data.
410
412 Where possible the EVP interface to symmetric ciphers should be used in
413 preference to the low level interfaces. This is because the code then
414 becomes transparent to the cipher used and much more flexible.
415 Additionally, the EVP interface will ensure the use of platform
416 specific cryptographic acceleration such as AES-NI (the low level
417 interfaces do not provide the guarantee).
418
419 PKCS padding works by adding n padding bytes of value n to make the
420 total length of the encrypted data a multiple of the block size.
421 Padding is always added so if the data is already a multiple of the
422 block size n will equal the block size. For example if the block size
423 is 8 and 11 bytes are to be encrypted then 5 padding bytes of value 5
424 will be added.
425
426 When decrypting the final block is checked to see if it has the correct
427 form.
428
429 Although the decryption operation can produce an error if padding is
430 enabled, it is not a strong test that the input data or key is correct.
431 A random block has better than 1 in 256 chance of being of the correct
432 format and problems with the input data earlier on will not produce a
433 final decrypt error.
434
435 If padding is disabled then the decryption operation will always
436 succeed if the total amount of data decrypted is a multiple of the
437 block size.
438
439 The functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(),
440 EVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained
441 for compatibility with existing code. New code should use
442 EVP_EncryptInit_ex(), EVP_EncryptFinal_ex(), EVP_DecryptInit_ex(),
443 EVP_DecryptFinal_ex(), EVP_CipherInit_ex() and EVP_CipherFinal_ex()
444 because they can reuse an existing context without allocating and
445 freeing it up on each call.
446
447 EVP_get_cipherbynid(), and EVP_get_cipherbyobj() are implemented as
448 macros.
449
451 EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal
452 ciphers with default key lengths. If custom ciphers exceed these values
453 the results are unpredictable. This is because it has become standard
454 practice to define a generic key as a fixed unsigned char array
455 containing EVP_MAX_KEY_LENGTH bytes.
456
457 The ASN1 code is incomplete (and sometimes inaccurate) it has only been
458 tested for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC
459 mode.
460
462 Encrypt a string using IDEA:
463
464 int do_crypt(char *outfile)
465 {
466 unsigned char outbuf[1024];
467 int outlen, tmplen;
468 /*
469 * Bogus key and IV: we'd normally set these from
470 * another source.
471 */
472 unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
473 unsigned char iv[] = {1,2,3,4,5,6,7,8};
474 char intext[] = "Some Crypto Text";
475 EVP_CIPHER_CTX *ctx;
476 FILE *out;
477
478 ctx = EVP_CIPHER_CTX_new();
479 EVP_EncryptInit_ex(ctx, EVP_idea_cbc(), NULL, key, iv);
480
481 if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) {
482 /* Error */
483 EVP_CIPHER_CTX_free(ctx);
484 return 0;
485 }
486 /*
487 * Buffer passed to EVP_EncryptFinal() must be after data just
488 * encrypted to avoid overwriting it.
489 */
490 if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) {
491 /* Error */
492 EVP_CIPHER_CTX_free(ctx);
493 return 0;
494 }
495 outlen += tmplen;
496 EVP_CIPHER_CTX_free(ctx);
497 /*
498 * Need binary mode for fopen because encrypted data is
499 * binary data. Also cannot use strlen() on it because
500 * it won't be NUL terminated and may contain embedded
501 * NULs.
502 */
503 out = fopen(outfile, "wb");
504 if (out == NULL) {
505 /* Error */
506 return 0;
507 }
508 fwrite(outbuf, 1, outlen, out);
509 fclose(out);
510 return 1;
511 }
512
513 The ciphertext from the above example can be decrypted using the
514 openssl utility with the command line (shown on two lines for clarity):
515
516 openssl idea -d \
517 -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 <filename
518
519 General encryption and decryption function example using FILE I/O and
520 AES128 with a 128-bit key:
521
522 int do_crypt(FILE *in, FILE *out, int do_encrypt)
523 {
524 /* Allow enough space in output buffer for additional block */
525 unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
526 int inlen, outlen;
527 EVP_CIPHER_CTX *ctx;
528 /*
529 * Bogus key and IV: we'd normally set these from
530 * another source.
531 */
532 unsigned char key[] = "0123456789abcdeF";
533 unsigned char iv[] = "1234567887654321";
534
535 /* Don't set key or IV right away; we want to check lengths */
536 ctx = EVP_CIPHER_CTX_new();
537 EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, NULL, NULL,
538 do_encrypt);
539 OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16);
540 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == 16);
541
542 /* Now we can set key and IV */
543 EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt);
544
545 for (;;) {
546 inlen = fread(inbuf, 1, 1024, in);
547 if (inlen <= 0)
548 break;
549 if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
550 /* Error */
551 EVP_CIPHER_CTX_free(ctx);
552 return 0;
553 }
554 fwrite(outbuf, 1, outlen, out);
555 }
556 if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
557 /* Error */
558 EVP_CIPHER_CTX_free(ctx);
559 return 0;
560 }
561 fwrite(outbuf, 1, outlen, out);
562
563 EVP_CIPHER_CTX_free(ctx);
564 return 1;
565 }
566
568 evp(7)
569
570 Supported ciphers are listed in:
571
572 EVP_aes(3), EVP_aria(3), EVP_bf(3), EVP_camellia(3), EVP_cast5(3),
573 EVP_chacha20(3), EVP_des(3), EVP_desx(3), EVP_idea(3), EVP_rc2(3),
574 EVP_rc4(3), EVP_rc5(3), EVP_seed(3), EVP_sm4(3)
575
577 Support for OCB mode was added in OpenSSL 1.1.0.
578
579 EVP_CIPHER_CTX was made opaque in OpenSSL 1.1.0. As a result,
580 EVP_CIPHER_CTX_reset() appeared and EVP_CIPHER_CTX_cleanup()
581 disappeared. EVP_CIPHER_CTX_init() remains as an alias for
582 EVP_CIPHER_CTX_reset().
583
585 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
586
587 Licensed under the OpenSSL license (the "License"). You may not use
588 this file except in compliance with the License. You can obtain a copy
589 in the file LICENSE in the source distribution or at
590 <https://www.openssl.org/source/license.html>.
591
592
593
5941.1.1d 2019-10-03 EVP_ENCRYPTINIT(3)