Commit manual pages after running 'man-update' and add new manual pages.
[dragonfly.git] / secure / lib / libcrypto / man / EVP_EncryptInit.3
1 .rn '' }`
2 ''' $RCSfile$$Revision$$Date$
3 '''
4 ''' $Log$
5 '''
6 .de Sh
7 .br
8 .if t .Sp
9 .ne 5
10 .PP
11 \fB\\$1\fR
12 .PP
13 ..
14 .de Sp
15 .if t .sp .5v
16 .if n .sp
17 ..
18 .de Ip
19 .br
20 .ie \\n(.$>=3 .ne \\$3
21 .el .ne 3
22 .IP "\\$1" \\$2
23 ..
24 .de Vb
25 .ft CW
26 .nf
27 .ne \\$1
28 ..
29 .de Ve
30 .ft R
31
32 .fi
33 ..
34 '''
35 '''
36 '''     Set up \*(-- to give an unbreakable dash;
37 '''     string Tr holds user defined translation string.
38 '''     Bell System Logo is used as a dummy character.
39 '''
40 .tr \(*W-|\(bv\*(Tr
41 .ie n \{\
42 .ds -- \(*W-
43 .ds PI pi
44 .if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
45 .if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
46 .ds L" ""
47 .ds R" ""
48 '''   \*(M", \*(S", \*(N" and \*(T" are the equivalent of
49 '''   \*(L" and \*(R", except that they are used on ".xx" lines,
50 '''   such as .IP and .SH, which do another additional levels of
51 '''   double-quote interpretation
52 .ds M" """
53 .ds S" """
54 .ds N" """""
55 .ds T" """""
56 .ds L' '
57 .ds R' '
58 .ds M' '
59 .ds S' '
60 .ds N' '
61 .ds T' '
62 'br\}
63 .el\{\
64 .ds -- \(em\|
65 .tr \*(Tr
66 .ds L" ``
67 .ds R" ''
68 .ds M" ``
69 .ds S" ''
70 .ds N" ``
71 .ds T" ''
72 .ds L' `
73 .ds R' '
74 .ds M' `
75 .ds S' '
76 .ds N' `
77 .ds T' '
78 .ds PI \(*p
79 'br\}
80 .\"     If the F register is turned on, we'll generate
81 .\"     index entries out stderr for the following things:
82 .\"             TH      Title 
83 .\"             SH      Header
84 .\"             Sh      Subsection 
85 .\"             Ip      Item
86 .\"             X<>     Xref  (embedded
87 .\"     Of course, you have to process the output yourself
88 .\"     in some meaninful fashion.
89 .if \nF \{
90 .de IX
91 .tm Index:\\$1\t\\n%\t"\\$2"
92 ..
93 .nr % 0
94 .rr F
95 .\}
96 .TH EVP_EncryptInit 3 "0.9.7d" "2/Sep/2004" "OpenSSL"
97 .UC
98 .if n .hy 0
99 .if n .na
100 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
101 .de CQ          \" put $1 in typewriter font
102 .ft CW
103 'if n "\c
104 'if t \\&\\$1\c
105 'if n \\&\\$1\c
106 'if n \&"
107 \\&\\$2 \\$3 \\$4 \\$5 \\$6 \\$7
108 '.ft R
109 ..
110 .\" @(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2
111 .       \" AM - accent mark definitions
112 .bd B 3
113 .       \" fudge factors for nroff and troff
114 .if n \{\
115 .       ds #H 0
116 .       ds #V .8m
117 .       ds #F .3m
118 .       ds #[ \f1
119 .       ds #] \fP
120 .\}
121 .if t \{\
122 .       ds #H ((1u-(\\\\n(.fu%2u))*.13m)
123 .       ds #V .6m
124 .       ds #F 0
125 .       ds #[ \&
126 .       ds #] \&
127 .\}
128 .       \" simple accents for nroff and troff
129 .if n \{\
130 .       ds ' \&
131 .       ds ` \&
132 .       ds ^ \&
133 .       ds , \&
134 .       ds ~ ~
135 .       ds ? ?
136 .       ds ! !
137 .       ds /
138 .       ds q
139 .\}
140 .if t \{\
141 .       ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
142 .       ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
143 .       ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
144 .       ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
145 .       ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
146 .       ds ? \s-2c\h'-\w'c'u*7/10'\u\h'\*(#H'\zi\d\s+2\h'\w'c'u*8/10'
147 .       ds ! \s-2\(or\s+2\h'-\w'\(or'u'\v'-.8m'.\v'.8m'
148 .       ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
149 .       ds q o\h'-\w'o'u*8/10'\s-4\v'.4m'\z\(*i\v'-.4m'\s+4\h'\w'o'u*8/10'
150 .\}
151 .       \" troff and (daisy-wheel) nroff accents
152 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
153 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
154 .ds v \\k:\h'-(\\n(.wu*9/10-\*(#H)'\v'-\*(#V'\*(#[\s-4v\s0\v'\*(#V'\h'|\\n:u'\*(#]
155 .ds _ \\k:\h'-(\\n(.wu*9/10-\*(#H+(\*(#F*2/3))'\v'-.4m'\z\(hy\v'.4m'\h'|\\n:u'
156 .ds . \\k:\h'-(\\n(.wu*8/10)'\v'\*(#V*4/10'\z.\v'-\*(#V*4/10'\h'|\\n:u'
157 .ds 3 \*(#[\v'.2m'\s-2\&3\s0\v'-.2m'\*(#]
158 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
159 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
160 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
161 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
162 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
163 .ds ae a\h'-(\w'a'u*4/10)'e
164 .ds Ae A\h'-(\w'A'u*4/10)'E
165 .ds oe o\h'-(\w'o'u*4/10)'e
166 .ds Oe O\h'-(\w'O'u*4/10)'E
167 .       \" corrections for vroff
168 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
169 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
170 .       \" for low resolution devices (crt and lpr)
171 .if \n(.H>23 .if \n(.V>19 \
172 \{\
173 .       ds : e
174 .       ds 8 ss
175 .       ds v \h'-1'\o'\(aa\(ga'
176 .       ds _ \h'-1'^
177 .       ds . \h'-1'.
178 .       ds 3 3
179 .       ds o a
180 .       ds d- d\h'-1'\(ga
181 .       ds D- D\h'-1'\(hy
182 .       ds th \o'bp'
183 .       ds Th \o'LP'
184 .       ds ae ae
185 .       ds Ae AE
186 .       ds oe oe
187 .       ds Oe OE
188 .\}
189 .rm #[ #] #H #V #F C
190 .SH "NAME"
191 EVP_CIPHER_CTX_init, EVP_EncryptInit_ex, EVP_EncryptUpdate,
192 EVP_EncryptFinal_ex, EVP_DecryptInit_ex, EVP_DecryptUpdate,
193 EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherUpdate,
194 EVP_CipherFinal_ex, EVP_CIPHER_CTX_set_key_length,
195 EVP_CIPHER_CTX_ctrl, EVP_CIPHER_CTX_cleanup, EVP_EncryptInit,
196 EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal,
197 EVP_CipherInit, EVP_CipherFinal, EVP_get_cipherbyname,
198 EVP_get_cipherbynid, EVP_get_cipherbyobj, EVP_CIPHER_nid,
199 EVP_CIPHER_block_size, EVP_CIPHER_key_length, EVP_CIPHER_iv_length,
200 EVP_CIPHER_flags, EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher,
201 EVP_CIPHER_CTX_nid, EVP_CIPHER_CTX_block_size, EVP_CIPHER_CTX_key_length,
202 EVP_CIPHER_CTX_iv_length, EVP_CIPHER_CTX_get_app_data,
203 EVP_CIPHER_CTX_set_app_data, EVP_CIPHER_CTX_type, EVP_CIPHER_CTX_flags,
204 EVP_CIPHER_CTX_mode, EVP_CIPHER_param_to_asn1, EVP_CIPHER_asn1_to_param,
205 EVP_CIPHER_CTX_set_padding \- EVP cipher routines
206 .SH "SYNOPSIS"
207 .PP
208 .Vb 1
209 \& #include <openssl/evp.h>
210 .Ve
211 .Vb 1
212 \& int EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a);
213 .Ve
214 .Vb 6
215 \& int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
216 \&         ENGINE *impl, unsigned char *key, unsigned char *iv);
217 \& int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
218 \&         int *outl, unsigned char *in, int inl);
219 \& int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out,
220 \&         int *outl);
221 .Ve
222 .Vb 6
223 \& int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
224 \&         ENGINE *impl, unsigned char *key, unsigned char *iv);
225 \& int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
226 \&         int *outl, unsigned char *in, int inl);
227 \& int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm,
228 \&         int *outl);
229 .Ve
230 .Vb 6
231 \& int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
232 \&         ENGINE *impl, unsigned char *key, unsigned char *iv, int enc);
233 \& int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
234 \&         int *outl, unsigned char *in, int inl);
235 \& int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm,
236 \&         int *outl);
237 .Ve
238 .Vb 4
239 \& int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
240 \&         unsigned char *key, unsigned char *iv);
241 \& int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out,
242 \&         int *outl);
243 .Ve
244 .Vb 4
245 \& int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
246 \&         unsigned char *key, unsigned char *iv);
247 \& int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm,
248 \&         int *outl);
249 .Ve
250 .Vb 4
251 \& int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
252 \&         unsigned char *key, unsigned char *iv, int enc);
253 \& int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm,
254 \&         int *outl);
255 .Ve
256 .Vb 4
257 \& int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
258 \& int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
259 \& int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
260 \& int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a);
261 .Ve
262 .Vb 3
263 \& const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
264 \& #define EVP_get_cipherbynid(a) EVP_get_cipherbyname(OBJ_nid2sn(a))
265 \& #define EVP_get_cipherbyobj(a) EVP_get_cipherbynid(OBJ_obj2nid(a))
266 .Ve
267 .Vb 7
268 \& #define EVP_CIPHER_nid(e)              ((e)->nid)
269 \& #define EVP_CIPHER_block_size(e)       ((e)->block_size)
270 \& #define EVP_CIPHER_key_length(e)       ((e)->key_len)
271 \& #define EVP_CIPHER_iv_length(e)                ((e)->iv_len)
272 \& #define EVP_CIPHER_flags(e)            ((e)->flags)
273 \& #define EVP_CIPHER_mode(e)             ((e)->flags) & EVP_CIPH_MODE)
274 \& int EVP_CIPHER_type(const EVP_CIPHER *ctx);
275 .Ve
276 .Vb 10
277 \& #define EVP_CIPHER_CTX_cipher(e)       ((e)->cipher)
278 \& #define EVP_CIPHER_CTX_nid(e)          ((e)->cipher->nid)
279 \& #define EVP_CIPHER_CTX_block_size(e)   ((e)->cipher->block_size)
280 \& #define EVP_CIPHER_CTX_key_length(e)   ((e)->key_len)
281 \& #define EVP_CIPHER_CTX_iv_length(e)    ((e)->cipher->iv_len)
282 \& #define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data)
283 \& #define EVP_CIPHER_CTX_set_app_data(e,d) ((e)->app_data=(char *)(d))
284 \& #define EVP_CIPHER_CTX_type(c)         EVP_CIPHER_type(EVP_CIPHER_CTX_cipher(c))
285 \& #define EVP_CIPHER_CTX_flags(e)                ((e)->cipher->flags)
286 \& #define EVP_CIPHER_CTX_mode(e)         ((e)->cipher->flags & EVP_CIPH_MODE)
287 .Ve
288 .Vb 2
289 \& int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
290 \& int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
291 .Ve
292 .SH "DESCRIPTION"
293 The EVP cipher routines are a high level interface to certain
294 symmetric ciphers.
295 .PP
296 \fIEVP_CIPHER_CTX_init()\fR initializes cipher contex \fBctx\fR.
297 .PP
298 \fIEVP_EncryptInit_ex()\fR sets up cipher context \fBctx\fR for encryption
299 with cipher \fBtype\fR from ENGINE \fBimpl\fR. \fBctx\fR must be initialized
300 before calling this function. \fBtype\fR is normally supplied
301 by a function such as \fIEVP_des_cbc()\fR. If \fBimpl\fR is NULL then the
302 default implementation is used. \fBkey\fR is the symmetric key to use
303 and \fBiv\fR is the IV to use (if necessary), the actual number of bytes
304 used for the key and IV depends on the cipher. It is possible to set
305 all parameters to NULL except \fBtype\fR in an initial call and supply
306 the remaining parameters in subsequent calls, all of which have \fBtype\fR
307 set to NULL. This is done when the default cipher parameters are not
308 appropriate.
309 .PP
310 \fIEVP_EncryptUpdate()\fR encrypts \fBinl\fR bytes from the buffer \fBin\fR and
311 writes the encrypted version to \fBout\fR. This function can be called
312 multiple times to encrypt successive blocks of data. The amount
313 of data written depends on the block alignment of the encrypted data:
314 as a result the amount of data written may be anything from zero bytes
315 to (inl + cipher_block_size \- 1) so \fBoutl\fR should contain sufficient
316 room. The actual number of bytes written is placed in \fBoutl\fR.
317 .PP
318 If padding is enabled (the default) then \fIEVP_EncryptFinal_ex()\fR encrypts
319 the \*(L"final\*(R" data, that is any data that remains in a partial block.
320 It uses standard block padding (aka PKCS padding). The encrypted
321 final data is written to \fBout\fR which should have sufficient space for
322 one cipher block. The number of bytes written is placed in \fBoutl\fR. After
323 this function is called the encryption operation is finished and no further
324 calls to \fIEVP_EncryptUpdate()\fR should be made.
325 .PP
326 If padding is disabled then \fIEVP_EncryptFinal_ex()\fR will not encrypt any more
327 data and it will return an error if any data remains in a partial block:
328 that is if the total data length is not a multiple of the block size. 
329 .PP
330 \fIEVP_DecryptInit_ex()\fR, \fIEVP_DecryptUpdate()\fR and \fIEVP_DecryptFinal_ex()\fR are the
331 corresponding decryption operations. \fIEVP_DecryptFinal()\fR will return an
332 error code if padding is enabled and the final block is not correctly
333 formatted. The parameters and restrictions are identical to the encryption
334 operations except that if padding is enabled the decrypted data buffer \fBout\fR
335 passed to \fIEVP_DecryptUpdate()\fR should have sufficient room for
336 (\fBinl\fR + cipher_block_size) bytes unless the cipher block size is 1 in
337 which case \fBinl\fR bytes is sufficient.
338 .PP
339 \fIEVP_CipherInit_ex()\fR, \fIEVP_CipherUpdate()\fR and \fIEVP_CipherFinal_ex()\fR are
340 functions that can be used for decryption or encryption. The operation
341 performed depends on the value of the \fBenc\fR parameter. It should be set
342 to 1 for encryption, 0 for decryption and \-1 to leave the value unchanged
343 (the actual value of \*(L'enc\*(R' being supplied in a previous call).
344 .PP
345 \fIEVP_CIPHER_CTX_cleanup()\fR clears all information from a cipher context
346 and free up any allocated memory associate with it. It should be called
347 after all operations using a cipher are complete so sensitive information
348 does not remain in memory.
349 .PP
350 \fIEVP_EncryptInit()\fR, \fIEVP_DecryptInit()\fR and \fIEVP_CipherInit()\fR behave in a
351 similar way to \fIEVP_EncryptInit_ex()\fR, EVP_DecryptInit_ex and
352 \fIEVP_CipherInit_ex()\fR except the \fBctx\fR paramter does not need to be
353 initialized and they always use the default cipher implementation.
354 .PP
355 \fIEVP_EncryptFinal()\fR, \fIEVP_DecryptFinal()\fR and \fIEVP_CipherFinal()\fR behave in a
356 similar way to \fIEVP_EncryptFinal_ex()\fR, \fIEVP_DecryptFinal_ex()\fR and
357 \fIEVP_CipherFinal_ex()\fR except \fBctx\fR is automatically cleaned up 
358 after the call.
359 .PP
360 \fIEVP_get_cipherbyname()\fR, \fIEVP_get_cipherbynid()\fR and \fIEVP_get_cipherbyobj()\fR
361 return an EVP_CIPHER structure when passed a cipher name, a NID or an
362 ASN1_OBJECT structure.
363 .PP
364 \fIEVP_CIPHER_nid()\fR and \fIEVP_CIPHER_CTX_nid()\fR return the NID of a cipher when
365 passed an \fBEVP_CIPHER\fR or \fBEVP_CIPHER_CTX\fR structure.  The actual NID
366 value is an internal value which may not have a corresponding OBJECT
367 IDENTIFIER.
368 .PP
369 \fIEVP_CIPHER_CTX_set_padding()\fR enables or disables padding. By default
370 encryption operations are padded using standard block padding and the
371 padding is checked and removed when decrypting. If the \fBpad\fR parameter
372 is zero then no padding is performed, the total amount of data encrypted
373 or decrypted must then be a multiple of the block size or an error will
374 occur.
375 .PP
376 \fIEVP_CIPHER_key_length()\fR and \fIEVP_CIPHER_CTX_key_length()\fR return the key
377 length of a cipher when passed an \fBEVP_CIPHER\fR or \fBEVP_CIPHER_CTX\fR
378 structure. The constant \fBEVP_MAX_KEY_LENGTH\fR is the maximum key length
379 for all ciphers. Note: although \fIEVP_CIPHER_key_length()\fR is fixed for a
380 given cipher, the value of \fIEVP_CIPHER_CTX_key_length()\fR may be different
381 for variable key length ciphers.
382 .PP
383 \fIEVP_CIPHER_CTX_set_key_length()\fR sets the key length of the cipher ctx.
384 If the cipher is a fixed length cipher then attempting to set the key
385 length to any value other than the fixed value is an error.
386 .PP
387 \fIEVP_CIPHER_iv_length()\fR and \fIEVP_CIPHER_CTX_iv_length()\fR return the IV
388 length of a cipher when passed an \fBEVP_CIPHER\fR or \fBEVP_CIPHER_CTX\fR.
389 It will return zero if the cipher does not use an IV.  The constant
390 \fBEVP_MAX_IV_LENGTH\fR is the maximum IV length for all ciphers.
391 .PP
392 \fIEVP_CIPHER_block_size()\fR and \fIEVP_CIPHER_CTX_block_size()\fR return the block
393 size of a cipher when passed an \fBEVP_CIPHER\fR or \fBEVP_CIPHER_CTX\fR
394 structure. The constant \fBEVP_MAX_IV_LENGTH\fR is also the maximum block
395 length for all ciphers.
396 .PP
397 \fIEVP_CIPHER_type()\fR and \fIEVP_CIPHER_CTX_type()\fR return the type of the passed
398 cipher or context. This \*(L"type\*(R" is the actual NID of the cipher OBJECT
399 IDENTIFIER as such it ignores the cipher parameters and 40 bit RC2 and
400 128 bit RC2 have the same NID. If the cipher does not have an object
401 identifier or does not have ASN1 support this function will return
402 \fBNID_undef\fR.
403 .PP
404 \fIEVP_CIPHER_CTX_cipher()\fR returns the \fBEVP_CIPHER\fR structure when passed
405 an \fBEVP_CIPHER_CTX\fR structure.
406 .PP
407 \fIEVP_CIPHER_mode()\fR and \fIEVP_CIPHER_CTX_mode()\fR return the block cipher mode:
408 EVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE or
409 EVP_CIPH_OFB_MODE. If the cipher is a stream cipher then
410 EVP_CIPH_STREAM_CIPHER is returned.
411 .PP
412 \fIEVP_CIPHER_param_to_asn1()\fR sets the AlgorithmIdentifier \*(L"parameter\*(R" based
413 on the passed cipher. This will typically include any parameters and an
414 IV. The cipher IV (if any) must be set when this call is made. This call
415 should be made before the cipher is actually \*(L"used\*(R" (before any
416 \fIEVP_EncryptUpdate()\fR, \fIEVP_DecryptUpdate()\fR calls for example). This function
417 may fail if the cipher does not have any ASN1 support.
418 .PP
419 \fIEVP_CIPHER_asn1_to_param()\fR sets the cipher parameters based on an ASN1
420 AlgorithmIdentifier \*(L"parameter\*(R". The precise effect depends on the cipher
421 In the case of RC2, for example, it will set the IV and effective key length.
422 This function should be called after the base cipher type is set but before
423 the key is set. For example \fIEVP_CipherInit()\fR will be called with the IV and
424 key set to NULL, \fIEVP_CIPHER_asn1_to_param()\fR will be called and finally
425 \fIEVP_CipherInit()\fR again with all parameters except the key set to NULL. It is
426 possible for this function to fail if the cipher does not have any ASN1 support
427 or the parameters cannot be set (for example the RC2 effective key length
428 is not supported.
429 .PP
430 \fIEVP_CIPHER_CTX_ctrl()\fR allows various cipher specific parameters to be determined
431 and set. Currently only the RC2 effective key length and the number of rounds of
432 RC5 can be set.
433 .SH "RETURN VALUES"
434 EVP_CIPHER_CTX_init, \fIEVP_EncryptInit_ex()\fR, \fIEVP_EncryptUpdate()\fR and
435 \fIEVP_EncryptFinal_ex()\fR return 1 for success and 0 for failure.
436 .PP
437 \fIEVP_DecryptInit_ex()\fR and \fIEVP_DecryptUpdate()\fR return 1 for success and 0 for failure.
438 \fIEVP_DecryptFinal_ex()\fR returns 0 if the decrypt failed or 1 for success.
439 .PP
440 \fIEVP_CipherInit_ex()\fR and \fIEVP_CipherUpdate()\fR return 1 for success and 0 for failure.
441 \fIEVP_CipherFinal_ex()\fR returns 0 for a decryption failure or 1 for success.
442 .PP
443 \fIEVP_CIPHER_CTX_cleanup()\fR returns 1 for success and 0 for failure.
444 .PP
445 \fIEVP_get_cipherbyname()\fR, \fIEVP_get_cipherbynid()\fR and \fIEVP_get_cipherbyobj()\fR
446 return an \fBEVP_CIPHER\fR structure or NULL on error.
447 .PP
448 \fIEVP_CIPHER_nid()\fR and \fIEVP_CIPHER_CTX_nid()\fR return a NID.
449 .PP
450 \fIEVP_CIPHER_block_size()\fR and \fIEVP_CIPHER_CTX_block_size()\fR return the block
451 size.
452 .PP
453 \fIEVP_CIPHER_key_length()\fR and \fIEVP_CIPHER_CTX_key_length()\fR return the key
454 length.
455 .PP
456 \fIEVP_CIPHER_CTX_set_padding()\fR always returns 1.
457 .PP
458 \fIEVP_CIPHER_iv_length()\fR and \fIEVP_CIPHER_CTX_iv_length()\fR return the IV
459 length or zero if the cipher does not use an IV.
460 .PP
461 \fIEVP_CIPHER_type()\fR and \fIEVP_CIPHER_CTX_type()\fR return the NID of the cipher's
462 OBJECT IDENTIFIER or NID_undef if it has no defined OBJECT IDENTIFIER.
463 .PP
464 \fIEVP_CIPHER_CTX_cipher()\fR returns an \fBEVP_CIPHER\fR structure.
465 .PP
466 \fIEVP_CIPHER_param_to_asn1()\fR and \fIEVP_CIPHER_asn1_to_param()\fR return 1 for 
467 success or zero for failure.
468 .SH "CIPHER LISTING"
469 All algorithms have a fixed key length unless otherwise stated.
470 .Ip "EVP_enc_null()" 4
471 Null cipher: does nothing.
472 .Ip "EVP_des_cbc(void), EVP_des_ecb(void), EVP_des_cfb(void), EVP_des_ofb(void)" 4
473 \s-1DES\s0 in \s-1CBC\s0, \s-1ECB\s0, \s-1CFB\s0 and \s-1OFB\s0 modes respectively. 
474 .Ip "EVP_des_ede_cbc(void), EVP_des_ede(), EVP_des_ede_ofb(void),  EVP_des_ede_cfb(void)" 4
475 Two key triple \s-1DES\s0 in \s-1CBC\s0, \s-1ECB\s0, \s-1CFB\s0 and \s-1OFB\s0 modes respectively.
476 .Ip "EVP_des_ede3_cbc(void), EVP_des_ede3(), EVP_des_ede3_ofb(void),  EVP_des_ede3_cfb(void)" 4
477 Three key triple \s-1DES\s0 in \s-1CBC\s0, \s-1ECB\s0, \s-1CFB\s0 and \s-1OFB\s0 modes respectively.
478 .Ip "EVP_desx_cbc(void)" 4
479 \s-1DESX\s0 algorithm in \s-1CBC\s0 mode.
480 .Ip "EVP_rc4(void)" 4
481 \s-1RC4\s0 stream cipher. This is a variable key length cipher with default key length 128 bits.
482 .Ip "EVP_rc4_40(void)" 4
483 \s-1RC4\s0 stream cipher with 40 bit key length. This is obsolete and new code should use \fIEVP_rc4()\fR
484 and the \fIEVP_CIPHER_CTX_set_key_length()\fR function.
485 .Ip "EVP_idea_cbc() EVP_idea_ecb(void), EVP_idea_cfb(void), EVP_idea_ofb(void), EVP_idea_cbc(void)" 4
486 \s-1IDEA\s0 encryption algorithm in \s-1CBC\s0, \s-1ECB\s0, \s-1CFB\s0 and \s-1OFB\s0 modes respectively.
487 .Ip "EVP_rc2_cbc(void), EVP_rc2_ecb(void), EVP_rc2_cfb(void), EVP_rc2_ofb(void)" 4
488 \s-1RC2\s0 encryption algorithm in \s-1CBC\s0, \s-1ECB\s0, \s-1CFB\s0 and \s-1OFB\s0 modes respectively. This is a variable key
489 length cipher with an additional parameter called \*(L"effective key bits\*(R" or \*(L"effective key length\*(R".
490 By default both are set to 128 bits.
491 .Ip "EVP_rc2_40_cbc(void), EVP_rc2_64_cbc(void)" 4
492 \s-1RC2\s0 algorithm in \s-1CBC\s0 mode with a default key length and effective key length of 40 and 64 bits.
493 These are obsolete and new code should use \fIEVP_rc2_cbc()\fR, \fIEVP_CIPHER_CTX_set_key_length()\fR and
494 \fIEVP_CIPHER_CTX_ctrl()\fR to set the key length and effective key length.
495 .Ip "EVP_bf_cbc(void), EVP_bf_ecb(void), EVP_bf_cfb(void), EVP_bf_ofb(void);" 4
496 Blowfish encryption algorithm in \s-1CBC\s0, \s-1ECB\s0, \s-1CFB\s0 and \s-1OFB\s0 modes respectively. This is a variable key
497 length cipher.
498 .Ip "EVP_cast5_cbc(void), EVP_cast5_ecb(void), EVP_cast5_cfb(void), EVP_cast5_ofb(void)" 4
499 \s-1CAST\s0 encryption algorithm in \s-1CBC\s0, \s-1ECB\s0, \s-1CFB\s0 and \s-1OFB\s0 modes respectively. This is a variable key
500 length cipher.
501 .Ip "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)" 4
502 \s-1RC5\s0 encryption algorithm in \s-1CBC\s0, \s-1ECB\s0, \s-1CFB\s0 and \s-1OFB\s0 modes respectively. This is a variable key length
503 cipher with an additional \*(L"number of rounds\*(R" parameter. By default the key length is set to 128
504 bits and 12 rounds.
505 .SH "NOTES"
506 Where possible the \fBEVP\fR interface to symmetric ciphers should be used in
507 preference to the low level interfaces. This is because the code then becomes
508 transparent to the cipher used and much more flexible.
509 .PP
510 PKCS padding works by adding \fBn\fR padding bytes of value \fBn\fR to make the total 
511 length of the encrypted data a multiple of the block size. Padding is always
512 added so if the data is already a multiple of the block size \fBn\fR will equal
513 the block size. For example if the block size is 8 and 11 bytes are to be
514 encrypted then 5 padding bytes of value 5 will be added.
515 .PP
516 When decrypting the final block is checked to see if it has the correct form.
517 .PP
518 Although the decryption operation can produce an error if padding is enabled,
519 it is not a strong test that the input data or key is correct. A random block
520 has better than 1 in 256 chance of being of the correct format and problems with
521 the input data earlier on will not produce a final decrypt error.
522 .PP
523 If padding is disabled then the decryption operation will always succeed if
524 the total amount of data decrypted is a multiple of the block size.
525 .PP
526 The functions \fIEVP_EncryptInit()\fR, \fIEVP_EncryptFinal()\fR, \fIEVP_DecryptInit()\fR,
527 \fIEVP_CipherInit()\fR and \fIEVP_CipherFinal()\fR are obsolete but are retained for
528 compatibility with existing code. New code should use \fIEVP_EncryptInit_ex()\fR,
529 \fIEVP_EncryptFinal_ex()\fR, \fIEVP_DecryptInit_ex()\fR, \fIEVP_DecryptFinal_ex()\fR,
530 \fIEVP_CipherInit_ex()\fR and \fIEVP_CipherFinal_ex()\fR because they can reuse an
531 existing context without allocating and freeing it up on each call.
532 .SH "BUGS"
533 For RC5 the number of rounds can currently only be set to 8, 12 or 16. This is
534 a limitation of the current RC5 code rather than the EVP interface.
535 .PP
536 EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with
537 default key lengths. If custom ciphers exceed these values the results are
538 unpredictable. This is because it has become standard practice to define a 
539 generic key as a fixed unsigned char array containing EVP_MAX_KEY_LENGTH bytes.
540 .PP
541 The ASN1 code is incomplete (and sometimes inaccurate) it has only been tested
542 for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode.
543 .SH "EXAMPLES"
544 Get the number of rounds used in RC5:
545 .PP
546 .Vb 2
547 \& int nrounds;
548 \& EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC5_ROUNDS, 0, &nrounds);
549 .Ve
550 Get the RC2 effective key length:
551 .PP
552 .Vb 2
553 \& int key_bits;
554 \& EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC2_KEY_BITS, 0, &key_bits);
555 .Ve
556 Set the number of rounds used in RC5:
557 .PP
558 .Vb 2
559 \& int nrounds;
560 \& EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC5_ROUNDS, nrounds, NULL);
561 .Ve
562 Set the effective key length used in RC2:
563 .PP
564 .Vb 2
565 \& int key_bits;
566 \& EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
567 .Ve
568 Encrypt a string using blowfish:
569 .PP
570 .Vb 14
571 \& int do_crypt(char *outfile)
572 \&        {
573 \&        unsigned char outbuf[1024];
574 \&        int outlen, tmplen;
575 \&        /* Bogus key and IV: we'd normally set these from
576 \&         * another source.
577 \&         */
578 \&        unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
579 \&        unsigned char iv[] = {1,2,3,4,5,6,7,8};
580 \&        char intext[] = "Some Crypto Text";
581 \&        EVP_CIPHER_CTX ctx;
582 \&        FILE *out;
583 \&        EVP_CIPHER_CTX_init(&ctx);
584 \&        EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv);
585 .Ve
586 .Vb 25
587 \&        if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext)))
588 \&                {
589 \&                /* Error */
590 \&                return 0;
591 \&                }
592 \&        /* Buffer passed to EVP_EncryptFinal() must be after data just
593 \&         * encrypted to avoid overwriting it.
594 \&         */
595 \&        if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen))
596 \&                {
597 \&                /* Error */
598 \&                return 0;
599 \&                }
600 \&        outlen += tmplen;
601 \&        EVP_CIPHER_CTX_cleanup(&ctx);
602 \&        /* Need binary mode for fopen because encrypted data is
603 \&         * binary data. Also cannot use strlen() on it because
604 \&         * it wont be null terminated and may contain embedded
605 \&         * nulls.
606 \&         */
607 \&        out = fopen(outfile, "wb");
608 \&        fwrite(outbuf, 1, outlen, out);
609 \&        fclose(out);
610 \&        return 1;
611 \&        }
612 .Ve
613 The ciphertext from the above example can be decrypted using the \fBopenssl\fR
614 utility with the command line:
615  
616  openssl\ bf\ \-in\ cipher.bin\ \-K\ 000102030405060708090A0B0C0D0E0F\ \-iv\ 0102030405060708\ \-d
617 .PP
618 General encryption, decryption function example using FILE I/O and RC2 with an
619 80 bit key:
620 .PP
621 .Vb 16
622 \& int do_crypt(FILE *in, FILE *out, int do_encrypt)
623 \&        {
624 \&        /* Allow enough space in output buffer for additional block */
625 \&        inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
626 \&        int inlen, outlen;
627 \&        /* Bogus key and IV: we'd normally set these from
628 \&         * another source.
629 \&         */
630 \&        unsigned char key[] = "0123456789";
631 \&        unsigned char iv[] = "12345678";
632 \&        /* Don't set key or IV because we will modify the parameters */
633 \&        EVP_CIPHER_CTX_init(&ctx);
634 \&        EVP_CipherInit_ex(&ctx, EVP_rc2(), NULL, NULL, NULL, do_encrypt);
635 \&        EVP_CIPHER_CTX_set_key_length(&ctx, 10);
636 \&        /* We finished modifying parameters so now we can set key and IV */
637 \&        EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
638 .Ve
639 .Vb 17
640 \&        for(;;) 
641 \&                {
642 \&                inlen = fread(inbuf, 1, 1024, in);
643 \&                if(inlen <= 0) break;
644 \&                if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen))
645 \&                        {
646 \&                        /* Error */
647 \&                        return 0;
648 \&                        }
649 \&                fwrite(outbuf, 1, outlen, out);
650 \&                }
651 \&        if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen))
652 \&                {
653 \&                /* Error */
654 \&                return 0;
655 \&                }
656 \&        fwrite(outbuf, 1, outlen, out);
657 .Ve
658 .Vb 3
659 \&        EVP_CIPHER_CTX_cleanup(&ctx);
660 \&        return 1;
661 \&        }
662 .Ve
663 .SH "SEE ALSO"
664 evp(3)
665 .SH "HISTORY"
666 \fIEVP_CIPHER_CTX_init()\fR, \fIEVP_EncryptInit_ex()\fR, \fIEVP_EncryptFinal_ex()\fR,
667 \fIEVP_DecryptInit_ex()\fR, \fIEVP_DecryptFinal_ex()\fR, \fIEVP_CipherInit_ex()\fR,
668 \fIEVP_CipherFinal_ex()\fR and \fIEVP_CIPHER_CTX_set_padding()\fR appeared in
669 OpenSSL 0.9.7.
670
671 .rn }` ''
672 .IX Title "EVP_EncryptInit 3"
673 .IX Name "EVP_CIPHER_CTX_init, EVP_EncryptInit_ex, EVP_EncryptUpdate,
674 EVP_EncryptFinal_ex, EVP_DecryptInit_ex, EVP_DecryptUpdate,
675 EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherUpdate,
676 EVP_CipherFinal_ex, EVP_CIPHER_CTX_set_key_length,
677 EVP_CIPHER_CTX_ctrl, EVP_CIPHER_CTX_cleanup, EVP_EncryptInit,
678 EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal,
679 EVP_CipherInit, EVP_CipherFinal, EVP_get_cipherbyname,
680 EVP_get_cipherbynid, EVP_get_cipherbyobj, EVP_CIPHER_nid,
681 EVP_CIPHER_block_size, EVP_CIPHER_key_length, EVP_CIPHER_iv_length,
682 EVP_CIPHER_flags, EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher,
683 EVP_CIPHER_CTX_nid, EVP_CIPHER_CTX_block_size, EVP_CIPHER_CTX_key_length,
684 EVP_CIPHER_CTX_iv_length, EVP_CIPHER_CTX_get_app_data,
685 EVP_CIPHER_CTX_set_app_data, EVP_CIPHER_CTX_type, EVP_CIPHER_CTX_flags,
686 EVP_CIPHER_CTX_mode, EVP_CIPHER_param_to_asn1, EVP_CIPHER_asn1_to_param,
687 EVP_CIPHER_CTX_set_padding - EVP cipher routines"
688
689 .IX Header "NAME"
690
691 .IX Header "SYNOPSIS"
692
693 .IX Header "DESCRIPTION"
694
695 .IX Header "RETURN VALUES"
696
697 .IX Header "CIPHER LISTING"
698
699 .IX Item "EVP_enc_null()"
700
701 .IX Item "EVP_des_cbc(void), EVP_des_ecb(void), EVP_des_cfb(void), EVP_des_ofb(void)"
702
703 .IX Item "EVP_des_ede_cbc(void), EVP_des_ede(), EVP_des_ede_ofb(void),  EVP_des_ede_cfb(void)"
704
705 .IX Item "EVP_des_ede3_cbc(void), EVP_des_ede3(), EVP_des_ede3_ofb(void),  EVP_des_ede3_cfb(void)"
706
707 .IX Item "EVP_desx_cbc(void)"
708
709 .IX Item "EVP_rc4(void)"
710
711 .IX Item "EVP_rc4_40(void)"
712
713 .IX Item "EVP_idea_cbc() EVP_idea_ecb(void), EVP_idea_cfb(void), EVP_idea_ofb(void), EVP_idea_cbc(void)"
714
715 .IX Item "EVP_rc2_cbc(void), EVP_rc2_ecb(void), EVP_rc2_cfb(void), EVP_rc2_ofb(void)"
716
717 .IX Item "EVP_rc2_40_cbc(void), EVP_rc2_64_cbc(void)"
718
719 .IX Item "EVP_bf_cbc(void), EVP_bf_ecb(void), EVP_bf_cfb(void), EVP_bf_ofb(void);"
720
721 .IX Item "EVP_cast5_cbc(void), EVP_cast5_ecb(void), EVP_cast5_cfb(void), EVP_cast5_ofb(void)"
722
723 .IX 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)"
724
725 .IX Header "NOTES"
726
727 .IX Header "BUGS"
728
729 .IX Header "EXAMPLES"
730
731 .IX Header "SEE ALSO"
732
733 .IX Header "HISTORY"
734