]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - secure/lib/libcrypto/man/EVP_EncryptInit.3
Merge OpenSSL 1.1.1a.
[FreeBSD/FreeBSD.git] / secure / lib / libcrypto / man / EVP_EncryptInit.3
1 .\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
6 .if t .sp .5v
7 .if n .sp
8 ..
9 .de Vb \" Begin verbatim text
10 .ft CW
11 .nf
12 .ne \\$1
13 ..
14 .de Ve \" End verbatim text
15 .ft R
16 .fi
17 ..
18 .\" Set up some character translations and predefined strings.  \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
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-
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26 .ie n \{\
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' ""
35 'br\}
36 .el\{\
37 .    ds -- \|\(em\|
38 .    ds PI \(*p
39 .    ds L" ``
40 .    ds R" ''
41 .    ds C`
42 .    ds C'
43 'br\}
44 .\"
45 .\" Escape single quotes in literal strings from groff's Unicode transform.
46 .ie \n(.g .ds Aq \(aq
47 .el       .ds Aq '
48 .\"
49 .\" If the F register is >0, we'll generate index entries on stderr for
50 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
51 .\" entries marked with X<> in POD.  Of course, you'll have to process the
52 .\" output yourself in some meaningful fashion.
53 .\"
54 .\" Avoid warning from groff about undefined register 'F'.
55 .de IX
56 ..
57 .if !\nF .nr F 0
58 .if \nF>0 \{\
59 .    de IX
60 .    tm Index:\\$1\t\\n%\t"\\$2"
61 ..
62 .    if !\nF==2 \{\
63 .        nr % 0
64 .        nr F 2
65 .    \}
66 .\}
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101 .\}
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "EVP_ENCRYPTINIT 3"
132 .TH EVP_ENCRYPTINIT 3 "2018-11-20" "1.1.1a" "OpenSSL"
133 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
134 .\" way too many mistakes in technical documents.
135 .if n .ad l
136 .nh
137 .SH "NAME"
138 EVP_CIPHER_CTX_new, EVP_CIPHER_CTX_reset, EVP_CIPHER_CTX_free, EVP_EncryptInit_ex, EVP_EncryptUpdate, EVP_EncryptFinal_ex, EVP_DecryptInit_ex, EVP_DecryptUpdate, EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherUpdate, EVP_CipherFinal_ex, EVP_CIPHER_CTX_set_key_length, EVP_CIPHER_CTX_ctrl, EVP_EncryptInit, EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal, EVP_CipherInit, EVP_CipherFinal, EVP_get_cipherbyname, EVP_get_cipherbynid, EVP_get_cipherbyobj, EVP_CIPHER_nid, EVP_CIPHER_block_size, EVP_CIPHER_key_length, EVP_CIPHER_iv_length, EVP_CIPHER_flags, EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher, EVP_CIPHER_CTX_nid, EVP_CIPHER_CTX_block_size, EVP_CIPHER_CTX_key_length, EVP_CIPHER_CTX_iv_length, EVP_CIPHER_CTX_get_app_data, EVP_CIPHER_CTX_set_app_data, EVP_CIPHER_CTX_type, EVP_CIPHER_CTX_flags, EVP_CIPHER_CTX_mode, EVP_CIPHER_param_to_asn1, EVP_CIPHER_asn1_to_param, EVP_CIPHER_CTX_set_padding, EVP_enc_null \&\- EVP cipher routines
139 .SH "SYNOPSIS"
140 .IX Header "SYNOPSIS"
141 .Vb 1
142 \& #include <openssl/evp.h>
143 \&
144 \& EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void);
145 \& int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx);
146 \& void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx);
147 \&
148 \& int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
149 \&                        ENGINE *impl, const unsigned char *key, const unsigned char *iv);
150 \& int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
151 \&                       int *outl, const unsigned char *in, int inl);
152 \& int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
153 \&
154 \& int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
155 \&                        ENGINE *impl, const unsigned char *key, const unsigned char *iv);
156 \& int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
157 \&                       int *outl, const unsigned char *in, int inl);
158 \& int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
159 \&
160 \& int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
161 \&                       ENGINE *impl, const unsigned char *key, const unsigned char *iv, int enc);
162 \& int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
163 \&                      int *outl, const unsigned char *in, int inl);
164 \& int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
165 \&
166 \& int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
167 \&                     const unsigned char *key, const unsigned char *iv);
168 \& int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
169 \&
170 \& int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
171 \&                     const unsigned char *key, const unsigned char *iv);
172 \& int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
173 \&
174 \& int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
175 \&                    const unsigned char *key, const unsigned char *iv, int enc);
176 \& int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
177 \&
178 \& int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
179 \& int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
180 \& int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
181 \& int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key);
182 \&
183 \& const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
184 \& const EVP_CIPHER *EVP_get_cipherbynid(int nid);
185 \& const EVP_CIPHER *EVP_get_cipherbyobj(const ASN1_OBJECT *a);
186 \&
187 \& int EVP_CIPHER_nid(const EVP_CIPHER *e);
188 \& int EVP_CIPHER_block_size(const EVP_CIPHER *e);
189 \& int EVP_CIPHER_key_length(const EVP_CIPHER *e);
190 \& int EVP_CIPHER_iv_length(const EVP_CIPHER *e);
191 \& unsigned long EVP_CIPHER_flags(const EVP_CIPHER *e);
192 \& unsigned long EVP_CIPHER_mode(const EVP_CIPHER *e);
193 \& int EVP_CIPHER_type(const EVP_CIPHER *ctx);
194 \&
195 \& const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx);
196 \& int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx);
197 \& int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx);
198 \& int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx);
199 \& int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx);
200 \& void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx);
201 \& void EVP_CIPHER_CTX_set_app_data(const EVP_CIPHER_CTX *ctx, void *data);
202 \& int EVP_CIPHER_CTX_type(const EVP_CIPHER_CTX *ctx);
203 \& int EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx);
204 \&
205 \& int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
206 \& int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
207 .Ve
208 .SH "DESCRIPTION"
209 .IX Header "DESCRIPTION"
210 The \s-1EVP\s0 cipher routines are a high level interface to certain
211 symmetric ciphers.
212 .PP
213 \&\fIEVP_CIPHER_CTX_new()\fR creates a cipher context.
214 .PP
215 \&\fIEVP_CIPHER_CTX_free()\fR clears all information from a cipher context
216 and free up any allocated memory associate with it, including \fBctx\fR
217 itself. This function should be called after all operations using a
218 cipher are complete so sensitive information does not remain in
219 memory.
220 .PP
221 \&\fIEVP_EncryptInit_ex()\fR sets up cipher context \fBctx\fR for encryption
222 with cipher \fBtype\fR from \s-1ENGINE\s0 \fBimpl\fR. \fBctx\fR must be created
223 before calling this function. \fBtype\fR is normally supplied
224 by a function such as \fIEVP_aes_256_cbc()\fR. If \fBimpl\fR is \s-1NULL\s0 then the
225 default implementation is used. \fBkey\fR is the symmetric key to use
226 and \fBiv\fR is the \s-1IV\s0 to use (if necessary), the actual number of bytes
227 used for the key and \s-1IV\s0 depends on the cipher. It is possible to set
228 all parameters to \s-1NULL\s0 except \fBtype\fR in an initial call and supply
229 the remaining parameters in subsequent calls, all of which have \fBtype\fR
230 set to \s-1NULL.\s0 This is done when the default cipher parameters are not
231 appropriate.
232 .PP
233 \&\fIEVP_EncryptUpdate()\fR encrypts \fBinl\fR bytes from the buffer \fBin\fR and
234 writes the encrypted version to \fBout\fR. This function can be called
235 multiple times to encrypt successive blocks of data. The amount
236 of data written depends on the block alignment of the encrypted data:
237 as a result the amount of data written may be anything from zero bytes
238 to (inl + cipher_block_size \- 1) so \fBout\fR should contain sufficient
239 room. The actual number of bytes written is placed in \fBoutl\fR. It also
240 checks if \fBin\fR and \fBout\fR are partially overlapping, and if they are
241 0 is returned to indicate failure.
242 .PP
243 If padding is enabled (the default) then \fIEVP_EncryptFinal_ex()\fR encrypts
244 the \*(L"final\*(R" data, that is any data that remains in a partial block.
245 It uses standard block padding (aka \s-1PKCS\s0 padding) as described in
246 the \s-1NOTES\s0 section, below. The encrypted
247 final data is written to \fBout\fR which should have sufficient space for
248 one cipher block. The number of bytes written is placed in \fBoutl\fR. After
249 this function is called the encryption operation is finished and no further
250 calls to \fIEVP_EncryptUpdate()\fR should be made.
251 .PP
252 If padding is disabled then \fIEVP_EncryptFinal_ex()\fR will not encrypt any more
253 data and it will return an error if any data remains in a partial block:
254 that is if the total data length is not a multiple of the block size.
255 .PP
256 \&\fIEVP_DecryptInit_ex()\fR, \fIEVP_DecryptUpdate()\fR and \fIEVP_DecryptFinal_ex()\fR are the
257 corresponding decryption operations. \fIEVP_DecryptFinal()\fR will return an
258 error code if padding is enabled and the final block is not correctly
259 formatted. The parameters and restrictions are identical to the encryption
260 operations except that if padding is enabled the decrypted data buffer \fBout\fR
261 passed to \fIEVP_DecryptUpdate()\fR should have sufficient room for
262 (\fBinl\fR + cipher_block_size) bytes unless the cipher block size is 1 in
263 which case \fBinl\fR bytes is sufficient.
264 .PP
265 \&\fIEVP_CipherInit_ex()\fR, \fIEVP_CipherUpdate()\fR and \fIEVP_CipherFinal_ex()\fR are
266 functions that can be used for decryption or encryption. The operation
267 performed depends on the value of the \fBenc\fR parameter. It should be set
268 to 1 for encryption, 0 for decryption and \-1 to leave the value unchanged
269 (the actual value of 'enc' being supplied in a previous call).
270 .PP
271 \&\fIEVP_CIPHER_CTX_reset()\fR clears all information from a cipher context
272 and free up any allocated memory associate with it, except the \fBctx\fR
273 itself. This function should be called anytime \fBctx\fR is to be reused
274 for another \fIEVP_CipherInit()\fR / \fIEVP_CipherUpdate()\fR / \fIEVP_CipherFinal()\fR
275 series of calls.
276 .PP
277 \&\fIEVP_EncryptInit()\fR, \fIEVP_DecryptInit()\fR and \fIEVP_CipherInit()\fR behave in a
278 similar way to \fIEVP_EncryptInit_ex()\fR, \fIEVP_DecryptInit_ex()\fR and
279 \&\fIEVP_CipherInit_ex()\fR except they always use the default cipher implementation.
280 .PP
281 \&\fIEVP_EncryptFinal()\fR, \fIEVP_DecryptFinal()\fR and \fIEVP_CipherFinal()\fR are
282 identical to \fIEVP_EncryptFinal_ex()\fR, \fIEVP_DecryptFinal_ex()\fR and
283 \&\fIEVP_CipherFinal_ex()\fR. In previous releases they also cleaned up
284 the \fBctx\fR, but this is no longer done and \fIEVP_CIPHER_CTX_clean()\fR
285 must be called to free any context resources.
286 .PP
287 \&\fIEVP_get_cipherbyname()\fR, \fIEVP_get_cipherbynid()\fR and \fIEVP_get_cipherbyobj()\fR
288 return an \s-1EVP_CIPHER\s0 structure when passed a cipher name, a \s-1NID\s0 or an
289 \&\s-1ASN1_OBJECT\s0 structure.
290 .PP
291 \&\fIEVP_CIPHER_nid()\fR and \fIEVP_CIPHER_CTX_nid()\fR return the \s-1NID\s0 of a cipher when
292 passed an \fB\s-1EVP_CIPHER\s0\fR or \fB\s-1EVP_CIPHER_CTX\s0\fR structure.  The actual \s-1NID\s0
293 value is an internal value which may not have a corresponding \s-1OBJECT
294 IDENTIFIER.\s0
295 .PP
296 \&\fIEVP_CIPHER_CTX_set_padding()\fR enables or disables padding. This
297 function should be called after the context is set up for encryption
298 or decryption with \fIEVP_EncryptInit_ex()\fR, \fIEVP_DecryptInit_ex()\fR or
299 \&\fIEVP_CipherInit_ex()\fR. By default encryption operations are padded using
300 standard block padding and the padding is checked and removed when
301 decrypting. If the \fBpad\fR parameter is zero then no padding is
302 performed, the total amount of data encrypted or decrypted must then
303 be a multiple of the block size or an error will occur.
304 .PP
305 \&\fIEVP_CIPHER_key_length()\fR and \fIEVP_CIPHER_CTX_key_length()\fR return the key
306 length of a cipher when passed an \fB\s-1EVP_CIPHER\s0\fR or \fB\s-1EVP_CIPHER_CTX\s0\fR
307 structure. The constant \fB\s-1EVP_MAX_KEY_LENGTH\s0\fR is the maximum key length
308 for all ciphers. Note: although \fIEVP_CIPHER_key_length()\fR is fixed for a
309 given cipher, the value of \fIEVP_CIPHER_CTX_key_length()\fR may be different
310 for variable key length ciphers.
311 .PP
312 \&\fIEVP_CIPHER_CTX_set_key_length()\fR sets the key length of the cipher ctx.
313 If the cipher is a fixed length cipher then attempting to set the key
314 length to any value other than the fixed value is an error.
315 .PP
316 \&\fIEVP_CIPHER_iv_length()\fR and \fIEVP_CIPHER_CTX_iv_length()\fR return the \s-1IV\s0
317 length of a cipher when passed an \fB\s-1EVP_CIPHER\s0\fR or \fB\s-1EVP_CIPHER_CTX\s0\fR.
318 It will return zero if the cipher does not use an \s-1IV.\s0  The constant
319 \&\fB\s-1EVP_MAX_IV_LENGTH\s0\fR is the maximum \s-1IV\s0 length for all ciphers.
320 .PP
321 \&\fIEVP_CIPHER_block_size()\fR and \fIEVP_CIPHER_CTX_block_size()\fR return the block
322 size of a cipher when passed an \fB\s-1EVP_CIPHER\s0\fR or \fB\s-1EVP_CIPHER_CTX\s0\fR
323 structure. The constant \fB\s-1EVP_MAX_BLOCK_LENGTH\s0\fR is also the maximum block
324 length for all ciphers.
325 .PP
326 \&\fIEVP_CIPHER_type()\fR and \fIEVP_CIPHER_CTX_type()\fR return the type of the passed
327 cipher or context. This \*(L"type\*(R" is the actual \s-1NID\s0 of the cipher \s-1OBJECT
328 IDENTIFIER\s0 as such it ignores the cipher parameters and 40 bit \s-1RC2\s0 and
329 128 bit \s-1RC2\s0 have the same \s-1NID.\s0 If the cipher does not have an object
330 identifier or does not have \s-1ASN1\s0 support this function will return
331 \&\fBNID_undef\fR.
332 .PP
333 \&\fIEVP_CIPHER_CTX_cipher()\fR returns the \fB\s-1EVP_CIPHER\s0\fR structure when passed
334 an \fB\s-1EVP_CIPHER_CTX\s0\fR structure.
335 .PP
336 \&\fIEVP_CIPHER_mode()\fR and \fIEVP_CIPHER_CTX_mode()\fR return the block cipher mode:
337 \&\s-1EVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE, EVP_CIPH_OFB_MODE,
338 EVP_CIPH_CTR_MODE, EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE, EVP_CIPH_XTS_MODE,
339 EVP_CIPH_WRAP_MODE\s0 or \s-1EVP_CIPH_OCB_MODE.\s0 If the cipher is a stream cipher then
340 \&\s-1EVP_CIPH_STREAM_CIPHER\s0 is returned.
341 .PP
342 \&\fIEVP_CIPHER_param_to_asn1()\fR sets the AlgorithmIdentifier \*(L"parameter\*(R" based
343 on the passed cipher. This will typically include any parameters and an
344 \&\s-1IV.\s0 The cipher \s-1IV\s0 (if any) must be set when this call is made. This call
345 should be made before the cipher is actually \*(L"used\*(R" (before any
346 \&\fIEVP_EncryptUpdate()\fR, \fIEVP_DecryptUpdate()\fR calls for example). This function
347 may fail if the cipher does not have any \s-1ASN1\s0 support.
348 .PP
349 \&\fIEVP_CIPHER_asn1_to_param()\fR sets the cipher parameters based on an \s-1ASN1\s0
350 AlgorithmIdentifier \*(L"parameter\*(R". The precise effect depends on the cipher
351 In the case of \s-1RC2,\s0 for example, it will set the \s-1IV\s0 and effective key length.
352 This function should be called after the base cipher type is set but before
353 the key is set. For example \fIEVP_CipherInit()\fR will be called with the \s-1IV\s0 and
354 key set to \s-1NULL,\s0 \fIEVP_CIPHER_asn1_to_param()\fR will be called and finally
355 \&\fIEVP_CipherInit()\fR again with all parameters except the key set to \s-1NULL.\s0 It is
356 possible for this function to fail if the cipher does not have any \s-1ASN1\s0 support
357 or the parameters cannot be set (for example the \s-1RC2\s0 effective key length
358 is not supported.
359 .PP
360 \&\fIEVP_CIPHER_CTX_ctrl()\fR allows various cipher specific parameters to be determined
361 and set.
362 .PP
363 \&\fIEVP_CIPHER_CTX_rand_key()\fR generates a random key of the appropriate length
364 based on the cipher context. The \s-1EVP_CIPHER\s0 can provide its own random key
365 generation routine to support keys of a specific form. \fBKey\fR must point to a
366 buffer at least as big as the value returned by \fIEVP_CIPHER_CTX_key_length()\fR.
367 .SH "RETURN VALUES"
368 .IX Header "RETURN VALUES"
369 \&\fIEVP_CIPHER_CTX_new()\fR returns a pointer to a newly created
370 \&\fB\s-1EVP_CIPHER_CTX\s0\fR for success and \fB\s-1NULL\s0\fR for failure.
371 .PP
372 \&\fIEVP_EncryptInit_ex()\fR, \fIEVP_EncryptUpdate()\fR and \fIEVP_EncryptFinal_ex()\fR
373 return 1 for success and 0 for failure.
374 .PP
375 \&\fIEVP_DecryptInit_ex()\fR and \fIEVP_DecryptUpdate()\fR return 1 for success and 0 for failure.
376 \&\fIEVP_DecryptFinal_ex()\fR returns 0 if the decrypt failed or 1 for success.
377 .PP
378 \&\fIEVP_CipherInit_ex()\fR and \fIEVP_CipherUpdate()\fR return 1 for success and 0 for failure.
379 \&\fIEVP_CipherFinal_ex()\fR returns 0 for a decryption failure or 1 for success.
380 .PP
381 \&\fIEVP_CIPHER_CTX_reset()\fR returns 1 for success and 0 for failure.
382 .PP
383 \&\fIEVP_get_cipherbyname()\fR, \fIEVP_get_cipherbynid()\fR and \fIEVP_get_cipherbyobj()\fR
384 return an \fB\s-1EVP_CIPHER\s0\fR structure or \s-1NULL\s0 on error.
385 .PP
386 \&\fIEVP_CIPHER_nid()\fR and \fIEVP_CIPHER_CTX_nid()\fR return a \s-1NID.\s0
387 .PP
388 \&\fIEVP_CIPHER_block_size()\fR and \fIEVP_CIPHER_CTX_block_size()\fR return the block
389 size.
390 .PP
391 \&\fIEVP_CIPHER_key_length()\fR and \fIEVP_CIPHER_CTX_key_length()\fR return the key
392 length.
393 .PP
394 \&\fIEVP_CIPHER_CTX_set_padding()\fR always returns 1.
395 .PP
396 \&\fIEVP_CIPHER_iv_length()\fR and \fIEVP_CIPHER_CTX_iv_length()\fR return the \s-1IV\s0
397 length or zero if the cipher does not use an \s-1IV.\s0
398 .PP
399 \&\fIEVP_CIPHER_type()\fR and \fIEVP_CIPHER_CTX_type()\fR return the \s-1NID\s0 of the cipher's
400 \&\s-1OBJECT IDENTIFIER\s0 or NID_undef if it has no defined \s-1OBJECT IDENTIFIER.\s0
401 .PP
402 \&\fIEVP_CIPHER_CTX_cipher()\fR returns an \fB\s-1EVP_CIPHER\s0\fR structure.
403 .PP
404 \&\fIEVP_CIPHER_param_to_asn1()\fR and \fIEVP_CIPHER_asn1_to_param()\fR return greater
405 than zero for success and zero or a negative number on failure.
406 .PP
407 \&\fIEVP_CIPHER_CTX_rand_key()\fR returns 1 for success.
408 .SH "CIPHER LISTING"
409 .IX Header "CIPHER LISTING"
410 All algorithms have a fixed key length unless otherwise stated.
411 .PP
412 Refer to \*(L"\s-1SEE ALSO\*(R"\s0 for the full list of ciphers available through the \s-1EVP\s0
413 interface.
414 .IP "\fIEVP_enc_null()\fR" 4
415 .IX Item "EVP_enc_null()"
416 Null cipher: does nothing.
417 .SH "AEAD Interface"
418 .IX Header "AEAD Interface"
419 The \s-1EVP\s0 interface for Authenticated Encryption with Associated Data (\s-1AEAD\s0)
420 modes are subtly altered and several additional \fIctrl\fR operations are supported
421 depending on the mode specified.
422 .PP
423 To specify additional authenticated data (\s-1AAD\s0), a call to \fIEVP_CipherUpdate()\fR,
424 \&\fIEVP_EncryptUpdate()\fR or \fIEVP_DecryptUpdate()\fR should be made with the output
425 parameter \fBout\fR set to \fB\s-1NULL\s0\fR.
426 .PP
427 When decrypting, the return value of \fIEVP_DecryptFinal()\fR or \fIEVP_CipherFinal()\fR
428 indicates whether the operation was successful. If it does not indicate success,
429 the authentication operation has failed and any output data \fB\s-1MUST NOT\s0\fR be used
430 as it is corrupted.
431 .SS "\s-1GCM\s0 and \s-1OCB\s0 Modes"
432 .IX Subsection "GCM and OCB Modes"
433 The following \fIctrl\fRs are supported in \s-1GCM\s0 and \s-1OCB\s0 modes.
434 .IP "EVP_CIPHER_CTX_ctrl(ctx, \s-1EVP_CTRL_AEAD_SET_IVLEN,\s0 ivlen, \s-1NULL\s0)" 4
435 .IX Item "EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)"
436 Sets the \s-1IV\s0 length. This call can only be made before specifying an \s-1IV.\s0 If
437 not called a default \s-1IV\s0 length is used.
438 .Sp
439 For \s-1GCM AES\s0 and \s-1OCB AES\s0 the default is 12 (i.e. 96 bits). For \s-1OCB\s0 mode the
440 maximum is 15.
441 .IP "EVP_CIPHER_CTX_ctrl(ctx, \s-1EVP_CTRL_AEAD_GET_TAG,\s0 taglen, tag)" 4
442 .IX Item "EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)"
443 Writes \f(CW\*(C`taglen\*(C'\fR bytes of the tag value to the buffer indicated by \f(CW\*(C`tag\*(C'\fR.
444 This call can only be made when encrypting data and \fBafter\fR all data has been
445 processed (e.g. after an \fIEVP_EncryptFinal()\fR call).
446 .Sp
447 For \s-1OCB,\s0 \f(CW\*(C`taglen\*(C'\fR must either be 16 or the value previously set via
448 \&\fB\s-1EVP_CTRL_AEAD_SET_TAG\s0\fR.
449 .IP "EVP_CIPHER_CTX_ctrl(ctx, \s-1EVP_CTRL_AEAD_SET_TAG,\s0 taglen, tag)" 4
450 .IX Item "EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)"
451 Sets the expected tag to \f(CW\*(C`taglen\*(C'\fR bytes from \f(CW\*(C`tag\*(C'\fR.
452 The tag length can only be set before specifying an \s-1IV.\s0
453 \&\f(CW\*(C`taglen\*(C'\fR must be between 1 and 16 inclusive.
454 .Sp
455 For \s-1GCM,\s0 this call is only valid when decrypting data.
456 .Sp
457 For \s-1OCB,\s0 this call is valid when decrypting data to set the expected tag,
458 and before encryption to set the desired tag length.
459 .Sp
460 In \s-1OCB\s0 mode, calling this before encryption with \f(CW\*(C`tag\*(C'\fR set to \f(CW\*(C`NULL\*(C'\fR sets the
461 tag length.  If this is not called prior to encryption, a default tag length is
462 used.
463 .Sp
464 For \s-1OCB AES,\s0 the default tag length is 16 (i.e. 128 bits).  It is also the
465 maximum tag length for \s-1OCB.\s0
466 .SS "\s-1CCM\s0 Mode"
467 .IX Subsection "CCM Mode"
468 The \s-1EVP\s0 interface for \s-1CCM\s0 mode is similar to that of the \s-1GCM\s0 mode but with a
469 few additional requirements and different \fIctrl\fR values.
470 .PP
471 For \s-1CCM\s0 mode, the total plaintext or ciphertext length \fB\s-1MUST\s0\fR be passed to
472 \&\fIEVP_CipherUpdate()\fR, \fIEVP_EncryptUpdate()\fR or \fIEVP_DecryptUpdate()\fR with the output
473 and input parameters (\fBin\fR and \fBout\fR) set to \fB\s-1NULL\s0\fR and the length passed in
474 the \fBinl\fR parameter.
475 .PP
476 The following \fIctrl\fRs are supported in \s-1CCM\s0 mode.
477 .IP "EVP_CIPHER_CTX_ctrl(ctx, \s-1EVP_CTRL_AEAD_SET_TAG,\s0 taglen, tag)" 4
478 .IX Item "EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)"
479 This call is made to set the expected \fB\s-1CCM\s0\fR tag value when decrypting or
480 the length of the tag (with the \f(CW\*(C`tag\*(C'\fR parameter set to \s-1NULL\s0) when encrypting.
481 The tag length is often referred to as \fBM\fR. If not set a default value is
482 used (12 for \s-1AES\s0).
483 .IP "EVP_CIPHER_CTX_ctrl(ctx, \s-1EVP_CTRL_CCM_SET_L,\s0 ivlen, \s-1NULL\s0)" 4
484 .IX Item "EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, ivlen, NULL)"
485 Sets the \s-1CCM\s0 \fBL\fR value. If not set a default is used (8 for \s-1AES\s0).
486 .IP "EVP_CIPHER_CTX_ctrl(ctx, \s-1EVP_CTRL_AEAD_SET_IVLEN,\s0 ivlen, \s-1NULL\s0)" 4
487 .IX Item "EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)"
488 Sets the \s-1CCM\s0 nonce (\s-1IV\s0) length. This call can only be made before specifying an
489 nonce value. The nonce length is given by \fB15 \- L\fR so it is 7 by default for
490 \&\s-1AES.\s0
491 .SS "ChaCha20\-Poly1305"
492 .IX Subsection "ChaCha20-Poly1305"
493 The following \fIctrl\fRs are supported for the ChaCha20\-Poly1305 \s-1AEAD\s0 algorithm.
494 .IP "EVP_CIPHER_CTX_ctrl(ctx, \s-1EVP_CTRL_AEAD_SET_IVLEN,\s0 ivlen, \s-1NULL\s0)" 4
495 .IX Item "EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)"
496 Sets the nonce length. This call can only be made before specifying the nonce.
497 If not called a default nonce length of 12 (i.e. 96 bits) is used. The maximum
498 nonce length is 16 (\fB\s-1CHACHA_CTR_SIZE\s0\fR, i.e. 128\-bits).
499 .IP "EVP_CIPHER_CTX_ctrl(ctx, \s-1EVP_CTRL_AEAD_GET_TAG,\s0 taglen, tag)" 4
500 .IX Item "EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)"
501 Writes \f(CW\*(C`taglen\*(C'\fR bytes of the tag value to the buffer indicated by \f(CW\*(C`tag\*(C'\fR.
502 This call can only be made when encrypting data and \fBafter\fR all data has been
503 processed (e.g. after an \fIEVP_EncryptFinal()\fR call).
504 .Sp
505 \&\f(CW\*(C`taglen\*(C'\fR specified here must be 16 (\fB\s-1POLY1305_BLOCK_SIZE\s0\fR, i.e. 128\-bits) or
506 less.
507 .IP "EVP_CIPHER_CTX_ctrl(ctx, \s-1EVP_CTRL_AEAD_SET_TAG,\s0 taglen, tag)" 4
508 .IX Item "EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)"
509 Sets the expected tag to \f(CW\*(C`taglen\*(C'\fR bytes from \f(CW\*(C`tag\*(C'\fR.
510 The tag length can only be set before specifying an \s-1IV.\s0
511 \&\f(CW\*(C`taglen\*(C'\fR must be between 1 and 16 (\fB\s-1POLY1305_BLOCK_SIZE\s0\fR) inclusive.
512 This call is only valid when decrypting data.
513 .SH "NOTES"
514 .IX Header "NOTES"
515 Where possible the \fB\s-1EVP\s0\fR interface to symmetric ciphers should be used in
516 preference to the low level interfaces. This is because the code then becomes
517 transparent to the cipher used and much more flexible. Additionally, the
518 \&\fB\s-1EVP\s0\fR interface will ensure the use of platform specific cryptographic
519 acceleration such as AES-NI (the low level interfaces do not provide the
520 guarantee).
521 .PP
522 \&\s-1PKCS\s0 padding works by adding \fBn\fR padding bytes of value \fBn\fR to make the total
523 length of the encrypted data a multiple of the block size. Padding is always
524 added so if the data is already a multiple of the block size \fBn\fR will equal
525 the block size. For example if the block size is 8 and 11 bytes are to be
526 encrypted then 5 padding bytes of value 5 will be added.
527 .PP
528 When decrypting the final block is checked to see if it has the correct form.
529 .PP
530 Although the decryption operation can produce an error if padding is enabled,
531 it is not a strong test that the input data or key is correct. A random block
532 has better than 1 in 256 chance of being of the correct format and problems with
533 the input data earlier on will not produce a final decrypt error.
534 .PP
535 If padding is disabled then the decryption operation will always succeed if
536 the total amount of data decrypted is a multiple of the block size.
537 .PP
538 The functions \fIEVP_EncryptInit()\fR, \fIEVP_EncryptFinal()\fR, \fIEVP_DecryptInit()\fR,
539 \&\fIEVP_CipherInit()\fR and \fIEVP_CipherFinal()\fR are obsolete but are retained for
540 compatibility with existing code. New code should use \fIEVP_EncryptInit_ex()\fR,
541 \&\fIEVP_EncryptFinal_ex()\fR, \fIEVP_DecryptInit_ex()\fR, \fIEVP_DecryptFinal_ex()\fR,
542 \&\fIEVP_CipherInit_ex()\fR and \fIEVP_CipherFinal_ex()\fR because they can reuse an
543 existing context without allocating and freeing it up on each call.
544 .PP
545 \&\fIEVP_get_cipherbynid()\fR, and \fIEVP_get_cipherbyobj()\fR are implemented as macros.
546 .SH "BUGS"
547 .IX Header "BUGS"
548 \&\fB\s-1EVP_MAX_KEY_LENGTH\s0\fR and \fB\s-1EVP_MAX_IV_LENGTH\s0\fR only refer to the internal
549 ciphers with default key lengths. If custom ciphers exceed these values the
550 results are unpredictable. This is because it has become standard practice to
551 define a generic key as a fixed unsigned char array containing
552 \&\fB\s-1EVP_MAX_KEY_LENGTH\s0\fR bytes.
553 .PP
554 The \s-1ASN1\s0 code is incomplete (and sometimes inaccurate) it has only been tested
555 for certain common S/MIME ciphers (\s-1RC2, DES,\s0 triple \s-1DES\s0) in \s-1CBC\s0 mode.
556 .SH "EXAMPLES"
557 .IX Header "EXAMPLES"
558 Encrypt a string using \s-1IDEA:\s0
559 .PP
560 .Vb 10
561 \& int do_crypt(char *outfile)
562 \& {
563 \&     unsigned char outbuf[1024];
564 \&     int outlen, tmplen;
565 \&     /*
566 \&      * Bogus key and IV: we\*(Aqd normally set these from
567 \&      * another source.
568 \&      */
569 \&     unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
570 \&     unsigned char iv[] = {1,2,3,4,5,6,7,8};
571 \&     char intext[] = "Some Crypto Text";
572 \&     EVP_CIPHER_CTX *ctx;
573 \&     FILE *out;
574 \&
575 \&     ctx = EVP_CIPHER_CTX_new();
576 \&     EVP_EncryptInit_ex(ctx, EVP_idea_cbc(), NULL, key, iv);
577 \&
578 \&     if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) {
579 \&         /* Error */
580 \&         EVP_CIPHER_CTX_free(ctx);
581 \&         return 0;
582 \&     }
583 \&     /*
584 \&      * Buffer passed to EVP_EncryptFinal() must be after data just
585 \&      * encrypted to avoid overwriting it.
586 \&      */
587 \&     if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) {
588 \&         /* Error */
589 \&         EVP_CIPHER_CTX_free(ctx);
590 \&         return 0;
591 \&     }
592 \&     outlen += tmplen;
593 \&     EVP_CIPHER_CTX_free(ctx);
594 \&     /*
595 \&      * Need binary mode for fopen because encrypted data is
596 \&      * binary data. Also cannot use strlen() on it because
597 \&      * it won\*(Aqt be NUL terminated and may contain embedded
598 \&      * NULs.
599 \&      */
600 \&     out = fopen(outfile, "wb");
601 \&     if (out == NULL) {
602 \&         /* Error */
603 \&         return 0;
604 \&     }
605 \&     fwrite(outbuf, 1, outlen, out);
606 \&     fclose(out);
607 \&     return 1;
608 \& }
609 .Ve
610 .PP
611 The ciphertext from the above example can be decrypted using the \fBopenssl\fR
612 utility with the command line (shown on two lines for clarity):
613 .PP
614 .Vb 2
615 \& openssl idea \-d \e
616 \&     \-K 000102030405060708090A0B0C0D0E0F \-iv 0102030405060708 <filename
617 .Ve
618 .PP
619 General encryption and decryption function example using \s-1FILE I/O\s0 and \s-1AES128\s0
620 with a 128\-bit key:
621 .PP
622 .Vb 12
623 \& int do_crypt(FILE *in, FILE *out, int do_encrypt)
624 \& {
625 \&     /* Allow enough space in output buffer for additional block */
626 \&     unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
627 \&     int inlen, outlen;
628 \&     EVP_CIPHER_CTX *ctx;
629 \&     /*
630 \&      * Bogus key and IV: we\*(Aqd normally set these from
631 \&      * another source.
632 \&      */
633 \&     unsigned char key[] = "0123456789abcdeF";
634 \&     unsigned char iv[] = "1234567887654321";
635 \&
636 \&     /* Don\*(Aqt set key or IV right away; we want to check lengths */
637 \&     ctx = EVP_CIPHER_CTX_new();
638 \&     EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, NULL, NULL,
639 \&                       do_encrypt);
640 \&     OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16);
641 \&     OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == 16);
642 \&
643 \&     /* Now we can set key and IV */
644 \&     EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt);
645 \&
646 \&     for (;;) {
647 \&         inlen = fread(inbuf, 1, 1024, in);
648 \&         if (inlen <= 0)
649 \&             break;
650 \&         if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
651 \&             /* Error */
652 \&             EVP_CIPHER_CTX_free(ctx);
653 \&             return 0;
654 \&         }
655 \&         fwrite(outbuf, 1, outlen, out);
656 \&     }
657 \&     if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
658 \&         /* Error */
659 \&         EVP_CIPHER_CTX_free(ctx);
660 \&         return 0;
661 \&     }
662 \&     fwrite(outbuf, 1, outlen, out);
663 \&
664 \&     EVP_CIPHER_CTX_free(ctx);
665 \&     return 1;
666 \& }
667 .Ve
668 .SH "SEE ALSO"
669 .IX Header "SEE ALSO"
670 \&\fIevp\fR\|(7)
671 .PP
672 Supported ciphers are listed in:
673 .PP
674 \&\fIEVP_aes\fR\|(3),
675 \&\fIEVP_aria\fR\|(3),
676 \&\fIEVP_bf\fR\|(3),
677 \&\fIEVP_camellia\fR\|(3),
678 \&\fIEVP_cast5\fR\|(3),
679 \&\fIEVP_chacha20\fR\|(3),
680 \&\fIEVP_des\fR\|(3),
681 \&\fIEVP_desx\fR\|(3),
682 \&\fIEVP_idea\fR\|(3),
683 \&\fIEVP_rc2\fR\|(3),
684 \&\fIEVP_rc4\fR\|(3),
685 \&\fIEVP_rc5\fR\|(3),
686 \&\fIEVP_seed\fR\|(3),
687 \&\fIEVP_sm4\fR\|(3)
688 .SH "HISTORY"
689 .IX Header "HISTORY"
690 Support for \s-1OCB\s0 mode was added in OpenSSL 1.1.0
691 .PP
692 \&\fB\s-1EVP_CIPHER_CTX\s0\fR was made opaque in OpenSSL 1.1.0.  As a result,
693 \&\fIEVP_CIPHER_CTX_reset()\fR appeared and \fIEVP_CIPHER_CTX_cleanup()\fR
694 disappeared.  \fIEVP_CIPHER_CTX_init()\fR remains as an alias for
695 \&\fIEVP_CIPHER_CTX_reset()\fR.
696 .SH "COPYRIGHT"
697 .IX Header "COPYRIGHT"
698 Copyright 2000\-2018 The OpenSSL Project Authors. All Rights Reserved.
699 .PP
700 Licensed under the OpenSSL license (the \*(L"License\*(R").  You may not use
701 this file except in compliance with the License.  You can obtain a copy
702 in the file \s-1LICENSE\s0 in the source distribution or at
703 <https://www.openssl.org/source/license.html>.