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