1EVP_EncryptInit(3)                  OpenSSL                 EVP_EncryptInit(3)
2
3
4

NAME

6       EVP_CIPHER_CTX_init, EVP_EncryptInit_ex, EVP_EncryptUpdate,
7       EVP_EncryptFinal_ex, EVP_DecryptInit_ex, EVP_DecryptUpdate,
8       EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherUpdate,
9       EVP_CipherFinal_ex, EVP_CIPHER_CTX_set_key_length, EVP_CIPHER_CTX_ctrl,
10       EVP_CIPHER_CTX_cleanup, EVP_EncryptInit, EVP_EncryptFinal,
11       EVP_DecryptInit, EVP_DecryptFinal, EVP_CipherInit, EVP_CipherFinal,
12       EVP_get_cipherbyname, EVP_get_cipherbynid, EVP_get_cipherbyobj,
13       EVP_CIPHER_nid, EVP_CIPHER_block_size, EVP_CIPHER_key_length,
14       EVP_CIPHER_iv_length, EVP_CIPHER_flags, EVP_CIPHER_mode,
15       EVP_CIPHER_type, EVP_CIPHER_CTX_cipher, EVP_CIPHER_CTX_nid,
16       EVP_CIPHER_CTX_block_size, EVP_CIPHER_CTX_key_length,
17       EVP_CIPHER_CTX_iv_length, EVP_CIPHER_CTX_get_app_data,
18       EVP_CIPHER_CTX_set_app_data, EVP_CIPHER_CTX_type, EVP_CIPHER_CTX_flags,
19       EVP_CIPHER_CTX_mode, EVP_CIPHER_param_to_asn1,
20       EVP_CIPHER_asn1_to_param, EVP_CIPHER_CTX_set_padding - EVP cipher
21       routines
22

SYNOPSIS

