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);
131 if (!strcmp(progname, "iotool")) {
132 do_edcrypt = TFC_DO_PLAIN;
134 ctr_mode = TFC_MODE_PLAIN;
138 while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:Pkzxc:l:qedn:vV:pwE:O:S:AmM:R:Z:WHD:")) != -1) {
141 read_defaults(optarg, NO);
150 if (!strcasecmp(optarg, "show"))
151 counter_opt = TFC_CTR_SHOW;
152 else if (!strcasecmp(optarg, "head"))
153 counter_opt = TFC_CTR_HEAD;
154 else if (!strcasecmp(optarg, "rand"))
155 counter_opt = TFC_CTR_RAND;
156 else if (!strcasecmp(optarg, "zero"))
157 counter_opt = TFC_CTR_ZERO;
158 else if (strchr(optarg, ':')) {
161 counter_opt = TFC_CTR_SSET;
164 s = d = optarg; t = NULL;
165 while ((s = strtok_r(d, ",", &t))) {
172 chr = (char)strtoul(s, &stoi, 16);
173 if (!str_empty(stoi)) continue;
174 x = (size_t)strtoul(ss, &stoi, 10);
175 if (!str_empty(stoi)) continue;
177 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
181 else counter_file = sksum_hashlist_file = optarg;
184 if (!strcasecmp(optarg, "ctr"))
185 ctr_mode = TFC_MODE_CTR;
186 else if (!strcasecmp(optarg, "stream"))
187 ctr_mode = TFC_MODE_STREAM;
188 else if (!strcasecmp(optarg, "cbc"))
189 ctr_mode = TFC_MODE_CBC;
190 else if (!strcasecmp(optarg, "ecb"))
191 ctr_mode = TFC_MODE_ECB;
192 else if (!strcasecmp(optarg, "xts"))
193 ctr_mode = TFC_MODE_XTS;
194 else if (!strcasecmp(optarg, "ocb"))
195 ctr_mode = TFC_MODE_OCB;
196 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
199 do_edcrypt = TFC_DO_PLAIN;
201 ctr_mode = TFC_MODE_PLAIN;
204 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
207 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
210 macbits = strtoul(optarg, &stoi, 10);
211 if (macbits == 0 || !str_empty(stoi) || macbits < 8
212 || macbits > TF_MAX_BITS || macbits % 8)
213 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
216 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
217 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
220 if (!strcasecmp(optarg, "key"))
221 mackey_opt = TFC_MACKEY_RAWKEY;
222 else if (!strcasecmp(optarg, "pwd"))
223 mackey_opt = TFC_MACKEY_PASSWORD;
225 mackey_opt = TFC_MACKEY_FILE;
233 rawkey = TFC_RAWKEY_KEYFILE;
236 rawkey = TFC_RAWKEY_ASKSTR;
239 rawkey = TFC_RAWKEY_ASKHEX;
250 if (maxlen != NOFSIZE) break;
252 maxlen = tfc_humanfsize(optarg, &stoi);
253 if (!str_empty(stoi)) {
254 maxlen = tfc_fnamesize(optarg, YES);
255 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
256 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
257 "%s: invalid count value", optarg);
259 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
260 if (counter_opt == TFC_CTR_HEAD)
261 maxlen += TF_BLOCK_SIZE;
264 overwrite_source = YES;
267 if (!strcmp(optarg, "xall")) {
268 catch_all_errors = YES;
271 if (!strcmp(optarg, "xseek")) {
272 ignore_seek_errors = YES;
275 if (!strcmp(optarg, "exit"))
276 error_action = TFC_ERRACT_EXIT;
277 else if (!strncmp(optarg, "cont", 4))
278 error_action = TFC_ERRACT_CONT;
279 else if (!strcmp(optarg, "sync"))
280 error_action = TFC_ERRACT_SYNC;
281 else if (!strcmp(optarg, "lsync"))
282 error_action = TFC_ERRACT_LSYNC;
283 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
286 s = d = optarg; t = NULL;
287 while ((s = strtok_r(d, ",", &t))) {
289 if (!strcmp(s, "sync"))
290 write_flags |= O_SYNC;
291 else if (!strcmp(s, "trunc"))
292 write_flags |= O_TRUNC;
293 else if (!strcmp(s, "fsync"))
295 else if (!strcmp(s, "pad"))
297 else if (!strcmp(s, "xtime"))
298 do_preserve_time = YES;
299 else if (!strcmp(s, "gibsize"))
300 do_stats_in_gibs = YES;
301 else if (!strcmp(s, "plainstats"))
302 do_statline_dynamic = NO;
303 else if (!strcmp(s, "statless"))
305 else if (!strcmp(s, "norepeat"))
307 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
309 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
310 mac_pw_prompt = s+10;
311 else if (!strcmp(s, "shorthex"))
312 do_full_hexdump = NO;
313 else if (!strcmp(s, "fullkey"))
315 else if (!strcmp(s, "showsecrets"))
317 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
319 blksize = (size_t)tfc_humanfsize(s, &stoi);
320 if (!str_empty(stoi)) {
321 blksize = (size_t)tfc_fnamesize(s, YES);
322 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
323 if (blksize == NOSIZE) xerror(NO, YES, YES,
324 "%s: invalid block size value", s);
326 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
327 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
328 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
329 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
330 "%s: block size exceeds %u bytes",
331 s, TFC_U(TFC_BLKSIZE));
333 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
335 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
336 if (!str_empty(stoi)) {
337 xtsblocks = (size_t)tfc_fnamesize(s, YES);
338 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
339 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
340 "%s: invalid blocks per xts block value", s);
342 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
343 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
344 "%s: nr of blocks per xts block is not round to %u bytes",
345 s, TFC_U(TFC_BLKSIZE));
346 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
347 "%s: nr of blocks per xts block exceeds %u bytes",
348 s, TFC_U(TFC_BLKSIZE));
350 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
352 iseek = tfc_humanfsize(s, &stoi);
353 if (!str_empty(stoi)) {
354 iseek = tfc_fnamesize(s, YES);
355 iseek = tfc_modifysize(iseek, strchr(s, ':'));
356 if (iseek == NOFSIZE) xerror(NO, YES, YES,
357 "%s: invalid iseek value", s);
359 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
360 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
362 "%s: not round to TF block size "
364 s, TFC_U(TF_BLOCK_SIZE));
365 iseek_blocks = iseek / TF_BLOCK_SIZE;
367 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
369 iseek = tfc_humanfsize(s, &stoi);
370 if (!str_empty(stoi)) {
371 iseek = tfc_fnamesize(s, YES);
372 iseek = tfc_modifysize(iseek, strchr(s, ':'));
373 if (iseek == NOFSIZE) xerror(NO, YES, YES,
374 "%s: invalid ixseek value", s);
376 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
378 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
380 iseek_blocks = tfc_humanfsize(s, &stoi);
381 if (!str_empty(stoi)) {
382 iseek_blocks = tfc_fnamesize(s, YES);
383 if (iseek_blocks == NOFSIZE)
385 "%s: invalid ictr value", s);
386 iseek_blocks /= TF_BLOCK_SIZE;
387 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
389 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
391 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
393 iseek_blocks = tfc_humanfsize(s, &stoi);
394 if (!str_empty(stoi)) {
395 iseek_blocks = tfc_fnamesize(s, YES);
396 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
397 if (iseek_blocks == NOFSIZE)
399 "%s: invalid ixctr value", s);
401 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
402 if (iseek_blocks % TF_BLOCK_SIZE)
404 "%s: not round to TF block size "
405 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
406 iseek_blocks /= TF_BLOCK_SIZE;
408 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
410 oseek = tfc_humanfsize(s, &stoi);
411 if (!str_empty(stoi)) {
412 oseek = tfc_fnamesize(s, YES);
413 oseek = tfc_modifysize(oseek, strchr(s, ':'));
414 if (oseek == NOFSIZE) xerror(NO, YES, YES,
415 "%s: invalid oseek value", s);
417 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
419 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
421 maxlen = tfc_humanfsize(s, &stoi);
422 if (!str_empty(stoi)) {
423 maxlen = tfc_fnamesize(s, YES);
424 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
425 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
426 "%s: invalid count value", s);
428 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
429 if (counter_opt == TFC_CTR_HEAD)
430 maxlen += TF_BLOCK_SIZE;
432 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
434 if (!strcmp(s, "tail")) {
435 do_ftrunc = TFC_FTRUNC_TAIL;
436 ftrunc_dfd = NOFSIZE;
439 do_ftrunc = TFC_DO_FTRUNC;
440 ftrunc_dfd = tfc_humanfsize(s, &stoi);
441 if (!str_empty(stoi)) {
442 ftrunc_dfd = tfc_fnamesize(s, YES);
443 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
444 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
445 "%s: invalid ftrunc value", s);
447 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
450 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
452 maxkeylen = tfc_humanfsize(s, &stoi);
453 if (!str_empty(stoi)) {
454 maxkeylen = tfc_fnamesize(s, YES);
455 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
456 if (maxkeylen == NOSIZE)
458 "%s: invalid key length value", s);
460 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
462 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
464 keyoffset = tfc_humanfsize(s, &stoi);
465 if (!str_empty(stoi)) {
466 keyoffset = tfc_fnamesize(s, YES);
467 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
468 if (keyoffset == NOFSIZE)
470 "%s: invalid key offset value", s);
472 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
474 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
476 ctrsz = tfc_humanfsize(s, &stoi);
477 if (!str_empty(stoi)) {
478 ctrsz = (size_t)tfc_fnamesize(s, YES);
479 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
482 "%s: invalid counter length value", s);
484 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
485 if (ctrsz > TF_BLOCK_SIZE)
486 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
488 else xerror(NO, YES, YES, "invalid option %s", s);
492 do_mac = TFC_MAC_SIGN;
493 if (strcasecmp(optarg, "mac") != 0)
494 do_mac_file = optarg;
497 do_mac = TFC_MAC_VRFY;
498 if (!strcasecmp(optarg, "drop"))
499 do_mac = TFC_MAC_DROP;
500 else if (strcasecmp(optarg, "mac") != 0)
501 do_mac_file = optarg;
504 if (do_mac != TFC_MAC_VRFY)
505 xerror(NO, YES, YES, "signature source was not specified");
506 do_mac = TFC_MAC_JUST_VRFY;
510 if (maxlen != NOFSIZE) {
511 if (c == 'Z') genzero_nr_bytes = maxlen;
512 else genrandom_nr_bytes = maxlen;
516 if (!strcasecmp(optarg, "cbs"))
518 else if (!strcasecmp(optarg, "ks"))
519 t = TF_FROM_BITS(TFC_KEY_BITS);
520 else if (!strcasecmp(optarg, "xks"))
521 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
522 else if (!strcasecmp(optarg, "iobs"))
525 t = tfc_humanfsize(optarg, &stoi);
526 if (!str_empty(stoi)) {
527 t = tfc_fnamesize(optarg, NO);
528 t = tfc_modifysize(t, strchr(optarg, ':'));
530 else t = tfc_modifysize(t, strchr(optarg, ':'));
532 if (c == 'Z') genzero_nr_bytes = maxlen = t;
533 else genrandom_nr_bytes = maxlen = t;
537 do_preserve_time = YES;
540 do_outfmt = TFC_OUTFMT_B64;
543 do_outfmt = TFC_OUTFMT_RAW;
546 do_outfmt = TFC_OUTFMT_HEX;
551 do_full_hexdump = NO;
558 td = strtod(optarg, &stoi);
559 status_timer = TFC_DTOUSECS(td);
560 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
568 if (!strcmp(progname, "tfbench")) {
569 if (!*(argv+optind)) usage();
571 td = strtod(*(argv+optind), &stoi);
572 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
574 "%s: invalid benchmark time in seconds", *(argv+optind));
575 bench_timer = TFC_DTOUSECS(td);
576 do_benchmark(bench_timer, td);
578 if (genrandom_nr_bytes) {
579 ctr_mode = TFC_MODE_STREAM;
580 do_edcrypt = TFC_DO_ENCRYPT;
581 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
583 if (genzero_nr_bytes) {
584 ctr_mode = TFC_MODE_PLAIN;
585 do_edcrypt = TFC_DO_PLAIN;
586 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
589 if (rawkey && password)
590 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
591 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
592 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
593 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
594 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
595 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
596 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
597 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
598 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
599 if (overwrite_source && counter_opt == TFC_CTR_RAND)
600 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
601 if (ctr_mode == TFC_MODE_PLAIN
602 && (do_edcrypt || do_mac || rawkey
603 || mackey_opt || counter_opt || counter_file))
604 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
612 memset(tfc_salt, 0, TFC_MAX_SALT);
614 if (!strcasecmp(saltf, "disable")) goto _nosalt;
616 if (!strcmp(saltf, "-")) saltfd = 0;
617 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
618 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
619 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
620 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
626 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
630 if (!strcmp(mackeyf, "-")) mkfd = 0;
631 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
632 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
634 skein_init(&sk, TFC_KEY_BITS);
641 lrem = lblock = sizeof(tmpdata);
642 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
643 _mkragain: lio = xread(mkfd, pblk, lrem);
644 if (lio == 0) do_stop = YES;
645 if (lio != NOSIZE) ldone += lio;
647 if (errno != EIO && catch_all_errors != YES)
648 xerror(NO, NO, NO, "%s", mackeyf);
649 switch (error_action) {
650 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
651 case TFC_ERRACT_SYNC:
652 case TFC_ERRACT_LSYNC:
653 xerror(YES, NO, NO, "%s", mackeyf);
654 lio = ldone = lrem = lblock;
655 memset(tmpdata, 0, lio);
656 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
657 else lseek(mkfd, rdpos + lio, SEEK_SET);
659 default: xerror(NO, NO, NO, "%s", mackeyf); break;
662 if (lio && lio < lrem) {
668 skein_update(&sk, tmpdata, ldone);
671 skein_final(mackey, &sk);
675 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
676 memset(&getps, 0, sizeof(struct getpasswd_state));
677 getps.fd = getps.efd = -1;
678 getps.passwd = pwdask;
679 getps.pwlen = sizeof(pwdask)-1;
680 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
681 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
682 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
683 getps.flags = GETP_WAITFILL;
684 n = xgetpasswd(&getps);
685 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
686 if (n == ((size_t)-2)) xexit(1);
687 if (verbose) say_hint(pwdask, n, "MAC password hint");
688 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
692 if ((strlen(progname) <= 9)
693 && ((!strcmp(progname, "sksum"))
694 || ((!memcmp(progname, "sk", 2))
695 && (!memcmp(progname+3, "sum", 3)
696 || !memcmp(progname+4, "sum", 3)
697 || !memcmp(progname+5, "sum", 3)
698 || !memcmp(progname+6, "sum", 3)))))
699 do_sksum(progname, argv+optind);
700 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
705 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
706 if (!strcmp(argv[idx], "-")) kfd = 0;
707 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
708 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
710 lio = strnlen(argv[idx], PATH_MAX);
711 memset(argv[idx], '*', lio);
718 if (do_full_key == NO && tweakf) {
721 if (!strcmp(tweakf, "-")) twfd = 0;
722 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
723 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
724 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
725 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
726 if (ldone < TF_TWEAK_SIZE)
727 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
734 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
736 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
737 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
738 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
740 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
742 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
743 if (verbose) tfc_esay("%s: disabling signature verification on "
744 "requested partial decryption.", progname);
748 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
749 maxlen = tfc_fdsize(sfd);
750 if (maxlen == NOFSIZE)
752 "Cannot verify embedded MAC with non-seekable source!");
753 maxlen -= TF_FROM_BITS(macbits);
755 srcfname = argv[idx];
759 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
760 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
762 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
763 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
765 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
770 if (!strcmp(counter_file, "-")) ctrfd = 0;
771 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
772 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
773 lio = xread(ctrfd, ctr, ctrsz);
774 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
775 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
778 else if (counter_opt == TFC_CTR_HEAD) {
781 lrem = lblock = ctrsz;
782 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
783 _ctrragain: lio = xread(sfd, pblk, lrem);
784 if (lio != NOSIZE) ldone += lio;
786 if (errno != EIO && catch_all_errors != YES)
787 xerror(NO, NO, NO, "%s", srcfname);
788 switch (error_action) {
789 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
790 case TFC_ERRACT_SYNC:
791 case TFC_ERRACT_LSYNC:
792 xerror(YES, NO, NO, "%s", srcfname);
793 lio = ldone = lrem = lblock;
795 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
796 else lseek(sfd, rdpos + lio, SEEK_SET);
798 default: xerror(NO, NO, NO, "%s", srcfname); break;
801 if (lio && lio < lrem) {
806 total_processed_src += ldone;
811 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
813 if (lseek(sfd, iseek, SEEK_SET) == -1)
814 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
817 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
819 if (verbose) tfc_esay("%s: hashing password", progname);
821 if (rawkey == TFC_RAWKEY_KEYFILE) {
822 tfc_yesno xtskeyset = NO;
826 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
827 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
828 _keyragain: lio = xread(kfd, pblk, lrem);
829 if (lio != NOSIZE) ldone += lio;
831 if (errno != EIO && catch_all_errors != YES)
832 xerror(NO, NO, NO, "reading key");
833 switch (error_action) {
834 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
835 case TFC_ERRACT_SYNC:
836 case TFC_ERRACT_LSYNC:
837 xerror(YES, NO, NO, "reading key");
838 lio = ldone = lrem = lblock;
840 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
841 else lseek(kfd, rdpos + lio, SEEK_SET);
843 default: xerror(NO, NO, NO, "reading key"); break;
846 if (lio && lio < lrem) {
851 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
853 if (ctr_mode == TFC_MODE_XTS) {
854 if (xtskeyset == NO) {
861 else if (rawkey == TFC_RAWKEY_ASKSTR) {
862 tfc_yesno xtskeyset = NO;
864 pblk = key; n = sizeof(key);
865 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
866 getps.fd = getps.efd = -1;
867 getps.passwd = (char *)pblk;
869 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
870 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
871 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
872 getps.flags = GETP_WAITFILL;
873 n = xgetpasswd(&getps);
874 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
875 if (n == ((size_t)-2)) xexit(1);
876 if (verbose) say_hint(pblk, n, "Raw string key hint");
877 if (ctr_mode == TFC_MODE_XTS) {
878 if (xtskeyset == NO) {
879 pblk = xtskey; n = sizeof(xtskey);
885 else if (rawkey == TFC_RAWKEY_ASKHEX) {
886 tfc_yesno xtskeyset = NO;
890 memset(&getps, 0, sizeof(struct getpasswd_state));
891 getps.fd = getps.efd = -1;
892 getps.passwd = pwdask;
893 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
894 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
895 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
896 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
897 getps.flags = GETP_WAITFILL;
898 n = xgetpasswd(&getps);
899 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
900 if (n == ((size_t)-2)) xexit(1);
902 tfc_esay("Please input even number of hex digits!");
903 goto _rawkey_hex_again;
905 hex2bin(pblk, pwdask);
906 memset(pwdask, 0, sizeof(pwdask));
907 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
908 if (ctr_mode == TFC_MODE_XTS) {
909 if (xtskeyset == NO) {
912 goto _rawkey_hex_again;
917 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
918 getps.fd = getps.efd = -1;
919 getps.passwd = pwdask;
920 getps.pwlen = sizeof(pwdask)-1;
921 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
922 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
923 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
924 getps.flags = GETP_WAITFILL;
925 n = xgetpasswd(&getps);
926 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
927 if (n == ((size_t)-2)) xexit(1);
928 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
929 getps.fd = getps.efd = -1;
930 getps.passwd = pwdagain;
931 getps.pwlen = sizeof(pwdagain)-1;
932 getps.echo = "Enter it again: ";
933 getps.flags = GETP_WAITFILL;
934 n = xgetpasswd(&getps);
935 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
936 if (n == ((size_t)-2)) xexit(1);
937 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
938 tfc_esay("Passwords are different, try again");
942 if (verbose) say_hint(pwdask, n, "Password hint");
943 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
944 memset(pwdask, 0, sizeof(pwdask));
945 memset(pwdagain, 0, sizeof(pwdagain));
948 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
949 xerror(NO, NO, YES, "hashing key");
953 if (tfc_saltsz > 0) {
954 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
955 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
957 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
958 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
959 memset(tfc_salt, 0, TFC_MAX_SALT);
962 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
963 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
968 tfc_yesno xtskeyset = NO;
971 if (!strcmp(genkeyf, "-")) krfd = 1;
972 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
973 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
974 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
975 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
976 if (verbose && xtskeyset == NO) {
977 tfc_esay("%s: password hashing done", progname);
978 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
979 tfc_esay("%s: Have a nice day!", progname);
982 if (ctr_mode == TFC_MODE_XTS) {
983 if (xtskeyset == NO) {
995 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
996 if (verbose) tfc_esay("%s: disabling signature verification on "
997 "requested partial decryption.", progname);
1002 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1003 if (ctr_mode < TFC_MODE_OCB) {
1004 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1005 "will be slower.", progname);
1006 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1007 else skein_init(&sk, macbits);
1011 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1012 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1016 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1017 if (do_full_key == NO) {
1018 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1019 tf_tweak_set(key, tweak);
1021 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1023 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1025 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1026 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1028 if (counter_opt == TFC_CTR_SHOW) {
1029 switch (do_outfmt) {
1030 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1031 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1032 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1035 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1042 if (verbose) tfc_esay("%s: password hashing done", progname);
1044 if (overwrite_source && srcfname) argv[idx] = srcfname;
1048 if (!strcmp(argv[idx], "-")) dfd = 1;
1049 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
1051 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1052 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
1054 dstfname = argv[idx];
1059 if (lseek(dfd, oseek, SEEK_SET) == -1)
1060 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1063 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1064 memset(&sigact, 0, sizeof(sigact));
1065 sigact.sa_flags = SA_RESTART;
1066 sigact.sa_handler = print_crypt_status;
1067 sigaction(SIGUSR1, &sigact, NULL);
1068 sigaction(SIGTSTP, &sigact, NULL);
1069 sigaction(SIGALRM, &sigact, NULL);
1070 if (status_timer) setup_next_alarm(status_timer);
1071 sigact.sa_handler = change_status_width;
1072 sigaction(SIGQUIT, &sigact, NULL);
1073 sigact.sa_handler = change_status_timer;
1074 sigaction(SIGUSR2, &sigact, NULL);
1076 sigact.sa_handler = print_crypt_status;
1077 sigaction(SIGINT, &sigact, NULL);
1078 sigaction(SIGTERM, &sigact, NULL);
1081 sigact.sa_handler = exit_sigterm;
1082 sigaction(SIGINT, &sigact, NULL);
1083 sigaction(SIGTERM, &sigact, NULL);
1085 memset(&sigact, 0, sizeof(struct sigaction));
1087 tfc_getcurtime(&delta_time);
1090 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1094 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1095 if (lio != NOSIZE) ldone += lio;
1096 else xerror(NO, NO, NO, "%s", dstfname);
1097 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1103 total_processed_dst += ldone;
1104 delta_processed += ldone;
1107 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1115 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1116 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1117 _ragain: lio = xread(sfd, pblk, lrem);
1118 if (lio == 0) do_stop = TFC_STOP_BEGAN;
1119 if (lio != NOSIZE) ldone += lio;
1121 if (errno != EIO && catch_all_errors != YES)
1122 xerror(NO, NO, NO, "%s", srcfname);
1123 switch (error_action) {
1124 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1125 case TFC_ERRACT_SYNC:
1126 case TFC_ERRACT_LSYNC:
1127 xerror(YES, NO, NO, "%s", srcfname);
1128 lio = ldone = lrem = lblock;
1129 memset(srcblk, 0, lio);
1130 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1131 else lseek(sfd, rdpos + lio, SEEK_SET);
1133 default: xerror(NO, NO, NO, "%s", srcfname); break;
1136 if (lio && lio < lrem) {
1141 total_processed_src += ldone;
1143 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1144 size_t orig = ldone;
1145 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1146 if (ldone > blksize) ldone = blksize;
1147 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1150 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1151 skein_update(&sk, srcblk, ldone);
1153 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1154 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1155 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1156 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1157 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1158 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1159 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1160 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1161 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1162 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1163 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1164 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1165 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1166 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1168 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1169 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1170 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1171 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1173 else if (ctr_mode == TFC_MODE_PLAIN)
1174 memcpy(dstblk, srcblk, ldone);
1176 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1177 skein_update(&sk, dstblk, ldone);
1178 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1183 _wagain: lio = xwrite(dfd, pblk, lrem);
1184 if (lio != NOSIZE) ldone += lio;
1185 else xerror(NO, NO, NO, "%s", dstfname);
1186 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1192 _nowrite: total_processed_dst += ldone;
1193 delta_processed += ldone;
1195 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1198 if (do_stop == TFC_STOP_FULL) goto _nomac;
1201 if (do_mac >= TFC_MAC_VRFY) {
1205 lrem = lblock = TF_FROM_BITS(macbits);
1206 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1207 _macragain: lio = xread(sfd, pblk, lrem);
1208 if (lio != NOSIZE) ldone += lio;
1210 if (errno != EIO && catch_all_errors != YES)
1211 xerror(NO, NO, NO, "%s", srcfname);
1212 switch (error_action) {
1213 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1214 case TFC_ERRACT_SYNC:
1215 case TFC_ERRACT_LSYNC:
1216 xerror(YES, NO, NO, "%s", srcfname);
1217 lio = ldone = lrem = lblock;
1218 memset(macvrfy, 0, lio);
1219 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1220 else lseek(sfd, rdpos + lio, SEEK_SET);
1222 default: xerror(NO, NO, NO, "%s", srcfname); break;
1225 if (lio && lio < lrem) {
1230 total_processed_src += ldone;
1235 if (!strcmp(do_mac_file, "-")) mfd = 0;
1236 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1237 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1238 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1239 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1240 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1241 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1242 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1243 lio = TF_FROM_BITS(macbits);
1244 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1246 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1250 if (ldone < TF_FROM_BITS(macbits)) {
1251 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1252 "not verifying", progname, ldone);
1257 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1258 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1260 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1261 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1262 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1263 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1264 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1265 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1267 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1269 tfc_esay("%s: signature is good", progname);
1271 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1272 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1277 if (quiet == NO) tfc_esay("%s: signature is BAD: "
1278 "wrong password, key, mode, or file is not signed", progname);
1282 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1283 memset(macresult, 0, sizeof(macresult));
1284 memset(tmpdata, 0, sizeof(tmpdata));
1287 else if (do_mac == TFC_MAC_SIGN) {
1288 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1289 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1291 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1292 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1293 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1294 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1295 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1296 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1297 memset(macresult, 0, sizeof(macresult));
1301 lio = lrem = TF_FROM_BITS(macbits);
1303 _macwagain: lio = xwrite(dfd, pblk, lrem);
1304 if (lio != NOSIZE) ldone += lio;
1305 else xerror(NO, NO, NO, "%s", dstfname);
1306 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1312 total_processed_dst += ldone;
1313 delta_processed += ldone;
1318 if (!strcmp(do_mac_file, "-")) mfd = 1;
1319 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1320 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1321 if (do_outfmt == TFC_OUTFMT_B64) {
1322 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1323 memset(tmpdata, 0, TFC_TMPSIZE);
1324 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1325 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1326 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1328 tmpdata[lrem] = '\n';
1331 lio = xwrite(mfd, tmpdata, lrem);
1333 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1334 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1335 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1339 memset(macvrfy, 0, sizeof(macvrfy));
1340 memset(macresult, 0, sizeof(macresult));
1341 memset(tmpdata, 0, sizeof(tmpdata));
1345 if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1347 if (do_preserve_time) fcopy_matime(dfd, &s_stat);
1349 if (do_ftrunc > TFC_NO_FTRUNC) {
1350 if (do_ftrunc == TFC_FTRUNC_TAIL) ftrunc_dfd = total_processed_dst;
1351 if (ftruncate(dfd, (off_t)ftrunc_dfd) == -1) xerror(YES, NO, YES, "ftruncate(%d)", dfd);