2 * tfcrypt -- high security Threefish encryption tool.
4 * tfcrypt is copyrighted:
5 * Copyright (C) 2012-2018 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 int main(int argc, char **argv)
90 char *s, *d, *t, *stoi;
93 progname = basename(argv[0]);
95 if (!isatty(2)) do_statline_dynamic = NO;
100 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
101 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
102 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
103 read_defaults(s, YES);
108 while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:Pkzxc:l:qedn:vV:pwE:O:S:AmM:R:Z:WHD:")) != -1) {
111 read_defaults(optarg, NO);
120 if (!strcasecmp(optarg, "show"))
121 counter_opt = TFC_CTR_SHOW;
122 else if (!strcasecmp(optarg, "head"))
123 counter_opt = TFC_CTR_HEAD;
124 else if (!strcasecmp(optarg, "rand"))
125 counter_opt = TFC_CTR_RAND;
126 else if (!strcasecmp(optarg, "zero"))
127 counter_opt = TFC_CTR_ZERO;
128 else counter_file = sksum_hashlist_file = optarg;
131 if (!strcasecmp(optarg, "ctr"))
132 ctr_mode = TFC_MODE_CTR;
133 else if (!strcasecmp(optarg, "stream"))
134 ctr_mode = TFC_MODE_STREAM;
135 else if (!strcasecmp(optarg, "cbc"))
136 ctr_mode = TFC_MODE_CBC;
137 else if (!strcasecmp(optarg, "ecb"))
138 ctr_mode = TFC_MODE_ECB;
139 else if (!strcasecmp(optarg, "xts"))
140 ctr_mode = TFC_MODE_XTS;
141 else if (!strcasecmp(optarg, "ocb"))
142 ctr_mode = TFC_MODE_OCB;
143 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
146 do_edcrypt = TFC_DO_PLAIN;
148 ctr_mode = TFC_MODE_PLAIN;
151 do_edcrypt = TFC_DO_ENCRYPT;
154 do_edcrypt = TFC_DO_DECRYPT;
157 macbits = strtoul(optarg, &stoi, 10);
158 if (macbits == 0 || !str_empty(stoi) || macbits < 8
159 || macbits > TF_MAX_BITS || macbits % 8)
160 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
163 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
164 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
167 if (!strcasecmp(optarg, "key"))
168 mackey_opt = TFC_MACKEY_RAWKEY;
169 else if (!strcasecmp(optarg, "pwd"))
170 mackey_opt = TFC_MACKEY_PASSWORD;
172 mackey_opt = TFC_MACKEY_FILE;
180 rawkey = TFC_RAWKEY_KEYFILE;
183 rawkey = TFC_RAWKEY_ASKSTR;
186 rawkey = TFC_RAWKEY_ASKHEX;
197 if (maxlen != NOFSIZE) break;
199 maxlen = tfc_humanfsize(optarg, &stoi);
200 if (!str_empty(stoi)) {
201 maxlen = tfc_fnamesize(optarg, YES);
202 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
203 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
204 "%s: invalid count value", optarg);
206 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
207 if (counter_opt == TFC_CTR_HEAD)
208 maxlen += TF_BLOCK_SIZE;
211 overwrite_source = YES;
214 if (!strcmp(optarg, "xall")) {
215 catch_all_errors = YES;
218 if (!strcmp(optarg, "xseek")) {
219 ignore_seek_errors = YES;
222 if (!strcmp(optarg, "exit"))
223 error_action = TFC_ERRACT_EXIT;
224 else if (!strncmp(optarg, "cont", 4))
225 error_action = TFC_ERRACT_CONT;
226 else if (!strcmp(optarg, "sync"))
227 error_action = TFC_ERRACT_SYNC;
228 else if (!strcmp(optarg, "lsync"))
229 error_action = TFC_ERRACT_LSYNC;
230 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
233 s = d = optarg; t = NULL;
234 while ((s = strtok_r(d, ",", &t))) {
236 if (!strcmp(s, "sync"))
237 write_flags |= O_SYNC;
238 else if (!strcmp(s, "trunc"))
239 write_flags |= O_TRUNC;
240 else if (!strcmp(s, "fsync"))
242 else if (!strcmp(s, "pad"))
244 else if (!strcmp(s, "xtime"))
245 do_preserve_time = YES;
246 else if (!strcmp(s, "gibsize"))
247 do_stats_in_gibs = YES;
248 else if (!strcmp(s, "plainstats"))
249 do_statline_dynamic = NO;
250 else if (!strcmp(s, "statless"))
252 else if (!strcmp(s, "norepeat"))
254 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
256 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
257 mac_pw_prompt = s+10;
258 else if (!strcmp(s, "shorthex"))
259 do_full_hexdump = NO;
260 else if (!strcmp(s, "fullkey"))
262 else if (!strcmp(s, "showsecrets"))
264 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
266 blksize = (size_t)tfc_humanfsize(s, &stoi);
267 if (!str_empty(stoi)) {
268 blksize = (size_t)tfc_fnamesize(s, YES);
269 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
270 if (blksize == NOSIZE) xerror(NO, YES, YES,
271 "%s: invalid block size value", s);
273 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
274 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
275 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
276 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
277 "%s: block size exceeds %u bytes",
278 s, TFC_U(TFC_BLKSIZE));
280 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
282 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
283 if (!str_empty(stoi)) {
284 xtsblocks = (size_t)tfc_fnamesize(s, YES);
285 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
286 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
287 "%s: invalid blocks per xts block value", s);
289 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
290 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
291 "%s: nr of blocks per xts block is not round to %u bytes",
292 s, TFC_U(TFC_BLKSIZE));
293 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
294 "%s: nr of blocks per xts block exceeds %u bytes",
295 s, TFC_U(TFC_BLKSIZE));
297 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
299 iseek = tfc_humanfsize(s, &stoi);
300 if (!str_empty(stoi)) {
301 iseek = tfc_fnamesize(s, YES);
302 iseek = tfc_modifysize(iseek, strchr(s, ':'));
303 if (iseek == NOFSIZE) xerror(NO, YES, YES,
304 "%s: invalid iseek value", s);
306 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
307 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
309 "%s: not round to TF block size "
311 s, TFC_U(TF_BLOCK_SIZE));
312 iseek_blocks = iseek / TF_BLOCK_SIZE;
314 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
316 iseek = tfc_humanfsize(s, &stoi);
317 if (!str_empty(stoi)) {
318 iseek = tfc_fnamesize(s, YES);
319 iseek = tfc_modifysize(iseek, strchr(s, ':'));
320 if (iseek == NOFSIZE) xerror(NO, YES, YES,
321 "%s: invalid ixseek value", s);
323 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
325 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
327 iseek_blocks = tfc_humanfsize(s, &stoi);
328 if (!str_empty(stoi)) {
329 iseek_blocks = tfc_fnamesize(s, YES);
330 if (iseek_blocks == NOFSIZE)
332 "%s: invalid ictr value", s);
333 iseek_blocks /= TF_BLOCK_SIZE;
334 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
336 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
338 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
340 iseek_blocks = tfc_humanfsize(s, &stoi);
341 if (!str_empty(stoi)) {
342 iseek_blocks = tfc_fnamesize(s, YES);
343 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
344 if (iseek_blocks == NOFSIZE)
346 "%s: invalid ixctr value", s);
348 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
349 if (iseek_blocks % TF_BLOCK_SIZE)
351 "%s: not round to TF block size "
352 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
353 iseek_blocks /= TF_BLOCK_SIZE;
355 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
357 oseek = tfc_humanfsize(s, &stoi);
358 if (!str_empty(stoi)) {
359 oseek = tfc_fnamesize(s, YES);
360 oseek = tfc_modifysize(oseek, strchr(s, ':'));
361 if (oseek == NOFSIZE) xerror(NO, YES, YES,
362 "%s: invalid oseek value", s);
364 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
366 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
368 maxlen = tfc_humanfsize(s, &stoi);
369 if (!str_empty(stoi)) {
370 maxlen = tfc_fnamesize(s, YES);
371 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
372 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
373 "%s: invalid count value", s);
375 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
376 if (counter_opt == TFC_CTR_HEAD)
377 maxlen += TF_BLOCK_SIZE;
379 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
381 ftrunc_dfd = tfc_humanfsize(s, &stoi);
382 if (!str_empty(stoi)) {
383 ftrunc_dfd = tfc_fnamesize(s, YES);
384 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
385 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
386 "%s: invalid ftrunc value", s);
388 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
390 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
392 maxkeylen = tfc_humanfsize(s, &stoi);
393 if (!str_empty(stoi)) {
394 maxkeylen = tfc_fnamesize(s, YES);
395 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
396 if (maxkeylen == NOSIZE)
398 "%s: invalid key length value", s);
400 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
402 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
404 keyoffset = tfc_humanfsize(s, &stoi);
405 if (!str_empty(stoi)) {
406 keyoffset = tfc_fnamesize(s, YES);
407 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
408 if (keyoffset == NOFSIZE)
410 "%s: invalid key offset value", s);
412 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
414 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
416 ctrsz = tfc_humanfsize(s, &stoi);
417 if (!str_empty(stoi)) {
418 ctrsz = (size_t)tfc_fnamesize(s, YES);
419 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
422 "%s: invalid counter length value", s);
424 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
425 if (ctrsz > TF_BLOCK_SIZE)
426 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
428 else xerror(NO, YES, YES, "invalid option %s", s);
432 do_mac = TFC_MAC_SIGN;
433 if (strcasecmp(optarg, "mac") != 0)
434 do_mac_file = optarg;
437 do_mac = TFC_MAC_VRFY;
438 if (!strcasecmp(optarg, "drop"))
439 do_mac = TFC_MAC_DROP;
440 else if (strcasecmp(optarg, "mac") != 0)
441 do_mac_file = optarg;
444 if (do_mac != TFC_MAC_VRFY)
445 xerror(NO, YES, YES, "signature source was not specified");
446 do_mac = TFC_MAC_JUST_VRFY;
450 if (maxlen != NOFSIZE) {
451 if (c == 'Z') genzero_nr_bytes = maxlen;
452 else genrandom_nr_bytes = maxlen;
456 if (!strcasecmp(optarg, "cbs"))
458 else if (!strcasecmp(optarg, "ks"))
459 t = TF_FROM_BITS(TFC_KEY_BITS);
460 else if (!strcasecmp(optarg, "xks"))
461 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
462 else if (!strcasecmp(optarg, "iobs"))
465 t = tfc_humanfsize(optarg, &stoi);
466 if (!str_empty(stoi)) {
467 t = tfc_fnamesize(optarg, NO);
468 t = tfc_modifysize(t, strchr(optarg, ':'));
470 else t = tfc_modifysize(t, strchr(optarg, ':'));
472 if (c == 'Z') genzero_nr_bytes = maxlen = t;
473 else genrandom_nr_bytes = maxlen = t;
477 do_preserve_time = YES;
480 do_outfmt = TFC_OUTFMT_B64;
483 do_outfmt = TFC_OUTFMT_RAW;
486 do_outfmt = TFC_OUTFMT_HEX;
497 td = strtod(optarg, &stoi);
498 status_timer = TFC_DTOUSECS(td);
499 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
507 if (!strcmp(progname, "tfbench")) {
508 if (!*(argv+optind)) usage();
510 td = strtod(*(argv+optind), &stoi);
511 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
513 "%s: invalid benchmark time in seconds", *(argv+optind));
514 bench_timer = TFC_DTOUSECS(td);
515 do_benchmark(bench_timer, td);
517 if (genrandom_nr_bytes) {
518 ctr_mode = TFC_MODE_STREAM;
519 do_edcrypt = TFC_DO_ENCRYPT;
520 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
522 if (genzero_nr_bytes) {
523 ctr_mode = TFC_MODE_PLAIN;
524 do_edcrypt = TFC_DO_PLAIN;
525 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
528 if (rawkey && password)
529 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
530 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
531 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
532 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
533 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
534 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
535 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
536 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
537 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
538 if (overwrite_source && counter_opt == TFC_CTR_RAND)
539 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
540 if (ctr_mode == TFC_MODE_PLAIN
541 && (do_edcrypt || do_mac || rawkey
542 || mackey_opt || counter_opt || counter_file))
543 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
551 memset(tfc_salt, 0, TFC_MAX_SALT);
553 if (!strcasecmp(saltf, "disable")) goto _nosalt;
555 if (!strcmp(saltf, "-")) saltfd = 0;
556 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
557 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
558 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
559 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
565 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
569 if (!strcmp(mackeyf, "-")) mkfd = 0;
570 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
571 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
573 skein_init(&sk, TFC_KEY_BITS);
580 lrem = lblock = sizeof(tmpdata);
581 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
582 _mkragain: lio = xread(mkfd, pblk, lrem);
583 if (lio == 0) do_stop = YES;
584 if (lio != NOSIZE) ldone += lio;
586 if (errno != EIO && catch_all_errors != YES)
587 xerror(NO, NO, NO, "%s", mackeyf);
588 switch (error_action) {
589 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
590 case TFC_ERRACT_SYNC:
591 case TFC_ERRACT_LSYNC:
592 xerror(YES, NO, NO, "%s", mackeyf);
593 lio = ldone = lrem = lblock;
594 memset(tmpdata, 0, lio);
595 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
596 else lseek(mkfd, rdpos + lio, SEEK_SET);
598 default: xerror(NO, NO, NO, "%s", mackeyf); break;
601 if (lio && lio < lrem) {
607 skein_update(&sk, tmpdata, ldone);
610 skein_final(mackey, &sk);
614 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
615 memset(&getps, 0, sizeof(struct getpasswd_state));
616 getps.fd = getps.efd = -1;
617 getps.passwd = pwdask;
618 getps.pwlen = sizeof(pwdask)-1;
619 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
620 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
621 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
622 getps.flags = GETP_WAITFILL;
623 n = xgetpasswd(&getps);
624 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
625 if (n == ((size_t)-2)) xexit(1);
626 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
628 skein(tmpdata, TF_MAX_BITS, NULL, mackey, TF_FROM_BITS(TF_MAX_BITS));
629 xor_shrink(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, tmpdata, TF_FROM_BITS(TF_MAX_BITS));
630 tfc_nfsay(stderr, "MAC password hint: ");
631 mehexdump(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, TF_SIZE_UNIT, 1);
632 memset(tmpdata, 0, sizeof(tmpdata));
637 if ((strlen(progname) <= 9)
638 && ((!strcmp(progname, "sksum"))
639 || ((!memcmp(progname, "sk", 2))
640 && (!memcmp(progname+3, "sum", 3)
641 || !memcmp(progname+4, "sum", 3)
642 || !memcmp(progname+5, "sum", 3)
643 || !memcmp(progname+6, "sum", 3)))))
644 do_sksum(progname, argv+optind);
645 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
650 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
651 if (!strcmp(argv[idx], "-")) kfd = 0;
652 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
653 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
655 lio = strnlen(argv[idx], PATH_MAX);
656 memset(argv[idx], '*', lio);
663 if (do_full_key == NO && tweakf) {
666 if (!strcmp(tweakf, "-")) twfd = 0;
667 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
668 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
669 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
670 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
671 if (ldone < TF_TWEAK_SIZE)
672 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
679 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
681 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
682 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
683 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
685 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
687 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
688 if (verbose) tfc_esay("%s: disabling signature verification on "
689 "requested partial decryption.", progname);
693 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
694 maxlen = tfc_fdsize(sfd);
695 if (maxlen == NOFSIZE)
697 "Cannot verify embedded MAC with non-seekable source!");
698 maxlen -= TF_FROM_BITS(macbits);
700 srcfname = argv[idx];
704 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
705 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
707 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
708 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
710 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
715 if (!strcmp(counter_file, "-")) ctrfd = 0;
716 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
717 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
718 lio = xread(ctrfd, ctr, ctrsz);
719 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
720 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
723 else if (counter_opt == TFC_CTR_HEAD) {
726 lrem = lblock = ctrsz;
727 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
728 _ctrragain: lio = xread(sfd, pblk, lrem);
729 if (lio != NOSIZE) ldone += lio;
731 if (errno != EIO && catch_all_errors != YES)
732 xerror(NO, NO, NO, "%s", srcfname);
733 switch (error_action) {
734 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
735 case TFC_ERRACT_SYNC:
736 case TFC_ERRACT_LSYNC:
737 xerror(YES, NO, NO, "%s", srcfname);
738 lio = ldone = lrem = lblock;
740 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
741 else lseek(sfd, rdpos + lio, SEEK_SET);
743 default: xerror(NO, NO, NO, "%s", srcfname); break;
746 if (lio && lio < lrem) {
751 total_processed_src += ldone;
756 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
758 if (lseek(sfd, iseek, SEEK_SET) == -1)
759 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
762 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
764 if (verbose) tfc_esay("%s: hashing password", progname);
766 if (rawkey == TFC_RAWKEY_KEYFILE) {
767 tfc_yesno xtskeyset = NO;
771 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
772 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
773 _keyragain: lio = xread(kfd, pblk, lrem);
774 if (lio != NOSIZE) ldone += lio;
776 if (errno != EIO && catch_all_errors != YES)
777 xerror(NO, NO, NO, "reading key");
778 switch (error_action) {
779 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
780 case TFC_ERRACT_SYNC:
781 case TFC_ERRACT_LSYNC:
782 xerror(YES, NO, NO, "reading key");
783 lio = ldone = lrem = lblock;
785 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
786 else lseek(kfd, rdpos + lio, SEEK_SET);
788 default: xerror(NO, NO, NO, "reading key"); break;
791 if (lio && lio < lrem) {
796 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
798 if (ctr_mode == TFC_MODE_XTS) {
799 if (xtskeyset == NO) {
806 else if (rawkey == TFC_RAWKEY_ASKSTR) {
807 tfc_yesno xtskeyset = NO;
809 pblk = key; n = sizeof(key);
810 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
811 getps.fd = getps.efd = -1;
812 getps.passwd = (char *)pblk;
814 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
815 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
816 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
817 getps.flags = GETP_WAITFILL;
818 n = xgetpasswd(&getps);
819 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
820 if (n == ((size_t)-2)) xexit(1);
821 if (ctr_mode == TFC_MODE_XTS) {
822 if (xtskeyset == NO) {
823 pblk = xtskey; n = sizeof(xtskey);
829 else if (rawkey == TFC_RAWKEY_ASKHEX) {
830 tfc_yesno xtskeyset = NO;
834 memset(&getps, 0, sizeof(struct getpasswd_state));
835 getps.fd = getps.efd = -1;
836 getps.passwd = pwdask;
837 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
838 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
839 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
840 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
841 getps.flags = GETP_WAITFILL;
842 n = xgetpasswd(&getps);
843 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
844 if (n == ((size_t)-2)) xexit(1);
846 tfc_esay("Please input even number of hex digits!");
847 goto _rawkey_hex_again;
849 hex2bin(pblk, pwdask);
850 memset(pwdask, 0, sizeof(pwdask));
851 if (ctr_mode == TFC_MODE_XTS) {
852 if (xtskeyset == NO) {
855 goto _rawkey_hex_again;
860 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
861 getps.fd = getps.efd = -1;
862 getps.passwd = pwdask;
863 getps.pwlen = sizeof(pwdask)-1;
864 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
865 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
866 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
867 getps.flags = GETP_WAITFILL;
868 n = xgetpasswd(&getps);
869 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
870 if (n == ((size_t)-2)) xexit(1);
871 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
872 getps.fd = getps.efd = -1;
873 getps.passwd = pwdagain;
874 getps.pwlen = sizeof(pwdagain)-1;
875 getps.echo = "Enter it again: ";
876 getps.flags = GETP_WAITFILL;
877 n = xgetpasswd(&getps);
878 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
879 if (n == ((size_t)-2)) xexit(1);
880 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
881 tfc_esay("Passwords are different, try again");
885 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
886 memset(pwdask, 0, sizeof(pwdask));
887 memset(pwdagain, 0, sizeof(pwdagain));
890 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
891 xerror(NO, NO, YES, "hashing key");
895 if (tfc_saltsz > 0) {
896 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
897 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
899 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
900 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
901 memset(tfc_salt, 0, TFC_MAX_SALT);
904 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
905 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
910 tfc_yesno xtskeyset = NO;
913 if (!strcmp(genkeyf, "-")) krfd = 1;
914 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
915 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
916 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
917 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
918 if (verbose && xtskeyset == NO) {
919 tfc_esay("%s: password hashing done", progname);
920 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
921 tfc_esay("%s: Have a nice day!", progname);
924 if (ctr_mode == TFC_MODE_XTS) {
925 if (xtskeyset == NO) {
937 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
938 if (verbose) tfc_esay("%s: disabling signature verification on "
939 "requested partial decryption.", progname);
944 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
945 if (ctr_mode < TFC_MODE_OCB) {
946 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
947 "will be slower.", progname);
948 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
949 else skein_init(&sk, macbits);
953 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
954 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
958 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
959 if (do_full_key == NO) {
960 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
961 tf_tweak_set(key, tweak);
963 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
965 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
967 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
968 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
970 if (counter_opt == TFC_CTR_SHOW) {
972 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
973 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
974 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
977 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
984 if (verbose) tfc_esay("%s: password hashing done", progname);
986 if (overwrite_source && srcfname) argv[idx] = srcfname;
990 if (!strcmp(argv[idx], "-")) dfd = 1;
991 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
993 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
994 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
996 dstfname = argv[idx];
1001 if (lseek(dfd, oseek, SEEK_SET) == -1)
1002 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1005 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1006 memset(&sigact, 0, sizeof(sigact));
1007 sigact.sa_flags = SA_RESTART;
1008 sigact.sa_handler = print_crypt_status;
1009 sigaction(SIGUSR1, &sigact, NULL);
1010 sigaction(SIGTSTP, &sigact, NULL);
1011 sigaction(SIGALRM, &sigact, NULL);
1012 if (status_timer) setup_next_alarm(status_timer);
1013 sigact.sa_handler = change_status_width;
1014 sigaction(SIGQUIT, &sigact, NULL);
1015 sigact.sa_handler = change_status_timer;
1016 sigaction(SIGUSR2, &sigact, NULL);
1018 sigact.sa_handler = print_crypt_status;
1019 sigaction(SIGINT, &sigact, NULL);
1020 sigaction(SIGTERM, &sigact, NULL);
1023 sigact.sa_handler = exit_sigterm;
1024 sigaction(SIGINT, &sigact, NULL);
1025 sigaction(SIGTERM, &sigact, NULL);
1027 memset(&sigact, 0, sizeof(struct sigaction));
1029 tfc_getcurtime(&delta_time);
1032 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1036 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1037 if (lio != NOSIZE) ldone += lio;
1038 else xerror(NO, NO, NO, "%s", dstfname);
1039 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1045 total_processed_dst += ldone;
1046 delta_processed += ldone;
1049 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1057 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1058 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1059 _ragain: lio = xread(sfd, pblk, lrem);
1060 if (lio == 0) do_stop = TFC_STOP_BEGAN;
1061 if (lio != NOSIZE) ldone += lio;
1063 if (errno != EIO && catch_all_errors != YES)
1064 xerror(NO, NO, NO, "%s", srcfname);
1065 switch (error_action) {
1066 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1067 case TFC_ERRACT_SYNC:
1068 case TFC_ERRACT_LSYNC:
1069 xerror(YES, NO, NO, "%s", srcfname);
1070 lio = ldone = lrem = lblock;
1071 memset(srcblk, 0, lio);
1072 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1073 else lseek(sfd, rdpos + lio, SEEK_SET);
1075 default: xerror(NO, NO, NO, "%s", srcfname); break;
1078 if (lio && lio < lrem) {
1083 total_processed_src += ldone;
1085 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1086 size_t orig = ldone;
1087 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1088 if (ldone > blksize) ldone = blksize;
1089 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1092 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1093 skein_update(&sk, srcblk, ldone);
1095 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1096 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1097 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1098 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1099 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1100 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1101 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1102 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1103 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1104 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1105 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1106 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1107 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1108 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1110 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1111 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1112 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1113 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1115 else if (ctr_mode == TFC_MODE_PLAIN)
1116 memcpy(dstblk, srcblk, ldone);
1118 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1119 skein_update(&sk, dstblk, ldone);
1120 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1125 _wagain: lio = xwrite(dfd, pblk, lrem);
1126 if (lio != NOSIZE) ldone += lio;
1127 else xerror(NO, NO, NO, "%s", dstfname);
1128 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1134 _nowrite: total_processed_dst += ldone;
1135 delta_processed += ldone;
1137 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1140 if (do_stop == TFC_STOP_FULL) goto _nomac;
1143 if (do_mac >= TFC_MAC_VRFY) {
1147 lrem = lblock = TF_FROM_BITS(macbits);
1148 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1149 _macragain: lio = xread(sfd, pblk, lrem);
1150 if (lio != NOSIZE) ldone += lio;
1152 if (errno != EIO && catch_all_errors != YES)
1153 xerror(NO, NO, NO, "%s", srcfname);
1154 switch (error_action) {
1155 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1156 case TFC_ERRACT_SYNC:
1157 case TFC_ERRACT_LSYNC:
1158 xerror(YES, NO, NO, "%s", srcfname);
1159 lio = ldone = lrem = lblock;
1160 memset(macvrfy, 0, lio);
1161 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1162 else lseek(sfd, rdpos + lio, SEEK_SET);
1164 default: xerror(NO, NO, NO, "%s", srcfname); break;
1167 if (lio && lio < lrem) {
1172 total_processed_src += ldone;
1177 if (!strcmp(do_mac_file, "-")) mfd = 0;
1178 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1179 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1180 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1181 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1182 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1183 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1184 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1185 lio = TF_FROM_BITS(macbits);
1186 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1188 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1192 if (ldone < TF_FROM_BITS(macbits)) {
1193 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1194 "not verifying", progname, ldone);
1199 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1200 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1202 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1203 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1204 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1205 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1206 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1207 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1209 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1211 tfc_esay("%s: signature is good", progname);
1213 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1214 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1219 if (quiet == NO) tfc_esay("%s: signature is BAD: "
1220 "wrong password, key, mode, or file is not signed", progname);
1224 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1225 memset(macresult, 0, sizeof(macresult));
1226 memset(tmpdata, 0, sizeof(tmpdata));
1229 else if (do_mac == TFC_MAC_SIGN) {
1230 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1231 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1233 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1234 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1235 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1236 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1237 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1238 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1239 memset(macresult, 0, sizeof(macresult));
1243 lio = lrem = TF_FROM_BITS(macbits);
1245 _macwagain: lio = xwrite(dfd, pblk, lrem);
1246 if (lio != NOSIZE) ldone += lio;
1247 else xerror(NO, NO, NO, "%s", dstfname);
1248 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1254 total_processed_dst += ldone;
1255 delta_processed += ldone;
1260 if (!strcmp(do_mac_file, "-")) mfd = 1;
1261 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1262 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1263 if (do_outfmt == TFC_OUTFMT_B64) {
1264 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1265 memset(tmpdata, 0, TFC_TMPSIZE);
1266 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1267 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1268 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1270 tmpdata[lrem] = '\n';
1273 lio = xwrite(mfd, tmpdata, lrem);
1275 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1276 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1277 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1281 memset(macvrfy, 0, sizeof(macvrfy));
1282 memset(macresult, 0, sizeof(macresult));
1283 memset(tmpdata, 0, sizeof(tmpdata));
1287 if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1289 if (do_preserve_time) fcopy_matime(dfd, &s_stat);
1291 if (ftrunc_dfd != NOFSIZE) if (ftruncate(dfd, (off_t)ftrunc_dfd) == -1) xerror(YES, NO, YES, "ftruncate(%d)", dfd);