Commit manual pages after running 'man-update' and add new manual pages.
[dragonfly.git] / secure / lib / libcrypto / man / EVP_EncryptInit.3
CommitLineData
74dab6c2
JR
1.rn '' }`
2''' $RCSfile$$Revision$$Date$
3'''
4''' $Log$
5'''
6.de Sh
984263bc
MD
7.br
8.if t .Sp
9.ne 5
10.PP
11\fB\\$1\fR
12.PP
13..
74dab6c2 14.de Sp
984263bc
MD
15.if t .sp .5v
16.if n .sp
17..
74dab6c2 18.de Ip
984263bc
MD
19.br
20.ie \\n(.$>=3 .ne \\$3
21.el .ne 3
22.IP "\\$1" \\$2
23..
74dab6c2 24.de Vb
984263bc
MD
25.ft CW
26.nf
27.ne \\$1
28..
74dab6c2 29.de Ve
984263bc
MD
30.ft R
31
32.fi
33..
74dab6c2
JR
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'''
984263bc 40.tr \(*W-|\(bv\*(Tr
984263bc 41.ie n \{\
74dab6c2
JR
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' '
984263bc
MD
62'br\}
63.el\{\
74dab6c2
JR
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
984263bc 79'br\}
74dab6c2
JR
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"
984263bc 92..
74dab6c2
JR
93.nr % 0
94.rr F
984263bc 95.\}
74dab6c2
JR
96.TH EVP_EncryptInit 3 "0.9.7d" "2/Sep/2004" "OpenSSL"
97.UC
98.if n .hy 0
984263bc 99.if n .na
74dab6c2
JR
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
984263bc 112.bd B 3
74dab6c2 113. \" fudge factors for nroff and troff
984263bc 114.if n \{\
74dab6c2
JR
115. ds #H 0
116. ds #V .8m
117. ds #F .3m
118. ds #[ \f1
119. ds #] \fP
984263bc
MD
120.\}
121.if t \{\
74dab6c2
JR
122. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
123. ds #V .6m
124. ds #F 0
125. ds #[ \&
126. ds #] \&
984263bc 127.\}
74dab6c2 128. \" simple accents for nroff and troff
984263bc 129.if n \{\
74dab6c2
JR
130. ds ' \&
131. ds ` \&
132. ds ^ \&
133. ds , \&
134. ds ~ ~
135. ds ? ?
136. ds ! !
137. ds /
138. ds q
984263bc
MD
139.\}
140.if t \{\
74dab6c2
JR
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'
984263bc 150.\}
74dab6c2 151. \" troff and (daisy-wheel) nroff accents
984263bc
MD
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'
74dab6c2
JR
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'\*(#]
984263bc
MD
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
74dab6c2
JR
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
984263bc
MD
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'
74dab6c2 170. \" for low resolution devices (crt and lpr)
984263bc
MD
171.if \n(.H>23 .if \n(.V>19 \
172\{\
74dab6c2
JR
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
984263bc
MD
188.\}
189.rm #[ #] #H #V #F C
984263bc
MD
190.SH "NAME"
191EVP_CIPHER_CTX_init, EVP_EncryptInit_ex, EVP_EncryptUpdate,
192EVP_EncryptFinal_ex, EVP_DecryptInit_ex, EVP_DecryptUpdate,
193EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherUpdate,
194EVP_CipherFinal_ex, EVP_CIPHER_CTX_set_key_length,
195EVP_CIPHER_CTX_ctrl, EVP_CIPHER_CTX_cleanup, EVP_EncryptInit,
196EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal,
197EVP_CipherInit, EVP_CipherFinal, EVP_get_cipherbyname,
198EVP_get_cipherbynid, EVP_get_cipherbyobj, EVP_CIPHER_nid,
199EVP_CIPHER_block_size, EVP_CIPHER_key_length, EVP_CIPHER_iv_length,
200EVP_CIPHER_flags, EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher,
201EVP_CIPHER_CTX_nid, EVP_CIPHER_CTX_block_size, EVP_CIPHER_CTX_key_length,
202EVP_CIPHER_CTX_iv_length, EVP_CIPHER_CTX_get_app_data,
203EVP_CIPHER_CTX_set_app_data, EVP_CIPHER_CTX_type, EVP_CIPHER_CTX_flags,
204EVP_CIPHER_CTX_mode, EVP_CIPHER_param_to_asn1, EVP_CIPHER_asn1_to_param,
74dab6c2 205EVP_CIPHER_CTX_set_padding \- EVP cipher routines
984263bc 206.SH "SYNOPSIS"
74dab6c2 207.PP
984263bc
MD
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"
74dab6c2 293The EVP cipher routines are a high level interface to certain
984263bc
MD
294symmetric ciphers.
295.PP
74dab6c2 296\fIEVP_CIPHER_CTX_init()\fR initializes cipher contex \fBctx\fR.
984263bc 297.PP
74dab6c2
JR
298\fIEVP_EncryptInit_ex()\fR sets up cipher context \fBctx\fR for encryption
299with cipher \fBtype\fR from ENGINE \fBimpl\fR. \fBctx\fR must be initialized
984263bc 300before calling this function. \fBtype\fR is normally supplied
74dab6c2 301by a function such as \fIEVP_des_cbc()\fR. If \fBimpl\fR is NULL then the
984263bc 302default implementation is used. \fBkey\fR is the symmetric key to use
74dab6c2
JR
303and \fBiv\fR is the IV to use (if necessary), the actual number of bytes
304used for the key and IV depends on the cipher. It is possible to set
305all parameters to NULL except \fBtype\fR in an initial call and supply
984263bc 306the remaining parameters in subsequent calls, all of which have \fBtype\fR
74dab6c2 307set to NULL. This is done when the default cipher parameters are not
984263bc
MD
308appropriate.
309.PP
74dab6c2 310\fIEVP_EncryptUpdate()\fR encrypts \fBinl\fR bytes from the buffer \fBin\fR and
984263bc
MD
311writes the encrypted version to \fBout\fR. This function can be called
312multiple times to encrypt successive blocks of data. The amount
313of data written depends on the block alignment of the encrypted data:
314as a result the amount of data written may be anything from zero bytes
315to (inl + cipher_block_size \- 1) so \fBoutl\fR should contain sufficient
316room. The actual number of bytes written is placed in \fBoutl\fR.
317.PP
318If padding is enabled (the default) then \fIEVP_EncryptFinal_ex()\fR encrypts
319the \*(L"final\*(R" data, that is any data that remains in a partial block.
74dab6c2 320It uses standard block padding (aka PKCS padding). The encrypted
984263bc
MD
321final data is written to \fBout\fR which should have sufficient space for
322one cipher block. The number of bytes written is placed in \fBoutl\fR. After
323this function is called the encryption operation is finished and no further
324calls to \fIEVP_EncryptUpdate()\fR should be made.
325.PP
326If padding is disabled then \fIEVP_EncryptFinal_ex()\fR will not encrypt any more
327data and it will return an error if any data remains in a partial block:
328that is if the total data length is not a multiple of the block size.
329.PP
74dab6c2 330\fIEVP_DecryptInit_ex()\fR, \fIEVP_DecryptUpdate()\fR and \fIEVP_DecryptFinal_ex()\fR are the
984263bc
MD
331corresponding decryption operations. \fIEVP_DecryptFinal()\fR will return an
332error code if padding is enabled and the final block is not correctly
333formatted. The parameters and restrictions are identical to the encryption
334operations except that if padding is enabled the decrypted data buffer \fBout\fR
335passed to \fIEVP_DecryptUpdate()\fR should have sufficient room for
336(\fBinl\fR + cipher_block_size) bytes unless the cipher block size is 1 in
337which case \fBinl\fR bytes is sufficient.
338.PP
74dab6c2 339\fIEVP_CipherInit_ex()\fR, \fIEVP_CipherUpdate()\fR and \fIEVP_CipherFinal_ex()\fR are
984263bc
MD
340functions that can be used for decryption or encryption. The operation
341performed depends on the value of the \fBenc\fR parameter. It should be set
342to 1 for encryption, 0 for decryption and \-1 to leave the value unchanged
74dab6c2 343(the actual value of \*(L'enc\*(R' being supplied in a previous call).
984263bc 344.PP
74dab6c2 345\fIEVP_CIPHER_CTX_cleanup()\fR clears all information from a cipher context
984263bc
MD
346and free up any allocated memory associate with it. It should be called
347after all operations using a cipher are complete so sensitive information
348does not remain in memory.
349.PP
74dab6c2 350\fIEVP_EncryptInit()\fR, \fIEVP_DecryptInit()\fR and \fIEVP_CipherInit()\fR behave in a
984263bc 351similar way to \fIEVP_EncryptInit_ex()\fR, EVP_DecryptInit_ex and
74dab6c2 352\fIEVP_CipherInit_ex()\fR except the \fBctx\fR paramter does not need to be
984263bc
MD
353initialized and they always use the default cipher implementation.
354.PP
74dab6c2 355\fIEVP_EncryptFinal()\fR, \fIEVP_DecryptFinal()\fR and \fIEVP_CipherFinal()\fR behave in a
984263bc 356similar way to \fIEVP_EncryptFinal_ex()\fR, \fIEVP_DecryptFinal_ex()\fR and
74dab6c2 357\fIEVP_CipherFinal_ex()\fR except \fBctx\fR is automatically cleaned up
984263bc
MD
358after the call.
359.PP
74dab6c2
JR
360\fIEVP_get_cipherbyname()\fR, \fIEVP_get_cipherbynid()\fR and \fIEVP_get_cipherbyobj()\fR
361return an EVP_CIPHER structure when passed a cipher name, a NID or an
362ASN1_OBJECT structure.
984263bc 363.PP
74dab6c2
JR
364\fIEVP_CIPHER_nid()\fR and \fIEVP_CIPHER_CTX_nid()\fR return the NID of a cipher when
365passed an \fBEVP_CIPHER\fR or \fBEVP_CIPHER_CTX\fR structure. The actual NID
366value is an internal value which may not have a corresponding OBJECT
367IDENTIFIER.
984263bc 368.PP
74dab6c2 369\fIEVP_CIPHER_CTX_set_padding()\fR enables or disables padding. By default
984263bc
MD
370encryption operations are padded using standard block padding and the
371padding is checked and removed when decrypting. If the \fBpad\fR parameter
372is zero then no padding is performed, the total amount of data encrypted
373or decrypted must then be a multiple of the block size or an error will
374occur.
375.PP
74dab6c2
JR
376\fIEVP_CIPHER_key_length()\fR and \fIEVP_CIPHER_CTX_key_length()\fR return the key
377length of a cipher when passed an \fBEVP_CIPHER\fR or \fBEVP_CIPHER_CTX\fR
378structure. The constant \fBEVP_MAX_KEY_LENGTH\fR is the maximum key length
984263bc
MD
379for all ciphers. Note: although \fIEVP_CIPHER_key_length()\fR is fixed for a
380given cipher, the value of \fIEVP_CIPHER_CTX_key_length()\fR may be different
381for variable key length ciphers.
382.PP
74dab6c2 383\fIEVP_CIPHER_CTX_set_key_length()\fR sets the key length of the cipher ctx.
984263bc
MD
384If the cipher is a fixed length cipher then attempting to set the key
385length to any value other than the fixed value is an error.
386.PP
74dab6c2
JR
387\fIEVP_CIPHER_iv_length()\fR and \fIEVP_CIPHER_CTX_iv_length()\fR return the IV
388length of a cipher when passed an \fBEVP_CIPHER\fR or \fBEVP_CIPHER_CTX\fR.
389It 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.
984263bc 391.PP
74dab6c2
JR
392\fIEVP_CIPHER_block_size()\fR and \fIEVP_CIPHER_CTX_block_size()\fR return the block
393size of a cipher when passed an \fBEVP_CIPHER\fR or \fBEVP_CIPHER_CTX\fR
394structure. The constant \fBEVP_MAX_IV_LENGTH\fR is also the maximum block
984263bc
MD
395length for all ciphers.
396.PP
74dab6c2
JR
397\fIEVP_CIPHER_type()\fR and \fIEVP_CIPHER_CTX_type()\fR return the type of the passed
398cipher or context. This \*(L"type\*(R" is the actual NID of the cipher OBJECT
399IDENTIFIER as such it ignores the cipher parameters and 40 bit RC2 and
400128 bit RC2 have the same NID. If the cipher does not have an object
401identifier or does not have ASN1 support this function will return
402\fBNID_undef\fR.
984263bc 403.PP
74dab6c2
JR
404\fIEVP_CIPHER_CTX_cipher()\fR returns the \fBEVP_CIPHER\fR structure when passed
405an \fBEVP_CIPHER_CTX\fR structure.
984263bc 406.PP
74dab6c2
JR
407\fIEVP_CIPHER_mode()\fR and \fIEVP_CIPHER_CTX_mode()\fR return the block cipher mode:
408EVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE or
409EVP_CIPH_OFB_MODE. If the cipher is a stream cipher then
410EVP_CIPH_STREAM_CIPHER is returned.
984263bc 411.PP
74dab6c2 412\fIEVP_CIPHER_param_to_asn1()\fR sets the AlgorithmIdentifier \*(L"parameter\*(R" based
984263bc 413on the passed cipher. This will typically include any parameters and an
74dab6c2 414IV. The cipher IV (if any) must be set when this call is made. This call
984263bc 415should be made before the cipher is actually \*(L"used\*(R" (before any
74dab6c2
JR
416\fIEVP_EncryptUpdate()\fR, \fIEVP_DecryptUpdate()\fR calls for example). This function
417may fail if the cipher does not have any ASN1 support.
984263bc 418.PP
74dab6c2 419\fIEVP_CIPHER_asn1_to_param()\fR sets the cipher parameters based on an ASN1
984263bc 420AlgorithmIdentifier \*(L"parameter\*(R". The precise effect depends on the cipher
74dab6c2 421In the case of RC2, for example, it will set the IV and effective key length.
984263bc 422This function should be called after the base cipher type is set but before
74dab6c2
JR
423the key is set. For example \fIEVP_CipherInit()\fR will be called with the IV and
424key 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
426possible for this function to fail if the cipher does not have any ASN1 support
427or the parameters cannot be set (for example the RC2 effective key length
984263bc
MD
428is not supported.
429.PP
74dab6c2
JR
430\fIEVP_CIPHER_CTX_ctrl()\fR allows various cipher specific parameters to be determined
431and set. Currently only the RC2 effective key length and the number of rounds of
432RC5 can be set.
984263bc 433.SH "RETURN VALUES"
984263bc 434EVP_CIPHER_CTX_init, \fIEVP_EncryptInit_ex()\fR, \fIEVP_EncryptUpdate()\fR and
74dab6c2 435\fIEVP_EncryptFinal_ex()\fR return 1 for success and 0 for failure.
984263bc 436.PP
74dab6c2
JR
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.
984263bc 439.PP
74dab6c2
JR
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.
984263bc 442.PP
74dab6c2 443\fIEVP_CIPHER_CTX_cleanup()\fR returns 1 for success and 0 for failure.
984263bc 444.PP
74dab6c2
JR
445\fIEVP_get_cipherbyname()\fR, \fIEVP_get_cipherbynid()\fR and \fIEVP_get_cipherbyobj()\fR
446return an \fBEVP_CIPHER\fR structure or NULL on error.
984263bc 447.PP
74dab6c2 448\fIEVP_CIPHER_nid()\fR and \fIEVP_CIPHER_CTX_nid()\fR return a NID.
984263bc 449.PP
74dab6c2 450\fIEVP_CIPHER_block_size()\fR and \fIEVP_CIPHER_CTX_block_size()\fR return the block
984263bc
MD
451size.
452.PP
74dab6c2 453\fIEVP_CIPHER_key_length()\fR and \fIEVP_CIPHER_CTX_key_length()\fR return the key
984263bc
MD
454length.
455.PP
74dab6c2 456\fIEVP_CIPHER_CTX_set_padding()\fR always returns 1.
984263bc 457.PP
74dab6c2
JR
458\fIEVP_CIPHER_iv_length()\fR and \fIEVP_CIPHER_CTX_iv_length()\fR return the IV
459length or zero if the cipher does not use an IV.
984263bc 460.PP
74dab6c2
JR
461\fIEVP_CIPHER_type()\fR and \fIEVP_CIPHER_CTX_type()\fR return the NID of the cipher's
462OBJECT IDENTIFIER or NID_undef if it has no defined OBJECT IDENTIFIER.
984263bc 463.PP
74dab6c2 464\fIEVP_CIPHER_CTX_cipher()\fR returns an \fBEVP_CIPHER\fR structure.
984263bc 465.PP
74dab6c2 466\fIEVP_CIPHER_param_to_asn1()\fR and \fIEVP_CIPHER_asn1_to_param()\fR return 1 for
984263bc
MD
467success or zero for failure.
468.SH "CIPHER LISTING"
984263bc 469All algorithms have a fixed key length unless otherwise stated.
74dab6c2 470.Ip "EVP_enc_null()" 4
984263bc
MD
471Null cipher: does nothing.
472.Ip "EVP_des_cbc(void), EVP_des_ecb(void), EVP_des_cfb(void), EVP_des_ofb(void)" 4
74dab6c2
JR
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
984263bc 475Two key triple \s-1DES\s0 in \s-1CBC\s0, \s-1ECB\s0, \s-1CFB\s0 and \s-1OFB\s0 modes respectively.
74dab6c2 476.Ip "EVP_des_ede3_cbc(void), EVP_des_ede3(), EVP_des_ede3_ofb(void), EVP_des_ede3_cfb(void)" 4
984263bc
MD
477Three 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
74dab6c2 479\s-1DESX\s0 algorithm in \s-1CBC\s0 mode.
984263bc 480.Ip "EVP_rc4(void)" 4
74dab6c2 481\s-1RC4\s0 stream cipher. This is a variable key length cipher with default key length 128 bits.
984263bc 482.Ip "EVP_rc4_40(void)" 4
74dab6c2 483\s-1RC4\s0 stream cipher with 40 bit key length. This is obsolete and new code should use \fIEVP_rc4()\fR
984263bc 484and the \fIEVP_CIPHER_CTX_set_key_length()\fR function.
74dab6c2
JR
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.
984263bc 487.Ip "EVP_rc2_cbc(void), EVP_rc2_ecb(void), EVP_rc2_cfb(void), EVP_rc2_ofb(void)" 4
74dab6c2 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
984263bc
MD
489length cipher with an additional parameter called \*(L"effective key bits\*(R" or \*(L"effective key length\*(R".
490By default both are set to 128 bits.
491.Ip "EVP_rc2_40_cbc(void), EVP_rc2_64_cbc(void)" 4
74dab6c2 492\s-1RC2\s0 algorithm in \s-1CBC\s0 mode with a default key length and effective key length of 40 and 64 bits.
984263bc 493These are obsolete and new code should use \fIEVP_rc2_cbc()\fR, \fIEVP_CIPHER_CTX_set_key_length()\fR and
74dab6c2 494\fIEVP_CIPHER_CTX_ctrl()\fR to set the key length and effective key length.
984263bc 495.Ip "EVP_bf_cbc(void), EVP_bf_ecb(void), EVP_bf_cfb(void), EVP_bf_ofb(void);" 4
984263bc
MD
496Blowfish encryption algorithm in \s-1CBC\s0, \s-1ECB\s0, \s-1CFB\s0 and \s-1OFB\s0 modes respectively. This is a variable key
497length cipher.
498.Ip "EVP_cast5_cbc(void), EVP_cast5_ecb(void), EVP_cast5_cfb(void), EVP_cast5_ofb(void)" 4
74dab6c2 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
984263bc
MD
500length 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
74dab6c2 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
984263bc
MD
503cipher with an additional \*(L"number of rounds\*(R" parameter. By default the key length is set to 128
504bits and 12 rounds.
505.SH "NOTES"
74dab6c2 506Where possible the \fBEVP\fR interface to symmetric ciphers should be used in
984263bc
MD
507preference to the low level interfaces. This is because the code then becomes
508transparent to the cipher used and much more flexible.
509.PP
74dab6c2 510PKCS padding works by adding \fBn\fR padding bytes of value \fBn\fR to make the total
984263bc
MD
511length of the encrypted data a multiple of the block size. Padding is always
512added so if the data is already a multiple of the block size \fBn\fR will equal
513the block size. For example if the block size is 8 and 11 bytes are to be
514encrypted then 5 padding bytes of value 5 will be added.
515.PP
516When decrypting the final block is checked to see if it has the correct form.
517.PP
518Although the decryption operation can produce an error if padding is enabled,
519it is not a strong test that the input data or key is correct. A random block
520has better than 1 in 256 chance of being of the correct format and problems with
521the input data earlier on will not produce a final decrypt error.
522.PP
523If padding is disabled then the decryption operation will always succeed if
524the total amount of data decrypted is a multiple of the block size.
525.PP
526The functions \fIEVP_EncryptInit()\fR, \fIEVP_EncryptFinal()\fR, \fIEVP_DecryptInit()\fR,
74dab6c2 527\fIEVP_CipherInit()\fR and \fIEVP_CipherFinal()\fR are obsolete but are retained for
984263bc 528compatibility with existing code. New code should use \fIEVP_EncryptInit_ex()\fR,
74dab6c2
JR
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
984263bc
MD
531existing context without allocating and freeing it up on each call.
532.SH "BUGS"
74dab6c2
JR
533For RC5 the number of rounds can currently only be set to 8, 12 or 16. This is
534a limitation of the current RC5 code rather than the EVP interface.
984263bc 535.PP
74dab6c2 536EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with
984263bc
MD
537default key lengths. If custom ciphers exceed these values the results are
538unpredictable. This is because it has become standard practice to define a
74dab6c2 539generic key as a fixed unsigned char array containing EVP_MAX_KEY_LENGTH bytes.
984263bc 540.PP
74dab6c2
JR
541The ASN1 code is incomplete (and sometimes inaccurate) it has only been tested
542for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode.
984263bc 543.SH "EXAMPLES"
74dab6c2 544Get the number of rounds used in RC5:
984263bc
MD
545.PP
546.Vb 2
547\& int nrounds;
548\& EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC5_ROUNDS, 0, &nrounds);
549.Ve
74dab6c2 550Get the RC2 effective key length:
984263bc
MD
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
74dab6c2 556Set the number of rounds used in RC5:
984263bc
MD
557.PP
558.Vb 2
559\& int nrounds;
560\& EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC5_ROUNDS, nrounds, NULL);
561.Ve
74dab6c2 562Set the effective key length used in RC2:
984263bc
MD
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
568Encrypt 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
613The ciphertext from the above example can be decrypted using the \fBopenssl\fR
614utility with the command line:
74dab6c2
JR
615
616 openssl\ bf\ \-in\ cipher.bin\ \-K\ 000102030405060708090A0B0C0D0E0F\ \-iv\ 0102030405060708\ \-d
984263bc 617.PP
74dab6c2 618General encryption, decryption function example using FILE I/O and RC2 with an
984263bc
MD
61980 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"
984263bc
MD
664evp(3)
665.SH "HISTORY"
74dab6c2
JR
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
984263bc 669OpenSSL 0.9.7.
74dab6c2
JR
670
671.rn }` ''
672.IX Title "EVP_EncryptInit 3"
673.IX Name "EVP_CIPHER_CTX_init, EVP_EncryptInit_ex, EVP_EncryptUpdate,
674EVP_EncryptFinal_ex, EVP_DecryptInit_ex, EVP_DecryptUpdate,
675EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherUpdate,
676EVP_CipherFinal_ex, EVP_CIPHER_CTX_set_key_length,
677EVP_CIPHER_CTX_ctrl, EVP_CIPHER_CTX_cleanup, EVP_EncryptInit,
678EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal,
679EVP_CipherInit, EVP_CipherFinal, EVP_get_cipherbyname,
680EVP_get_cipherbynid, EVP_get_cipherbyobj, EVP_CIPHER_nid,
681EVP_CIPHER_block_size, EVP_CIPHER_key_length, EVP_CIPHER_iv_length,
682EVP_CIPHER_flags, EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher,
683EVP_CIPHER_CTX_nid, EVP_CIPHER_CTX_block_size, EVP_CIPHER_CTX_key_length,
684EVP_CIPHER_CTX_iv_length, EVP_CIPHER_CTX_get_app_data,
685EVP_CIPHER_CTX_set_app_data, EVP_CIPHER_CTX_type, EVP_CIPHER_CTX_flags,
686EVP_CIPHER_CTX_mode, EVP_CIPHER_param_to_asn1, EVP_CIPHER_asn1_to_param,
687EVP_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