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) xerror(NO, NO, YES, "dup2(%d, 2) for %s", fd, logfile);
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 (!strncmp(s, "logfile", 7) && *(s+7) == '=')
337 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
339 blksize = (size_t)tfc_humanfsize(s, &stoi);
340 if (!str_empty(stoi)) {
341 blksize = (size_t)tfc_fnamesize(s, YES);
342 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
343 if (blksize == NOSIZE) xerror(NO, YES, YES,
344 "%s: invalid block size value", s);
346 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
347 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
348 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
349 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
350 "%s: block size exceeds %u bytes",
351 s, TFC_U(TFC_BLKSIZE));
353 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
355 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
356 if (!str_empty(stoi)) {
357 xtsblocks = (size_t)tfc_fnamesize(s, YES);
358 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
359 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
360 "%s: invalid blocks per xts block value", s);
362 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
363 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
364 "%s: nr of blocks per xts block is not round to %u bytes",
365 s, TFC_U(TFC_BLKSIZE));
366 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
367 "%s: nr of blocks per xts block exceeds %u bytes",
368 s, TFC_U(TFC_BLKSIZE));
370 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
372 iseek = tfc_humanfsize(s, &stoi);
373 if (!str_empty(stoi)) {
374 iseek = tfc_fnamesize(s, YES);
375 iseek = tfc_modifysize(iseek, strchr(s, ':'));
376 if (iseek == NOFSIZE) xerror(NO, YES, YES,
377 "%s: invalid iseek value", s);
379 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
380 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
382 "%s: not round to TF block size "
384 s, TFC_U(TF_BLOCK_SIZE));
385 iseek_blocks = iseek / TF_BLOCK_SIZE;
387 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
389 iseek = tfc_humanfsize(s, &stoi);
390 if (!str_empty(stoi)) {
391 iseek = tfc_fnamesize(s, YES);
392 iseek = tfc_modifysize(iseek, strchr(s, ':'));
393 if (iseek == NOFSIZE) xerror(NO, YES, YES,
394 "%s: invalid ixseek value", s);
396 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
398 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
400 iseek_blocks = tfc_humanfsize(s, &stoi);
401 if (!str_empty(stoi)) {
402 iseek_blocks = tfc_fnamesize(s, YES);
403 if (iseek_blocks == NOFSIZE)
405 "%s: invalid ictr value", s);
406 iseek_blocks /= TF_BLOCK_SIZE;
407 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
409 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
411 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
413 iseek_blocks = tfc_humanfsize(s, &stoi);
414 if (!str_empty(stoi)) {
415 iseek_blocks = tfc_fnamesize(s, YES);
416 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
417 if (iseek_blocks == NOFSIZE)
419 "%s: invalid ixctr value", s);
421 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
422 if (iseek_blocks % TF_BLOCK_SIZE)
424 "%s: not round to TF block size "
425 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
426 iseek_blocks /= TF_BLOCK_SIZE;
428 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
430 oseek = tfc_humanfsize(s, &stoi);
431 if (!str_empty(stoi)) {
432 oseek = tfc_fnamesize(s, YES);
433 oseek = tfc_modifysize(oseek, strchr(s, ':'));
434 if (oseek == NOFSIZE) xerror(NO, YES, YES,
435 "%s: invalid oseek value", s);
437 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
439 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
441 maxlen = tfc_humanfsize(s, &stoi);
442 if (!str_empty(stoi)) {
443 maxlen = tfc_fnamesize(s, YES);
444 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
445 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
446 "%s: invalid count value", s);
448 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
449 if (counter_opt == TFC_CTR_HEAD)
450 maxlen += TF_BLOCK_SIZE;
452 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
454 if (!strcmp(s, "tail")) {
455 do_ftrunc = TFC_FTRUNC_TAIL;
456 ftrunc_dfd = NOFSIZE;
459 do_ftrunc = TFC_DO_FTRUNC;
460 ftrunc_dfd = tfc_humanfsize(s, &stoi);
461 if (!str_empty(stoi)) {
462 ftrunc_dfd = tfc_fnamesize(s, YES);
463 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
464 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
465 "%s: invalid ftrunc value", s);
467 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
470 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
472 maxkeylen = tfc_humanfsize(s, &stoi);
473 if (!str_empty(stoi)) {
474 maxkeylen = tfc_fnamesize(s, YES);
475 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
476 if (maxkeylen == NOSIZE)
478 "%s: invalid key length value", s);
480 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
482 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
484 keyoffset = tfc_humanfsize(s, &stoi);
485 if (!str_empty(stoi)) {
486 keyoffset = tfc_fnamesize(s, YES);
487 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
488 if (keyoffset == NOFSIZE)
490 "%s: invalid key offset value", s);
492 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
494 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
496 ctrsz = tfc_humanfsize(s, &stoi);
497 if (!str_empty(stoi)) {
498 ctrsz = (size_t)tfc_fnamesize(s, YES);
499 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
502 "%s: invalid counter length value", s);
504 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
505 if (ctrsz > TF_BLOCK_SIZE)
506 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
508 else xerror(NO, YES, YES, "invalid option %s", s);
512 do_mac = TFC_MAC_SIGN;
513 if (strcasecmp(optarg, "mac") != 0)
514 do_mac_file = optarg;
517 do_mac = TFC_MAC_VRFY;
518 if (!strcasecmp(optarg, "drop"))
519 do_mac = TFC_MAC_DROP;
520 else if (strcasecmp(optarg, "mac") != 0)
521 do_mac_file = optarg;
525 if (do_mac != TFC_MAC_VRFY)
526 xerror(NO, YES, YES, "signature source was not specified");
527 do_mac = TFC_MAC_JUST_VRFY;
528 if (c == 'u') do_mac = TFC_MAC_JUST_VRFY2;
532 if (maxlen != NOFSIZE) {
533 if (c == 'Z') genzero_nr_bytes = maxlen;
534 else genrandom_nr_bytes = maxlen;
538 if (!strcasecmp(optarg, "cbs"))
540 else if (!strcasecmp(optarg, "ks"))
541 t = TF_FROM_BITS(TFC_KEY_BITS);
542 else if (!strcasecmp(optarg, "xks"))
543 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
544 else if (!strcasecmp(optarg, "iobs"))
547 t = tfc_humanfsize(optarg, &stoi);
548 if (!str_empty(stoi)) {
549 t = tfc_fnamesize(optarg, NO);
550 t = tfc_modifysize(t, strchr(optarg, ':'));
552 else t = tfc_modifysize(t, strchr(optarg, ':'));
554 if (c == 'Z') genzero_nr_bytes = maxlen = t;
555 else genrandom_nr_bytes = maxlen = t;
559 do_preserve_time = YES;
562 do_outfmt = TFC_OUTFMT_B64;
565 do_outfmt = TFC_OUTFMT_RAW;
568 do_outfmt = TFC_OUTFMT_HEX;
573 do_full_hexdump = NO;
580 td = strtod(optarg, &stoi);
581 status_timer = TFC_DTOUSECS(td);
582 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
590 if (!strcmp(progname, "tfbench")) {
591 if (!*(argv+optind)) usage();
593 td = strtod(*(argv+optind), &stoi);
594 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
596 "%s: invalid benchmark time in seconds", *(argv+optind));
597 bench_timer = TFC_DTOUSECS(td);
598 do_benchmark(bench_timer, td);
600 if (genrandom_nr_bytes) {
601 ctr_mode = TFC_MODE_STREAM;
602 do_edcrypt = TFC_DO_ENCRYPT;
603 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
605 if (genzero_nr_bytes) {
606 ctr_mode = TFC_MODE_PLAIN;
607 do_edcrypt = TFC_DO_PLAIN;
608 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
611 if (rawkey && password)
612 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
613 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
614 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
615 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
616 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
617 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
618 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
619 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
620 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
621 if (overwrite_source && counter_opt == TFC_CTR_RAND)
622 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
623 if (ctr_mode == TFC_MODE_PLAIN
624 && (do_edcrypt || do_mac || rawkey
625 || mackey_opt || counter_opt || counter_file))
626 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
634 memset(tfc_salt, 0, TFC_MAX_SALT);
636 if (!strcasecmp(saltf, "disable")) goto _nosalt;
638 if (!strcmp(saltf, "-")) saltfd = 0;
639 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
640 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
641 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
642 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
648 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
652 if (!strcmp(mackeyf, "-")) mkfd = 0;
653 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
654 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
656 skein_init(&sk, TFC_KEY_BITS);
663 lrem = lblock = sizeof(tmpdata);
664 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
665 _mkragain: lio = xread(mkfd, pblk, lrem);
666 if (lio == 0 && do_stop == NO) do_stop = YES;
667 if (lio != NOSIZE) ldone += lio;
669 if (errno != EIO && catch_all_errors != YES)
670 xerror(NO, NO, NO, "%s", mackeyf);
671 switch (error_action) {
672 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
673 case TFC_ERRACT_SYNC:
674 case TFC_ERRACT_LSYNC:
675 xerror(YES, NO, NO, "%s", mackeyf);
676 lio = ldone = lrem = lblock;
677 memset(tmpdata, 0, lio);
678 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
679 else lseek(mkfd, rdpos + lio, SEEK_SET);
681 default: xerror(NO, NO, NO, "%s", mackeyf); break;
684 if (lio && lio < lrem) {
690 skein_update(&sk, tmpdata, ldone);
693 skein_final(mackey, &sk);
697 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
698 memset(&getps, 0, sizeof(struct getpasswd_state));
699 getps.fd = getps.efd = -1;
700 getps.passwd = pwdask;
701 getps.pwlen = sizeof(pwdask)-1;
702 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
703 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
704 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
705 getps.flags = GETP_WAITFILL;
706 n = xgetpasswd(&getps);
707 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
708 if (n == ((size_t)-2)) xexit(1);
709 if (verbose) say_hint(pwdask, n, "MAC password hint");
710 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
714 if ((strlen(progname) <= 9)
715 && ((!strcmp(progname, "sksum"))
716 || ((!memcmp(progname, "sk", 2))
717 && (!memcmp(progname+3, "sum", 3)
718 || !memcmp(progname+4, "sum", 3)
719 || !memcmp(progname+5, "sum", 3)
720 || !memcmp(progname+6, "sum", 3)))))
721 do_sksum(progname, argv+optind);
722 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
727 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
728 if (!strcmp(argv[idx], "-")) kfd = 0;
729 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
730 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
732 lio = strnlen(argv[idx], PATH_MAX);
733 memset(argv[idx], '*', lio);
740 if (do_full_key == NO && tweakf) {
743 if (!strcmp(tweakf, "-")) twfd = 0;
744 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
745 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
746 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
747 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
748 if (ldone < TF_TWEAK_SIZE)
749 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
756 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
758 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
759 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
760 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
762 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
764 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
765 if (verbose) tfc_esay("%s: disabling signature verification on "
766 "requested partial decryption.", progname);
770 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
771 maxlen = tfc_fdsize(sfd);
772 if (maxlen == NOFSIZE)
774 "Cannot verify embedded MAC with non-seekable source!");
775 maxlen -= TF_FROM_BITS(macbits);
777 srcfname = argv[idx];
781 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
782 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
784 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
785 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
787 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
792 if (!strcmp(counter_file, "-")) ctrfd = 0;
793 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
794 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
795 lio = xread(ctrfd, ctr, ctrsz);
796 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
797 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
800 else if (counter_opt == TFC_CTR_HEAD) {
803 lrem = lblock = ctrsz;
804 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
805 _ctrragain: lio = xread(sfd, pblk, lrem);
806 if (lio != NOSIZE) ldone += lio;
808 if (errno != EIO && catch_all_errors != YES)
809 xerror(NO, NO, NO, "%s", srcfname);
810 switch (error_action) {
811 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
812 case TFC_ERRACT_SYNC:
813 case TFC_ERRACT_LSYNC:
814 xerror(YES, NO, NO, "%s", srcfname);
815 lio = ldone = lrem = lblock;
817 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
818 else lseek(sfd, rdpos + lio, SEEK_SET);
820 default: xerror(NO, NO, NO, "%s", srcfname); break;
823 if (lio && lio < lrem) {
828 total_processed_src += ldone;
833 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
835 if (lseek(sfd, iseek, SEEK_SET) == -1)
836 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
839 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
841 if (verbose) tfc_esay("%s: hashing password", progname);
843 if (rawkey == TFC_RAWKEY_KEYFILE) {
844 tfc_yesno xtskeyset = NO;
848 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
849 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
850 _keyragain: lio = xread(kfd, pblk, lrem);
851 if (lio != NOSIZE) ldone += lio;
853 if (errno != EIO && catch_all_errors != YES)
854 xerror(NO, NO, NO, "reading key");
855 switch (error_action) {
856 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
857 case TFC_ERRACT_SYNC:
858 case TFC_ERRACT_LSYNC:
859 xerror(YES, NO, NO, "reading key");
860 lio = ldone = lrem = lblock;
862 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
863 else lseek(kfd, rdpos + lio, SEEK_SET);
865 default: xerror(NO, NO, NO, "reading key"); break;
868 if (lio && lio < lrem) {
873 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
875 if (ctr_mode == TFC_MODE_XTS) {
876 if (xtskeyset == NO) {
883 else if (rawkey == TFC_RAWKEY_ASKSTR) {
884 tfc_yesno xtskeyset = NO;
886 pblk = key; n = sizeof(key);
887 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
888 getps.fd = getps.efd = -1;
889 getps.passwd = (char *)pblk;
891 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
892 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
893 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
894 getps.flags = GETP_WAITFILL;
895 n = xgetpasswd(&getps);
896 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
897 if (n == ((size_t)-2)) xexit(1);
898 if (verbose) say_hint(pblk, n, "Raw string key hint");
899 if (ctr_mode == TFC_MODE_XTS) {
900 if (xtskeyset == NO) {
901 pblk = xtskey; n = sizeof(xtskey);
907 else if (rawkey == TFC_RAWKEY_ASKHEX) {
908 tfc_yesno xtskeyset = NO;
912 memset(&getps, 0, sizeof(struct getpasswd_state));
913 getps.fd = getps.efd = -1;
914 getps.passwd = pwdask;
915 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
916 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
917 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
918 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
919 getps.flags = GETP_WAITFILL;
920 n = xgetpasswd(&getps);
921 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
922 if (n == ((size_t)-2)) xexit(1);
924 tfc_esay("Please input even number of hex digits!");
925 goto _rawkey_hex_again;
927 hex2bin(pblk, pwdask);
928 memset(pwdask, 0, sizeof(pwdask));
929 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
930 if (ctr_mode == TFC_MODE_XTS) {
931 if (xtskeyset == NO) {
934 goto _rawkey_hex_again;
939 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
940 getps.fd = getps.efd = -1;
941 getps.passwd = pwdask;
942 getps.pwlen = sizeof(pwdask)-1;
943 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
944 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
945 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
946 getps.flags = GETP_WAITFILL;
947 n = xgetpasswd(&getps);
948 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
949 if (n == ((size_t)-2)) xexit(1);
950 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
951 getps.fd = getps.efd = -1;
952 getps.passwd = pwdagain;
953 getps.pwlen = sizeof(pwdagain)-1;
954 getps.echo = "Enter it again: ";
955 getps.flags = GETP_WAITFILL;
956 n = xgetpasswd(&getps);
957 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
958 if (n == ((size_t)-2)) xexit(1);
959 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
960 tfc_esay("Passwords are different, try again");
964 if (verbose) say_hint(pwdask, n, "Password hint");
965 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
966 memset(pwdask, 0, sizeof(pwdask));
967 memset(pwdagain, 0, sizeof(pwdagain));
970 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
971 xerror(NO, NO, YES, "hashing key");
975 if (tfc_saltsz > 0) {
976 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
977 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
979 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
980 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
981 memset(tfc_salt, 0, TFC_MAX_SALT);
984 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
985 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
990 tfc_yesno xtskeyset = NO;
993 if (!strcmp(genkeyf, "-")) krfd = 1;
994 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
995 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
996 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
997 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
998 if (verbose && xtskeyset == NO) {
999 tfc_esay("%s: password hashing done", progname);
1000 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
1001 tfc_esay("%s: Have a nice day!", progname);
1004 if (ctr_mode == TFC_MODE_XTS) {
1005 if (xtskeyset == NO) {
1017 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
1018 if (verbose) tfc_esay("%s: disabling signature verification on "
1019 "requested partial decryption.", progname);
1024 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1025 if (ctr_mode < TFC_MODE_OCB) {
1026 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1027 "will be slower.", progname);
1028 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1029 else skein_init(&sk, macbits);
1033 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1034 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1038 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1039 if (do_full_key == NO) {
1040 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1041 tf_tweak_set(key, tweak);
1043 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1045 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1047 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1048 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1049 if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1051 if (counter_opt == TFC_CTR_SHOW) {
1052 switch (do_outfmt) {
1053 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1054 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1055 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1058 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1065 if (verbose) tfc_esay("%s: password hashing done", progname);
1067 if (overwrite_source && srcfname) argv[idx] = srcfname;
1071 if (!strcmp(argv[idx], "-")) dfd = 1;
1072 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
1074 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1075 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
1077 dstfname = argv[idx];
1082 if (lseek(dfd, oseek, SEEK_SET) == -1)
1083 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1086 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1087 memset(&sigact, 0, sizeof(sigact));
1088 sigact.sa_flags = SA_RESTART;
1089 sigact.sa_handler = print_crypt_status;
1090 sigaction(SIGUSR1, &sigact, NULL);
1091 sigaction(SIGALRM, &sigact, NULL);
1092 if (status_timer) setup_next_alarm(status_timer);
1093 sigact.sa_handler = change_status_width;
1094 sigaction(SIGQUIT, &sigact, NULL);
1095 sigact.sa_handler = change_status_timer;
1096 sigaction(SIGUSR2, &sigact, NULL);
1098 sigact.sa_handler = print_crypt_status;
1099 sigaction(SIGINT, &sigact, NULL);
1100 sigaction(SIGTERM, &sigact, NULL);
1101 sigaction(SIGTSTP, &sigact, NULL);
1104 sigact.sa_handler = exit_sigterm;
1105 sigaction(SIGINT, &sigact, NULL);
1106 sigaction(SIGTERM, &sigact, NULL);
1107 sigact.sa_handler = handle_sigtstp;
1108 sigaction(SIGTSTP, &sigact, NULL);
1110 memset(&sigact, 0, sizeof(struct sigaction));
1112 tfc_getcurtime(&delta_time);
1115 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1119 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1120 if (lio != NOSIZE) ldone += lio;
1121 else xerror(NO, NO, NO, "%s", dstfname);
1122 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1128 total_processed_dst += ldone;
1129 delta_processed += ldone;
1132 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1134 if (do_mac == TFC_MAC_JUST_VRFY2) {
1135 rwd = tfc_fdgetpos(sfd);
1136 if (rwd == NOFSIZE) {
1137 tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", progname);
1138 do_mac = TFC_MAC_VRFY;
1140 goto _nodecrypt_again_vrfy2;
1142 _decrypt_again_vrfy2:
1143 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1144 xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1146 total_processed_src = rwd;
1147 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1148 memset(svctr, 0, TF_BLOCK_SIZE);
1151 _nodecrypt_again_vrfy2:
1158 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1159 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1160 _ragain: lio = xread(sfd, pblk, lrem);
1161 if (lio == 0) do_stop = YES;
1162 if (lio != NOSIZE) ldone += lio;
1164 if (errno != EIO && catch_all_errors != YES)
1165 xerror(NO, NO, NO, "%s", srcfname);
1166 switch (error_action) {
1167 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1168 case TFC_ERRACT_SYNC:
1169 case TFC_ERRACT_LSYNC:
1170 xerror(YES, NO, NO, "%s", srcfname);
1171 lio = ldone = lrem = lblock;
1172 memset(srcblk, 0, lio);
1173 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1174 else lseek(sfd, rdpos + lio, SEEK_SET);
1176 default: xerror(NO, NO, NO, "%s", srcfname); break;
1179 if (lio && lio < lrem) {
1184 total_processed_src += ldone;
1186 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1187 size_t orig = ldone;
1188 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1189 if (ldone > blksize) ldone = blksize;
1190 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1193 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1194 skein_update(&sk, srcblk, ldone);
1196 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1197 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1198 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1199 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1200 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1201 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1202 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1203 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1204 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1205 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1206 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1207 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1208 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1209 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1211 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1212 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1213 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1214 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1216 else if (ctr_mode == TFC_MODE_PLAIN)
1217 memcpy(dstblk, srcblk, ldone);
1219 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1220 skein_update(&sk, dstblk, ldone);
1221 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1226 _wagain: lio = xwrite(dfd, pblk, lrem);
1227 if (lio != NOSIZE) ldone += lio;
1228 else xerror(NO, NO, NO, "%s", dstfname);
1229 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1235 total_written_dst += ldone;
1236 _nowrite: total_processed_dst += ldone;
1237 delta_processed += ldone;
1239 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1246 if (do_mac >= TFC_MAC_VRFY) {
1250 lrem = lblock = TF_FROM_BITS(macbits);
1251 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1252 _macragain: lio = xread(sfd, pblk, lrem);
1253 if (lio != NOSIZE) ldone += lio;
1255 if (errno != EIO && catch_all_errors != YES)
1256 xerror(NO, NO, NO, "%s", srcfname);
1257 switch (error_action) {
1258 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1259 case TFC_ERRACT_SYNC:
1260 case TFC_ERRACT_LSYNC:
1261 xerror(YES, NO, NO, "%s", srcfname);
1262 lio = ldone = lrem = lblock;
1263 memset(macvrfy, 0, lio);
1264 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1265 else lseek(sfd, rdpos + lio, SEEK_SET);
1267 default: xerror(NO, NO, NO, "%s", srcfname); break;
1270 if (lio && lio < lrem) {
1275 total_processed_src += ldone;
1280 if (!strcmp(do_mac_file, "-")) mfd = 0;
1281 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1282 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1283 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1284 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1285 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1286 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1287 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1288 lio = TF_FROM_BITS(macbits);
1289 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1291 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1295 if (ldone < TF_FROM_BITS(macbits)) {
1296 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1297 "not verifying", progname, ldone);
1302 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1303 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1305 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1306 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1307 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1308 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1309 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1310 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1312 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1314 tfc_esay("%s: signature is good", progname);
1316 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1317 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1320 if (do_mac == TFC_MAC_JUST_VRFY2) {
1321 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", progname);
1322 do_mac = TFC_MAC_DROP;
1323 goto _decrypt_again_vrfy2;
1328 tfc_esay("%s: signature is BAD: "
1329 "wrong password, key, mode, or file is not signed", progname);
1330 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", progname);
1335 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1336 memset(macresult, 0, sizeof(macresult));
1337 memset(tmpdata, 0, sizeof(tmpdata));
1340 else if (do_mac == TFC_MAC_SIGN) {
1341 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1342 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1344 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1345 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1346 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1347 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1348 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1349 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1350 memset(macresult, 0, sizeof(macresult));
1354 lio = lrem = TF_FROM_BITS(macbits);
1356 _macwagain: lio = xwrite(dfd, pblk, lrem);
1357 if (lio != NOSIZE) ldone += lio;
1358 else xerror(NO, NO, NO, "%s", dstfname);
1359 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1365 total_processed_dst += ldone;
1366 delta_processed += ldone;
1371 if (!strcmp(do_mac_file, "-")) mfd = 1;
1372 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1373 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1374 if (do_outfmt == TFC_OUTFMT_B64) {
1375 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1376 memset(tmpdata, 0, TFC_TMPSIZE);
1377 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1378 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1379 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1381 tmpdata[lrem] = '\n';
1384 lio = xwrite(mfd, tmpdata, lrem);
1386 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1387 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1388 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1392 memset(macvrfy, 0, sizeof(macvrfy));
1393 memset(macresult, 0, sizeof(macresult));
1394 memset(tmpdata, 0, sizeof(tmpdata));
1397 if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(0);