summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/man/EVP_EncryptInit.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/man/EVP_EncryptInit.3')
-rw-r--r--src/lib/libcrypto/man/EVP_EncryptInit.3939
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
264The EVP cipher routines are a high level interface to certain symmetric
265ciphers.
266.Pp
267.Fn EVP_CIPHER_CTX_init
268initializes the cipher context
269.Fa ctx .
270.Pp
271.Fn EVP_EncryptInit_ex
272sets up the cipher context
273.Fa ctx
274for encryption with cipher
275.Fa type
276from
277.Vt ENGINE
278.Fa impl .
279.Fa ctx
280must be initialized before calling this function.
281.Fa type
282is normally supplied by a function such as
283.Fn EVP_aes_256_cbc .
284If
285.Fa impl
286is
287.Dv NULL ,
288then the default implementation is used.
289.Fa key
290is the symmetric key to use and
291.Fa iv
292is the IV to use (if necessary), the actual number of bytes used for the
293key and IV depends on the cipher.
294It is possible to set all parameters to
295.Dv NULL
296except
297.Fa type
298in an initial call and supply the remaining parameters in subsequent
299calls, all of which have
300.Fa type
301set to
302.Dv NULL .
303This is done when the default cipher parameters are not appropriate.
304.Pp
305.Fn EVP_EncryptUpdate
306encrypts
307.Fa inl
308bytes from the buffer
309.Fa in
310and writes the encrypted version to
311.Fa out .
312This function can be called multiple times to encrypt successive blocks
313of data.
314The amount of data written depends on the block alignment of the
315encrypted data: as a result the amount of data written may be anything
316from zero bytes to (inl + cipher_block_size - 1) so
317.Fa outl
318should contain sufficient room.
319The actual number of bytes written is placed in
320.Fa outl .
321.Pp
322If padding is enabled (the default) then
323.Fn EVP_EncryptFinal_ex
324encrypts the "final" data, that is any data that remains in a partial
325block.
326It uses NOTES (aka PKCS padding).
327The encrypted final data is written to
328.Fa out
329which should have sufficient space for one cipher block.
330The number of bytes written is placed in
331.Fa outl .
332After this function is called the encryption operation is finished and
333no further calls to
334.Fn EVP_EncryptUpdate
335should be made.
336.Pp
337If padding is disabled then
338.Fn EVP_EncryptFinal_ex
339will not encrypt any more data and it will return an error if any data
340remains in a partial block: that is if the total data length is not a
341multiple of the block size.
342.Pp
343.Fn EVP_DecryptInit_ex ,
344.Fn EVP_DecryptUpdate ,
345and
346.Fn EVP_DecryptFinal_ex
347are the corresponding decryption operations.
348.Fn EVP_DecryptFinal
349will return an error code if padding is enabled and the final block is
350not correctly formatted.
351The parameters and restrictions are identical to the encryption
352operations except that if padding is enabled the decrypted data buffer
353.Fa out
354passed to
355.Fn EVP_DecryptUpdate
356should have sufficient room for (inl + cipher_block_size) bytes
357unless the cipher block size is 1 in which case
358.Fa inl
359bytes is sufficient.
360.Pp
361.Fn EVP_CipherInit_ex ,
362.Fn EVP_CipherUpdate ,
363and
364.Fn EVP_CipherFinal_ex
365are functions that can be used for decryption or encryption.
366The operation performed depends on the value of the
367.Fa enc
368parameter.
369It should be set to 1 for encryption, 0 for decryption and -1 to leave
370the value unchanged (the actual value of
371.Fa enc
372being supplied in a previous call).
373.Pp
374.Fn EVP_CIPHER_CTX_cleanup
375clears all information from a cipher context and free up any allocated
376memory associate with it.
377It should be called after all operations using a cipher are complete so
378sensitive information does not remain in memory.
379.Pp
380.Fn EVP_EncryptInit ,
381.Fn EVP_DecryptInit ,
382and
383.Fn EVP_CipherInit
384behave in a similar way to
385.Fn EVP_EncryptInit_ex ,
386.Fn EVP_DecryptInit_ex ,
387and
388.Fn EVP_CipherInit_ex
389except the
390.Fa ctx
391parameter does not need to be initialized and they always use the
392default cipher implementation.
393.Pp
394.Fn EVP_EncryptFinal ,
395.Fn EVP_DecryptFinal ,
396and
397.Fn EVP_CipherFinal
398are identical to
399.Fn EVP_EncryptFinal_ex ,
400.Fn EVP_DecryptFinal_ex ,
401and
402.Fn EVP_CipherFinal_ex .
403In previous releases of OpenSSL, they also used to clean up the
404.Fa ctx ,
405but this is no longer done and
406.Fn EVP_CIPHER_CTX_cleanup
407must be called to free any context resources.
408.Pp
409.Fn EVP_get_cipherbyname ,
410.Fn EVP_get_cipherbynid ,
411and
412.Fn EVP_get_cipherbyobj
413return an
414.Vt EVP_CIPHER
415structure when passed a cipher name, a NID or an
416.Vt ASN1_OBJECT
417structure.
418.Pp
419.Fn EVP_CIPHER_nid
420and
421.Fn EVP_CIPHER_CTX_nid
422return the NID of a cipher when passed an
423.Vt EVP_CIPHER
424or
425.Vt EVP_CIPHER_CTX
426structure.
427The actual NID value is an internal value which may not have a
428corresponding OBJECT IDENTIFIER.
429.Pp
430.Fn EVP_CIPHER_CTX_set_padding
431enables or disables padding.
432By default encryption operations are padded using standard block padding
433and the padding is checked and removed when decrypting.
434If the
435.Sy padding
436parameter is zero, then no padding is performed, the total amount of data
437encrypted or decrypted must then be a multiple of the block size or an
438error will occur.
439.Pp
440.Fn EVP_CIPHER_key_length
441and
442.Fn EVP_CIPHER_CTX_key_length
443return the key length of a cipher when passed an
444.Vt EVP_CIPHER
445or
446.Vt EVP_CIPHER_CTX
447structure.
448The constant
449.Dv EVP_MAX_KEY_LENGTH
450is the maximum key length for all ciphers.
451Note: although
452.Fn EVP_CIPHER_key_length
453is fixed for a given cipher, the value of
454.Fn EVP_CIPHER_CTX_key_length
455may be different for variable key length ciphers.
456.Pp
457.Fn EVP_CIPHER_CTX_set_key_length
458sets the key length of the cipher ctx.
459If the cipher is a fixed length cipher, then attempting to set the key
460length to any value other than the fixed value is an error.
461.Pp
462.Fn EVP_CIPHER_iv_length
463and
464.Fn EVP_CIPHER_CTX_iv_length
465return the IV length of a cipher when passed an
466.Vt EVP_CIPHER
467or
468.Vt EVP_CIPHER_CTX .
469It will return zero if the cipher does not use an IV.
470The constant
471.Dv EVP_MAX_IV_LENGTH
472is the maximum IV length for all ciphers.
473.Pp
474.Fn EVP_CIPHER_block_size
475and
476.Fn EVP_CIPHER_CTX_block_size
477return the block size of a cipher when passed an
478.Vt EVP_CIPHER
479or
480.Vt EVP_CIPHER_CTX
481structure.
482The constant
483.Dv EVP_MAX_IV_LENGTH
484is also the maximum block length for all ciphers.
485.Pp
486.Fn EVP_CIPHER_type
487and
488.Fn EVP_CIPHER_CTX_type
489return the type of the passed cipher or context.
490This "type" is the actual NID of the cipher OBJECT IDENTIFIER as such it
491ignores the cipher parameters and 40 bit RC2 and 128 bit RC2 have the
492same NID. If the cipher does not have an object identifier or does not
493have ASN1 support this function will return
494.Dv NID_undef .
495.Pp
496.Fn EVP_CIPHER_CTX_cipher
497returns the
498.Vt EVP_CIPHER
499structure when passed an
500.Vt EVP_CIPHER_CTX
501structure.
502.Pp
503.Fn EVP_CIPHER_mode
504and
505.Fn EVP_CIPHER_CTX_mode
506return the block cipher mode:
507.Dv EVP_CIPH_ECB_MODE ,
508.Dv EVP_CIPH_CBC_MODE ,
509.Dv EVP_CIPH_CFB_MODE ,
510or
511.Dv EVP_CIPH_OFB_MODE .
512If the cipher is a stream cipher then
513.Dv EVP_CIPH_STREAM_CIPHER
514is returned.
515.Pp
516.Fn EVP_CIPHER_param_to_asn1
517sets the AlgorithmIdentifier "parameter" based on the passed cipher.
518This will typically include any parameters and an IV.
519The cipher IV (if any) must be set when this call is made.
520This call should be made before the cipher is actually "used" (before any
521.Fn EVP_EncryptUpdate ,
522.Fn EVP_DecryptUpdate
523calls for example).
524This function may fail if the cipher does not have any ASN1 support.
525.Pp
526.Fn EVP_CIPHER_asn1_to_param
527sets the cipher parameters based on an ASN1 AlgorithmIdentifier
528"parameter".
529The precise effect depends on the cipher.
530In the case of RC2, for example, it will set the IV and effective
531key length.
532This function should be called after the base cipher type is set but
533before the key is set.
534For example
535.Fn EVP_CipherInit
536will be called with the IV and key set to
537.Dv NULL ,
538.Fn EVP_CIPHER_asn1_to_param
539will be called and finally
540.Fn EVP_CipherInit
541again with all parameters except the key set to
542.Dv NULL .
543It is possible for this function to fail if the cipher does not
544have any ASN1 support or the parameters cannot be set (for example
545the RC2 effective key length is not supported).
546.Pp
547.Fn EVP_CIPHER_CTX_ctrl
548allows various cipher specific parameters to be determined and set.
549Currently only the RC2 effective key length and the number of rounds of
550RC5 can be set.
551.Sh RETURN VALUES
552.Fn EVP_EncryptInit_ex ,
553.Fn EVP_EncryptUpdate ,
554and
555.Fn EVP_EncryptFinal_ex
556return 1 for success and 0 for failure.
557.Pp
558.Fn EVP_DecryptInit_ex
559and
560.Fn EVP_DecryptUpdate
561return 1 for success and 0 for failure.
562.Fn EVP_DecryptFinal_ex
563returns 0 if the decrypt failed or 1 for success.
564.Pp
565.Fn EVP_CipherInit_ex
566and
567.Fn EVP_CipherUpdate
568return 1 for success and 0 for failure.
569.Fn EVP_CipherFinal_ex
570returns 0 for a decryption failure or 1 for success.
571.Pp
572.Fn EVP_CIPHER_CTX_cleanup
573returns 1 for success and 0 for failure.
574.Pp
575.Fn EVP_get_cipherbyname ,
576.Fn EVP_get_cipherbynid ,
577and
578.Fn EVP_get_cipherbyobj
579return an
580.Vt EVP_CIPHER
581structure or
582.Dv NULL
583on error.
584.Pp
585.Fn EVP_CIPHER_nid
586and
587.Fn EVP_CIPHER_CTX_nid
588return a NID.
589.Pp
590.Fn EVP_CIPHER_block_size
591and
592.Fn EVP_CIPHER_CTX_block_size
593return the block size.
594.Pp
595.Fn EVP_CIPHER_key_length
596and
597.Fn EVP_CIPHER_CTX_key_length
598return the key length.
599.Pp
600.Fn EVP_CIPHER_CTX_set_padding
601always returns 1.
602.Pp
603.Fn EVP_CIPHER_iv_length
604and
605.Fn EVP_CIPHER_CTX_iv_length
606return the IV length or zero if the cipher does not use an IV.
607.Pp
608.Fn EVP_CIPHER_type
609and
610.Fn EVP_CIPHER_CTX_type
611return the NID of the cipher's OBJECT IDENTIFIER or
612.Dv NID_undef
613if it has no defined OBJECT IDENTIFIER.
614.Pp
615.Fn EVP_CIPHER_CTX_cipher
616returns an
617.Vt EVP_CIPHER
618structure.
619.Pp
620.Fn EVP_CIPHER_param_to_asn1
621and
622.Fn EVP_CIPHER_asn1_to_param
623return 1 for success or 0 for failure.
624.Pp
625Where possible the EVP interface to symmetric ciphers should be
626used in preference to the low level interfaces.
627This is because the code then becomes transparent to the cipher used and
628much more flexible.
629.Pp
630PKCS padding works by adding n padding bytes of value n to make the
631total length of the encrypted data a multiple of the block size.
632Padding is always added so if the data is already a multiple of the
633block size n will equal the block size.
634For example if the block size is 8 and 11 bytes are to be encrypted then
6355 padding bytes of value 5 will be added.
636.Pp
637When decrypting the final block is checked to see if it has the correct
638form.
639.Pp
640Although the decryption operation can produce an error if padding is
641enabled, it is not a strong test that the input data or key is correct.
642A random block has better than 1 in 256 chance of being of the correct
643format and problems with the input data earlier on will not produce a
644final decrypt error.
645.Pp
646If padding is disabled then the decryption operation will always succeed
647if the total amount of data decrypted is a multiple of the block size.
648.Pp
649The functions
650.Fn EVP_EncryptInit ,
651.Fn EVP_EncryptFinal ,
652.Fn EVP_DecryptInit ,
653.Fn EVP_CipherInit ,
654and
655.Fn EVP_CipherFinal
656are obsolete but are retained for compatibility with existing code.
657New 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 ,
663and
664.Fn EVP_CipherFinal_ex
665because they can reuse an existing context without allocating and
666freeing it up on each call.
667.Sh CIPHER LISTING
668All algorithms have a fixed key length unless otherwise stated.
669.Bl -tag -width Ds
670.It Fn EVP_enc_null void
671Null 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
678128-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
685192-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
692256-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
699DES 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
706Two 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
713Three key triple DES in CBC, ECB, CFB and OFB modes respectively.
714.It Fn EVP_desx_cbc void
715DESX algorithm in CBC mode.
716.It Fn EVP_rc4 void
717RC4 stream cipher.
718This is a variable key length cipher with default key length 128 bits.
719.It Fn EVP_rc4_40 void
720RC4 stream cipher with 40 bit key length.
721This is obsolete and new code should use
722.Fn EVP_rc4
723and the
724.Fn EVP_CIPHER_CTX_set_key_length
725function.
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
732IDEA 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
739RC2 encryption algorithm in CBC, ECB, CFB and OFB modes respectively.
740This is a variable key length cipher with an additional parameter called
741"effective key bits" or "effective key length".
742By 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
747RC2 algorithm in CBC mode with a default key length and effective key
748length of 40 and 64 bits.
749These are obsolete and new code should use
750.Fn EVP_rc2_cbc ,
751.Fn EVP_CIPHER_CTX_set_key_length ,
752and
753.Fn EVP_CIPHER_CTX_ctrl
754to 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
761Blowfish encryption algorithm in CBC, ECB, CFB and OFB modes
762respectively.
763This 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
770CAST encryption algorithm in CBC, ECB, CFB and OFB modes respectively.
771This 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
778RC5 encryption algorithm in CBC, ECB, CFB and OFB modes respectively.
779This is a variable key length cipher with an additional "number of
780rounds" parameter.
781By default the key length is set to 128 bits and 12 rounds.
782.El
783.Sh EXAMPLES
784Get the number of rounds used in RC5:
785.Bd -literal
786int nrounds;
787EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC5_ROUNDS, 0, &nrounds);
788.Ed
789.Pp
790Get the RC2 effective key length:
791.Bd -literal
792int key_bits;
793EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC2_KEY_BITS, 0, &key_bits);
794.Ed
795.Pp
796Set the number of rounds used in RC5:
797.Bd -literal
798int nrounds;
799EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC5_ROUNDS, nrounds, NULL);
800.Ed
801.Pp
802Set the effective key length used in RC2:
803.Bd -literal
804int key_bits;
805EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
806.Ed
807.Pp
808Encrypt a string using blowfish:
809.Bd -literal
810int
811do_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
855The ciphertext from the above example can be decrypted using the
856.Xr openssl 1
857utility with the command line:
858.Bd -literal
859openssl bf -in cipher.bin -K 000102030405060708090A0B0C0D0E0F \e
860 -iv 0102030405060708 -d
861.Ed
862.Pp
863General encryption, decryption function example using FILE I/O and RC2
864with an 80 bit key:
865.Bd -literal
866int
867do_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 ,
919and
920.Fn EVP_CIPHER_CTX_set_padding
921appeared in OpenSSL 0.9.7.
922.Sh BUGS
923For RC5 the number of rounds can currently only be set to 8, 12 or 16.
924This is a limitation of the current RC5 code rather than the EVP
925interface.
926.Pp
927.Dv EVP_MAX_KEY_LENGTH
928and
929.Dv EVP_MAX_IV_LENGTH
930only refer to the internal ciphers with default key lengths.
931If custom ciphers exceed these values the results are unpredictable.
932This is because it has become standard practice to define a generic key
933as a fixed unsigned char array containing
934.Dv EVP_MAX_KEY_LENGTH
935bytes.
936.Pp
937The ASN1 code is incomplete (and sometimes inaccurate) it has only been
938tested for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC
939mode.