EVP_EncryptInit.pod revision 279264
1=pod
2
3=head1 NAME
4
5EVP_CIPHER_CTX_init, EVP_EncryptInit_ex, EVP_EncryptUpdate,
6EVP_EncryptFinal_ex, EVP_DecryptInit_ex, EVP_DecryptUpdate,
7EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherUpdate,
8EVP_CipherFinal_ex, EVP_CIPHER_CTX_set_key_length,
9EVP_CIPHER_CTX_ctrl, EVP_CIPHER_CTX_cleanup, EVP_EncryptInit,
10EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal,
11EVP_CipherInit, EVP_CipherFinal, EVP_get_cipherbyname,
12EVP_get_cipherbynid, EVP_get_cipherbyobj, EVP_CIPHER_nid,
13EVP_CIPHER_block_size, EVP_CIPHER_key_length, EVP_CIPHER_iv_length,
14EVP_CIPHER_flags, EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher,
15EVP_CIPHER_CTX_nid, EVP_CIPHER_CTX_block_size, EVP_CIPHER_CTX_key_length,
16EVP_CIPHER_CTX_iv_length, EVP_CIPHER_CTX_get_app_data,
17EVP_CIPHER_CTX_set_app_data, EVP_CIPHER_CTX_type, EVP_CIPHER_CTX_flags,
18EVP_CIPHER_CTX_mode, EVP_CIPHER_param_to_asn1, EVP_CIPHER_asn1_to_param,
19EVP_CIPHER_CTX_set_padding - EVP cipher routines
20
21=head1 SYNOPSIS
22
23 #include <openssl/evp.h>
24
25 void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a);
26
27 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
28	 ENGINE *impl, unsigned char *key, unsigned char *iv);
29 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
30         int *outl, unsigned char *in, int inl);
31 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out,
32         int *outl);
33
34 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
35	 ENGINE *impl, unsigned char *key, unsigned char *iv);
36 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
37         int *outl, unsigned char *in, int inl);
38 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm,
39         int *outl);
40
41 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
42         ENGINE *impl, unsigned char *key, unsigned char *iv, int enc);
43 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
44         int *outl, unsigned char *in, int inl);
45 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm,
46         int *outl);
47
48 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
49         unsigned char *key, unsigned char *iv);
50 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out,
51         int *outl);
52
53 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
54         unsigned char *key, unsigned char *iv);
55 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm,
56         int *outl);
57
58 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
59         unsigned char *key, unsigned char *iv, int enc);
60 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm,
61         int *outl);
62
63 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
64 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
65 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
66 int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a);
67
68 const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
69 #define EVP_get_cipherbynid(a) EVP_get_cipherbyname(OBJ_nid2sn(a))
70 #define EVP_get_cipherbyobj(a) EVP_get_cipherbynid(OBJ_obj2nid(a))
71
72 #define EVP_CIPHER_nid(e)		((e)->nid)
73 #define EVP_CIPHER_block_size(e)	((e)->block_size)
74 #define EVP_CIPHER_key_length(e)	((e)->key_len)
75 #define EVP_CIPHER_iv_length(e)		((e)->iv_len)
76 #define EVP_CIPHER_flags(e)		((e)->flags)
77 #define EVP_CIPHER_mode(e)		((e)->flags) & EVP_CIPH_MODE)
78 int EVP_CIPHER_type(const EVP_CIPHER *ctx);
79
80 #define EVP_CIPHER_CTX_cipher(e)	((e)->cipher)
81 #define EVP_CIPHER_CTX_nid(e)		((e)->cipher->nid)
82 #define EVP_CIPHER_CTX_block_size(e)	((e)->cipher->block_size)
83 #define EVP_CIPHER_CTX_key_length(e)	((e)->key_len)
84 #define EVP_CIPHER_CTX_iv_length(e)	((e)->cipher->iv_len)
85 #define EVP_CIPHER_CTX_get_app_data(e)	((e)->app_data)
86 #define EVP_CIPHER_CTX_set_app_data(e,d) ((e)->app_data=(char *)(d))
87 #define EVP_CIPHER_CTX_type(c)         EVP_CIPHER_type(EVP_CIPHER_CTX_cipher(c))
88 #define EVP_CIPHER_CTX_flags(e)		((e)->cipher->flags)
89 #define EVP_CIPHER_CTX_mode(e)		((e)->cipher->flags & EVP_CIPH_MODE)
90
91 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
92 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
93
94=head1 DESCRIPTION
95
96The EVP cipher routines are a high level interface to certain
97symmetric ciphers.
98
99EVP_CIPHER_CTX_init() initializes cipher contex B<ctx>.
100
101EVP_EncryptInit_ex() sets up cipher context B<ctx> for encryption
102with cipher B<type> from ENGINE B<impl>. B<ctx> must be initialized
103before calling this function. B<type> is normally supplied
104by a function such as EVP_des_cbc(). If B<impl> is NULL then the
105default implementation is used. B<key> is the symmetric key to use
106and B<iv> is the IV to use (if necessary), the actual number of bytes
107used for the key and IV depends on the cipher. It is possible to set
108all parameters to NULL except B<type> in an initial call and supply
109the remaining parameters in subsequent calls, all of which have B<type>
110set to NULL. This is done when the default cipher parameters are not
111appropriate.
112
113EVP_EncryptUpdate() encrypts B<inl> bytes from the buffer B<in> and
114writes the encrypted version to B<out>. This function can be called
115multiple times to encrypt successive blocks of data. The amount
116of data written depends on the block alignment of the encrypted data:
117as a result the amount of data written may be anything from zero bytes
118to (inl + cipher_block_size - 1) so B<out> should contain sufficient
119room. The actual number of bytes written is placed in B<outl>.
120
121If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts
122the "final" data, that is any data that remains in a partial block.
123It uses L<standard block padding|/NOTES> (aka PKCS padding). The encrypted
124final data is written to B<out> which should have sufficient space for
125one cipher block. The number of bytes written is placed in B<outl>. After
126this function is called the encryption operation is finished and no further
127calls to EVP_EncryptUpdate() should be made.
128
129If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more
130data and it will return an error if any data remains in a partial block:
131that is if the total data length is not a multiple of the block size. 
132
133EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are the
134corresponding decryption operations. EVP_DecryptFinal() will return an
135error code if padding is enabled and the final block is not correctly
136formatted. The parameters and restrictions are identical to the encryption
137operations except that if padding is enabled the decrypted data buffer B<out>
138passed to EVP_DecryptUpdate() should have sufficient room for
139(B<inl> + cipher_block_size) bytes unless the cipher block size is 1 in
140which case B<inl> bytes is sufficient.
141
142EVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex() are
143functions that can be used for decryption or encryption. The operation
144performed depends on the value of the B<enc> parameter. It should be set
145to 1 for encryption, 0 for decryption and -1 to leave the value unchanged
146(the actual value of 'enc' being supplied in a previous call).
147
148EVP_CIPHER_CTX_cleanup() clears all information from a cipher context
149and free up any allocated memory associate with it. It should be called
150after all operations using a cipher are complete so sensitive information
151does not remain in memory.
152
153EVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit() behave in a
154similar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex and
155EVP_CipherInit_ex() except the B<ctx> parameter does not need to be
156initialized and they always use the default cipher implementation.
157
158EVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal() behave in a
159similar way to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and
160EVP_CipherFinal_ex() except B<ctx> is automatically cleaned up 
161after the call.
162
163EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
164return an EVP_CIPHER structure when passed a cipher name, a NID or an
165ASN1_OBJECT structure.
166
167EVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return the NID of a cipher when
168passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX> structure.  The actual NID
169value is an internal value which may not have a corresponding OBJECT
170IDENTIFIER.
171
172EVP_CIPHER_CTX_set_padding() enables or disables padding. By default
173encryption operations are padded using standard block padding and the
174padding is checked and removed when decrypting. If the B<pad> parameter
175is zero then no padding is performed, the total amount of data encrypted
176or decrypted must then be a multiple of the block size or an error will
177occur.
178
179EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
180length of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>
181structure. The constant B<EVP_MAX_KEY_LENGTH> is the maximum key length
182for all ciphers. Note: although EVP_CIPHER_key_length() is fixed for a
183given cipher, the value of EVP_CIPHER_CTX_key_length() may be different
184for variable key length ciphers.
185
186EVP_CIPHER_CTX_set_key_length() sets the key length of the cipher ctx.
187If the cipher is a fixed length cipher then attempting to set the key
188length to any value other than the fixed value is an error.
189
190EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
191length of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>.
192It will return zero if the cipher does not use an IV.  The constant
193B<EVP_MAX_IV_LENGTH> is the maximum IV length for all ciphers.
194
195EVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the block
196size of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>
197structure. The constant B<EVP_MAX_IV_LENGTH> is also the maximum block
198length for all ciphers.
199
200EVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the type of the passed
201cipher or context. This "type" is the actual NID of the cipher OBJECT
202IDENTIFIER as such it ignores the cipher parameters and 40 bit RC2 and
203128 bit RC2 have the same NID. If the cipher does not have an object
204identifier or does not have ASN1 support this function will return
205B<NID_undef>.
206
207EVP_CIPHER_CTX_cipher() returns the B<EVP_CIPHER> structure when passed
208an B<EVP_CIPHER_CTX> structure.
209
210EVP_CIPHER_mode() and EVP_CIPHER_CTX_mode() return the block cipher mode:
211EVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE or
212EVP_CIPH_OFB_MODE. If the cipher is a stream cipher then
213EVP_CIPH_STREAM_CIPHER is returned.
214
215EVP_CIPHER_param_to_asn1() sets the AlgorithmIdentifier "parameter" based
216on the passed cipher. This will typically include any parameters and an
217IV. The cipher IV (if any) must be set when this call is made. This call
218should be made before the cipher is actually "used" (before any
219EVP_EncryptUpdate(), EVP_DecryptUpdate() calls for example). This function
220may fail if the cipher does not have any ASN1 support.
221
222EVP_CIPHER_asn1_to_param() sets the cipher parameters based on an ASN1
223AlgorithmIdentifier "parameter". The precise effect depends on the cipher
224In the case of RC2, for example, it will set the IV and effective key length.
225This function should be called after the base cipher type is set but before
226the key is set. For example EVP_CipherInit() will be called with the IV and
227key set to NULL, EVP_CIPHER_asn1_to_param() will be called and finally
228EVP_CipherInit() again with all parameters except the key set to NULL. It is
229possible for this function to fail if the cipher does not have any ASN1 support
230or the parameters cannot be set (for example the RC2 effective key length
231is not supported.
232
233EVP_CIPHER_CTX_ctrl() allows various cipher specific parameters to be determined
234and set. Currently only the RC2 effective key length and the number of rounds of
235RC5 can be set.
236
237=head1 RETURN VALUES
238
239EVP_EncryptInit_ex(), EVP_EncryptUpdate() and EVP_EncryptFinal_ex()
240return 1 for success and 0 for failure.
241
242EVP_DecryptInit_ex() and EVP_DecryptUpdate() return 1 for success and 0 for failure.
243EVP_DecryptFinal_ex() returns 0 if the decrypt failed or 1 for success.
244
245EVP_CipherInit_ex() and EVP_CipherUpdate() return 1 for success and 0 for failure.
246EVP_CipherFinal_ex() returns 0 for a decryption failure or 1 for success.
247
248EVP_CIPHER_CTX_cleanup() returns 1 for success and 0 for failure.
249
250EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
251return an B<EVP_CIPHER> structure or NULL on error.
252
253EVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return a NID.
254
255EVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the block
256size.
257
258EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
259length.
260
261EVP_CIPHER_CTX_set_padding() always returns 1.
262
263EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
264length or zero if the cipher does not use an IV.
265
266EVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the NID of the cipher's
267OBJECT IDENTIFIER or NID_undef if it has no defined OBJECT IDENTIFIER.
268
269EVP_CIPHER_CTX_cipher() returns an B<EVP_CIPHER> structure.
270
271EVP_CIPHER_param_to_asn1() and EVP_CIPHER_asn1_to_param() return 1 for 
272success or zero for failure.
273
274=head1 CIPHER LISTING
275
276All algorithms have a fixed key length unless otherwise stated.
277
278=over 4
279
280=item EVP_enc_null()
281
282Null cipher: does nothing.
283
284=item EVP_des_cbc(void), EVP_des_ecb(void), EVP_des_cfb(void), EVP_des_ofb(void)
285
286DES in CBC, ECB, CFB and OFB modes respectively. 
287
288=item EVP_des_ede_cbc(void), EVP_des_ede(), EVP_des_ede_ofb(void),  EVP_des_ede_cfb(void)
289
290Two key triple DES in CBC, ECB, CFB and OFB modes respectively.
291
292=item EVP_des_ede3_cbc(void), EVP_des_ede3(), EVP_des_ede3_ofb(void),  EVP_des_ede3_cfb(void)
293
294Three key triple DES in CBC, ECB, CFB and OFB modes respectively.
295
296=item EVP_desx_cbc(void)
297
298DESX algorithm in CBC mode.
299
300=item EVP_rc4(void)
301
302RC4 stream cipher. This is a variable key length cipher with default key length 128 bits.
303
304=item EVP_rc4_40(void)
305
306RC4 stream cipher with 40 bit key length. This is obsolete and new code should use EVP_rc4()
307and the EVP_CIPHER_CTX_set_key_length() function.
308
309=item EVP_idea_cbc() EVP_idea_ecb(void), EVP_idea_cfb(void), EVP_idea_ofb(void), EVP_idea_cbc(void)
310
311IDEA encryption algorithm in CBC, ECB, CFB and OFB modes respectively.
312
313=item EVP_rc2_cbc(void), EVP_rc2_ecb(void), EVP_rc2_cfb(void), EVP_rc2_ofb(void)
314
315RC2 encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This is a variable key
316length cipher with an additional parameter called "effective key bits" or "effective key length".
317By default both are set to 128 bits.
318
319=item EVP_rc2_40_cbc(void), EVP_rc2_64_cbc(void)
320
321RC2 algorithm in CBC mode with a default key length and effective key length of 40 and 64 bits.
322These are obsolete and new code should use EVP_rc2_cbc(), EVP_CIPHER_CTX_set_key_length() and
323EVP_CIPHER_CTX_ctrl() to set the key length and effective key length.
324
325=item EVP_bf_cbc(void), EVP_bf_ecb(void), EVP_bf_cfb(void), EVP_bf_ofb(void);
326
327Blowfish encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This is a variable key
328length cipher.
329
330=item EVP_cast5_cbc(void), EVP_cast5_ecb(void), EVP_cast5_cfb(void), EVP_cast5_ofb(void)
331
332CAST encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This is a variable key
333length cipher.
334
335=item EVP_rc5_32_12_16_cbc(void), EVP_rc5_32_12_16_ecb(void), EVP_rc5_32_12_16_cfb(void), EVP_rc5_32_12_16_ofb(void)
336
337RC5 encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This is a variable key length
338cipher with an additional "number of rounds" parameter. By default the key length is set to 128
339bits and 12 rounds.
340
341=back
342
343=head1 NOTES
344
345Where possible the B<EVP> interface to symmetric ciphers should be used in
346preference to the low level interfaces. This is because the code then becomes
347transparent to the cipher used and much more flexible. Additionally, the
348B<EVP> interface will ensure the use of platform specific cryptographic
349acceleration such as AES-NI (the low level interfaces do not provide the
350guarantee).
351
352PKCS padding works by adding B<n> padding bytes of value B<n> to make the total 
353length of the encrypted data a multiple of the block size. Padding is always
354added so if the data is already a multiple of the block size B<n> will equal
355the block size. For example if the block size is 8 and 11 bytes are to be
356encrypted then 5 padding bytes of value 5 will be added.
357
358When decrypting the final block is checked to see if it has the correct form.
359
360Although the decryption operation can produce an error if padding is enabled,
361it is not a strong test that the input data or key is correct. A random block
362has better than 1 in 256 chance of being of the correct format and problems with
363the input data earlier on will not produce a final decrypt error.
364
365If padding is disabled then the decryption operation will always succeed if
366the total amount of data decrypted is a multiple of the block size.
367
368The functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(),
369EVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained for
370compatibility with existing code. New code should use EVP_EncryptInit_ex(),
371EVP_EncryptFinal_ex(), EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(),
372EVP_CipherInit_ex() and EVP_CipherFinal_ex() because they can reuse an
373existing context without allocating and freeing it up on each call.
374
375=head1 BUGS
376
377For RC5 the number of rounds can currently only be set to 8, 12 or 16. This is
378a limitation of the current RC5 code rather than the EVP interface.
379
380EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with
381default key lengths. If custom ciphers exceed these values the results are
382unpredictable. This is because it has become standard practice to define a 
383generic key as a fixed unsigned char array containing EVP_MAX_KEY_LENGTH bytes.
384
385The ASN1 code is incomplete (and sometimes inaccurate) it has only been tested
386for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode.
387
388=head1 EXAMPLES
389
390Encrypt a string using IDEA:
391
392 int do_crypt(char *outfile)
393 	{
394	unsigned char outbuf[1024];
395	int outlen, tmplen;
396	/* Bogus key and IV: we'd normally set these from
397	 * another source.
398	 */
399	unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
400	unsigned char iv[] = {1,2,3,4,5,6,7,8};
401	char intext[] = "Some Crypto Text";
402	EVP_CIPHER_CTX ctx;
403	FILE *out;
404
405	EVP_CIPHER_CTX_init(&ctx);
406	EVP_EncryptInit_ex(&ctx, EVP_idea_cbc(), NULL, key, iv);
407
408	if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext)))
409		{
410		/* Error */
411		return 0;
412		}
413	/* Buffer passed to EVP_EncryptFinal() must be after data just
414	 * encrypted to avoid overwriting it.
415	 */
416	if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen))
417		{
418		/* Error */
419		return 0;
420		}
421	outlen += tmplen;
422	EVP_CIPHER_CTX_cleanup(&ctx);
423	/* Need binary mode for fopen because encrypted data is
424	 * binary data. Also cannot use strlen() on it because
425         * it wont be null terminated and may contain embedded
426	 * nulls.
427	 */
428	out = fopen(outfile, "wb");
429	fwrite(outbuf, 1, outlen, out);
430	fclose(out);
431	return 1;
432	}
433
434The ciphertext from the above example can be decrypted using the B<openssl>
435utility with the command line (shown on two lines for clarity):
436 
437 openssl idea -d <filename
438          -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708
439
440General encryption and decryption function example using FILE I/O and AES128
441with a 128-bit key:
442
443 int do_crypt(FILE *in, FILE *out, int do_encrypt)
444 	{
445	/* Allow enough space in output buffer for additional block */
446	unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
447	int inlen, outlen;
448	EVP_CIPHER_CTX ctx;
449	/* Bogus key and IV: we'd normally set these from
450	 * another source.
451	 */
452	unsigned char key[] = "0123456789abcdeF";
453	unsigned char iv[] = "1234567887654321";
454
455	/* Don't set key or IV right away; we want to check lengths */
456	EVP_CIPHER_CTX_init(&ctx);
457	EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, NULL, NULL,
458		do_encrypt);
459	OPENSSL_assert(EVP_CIPHER_CTX_key_length(&ctx) == 16);
460	OPENSSL_assert(EVP_CIPHER_CTX_iv_length(&ctx) == 16);
461
462	/* Now we can set key and IV */
463	EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
464
465	for(;;) 
466		{
467		inlen = fread(inbuf, 1, 1024, in);
468		if(inlen <= 0) break;
469		if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen))
470			{
471			/* Error */
472			EVP_CIPHER_CTX_cleanup(&ctx);
473			return 0;
474			}
475		fwrite(outbuf, 1, outlen, out);
476		}
477	if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen))
478		{
479		/* Error */
480		EVP_CIPHER_CTX_cleanup(&ctx);
481		return 0;
482		}
483	fwrite(outbuf, 1, outlen, out);
484
485	EVP_CIPHER_CTX_cleanup(&ctx);
486	return 1;
487	}
488
489
490=head1 SEE ALSO
491
492L<evp(3)|evp(3)>
493
494=head1 HISTORY
495
496EVP_CIPHER_CTX_init(), EVP_EncryptInit_ex(), EVP_EncryptFinal_ex(),
497EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(), EVP_CipherInit_ex(),
498EVP_CipherFinal_ex() and EVP_CIPHER_CTX_set_padding() appeared in
499OpenSSL 0.9.7.
500
501IDEA appeared in OpenSSL 0.9.7 but was often disabled due to
502patent concerns; the last patents expired in 2012.
503
504=cut
505