diff options
Diffstat (limited to 'src/lib/libcrypto/man/EVP_EncryptInit.3')
-rw-r--r-- | src/lib/libcrypto/man/EVP_EncryptInit.3 | 939 |
1 files changed, 939 insertions, 0 deletions
diff --git a/src/lib/libcrypto/man/EVP_EncryptInit.3 b/src/lib/libcrypto/man/EVP_EncryptInit.3 new file mode 100644 index 0000000000..44967f863f --- /dev/null +++ b/src/lib/libcrypto/man/EVP_EncryptInit.3 | |||
@@ -0,0 +1,939 @@ | |||
1 | .Dd $Mdocdate: November 3 2016 $ | ||
2 | .Dt EVP_ENCRYPTINIT 3 | ||
3 | .Os | ||
4 | .Sh NAME | ||
5 | .Nm EVP_CIPHER_CTX_init , | ||
6 | .Nm EVP_EncryptInit_ex , | ||
7 | .Nm EVP_EncryptUpdate , | ||
8 | .Nm EVP_EncryptFinal_ex , | ||
9 | .Nm EVP_DecryptInit_ex , | ||
10 | .Nm EVP_DecryptUpdate , | ||
11 | .Nm EVP_DecryptFinal_ex , | ||
12 | .Nm EVP_CipherInit_ex , | ||
13 | .Nm EVP_CipherUpdate , | ||
14 | .Nm EVP_CipherFinal_ex , | ||
15 | .Nm EVP_CIPHER_CTX_set_key_length , | ||
16 | .Nm EVP_CIPHER_CTX_ctrl , | ||
17 | .Nm EVP_CIPHER_CTX_cleanup , | ||
18 | .Nm EVP_EncryptInit , | ||
19 | .Nm EVP_EncryptFinal , | ||
20 | .Nm EVP_DecryptInit , | ||
21 | .Nm EVP_DecryptFinal , | ||
22 | .Nm EVP_CipherInit , | ||
23 | .Nm EVP_CipherFinal , | ||
24 | .Nm EVP_get_cipherbyname , | ||
25 | .Nm EVP_get_cipherbynid , | ||
26 | .Nm EVP_get_cipherbyobj , | ||
27 | .Nm EVP_CIPHER_nid , | ||
28 | .Nm EVP_CIPHER_block_size , | ||
29 | .Nm EVP_CIPHER_key_length , | ||
30 | .Nm EVP_CIPHER_iv_length , | ||
31 | .Nm EVP_CIPHER_flags , | ||
32 | .Nm EVP_CIPHER_mode , | ||
33 | .Nm EVP_CIPHER_type , | ||
34 | .Nm EVP_CIPHER_CTX_cipher , | ||
35 | .Nm EVP_CIPHER_CTX_nid , | ||
36 | .Nm EVP_CIPHER_CTX_block_size , | ||
37 | .Nm EVP_CIPHER_CTX_key_length , | ||
38 | .Nm EVP_CIPHER_CTX_iv_length , | ||
39 | .Nm EVP_CIPHER_CTX_get_app_data , | ||
40 | .Nm EVP_CIPHER_CTX_set_app_data , | ||
41 | .Nm EVP_CIPHER_CTX_type , | ||
42 | .Nm EVP_CIPHER_CTX_flags , | ||
43 | .Nm EVP_CIPHER_CTX_mode , | ||
44 | .Nm EVP_CIPHER_param_to_asn1 , | ||
45 | .Nm EVP_CIPHER_asn1_to_param , | ||
46 | .Nm EVP_CIPHER_CTX_set_padding , | ||
47 | .Nm EVP_enc_null , | ||
48 | .Nm EVP_des_cbc , | ||
49 | .Nm EVP_des_ecb , | ||
50 | .Nm EVP_des_cfb , | ||
51 | .Nm EVP_des_ofb , | ||
52 | .Nm EVP_des_ede_cbc , | ||
53 | .Nm EVP_des_ede , | ||
54 | .Nm EVP_des_ede_ofb , | ||
55 | .Nm EVP_des_ede_cfb , | ||
56 | .Nm EVP_des_ede3_cbc , | ||
57 | .Nm EVP_des_ede3 , | ||
58 | .Nm EVP_des_ede3_ofb , | ||
59 | .Nm EVP_des_ede3_cfb , | ||
60 | .Nm EVP_desx_cbc , | ||
61 | .Nm EVP_rc4 , | ||
62 | .Nm EVP_rc4_40 , | ||
63 | .Nm EVP_idea_cbc , | ||
64 | .Nm EVP_idea_ecb , | ||
65 | .Nm EVP_idea_cfb , | ||
66 | .Nm EVP_idea_ofb , | ||
67 | .Nm EVP_idea_cbc , | ||
68 | .Nm EVP_rc2_cbc , | ||
69 | .Nm EVP_rc2_ecb , | ||
70 | .Nm EVP_rc2_cfb , | ||
71 | .Nm EVP_rc2_ofb , | ||
72 | .Nm EVP_rc2_40_cbc , | ||
73 | .Nm EVP_rc2_64_cbc , | ||
74 | .Nm EVP_bf_cbc , | ||
75 | .Nm EVP_bf_ecb , | ||
76 | .Nm EVP_bf_cfb , | ||
77 | .Nm EVP_bf_ofb , | ||
78 | .Nm EVP_cast5_cbc , | ||
79 | .Nm EVP_cast5_ecb , | ||
80 | .Nm EVP_cast5_cfb , | ||
81 | .Nm EVP_cast5_ofb , | ||
82 | .Nm EVP_aes_128_gcm , | ||
83 | .Nm EVP_aes_192_gcm , | ||
84 | .Nm EVP_aes_256_gcm , | ||
85 | .Nm EVP_aes_128_ccm , | ||
86 | .Nm EVP_aes_192_ccm , | ||
87 | .Nm EVP_aes_256_ccm , | ||
88 | .Nm EVP_rc5_32_12_16_cbc , | ||
89 | .Nm EVP_rc5_32_12_16_cfb , | ||
90 | .Nm EVP_rc5_32_12_16_ecb , | ||
91 | .Nm EVP_rc5_32_12_16_ofb | ||
92 | .Nd EVP cipher routines | ||
93 | .Sh SYNOPSIS | ||
94 | .In openssl/evp.h | ||
95 | .Ft void | ||
96 | .Fo EVP_CIPHER_CTX_init | ||
97 | .Fa "EVP_CIPHER_CTX *ctx" | ||
98 | .Fc | ||
99 | .Ft int | ||
100 | .Fo EVP_EncryptInit_ex | ||
101 | .Fa "EVP_CIPHER_CTX *ctx" | ||
102 | .Fa "const EVP_CIPHER *type" | ||
103 | .Fa "ENGINE *impl" | ||
104 | .Fa "unsigned char *key" | ||
105 | .Fa "unsigned char *iv" | ||
106 | .Fc | ||
107 | .Ft int | ||
108 | .Fo EVP_EncryptUpdate | ||
109 | .Fa "EVP_CIPHER_CTX *ctx" | ||
110 | .Fa "unsigned char *out" | ||
111 | .Fa "int *outl" | ||
112 | .Fa "unsigned char *in" | ||
113 | .Fa "int inl" | ||
114 | .Fc | ||
115 | .Ft int | ||
116 | .Fo EVP_EncryptFinal_ex | ||
117 | .Fa "EVP_CIPHER_CTX *ctx" | ||
118 | .Fa "unsigned char *out" | ||
119 | .Fa "int *outl" | ||
120 | .Fc | ||
121 | .Ft int | ||
122 | .Fo EVP_DecryptInit_ex | ||
123 | .Fa "EVP_CIPHER_CTX *ctx" | ||
124 | .Fa "const EVP_CIPHER *type" | ||
125 | .Fa "ENGINE *impl" | ||
126 | .Fa "unsigned char *key" | ||
127 | .Fa "unsigned char *iv" | ||
128 | .Fc | ||
129 | .Ft int | ||
130 | .Fo EVP_DecryptUpdate | ||
131 | .Fa "EVP_CIPHER_CTX *ctx" | ||
132 | .Fa "unsigned char *out" | ||
133 | .Fa "int *outl" | ||
134 | .Fa "unsigned char *in" | ||
135 | .Fa "int inl" | ||
136 | .Fc | ||
137 | .Ft int | ||
138 | .Fo EVP_DecryptFinal_ex | ||
139 | .Fa "EVP_CIPHER_CTX *ctx" | ||
140 | .Fa "unsigned char *outm" | ||
141 | .Fa "int *outl" | ||
142 | .Fc | ||
143 | .Ft int | ||
144 | .Fo EVP_CipherInit_ex | ||
145 | .Fa "EVP_CIPHER_CTX *ctx" | ||
146 | .Fa "const EVP_CIPHER *type" | ||
147 | .Fa "ENGINE *impl" | ||
148 | .Fa "unsigned char *key" | ||
149 | .Fa "unsigned char *iv" | ||
150 | .Fa "int enc" | ||
151 | .Fc | ||
152 | .Ft int | ||
153 | .Fo EVP_CipherUpdate | ||
154 | .Fa "EVP_CIPHER_CTX *ctx" | ||
155 | .Fa "unsigned char *out" | ||
156 | .Fa "int *outl" | ||
157 | .Fa "unsigned char *in" | ||
158 | .Fa "int inl" | ||
159 | .Fc | ||
160 | .Ft int | ||
161 | .Fo EVP_CipherFinal_ex | ||
162 | .Fa "EVP_CIPHER_CTX *ctx" | ||
163 | .Fa "unsigned char *outm" | ||
164 | .Fa "int *outl" | ||
165 | .Fc | ||
166 | .Ft int | ||
167 | .Fo EVP_EncryptInit | ||
168 | .Fa "EVP_CIPHER_CTX *ctx" | ||
169 | .Fa "const EVP_CIPHER *type" | ||
170 | .Fa "unsigned char *key" | ||
171 | .Fa "unsigned char *iv" | ||
172 | .Fc | ||
173 | .Ft int | ||
174 | .Fo EVP_EncryptFinal | ||
175 | .Fa "EVP_CIPHER_CTX *ctx" | ||
176 | .Fa "unsigned char *out" | ||
177 | .Fa "int *outl" | ||
178 | .Fc | ||
179 | .Ft int | ||
180 | .Fo EVP_DecryptInit | ||
181 | .Fa "EVP_CIPHER_CTX *ctx" | ||
182 | .Fa "const EVP_CIPHER *type" | ||
183 | .Fa "unsigned char *key" | ||
184 | .Fa "unsigned char *iv" | ||
185 | .Fc | ||
186 | .Ft int | ||
187 | .Fo EVP_DecryptFinal | ||
188 | .Fa "EVP_CIPHER_CTX *ctx" | ||
189 | .Fa "unsigned char *outm" | ||
190 | .Fa "int *outl" | ||
191 | .Fc | ||
192 | .Ft int | ||
193 | .Fo EVP_CipherInit | ||
194 | .Fa "EVP_CIPHER_CTX *ctx" | ||
195 | .Fa "const EVP_CIPHER *type" | ||
196 | .Fa "unsigned char *key" | ||
197 | .Fa "unsigned char *iv" | ||
198 | .Fa "int enc" | ||
199 | .Fc | ||
200 | .Ft int | ||
201 | .Fo EVP_CipherFinal | ||
202 | .Fa "EVP_CIPHER_CTX *ctx" | ||
203 | .Fa "unsigned char *outm" | ||
204 | .Fa "int *outl" | ||
205 | .Fc | ||
206 | .Ft int | ||
207 | .Fo EVP_CIPHER_CTX_set_padding | ||
208 | .Fa "EVP_CIPHER_CTX *x" | ||
209 | .Fa "int padding" | ||
210 | .Fc | ||
211 | .Ft int | ||
212 | .Fo EVP_CIPHER_CTX_set_key_length | ||
213 | .Fa "EVP_CIPHER_CTX *x" | ||
214 | .Fa "int keylen" | ||
215 | .Fc | ||
216 | .Ft int | ||
217 | .Fo EVP_CIPHER_CTX_ctrl | ||
218 | .Fa "EVP_CIPHER_CTX *ctx" | ||
219 | .Fa "int type" | ||
220 | .Fa "int arg" | ||
221 | .Fa "void *ptr" | ||
222 | .Fc | ||
223 | .Ft int | ||
224 | .Fo EVP_CIPHER_CTX_cleanup | ||
225 | .Fa "EVP_CIPHER_CTX *ctx" | ||
226 | .Fc | ||
227 | .Ft const EVP_CIPHER * | ||
228 | .Fo EVP_get_cipherbyname | ||
229 | .Fa "const char *name" | ||
230 | .Fc | ||
231 | .Fd #define EVP_get_cipherbynid(a) EVP_get_cipherbyname(OBJ_nid2sn(a)) | ||
232 | .Fd #define EVP_get_cipherbyobj(a) EVP_get_cipherbynid(OBJ_obj2nid(a)) | ||
233 | .Fd #define EVP_CIPHER_nid(e) ((e)->nid) | ||
234 | .Fd #define EVP_CIPHER_block_size(e) ((e)->block_size) | ||
235 | .Fd #define EVP_CIPHER_key_length(e) ((e)->key_len) | ||
236 | .Fd #define EVP_CIPHER_iv_length(e) ((e)->iv_len) | ||
237 | .Fd #define EVP_CIPHER_flags(e) ((e)->flags) | ||
238 | .Fd #define EVP_CIPHER_mode(e) ((e)->flags) & EVP_CIPH_MODE) | ||
239 | .Ft int | ||
240 | .Fo EVP_CIPHER_type | ||
241 | .Fa "const EVP_CIPHER *ctx" | ||
242 | .Fc | ||
243 | .Fd #define EVP_CIPHER_CTX_cipher(e) ((e)->cipher) | ||
244 | .Fd #define EVP_CIPHER_CTX_nid(e) ((e)->cipher->nid) | ||
245 | .Fd #define EVP_CIPHER_CTX_block_size(e) ((e)->cipher->block_size) | ||
246 | .Fd #define EVP_CIPHER_CTX_key_length(e) ((e)->key_len) | ||
247 | .Fd #define EVP_CIPHER_CTX_iv_length(e) ((e)->cipher->iv_len) | ||
248 | .Fd #define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data) | ||
249 | .Fd #define EVP_CIPHER_CTX_set_app_data(e,d) ((e)->app_data=(char *)(d)) | ||
250 | .Fd #define EVP_CIPHER_CTX_type(c) EVP_CIPHER_type(EVP_CIPHER_CTX_cipher(c)) | ||
251 | .Fd #define EVP_CIPHER_CTX_flags(e) ((e)->cipher->flags) | ||
252 | .Fd #define EVP_CIPHER_CTX_mode(e) ((e)->cipher->flags & EVP_CIPH_MODE) | ||
253 | .Ft int | ||
254 | .Fo EVP_CIPHER_param_to_asn1 | ||
255 | .Fa "EVP_CIPHER_CTX *c" | ||
256 | .Fa "ASN1_TYPE *type" | ||
257 | .Fc | ||
258 | .Ft int | ||
259 | .Fo EVP_CIPHER_asn1_to_param | ||
260 | .Fa "EVP_CIPHER_CTX *c" | ||
261 | .Fa "ASN1_TYPE *type" | ||
262 | .Fc | ||
263 | .Sh DESCRIPTION | ||
264 | The EVP cipher routines are a high level interface to certain symmetric | ||
265 | ciphers. | ||
266 | .Pp | ||
267 | .Fn EVP_CIPHER_CTX_init | ||
268 | initializes the cipher context | ||
269 | .Fa ctx . | ||
270 | .Pp | ||
271 | .Fn EVP_EncryptInit_ex | ||
272 | sets up the cipher context | ||
273 | .Fa ctx | ||
274 | for encryption with cipher | ||
275 | .Fa type | ||
276 | from | ||
277 | .Vt ENGINE | ||
278 | .Fa impl . | ||
279 | .Fa ctx | ||
280 | must be initialized before calling this function. | ||
281 | .Fa type | ||
282 | is normally supplied by a function such as | ||
283 | .Fn EVP_aes_256_cbc . | ||
284 | If | ||
285 | .Fa impl | ||
286 | is | ||
287 | .Dv NULL , | ||
288 | then the default implementation is used. | ||
289 | .Fa key | ||
290 | is the symmetric key to use and | ||
291 | .Fa iv | ||
292 | is the IV to use (if necessary), the actual number of bytes used for the | ||
293 | key and IV depends on the cipher. | ||
294 | It is possible to set all parameters to | ||
295 | .Dv NULL | ||
296 | except | ||
297 | .Fa type | ||
298 | in an initial call and supply the remaining parameters in subsequent | ||
299 | calls, all of which have | ||
300 | .Fa type | ||
301 | set to | ||
302 | .Dv NULL . | ||
303 | This is done when the default cipher parameters are not appropriate. | ||
304 | .Pp | ||
305 | .Fn EVP_EncryptUpdate | ||
306 | encrypts | ||
307 | .Fa inl | ||
308 | bytes from the buffer | ||
309 | .Fa in | ||
310 | and writes the encrypted version to | ||
311 | .Fa out . | ||
312 | This function can be called multiple times to encrypt successive blocks | ||
313 | of data. | ||
314 | The amount of data written depends on the block alignment of the | ||
315 | encrypted data: as a result the amount of data written may be anything | ||
316 | from zero bytes to (inl + cipher_block_size - 1) so | ||
317 | .Fa outl | ||
318 | should contain sufficient room. | ||
319 | The actual number of bytes written is placed in | ||
320 | .Fa outl . | ||
321 | .Pp | ||
322 | If padding is enabled (the default) then | ||
323 | .Fn EVP_EncryptFinal_ex | ||
324 | encrypts the "final" data, that is any data that remains in a partial | ||
325 | block. | ||
326 | It uses NOTES (aka PKCS padding). | ||
327 | The encrypted final data is written to | ||
328 | .Fa out | ||
329 | which should have sufficient space for one cipher block. | ||
330 | The number of bytes written is placed in | ||
331 | .Fa outl . | ||
332 | After this function is called the encryption operation is finished and | ||
333 | no further calls to | ||
334 | .Fn EVP_EncryptUpdate | ||
335 | should be made. | ||
336 | .Pp | ||
337 | If padding is disabled then | ||
338 | .Fn EVP_EncryptFinal_ex | ||
339 | will not encrypt any more data and it will return an error if any data | ||
340 | remains in a partial block: that is if the total data length is not a | ||
341 | multiple of the block size. | ||
342 | .Pp | ||
343 | .Fn EVP_DecryptInit_ex , | ||
344 | .Fn EVP_DecryptUpdate , | ||
345 | and | ||
346 | .Fn EVP_DecryptFinal_ex | ||
347 | are the corresponding decryption operations. | ||
348 | .Fn EVP_DecryptFinal | ||
349 | will return an error code if padding is enabled and the final block is | ||
350 | not correctly formatted. | ||
351 | The parameters and restrictions are identical to the encryption | ||
352 | operations except that if padding is enabled the decrypted data buffer | ||
353 | .Fa out | ||
354 | passed to | ||
355 | .Fn EVP_DecryptUpdate | ||
356 | should have sufficient room for (inl + cipher_block_size) bytes | ||
357 | unless the cipher block size is 1 in which case | ||
358 | .Fa inl | ||
359 | bytes is sufficient. | ||
360 | .Pp | ||
361 | .Fn EVP_CipherInit_ex , | ||
362 | .Fn EVP_CipherUpdate , | ||
363 | and | ||
364 | .Fn EVP_CipherFinal_ex | ||
365 | are functions that can be used for decryption or encryption. | ||
366 | The operation performed depends on the value of the | ||
367 | .Fa enc | ||
368 | parameter. | ||
369 | It should be set to 1 for encryption, 0 for decryption and -1 to leave | ||
370 | the value unchanged (the actual value of | ||
371 | .Fa enc | ||
372 | being supplied in a previous call). | ||
373 | .Pp | ||
374 | .Fn EVP_CIPHER_CTX_cleanup | ||
375 | clears all information from a cipher context and free up any allocated | ||
376 | memory associate with it. | ||
377 | It should be called after all operations using a cipher are complete so | ||
378 | sensitive information does not remain in memory. | ||
379 | .Pp | ||
380 | .Fn EVP_EncryptInit , | ||
381 | .Fn EVP_DecryptInit , | ||
382 | and | ||
383 | .Fn EVP_CipherInit | ||
384 | behave in a similar way to | ||
385 | .Fn EVP_EncryptInit_ex , | ||
386 | .Fn EVP_DecryptInit_ex , | ||
387 | and | ||
388 | .Fn EVP_CipherInit_ex | ||
389 | except the | ||
390 | .Fa ctx | ||
391 | parameter does not need to be initialized and they always use the | ||
392 | default cipher implementation. | ||
393 | .Pp | ||
394 | .Fn EVP_EncryptFinal , | ||
395 | .Fn EVP_DecryptFinal , | ||
396 | and | ||
397 | .Fn EVP_CipherFinal | ||
398 | are identical to | ||
399 | .Fn EVP_EncryptFinal_ex , | ||
400 | .Fn EVP_DecryptFinal_ex , | ||
401 | and | ||
402 | .Fn EVP_CipherFinal_ex . | ||
403 | In previous releases of OpenSSL, they also used to clean up the | ||
404 | .Fa ctx , | ||
405 | but this is no longer done and | ||
406 | .Fn EVP_CIPHER_CTX_cleanup | ||
407 | must be called to free any context resources. | ||
408 | .Pp | ||
409 | .Fn EVP_get_cipherbyname , | ||
410 | .Fn EVP_get_cipherbynid , | ||
411 | and | ||
412 | .Fn EVP_get_cipherbyobj | ||
413 | return an | ||
414 | .Vt EVP_CIPHER | ||
415 | structure when passed a cipher name, a NID or an | ||
416 | .Vt ASN1_OBJECT | ||
417 | structure. | ||
418 | .Pp | ||
419 | .Fn EVP_CIPHER_nid | ||
420 | and | ||
421 | .Fn EVP_CIPHER_CTX_nid | ||
422 | return the NID of a cipher when passed an | ||
423 | .Vt EVP_CIPHER | ||
424 | or | ||
425 | .Vt EVP_CIPHER_CTX | ||
426 | structure. | ||
427 | The actual NID value is an internal value which may not have a | ||
428 | corresponding OBJECT IDENTIFIER. | ||
429 | .Pp | ||
430 | .Fn EVP_CIPHER_CTX_set_padding | ||
431 | enables or disables padding. | ||
432 | By default encryption operations are padded using standard block padding | ||
433 | and the padding is checked and removed when decrypting. | ||
434 | If the | ||
435 | .Sy padding | ||
436 | parameter is zero, then no padding is performed, the total amount of data | ||
437 | encrypted or decrypted must then be a multiple of the block size or an | ||
438 | error will occur. | ||
439 | .Pp | ||
440 | .Fn EVP_CIPHER_key_length | ||
441 | and | ||
442 | .Fn EVP_CIPHER_CTX_key_length | ||
443 | return the key length of a cipher when passed an | ||
444 | .Vt EVP_CIPHER | ||
445 | or | ||
446 | .Vt EVP_CIPHER_CTX | ||
447 | structure. | ||
448 | The constant | ||
449 | .Dv EVP_MAX_KEY_LENGTH | ||
450 | is the maximum key length for all ciphers. | ||
451 | Note: although | ||
452 | .Fn EVP_CIPHER_key_length | ||
453 | is fixed for a given cipher, the value of | ||
454 | .Fn EVP_CIPHER_CTX_key_length | ||
455 | may be different for variable key length ciphers. | ||
456 | .Pp | ||
457 | .Fn EVP_CIPHER_CTX_set_key_length | ||
458 | sets the key length of the cipher ctx. | ||
459 | If the cipher is a fixed length cipher, then attempting to set the key | ||
460 | length to any value other than the fixed value is an error. | ||
461 | .Pp | ||
462 | .Fn EVP_CIPHER_iv_length | ||
463 | and | ||
464 | .Fn EVP_CIPHER_CTX_iv_length | ||
465 | return the IV length of a cipher when passed an | ||
466 | .Vt EVP_CIPHER | ||
467 | or | ||
468 | .Vt EVP_CIPHER_CTX . | ||
469 | It will return zero if the cipher does not use an IV. | ||
470 | The constant | ||
471 | .Dv EVP_MAX_IV_LENGTH | ||
472 | is the maximum IV length for all ciphers. | ||
473 | .Pp | ||
474 | .Fn EVP_CIPHER_block_size | ||
475 | and | ||
476 | .Fn EVP_CIPHER_CTX_block_size | ||
477 | return the block size of a cipher when passed an | ||
478 | .Vt EVP_CIPHER | ||
479 | or | ||
480 | .Vt EVP_CIPHER_CTX | ||
481 | structure. | ||
482 | The constant | ||
483 | .Dv EVP_MAX_IV_LENGTH | ||
484 | is also the maximum block length for all ciphers. | ||
485 | .Pp | ||
486 | .Fn EVP_CIPHER_type | ||
487 | and | ||
488 | .Fn EVP_CIPHER_CTX_type | ||
489 | return the type of the passed cipher or context. | ||
490 | This "type" is the actual NID of the cipher OBJECT IDENTIFIER as such it | ||
491 | ignores the cipher parameters and 40 bit RC2 and 128 bit RC2 have the | ||
492 | same NID. If the cipher does not have an object identifier or does not | ||
493 | have ASN1 support this function will return | ||
494 | .Dv NID_undef . | ||
495 | .Pp | ||
496 | .Fn EVP_CIPHER_CTX_cipher | ||
497 | returns the | ||
498 | .Vt EVP_CIPHER | ||
499 | structure when passed an | ||
500 | .Vt EVP_CIPHER_CTX | ||
501 | structure. | ||
502 | .Pp | ||
503 | .Fn EVP_CIPHER_mode | ||
504 | and | ||
505 | .Fn EVP_CIPHER_CTX_mode | ||
506 | return the block cipher mode: | ||
507 | .Dv EVP_CIPH_ECB_MODE , | ||
508 | .Dv EVP_CIPH_CBC_MODE , | ||
509 | .Dv EVP_CIPH_CFB_MODE , | ||
510 | or | ||
511 | .Dv EVP_CIPH_OFB_MODE . | ||
512 | If the cipher is a stream cipher then | ||
513 | .Dv EVP_CIPH_STREAM_CIPHER | ||
514 | is returned. | ||
515 | .Pp | ||
516 | .Fn EVP_CIPHER_param_to_asn1 | ||
517 | sets the AlgorithmIdentifier "parameter" based on the passed cipher. | ||
518 | This will typically include any parameters and an IV. | ||
519 | The cipher IV (if any) must be set when this call is made. | ||
520 | This call should be made before the cipher is actually "used" (before any | ||
521 | .Fn EVP_EncryptUpdate , | ||
522 | .Fn EVP_DecryptUpdate | ||
523 | calls for example). | ||
524 | This function may fail if the cipher does not have any ASN1 support. | ||
525 | .Pp | ||
526 | .Fn EVP_CIPHER_asn1_to_param | ||
527 | sets the cipher parameters based on an ASN1 AlgorithmIdentifier | ||
528 | "parameter". | ||
529 | The precise effect depends on the cipher. | ||
530 | In the case of RC2, for example, it will set the IV and effective | ||
531 | key length. | ||
532 | This function should be called after the base cipher type is set but | ||
533 | before the key is set. | ||
534 | For example | ||
535 | .Fn EVP_CipherInit | ||
536 | will be called with the IV and key set to | ||
537 | .Dv NULL , | ||
538 | .Fn EVP_CIPHER_asn1_to_param | ||
539 | will be called and finally | ||
540 | .Fn EVP_CipherInit | ||
541 | again with all parameters except the key set to | ||
542 | .Dv NULL . | ||
543 | It is possible for this function to fail if the cipher does not | ||
544 | have any ASN1 support or the parameters cannot be set (for example | ||
545 | the RC2 effective key length is not supported). | ||
546 | .Pp | ||
547 | .Fn EVP_CIPHER_CTX_ctrl | ||
548 | allows various cipher specific parameters to be determined and set. | ||
549 | Currently only the RC2 effective key length and the number of rounds of | ||
550 | RC5 can be set. | ||
551 | .Sh RETURN VALUES | ||
552 | .Fn EVP_EncryptInit_ex , | ||
553 | .Fn EVP_EncryptUpdate , | ||
554 | and | ||
555 | .Fn EVP_EncryptFinal_ex | ||
556 | return 1 for success and 0 for failure. | ||
557 | .Pp | ||
558 | .Fn EVP_DecryptInit_ex | ||
559 | and | ||
560 | .Fn EVP_DecryptUpdate | ||
561 | return 1 for success and 0 for failure. | ||
562 | .Fn EVP_DecryptFinal_ex | ||
563 | returns 0 if the decrypt failed or 1 for success. | ||
564 | .Pp | ||
565 | .Fn EVP_CipherInit_ex | ||
566 | and | ||
567 | .Fn EVP_CipherUpdate | ||
568 | return 1 for success and 0 for failure. | ||
569 | .Fn EVP_CipherFinal_ex | ||
570 | returns 0 for a decryption failure or 1 for success. | ||
571 | .Pp | ||
572 | .Fn EVP_CIPHER_CTX_cleanup | ||
573 | returns 1 for success and 0 for failure. | ||
574 | .Pp | ||
575 | .Fn EVP_get_cipherbyname , | ||
576 | .Fn EVP_get_cipherbynid , | ||
577 | and | ||
578 | .Fn EVP_get_cipherbyobj | ||
579 | return an | ||
580 | .Vt EVP_CIPHER | ||
581 | structure or | ||
582 | .Dv NULL | ||
583 | on error. | ||
584 | .Pp | ||
585 | .Fn EVP_CIPHER_nid | ||
586 | and | ||
587 | .Fn EVP_CIPHER_CTX_nid | ||
588 | return a NID. | ||
589 | .Pp | ||
590 | .Fn EVP_CIPHER_block_size | ||
591 | and | ||
592 | .Fn EVP_CIPHER_CTX_block_size | ||
593 | return the block size. | ||
594 | .Pp | ||
595 | .Fn EVP_CIPHER_key_length | ||
596 | and | ||
597 | .Fn EVP_CIPHER_CTX_key_length | ||
598 | return the key length. | ||
599 | .Pp | ||
600 | .Fn EVP_CIPHER_CTX_set_padding | ||
601 | always returns 1. | ||
602 | .Pp | ||
603 | .Fn EVP_CIPHER_iv_length | ||
604 | and | ||
605 | .Fn EVP_CIPHER_CTX_iv_length | ||
606 | return the IV length or zero if the cipher does not use an IV. | ||
607 | .Pp | ||
608 | .Fn EVP_CIPHER_type | ||
609 | and | ||
610 | .Fn EVP_CIPHER_CTX_type | ||
611 | return the NID of the cipher's OBJECT IDENTIFIER or | ||
612 | .Dv NID_undef | ||
613 | if it has no defined OBJECT IDENTIFIER. | ||
614 | .Pp | ||
615 | .Fn EVP_CIPHER_CTX_cipher | ||
616 | returns an | ||
617 | .Vt EVP_CIPHER | ||
618 | structure. | ||
619 | .Pp | ||
620 | .Fn EVP_CIPHER_param_to_asn1 | ||
621 | and | ||
622 | .Fn EVP_CIPHER_asn1_to_param | ||
623 | return 1 for success or 0 for failure. | ||
624 | .Pp | ||
625 | Where possible the EVP interface to symmetric ciphers should be | ||
626 | used in preference to the low level interfaces. | ||
627 | This is because the code then becomes transparent to the cipher used and | ||
628 | much more flexible. | ||
629 | .Pp | ||
630 | PKCS padding works by adding n padding bytes of value n to make the | ||
631 | total length of the encrypted data a multiple of the block size. | ||
632 | Padding is always added so if the data is already a multiple of the | ||
633 | block size n will equal the block size. | ||
634 | For example if the block size is 8 and 11 bytes are to be encrypted then | ||
635 | 5 padding bytes of value 5 will be added. | ||
636 | .Pp | ||
637 | When decrypting the final block is checked to see if it has the correct | ||
638 | form. | ||
639 | .Pp | ||
640 | Although the decryption operation can produce an error if padding is | ||
641 | enabled, it is not a strong test that the input data or key is correct. | ||
642 | A random block has better than 1 in 256 chance of being of the correct | ||
643 | format and problems with the input data earlier on will not produce a | ||
644 | final decrypt error. | ||
645 | .Pp | ||
646 | If padding is disabled then the decryption operation will always succeed | ||
647 | if the total amount of data decrypted is a multiple of the block size. | ||
648 | .Pp | ||
649 | The functions | ||
650 | .Fn EVP_EncryptInit , | ||
651 | .Fn EVP_EncryptFinal , | ||
652 | .Fn EVP_DecryptInit , | ||
653 | .Fn EVP_CipherInit , | ||
654 | and | ||
655 | .Fn EVP_CipherFinal | ||
656 | are obsolete but are retained for compatibility with existing code. | ||
657 | New code should use | ||
658 | .Fn EVP_EncryptInit_ex , | ||
659 | .Fn EVP_EncryptFinal_ex , | ||
660 | .Fn EVP_DecryptInit_ex , | ||
661 | .Fn EVP_DecryptFinal_ex , | ||
662 | .Fn EVP_CipherInit_ex , | ||
663 | and | ||
664 | .Fn EVP_CipherFinal_ex | ||
665 | because they can reuse an existing context without allocating and | ||
666 | freeing it up on each call. | ||
667 | .Sh CIPHER LISTING | ||
668 | All algorithms have a fixed key length unless otherwise stated. | ||
669 | .Bl -tag -width Ds | ||
670 | .It Fn EVP_enc_null void | ||
671 | Null cipher: does nothing. | ||
672 | .It Xo | ||
673 | .Fn EVP_aes_128_cbc void , | ||
674 | .Fn EVP_aes_128_ecb void , | ||
675 | .Fn EVP_aes_128_cfb void , | ||
676 | .Fn EVP_aes_128_ofb void | ||
677 | .Xc | ||
678 | 128-bit AES in CBC, ECB, CFB and OFB modes respectively. | ||
679 | .It Xo | ||
680 | .Fn EVP_aes_192_cbc void , | ||
681 | .Fn EVP_aes_192_ecb void , | ||
682 | .Fn EVP_aes_192_cfb void , | ||
683 | .Fn EVP_aes_192_ofb void | ||
684 | .Xc | ||
685 | 192-bit AES in CBC, ECB, CFB and OFB modes respectively. | ||
686 | .It Xo | ||
687 | .Fn EVP_aes_256_cbc void , | ||
688 | .Fn EVP_aes_256_ecb void , | ||
689 | .Fn EVP_aes_256_cfb void , | ||
690 | .Fn EVP_aes_256_ofb void | ||
691 | .Xc | ||
692 | 256-bit AES in CBC, ECB, CFB and OFB modes respectively. | ||
693 | .It Xo | ||
694 | .Fn EVP_des_cbc void , | ||
695 | .Fn EVP_des_ecb void , | ||
696 | .Fn EVP_des_cfb void , | ||
697 | .Fn EVP_des_ofb void | ||
698 | .Xc | ||
699 | DES in CBC, ECB, CFB and OFB modes respectively. | ||
700 | .It Xo | ||
701 | .Fn EVP_des_ede_cbc void , | ||
702 | .Fn EVP_des_ede void , | ||
703 | .Fn EVP_des_ede_ofb void , | ||
704 | .Fn EVP_des_ede_cfb void | ||
705 | .Xc | ||
706 | Two key triple DES in CBC, ECB, CFB and OFB modes respectively. | ||
707 | .It Xo | ||
708 | .Fn EVP_des_ede3_cbc void , | ||
709 | .Fn EVP_des_ede3 void , | ||
710 | .Fn EVP_des_ede3_ofb void , | ||
711 | .Fn EVP_des_ede3_cfb void | ||
712 | .Xc | ||
713 | Three key triple DES in CBC, ECB, CFB and OFB modes respectively. | ||
714 | .It Fn EVP_desx_cbc void | ||
715 | DESX algorithm in CBC mode. | ||
716 | .It Fn EVP_rc4 void | ||
717 | RC4 stream cipher. | ||
718 | This is a variable key length cipher with default key length 128 bits. | ||
719 | .It Fn EVP_rc4_40 void | ||
720 | RC4 stream cipher with 40 bit key length. | ||
721 | This is obsolete and new code should use | ||
722 | .Fn EVP_rc4 | ||
723 | and the | ||
724 | .Fn EVP_CIPHER_CTX_set_key_length | ||
725 | function. | ||
726 | .It Xo | ||
727 | .Fn EVP_idea_cbc void , | ||
728 | .Fn EVP_idea_ecb void , | ||
729 | .Fn EVP_idea_cfb void , | ||
730 | .Fn EVP_idea_ofb void | ||
731 | .Xc | ||
732 | IDEA encryption algorithm in CBC, ECB, CFB and OFB modes respectively. | ||
733 | .It Xo | ||
734 | .Fn EVP_rc2_cbc void , | ||
735 | .Fn EVP_rc2_ecb void , | ||
736 | .Fn EVP_rc2_cfb void , | ||
737 | .Fn EVP_rc2_ofb void | ||
738 | .Xc | ||
739 | RC2 encryption algorithm in CBC, ECB, CFB and OFB modes respectively. | ||
740 | This is a variable key length cipher with an additional parameter called | ||
741 | "effective key bits" or "effective key length". | ||
742 | By default both are set to 128 bits. | ||
743 | .It Xo | ||
744 | .Fn EVP_rc2_40_cbc void , | ||
745 | .Fn EVP_rc2_64_cbc void | ||
746 | .Xc | ||
747 | RC2 algorithm in CBC mode with a default key length and effective key | ||
748 | length of 40 and 64 bits. | ||
749 | These are obsolete and new code should use | ||
750 | .Fn EVP_rc2_cbc , | ||
751 | .Fn EVP_CIPHER_CTX_set_key_length , | ||
752 | and | ||
753 | .Fn EVP_CIPHER_CTX_ctrl | ||
754 | to set the key length and effective key length. | ||
755 | .It Xo | ||
756 | .Fn EVP_bf_cbc void , | ||
757 | .Fn EVP_bf_ecb void , | ||
758 | .Fn EVP_bf_cfb void , | ||
759 | .Fn EVP_bf_ofb void | ||
760 | .Xc | ||
761 | Blowfish encryption algorithm in CBC, ECB, CFB and OFB modes | ||
762 | respectively. | ||
763 | This is a variable key length cipher. | ||
764 | .It Xo | ||
765 | .Fn EVP_cast5_cbc void , | ||
766 | .Fn EVP_cast5_ecb void , | ||
767 | .Fn EVP_cast5_cfb void , | ||
768 | .Fn EVP_cast5_ofb void | ||
769 | .Xc | ||
770 | CAST encryption algorithm in CBC, ECB, CFB and OFB modes respectively. | ||
771 | This is a variable key length cipher. | ||
772 | .It Xo | ||
773 | .Fn EVP_rc5_32_12_16_cbc void , | ||
774 | .Fn EVP_rc5_32_12_16_ecb void , | ||
775 | .Fn EVP_rc5_32_12_16_cfb void , | ||
776 | .Fn EVP_rc5_32_12_16_ofb void | ||
777 | .Xc | ||
778 | RC5 encryption algorithm in CBC, ECB, CFB and OFB modes respectively. | ||
779 | This is a variable key length cipher with an additional "number of | ||
780 | rounds" parameter. | ||
781 | By default the key length is set to 128 bits and 12 rounds. | ||
782 | .El | ||
783 | .Sh EXAMPLES | ||
784 | Get the number of rounds used in RC5: | ||
785 | .Bd -literal | ||
786 | int nrounds; | ||
787 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC5_ROUNDS, 0, &nrounds); | ||
788 | .Ed | ||
789 | .Pp | ||
790 | Get the RC2 effective key length: | ||
791 | .Bd -literal | ||
792 | int key_bits; | ||
793 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC2_KEY_BITS, 0, &key_bits); | ||
794 | .Ed | ||
795 | .Pp | ||
796 | Set the number of rounds used in RC5: | ||
797 | .Bd -literal | ||
798 | int nrounds; | ||
799 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC5_ROUNDS, nrounds, NULL); | ||
800 | .Ed | ||
801 | .Pp | ||
802 | Set the effective key length used in RC2: | ||
803 | .Bd -literal | ||
804 | int key_bits; | ||
805 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL); | ||
806 | .Ed | ||
807 | .Pp | ||
808 | Encrypt a string using blowfish: | ||
809 | .Bd -literal | ||
810 | int | ||
811 | do_crypt(char *outfile) | ||
812 | { | ||
813 | unsigned char outbuf[1024]; | ||
814 | int outlen, tmplen; | ||
815 | /* | ||
816 | * Bogus key and IV: we'd normally set these from | ||
817 | * another source. | ||
818 | */ | ||
819 | unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; | ||
820 | unsigned char iv[] = {1,2,3,4,5,6,7,8}; | ||
821 | const char intext[] = "Some Crypto Text"; | ||
822 | EVP_CIPHER_CTX ctx; | ||
823 | FILE *out; | ||
824 | EVP_CIPHER_CTX_init(&ctx); | ||
825 | EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv); | ||
826 | |||
827 | if (!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, | ||
828 | strlen(intext))) { | ||
829 | /* Error */ | ||
830 | return 0; | ||
831 | } | ||
832 | /* | ||
833 | * Buffer passed to EVP_EncryptFinal() must be after data just | ||
834 | * encrypted to avoid overwriting it. | ||
835 | */ | ||
836 | if (!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) { | ||
837 | /* Error */ | ||
838 | return 0; | ||
839 | } | ||
840 | outlen += tmplen; | ||
841 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
842 | /* | ||
843 | * Need binary mode for fopen because encrypted data is | ||
844 | * binary data. Also cannot use strlen() on it because | ||
845 | * it won't be NUL terminated and may contain embedded | ||
846 | * NULs. | ||
847 | */ | ||
848 | out = fopen(outfile, "wb"); | ||
849 | fwrite(outbuf, 1, outlen, out); | ||
850 | fclose(out); | ||
851 | return 1; | ||
852 | } | ||
853 | .Ed | ||
854 | .Pp | ||
855 | The ciphertext from the above example can be decrypted using the | ||
856 | .Xr openssl 1 | ||
857 | utility with the command line: | ||
858 | .Bd -literal | ||
859 | openssl bf -in cipher.bin -K 000102030405060708090A0B0C0D0E0F \e | ||
860 | -iv 0102030405060708 -d | ||
861 | .Ed | ||
862 | .Pp | ||
863 | General encryption, decryption function example using FILE I/O and RC2 | ||
864 | with an 80 bit key: | ||
865 | .Bd -literal | ||
866 | int | ||
867 | do_crypt(FILE *in, FILE *out, int do_encrypt) | ||
868 | { | ||
869 | /* Allow enough space in output buffer for additional block */ | ||
870 | inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; | ||
871 | int inlen, outlen; | ||
872 | /* | ||
873 | * Bogus key and IV: we'd normally set these from | ||
874 | * another source. | ||
875 | */ | ||
876 | unsigned char key[] = "0123456789"; | ||
877 | unsigned char iv[] = "12345678"; | ||
878 | |||
879 | /* Don't set key or IV because we will modify the parameters */ | ||
880 | EVP_CIPHER_CTX_init(&ctx); | ||
881 | EVP_CipherInit_ex(&ctx, EVP_rc2(), NULL, NULL, NULL, do_encrypt); | ||
882 | EVP_CIPHER_CTX_set_key_length(&ctx, 10); | ||
883 | /* We finished modifying parameters so now we can set key and IV */ | ||
884 | EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt); | ||
885 | |||
886 | for(;;) { | ||
887 | inlen = fread(inbuf, 1, 1024, in); | ||
888 | if (inlen <= 0) | ||
889 | break; | ||
890 | if (!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, | ||
891 | inlen)) { | ||
892 | /* Error */ | ||
893 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
894 | return 0; | ||
895 | } | ||
896 | fwrite(outbuf, 1, outlen, out); | ||
897 | } | ||
898 | if (!EVP_CipherFinal_ex(&ctx, outbuf, &outlen)) { | ||
899 | /* Error */ | ||
900 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
901 | return 0; | ||
902 | } | ||
903 | fwrite(outbuf, 1, outlen, out); | ||
904 | |||
905 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
906 | return 1; | ||
907 | } | ||
908 | .Ed | ||
909 | .Sh SEE ALSO | ||
910 | .Xr evp 3 | ||
911 | .Sh HISTORY | ||
912 | .Fn EVP_CIPHER_CTX_init , | ||
913 | .Fn EVP_EncryptInit_ex , | ||
914 | .Fn EVP_EncryptFinal_ex , | ||
915 | .Fn EVP_DecryptInit_ex , | ||
916 | .Fn EVP_DecryptFinal_ex , | ||
917 | .Fn EVP_CipherInit_ex , | ||
918 | .Fn EVP_CipherFinal_ex , | ||
919 | and | ||
920 | .Fn EVP_CIPHER_CTX_set_padding | ||
921 | appeared in OpenSSL 0.9.7. | ||
922 | .Sh BUGS | ||
923 | For RC5 the number of rounds can currently only be set to 8, 12 or 16. | ||
924 | This is a limitation of the current RC5 code rather than the EVP | ||
925 | interface. | ||
926 | .Pp | ||
927 | .Dv EVP_MAX_KEY_LENGTH | ||
928 | and | ||
929 | .Dv EVP_MAX_IV_LENGTH | ||
930 | only refer to the internal ciphers with default key lengths. | ||
931 | If custom ciphers exceed these values the results are unpredictable. | ||
932 | This is because it has become standard practice to define a generic key | ||
933 | as a fixed unsigned char array containing | ||
934 | .Dv EVP_MAX_KEY_LENGTH | ||
935 | bytes. | ||
936 | .Pp | ||
937 | The ASN1 code is incomplete (and sometimes inaccurate) it has only been | ||
938 | tested for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC | ||
939 | mode. | ||