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];
33 static void open_log(const char *logfile)
37 fd = open(logfile, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC, 0666);
38 if (fd == -1) xerror(NO, NO, YES, "%s", logfile);
40 if (dup2(fd, 2) == -1) xexit(2);
42 do_statline_dynamic = NO;
45 static int getps_filter(struct getpasswd_state *getps, char chr, size_t pos)
48 getps->retn = ((size_t)-2);
54 static int getps_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
57 getps->retn = ((size_t)-2);
60 if (chr >= '0' && chr <= '9') return 1;
61 if (chr >= 'a' && chr <= 'f') return 1;
62 if (chr >= 'A' && chr <= 'F') return 1;
63 if (chr == '\x7f' || chr == '\x08'
64 || chr == '\x15' || chr == '\x17') return 1;
68 static inline int isctrlchr(int c)
71 if (c >= 0 && c <= 31) return 1;
72 if (c == 127) return 1;
76 static int getps_plain_filter(struct getpasswd_state *getps, char chr, size_t pos)
80 x = getps_filter(getps, chr, pos);
83 if (pos < getps->pwlen && !isctrlchr(chr))
84 write(getps->efd, &chr, sizeof(char));
88 static int getps_plain_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
92 x = getps_hex_filter(getps, chr, pos);
95 if (pos < getps->pwlen && !isctrlchr(chr))
96 write(getps->efd, &chr, sizeof(char));
100 static void make_hint(void *hint, size_t szhint, const void *data, size_t szdata)
102 char t[TF_FROM_BITS(TF_MAX_BITS)];
104 skein(t, TF_MAX_BITS, NULL, data, szdata);
105 xor_shrink(hint, szhint, t, sizeof(t));
106 memset(t, 0, sizeof(t));
109 static void raw_say_hint(void *hint, size_t szhint, const void *data, size_t szdata, const char *prompt)
111 make_hint(hint, szhint, data, szdata);
112 if (prompt) tfc_nfsay(stderr, "%s: ", prompt);
113 mehexdump(hint, szhint, szhint, 1);
114 memset(hint, 0, szhint);
117 static void say_hint(const void *data, size_t szdata, const char *prompt)
119 char t[TF_SIZE_UNIT];
120 raw_say_hint(t, TF_SIZE_UNIT, data, szdata, prompt);
121 /* t[] is erased (automatically) */
124 int main(int argc, char **argv)
128 char *s, *d, *t, *stoi;
132 progname = basename(argv[0]);
134 if (!isatty(2)) do_statline_dynamic = NO;
139 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
140 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
141 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
142 read_defaults(s, YES);
146 if (!strcmp(progname, "iotool")) {
147 do_edcrypt = TFC_DO_PLAIN;
149 ctr_mode = TFC_MODE_PLAIN;
153 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) {
156 read_defaults(optarg, NO);
165 if (!strcasecmp(optarg, "show"))
166 counter_opt = TFC_CTR_SHOW;
167 else if (!strcasecmp(optarg, "head"))
168 counter_opt = TFC_CTR_HEAD;
169 else if (!strcasecmp(optarg, "rand"))
170 counter_opt = TFC_CTR_RAND;
171 else if (!strcasecmp(optarg, "zero"))
172 counter_opt = TFC_CTR_ZERO;
173 else if (strchr(optarg, ':')) {
176 counter_opt = TFC_CTR_SSET;
179 s = d = optarg; t = NULL;
180 while ((s = strtok_r(d, ",", &t))) {
187 chr = (char)strtoul(s, &stoi, 16);
188 if (!str_empty(stoi)) continue;
189 x = (size_t)strtoul(ss, &stoi, 10);
190 if (!str_empty(stoi)) continue;
192 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
196 else counter_file = sksum_hashlist_file = optarg;
199 if (!strcasecmp(optarg, "ctr"))
200 ctr_mode = TFC_MODE_CTR;
201 else if (!strcasecmp(optarg, "stream"))
202 ctr_mode = TFC_MODE_STREAM;
203 else if (!strcasecmp(optarg, "cbc"))
204 ctr_mode = TFC_MODE_CBC;
205 else if (!strcasecmp(optarg, "ecb"))
206 ctr_mode = TFC_MODE_ECB;
207 else if (!strcasecmp(optarg, "xts"))
208 ctr_mode = TFC_MODE_XTS;
209 else if (!strcasecmp(optarg, "ocb"))
210 ctr_mode = TFC_MODE_OCB;
211 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
214 do_edcrypt = TFC_DO_PLAIN;
216 ctr_mode = TFC_MODE_PLAIN;
219 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
222 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
225 macbits = strtoul(optarg, &stoi, 10);
226 if (macbits == 0 || !str_empty(stoi) || macbits < 8
227 || macbits > TF_MAX_BITS || macbits % 8)
228 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
231 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
232 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
235 if (!strcasecmp(optarg, "key"))
236 mackey_opt = TFC_MACKEY_RAWKEY;
237 else if (!strcasecmp(optarg, "pwd"))
238 mackey_opt = TFC_MACKEY_PASSWORD;
240 mackey_opt = TFC_MACKEY_FILE;
248 rawkey = TFC_RAWKEY_KEYFILE;
251 rawkey = TFC_RAWKEY_ASKSTR;
254 rawkey = TFC_RAWKEY_ASKHEX;
265 if (maxlen != NOFSIZE) break;
267 maxlen = tfc_humanfsize(optarg, &stoi);
268 if (!str_empty(stoi)) {
269 maxlen = tfc_fnamesize(optarg, YES);
270 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
271 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
272 "%s: invalid count value", optarg);
274 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
275 if (counter_opt == TFC_CTR_HEAD)
276 maxlen += TF_BLOCK_SIZE;
279 overwrite_source = YES;
282 if (!strcmp(optarg, "xall")) {
283 catch_all_errors = YES;
286 if (!strcmp(optarg, "xseek")) {
287 ignore_seek_errors = YES;
290 if (!strcmp(optarg, "exit"))
291 error_action = TFC_ERRACT_EXIT;
292 else if (!strncmp(optarg, "cont", 4))
293 error_action = TFC_ERRACT_CONT;
294 else if (!strcmp(optarg, "sync"))
295 error_action = TFC_ERRACT_SYNC;
296 else if (!strcmp(optarg, "lsync"))
297 error_action = TFC_ERRACT_LSYNC;
298 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
304 s = d = optarg; t = NULL;
305 while ((s = strtok_r(d, ",", &t))) {
307 if (!strcmp(s, "sync"))
308 write_flags |= O_SYNC;
309 else if (!strcmp(s, "trunc"))
310 write_flags |= O_TRUNC;
311 else if (!strcmp(s, "fsync"))
313 else if (!strcmp(s, "pad"))
315 else if (!strcmp(s, "xtime"))
316 do_preserve_time = YES;
317 else if (!strcmp(s, "gibsize"))
318 do_stats_in_gibs = YES;
319 else if (!strcmp(s, "plainstats"))
320 do_statline_dynamic = NO;
321 else if (!strcmp(s, "statless"))
323 else if (!strcmp(s, "norepeat"))
325 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
327 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
328 mac_pw_prompt = s+10;
329 else if (!strcmp(s, "shorthex"))
330 do_full_hexdump = NO;
331 else if (!strcmp(s, "fullkey"))
333 else if (!strcmp(s, "showsecrets"))
335 else if (!strcmp(s, "finished"))
336 show_when_done = YES;
337 else if (!strncmp(s, "logfile", 7) && *(s+7) == '=')
339 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
341 blksize = (size_t)tfc_humanfsize(s, &stoi);
342 if (!str_empty(stoi)) {
343 blksize = (size_t)tfc_fnamesize(s, YES);
344 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
345 if (blksize == NOSIZE) xerror(NO, YES, YES,
346 "%s: invalid block size value", s);
348 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
349 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
350 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
351 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
352 "%s: block size exceeds %u bytes",
353 s, TFC_U(TFC_BLKSIZE));
355 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
357 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
358 if (!str_empty(stoi)) {
359 xtsblocks = (size_t)tfc_fnamesize(s, YES);
360 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
361 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
362 "%s: invalid blocks per xts block value", s);
364 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
365 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
366 "%s: nr of blocks per xts block is not round to %u bytes",
367 s, TFC_U(TFC_BLKSIZE));
368 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
369 "%s: nr of blocks per xts block exceeds %u bytes",
370 s, TFC_U(TFC_BLKSIZE));
372 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
374 iseek = tfc_humanfsize(s, &stoi);
375 if (!str_empty(stoi)) {
376 iseek = tfc_fnamesize(s, YES);
377 iseek = tfc_modifysize(iseek, strchr(s, ':'));
378 if (iseek == NOFSIZE) xerror(NO, YES, YES,
379 "%s: invalid iseek value", s);
381 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
382 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
384 "%s: not round to TF block size "
386 s, TFC_U(TF_BLOCK_SIZE));
387 iseek_blocks = iseek / TF_BLOCK_SIZE;
389 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
391 iseek = tfc_humanfsize(s, &stoi);
392 if (!str_empty(stoi)) {
393 iseek = tfc_fnamesize(s, YES);
394 iseek = tfc_modifysize(iseek, strchr(s, ':'));
395 if (iseek == NOFSIZE) xerror(NO, YES, YES,
396 "%s: invalid ixseek value", s);
398 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
400 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
402 iseek_blocks = tfc_humanfsize(s, &stoi);
403 if (!str_empty(stoi)) {
404 iseek_blocks = tfc_fnamesize(s, YES);
405 if (iseek_blocks == NOFSIZE)
407 "%s: invalid ictr value", s);
408 iseek_blocks /= TF_BLOCK_SIZE;
409 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
411 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
413 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
415 iseek_blocks = tfc_humanfsize(s, &stoi);
416 if (!str_empty(stoi)) {
417 iseek_blocks = tfc_fnamesize(s, YES);
418 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
419 if (iseek_blocks == NOFSIZE)
421 "%s: invalid ixctr value", s);
423 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
424 if (iseek_blocks % TF_BLOCK_SIZE)
426 "%s: not round to TF block size "
427 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
428 iseek_blocks /= TF_BLOCK_SIZE;
430 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
432 oseek = tfc_humanfsize(s, &stoi);
433 if (!str_empty(stoi)) {
434 oseek = tfc_fnamesize(s, YES);
435 oseek = tfc_modifysize(oseek, strchr(s, ':'));
436 if (oseek == NOFSIZE) xerror(NO, YES, YES,
437 "%s: invalid oseek value", s);
439 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
441 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
443 maxlen = tfc_humanfsize(s, &stoi);
444 if (!str_empty(stoi)) {
445 maxlen = tfc_fnamesize(s, YES);
446 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
447 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
448 "%s: invalid count value", s);
450 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
451 if (counter_opt == TFC_CTR_HEAD)
452 maxlen += TF_BLOCK_SIZE;
454 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
456 if (!strcmp(s, "tail")) {
457 do_ftrunc = TFC_FTRUNC_TAIL;
458 ftrunc_dfd = NOFSIZE;
461 do_ftrunc = TFC_DO_FTRUNC;
462 ftrunc_dfd = tfc_humanfsize(s, &stoi);
463 if (!str_empty(stoi)) {
464 ftrunc_dfd = tfc_fnamesize(s, YES);
465 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
466 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
467 "%s: invalid ftrunc value", s);
469 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
472 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
474 maxkeylen = tfc_humanfsize(s, &stoi);
475 if (!str_empty(stoi)) {
476 maxkeylen = tfc_fnamesize(s, YES);
477 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
478 if (maxkeylen == NOSIZE)
480 "%s: invalid key length value", s);
482 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
484 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
486 keyoffset = tfc_humanfsize(s, &stoi);
487 if (!str_empty(stoi)) {
488 keyoffset = tfc_fnamesize(s, YES);
489 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
490 if (keyoffset == NOFSIZE)
492 "%s: invalid key offset value", s);
494 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
496 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
498 ctrsz = tfc_humanfsize(s, &stoi);
499 if (!str_empty(stoi)) {
500 ctrsz = (size_t)tfc_fnamesize(s, YES);
501 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
504 "%s: invalid counter length value", s);
506 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
507 if (ctrsz > TF_BLOCK_SIZE)
508 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
510 else xerror(NO, YES, YES, "invalid option %s", s);
514 do_mac = TFC_MAC_SIGN;
515 if (strcasecmp(optarg, "mac") != 0)
516 do_mac_file = optarg;
519 do_mac = TFC_MAC_VRFY;
520 if (!strcasecmp(optarg, "drop"))
521 do_mac = TFC_MAC_DROP;
522 else if (strcasecmp(optarg, "mac") != 0)
523 do_mac_file = optarg;
527 if (do_mac != TFC_MAC_VRFY)
528 xerror(NO, YES, YES, "signature source was not specified");
529 do_mac = TFC_MAC_JUST_VRFY;
530 if (c == 'u') do_mac = TFC_MAC_JUST_VRFY2;
534 if (maxlen != NOFSIZE) {
535 if (c == 'Z') genzero_nr_bytes = maxlen;
536 else genrandom_nr_bytes = maxlen;
540 if (!strcasecmp(optarg, "cbs"))
542 else if (!strcasecmp(optarg, "ks"))
543 t = TF_FROM_BITS(TFC_KEY_BITS);
544 else if (!strcasecmp(optarg, "xks"))
545 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
546 else if (!strcasecmp(optarg, "iobs"))
549 t = tfc_humanfsize(optarg, &stoi);
550 if (!str_empty(stoi)) {
551 t = tfc_fnamesize(optarg, NO);
552 t = tfc_modifysize(t, strchr(optarg, ':'));
554 else t = tfc_modifysize(t, strchr(optarg, ':'));
556 if (c == 'Z') genzero_nr_bytes = maxlen = t;
557 else genrandom_nr_bytes = maxlen = t;
561 do_preserve_time = YES;
564 do_outfmt = TFC_OUTFMT_B64;
567 do_outfmt = TFC_OUTFMT_RAW;
570 do_outfmt = TFC_OUTFMT_HEX;
575 do_full_hexdump = NO;
582 td = strtod(optarg, &stoi);
583 status_timer = TFC_DTOUSECS(td);
584 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
592 if (!strcmp(progname, "tfbench")) {
593 if (!*(argv+optind)) usage();
595 td = strtod(*(argv+optind), &stoi);
596 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
598 "%s: invalid benchmark time in seconds", *(argv+optind));
599 bench_timer = TFC_DTOUSECS(td);
600 do_benchmark(bench_timer, td);
602 if (genrandom_nr_bytes) {
603 ctr_mode = TFC_MODE_STREAM;
604 do_edcrypt = TFC_DO_ENCRYPT;
605 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
607 if (genzero_nr_bytes) {
608 ctr_mode = TFC_MODE_PLAIN;
609 do_edcrypt = TFC_DO_PLAIN;
610 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
613 if (rawkey && password)
614 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
615 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
616 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
617 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
618 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
619 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
620 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
621 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
622 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
623 if (overwrite_source && counter_opt == TFC_CTR_RAND)
624 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
625 if (ctr_mode == TFC_MODE_PLAIN
626 && (do_edcrypt || do_mac || rawkey
627 || mackey_opt || counter_opt || counter_file))
628 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
636 memset(tfc_salt, 0, TFC_MAX_SALT);
638 if (!strcasecmp(saltf, "disable")) goto _nosalt;
640 if (!strcmp(saltf, "-")) saltfd = 0;
641 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
642 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
643 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
644 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
650 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
654 if (!strcmp(mackeyf, "-")) mkfd = 0;
655 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
656 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
658 skein_init(&sk, TFC_KEY_BITS);
665 lrem = lblock = sizeof(tmpdata);
666 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
667 _mkragain: lio = xread(mkfd, pblk, lrem);
668 if (lio == 0 && do_stop == NO) do_stop = YES;
669 if (lio != NOSIZE) ldone += lio;
671 if (errno != EIO && catch_all_errors != YES)
672 xerror(NO, NO, NO, "%s", mackeyf);
673 switch (error_action) {
674 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
675 case TFC_ERRACT_SYNC:
676 case TFC_ERRACT_LSYNC:
677 xerror(YES, NO, NO, "%s", mackeyf);
678 lio = ldone = lrem = lblock;
679 memset(tmpdata, 0, lio);
680 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
681 else lseek(mkfd, rdpos + lio, SEEK_SET);
683 default: xerror(NO, NO, NO, "%s", mackeyf); break;
686 if (lio && lio < lrem) {
692 skein_update(&sk, tmpdata, ldone);
695 skein_final(mackey, &sk);
699 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
700 memset(&getps, 0, sizeof(struct getpasswd_state));
701 getps.fd = getps.efd = -1;
702 getps.passwd = pwdask;
703 getps.pwlen = sizeof(pwdask)-1;
704 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
705 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
706 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
707 getps.flags = GETP_WAITFILL;
708 n = xgetpasswd(&getps);
709 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
710 if (n == ((size_t)-2)) xexit(1);
711 if (verbose) say_hint(pwdask, n, "MAC password hint");
712 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
716 if ((strlen(progname) <= 9)
717 && ((!strcmp(progname, "sksum"))
718 || ((!memcmp(progname, "sk", 2))
719 && (!memcmp(progname+3, "sum", 3)
720 || !memcmp(progname+4, "sum", 3)
721 || !memcmp(progname+5, "sum", 3)
722 || !memcmp(progname+6, "sum", 3)))))
723 do_sksum(progname, argv+optind);
724 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
729 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
730 if (!strcmp(argv[idx], "-")) kfd = 0;
731 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
732 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
734 lio = strnlen(argv[idx], PATH_MAX);
735 memset(argv[idx], '*', lio);
742 if (do_full_key == NO && tweakf) {
745 if (!strcmp(tweakf, "-")) twfd = 0;
746 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
747 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
748 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
749 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
750 if (ldone < TF_TWEAK_SIZE)
751 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
758 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
760 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
761 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
762 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
764 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
766 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
767 if (verbose) tfc_esay("%s: disabling signature verification on "
768 "requested partial decryption.", progname);
772 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
773 maxlen = tfc_fdsize(sfd);
774 if (maxlen == NOFSIZE)
776 "Cannot verify embedded MAC with non-seekable source!");
777 maxlen -= TF_FROM_BITS(macbits);
779 srcfname = argv[idx];
783 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
784 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
786 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
787 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
789 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
794 if (!strcmp(counter_file, "-")) ctrfd = 0;
795 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
796 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
797 lio = xread(ctrfd, ctr, ctrsz);
798 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
799 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
802 else if (counter_opt == TFC_CTR_HEAD) {
805 lrem = lblock = ctrsz;
806 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
807 _ctrragain: lio = xread(sfd, pblk, lrem);
808 if (lio != NOSIZE) ldone += lio;
810 if (errno != EIO && catch_all_errors != YES)
811 xerror(NO, NO, NO, "%s", srcfname);
812 switch (error_action) {
813 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
814 case TFC_ERRACT_SYNC:
815 case TFC_ERRACT_LSYNC:
816 xerror(YES, NO, NO, "%s", srcfname);
817 lio = ldone = lrem = lblock;
819 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
820 else lseek(sfd, rdpos + lio, SEEK_SET);
822 default: xerror(NO, NO, NO, "%s", srcfname); break;
825 if (lio && lio < lrem) {
830 total_processed_src += ldone;
835 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
837 if (lseek(sfd, iseek, SEEK_SET) == -1)
838 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
841 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
843 if (verbose) tfc_esay("%s: hashing password", progname);
845 if (rawkey == TFC_RAWKEY_KEYFILE) {
846 tfc_yesno xtskeyset = NO;
850 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
851 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
852 _keyragain: lio = xread(kfd, pblk, lrem);
853 if (lio != NOSIZE) ldone += lio;
855 if (errno != EIO && catch_all_errors != YES)
856 xerror(NO, NO, NO, "reading key");
857 switch (error_action) {
858 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
859 case TFC_ERRACT_SYNC:
860 case TFC_ERRACT_LSYNC:
861 xerror(YES, NO, NO, "reading key");
862 lio = ldone = lrem = lblock;
864 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
865 else lseek(kfd, rdpos + lio, SEEK_SET);
867 default: xerror(NO, NO, NO, "reading key"); break;
870 if (lio && lio < lrem) {
875 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
877 if (ctr_mode == TFC_MODE_XTS) {
878 if (xtskeyset == NO) {
885 else if (rawkey == TFC_RAWKEY_ASKSTR) {
886 tfc_yesno xtskeyset = NO;
888 pblk = key; n = sizeof(key);
889 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
890 getps.fd = getps.efd = -1;
891 getps.passwd = (char *)pblk;
893 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
894 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
895 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
896 getps.flags = GETP_WAITFILL;
897 n = xgetpasswd(&getps);
898 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
899 if (n == ((size_t)-2)) xexit(1);
900 if (verbose) say_hint(pblk, n, "Raw string key hint");
901 if (ctr_mode == TFC_MODE_XTS) {
902 if (xtskeyset == NO) {
903 pblk = xtskey; n = sizeof(xtskey);
909 else if (rawkey == TFC_RAWKEY_ASKHEX) {
910 tfc_yesno xtskeyset = NO;
914 memset(&getps, 0, sizeof(struct getpasswd_state));
915 getps.fd = getps.efd = -1;
916 getps.passwd = pwdask;
917 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
918 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
919 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
920 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
921 getps.flags = GETP_WAITFILL;
922 n = xgetpasswd(&getps);
923 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
924 if (n == ((size_t)-2)) xexit(1);
926 tfc_esay("Please input even number of hex digits!");
927 goto _rawkey_hex_again;
929 hex2bin(pblk, pwdask);
930 memset(pwdask, 0, sizeof(pwdask));
931 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
932 if (ctr_mode == TFC_MODE_XTS) {
933 if (xtskeyset == NO) {
936 goto _rawkey_hex_again;
941 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
942 getps.fd = getps.efd = -1;
943 getps.passwd = pwdask;
944 getps.pwlen = sizeof(pwdask)-1;
945 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
946 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
947 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
948 getps.flags = GETP_WAITFILL;
949 n = xgetpasswd(&getps);
950 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
951 if (n == ((size_t)-2)) xexit(1);
952 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
953 getps.fd = getps.efd = -1;
954 getps.passwd = pwdagain;
955 getps.pwlen = sizeof(pwdagain)-1;
956 getps.echo = "Enter it again: ";
957 getps.flags = GETP_WAITFILL;
958 n = xgetpasswd(&getps);
959 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
960 if (n == ((size_t)-2)) xexit(1);
961 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
962 tfc_esay("Passwords are different, try again");
966 if (verbose) say_hint(pwdask, n, "Password hint");
967 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
968 memset(pwdask, 0, sizeof(pwdask));
969 memset(pwdagain, 0, sizeof(pwdagain));
972 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
973 xerror(NO, NO, YES, "hashing key");
977 if (tfc_saltsz > 0) {
978 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
979 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
981 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
982 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
983 memset(tfc_salt, 0, TFC_MAX_SALT);
986 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
987 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
992 tfc_yesno xtskeyset = NO;
995 if (!strcmp(genkeyf, "-")) krfd = 1;
996 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
997 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
998 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
999 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
1000 if (verbose && xtskeyset == NO) {
1001 tfc_esay("%s: password hashing done", progname);
1002 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
1003 tfc_esay("%s: Have a nice day!", progname);
1006 if (ctr_mode == TFC_MODE_XTS) {
1007 if (xtskeyset == NO) {
1019 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
1020 if (verbose) tfc_esay("%s: disabling signature verification on "
1021 "requested partial decryption.", progname);
1026 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1027 if (ctr_mode < TFC_MODE_OCB) {
1028 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1029 "will be slower.", progname);
1030 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1031 else skein_init(&sk, macbits);
1035 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1036 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1040 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1041 if (do_full_key == NO) {
1042 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1043 tf_tweak_set(key, tweak);
1045 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1047 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1049 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1050 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1051 if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1053 if (counter_opt == TFC_CTR_SHOW) {
1054 switch (do_outfmt) {
1055 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1056 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1057 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1060 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1067 if (verbose) tfc_esay("%s: password hashing done", progname);
1069 if (overwrite_source && srcfname) argv[idx] = srcfname;
1073 if (!strcmp(argv[idx], "-")) dfd = 1;
1074 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
1076 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1077 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
1079 dstfname = argv[idx];
1084 if (lseek(dfd, oseek, SEEK_SET) == -1)
1085 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1088 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1089 memset(&sigact, 0, sizeof(sigact));
1090 sigact.sa_flags = SA_RESTART;
1091 sigact.sa_handler = print_crypt_status;
1092 sigaction(SIGUSR1, &sigact, NULL);
1093 sigaction(SIGALRM, &sigact, NULL);
1094 if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
1095 sigact.sa_handler = change_status_width;
1096 sigaction(SIGQUIT, &sigact, NULL);
1097 sigact.sa_handler = change_status_timer;
1098 sigaction(SIGUSR2, &sigact, NULL);
1100 sigact.sa_handler = print_crypt_status;
1101 sigaction(SIGINT, &sigact, NULL);
1102 sigaction(SIGTERM, &sigact, NULL);
1103 sigaction(SIGTSTP, &sigact, NULL);
1106 sigact.sa_handler = exit_sigterm;
1107 sigaction(SIGINT, &sigact, NULL);
1108 sigaction(SIGTERM, &sigact, NULL);
1109 sigact.sa_handler = handle_sigtstp;
1110 sigaction(SIGTSTP, &sigact, NULL);
1112 memset(&sigact, 0, sizeof(struct sigaction));
1114 tfc_getcurtime(&delta_time);
1117 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1121 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1122 if (lio != NOSIZE) ldone += lio;
1123 else xerror(NO, NO, NO, "%s", dstfname);
1124 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1130 total_processed_dst += ldone;
1131 delta_processed += ldone;
1134 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1136 if (do_mac == TFC_MAC_JUST_VRFY2) {
1137 rwd = tfc_fdgetpos(sfd);
1138 if (rwd == NOFSIZE) {
1139 tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", progname);
1140 do_mac = TFC_MAC_VRFY;
1142 goto _nodecrypt_again_vrfy2;
1144 _decrypt_again_vrfy2:
1145 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1146 xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1148 total_processed_src = rwd;
1149 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1150 memset(svctr, 0, TF_BLOCK_SIZE);
1153 _nodecrypt_again_vrfy2:
1160 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1161 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1162 _ragain: lio = xread(sfd, pblk, lrem);
1163 if (lio == 0) do_stop = YES;
1164 if (lio != NOSIZE) ldone += lio;
1166 if (errno != EIO && catch_all_errors != YES)
1167 xerror(NO, NO, NO, "%s", srcfname);
1168 switch (error_action) {
1169 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1170 case TFC_ERRACT_SYNC:
1171 case TFC_ERRACT_LSYNC:
1172 xerror(YES, NO, NO, "%s", srcfname);
1173 lio = ldone = lrem = lblock;
1174 memset(srcblk, 0, lio);
1175 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1176 else lseek(sfd, rdpos + lio, SEEK_SET);
1178 default: xerror(NO, NO, NO, "%s", srcfname); break;
1181 if (lio && lio < lrem) {
1186 total_processed_src += ldone;
1188 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1189 size_t orig = ldone;
1190 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1191 if (ldone > blksize) ldone = blksize;
1192 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1195 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1196 skein_update(&sk, srcblk, ldone);
1198 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1199 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1200 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1201 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1202 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1203 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1204 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1205 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1206 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1207 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1208 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1209 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1210 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1211 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1213 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1214 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1215 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1216 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1218 else if (ctr_mode == TFC_MODE_PLAIN)
1219 memcpy(dstblk, srcblk, ldone);
1221 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1222 skein_update(&sk, dstblk, ldone);
1223 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1228 _wagain: lio = xwrite(dfd, pblk, lrem);
1229 if (lio != NOSIZE) ldone += lio;
1230 else xerror(NO, NO, NO, "%s", dstfname);
1231 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1237 total_written_dst += ldone;
1238 _nowrite: total_processed_dst += ldone;
1239 delta_processed += ldone;
1241 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1248 if (do_mac >= TFC_MAC_VRFY) {
1252 lrem = lblock = TF_FROM_BITS(macbits);
1253 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1254 _macragain: lio = xread(sfd, pblk, lrem);
1255 if (lio != NOSIZE) ldone += lio;
1257 if (errno != EIO && catch_all_errors != YES)
1258 xerror(NO, NO, NO, "%s", srcfname);
1259 switch (error_action) {
1260 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1261 case TFC_ERRACT_SYNC:
1262 case TFC_ERRACT_LSYNC:
1263 xerror(YES, NO, NO, "%s", srcfname);
1264 lio = ldone = lrem = lblock;
1265 memset(macvrfy, 0, lio);
1266 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1267 else lseek(sfd, rdpos + lio, SEEK_SET);
1269 default: xerror(NO, NO, NO, "%s", srcfname); break;
1272 if (lio && lio < lrem) {
1277 total_processed_src += ldone;
1282 if (!strcmp(do_mac_file, "-")) mfd = 0;
1283 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1284 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1285 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1286 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1287 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1288 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1289 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1290 lio = TF_FROM_BITS(macbits);
1291 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1293 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1297 if (ldone < TF_FROM_BITS(macbits)) {
1298 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1299 "not verifying", progname, ldone);
1304 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1305 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1307 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1308 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1309 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1310 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1311 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1312 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1314 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1316 tfc_esay("%s: signature is good", progname);
1318 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1319 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1322 if (do_mac == TFC_MAC_JUST_VRFY2) {
1323 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", progname);
1324 do_mac = TFC_MAC_DROP;
1325 goto _decrypt_again_vrfy2;
1330 tfc_esay("%s: signature is BAD: "
1331 "wrong password, key, mode, or file is not signed", progname);
1332 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", progname);
1337 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1338 memset(macresult, 0, sizeof(macresult));
1339 memset(tmpdata, 0, sizeof(tmpdata));
1342 else if (do_mac == TFC_MAC_SIGN) {
1343 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1344 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1346 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1347 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1348 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1349 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1350 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1351 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1352 memset(macresult, 0, sizeof(macresult));
1356 lio = lrem = TF_FROM_BITS(macbits);
1358 _macwagain: lio = xwrite(dfd, pblk, lrem);
1359 if (lio != NOSIZE) ldone += lio;
1360 else xerror(NO, NO, NO, "%s", dstfname);
1361 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1367 total_processed_dst += ldone;
1368 delta_processed += ldone;
1373 if (!strcmp(do_mac_file, "-")) mfd = 1;
1374 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1375 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1376 if (do_outfmt == TFC_OUTFMT_B64) {
1377 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1378 memset(tmpdata, 0, TFC_TMPSIZE);
1379 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1380 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1381 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1383 tmpdata[lrem] = '\n';
1386 lio = xwrite(mfd, tmpdata, lrem);
1388 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1389 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1390 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1394 memset(macvrfy, 0, sizeof(macvrfy));
1395 memset(macresult, 0, sizeof(macresult));
1396 memset(tmpdata, 0, sizeof(tmpdata));
1399 if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(0);