24        #include <openssl/evp.h>
25
26        void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a);
27
28        int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
29                ENGINE *impl, unsigned char *key, unsigned char *iv);
30        int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
31                int *outl, unsigned char *in, int inl);
32        int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out,
33                int *outl);
34
35        int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
36                ENGINE *impl, unsigned char *key, unsigned char *iv);
37        int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
38                int *outl, unsigned char *in, int inl);
39        int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm,
40                int *outl);
41
42        int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
43                ENGINE *impl, unsigned char *key, unsigned char *iv, int enc);
44        int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
45                int *outl, unsigned char *in, int inl);
46        int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm,
47                int *outl);
48
49        int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
50                unsigned char *key, unsigned char *iv);
51        int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out,
52                int *outl);
53
54        int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
55                unsigned char *key, unsigned char *iv);
56        int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm,
57                int *outl);
58
59        int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
60                unsigned char *key, unsigned char *iv, int enc);
61        int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm,
62                int *outl);
63
64        int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
65        int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
66        int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
67        int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a);
68
69        const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
70        #define EVP_get_cipherbynid(a) EVP_get_cipherbyname(OBJ_nid2sn(a))
71        #define EVP_get_cipherbyobj(a) EVP_get_cipherbynid(OBJ_obj2nid(a))
72
73        #define EVP_CIPHER_nid(e)              ((e)->nid)
74        #define EVP_CIPHER_block_size(e)       ((e)->block_size)
75        #define EVP_CIPHER_key_length(e)       ((e)->key_len)
76        #define EVP_CIPHER_iv_length(e)                ((e)->iv_len)
77        #define EVP_CIPHER_flags(e)            ((e)->flags)
78        #define EVP_CIPHER_mode(e)             ((e)->flags) & EVP_CIPH_MODE)
79        int EVP_CIPHER_type(const EVP_CIPHER *ctx);
80
81        #define EVP_CIPHER_CTX_cipher(e)       ((e)->cipher)
82        #define EVP_CIPHER_CTX_nid(e)          ((e)->cipher->nid)
83        #define EVP_CIPHER_CTX_block_size(e)   ((e)->cipher->block_size)
84        #define EVP_CIPHER_CTX_key_length(e)   ((e)->key_len)
85        #define EVP_CIPHER_CTX_iv_length(e)    ((e)->cipher->iv_len)
86        #define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data)
87        #define EVP_CIPHER_CTX_set_app_data(e,d) ((e)->app_data=(char *)(d))
88        #define EVP_CIPHER_CTX_type(c)         EVP_CIPHER_type(EVP_CIPHER_CTX_cipher(c))
89        #define EVP_CIPHER_CTX_flags(e)                ((e)->cipher->flags)
90        #define EVP_CIPHER_CTX_mode(e)         ((e)->cipher->flags & EVP_CIPH_MODE)
91
92        int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
93        int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
94
95        const EVP_CIPHER *EVP_des_ede3(void);
96        const EVP_CIPHER *EVP_des_ede3_ecb(void);
97        const EVP_CIPHER *EVP_des_ede3_cfb64(void);
98        const EVP_CIPHER *EVP_des_ede3_cfb1(void);
99        const EVP_CIPHER *EVP_des_ede3_cfb8(void);
100        const EVP_CIPHER *EVP_des_ede3_ofb(void);
101        const EVP_CIPHER *EVP_des_ede3_cbc(void);
102        const EVP_CIPHER *EVP_aes_128_ecb(void);
103        const EVP_CIPHER *EVP_aes_128_cbc(void);
104        const EVP_CIPHER *EVP_aes_128_cfb1(void);
105        const EVP_CIPHER *EVP_aes_128_cfb8(void);
106        const EVP_CIPHER *EVP_aes_128_cfb128(void);
107        const EVP_CIPHER *EVP_aes_128_ofb(void);
108        const EVP_CIPHER *EVP_aes_192_ecb(void);
109        const EVP_CIPHER *EVP_aes_192_cbc(void);
110        const EVP_CIPHER *EVP_aes_192_cfb1(void);
111        const EVP_CIPHER *EVP_aes_192_cfb8(void);
112        const EVP_CIPHER *EVP_aes_192_cfb128(void);
113        const EVP_CIPHER *EVP_aes_192_ofb(void);
114        const EVP_CIPHER *EVP_aes_256_ecb(void);
115        const EVP_CIPHER *EVP_aes_256_cbc(void);
116        const EVP_CIPHER *EVP_aes_256_cfb1(void);
117        const EVP_CIPHER *EVP_aes_256_cfb8(void);
118        const EVP_CIPHER *EVP_aes_256_cfb128(void);
119        const EVP_CIPHER *EVP_aes_256_ofb(void);
120

DESCRIPTION

