summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/doc/EVP_EncryptInit.pod
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/doc/EVP_EncryptInit.pod')
-rw-r--r--src/lib/libcrypto/doc/EVP_EncryptInit.pod546
1 files changed, 0 insertions, 546 deletions
diff --git a/src/lib/libcrypto/doc/EVP_EncryptInit.pod b/src/lib/libcrypto/doc/EVP_EncryptInit.pod
deleted file mode 100644
index a0a782e772..0000000000
--- a/src/lib/libcrypto/doc/EVP_EncryptInit.pod
+++ /dev/null
@@ -1,546 +0,0 @@
1=pod
2
3=head1 NAME
4
5EVP_CIPHER_CTX_init, EVP_EncryptInit_ex, EVP_EncryptUpdate,
6EVP_EncryptFinal_ex, EVP_DecryptInit_ex, EVP_DecryptUpdate,
7EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherUpdate,
8EVP_CipherFinal_ex, EVP_CIPHER_CTX_set_key_length,
9EVP_CIPHER_CTX_ctrl, EVP_CIPHER_CTX_cleanup, EVP_EncryptInit,
10EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal,
11EVP_CipherInit, EVP_CipherFinal, EVP_get_cipherbyname,
12EVP_get_cipherbynid, EVP_get_cipherbyobj, EVP_CIPHER_nid,
13EVP_CIPHER_block_size, EVP_CIPHER_key_length, EVP_CIPHER_iv_length,
14EVP_CIPHER_flags, EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher,
15EVP_CIPHER_CTX_nid, EVP_CIPHER_CTX_block_size, EVP_CIPHER_CTX_key_length,
16EVP_CIPHER_CTX_iv_length, EVP_CIPHER_CTX_get_app_data,
17EVP_CIPHER_CTX_set_app_data, EVP_CIPHER_CTX_type, EVP_CIPHER_CTX_flags,
18EVP_CIPHER_CTX_mode, EVP_CIPHER_param_to_asn1, EVP_CIPHER_asn1_to_param,
19EVP_CIPHER_CTX_set_padding, EVP_enc_null, EVP_des_cbc, EVP_des_ecb,
20EVP_des_cfb, EVP_des_ofb, EVP_des_ede_cbc, EVP_des_ede, EVP_des_ede_ofb,
21EVP_des_ede_cfb, EVP_des_ede3_cbc, EVP_des_ede3, EVP_des_ede3_ofb,
22EVP_des_ede3_cfb, EVP_desx_cbc, EVP_rc4, EVP_rc4_40, EVP_idea_cbc,
23EVP_idea_ecb, EVP_idea_cfb, EVP_idea_ofb, EVP_idea_cbc, EVP_rc2_cbc,
24EVP_rc2_ecb, EVP_rc2_cfb, EVP_rc2_ofb, EVP_rc2_40_cbc, EVP_rc2_64_cbc,
25EVP_bf_cbc, EVP_bf_ecb, EVP_bf_cfb, EVP_bf_ofb, EVP_cast5_cbc,
26EVP_cast5_ecb, EVP_cast5_cfb, EVP_cast5_ofb,
27EVP_aes_128_gcm, EVP_aes_192_gcm, EVP_aes_256_gcm, EVP_aes_128_ccm,
28EVP_aes_192_ccm, EVP_aes_256_ccm - EVP cipher routines
29
30=head1 SYNOPSIS
31
32 #include <openssl/evp.h>
33
34 void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a);
35
36 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
37 ENGINE *impl, unsigned char *key, unsigned char *iv);
38 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
39 int *outl, unsigned char *in, int inl);
40 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out,
41 int *outl);
42
43 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
44 ENGINE *impl, unsigned char *key, unsigned char *iv);
45 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
46 int *outl, unsigned char *in, int inl);
47 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm,
48 int *outl);
49
50 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
51 ENGINE *impl, unsigned char *key, unsigned char *iv, int enc);
52 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
53 int *outl, unsigned char *in, int inl);
54 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm,
55 int *outl);
56
57 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
58 unsigned char *key, unsigned char *iv);
59 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out,
60 int *outl);
61
62 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
63 unsigned char *key, unsigned char *iv);
64 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm,
65 int *outl);
66
67 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
68 unsigned char *key, unsigned char *iv, int enc);
69 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm,
70 int *outl);
71
72 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
73 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
74 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
75 int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a);
76
77 const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
78 #define EVP_get_cipherbynid(a) EVP_get_cipherbyname(OBJ_nid2sn(a))
79 #define EVP_get_cipherbyobj(a) EVP_get_cipherbynid(OBJ_obj2nid(a))
80
81 #define EVP_CIPHER_nid(e) ((e)->nid)
82 #define EVP_CIPHER_block_size(e) ((e)->block_size)
83 #define EVP_CIPHER_key_length(e) ((e)->key_len)
84 #define EVP_CIPHER_iv_length(e) ((e)->iv_len)
85 #define EVP_CIPHER_flags(e) ((e)->flags)
86 #define EVP_CIPHER_mode(e) ((e)->flags) & EVP_CIPH_MODE)
87 int EVP_CIPHER_type(const EVP_CIPHER *ctx);
88
89 #define EVP_CIPHER_CTX_cipher(e) ((e)->cipher)
90 #define EVP_CIPHER_CTX_nid(e) ((e)->cipher->nid)
91 #define EVP_CIPHER_CTX_block_size(e) ((e)->cipher->block_size)
92 #define EVP_CIPHER_CTX_key_length(e) ((e)->key_len)
93 #define EVP_CIPHER_CTX_iv_length(e) ((e)->cipher->iv_len)
94 #define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data)
95 #define EVP_CIPHER_CTX_set_app_data(e,d) ((e)->app_data=(char *)(d))
96 #define EVP_CIPHER_CTX_type(c) EVP_CIPHER_type(EVP_CIPHER_CTX_cipher(c))
97 #define EVP_CIPHER_CTX_flags(e) ((e)->cipher->flags)
98 #define EVP_CIPHER_CTX_mode(e) ((e)->cipher->flags & EVP_CIPH_MODE)
99
100 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
101 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
102
103=head1 DESCRIPTION
104
105The EVP cipher routines are a high level interface to certain
106symmetric ciphers.
107
108EVP_CIPHER_CTX_init() initializes cipher contex B<ctx>.
109
110EVP_EncryptInit_ex() sets up cipher context B<ctx> for encryption
111with cipher B<type> from ENGINE B<impl>. B<ctx> must be initialized
112before calling this function. B<type> is normally supplied
113by a function such as EVP_aes_256_cbc(). If B<impl> is NULL then the
114default implementation is used. B<key> is the symmetric key to use
115and B<iv> is the IV to use (if necessary), the actual number of bytes
116used for the key and IV depends on the cipher. It is possible to set
117all parameters to NULL except B<type> in an initial call and supply
118the remaining parameters in subsequent calls, all of which have B<type>
119set to NULL. This is done when the default cipher parameters are not
120appropriate.
121
122EVP_EncryptUpdate() encrypts B<inl> bytes from the buffer B<in> and
123writes the encrypted version to B<out>. This function can be called
124multiple times to encrypt successive blocks of data. The amount
125of data written depends on the block alignment of the encrypted data:
126as a result the amount of data written may be anything from zero bytes
127to (inl + cipher_block_size - 1) so B<outl> should contain sufficient
128room. The actual number of bytes written is placed in B<outl>.
129
130If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts
131the "final" data, that is any data that remains in a partial block.
132It uses L<standard block padding|/NOTES> (aka PKCS padding). The encrypted
133final data is written to B<out> which should have sufficient space for
134one cipher block. The number of bytes written is placed in B<outl>. After
135this function is called the encryption operation is finished and no further
136calls to EVP_EncryptUpdate() should be made.
137
138If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more
139data and it will return an error if any data remains in a partial block:
140that is if the total data length is not a multiple of the block size.
141
142EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are the
143corresponding decryption operations. EVP_DecryptFinal() will return an
144error code if padding is enabled and the final block is not correctly
145formatted. The parameters and restrictions are identical to the encryption
146operations except that if padding is enabled the decrypted data buffer B<out>
147passed to EVP_DecryptUpdate() should have sufficient room for
148(B<inl> + cipher_block_size) bytes unless the cipher block size is 1 in
149which case B<inl> bytes is sufficient.
150
151EVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex() are
152functions that can be used for decryption or encryption. The operation
153performed depends on the value of the B<enc> parameter. It should be set
154to 1 for encryption, 0 for decryption and -1 to leave the value unchanged
155(the actual value of 'enc' being supplied in a previous call).
156
157EVP_CIPHER_CTX_cleanup() clears all information from a cipher context
158and free up any allocated memory associate with it. It should be called
159after all operations using a cipher are complete so sensitive information
160does not remain in memory.
161
162EVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit() behave in a
163similar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex and
164EVP_CipherInit_ex() except the B<ctx> parameter does not need to be
165initialized and they always use the default cipher implementation.
166
167EVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal() are
168identical to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and
169EVP_CipherFinal_ex(). In previous releases they also used to clean up
170the B<ctx>, but this is no longer done and EVP_CIPHER_CTX_clean()
171must be called to free any context resources.
172
173EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
174return an EVP_CIPHER structure when passed a cipher name, a NID or an
175ASN1_OBJECT structure.
176
177EVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return the NID of a cipher when
178passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX> structure. The actual NID
179value is an internal value which may not have a corresponding OBJECT
180IDENTIFIER.
181
182EVP_CIPHER_CTX_set_padding() enables or disables padding. By default
183encryption operations are padded using standard block padding and the
184padding is checked and removed when decrypting. If the B<pad> parameter
185is zero then no padding is performed, the total amount of data encrypted
186or decrypted must then be a multiple of the block size or an error will
187occur.
188
189EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
190length of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>
191structure. The constant B<EVP_MAX_KEY_LENGTH> is the maximum key length
192for all ciphers. Note: although EVP_CIPHER_key_length() is fixed for a
193given cipher, the value of EVP_CIPHER_CTX_key_length() may be different
194for variable key length ciphers.
195
196EVP_CIPHER_CTX_set_key_length() sets the key length of the cipher ctx.
197If the cipher is a fixed length cipher then attempting to set the key
198length to any value other than the fixed value is an error.
199
200EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
201length of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>.
202It will return zero if the cipher does not use an IV. The constant
203B<EVP_MAX_IV_LENGTH> is the maximum IV length for all ciphers.
204
205EVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the block
206size of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>
207structure. The constant B<EVP_MAX_IV_LENGTH> is also the maximum block
208length for all ciphers.
209
210EVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the type of the passed
211cipher or context. This "type" is the actual NID of the cipher OBJECT
212IDENTIFIER as such it ignores the cipher parameters and 40 bit RC2 and
213128 bit RC2 have the same NID. If the cipher does not have an object
214identifier or does not have ASN1 support this function will return
215B<NID_undef>.
216
217EVP_CIPHER_CTX_cipher() returns the B<EVP_CIPHER> structure when passed
218an B<EVP_CIPHER_CTX> structure.
219
220EVP_CIPHER_mode() and EVP_CIPHER_CTX_mode() return the block cipher mode:
221EVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE or
222EVP_CIPH_OFB_MODE. If the cipher is a stream cipher then
223EVP_CIPH_STREAM_CIPHER is returned.
224
225EVP_CIPHER_param_to_asn1() sets the AlgorithmIdentifier "parameter" based
226on the passed cipher. This will typically include any parameters and an
227IV. The cipher IV (if any) must be set when this call is made. This call
228should be made before the cipher is actually "used" (before any
229EVP_EncryptUpdate(), EVP_DecryptUpdate() calls for example). This function
230may fail if the cipher does not have any ASN1 support.
231
232EVP_CIPHER_asn1_to_param() sets the cipher parameters based on an ASN1
233AlgorithmIdentifier "parameter". The precise effect depends on the cipher
234In the case of RC2, for example, it will set the IV and effective key length.
235This function should be called after the base cipher type is set but before
236the key is set. For example EVP_CipherInit() will be called with the IV and
237key set to NULL, EVP_CIPHER_asn1_to_param() will be called and finally
238EVP_CipherInit() again with all parameters except the key set to NULL. It is
239possible for this function to fail if the cipher does not have any ASN1 support
240or the parameters cannot be set (for example the RC2 effective key length
241is not supported.
242
243EVP_CIPHER_CTX_ctrl() allows various cipher specific parameters to be determined
244and set. Currently only the RC2 effective key length and the number of rounds of
245RC5 can be set.
246
247=head1 RETURN VALUES
248
249EVP_EncryptInit_ex(), EVP_EncryptUpdate() and EVP_EncryptFinal_ex()
250return 1 for success and 0 for failure.
251
252EVP_DecryptInit_ex() and EVP_DecryptUpdate() return 1 for success and 0 for
253failure. EVP_DecryptFinal_ex() returns 0 if the decrypt failed or 1 for
254success.
255
256EVP_CipherInit_ex() and EVP_CipherUpdate() return 1 for success and 0 for
257failure. EVP_CipherFinal_ex() returns 0 for a decryption failure or 1 for
258success.
259
260EVP_CIPHER_CTX_cleanup() returns 1 for success and 0 for failure.
261
262EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
263return an B<EVP_CIPHER> structure or NULL on error.
264
265EVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return a NID.
266
267EVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the block
268size.
269
270EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
271length.
272
273EVP_CIPHER_CTX_set_padding() always returns 1.
274
275EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
276length or zero if the cipher does not use an IV.
277
278EVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the NID of the cipher's
279OBJECT IDENTIFIER or NID_undef if it has no defined OBJECT IDENTIFIER.
280
281EVP_CIPHER_CTX_cipher() returns an B<EVP_CIPHER> structure.
282
283EVP_CIPHER_param_to_asn1() and EVP_CIPHER_asn1_to_param() return 1 for
284success or zero for failure.
285
286=head1 CIPHER LISTING
287
288All algorithms have a fixed key length unless otherwise stated.
289
290=over 4
291
292=item EVP_enc_null(void)
293
294Null cipher: does nothing.
295
296=item EVP_aes_128_cbc(void), EVP_aes_128_ecb(void), EVP_aes_128_cfb(void), EVP_aes_128_ofb(void)
297
298128-bit AES in CBC, ECB, CFB and OFB modes respectively.
299
300=item EVP_aes_192_cbc(void), EVP_aes_192_ecb(void), EVP_aes_192_cfb(void), EVP_aes_192_ofb(void)
301
302192-bit AES in CBC, ECB, CFB and OFB modes respectively.
303
304=item EVP_aes_256_cbc(void), EVP_aes_256_ecb(void), EVP_aes_256_cfb(void), EVP_aes_256_ofb(void)
305
306256-bit AES in CBC, ECB, CFB and OFB modes respectively.
307
308=item EVP_des_cbc(void), EVP_des_ecb(void), EVP_des_cfb(void), EVP_des_ofb(void)
309
310DES in CBC, ECB, CFB and OFB modes respectively.
311
312=item EVP_des_ede_cbc(void), EVP_des_ede(), EVP_des_ede_ofb(void),
313EVP_des_ede_cfb(void)
314
315Two key triple DES in CBC, ECB, CFB and OFB modes respectively.
316
317=item EVP_des_ede3_cbc(void), EVP_des_ede3(), EVP_des_ede3_ofb(void),
318EVP_des_ede3_cfb(void)
319
320Three key triple DES in CBC, ECB, CFB and OFB modes respectively.
321
322=item EVP_desx_cbc(void)
323
324DESX algorithm in CBC mode.
325
326=item EVP_rc4(void)
327
328RC4 stream cipher. This is a variable key length cipher with default key length
329128 bits.
330
331=item EVP_rc4_40(void)
332
333RC4 stream cipher with 40 bit key length. This is obsolete and new code should
334use EVP_rc4() and the EVP_CIPHER_CTX_set_key_length() function.
335
336=item EVP_idea_cbc() EVP_idea_ecb(void), EVP_idea_cfb(void),
337EVP_idea_ofb(void), EVP_idea_cbc(void)
338
339IDEA encryption algorithm in CBC, ECB, CFB and OFB modes respectively.
340
341=item EVP_rc2_cbc(void), EVP_rc2_ecb(void), EVP_rc2_cfb(void), EVP_rc2_ofb(void)
342
343RC2 encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This is a
344variable key length cipher with an additional parameter called "effective key
345bits" or "effective key length". By default both are set to 128 bits.
346
347=item EVP_rc2_40_cbc(void), EVP_rc2_64_cbc(void)
348
349RC2 algorithm in CBC mode with a default key length and effective key length of
35040 and 64 bits. These are obsolete and new code should use EVP_rc2_cbc(),
351EVP_CIPHER_CTX_set_key_length() and EVP_CIPHER_CTX_ctrl() to set the key length
352and effective key length.
353
354=item EVP_bf_cbc(void), EVP_bf_ecb(void), EVP_bf_cfb(void), EVP_bf_ofb(void);
355
356Blowfish encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This
357is a variable key length cipher.
358
359=item EVP_cast5_cbc(void), EVP_cast5_ecb(void), EVP_cast5_cfb(void),
360EVP_cast5_ofb(void)
361
362CAST encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This is
363a variable key length cipher.
364
365=item EVP_rc5_32_12_16_cbc(void), EVP_rc5_32_12_16_ecb(void),
366EVP_rc5_32_12_16_cfb(void), EVP_rc5_32_12_16_ofb(void)
367
368RC5 encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This is a
369variable key length cipher with an additional "number of rounds" parameter. By
370default the key length is set to 128 bits and 12 rounds.
371
372=back
373
374=head1 NOTES
375
376Where possible the B<EVP> interface to symmetric ciphers should be used in
377preference to the low level interfaces. This is because the code then becomes
378transparent to the cipher used and much more flexible.
379
380PKCS padding works by adding B<n> padding bytes of value B<n> to make the total
381length of the encrypted data a multiple of the block size. Padding is always
382added so if the data is already a multiple of the block size B<n> will equal
383the block size. For example if the block size is 8 and 11 bytes are to be
384encrypted then 5 padding bytes of value 5 will be added.
385
386When decrypting the final block is checked to see if it has the correct form.
387
388Although the decryption operation can produce an error if padding is enabled,
389it is not a strong test that the input data or key is correct. A random block
390has better than 1 in 256 chance of being of the correct format and problems with
391the input data earlier on will not produce a final decrypt error.
392
393If padding is disabled then the decryption operation will always succeed if
394the total amount of data decrypted is a multiple of the block size.
395
396The functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(),
397EVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained for
398compatibility with existing code. New code should use EVP_EncryptInit_ex(),
399EVP_EncryptFinal_ex(), EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(),
400EVP_CipherInit_ex() and EVP_CipherFinal_ex() because they can reuse an
401existing context without allocating and freeing it up on each call.
402
403=head1 BUGS
404
405For RC5 the number of rounds can currently only be set to 8, 12 or 16. This is
406a limitation of the current RC5 code rather than the EVP interface.
407
408EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with
409default key lengths. If custom ciphers exceed these values the results are
410unpredictable. This is because it has become standard practice to define a
411generic key as a fixed unsigned char array containing EVP_MAX_KEY_LENGTH bytes.
412
413The ASN1 code is incomplete (and sometimes inaccurate) it has only been tested
414for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode.
415
416=head1 EXAMPLES
417
418Get the number of rounds used in RC5:
419
420 int nrounds;
421 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC5_ROUNDS, 0, &nrounds);
422
423Get the RC2 effective key length:
424
425 int key_bits;
426 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC2_KEY_BITS, 0, &key_bits);
427
428Set the number of rounds used in RC5:
429
430 int nrounds;
431 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC5_ROUNDS, nrounds, NULL);
432
433Set the effective key length used in RC2:
434
435 int key_bits;
436 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
437
438Encrypt a string using blowfish:
439
440 int
441 do_crypt(char *outfile)
442 {
443 unsigned char outbuf[1024];
444 int outlen, tmplen;
445 /*
446 * Bogus key and IV: we'd normally set these from
447 * another source.
448 */
449 unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
450 unsigned char iv[] = {1,2,3,4,5,6,7,8};
451 const char intext[] = "Some Crypto Text";
452 EVP_CIPHER_CTX ctx;
453 FILE *out;
454 EVP_CIPHER_CTX_init(&ctx);
455 EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv);
456
457 if (!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext,
458 strlen(intext))) {
459 /* Error */
460 return 0;
461 }
462 /*
463 * Buffer passed to EVP_EncryptFinal() must be after data just
464 * encrypted to avoid overwriting it.
465 */
466 if (!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) {
467 /* Error */
468 return 0;
469 }
470 outlen += tmplen;
471 EVP_CIPHER_CTX_cleanup(&ctx);
472 /*
473 * Need binary mode for fopen because encrypted data is
474 * binary data. Also cannot use strlen() on it because
475 * it won't be NUL terminated and may contain embedded
476 * NULs.
477 */
478 out = fopen(outfile, "wb");
479 fwrite(outbuf, 1, outlen, out);
480 fclose(out);
481 return 1;
482 }
483
484The ciphertext from the above example can be decrypted using the B<openssl>
485utility with the command line:
486
487 S<openssl bf -in cipher.bin -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 -d>
488
489General encryption, decryption function example using FILE I/O and RC2 with an
49080 bit key:
491
492 int
493 do_crypt(FILE *in, FILE *out, int do_encrypt)
494 {
495 /* Allow enough space in output buffer for additional block */
496 inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
497 int inlen, outlen;
498 /*
499 * Bogus key and IV: we'd normally set these from
500 * another source.
501 */
502 unsigned char key[] = "0123456789";
503 unsigned char iv[] = "12345678";
504
505 /* Don't set key or IV because we will modify the parameters */
506 EVP_CIPHER_CTX_init(&ctx);
507 EVP_CipherInit_ex(&ctx, EVP_rc2(), NULL, NULL, NULL, do_encrypt);
508 EVP_CIPHER_CTX_set_key_length(&ctx, 10);
509 /* We finished modifying parameters so now we can set key and IV */
510 EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
511
512 for(;;) {
513 inlen = fread(inbuf, 1, 1024, in);
514 if (inlen <= 0)
515 break;
516 if (!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf,
517 inlen)) {
518 /* Error */
519 EVP_CIPHER_CTX_cleanup(&ctx);
520 return 0;
521 }
522 fwrite(outbuf, 1, outlen, out);
523 }
524 if (!EVP_CipherFinal_ex(&ctx, outbuf, &outlen)) {
525 /* Error */
526 EVP_CIPHER_CTX_cleanup(&ctx);
527 return 0;
528 }
529 fwrite(outbuf, 1, outlen, out);
530
531 EVP_CIPHER_CTX_cleanup(&ctx);
532 return 1;
533 }
534
535=head1 SEE ALSO
536
537L<evp(3)|evp(3)>
538
539=head1 HISTORY
540
541EVP_CIPHER_CTX_init(), EVP_EncryptInit_ex(), EVP_EncryptFinal_ex(),
542EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(), EVP_CipherInit_ex(),
543EVP_CipherFinal_ex() and EVP_CIPHER_CTX_set_padding() appeared in
544OpenSSL 0.9.7.
545
546=cut