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 ftrunc_dfd = tfc_humanfsize(s, &stoi);
429 if (!str_empty(stoi)) {
430 ftrunc_dfd = tfc_fnamesize(s, YES);
431 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
432 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
433 "%s: invalid ftrunc value", s);
435 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
437 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
439 maxkeylen = tfc_humanfsize(s, &stoi);
440 if (!str_empty(stoi)) {
441 maxkeylen = tfc_fnamesize(s, YES);
442 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
443 if (maxkeylen == NOSIZE)
445 "%s: invalid key length value", s);
447 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
449 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
451 keyoffset = tfc_humanfsize(s, &stoi);
452 if (!str_empty(stoi)) {
453 keyoffset = tfc_fnamesize(s, YES);
454 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
455 if (keyoffset == NOFSIZE)
457 "%s: invalid key offset value", s);
459 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
461 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
463 ctrsz = tfc_humanfsize(s, &stoi);
464 if (!str_empty(stoi)) {
465 ctrsz = (size_t)tfc_fnamesize(s, YES);
466 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
469 "%s: invalid counter length value", s);
471 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
472 if (ctrsz > TF_BLOCK_SIZE)
473 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
475 else xerror(NO, YES, YES, "invalid option %s", s);
479 do_mac = TFC_MAC_SIGN;
480 if (strcasecmp(optarg, "mac") != 0)
481 do_mac_file = optarg;
484 do_mac = TFC_MAC_VRFY;
485 if (!strcasecmp(optarg, "drop"))
486 do_mac = TFC_MAC_DROP;
487 else if (strcasecmp(optarg, "mac") != 0)
488 do_mac_file = optarg;
491 if (do_mac != TFC_MAC_VRFY)
492 xerror(NO, YES, YES, "signature source was not specified");
493 do_mac = TFC_MAC_JUST_VRFY;
497 if (maxlen != NOFSIZE) {
498 if (c == 'Z') genzero_nr_bytes = maxlen;
499 else genrandom_nr_bytes = maxlen;
503 if (!strcasecmp(optarg, "cbs"))
505 else if (!strcasecmp(optarg, "ks"))
506 t = TF_FROM_BITS(TFC_KEY_BITS);
507 else if (!strcasecmp(optarg, "xks"))
508 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
509 else if (!strcasecmp(optarg, "iobs"))
512 t = tfc_humanfsize(optarg, &stoi);
513 if (!str_empty(stoi)) {
514 t = tfc_fnamesize(optarg, NO);
515 t = tfc_modifysize(t, strchr(optarg, ':'));
517 else t = tfc_modifysize(t, strchr(optarg, ':'));
519 if (c == 'Z') genzero_nr_bytes = maxlen = t;
520 else genrandom_nr_bytes = maxlen = t;
524 do_preserve_time = YES;
527 do_outfmt = TFC_OUTFMT_B64;
530 do_outfmt = TFC_OUTFMT_RAW;
533 do_outfmt = TFC_OUTFMT_HEX;
538 do_full_hexdump = NO;
545 td = strtod(optarg, &stoi);
546 status_timer = TFC_DTOUSECS(td);
547 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
555 if (!strcmp(progname, "tfbench")) {
556 if (!*(argv+optind)) usage();
558 td = strtod(*(argv+optind), &stoi);
559 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
561 "%s: invalid benchmark time in seconds", *(argv+optind));
562 bench_timer = TFC_DTOUSECS(td);
563 do_benchmark(bench_timer, td);
565 if (genrandom_nr_bytes) {
566 ctr_mode = TFC_MODE_STREAM;
567 do_edcrypt = TFC_DO_ENCRYPT;
568 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
570 if (genzero_nr_bytes) {
571 ctr_mode = TFC_MODE_PLAIN;
572 do_edcrypt = TFC_DO_PLAIN;
573 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
576 if (rawkey && password)
577 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
578 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
579 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
580 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
581 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
582 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
583 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
584 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
585 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
586 if (overwrite_source && counter_opt == TFC_CTR_RAND)
587 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
588 if (ctr_mode == TFC_MODE_PLAIN
589 && (do_edcrypt || do_mac || rawkey
590 || mackey_opt || counter_opt || counter_file))
591 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
599 memset(tfc_salt, 0, TFC_MAX_SALT);
601 if (!strcasecmp(saltf, "disable")) goto _nosalt;
603 if (!strcmp(saltf, "-")) saltfd = 0;
604 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
605 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
606 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
607 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
613 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
617 if (!strcmp(mackeyf, "-")) mkfd = 0;
618 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
619 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
621 skein_init(&sk, TFC_KEY_BITS);
628 lrem = lblock = sizeof(tmpdata);
629 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
630 _mkragain: lio = xread(mkfd, pblk, lrem);
631 if (lio == 0) do_stop = YES;
632 if (lio != NOSIZE) ldone += lio;
634 if (errno != EIO && catch_all_errors != YES)
635 xerror(NO, NO, NO, "%s", mackeyf);
636 switch (error_action) {
637 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
638 case TFC_ERRACT_SYNC:
639 case TFC_ERRACT_LSYNC:
640 xerror(YES, NO, NO, "%s", mackeyf);
641 lio = ldone = lrem = lblock;
642 memset(tmpdata, 0, lio);
643 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
644 else lseek(mkfd, rdpos + lio, SEEK_SET);
646 default: xerror(NO, NO, NO, "%s", mackeyf); break;
649 if (lio && lio < lrem) {
655 skein_update(&sk, tmpdata, ldone);
658 skein_final(mackey, &sk);
662 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
663 memset(&getps, 0, sizeof(struct getpasswd_state));
664 getps.fd = getps.efd = -1;
665 getps.passwd = pwdask;
666 getps.pwlen = sizeof(pwdask)-1;
667 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
668 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
669 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
670 getps.flags = GETP_WAITFILL;
671 n = xgetpasswd(&getps);
672 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
673 if (n == ((size_t)-2)) xexit(1);
674 if (verbose) say_hint(pwdask, n, "MAC password hint");
675 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
679 if ((strlen(progname) <= 9)
680 && ((!strcmp(progname, "sksum"))
681 || ((!memcmp(progname, "sk", 2))
682 && (!memcmp(progname+3, "sum", 3)
683 || !memcmp(progname+4, "sum", 3)
684 || !memcmp(progname+5, "sum", 3)
685 || !memcmp(progname+6, "sum", 3)))))
686 do_sksum(progname, argv+optind);
687 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
692 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
693 if (!strcmp(argv[idx], "-")) kfd = 0;
694 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
695 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
697 lio = strnlen(argv[idx], PATH_MAX);
698 memset(argv[idx], '*', lio);
705 if (do_full_key == NO && tweakf) {
708 if (!strcmp(tweakf, "-")) twfd = 0;
709 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
710 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
711 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
712 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
713 if (ldone < TF_TWEAK_SIZE)
714 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
721 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
723 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
724 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
725 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
727 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
729 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
730 if (verbose) tfc_esay("%s: disabling signature verification on "
731 "requested partial decryption.", progname);
735 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
736 maxlen = tfc_fdsize(sfd);
737 if (maxlen == NOFSIZE)
739 "Cannot verify embedded MAC with non-seekable source!");
740 maxlen -= TF_FROM_BITS(macbits);
742 srcfname = argv[idx];
746 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
747 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
749 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
750 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
752 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
757 if (!strcmp(counter_file, "-")) ctrfd = 0;
758 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
759 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
760 lio = xread(ctrfd, ctr, ctrsz);
761 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
762 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
765 else if (counter_opt == TFC_CTR_HEAD) {
768 lrem = lblock = ctrsz;
769 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
770 _ctrragain: lio = xread(sfd, pblk, lrem);
771 if (lio != NOSIZE) ldone += lio;
773 if (errno != EIO && catch_all_errors != YES)
774 xerror(NO, NO, NO, "%s", srcfname);
775 switch (error_action) {
776 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
777 case TFC_ERRACT_SYNC:
778 case TFC_ERRACT_LSYNC:
779 xerror(YES, NO, NO, "%s", srcfname);
780 lio = ldone = lrem = lblock;
782 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
783 else lseek(sfd, rdpos + lio, SEEK_SET);
785 default: xerror(NO, NO, NO, "%s", srcfname); break;
788 if (lio && lio < lrem) {
793 total_processed_src += ldone;
798 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
800 if (lseek(sfd, iseek, SEEK_SET) == -1)
801 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
804 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
806 if (verbose) tfc_esay("%s: hashing password", progname);
808 if (rawkey == TFC_RAWKEY_KEYFILE) {
809 tfc_yesno xtskeyset = NO;
813 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
814 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
815 _keyragain: lio = xread(kfd, pblk, lrem);
816 if (lio != NOSIZE) ldone += lio;
818 if (errno != EIO && catch_all_errors != YES)
819 xerror(NO, NO, NO, "reading key");
820 switch (error_action) {
821 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
822 case TFC_ERRACT_SYNC:
823 case TFC_ERRACT_LSYNC:
824 xerror(YES, NO, NO, "reading key");
825 lio = ldone = lrem = lblock;
827 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
828 else lseek(kfd, rdpos + lio, SEEK_SET);
830 default: xerror(NO, NO, NO, "reading key"); break;
833 if (lio && lio < lrem) {
838 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
840 if (ctr_mode == TFC_MODE_XTS) {
841 if (xtskeyset == NO) {
848 else if (rawkey == TFC_RAWKEY_ASKSTR) {
849 tfc_yesno xtskeyset = NO;
851 pblk = key; n = sizeof(key);
852 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
853 getps.fd = getps.efd = -1;
854 getps.passwd = (char *)pblk;
856 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
857 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
858 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
859 getps.flags = GETP_WAITFILL;
860 n = xgetpasswd(&getps);
861 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
862 if (n == ((size_t)-2)) xexit(1);
863 if (verbose) say_hint(pblk, n, "Raw string key hint");
864 if (ctr_mode == TFC_MODE_XTS) {
865 if (xtskeyset == NO) {
866 pblk = xtskey; n = sizeof(xtskey);
872 else if (rawkey == TFC_RAWKEY_ASKHEX) {
873 tfc_yesno xtskeyset = NO;
877 memset(&getps, 0, sizeof(struct getpasswd_state));
878 getps.fd = getps.efd = -1;
879 getps.passwd = pwdask;
880 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
881 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
882 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
883 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
884 getps.flags = GETP_WAITFILL;
885 n = xgetpasswd(&getps);
886 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
887 if (n == ((size_t)-2)) xexit(1);
889 tfc_esay("Please input even number of hex digits!");
890 goto _rawkey_hex_again;
892 hex2bin(pblk, pwdask);
893 memset(pwdask, 0, sizeof(pwdask));
894 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
895 if (ctr_mode == TFC_MODE_XTS) {
896 if (xtskeyset == NO) {
899 goto _rawkey_hex_again;
904 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
905 getps.fd = getps.efd = -1;
906 getps.passwd = pwdask;
907 getps.pwlen = sizeof(pwdask)-1;
908 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
909 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
910 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
911 getps.flags = GETP_WAITFILL;
912 n = xgetpasswd(&getps);
913 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
914 if (n == ((size_t)-2)) xexit(1);
915 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
916 getps.fd = getps.efd = -1;
917 getps.passwd = pwdagain;
918 getps.pwlen = sizeof(pwdagain)-1;
919 getps.echo = "Enter it again: ";
920 getps.flags = GETP_WAITFILL;
921 n = xgetpasswd(&getps);
922 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
923 if (n == ((size_t)-2)) xexit(1);
924 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
925 tfc_esay("Passwords are different, try again");
929 if (verbose) say_hint(pwdask, n, "Password hint");
930 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
931 memset(pwdask, 0, sizeof(pwdask));
932 memset(pwdagain, 0, sizeof(pwdagain));
935 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
936 xerror(NO, NO, YES, "hashing key");
940 if (tfc_saltsz > 0) {
941 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
942 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
944 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
945 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
946 memset(tfc_salt, 0, TFC_MAX_SALT);
949 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
950 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
955 tfc_yesno xtskeyset = NO;
958 if (!strcmp(genkeyf, "-")) krfd = 1;
959 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
960 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
961 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
962 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
963 if (verbose && xtskeyset == NO) {
964 tfc_esay("%s: password hashing done", progname);
965 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
966 tfc_esay("%s: Have a nice day!", progname);
969 if (ctr_mode == TFC_MODE_XTS) {
970 if (xtskeyset == NO) {
982 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
983 if (verbose) tfc_esay("%s: disabling signature verification on "
984 "requested partial decryption.", progname);
989 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
990 if (ctr_mode < TFC_MODE_OCB) {
991 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
992 "will be slower.", progname);
993 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
994 else skein_init(&sk, macbits);
998 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
999 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1003 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1004 if (do_full_key == NO) {
1005 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1006 tf_tweak_set(key, tweak);
1008 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1010 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1012 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1013 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1015 if (counter_opt == TFC_CTR_SHOW) {
1016 switch (do_outfmt) {
1017 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1018 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1019 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1022 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1029 if (verbose) tfc_esay("%s: password hashing done", progname);
1031 if (overwrite_source && srcfname) argv[idx] = srcfname;
1035 if (!strcmp(argv[idx], "-")) dfd = 1;
1036 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
1038 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1039 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
1041 dstfname = argv[idx];
1046 if (lseek(dfd, oseek, SEEK_SET) == -1)
1047 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1050 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1051 memset(&sigact, 0, sizeof(sigact));
1052 sigact.sa_flags = SA_RESTART;
1053 sigact.sa_handler = print_crypt_status;
1054 sigaction(SIGUSR1, &sigact, NULL);
1055 sigaction(SIGTSTP, &sigact, NULL);
1056 sigaction(SIGALRM, &sigact, NULL);
1057 if (status_timer) setup_next_alarm(status_timer);
1058 sigact.sa_handler = change_status_width;
1059 sigaction(SIGQUIT, &sigact, NULL);
1060 sigact.sa_handler = change_status_timer;
1061 sigaction(SIGUSR2, &sigact, NULL);
1063 sigact.sa_handler = print_crypt_status;
1064 sigaction(SIGINT, &sigact, NULL);
1065 sigaction(SIGTERM, &sigact, NULL);
1068 sigact.sa_handler = exit_sigterm;
1069 sigaction(SIGINT, &sigact, NULL);
1070 sigaction(SIGTERM, &sigact, NULL);
1072 memset(&sigact, 0, sizeof(struct sigaction));
1074 tfc_getcurtime(&delta_time);
1077 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1081 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1082 if (lio != NOSIZE) ldone += lio;
1083 else xerror(NO, NO, NO, "%s", dstfname);
1084 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1090 total_processed_dst += ldone;
1091 delta_processed += ldone;
1094 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1102 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1103 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1104 _ragain: lio = xread(sfd, pblk, lrem);
1105 if (lio == 0) do_stop = TFC_STOP_BEGAN;
1106 if (lio != NOSIZE) ldone += lio;
1108 if (errno != EIO && catch_all_errors != YES)
1109 xerror(NO, NO, NO, "%s", srcfname);
1110 switch (error_action) {
1111 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1112 case TFC_ERRACT_SYNC:
1113 case TFC_ERRACT_LSYNC:
1114 xerror(YES, NO, NO, "%s", srcfname);
1115 lio = ldone = lrem = lblock;
1116 memset(srcblk, 0, lio);
1117 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1118 else lseek(sfd, rdpos + lio, SEEK_SET);
1120 default: xerror(NO, NO, NO, "%s", srcfname); break;
1123 if (lio && lio < lrem) {
1128 total_processed_src += ldone;
1130 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1131 size_t orig = ldone;
1132 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1133 if (ldone > blksize) ldone = blksize;
1134 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1137 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1138 skein_update(&sk, srcblk, ldone);
1140 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1141 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1142 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1143 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1144 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1145 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1146 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1147 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1148 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1149 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1150 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1151 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1152 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1153 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1155 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1156 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1157 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1158 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1160 else if (ctr_mode == TFC_MODE_PLAIN)
1161 memcpy(dstblk, srcblk, ldone);
1163 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1164 skein_update(&sk, dstblk, ldone);
1165 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1170 _wagain: lio = xwrite(dfd, pblk, lrem);
1171 if (lio != NOSIZE) ldone += lio;
1172 else xerror(NO, NO, NO, "%s", dstfname);
1173 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1179 _nowrite: total_processed_dst += ldone;
1180 delta_processed += ldone;
1182 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1185 if (do_stop == TFC_STOP_FULL) goto _nomac;
1188 if (do_mac >= TFC_MAC_VRFY) {
1192 lrem = lblock = TF_FROM_BITS(macbits);
1193 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1194 _macragain: lio = xread(sfd, pblk, lrem);
1195 if (lio != NOSIZE) ldone += lio;
1197 if (errno != EIO && catch_all_errors != YES)
1198 xerror(NO, NO, NO, "%s", srcfname);
1199 switch (error_action) {
1200 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1201 case TFC_ERRACT_SYNC:
1202 case TFC_ERRACT_LSYNC:
1203 xerror(YES, NO, NO, "%s", srcfname);
1204 lio = ldone = lrem = lblock;
1205 memset(macvrfy, 0, lio);
1206 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1207 else lseek(sfd, rdpos + lio, SEEK_SET);
1209 default: xerror(NO, NO, NO, "%s", srcfname); break;
1212 if (lio && lio < lrem) {
1217 total_processed_src += ldone;
1222 if (!strcmp(do_mac_file, "-")) mfd = 0;
1223 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1224 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1225 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1226 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1227 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1228 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1229 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1230 lio = TF_FROM_BITS(macbits);
1231 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1233 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1237 if (ldone < TF_FROM_BITS(macbits)) {
1238 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1239 "not verifying", progname, ldone);
1244 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1245 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1247 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1248 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1249 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1250 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1251 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1252 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1254 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1256 tfc_esay("%s: signature is good", progname);
1258 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1259 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1264 if (quiet == NO) tfc_esay("%s: signature is BAD: "
1265 "wrong password, key, mode, or file is not signed", progname);
1269 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1270 memset(macresult, 0, sizeof(macresult));
1271 memset(tmpdata, 0, sizeof(tmpdata));
1274 else if (do_mac == TFC_MAC_SIGN) {
1275 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1276 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1278 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1279 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1280 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1281 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1282 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1283 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1284 memset(macresult, 0, sizeof(macresult));
1288 lio = lrem = TF_FROM_BITS(macbits);
1290 _macwagain: lio = xwrite(dfd, pblk, lrem);
1291 if (lio != NOSIZE) ldone += lio;
1292 else xerror(NO, NO, NO, "%s", dstfname);
1293 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1299 total_processed_dst += ldone;
1300 delta_processed += ldone;
1305 if (!strcmp(do_mac_file, "-")) mfd = 1;
1306 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1307 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1308 if (do_outfmt == TFC_OUTFMT_B64) {
1309 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1310 memset(tmpdata, 0, TFC_TMPSIZE);
1311 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1312 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1313 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1315 tmpdata[lrem] = '\n';
1318 lio = xwrite(mfd, tmpdata, lrem);
1320 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1321 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1322 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1326 memset(macvrfy, 0, sizeof(macvrfy));
1327 memset(macresult, 0, sizeof(macresult));
1328 memset(tmpdata, 0, sizeof(tmpdata));
1332 if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1334 if (do_preserve_time) fcopy_matime(dfd, &s_stat);
1336 if (ftrunc_dfd != NOFSIZE) if (ftruncate(dfd, (off_t)ftrunc_dfd) == -1) xerror(YES, NO, YES, "ftruncate(%d)", dfd);