xref: /netbsd-src/crypto/external/bsd/openssl.old/dist/doc/man3/EVP_EncryptInit.pod (revision 4724848cf0da353df257f730694b7882798e5daf)
1*4724848cSchristos=pod
2*4724848cSchristos
3*4724848cSchristos=head1 NAME
4*4724848cSchristos
5*4724848cSchristosEVP_CIPHER_CTX_new,
6*4724848cSchristosEVP_CIPHER_CTX_reset,
7*4724848cSchristosEVP_CIPHER_CTX_free,
8*4724848cSchristosEVP_EncryptInit_ex,
9*4724848cSchristosEVP_EncryptUpdate,
10*4724848cSchristosEVP_EncryptFinal_ex,
11*4724848cSchristosEVP_DecryptInit_ex,
12*4724848cSchristosEVP_DecryptUpdate,
13*4724848cSchristosEVP_DecryptFinal_ex,
14*4724848cSchristosEVP_CipherInit_ex,
15*4724848cSchristosEVP_CipherUpdate,
16*4724848cSchristosEVP_CipherFinal_ex,
17*4724848cSchristosEVP_CIPHER_CTX_set_key_length,
18*4724848cSchristosEVP_CIPHER_CTX_ctrl,
19*4724848cSchristosEVP_EncryptInit,
20*4724848cSchristosEVP_EncryptFinal,
21*4724848cSchristosEVP_DecryptInit,
22*4724848cSchristosEVP_DecryptFinal,
23*4724848cSchristosEVP_CipherInit,
24*4724848cSchristosEVP_CipherFinal,
25*4724848cSchristosEVP_get_cipherbyname,
26*4724848cSchristosEVP_get_cipherbynid,
27*4724848cSchristosEVP_get_cipherbyobj,
28*4724848cSchristosEVP_CIPHER_nid,
29*4724848cSchristosEVP_CIPHER_block_size,
30*4724848cSchristosEVP_CIPHER_key_length,
31*4724848cSchristosEVP_CIPHER_iv_length,
32*4724848cSchristosEVP_CIPHER_flags,
33*4724848cSchristosEVP_CIPHER_mode,
34*4724848cSchristosEVP_CIPHER_type,
35*4724848cSchristosEVP_CIPHER_CTX_cipher,
36*4724848cSchristosEVP_CIPHER_CTX_nid,
37*4724848cSchristosEVP_CIPHER_CTX_block_size,
38*4724848cSchristosEVP_CIPHER_CTX_key_length,
39*4724848cSchristosEVP_CIPHER_CTX_iv_length,
40*4724848cSchristosEVP_CIPHER_CTX_get_app_data,
41*4724848cSchristosEVP_CIPHER_CTX_set_app_data,
42*4724848cSchristosEVP_CIPHER_CTX_type,
43*4724848cSchristosEVP_CIPHER_CTX_flags,
44*4724848cSchristosEVP_CIPHER_CTX_mode,
45*4724848cSchristosEVP_CIPHER_param_to_asn1,
46*4724848cSchristosEVP_CIPHER_asn1_to_param,
47*4724848cSchristosEVP_CIPHER_CTX_set_padding,
48*4724848cSchristosEVP_enc_null
49*4724848cSchristos- EVP cipher routines
50*4724848cSchristos
51*4724848cSchristos=head1 SYNOPSIS
52*4724848cSchristos
53*4724848cSchristos=for comment generic
54*4724848cSchristos
55*4724848cSchristos #include <openssl/evp.h>
56*4724848cSchristos
57*4724848cSchristos EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void);
58*4724848cSchristos int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx);
59*4724848cSchristos void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx);
60*4724848cSchristos
61*4724848cSchristos int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
62*4724848cSchristos                        ENGINE *impl, const unsigned char *key, const unsigned char *iv);
63*4724848cSchristos int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
64*4724848cSchristos                       int *outl, const unsigned char *in, int inl);
65*4724848cSchristos int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
66*4724848cSchristos
67*4724848cSchristos int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
68*4724848cSchristos                        ENGINE *impl, const unsigned char *key, const unsigned char *iv);
69*4724848cSchristos int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
70*4724848cSchristos                       int *outl, const unsigned char *in, int inl);
71*4724848cSchristos int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
72*4724848cSchristos
73*4724848cSchristos int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
74*4724848cSchristos                       ENGINE *impl, const unsigned char *key, const unsigned char *iv, int enc);
75*4724848cSchristos int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
76*4724848cSchristos                      int *outl, const unsigned char *in, int inl);
77*4724848cSchristos int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
78*4724848cSchristos
79*4724848cSchristos int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
80*4724848cSchristos                     const unsigned char *key, const unsigned char *iv);
81*4724848cSchristos int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
82*4724848cSchristos
83*4724848cSchristos int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
84*4724848cSchristos                     const unsigned char *key, const unsigned char *iv);
85*4724848cSchristos int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
86*4724848cSchristos
87*4724848cSchristos int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
88*4724848cSchristos                    const unsigned char *key, const unsigned char *iv, int enc);
89*4724848cSchristos int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
90*4724848cSchristos
91*4724848cSchristos int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
92*4724848cSchristos int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
93*4724848cSchristos int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
94*4724848cSchristos int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key);
95*4724848cSchristos
96*4724848cSchristos const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
97*4724848cSchristos const EVP_CIPHER *EVP_get_cipherbynid(int nid);
98*4724848cSchristos const EVP_CIPHER *EVP_get_cipherbyobj(const ASN1_OBJECT *a);
99*4724848cSchristos
100*4724848cSchristos int EVP_CIPHER_nid(const EVP_CIPHER *e);
101*4724848cSchristos int EVP_CIPHER_block_size(const EVP_CIPHER *e);
102*4724848cSchristos int EVP_CIPHER_key_length(const EVP_CIPHER *e);
103*4724848cSchristos int EVP_CIPHER_iv_length(const EVP_CIPHER *e);
104*4724848cSchristos unsigned long EVP_CIPHER_flags(const EVP_CIPHER *e);
105*4724848cSchristos unsigned long EVP_CIPHER_mode(const EVP_CIPHER *e);
106*4724848cSchristos int EVP_CIPHER_type(const EVP_CIPHER *ctx);
107*4724848cSchristos
108*4724848cSchristos const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx);
109*4724848cSchristos int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx);
110*4724848cSchristos int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx);
111*4724848cSchristos int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx);
112*4724848cSchristos int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx);
113*4724848cSchristos void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx);
114*4724848cSchristos void EVP_CIPHER_CTX_set_app_data(const EVP_CIPHER_CTX *ctx, void *data);
115*4724848cSchristos int EVP_CIPHER_CTX_type(const EVP_CIPHER_CTX *ctx);
116*4724848cSchristos int EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx);
117*4724848cSchristos
118*4724848cSchristos int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
119*4724848cSchristos int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
120*4724848cSchristos
121*4724848cSchristos=head1 DESCRIPTION
122*4724848cSchristos
123*4724848cSchristosThe EVP cipher routines are a high-level interface to certain
124*4724848cSchristossymmetric ciphers.
125*4724848cSchristos
126*4724848cSchristosEVP_CIPHER_CTX_new() creates a cipher context.
127*4724848cSchristos
128*4724848cSchristosEVP_CIPHER_CTX_free() clears all information from a cipher context
129*4724848cSchristosand free up any allocated memory associate with it, including B<ctx>
130*4724848cSchristositself. This function should be called after all operations using a
131*4724848cSchristoscipher are complete so sensitive information does not remain in
132*4724848cSchristosmemory.
133*4724848cSchristos
134*4724848cSchristosEVP_EncryptInit_ex() sets up cipher context B<ctx> for encryption
135*4724848cSchristoswith cipher B<type> from ENGINE B<impl>. B<ctx> must be created
136*4724848cSchristosbefore calling this function. B<type> is normally supplied
137*4724848cSchristosby a function such as EVP_aes_256_cbc(). If B<impl> is NULL then the
138*4724848cSchristosdefault implementation is used. B<key> is the symmetric key to use
139*4724848cSchristosand B<iv> is the IV to use (if necessary), the actual number of bytes
140*4724848cSchristosused for the key and IV depends on the cipher. It is possible to set
141*4724848cSchristosall parameters to NULL except B<type> in an initial call and supply
142*4724848cSchristosthe remaining parameters in subsequent calls, all of which have B<type>
143*4724848cSchristosset to NULL. This is done when the default cipher parameters are not
144*4724848cSchristosappropriate.
145*4724848cSchristos
146*4724848cSchristosEVP_EncryptUpdate() encrypts B<inl> bytes from the buffer B<in> and
147*4724848cSchristoswrites the encrypted version to B<out>. This function can be called
148*4724848cSchristosmultiple times to encrypt successive blocks of data. The amount
149*4724848cSchristosof data written depends on the block alignment of the encrypted data.
150*4724848cSchristosFor most ciphers and modes, the amount of data written can be anything
151*4724848cSchristosfrom zero bytes to (inl + cipher_block_size - 1) bytes.
152*4724848cSchristosFor wrap cipher modes, the amount of data written can be anything
153*4724848cSchristosfrom zero bytes to (inl + cipher_block_size) bytes.
154*4724848cSchristosFor stream ciphers, the amount of data written can be anything from zero
155*4724848cSchristosbytes to inl bytes.
156*4724848cSchristosThus, B<out> should contain sufficient room for the operation being performed.
157*4724848cSchristosThe actual number of bytes written is placed in B<outl>. It also
158*4724848cSchristoschecks if B<in> and B<out> are partially overlapping, and if they are
159*4724848cSchristos0 is returned to indicate failure.
160*4724848cSchristos
161*4724848cSchristosIf padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts
162*4724848cSchristosthe "final" data, that is any data that remains in a partial block.
163*4724848cSchristosIt uses standard block padding (aka PKCS padding) as described in
164*4724848cSchristosthe NOTES section, below. The encrypted
165*4724848cSchristosfinal data is written to B<out> which should have sufficient space for
166*4724848cSchristosone cipher block. The number of bytes written is placed in B<outl>. After
167*4724848cSchristosthis function is called the encryption operation is finished and no further
168*4724848cSchristoscalls to EVP_EncryptUpdate() should be made.
169*4724848cSchristos
170*4724848cSchristosIf padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more
171*4724848cSchristosdata and it will return an error if any data remains in a partial block:
172*4724848cSchristosthat is if the total data length is not a multiple of the block size.
173*4724848cSchristos
174*4724848cSchristosEVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are the
175*4724848cSchristoscorresponding decryption operations. EVP_DecryptFinal() will return an
176*4724848cSchristoserror code if padding is enabled and the final block is not correctly
177*4724848cSchristosformatted. The parameters and restrictions are identical to the encryption
178*4724848cSchristosoperations except that if padding is enabled the decrypted data buffer B<out>
179*4724848cSchristospassed to EVP_DecryptUpdate() should have sufficient room for
180*4724848cSchristos(B<inl> + cipher_block_size) bytes unless the cipher block size is 1 in
181*4724848cSchristoswhich case B<inl> bytes is sufficient.
182*4724848cSchristos
183*4724848cSchristosEVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex() are
184*4724848cSchristosfunctions that can be used for decryption or encryption. The operation
185*4724848cSchristosperformed depends on the value of the B<enc> parameter. It should be set
186*4724848cSchristosto 1 for encryption, 0 for decryption and -1 to leave the value unchanged
187*4724848cSchristos(the actual value of 'enc' being supplied in a previous call).
188*4724848cSchristos
189*4724848cSchristosEVP_CIPHER_CTX_reset() clears all information from a cipher context
190*4724848cSchristosand free up any allocated memory associate with it, except the B<ctx>
191*4724848cSchristositself. This function should be called anytime B<ctx> is to be reused
192*4724848cSchristosfor another EVP_CipherInit() / EVP_CipherUpdate() / EVP_CipherFinal()
193*4724848cSchristosseries of calls.
194*4724848cSchristos
195*4724848cSchristosEVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit() behave in a
196*4724848cSchristossimilar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex() and
197*4724848cSchristosEVP_CipherInit_ex() except they always use the default cipher implementation.
198*4724848cSchristos
199*4724848cSchristosEVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal() are
200*4724848cSchristosidentical to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and
201*4724848cSchristosEVP_CipherFinal_ex(). In previous releases they also cleaned up
202*4724848cSchristosthe B<ctx>, but this is no longer done and EVP_CIPHER_CTX_clean()
203*4724848cSchristosmust be called to free any context resources.
204*4724848cSchristos
205*4724848cSchristosEVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
206*4724848cSchristosreturn an EVP_CIPHER structure when passed a cipher name, a NID or an
207*4724848cSchristosASN1_OBJECT structure.
208*4724848cSchristos
209*4724848cSchristosEVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return the NID of a cipher when
210*4724848cSchristospassed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX> structure.  The actual NID
211*4724848cSchristosvalue is an internal value which may not have a corresponding OBJECT
212*4724848cSchristosIDENTIFIER.
213*4724848cSchristos
214*4724848cSchristosEVP_CIPHER_CTX_set_padding() enables or disables padding. This
215*4724848cSchristosfunction should be called after the context is set up for encryption
216*4724848cSchristosor decryption with EVP_EncryptInit_ex(), EVP_DecryptInit_ex() or
217*4724848cSchristosEVP_CipherInit_ex(). By default encryption operations are padded using
218*4724848cSchristosstandard block padding and the padding is checked and removed when
219*4724848cSchristosdecrypting. If the B<pad> parameter is zero then no padding is
220*4724848cSchristosperformed, the total amount of data encrypted or decrypted must then
221*4724848cSchristosbe a multiple of the block size or an error will occur.
222*4724848cSchristos
223*4724848cSchristosEVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
224*4724848cSchristoslength of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>
225*4724848cSchristosstructure. The constant B<EVP_MAX_KEY_LENGTH> is the maximum key length
226*4724848cSchristosfor all ciphers. Note: although EVP_CIPHER_key_length() is fixed for a
227*4724848cSchristosgiven cipher, the value of EVP_CIPHER_CTX_key_length() may be different
228*4724848cSchristosfor variable key length ciphers.
229*4724848cSchristos
230*4724848cSchristosEVP_CIPHER_CTX_set_key_length() sets the key length of the cipher ctx.
231*4724848cSchristosIf the cipher is a fixed length cipher then attempting to set the key
232*4724848cSchristoslength to any value other than the fixed value is an error.
233*4724848cSchristos
234*4724848cSchristosEVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
235*4724848cSchristoslength of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>.
236*4724848cSchristosIt will return zero if the cipher does not use an IV.  The constant
237*4724848cSchristosB<EVP_MAX_IV_LENGTH> is the maximum IV length for all ciphers.
238*4724848cSchristos
239*4724848cSchristosEVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the block
240*4724848cSchristossize of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>
241*4724848cSchristosstructure. The constant B<EVP_MAX_BLOCK_LENGTH> is also the maximum block
242*4724848cSchristoslength for all ciphers.
243*4724848cSchristos
244*4724848cSchristosEVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the type of the passed
245*4724848cSchristoscipher or context. This "type" is the actual NID of the cipher OBJECT
246*4724848cSchristosIDENTIFIER as such it ignores the cipher parameters and 40 bit RC2 and
247*4724848cSchristos128 bit RC2 have the same NID. If the cipher does not have an object
248*4724848cSchristosidentifier or does not have ASN1 support this function will return
249*4724848cSchristosB<NID_undef>.
250*4724848cSchristos
251*4724848cSchristosEVP_CIPHER_CTX_cipher() returns the B<EVP_CIPHER> structure when passed
252*4724848cSchristosan B<EVP_CIPHER_CTX> structure.
253*4724848cSchristos
254*4724848cSchristosEVP_CIPHER_mode() and EVP_CIPHER_CTX_mode() return the block cipher mode:
255*4724848cSchristosEVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE, EVP_CIPH_OFB_MODE,
256*4724848cSchristosEVP_CIPH_CTR_MODE, EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE, EVP_CIPH_XTS_MODE,
257*4724848cSchristosEVP_CIPH_WRAP_MODE or EVP_CIPH_OCB_MODE. If the cipher is a stream cipher then
258*4724848cSchristosEVP_CIPH_STREAM_CIPHER is returned.
259*4724848cSchristos
260*4724848cSchristosEVP_CIPHER_param_to_asn1() sets the AlgorithmIdentifier "parameter" based
261*4724848cSchristoson the passed cipher. This will typically include any parameters and an
262*4724848cSchristosIV. The cipher IV (if any) must be set when this call is made. This call
263*4724848cSchristosshould be made before the cipher is actually "used" (before any
264*4724848cSchristosEVP_EncryptUpdate(), EVP_DecryptUpdate() calls for example). This function
265*4724848cSchristosmay fail if the cipher does not have any ASN1 support.
266*4724848cSchristos
267*4724848cSchristosEVP_CIPHER_asn1_to_param() sets the cipher parameters based on an ASN1
268*4724848cSchristosAlgorithmIdentifier "parameter". The precise effect depends on the cipher
269*4724848cSchristosIn the case of RC2, for example, it will set the IV and effective key length.
270*4724848cSchristosThis function should be called after the base cipher type is set but before
271*4724848cSchristosthe key is set. For example EVP_CipherInit() will be called with the IV and
272*4724848cSchristoskey set to NULL, EVP_CIPHER_asn1_to_param() will be called and finally
273*4724848cSchristosEVP_CipherInit() again with all parameters except the key set to NULL. It is
274*4724848cSchristospossible for this function to fail if the cipher does not have any ASN1 support
275*4724848cSchristosor the parameters cannot be set (for example the RC2 effective key length
276*4724848cSchristosis not supported.
277*4724848cSchristos
278*4724848cSchristosEVP_CIPHER_CTX_ctrl() allows various cipher specific parameters to be determined
279*4724848cSchristosand set.
280*4724848cSchristos
281*4724848cSchristosEVP_CIPHER_CTX_rand_key() generates a random key of the appropriate length
282*4724848cSchristosbased on the cipher context. The EVP_CIPHER can provide its own random key
283*4724848cSchristosgeneration routine to support keys of a specific form. B<Key> must point to a
284*4724848cSchristosbuffer at least as big as the value returned by EVP_CIPHER_CTX_key_length().
285*4724848cSchristos
286*4724848cSchristos=head1 RETURN VALUES
287*4724848cSchristos
288*4724848cSchristosEVP_CIPHER_CTX_new() returns a pointer to a newly created
289*4724848cSchristosB<EVP_CIPHER_CTX> for success and B<NULL> for failure.
290*4724848cSchristos
291*4724848cSchristosEVP_EncryptInit_ex(), EVP_EncryptUpdate() and EVP_EncryptFinal_ex()
292*4724848cSchristosreturn 1 for success and 0 for failure.
293*4724848cSchristos
294*4724848cSchristosEVP_DecryptInit_ex() and EVP_DecryptUpdate() return 1 for success and 0 for failure.
295*4724848cSchristosEVP_DecryptFinal_ex() returns 0 if the decrypt failed or 1 for success.
296*4724848cSchristos
297*4724848cSchristosEVP_CipherInit_ex() and EVP_CipherUpdate() return 1 for success and 0 for failure.
298*4724848cSchristosEVP_CipherFinal_ex() returns 0 for a decryption failure or 1 for success.
299*4724848cSchristos
300*4724848cSchristosEVP_CIPHER_CTX_reset() returns 1 for success and 0 for failure.
301*4724848cSchristos
302*4724848cSchristosEVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
303*4724848cSchristosreturn an B<EVP_CIPHER> structure or NULL on error.
304*4724848cSchristos
305*4724848cSchristosEVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return a NID.
306*4724848cSchristos
307*4724848cSchristosEVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the block
308*4724848cSchristossize.
309*4724848cSchristos
310*4724848cSchristosEVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
311*4724848cSchristoslength.
312*4724848cSchristos
313*4724848cSchristosEVP_CIPHER_CTX_set_padding() always returns 1.
314*4724848cSchristos
315*4724848cSchristosEVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
316*4724848cSchristoslength, zero if the cipher does not use an IV and a negative value on error.
317*4724848cSchristos
318*4724848cSchristosEVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the NID of the cipher's
319*4724848cSchristosOBJECT IDENTIFIER or NID_undef if it has no defined OBJECT IDENTIFIER.
320*4724848cSchristos
321*4724848cSchristosEVP_CIPHER_CTX_cipher() returns an B<EVP_CIPHER> structure.
322*4724848cSchristos
323*4724848cSchristosEVP_CIPHER_param_to_asn1() and EVP_CIPHER_asn1_to_param() return greater
324*4724848cSchristosthan zero for success and zero or a negative number on failure.
325*4724848cSchristos
326*4724848cSchristosEVP_CIPHER_CTX_rand_key() returns 1 for success.
327*4724848cSchristos
328*4724848cSchristos=head1 CIPHER LISTING
329*4724848cSchristos
330*4724848cSchristosAll algorithms have a fixed key length unless otherwise stated.
331*4724848cSchristos
332*4724848cSchristosRefer to L<SEE ALSO> for the full list of ciphers available through the EVP
333*4724848cSchristosinterface.
334*4724848cSchristos
335*4724848cSchristos=over 4
336*4724848cSchristos
337*4724848cSchristos=item EVP_enc_null()
338*4724848cSchristos
339*4724848cSchristosNull cipher: does nothing.
340*4724848cSchristos
341*4724848cSchristos=back
342*4724848cSchristos
343*4724848cSchristos=head1 AEAD Interface
344*4724848cSchristos
345*4724848cSchristosThe EVP interface for Authenticated Encryption with Associated Data (AEAD)
346*4724848cSchristosmodes are subtly altered and several additional I<ctrl> operations are supported
347*4724848cSchristosdepending on the mode specified.
348*4724848cSchristos
349*4724848cSchristosTo specify additional authenticated data (AAD), a call to EVP_CipherUpdate(),
350*4724848cSchristosEVP_EncryptUpdate() or EVP_DecryptUpdate() should be made with the output
351*4724848cSchristosparameter B<out> set to B<NULL>.
352*4724848cSchristos
353*4724848cSchristosWhen decrypting, the return value of EVP_DecryptFinal() or EVP_CipherFinal()
354*4724848cSchristosindicates whether the operation was successful. If it does not indicate success,
355*4724848cSchristosthe authentication operation has failed and any output data B<MUST NOT> be used
356*4724848cSchristosas it is corrupted.
357*4724848cSchristos
358*4724848cSchristos=head2 GCM and OCB Modes
359*4724848cSchristos
360*4724848cSchristosThe following I<ctrl>s are supported in GCM and OCB modes.
361*4724848cSchristos
362*4724848cSchristos=over 4
363*4724848cSchristos
364*4724848cSchristos=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
365*4724848cSchristos
366*4724848cSchristosSets the IV length. This call can only be made before specifying an IV. If
367*4724848cSchristosnot called a default IV length is used.
368*4724848cSchristos
369*4724848cSchristosFor GCM AES and OCB AES the default is 12 (i.e. 96 bits). For OCB mode the
370*4724848cSchristosmaximum is 15.
371*4724848cSchristos
372*4724848cSchristos=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)
373*4724848cSchristos
374*4724848cSchristosWrites C<taglen> bytes of the tag value to the buffer indicated by C<tag>.
375*4724848cSchristosThis call can only be made when encrypting data and B<after> all data has been
376*4724848cSchristosprocessed (e.g. after an EVP_EncryptFinal() call).
377*4724848cSchristos
378*4724848cSchristosFor OCB, C<taglen> must either be 16 or the value previously set via
379*4724848cSchristosB<EVP_CTRL_AEAD_SET_TAG>.
380*4724848cSchristos
381*4724848cSchristos=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
382*4724848cSchristos
383*4724848cSchristosWhen decrypting, this call sets the expected tag to C<taglen> bytes from C<tag>.
384*4724848cSchristosC<taglen> must be between 1 and 16 inclusive.
385*4724848cSchristosThe tag must be set prior to any call to EVP_DecryptFinal() or
386*4724848cSchristosEVP_DecryptFinal_ex().
387*4724848cSchristos
388*4724848cSchristosFor GCM, this call is only valid when decrypting data.
389*4724848cSchristos
390*4724848cSchristosFor OCB, this call is valid when decrypting data to set the expected tag,
391*4724848cSchristosand when encrypting to set the desired tag length.
392*4724848cSchristos
393*4724848cSchristosIn OCB mode, calling this when encrypting with C<tag> set to C<NULL> sets the
394*4724848cSchristostag length. The tag length can only be set before specifying an IV. If this is
395*4724848cSchristosnot called prior to setting the IV during encryption, then a default tag length
396*4724848cSchristosis used.
397*4724848cSchristos
398*4724848cSchristosFor OCB AES, the default tag length is 16 (i.e. 128 bits).  It is also the
399*4724848cSchristosmaximum tag length for OCB.
400*4724848cSchristos
401*4724848cSchristos=back
402*4724848cSchristos
403*4724848cSchristos=head2 CCM Mode
404*4724848cSchristos
405*4724848cSchristosThe EVP interface for CCM mode is similar to that of the GCM mode but with a
406*4724848cSchristosfew additional requirements and different I<ctrl> values.
407*4724848cSchristos
408*4724848cSchristosFor CCM mode, the total plaintext or ciphertext length B<MUST> be passed to
409*4724848cSchristosEVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with the output
410*4724848cSchristosand input parameters (B<in> and B<out>) set to B<NULL> and the length passed in
411*4724848cSchristosthe B<inl> parameter.
412*4724848cSchristos
413*4724848cSchristosThe following I<ctrl>s are supported in CCM mode.
414*4724848cSchristos
415*4724848cSchristos=over 4
416*4724848cSchristos
417*4724848cSchristos=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
418*4724848cSchristos
419*4724848cSchristosThis call is made to set the expected B<CCM> tag value when decrypting or
420*4724848cSchristosthe length of the tag (with the C<tag> parameter set to NULL) when encrypting.
421*4724848cSchristosThe tag length is often referred to as B<M>. If not set a default value is
422*4724848cSchristosused (12 for AES). When decrypting, the tag needs to be set before passing
423*4724848cSchristosin data to be decrypted, but as in GCM and OCB mode, it can be set after
424*4724848cSchristospassing additional authenticated data (see L<AEAD Interface>).
425*4724848cSchristos
426*4724848cSchristos=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, ivlen, NULL)
427*4724848cSchristos
428*4724848cSchristosSets the CCM B<L> value. If not set a default is used (8 for AES).
429*4724848cSchristos
430*4724848cSchristos=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
431*4724848cSchristos
432*4724848cSchristosSets the CCM nonce (IV) length. This call can only be made before specifying
433*4724848cSchristosa nonce value. The nonce length is given by B<15 - L> so it is 7 by default for
434*4724848cSchristosAES.
435*4724848cSchristos
436*4724848cSchristos=back
437*4724848cSchristos
438*4724848cSchristos=head2 ChaCha20-Poly1305
439*4724848cSchristos
440*4724848cSchristosThe following I<ctrl>s are supported for the ChaCha20-Poly1305 AEAD algorithm.
441*4724848cSchristos
442*4724848cSchristos=over 4
443*4724848cSchristos
444*4724848cSchristos=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
445*4724848cSchristos
446*4724848cSchristosSets the nonce length. This call can only be made before specifying the nonce.
447*4724848cSchristosIf not called a default nonce length of 12 (i.e. 96 bits) is used. The maximum
448*4724848cSchristosnonce length is 12 bytes (i.e. 96-bits). If a nonce of less than 12 bytes is set
449*4724848cSchristosthen the nonce is automatically padded with leading 0 bytes to make it 12 bytes
450*4724848cSchristosin length.
451*4724848cSchristos
452*4724848cSchristos=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)
453*4724848cSchristos
454*4724848cSchristosWrites C<taglen> bytes of the tag value to the buffer indicated by C<tag>.
455*4724848cSchristosThis call can only be made when encrypting data and B<after> all data has been
456*4724848cSchristosprocessed (e.g. after an EVP_EncryptFinal() call).
457*4724848cSchristos
458*4724848cSchristosC<taglen> specified here must be 16 (B<POLY1305_BLOCK_SIZE>, i.e. 128-bits) or
459*4724848cSchristosless.
460*4724848cSchristos
461*4724848cSchristos=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)
462*4724848cSchristos
463*4724848cSchristosSets the expected tag to C<taglen> bytes from C<tag>.
464*4724848cSchristosThe tag length can only be set before specifying an IV.
465*4724848cSchristosC<taglen> must be between 1 and 16 (B<POLY1305_BLOCK_SIZE>) inclusive.
466*4724848cSchristosThis call is only valid when decrypting data.
467*4724848cSchristos
468*4724848cSchristos=back
469*4724848cSchristos
470*4724848cSchristos=head1 NOTES
471*4724848cSchristos
472*4724848cSchristosWhere possible the B<EVP> interface to symmetric ciphers should be used in
473*4724848cSchristospreference to the low-level interfaces. This is because the code then becomes
474*4724848cSchristostransparent to the cipher used and much more flexible. Additionally, the
475*4724848cSchristosB<EVP> interface will ensure the use of platform specific cryptographic
476*4724848cSchristosacceleration such as AES-NI (the low-level interfaces do not provide the
477*4724848cSchristosguarantee).
478*4724848cSchristos
479*4724848cSchristosPKCS padding works by adding B<n> padding bytes of value B<n> to make the total
480*4724848cSchristoslength of the encrypted data a multiple of the block size. Padding is always
481*4724848cSchristosadded so if the data is already a multiple of the block size B<n> will equal
482*4724848cSchristosthe block size. For example if the block size is 8 and 11 bytes are to be
483*4724848cSchristosencrypted then 5 padding bytes of value 5 will be added.
484*4724848cSchristos
485*4724848cSchristosWhen decrypting the final block is checked to see if it has the correct form.
486*4724848cSchristos
487*4724848cSchristosAlthough the decryption operation can produce an error if padding is enabled,
488*4724848cSchristosit is not a strong test that the input data or key is correct. A random block
489*4724848cSchristoshas better than 1 in 256 chance of being of the correct format and problems with
490*4724848cSchristosthe input data earlier on will not produce a final decrypt error.
491*4724848cSchristos
492*4724848cSchristosIf padding is disabled then the decryption operation will always succeed if
493*4724848cSchristosthe total amount of data decrypted is a multiple of the block size.
494*4724848cSchristos
495*4724848cSchristosThe functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(),
496*4724848cSchristosEVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained for
497*4724848cSchristoscompatibility with existing code. New code should use EVP_EncryptInit_ex(),
498*4724848cSchristosEVP_EncryptFinal_ex(), EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(),
499*4724848cSchristosEVP_CipherInit_ex() and EVP_CipherFinal_ex() because they can reuse an
500*4724848cSchristosexisting context without allocating and freeing it up on each call.
501*4724848cSchristos
502*4724848cSchristosThere are some differences between functions EVP_CipherInit() and
503*4724848cSchristosEVP_CipherInit_ex(), significant in some circumstances. EVP_CipherInit() fills
504*4724848cSchristosthe passed context object with zeros.  As a consequence, EVP_CipherInit() does
505*4724848cSchristosnot allow step-by-step initialization of the ctx when the I<key> and I<iv> are
506*4724848cSchristospassed in separate calls. It also means that the flags set for the CTX are
507*4724848cSchristosremoved, and it is especially important for the
508*4724848cSchristosB<EVP_CIPHER_CTX_FLAG_WRAP_ALLOW> flag treated specially in
509*4724848cSchristosEVP_CipherInit_ex().
510*4724848cSchristos
511*4724848cSchristosEVP_get_cipherbynid(), and EVP_get_cipherbyobj() are implemented as macros.
512*4724848cSchristos
513*4724848cSchristos=head1 BUGS
514*4724848cSchristos
515*4724848cSchristosB<EVP_MAX_KEY_LENGTH> and B<EVP_MAX_IV_LENGTH> only refer to the internal
516*4724848cSchristosciphers with default key lengths. If custom ciphers exceed these values the
517*4724848cSchristosresults are unpredictable. This is because it has become standard practice to
518*4724848cSchristosdefine a generic key as a fixed unsigned char array containing
519*4724848cSchristosB<EVP_MAX_KEY_LENGTH> bytes.
520*4724848cSchristos
521*4724848cSchristosThe ASN1 code is incomplete (and sometimes inaccurate) it has only been tested
522*4724848cSchristosfor certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode.
523*4724848cSchristos
524*4724848cSchristos=head1 EXAMPLES
525*4724848cSchristos
526*4724848cSchristosEncrypt a string using IDEA:
527*4724848cSchristos
528*4724848cSchristos int do_crypt(char *outfile)
529*4724848cSchristos {
530*4724848cSchristos     unsigned char outbuf[1024];
531*4724848cSchristos     int outlen, tmplen;
532*4724848cSchristos     /*
533*4724848cSchristos      * Bogus key and IV: we'd normally set these from
534*4724848cSchristos      * another source.
535*4724848cSchristos      */
536*4724848cSchristos     unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
537*4724848cSchristos     unsigned char iv[] = {1,2,3,4,5,6,7,8};
538*4724848cSchristos     char intext[] = "Some Crypto Text";
539*4724848cSchristos     EVP_CIPHER_CTX *ctx;
540*4724848cSchristos     FILE *out;
541*4724848cSchristos
542*4724848cSchristos     ctx = EVP_CIPHER_CTX_new();
543*4724848cSchristos     EVP_EncryptInit_ex(ctx, EVP_idea_cbc(), NULL, key, iv);
544*4724848cSchristos
545*4724848cSchristos     if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) {
546*4724848cSchristos         /* Error */
547*4724848cSchristos         EVP_CIPHER_CTX_free(ctx);
548*4724848cSchristos         return 0;
549*4724848cSchristos     }
550*4724848cSchristos     /*
551*4724848cSchristos      * Buffer passed to EVP_EncryptFinal() must be after data just
552*4724848cSchristos      * encrypted to avoid overwriting it.
553*4724848cSchristos      */
554*4724848cSchristos     if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) {
555*4724848cSchristos         /* Error */
556*4724848cSchristos         EVP_CIPHER_CTX_free(ctx);
557*4724848cSchristos         return 0;
558*4724848cSchristos     }
559*4724848cSchristos     outlen += tmplen;
560*4724848cSchristos     EVP_CIPHER_CTX_free(ctx);
561*4724848cSchristos     /*
562*4724848cSchristos      * Need binary mode for fopen because encrypted data is
563*4724848cSchristos      * binary data. Also cannot use strlen() on it because
564*4724848cSchristos      * it won't be NUL terminated and may contain embedded
565*4724848cSchristos      * NULs.
566*4724848cSchristos      */
567*4724848cSchristos     out = fopen(outfile, "wb");
568*4724848cSchristos     if (out == NULL) {
569*4724848cSchristos         /* Error */
570*4724848cSchristos         return 0;
571*4724848cSchristos     }
572*4724848cSchristos     fwrite(outbuf, 1, outlen, out);
573*4724848cSchristos     fclose(out);
574*4724848cSchristos     return 1;
575*4724848cSchristos }
576*4724848cSchristos
577*4724848cSchristosThe ciphertext from the above example can be decrypted using the B<openssl>
578*4724848cSchristosutility with the command line (shown on two lines for clarity):
579*4724848cSchristos
580*4724848cSchristos openssl idea -d \
581*4724848cSchristos     -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 <filename
582*4724848cSchristos
583*4724848cSchristosGeneral encryption and decryption function example using FILE I/O and AES128
584*4724848cSchristoswith a 128-bit key:
585*4724848cSchristos
586*4724848cSchristos int do_crypt(FILE *in, FILE *out, int do_encrypt)
587*4724848cSchristos {
588*4724848cSchristos     /* Allow enough space in output buffer for additional block */
589*4724848cSchristos     unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
590*4724848cSchristos     int inlen, outlen;
591*4724848cSchristos     EVP_CIPHER_CTX *ctx;
592*4724848cSchristos     /*
593*4724848cSchristos      * Bogus key and IV: we'd normally set these from
594*4724848cSchristos      * another source.
595*4724848cSchristos      */
596*4724848cSchristos     unsigned char key[] = "0123456789abcdeF";
597*4724848cSchristos     unsigned char iv[] = "1234567887654321";
598*4724848cSchristos
599*4724848cSchristos     /* Don't set key or IV right away; we want to check lengths */
600*4724848cSchristos     ctx = EVP_CIPHER_CTX_new();
601*4724848cSchristos     EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, NULL, NULL,
602*4724848cSchristos                       do_encrypt);
603*4724848cSchristos     OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16);
604*4724848cSchristos     OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == 16);
605*4724848cSchristos
606*4724848cSchristos     /* Now we can set key and IV */
607*4724848cSchristos     EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt);
608*4724848cSchristos
609*4724848cSchristos     for (;;) {
610*4724848cSchristos         inlen = fread(inbuf, 1, 1024, in);
611*4724848cSchristos         if (inlen <= 0)
612*4724848cSchristos             break;
613*4724848cSchristos         if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
614*4724848cSchristos             /* Error */
615*4724848cSchristos             EVP_CIPHER_CTX_free(ctx);
616*4724848cSchristos             return 0;
617*4724848cSchristos         }
618*4724848cSchristos         fwrite(outbuf, 1, outlen, out);
619*4724848cSchristos     }
620*4724848cSchristos     if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
621*4724848cSchristos         /* Error */
622*4724848cSchristos         EVP_CIPHER_CTX_free(ctx);
623*4724848cSchristos         return 0;
624*4724848cSchristos     }
625*4724848cSchristos     fwrite(outbuf, 1, outlen, out);
626*4724848cSchristos
627*4724848cSchristos     EVP_CIPHER_CTX_free(ctx);
628*4724848cSchristos     return 1;
629*4724848cSchristos }
630*4724848cSchristos
631*4724848cSchristos
632*4724848cSchristos=head1 SEE ALSO
633*4724848cSchristos
634*4724848cSchristosL<evp(7)>
635*4724848cSchristos
636*4724848cSchristosSupported ciphers are listed in:
637*4724848cSchristos
638*4724848cSchristosL<EVP_aes(3)>,
639*4724848cSchristosL<EVP_aria(3)>,
640*4724848cSchristosL<EVP_bf(3)>,
641*4724848cSchristosL<EVP_camellia(3)>,
642*4724848cSchristosL<EVP_cast5(3)>,
643*4724848cSchristosL<EVP_chacha20(3)>,
644*4724848cSchristosL<EVP_des(3)>,
645*4724848cSchristosL<EVP_desx(3)>,
646*4724848cSchristosL<EVP_idea(3)>,
647*4724848cSchristosL<EVP_rc2(3)>,
648*4724848cSchristosL<EVP_rc4(3)>,
649*4724848cSchristosL<EVP_rc5(3)>,
650*4724848cSchristosL<EVP_seed(3)>,
651*4724848cSchristosL<EVP_sm4(3)>
652*4724848cSchristos
653*4724848cSchristos=head1 HISTORY
654*4724848cSchristos
655*4724848cSchristosSupport for OCB mode was added in OpenSSL 1.1.0.
656*4724848cSchristos
657*4724848cSchristosB<EVP_CIPHER_CTX> was made opaque in OpenSSL 1.1.0.  As a result,
658*4724848cSchristosEVP_CIPHER_CTX_reset() appeared and EVP_CIPHER_CTX_cleanup()
659*4724848cSchristosdisappeared.  EVP_CIPHER_CTX_init() remains as an alias for
660*4724848cSchristosEVP_CIPHER_CTX_reset().
661*4724848cSchristos
662*4724848cSchristos=head1 COPYRIGHT
663*4724848cSchristos
664*4724848cSchristosCopyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
665*4724848cSchristos
666*4724848cSchristosLicensed under the OpenSSL license (the "License").  You may not use
667*4724848cSchristosthis file except in compliance with the License.  You can obtain a copy
668*4724848cSchristosin the file LICENSE in the source distribution or at
669*4724848cSchristosL<https://www.openssl.org/source/license.html>.
670*4724848cSchristos
671*4724848cSchristos=cut
672