summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/doc/EVP_EncryptInit.pod
diff options
context:
space:
mode:
authorbeck <>2002-05-15 02:29:21 +0000
committerbeck <>2002-05-15 02:29:21 +0000
commitb64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9 (patch)
treefa27cf82a1250b64ed3bf5f4a18c7354d470bbcc /src/lib/libcrypto/doc/EVP_EncryptInit.pod
parente471e1ea98d673597b182ea85f29e30c97cd08b5 (diff)
downloadopenbsd-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.pod289
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
5EVP_EncryptInit, EVP_EncryptUpdate, EVP_EncryptFinal, EVP_DecryptInit, 5EVP_CIPHER_CTX_init, EVP_EncryptInit_ex, EVP_EncryptUpdate,
6EVP_DecryptUpdate, EVP_DecryptFinal, EVP_CipherInit, EVP_CipherUpdate, 6EVP_EncryptFinal_ex, EVP_DecryptInit_ex, EVP_DecryptUpdate,
7EVP_CipherFinal, EVP_CIPHER_CTX_set_key_length, EVP_CIPHER_CTX_ctrl, 7EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherUpdate,
8EVP_CIPHER_CTX_cleanup, EVP_get_cipherbyname, EVP_get_cipherbynid, 8EVP_CipherFinal_ex, EVP_CIPHER_CTX_set_key_length,
9EVP_get_cipherbyobj, EVP_CIPHER_nid, EVP_CIPHER_block_size, 9EVP_CIPHER_CTX_ctrl, EVP_CIPHER_CTX_cleanup, EVP_EncryptInit,
10EVP_CIPHER_key_length, EVP_CIPHER_iv_length, EVP_CIPHER_flags, 10EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal,
11EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher, EVP_CIPHER_CTX_nid, 11EVP_CipherInit, EVP_CipherFinal, EVP_get_cipherbyname,
12EVP_CIPHER_CTX_block_size, EVP_CIPHER_CTX_key_length, EVP_CIPHER_CTX_iv_length, 12EVP_get_cipherbynid, EVP_get_cipherbyobj, EVP_CIPHER_nid,
13EVP_CIPHER_CTX_get_app_data, EVP_CIPHER_CTX_set_app_data, EVP_CIPHER_CTX_type, 13EVP_CIPHER_block_size, EVP_CIPHER_key_length, EVP_CIPHER_iv_length,
14EVP_CIPHER_CTX_flags, EVP_CIPHER_CTX_mode, EVP_CIPHER_param_to_asn1, 14EVP_CIPHER_flags, EVP_CIPHER_mode, EVP_CIPHER_type, EVP_CIPHER_CTX_cipher,
15EVP_CIPHER_asn1_to_param - EVP cipher routines 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 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
74The EVP cipher routines are a high level interface to certain 96The EVP cipher routines are a high level interface to certain
75symmetric ciphers. 97symmetric ciphers.
76 98
77EVP_EncryptInit() initializes a cipher context B<ctx> for encryption 99EVP_CIPHER_CTX_init() initializes cipher contex B<ctx>.
78with cipher B<type>. B<type> is normally supplied by a function such 100
79as EVP_des_cbc() . B<key> is the symmetric key to use and B<iv> is the 101EVP_EncryptInit_ex() sets up cipher context B<ctx> for encryption
80IV to use (if necessary), the actual number of bytes used for the 102with cipher B<type> from ENGINE B<impl>. B<ctx> must be initialized
81key and IV depends on the cipher. It is possible to set all parameters 103before calling this function. B<type> is normally supplied
82to NULL except B<type> in an initial call and supply the remaining 104by a function such as EVP_des_cbc(). If B<impl> is NULL then the
83parameters in subsequent calls, all of which have B<type> set to NULL. 105default implementation is used. B<key> is the symmetric key to use
84This is done when the default cipher parameters are not appropriate. 106and B<iv> is the IV to use (if necessary), the actual number of bytes
107used for the key and IV depends on the cipher. It is possible to set
108all parameters to NULL except B<type> in an initial call and supply
109the remaining parameters in subsequent calls, all of which have B<type>
110set to NULL. This is done when the default cipher parameters are not
111appropriate.
85 112
86EVP_EncryptUpdate() encrypts B<inl> bytes from the buffer B<in> and 113EVP_EncryptUpdate() encrypts B<inl> bytes from the buffer B<in> and
87writes the encrypted version to B<out>. This function can be called 114writes 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
89of data written depends on the block alignment of the encrypted data: 116of data written depends on the block alignment of the encrypted data:
90as a result the amount of data written may be anything from zero bytes 117as a result the amount of data written may be anything from zero bytes
91to (inl + cipher_block_size - 1) so B<outl> should contain sufficient 118to (inl + cipher_block_size - 1) so B<outl> should contain sufficient
92room. The actual number of bytes written is placed in B<outl>. 119room. The actual number of bytes written is placed in B<outl>.
120
121If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts
122the "final" data, that is any data that remains in a partial block.
123It uses L<standard block padding|/NOTES> (aka PKCS padding). The encrypted
124final data is written to B<out> which should have sufficient space for
125one cipher block. The number of bytes written is placed in B<outl>. After
126this function is called the encryption operation is finished and no further
127calls to EVP_EncryptUpdate() should be made.
93 128
94EVP_EncryptFinal() encrypts the "final" data, that is any data that 129If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more
95remains in a partial block. It uses L<standard block padding|/NOTES> (aka PKCS 130data and it will return an error if any data remains in a partial block:
96padding). The encrypted final data is written to B<out> which should 131that is if the total data length is not a multiple of the block size.
97have sufficient space for one cipher block. The number of bytes written
98is placed in B<outl>. After this function is called the encryption operation
99is finished and no further calls to EVP_EncryptUpdate() should be made.
100 132
101EVP_DecryptInit(), EVP_DecryptUpdate() and EVP_DecryptFinal() are the 133EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are the
102corresponding decryption operations. EVP_DecryptFinal() will return an 134corresponding decryption operations. EVP_DecryptFinal() will return an
103error code if the final block is not correctly formatted. The parameters 135error code if padding is enabled and the final block is not correctly
104and restrictions are identical to the encryption operations except that 136formatted. The parameters and restrictions are identical to the encryption
105the decrypted data buffer B<out> passed to EVP_DecryptUpdate() should 137operations except that if padding is enabled the decrypted data buffer B<out>
106have sufficient room for (B<inl> + cipher_block_size) bytes unless the 138passed to EVP_DecryptUpdate() should have sufficient room for
107cipher 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 140which case B<inl> bytes is sufficient.
109EVP_CipherInit(), EVP_CipherUpdate() and EVP_CipherFinal() are functions 141
110that can be used for decryption or encryption. The operation performed 142EVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex() are
111depends on the value of the B<enc> parameter. It should be set to 1 for 143functions that can be used for decryption or encryption. The operation
112encryption, 0 for decryption and -1 to leave the value unchanged (the 144performed depends on the value of the B<enc> parameter. It should be set
113actual value of 'enc' being supplied in a previous call). 145to 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).
115EVP_CIPHER_CTX_cleanup() clears all information from a cipher context. 147
116It should be called after all operations using a cipher are complete 148EVP_CIPHER_CTX_cleanup() clears all information from a cipher context
117so sensitive information does not remain in memory. 149and free up any allocated memory associate with it. It should be called
150after all operations using a cipher are complete so sensitive information
151does not remain in memory.
152
153EVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit() behave in a
154similar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex and
155EVP_CipherInit_ex() except the B<ctx> paramter does not need to be
156initialized and they always use the default cipher implementation.
157
158EVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal() behave in a
159similar way to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and
160EVP_CipherFinal_ex() except B<ctx> is automatically cleaned up
161after the call.
118 162
119EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj() 163EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
120return an EVP_CIPHER structure when passed a cipher name, a NID or an 164return 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
125value is an internal value which may not have a corresponding OBJECT 169value is an internal value which may not have a corresponding OBJECT
126IDENTIFIER. 170IDENTIFIER.
127 171
172EVP_CIPHER_CTX_set_padding() enables or disables padding. By default
173encryption operations are padded using standard block padding and the
174padding is checked and removed when decrypting. If the B<pad> parameter
175is zero then no padding is performed, the total amount of data encrypted
176or decrypted must then be a multiple of the block size or an error will
177occur.
178
128EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key 179EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
129length of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX> 180length of a cipher when passed an B<EVP_CIPHER> or B<EVP_CIPHER_CTX>
130structure. The constant B<EVP_MAX_KEY_LENGTH> is the maximum key length 181structure. 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
188EVP_EncryptInit(), EVP_EncryptUpdate() and EVP_EncryptFinal() return 1 for success 239EVP_CIPHER_CTX_init, EVP_EncryptInit_ex(), EVP_EncryptUpdate() and
189and 0 for failure. 240EVP_EncryptFinal_ex() return 1 for success and 0 for failure.
190 241
191EVP_DecryptInit() and EVP_DecryptUpdate() return 1 for success and 0 for failure. 242EVP_DecryptInit_ex() and EVP_DecryptUpdate() return 1 for success and 0 for failure.
192EVP_DecryptFinal() returns 0 if the decrypt failed or 1 for success. 243EVP_DecryptFinal_ex() returns 0 if the decrypt failed or 1 for success.
193 244
194EVP_CipherInit() and EVP_CipherUpdate() return 1 for success and 0 for failure. 245EVP_CipherInit_ex() and EVP_CipherUpdate() return 1 for success and 0 for failure.
195EVP_CipherFinal() returns 1 for a decryption failure or 1 for success. 246EVP_CipherFinal_ex() returns 0 for a decryption failure or 1 for success.
196 247
197EVP_CIPHER_CTX_cleanup() returns 1 for success and 0 for failure. 248EVP_CIPHER_CTX_cleanup() returns 1 for success and 0 for failure.
198 249
@@ -207,6 +258,8 @@ size.
207EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key 258EVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key
208length. 259length.
209 260
261EVP_CIPHER_CTX_set_padding() always returns 1.
262
210EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV 263EVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the IV
211length or zero if the cipher does not use an IV. 264length 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
302When decrypting the final block is checked to see if it has the correct form. 355When decrypting the final block is checked to see if it has the correct form.
303 356
304Although the decryption operation can produce an error, it is not a strong 357Although the decryption operation can produce an error if padding is enabled,
305test that the input data or key is correct. A random block has better than 358it is not a strong test that the input data or key is correct. A random block
3061 in 256 chance of being of the correct format and problems with the 359has better than 1 in 256 chance of being of the correct format and problems with
307input data earlier on will not produce a final decrypt error. 360the input data earlier on will not produce a final decrypt error.
308 361
309The functions EVP_EncryptInit(), EVP_EncryptUpdate(), EVP_EncryptFinal(), 362If padding is disabled then the decryption operation will always succeed if
310EVP_DecryptInit(), EVP_DecryptUpdate(), EVP_CipherInit() and EVP_CipherUpdate() 363the total amount of data decrypted is a multiple of the block size.
311and EVP_CIPHER_CTX_cleanup() did not return errors in OpenSSL version 0.9.5a or 364
312earlier. Software only versions of encryption algorithms will never return 365The functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(),
313error codes for these functions, unless there is a programming error (for example 366EVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained for
314and attempt to set the key before the cipher is set in EVP_EncryptInit() ). 367compatibility with existing code. New code should use EVP_EncryptInit_ex(),
368EVP_EncryptFinal_ex(), EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(),
369EVP_CipherInit_ex() and EVP_CipherFinal_ex() because they can reuse an
370existing context without allocating and freeing it up on each call.
315 371
316=head1 BUGS 372=head1 BUGS
317 373
318For RC5 the number of rounds can currently only be set to 8, 12 or 16. This is 374For RC5 the number of rounds can currently only be set to 8, 12 or 16. This is
319a limitation of the current RC5 code rather than the EVP interface. 375a limitation of the current RC5 code rather than the EVP interface.
320 376
321It should be possible to disable PKCS padding: currently it isn't.
322
323EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with 377EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with
324default key lengths. If custom ciphers exceed these values the results are 378default key lengths. If custom ciphers exceed these values the results are
325unpredictable. This is because it has become standard practice to define a 379unpredictable. 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.
333Get the number of rounds used in RC5: 387Get 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
338Get the RC2 effective key length: 392Get 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
343Set the number of rounds used in RC5: 397Set 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
348Set the number of rounds used in RC2: 402Set 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
407Encrypt 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
450The ciphertext from the above example can be decrypted using the B<openssl>
451utility with the command line:
452
453 S<openssl bf -in cipher.bin -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 -d>
454
455General encryption, decryption function example using FILE I/O and RC2 with an
45680 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