xref: /freebsd/crypto/openssl/doc/man3/EVP_EncryptInit.pod (revision b2bf0c7e5f4037d63458def91a026592468afd2f)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5e71b7053SJung-uk KimEVP_CIPHER_CTX_new,
6e71b7053SJung-uk KimEVP_CIPHER_CTX_reset,
7e71b7053SJung-uk KimEVP_CIPHER_CTX_free,
8e71b7053SJung-uk KimEVP_EncryptInit_ex,
9e71b7053SJung-uk KimEVP_EncryptUpdate,
10e71b7053SJung-uk KimEVP_EncryptFinal_ex,
11e71b7053SJung-uk KimEVP_DecryptInit_ex,
12e71b7053SJung-uk KimEVP_DecryptUpdate,
13e71b7053SJung-uk KimEVP_DecryptFinal_ex,
14e71b7053SJung-uk KimEVP_CipherInit_ex,
15e71b7053SJung-uk KimEVP_CipherUpdate,
16e71b7053SJung-uk KimEVP_CipherFinal_ex,
17e71b7053SJung-uk KimEVP_CIPHER_CTX_set_key_length,
18e71b7053SJung-uk KimEVP_CIPHER_CTX_ctrl,
19e71b7053SJung-uk KimEVP_EncryptInit,
20e71b7053SJung-uk KimEVP_EncryptFinal,
21e71b7053SJung-uk KimEVP_DecryptInit,
22e71b7053SJung-uk KimEVP_DecryptFinal,
23e71b7053SJung-uk KimEVP_CipherInit,
24e71b7053SJung-uk KimEVP_CipherFinal,
25e71b7053SJung-uk KimEVP_get_cipherbyname,
26e71b7053SJung-uk KimEVP_get_cipherbynid,
27e71b7053SJung-uk KimEVP_get_cipherbyobj,
28e71b7053SJung-uk KimEVP_CIPHER_nid,
29e71b7053SJung-uk KimEVP_CIPHER_block_size,
30e71b7053SJung-uk KimEVP_CIPHER_key_length,
31e71b7053SJung-uk KimEVP_CIPHER_iv_length,
32e71b7053SJung-uk KimEVP_CIPHER_flags,
33e71b7053SJung-uk KimEVP_CIPHER_mode,
34e71b7053SJung-uk KimEVP_CIPHER_type,
35e71b7053SJung-uk KimEVP_CIPHER_CTX_cipher,
36e71b7053SJung-uk KimEVP_CIPHER_CTX_nid,
37e71b7053SJung-uk KimEVP_CIPHER_CTX_block_size,
38e71b7053SJung-uk KimEVP_CIPHER_CTX_key_length,
39e71b7053SJung-uk KimEVP_CIPHER_CTX_iv_length,
40e71b7053SJung-uk KimEVP_CIPHER_CTX_get_app_data,
41e71b7053SJung-uk KimEVP_CIPHER_CTX_set_app_data,
42e71b7053SJung-uk KimEVP_CIPHER_CTX_type,
43e71b7053SJung-uk KimEVP_CIPHER_CTX_flags,
44e71b7053SJung-uk KimEVP_CIPHER_CTX_mode,
45e71b7053SJung-uk KimEVP_CIPHER_param_to_asn1,
46e71b7053SJung-uk KimEVP_CIPHER_asn1_to_param,
47e71b7053SJung-uk KimEVP_CIPHER_CTX_set_padding,
48e71b7053SJung-uk KimEVP_enc_null
49e71b7053SJung-uk Kim- EVP cipher routines
50e71b7053SJung-uk Kim
51e71b7053SJung-uk Kim=head1 SYNOPSIS
52e71b7053SJung-uk Kim
53e71b7053SJung-uk Kim=for comment generic
54e71b7053SJung-uk Kim
55e71b7053SJung-uk Kim #include <openssl/evp.h>
56e71b7053SJung-uk Kim
57e71b7053SJung-uk Kim EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void);
58e71b7053SJung-uk Kim int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx);
59e71b7053SJung-uk Kim void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx);
60e71b7053SJung-uk Kim
61e71b7053SJung-uk Kim int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
62e71b7053SJung-uk Kim                        ENGINE *impl, const unsigned char *key, const unsigned char *iv);
63e71b7053SJung-uk Kim int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
64e71b7053SJung-uk Kim                       int *outl, const unsigned char *in, int inl);
65e71b7053SJung-uk Kim int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
66e71b7053SJung-uk Kim
67e71b7053SJung-uk Kim int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
68e71b7053SJung-uk Kim                        ENGINE *impl, const unsigned char *key, const unsigned char *iv);
69e71b7053SJung-uk Kim int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
70e71b7053SJung-uk Kim                       int *outl, const unsigned char *in, int inl);
71e71b7053SJung-uk Kim int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
72e71b7053SJung-uk Kim
73e71b7053SJung-uk Kim int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
74e71b7053SJung-uk Kim                       ENGINE *impl, const unsigned char *key, const unsigned char *iv, int enc);
75e71b7053SJung-uk Kim int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
76e71b7053SJung-uk Kim                      int *outl, const unsigned char *in, int inl);
77e71b7053SJung-uk Kim int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
78e71b7053SJung-uk Kim
79e71b7053SJung-uk Kim int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
80e71b7053SJung-uk Kim                     const unsigned char *key, const unsigned char *iv);
81e71b7053SJung-uk Kim int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
82e71b7053SJung-uk Kim
83e71b7053SJung-uk Kim int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
84e71b7053SJung-uk Kim                     const unsigned char *key, const unsigned char *iv);
85e71b7053SJung-uk Kim int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
86e71b7053SJung-uk Kim
87e71b7053SJung-uk Kim int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
88e71b7053SJung-uk Kim                    const unsigned char *key, const unsigned char *iv, int enc);
89e71b7053SJung-uk Kim int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
90e71b7053SJung-uk Kim
91e71b7053SJung-uk Kim int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
92e71b7053SJung-uk Kim int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
93e71b7053SJung-uk Kim int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
94e71b7053SJung-uk Kim int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key);
95e71b7053SJung-uk Kim
96e71b7053SJung-uk Kim const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
97e71b7053SJung-uk Kim const EVP_CIPHER *EVP_get_cipherbynid(int nid);
98e71b7053SJung-uk Kim const EVP_CIPHER *EVP_get_cipherbyobj(const ASN1_OBJECT *a);
99e71b7053SJung-uk Kim
100e71b7053SJung-uk Kim int EVP_CIPHER_nid(const EVP_CIPHER *e);
101e71b7053SJung-uk Kim int EVP_CIPHER_block_size(const EVP_CIPHER *e);
102e71b7053SJung-uk Kim int EVP_CIPHER_key_length(const EVP_CIPHER *e);
103e71b7053SJung-uk Kim int EVP_CIPHER_iv_length(const EVP_CIPHER *e);
104e71b7053SJung-uk Kim unsigned long EVP_CIPHER_flags(const EVP_CIPHER *e);
105e71b7053SJung-uk Kim unsigned long EVP_CIPHER_mode(const EVP_CIPHER *e);
106e71b7053SJung-uk Kim int EVP_CIPHER_type(const EVP_CIPHER *ctx);
107e71b7053SJung-uk Kim
108e71b7053SJung-uk Kim const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx);
109e71b7053SJung-uk Kim int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx);
110e71b7053SJung-uk Kim int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx);
111e71b7053SJung-uk Kim int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx);
112e71b7053SJung-uk Kim int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx);
113e71b7053SJung-uk Kim void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx);
114e71b7053SJung-uk Kim void EVP_CIPHER_CTX_set_app_data(const EVP_CIPHER_CTX *ctx, void *data);
115e71b7053SJung-uk Kim int EVP_CIPHER_CTX_type(const EVP_CIPHER_CTX *ctx);
116e71b7053SJung-uk Kim int EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx);
117e71b7053SJung-uk Kim
118e71b7053SJung-uk Kim int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
119e71b7053SJung-uk Kim int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
120e71b7053SJung-uk Kim
121e71b7053SJung-uk Kim=head1 DESCRIPTION
122e71b7053SJung-uk Kim
12358f35182SJung-uk KimThe EVP cipher routines are a high-level interface to certain
124e71b7053SJung-uk Kimsymmetric ciphers.
125e71b7053SJung-uk Kim
126e71b7053SJung-uk KimEVP_CIPHER_CTX_new() creates a cipher context.
127e71b7053SJung-uk Kim
128e71b7053SJung-uk KimEVP_CIPHER_CTX_free() clears all information from a cipher context
129e71b7053SJung-uk Kimand free up any allocated memory associate with it, including B<ctx>
130e71b7053SJung-uk Kimitself. This function should be called after all operations using a
131e71b7053SJung-uk Kimcipher are complete so sensitive information does not remain in
132e71b7053SJung-uk Kimmemory.
133e71b7053SJung-uk Kim
134e71b7053SJung-uk KimEVP_EncryptInit_ex() sets up cipher context B<ctx> for encryption
135e71b7053SJung-uk Kimwith cipher B<type> from ENGINE B<impl>. B<ctx> must be created
136e71b7053SJung-uk Kimbefore calling this function. B<type> is normally supplied
137e71b7053SJung-uk Kimby a function such as EVP_aes_256_cbc(). If B<impl> is NULL then the
138e71b7053SJung-uk Kimdefault implementation is used. B<key> is the symmetric key to use
139e71b7053SJung-uk Kimand B<iv> is the IV to use (if necessary), the actual number of bytes
140e71b7053SJung-uk Kimused for the key and IV depends on the cipher. It is possible to set
141e71b7053SJung-uk Kimall parameters to NULL except B<type> in an initial call and supply
142e71b7053SJung-uk Kimthe remaining parameters in subsequent calls, all of which have B<type>
143e71b7053SJung-uk Kimset to NULL. This is done when the default cipher parameters are not
144e71b7053SJung-uk Kimappropriate.
145e71b7053SJung-uk Kim
146e71b7053SJung-uk KimEVP_EncryptUpdate() encrypts B<inl> bytes from the buffer B<in> and
147e71b7053SJung-uk Kimwrites the encrypted version to B<out>. This function can be called
148e71b7053SJung-uk Kimmultiple times to encrypt successive blocks of data. The amount
14958f35182SJung-uk Kimof data written depends on the block alignment of the encrypted data.
15058f35182SJung-uk KimFor most ciphers and modes, the amount of data written can be anything
15158f35182SJung-uk Kimfrom zero bytes to (inl + cipher_block_size - 1) bytes.
15258f35182SJung-uk KimFor wrap cipher modes, the amount of data written can be anything
15358f35182SJung-uk Kimfrom zero bytes to (inl + cipher_block_size) bytes.
15458f35182SJung-uk KimFor stream ciphers, the amount of data written can be anything from zero
15558f35182SJung-uk Kimbytes to inl bytes.
15658f35182SJung-uk KimThus, B<out> should contain sufficient room for the operation being performed.
15758f35182SJung-uk KimThe actual number of bytes written is placed in B<outl>. It also
158e71b7053SJung-uk Kimchecks if B<in> and B<out> are partially overlapping, and if they are
159e71b7053SJung-uk Kim0 is returned to indicate failure.
160e71b7053SJung-uk Kim
161e71b7053SJung-uk KimIf padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts
162e71b7053SJung-uk Kimthe "final" data, that is any data that remains in a partial block.
163e71b7053SJung-uk KimIt uses standard block padding (aka PKCS padding) as described in
164e71b7053SJung-uk Kimthe NOTES section, below. The encrypted
165e71b7053SJung-uk Kimfinal data is written to B<out> which should have sufficient space for
166e71b7053SJung-uk Kimone cipher block. The number of bytes written is placed in B<outl>. After
167e71b7053SJung-uk Kimthis function is called the encryption operation is finished and no further
168e71b7053SJung-uk Kimcalls to EVP_EncryptUpdate() should be made.
169e71b7053SJung-uk Kim
170e71b7053SJung-uk KimIf padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more
171e71b7053SJung-uk Kimdata and it will return an error if any data remains in a partial block:
172e71b7053SJung-uk Kimthat is if the total data length is not a multiple of the block size.
173e71b7053SJung-uk Kim
174e71b7053SJung-uk KimEVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are the
175e71b7053SJung-uk Kimcorresponding decryption operations. EVP_DecryptFinal() will return an
176e71b7053SJung-uk Kimerror code if padding is enabled and the final block is not correctly
177e71b7053SJung-uk Kimformatted. The parameters and restrictions are identical to the encryption
178e71b7053SJung-uk Kimoperations except that if padding is enabled the decrypted data buffer B<out>
179e71b7053SJung-uk Kimpassed to EVP_DecryptUpdate() should have sufficient room for
180e71b7053SJung-uk Kim(B<inl> + cipher_block_size) bytes unless the cipher block size is 1 in
181e71b7053SJung-uk Kimwhich case B<inl> bytes is sufficient.
182e71b7053SJung-uk Kim
183e71b7053SJung-uk KimEVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex() are
184e71b7053SJung-uk Kimfunctions that can be used for decryption or encryption. The operation
185e71b7053SJung-uk Kimperformed depends on the value of the B<enc> parameter. It should be set
186e71b7053SJung-uk Kimto 1 for encryption, 0 for decryption and -1 to leave the value unchanged
187e71b7053SJung-uk Kim(the actual value of 'enc' being supplied in a previous call).
188e71b7053SJung-uk Kim
189e71b7053SJung-uk KimEVP_CIPHER_CTX_reset() clears all information from a cipher context
190e71b7053SJung-uk Kimand free up any allocated memory associate with it, except the B<ctx>
191e71b7053SJung-uk Kimitself. This function should be called anytime B<ctx> is to be reused
192e71b7053SJung-uk Kimfor another EVP_CipherInit() / EVP_CipherUpdate() / EVP_CipherFinal()
193e71b7053SJung-uk Kimseries of calls.
194e71b7053SJung-uk Kim
195e71b7053SJung-uk KimEVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit() behave in a
196e71b7053SJung-uk Kimsimilar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex() and
197e71b7053SJung-uk KimEVP_CipherInit_ex() except they always use the default cipher implementation.
198e71b7053SJung-uk Kim
199e71b7053SJung-uk KimEVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal() are
200e71b7053SJung-uk Kimidentical to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and
201e71b7053SJung-uk KimEVP_CipherFinal_ex(). In previous releases they also cleaned up
202e71b7053SJung-uk Kimthe B<ctx>, but this is no longer done and EVP_CIPHER_CTX_clean()
203e71b7053SJung-uk Kimmust be called to free any context resources.
204e71b7053SJung-uk Kim
205e71b7053SJung-uk KimEVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
206e71b7053SJung-uk Kimreturn an EVP_CIPHER structure when passed a cipher name, a NID or an
207e71b7053SJung-uk KimASN1_OBJECT structure.
208e71b7053SJung-uk Kim
209e71b7053SJung-uk KimEVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return the NID of a cipher when
210e71b7053SJung-uk Kimpassed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX> structure.  The actual NID
211e71b7053SJung-uk Kimvalue is an internal value which may not have a corresponding OBJECT
212e71b7053SJung-uk KimIDENTIFIER.
213e71b7053SJung-uk Kim
214e71b7053SJung-uk KimEVP_CIPHER_CTX_set_padding() enables or disables padding. This
215e71b7053SJung-uk Kimfunction should be called after the context is set up for encryption
216e71b7053SJung-uk Kimor decryption with EVP_EncryptInit_ex(), EVP_DecryptInit_ex() or
217e71b7053SJung-uk KimEVP_CipherInit_ex(). By default encryption operations are padded using
218e71b7053SJung-uk Kimstandard block padding and the padding is checked and removed when
219e71b7053SJung-uk Kimdecrypting. If the B<pad> parameter is zero then no padding is
220e71b7053SJung-uk Kimperformed, the total amount of data encrypted or decrypted must then
221e71b7053SJung-uk Kimbe a multiple of the block size or an error will occur.
222e71b7053SJung-uk Kim
223e71b7053SJung-uk KimEVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
224e71b7053SJung-uk Kimlength of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>
225e71b7053SJung-uk Kimstructure. The constant B<EVP_MAX_KEY_LENGTH> is the maximum key length
226e71b7053SJung-uk Kimfor all ciphers. Note: although EVP_CIPHER_key_length() is fixed for a
227e71b7053SJung-uk Kimgiven cipher, the value of EVP_CIPHER_CTX_key_length() may be different
228e71b7053SJung-uk Kimfor variable key length ciphers.
229e71b7053SJung-uk Kim
230e71b7053SJung-uk KimEVP_CIPHER_CTX_set_key_length() sets the key length of the cipher ctx.
231e71b7053SJung-uk KimIf the cipher is a fixed length cipher then attempting to set the key
232e71b7053SJung-uk Kimlength to any value other than the fixed value is an error.
233e71b7053SJung-uk Kim
234e71b7053SJung-uk KimEVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
235e71b7053SJung-uk Kimlength of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>.
236e71b7053SJung-uk KimIt will return zero if the cipher does not use an IV.  The constant
237e71b7053SJung-uk KimB<EVP_MAX_IV_LENGTH> is the maximum IV length for all ciphers.
238e71b7053SJung-uk Kim
239e71b7053SJung-uk KimEVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the block
240e71b7053SJung-uk Kimsize of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>
241e71b7053SJung-uk Kimstructure. The constant B<EVP_MAX_BLOCK_LENGTH> is also the maximum block
242e71b7053SJung-uk Kimlength for all ciphers.
243e71b7053SJung-uk Kim
244e71b7053SJung-uk KimEVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the type of the passed
245e71b7053SJung-uk Kimcipher or context. This "type" is the actual NID of the cipher OBJECT
246e71b7053SJung-uk KimIDENTIFIER as such it ignores the cipher parameters and 40 bit RC2 and
247e71b7053SJung-uk Kim128 bit RC2 have the same NID. If the cipher does not have an object
248e71b7053SJung-uk Kimidentifier or does not have ASN1 support this function will return
249e71b7053SJung-uk KimB<NID_undef>.
250e71b7053SJung-uk Kim
251e71b7053SJung-uk KimEVP_CIPHER_CTX_cipher() returns the B<EVP_CIPHER> structure when passed
252e71b7053SJung-uk Kiman B<EVP_CIPHER_CTX> structure.
253e71b7053SJung-uk Kim
254e71b7053SJung-uk KimEVP_CIPHER_mode() and EVP_CIPHER_CTX_mode() return the block cipher mode:
255e71b7053SJung-uk KimEVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE, EVP_CIPH_OFB_MODE,
256e71b7053SJung-uk KimEVP_CIPH_CTR_MODE, EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE, EVP_CIPH_XTS_MODE,
257e71b7053SJung-uk KimEVP_CIPH_WRAP_MODE or EVP_CIPH_OCB_MODE. If the cipher is a stream cipher then
258e71b7053SJung-uk KimEVP_CIPH_STREAM_CIPHER is returned.
259e71b7053SJung-uk Kim
260e71b7053SJung-uk KimEVP_CIPHER_param_to_asn1() sets the AlgorithmIdentifier "parameter" based
261e71b7053SJung-uk Kimon the passed cipher. This will typically include any parameters and an
262e71b7053SJung-uk KimIV. The cipher IV (if any) must be set when this call is made. This call
263e71b7053SJung-uk Kimshould be made before the cipher is actually "used" (before any
264e71b7053SJung-uk KimEVP_EncryptUpdate(), EVP_DecryptUpdate() calls for example). This function
265e71b7053SJung-uk Kimmay fail if the cipher does not have any ASN1 support.
266e71b7053SJung-uk Kim
267e71b7053SJung-uk KimEVP_CIPHER_asn1_to_param() sets the cipher parameters based on an ASN1
268e71b7053SJung-uk KimAlgorithmIdentifier "parameter". The precise effect depends on the cipher
269e71b7053SJung-uk KimIn the case of RC2, for example, it will set the IV and effective key length.
270e71b7053SJung-uk KimThis function should be called after the base cipher type is set but before
271e71b7053SJung-uk Kimthe key is set. For example EVP_CipherInit() will be called with the IV and
272e71b7053SJung-uk Kimkey set to NULL, EVP_CIPHER_asn1_to_param() will be called and finally
273e71b7053SJung-uk KimEVP_CipherInit() again with all parameters except the key set to NULL. It is
274e71b7053SJung-uk Kimpossible for this function to fail if the cipher does not have any ASN1 support
275e71b7053SJung-uk Kimor the parameters cannot be set (for example the RC2 effective key length
276e71b7053SJung-uk Kimis not supported.
277e71b7053SJung-uk Kim
278e71b7053SJung-uk KimEVP_CIPHER_CTX_ctrl() allows various cipher specific parameters to be determined
279e71b7053SJung-uk Kimand set.
280e71b7053SJung-uk Kim
281e71b7053SJung-uk KimEVP_CIPHER_CTX_rand_key() generates a random key of the appropriate length
282e71b7053SJung-uk Kimbased on the cipher context. The EVP_CIPHER can provide its own random key
283e71b7053SJung-uk Kimgeneration routine to support keys of a specific form. B<Key> must point to a
284e71b7053SJung-uk Kimbuffer at least as big as the value returned by EVP_CIPHER_CTX_key_length().
285e71b7053SJung-uk Kim
286e71b7053SJung-uk Kim=head1 RETURN VALUES
287e71b7053SJung-uk Kim
288e71b7053SJung-uk KimEVP_CIPHER_CTX_new() returns a pointer to a newly created
289e71b7053SJung-uk KimB<EVP_CIPHER_CTX> for success and B<NULL> for failure.
290e71b7053SJung-uk Kim
291e71b7053SJung-uk KimEVP_EncryptInit_ex(), EVP_EncryptUpdate() and EVP_EncryptFinal_ex()
292e71b7053SJung-uk Kimreturn 1 for success and 0 for failure.
293e71b7053SJung-uk Kim
294e71b7053SJung-uk KimEVP_DecryptInit_ex() and EVP_DecryptUpdate() return 1 for success and 0 for failure.
295e71b7053SJung-uk KimEVP_DecryptFinal_ex() returns 0 if the decrypt failed or 1 for success.
296e71b7053SJung-uk Kim
297e71b7053SJung-uk KimEVP_CipherInit_ex() and EVP_CipherUpdate() return 1 for success and 0 for failure.
298e71b7053SJung-uk KimEVP_CipherFinal_ex() returns 0 for a decryption failure or 1 for success.
299e71b7053SJung-uk Kim
300e71b7053SJung-uk KimEVP_CIPHER_CTX_reset() returns 1 for success and 0 for failure.
301e71b7053SJung-uk Kim
302e71b7053SJung-uk KimEVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
303e71b7053SJung-uk Kimreturn an B<EVP_CIPHER> structure or NULL on error.
304e71b7053SJung-uk Kim
305e71b7053SJung-uk KimEVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return a NID.
306e71b7053SJung-uk Kim
307e71b7053SJung-uk KimEVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the block
308e71b7053SJung-uk Kimsize.
309e71b7053SJung-uk Kim
310e71b7053SJung-uk KimEVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
311e71b7053SJung-uk Kimlength.
312e71b7053SJung-uk Kim
313e71b7053SJung-uk KimEVP_CIPHER_CTX_set_padding() always returns 1.
314e71b7053SJung-uk Kim
315e71b7053SJung-uk KimEVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
316e71b7053SJung-uk Kimlength or zero if the cipher does not use an IV.
317e71b7053SJung-uk Kim
318e71b7053SJung-uk KimEVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the NID of the cipher's
319e71b7053SJung-uk KimOBJECT IDENTIFIER or NID_undef if it has no defined OBJECT IDENTIFIER.
320e71b7053SJung-uk Kim
321e71b7053SJung-uk KimEVP_CIPHER_CTX_cipher() returns an B<EVP_CIPHER> structure.
322e71b7053SJung-uk Kim
323e71b7053SJung-uk KimEVP_CIPHER_param_to_asn1() and EVP_CIPHER_asn1_to_param() return greater
324e71b7053SJung-uk Kimthan zero for success and zero or a negative number on failure.
325e71b7053SJung-uk Kim
326e71b7053SJung-uk KimEVP_CIPHER_CTX_rand_key() returns 1 for success.
327e71b7053SJung-uk Kim
328e71b7053SJung-uk Kim=head1 CIPHER LISTING
329e71b7053SJung-uk Kim
330e71b7053SJung-uk KimAll algorithms have a fixed key length unless otherwise stated.
331e71b7053SJung-uk Kim
332e71b7053SJung-uk KimRefer to L<SEE ALSO> for the full list of ciphers available through the EVP
333e71b7053SJung-uk Kiminterface.
334e71b7053SJung-uk Kim
335e71b7053SJung-uk Kim=over 4
336e71b7053SJung-uk Kim
337e71b7053SJung-uk Kim=item EVP_enc_null()
338e71b7053SJung-uk Kim
339e71b7053SJung-uk KimNull cipher: does nothing.
340e71b7053SJung-uk Kim
341e71b7053SJung-uk Kim=back
342e71b7053SJung-uk Kim
343e71b7053SJung-uk Kim=head1 AEAD Interface
344e71b7053SJung-uk Kim
345e71b7053SJung-uk KimThe EVP interface for Authenticated Encryption with Associated Data (AEAD)
346e71b7053SJung-uk Kimmodes are subtly altered and several additional I<ctrl> operations are supported
347e71b7053SJung-uk Kimdepending on the mode specified.
348e71b7053SJung-uk Kim
349e71b7053SJung-uk KimTo specify additional authenticated data (AAD), a call to EVP_CipherUpdate(),
350e71b7053SJung-uk KimEVP_EncryptUpdate() or EVP_DecryptUpdate() should be made with the output
351e71b7053SJung-uk Kimparameter B<out> set to B<NULL>.
352e71b7053SJung-uk Kim
353e71b7053SJung-uk KimWhen decrypting, the return value of EVP_DecryptFinal() or EVP_CipherFinal()
354e71b7053SJung-uk Kimindicates whether the operation was successful. If it does not indicate success,
355e71b7053SJung-uk Kimthe authentication operation has failed and any output data B<MUST NOT> be used
356e71b7053SJung-uk Kimas it is corrupted.
357e71b7053SJung-uk Kim
358e71b7053SJung-uk Kim=head2 GCM and OCB Modes
359e71b7053SJung-uk Kim
360e71b7053SJung-uk KimThe following I<ctrl>s are supported in GCM and OCB modes.
361e71b7053SJung-uk Kim
362e71b7053SJung-uk Kim=over 4
363e71b7053SJung-uk Kim
364e71b7053SJung-uk Kim=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
365e71b7053SJung-uk Kim
366e71b7053SJung-uk KimSets the IV length. This call can only be made before specifying an IV. If
367e71b7053SJung-uk Kimnot called a default IV length is used.
368e71b7053SJung-uk Kim
369e71b7053SJung-uk KimFor GCM AES and OCB AES the default is 12 (i.e. 96 bits). For OCB mode the
370e71b7053SJung-uk Kimmaximum is 15.
371e71b7053SJung-uk Kim
372e71b7053SJung-uk Kim=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)
373e71b7053SJung-uk Kim
374e71b7053SJung-uk KimWrites C<taglen> bytes of the tag value to the buffer indicated by C<tag>.
375e71b7053SJung-uk KimThis call can only be made when encrypting data and B<after> all data has been
376e71b7053SJung-uk Kimprocessed (e.g. after an EVP_EncryptFinal() call).
377e71b7053SJung-uk Kim
378e71b7053SJung-uk KimFor OCB, C<taglen> must either be 16 or the value previously set via
379e71b7053SJung-uk KimB<EVP_CTRL_AEAD_SET_TAG>.
380e71b7053SJung-uk Kim
381e71b7053SJung-uk Kim=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
382e71b7053SJung-uk Kim
383*b2bf0c7eSJung-uk KimWhen decrypting, this call sets the expected tag to C<taglen> bytes from C<tag>.
384e71b7053SJung-uk KimC<taglen> must be between 1 and 16 inclusive.
385*b2bf0c7eSJung-uk KimThe tag must be set prior to any call to EVP_DecryptFinal() or
386*b2bf0c7eSJung-uk KimEVP_DecryptFinal_ex().
387e71b7053SJung-uk Kim
388e71b7053SJung-uk KimFor GCM, this call is only valid when decrypting data.
389e71b7053SJung-uk Kim
390e71b7053SJung-uk KimFor OCB, this call is valid when decrypting data to set the expected tag,
391*b2bf0c7eSJung-uk Kimand when encrypting to set the desired tag length.
392e71b7053SJung-uk Kim
393*b2bf0c7eSJung-uk KimIn OCB mode, calling this when encrypting with C<tag> set to C<NULL> sets the
394*b2bf0c7eSJung-uk Kimtag length. The tag length can only be set before specifying an IV. If this is
395*b2bf0c7eSJung-uk Kimnot called prior to setting the IV during encryption, then a default tag length
396*b2bf0c7eSJung-uk Kimis used.
397e71b7053SJung-uk Kim
398e71b7053SJung-uk KimFor OCB AES, the default tag length is 16 (i.e. 128 bits).  It is also the
399e71b7053SJung-uk Kimmaximum tag length for OCB.
400e71b7053SJung-uk Kim
401e71b7053SJung-uk Kim=back
402e71b7053SJung-uk Kim
403e71b7053SJung-uk Kim=head2 CCM Mode
404e71b7053SJung-uk Kim
405e71b7053SJung-uk KimThe EVP interface for CCM mode is similar to that of the GCM mode but with a
406e71b7053SJung-uk Kimfew additional requirements and different I<ctrl> values.
407e71b7053SJung-uk Kim
408e71b7053SJung-uk KimFor CCM mode, the total plaintext or ciphertext length B<MUST> be passed to
409e71b7053SJung-uk KimEVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with the output
410e71b7053SJung-uk Kimand input parameters (B<in> and B<out>) set to B<NULL> and the length passed in
411e71b7053SJung-uk Kimthe B<inl> parameter.
412e71b7053SJung-uk Kim
413e71b7053SJung-uk KimThe following I<ctrl>s are supported in CCM mode.
414e71b7053SJung-uk Kim
415e71b7053SJung-uk Kim=over 4
416e71b7053SJung-uk Kim
417e71b7053SJung-uk Kim=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
418e71b7053SJung-uk Kim
419e71b7053SJung-uk KimThis call is made to set the expected B<CCM> tag value when decrypting or
420e71b7053SJung-uk Kimthe length of the tag (with the C<tag> parameter set to NULL) when encrypting.
421e71b7053SJung-uk KimThe tag length is often referred to as B<M>. If not set a default value is
422610a21fdSJung-uk Kimused (12 for AES). When decrypting, the tag needs to be set before passing
423610a21fdSJung-uk Kimin data to be decrypted, but as in GCM and OCB mode, it can be set after
424610a21fdSJung-uk Kimpassing additional authenticated data (see L<AEAD Interface>).
425e71b7053SJung-uk Kim
426e71b7053SJung-uk Kim=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, ivlen, NULL)
427e71b7053SJung-uk Kim
428e71b7053SJung-uk KimSets the CCM B<L> value. If not set a default is used (8 for AES).
429e71b7053SJung-uk Kim
430e71b7053SJung-uk Kim=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
431e71b7053SJung-uk Kim
43258f35182SJung-uk KimSets the CCM nonce (IV) length. This call can only be made before specifying
43358f35182SJung-uk Kima nonce value. The nonce length is given by B<15 - L> so it is 7 by default for
434e71b7053SJung-uk KimAES.
435e71b7053SJung-uk Kim
436e71b7053SJung-uk Kim=back
437e71b7053SJung-uk Kim
438e71b7053SJung-uk Kim=head2 ChaCha20-Poly1305
439e71b7053SJung-uk Kim
440e71b7053SJung-uk KimThe following I<ctrl>s are supported for the ChaCha20-Poly1305 AEAD algorithm.
441e71b7053SJung-uk Kim
442e71b7053SJung-uk Kim=over 4
443e71b7053SJung-uk Kim
444e71b7053SJung-uk Kim=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
445e71b7053SJung-uk Kim
446e71b7053SJung-uk KimSets the nonce length. This call can only be made before specifying the nonce.
447e71b7053SJung-uk KimIf not called a default nonce length of 12 (i.e. 96 bits) is used. The maximum
448610a21fdSJung-uk Kimnonce length is 12 bytes (i.e. 96-bits). If a nonce of less than 12 bytes is set
449610a21fdSJung-uk Kimthen the nonce is automatically padded with leading 0 bytes to make it 12 bytes
450610a21fdSJung-uk Kimin length.
451e71b7053SJung-uk Kim
452e71b7053SJung-uk Kim=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)
453e71b7053SJung-uk Kim
454e71b7053SJung-uk KimWrites C<taglen> bytes of the tag value to the buffer indicated by C<tag>.
455e71b7053SJung-uk KimThis call can only be made when encrypting data and B<after> all data has been
456e71b7053SJung-uk Kimprocessed (e.g. after an EVP_EncryptFinal() call).
457e71b7053SJung-uk Kim
458e71b7053SJung-uk KimC<taglen> specified here must be 16 (B<POLY1305_BLOCK_SIZE>, i.e. 128-bits) or
459e71b7053SJung-uk Kimless.
460e71b7053SJung-uk Kim
461e71b7053SJung-uk Kim=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
462e71b7053SJung-uk Kim
463e71b7053SJung-uk KimSets the expected tag to C<taglen> bytes from C<tag>.
464e71b7053SJung-uk KimThe tag length can only be set before specifying an IV.
465e71b7053SJung-uk KimC<taglen> must be between 1 and 16 (B<POLY1305_BLOCK_SIZE>) inclusive.
466e71b7053SJung-uk KimThis call is only valid when decrypting data.
467e71b7053SJung-uk Kim
468e71b7053SJung-uk Kim=back
469e71b7053SJung-uk Kim
470e71b7053SJung-uk Kim=head1 NOTES
471e71b7053SJung-uk Kim
472e71b7053SJung-uk KimWhere possible the B<EVP> interface to symmetric ciphers should be used in
47358f35182SJung-uk Kimpreference to the low-level interfaces. This is because the code then becomes
474e71b7053SJung-uk Kimtransparent to the cipher used and much more flexible. Additionally, the
475e71b7053SJung-uk KimB<EVP> interface will ensure the use of platform specific cryptographic
47658f35182SJung-uk Kimacceleration such as AES-NI (the low-level interfaces do not provide the
477e71b7053SJung-uk Kimguarantee).
478e71b7053SJung-uk Kim
479e71b7053SJung-uk KimPKCS padding works by adding B<n> padding bytes of value B<n> to make the total
480e71b7053SJung-uk Kimlength of the encrypted data a multiple of the block size. Padding is always
481e71b7053SJung-uk Kimadded so if the data is already a multiple of the block size B<n> will equal
482e71b7053SJung-uk Kimthe block size. For example if the block size is 8 and 11 bytes are to be
483e71b7053SJung-uk Kimencrypted then 5 padding bytes of value 5 will be added.
484e71b7053SJung-uk Kim
485e71b7053SJung-uk KimWhen decrypting the final block is checked to see if it has the correct form.
486e71b7053SJung-uk Kim
487e71b7053SJung-uk KimAlthough the decryption operation can produce an error if padding is enabled,
488e71b7053SJung-uk Kimit is not a strong test that the input data or key is correct. A random block
489e71b7053SJung-uk Kimhas better than 1 in 256 chance of being of the correct format and problems with
490e71b7053SJung-uk Kimthe input data earlier on will not produce a final decrypt error.
491e71b7053SJung-uk Kim
492e71b7053SJung-uk KimIf padding is disabled then the decryption operation will always succeed if
493e71b7053SJung-uk Kimthe total amount of data decrypted is a multiple of the block size.
494e71b7053SJung-uk Kim
495e71b7053SJung-uk KimThe functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(),
496e71b7053SJung-uk KimEVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained for
497e71b7053SJung-uk Kimcompatibility with existing code. New code should use EVP_EncryptInit_ex(),
498e71b7053SJung-uk KimEVP_EncryptFinal_ex(), EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(),
499e71b7053SJung-uk KimEVP_CipherInit_ex() and EVP_CipherFinal_ex() because they can reuse an
500e71b7053SJung-uk Kimexisting context without allocating and freeing it up on each call.
501e71b7053SJung-uk Kim
50217f01e99SJung-uk KimThere are some differences between functions EVP_CipherInit() and
50317f01e99SJung-uk KimEVP_CipherInit_ex(), significant in some circumstances. EVP_CipherInit() fills
50417f01e99SJung-uk Kimthe passed context object with zeros.  As a consequence, EVP_CipherInit() does
50517f01e99SJung-uk Kimnot allow step-by-step initialization of the ctx when the I<key> and I<iv> are
50617f01e99SJung-uk Kimpassed in separate calls. It also means that the flags set for the CTX are
50717f01e99SJung-uk Kimremoved, and it is especially important for the
50817f01e99SJung-uk KimB<EVP_CIPHER_CTX_FLAG_WRAP_ALLOW> flag treated specially in
50917f01e99SJung-uk KimEVP_CipherInit_ex().
51017f01e99SJung-uk Kim
511e71b7053SJung-uk KimEVP_get_cipherbynid(), and EVP_get_cipherbyobj() are implemented as macros.
512e71b7053SJung-uk Kim
513e71b7053SJung-uk Kim=head1 BUGS
514e71b7053SJung-uk Kim
515e71b7053SJung-uk KimB<EVP_MAX_KEY_LENGTH> and B<EVP_MAX_IV_LENGTH> only refer to the internal
516e71b7053SJung-uk Kimciphers with default key lengths. If custom ciphers exceed these values the
517e71b7053SJung-uk Kimresults are unpredictable. This is because it has become standard practice to
518e71b7053SJung-uk Kimdefine a generic key as a fixed unsigned char array containing
519e71b7053SJung-uk KimB<EVP_MAX_KEY_LENGTH> bytes.
520e71b7053SJung-uk Kim
521e71b7053SJung-uk KimThe ASN1 code is incomplete (and sometimes inaccurate) it has only been tested
522e71b7053SJung-uk Kimfor certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode.
523e71b7053SJung-uk Kim
524e71b7053SJung-uk Kim=head1 EXAMPLES
525e71b7053SJung-uk Kim
526e71b7053SJung-uk KimEncrypt a string using IDEA:
527e71b7053SJung-uk Kim
528e71b7053SJung-uk Kim int do_crypt(char *outfile)
529e71b7053SJung-uk Kim {
530e71b7053SJung-uk Kim     unsigned char outbuf[1024];
531e71b7053SJung-uk Kim     int outlen, tmplen;
532e71b7053SJung-uk Kim     /*
533e71b7053SJung-uk Kim      * Bogus key and IV: we'd normally set these from
534e71b7053SJung-uk Kim      * another source.
535e71b7053SJung-uk Kim      */
536e71b7053SJung-uk Kim     unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
537e71b7053SJung-uk Kim     unsigned char iv[] = {1,2,3,4,5,6,7,8};
538e71b7053SJung-uk Kim     char intext[] = "Some Crypto Text";
539e71b7053SJung-uk Kim     EVP_CIPHER_CTX *ctx;
540e71b7053SJung-uk Kim     FILE *out;
541e71b7053SJung-uk Kim
542e71b7053SJung-uk Kim     ctx = EVP_CIPHER_CTX_new();
543e71b7053SJung-uk Kim     EVP_EncryptInit_ex(ctx, EVP_idea_cbc(), NULL, key, iv);
544e71b7053SJung-uk Kim
545e71b7053SJung-uk Kim     if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) {
546e71b7053SJung-uk Kim         /* Error */
547e71b7053SJung-uk Kim         EVP_CIPHER_CTX_free(ctx);
548e71b7053SJung-uk Kim         return 0;
549e71b7053SJung-uk Kim     }
550e71b7053SJung-uk Kim     /*
551e71b7053SJung-uk Kim      * Buffer passed to EVP_EncryptFinal() must be after data just
552e71b7053SJung-uk Kim      * encrypted to avoid overwriting it.
553e71b7053SJung-uk Kim      */
554e71b7053SJung-uk Kim     if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) {
555e71b7053SJung-uk Kim         /* Error */
556e71b7053SJung-uk Kim         EVP_CIPHER_CTX_free(ctx);
557e71b7053SJung-uk Kim         return 0;
558e71b7053SJung-uk Kim     }
559e71b7053SJung-uk Kim     outlen += tmplen;
560e71b7053SJung-uk Kim     EVP_CIPHER_CTX_free(ctx);
561e71b7053SJung-uk Kim     /*
562e71b7053SJung-uk Kim      * Need binary mode for fopen because encrypted data is
563e71b7053SJung-uk Kim      * binary data. Also cannot use strlen() on it because
564e71b7053SJung-uk Kim      * it won't be NUL terminated and may contain embedded
565e71b7053SJung-uk Kim      * NULs.
566e71b7053SJung-uk Kim      */
567e71b7053SJung-uk Kim     out = fopen(outfile, "wb");
568e71b7053SJung-uk Kim     if (out == NULL) {
569e71b7053SJung-uk Kim         /* Error */
570e71b7053SJung-uk Kim         return 0;
571e71b7053SJung-uk Kim     }
572e71b7053SJung-uk Kim     fwrite(outbuf, 1, outlen, out);
573e71b7053SJung-uk Kim     fclose(out);
574e71b7053SJung-uk Kim     return 1;
575e71b7053SJung-uk Kim }
576e71b7053SJung-uk Kim
577e71b7053SJung-uk KimThe ciphertext from the above example can be decrypted using the B<openssl>
578e71b7053SJung-uk Kimutility with the command line (shown on two lines for clarity):
579e71b7053SJung-uk Kim
580e71b7053SJung-uk Kim openssl idea -d \
581e71b7053SJung-uk Kim     -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 <filename
582e71b7053SJung-uk Kim
583e71b7053SJung-uk KimGeneral encryption and decryption function example using FILE I/O and AES128
584e71b7053SJung-uk Kimwith a 128-bit key:
585e71b7053SJung-uk Kim
586e71b7053SJung-uk Kim int do_crypt(FILE *in, FILE *out, int do_encrypt)
587e71b7053SJung-uk Kim {
588e71b7053SJung-uk Kim     /* Allow enough space in output buffer for additional block */
589e71b7053SJung-uk Kim     unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
590e71b7053SJung-uk Kim     int inlen, outlen;
591e71b7053SJung-uk Kim     EVP_CIPHER_CTX *ctx;
592e71b7053SJung-uk Kim     /*
593e71b7053SJung-uk Kim      * Bogus key and IV: we'd normally set these from
594e71b7053SJung-uk Kim      * another source.
595e71b7053SJung-uk Kim      */
596e71b7053SJung-uk Kim     unsigned char key[] = "0123456789abcdeF";
597e71b7053SJung-uk Kim     unsigned char iv[] = "1234567887654321";
598e71b7053SJung-uk Kim
599e71b7053SJung-uk Kim     /* Don't set key or IV right away; we want to check lengths */
600e71b7053SJung-uk Kim     ctx = EVP_CIPHER_CTX_new();
60158f35182SJung-uk Kim     EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, NULL, NULL,
602e71b7053SJung-uk Kim                       do_encrypt);
603e71b7053SJung-uk Kim     OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16);
604e71b7053SJung-uk Kim     OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == 16);
605e71b7053SJung-uk Kim
606e71b7053SJung-uk Kim     /* Now we can set key and IV */
607e71b7053SJung-uk Kim     EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt);
608e71b7053SJung-uk Kim
609e71b7053SJung-uk Kim     for (;;) {
610e71b7053SJung-uk Kim         inlen = fread(inbuf, 1, 1024, in);
611e71b7053SJung-uk Kim         if (inlen <= 0)
612e71b7053SJung-uk Kim             break;
613e71b7053SJung-uk Kim         if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
614e71b7053SJung-uk Kim             /* Error */
615e71b7053SJung-uk Kim             EVP_CIPHER_CTX_free(ctx);
616e71b7053SJung-uk Kim             return 0;
617e71b7053SJung-uk Kim         }
618e71b7053SJung-uk Kim         fwrite(outbuf, 1, outlen, out);
619e71b7053SJung-uk Kim     }
620e71b7053SJung-uk Kim     if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
621e71b7053SJung-uk Kim         /* Error */
622e71b7053SJung-uk Kim         EVP_CIPHER_CTX_free(ctx);
623e71b7053SJung-uk Kim         return 0;
624e71b7053SJung-uk Kim     }
625e71b7053SJung-uk Kim     fwrite(outbuf, 1, outlen, out);
626e71b7053SJung-uk Kim
627e71b7053SJung-uk Kim     EVP_CIPHER_CTX_free(ctx);
628e71b7053SJung-uk Kim     return 1;
629e71b7053SJung-uk Kim }
630e71b7053SJung-uk Kim
631e71b7053SJung-uk Kim
632e71b7053SJung-uk Kim=head1 SEE ALSO
633e71b7053SJung-uk Kim
634e71b7053SJung-uk KimL<evp(7)>
635e71b7053SJung-uk Kim
636e71b7053SJung-uk KimSupported ciphers are listed in:
637e71b7053SJung-uk Kim
638e71b7053SJung-uk KimL<EVP_aes(3)>,
639e71b7053SJung-uk KimL<EVP_aria(3)>,
640e71b7053SJung-uk KimL<EVP_bf(3)>,
641e71b7053SJung-uk KimL<EVP_camellia(3)>,
642e71b7053SJung-uk KimL<EVP_cast5(3)>,
643e71b7053SJung-uk KimL<EVP_chacha20(3)>,
644e71b7053SJung-uk KimL<EVP_des(3)>,
645e71b7053SJung-uk KimL<EVP_desx(3)>,
646e71b7053SJung-uk KimL<EVP_idea(3)>,
647e71b7053SJung-uk KimL<EVP_rc2(3)>,
648e71b7053SJung-uk KimL<EVP_rc4(3)>,
649e71b7053SJung-uk KimL<EVP_rc5(3)>,
650e71b7053SJung-uk KimL<EVP_seed(3)>,
651e71b7053SJung-uk KimL<EVP_sm4(3)>
652e71b7053SJung-uk Kim
653e71b7053SJung-uk Kim=head1 HISTORY
654e71b7053SJung-uk Kim
6556935a639SJung-uk KimSupport for OCB mode was added in OpenSSL 1.1.0.
656e71b7053SJung-uk Kim
657e71b7053SJung-uk KimB<EVP_CIPHER_CTX> was made opaque in OpenSSL 1.1.0.  As a result,
658e71b7053SJung-uk KimEVP_CIPHER_CTX_reset() appeared and EVP_CIPHER_CTX_cleanup()
659e71b7053SJung-uk Kimdisappeared.  EVP_CIPHER_CTX_init() remains as an alias for
660e71b7053SJung-uk KimEVP_CIPHER_CTX_reset().
661e71b7053SJung-uk Kim
662e71b7053SJung-uk Kim=head1 COPYRIGHT
663e71b7053SJung-uk Kim
664*b2bf0c7eSJung-uk KimCopyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
665e71b7053SJung-uk Kim
666e71b7053SJung-uk KimLicensed under the OpenSSL license (the "License").  You may not use
667e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
668e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
669e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
670e71b7053SJung-uk Kim
671e71b7053SJung-uk Kim=cut
672