122       The EVP cipher routines are a high level interface to certain symmetric
123       ciphers.
124
125       EVP_CIPHER_CTX_init() initializes cipher contex ctx.
126
127       EVP_EncryptInit_ex() sets up cipher context ctx for encryption with
128       cipher type from ENGINE impl. ctx must be initialized before calling
129       this function. type is normally supplied by a function such as
130       EVP_des_cbc(). If impl is NULL then the default implementation is used.
131       key is the symmetric key to use and iv is the IV to use (if necessary),
132       the actual number of bytes used for the key and IV depends on the
133       cipher. It is possible to set all parameters to NULL except type in an
134       initial call and supply the remaining parameters in subsequent calls,
135       all of which have type set to NULL. This is done when the default
136       cipher parameters are not appropriate.
137
138       EVP_EncryptUpdate() encrypts inl bytes from the buffer in and writes
139       the encrypted version to out. This function can be called multiple
140       times to encrypt successive blocks of data. The amount of data written
141       depends on the block alignment of the encrypted data: as a result the
142       amount of data written may be anything from zero bytes to (inl +
143       cipher_block_size - 1) so outl should contain sufficient room. The
144       actual number of bytes written is placed in outl.
145
146       If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts
147       the "final" data, that is any data that remains in a partial block.  It
148       uses standard block padding (aka PKCS padding). The encrypted final
149       data is written to out which should have sufficient space for one
150       cipher block. The number of bytes written is placed in outl. After this
151       function is called the encryption operation is finished and no further
152       calls to EVP_EncryptUpdate() should be made.
153
154       If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any
155       more data and it will return an error if any data remains in a partial
156       block: that is if the total data length is not a multiple of the block
157       size.
158
159       EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are
160       the corresponding decryption operations. EVP_DecryptFinal() will return
161       an error code if padding is enabled and the final block is not
162       correctly formatted. The parameters and restrictions are identical to
163       the encryption operations except that if padding is enabled the
164       decrypted data buffer out passed to EVP_DecryptUpdate() should have
165       sufficient room for (inl + cipher_block_size) bytes unless the cipher
166       block size is 1 in which case inl bytes is sufficient.
167
168       EVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex() are
169       functions that can be used for decryption or encryption. The operation
170       performed depends on the value of the enc parameter. It should be set
171       to 1 for encryption, 0 for decryption and -1 to leave the value
172       unchanged (the actual value of 'enc' being supplied in a previous
173       call).
174
175       EVP_CIPHER_CTX_cleanup() clears all information from a cipher context
176       and free up any allocated memory associate with it. It should be called
177       after all operations using a cipher are complete so sensitive
178       information does not remain in memory.
179
180       EVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit() behave in a
181       similar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex and
182       EVP_CipherInit_ex() except the ctx paramter does not need to be
183       initialized and they always use the default cipher implementation.
184
185       EVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal() behave in
186       a similar way to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and
187       EVP_CipherFinal_ex() except ctx is automatically cleaned up after the
188       call.
189
190       EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
191       return an EVP_CIPHER structure when passed a cipher name, a NID or an
192       ASN1_OBJECT structure.
193
194       EVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return the NID of a cipher
195       when passed an EVP_CIPHER or EVP_CIPHER_CTX structure.  The actual NID
196       value is an internal value which may not have a corresponding OBJECT
197       IDENTIFIER.
198
199       EVP_CIPHER_CTX_set_padding() enables or disables padding. By default
200       encryption operations are padded using standard block padding and the
201       padding is checked and removed when decrypting. If the pad parameter is
202       zero then no padding is performed, the total amount of data encrypted
203       or decrypted must then be a multiple of the block size or an error will
204       occur.
205
206       EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
207       length of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX
208       structure. The constant EVP_MAX_KEY_LENGTH is the maximum key length
209       for all ciphers. Note: although EVP_CIPHER_key_length() is fixed for a
210       given cipher, the value of EVP_CIPHER_CTX_key_length() may be different
211       for variable key length ciphers.
212
213       EVP_CIPHER_CTX_set_key_length() sets the key length of the cipher ctx.
214       If the cipher is a fixed length cipher then attempting to set the key
215       length to any value other than the fixed value is an error.
216
217       EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
218       length of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX.  It
219       will return zero if the cipher does not use an IV.  The constant
220       EVP_MAX_IV_LENGTH is the maximum IV length for all ciphers.
221
222       EVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the
223       block size of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX
224       structure. The constant EVP_MAX_IV_LENGTH is also the maximum block
225       length for all ciphers.
226
227       EVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the type of the
228       passed cipher or context. This "type" is the actual NID of the cipher
229       OBJECT IDENTIFIER as such it ignores the cipher parameters and 40 bit
230       RC2 and 128 bit RC2 have the same NID. If the cipher does not have an
231       object identifier or does not have ASN1 support this function will
232       return NID_undef.
233
234       EVP_CIPHER_CTX_cipher() returns the EVP_CIPHER structure when passed an
235       EVP_CIPHER_CTX structure.
236
237       EVP_CIPHER_mode() and EVP_CIPHER_CTX_mode() return the block cipher
238       mode: EVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE or
239       EVP_CIPH_OFB_MODE. If the cipher is a stream cipher then
240       EVP_CIPH_STREAM_CIPHER is returned.
241
242       EVP_CIPHER_param_to_asn1() sets the AlgorithmIdentifier "parameter"
243       based on the passed cipher. This will typically include any parameters
244       and an IV. The cipher IV (if any) must be set when this call is made.
245       This call should be made before the cipher is actually "used" (before
246       any EVP_EncryptUpdate(), EVP_DecryptUpdate() calls for example). This
247       function may fail if the cipher does not have any ASN1 support.
248
249       EVP_CIPHER_asn1_to_param() sets the cipher parameters based on an ASN1
250       AlgorithmIdentifier "parameter". The precise effect depends on the
251       cipher In the case of RC2, for example, it will set the IV and
252       effective key length.  This function should be called after the base
253       cipher type is set but before the key is set. For example
254       EVP_CipherInit() will be called with the IV and key set to NULL,
255       EVP_CIPHER_asn1_to_param() will be called and finally EVP_CipherInit()
256       again with all parameters except the key set to NULL. It is possible
257       for this function to fail if the cipher does not have any ASN1 support
258       or the parameters cannot be set (for example the RC2 effective key
259       length is not supported.
260
261       EVP_CIPHER_CTX_ctrl() allows various cipher specific parameters to be
262       determined and set. Currently only the RC2 effective key length and the
263       number of rounds of RC5 can be set.
264

RETURN VALUES

266       EVP_EncryptInit_ex(), EVP_EncryptUpdate() and EVP_EncryptFinal_ex()
267       return 1 for success and 0 for failure.
268
269       EVP_DecryptInit_ex() and EVP_DecryptUpdate() return 1 for success and 0
270       for failure.  EVP_DecryptFinal_ex() returns 0 if the decrypt failed or
271       1 for success.
272
273       EVP_CipherInit_ex() and EVP_CipherUpdate() return 1 for success and 0
274       for failure.  EVP_CipherFinal_ex() returns 0 for a decryption failure
275       or 1 for success.
276
277       EVP_CIPHER_CTX_cleanup() returns 1 for success and 0 for failure.
278
279       EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
280       return an EVP_CIPHER structure or NULL on error.
281
282       EVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return a NID.
283
284       EVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the
285       block size.
286
287       EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
288       length.
289
290       EVP_CIPHER_CTX_set_padding() always returns 1.
291
292       EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
293       length or zero if the cipher does not use an IV.
294
295       EVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the NID of the
296       cipher's OBJECT IDENTIFIER or NID_undef if it has no defined OBJECT
297       IDENTIFIER.
298
299       EVP_CIPHER_CTX_cipher() returns an EVP_CIPHER structure.
300
301       EVP_CIPHER_param_to_asn1() and EVP_CIPHER_asn1_to_param() return 1 for
302       success or zero for failure.
303

CIPHER LISTING

305       All algorithms have a fixed key length unless otherwise stated.
306
307       EVP_enc_null()
308           Null cipher: does nothing.
309
310       EVP_des_cbc(void), EVP_des_ecb(void), EVP_des_cfb(void),
311       EVP_des_ofb(void)
312           DES in CBC, ECB, CFB and OFB modes respectively.
313
314       EVP_des_ede_cbc(void), EVP_des_ede(), EVP_des_ede_ofb(void),
315       EVP_des_ede_cfb(void)
316           Two key triple DES in CBC, ECB, CFB and OFB modes respectively.
317
318       EVP_des_ede3_cbc(void), EVP_des_ede3(), EVP_des_ede3_ofb(void),
319       EVP_des_ede3_cfb(void)
320           Three key triple DES in CBC, ECB, CFB and OFB modes respectively.
321
322       EVP_desx_cbc(void)
323           DESX algorithm in CBC mode.
324
325       EVP_aes_128_cbc(void), EVP_aes_128_ecb(), EVP_aes_128_ofb(void),
326       EVP_aes_128_cfb1(void), EVP_aes_128_cfb8(void),
327       EVP_aes_128_cfb128(void)
328           AES with 128 bit key length in CBC, ECB, OFB and CFB modes
329           respectively.
330
331       EVP_aes_192_cbc(void), EVP_aes_192_ecb(), EVP_aes_192_ofb(void),
332       EVP_aes_192_cfb1(void), EVP_aes_192_cfb8(void),
333       EVP_aes_192_cfb128(void)
334           AES with 192 bit key length in CBC, ECB, OFB and CFB modes
335           respectively.
336
337       EVP_aes_256_cbc(void), EVP_aes_256_ecb(), EVP_aes_256_ofb(void),
338       EVP_aes_256_cfb1(void), EVP_aes_256_cfb8(void),
339       EVP_aes_256_cfb128(void)
340           AES with 256 bit key length in CBC, ECB, OFB and CFB modes
341           respectively.
342
343       EVP_rc4(void)
344           RC4 stream cipher. This is a variable key length cipher with
345           default key length 128 bits.
346
347       EVP_rc4_40(void)
348           RC4 stream cipher with 40 bit key length. This is obsolete and new
349           code should use EVP_rc4() and the EVP_CIPHER_CTX_set_key_length()
350           function.
351
352       EVP_idea_cbc() EVP_idea_ecb(void), EVP_idea_cfb(void),
353       EVP_idea_ofb(void), EVP_idea_cbc(void)
354           IDEA encryption algorithm in CBC, ECB, CFB and OFB modes
355           respectively.
356
357       EVP_rc2_cbc(void), EVP_rc2_ecb(void), EVP_rc2_cfb(void),
358       EVP_rc2_ofb(void)
359           RC2 encryption algorithm in CBC, ECB, CFB and OFB modes
360           respectively. This is a variable key length cipher with an
361           additional parameter called "effective key bits" or "effective key
362           length".  By default both are set to 128 bits.
363
364       EVP_rc2_40_cbc(void), EVP_rc2_64_cbc(void)
365           RC2 algorithm in CBC mode with a default key length and effective
366           key length of 40 and 64 bits.  These are obsolete and new code
367           should use EVP_rc2_cbc(), EVP_CIPHER_CTX_set_key_length() and
368           EVP_CIPHER_CTX_ctrl() to set the key length and effective key
369           length.
370
371       EVP_bf_cbc(void), EVP_bf_ecb(void), EVP_bf_cfb(void), EVP_bf_ofb(void);
372           Blowfish encryption algorithm in CBC, ECB, CFB and OFB modes
373           respectively. This is a variable key length cipher.
374
375       EVP_cast5_cbc(void), EVP_cast5_ecb(void), EVP_cast5_cfb(void),
376       EVP_cast5_ofb(void)
377           CAST encryption algorithm in CBC, ECB, CFB and OFB modes
378           respectively. This is a variable key length cipher.
379
380       EVP_rc5_32_12_16_cbc(void), EVP_rc5_32_12_16_ecb(void),
381       EVP_rc5_32_12_16_cfb(void), EVP_rc5_32_12_16_ofb(void)
382           RC5 encryption algorithm in CBC, ECB, CFB and OFB modes
383           respectively. This is a variable key length cipher with an
384           additional "number of rounds" parameter. By default the key length
385           is set to 128 bits and 12 rounds.
386

NOTES

388       Where possible the EVP interface to symmetric ciphers should be used in
389       preference to the low level interfaces. This is because the code then
390       becomes transparent to the cipher used and much more flexible.
391
392       PKCS padding works by adding n padding bytes of value n to make the
393       total length of the encrypted data a multiple of the block size.
394       Padding is always added so if the data is already a multiple of the
395       block size n will equal the block size. For example if the block size
396       is 8 and 11 bytes are to be encrypted then 5 padding bytes of value 5
397       will be added.
398
399       When decrypting the final block is checked to see if it has the correct
400       form.
401
402       Although the decryption operation can produce an error if padding is
403       enabled, it is not a strong test that the input data or key is correct.
404       A random block has better than 1 in 256 chance of being of the correct
405       format and problems with the input data earlier on will not produce a
406       final decrypt error.
407
408       If padding is disabled then the decryption operation will always
409       succeed if the total amount of data decrypted is a multiple of the
410       block size.
411
412       The functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(),
413       EVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained
414       for compatibility with existing code. New code should use
415       EVP_EncryptInit_ex(), EVP_EncryptFinal_ex(), EVP_DecryptInit_ex(),
416       EVP_DecryptFinal_ex(), EVP_CipherInit_ex() and EVP_CipherFinal_ex()
417       because they can reuse an existing context without allocating and
418       freeing it up on each call.
419

BUGS

421       For RC5 the number of rounds can currently only be set to 8, 12 or 16.
422       This is a limitation of the current RC5 code rather than the EVP
423       interface.
424
425       EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal
426       ciphers with default key lengths. If custom ciphers exceed these values
427       the results are unpredictable. This is because it has become standard
428       practice to define a generic key as a fixed unsigned char array
429       containing EVP_MAX_KEY_LENGTH bytes.
430
431       The ASN1 code is incomplete (and sometimes inaccurate) it has only been
432       tested for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC
433       mode.
434

EXAMPLES

436       Get the number of rounds used in RC5:
437
438        int nrounds;
439        EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC5_ROUNDS, 0, &nrounds);
440
441       Get the RC2 effective key length:
442
443        int key_bits;
444        EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC2_KEY_BITS, 0, &key_bits);
445
446       Set the number of rounds used in RC5:
447
448        int nrounds;
449        EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC5_ROUNDS, nrounds, NULL);
450
451       Set the effective key length used in RC2:
452
453        int key_bits;
454        EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
455
456       Encrypt a string using blowfish:
457
458        int do_crypt(char *outfile)
459               {
460               unsigned char outbuf[1024];
461               int outlen, tmplen;
462               /* Bogus key and IV: we'd normally set these from
463                * another source.
464                */
465               unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
466               unsigned char iv[] = {1,2,3,4,5,6,7,8};
467               char intext[] = "Some Crypto Text";
468               EVP_CIPHER_CTX ctx;
469               FILE *out;
470               EVP_CIPHER_CTX_init(&ctx);
471               EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv);
472
473               if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext)))
474                       {
475                       /* Error */
476                       return 0;
477                       }
478               /* Buffer passed to EVP_EncryptFinal() must be after data just
479                * encrypted to avoid overwriting it.
480                */
481               if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen))
482                       {
483                       /* Error */
484                       return 0;
485                       }
486               outlen += tmplen;
487               EVP_CIPHER_CTX_cleanup(&ctx);
488               /* Need binary mode for fopen because encrypted data is
489                * binary data. Also cannot use strlen() on it because
490                * it wont be null terminated and may contain embedded
491                * nulls.
492                */
493               out = fopen(outfile, "wb");
494               fwrite(outbuf, 1, outlen, out);
495               fclose(out);
496               return 1;
497               }
498
499       The ciphertext from the above example can be decrypted using the
500       openssl utility with the command line:
501
502        S<openssl bf -in cipher.bin -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 -d>
503
504       General encryption, decryption function example using FILE I/O and RC2
505       with an 80 bit key:
506
507        int do_crypt(FILE *in, FILE *out, int do_encrypt)
508               {
509               /* Allow enough space in output buffer for additional block */
510               inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
511               int inlen, outlen;
512               /* Bogus key and IV: we'd normally set these from
513                * another source.
514                */
515               unsigned char key[] = "0123456789";
516               unsigned char iv[] = "12345678";
517               /* Don't set key or IV because we will modify the parameters */
518               EVP_CIPHER_CTX_init(&ctx);
519               EVP_CipherInit_ex(&ctx, EVP_rc2(), NULL, NULL, NULL, do_encrypt);
520               EVP_CIPHER_CTX_set_key_length(&ctx, 10);
521               /* We finished modifying parameters so now we can set key and IV */
522               EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
523
524               for(;;)
525                       {
526                       inlen = fread(inbuf, 1, 1024, in);
527                       if(inlen <= 0) break;
528                       if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen))
529                               {
530                               /* Error */
531                               EVP_CIPHER_CTX_cleanup(&ctx);
532                               return 0;
533                               }
534                       fwrite(outbuf, 1, outlen, out);
535                       }
536               if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen))
537                       {
538                       /* Error */
539                       EVP_CIPHER_CTX_cleanup(&ctx);
540                       return 0;
541                       }
542               fwrite(outbuf, 1, outlen, out);
543
544               EVP_CIPHER_CTX_cleanup(&ctx);
545               return 1;
546               }
547

SEE ALSO

549       evp(3)
550

HISTORY

552       EVP_CIPHER_CTX_init(), EVP_EncryptInit_ex(), EVP_EncryptFinal_ex(),
553       EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(), EVP_CipherInit_ex(),
554       EVP_CipherFinal_ex() and EVP_CIPHER_CTX_set_padding() appeared in
555       OpenSSL 0.9.7.
556
557
558
5591.0.0e                            2011-09-07                EVP_EncryptInit(3)
Impressum