blob: daf57e5895f1cdb4ed3e0dc15798f697ac995095 [file] [log] [blame]
Dr. Stephen Henson72b60352000-02-22 02:59:26 +00001=pod
2
3=head1 NAME
4
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +00005EVP_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
Dr. Stephen Henson72b60352000-02-22 02:59:26 +000020
21=head1 SYNOPSIS
22
23 #include <openssl/evp.h>
24
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +000025 int 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);
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +000029 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
Ulf Möller4d524e12000-02-24 11:55:57 +000030 int *outl, unsigned char *in, int inl);
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +000031 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);
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +000050 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out,
Ulf Möller4d524e12000-02-24 11:55:57 +000051 int *outl);
Dr. Stephen Henson72b60352000-02-22 02:59:26 +000052
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +000053 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
Ulf Möller4d524e12000-02-24 11:55:57 +000054 unsigned char *key, unsigned char *iv);
Ulf Möller4d524e12000-02-24 11:55:57 +000055 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm,
56 int *outl);
Dr. Stephen Henson72b60352000-02-22 02:59:26 +000057
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +000058 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
Ulf Möller4d524e12000-02-24 11:55:57 +000059 unsigned char *key, unsigned char *iv, int enc);
Ulf Möller4d524e12000-02-24 11:55:57 +000060 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm,
61 int *outl);
Dr. Stephen Henson72b60352000-02-22 02:59:26 +000062
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +000063 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +000064 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);
Dr. Stephen Henson72b60352000-02-22 02:59:26 +000067
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)
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +000075 #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)
Dr. Stephen Henson72b60352000-02-22 02:59:26 +000078 int EVP_CIPHER_type(const EVP_CIPHER *ctx);
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +000079
Dr. Stephen Henson72b60352000-02-22 02:59:26 +000080 #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)
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +000083 #define EVP_CIPHER_CTX_key_length(e) ((e)->key_len)
Dr. Stephen Henson72b60352000-02-22 02:59:26 +000084 #define EVP_CIPHER_CTX_iv_length(e) ((e)->cipher->iv_len)
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +000085 #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))
Dr. Stephen Henson72b60352000-02-22 02:59:26 +000087 #define EVP_CIPHER_CTX_type(c) EVP_CIPHER_type(EVP_CIPHER_CTX_cipher(c))
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +000088 #define EVP_CIPHER_CTX_flags(e) ((e)->cipher->flags)
89 #define EVP_CIPHER_CTX_mode(e) ((e)->cipher->flags & EVP_CIPH_MODE)
Dr. Stephen Henson72b60352000-02-22 02:59:26 +000090
Dr. Stephen Henson3f2b5a82000-02-22 14:16:23 +000091 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
Dr. Stephen Henson72b60352000-02-22 02:59:26 +000094=head1 DESCRIPTION
95
96The EVP cipher routines are a high level interface to certain
97symmetric ciphers.
98
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +000099EVP_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.
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000112
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<outl> should contain sufficient
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000119room. The actual number of bytes written is placed in B<outl>.
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000120
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000121If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000122the "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
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000129If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000130data 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.
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000132
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000133EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are the
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000134corresponding decryption operations. EVP_DecryptFinal() will return an
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000135error 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.
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000141
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000142EVP_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).
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000147
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000148EVP_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> paramter 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.
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000162
Dr. Stephen Henson3f2b5a82000-02-22 14:16:23 +0000163EVP_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
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000172EVP_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
Dr. Stephen Henson3f2b5a82000-02-22 14:16:23 +0000179EVP_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
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000182for 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.
Dr. Stephen Henson3f2b5a82000-02-22 14:16:23 +0000189
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
Dr. Stephen Henson41e68ef2000-02-23 14:27:47 +0000203128 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>.
Dr. Stephen Henson3f2b5a82000-02-22 14:16:23 +0000206
207EVP_CIPHER_CTX_cipher() returns the B<EVP_CIPHER> structure when passed
208an B<EVP_CIPHER_CTX> structure.
209
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000210EVP_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
Dr. Stephen Henson3f2b5a82000-02-22 14:16:23 +0000215EVP_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
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000231is 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.
Dr. Stephen Henson3f2b5a82000-02-22 14:16:23 +0000236
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000237=head1 RETURN VALUES
238
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000239EVP_CIPHER_CTX_init, EVP_EncryptInit_ex(), EVP_EncryptUpdate() and
240EVP_EncryptFinal_ex() return 1 for success and 0 for failure.
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000241
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000242EVP_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.
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000244
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000245EVP_CipherInit_ex() and EVP_CipherUpdate() return 1 for success and 0 for failure.
Richard Levitte21d5ed92002-05-08 15:20:38 +0000246EVP_CipherFinal_ex() returns 0 for a decryption failure or 1 for success.
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000247
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000248EVP_CIPHER_CTX_cleanup() returns 1 for success and 0 for failure.
Dr. Stephen Henson3f2b5a82000-02-22 14:16:23 +0000249
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
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000261EVP_CIPHER_CTX_set_padding() always returns 1.
262
Dr. Stephen Henson3f2b5a82000-02-22 14:16:23 +0000263EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
264length or zero if the cipher does not use an IV.
265
Dr. Stephen Henson41e68ef2000-02-23 14:27:47 +0000266EVP_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
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000274=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
Ulf Möllerc8973692000-09-20 03:24:36 +0000311IDEA encryption algorithm in CBC, ECB, CFB and OFB modes respectively.
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000312
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
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000343=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.
348
349PKCS padding works by adding B<n> padding bytes of value B<n> to make the total
350length of the encrypted data a multiple of the block size. Padding is always
351added so if the data is already a multiple of the block size B<n> will equal
352the block size. For example if the block size is 8 and 11 bytes are to be
353encrypted then 5 padding bytes of value 5 will be added.
354
355When decrypting the final block is checked to see if it has the correct form.
356
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000357Although the decryption operation can produce an error if padding is enabled,
358it is not a strong test that the input data or key is correct. A random block
359has better than 1 in 256 chance of being of the correct format and problems with
360the input data earlier on will not produce a final decrypt error.
361
362If padding is disabled then the decryption operation will always succeed if
363the total amount of data decrypted is a multiple of the block size.
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000364
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000365The functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(),
366EVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained for
367compatibility with existing code. New code should use EVP_EncryptInit_ex(),
368EVP_EncryptFinal_ex(), EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(),
369EVP_CipherInit_ex() and EVP_CipherFinal_ex() because they can reuse an
370existing context without allocating and freeing it up on each call.
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000371
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000372=head1 BUGS
373
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000374For RC5 the number of rounds can currently only be set to 8, 12 or 16. This is
375a limitation of the current RC5 code rather than the EVP interface.
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000376
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000377EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with
378default key lengths. If custom ciphers exceed these values the results are
379unpredictable. This is because it has become standard practice to define a
380generic key as a fixed unsigned char array containing EVP_MAX_KEY_LENGTH bytes.
381
Ulf Möllerc8973692000-09-20 03:24:36 +0000382The ASN1 code is incomplete (and sometimes inaccurate) it has only been tested
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000383for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode.
384
385=head1 EXAMPLES
386
387Get the number of rounds used in RC5:
388
389 int nrounds;
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000390 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC5_ROUNDS, 0, &nrounds);
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000391
392Get the RC2 effective key length:
393
394 int key_bits;
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000395 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC2_KEY_BITS, 0, &key_bits);
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000396
397Set the number of rounds used in RC5:
398
399 int nrounds;
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000400 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC5_ROUNDS, nrounds, NULL);
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000401
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000402Set the effective key length used in RC2:
Dr. Stephen Hensona91dedc2000-06-11 12:18:15 +0000403
Dr. Stephen Hensonf2e5ca82001-02-14 02:11:52 +0000404 int key_bits;
405 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000406
Dr. Stephen Henson18135562001-05-17 13:03:20 +0000407Encrypt 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;
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000421 EVP_CIPHER_CTX_init(&ctx);
Lutz Jänicke284b2162002-10-18 09:47:14 +0000422 EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv);
Dr. Stephen Henson18135562001-05-17 13:03:20 +0000423
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 */
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000432 if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen))
Dr. Stephen Henson18135562001-05-17 13:03:20 +0000433 {
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
450The ciphertext from the above example can be decrypted using the B<openssl>
451utility with the command line:
452
453 S<openssl bf -in cipher.bin -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 -d>
454
455General encryption, decryption function example using FILE I/O and RC2 with an
45680 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 */
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000469 EVP_CIPHER_CTX_init(&ctx);
470 EVP_CipherInit_ex(&ctx, EVP_rc2(), NULL, NULL, NULL, do_encrypt);
Dr. Stephen Henson18135562001-05-17 13:03:20 +0000471 EVP_CIPHER_CTX_set_key_length(&ctx, 10);
472 /* We finished modifying parameters so now we can set key and IV */
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000473 EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
Dr. Stephen Henson18135562001-05-17 13:03:20 +0000474
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 return 0;
483 }
484 fwrite(outbuf, 1, outlen, out);
485 }
Dr. Stephen Henson3811eed2001-10-17 01:50:32 +0000486 if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen))
Dr. Stephen Henson18135562001-05-17 13:03:20 +0000487 {
488 /* Error */
489 return 0;
490 }
491 fwrite(outbuf, 1, outlen, out);
492
493 EVP_CIPHER_CTX_cleanup(&ctx);
494 return 1;
495 }
496
497
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000498=head1 SEE ALSO
499
500L<evp(3)|evp(3)>
501
502=head1 HISTORY
503
Richard Levitte503f3b12002-07-18 18:54:46 +0000504EVP_CIPHER_CTX_init(), EVP_EncryptInit_ex(), EVP_EncryptFinal_ex(),
505EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(), EVP_CipherInit_ex(),
506EVP_CipherFinal_ex() and EVP_CIPHER_CTX_set_padding() appeared in
507OpenSSL 0.9.7.
508
Dr. Stephen Henson72b60352000-02-22 02:59:26 +0000509=cut