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;
134 progname = basename(argv[0]);
136 if (!isatty(2)) do_statline_dynamic = NO;
141 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
142 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
143 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
144 read_defaults(s, YES);
148 if (!strcmp(progname, "iotool")) {
149 do_edcrypt = TFC_DO_PLAIN;
151 ctr_mode = TFC_MODE_PLAIN;
155 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) {
158 read_defaults(optarg, NO);
167 if (!strcasecmp(optarg, "show"))
168 counter_opt = TFC_CTR_SHOW;
169 else if (!strcasecmp(optarg, "head"))
170 counter_opt = TFC_CTR_HEAD;
171 else if (!strcasecmp(optarg, "rand"))
172 counter_opt = TFC_CTR_RAND;
173 else if (!strcasecmp(optarg, "zero"))
174 counter_opt = TFC_CTR_ZERO;
175 else if (strchr(optarg, ':')) {
178 counter_opt = TFC_CTR_SSET;
181 s = d = optarg; t = NULL;
182 while ((s = strtok_r(d, ",", &t))) {
189 chr = (char)strtoul(s, &stoi, 16);
190 if (!str_empty(stoi)) continue;
191 x = (size_t)strtoul(ss, &stoi, 10);
192 if (!str_empty(stoi)) continue;
194 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
198 else counter_file = sksum_hashlist_file = optarg;
201 if (!strcasecmp(optarg, "ctr"))
202 ctr_mode = TFC_MODE_CTR;
203 else if (!strcasecmp(optarg, "stream"))
204 ctr_mode = TFC_MODE_STREAM;
205 else if (!strcasecmp(optarg, "cbc"))
206 ctr_mode = TFC_MODE_CBC;
207 else if (!strcasecmp(optarg, "ecb"))
208 ctr_mode = TFC_MODE_ECB;
209 else if (!strcasecmp(optarg, "xts"))
210 ctr_mode = TFC_MODE_XTS;
211 else if (!strcasecmp(optarg, "ocb"))
212 ctr_mode = TFC_MODE_OCB;
213 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
216 do_edcrypt = TFC_DO_PLAIN;
218 ctr_mode = TFC_MODE_PLAIN;
221 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
224 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
227 macbits = strtoul(optarg, &stoi, 10);
228 if (macbits == 0 || !str_empty(stoi) || macbits < 8
229 || macbits > TF_MAX_BITS || macbits % 8)
230 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
233 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
234 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
237 if (!strcasecmp(optarg, "key"))
238 mackey_opt = TFC_MACKEY_RAWKEY;
239 else if (!strcasecmp(optarg, "pwd"))
240 mackey_opt = TFC_MACKEY_PASSWORD;
242 mackey_opt = TFC_MACKEY_FILE;
250 rawkey = TFC_RAWKEY_KEYFILE;
253 rawkey = TFC_RAWKEY_ASKSTR;
256 rawkey = TFC_RAWKEY_ASKHEX;
267 if (maxlen != NOFSIZE) break;
269 maxlen = tfc_humanfsize(optarg, &stoi);
270 if (!str_empty(stoi)) {
271 maxlen = tfc_fnamesize(optarg, YES);
272 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
273 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
274 "%s: invalid count value", optarg);
276 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
277 if (counter_opt == TFC_CTR_HEAD)
278 maxlen += TF_BLOCK_SIZE;
281 overwrite_source = YES;
284 if (!strcmp(optarg, "xall")) {
285 catch_all_errors = YES;
288 if (!strcmp(optarg, "xseek")) {
289 ignore_seek_errors = YES;
292 if (!strcmp(optarg, "exit"))
293 error_action = TFC_ERRACT_EXIT;
294 else if (!strncmp(optarg, "cont", 4))
295 error_action = TFC_ERRACT_CONT;
296 else if (!strcmp(optarg, "sync"))
297 error_action = TFC_ERRACT_SYNC;
298 else if (!strcmp(optarg, "lsync"))
299 error_action = TFC_ERRACT_LSYNC;
300 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
306 s = d = optarg; t = NULL;
307 while ((s = strtok_r(d, ",", &t))) {
309 if (!strcmp(s, "sync"))
310 write_flags |= O_SYNC;
311 else if (!strcmp(s, "trunc"))
312 write_flags |= O_TRUNC;
313 else if (!strcmp(s, "fsync"))
315 else if (!strcmp(s, "pad"))
317 else if (!strcmp(s, "xtime"))
318 do_preserve_time = YES;
319 else if (!strcmp(s, "gibsize"))
320 do_stats_in_gibs = YES;
321 else if (!strcmp(s, "plainstats"))
322 do_statline_dynamic = NO;
323 else if (!strcmp(s, "statless"))
325 else if (!strcmp(s, "norepeat"))
327 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
329 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
330 mac_pw_prompt = s+10;
331 else if (!strcmp(s, "shorthex"))
332 do_full_hexdump = NO;
333 else if (!strcmp(s, "fullkey"))
335 else if (!strcmp(s, "showsecrets"))
337 else if (!strcmp(s, "finished"))
338 show_when_done = YES;
339 else if (!strncmp(s, "logfile", 7) && *(s+7) == '=')
341 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
343 blksize = (size_t)tfc_humanfsize(s, &stoi);
344 if (!str_empty(stoi)) {
345 blksize = (size_t)tfc_fnamesize(s, YES);
346 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
347 if (blksize == NOSIZE) xerror(NO, YES, YES,
348 "%s: invalid block size value", s);
350 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
351 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
352 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
353 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
354 "%s: block size exceeds %u bytes",
355 s, TFC_U(TFC_BLKSIZE));
357 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
359 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
360 if (!str_empty(stoi)) {
361 xtsblocks = (size_t)tfc_fnamesize(s, YES);
362 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
363 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
364 "%s: invalid blocks per xts block value", s);
366 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
367 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
368 "%s: nr of blocks per xts block is not round to %u bytes",
369 s, TFC_U(TFC_BLKSIZE));
370 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
371 "%s: nr of blocks per xts block exceeds %u bytes",
372 s, TFC_U(TFC_BLKSIZE));
374 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
376 iseek = tfc_humanfsize(s, &stoi);
377 if (!str_empty(stoi)) {
378 iseek = tfc_fnamesize(s, YES);
379 iseek = tfc_modifysize(iseek, strchr(s, ':'));
380 if (iseek == NOFSIZE) xerror(NO, YES, YES,
381 "%s: invalid iseek value", s);
383 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
384 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
386 "%s: not round to TF block size "
388 s, TFC_U(TF_BLOCK_SIZE));
389 iseek_blocks = iseek / TF_BLOCK_SIZE;
391 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
393 iseek = tfc_humanfsize(s, &stoi);
394 if (!str_empty(stoi)) {
395 iseek = tfc_fnamesize(s, YES);
396 iseek = tfc_modifysize(iseek, strchr(s, ':'));
397 if (iseek == NOFSIZE) xerror(NO, YES, YES,
398 "%s: invalid ixseek value", s);
400 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
402 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
404 iseek_blocks = tfc_humanfsize(s, &stoi);
405 if (!str_empty(stoi)) {
406 iseek_blocks = tfc_fnamesize(s, YES);
407 if (iseek_blocks == NOFSIZE)
409 "%s: invalid ictr value", s);
410 iseek_blocks /= TF_BLOCK_SIZE;
411 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
413 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
415 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
417 iseek_blocks = tfc_humanfsize(s, &stoi);
418 if (!str_empty(stoi)) {
419 iseek_blocks = tfc_fnamesize(s, YES);
420 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
421 if (iseek_blocks == NOFSIZE)
423 "%s: invalid ixctr value", s);
425 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
426 if (iseek_blocks % TF_BLOCK_SIZE)
428 "%s: not round to TF block size "
429 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
430 iseek_blocks /= TF_BLOCK_SIZE;
432 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
434 oseek = tfc_humanfsize(s, &stoi);
435 if (!str_empty(stoi)) {
436 oseek = tfc_fnamesize(s, YES);
437 oseek = tfc_modifysize(oseek, strchr(s, ':'));
438 if (oseek == NOFSIZE) xerror(NO, YES, YES,
439 "%s: invalid oseek value", s);
441 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
443 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
445 maxlen = tfc_humanfsize(s, &stoi);
446 if (!str_empty(stoi)) {
447 maxlen = tfc_fnamesize(s, YES);
448 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
449 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
450 "%s: invalid count value", s);
452 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
453 if (counter_opt == TFC_CTR_HEAD)
454 maxlen += TF_BLOCK_SIZE;
456 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
458 if (!strcmp(s, "tail")) {
459 do_ftrunc = TFC_FTRUNC_TAIL;
460 ftrunc_dfd = NOFSIZE;
463 do_ftrunc = TFC_DO_FTRUNC;
464 ftrunc_dfd = tfc_humanfsize(s, &stoi);
465 if (!str_empty(stoi)) {
466 ftrunc_dfd = tfc_fnamesize(s, YES);
467 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
468 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
469 "%s: invalid ftrunc value", s);
471 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
474 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
476 maxkeylen = tfc_humanfsize(s, &stoi);
477 if (!str_empty(stoi)) {
478 maxkeylen = tfc_fnamesize(s, YES);
479 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
480 if (maxkeylen == NOSIZE)
482 "%s: invalid key length value", s);
484 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
486 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
488 keyoffset = tfc_humanfsize(s, &stoi);
489 if (!str_empty(stoi)) {
490 keyoffset = tfc_fnamesize(s, YES);
491 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
492 if (keyoffset == NOFSIZE)
494 "%s: invalid key offset value", s);
496 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
498 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
500 ctrsz = tfc_humanfsize(s, &stoi);
501 if (!str_empty(stoi)) {
502 ctrsz = (size_t)tfc_fnamesize(s, YES);
503 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
506 "%s: invalid counter length value", s);
508 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
509 if (ctrsz > TF_BLOCK_SIZE)
510 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
512 else xerror(NO, YES, YES, "invalid option %s", s);
516 do_mac = TFC_MAC_SIGN;
517 if (strcasecmp(optarg, "mac") != 0)
518 do_mac_file = optarg;
521 do_mac = TFC_MAC_VRFY;
522 if (!strcasecmp(optarg, "drop"))
523 do_mac = TFC_MAC_DROP;
524 else if (strcasecmp(optarg, "mac") != 0)
525 do_mac_file = optarg;
529 if (do_mac != TFC_MAC_VRFY)
530 xerror(NO, YES, YES, "signature source was not specified");
531 do_mac = TFC_MAC_JUST_VRFY;
532 if (c == 'u') do_mac = TFC_MAC_JUST_VRFY2;
536 if (maxlen != NOFSIZE) {
537 if (c == 'Z') genzero_nr_bytes = maxlen;
538 else genrandom_nr_bytes = maxlen;
542 if (!strcasecmp(optarg, "cbs"))
544 else if (!strcasecmp(optarg, "ks"))
545 t = TF_FROM_BITS(TFC_KEY_BITS);
546 else if (!strcasecmp(optarg, "xks"))
547 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
548 else if (!strcasecmp(optarg, "iobs"))
551 t = tfc_humanfsize(optarg, &stoi);
552 if (!str_empty(stoi)) {
553 t = tfc_fnamesize(optarg, NO);
554 t = tfc_modifysize(t, strchr(optarg, ':'));
556 else t = tfc_modifysize(t, strchr(optarg, ':'));
558 if (c == 'Z') genzero_nr_bytes = maxlen = t;
559 else genrandom_nr_bytes = maxlen = t;
563 do_preserve_time = YES;
566 do_outfmt = TFC_OUTFMT_B64;
569 do_outfmt = TFC_OUTFMT_RAW;
572 do_outfmt = TFC_OUTFMT_HEX;
577 do_full_hexdump = NO;
584 td = strtod(optarg, &stoi);
585 status_timer = TFC_DTOUSECS(td);
586 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
594 if (!strcmp(progname, "tfbench")) {
595 if (!*(argv+optind)) usage();
597 td = strtod(*(argv+optind), &stoi);
598 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
600 "%s: invalid benchmark time in seconds", *(argv+optind));
601 bench_timer = TFC_DTOUSECS(td);
602 do_benchmark(bench_timer, td);
604 if (genrandom_nr_bytes) {
605 ctr_mode = TFC_MODE_STREAM;
606 do_edcrypt = TFC_DO_ENCRYPT;
607 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
609 if (genzero_nr_bytes) {
610 ctr_mode = TFC_MODE_PLAIN;
611 do_edcrypt = TFC_DO_PLAIN;
612 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
615 if (rawkey && password)
616 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
617 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
618 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
619 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
620 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
621 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
622 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
623 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
624 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
625 if (overwrite_source && counter_opt == TFC_CTR_RAND)
626 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
627 if (ctr_mode == TFC_MODE_PLAIN
628 && (do_edcrypt || do_mac || rawkey
629 || mackey_opt || counter_opt || counter_file))
630 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
638 memset(tfc_salt, 0, TFC_MAX_SALT);
640 if (!strcasecmp(saltf, "disable")) goto _nosalt;
642 if (!strcmp(saltf, "-")) saltfd = 0;
643 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
644 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
645 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
646 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
652 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
656 if (!strcmp(mackeyf, "-")) mkfd = 0;
657 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
658 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
660 skein_init(&sk, TFC_KEY_BITS);
667 lrem = lblock = sizeof(tmpdata);
668 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
669 _mkragain: lio = xread(mkfd, pblk, lrem);
670 if (lio == 0 && do_stop == NO) do_stop = YES;
671 if (lio != NOSIZE) ldone += lio;
673 if (errno != EIO && catch_all_errors != YES)
674 xerror(NO, NO, NO, "%s", mackeyf);
675 switch (error_action) {
676 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
677 case TFC_ERRACT_SYNC:
678 case TFC_ERRACT_LSYNC:
679 xerror(YES, NO, NO, "%s", mackeyf);
680 lio = ldone = lrem = lblock;
681 memset(tmpdata, 0, lio);
682 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
683 else lseek(mkfd, rdpos + lio, SEEK_SET);
685 default: xerror(NO, NO, NO, "%s", mackeyf); break;
688 if (lio && lio < lrem) {
694 skein_update(&sk, tmpdata, ldone);
697 skein_final(mackey, &sk);
701 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
702 memset(&getps, 0, sizeof(struct getpasswd_state));
703 getps.fd = getps.efd = -1;
704 getps.passwd = pwdask;
705 getps.pwlen = sizeof(pwdask)-1;
706 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
707 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
708 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
709 getps.flags = GETP_WAITFILL;
710 n = xgetpasswd(&getps);
711 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
712 if (n == ((size_t)-2)) xexit(1);
713 if (verbose) say_hint(pwdask, n, "MAC password hint");
714 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
718 if ((strlen(progname) <= 9)
719 && ((!strcmp(progname, "sksum"))
720 || ((!memcmp(progname, "sk", 2))
721 && (!memcmp(progname+3, "sum", 3)
722 || !memcmp(progname+4, "sum", 3)
723 || !memcmp(progname+5, "sum", 3)
724 || !memcmp(progname+6, "sum", 3)))))
725 do_sksum(progname, argv+optind);
726 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
731 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
732 if (!strcmp(argv[idx], "-")) kfd = 0;
733 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
734 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
736 lio = strnlen(argv[idx], PATH_MAX);
737 memset(argv[idx], '*', lio);
744 if (do_full_key == NO && tweakf) {
747 if (!strcmp(tweakf, "-")) twfd = 0;
748 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
749 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
750 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
751 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
752 if (ldone < TF_TWEAK_SIZE)
753 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
760 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
762 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
763 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
764 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
766 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
768 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
769 if (verbose) tfc_esay("%s: disabling signature verification on "
770 "requested partial decryption.", progname);
774 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
775 maxlen = tfc_fdsize(sfd);
776 if (maxlen == NOFSIZE)
778 "Cannot verify embedded MAC with non-seekable source!");
779 maxlen -= TF_FROM_BITS(macbits);
781 srcfname = argv[idx];
785 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
786 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
788 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
789 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
791 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
796 if (!strcmp(counter_file, "-")) ctrfd = 0;
797 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
798 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
799 lio = xread(ctrfd, ctr, ctrsz);
800 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
801 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
804 else if (counter_opt == TFC_CTR_HEAD) {
807 lrem = lblock = ctrsz;
808 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
809 _ctrragain: lio = xread(sfd, pblk, lrem);
810 if (lio != NOSIZE) ldone += lio;
812 if (errno != EIO && catch_all_errors != YES)
813 xerror(NO, NO, NO, "%s", srcfname);
814 switch (error_action) {
815 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
816 case TFC_ERRACT_SYNC:
817 case TFC_ERRACT_LSYNC:
818 xerror(YES, NO, NO, "%s", srcfname);
819 lio = ldone = lrem = lblock;
821 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
822 else lseek(sfd, rdpos + lio, SEEK_SET);
824 default: xerror(NO, NO, NO, "%s", srcfname); break;
827 if (lio && lio < lrem) {
832 total_processed_src += ldone;
837 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
839 if (lseek(sfd, iseek, SEEK_SET) == -1)
840 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
843 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
845 if (verbose) tfc_esay("%s: hashing password", progname);
847 if (rawkey == TFC_RAWKEY_KEYFILE) {
848 tfc_yesno xtskeyset = NO;
852 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
853 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
854 _keyragain: lio = xread(kfd, pblk, lrem);
855 if (lio != NOSIZE) ldone += lio;
857 if (errno != EIO && catch_all_errors != YES)
858 xerror(NO, NO, NO, "reading key");
859 switch (error_action) {
860 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
861 case TFC_ERRACT_SYNC:
862 case TFC_ERRACT_LSYNC:
863 xerror(YES, NO, NO, "reading key");
864 lio = ldone = lrem = lblock;
866 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
867 else lseek(kfd, rdpos + lio, SEEK_SET);
869 default: xerror(NO, NO, NO, "reading key"); break;
872 if (lio && lio < lrem) {
877 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
879 if (ctr_mode == TFC_MODE_XTS) {
880 if (xtskeyset == NO) {
887 else if (rawkey == TFC_RAWKEY_ASKSTR) {
888 tfc_yesno xtskeyset = NO;
890 pblk = key; n = sizeof(key);
891 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
892 getps.fd = getps.efd = -1;
893 getps.passwd = (char *)pblk;
895 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
896 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
897 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
898 getps.flags = GETP_WAITFILL;
899 n = xgetpasswd(&getps);
900 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
901 if (n == ((size_t)-2)) xexit(1);
902 if (verbose) say_hint(pblk, n, "Raw string key hint");
903 if (ctr_mode == TFC_MODE_XTS) {
904 if (xtskeyset == NO) {
905 pblk = xtskey; n = sizeof(xtskey);
911 else if (rawkey == TFC_RAWKEY_ASKHEX) {
912 tfc_yesno xtskeyset = NO;
916 memset(&getps, 0, sizeof(struct getpasswd_state));
917 getps.fd = getps.efd = -1;
918 getps.passwd = pwdask;
919 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
920 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
921 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
922 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
923 getps.flags = GETP_WAITFILL;
924 n = xgetpasswd(&getps);
925 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
926 if (n == ((size_t)-2)) xexit(1);
928 tfc_esay("Please input even number of hex digits!");
929 goto _rawkey_hex_again;
931 hex2bin(pblk, pwdask);
932 memset(pwdask, 0, sizeof(pwdask));
933 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
934 if (ctr_mode == TFC_MODE_XTS) {
935 if (xtskeyset == NO) {
938 goto _rawkey_hex_again;
943 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
944 getps.fd = getps.efd = -1;
945 getps.passwd = pwdask;
946 getps.pwlen = sizeof(pwdask)-1;
947 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
948 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
949 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
950 getps.flags = GETP_WAITFILL;
951 n = xgetpasswd(&getps);
952 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
953 if (n == ((size_t)-2)) xexit(1);
954 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
955 getps.fd = getps.efd = -1;
956 getps.passwd = pwdagain;
957 getps.pwlen = sizeof(pwdagain)-1;
958 getps.echo = "Enter it again: ";
959 getps.flags = GETP_WAITFILL;
960 n = xgetpasswd(&getps);
961 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
962 if (n == ((size_t)-2)) xexit(1);
963 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
964 tfc_esay("Passwords are different, try again");
968 if (verbose) say_hint(pwdask, n, "Password hint");
969 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
970 memset(pwdask, 0, sizeof(pwdask));
971 memset(pwdagain, 0, sizeof(pwdagain));
974 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
975 xerror(NO, NO, YES, "hashing key");
979 if (tfc_saltsz > 0) {
980 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
981 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
983 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
984 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
985 memset(tfc_salt, 0, TFC_MAX_SALT);
988 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
989 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
994 tfc_yesno xtskeyset = NO;
997 if (!strcmp(genkeyf, "-")) krfd = 1;
998 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
999 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
1000 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
1001 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
1002 if (verbose && xtskeyset == NO) {
1003 tfc_esay("%s: password hashing done", progname);
1004 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
1005 tfc_esay("%s: Have a nice day!", progname);
1008 if (ctr_mode == TFC_MODE_XTS) {
1009 if (xtskeyset == NO) {
1021 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
1022 if (verbose) tfc_esay("%s: disabling signature verification on "
1023 "requested partial decryption.", progname);
1028 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1029 if (ctr_mode < TFC_MODE_OCB) {
1030 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1031 "will be slower.", progname);
1032 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1033 else skein_init(&sk, macbits);
1037 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1038 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1042 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1043 if (do_full_key == NO) {
1044 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1045 tf_tweak_set(key, tweak);
1047 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1049 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1051 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1052 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1053 if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1055 if (counter_opt == TFC_CTR_SHOW) {
1056 switch (do_outfmt) {
1057 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1058 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1059 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1062 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1069 if (verbose) tfc_esay("%s: password hashing done", progname);
1071 if (overwrite_source && srcfname) argv[idx] = srcfname;
1075 if (!strcmp(argv[idx], "-")) dfd = 1;
1076 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
1078 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1079 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
1081 dstfname = argv[idx];
1086 if (lseek(dfd, oseek, SEEK_SET) == -1)
1087 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1090 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1091 memset(&sigact, 0, sizeof(sigact));
1092 sigact.sa_flags = SA_RESTART;
1093 sigact.sa_handler = print_crypt_status;
1094 sigaction(SIGUSR1, &sigact, NULL);
1095 sigaction(SIGALRM, &sigact, NULL);
1096 if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
1097 sigact.sa_handler = change_status_width;
1098 sigaction(SIGQUIT, &sigact, NULL);
1099 sigact.sa_handler = change_status_timer;
1100 sigaction(SIGUSR2, &sigact, NULL);
1102 sigact.sa_handler = print_crypt_status;
1103 sigaction(SIGINT, &sigact, NULL);
1104 sigaction(SIGTERM, &sigact, NULL);
1105 sigaction(SIGTSTP, &sigact, NULL);
1108 sigact.sa_handler = exit_sigterm;
1109 sigaction(SIGINT, &sigact, NULL);
1110 sigaction(SIGTERM, &sigact, NULL);
1111 sigact.sa_handler = handle_sigtstp;
1112 sigaction(SIGTSTP, &sigact, NULL);
1114 memset(&sigact, 0, sizeof(struct sigaction));
1116 tfc_getcurtime(&delta_time);
1119 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1123 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1124 if (lio != NOSIZE) ldone += lio;
1125 else xerror(NO, NO, NO, "%s", dstfname);
1126 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1132 total_processed_dst += ldone;
1133 delta_processed += ldone;
1136 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1138 if (do_mac == TFC_MAC_JUST_VRFY2) {
1139 rwd = tfc_fdgetpos(sfd);
1140 if (rwd == NOFSIZE) {
1141 tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", progname);
1142 do_mac = TFC_MAC_VRFY;
1144 goto _nodecrypt_again_vrfy2;
1146 _decrypt_again_vrfy2:
1147 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1148 xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1150 total_processed_src = rwd;
1151 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1152 memset(svctr, 0, TF_BLOCK_SIZE);
1155 _nodecrypt_again_vrfy2:
1162 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1163 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1164 _ragain: lio = xread(sfd, pblk, lrem);
1165 if (lio == 0) do_stop = YES;
1166 if (lio != NOSIZE) ldone += lio;
1168 if (errno != EIO && catch_all_errors != YES)
1169 xerror(NO, NO, NO, "%s", srcfname);
1170 switch (error_action) {
1171 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1172 case TFC_ERRACT_SYNC:
1173 case TFC_ERRACT_LSYNC:
1174 xerror(YES, NO, NO, "%s", srcfname);
1175 lio = ldone = lrem = lblock;
1176 memset(srcblk, 0, lio);
1177 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1178 else lseek(sfd, rdpos + lio, SEEK_SET);
1180 default: xerror(NO, NO, NO, "%s", srcfname); break;
1183 if (lio && lio < lrem) {
1188 total_processed_src += ldone;
1190 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1191 size_t orig = ldone;
1192 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1193 if (ldone > blksize) ldone = blksize;
1194 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1197 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1198 skein_update(&sk, srcblk, ldone);
1200 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1201 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1202 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1203 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1204 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1205 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1206 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1207 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1208 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1209 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1210 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1211 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1212 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1213 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1215 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1216 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1217 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1218 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1220 else if (ctr_mode == TFC_MODE_PLAIN)
1221 memcpy(dstblk, srcblk, ldone);
1223 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1224 skein_update(&sk, dstblk, ldone);
1225 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1230 _wagain: lio = xwrite(dfd, pblk, lrem);
1231 if (lio != NOSIZE) ldone += lio;
1232 else xerror(NO, NO, NO, "%s", dstfname);
1233 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1239 total_written_dst += ldone;
1240 _nowrite: total_processed_dst += ldone;
1241 delta_processed += ldone;
1243 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1250 if (do_mac >= TFC_MAC_VRFY) {
1254 lrem = lblock = TF_FROM_BITS(macbits);
1255 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1256 _macragain: lio = xread(sfd, pblk, lrem);
1257 if (lio != NOSIZE) ldone += lio;
1259 if (errno != EIO && catch_all_errors != YES)
1260 xerror(NO, NO, NO, "%s", srcfname);
1261 switch (error_action) {
1262 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1263 case TFC_ERRACT_SYNC:
1264 case TFC_ERRACT_LSYNC:
1265 xerror(YES, NO, NO, "%s", srcfname);
1266 lio = ldone = lrem = lblock;
1267 memset(macvrfy, 0, lio);
1268 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1269 else lseek(sfd, rdpos + lio, SEEK_SET);
1271 default: xerror(NO, NO, NO, "%s", srcfname); break;
1274 if (lio && lio < lrem) {
1279 total_processed_src += ldone;
1284 if (!strcmp(do_mac_file, "-")) mfd = 0;
1285 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1286 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1287 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1288 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1289 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1290 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1291 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1292 lio = TF_FROM_BITS(macbits);
1293 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1295 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1299 if (ldone < TF_FROM_BITS(macbits)) {
1300 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1301 "not verifying", progname, ldone);
1306 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1307 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1309 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1310 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1311 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1312 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1313 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1314 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1316 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1318 tfc_esay("%s: signature is good", progname);
1320 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1321 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1324 if (do_mac == TFC_MAC_JUST_VRFY2) {
1325 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", progname);
1326 do_mac = TFC_MAC_DROP;
1327 goto _decrypt_again_vrfy2;
1332 tfc_esay("%s: signature is BAD: "
1333 "wrong password, key, mode, or file is not signed", progname);
1334 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", progname);
1339 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1340 memset(macresult, 0, sizeof(macresult));
1341 memset(tmpdata, 0, sizeof(tmpdata));
1344 else if (do_mac == TFC_MAC_SIGN) {
1345 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1346 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1348 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1349 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1350 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1351 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1352 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1353 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1354 memset(macresult, 0, sizeof(macresult));
1358 lio = lrem = TF_FROM_BITS(macbits);
1360 _macwagain: lio = xwrite(dfd, pblk, lrem);
1361 if (lio != NOSIZE) ldone += lio;
1362 else xerror(NO, NO, NO, "%s", dstfname);
1363 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1369 total_processed_dst += ldone;
1370 delta_processed += ldone;
1375 if (!strcmp(do_mac_file, "-")) mfd = 1;
1376 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1377 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1378 if (do_outfmt == TFC_OUTFMT_B64) {
1379 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1380 memset(tmpdata, 0, TFC_TMPSIZE);
1381 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1382 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1383 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1385 tmpdata[lrem] = '\n';
1388 lio = xwrite(mfd, tmpdata, lrem);
1390 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1391 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1392 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1396 memset(macvrfy, 0, sizeof(macvrfy));
1397 memset(macresult, 0, sizeof(macresult));
1398 memset(tmpdata, 0, sizeof(tmpdata));
1401 if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(0);