2 * tfcrypt -- high security Threefish encryption tool.
4 * tfcrypt is copyrighted:
5 * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
7 * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 static tfc_byte svctr[TF_BLOCK_SIZE];
33 static void open_log(const char *logfile)
37 if (!strcmp(logfile, "-")) return;
39 fd = open(logfile, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC, 0666);
40 if (fd == -1) xerror(NO, NO, YES, "%s", logfile);
42 if (dup2(fd, 2) == -1) xexit(2);
44 do_statline_dynamic = NO;
47 static int getps_filter(struct getpasswd_state *getps, char chr, size_t pos)
50 getps->retn = ((size_t)-2);
56 static int getps_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
59 getps->retn = ((size_t)-2);
62 if (chr >= '0' && chr <= '9') return 1;
63 if (chr >= 'a' && chr <= 'f') return 1;
64 if (chr >= 'A' && chr <= 'F') return 1;
65 if (chr == '\x7f' || chr == '\x08'
66 || chr == '\x15' || chr == '\x17') return 1;
70 static inline int isctrlchr(int c)
73 if (c >= 0 && c <= 31) return 1;
74 if (c == 127) return 1;
78 static int getps_plain_filter(struct getpasswd_state *getps, char chr, size_t pos)
82 x = getps_filter(getps, chr, pos);
85 if (pos < getps->pwlen && !isctrlchr(chr))
86 write(getps->efd, &chr, sizeof(char));
90 static int getps_plain_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
94 x = getps_hex_filter(getps, chr, pos);
97 if (pos < getps->pwlen && !isctrlchr(chr))
98 write(getps->efd, &chr, sizeof(char));
102 static void make_hint(void *hint, size_t szhint, const void *data, size_t szdata)
104 char t[TF_FROM_BITS(TF_MAX_BITS)];
106 skein(t, TF_MAX_BITS, NULL, data, szdata);
107 xor_shrink(hint, szhint, t, sizeof(t));
108 memset(t, 0, sizeof(t));
111 static void raw_say_hint(void *hint, size_t szhint, const void *data, size_t szdata, const char *prompt)
113 make_hint(hint, szhint, data, szdata);
114 if (prompt) tfc_nfsay(stderr, "%s: ", prompt);
115 mehexdump(hint, szhint, szhint, 1);
116 memset(hint, 0, szhint);
119 static void say_hint(const void *data, size_t szdata, const char *prompt)
121 char t[TF_SIZE_UNIT];
122 raw_say_hint(t, TF_SIZE_UNIT, data, szdata, prompt);
123 /* t[] is erased (automatically) */
126 int main(int argc, char **argv)
130 char *s, *d, *t, *stoi;
135 progname = basename(argv[0]);
137 if (!isatty(2)) do_statline_dynamic = NO;
142 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
143 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
144 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
145 read_defaults(s, YES);
149 if (!strcmp(progname, "iotool")) {
150 do_edcrypt = TFC_DO_PLAIN;
152 ctr_mode = TFC_MODE_PLAIN;
156 while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:Pkzxc:l:qedn:vV:pwE:o:O:S:AmuM:R:Z:WHD:")) != -1) {
159 read_defaults(optarg, NO);
168 if (!strcasecmp(optarg, "show"))
169 counter_opt = TFC_CTR_SHOW;
170 else if (!strcasecmp(optarg, "head"))
171 counter_opt = TFC_CTR_HEAD;
172 else if (!strcasecmp(optarg, "rand"))
173 counter_opt = TFC_CTR_RAND;
174 else if (!strcasecmp(optarg, "zero"))
175 counter_opt = TFC_CTR_ZERO;
176 else if (strchr(optarg, ':')) {
179 counter_opt = TFC_CTR_SSET;
182 s = d = optarg; t = NULL;
183 while ((s = strtok_r(d, ",", &t))) {
190 chr = (char)strtoul(s, &stoi, 16);
191 if (!str_empty(stoi)) continue;
192 x = (size_t)strtoul(ss, &stoi, 10);
193 if (!str_empty(stoi)) continue;
195 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
199 else counter_file = sksum_hashlist_file = optarg;
202 if (!strcasecmp(optarg, "ctr"))
203 ctr_mode = TFC_MODE_CTR;
204 else if (!strcasecmp(optarg, "stream"))
205 ctr_mode = TFC_MODE_STREAM;
206 else if (!strcasecmp(optarg, "cbc"))
207 ctr_mode = TFC_MODE_CBC;
208 else if (!strcasecmp(optarg, "ecb"))
209 ctr_mode = TFC_MODE_ECB;
210 else if (!strcasecmp(optarg, "xts"))
211 ctr_mode = TFC_MODE_XTS;
212 else if (!strcasecmp(optarg, "ocb"))
213 ctr_mode = TFC_MODE_OCB;
214 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
217 do_edcrypt = TFC_DO_PLAIN;
219 ctr_mode = TFC_MODE_PLAIN;
222 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
225 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
228 macbits = strtoul(optarg, &stoi, 10);
229 if (macbits == 0 || !str_empty(stoi) || macbits < 8
230 || macbits > TF_MAX_BITS || macbits % 8)
231 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
234 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
235 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
238 if (!strcasecmp(optarg, "key"))
239 mackey_opt = TFC_MACKEY_RAWKEY;
240 else if (!strcasecmp(optarg, "pwd"))
241 mackey_opt = TFC_MACKEY_PASSWORD;
243 mackey_opt = TFC_MACKEY_FILE;
251 rawkey = TFC_RAWKEY_KEYFILE;
254 rawkey = TFC_RAWKEY_ASKSTR;
257 rawkey = TFC_RAWKEY_ASKHEX;
268 if (maxlen != NOFSIZE) break;
270 maxlen = tfc_humanfsize(optarg, &stoi);
271 if (!str_empty(stoi)) {
272 maxlen = tfc_fnamesize(optarg, YES);
273 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
274 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
275 "%s: invalid count value", optarg);
277 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
278 if (counter_opt == TFC_CTR_HEAD)
279 maxlen += TF_BLOCK_SIZE;
282 overwrite_source = YES;
285 if (!strcmp(optarg, "xall")) {
286 catch_all_errors = YES;
289 if (!strcmp(optarg, "xseek")) {
290 ignore_seek_errors = YES;
293 if (!strcmp(optarg, "exit"))
294 error_action = TFC_ERRACT_EXIT;
295 else if (!strncmp(optarg, "cont", 4))
296 error_action = TFC_ERRACT_CONT;
297 else if (!strcmp(optarg, "sync"))
298 error_action = TFC_ERRACT_SYNC;
299 else if (!strcmp(optarg, "lsync"))
300 error_action = TFC_ERRACT_LSYNC;
301 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
307 s = d = optarg; t = NULL;
308 while ((s = strtok_r(d, ",", &t))) {
310 if (!strcmp(s, "sync"))
311 write_flags |= O_SYNC;
312 else if (!strcmp(s, "trunc"))
313 write_flags |= O_TRUNC;
314 else if (!strcmp(s, "fsync"))
316 else if (!strcmp(s, "pad"))
318 else if (!strcmp(s, "xtime"))
319 do_preserve_time = YES;
320 else if (!strcmp(s, "gibsize"))
321 do_stats_in_gibs = YES;
322 else if (!strcmp(s, "plainstats"))
323 do_statline_dynamic = NO;
324 else if (!strcmp(s, "statless"))
326 else if (!strcmp(s, "norepeat"))
328 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
330 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
331 mac_pw_prompt = s+10;
332 else if (!strcmp(s, "shorthex"))
333 do_full_hexdump = NO;
334 else if (!strcmp(s, "fullkey"))
336 else if (!strcmp(s, "showsecrets"))
338 else if (!strcmp(s, "finished"))
339 show_when_done = YES;
340 else if (!strcmp(s, "pid"))
342 else if (!strncmp(s, "logfile", 7) && *(s+7) == '=')
344 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
346 blksize = (size_t)tfc_humanfsize(s, &stoi);
347 if (!str_empty(stoi)) {
348 blksize = (size_t)tfc_fnamesize(s, YES);
349 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
350 if (blksize == NOSIZE) xerror(NO, YES, YES,
351 "%s: invalid block size value", s);
353 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
354 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
355 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
356 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
357 "%s: block size exceeds %u bytes",
358 s, TFC_U(TFC_BLKSIZE));
360 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
362 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
363 if (!str_empty(stoi)) {
364 xtsblocks = (size_t)tfc_fnamesize(s, YES);
365 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
366 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
367 "%s: invalid blocks per xts block value", s);
369 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
370 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
371 "%s: nr of blocks per xts block is not round to %u bytes",
372 s, TFC_U(TFC_BLKSIZE));
373 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
374 "%s: nr of blocks per xts block exceeds %u bytes",
375 s, TFC_U(TFC_BLKSIZE));
377 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
379 iseek = tfc_humanfsize(s, &stoi);
380 if (!str_empty(stoi)) {
381 iseek = tfc_fnamesize(s, YES);
382 iseek = tfc_modifysize(iseek, strchr(s, ':'));
383 if (iseek == NOFSIZE) xerror(NO, YES, YES,
384 "%s: invalid iseek value", s);
386 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
387 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
389 "%s: not round to TF block size "
391 s, TFC_U(TF_BLOCK_SIZE));
392 iseek_blocks = iseek / TF_BLOCK_SIZE;
394 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
396 iseek = tfc_humanfsize(s, &stoi);
397 if (!str_empty(stoi)) {
398 iseek = tfc_fnamesize(s, YES);
399 iseek = tfc_modifysize(iseek, strchr(s, ':'));
400 if (iseek == NOFSIZE) xerror(NO, YES, YES,
401 "%s: invalid ixseek value", s);
403 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
405 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
407 iseek_blocks = tfc_humanfsize(s, &stoi);
408 if (!str_empty(stoi)) {
409 iseek_blocks = tfc_fnamesize(s, YES);
410 if (iseek_blocks == NOFSIZE)
412 "%s: invalid ictr value", s);
413 iseek_blocks /= TF_BLOCK_SIZE;
414 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
416 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
418 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
420 iseek_blocks = tfc_humanfsize(s, &stoi);
421 if (!str_empty(stoi)) {
422 iseek_blocks = tfc_fnamesize(s, YES);
423 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
424 if (iseek_blocks == NOFSIZE)
426 "%s: invalid ixctr value", s);
428 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
429 if (iseek_blocks % TF_BLOCK_SIZE)
431 "%s: not round to TF block size "
432 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
433 iseek_blocks /= TF_BLOCK_SIZE;
435 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
437 oseek = tfc_humanfsize(s, &stoi);
438 if (!str_empty(stoi)) {
439 oseek = tfc_fnamesize(s, YES);
440 oseek = tfc_modifysize(oseek, strchr(s, ':'));
441 if (oseek == NOFSIZE) xerror(NO, YES, YES,
442 "%s: invalid oseek value", s);
444 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
446 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
448 maxlen = tfc_humanfsize(s, &stoi);
449 if (!str_empty(stoi)) {
450 maxlen = tfc_fnamesize(s, YES);
451 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
452 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
453 "%s: invalid count value", s);
455 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
456 if (counter_opt == TFC_CTR_HEAD)
457 maxlen += TF_BLOCK_SIZE;
459 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
461 if (!strcmp(s, "tail")) {
462 do_ftrunc = TFC_FTRUNC_TAIL;
463 ftrunc_dfd = NOFSIZE;
466 do_ftrunc = TFC_DO_FTRUNC;
467 ftrunc_dfd = tfc_humanfsize(s, &stoi);
468 if (!str_empty(stoi)) {
469 ftrunc_dfd = tfc_fnamesize(s, YES);
470 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
471 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
472 "%s: invalid ftrunc value", s);
474 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
477 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
479 maxkeylen = tfc_humanfsize(s, &stoi);
480 if (!str_empty(stoi)) {
481 maxkeylen = tfc_fnamesize(s, YES);
482 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
483 if (maxkeylen == NOSIZE)
485 "%s: invalid key length value", s);
487 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
489 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
491 keyoffset = tfc_humanfsize(s, &stoi);
492 if (!str_empty(stoi)) {
493 keyoffset = tfc_fnamesize(s, YES);
494 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
495 if (keyoffset == NOFSIZE)
497 "%s: invalid key offset value", s);
499 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
501 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
503 ctrsz = tfc_humanfsize(s, &stoi);
504 if (!str_empty(stoi)) {
505 ctrsz = (size_t)tfc_fnamesize(s, YES);
506 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
509 "%s: invalid counter length value", s);
511 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
512 if (ctrsz > TF_BLOCK_SIZE)
513 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
515 else xerror(NO, YES, YES, "invalid option %s", s);
519 do_mac = TFC_MAC_SIGN;
520 if (strcasecmp(optarg, "mac") != 0)
521 do_mac_file = optarg;
524 do_mac = TFC_MAC_VRFY;
525 if (!strcasecmp(optarg, "drop"))
526 do_mac = TFC_MAC_DROP;
527 else if (strcasecmp(optarg, "mac") != 0)
528 do_mac_file = optarg;
532 if (do_mac != TFC_MAC_VRFY)
533 xerror(NO, YES, YES, "signature source was not specified");
534 do_mac = TFC_MAC_JUST_VRFY;
535 if (c == 'u') do_mac = TFC_MAC_JUST_VRFY2;
539 if (maxlen != NOFSIZE) {
540 if (c == 'Z') genzero_nr_bytes = maxlen;
541 else genrandom_nr_bytes = maxlen;
545 if (!strcasecmp(optarg, "cbs"))
547 else if (!strcasecmp(optarg, "ks"))
548 t = TF_FROM_BITS(TFC_KEY_BITS);
549 else if (!strcasecmp(optarg, "xks"))
550 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
551 else if (!strcasecmp(optarg, "iobs"))
554 t = tfc_humanfsize(optarg, &stoi);
555 if (!str_empty(stoi)) {
556 t = tfc_fnamesize(optarg, NO);
557 t = tfc_modifysize(t, strchr(optarg, ':'));
559 else t = tfc_modifysize(t, strchr(optarg, ':'));
561 if (c == 'Z') genzero_nr_bytes = maxlen = t;
562 else genrandom_nr_bytes = maxlen = t;
566 do_preserve_time = YES;
569 do_outfmt = TFC_OUTFMT_B64;
572 do_outfmt = TFC_OUTFMT_RAW;
575 do_outfmt = TFC_OUTFMT_HEX;
581 do_full_hexdump = NO;
588 td = strtod(optarg, &stoi);
589 status_timer = TFC_DTOUSECS(td);
590 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
598 if (!strcmp(progname, "tfbench")) {
599 if (!*(argv+optind)) usage();
601 td = strtod(*(argv+optind), &stoi);
602 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
604 "%s: invalid benchmark time in seconds", *(argv+optind));
605 bench_timer = TFC_DTOUSECS(td);
606 do_benchmark(bench_timer, td);
608 if (genrandom_nr_bytes) {
609 ctr_mode = TFC_MODE_STREAM;
610 do_edcrypt = TFC_DO_ENCRYPT;
611 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
613 if (genzero_nr_bytes) {
614 ctr_mode = TFC_MODE_PLAIN;
615 do_edcrypt = TFC_DO_PLAIN;
616 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
619 if (rawkey && password)
620 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
621 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
622 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
623 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
624 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
625 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
626 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
627 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
628 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
629 if (overwrite_source && counter_opt == TFC_CTR_RAND)
630 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
631 if (ctr_mode == TFC_MODE_PLAIN
632 && (do_edcrypt || do_mac || rawkey
633 || mackey_opt || counter_opt || counter_file))
634 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
642 memset(tfc_salt, 0, TFC_MAX_SALT);
644 if (!strcasecmp(saltf, "disable")) goto _nosalt;
646 if (!strcmp(saltf, "-")) saltfd = 0;
647 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
648 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
649 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
650 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
656 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
660 if (!strcmp(mackeyf, "-")) mkfd = 0;
661 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
662 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
664 skein_init(&sk, TFC_KEY_BITS);
671 lrem = lblock = sizeof(tmpdata);
672 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
673 _mkragain: lio = xread(mkfd, pblk, lrem);
674 if (lio == 0 && do_stop == NO) do_stop = YES;
675 if (lio != NOSIZE) ldone += lio;
677 if (errno != EIO && catch_all_errors != YES)
678 xerror(NO, NO, NO, "%s", mackeyf);
679 switch (error_action) {
680 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
681 case TFC_ERRACT_SYNC:
682 case TFC_ERRACT_LSYNC:
683 xerror(YES, NO, NO, "%s", mackeyf);
684 lio = ldone = lrem = lblock;
685 memset(tmpdata, 0, lio);
686 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
687 else lseek(mkfd, rdpos + lio, SEEK_SET);
689 default: xerror(NO, NO, NO, "%s", mackeyf); break;
692 if (lio && lio < lrem) {
698 skein_update(&sk, tmpdata, ldone);
701 skein_final(mackey, &sk);
705 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
706 memset(&getps, 0, sizeof(struct getpasswd_state));
707 getps.fd = getps.efd = -1;
708 getps.passwd = pwdask;
709 getps.pwlen = sizeof(pwdask)-1;
710 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
711 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
712 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
713 getps.flags = GETP_WAITFILL;
714 n = xgetpasswd(&getps);
715 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
716 if (n == ((size_t)-2)) xexit(1);
717 if (verbose) say_hint(pwdask, n, "MAC password hint");
718 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
722 if ((strlen(progname) <= 9)
723 && ((!strcmp(progname, "sksum"))
724 || ((!memcmp(progname, "sk", 2))
725 && (!memcmp(progname+3, "sum", 3)
726 || !memcmp(progname+4, "sum", 3)
727 || !memcmp(progname+5, "sum", 3)
728 || !memcmp(progname+6, "sum", 3)))))
729 do_sksum(progname, argv+optind);
730 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
735 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
736 if (!strcmp(argv[idx], "-")) kfd = 0;
737 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
738 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
740 lio = strnlen(argv[idx], PATH_MAX);
741 memset(argv[idx], '*', lio);
748 if (do_full_key == NO && tweakf) {
751 if (!strcmp(tweakf, "-")) twfd = 0;
752 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
753 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
754 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
755 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
756 if (ldone < TF_TWEAK_SIZE)
757 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
764 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
766 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
767 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
768 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
770 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
772 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
773 if (verbose) tfc_esay("%s: disabling signature verification on "
774 "requested partial decryption.", tfc_format_pid(progname));
778 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
779 maxlen = tfc_fdsize(sfd);
780 if (maxlen == NOFSIZE)
782 "Cannot verify embedded MAC with non-seekable source!");
783 maxlen -= TF_FROM_BITS(macbits);
785 srcfname = argv[idx];
789 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
790 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
792 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
793 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
795 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
800 if (!strcmp(counter_file, "-")) ctrfd = 0;
801 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
802 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
803 lio = xread(ctrfd, ctr, ctrsz);
804 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
805 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
808 else if (counter_opt == TFC_CTR_HEAD) {
811 lrem = lblock = ctrsz;
812 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
813 _ctrragain: lio = xread(sfd, pblk, lrem);
814 if (lio != NOSIZE) ldone += lio;
816 if (errno != EIO && catch_all_errors != YES)
817 xerror(NO, NO, NO, "%s", srcfname);
818 switch (error_action) {
819 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
820 case TFC_ERRACT_SYNC:
821 case TFC_ERRACT_LSYNC:
822 xerror(YES, NO, NO, "%s", srcfname);
823 lio = ldone = lrem = lblock;
825 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
826 else lseek(sfd, rdpos + lio, SEEK_SET);
828 default: xerror(NO, NO, NO, "%s", srcfname); break;
831 if (lio && lio < lrem) {
836 total_processed_src += ldone;
841 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
843 if (lseek(sfd, iseek, SEEK_SET) == -1)
844 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
847 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
849 if (verbose) tfc_esay("%s: hashing password", tfc_format_pid(progname));
851 if (rawkey == TFC_RAWKEY_KEYFILE) {
852 tfc_yesno xtskeyset = NO;
856 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
857 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
858 _keyragain: lio = xread(kfd, pblk, lrem);
859 if (lio != NOSIZE) ldone += lio;
861 if (errno != EIO && catch_all_errors != YES)
862 xerror(NO, NO, NO, "reading key");
863 switch (error_action) {
864 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
865 case TFC_ERRACT_SYNC:
866 case TFC_ERRACT_LSYNC:
867 xerror(YES, NO, NO, "reading key");
868 lio = ldone = lrem = lblock;
870 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
871 else lseek(kfd, rdpos + lio, SEEK_SET);
873 default: xerror(NO, NO, NO, "reading key"); break;
876 if (lio && lio < lrem) {
881 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
883 if (ctr_mode == TFC_MODE_XTS) {
884 if (xtskeyset == NO) {
891 else if (rawkey == TFC_RAWKEY_ASKSTR) {
892 tfc_yesno xtskeyset = NO;
894 pblk = key; n = sizeof(key);
895 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
896 getps.fd = getps.efd = -1;
897 getps.passwd = (char *)pblk;
899 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
900 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
901 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
902 getps.flags = GETP_WAITFILL;
903 n = xgetpasswd(&getps);
904 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
905 if (n == ((size_t)-2)) xexit(1);
906 if (verbose) say_hint(pblk, n, "Raw string key hint");
907 if (ctr_mode == TFC_MODE_XTS) {
908 if (xtskeyset == NO) {
909 pblk = xtskey; n = sizeof(xtskey);
915 else if (rawkey == TFC_RAWKEY_ASKHEX) {
916 tfc_yesno xtskeyset = NO;
920 memset(&getps, 0, sizeof(struct getpasswd_state));
921 getps.fd = getps.efd = -1;
922 getps.passwd = pwdask;
923 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
924 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
925 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
926 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
927 getps.flags = GETP_WAITFILL;
928 n = xgetpasswd(&getps);
929 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
930 if (n == ((size_t)-2)) xexit(1);
932 tfc_esay("Please input even number of hex digits!");
933 goto _rawkey_hex_again;
935 hex2bin(pblk, pwdask);
936 memset(pwdask, 0, sizeof(pwdask));
937 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
938 if (ctr_mode == TFC_MODE_XTS) {
939 if (xtskeyset == NO) {
942 goto _rawkey_hex_again;
947 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
948 getps.fd = getps.efd = -1;
949 getps.passwd = pwdask;
950 getps.pwlen = sizeof(pwdask)-1;
951 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
952 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
953 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
954 getps.flags = GETP_WAITFILL;
955 n = xgetpasswd(&getps);
956 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
957 if (n == ((size_t)-2)) xexit(1);
958 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
959 getps.fd = getps.efd = -1;
960 getps.passwd = pwdagain;
961 getps.pwlen = sizeof(pwdagain)-1;
962 getps.echo = "Enter it again: ";
963 getps.flags = GETP_WAITFILL;
964 n = xgetpasswd(&getps);
965 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
966 if (n == ((size_t)-2)) xexit(1);
967 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
968 tfc_esay("Passwords are different, try again");
972 if (verbose) say_hint(pwdask, n, "Password hint");
973 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
974 memset(pwdask, 0, sizeof(pwdask));
975 memset(pwdagain, 0, sizeof(pwdagain));
978 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
979 xerror(NO, NO, YES, "hashing key");
983 if (tfc_saltsz > 0) {
984 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
985 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
987 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
988 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
989 memset(tfc_salt, 0, TFC_MAX_SALT);
992 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
993 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
998 tfc_yesno xtskeyset = NO;
1001 if (!strcmp(genkeyf, "-")) krfd = 1;
1002 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1003 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
1004 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
1005 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
1006 if (verbose && xtskeyset == NO) {
1007 tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1008 tfc_esay("%s: rawkey written to %s.", tfc_format_pid(progname), genkeyf);
1009 tfc_esay("%s: Have a nice day!", tfc_format_pid(progname));
1012 if (ctr_mode == TFC_MODE_XTS) {
1013 if (xtskeyset == NO) {
1025 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
1026 if (verbose) tfc_esay("%s: disabling signature verification on "
1027 "requested partial decryption.", tfc_format_pid(progname));
1032 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1033 if (ctr_mode < TFC_MODE_OCB) {
1034 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1035 "will be slower.", tfc_format_pid(progname));
1036 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1037 else skein_init(&sk, macbits);
1041 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1042 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1046 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1047 if (do_full_key == NO) {
1048 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1049 tf_tweak_set(key, tweak);
1051 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1053 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1055 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1056 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1057 if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1059 if (counter_opt == TFC_CTR_SHOW) {
1060 switch (do_outfmt) {
1061 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1062 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1063 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1066 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1073 if (verbose) tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1075 if (overwrite_source && srcfname) argv[idx] = srcfname;
1079 if (!strcmp(argv[idx], "-")) dfd = 1;
1080 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
1082 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1083 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
1085 dstfname = argv[idx];
1090 if (lseek(dfd, oseek, SEEK_SET) == -1)
1091 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1094 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1095 memset(&sigact, 0, sizeof(sigact));
1096 sigact.sa_flags = SA_RESTART;
1097 sigact.sa_handler = print_crypt_status;
1098 sigaction(SIGUSR1, &sigact, NULL);
1099 sigaction(SIGALRM, &sigact, NULL);
1100 if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
1101 sigact.sa_handler = change_status_width;
1102 sigaction(SIGQUIT, &sigact, NULL);
1103 sigact.sa_handler = change_status_timer;
1104 sigaction(SIGUSR2, &sigact, NULL);
1106 sigact.sa_handler = print_crypt_status;
1107 sigaction(SIGINT, &sigact, NULL);
1108 sigaction(SIGTERM, &sigact, NULL);
1109 sigaction(SIGTSTP, &sigact, NULL);
1112 sigact.sa_handler = exit_sigterm;
1113 sigaction(SIGINT, &sigact, NULL);
1114 sigaction(SIGTERM, &sigact, NULL);
1115 sigact.sa_handler = handle_sigtstp;
1116 sigaction(SIGTSTP, &sigact, NULL);
1118 memset(&sigact, 0, sizeof(struct sigaction));
1120 tfc_getcurtime(&delta_time);
1123 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1127 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1128 if (lio != NOSIZE) ldone += lio;
1129 else xerror(NO, NO, NO, "%s", dstfname);
1130 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1136 total_processed_dst += ldone;
1137 delta_processed += ldone;
1140 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1142 if (do_mac == TFC_MAC_JUST_VRFY2) {
1143 rwd = tfc_fdgetpos(sfd);
1144 if (rwd == NOFSIZE) {
1145 tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", tfc_format_pid(progname));
1146 do_mac = TFC_MAC_VRFY;
1148 goto _nodecrypt_again_vrfy2;
1150 _decrypt_again_vrfy2:
1151 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1152 xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1154 total_processed_src = rwd;
1155 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1156 memset(svctr, 0, TF_BLOCK_SIZE);
1159 _nodecrypt_again_vrfy2:
1166 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1167 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1168 _ragain: lio = xread(sfd, pblk, lrem);
1169 if (lio == 0) do_stop = YES;
1170 if (lio != NOSIZE) ldone += lio;
1172 if (errno != EIO && catch_all_errors != YES)
1173 xerror(NO, NO, NO, "%s", srcfname);
1174 switch (error_action) {
1175 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1176 case TFC_ERRACT_SYNC:
1177 case TFC_ERRACT_LSYNC:
1178 xerror(YES, NO, NO, "%s", srcfname);
1179 lio = ldone = lrem = lblock;
1180 memset(srcblk, 0, lio);
1181 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1182 else lseek(sfd, rdpos + lio, SEEK_SET);
1184 default: xerror(NO, NO, NO, "%s", srcfname); break;
1187 if (lio && lio < lrem) {
1192 total_processed_src += ldone;
1194 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1195 size_t orig = ldone;
1196 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1197 if (ldone > blksize) ldone = blksize;
1198 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1201 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1202 skein_update(&sk, srcblk, ldone);
1204 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1205 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1206 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1207 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1208 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1209 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1210 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1211 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1212 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1213 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1214 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1215 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1216 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1217 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1219 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1220 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1221 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1222 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1224 else if (ctr_mode == TFC_MODE_PLAIN)
1225 memcpy(dstblk, srcblk, ldone);
1227 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1228 skein_update(&sk, dstblk, ldone);
1229 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1234 _wagain: lio = xwrite(dfd, pblk, lrem);
1235 if (lio != NOSIZE) ldone += lio;
1236 else xerror(NO, NO, NO, "%s", dstfname);
1237 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1243 total_written_dst += ldone;
1244 _nowrite: total_processed_dst += ldone;
1245 delta_processed += ldone;
1247 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1254 if (do_mac >= TFC_MAC_VRFY) {
1258 lrem = lblock = TF_FROM_BITS(macbits);
1259 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1260 _macragain: lio = xread(sfd, pblk, lrem);
1261 if (lio != NOSIZE) ldone += lio;
1263 if (errno != EIO && catch_all_errors != YES)
1264 xerror(NO, NO, NO, "%s", srcfname);
1265 switch (error_action) {
1266 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1267 case TFC_ERRACT_SYNC:
1268 case TFC_ERRACT_LSYNC:
1269 xerror(YES, NO, NO, "%s", srcfname);
1270 lio = ldone = lrem = lblock;
1271 memset(macvrfy, 0, lio);
1272 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1273 else lseek(sfd, rdpos + lio, SEEK_SET);
1275 default: xerror(NO, NO, NO, "%s", srcfname); break;
1278 if (lio && lio < lrem) {
1283 total_processed_src += ldone;
1288 if (!strcmp(do_mac_file, "-")) mfd = 0;
1289 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1290 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1291 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1292 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1293 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1294 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1295 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1296 lio = TF_FROM_BITS(macbits);
1297 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1299 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1303 if (ldone < TF_FROM_BITS(macbits)) {
1304 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1305 "not verifying", tfc_format_pid(progname), ldone);
1310 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1311 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1313 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1314 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1315 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1316 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1317 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1318 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1320 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1322 tfc_esay("%s: signature is good", tfc_format_pid(progname));
1324 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1325 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1328 if (do_mac == TFC_MAC_JUST_VRFY2) {
1329 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", tfc_format_pid(progname));
1330 do_mac = TFC_MAC_DROP;
1331 goto _decrypt_again_vrfy2;
1336 tfc_esay("%s: signature is BAD: "
1337 "wrong password, key, mode, or file is not signed", tfc_format_pid(progname));
1338 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", tfc_format_pid(progname));
1343 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1344 memset(macresult, 0, sizeof(macresult));
1345 memset(tmpdata, 0, sizeof(tmpdata));
1348 else if (do_mac == TFC_MAC_SIGN) {
1349 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1350 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1352 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1353 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1354 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1355 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1356 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1357 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1358 memset(macresult, 0, sizeof(macresult));
1362 lio = lrem = TF_FROM_BITS(macbits);
1364 _macwagain: lio = xwrite(dfd, pblk, lrem);
1365 if (lio != NOSIZE) ldone += lio;
1366 else xerror(NO, NO, NO, "%s", dstfname);
1367 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1373 total_processed_dst += ldone;
1374 delta_processed += ldone;
1379 if (!strcmp(do_mac_file, "-")) mfd = 1;
1380 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1381 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1382 if (do_outfmt == TFC_OUTFMT_B64) {
1383 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1384 memset(tmpdata, 0, TFC_TMPSIZE);
1385 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1386 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1387 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1389 tmpdata[lrem] = '\n';
1392 lio = xwrite(mfd, tmpdata, lrem);
1394 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1395 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1396 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1400 memset(macvrfy, 0, sizeof(macvrfy));
1401 memset(macresult, 0, sizeof(macresult));
1402 memset(tmpdata, 0, sizeof(tmpdata));
1405 if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(0);