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 int getps_filter(struct getpasswd_state *getps, char chr, size_t pos)
36 getps->retn = ((size_t)-2);
42 static int getps_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
45 getps->retn = ((size_t)-2);
48 if (chr >= '0' && chr <= '9') return 1;
49 if (chr >= 'a' && chr <= 'f') return 1;
50 if (chr >= 'A' && chr <= 'F') return 1;
51 if (chr == '\x7f' || chr == '\x08'
52 || chr == '\x15' || chr == '\x17') return 1;
56 static inline int isctrlchr(int c)
59 if (c >= 0 && c <= 31) return 1;
60 if (c == 127) return 1;
64 static int getps_plain_filter(struct getpasswd_state *getps, char chr, size_t pos)
68 x = getps_filter(getps, chr, pos);
71 if (pos < getps->pwlen && !isctrlchr(chr))
72 write(getps->efd, &chr, sizeof(char));
76 static int getps_plain_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
80 x = getps_hex_filter(getps, chr, pos);
83 if (pos < getps->pwlen && !isctrlchr(chr))
84 write(getps->efd, &chr, sizeof(char));
88 static void make_hint(void *hint, size_t szhint, const void *data, size_t szdata)
90 char t[TF_FROM_BITS(TF_MAX_BITS)];
92 skein(t, TF_MAX_BITS, NULL, data, szdata);
93 xor_shrink(hint, szhint, t, sizeof(t));
94 memset(t, 0, sizeof(t));
97 static void raw_say_hint(void *hint, size_t szhint, const void *data, size_t szdata, const char *prompt)
99 make_hint(hint, szhint, data, szdata);
100 if (prompt) tfc_nfsay(stderr, "%s: ", prompt);
101 mehexdump(hint, szhint, szhint, 1);
102 memset(hint, 0, szhint);
105 static void say_hint(const void *data, size_t szdata, const char *prompt)
107 char t[TF_SIZE_UNIT];
108 raw_say_hint(t, TF_SIZE_UNIT, data, szdata, prompt);
109 /* t[] is erased (automatically) */
112 int main(int argc, char **argv)
116 char *s, *d, *t, *stoi;
120 progname = basename(argv[0]);
122 if (!isatty(2)) do_statline_dynamic = NO;
127 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
128 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
129 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
130 read_defaults(s, YES);
134 if (!strcmp(progname, "iotool")) {
135 do_edcrypt = TFC_DO_PLAIN;
137 ctr_mode = TFC_MODE_PLAIN;
141 while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:Pkzxc:l:qedn:vV:pwE:O:S:AmuM:R:Z:WHD:")) != -1) {
144 read_defaults(optarg, NO);
153 if (!strcasecmp(optarg, "show"))
154 counter_opt = TFC_CTR_SHOW;
155 else if (!strcasecmp(optarg, "head"))
156 counter_opt = TFC_CTR_HEAD;
157 else if (!strcasecmp(optarg, "rand"))
158 counter_opt = TFC_CTR_RAND;
159 else if (!strcasecmp(optarg, "zero"))
160 counter_opt = TFC_CTR_ZERO;
161 else if (strchr(optarg, ':')) {
164 counter_opt = TFC_CTR_SSET;
167 s = d = optarg; t = NULL;
168 while ((s = strtok_r(d, ",", &t))) {
175 chr = (char)strtoul(s, &stoi, 16);
176 if (!str_empty(stoi)) continue;
177 x = (size_t)strtoul(ss, &stoi, 10);
178 if (!str_empty(stoi)) continue;
180 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
184 else counter_file = sksum_hashlist_file = optarg;
187 if (!strcasecmp(optarg, "ctr"))
188 ctr_mode = TFC_MODE_CTR;
189 else if (!strcasecmp(optarg, "stream"))
190 ctr_mode = TFC_MODE_STREAM;
191 else if (!strcasecmp(optarg, "cbc"))
192 ctr_mode = TFC_MODE_CBC;
193 else if (!strcasecmp(optarg, "ecb"))
194 ctr_mode = TFC_MODE_ECB;
195 else if (!strcasecmp(optarg, "xts"))
196 ctr_mode = TFC_MODE_XTS;
197 else if (!strcasecmp(optarg, "ocb"))
198 ctr_mode = TFC_MODE_OCB;
199 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
202 do_edcrypt = TFC_DO_PLAIN;
204 ctr_mode = TFC_MODE_PLAIN;
207 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
210 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
213 macbits = strtoul(optarg, &stoi, 10);
214 if (macbits == 0 || !str_empty(stoi) || macbits < 8
215 || macbits > TF_MAX_BITS || macbits % 8)
216 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
219 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
220 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
223 if (!strcasecmp(optarg, "key"))
224 mackey_opt = TFC_MACKEY_RAWKEY;
225 else if (!strcasecmp(optarg, "pwd"))
226 mackey_opt = TFC_MACKEY_PASSWORD;
228 mackey_opt = TFC_MACKEY_FILE;
236 rawkey = TFC_RAWKEY_KEYFILE;
239 rawkey = TFC_RAWKEY_ASKSTR;
242 rawkey = TFC_RAWKEY_ASKHEX;
253 if (maxlen != NOFSIZE) break;
255 maxlen = tfc_humanfsize(optarg, &stoi);
256 if (!str_empty(stoi)) {
257 maxlen = tfc_fnamesize(optarg, YES);
258 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
259 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
260 "%s: invalid count value", optarg);
262 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
263 if (counter_opt == TFC_CTR_HEAD)
264 maxlen += TF_BLOCK_SIZE;
267 overwrite_source = YES;
270 if (!strcmp(optarg, "xall")) {
271 catch_all_errors = YES;
274 if (!strcmp(optarg, "xseek")) {
275 ignore_seek_errors = YES;
278 if (!strcmp(optarg, "exit"))
279 error_action = TFC_ERRACT_EXIT;
280 else if (!strncmp(optarg, "cont", 4))
281 error_action = TFC_ERRACT_CONT;
282 else if (!strcmp(optarg, "sync"))
283 error_action = TFC_ERRACT_SYNC;
284 else if (!strcmp(optarg, "lsync"))
285 error_action = TFC_ERRACT_LSYNC;
286 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
289 s = d = optarg; t = NULL;
290 while ((s = strtok_r(d, ",", &t))) {
292 if (!strcmp(s, "sync"))
293 write_flags |= O_SYNC;
294 else if (!strcmp(s, "trunc"))
295 write_flags |= O_TRUNC;
296 else if (!strcmp(s, "fsync"))
298 else if (!strcmp(s, "pad"))
300 else if (!strcmp(s, "xtime"))
301 do_preserve_time = YES;
302 else if (!strcmp(s, "gibsize"))
303 do_stats_in_gibs = YES;
304 else if (!strcmp(s, "plainstats"))
305 do_statline_dynamic = NO;
306 else if (!strcmp(s, "statless"))
308 else if (!strcmp(s, "norepeat"))
310 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
312 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
313 mac_pw_prompt = s+10;
314 else if (!strcmp(s, "shorthex"))
315 do_full_hexdump = NO;
316 else if (!strcmp(s, "fullkey"))
318 else if (!strcmp(s, "showsecrets"))
320 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
322 blksize = (size_t)tfc_humanfsize(s, &stoi);
323 if (!str_empty(stoi)) {
324 blksize = (size_t)tfc_fnamesize(s, YES);
325 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
326 if (blksize == NOSIZE) xerror(NO, YES, YES,
327 "%s: invalid block size value", s);
329 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
330 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
331 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
332 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
333 "%s: block size exceeds %u bytes",
334 s, TFC_U(TFC_BLKSIZE));
336 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
338 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
339 if (!str_empty(stoi)) {
340 xtsblocks = (size_t)tfc_fnamesize(s, YES);
341 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
342 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
343 "%s: invalid blocks per xts block value", s);
345 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
346 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
347 "%s: nr of blocks per xts block is not round to %u bytes",
348 s, TFC_U(TFC_BLKSIZE));
349 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
350 "%s: nr of blocks per xts block exceeds %u bytes",
351 s, TFC_U(TFC_BLKSIZE));
353 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
355 iseek = tfc_humanfsize(s, &stoi);
356 if (!str_empty(stoi)) {
357 iseek = tfc_fnamesize(s, YES);
358 iseek = tfc_modifysize(iseek, strchr(s, ':'));
359 if (iseek == NOFSIZE) xerror(NO, YES, YES,
360 "%s: invalid iseek value", s);
362 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
363 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
365 "%s: not round to TF block size "
367 s, TFC_U(TF_BLOCK_SIZE));
368 iseek_blocks = iseek / TF_BLOCK_SIZE;
370 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
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 ixseek value", s);
379 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
381 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
383 iseek_blocks = tfc_humanfsize(s, &stoi);
384 if (!str_empty(stoi)) {
385 iseek_blocks = tfc_fnamesize(s, YES);
386 if (iseek_blocks == NOFSIZE)
388 "%s: invalid ictr value", s);
389 iseek_blocks /= TF_BLOCK_SIZE;
390 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
392 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
394 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
396 iseek_blocks = tfc_humanfsize(s, &stoi);
397 if (!str_empty(stoi)) {
398 iseek_blocks = tfc_fnamesize(s, YES);
399 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
400 if (iseek_blocks == NOFSIZE)
402 "%s: invalid ixctr value", s);
404 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
405 if (iseek_blocks % TF_BLOCK_SIZE)
407 "%s: not round to TF block size "
408 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
409 iseek_blocks /= TF_BLOCK_SIZE;
411 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
413 oseek = tfc_humanfsize(s, &stoi);
414 if (!str_empty(stoi)) {
415 oseek = tfc_fnamesize(s, YES);
416 oseek = tfc_modifysize(oseek, strchr(s, ':'));
417 if (oseek == NOFSIZE) xerror(NO, YES, YES,
418 "%s: invalid oseek value", s);
420 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
422 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
424 maxlen = tfc_humanfsize(s, &stoi);
425 if (!str_empty(stoi)) {
426 maxlen = tfc_fnamesize(s, YES);
427 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
428 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
429 "%s: invalid count value", s);
431 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
432 if (counter_opt == TFC_CTR_HEAD)
433 maxlen += TF_BLOCK_SIZE;
435 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
437 if (!strcmp(s, "tail")) {
438 do_ftrunc = TFC_FTRUNC_TAIL;
439 ftrunc_dfd = NOFSIZE;
442 do_ftrunc = TFC_DO_FTRUNC;
443 ftrunc_dfd = tfc_humanfsize(s, &stoi);
444 if (!str_empty(stoi)) {
445 ftrunc_dfd = tfc_fnamesize(s, YES);
446 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
447 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
448 "%s: invalid ftrunc value", s);
450 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
453 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
455 maxkeylen = tfc_humanfsize(s, &stoi);
456 if (!str_empty(stoi)) {
457 maxkeylen = tfc_fnamesize(s, YES);
458 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
459 if (maxkeylen == NOSIZE)
461 "%s: invalid key length value", s);
463 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
465 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
467 keyoffset = tfc_humanfsize(s, &stoi);
468 if (!str_empty(stoi)) {
469 keyoffset = tfc_fnamesize(s, YES);
470 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
471 if (keyoffset == NOFSIZE)
473 "%s: invalid key offset value", s);
475 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
477 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
479 ctrsz = tfc_humanfsize(s, &stoi);
480 if (!str_empty(stoi)) {
481 ctrsz = (size_t)tfc_fnamesize(s, YES);
482 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
485 "%s: invalid counter length value", s);
487 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
488 if (ctrsz > TF_BLOCK_SIZE)
489 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
491 else xerror(NO, YES, YES, "invalid option %s", s);
495 do_mac = TFC_MAC_SIGN;
496 if (strcasecmp(optarg, "mac") != 0)
497 do_mac_file = optarg;
500 do_mac = TFC_MAC_VRFY;
501 if (!strcasecmp(optarg, "drop"))
502 do_mac = TFC_MAC_DROP;
503 else if (strcasecmp(optarg, "mac") != 0)
504 do_mac_file = optarg;
508 if (do_mac != TFC_MAC_VRFY)
509 xerror(NO, YES, YES, "signature source was not specified");
510 do_mac = TFC_MAC_JUST_VRFY;
511 if (c == 'u') do_mac = TFC_MAC_JUST_VRFY2;
515 if (maxlen != NOFSIZE) {
516 if (c == 'Z') genzero_nr_bytes = maxlen;
517 else genrandom_nr_bytes = maxlen;
521 if (!strcasecmp(optarg, "cbs"))
523 else if (!strcasecmp(optarg, "ks"))
524 t = TF_FROM_BITS(TFC_KEY_BITS);
525 else if (!strcasecmp(optarg, "xks"))
526 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
527 else if (!strcasecmp(optarg, "iobs"))
530 t = tfc_humanfsize(optarg, &stoi);
531 if (!str_empty(stoi)) {
532 t = tfc_fnamesize(optarg, NO);
533 t = tfc_modifysize(t, strchr(optarg, ':'));
535 else t = tfc_modifysize(t, strchr(optarg, ':'));
537 if (c == 'Z') genzero_nr_bytes = maxlen = t;
538 else genrandom_nr_bytes = maxlen = t;
542 do_preserve_time = YES;
545 do_outfmt = TFC_OUTFMT_B64;
548 do_outfmt = TFC_OUTFMT_RAW;
551 do_outfmt = TFC_OUTFMT_HEX;
556 do_full_hexdump = NO;
563 td = strtod(optarg, &stoi);
564 status_timer = TFC_DTOUSECS(td);
565 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
573 if (!strcmp(progname, "tfbench")) {
574 if (!*(argv+optind)) usage();
576 td = strtod(*(argv+optind), &stoi);
577 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
579 "%s: invalid benchmark time in seconds", *(argv+optind));
580 bench_timer = TFC_DTOUSECS(td);
581 do_benchmark(bench_timer, td);
583 if (genrandom_nr_bytes) {
584 ctr_mode = TFC_MODE_STREAM;
585 do_edcrypt = TFC_DO_ENCRYPT;
586 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
588 if (genzero_nr_bytes) {
589 ctr_mode = TFC_MODE_PLAIN;
590 do_edcrypt = TFC_DO_PLAIN;
591 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
594 if (rawkey && password)
595 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
596 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
597 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
598 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
599 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
600 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
601 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
602 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
603 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
604 if (overwrite_source && counter_opt == TFC_CTR_RAND)
605 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
606 if (ctr_mode == TFC_MODE_PLAIN
607 && (do_edcrypt || do_mac || rawkey
608 || mackey_opt || counter_opt || counter_file))
609 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
617 memset(tfc_salt, 0, TFC_MAX_SALT);
619 if (!strcasecmp(saltf, "disable")) goto _nosalt;
621 if (!strcmp(saltf, "-")) saltfd = 0;
622 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
623 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
624 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
625 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
631 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
635 if (!strcmp(mackeyf, "-")) mkfd = 0;
636 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
637 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
639 skein_init(&sk, TFC_KEY_BITS);
646 lrem = lblock = sizeof(tmpdata);
647 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
648 _mkragain: lio = xread(mkfd, pblk, lrem);
649 if (lio == 0 && do_stop == NO) do_stop = YES;
650 if (lio != NOSIZE) ldone += lio;
652 if (errno != EIO && catch_all_errors != YES)
653 xerror(NO, NO, NO, "%s", mackeyf);
654 switch (error_action) {
655 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
656 case TFC_ERRACT_SYNC:
657 case TFC_ERRACT_LSYNC:
658 xerror(YES, NO, NO, "%s", mackeyf);
659 lio = ldone = lrem = lblock;
660 memset(tmpdata, 0, lio);
661 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
662 else lseek(mkfd, rdpos + lio, SEEK_SET);
664 default: xerror(NO, NO, NO, "%s", mackeyf); break;
667 if (lio && lio < lrem) {
673 skein_update(&sk, tmpdata, ldone);
676 skein_final(mackey, &sk);
680 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
681 memset(&getps, 0, sizeof(struct getpasswd_state));
682 getps.fd = getps.efd = -1;
683 getps.passwd = pwdask;
684 getps.pwlen = sizeof(pwdask)-1;
685 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
686 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
687 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
688 getps.flags = GETP_WAITFILL;
689 n = xgetpasswd(&getps);
690 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
691 if (n == ((size_t)-2)) xexit(1);
692 if (verbose) say_hint(pwdask, n, "MAC password hint");
693 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
697 if ((strlen(progname) <= 9)
698 && ((!strcmp(progname, "sksum"))
699 || ((!memcmp(progname, "sk", 2))
700 && (!memcmp(progname+3, "sum", 3)
701 || !memcmp(progname+4, "sum", 3)
702 || !memcmp(progname+5, "sum", 3)
703 || !memcmp(progname+6, "sum", 3)))))
704 do_sksum(progname, argv+optind);
705 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
710 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
711 if (!strcmp(argv[idx], "-")) kfd = 0;
712 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
713 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
715 lio = strnlen(argv[idx], PATH_MAX);
716 memset(argv[idx], '*', lio);
723 if (do_full_key == NO && tweakf) {
726 if (!strcmp(tweakf, "-")) twfd = 0;
727 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
728 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
729 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
730 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
731 if (ldone < TF_TWEAK_SIZE)
732 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
739 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
741 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
742 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
743 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
745 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
747 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
748 if (verbose) tfc_esay("%s: disabling signature verification on "
749 "requested partial decryption.", progname);
753 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
754 maxlen = tfc_fdsize(sfd);
755 if (maxlen == NOFSIZE)
757 "Cannot verify embedded MAC with non-seekable source!");
758 maxlen -= TF_FROM_BITS(macbits);
760 srcfname = argv[idx];
764 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
765 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
767 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
768 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
770 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
775 if (!strcmp(counter_file, "-")) ctrfd = 0;
776 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
777 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
778 lio = xread(ctrfd, ctr, ctrsz);
779 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
780 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
783 else if (counter_opt == TFC_CTR_HEAD) {
786 lrem = lblock = ctrsz;
787 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
788 _ctrragain: lio = xread(sfd, pblk, lrem);
789 if (lio != NOSIZE) ldone += lio;
791 if (errno != EIO && catch_all_errors != YES)
792 xerror(NO, NO, NO, "%s", srcfname);
793 switch (error_action) {
794 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
795 case TFC_ERRACT_SYNC:
796 case TFC_ERRACT_LSYNC:
797 xerror(YES, NO, NO, "%s", srcfname);
798 lio = ldone = lrem = lblock;
800 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
801 else lseek(sfd, rdpos + lio, SEEK_SET);
803 default: xerror(NO, NO, NO, "%s", srcfname); break;
806 if (lio && lio < lrem) {
811 total_processed_src += ldone;
816 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
818 if (lseek(sfd, iseek, SEEK_SET) == -1)
819 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
822 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
824 if (verbose) tfc_esay("%s: hashing password", progname);
826 if (rawkey == TFC_RAWKEY_KEYFILE) {
827 tfc_yesno xtskeyset = NO;
831 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
832 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
833 _keyragain: lio = xread(kfd, pblk, lrem);
834 if (lio != NOSIZE) ldone += lio;
836 if (errno != EIO && catch_all_errors != YES)
837 xerror(NO, NO, NO, "reading key");
838 switch (error_action) {
839 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
840 case TFC_ERRACT_SYNC:
841 case TFC_ERRACT_LSYNC:
842 xerror(YES, NO, NO, "reading key");
843 lio = ldone = lrem = lblock;
845 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
846 else lseek(kfd, rdpos + lio, SEEK_SET);
848 default: xerror(NO, NO, NO, "reading key"); break;
851 if (lio && lio < lrem) {
856 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
858 if (ctr_mode == TFC_MODE_XTS) {
859 if (xtskeyset == NO) {
866 else if (rawkey == TFC_RAWKEY_ASKSTR) {
867 tfc_yesno xtskeyset = NO;
869 pblk = key; n = sizeof(key);
870 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
871 getps.fd = getps.efd = -1;
872 getps.passwd = (char *)pblk;
874 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
875 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
876 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
877 getps.flags = GETP_WAITFILL;
878 n = xgetpasswd(&getps);
879 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
880 if (n == ((size_t)-2)) xexit(1);
881 if (verbose) say_hint(pblk, n, "Raw string key hint");
882 if (ctr_mode == TFC_MODE_XTS) {
883 if (xtskeyset == NO) {
884 pblk = xtskey; n = sizeof(xtskey);
890 else if (rawkey == TFC_RAWKEY_ASKHEX) {
891 tfc_yesno xtskeyset = NO;
895 memset(&getps, 0, sizeof(struct getpasswd_state));
896 getps.fd = getps.efd = -1;
897 getps.passwd = pwdask;
898 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
899 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
900 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
901 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
902 getps.flags = GETP_WAITFILL;
903 n = xgetpasswd(&getps);
904 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
905 if (n == ((size_t)-2)) xexit(1);
907 tfc_esay("Please input even number of hex digits!");
908 goto _rawkey_hex_again;
910 hex2bin(pblk, pwdask);
911 memset(pwdask, 0, sizeof(pwdask));
912 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
913 if (ctr_mode == TFC_MODE_XTS) {
914 if (xtskeyset == NO) {
917 goto _rawkey_hex_again;
922 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
923 getps.fd = getps.efd = -1;
924 getps.passwd = pwdask;
925 getps.pwlen = sizeof(pwdask)-1;
926 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
927 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
928 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
929 getps.flags = GETP_WAITFILL;
930 n = xgetpasswd(&getps);
931 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
932 if (n == ((size_t)-2)) xexit(1);
933 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
934 getps.fd = getps.efd = -1;
935 getps.passwd = pwdagain;
936 getps.pwlen = sizeof(pwdagain)-1;
937 getps.echo = "Enter it again: ";
938 getps.flags = GETP_WAITFILL;
939 n = xgetpasswd(&getps);
940 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
941 if (n == ((size_t)-2)) xexit(1);
942 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
943 tfc_esay("Passwords are different, try again");
947 if (verbose) say_hint(pwdask, n, "Password hint");
948 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
949 memset(pwdask, 0, sizeof(pwdask));
950 memset(pwdagain, 0, sizeof(pwdagain));
953 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
954 xerror(NO, NO, YES, "hashing key");
958 if (tfc_saltsz > 0) {
959 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
960 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
962 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
963 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
964 memset(tfc_salt, 0, TFC_MAX_SALT);
967 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
968 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
973 tfc_yesno xtskeyset = NO;
976 if (!strcmp(genkeyf, "-")) krfd = 1;
977 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
978 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
979 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
980 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
981 if (verbose && xtskeyset == NO) {
982 tfc_esay("%s: password hashing done", progname);
983 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
984 tfc_esay("%s: Have a nice day!", progname);
987 if (ctr_mode == TFC_MODE_XTS) {
988 if (xtskeyset == NO) {
1000 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
1001 if (verbose) tfc_esay("%s: disabling signature verification on "
1002 "requested partial decryption.", progname);
1007 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1008 if (ctr_mode < TFC_MODE_OCB) {
1009 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1010 "will be slower.", progname);
1011 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1012 else skein_init(&sk, macbits);
1016 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1017 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1021 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1022 if (do_full_key == NO) {
1023 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1024 tf_tweak_set(key, tweak);
1026 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1028 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1030 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1031 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1032 if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1034 if (counter_opt == TFC_CTR_SHOW) {
1035 switch (do_outfmt) {
1036 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1037 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1038 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1041 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1048 if (verbose) tfc_esay("%s: password hashing done", progname);
1050 if (overwrite_source && srcfname) argv[idx] = srcfname;
1054 if (!strcmp(argv[idx], "-")) dfd = 1;
1055 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
1057 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1058 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
1060 dstfname = argv[idx];
1065 if (lseek(dfd, oseek, SEEK_SET) == -1)
1066 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1069 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1070 memset(&sigact, 0, sizeof(sigact));
1071 sigact.sa_flags = SA_RESTART;
1072 sigact.sa_handler = print_crypt_status;
1073 sigaction(SIGUSR1, &sigact, NULL);
1074 sigaction(SIGALRM, &sigact, NULL);
1075 if (status_timer) setup_next_alarm(status_timer);
1076 sigact.sa_handler = change_status_width;
1077 sigaction(SIGQUIT, &sigact, NULL);
1078 sigact.sa_handler = change_status_timer;
1079 sigaction(SIGUSR2, &sigact, NULL);
1081 sigact.sa_handler = print_crypt_status;
1082 sigaction(SIGINT, &sigact, NULL);
1083 sigaction(SIGTERM, &sigact, NULL);
1084 sigaction(SIGTSTP, &sigact, NULL);
1087 sigact.sa_handler = exit_sigterm;
1088 sigaction(SIGINT, &sigact, NULL);
1089 sigaction(SIGTERM, &sigact, NULL);
1090 sigact.sa_handler = handle_sigtstp;
1091 sigaction(SIGTSTP, &sigact, NULL);
1093 memset(&sigact, 0, sizeof(struct sigaction));
1095 tfc_getcurtime(&delta_time);
1098 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1102 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1103 if (lio != NOSIZE) ldone += lio;
1104 else xerror(NO, NO, NO, "%s", dstfname);
1105 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1111 total_processed_dst += ldone;
1112 delta_processed += ldone;
1115 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1117 if (do_mac == TFC_MAC_JUST_VRFY2) {
1118 rwd = tfc_fdgetpos(sfd);
1119 if (rwd == NOFSIZE) {
1120 tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", progname);
1121 do_mac = TFC_MAC_VRFY;
1123 goto _nodecrypt_again_vrfy2;
1125 _decrypt_again_vrfy2:
1126 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1127 xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1129 total_processed_src = rwd;
1130 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1131 memset(svctr, 0, TF_BLOCK_SIZE);
1134 _nodecrypt_again_vrfy2:
1141 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1142 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1143 _ragain: lio = xread(sfd, pblk, lrem);
1144 if (lio == 0) do_stop = YES;
1145 if (lio != NOSIZE) ldone += lio;
1147 if (errno != EIO && catch_all_errors != YES)
1148 xerror(NO, NO, NO, "%s", srcfname);
1149 switch (error_action) {
1150 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1151 case TFC_ERRACT_SYNC:
1152 case TFC_ERRACT_LSYNC:
1153 xerror(YES, NO, NO, "%s", srcfname);
1154 lio = ldone = lrem = lblock;
1155 memset(srcblk, 0, lio);
1156 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1157 else lseek(sfd, rdpos + lio, SEEK_SET);
1159 default: xerror(NO, NO, NO, "%s", srcfname); break;
1162 if (lio && lio < lrem) {
1167 total_processed_src += ldone;
1169 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1170 size_t orig = ldone;
1171 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1172 if (ldone > blksize) ldone = blksize;
1173 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1176 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1177 skein_update(&sk, srcblk, ldone);
1179 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1180 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1181 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1182 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1183 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1184 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1185 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1186 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1187 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1188 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1189 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1190 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1191 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1192 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1194 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1195 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1196 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1197 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1199 else if (ctr_mode == TFC_MODE_PLAIN)
1200 memcpy(dstblk, srcblk, ldone);
1202 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1203 skein_update(&sk, dstblk, ldone);
1204 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1209 _wagain: lio = xwrite(dfd, pblk, lrem);
1210 if (lio != NOSIZE) ldone += lio;
1211 else xerror(NO, NO, NO, "%s", dstfname);
1212 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1218 total_written_dst += ldone;
1219 _nowrite: total_processed_dst += ldone;
1220 delta_processed += ldone;
1222 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1226 if (do_mac >= TFC_MAC_VRFY) {
1230 lrem = lblock = TF_FROM_BITS(macbits);
1231 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1232 _macragain: lio = xread(sfd, pblk, lrem);
1233 if (lio != NOSIZE) ldone += lio;
1235 if (errno != EIO && catch_all_errors != YES)
1236 xerror(NO, NO, NO, "%s", srcfname);
1237 switch (error_action) {
1238 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1239 case TFC_ERRACT_SYNC:
1240 case TFC_ERRACT_LSYNC:
1241 xerror(YES, NO, NO, "%s", srcfname);
1242 lio = ldone = lrem = lblock;
1243 memset(macvrfy, 0, lio);
1244 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1245 else lseek(sfd, rdpos + lio, SEEK_SET);
1247 default: xerror(NO, NO, NO, "%s", srcfname); break;
1250 if (lio && lio < lrem) {
1255 total_processed_src += ldone;
1260 if (!strcmp(do_mac_file, "-")) mfd = 0;
1261 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1262 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1263 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1264 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1265 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1266 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1267 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1268 lio = TF_FROM_BITS(macbits);
1269 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1271 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1275 if (ldone < TF_FROM_BITS(macbits)) {
1276 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1277 "not verifying", progname, ldone);
1282 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1283 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1285 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1286 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1287 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1288 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1289 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1290 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1292 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1294 tfc_esay("%s: signature is good", progname);
1296 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1297 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1300 if (do_mac == TFC_MAC_JUST_VRFY2) {
1301 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", progname);
1302 do_mac = TFC_MAC_DROP;
1303 goto _decrypt_again_vrfy2;
1308 tfc_esay("%s: signature is BAD: "
1309 "wrong password, key, mode, or file is not signed", progname);
1310 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", progname);
1315 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1316 memset(macresult, 0, sizeof(macresult));
1317 memset(tmpdata, 0, sizeof(tmpdata));
1320 else if (do_mac == TFC_MAC_SIGN) {
1321 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1322 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1324 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1325 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1326 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1327 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1328 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1329 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1330 memset(macresult, 0, sizeof(macresult));
1334 lio = lrem = TF_FROM_BITS(macbits);
1336 _macwagain: lio = xwrite(dfd, pblk, lrem);
1337 if (lio != NOSIZE) ldone += lio;
1338 else xerror(NO, NO, NO, "%s", dstfname);
1339 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1345 total_processed_dst += ldone;
1346 delta_processed += ldone;
1351 if (!strcmp(do_mac_file, "-")) mfd = 1;
1352 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1353 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1354 if (do_outfmt == TFC_OUTFMT_B64) {
1355 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1356 memset(tmpdata, 0, TFC_TMPSIZE);
1357 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1358 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1359 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1361 tmpdata[lrem] = '\n';
1364 lio = xwrite(mfd, tmpdata, lrem);
1366 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1367 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1368 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1372 memset(macvrfy, 0, sizeof(macvrfy));
1373 memset(macresult, 0, sizeof(macresult));
1374 memset(tmpdata, 0, sizeof(tmpdata));
1377 if (verbose || status_timer || do_stop == YES) print_crypt_status(0);