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