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