diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/man/EVP_EncryptInit.3 | 166 |
1 files changed, 96 insertions, 70 deletions
diff --git a/src/lib/libcrypto/man/EVP_EncryptInit.3 b/src/lib/libcrypto/man/EVP_EncryptInit.3 index f883d1ce2a..a9377b2264 100644 --- a/src/lib/libcrypto/man/EVP_EncryptInit.3 +++ b/src/lib/libcrypto/man/EVP_EncryptInit.3 | |||
@@ -1,8 +1,9 @@ | |||
1 | .\" $OpenBSD: EVP_EncryptInit.3,v 1.7 2018/02/14 00:19:03 schwarze Exp $ | 1 | .\" $OpenBSD: EVP_EncryptInit.3,v 1.8 2018/02/19 10:04:08 schwarze Exp $ |
2 | .\" full merge up to: OpenSSL 5211e094 Nov 11 14:39:11 2014 -0800 | 2 | .\" full merge up to: OpenSSL 5211e094 Nov 11 14:39:11 2014 -0800 |
3 | .\" selective merge up to: OpenSSL 5c5eb286 Dec 5 00:36:43 2017 +0100 | 3 | .\" selective merge up to: OpenSSL 5c5eb286 Dec 5 00:36:43 2017 +0100 |
4 | .\" | 4 | .\" |
5 | .\" This file was written by Dr. Stephen Henson <steve@openssl.org>. | 5 | .\" This file was written by Dr. Stephen Henson <steve@openssl.org> |
6 | .\" and Richard Levitte <levitte@openssl.org>. | ||
6 | .\" Copyright (c) 2000-2002, 2005, 2012-2016 The OpenSSL Project. | 7 | .\" Copyright (c) 2000-2002, 2005, 2012-2016 The OpenSSL Project. |
7 | .\" All rights reserved. | 8 | .\" All rights reserved. |
8 | .\" | 9 | .\" |
@@ -50,11 +51,13 @@ | |||
50 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | 51 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
51 | .\" OF THE POSSIBILITY OF SUCH DAMAGE. | 52 | .\" OF THE POSSIBILITY OF SUCH DAMAGE. |
52 | .\" | 53 | .\" |
53 | .Dd $Mdocdate: February 14 2018 $ | 54 | .Dd $Mdocdate: February 19 2018 $ |
54 | .Dt EVP_ENCRYPTINIT 3 | 55 | .Dt EVP_ENCRYPTINIT 3 |
55 | .Os | 56 | .Os |
56 | .Sh NAME | 57 | .Sh NAME |
57 | .Nm EVP_CIPHER_CTX_new , | 58 | .Nm EVP_CIPHER_CTX_new , |
59 | .Nm EVP_CIPHER_CTX_reset , | ||
60 | .Nm EVP_CIPHER_CTX_cleanup , | ||
58 | .Nm EVP_CIPHER_CTX_init , | 61 | .Nm EVP_CIPHER_CTX_init , |
59 | .Nm EVP_CIPHER_CTX_free , | 62 | .Nm EVP_CIPHER_CTX_free , |
60 | .Nm EVP_EncryptInit_ex , | 63 | .Nm EVP_EncryptInit_ex , |
@@ -76,7 +79,6 @@ | |||
76 | .Nm EVP_CIPHER_CTX_set_key_length , | 79 | .Nm EVP_CIPHER_CTX_set_key_length , |
77 | .Nm EVP_CIPHER_CTX_ctrl , | 80 | .Nm EVP_CIPHER_CTX_ctrl , |
78 | .Nm EVP_CIPHER_CTX_rand_key , | 81 | .Nm EVP_CIPHER_CTX_rand_key , |
79 | .Nm EVP_CIPHER_CTX_cleanup , | ||
80 | .Nm EVP_get_cipherbyname , | 82 | .Nm EVP_get_cipherbyname , |
81 | .Nm EVP_get_cipherbynid , | 83 | .Nm EVP_get_cipherbynid , |
82 | .Nm EVP_get_cipherbyobj , | 84 | .Nm EVP_get_cipherbyobj , |
@@ -164,6 +166,14 @@ | |||
164 | .In openssl/evp.h | 166 | .In openssl/evp.h |
165 | .Ft EVP_CIPHER_CTX * | 167 | .Ft EVP_CIPHER_CTX * |
166 | .Fn EVP_CIPHER_CTX_new void | 168 | .Fn EVP_CIPHER_CTX_new void |
169 | .Ft int | ||
170 | .Fo EVP_CIPHER_CTX_reset | ||
171 | .Fa "EVP_CIPHER_CTX *ctx" | ||
172 | .Fc | ||
173 | .Ft int | ||
174 | .Fo EVP_CIPHER_CTX_cleanup | ||
175 | .Fa "EVP_CIPHER_CTX *ctx" | ||
176 | .Fc | ||
167 | .Ft void | 177 | .Ft void |
168 | .Fo EVP_CIPHER_CTX_init | 178 | .Fo EVP_CIPHER_CTX_init |
169 | .Fa "EVP_CIPHER_CTX *ctx" | 179 | .Fa "EVP_CIPHER_CTX *ctx" |
@@ -301,10 +311,6 @@ | |||
301 | .Fa "EVP_CIPHER_CTX *ctx" | 311 | .Fa "EVP_CIPHER_CTX *ctx" |
302 | .Fa "unsigned char *key" | 312 | .Fa "unsigned char *key" |
303 | .Fc | 313 | .Fc |
304 | .Ft int | ||
305 | .Fo EVP_CIPHER_CTX_cleanup | ||
306 | .Fa "EVP_CIPHER_CTX *ctx" | ||
307 | .Fc | ||
308 | .Ft const EVP_CIPHER * | 314 | .Ft const EVP_CIPHER * |
309 | .Fo EVP_get_cipherbyname | 315 | .Fo EVP_get_cipherbyname |
310 | .Fa "const char *name" | 316 | .Fa "const char *name" |
@@ -401,15 +407,35 @@ The EVP cipher routines are a high level interface to certain symmetric | |||
401 | ciphers. | 407 | ciphers. |
402 | .Pp | 408 | .Pp |
403 | .Fn EVP_CIPHER_CTX_new | 409 | .Fn EVP_CIPHER_CTX_new |
404 | creates a cipher context. | 410 | creates a new, empty cipher context. |
411 | .Pp | ||
412 | .Fn EVP_CIPHER_CTX_reset | ||
413 | clears all information from | ||
414 | .Fa ctx | ||
415 | and frees all allocated memory associated with it, except the | ||
416 | .Fa ctx | ||
417 | object itself, such that it can be reused for another series of calls to | ||
418 | .Fn EVP_CipherInit , | ||
419 | .Fn EVP_CipherUpdate , | ||
420 | and | ||
421 | .Fn EVP_CipherFinal . | ||
422 | It is also suitable for cipher contexts on the stack that were used | ||
423 | and are no longer needed. | ||
424 | .Fn EVP_CIPHER_CTX_cleanup | ||
425 | is a deprecated alias for | ||
426 | .Fn EVP_CIPHER_CTX_reset . | ||
405 | .Pp | 427 | .Pp |
406 | .Fn EVP_CIPHER_CTX_init | 428 | .Fn EVP_CIPHER_CTX_init |
407 | initializes the cipher context | 429 | is a deprecated function to clear a cipher context on the stack |
408 | .Fa ctx . | 430 | before use. |
431 | Do not use it on a cipher context returned from | ||
432 | .Fn EVP_CIPHER_CTX_new | ||
433 | or one one that was already used. | ||
409 | .Pp | 434 | .Pp |
410 | .Fn EVP_CIPHER_CTX_free | 435 | .Fn EVP_CIPHER_CTX_free |
411 | clears all information from a cipher context and frees up any | 436 | clears all information from |
412 | allocated memory associate with it, including | 437 | .Fa ctx |
438 | and frees all allocated memory associated with it, including | ||
413 | .Fa ctx | 439 | .Fa ctx |
414 | itself. | 440 | itself. |
415 | This function should be called after all operations using a cipher | 441 | This function should be called after all operations using a cipher |
@@ -428,8 +454,11 @@ for encryption with cipher | |||
428 | from | 454 | from |
429 | .Vt ENGINE | 455 | .Vt ENGINE |
430 | .Fa impl . | 456 | .Fa impl . |
457 | If | ||
431 | .Fa ctx | 458 | .Fa ctx |
432 | must be initialized before calling this function. | 459 | points to an unused object on the stack, it must be initialized with |
460 | .Fn EVP_MD_CTX_init | ||
461 | before calling this function. | ||
433 | .Fa type | 462 | .Fa type |
434 | is normally supplied by a function such as | 463 | is normally supplied by a function such as |
435 | .Fn EVP_aes_256_cbc . | 464 | .Fn EVP_aes_256_cbc . |
@@ -524,25 +553,19 @@ the value unchanged (the actual value of | |||
524 | .Fa enc | 553 | .Fa enc |
525 | being supplied in a previous call). | 554 | being supplied in a previous call). |
526 | .Pp | 555 | .Pp |
527 | .Fn EVP_CIPHER_CTX_cleanup | ||
528 | clears all information from a cipher context and free up any allocated | ||
529 | memory associated with it. | ||
530 | It should be called after all operations using a cipher are complete so | ||
531 | sensitive information does not remain in memory. | ||
532 | .Pp | ||
533 | .Fn EVP_EncryptInit , | 556 | .Fn EVP_EncryptInit , |
534 | .Fn EVP_DecryptInit , | 557 | .Fn EVP_DecryptInit , |
535 | and | 558 | and |
536 | .Fn EVP_CipherInit | 559 | .Fn EVP_CipherInit |
537 | behave in a similar way to | 560 | are deprecated functions behaving like |
538 | .Fn EVP_EncryptInit_ex , | 561 | .Fn EVP_EncryptInit_ex , |
539 | .Fn EVP_DecryptInit_ex , | 562 | .Fn EVP_DecryptInit_ex , |
540 | and | 563 | and |
541 | .Fn EVP_CipherInit_ex | 564 | .Fn EVP_CipherInit_ex |
542 | except the | 565 | except that they always use the default cipher implementation |
543 | .Fa ctx | 566 | and that they require |
544 | parameter does not need to be initialized and they always use the | 567 | .Fn EVP_CIPHER_CTX_reset |
545 | default cipher implementation. | 568 | before they can be used on a context that was already used. |
546 | .Pp | 569 | .Pp |
547 | .Fn EVP_EncryptFinal , | 570 | .Fn EVP_EncryptFinal , |
548 | .Fn EVP_DecryptFinal , | 571 | .Fn EVP_DecryptFinal , |
@@ -556,7 +579,9 @@ and | |||
556 | In previous releases of OpenSSL, they also used to clean up the | 579 | In previous releases of OpenSSL, they also used to clean up the |
557 | .Fa ctx , | 580 | .Fa ctx , |
558 | but this is no longer done and | 581 | but this is no longer done and |
559 | .Fn EVP_CIPHER_CTX_cleanup | 582 | .Fn EVP_CIPHER_CTX_reset |
583 | or | ||
584 | .Fn EVP_CIPHER_CTX_free | ||
560 | must be called to free any context resources. | 585 | must be called to free any context resources. |
561 | .Pp | 586 | .Pp |
562 | .Fn EVP_get_cipherbyname , | 587 | .Fn EVP_get_cipherbyname , |
@@ -782,31 +807,31 @@ for success or | |||
782 | .Dv NULL | 807 | .Dv NULL |
783 | for failure. | 808 | for failure. |
784 | .Pp | 809 | .Pp |
810 | .Fn EVP_CIPHER_CTX_reset , | ||
811 | .Fn EVP_CIPHER_CTX_cleanup , | ||
785 | .Fn EVP_EncryptInit_ex , | 812 | .Fn EVP_EncryptInit_ex , |
786 | .Fn EVP_EncryptUpdate , | 813 | .Fn EVP_EncryptUpdate , |
814 | .Fn EVP_EncryptFinal_ex , | ||
815 | .Fn EVP_DecryptInit_ex , | ||
816 | .Fn EVP_DecryptUpdate , | ||
817 | .Fn EVP_DecryptFinal_ex , | ||
818 | .Fn EVP_CipherInit_ex , | ||
819 | .Fn EVP_CipherUpdate , | ||
820 | .Fn EVP_CipherFinal_ex , | ||
821 | .Fn EVP_EncryptInit , | ||
822 | .Fn EVP_EncryptFinal , | ||
823 | .Fn EVP_DecryptInit , | ||
824 | .Fn EVP_DecryptFinal , | ||
825 | .Fn EVP_CipherInit , | ||
826 | .Fn EVP_CipherFinal , | ||
827 | .Fn EVP_CIPHER_CTX_set_key_length , | ||
787 | and | 828 | and |
788 | .Fn EVP_EncryptFinal_ex | ||
789 | return 1 for success and 0 for failure. | ||
790 | .Pp | ||
791 | .Fn EVP_DecryptInit_ex | ||
792 | and | ||
793 | .Fn EVP_DecryptUpdate | ||
794 | return 1 for success and 0 for failure. | ||
795 | .Fn EVP_DecryptFinal_ex | ||
796 | returns 0 if the decrypt failed or 1 for success. | ||
797 | .Pp | ||
798 | .Fn EVP_CipherInit_ex | ||
799 | and | ||
800 | .Fn EVP_CipherUpdate | ||
801 | return 1 for success and 0 for failure. | ||
802 | .Fn EVP_CipherFinal_ex | ||
803 | returns 0 for a decryption failure or 1 for success. | ||
804 | .Pp | ||
805 | .Fn EVP_CIPHER_CTX_rand_key | 829 | .Fn EVP_CIPHER_CTX_rand_key |
806 | and | ||
807 | .Fn EVP_CIPHER_CTX_cleanup | ||
808 | return 1 for success or 0 for failure. | 830 | return 1 for success or 0 for failure. |
809 | .Pp | 831 | .Pp |
832 | .Fn EVP_CIPHER_CTX_set_padding | ||
833 | always returns 1. | ||
834 | .Pp | ||
810 | .Fn EVP_get_cipherbyname , | 835 | .Fn EVP_get_cipherbyname , |
811 | .Fn EVP_get_cipherbynid , | 836 | .Fn EVP_get_cipherbynid , |
812 | and | 837 | and |
@@ -832,9 +857,6 @@ and | |||
832 | .Fn EVP_CIPHER_CTX_key_length | 857 | .Fn EVP_CIPHER_CTX_key_length |
833 | return the key length. | 858 | return the key length. |
834 | .Pp | 859 | .Pp |
835 | .Fn EVP_CIPHER_CTX_set_padding | ||
836 | always returns 1. | ||
837 | .Pp | ||
838 | .Fn EVP_CIPHER_iv_length | 860 | .Fn EVP_CIPHER_iv_length |
839 | and | 861 | and |
840 | .Fn EVP_CIPHER_CTX_iv_length | 862 | .Fn EVP_CIPHER_CTX_iv_length |
@@ -1102,10 +1124,11 @@ do_crypt(char *outfile) | |||
1102 | const char intext[] = "Some Crypto Text"; | 1124 | const char intext[] = "Some Crypto Text"; |
1103 | EVP_CIPHER_CTX *ctx; | 1125 | EVP_CIPHER_CTX *ctx; |
1104 | FILE *out; | 1126 | FILE *out; |
1105 | EVP_CIPHER_CTX_init(&ctx); | ||
1106 | EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv); | ||
1107 | 1127 | ||
1108 | if (!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, | 1128 | ctx = EVP_CIPHER_CTX_new(); |
1129 | EVP_EncryptInit_ex(ctx, EVP_bf_cbc(), NULL, key, iv); | ||
1130 | |||
1131 | if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, | ||
1109 | strlen(intext))) { | 1132 | strlen(intext))) { |
1110 | /* Error */ | 1133 | /* Error */ |
1111 | EVP_CIPHER_CTX_free(ctx); | 1134 | EVP_CIPHER_CTX_free(ctx); |
@@ -1115,13 +1138,13 @@ do_crypt(char *outfile) | |||
1115 | * Buffer passed to EVP_EncryptFinal() must be after data just | 1138 | * Buffer passed to EVP_EncryptFinal() must be after data just |
1116 | * encrypted to avoid overwriting it. | 1139 | * encrypted to avoid overwriting it. |
1117 | */ | 1140 | */ |
1118 | if (!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) { | 1141 | if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) { |
1119 | /* Error */ | 1142 | /* Error */ |
1120 | EVP_CIPHER_CTX_free(ctx); | 1143 | EVP_CIPHER_CTX_free(ctx); |
1121 | return 0; | 1144 | return 0; |
1122 | } | 1145 | } |
1123 | outlen += tmplen; | 1146 | outlen += tmplen; |
1124 | EVP_CIPHER_CTX_cleanup(&ctx); | 1147 | EVP_CIPHER_CTX_free(ctx); |
1125 | /* | 1148 | /* |
1126 | * Need binary mode for fopen because encrypted data is | 1149 | * Need binary mode for fopen because encrypted data is |
1127 | * binary data. Also cannot use strlen() on it because | 1150 | * binary data. Also cannot use strlen() on it because |
@@ -1147,49 +1170,49 @@ openssl bf -in cipher.bin -K 000102030405060708090A0B0C0D0E0F \e | |||
1147 | -iv 0102030405060708 -d | 1170 | -iv 0102030405060708 -d |
1148 | .Ed | 1171 | .Ed |
1149 | .Pp | 1172 | .Pp |
1150 | General encryption, decryption function example using FILE I/O and RC2 | 1173 | General encryption, decryption function example using FILE I/O and AES128 |
1151 | with an 80-bit key: | 1174 | with an 128-bit key: |
1152 | .Bd -literal | 1175 | .Bd -literal |
1153 | int | 1176 | int |
1154 | do_crypt(FILE *in, FILE *out, int do_encrypt) | 1177 | do_crypt(FILE *in, FILE *out, int do_encrypt) |
1155 | { | 1178 | { |
1156 | /* Allow enough space in output buffer for additional block */ | 1179 | /* Allow enough space in output buffer for additional block */ |
1157 | inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; | 1180 | unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; |
1158 | int inlen, outlen; | 1181 | int inlen, outlen; |
1182 | EVP_CIPHER_CTX *ctx; | ||
1183 | |||
1159 | /* | 1184 | /* |
1160 | * Bogus key and IV: we'd normally set these from | 1185 | * Bogus key and IV: we'd normally set these from |
1161 | * another source. | 1186 | * another source. |
1162 | */ | 1187 | */ |
1163 | unsigned char key[] = "0123456789"; | 1188 | unsigned char key[] = "0123456789abcdeF"; |
1164 | unsigned char iv[] = "12345678"; | 1189 | unsigned char iv[] = "1234567887654321"; |
1165 | 1190 | ||
1166 | /* Don't set key or IV because we will modify the parameters */ | 1191 | ctx = EVP_CIPHER_CTX_new(); |
1167 | EVP_CIPHER_CTX_init(&ctx); | 1192 | EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, NULL, NULL, |
1168 | EVP_CipherInit_ex(&ctx, EVP_rc2(), NULL, NULL, NULL, do_encrypt); | 1193 | do_encrypt); |
1169 | EVP_CIPHER_CTX_set_key_length(&ctx, 10); | 1194 | EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt); |
1170 | /* We finished modifying parameters so now we can set key and IV */ | ||
1171 | EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt); | ||
1172 | 1195 | ||
1173 | for(;;) { | 1196 | for (;;) { |
1174 | inlen = fread(inbuf, 1, 1024, in); | 1197 | inlen = fread(inbuf, 1, 1024, in); |
1175 | if (inlen <= 0) | 1198 | if (inlen <= 0) |
1176 | break; | 1199 | break; |
1177 | if (!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, | 1200 | if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, |
1178 | inlen)) { | 1201 | inlen)) { |
1179 | /* Error */ | 1202 | /* Error */ |
1180 | EVP_CIPHER_CTX_cleanup(&ctx); | 1203 | EVP_CIPHER_CTX_free(ctx); |
1181 | return 0; | 1204 | return 0; |
1182 | } | 1205 | } |
1183 | fwrite(outbuf, 1, outlen, out); | 1206 | fwrite(outbuf, 1, outlen, out); |
1184 | } | 1207 | } |
1185 | if (!EVP_CipherFinal_ex(&ctx, outbuf, &outlen)) { | 1208 | if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) { |
1186 | /* Error */ | 1209 | /* Error */ |
1187 | EVP_CIPHER_CTX_cleanup(&ctx); | 1210 | EVP_CIPHER_CTX_free(ctx); |
1188 | return 0; | 1211 | return 0; |
1189 | } | 1212 | } |
1190 | fwrite(outbuf, 1, outlen, out); | 1213 | fwrite(outbuf, 1, outlen, out); |
1191 | 1214 | ||
1192 | EVP_CIPHER_CTX_cleanup(&ctx); | 1215 | EVP_CIPHER_CTX_free(ctx); |
1193 | return 1; | 1216 | return 1; |
1194 | } | 1217 | } |
1195 | .Ed | 1218 | .Ed |
@@ -1206,6 +1229,9 @@ do_crypt(FILE *in, FILE *out, int do_encrypt) | |||
1206 | and | 1229 | and |
1207 | .Fn EVP_CIPHER_CTX_set_padding | 1230 | .Fn EVP_CIPHER_CTX_set_padding |
1208 | appeared in OpenSSL 0.9.7. | 1231 | appeared in OpenSSL 0.9.7. |
1232 | .Pp | ||
1233 | .Fn EVP_CIPHER_CTX_reset | ||
1234 | fist appeared in OpenSSL 1.1.0. | ||
1209 | .Sh BUGS | 1235 | .Sh BUGS |
1210 | For RC5 the number of rounds can currently only be set to 8, 12 or 16. | 1236 | For RC5 the number of rounds can currently only be set to 8, 12 or 16. |
1211 | This is a limitation of the current RC5 code rather than the EVP | 1237 | This is a limitation of the current RC5 code rather than the EVP |