diff options
author | beck <> | 2002-05-15 02:29:21 +0000 |
---|---|---|
committer | beck <> | 2002-05-15 02:29:21 +0000 |
commit | b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9 (patch) | |
tree | fa27cf82a1250b64ed3bf5f4a18c7354d470bbcc /src/lib/libcrypto/doc/EVP_EncryptInit.pod | |
parent | e471e1ea98d673597b182ea85f29e30c97cd08b5 (diff) | |
download | openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.gz openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.bz2 openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.zip |
OpenSSL 0.9.7 stable 2002 05 08 merge
Diffstat (limited to 'src/lib/libcrypto/doc/EVP_EncryptInit.pod')
-rw-r--r-- | src/lib/libcrypto/doc/EVP_EncryptInit.pod | 289 |
1 files changed, 217 insertions, 72 deletions
diff --git a/src/lib/libcrypto/doc/EVP_EncryptInit.pod b/src/lib/libcrypto/doc/EVP_EncryptInit.pod index 9afe2396e2..371b6a2287 100644 --- a/src/lib/libcrypto/doc/EVP_EncryptInit.pod +++ b/src/lib/libcrypto/doc/EVP_EncryptInit.pod | |||
@@ -2,43 +2,65 @@ | |||
2 | 2 | ||
3 | =head1 NAME | 3 | =head1 NAME |
4 | 4 | ||
5 | EVP_EncryptInit, EVP_EncryptUpdate, EVP_EncryptFinal, EVP_DecryptInit, | 5 | EVP_CIPHER_CTX_init, EVP_EncryptInit_ex, EVP_EncryptUpdate, |
6 | EVP_DecryptUpdate, EVP_DecryptFinal, EVP_CipherInit, EVP_CipherUpdate, | 6 | EVP_EncryptFinal_ex, EVP_DecryptInit_ex, EVP_DecryptUpdate, |
7 | EVP_CipherFinal, EVP_CIPHER_CTX_set_key_length, EVP_CIPHER_CTX_ctrl, | 7 | EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherUpdate, |
8 | EVP_CIPHER_CTX_cleanup, EVP_get_cipherbyname, EVP_get_cipherbynid, | 8 | EVP_CipherFinal_ex, EVP_CIPHER_CTX_set_key_length, |
9 | EVP_get_cipherbyobj, EVP_CIPHER_nid, EVP_CIPHER_block_size, | 9 | EVP_CIPHER_CTX_ctrl, EVP_CIPHER_CTX_cleanup, EVP_EncryptInit, |
10 | EVP_CIPHER_key_length, EVP_CIPHER_iv_length, EVP_CIPHER_flags, | 10 | EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal, |
11 | EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher, EVP_CIPHER_CTX_nid, | 11 | EVP_CipherInit, EVP_CipherFinal, EVP_get_cipherbyname, |
12 | EVP_CIPHER_CTX_block_size, EVP_CIPHER_CTX_key_length, EVP_CIPHER_CTX_iv_length, | 12 | EVP_get_cipherbynid, EVP_get_cipherbyobj, EVP_CIPHER_nid, |
13 | EVP_CIPHER_CTX_get_app_data, EVP_CIPHER_CTX_set_app_data, EVP_CIPHER_CTX_type, | 13 | EVP_CIPHER_block_size, EVP_CIPHER_key_length, EVP_CIPHER_iv_length, |
14 | EVP_CIPHER_CTX_flags, EVP_CIPHER_CTX_mode, EVP_CIPHER_param_to_asn1, | 14 | EVP_CIPHER_flags, EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher, |
15 | EVP_CIPHER_asn1_to_param - EVP cipher routines | 15 | EVP_CIPHER_CTX_nid, EVP_CIPHER_CTX_block_size, EVP_CIPHER_CTX_key_length, |
16 | EVP_CIPHER_CTX_iv_length, EVP_CIPHER_CTX_get_app_data, | ||
17 | EVP_CIPHER_CTX_set_app_data, EVP_CIPHER_CTX_type, EVP_CIPHER_CTX_flags, | ||
18 | EVP_CIPHER_CTX_mode, EVP_CIPHER_param_to_asn1, EVP_CIPHER_asn1_to_param, | ||
19 | EVP_CIPHER_CTX_set_padding - EVP cipher routines | ||
16 | 20 | ||
17 | =head1 SYNOPSIS | 21 | =head1 SYNOPSIS |
18 | 22 | ||
19 | #include <openssl/evp.h> | 23 | #include <openssl/evp.h> |
20 | 24 | ||
21 | int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, | 25 | int EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a); |
22 | unsigned char *key, unsigned char *iv); | 26 | |
27 | int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, | ||
28 | ENGINE *impl, unsigned char *key, unsigned char *iv); | ||
23 | int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, | 29 | int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, |
24 | int *outl, unsigned char *in, int inl); | 30 | int *outl, unsigned char *in, int inl); |
31 | int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
32 | int *outl); | ||
33 | |||
34 | int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, | ||
35 | ENGINE *impl, unsigned char *key, unsigned char *iv); | ||
36 | int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
37 | int *outl, unsigned char *in, int inl); | ||
38 | int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, | ||
39 | int *outl); | ||
40 | |||
41 | int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, | ||
42 | ENGINE *impl, unsigned char *key, unsigned char *iv, int enc); | ||
43 | int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
44 | int *outl, unsigned char *in, int inl); | ||
45 | int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, | ||
46 | int *outl); | ||
47 | |||
48 | int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, | ||
49 | unsigned char *key, unsigned char *iv); | ||
25 | int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, | 50 | int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, |
26 | int *outl); | 51 | int *outl); |
27 | 52 | ||
28 | int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, | 53 | int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, |
29 | unsigned char *key, unsigned char *iv); | 54 | unsigned char *key, unsigned char *iv); |
30 | int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
31 | int *outl, unsigned char *in, int inl); | ||
32 | int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, | 55 | int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, |
33 | int *outl); | 56 | int *outl); |
34 | 57 | ||
35 | int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, | 58 | int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, |
36 | unsigned char *key, unsigned char *iv, int enc); | 59 | unsigned char *key, unsigned char *iv, int enc); |
37 | int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
38 | int *outl, unsigned char *in, int inl); | ||
39 | int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, | 60 | int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, |
40 | int *outl); | 61 | int *outl); |
41 | 62 | ||
63 | int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding); | ||
42 | int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen); | 64 | int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen); |
43 | int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr); | 65 | int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr); |
44 | int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a); | 66 | int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a); |
@@ -74,14 +96,19 @@ EVP_CIPHER_asn1_to_param - EVP cipher routines | |||
74 | The EVP cipher routines are a high level interface to certain | 96 | The EVP cipher routines are a high level interface to certain |
75 | symmetric ciphers. | 97 | symmetric ciphers. |
76 | 98 | ||
77 | EVP_EncryptInit() initializes a cipher context B<ctx> for encryption | 99 | EVP_CIPHER_CTX_init() initializes cipher contex B<ctx>. |
78 | with cipher B<type>. B<type> is normally supplied by a function such | 100 | |
79 | as EVP_des_cbc() . B<key> is the symmetric key to use and B<iv> is the | 101 | EVP_EncryptInit_ex() sets up cipher context B<ctx> for encryption |
80 | IV to use (if necessary), the actual number of bytes used for the | 102 | with cipher B<type> from ENGINE B<impl>. B<ctx> must be initialized |
81 | key and IV depends on the cipher. It is possible to set all parameters | 103 | before calling this function. B<type> is normally supplied |
82 | to NULL except B<type> in an initial call and supply the remaining | 104 | by a function such as EVP_des_cbc(). If B<impl> is NULL then the |
83 | parameters in subsequent calls, all of which have B<type> set to NULL. | 105 | default implementation is used. B<key> is the symmetric key to use |
84 | This is done when the default cipher parameters are not appropriate. | 106 | and B<iv> is the IV to use (if necessary), the actual number of bytes |
107 | used for the key and IV depends on the cipher. It is possible to set | ||
108 | all parameters to NULL except B<type> in an initial call and supply | ||
109 | the remaining parameters in subsequent calls, all of which have B<type> | ||
110 | set to NULL. This is done when the default cipher parameters are not | ||
111 | appropriate. | ||
85 | 112 | ||
86 | EVP_EncryptUpdate() encrypts B<inl> bytes from the buffer B<in> and | 113 | EVP_EncryptUpdate() encrypts B<inl> bytes from the buffer B<in> and |
87 | writes the encrypted version to B<out>. This function can be called | 114 | writes the encrypted version to B<out>. This function can be called |
@@ -89,32 +116,49 @@ multiple times to encrypt successive blocks of data. The amount | |||
89 | of data written depends on the block alignment of the encrypted data: | 116 | of data written depends on the block alignment of the encrypted data: |
90 | as a result the amount of data written may be anything from zero bytes | 117 | as a result the amount of data written may be anything from zero bytes |
91 | to (inl + cipher_block_size - 1) so B<outl> should contain sufficient | 118 | to (inl + cipher_block_size - 1) so B<outl> should contain sufficient |
92 | room. The actual number of bytes written is placed in B<outl>. | 119 | room. The actual number of bytes written is placed in B<outl>. |
120 | |||
121 | If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts | ||
122 | the "final" data, that is any data that remains in a partial block. | ||
123 | It uses L<standard block padding|/NOTES> (aka PKCS padding). The encrypted | ||
124 | final data is written to B<out> which should have sufficient space for | ||
125 | one cipher block. The number of bytes written is placed in B<outl>. After | ||
126 | this function is called the encryption operation is finished and no further | ||
127 | calls to EVP_EncryptUpdate() should be made. | ||
93 | 128 | ||
94 | EVP_EncryptFinal() encrypts the "final" data, that is any data that | 129 | If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more |
95 | remains in a partial block. It uses L<standard block padding|/NOTES> (aka PKCS | 130 | data and it will return an error if any data remains in a partial block: |
96 | padding). The encrypted final data is written to B<out> which should | 131 | that is if the total data length is not a multiple of the block size. |
97 | have sufficient space for one cipher block. The number of bytes written | ||
98 | is placed in B<outl>. After this function is called the encryption operation | ||
99 | is finished and no further calls to EVP_EncryptUpdate() should be made. | ||
100 | 132 | ||
101 | EVP_DecryptInit(), EVP_DecryptUpdate() and EVP_DecryptFinal() are the | 133 | EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are the |
102 | corresponding decryption operations. EVP_DecryptFinal() will return an | 134 | corresponding decryption operations. EVP_DecryptFinal() will return an |
103 | error code if the final block is not correctly formatted. The parameters | 135 | error code if padding is enabled and the final block is not correctly |
104 | and restrictions are identical to the encryption operations except that | 136 | formatted. The parameters and restrictions are identical to the encryption |
105 | the decrypted data buffer B<out> passed to EVP_DecryptUpdate() should | 137 | operations except that if padding is enabled the decrypted data buffer B<out> |
106 | have sufficient room for (B<inl> + cipher_block_size) bytes unless the | 138 | passed to EVP_DecryptUpdate() should have sufficient room for |
107 | cipher block size is 1 in which case B<inl> bytes is sufficient. | 139 | (B<inl> + cipher_block_size) bytes unless the cipher block size is 1 in |
108 | 140 | which case B<inl> bytes is sufficient. | |
109 | EVP_CipherInit(), EVP_CipherUpdate() and EVP_CipherFinal() are functions | 141 | |
110 | that can be used for decryption or encryption. The operation performed | 142 | EVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex() are |
111 | depends on the value of the B<enc> parameter. It should be set to 1 for | 143 | functions that can be used for decryption or encryption. The operation |
112 | encryption, 0 for decryption and -1 to leave the value unchanged (the | 144 | performed depends on the value of the B<enc> parameter. It should be set |
113 | actual value of 'enc' being supplied in a previous call). | 145 | to 1 for encryption, 0 for decryption and -1 to leave the value unchanged |
114 | 146 | (the actual value of 'enc' being supplied in a previous call). | |
115 | EVP_CIPHER_CTX_cleanup() clears all information from a cipher context. | 147 | |
116 | It should be called after all operations using a cipher are complete | 148 | EVP_CIPHER_CTX_cleanup() clears all information from a cipher context |
117 | so sensitive information does not remain in memory. | 149 | and free up any allocated memory associate with it. It should be called |
150 | after all operations using a cipher are complete so sensitive information | ||
151 | does not remain in memory. | ||
152 | |||
153 | EVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit() behave in a | ||
154 | similar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex and | ||
155 | EVP_CipherInit_ex() except the B<ctx> paramter does not need to be | ||
156 | initialized and they always use the default cipher implementation. | ||
157 | |||
158 | EVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal() behave in a | ||
159 | similar way to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and | ||
160 | EVP_CipherFinal_ex() except B<ctx> is automatically cleaned up | ||
161 | after the call. | ||
118 | 162 | ||
119 | EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj() | 163 | EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj() |
120 | return an EVP_CIPHER structure when passed a cipher name, a NID or an | 164 | return an EVP_CIPHER structure when passed a cipher name, a NID or an |
@@ -125,6 +169,13 @@ passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX> structure. The actual NID | |||
125 | value is an internal value which may not have a corresponding OBJECT | 169 | value is an internal value which may not have a corresponding OBJECT |
126 | IDENTIFIER. | 170 | IDENTIFIER. |
127 | 171 | ||
172 | EVP_CIPHER_CTX_set_padding() enables or disables padding. By default | ||
173 | encryption operations are padded using standard block padding and the | ||
174 | padding is checked and removed when decrypting. If the B<pad> parameter | ||
175 | is zero then no padding is performed, the total amount of data encrypted | ||
176 | or decrypted must then be a multiple of the block size or an error will | ||
177 | occur. | ||
178 | |||
128 | EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key | 179 | EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key |
129 | length of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX> | 180 | length of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX> |
130 | structure. The constant B<EVP_MAX_KEY_LENGTH> is the maximum key length | 181 | structure. The constant B<EVP_MAX_KEY_LENGTH> is the maximum key length |
@@ -185,14 +236,14 @@ RC5 can be set. | |||
185 | 236 | ||
186 | =head1 RETURN VALUES | 237 | =head1 RETURN VALUES |
187 | 238 | ||
188 | EVP_EncryptInit(), EVP_EncryptUpdate() and EVP_EncryptFinal() return 1 for success | 239 | EVP_CIPHER_CTX_init, EVP_EncryptInit_ex(), EVP_EncryptUpdate() and |
189 | and 0 for failure. | 240 | EVP_EncryptFinal_ex() return 1 for success and 0 for failure. |
190 | 241 | ||
191 | EVP_DecryptInit() and EVP_DecryptUpdate() return 1 for success and 0 for failure. | 242 | EVP_DecryptInit_ex() and EVP_DecryptUpdate() return 1 for success and 0 for failure. |
192 | EVP_DecryptFinal() returns 0 if the decrypt failed or 1 for success. | 243 | EVP_DecryptFinal_ex() returns 0 if the decrypt failed or 1 for success. |
193 | 244 | ||
194 | EVP_CipherInit() and EVP_CipherUpdate() return 1 for success and 0 for failure. | 245 | EVP_CipherInit_ex() and EVP_CipherUpdate() return 1 for success and 0 for failure. |
195 | EVP_CipherFinal() returns 1 for a decryption failure or 1 for success. | 246 | EVP_CipherFinal_ex() returns 0 for a decryption failure or 1 for success. |
196 | 247 | ||
197 | EVP_CIPHER_CTX_cleanup() returns 1 for success and 0 for failure. | 248 | EVP_CIPHER_CTX_cleanup() returns 1 for success and 0 for failure. |
198 | 249 | ||
@@ -207,6 +258,8 @@ size. | |||
207 | EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key | 258 | EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key |
208 | length. | 259 | length. |
209 | 260 | ||
261 | EVP_CIPHER_CTX_set_padding() always returns 1. | ||
262 | |||
210 | EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV | 263 | EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV |
211 | length or zero if the cipher does not use an IV. | 264 | length or zero if the cipher does not use an IV. |
212 | 265 | ||
@@ -301,25 +354,26 @@ encrypted then 5 padding bytes of value 5 will be added. | |||
301 | 354 | ||
302 | When decrypting the final block is checked to see if it has the correct form. | 355 | When decrypting the final block is checked to see if it has the correct form. |
303 | 356 | ||
304 | Although the decryption operation can produce an error, it is not a strong | 357 | Although the decryption operation can produce an error if padding is enabled, |
305 | test that the input data or key is correct. A random block has better than | 358 | it is not a strong test that the input data or key is correct. A random block |
306 | 1 in 256 chance of being of the correct format and problems with the | 359 | has better than 1 in 256 chance of being of the correct format and problems with |
307 | input data earlier on will not produce a final decrypt error. | 360 | the input data earlier on will not produce a final decrypt error. |
308 | 361 | ||
309 | The functions EVP_EncryptInit(), EVP_EncryptUpdate(), EVP_EncryptFinal(), | 362 | If padding is disabled then the decryption operation will always succeed if |
310 | EVP_DecryptInit(), EVP_DecryptUpdate(), EVP_CipherInit() and EVP_CipherUpdate() | 363 | the total amount of data decrypted is a multiple of the block size. |
311 | and EVP_CIPHER_CTX_cleanup() did not return errors in OpenSSL version 0.9.5a or | 364 | |
312 | earlier. Software only versions of encryption algorithms will never return | 365 | The functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(), |
313 | error codes for these functions, unless there is a programming error (for example | 366 | EVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained for |
314 | and attempt to set the key before the cipher is set in EVP_EncryptInit() ). | 367 | compatibility with existing code. New code should use EVP_EncryptInit_ex(), |
368 | EVP_EncryptFinal_ex(), EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(), | ||
369 | EVP_CipherInit_ex() and EVP_CipherFinal_ex() because they can reuse an | ||
370 | existing context without allocating and freeing it up on each call. | ||
315 | 371 | ||
316 | =head1 BUGS | 372 | =head1 BUGS |
317 | 373 | ||
318 | For RC5 the number of rounds can currently only be set to 8, 12 or 16. This is | 374 | For RC5 the number of rounds can currently only be set to 8, 12 or 16. This is |
319 | a limitation of the current RC5 code rather than the EVP interface. | 375 | a limitation of the current RC5 code rather than the EVP interface. |
320 | 376 | ||
321 | It should be possible to disable PKCS padding: currently it isn't. | ||
322 | |||
323 | EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with | 377 | EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with |
324 | default key lengths. If custom ciphers exceed these values the results are | 378 | default key lengths. If custom ciphers exceed these values the results are |
325 | unpredictable. This is because it has become standard practice to define a | 379 | unpredictable. This is because it has become standard practice to define a |
@@ -333,22 +387,113 @@ for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode. | |||
333 | Get the number of rounds used in RC5: | 387 | Get the number of rounds used in RC5: |
334 | 388 | ||
335 | int nrounds; | 389 | int nrounds; |
336 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC5_ROUNDS, 0, &i); | 390 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC5_ROUNDS, 0, &nrounds); |
337 | 391 | ||
338 | Get the RC2 effective key length: | 392 | Get the RC2 effective key length: |
339 | 393 | ||
340 | int key_bits; | 394 | int key_bits; |
341 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i); | 395 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC2_KEY_BITS, 0, &key_bits); |
342 | 396 | ||
343 | Set the number of rounds used in RC5: | 397 | Set the number of rounds used in RC5: |
344 | 398 | ||
345 | int nrounds; | 399 | int nrounds; |
346 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC5_ROUNDS, i, NULL); | 400 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC5_ROUNDS, nrounds, NULL); |
347 | 401 | ||
348 | Set the number of rounds used in RC2: | 402 | Set the effective key length used in RC2: |
403 | |||
404 | int key_bits; | ||
405 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL); | ||
406 | |||
407 | Encrypt a string using blowfish: | ||
408 | |||
409 | int do_crypt(char *outfile) | ||
410 | { | ||
411 | unsigned char outbuf[1024]; | ||
412 | int outlen, tmplen; | ||
413 | /* Bogus key and IV: we'd normally set these from | ||
414 | * another source. | ||
415 | */ | ||
416 | unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; | ||
417 | unsigned char iv[] = {1,2,3,4,5,6,7,8}; | ||
418 | char intext[] = "Some Crypto Text"; | ||
419 | EVP_CIPHER_CTX ctx; | ||
420 | FILE *out; | ||
421 | EVP_CIPHER_CTX_init(&ctx); | ||
422 | EVP_EncryptInit_ex(&ctx, NULL, EVP_bf_cbc(), key, iv); | ||
423 | |||
424 | if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext))) | ||
425 | { | ||
426 | /* Error */ | ||
427 | return 0; | ||
428 | } | ||
429 | /* Buffer passed to EVP_EncryptFinal() must be after data just | ||
430 | * encrypted to avoid overwriting it. | ||
431 | */ | ||
432 | if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) | ||
433 | { | ||
434 | /* Error */ | ||
435 | return 0; | ||
436 | } | ||
437 | outlen += tmplen; | ||
438 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
439 | /* Need binary mode for fopen because encrypted data is | ||
440 | * binary data. Also cannot use strlen() on it because | ||
441 | * it wont be null terminated and may contain embedded | ||
442 | * nulls. | ||
443 | */ | ||
444 | out = fopen(outfile, "wb"); | ||
445 | fwrite(outbuf, 1, outlen, out); | ||
446 | fclose(out); | ||
447 | return 1; | ||
448 | } | ||
449 | |||
450 | The ciphertext from the above example can be decrypted using the B<openssl> | ||
451 | utility with the command line: | ||
452 | |||
453 | S<openssl bf -in cipher.bin -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 -d> | ||
454 | |||
455 | General encryption, decryption function example using FILE I/O and RC2 with an | ||
456 | 80 bit key: | ||
457 | |||
458 | int do_crypt(FILE *in, FILE *out, int do_encrypt) | ||
459 | { | ||
460 | /* Allow enough space in output buffer for additional block */ | ||
461 | inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; | ||
462 | int inlen, outlen; | ||
463 | /* Bogus key and IV: we'd normally set these from | ||
464 | * another source. | ||
465 | */ | ||
466 | unsigned char key[] = "0123456789"; | ||
467 | unsigned char iv[] = "12345678"; | ||
468 | /* Don't set key or IV because we will modify the parameters */ | ||
469 | EVP_CIPHER_CTX_init(&ctx); | ||
470 | EVP_CipherInit_ex(&ctx, EVP_rc2(), NULL, NULL, NULL, do_encrypt); | ||
471 | EVP_CIPHER_CTX_set_key_length(&ctx, 10); | ||
472 | /* We finished modifying parameters so now we can set key and IV */ | ||
473 | EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt); | ||
474 | |||
475 | for(;;) | ||
476 | { | ||
477 | inlen = fread(inbuf, 1, 1024, in); | ||
478 | if(inlen <= 0) break; | ||
479 | if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen)) | ||
480 | { | ||
481 | /* Error */ | ||
482 | return 0; | ||
483 | } | ||
484 | fwrite(outbuf, 1, outlen, out); | ||
485 | } | ||
486 | if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen)) | ||
487 | { | ||
488 | /* Error */ | ||
489 | return 0; | ||
490 | } | ||
491 | fwrite(outbuf, 1, outlen, out); | ||
492 | |||
493 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
494 | return 1; | ||
495 | } | ||
349 | 496 | ||
350 | int nrounds; | ||
351 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, i, NULL); | ||
352 | 497 | ||
353 | =head1 SEE ALSO | 498 | =head1 SEE ALSO |
354 | 499 | ||