]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/enc.c
Copyright year updates
[thirdparty/openssl.git] / apps / enc.c
CommitLineData
846e33c7 1/*
da1c088f 2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
d02b48c6
RE
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
bd4850df 13#include <limits.h>
d02b48c6 14#include "apps.h"
dab2cd68 15#include "progs.h"
ec577822
BM
16#include <openssl/bio.h>
17#include <openssl/err.h>
18#include <openssl/evp.h>
19#include <openssl/objects.h>
20#include <openssl/x509.h>
fd699ac5 21#include <openssl/rand.h>
ec577822 22#include <openssl/pem.h>
9494e99b 23#ifndef OPENSSL_NO_COMP
0f113f3e 24# include <openssl/comp.h>
9494e99b 25#endif
646d5695 26#include <ctype.h>
d02b48c6 27
d02b48c6
RE
28#undef SIZE
29#undef BSIZE
0f113f3e
MC
30#define SIZE (512)
31#define BSIZE (8*1024)
646d5695 32
dc43f080
P
33#define PBKDF2_ITER_DEFAULT 10000
34#define STR(a) XSTR(a)
35#define XSTR(a) #a
36
d5961b22 37static int set_hex(const char *in, unsigned char *out, int size);
7e1b7485
RS
38static void show_ciphers(const OBJ_NAME *name, void *bio_);
39
2b305ab0
P
40struct doall_enc_ciphers {
41 BIO *bio;
42 int n;
43};
44
7e1b7485 45typedef enum OPTION_choice {
b0f96018 46 OPT_COMMON,
3b5bea36 47 OPT_LIST,
7e1b7485
RS
48 OPT_E, OPT_IN, OPT_OUT, OPT_PASS, OPT_ENGINE, OPT_D, OPT_P, OPT_V,
49 OPT_NOPAD, OPT_SALT, OPT_NOSALT, OPT_DEBUG, OPT_UPPER_P, OPT_UPPER_A,
50 OPT_A, OPT_Z, OPT_BUFSIZE, OPT_K, OPT_KFILE, OPT_UPPER_K, OPT_NONE,
405988f2 51 OPT_UPPER_S, OPT_IV, OPT_MD, OPT_ITER, OPT_PBKDF2, OPT_CIPHER,
e3994583 52 OPT_SALTLEN, OPT_R_ENUM, OPT_PROV_ENUM
7e1b7485
RS
53} OPTION_CHOICE;
54
44c83ebd 55const OPTIONS enc_options[] = {
5388f986 56 OPT_SECTION("General"),
7e1b7485 57 {"help", OPT_HELP, '-', "Display this summary"},
f62d67b6 58 {"list", OPT_LIST, '-', "List ciphers"},
936c2b9e 59#ifndef OPENSSL_NO_DEPRECATED_3_0
f62d67b6 60 {"ciphers", OPT_LIST, '-', "Alias for -list"},
61#endif
7e1b7485
RS
62 {"e", OPT_E, '-', "Encrypt"},
63 {"d", OPT_D, '-', "Decrypt"},
64 {"p", OPT_P, '-', "Print the iv/key"},
65 {"P", OPT_UPPER_P, '-', "Print the iv/key and exit"},
5388f986
RS
66#ifndef OPENSSL_NO_ENGINE
67 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
68#endif
69
70 OPT_SECTION("Input"),
71 {"in", OPT_IN, '<', "Input file"},
72 {"k", OPT_K, 's', "Passphrase"},
73 {"kfile", OPT_KFILE, '<', "Read passphrase from file"},
74
75 OPT_SECTION("Output"),
76 {"out", OPT_OUT, '>', "Output file"},
77 {"pass", OPT_PASS, 's', "Passphrase source"},
9a13bb38 78 {"v", OPT_V, '-', "Verbose output"},
9a13bb38
RS
79 {"a", OPT_A, '-', "Base64 encode/decode, depending on encryption flag"},
80 {"base64", OPT_A, '-', "Same as option -a"},
81 {"A", OPT_UPPER_A, '-',
82 "Used with -[base64|a] to specify base64 buffer as a single line"},
5388f986
RS
83
84 OPT_SECTION("Encryption"),
85 {"nopad", OPT_NOPAD, '-', "Disable standard block padding"},
86 {"salt", OPT_SALT, '-', "Use salt in the KDF (default)"},
87 {"nosalt", OPT_NOSALT, '-', "Do not use salt in the KDF"},
88 {"debug", OPT_DEBUG, '-', "Print debug info"},
89
7e1b7485 90 {"bufsize", OPT_BUFSIZE, 's', "Buffer size"},
b256f717 91 {"K", OPT_UPPER_K, 's', "Raw key, in hex"},
7e1b7485
RS
92 {"S", OPT_UPPER_S, 's', "Salt, in hex"},
93 {"iv", OPT_IV, 's', "IV in hex"},
5507b961 94 {"md", OPT_MD, 's', "Use specified digest to create a key from the passphrase"},
dc43f080
P
95 {"iter", OPT_ITER, 'p',
96 "Specify the iteration count and force the use of PBKDF2"},
97 {OPT_MORE_STR, 0, 0, "Default: " STR(PBKDF2_ITER_DEFAULT)},
98 {"pbkdf2", OPT_PBKDF2, '-',
99 "Use password-based key derivation function 2 (PBKDF2)"},
100 {OPT_MORE_STR, 0, 0,
101 "Use -iter to change the iteration count from " STR(PBKDF2_ITER_DEFAULT)},
7e1b7485 102 {"none", OPT_NONE, '-', "Don't encrypt"},
e3994583 103 {"saltlen", OPT_SALTLEN, 'p', "Specify the PBKDF2 salt length (in bytes)"},
104 {OPT_MORE_STR, 0, 0, "Default: 16"},
59d21298 105#ifndef OPENSSL_NO_ZLIB
6635ea53 106 {"z", OPT_Z, '-', "Compress or decompress encrypted data using zlib"},
9c3bcfa0 107#endif
5388f986
RS
108 {"", OPT_CIPHER, '-', "Any supported cipher"},
109
110 OPT_R_OPTIONS,
6bd4e3f2 111 OPT_PROV_OPTIONS,
7e1b7485
RS
112 {NULL}
113};
114
115int enc_main(int argc, char **argv)
0f113f3e 116{
7e1b7485 117 static char buf[128];
0f113f3e 118 static const char magic[] = "Salted__";
dd1abd44 119 ENGINE *e = NULL;
7e1b7485
RS
120 BIO *in = NULL, *out = NULL, *b64 = NULL, *benc = NULL, *rbio =
121 NULL, *wbio = NULL;
122 EVP_CIPHER_CTX *ctx = NULL;
606a417f
RS
123 EVP_CIPHER *cipher = NULL;
124 EVP_MD *dgst = NULL;
d0190e11 125 const char *digestname = NULL;
333b070e 126 char *hkey = NULL, *hiv = NULL, *hsalt = NULL, *p;
50ca7e18 127 char *infile = NULL, *outfile = NULL, *prog;
7e1b7485 128 char *str = NULL, *passarg = NULL, *pass = NULL, *strbuf = NULL;
50ca7e18 129 const char *ciphername = NULL;
cbe29648 130 char mbuf[sizeof(magic) - 1];
7e1b7485
RS
131 OPTION_CHOICE o;
132 int bsize = BSIZE, verbose = 0, debug = 0, olb64 = 0, nosalt = 0;
5f62e044
RL
133 int enc = 1, printkey = 0, i, k;
134 int base64 = 0, informat = FORMAT_BINARY, outformat = FORMAT_BINARY;
700b4a4a 135 int ret = 1, inl, nopad = 0;
0f113f3e 136 unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
e3994583 137 unsigned char *buff = NULL, salt[EVP_MAX_IV_LENGTH];
138 int saltlen = 0;
405988f2
E
139 int pbkdf2 = 0;
140 int iter = 0;
bd4850df 141 long n;
7850cc83
E
142 int streamable = 1;
143 int wrap = 0;
2b305ab0 144 struct doall_enc_ciphers dec;
59d21298 145#ifndef OPENSSL_NO_ZLIB
0f113f3e
MC
146 int do_zlib = 0;
147 BIO *bzl = NULL;
8931b30d 148#endif
12e96a23
TS
149 int do_brotli = 0;
150 BIO *bbrot = NULL;
caf9317d
TS
151 int do_zstd = 0;
152 BIO *bzstd = NULL;
3647bee2 153
33720392 154 /* first check the command name */
50ca7e18 155 if (strcmp(argv[0], "base64") == 0)
5f62e044 156 base64 = 1;
59d21298 157#ifndef OPENSSL_NO_ZLIB
50ca7e18 158 else if (strcmp(argv[0], "zlib") == 0)
0f113f3e 159 do_zlib = 1;
12e96a23
TS
160#endif
161#ifndef OPENSSL_NO_BROTLI
162 else if (strcmp(argv[0], "brotli") == 0)
163 do_brotli = 1;
caf9317d
TS
164#endif
165#ifndef OPENSSL_NO_ZSTD
166 else if (strcmp(argv[0], "zstd") == 0)
167 do_zstd = 1;
8931b30d 168#endif
50ca7e18
RS
169 else if (strcmp(argv[0], "enc") != 0)
170 ciphername = argv[0];
0f113f3e 171
2c272447 172 opt_set_unknown_name("cipher");
7e1b7485
RS
173 prog = opt_init(argc, argv, enc_options);
174 while ((o = opt_next()) != OPT_EOF) {
175 switch (o) {
176 case OPT_EOF:
177 case OPT_ERR:
178 opthelp:
179 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
180 goto end;
181 case OPT_HELP:
182 opt_help(enc_options);
183 ret = 0;
3b5bea36
RS
184 goto end;
185 case OPT_LIST:
341de5f1 186 BIO_printf(bio_out, "Supported ciphers:\n");
2b305ab0
P
187 dec.bio = bio_out;
188 dec.n = 0;
7e1b7485 189 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
2b305ab0 190 show_ciphers, &dec);
341de5f1
RL
191 BIO_printf(bio_out, "\n");
192 ret = 0;
7e1b7485
RS
193 goto end;
194 case OPT_E:
0f113f3e 195 enc = 1;
7e1b7485
RS
196 break;
197 case OPT_IN:
198 infile = opt_arg();
199 break;
200 case OPT_OUT:
201 outfile = opt_arg();
202 break;
203 case OPT_PASS:
204 passarg = opt_arg();
205 break;
206 case OPT_ENGINE:
dd1abd44 207 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
208 break;
209 case OPT_D:
0f113f3e 210 enc = 0;
7e1b7485
RS
211 break;
212 case OPT_P:
0f113f3e 213 printkey = 1;
7e1b7485
RS
214 break;
215 case OPT_V:
0f113f3e 216 verbose = 1;
7e1b7485
RS
217 break;
218 case OPT_NOPAD:
0f113f3e 219 nopad = 1;
7e1b7485
RS
220 break;
221 case OPT_SALT:
0f113f3e 222 nosalt = 0;
7e1b7485
RS
223 break;
224 case OPT_NOSALT:
0f113f3e 225 nosalt = 1;
7e1b7485
RS
226 break;
227 case OPT_DEBUG:
0f113f3e 228 debug = 1;
7e1b7485
RS
229 break;
230 case OPT_UPPER_P:
0f113f3e 231 printkey = 2;
7e1b7485
RS
232 break;
233 case OPT_UPPER_A:
0f113f3e 234 olb64 = 1;
7e1b7485
RS
235 break;
236 case OPT_A:
5f62e044 237 base64 = 1;
7e1b7485
RS
238 break;
239 case OPT_Z:
59d21298 240#ifndef OPENSSL_NO_ZLIB
0f113f3e 241 do_zlib = 1;
8931b30d 242#endif
7e1b7485
RS
243 break;
244 case OPT_BUFSIZE:
245 p = opt_arg();
246 i = (int)strlen(p) - 1;
247 k = i >= 1 && p[i] == 'k';
248 if (k)
249 p[i] = '\0';
bd4850df
RS
250 if (!opt_long(opt_arg(), &n)
251 || n < 0 || (k && n >= LONG_MAX / 1024))
7e1b7485
RS
252 goto opthelp;
253 if (k)
254 n *= 1024;
255 bsize = (int)n;
256 break;
257 case OPT_K:
258 str = opt_arg();
259 break;
260 case OPT_KFILE:
bdd58d98 261 in = bio_open_default(opt_arg(), 'r', FORMAT_TEXT);
7e1b7485
RS
262 if (in == NULL)
263 goto opthelp;
cbe29648 264 i = BIO_gets(in, buf, sizeof(buf));
7e1b7485
RS
265 BIO_free(in);
266 in = NULL;
267 if (i <= 0) {
268 BIO_printf(bio_err,
269 "%s Can't read key from %s\n", prog, opt_arg());
270 goto opthelp;
0f113f3e 271 }
7e1b7485
RS
272 while (--i > 0 && (buf[i] == '\r' || buf[i] == '\n'))
273 buf[i] = '\0';
274 if (i <= 0) {
275 BIO_printf(bio_err, "%s: zero length password\n", prog);
276 goto opthelp;
0f113f3e
MC
277 }
278 str = buf;
7e1b7485
RS
279 break;
280 case OPT_UPPER_K:
281 hkey = opt_arg();
282 break;
283 case OPT_UPPER_S:
284 hsalt = opt_arg();
285 break;
286 case OPT_IV:
287 hiv = opt_arg();
288 break;
289 case OPT_MD:
d0190e11 290 digestname = opt_arg();
7e1b7485 291 break;
7e1b7485 292 case OPT_CIPHER:
50ca7e18 293 ciphername = opt_unknown();
7e1b7485 294 break;
405988f2 295 case OPT_ITER:
d830526c 296 iter = opt_int_arg();
405988f2
E
297 pbkdf2 = 1;
298 break;
e3994583 299 case OPT_SALTLEN:
300 if (!opt_int(opt_arg(), &saltlen))
301 goto opthelp;
302 if (saltlen > (int)sizeof(salt))
303 saltlen = (int)sizeof(salt);
304 break;
405988f2
E
305 case OPT_PBKDF2:
306 pbkdf2 = 1;
307 if (iter == 0) /* do not overwrite a chosen value */
dc43f080 308 iter = PBKDF2_ITER_DEFAULT;
405988f2 309 break;
7e1b7485 310 case OPT_NONE:
0f113f3e 311 cipher = NULL;
7e1b7485 312 break;
3ee1eac2
RS
313 case OPT_R_CASES:
314 if (!opt_rand(o))
315 goto end;
316 break;
6bd4e3f2
P
317 case OPT_PROV_CASES:
318 if (!opt_provider(o))
319 goto end;
320 break;
0f113f3e 321 }
0f113f3e 322 }
021410ea
RS
323
324 /* No extra arguments. */
d9f07357 325 if (!opt_check_rest_arg(NULL))
c27363f5 326 goto opthelp;
3ad60309
DDO
327 if (!app_RAND_load())
328 goto end;
e3994583 329 if (saltlen == 0 || pbkdf2 == 0)
330 saltlen = PKCS5_SALT_LEN;
d02b48c6 331
50ca7e18 332 /* Get the cipher name, either from progname (if set) or flag. */
d9f07357
DDO
333 if (!opt_cipher(ciphername, &cipher))
334 goto opthelp;
7850cc83
E
335 if (cipher && (EVP_CIPHER_mode(cipher) == EVP_CIPH_WRAP_MODE)) {
336 wrap = 1;
337 streamable = 0;
338 }
d0190e11
RS
339 if (digestname != NULL) {
340 if (!opt_md(digestname, &dgst))
50ca7e18
RS
341 goto opthelp;
342 }
7e1b7485 343 if (dgst == NULL)
606a417f 344 dgst = (EVP_MD *)EVP_sha256();
0f113f3e 345
405988f2
E
346 if (iter == 0)
347 iter = 1;
348
7e1b7485 349 /* It must be large enough for a base64 encoded line */
5f62e044 350 if (base64 && bsize < 80)
7e1b7485
RS
351 bsize = 80;
352 if (verbose)
353 BIO_printf(bio_err, "bufsize=%d\n", bsize);
0f113f3e 354
59d21298 355#ifndef OPENSSL_NO_ZLIB
12e96a23
TS
356 if (do_zlib)
357 base64 = 0;
802d2243 358#endif
12e96a23
TS
359 if (do_brotli)
360 base64 = 0;
caf9317d
TS
361 if (do_zstd)
362 base64 = 0;
12e96a23
TS
363
364 if (base64) {
365 if (enc)
366 outformat = FORMAT_BASE64;
367 else
368 informat = FORMAT_BASE64;
369 }
5f62e044 370
68dc6824
RS
371 strbuf = app_malloc(SIZE, "strbuf");
372 buff = app_malloc(EVP_ENCODE_LENGTH(bsize), "evp buffer");
0f113f3e 373
7e1b7485 374 if (infile == NULL) {
f4f397a5 375 if (!streamable && printkey != 2) { /* if just print key and exit, it's ok */
7850cc83
E
376 BIO_printf(bio_err, "Unstreamable cipher mode\n");
377 goto end;
378 }
5f62e044 379 in = dup_bio_in(informat);
2234212c 380 } else {
5f62e044 381 in = bio_open_default(infile, 'r', informat);
2234212c 382 }
7e1b7485
RS
383 if (in == NULL)
384 goto end;
0f113f3e 385
2234212c 386 if (str == NULL && passarg != NULL) {
7e1b7485 387 if (!app_passwd(passarg, NULL, &pass, NULL)) {
0f113f3e
MC
388 BIO_printf(bio_err, "Error getting password\n");
389 goto end;
390 }
391 str = pass;
392 }
393
394 if ((str == NULL) && (cipher != NULL) && (hkey == NULL)) {
923b1857 395 if (1) {
48feaceb 396#ifndef OPENSSL_NO_UI_CONSOLE
923b1857
RL
397 for (;;) {
398 char prompt[200];
0f113f3e 399
eee95522 400 BIO_snprintf(prompt, sizeof(prompt), "enter %s %s password:",
ed576acd 401 EVP_CIPHER_get0_name(cipher),
0904e79a 402 (enc) ? "encryption" : "decryption");
923b1857
RL
403 strbuf[0] = '\0';
404 i = EVP_read_pw_string((char *)strbuf, SIZE, prompt, enc);
405 if (i == 0) {
406 if (strbuf[0] == '\0') {
407 ret = 1;
408 goto end;
409 }
410 str = strbuf;
411 break;
412 }
413 if (i < 0) {
414 BIO_printf(bio_err, "bad password read\n");
0f113f3e
MC
415 goto end;
416 }
0f113f3e 417 }
923b1857
RL
418 } else {
419#endif
420 BIO_printf(bio_err, "password required\n");
421 goto end;
0f113f3e
MC
422 }
423 }
424
5f62e044 425 out = bio_open_default(outfile, 'w', outformat);
7e1b7485
RS
426 if (out == NULL)
427 goto end;
0f113f3e 428
7a82f778 429 if (debug) {
0800318a
TM
430 BIO_set_callback_ex(in, BIO_debug_callback_ex);
431 BIO_set_callback_ex(out, BIO_debug_callback_ex);
7a82f778
RL
432 BIO_set_callback_arg(in, (char *)bio_err);
433 BIO_set_callback_arg(out, (char *)bio_err);
434 }
435
0f113f3e
MC
436 rbio = in;
437 wbio = out;
fd699ac5 438
12e96a23 439#ifndef OPENSSL_NO_COMP
59d21298 440# ifndef OPENSSL_NO_ZLIB
0f113f3e
MC
441 if (do_zlib) {
442 if ((bzl = BIO_new(BIO_f_zlib())) == NULL)
443 goto end;
7a82f778 444 if (debug) {
0800318a 445 BIO_set_callback_ex(bzl, BIO_debug_callback_ex);
7a82f778
RL
446 BIO_set_callback_arg(bzl, (char *)bio_err);
447 }
0f113f3e
MC
448 if (enc)
449 wbio = BIO_push(bzl, wbio);
450 else
451 rbio = BIO_push(bzl, rbio);
452 }
12e96a23
TS
453# endif
454
455 if (do_brotli) {
456 if ((bbrot = BIO_new(BIO_f_brotli())) == NULL)
457 goto end;
458 if (debug) {
459 BIO_set_callback_ex(bbrot, BIO_debug_callback_ex);
460 BIO_set_callback_arg(bbrot, (char *)bio_err);
461 }
462 if (enc)
463 wbio = BIO_push(bbrot, wbio);
464 else
465 rbio = BIO_push(bbrot, rbio);
466 }
caf9317d
TS
467
468 if (do_zstd) {
469 if ((bzstd = BIO_new(BIO_f_zstd())) == NULL)
470 goto end;
471 if (debug) {
472 BIO_set_callback_ex(bzstd, BIO_debug_callback_ex);
473 BIO_set_callback_arg(bzstd, (char *)bio_err);
474 }
475 if (enc)
476 wbio = BIO_push(bzstd, wbio);
477 else
478 rbio = BIO_push(bzstd, rbio);
479 }
8931b30d
DSH
480#endif
481
5f62e044 482 if (base64) {
0f113f3e
MC
483 if ((b64 = BIO_new(BIO_f_base64())) == NULL)
484 goto end;
485 if (debug) {
0800318a 486 BIO_set_callback_ex(b64, BIO_debug_callback_ex);
0f113f3e
MC
487 BIO_set_callback_arg(b64, (char *)bio_err);
488 }
489 if (olb64)
490 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
491 if (enc)
492 wbio = BIO_push(b64, wbio);
493 else
494 rbio = BIO_push(b64, rbio);
495 }
496
497 if (cipher != NULL) {
c4c8791e 498 if (str != NULL) { /* a passphrase is available */
0f113f3e 499 /*
c4c8791e
E
500 * Salt handling: if encrypting generate a salt if not supplied,
501 * and write to output BIO. If decrypting use salt from input BIO
502 * if not given with args
0f113f3e
MC
503 */
504 unsigned char *sptr;
f6c460e8
F
505 size_t str_len = strlen(str);
506
2234212c 507 if (nosalt) {
0f113f3e 508 sptr = NULL;
2234212c 509 } else {
e3994583 510 if (hsalt != NULL && !set_hex(hsalt, salt, saltlen)) {
c4c8791e
E
511 BIO_printf(bio_err, "invalid hex salt value\n");
512 goto end;
513 }
514 if (enc) { /* encryption */
515 if (hsalt == NULL) {
e3994583 516 if (RAND_bytes(salt, saltlen) <= 0) {
c4c8791e
E
517 BIO_printf(bio_err, "RAND_bytes failed\n");
518 goto end;
519 }
520 /*
521 * If -P option then don't bother writing.
522 * If salt is given, shouldn't either ?
523 */
524 if ((printkey != 2)
525 && (BIO_write(wbio, magic,
526 sizeof(magic) - 1) != sizeof(magic) - 1
527 || BIO_write(wbio,
528 (char *)salt,
e3994583 529 saltlen) != saltlen)) {
c4c8791e 530 BIO_printf(bio_err, "error writing output file\n");
0f113f3e
MC
531 goto end;
532 }
2234212c 533 }
c4c8791e
E
534 } else { /* decryption */
535 if (hsalt == NULL) {
536 if (BIO_read(rbio, mbuf, sizeof(mbuf)) != sizeof(mbuf)) {
537 BIO_printf(bio_err, "error reading input file\n");
538 goto end;
539 }
540 if (memcmp(mbuf, magic, sizeof(mbuf)) == 0) { /* file IS salted */
541 if (BIO_read(rbio, salt,
e3994583 542 saltlen) != saltlen) {
c4c8791e
E
543 BIO_printf(bio_err, "error reading input file\n");
544 goto end;
545 }
546 } else { /* file is NOT salted, NO salt available */
547 BIO_printf(bio_err, "bad magic number\n");
548 goto end;
549 }
0f113f3e 550 }
0f113f3e 551 }
0f113f3e
MC
552 sptr = salt;
553 }
554
405988f2
E
555 if (pbkdf2 == 1) {
556 /*
557 * derive key and default iv
558 * concatenated into a temporary buffer
559 */
560 unsigned char tmpkeyiv[EVP_MAX_KEY_LENGTH + EVP_MAX_IV_LENGTH];
ed576acd
TM
561 int iklen = EVP_CIPHER_get_key_length(cipher);
562 int ivlen = EVP_CIPHER_get_iv_length(cipher);
405988f2 563 /* not needed if HASH_UPDATE() is fixed : */
e3994583 564 int islen = (sptr != NULL ? saltlen : 0);
565
405988f2
E
566 if (!PKCS5_PBKDF2_HMAC(str, str_len, sptr, islen,
567 iter, dgst, iklen+ivlen, tmpkeyiv)) {
568 BIO_printf(bio_err, "PKCS5_PBKDF2_HMAC failed\n");
569 goto end;
570 }
571 /* split and move data back to global buffer */
572 memcpy(key, tmpkeyiv, iklen);
573 memcpy(iv, tmpkeyiv+iklen, ivlen);
574 } else {
575 BIO_printf(bio_err, "*** WARNING : "
576 "deprecated key derivation used.\n"
577 "Using -iter or -pbkdf2 would be better.\n");
578 if (!EVP_BytesToKey(cipher, dgst, sptr,
579 (unsigned char *)str, str_len,
580 1, key, iv)) {
581 BIO_printf(bio_err, "EVP_BytesToKey failed\n");
582 goto end;
583 }
0f113f3e
MC
584 }
585 /*
586 * zero the complete buffer or the string passed from the command
e3713c36 587 * line.
0f113f3e
MC
588 */
589 if (str == strbuf)
590 OPENSSL_cleanse(str, SIZE);
591 else
f6c460e8 592 OPENSSL_cleanse(str, str_len);
0f113f3e 593 }
8920a7cd 594 if (hiv != NULL) {
ed576acd 595 int siz = EVP_CIPHER_get_iv_length(cipher);
8920a7cd 596 if (siz == 0) {
9c119bc6 597 BIO_printf(bio_err, "warning: iv not used by this cipher\n");
d5961b22 598 } else if (!set_hex(hiv, iv, siz)) {
8920a7cd
RL
599 BIO_printf(bio_err, "invalid hex iv value\n");
600 goto end;
601 }
0f113f3e
MC
602 }
603 if ((hiv == NULL) && (str == NULL)
7850cc83
E
604 && EVP_CIPHER_get_iv_length(cipher) != 0
605 && wrap == 0) {
0f113f3e 606 /*
405988f2
E
607 * No IV was explicitly set and no IV was generated.
608 * Hence the IV is undefined, making correct decryption impossible.
0f113f3e
MC
609 */
610 BIO_printf(bio_err, "iv undefined\n");
611 goto end;
612 }
1f83edda 613 if (hkey != NULL) {
ed576acd 614 if (!set_hex(hkey, key, EVP_CIPHER_get_key_length(cipher))) {
1f83edda
E
615 BIO_printf(bio_err, "invalid hex key value\n");
616 goto end;
617 }
618 /* wiping secret data as we no longer need it */
6d382c74 619 cleanse(hkey);
0f113f3e
MC
620 }
621
622 if ((benc = BIO_new(BIO_f_cipher())) == NULL)
623 goto end;
624
625 /*
626 * Since we may be changing parameters work on the encryption context
627 * rather than calling BIO_set_cipher().
628 */
629
630 BIO_get_cipher_ctx(benc, &ctx);
631
7850cc83
E
632 if (wrap == 1)
633 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
634
1d864f0f 635 if (!EVP_CipherInit_ex(ctx, cipher, e, NULL, NULL, enc)) {
0f113f3e 636 BIO_printf(bio_err, "Error setting cipher %s\n",
ed576acd 637 EVP_CIPHER_get0_name(cipher));
0f113f3e
MC
638 ERR_print_errors(bio_err);
639 goto end;
640 }
641
642 if (nopad)
643 EVP_CIPHER_CTX_set_padding(ctx, 0);
644
7850cc83
E
645 if (!EVP_CipherInit_ex(ctx, NULL, NULL, key,
646 (hiv == NULL && wrap == 1 ? NULL : iv), enc)) {
0f113f3e 647 BIO_printf(bio_err, "Error setting cipher %s\n",
ed576acd 648 EVP_CIPHER_get0_name(cipher));
0f113f3e
MC
649 ERR_print_errors(bio_err);
650 goto end;
651 }
652
653 if (debug) {
0800318a 654 BIO_set_callback_ex(benc, BIO_debug_callback_ex);
0f113f3e
MC
655 BIO_set_callback_arg(benc, (char *)bio_err);
656 }
657
658 if (printkey) {
659 if (!nosalt) {
660 printf("salt=");
e3994583 661 for (i = 0; i < (int)saltlen; i++)
0f113f3e
MC
662 printf("%02X", salt[i]);
663 printf("\n");
664 }
ed576acd 665 if (EVP_CIPHER_get_key_length(cipher) > 0) {
0f113f3e 666 printf("key=");
ed576acd 667 for (i = 0; i < EVP_CIPHER_get_key_length(cipher); i++)
0f113f3e
MC
668 printf("%02X", key[i]);
669 printf("\n");
670 }
ed576acd 671 if (EVP_CIPHER_get_iv_length(cipher) > 0) {
0f113f3e 672 printf("iv =");
ed576acd 673 for (i = 0; i < EVP_CIPHER_get_iv_length(cipher); i++)
0f113f3e
MC
674 printf("%02X", iv[i]);
675 printf("\n");
676 }
677 if (printkey == 2) {
678 ret = 0;
679 goto end;
680 }
681 }
682 }
683
684 /* Only encrypt/decrypt as we write the file */
685 if (benc != NULL)
686 wbio = BIO_push(benc, wbio);
687
8ed7bbb4 688 while (BIO_pending(rbio) || !BIO_eof(rbio)) {
0f113f3e
MC
689 inl = BIO_read(rbio, (char *)buff, bsize);
690 if (inl <= 0)
691 break;
7850cc83
E
692 if (!streamable && !BIO_eof(rbio)) { /* do not output data */
693 BIO_printf(bio_err, "Unstreamable cipher mode\n");
694 goto end;
695 }
0f113f3e
MC
696 if (BIO_write(wbio, (char *)buff, inl) != inl) {
697 BIO_printf(bio_err, "error writing output file\n");
698 goto end;
699 }
7850cc83
E
700 if (!streamable)
701 break;
0f113f3e
MC
702 }
703 if (!BIO_flush(wbio)) {
704 BIO_printf(bio_err, "bad decrypt\n");
705 goto end;
706 }
707
708 ret = 0;
709 if (verbose) {
12997aa9
RS
710 BIO_printf(bio_err, "bytes read : %8ju\n", BIO_number_read(in));
711 BIO_printf(bio_err, "bytes written: %8ju\n", BIO_number_written(out));
0f113f3e
MC
712 }
713 end:
714 ERR_print_errors(bio_err);
b548a1f1
RS
715 OPENSSL_free(strbuf);
716 OPENSSL_free(buff);
ca3a82c3
RS
717 BIO_free(in);
718 BIO_free_all(out);
719 BIO_free(benc);
720 BIO_free(b64);
606a417f
RS
721 EVP_MD_free(dgst);
722 EVP_CIPHER_free(cipher);
59d21298 723#ifndef OPENSSL_NO_ZLIB
ca3a82c3 724 BIO_free(bzl);
8931b30d 725#endif
12e96a23 726 BIO_free(bbrot);
caf9317d 727 BIO_free(bzstd);
dd1abd44 728 release_engine(e);
b548a1f1 729 OPENSSL_free(pass);
eee95522 730 return ret;
7e1b7485
RS
731}
732
2b305ab0 733static void show_ciphers(const OBJ_NAME *name, void *arg)
7e1b7485 734{
2b305ab0 735 struct doall_enc_ciphers *dec = (struct doall_enc_ciphers *)arg;
777f1708 736 const EVP_CIPHER *cipher;
7e1b7485
RS
737
738 if (!islower((unsigned char)*name->name))
739 return;
740
777f1708
P
741 /* Filter out ciphers that we cannot use */
742 cipher = EVP_get_cipherbyname(name->name);
f7d2427a
P
743 if (cipher == NULL
744 || (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) != 0
745 || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_XTS_MODE)
777f1708
P
746 return;
747
2b305ab0
P
748 BIO_printf(dec->bio, "-%-25s", name->name);
749 if (++dec->n == 3) {
750 BIO_printf(dec->bio, "\n");
751 dec->n = 0;
7e1b7485 752 } else
2b305ab0 753 BIO_printf(dec->bio, " ");
0f113f3e 754}
d02b48c6 755
d5961b22 756static int set_hex(const char *in, unsigned char *out, int size)
0f113f3e
MC
757{
758 int i, n;
759 unsigned char j;
760
d5961b22 761 i = size * 2;
0f113f3e 762 n = strlen(in);
d5961b22
E
763 if (n > i) {
764 BIO_printf(bio_err, "hex string is too long, ignoring excess\n");
765 n = i; /* ignore exceeding part */
766 } else if (n < i) {
767 BIO_printf(bio_err, "hex string is too short, padding with zero bytes to length\n");
0f113f3e 768 }
d5961b22 769
0f113f3e
MC
770 memset(out, 0, size);
771 for (i = 0; i < n; i++) {
d5961b22 772 j = (unsigned char)*in++;
2fa45e6e 773 if (!isxdigit(j)) {
0f113f3e 774 BIO_printf(bio_err, "non-hex digit\n");
eee95522 775 return 0;
0f113f3e 776 }
14f051a0 777 j = (unsigned char)OPENSSL_hexchar2int(j);
0f113f3e
MC
778 if (i & 1)
779 out[i / 2] |= j;
780 else
781 out[i / 2] = (j << 4);
782 }
eee95522 783 return 1;
0f113f3e 784}