2 * tfcrypt -- high security Threefish encryption tool.
4 * tfcrypt is copyrighted:
5 * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
7 * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 static tfc_byte svctr[TF_BLOCK_SIZE];
32 static tfc_fsize rwd, do_read_loops, loopcnt;
34 static void open_log(const char *logfile)
39 if (!strcmp(logfile, "-")) return;
43 fd = xopen(logfile, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC);
46 if (dup2(fd, 2) == -1) xexit(2);
48 do_statline_dynamic = NO;
51 static int getps_filter(struct getpasswd_state *getps, char chr, size_t pos)
54 getps->retn = ((size_t)-2);
60 static int getps_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
63 getps->retn = ((size_t)-2);
66 if (chr >= '0' && chr <= '9') return 1;
67 if (chr >= 'a' && chr <= 'f') return 1;
68 if (chr >= 'A' && chr <= 'F') return 1;
69 if (chr == '\x7f' || chr == '\x08'
70 || chr == '\x15' || chr == '\x17') return 1;
74 static inline int isctrlchr(int c)
77 if (c >= 0 && c <= 31) return 1;
78 if (c == 127) return 1;
82 static int getps_plain_filter(struct getpasswd_state *getps, char chr, size_t pos)
86 x = getps_filter(getps, chr, pos);
89 if (pos < getps->pwlen && !isctrlchr(chr))
90 write(getps->efd, &chr, sizeof(char));
94 static int getps_plain_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
98 x = getps_hex_filter(getps, chr, pos);
101 if (pos < getps->pwlen && !isctrlchr(chr))
102 write(getps->efd, &chr, sizeof(char));
106 static void make_hint(void *hint, size_t szhint, const void *data, size_t szdata)
108 char t[TF_FROM_BITS(TF_MAX_BITS)];
110 skein(t, TF_MAX_BITS, NULL, data, szdata);
111 xor_shrink(hint, szhint, t, sizeof(t));
112 memset(t, 0, sizeof(t));
115 static void raw_say_hint(void *hint, size_t szhint, const void *data, size_t szdata, const char *prompt)
117 make_hint(hint, szhint, data, szdata);
118 if (prompt) tfc_nfsay(stderr, "%s: ", prompt);
119 mehexdump(hint, szhint, szhint, 1);
120 memset(hint, 0, szhint);
123 static void say_hint(const void *data, size_t szdata, const char *prompt)
125 char t[TF_SIZE_UNIT];
126 raw_say_hint(t, TF_SIZE_UNIT, data, szdata, prompt);
127 /* t[] is erased (automatically) */
130 int main(int argc, char **argv)
134 char *s, *d, *t, *stoi;
138 progname = basename(argv[0]);
140 if (!isatty(2)) do_statline_dynamic = NO;
145 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
146 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
147 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
148 read_defaults(s, YES);
152 if (!strcmp(progname, "iotool")) {
153 do_edcrypt = TFC_DO_PLAIN;
155 ctr_mode = TFC_MODE_PLAIN;
159 while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:Pkzxc:l:qedn:vV:pwE:o:O:S:AmuM:R:Z:WHD:")) != -1) {
162 read_defaults(optarg, NO);
171 if (!strcasecmp(optarg, "show"))
172 counter_opt = TFC_CTR_SHOW;
173 else if (!strcasecmp(optarg, "head"))
174 counter_opt = TFC_CTR_HEAD;
175 else if (!strcasecmp(optarg, "rand"))
176 counter_opt = TFC_CTR_RAND;
177 else if (!strcasecmp(optarg, "zero"))
178 counter_opt = TFC_CTR_ZERO;
179 else if (strchr(optarg, ':')) {
182 counter_opt = TFC_CTR_SSET;
185 s = d = optarg; t = NULL;
186 while ((s = strtok_r(d, ",", &t))) {
193 chr = (char)strtoul(s, &stoi, 16);
194 if (!str_empty(stoi)) continue;
195 x = (size_t)strtoul(ss, &stoi, 10);
196 if (!str_empty(stoi)) continue;
198 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
202 else counter_file = sksum_hashlist_file = optarg;
205 if (!strcasecmp(optarg, "ctr"))
206 ctr_mode = TFC_MODE_CTR;
207 else if (!strcasecmp(optarg, "stream"))
208 ctr_mode = TFC_MODE_STREAM;
209 else if (!strcasecmp(optarg, "cbc"))
210 ctr_mode = TFC_MODE_CBC;
211 else if (!strcasecmp(optarg, "ecb"))
212 ctr_mode = TFC_MODE_ECB;
213 else if (!strcasecmp(optarg, "xts"))
214 ctr_mode = TFC_MODE_XTS;
215 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
218 do_edcrypt = TFC_DO_PLAIN;
220 ctr_mode = TFC_MODE_PLAIN;
223 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
226 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
229 macbits = strtoul(optarg, &stoi, 10);
230 if (macbits == 0 || !str_empty(stoi) || macbits < 8
231 || macbits > TF_MAX_BITS || macbits % 8)
232 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
235 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
236 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
239 if (!strcasecmp(optarg, "key"))
240 mackey_opt = TFC_MACKEY_RAWKEY;
241 else if (!strcasecmp(optarg, "pwd"))
242 mackey_opt = TFC_MACKEY_PASSWORD;
244 mackey_opt = TFC_MACKEY_FILE;
252 rawkey = TFC_RAWKEY_KEYFILE;
255 rawkey = TFC_RAWKEY_ASKSTR;
258 rawkey = TFC_RAWKEY_ASKHEX;
269 if (maxlen != NOFSIZE) break;
271 maxlen = tfc_humanfsize(optarg, &stoi);
272 if (!str_empty(stoi)) {
273 maxlen = tfc_fnamesize(optarg, YES);
274 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
275 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
276 "%s: invalid count value", optarg);
278 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
279 if (counter_opt == TFC_CTR_HEAD)
280 maxlen += TF_BLOCK_SIZE;
283 overwrite_source = YES;
286 if (!strcmp(optarg, "xall")) {
287 catch_all_errors = YES;
290 if (!strcmp(optarg, "xseek")) {
291 ignore_seek_errors = YES;
294 if (!strcmp(optarg, "exit"))
295 error_action = TFC_ERRACT_EXIT;
296 else if (!strncmp(optarg, "cont", 4))
297 error_action = TFC_ERRACT_CONT;
298 else if (!strcmp(optarg, "sync"))
299 error_action = TFC_ERRACT_SYNC;
300 else if (!strcmp(optarg, "lsync"))
301 error_action = TFC_ERRACT_LSYNC;
302 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
308 s = d = optarg; t = NULL;
309 while ((s = strtok_r(d, ",", &t))) {
311 if (!strcmp(s, "sync"))
312 write_flags |= O_SYNC;
313 else if (!strcmp(s, "trunc"))
314 write_flags |= O_TRUNC;
315 else if (!strcmp(s, "append"))
316 write_flags |= O_APPEND;
317 else if (!strcmp(s, "fsync"))
319 else if (!strcmp(s, "pad"))
321 else if (!strcmp(s, "ro"))
323 else if (!strcmp(s, "rw"))
325 else if (!strcmp(s, "xtime"))
326 do_preserve_time = YES;
327 else if (!strcmp(s, "gibsize"))
328 do_stats_in_gibs = YES;
329 else if (!strcmp(s, "plainstats"))
330 do_statline_dynamic = NO;
331 else if (!strcmp(s, "statless"))
333 else if (!strcmp(s, "norepeat"))
335 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
337 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
338 mac_pw_prompt = s+10;
339 else if (!strcmp(s, "shorthex"))
340 do_full_hexdump = NO;
341 else if (!strcmp(s, "fullkey"))
343 else if (!strcmp(s, "showsecrets"))
345 else if (!strcmp(s, "finished"))
346 show_when_done = YES;
347 else if (!strcmp(s, "pid"))
349 else if (!strncmp(s, "readloops", 9) && *(s+9) == '=') {
350 do_read_loops = tfc_humanfsize(s+10, &stoi);
351 if (!str_empty(stoi)) do_read_loops = NOSIZE;
353 else if (!strncmp(s, "logfile", 7) && *(s+7) == '=')
355 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
357 blksize = (size_t)tfc_humanfsize(s, &stoi);
358 if (!str_empty(stoi)) {
359 blksize = (size_t)tfc_fnamesize(s, YES);
360 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
361 if (blksize == NOSIZE) xerror(NO, YES, YES,
362 "%s: invalid block size value", s);
364 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
365 if (do_edcrypt != TFC_DO_PLAIN && blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
366 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
367 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
368 "%s: block size exceeds %u bytes",
369 s, TFC_U(TFC_BLKSIZE));
371 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
373 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
374 if (!str_empty(stoi)) {
375 xtsblocks = (size_t)tfc_fnamesize(s, YES);
376 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
377 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
378 "%s: invalid blocks per xts block value", s);
380 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
381 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
382 "%s: nr of blocks per xts block is not round to %u bytes",
383 s, TFC_U(TFC_BLKSIZE));
384 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
385 "%s: nr of blocks per xts block exceeds %u bytes",
386 s, TFC_U(TFC_BLKSIZE));
388 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
390 iseek = tfc_humanfsize(s, &stoi);
391 if (!str_empty(stoi)) {
392 iseek = tfc_fnamesize(s, YES);
393 iseek = tfc_modifysize(iseek, strchr(s, ':'));
394 if (iseek == NOFSIZE) xerror(NO, YES, YES,
395 "%s: invalid iseek value", s);
397 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
398 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
400 "%s: not round to TF block size "
402 s, TFC_U(TF_BLOCK_SIZE));
403 iseek_blocks = iseek / TF_BLOCK_SIZE;
405 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
407 iseek = tfc_humanfsize(s, &stoi);
408 if (!str_empty(stoi)) {
409 iseek = tfc_fnamesize(s, YES);
410 iseek = tfc_modifysize(iseek, strchr(s, ':'));
411 if (iseek == NOFSIZE) xerror(NO, YES, YES,
412 "%s: invalid ixseek value", s);
414 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
416 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
418 iseek_blocks = tfc_humanfsize(s, &stoi);
419 if (!str_empty(stoi)) {
420 iseek_blocks = tfc_fnamesize(s, YES);
421 if (iseek_blocks == NOFSIZE)
423 "%s: invalid ictr value", s);
424 iseek_blocks /= TF_BLOCK_SIZE;
425 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
427 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
429 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
431 iseek_blocks = tfc_humanfsize(s, &stoi);
432 if (!str_empty(stoi)) {
433 iseek_blocks = tfc_fnamesize(s, YES);
434 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
435 if (iseek_blocks == NOFSIZE)
437 "%s: invalid ixctr value", s);
439 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
440 if (iseek_blocks % TF_BLOCK_SIZE)
442 "%s: not round to TF block size "
443 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
444 iseek_blocks /= TF_BLOCK_SIZE;
446 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
448 oseek = tfc_humanfsize(s, &stoi);
449 if (!str_empty(stoi)) {
450 oseek = tfc_fnamesize(s, YES);
451 oseek = tfc_modifysize(oseek, strchr(s, ':'));
452 if (oseek == NOFSIZE) xerror(NO, YES, YES,
453 "%s: invalid oseek value", s);
455 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
457 else if (!strncmp(s, "ioseek", 6) && *(s+6) == '=') {
460 iseek = tfc_humanfsize(s, &stoi);
461 if (!str_empty(stoi)) {
462 iseek = tfc_fnamesize(s, YES);
463 iseek = tfc_modifysize(iseek, strchr(s, ':'));
464 if (iseek == NOFSIZE) xerror(NO, YES, YES,
465 "%s: invalid iseek value", s);
467 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
468 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
470 "%s: not round to TF block size "
472 s, TFC_U(TF_BLOCK_SIZE));
473 iseek_blocks = iseek / TF_BLOCK_SIZE;
475 oseek = tfc_humanfsize(s, &stoi);
476 if (!str_empty(stoi)) {
477 oseek = tfc_fnamesize(s, YES);
478 oseek = tfc_modifysize(oseek, strchr(s, ':'));
479 if (oseek == NOFSIZE) xerror(NO, YES, YES,
480 "%s: invalid oseek value", s);
482 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
484 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
486 maxlen = tfc_humanfsize(s, &stoi);
487 if (!str_empty(stoi)) {
488 maxlen = tfc_fnamesize(s, YES);
489 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
490 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
491 "%s: invalid count value", s);
493 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
494 if (counter_opt == TFC_CTR_HEAD)
495 maxlen += TF_BLOCK_SIZE;
497 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
499 if (!strcmp(s, "tail")) {
500 do_ftrunc = TFC_FTRUNC_TAIL;
501 ftrunc_dfd = NOFSIZE;
504 do_ftrunc = TFC_DO_FTRUNC;
505 ftrunc_dfd = tfc_humanfsize(s, &stoi);
506 if (!str_empty(stoi)) {
507 ftrunc_dfd = tfc_fnamesize(s, YES);
508 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
509 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
510 "%s: invalid ftrunc value", s);
512 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
515 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
517 maxkeylen = tfc_humanfsize(s, &stoi);
518 if (!str_empty(stoi)) {
519 maxkeylen = tfc_fnamesize(s, YES);
520 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
521 if (maxkeylen == NOSIZE)
523 "%s: invalid key length value", s);
525 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
527 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
529 keyoffset = tfc_humanfsize(s, &stoi);
530 if (!str_empty(stoi)) {
531 keyoffset = tfc_fnamesize(s, YES);
532 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
533 if (keyoffset == NOFSIZE)
535 "%s: invalid key offset value", s);
537 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
539 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
541 ctrsz = tfc_humanfsize(s, &stoi);
542 if (!str_empty(stoi)) {
543 ctrsz = (size_t)tfc_fnamesize(s, YES);
544 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
547 "%s: invalid counter length value", s);
549 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
550 if (ctrsz > TF_BLOCK_SIZE)
551 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
553 else xerror(NO, YES, YES, "invalid option %s", s);
557 do_mac = TFC_MAC_SIGN;
558 if (strcasecmp(optarg, "mac") != 0)
559 do_mac_file = optarg;
562 do_mac = TFC_MAC_VRFY;
563 if (!strcasecmp(optarg, "drop"))
564 do_mac = TFC_MAC_DROP;
565 else if (strcasecmp(optarg, "mac") != 0)
566 do_mac_file = optarg;
570 if (do_mac != TFC_MAC_VRFY)
571 xerror(NO, YES, YES, "signature source was not specified");
572 do_mac = TFC_MAC_JUST_VRFY;
573 if (c == 'u') do_mac = TFC_MAC_JUST_VRFY2;
577 if (maxlen != NOFSIZE) {
578 if (c == 'Z') genzero_nr_bytes = maxlen;
579 else genrandom_nr_bytes = maxlen;
583 if (!strcasecmp(optarg, "cbs"))
585 else if (!strcasecmp(optarg, "ks"))
586 t = TF_FROM_BITS(TFC_KEY_BITS);
587 else if (!strcasecmp(optarg, "xks"))
588 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
589 else if (!strcasecmp(optarg, "iobs"))
592 t = tfc_humanfsize(optarg, &stoi);
593 if (!str_empty(stoi)) {
594 t = tfc_fnamesize(optarg, NO);
595 t = tfc_modifysize(t, strchr(optarg, ':'));
597 else t = tfc_modifysize(t, strchr(optarg, ':'));
599 if (c == 'Z') genzero_nr_bytes = maxlen = t;
600 else genrandom_nr_bytes = maxlen = t;
604 do_preserve_time = YES;
607 do_outfmt = TFC_OUTFMT_B64;
610 do_outfmt = TFC_OUTFMT_RAW;
613 do_outfmt = TFC_OUTFMT_HEX;
619 do_full_hexdump = NO;
626 td = strtod(optarg, &stoi);
627 status_timer = TFC_DTOUSECS(td);
628 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
636 if (!strcmp(progname, "tfbench")) {
637 if (!*(argv+optind)) usage();
639 td = strtod(*(argv+optind), &stoi);
640 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
642 "%s: invalid benchmark time in seconds", *(argv+optind));
643 bench_timer = TFC_DTOUSECS(td);
644 do_benchmark(bench_timer, td);
646 if (genrandom_nr_bytes) {
647 ctr_mode = TFC_MODE_STREAM;
648 do_edcrypt = TFC_DO_ENCRYPT;
649 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
651 if (genzero_nr_bytes) {
652 ctr_mode = TFC_MODE_PLAIN;
653 do_edcrypt = TFC_DO_PLAIN;
654 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
657 if (rawkey && password)
658 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
659 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
660 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
661 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
662 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
663 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
664 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
665 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
666 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
667 if (overwrite_source && counter_opt == TFC_CTR_RAND)
668 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
669 if (ctr_mode == TFC_MODE_PLAIN
670 && (do_edcrypt || do_mac || rawkey
671 || mackey_opt || counter_opt || counter_file))
672 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
680 memset(tfc_salt, 0, TFC_MAX_SALT);
682 if (!strcasecmp(saltf, "disable")) goto _nosalt;
684 if (!strcmp(saltf, "-")) saltfd = 0;
685 else saltfd = xopen(saltf, O_RDONLY | O_LARGEFILE);
686 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
687 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
693 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
697 if (!strcmp(mackeyf, "-")) mkfd = 0;
698 else mkfd = xopen(mackeyf, O_RDONLY | O_LARGEFILE);
700 skein_init(&sk, TFC_KEY_BITS);
707 lrem = lblock = sizeof(tmpdata);
708 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
709 _mkragain: lio = xread(mkfd, pblk, lrem);
710 if (lio == 0 && do_stop == NO) do_stop = YES;
711 if (lio != NOSIZE) ldone += lio;
713 if (errno != EIO && catch_all_errors != YES)
714 xerror(NO, NO, NO, "%s", mackeyf);
715 switch (error_action) {
716 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
717 case TFC_ERRACT_SYNC:
718 case TFC_ERRACT_LSYNC:
719 xerror(YES, NO, NO, "%s", mackeyf);
720 lio = ldone = lrem = lblock;
721 memset(tmpdata, 0, lio);
722 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
723 else lseek(mkfd, rdpos + lio, SEEK_SET);
725 default: xerror(NO, NO, NO, "%s", mackeyf); break;
728 if (lio && lio < lrem) {
734 skein_update(&sk, tmpdata, ldone);
737 skein_final(mackey, &sk);
741 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
742 memset(&getps, 0, sizeof(struct getpasswd_state));
743 getps.fd = getps.efd = -1;
744 getps.passwd = pwdask;
745 getps.pwlen = sizeof(pwdask)-1;
746 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
747 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
748 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
749 getps.flags = GETP_WAITFILL;
750 n = xgetpasswd(&getps);
751 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
752 if (n == ((size_t)-2)) xexit(1);
753 if (verbose) say_hint(pwdask, n, "MAC password hint");
754 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
758 if ((strlen(progname) <= 9)
759 && ((!strcmp(progname, "sksum"))
760 || ((!memcmp(progname, "sk", 2))
761 && (!memcmp(progname+3, "sum", 3)
762 || !memcmp(progname+4, "sum", 3)
763 || !memcmp(progname+5, "sum", 3)
764 || !memcmp(progname+6, "sum", 3)))))
765 do_sksum(progname, argv+optind);
766 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
771 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
772 if (!strcmp(argv[idx], "-")) kfd = 0;
773 else kfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
775 lio = strnlen(argv[idx], PATH_MAX);
776 memset(argv[idx], '*', lio);
783 if (do_full_key == NO && tweakf) {
786 if (!strcmp(tweakf, "-")) twfd = 0;
787 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
788 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
789 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
790 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
791 if (ldone < TF_TWEAK_SIZE)
792 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
799 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
801 sfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
802 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
803 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
806 if ((do_mac >= TFC_MAC_VRFY || do_mac <= TFC_MAC_DROP) && !do_mac_file) {
807 maxlen = tfc_fdsize(sfd);
808 if (maxlen == NOFSIZE)
810 "Cannot verify embedded MAC with non-seekable source!");
811 maxlen -= TF_FROM_BITS(macbits);
813 srcfname = argv[idx];
817 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
818 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
820 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
821 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
823 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
828 if (!strcmp(counter_file, "-")) ctrfd = 0;
829 else ctrfd = xopen(counter_file, O_RDONLY | O_LARGEFILE);
830 lio = xread(ctrfd, ctr, ctrsz);
831 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
832 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
835 else if (counter_opt == TFC_CTR_HEAD) {
838 lrem = lblock = ctrsz;
839 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
840 _ctrragain: lio = xread(sfd, pblk, lrem);
841 if (lio != NOSIZE) ldone += lio;
843 if (errno != EIO && catch_all_errors != YES)
844 xerror(NO, NO, NO, "%s", srcfname);
845 switch (error_action) {
846 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
847 case TFC_ERRACT_SYNC:
848 case TFC_ERRACT_LSYNC:
849 xerror(YES, NO, NO, "%s", srcfname);
850 lio = ldone = lrem = lblock;
852 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
853 else lseek(sfd, rdpos + lio, SEEK_SET);
855 default: xerror(NO, NO, NO, "%s", srcfname); break;
858 if (lio && lio < lrem) {
863 total_processed_src += ldone;
868 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
870 if (lseek(sfd, iseek, SEEK_SET) == -1)
871 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
874 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
876 if (verbose) tfc_esay("%s: hashing password", tfc_format_pid(progname));
878 if (rawkey == TFC_RAWKEY_KEYFILE) {
879 tfc_yesno xtskeyset = NO;
883 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
884 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
885 _keyragain: lio = xread(kfd, pblk, lrem);
886 if (lio != NOSIZE) ldone += lio;
888 if (errno != EIO && catch_all_errors != YES)
889 xerror(NO, NO, NO, "reading key");
890 switch (error_action) {
891 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
892 case TFC_ERRACT_SYNC:
893 case TFC_ERRACT_LSYNC:
894 xerror(YES, NO, NO, "reading key");
895 lio = ldone = lrem = lblock;
897 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
898 else lseek(kfd, rdpos + lio, SEEK_SET);
900 default: xerror(NO, NO, NO, "reading key"); break;
903 if (lio && lio < lrem) {
908 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
910 if (ctr_mode == TFC_MODE_XTS) {
911 if (xtskeyset == NO) {
918 else if (rawkey == TFC_RAWKEY_ASKSTR) {
919 tfc_yesno xtskeyset = NO;
921 pblk = key; n = sizeof(key);
922 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
923 getps.fd = getps.efd = -1;
924 getps.passwd = (char *)pblk;
926 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
927 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
928 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
929 getps.flags = GETP_WAITFILL;
930 n = xgetpasswd(&getps);
931 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
932 if (n == ((size_t)-2)) xexit(1);
933 if (verbose) say_hint(pblk, n, "Raw string key hint");
934 if (ctr_mode == TFC_MODE_XTS) {
935 if (xtskeyset == NO) {
936 pblk = xtskey; n = sizeof(xtskey);
942 else if (rawkey == TFC_RAWKEY_ASKHEX) {
943 tfc_yesno xtskeyset = NO;
947 memset(&getps, 0, sizeof(struct getpasswd_state));
948 getps.fd = getps.efd = -1;
949 getps.passwd = pwdask;
950 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
951 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
952 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
953 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
954 getps.flags = GETP_WAITFILL;
955 n = xgetpasswd(&getps);
956 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
957 if (n == ((size_t)-2)) xexit(1);
959 tfc_esay("Please input even number of hex digits!");
960 goto _rawkey_hex_again;
962 hex2bin(pblk, pwdask);
963 memset(pwdask, 0, sizeof(pwdask));
964 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
965 if (ctr_mode == TFC_MODE_XTS) {
966 if (xtskeyset == NO) {
969 goto _rawkey_hex_again;
974 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
975 getps.fd = getps.efd = -1;
976 getps.passwd = pwdask;
977 getps.pwlen = sizeof(pwdask)-1;
978 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
979 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
980 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
981 getps.flags = GETP_WAITFILL;
982 n = xgetpasswd(&getps);
983 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
984 if (n == ((size_t)-2)) xexit(1);
985 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
986 getps.fd = getps.efd = -1;
987 getps.passwd = pwdagain;
988 getps.pwlen = sizeof(pwdagain)-1;
989 getps.echo = "Enter it again: ";
990 getps.flags = GETP_WAITFILL;
991 n = xgetpasswd(&getps);
992 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
993 if (n == ((size_t)-2)) xexit(1);
994 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
995 tfc_esay("Passwords are different, try again");
999 if (verbose) say_hint(pwdask, n, "Password hint");
1000 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
1001 memset(pwdask, 0, sizeof(pwdask));
1002 memset(pwdagain, 0, sizeof(pwdagain));
1005 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
1006 xerror(NO, NO, YES, "hashing key");
1010 if (tfc_saltsz > 0) {
1011 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
1012 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
1014 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
1015 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1016 memset(tfc_salt, 0, TFC_MAX_SALT);
1019 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
1020 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1025 tfc_yesno xtskeyset = NO;
1028 if (!strcmp(genkeyf, "-")) krfd = 1;
1029 else krfd = xopen(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1030 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
1031 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
1032 if (verbose && xtskeyset == NO) {
1033 tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1034 tfc_esay("%s: rawkey written to %s.", tfc_format_pid(progname), genkeyf);
1035 tfc_esay("%s: Have a nice day!", tfc_format_pid(progname));
1038 if (ctr_mode == TFC_MODE_XTS) {
1039 if (xtskeyset == NO) {
1052 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1053 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1054 "will be slower.", tfc_format_pid(progname));
1055 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1056 else skein_init(&sk, macbits);
1059 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1060 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1064 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1065 if (do_full_key == NO) {
1066 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1067 tf_tweak_set(key, tweak);
1069 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1071 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1073 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1074 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1075 if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1077 if (counter_opt == TFC_CTR_SHOW) {
1078 switch (do_outfmt) {
1079 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1080 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1081 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1084 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1091 if (verbose) tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1093 if (overwrite_source && srcfname) argv[idx] = srcfname;
1097 if (!strcmp(argv[idx], "-")) dfd = 1;
1098 else dfd = xxopen(YES, argv[idx], O_RDWR | O_LARGEFILE | write_flags);
1099 if (dfd == -1) dfd = xopen(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1100 dstfname = argv[idx];
1105 if (lseek(dfd, oseek, SEEK_SET) == -1)
1106 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1109 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1110 memset(&sigact, 0, sizeof(sigact));
1111 sigact.sa_flags = SA_RESTART;
1112 sigact.sa_handler = print_crypt_status;
1113 sigaction(SIGUSR1, &sigact, NULL);
1114 sigaction(SIGALRM, &sigact, NULL);
1115 if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
1116 sigact.sa_handler = change_status_width;
1117 sigaction(SIGQUIT, &sigact, NULL);
1118 sigact.sa_handler = change_status_timer;
1119 sigaction(SIGUSR2, &sigact, NULL);
1121 sigact.sa_handler = print_crypt_status;
1122 sigaction(SIGINT, &sigact, NULL);
1123 sigaction(SIGTERM, &sigact, NULL);
1124 sigaction(SIGTSTP, &sigact, NULL);
1127 sigact.sa_handler = exit_sigterm;
1128 sigaction(SIGINT, &sigact, NULL);
1129 sigaction(SIGTERM, &sigact, NULL);
1130 sigact.sa_handler = handle_sigtstp;
1131 sigaction(SIGTSTP, &sigact, NULL);
1133 memset(&sigact, 0, sizeof(struct sigaction));
1135 tfc_getcurtime(&delta_time);
1138 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1142 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1143 if (lio != NOSIZE) ldone += lio;
1144 else xerror(NO, NO, NO, "%s", dstfname);
1145 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1151 total_written_dst += ldone;
1152 total_processed_dst += ldone;
1153 delta_processed += ldone;
1156 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1158 if (do_mac == TFC_MAC_JUST_VRFY2) {
1159 rwd = tfc_fdgetpos(sfd);
1160 if (rwd == NOFSIZE) {
1161 tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", tfc_format_pid(progname));
1162 do_mac = TFC_MAC_VRFY;
1164 goto _nodecrypt_again_vrfy2;
1166 _decrypt_again_vrfy2:
1167 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1168 xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1170 total_processed_src = rwd;
1171 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1172 memset(svctr, 0, TF_BLOCK_SIZE);
1175 _nodecrypt_again_vrfy2:
1183 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1184 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1185 _ragain: lio = xread(sfd, pblk, lrem);
1187 if ((do_read_loops != 0 && sfd != 0) && (loopcnt < do_read_loops)) {
1188 lseek(sfd, 0L, SEEK_SET);
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 _ragain; 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(srcblk, 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;
1219 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1220 size_t orig = ldone;
1221 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1222 if (ldone > blksize) ldone = blksize;
1223 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1226 if (do_mac == TFC_MAC_SIGN) skein_update(&sk, srcblk, ldone);
1228 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1229 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1230 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1231 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1232 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1233 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1234 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1235 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1236 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1237 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1238 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1239 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1240 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1241 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1243 else if (ctr_mode == TFC_MODE_PLAIN)
1244 memcpy(dstblk, srcblk, ldone);
1246 if (do_mac >= TFC_MAC_VRFY) skein_update(&sk, dstblk, ldone);
1247 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1252 _wagain: lio = xwrite(dfd, pblk, lrem);
1253 if (lio != NOSIZE) ldone += lio;
1254 else xerror(NO, NO, NO, "%s", dstfname);
1255 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1261 total_written_dst += ldone;
1262 _nowrite: total_processed_dst += ldone;
1263 delta_processed += ldone;
1265 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1271 if (verbose && status_timer && do_statline_dynamic == YES && statline_was_shown == YES) tfc_esay("\n");
1274 if (do_mac >= TFC_MAC_VRFY) {
1278 lrem = lblock = TF_FROM_BITS(macbits);
1279 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1280 _macragain: lio = xread(sfd, pblk, lrem);
1281 if (lio != NOSIZE) ldone += lio;
1283 if (errno != EIO && catch_all_errors != YES)
1284 xerror(NO, NO, NO, "%s", srcfname);
1285 switch (error_action) {
1286 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1287 case TFC_ERRACT_SYNC:
1288 case TFC_ERRACT_LSYNC:
1289 xerror(YES, NO, NO, "%s", srcfname);
1290 lio = ldone = lrem = lblock;
1291 memset(macvrfy, 0, lio);
1292 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1293 else lseek(sfd, rdpos + lio, SEEK_SET);
1295 default: xerror(NO, NO, NO, "%s", srcfname); break;
1298 if (lio && lio < lrem) {
1303 total_processed_src += ldone;
1308 if (!strcmp(do_mac_file, "-")) mfd = 0;
1309 else mfd = xopen(do_mac_file, O_RDONLY | O_LARGEFILE);
1310 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1311 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1312 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1313 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1314 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1315 lio = TF_FROM_BITS(macbits);
1316 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1318 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1322 if (ldone < TF_FROM_BITS(macbits)) {
1323 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1324 "not verifying", tfc_format_pid(progname), ldone);
1329 skein_final(macresult, &sk);
1331 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1332 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1333 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1334 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1335 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1337 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1339 tfc_esay("%s: signature is good", tfc_format_pid(progname));
1341 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1342 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1345 if (do_mac == TFC_MAC_JUST_VRFY2) {
1346 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", tfc_format_pid(progname));
1347 maxlen = total_processed_src - SKEIN_DIGEST_SIZE;
1348 do_mac = TFC_MAC_DROP2;
1349 goto _decrypt_again_vrfy2;
1354 tfc_esay("%s: signature is BAD: "
1355 "wrong password, key, mode, or file is not signed", tfc_format_pid(progname));
1356 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", tfc_format_pid(progname));
1361 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1362 memset(macresult, 0, sizeof(macresult));
1363 memset(tmpdata, 0, sizeof(tmpdata));
1365 else if (do_mac == TFC_MAC_SIGN) {
1366 skein_final(macresult, &sk);
1368 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1369 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1370 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1371 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1372 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1373 memset(macresult, 0, sizeof(macresult));
1377 lio = lrem = TF_FROM_BITS(macbits);
1379 _macwagain: lio = xwrite(dfd, pblk, lrem);
1380 if (lio != NOSIZE) ldone += lio;
1381 else xerror(NO, NO, NO, "%s", dstfname);
1382 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1388 total_written_dst += ldone;
1389 total_processed_dst += ldone;
1390 delta_processed += ldone;
1395 if (!strcmp(do_mac_file, "-")) mfd = 1;
1396 else mfd = xopen(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1397 if (do_outfmt == TFC_OUTFMT_B64) {
1398 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1399 memset(tmpdata, 0, TFC_TMPSIZE);
1400 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1401 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1402 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1404 tmpdata[lrem] = '\n';
1407 lio = xwrite(mfd, tmpdata, lrem);
1409 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1410 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1411 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1415 memset(macvrfy, 0, sizeof(macvrfy));
1416 memset(macresult, 0, sizeof(macresult));
1417 memset(tmpdata, 0, sizeof(tmpdata));
1419 else if (do_mac == TFC_MAC_DROP2) total_processed_src += SKEIN_DIGEST_SIZE;
1421 if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(0);