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 int getps_filter(struct getpasswd_state *getps, char chr, size_t pos)
34 getps->retn = ((size_t)-2);
40 static int getps_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
43 getps->retn = ((size_t)-2);
46 if (chr >= '0' && chr <= '9') return 1;
47 if (chr >= 'a' && chr <= 'f') return 1;
48 if (chr >= 'A' && chr <= 'F') return 1;
49 if (chr == '\x7f' || chr == '\x08'
50 || chr == '\x15' || chr == '\x17') return 1;
54 static inline int isctrlchr(int c)
57 if (c >= 0 && c <= 31) return 1;
58 if (c == 127) return 1;
62 static int getps_plain_filter(struct getpasswd_state *getps, char chr, size_t pos)
66 x = getps_filter(getps, chr, pos);
69 if (pos < getps->pwlen && !isctrlchr(chr))
70 write(getps->efd, &chr, sizeof(char));
74 static int getps_plain_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
78 x = getps_hex_filter(getps, chr, pos);
81 if (pos < getps->pwlen && !isctrlchr(chr))
82 write(getps->efd, &chr, sizeof(char));
86 static void make_hint(void *hint, size_t szhint, const void *data, size_t szdata)
88 char t[TF_FROM_BITS(TF_MAX_BITS)];
90 skein(t, TF_MAX_BITS, NULL, data, szdata);
91 xor_shrink(hint, szhint, t, sizeof(t));
92 memset(t, 0, sizeof(t));
95 static void raw_say_hint(void *hint, size_t szhint, const void *data, size_t szdata, const char *prompt)
97 make_hint(hint, szhint, data, szdata);
98 if (prompt) tfc_nfsay(stderr, "%s: ", prompt);
99 mehexdump(hint, szhint, szhint, 1);
100 memset(hint, 0, szhint);
103 static void say_hint(const void *data, size_t szdata, const char *prompt)
105 char t[TF_SIZE_UNIT];
106 raw_say_hint(t, TF_SIZE_UNIT, data, szdata, prompt);
107 /* t[] is erased (automatically) */
110 int main(int argc, char **argv)
114 char *s, *d, *t, *stoi;
117 progname = basename(argv[0]);
119 if (!isatty(2)) do_statline_dynamic = NO;
124 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
125 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
126 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
127 read_defaults(s, YES);
132 while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:Pkzxc:l:qedn:vV:pwE:O:S:AmM:R:Z:WHD:")) != -1) {
135 read_defaults(optarg, NO);
144 if (!strcasecmp(optarg, "show"))
145 counter_opt = TFC_CTR_SHOW;
146 else if (!strcasecmp(optarg, "head"))
147 counter_opt = TFC_CTR_HEAD;
148 else if (!strcasecmp(optarg, "rand"))
149 counter_opt = TFC_CTR_RAND;
150 else if (!strcasecmp(optarg, "zero"))
151 counter_opt = TFC_CTR_ZERO;
152 else if (strchr(optarg, ':')) {
155 counter_opt = TFC_CTR_SSET;
158 s = d = optarg; t = NULL;
159 while ((s = strtok_r(d, ",", &t))) {
166 chr = (char)strtoul(s, &stoi, 16);
167 if (!str_empty(stoi)) continue;
168 x = (size_t)strtoul(ss, &stoi, 10);
169 if (!str_empty(stoi)) continue;
171 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
175 else counter_file = sksum_hashlist_file = optarg;
178 if (!strcasecmp(optarg, "ctr"))
179 ctr_mode = TFC_MODE_CTR;
180 else if (!strcasecmp(optarg, "stream"))
181 ctr_mode = TFC_MODE_STREAM;
182 else if (!strcasecmp(optarg, "cbc"))
183 ctr_mode = TFC_MODE_CBC;
184 else if (!strcasecmp(optarg, "ecb"))
185 ctr_mode = TFC_MODE_ECB;
186 else if (!strcasecmp(optarg, "xts"))
187 ctr_mode = TFC_MODE_XTS;
188 else if (!strcasecmp(optarg, "ocb"))
189 ctr_mode = TFC_MODE_OCB;
190 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
193 do_edcrypt = TFC_DO_PLAIN;
195 ctr_mode = TFC_MODE_PLAIN;
198 do_edcrypt = TFC_DO_ENCRYPT;
201 do_edcrypt = TFC_DO_DECRYPT;
204 macbits = strtoul(optarg, &stoi, 10);
205 if (macbits == 0 || !str_empty(stoi) || macbits < 8
206 || macbits > TF_MAX_BITS || macbits % 8)
207 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
210 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
211 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
214 if (!strcasecmp(optarg, "key"))
215 mackey_opt = TFC_MACKEY_RAWKEY;
216 else if (!strcasecmp(optarg, "pwd"))
217 mackey_opt = TFC_MACKEY_PASSWORD;
219 mackey_opt = TFC_MACKEY_FILE;
227 rawkey = TFC_RAWKEY_KEYFILE;
230 rawkey = TFC_RAWKEY_ASKSTR;
233 rawkey = TFC_RAWKEY_ASKHEX;
244 if (maxlen != NOFSIZE) break;
246 maxlen = tfc_humanfsize(optarg, &stoi);
247 if (!str_empty(stoi)) {
248 maxlen = tfc_fnamesize(optarg, YES);
249 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
250 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
251 "%s: invalid count value", optarg);
253 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
254 if (counter_opt == TFC_CTR_HEAD)
255 maxlen += TF_BLOCK_SIZE;
258 overwrite_source = YES;
261 if (!strcmp(optarg, "xall")) {
262 catch_all_errors = YES;
265 if (!strcmp(optarg, "xseek")) {
266 ignore_seek_errors = YES;
269 if (!strcmp(optarg, "exit"))
270 error_action = TFC_ERRACT_EXIT;
271 else if (!strncmp(optarg, "cont", 4))
272 error_action = TFC_ERRACT_CONT;
273 else if (!strcmp(optarg, "sync"))
274 error_action = TFC_ERRACT_SYNC;
275 else if (!strcmp(optarg, "lsync"))
276 error_action = TFC_ERRACT_LSYNC;
277 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
280 s = d = optarg; t = NULL;
281 while ((s = strtok_r(d, ",", &t))) {
283 if (!strcmp(s, "sync"))
284 write_flags |= O_SYNC;
285 else if (!strcmp(s, "trunc"))
286 write_flags |= O_TRUNC;
287 else if (!strcmp(s, "fsync"))
289 else if (!strcmp(s, "pad"))
291 else if (!strcmp(s, "xtime"))
292 do_preserve_time = YES;
293 else if (!strcmp(s, "gibsize"))
294 do_stats_in_gibs = YES;
295 else if (!strcmp(s, "plainstats"))
296 do_statline_dynamic = NO;
297 else if (!strcmp(s, "statless"))
299 else if (!strcmp(s, "norepeat"))
301 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
303 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
304 mac_pw_prompt = s+10;
305 else if (!strcmp(s, "shorthex"))
306 do_full_hexdump = NO;
307 else if (!strcmp(s, "fullkey"))
309 else if (!strcmp(s, "showsecrets"))
311 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
313 blksize = (size_t)tfc_humanfsize(s, &stoi);
314 if (!str_empty(stoi)) {
315 blksize = (size_t)tfc_fnamesize(s, YES);
316 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
317 if (blksize == NOSIZE) xerror(NO, YES, YES,
318 "%s: invalid block size value", s);
320 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
321 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
322 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
323 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
324 "%s: block size exceeds %u bytes",
325 s, TFC_U(TFC_BLKSIZE));
327 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
329 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
330 if (!str_empty(stoi)) {
331 xtsblocks = (size_t)tfc_fnamesize(s, YES);
332 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
333 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
334 "%s: invalid blocks per xts block value", s);
336 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
337 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
338 "%s: nr of blocks per xts block is not round to %u bytes",
339 s, TFC_U(TFC_BLKSIZE));
340 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
341 "%s: nr of blocks per xts block exceeds %u bytes",
342 s, TFC_U(TFC_BLKSIZE));
344 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
346 iseek = tfc_humanfsize(s, &stoi);
347 if (!str_empty(stoi)) {
348 iseek = tfc_fnamesize(s, YES);
349 iseek = tfc_modifysize(iseek, strchr(s, ':'));
350 if (iseek == NOFSIZE) xerror(NO, YES, YES,
351 "%s: invalid iseek value", s);
353 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
354 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
356 "%s: not round to TF block size "
358 s, TFC_U(TF_BLOCK_SIZE));
359 iseek_blocks = iseek / TF_BLOCK_SIZE;
361 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
363 iseek = tfc_humanfsize(s, &stoi);
364 if (!str_empty(stoi)) {
365 iseek = tfc_fnamesize(s, YES);
366 iseek = tfc_modifysize(iseek, strchr(s, ':'));
367 if (iseek == NOFSIZE) xerror(NO, YES, YES,
368 "%s: invalid ixseek value", s);
370 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
372 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
374 iseek_blocks = tfc_humanfsize(s, &stoi);
375 if (!str_empty(stoi)) {
376 iseek_blocks = tfc_fnamesize(s, YES);
377 if (iseek_blocks == NOFSIZE)
379 "%s: invalid ictr value", s);
380 iseek_blocks /= TF_BLOCK_SIZE;
381 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
383 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
385 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
387 iseek_blocks = tfc_humanfsize(s, &stoi);
388 if (!str_empty(stoi)) {
389 iseek_blocks = tfc_fnamesize(s, YES);
390 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
391 if (iseek_blocks == NOFSIZE)
393 "%s: invalid ixctr value", s);
395 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
396 if (iseek_blocks % TF_BLOCK_SIZE)
398 "%s: not round to TF block size "
399 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
400 iseek_blocks /= TF_BLOCK_SIZE;
402 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
404 oseek = tfc_humanfsize(s, &stoi);
405 if (!str_empty(stoi)) {
406 oseek = tfc_fnamesize(s, YES);
407 oseek = tfc_modifysize(oseek, strchr(s, ':'));
408 if (oseek == NOFSIZE) xerror(NO, YES, YES,
409 "%s: invalid oseek value", s);
411 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
413 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
415 maxlen = tfc_humanfsize(s, &stoi);
416 if (!str_empty(stoi)) {
417 maxlen = tfc_fnamesize(s, YES);
418 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
419 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
420 "%s: invalid count value", s);
422 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
423 if (counter_opt == TFC_CTR_HEAD)
424 maxlen += TF_BLOCK_SIZE;
426 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
428 if (!strcmp(s, "tail")) {
429 do_ftrunc = TFC_FTRUNC_TAIL;
430 ftrunc_dfd = NOFSIZE;
433 do_ftrunc = TFC_DO_FTRUNC;
434 ftrunc_dfd = tfc_humanfsize(s, &stoi);
435 if (!str_empty(stoi)) {
436 ftrunc_dfd = tfc_fnamesize(s, YES);
437 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
438 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
439 "%s: invalid ftrunc value", s);
441 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
444 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
446 maxkeylen = tfc_humanfsize(s, &stoi);
447 if (!str_empty(stoi)) {
448 maxkeylen = tfc_fnamesize(s, YES);
449 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
450 if (maxkeylen == NOSIZE)
452 "%s: invalid key length value", s);
454 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
456 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
458 keyoffset = tfc_humanfsize(s, &stoi);
459 if (!str_empty(stoi)) {
460 keyoffset = tfc_fnamesize(s, YES);
461 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
462 if (keyoffset == NOFSIZE)
464 "%s: invalid key offset value", s);
466 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
468 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
470 ctrsz = tfc_humanfsize(s, &stoi);
471 if (!str_empty(stoi)) {
472 ctrsz = (size_t)tfc_fnamesize(s, YES);
473 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
476 "%s: invalid counter length value", s);
478 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
479 if (ctrsz > TF_BLOCK_SIZE)
480 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
482 else xerror(NO, YES, YES, "invalid option %s", s);
486 do_mac = TFC_MAC_SIGN;
487 if (strcasecmp(optarg, "mac") != 0)
488 do_mac_file = optarg;
491 do_mac = TFC_MAC_VRFY;
492 if (!strcasecmp(optarg, "drop"))
493 do_mac = TFC_MAC_DROP;
494 else if (strcasecmp(optarg, "mac") != 0)
495 do_mac_file = optarg;
498 if (do_mac != TFC_MAC_VRFY)
499 xerror(NO, YES, YES, "signature source was not specified");
500 do_mac = TFC_MAC_JUST_VRFY;
504 if (maxlen != NOFSIZE) {
505 if (c == 'Z') genzero_nr_bytes = maxlen;
506 else genrandom_nr_bytes = maxlen;
510 if (!strcasecmp(optarg, "cbs"))
512 else if (!strcasecmp(optarg, "ks"))
513 t = TF_FROM_BITS(TFC_KEY_BITS);
514 else if (!strcasecmp(optarg, "xks"))
515 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
516 else if (!strcasecmp(optarg, "iobs"))
519 t = tfc_humanfsize(optarg, &stoi);
520 if (!str_empty(stoi)) {
521 t = tfc_fnamesize(optarg, NO);
522 t = tfc_modifysize(t, strchr(optarg, ':'));
524 else t = tfc_modifysize(t, strchr(optarg, ':'));
526 if (c == 'Z') genzero_nr_bytes = maxlen = t;
527 else genrandom_nr_bytes = maxlen = t;
531 do_preserve_time = YES;
534 do_outfmt = TFC_OUTFMT_B64;
537 do_outfmt = TFC_OUTFMT_RAW;
540 do_outfmt = TFC_OUTFMT_HEX;
545 do_full_hexdump = NO;
552 td = strtod(optarg, &stoi);
553 status_timer = TFC_DTOUSECS(td);
554 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
562 if (!strcmp(progname, "tfbench")) {
563 if (!*(argv+optind)) usage();
565 td = strtod(*(argv+optind), &stoi);
566 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
568 "%s: invalid benchmark time in seconds", *(argv+optind));
569 bench_timer = TFC_DTOUSECS(td);
570 do_benchmark(bench_timer, td);
572 if (genrandom_nr_bytes) {
573 ctr_mode = TFC_MODE_STREAM;
574 do_edcrypt = TFC_DO_ENCRYPT;
575 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
577 if (genzero_nr_bytes) {
578 ctr_mode = TFC_MODE_PLAIN;
579 do_edcrypt = TFC_DO_PLAIN;
580 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
583 if (rawkey && password)
584 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
585 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
586 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
587 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
588 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
589 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
590 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
591 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
592 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
593 if (overwrite_source && counter_opt == TFC_CTR_RAND)
594 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
595 if (ctr_mode == TFC_MODE_PLAIN
596 && (do_edcrypt || do_mac || rawkey
597 || mackey_opt || counter_opt || counter_file))
598 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
606 memset(tfc_salt, 0, TFC_MAX_SALT);
608 if (!strcasecmp(saltf, "disable")) goto _nosalt;
610 if (!strcmp(saltf, "-")) saltfd = 0;
611 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
612 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
613 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
614 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
620 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
624 if (!strcmp(mackeyf, "-")) mkfd = 0;
625 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
626 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
628 skein_init(&sk, TFC_KEY_BITS);
635 lrem = lblock = sizeof(tmpdata);
636 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
637 _mkragain: lio = xread(mkfd, pblk, lrem);
638 if (lio == 0) do_stop = YES;
639 if (lio != NOSIZE) ldone += lio;
641 if (errno != EIO && catch_all_errors != YES)
642 xerror(NO, NO, NO, "%s", mackeyf);
643 switch (error_action) {
644 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
645 case TFC_ERRACT_SYNC:
646 case TFC_ERRACT_LSYNC:
647 xerror(YES, NO, NO, "%s", mackeyf);
648 lio = ldone = lrem = lblock;
649 memset(tmpdata, 0, lio);
650 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
651 else lseek(mkfd, rdpos + lio, SEEK_SET);
653 default: xerror(NO, NO, NO, "%s", mackeyf); break;
656 if (lio && lio < lrem) {
662 skein_update(&sk, tmpdata, ldone);
665 skein_final(mackey, &sk);
669 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
670 memset(&getps, 0, sizeof(struct getpasswd_state));
671 getps.fd = getps.efd = -1;
672 getps.passwd = pwdask;
673 getps.pwlen = sizeof(pwdask)-1;
674 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
675 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
676 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
677 getps.flags = GETP_WAITFILL;
678 n = xgetpasswd(&getps);
679 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
680 if (n == ((size_t)-2)) xexit(1);
681 if (verbose) say_hint(pwdask, n, "MAC password hint");
682 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
686 if ((strlen(progname) <= 9)
687 && ((!strcmp(progname, "sksum"))
688 || ((!memcmp(progname, "sk", 2))
689 && (!memcmp(progname+3, "sum", 3)
690 || !memcmp(progname+4, "sum", 3)
691 || !memcmp(progname+5, "sum", 3)
692 || !memcmp(progname+6, "sum", 3)))))
693 do_sksum(progname, argv+optind);
694 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
699 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
700 if (!strcmp(argv[idx], "-")) kfd = 0;
701 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
702 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
704 lio = strnlen(argv[idx], PATH_MAX);
705 memset(argv[idx], '*', lio);
712 if (do_full_key == NO && tweakf) {
715 if (!strcmp(tweakf, "-")) twfd = 0;
716 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
717 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
718 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
719 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
720 if (ldone < TF_TWEAK_SIZE)
721 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
728 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
730 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
731 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
732 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
734 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
736 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
737 if (verbose) tfc_esay("%s: disabling signature verification on "
738 "requested partial decryption.", progname);
742 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
743 maxlen = tfc_fdsize(sfd);
744 if (maxlen == NOFSIZE)
746 "Cannot verify embedded MAC with non-seekable source!");
747 maxlen -= TF_FROM_BITS(macbits);
749 srcfname = argv[idx];
753 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
754 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
756 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
757 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
759 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
764 if (!strcmp(counter_file, "-")) ctrfd = 0;
765 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
766 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
767 lio = xread(ctrfd, ctr, ctrsz);
768 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
769 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
772 else if (counter_opt == TFC_CTR_HEAD) {
775 lrem = lblock = ctrsz;
776 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
777 _ctrragain: lio = xread(sfd, pblk, lrem);
778 if (lio != NOSIZE) ldone += lio;
780 if (errno != EIO && catch_all_errors != YES)
781 xerror(NO, NO, NO, "%s", srcfname);
782 switch (error_action) {
783 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
784 case TFC_ERRACT_SYNC:
785 case TFC_ERRACT_LSYNC:
786 xerror(YES, NO, NO, "%s", srcfname);
787 lio = ldone = lrem = lblock;
789 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
790 else lseek(sfd, rdpos + lio, SEEK_SET);
792 default: xerror(NO, NO, NO, "%s", srcfname); break;
795 if (lio && lio < lrem) {
800 total_processed_src += ldone;
805 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
807 if (lseek(sfd, iseek, SEEK_SET) == -1)
808 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
811 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
813 if (verbose) tfc_esay("%s: hashing password", progname);
815 if (rawkey == TFC_RAWKEY_KEYFILE) {
816 tfc_yesno xtskeyset = NO;
820 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
821 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
822 _keyragain: lio = xread(kfd, pblk, lrem);
823 if (lio != NOSIZE) ldone += lio;
825 if (errno != EIO && catch_all_errors != YES)
826 xerror(NO, NO, NO, "reading key");
827 switch (error_action) {
828 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
829 case TFC_ERRACT_SYNC:
830 case TFC_ERRACT_LSYNC:
831 xerror(YES, NO, NO, "reading key");
832 lio = ldone = lrem = lblock;
834 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
835 else lseek(kfd, rdpos + lio, SEEK_SET);
837 default: xerror(NO, NO, NO, "reading key"); break;
840 if (lio && lio < lrem) {
845 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
847 if (ctr_mode == TFC_MODE_XTS) {
848 if (xtskeyset == NO) {
855 else if (rawkey == TFC_RAWKEY_ASKSTR) {
856 tfc_yesno xtskeyset = NO;
858 pblk = key; n = sizeof(key);
859 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
860 getps.fd = getps.efd = -1;
861 getps.passwd = (char *)pblk;
863 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
864 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
865 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
866 getps.flags = GETP_WAITFILL;
867 n = xgetpasswd(&getps);
868 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
869 if (n == ((size_t)-2)) xexit(1);
870 if (verbose) say_hint(pblk, n, "Raw string key hint");
871 if (ctr_mode == TFC_MODE_XTS) {
872 if (xtskeyset == NO) {
873 pblk = xtskey; n = sizeof(xtskey);
879 else if (rawkey == TFC_RAWKEY_ASKHEX) {
880 tfc_yesno xtskeyset = NO;
884 memset(&getps, 0, sizeof(struct getpasswd_state));
885 getps.fd = getps.efd = -1;
886 getps.passwd = pwdask;
887 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
888 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
889 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
890 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
891 getps.flags = GETP_WAITFILL;
892 n = xgetpasswd(&getps);
893 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
894 if (n == ((size_t)-2)) xexit(1);
896 tfc_esay("Please input even number of hex digits!");
897 goto _rawkey_hex_again;
899 hex2bin(pblk, pwdask);
900 memset(pwdask, 0, sizeof(pwdask));
901 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
902 if (ctr_mode == TFC_MODE_XTS) {
903 if (xtskeyset == NO) {
906 goto _rawkey_hex_again;
911 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
912 getps.fd = getps.efd = -1;
913 getps.passwd = pwdask;
914 getps.pwlen = sizeof(pwdask)-1;
915 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
916 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
917 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
918 getps.flags = GETP_WAITFILL;
919 n = xgetpasswd(&getps);
920 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
921 if (n == ((size_t)-2)) xexit(1);
922 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
923 getps.fd = getps.efd = -1;
924 getps.passwd = pwdagain;
925 getps.pwlen = sizeof(pwdagain)-1;
926 getps.echo = "Enter it again: ";
927 getps.flags = GETP_WAITFILL;
928 n = xgetpasswd(&getps);
929 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
930 if (n == ((size_t)-2)) xexit(1);
931 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
932 tfc_esay("Passwords are different, try again");
936 if (verbose) say_hint(pwdask, n, "Password hint");
937 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
938 memset(pwdask, 0, sizeof(pwdask));
939 memset(pwdagain, 0, sizeof(pwdagain));
942 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
943 xerror(NO, NO, YES, "hashing key");
947 if (tfc_saltsz > 0) {
948 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
949 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
951 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
952 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
953 memset(tfc_salt, 0, TFC_MAX_SALT);
956 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
957 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
962 tfc_yesno xtskeyset = NO;
965 if (!strcmp(genkeyf, "-")) krfd = 1;
966 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
967 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
968 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
969 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
970 if (verbose && xtskeyset == NO) {
971 tfc_esay("%s: password hashing done", progname);
972 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
973 tfc_esay("%s: Have a nice day!", progname);
976 if (ctr_mode == TFC_MODE_XTS) {
977 if (xtskeyset == NO) {
989 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
990 if (verbose) tfc_esay("%s: disabling signature verification on "
991 "requested partial decryption.", progname);
996 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
997 if (ctr_mode < TFC_MODE_OCB) {
998 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
999 "will be slower.", progname);
1000 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1001 else skein_init(&sk, macbits);
1005 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1006 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1010 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1011 if (do_full_key == NO) {
1012 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1013 tf_tweak_set(key, tweak);
1015 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1017 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1019 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1020 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1022 if (counter_opt == TFC_CTR_SHOW) {
1023 switch (do_outfmt) {
1024 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1025 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1026 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1029 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1036 if (verbose) tfc_esay("%s: password hashing done", progname);
1038 if (overwrite_source && srcfname) argv[idx] = srcfname;
1042 if (!strcmp(argv[idx], "-")) dfd = 1;
1043 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
1045 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1046 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
1048 dstfname = argv[idx];
1053 if (lseek(dfd, oseek, SEEK_SET) == -1)
1054 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1057 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1058 memset(&sigact, 0, sizeof(sigact));
1059 sigact.sa_flags = SA_RESTART;
1060 sigact.sa_handler = print_crypt_status;
1061 sigaction(SIGUSR1, &sigact, NULL);
1062 sigaction(SIGTSTP, &sigact, NULL);
1063 sigaction(SIGALRM, &sigact, NULL);
1064 if (status_timer) setup_next_alarm(status_timer);
1065 sigact.sa_handler = change_status_width;
1066 sigaction(SIGQUIT, &sigact, NULL);
1067 sigact.sa_handler = change_status_timer;
1068 sigaction(SIGUSR2, &sigact, NULL);
1070 sigact.sa_handler = print_crypt_status;
1071 sigaction(SIGINT, &sigact, NULL);
1072 sigaction(SIGTERM, &sigact, NULL);
1075 sigact.sa_handler = exit_sigterm;
1076 sigaction(SIGINT, &sigact, NULL);
1077 sigaction(SIGTERM, &sigact, NULL);
1079 memset(&sigact, 0, sizeof(struct sigaction));
1081 tfc_getcurtime(&delta_time);
1084 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1088 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1089 if (lio != NOSIZE) ldone += lio;
1090 else xerror(NO, NO, NO, "%s", dstfname);
1091 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1097 total_processed_dst += ldone;
1098 delta_processed += ldone;
1101 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1109 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1110 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1111 _ragain: lio = xread(sfd, pblk, lrem);
1112 if (lio == 0) do_stop = TFC_STOP_BEGAN;
1113 if (lio != NOSIZE) ldone += lio;
1115 if (errno != EIO && catch_all_errors != YES)
1116 xerror(NO, NO, NO, "%s", srcfname);
1117 switch (error_action) {
1118 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1119 case TFC_ERRACT_SYNC:
1120 case TFC_ERRACT_LSYNC:
1121 xerror(YES, NO, NO, "%s", srcfname);
1122 lio = ldone = lrem = lblock;
1123 memset(srcblk, 0, lio);
1124 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1125 else lseek(sfd, rdpos + lio, SEEK_SET);
1127 default: xerror(NO, NO, NO, "%s", srcfname); break;
1130 if (lio && lio < lrem) {
1135 total_processed_src += ldone;
1137 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1138 size_t orig = ldone;
1139 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1140 if (ldone > blksize) ldone = blksize;
1141 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1144 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1145 skein_update(&sk, srcblk, ldone);
1147 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1148 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1149 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1150 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1151 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1152 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1153 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1154 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1155 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1156 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1157 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1158 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1159 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1160 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1162 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1163 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1164 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1165 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1167 else if (ctr_mode == TFC_MODE_PLAIN)
1168 memcpy(dstblk, srcblk, ldone);
1170 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1171 skein_update(&sk, dstblk, ldone);
1172 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1177 _wagain: lio = xwrite(dfd, pblk, lrem);
1178 if (lio != NOSIZE) ldone += lio;
1179 else xerror(NO, NO, NO, "%s", dstfname);
1180 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1186 _nowrite: total_processed_dst += ldone;
1187 delta_processed += ldone;
1189 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1192 if (do_stop == TFC_STOP_FULL) goto _nomac;
1195 if (do_mac >= TFC_MAC_VRFY) {
1199 lrem = lblock = TF_FROM_BITS(macbits);
1200 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1201 _macragain: lio = xread(sfd, pblk, lrem);
1202 if (lio != NOSIZE) ldone += lio;
1204 if (errno != EIO && catch_all_errors != YES)
1205 xerror(NO, NO, NO, "%s", srcfname);
1206 switch (error_action) {
1207 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1208 case TFC_ERRACT_SYNC:
1209 case TFC_ERRACT_LSYNC:
1210 xerror(YES, NO, NO, "%s", srcfname);
1211 lio = ldone = lrem = lblock;
1212 memset(macvrfy, 0, lio);
1213 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1214 else lseek(sfd, rdpos + lio, SEEK_SET);
1216 default: xerror(NO, NO, NO, "%s", srcfname); break;
1219 if (lio && lio < lrem) {
1224 total_processed_src += ldone;
1229 if (!strcmp(do_mac_file, "-")) mfd = 0;
1230 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1231 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1232 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1233 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1234 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1235 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1236 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1237 lio = TF_FROM_BITS(macbits);
1238 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1240 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1244 if (ldone < TF_FROM_BITS(macbits)) {
1245 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1246 "not verifying", progname, ldone);
1251 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1252 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1254 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1255 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1256 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1257 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1258 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1259 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1261 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1263 tfc_esay("%s: signature is good", progname);
1265 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1266 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1271 if (quiet == NO) tfc_esay("%s: signature is BAD: "
1272 "wrong password, key, mode, or file is not signed", progname);
1276 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1277 memset(macresult, 0, sizeof(macresult));
1278 memset(tmpdata, 0, sizeof(tmpdata));
1281 else if (do_mac == TFC_MAC_SIGN) {
1282 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1283 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1285 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1286 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1287 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1288 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1289 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1290 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1291 memset(macresult, 0, sizeof(macresult));
1295 lio = lrem = TF_FROM_BITS(macbits);
1297 _macwagain: lio = xwrite(dfd, pblk, lrem);
1298 if (lio != NOSIZE) ldone += lio;
1299 else xerror(NO, NO, NO, "%s", dstfname);
1300 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1306 total_processed_dst += ldone;
1307 delta_processed += ldone;
1312 if (!strcmp(do_mac_file, "-")) mfd = 1;
1313 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1314 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1315 if (do_outfmt == TFC_OUTFMT_B64) {
1316 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1317 memset(tmpdata, 0, TFC_TMPSIZE);
1318 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1319 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1320 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1322 tmpdata[lrem] = '\n';
1325 lio = xwrite(mfd, tmpdata, lrem);
1327 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1328 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1329 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1333 memset(macvrfy, 0, sizeof(macvrfy));
1334 memset(macresult, 0, sizeof(macresult));
1335 memset(tmpdata, 0, sizeof(tmpdata));
1339 if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1341 if (do_preserve_time) fcopy_matime(dfd, &s_stat);
1343 if (do_ftrunc > TFC_NO_FTRUNC) {
1344 if (do_ftrunc == TFC_FTRUNC_TAIL) ftrunc_dfd = total_processed_dst;
1345 if (ftruncate(dfd, (off_t)ftrunc_dfd) == -1) xerror(YES, NO, YES, "ftruncate(%d)", dfd);