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;
33 static tfc_yesno unbuffered;
35 static void open_log(const char *logfile)
40 if (!strcmp(logfile, "-")) return;
44 fd = xopen(logfile, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC);
47 if (dup2(fd, 2) == -1) xexit(2);
49 do_statline_dynamic = NO;
52 static int getps_filter(struct getpasswd_state *getps, char chr, size_t pos)
55 getps->retn = ((size_t)-2);
61 static int getps_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
64 getps->retn = ((size_t)-2);
67 if (chr >= '0' && chr <= '9') return 1;
68 if (chr >= 'a' && chr <= 'f') return 1;
69 if (chr >= 'A' && chr <= 'F') return 1;
70 if (chr == '\x7f' || chr == '\x08'
71 || chr == '\x15' || chr == '\x17') return 1;
75 static inline int isctrlchr(int c)
78 if (c >= 0 && c <= 31) return 1;
79 if (c == 127) return 1;
83 static int getps_plain_filter(struct getpasswd_state *getps, char chr, size_t pos)
87 x = getps_filter(getps, chr, pos);
90 if (pos < getps->pwlen && !isctrlchr(chr))
91 write(getps->efd, &chr, sizeof(char));
95 static int getps_plain_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
99 x = getps_hex_filter(getps, chr, pos);
100 if (x != 1) return x;
102 if (pos < getps->pwlen && !isctrlchr(chr))
103 write(getps->efd, &chr, sizeof(char));
107 static void make_hint(void *hint, size_t szhint, const void *data, size_t szdata)
109 char t[TF_FROM_BITS(TF_MAX_BITS)];
111 skein(t, TF_MAX_BITS, NULL, data, szdata);
112 xor_shrink(hint, szhint, t, sizeof(t));
113 memset(t, 0, sizeof(t));
116 static void raw_say_hint(void *hint, size_t szhint, const void *data, size_t szdata, const char *prompt)
118 make_hint(hint, szhint, data, szdata);
119 if (prompt) tfc_nfsay(stderr, "%s: ", prompt);
120 mehexdump(hint, szhint, szhint, 1);
121 memset(hint, 0, szhint);
124 static void say_hint(const void *data, size_t szdata, const char *prompt)
126 char t[TF_SIZE_UNIT];
127 raw_say_hint(t, TF_SIZE_UNIT, data, szdata, prompt);
128 /* t[] is erased (automatically) */
131 int main(int argc, char **argv)
135 char *s, *d, *t, *stoi;
139 progname = basename(argv[0]);
141 if (!isatty(2)) do_statline_dynamic = NO;
146 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
147 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
148 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
149 read_defaults(s, YES);
153 if (!strcmp(progname, "iotool")) {
154 do_edcrypt = TFC_DO_PLAIN;
156 ctr_mode = TFC_MODE_PLAIN;
160 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) {
163 read_defaults(optarg, NO);
172 if (!strcasecmp(optarg, "show"))
173 counter_opt = TFC_CTR_SHOW;
174 else if (!strcasecmp(optarg, "head"))
175 counter_opt = TFC_CTR_HEAD;
176 else if (!strcasecmp(optarg, "rand"))
177 counter_opt = TFC_CTR_RAND;
178 else if (!strcasecmp(optarg, "zero"))
179 counter_opt = TFC_CTR_ZERO;
180 else if (strchr(optarg, ':')) {
183 counter_opt = TFC_CTR_SSET;
186 s = d = optarg; t = NULL;
187 while ((s = strtok_r(d, ",", &t))) {
194 chr = (char)strtoul(s, &stoi, 16);
195 if (!str_empty(stoi)) continue;
196 x = (size_t)strtoul(ss, &stoi, 10);
197 if (!str_empty(stoi)) continue;
199 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
203 else counter_file = sksum_hashlist_file = optarg;
206 if (!strcasecmp(optarg, "ctr"))
207 ctr_mode = TFC_MODE_CTR;
208 else if (!strcasecmp(optarg, "stream"))
209 ctr_mode = TFC_MODE_STREAM;
210 else if (!strcasecmp(optarg, "cbc"))
211 ctr_mode = TFC_MODE_CBC;
212 else if (!strcasecmp(optarg, "ecb"))
213 ctr_mode = TFC_MODE_ECB;
214 else if (!strcasecmp(optarg, "xts"))
215 ctr_mode = TFC_MODE_XTS;
216 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
219 do_edcrypt = TFC_DO_PLAIN;
221 ctr_mode = TFC_MODE_PLAIN;
224 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
227 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
230 macbits = strtoul(optarg, &stoi, 10);
231 if (macbits == 0 || !str_empty(stoi) || macbits < 8
232 || macbits > TF_MAX_BITS || macbits % 8)
233 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
236 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
237 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
240 if (!strcasecmp(optarg, "key"))
241 mackey_opt = TFC_MACKEY_RAWKEY;
242 else if (!strcasecmp(optarg, "pwd"))
243 mackey_opt = TFC_MACKEY_PASSWORD;
245 mackey_opt = TFC_MACKEY_FILE;
253 rawkey = TFC_RAWKEY_KEYFILE;
256 rawkey = TFC_RAWKEY_ASKSTR;
259 rawkey = TFC_RAWKEY_ASKHEX;
270 if (maxlen != NOFSIZE) break;
272 maxlen = tfc_humanfsize(optarg, &stoi);
273 if (!str_empty(stoi)) {
274 maxlen = tfc_fnamesize(optarg, YES);
275 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
276 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
277 "%s: invalid count value", optarg);
279 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
280 if (counter_opt == TFC_CTR_HEAD)
281 maxlen += TF_BLOCK_SIZE;
284 overwrite_source = YES;
287 if (!strcmp(optarg, "xall")) {
288 catch_all_errors = YES;
291 if (!strcmp(optarg, "xseek")) {
292 ignore_seek_errors = YES;
295 if (!strcmp(optarg, "exit"))
296 error_action = TFC_ERRACT_EXIT;
297 else if (!strncmp(optarg, "cont", 4))
298 error_action = TFC_ERRACT_CONT;
299 else if (!strcmp(optarg, "sync"))
300 error_action = TFC_ERRACT_SYNC;
301 else if (!strcmp(optarg, "lsync"))
302 error_action = TFC_ERRACT_LSYNC;
303 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
309 s = d = optarg; t = NULL;
310 while ((s = strtok_r(d, ",", &t))) {
312 if (!strcmp(s, "sync"))
313 write_flags |= O_SYNC;
314 else if (!strcmp(s, "trunc"))
315 write_flags |= O_TRUNC;
316 else if (!strcmp(s, "append"))
317 write_flags |= O_APPEND;
318 else if (!strcmp(s, "fsync"))
320 else if (!strcmp(s, "pad"))
322 else if (!strcmp(s, "ro"))
324 else if (!strcmp(s, "rw"))
326 else if (!strcmp(s, "xtime"))
327 do_preserve_time = YES;
328 else if (!strcmp(s, "gibsize"))
329 do_stats_in_gibs = YES;
330 else if (!strcmp(s, "plainstats"))
331 do_statline_dynamic = NO;
332 else if (!strcmp(s, "statless"))
334 else if (!strcmp(s, "norepeat"))
336 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
338 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
339 mac_pw_prompt = s+10;
340 else if (!strcmp(s, "shorthex"))
341 do_full_hexdump = NO;
342 else if (!strcmp(s, "fullkey"))
344 else if (!strcmp(s, "showsecrets"))
346 else if (!strcmp(s, "finished"))
347 show_when_done = YES;
348 else if (!strcmp(s, "pid"))
350 else if (!strcmp(s, "nobuf")) {
351 if (!tfc_is_freestream(ctr_mode)) xerror(NO, YES, YES,
352 "cannot activate unbuffered mode for non-stream cipher mode %s!",
353 tfc_modename(ctr_mode));
354 else unbuffered = YES;
356 else if (!strncmp(s, "readloops", 9) && *(s+9) == '=') {
357 do_read_loops = tfc_humanfsize(s+10, &stoi);
358 if (!str_empty(stoi)) do_read_loops = NOSIZE;
360 else if (!strncmp(s, "logfile", 7) && *(s+7) == '=')
362 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
364 blksize = (size_t)tfc_humanfsize(s, &stoi);
365 if (!str_empty(stoi)) {
366 blksize = (size_t)tfc_fnamesize(s, YES);
367 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
368 if (blksize == NOSIZE) xerror(NO, YES, YES,
369 "%s: invalid block size value", s);
371 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
372 if (!tfc_is_freestream(ctr_mode) && blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
373 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
374 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
375 "%s: block size exceeds %u bytes",
376 s, TFC_U(TFC_BLKSIZE));
378 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
380 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
381 if (!str_empty(stoi)) {
382 xtsblocks = (size_t)tfc_fnamesize(s, YES);
383 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
384 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
385 "%s: invalid blocks per xts block value", s);
387 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
388 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
389 "%s: nr of blocks per xts block is not round to %u bytes",
390 s, TFC_U(TFC_BLKSIZE));
391 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
392 "%s: nr of blocks per xts block exceeds %u bytes",
393 s, TFC_U(TFC_BLKSIZE));
395 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
397 iseek = tfc_humanfsize(s, &stoi);
398 if (!str_empty(stoi)) {
399 iseek = tfc_fnamesize(s, YES);
400 iseek = tfc_modifysize(iseek, strchr(s, ':'));
401 if (iseek == NOFSIZE) xerror(NO, YES, YES,
402 "%s: invalid iseek value", s);
404 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
405 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
407 "%s: not round to TF block size "
409 s, TFC_U(TF_BLOCK_SIZE));
410 iseek_blocks = iseek / TF_BLOCK_SIZE;
412 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
414 iseek = tfc_humanfsize(s, &stoi);
415 if (!str_empty(stoi)) {
416 iseek = tfc_fnamesize(s, YES);
417 iseek = tfc_modifysize(iseek, strchr(s, ':'));
418 if (iseek == NOFSIZE) xerror(NO, YES, YES,
419 "%s: invalid ixseek value", s);
421 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
423 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
425 iseek_blocks = tfc_humanfsize(s, &stoi);
426 if (!str_empty(stoi)) {
427 iseek_blocks = tfc_fnamesize(s, YES);
428 if (iseek_blocks == NOFSIZE)
430 "%s: invalid ictr value", s);
431 iseek_blocks /= TF_BLOCK_SIZE;
432 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
434 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
436 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
438 iseek_blocks = tfc_humanfsize(s, &stoi);
439 if (!str_empty(stoi)) {
440 iseek_blocks = tfc_fnamesize(s, YES);
441 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
442 if (iseek_blocks == NOFSIZE)
444 "%s: invalid ixctr value", s);
446 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
447 if (iseek_blocks % TF_BLOCK_SIZE)
449 "%s: not round to TF block size "
450 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
451 iseek_blocks /= TF_BLOCK_SIZE;
453 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
455 oseek = tfc_humanfsize(s, &stoi);
456 if (!str_empty(stoi)) {
457 oseek = tfc_fnamesize(s, YES);
458 oseek = tfc_modifysize(oseek, strchr(s, ':'));
459 if (oseek == NOFSIZE) xerror(NO, YES, YES,
460 "%s: invalid oseek value", s);
462 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
464 else if (!strncmp(s, "ioseek", 6) && *(s+6) == '=') {
467 iseek = tfc_humanfsize(s, &stoi);
468 if (!str_empty(stoi)) {
469 iseek = tfc_fnamesize(s, YES);
470 iseek = tfc_modifysize(iseek, strchr(s, ':'));
471 if (iseek == NOFSIZE) xerror(NO, YES, YES,
472 "%s: invalid iseek value", s);
474 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
475 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
477 "%s: not round to TF block size "
479 s, TFC_U(TF_BLOCK_SIZE));
480 iseek_blocks = iseek / TF_BLOCK_SIZE;
482 oseek = tfc_humanfsize(s, &stoi);
483 if (!str_empty(stoi)) {
484 oseek = tfc_fnamesize(s, YES);
485 oseek = tfc_modifysize(oseek, strchr(s, ':'));
486 if (oseek == NOFSIZE) xerror(NO, YES, YES,
487 "%s: invalid oseek value", s);
489 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
491 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
493 maxlen = tfc_humanfsize(s, &stoi);
494 if (!str_empty(stoi)) {
495 maxlen = tfc_fnamesize(s, YES);
496 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
497 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
498 "%s: invalid count value", s);
500 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
501 if (counter_opt == TFC_CTR_HEAD)
502 maxlen += TF_BLOCK_SIZE;
504 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
506 if (!strcmp(s, "tail")) {
507 do_ftrunc = TFC_FTRUNC_TAIL;
508 ftrunc_dfd = NOFSIZE;
511 do_ftrunc = TFC_DO_FTRUNC;
512 ftrunc_dfd = tfc_humanfsize(s, &stoi);
513 if (!str_empty(stoi)) {
514 ftrunc_dfd = tfc_fnamesize(s, YES);
515 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
516 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
517 "%s: invalid ftrunc value", s);
519 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
522 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
524 maxkeylen = tfc_humanfsize(s, &stoi);
525 if (!str_empty(stoi)) {
526 maxkeylen = tfc_fnamesize(s, YES);
527 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
528 if (maxkeylen == NOSIZE)
530 "%s: invalid key length value", s);
532 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
534 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
536 keyoffset = tfc_humanfsize(s, &stoi);
537 if (!str_empty(stoi)) {
538 keyoffset = tfc_fnamesize(s, YES);
539 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
540 if (keyoffset == NOFSIZE)
542 "%s: invalid key offset value", s);
544 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
546 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
548 ctrsz = tfc_humanfsize(s, &stoi);
549 if (!str_empty(stoi)) {
550 ctrsz = (size_t)tfc_fnamesize(s, YES);
551 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
554 "%s: invalid counter length value", s);
556 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
557 if (ctrsz > TF_BLOCK_SIZE)
558 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
560 else xerror(NO, YES, YES, "invalid option %s", s);
564 do_mac = TFC_MAC_SIGN;
565 if (strcasecmp(optarg, "mac") != 0)
566 do_mac_file = optarg;
569 do_mac = TFC_MAC_VRFY;
570 if (!strcasecmp(optarg, "drop"))
571 do_mac = TFC_MAC_DROP;
572 else if (strcasecmp(optarg, "mac") != 0)
573 do_mac_file = optarg;
577 if (do_mac != TFC_MAC_VRFY)
578 xerror(NO, YES, YES, "signature source was not specified");
579 do_mac = TFC_MAC_JUST_VRFY;
580 if (c == 'u') do_mac = TFC_MAC_JUST_VRFY2;
584 if (maxlen != NOFSIZE) {
585 if (c == 'Z') genzero_nr_bytes = maxlen;
586 else genrandom_nr_bytes = maxlen;
590 if (!strcasecmp(optarg, "cbs"))
592 else if (!strcasecmp(optarg, "ks"))
593 t = TF_FROM_BITS(TFC_KEY_BITS);
594 else if (!strcasecmp(optarg, "xks"))
595 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
596 else if (!strcasecmp(optarg, "iobs"))
599 t = tfc_humanfsize(optarg, &stoi);
600 if (!str_empty(stoi)) {
601 t = tfc_fnamesize(optarg, NO);
602 t = tfc_modifysize(t, strchr(optarg, ':'));
604 else t = tfc_modifysize(t, strchr(optarg, ':'));
606 if (c == 'Z') genzero_nr_bytes = maxlen = t;
607 else genrandom_nr_bytes = maxlen = t;
611 do_preserve_time = YES;
614 do_outfmt = TFC_OUTFMT_B64;
617 do_outfmt = TFC_OUTFMT_RAW;
620 do_outfmt = TFC_OUTFMT_HEX;
626 do_full_hexdump = NO;
633 td = strtod(optarg, &stoi);
634 status_timer = TFC_DTOUSECS(td);
635 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
643 if (!strcmp(progname, "tfbench")) {
644 if (!*(argv+optind)) usage();
646 td = strtod(*(argv+optind), &stoi);
647 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
649 "%s: invalid benchmark time in seconds", *(argv+optind));
650 bench_timer = TFC_DTOUSECS(td);
651 do_benchmark(bench_timer, td);
653 if (genrandom_nr_bytes) {
654 ctr_mode = TFC_MODE_STREAM;
655 do_edcrypt = TFC_DO_ENCRYPT;
656 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
658 if (genzero_nr_bytes) {
659 ctr_mode = TFC_MODE_PLAIN;
660 do_edcrypt = TFC_DO_PLAIN;
661 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
664 if (rawkey && password)
665 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
666 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
667 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
668 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
669 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
670 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
671 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
672 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
673 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
674 if (overwrite_source && counter_opt == TFC_CTR_RAND)
675 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
676 if (ctr_mode == TFC_MODE_PLAIN
677 && (do_edcrypt || do_mac || rawkey
678 || mackey_opt || counter_opt || counter_file))
679 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
687 memset(tfc_salt, 0, TFC_MAX_SALT);
689 if (!strcasecmp(saltf, "disable")) goto _nosalt;
691 if (!strcmp(saltf, "-")) saltfd = 0;
692 else saltfd = xopen(saltf, O_RDONLY | O_LARGEFILE);
693 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
694 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
700 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
704 if (!strcmp(mackeyf, "-")) mkfd = 0;
705 else mkfd = xopen(mackeyf, O_RDONLY | O_LARGEFILE);
707 skein_init(&sk, TFC_KEY_BITS);
714 lrem = lblock = sizeof(tmpdata);
715 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
716 _mkragain: lio = xread(mkfd, pblk, lrem);
717 if (lio == 0 && do_stop == NO) do_stop = YES;
718 if (lio != NOSIZE) ldone += lio;
720 if (errno != EIO && catch_all_errors != YES)
721 xerror(NO, NO, NO, "%s", mackeyf);
722 switch (error_action) {
723 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
724 case TFC_ERRACT_SYNC:
725 case TFC_ERRACT_LSYNC:
726 xerror(YES, NO, NO, "%s", mackeyf);
727 lio = ldone = lrem = lblock;
728 memset(tmpdata, 0, lio);
729 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
730 else lseek(mkfd, rdpos + lio, SEEK_SET);
732 default: xerror(NO, NO, NO, "%s", mackeyf); break;
735 if (lio && lio < lrem) {
741 skein_update(&sk, tmpdata, ldone);
744 skein_final(mackey, &sk);
748 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
749 memset(&getps, 0, sizeof(struct getpasswd_state));
750 getps.fd = getps.efd = -1;
751 getps.passwd = pwdask;
752 getps.pwlen = sizeof(pwdask)-1;
753 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
754 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
755 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
756 getps.flags = GETP_WAITFILL;
757 n = xgetpasswd(&getps);
758 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
759 if (n == ((size_t)-2)) xexit(1);
760 if (verbose) say_hint(pwdask, n, "MAC password hint");
761 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
765 if ((strlen(progname) <= 9)
766 && ((!strcmp(progname, "sksum"))
767 || ((!memcmp(progname, "sk", 2))
768 && (!memcmp(progname+3, "sum", 3)
769 || !memcmp(progname+4, "sum", 3)
770 || !memcmp(progname+5, "sum", 3)
771 || !memcmp(progname+6, "sum", 3)))))
772 do_sksum(progname, argv+optind);
773 if (!strcmp(progname, "base64")) do_edbase64(argv+optind);
778 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
779 if (!strcmp(argv[idx], "-")) kfd = 0;
780 else kfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
782 lio = strnlen(argv[idx], PATH_MAX);
783 memset(argv[idx], '*', lio);
790 if (do_full_key == NO && tweakf) {
793 if (!strcmp(tweakf, "-")) twfd = 0;
794 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
795 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
796 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
797 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
798 if (ldone < TF_TWEAK_SIZE)
799 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
806 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
808 sfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
809 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
810 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
813 if ((do_mac >= TFC_MAC_VRFY || do_mac <= TFC_MAC_DROP) && !do_mac_file) {
814 maxlen = tfc_fdsize(sfd);
815 if (maxlen == NOFSIZE)
817 "Cannot verify embedded MAC with non-seekable source!");
818 maxlen -= TF_FROM_BITS(macbits);
820 srcfname = argv[idx];
824 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
825 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
827 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
828 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
830 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
835 if (!strcmp(counter_file, "-")) ctrfd = 0;
836 else ctrfd = xopen(counter_file, O_RDONLY | O_LARGEFILE);
837 lio = xread(ctrfd, ctr, ctrsz);
838 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
839 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
842 else if (counter_opt == TFC_CTR_HEAD) {
845 lrem = lblock = ctrsz;
846 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
847 _ctrragain: lio = xread(sfd, pblk, lrem);
848 if (lio != NOSIZE) ldone += lio;
850 if (errno != EIO && catch_all_errors != YES)
851 xerror(NO, NO, NO, "%s", srcfname);
852 switch (error_action) {
853 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
854 case TFC_ERRACT_SYNC:
855 case TFC_ERRACT_LSYNC:
856 xerror(YES, NO, NO, "%s", srcfname);
857 lio = ldone = lrem = lblock;
859 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
860 else lseek(sfd, rdpos + lio, SEEK_SET);
862 default: xerror(NO, NO, NO, "%s", srcfname); break;
865 if (lio && lio < lrem) {
870 total_processed_src += ldone;
875 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
877 if (lseek(sfd, iseek, SEEK_SET) == -1)
878 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
881 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
883 if (verbose) tfc_esay("%s: hashing password", tfc_format_pid(progname));
885 if (rawkey == TFC_RAWKEY_KEYFILE) {
886 tfc_yesno xtskeyset = NO;
890 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
891 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
892 _keyragain: lio = xread(kfd, pblk, lrem);
893 if (lio != NOSIZE) ldone += lio;
895 if (errno != EIO && catch_all_errors != YES)
896 xerror(NO, NO, NO, "reading key");
897 switch (error_action) {
898 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
899 case TFC_ERRACT_SYNC:
900 case TFC_ERRACT_LSYNC:
901 xerror(YES, NO, NO, "reading key");
902 lio = ldone = lrem = lblock;
904 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
905 else lseek(kfd, rdpos + lio, SEEK_SET);
907 default: xerror(NO, NO, NO, "reading key"); break;
910 if (lio && lio < lrem) {
915 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
917 if (ctr_mode == TFC_MODE_XTS) {
918 if (xtskeyset == NO) {
925 else if (rawkey == TFC_RAWKEY_ASKSTR) {
926 tfc_yesno xtskeyset = NO;
928 pblk = key; n = sizeof(key);
929 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
930 getps.fd = getps.efd = -1;
931 getps.passwd = (char *)pblk;
933 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
934 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
935 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
936 getps.flags = GETP_WAITFILL;
937 n = xgetpasswd(&getps);
938 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
939 if (n == ((size_t)-2)) xexit(1);
940 if (verbose) say_hint(pblk, n, "Raw string key hint");
941 if (ctr_mode == TFC_MODE_XTS) {
942 if (xtskeyset == NO) {
943 pblk = xtskey; n = sizeof(xtskey);
949 else if (rawkey == TFC_RAWKEY_ASKHEX) {
950 tfc_yesno xtskeyset = NO;
954 memset(&getps, 0, sizeof(struct getpasswd_state));
955 getps.fd = getps.efd = -1;
956 getps.passwd = pwdask;
957 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
958 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
959 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
960 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
961 getps.flags = GETP_WAITFILL;
962 n = xgetpasswd(&getps);
963 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
964 if (n == ((size_t)-2)) xexit(1);
966 tfc_esay("Please input even number of hex digits!");
967 goto _rawkey_hex_again;
969 hex2bin(pblk, pwdask);
970 memset(pwdask, 0, sizeof(pwdask));
971 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
972 if (ctr_mode == TFC_MODE_XTS) {
973 if (xtskeyset == NO) {
976 goto _rawkey_hex_again;
981 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
982 getps.fd = getps.efd = -1;
983 getps.passwd = pwdask;
984 getps.pwlen = sizeof(pwdask)-1;
985 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
986 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
987 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
988 getps.flags = GETP_WAITFILL;
989 n = xgetpasswd(&getps);
990 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
991 if (n == ((size_t)-2)) xexit(1);
992 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
993 getps.fd = getps.efd = -1;
994 getps.passwd = pwdagain;
995 getps.pwlen = sizeof(pwdagain)-1;
996 getps.echo = "Enter it again: ";
997 getps.flags = GETP_WAITFILL;
998 n = xgetpasswd(&getps);
999 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
1000 if (n == ((size_t)-2)) xexit(1);
1001 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
1002 tfc_esay("Passwords are different, try again");
1006 if (verbose) say_hint(pwdask, n, "Password hint");
1007 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
1008 memset(pwdask, 0, sizeof(pwdask));
1009 memset(pwdagain, 0, sizeof(pwdagain));
1012 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
1013 xerror(NO, NO, YES, "hashing key");
1017 if (tfc_saltsz > 0) {
1018 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
1019 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
1021 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
1022 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1023 memset(tfc_salt, 0, TFC_MAX_SALT);
1026 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
1027 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1032 tfc_yesno xtskeyset = NO;
1035 if (!strcmp(genkeyf, "-")) krfd = 1;
1036 else krfd = xopen(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1037 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
1038 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
1039 if (verbose && xtskeyset == NO) {
1040 tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1041 tfc_esay("%s: rawkey written to %s.", tfc_format_pid(progname), genkeyf);
1042 tfc_esay("%s: Have a nice day!", tfc_format_pid(progname));
1045 if (ctr_mode == TFC_MODE_XTS) {
1046 if (xtskeyset == NO) {
1059 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1060 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1061 "will be slower.", tfc_format_pid(progname));
1062 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1063 else skein_init(&sk, macbits);
1066 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1067 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1071 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1072 if (do_full_key == NO) {
1073 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1074 tf_tweak_set(key, tweak);
1076 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1078 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1080 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1081 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1082 if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1084 if (counter_opt == TFC_CTR_SHOW) {
1085 switch (do_outfmt) {
1086 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1087 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1088 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1091 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1098 if (verbose) tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1100 if (overwrite_source && srcfname) argv[idx] = srcfname;
1104 if (!strcmp(argv[idx], "-")) dfd = 1;
1105 else dfd = xxopen(YES, argv[idx], O_RDWR | O_LARGEFILE | write_flags);
1106 if (dfd == -1) dfd = xopen(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1107 dstfname = argv[idx];
1112 if (lseek(dfd, oseek, SEEK_SET) == -1)
1113 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1116 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1117 memset(&sigact, 0, sizeof(sigact));
1118 sigact.sa_flags = SA_RESTART;
1119 sigact.sa_handler = print_crypt_status;
1120 sigaction(SIGUSR1, &sigact, NULL);
1121 sigaction(SIGALRM, &sigact, NULL);
1122 if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
1123 sigact.sa_handler = change_status_width;
1124 sigaction(SIGQUIT, &sigact, NULL);
1125 sigact.sa_handler = change_status_timer;
1126 sigaction(SIGUSR2, &sigact, NULL);
1128 sigact.sa_handler = print_crypt_status;
1129 sigaction(SIGINT, &sigact, NULL);
1130 sigaction(SIGTERM, &sigact, NULL);
1131 sigaction(SIGTSTP, &sigact, NULL);
1134 sigact.sa_handler = exit_sigterm;
1135 sigaction(SIGINT, &sigact, NULL);
1136 sigaction(SIGTERM, &sigact, NULL);
1137 sigact.sa_handler = handle_sigtstp;
1138 sigaction(SIGTSTP, &sigact, NULL);
1140 memset(&sigact, 0, sizeof(struct sigaction));
1142 tfc_getcurtime(&delta_time);
1145 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1149 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1150 if (lio != NOSIZE) ldone += lio;
1151 else xerror(NO, NO, NO, "%s", dstfname);
1152 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1158 total_written_dst += ldone;
1159 total_processed_dst += ldone;
1160 delta_processed += ldone;
1163 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1165 if (do_mac == TFC_MAC_JUST_VRFY2) {
1166 rwd = tfc_fdgetpos(sfd);
1167 if (rwd == NOFSIZE) {
1168 tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", tfc_format_pid(progname));
1169 do_mac = TFC_MAC_VRFY;
1171 goto _nodecrypt_again_vrfy2;
1173 _decrypt_again_vrfy2:
1174 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1175 xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1177 total_processed_src = rwd;
1178 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1179 memset(svctr, 0, TF_BLOCK_SIZE);
1182 _nodecrypt_again_vrfy2:
1190 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1191 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1192 _ragain: lio = xread(sfd, pblk, lrem);
1194 if ((do_read_loops != 0 && sfd != 0) && (loopcnt < do_read_loops)) {
1195 lseek(sfd, 0L, SEEK_SET);
1202 if (lio != NOSIZE) ldone += lio;
1204 if (errno != EIO && catch_all_errors != YES)
1205 xerror(NO, NO, NO, "%s", srcfname);
1206 switch (error_action) {
1207 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1208 case TFC_ERRACT_SYNC:
1209 case TFC_ERRACT_LSYNC:
1210 xerror(YES, NO, NO, "%s", srcfname);
1211 lio = ldone = lrem = lblock;
1212 memset(srcblk, 0, lio);
1213 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1214 else lseek(sfd, rdpos + lio, SEEK_SET);
1216 default: xerror(NO, NO, NO, "%s", srcfname); break;
1219 if (unbuffered == NO && lio && lio < lrem) {
1224 total_processed_src += ldone;
1226 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1227 size_t orig = ldone;
1228 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1229 if (ldone > blksize) ldone = blksize;
1230 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1233 if (do_mac == TFC_MAC_SIGN) skein_update(&sk, srcblk, ldone);
1235 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1236 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1237 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1238 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1239 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1240 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1241 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1242 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1243 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1244 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1245 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1246 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1247 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1248 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1250 else if (ctr_mode == TFC_MODE_PLAIN)
1251 memcpy(dstblk, srcblk, ldone);
1253 if (do_mac >= TFC_MAC_VRFY) skein_update(&sk, dstblk, ldone);
1254 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1259 _wagain: lio = xwrite(dfd, pblk, lrem);
1260 if (lio != NOSIZE) ldone += lio;
1261 else xerror(NO, NO, NO, "%s", dstfname);
1262 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1268 total_written_dst += ldone;
1269 _nowrite: total_processed_dst += ldone;
1270 delta_processed += ldone;
1272 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1278 if (verbose && status_timer && do_statline_dynamic == YES && statline_was_shown == YES) tfc_esay("\n");
1281 if (do_mac >= TFC_MAC_VRFY) {
1285 lrem = lblock = TF_FROM_BITS(macbits);
1286 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1287 _macragain: lio = xread(sfd, pblk, lrem);
1288 if (lio != NOSIZE) ldone += lio;
1290 if (errno != EIO && catch_all_errors != YES)
1291 xerror(NO, NO, NO, "%s", srcfname);
1292 switch (error_action) {
1293 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1294 case TFC_ERRACT_SYNC:
1295 case TFC_ERRACT_LSYNC:
1296 xerror(YES, NO, NO, "%s", srcfname);
1297 lio = ldone = lrem = lblock;
1298 memset(macvrfy, 0, lio);
1299 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1300 else lseek(sfd, rdpos + lio, SEEK_SET);
1302 default: xerror(NO, NO, NO, "%s", srcfname); break;
1305 if (lio && lio < lrem) {
1310 total_processed_src += ldone;
1315 if (!strcmp(do_mac_file, "-")) mfd = 0;
1316 else mfd = xopen(do_mac_file, O_RDONLY | O_LARGEFILE);
1317 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1318 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1319 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1320 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1321 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1322 lio = TF_FROM_BITS(macbits);
1323 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1325 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1329 if (ldone < TF_FROM_BITS(macbits)) {
1330 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1331 "not verifying", tfc_format_pid(progname), ldone);
1336 skein_final(macresult, &sk);
1338 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1339 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1340 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1341 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1342 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1344 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1346 tfc_esay("%s: signature is good", tfc_format_pid(progname));
1348 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1349 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1352 if (do_mac == TFC_MAC_JUST_VRFY2) {
1353 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", tfc_format_pid(progname));
1354 maxlen = total_processed_src - SKEIN_DIGEST_SIZE;
1355 do_mac = TFC_MAC_DROP2;
1356 goto _decrypt_again_vrfy2;
1361 tfc_esay("%s: signature is BAD: "
1362 "wrong password, key, mode, or file is not signed", tfc_format_pid(progname));
1363 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", tfc_format_pid(progname));
1368 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1369 memset(macresult, 0, sizeof(macresult));
1370 memset(tmpdata, 0, sizeof(tmpdata));
1372 else if (do_mac == TFC_MAC_SIGN) {
1373 skein_final(macresult, &sk);
1375 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1376 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1377 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1378 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1379 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1380 memset(macresult, 0, sizeof(macresult));
1384 lio = lrem = TF_FROM_BITS(macbits);
1386 _macwagain: lio = xwrite(dfd, pblk, lrem);
1387 if (lio != NOSIZE) ldone += lio;
1388 else xerror(NO, NO, NO, "%s", dstfname);
1389 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1395 total_written_dst += ldone;
1396 total_processed_dst += ldone;
1397 delta_processed += ldone;
1402 if (!strcmp(do_mac_file, "-")) mfd = 1;
1403 else mfd = xopen(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1404 if (do_outfmt == TFC_OUTFMT_B64) {
1405 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1406 memset(tmpdata, 0, TFC_TMPSIZE);
1407 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1408 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1409 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1411 tmpdata[lrem] = '\n';
1414 lio = xwrite(mfd, tmpdata, lrem);
1416 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1417 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1418 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1422 memset(macvrfy, 0, sizeof(macvrfy));
1423 memset(macresult, 0, sizeof(macresult));
1424 memset(tmpdata, 0, sizeof(tmpdata));
1426 else if (do_mac == TFC_MAC_DROP2) total_processed_src += SKEIN_DIGEST_SIZE;
1428 if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(0);