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