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).
372
373 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, ivlen, NULL)
374 Sets the CCM L value. If not set a default is used (8 for AES).
375
376 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
377 Sets the CCM nonce (IV) length. This call can only be made before
378 specifying an nonce value. The nonce length is given by 15 - L so
379 it is 7 by default for AES.
380
381 ChaCha20-Poly1305
382 The following ctrls are supported for the ChaCha20-Poly1305 AEAD
383 algorithm.
384
385 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
386 Sets the nonce length. This call can only be made before specifying
387 the nonce. If not called a default nonce length of 12 (i.e. 96
388 bits) is used. The maximum nonce length is 16 (CHACHA_CTR_SIZE,
389 i.e. 128-bits).
390
391 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)
392 Writes "taglen" bytes of the tag value to the buffer indicated by
393 "tag". This call can only be made when encrypting data and after
394 all data has been processed (e.g. after an EVP_EncryptFinal()
395 call).
396
397 "taglen" specified here must be 16 (POLY1305_BLOCK_SIZE, i.e.
398 128-bits) or less.
399
400 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
401 Sets the expected tag to "taglen" bytes from "tag". The tag length
402 can only be set before specifying an IV. "taglen" must be between
403 1 and 16 (POLY1305_BLOCK_SIZE) inclusive. This call is only valid
404 when decrypting data.
405
407 Where possible the EVP interface to symmetric ciphers should be used in
408 preference to the low level interfaces. This is because the code then
409 becomes transparent to the cipher used and much more flexible.
410 Additionally, the EVP interface will ensure the use of platform
411 specific cryptographic acceleration such as AES-NI (the low level
412 interfaces do not provide the guarantee).
413
414 PKCS padding works by adding n padding bytes of value n to make the
415 total length of the encrypted data a multiple of the block size.
416 Padding is always added so if the data is already a multiple of the
417 block size n will equal the block size. For example if the block size
418 is 8 and 11 bytes are to be encrypted then 5 padding bytes of value 5
419 will be added.
420
421 When decrypting the final block is checked to see if it has the correct
422 form.
423
424 Although the decryption operation can produce an error if padding is
425 enabled, it is not a strong test that the input data or key is correct.
426 A random block has better than 1 in 256 chance of being of the correct
427 format and problems with the input data earlier on will not produce a
428 final decrypt error.
429
430 If padding is disabled then the decryption operation will always
431 succeed if the total amount of data decrypted is a multiple of the
432 block size.
433
434 The functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(),
435 EVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained
436 for compatibility with existing code. New code should use
437 EVP_EncryptInit_ex(), EVP_EncryptFinal_ex(), EVP_DecryptInit_ex(),
438 EVP_DecryptFinal_ex(), EVP_CipherInit_ex() and EVP_CipherFinal_ex()
439 because they can reuse an existing context without allocating and
440 freeing it up on each call.
441
442 EVP_get_cipherbynid(), and EVP_get_cipherbyobj() are implemented as
443 macros.
444
446 EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal
447 ciphers with default key lengths. If custom ciphers exceed these values
448 the results are unpredictable. This is because it has become standard
449 practice to define a generic key as a fixed unsigned char array
450 containing EVP_MAX_KEY_LENGTH bytes.
451
452 The ASN1 code is incomplete (and sometimes inaccurate) it has only been
453 tested for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC
454 mode.
455
457 Encrypt a string using IDEA:
458
459 int do_crypt(char *outfile)
460 {
461 unsigned char outbuf[1024];
462 int outlen, tmplen;
463 /*
464 * Bogus key and IV: we'd normally set these from
465 * another source.
466 */
467 unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
468 unsigned char iv[] = {1,2,3,4,5,6,7,8};
469 char intext[] = "Some Crypto Text";
470 EVP_CIPHER_CTX *ctx;
471 FILE *out;
472
473 ctx = EVP_CIPHER_CTX_new();
474 EVP_EncryptInit_ex(ctx, EVP_idea_cbc(), NULL, key, iv);
475
476 if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) {
477 /* Error */
478 EVP_CIPHER_CTX_free(ctx);
479 return 0;
480 }
481 /*
482 * Buffer passed to EVP_EncryptFinal() must be after data just
483 * encrypted to avoid overwriting it.
484 */
485 if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) {
486 /* Error */
487 EVP_CIPHER_CTX_free(ctx);
488 return 0;
489 }
490 outlen += tmplen;
491 EVP_CIPHER_CTX_free(ctx);
492 /*
493 * Need binary mode for fopen because encrypted data is
494 * binary data. Also cannot use strlen() on it because
495 * it won't be NUL terminated and may contain embedded
496 * NULs.
497 */
498 out = fopen(outfile, "wb");
499 if (out == NULL) {
500 /* Error */
501 return 0;
502 }
503 fwrite(outbuf, 1, outlen, out);
504 fclose(out);
505 return 1;
506 }
507
508 The ciphertext from the above example can be decrypted using the
509 openssl utility with the command line (shown on two lines for clarity):
510
511 openssl idea -d \
512 -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 <filename
513
514 General encryption and decryption function example using FILE I/O and
515 AES128 with a 128-bit key:
516
517 int do_crypt(FILE *in, FILE *out, int do_encrypt)
518 {
519 /* Allow enough space in output buffer for additional block */
520 unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
521 int inlen, outlen;
522 EVP_CIPHER_CTX *ctx;
523 /*
524 * Bogus key and IV: we'd normally set these from
525 * another source.
526 */
527 unsigned char key[] = "0123456789abcdeF";
528 unsigned char iv[] = "1234567887654321";
529
530 /* Don't set key or IV right away; we want to check lengths */
531 ctx = EVP_CIPHER_CTX_new();
532 EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, NULL, NULL,
533 do_encrypt);
534 OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16);
535 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == 16);
536
537 /* Now we can set key and IV */
538 EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt);
539
540 for (;;) {
541 inlen = fread(inbuf, 1, 1024, in);
542 if (inlen <= 0)
543 break;
544 if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
545 /* Error */
546 EVP_CIPHER_CTX_free(ctx);
547 return 0;
548 }
549 fwrite(outbuf, 1, outlen, out);
550 }
551 if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
552 /* Error */
553 EVP_CIPHER_CTX_free(ctx);
554 return 0;
555 }
556 fwrite(outbuf, 1, outlen, out);
557
558 EVP_CIPHER_CTX_free(ctx);
559 return 1;
560 }
561
563 evp(7)
564
565 Supported ciphers are listed in:
566
567 EVP_aes(3), EVP_aria(3), EVP_bf(3), EVP_camellia(3), EVP_cast5(3),
568 EVP_chacha20(3), EVP_des(3), EVP_desx(3), EVP_idea(3), EVP_rc2(3),
569 EVP_rc4(3), EVP_rc5(3), EVP_seed(3), EVP_sm4(3)
570
572 Support for OCB mode was added in OpenSSL 1.1.0
573
574 EVP_CIPHER_CTX was made opaque in OpenSSL 1.1.0. As a result,
575 EVP_CIPHER_CTX_reset() appeared and EVP_CIPHER_CTX_cleanup()
576 disappeared. EVP_CIPHER_CTX_init() remains as an alias for
577 EVP_CIPHER_CTX_reset().
578
580 Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
581
582 Licensed under the OpenSSL license (the "License"). You may not use
583 this file except in compliance with the License. You can obtain a copy
584 in the file LICENSE in the source distribution or at
585 <https://www.openssl.org/source/license.html>.
586
587
588
5891.1.1 2018-09-11 EVP_ENCRYPTINIT(3)