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