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