2 * tfcrypt -- high security Threefish encryption tool.
4 * tfcrypt is copyrighted:
5 * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
7 * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 static tfc_byte svctr[TF_BLOCK_SIZE];
32 static tfc_fsize rwd, do_read_loops, loopcnt;
34 static void open_log(const char *logfile)
39 if (!strcmp(logfile, "-")) return;
43 fd = xopen(logfile, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC);
46 if (dup2(fd, 2) == -1) xexit(2);
48 do_statline_dynamic = NO;
51 static int getps_filter(struct getpasswd_state *getps, char chr, size_t pos)
54 getps->retn = ((size_t)-2);
60 static int getps_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
63 getps->retn = ((size_t)-2);
66 if (chr >= '0' && chr <= '9') return 1;
67 if (chr >= 'a' && chr <= 'f') return 1;
68 if (chr >= 'A' && chr <= 'F') return 1;
69 if (chr == '\x7f' || chr == '\x08'
70 || chr == '\x15' || chr == '\x17') return 1;
74 static inline int isctrlchr(int c)
77 if (c >= 0 && c <= 31) return 1;
78 if (c == 127) return 1;
82 static int getps_plain_filter(struct getpasswd_state *getps, char chr, size_t pos)
86 x = getps_filter(getps, chr, pos);
89 if (pos < getps->pwlen && !isctrlchr(chr))
90 write(getps->efd, &chr, sizeof(char));
94 static int getps_plain_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
98 x = getps_hex_filter(getps, chr, pos);
101 if (pos < getps->pwlen && !isctrlchr(chr))
102 write(getps->efd, &chr, sizeof(char));
106 static void make_hint(void *hint, size_t szhint, const void *data, size_t szdata)
108 char t[TF_FROM_BITS(TF_MAX_BITS)];
110 skein(t, TF_MAX_BITS, NULL, data, szdata);
111 xor_shrink(hint, szhint, t, sizeof(t));
112 memset(t, 0, sizeof(t));
115 static void raw_say_hint(void *hint, size_t szhint, const void *data, size_t szdata, const char *prompt)
117 make_hint(hint, szhint, data, szdata);
118 if (prompt) tfc_nfsay(stderr, "%s: ", prompt);
119 mehexdump(hint, szhint, szhint, 1);
120 memset(hint, 0, szhint);
123 static void say_hint(const void *data, size_t szdata, const char *prompt)
125 char t[TF_SIZE_UNIT];
126 raw_say_hint(t, TF_SIZE_UNIT, data, szdata, prompt);
127 /* t[] is erased (automatically) */
130 int main(int argc, char **argv)
134 char *s, *d, *t, *stoi;
138 progname = basename(argv[0]);
140 if (!isatty(2)) do_statline_dynamic = NO;
145 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
146 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
147 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
148 read_defaults(s, YES);
152 if (!strcmp(progname, "iotool")) {
153 do_edcrypt = TFC_DO_PLAIN;
155 ctr_mode = TFC_MODE_PLAIN;
159 while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:Pkzxc:l:qedn:vV:pwE:o:O:S:AmuM:R:Z:WHD:")) != -1) {
162 read_defaults(optarg, NO);
171 if (!strcasecmp(optarg, "show"))
172 counter_opt = TFC_CTR_SHOW;
173 else if (!strcasecmp(optarg, "head"))
174 counter_opt = TFC_CTR_HEAD;
175 else if (!strcasecmp(optarg, "rand"))
176 counter_opt = TFC_CTR_RAND;
177 else if (!strcasecmp(optarg, "zero"))
178 counter_opt = TFC_CTR_ZERO;
179 else if (strchr(optarg, ':')) {
182 counter_opt = TFC_CTR_SSET;
185 s = d = optarg; t = NULL;
186 while ((s = strtok_r(d, ",", &t))) {
193 chr = (char)strtoul(s, &stoi, 16);
194 if (!str_empty(stoi)) continue;
195 x = (size_t)strtoul(ss, &stoi, 10);
196 if (!str_empty(stoi)) continue;
198 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
202 else counter_file = sksum_hashlist_file = optarg;
205 if (!strcasecmp(optarg, "ctr"))
206 ctr_mode = TFC_MODE_CTR;
207 else if (!strcasecmp(optarg, "stream"))
208 ctr_mode = TFC_MODE_STREAM;
209 else if (!strcasecmp(optarg, "cbc"))
210 ctr_mode = TFC_MODE_CBC;
211 else if (!strcasecmp(optarg, "ecb"))
212 ctr_mode = TFC_MODE_ECB;
213 else if (!strcasecmp(optarg, "xts"))
214 ctr_mode = TFC_MODE_XTS;
215 else if (!strcasecmp(optarg, "ocb"))
216 ctr_mode = TFC_MODE_OCB;
217 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
220 do_edcrypt = TFC_DO_PLAIN;
222 ctr_mode = TFC_MODE_PLAIN;
225 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
228 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
231 macbits = strtoul(optarg, &stoi, 10);
232 if (macbits == 0 || !str_empty(stoi) || macbits < 8
233 || macbits > TF_MAX_BITS || macbits % 8)
234 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
237 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
238 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
241 if (!strcasecmp(optarg, "key"))
242 mackey_opt = TFC_MACKEY_RAWKEY;
243 else if (!strcasecmp(optarg, "pwd"))
244 mackey_opt = TFC_MACKEY_PASSWORD;
246 mackey_opt = TFC_MACKEY_FILE;
254 rawkey = TFC_RAWKEY_KEYFILE;
257 rawkey = TFC_RAWKEY_ASKSTR;
260 rawkey = TFC_RAWKEY_ASKHEX;
271 if (maxlen != NOFSIZE) break;
273 maxlen = tfc_humanfsize(optarg, &stoi);
274 if (!str_empty(stoi)) {
275 maxlen = tfc_fnamesize(optarg, YES);
276 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
277 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
278 "%s: invalid count value", optarg);
280 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
281 if (counter_opt == TFC_CTR_HEAD)
282 maxlen += TF_BLOCK_SIZE;
285 overwrite_source = YES;
288 if (!strcmp(optarg, "xall")) {
289 catch_all_errors = YES;
292 if (!strcmp(optarg, "xseek")) {
293 ignore_seek_errors = YES;
296 if (!strcmp(optarg, "exit"))
297 error_action = TFC_ERRACT_EXIT;
298 else if (!strncmp(optarg, "cont", 4))
299 error_action = TFC_ERRACT_CONT;
300 else if (!strcmp(optarg, "sync"))
301 error_action = TFC_ERRACT_SYNC;
302 else if (!strcmp(optarg, "lsync"))
303 error_action = TFC_ERRACT_LSYNC;
304 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
310 s = d = optarg; t = NULL;
311 while ((s = strtok_r(d, ",", &t))) {
313 if (!strcmp(s, "sync"))
314 write_flags |= O_SYNC;
315 else if (!strcmp(s, "trunc"))
316 write_flags |= O_TRUNC;
317 else if (!strcmp(s, "append"))
318 write_flags |= O_APPEND;
319 else if (!strcmp(s, "fsync"))
321 else if (!strcmp(s, "pad"))
323 else if (!strcmp(s, "ro"))
325 else if (!strcmp(s, "rw"))
327 else if (!strcmp(s, "xtime"))
328 do_preserve_time = YES;
329 else if (!strcmp(s, "gibsize"))
330 do_stats_in_gibs = YES;
331 else if (!strcmp(s, "plainstats"))
332 do_statline_dynamic = NO;
333 else if (!strcmp(s, "statless"))
335 else if (!strcmp(s, "norepeat"))
337 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
339 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
340 mac_pw_prompt = s+10;
341 else if (!strcmp(s, "shorthex"))
342 do_full_hexdump = NO;
343 else if (!strcmp(s, "fullkey"))
345 else if (!strcmp(s, "showsecrets"))
347 else if (!strcmp(s, "finished"))
348 show_when_done = YES;
349 else if (!strcmp(s, "pid"))
351 else if (!strncmp(s, "readloops", 9) && *(s+9) == '=') {
352 do_read_loops = tfc_humanfsize(s+10, &stoi);
353 if (!str_empty(stoi)) do_read_loops = NOSIZE;
355 else if (!strncmp(s, "logfile", 7) && *(s+7) == '=')
357 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
359 blksize = (size_t)tfc_humanfsize(s, &stoi);
360 if (!str_empty(stoi)) {
361 blksize = (size_t)tfc_fnamesize(s, YES);
362 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
363 if (blksize == NOSIZE) xerror(NO, YES, YES,
364 "%s: invalid block size value", s);
366 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
367 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
368 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
369 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
370 "%s: block size exceeds %u bytes",
371 s, TFC_U(TFC_BLKSIZE));
373 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
375 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
376 if (!str_empty(stoi)) {
377 xtsblocks = (size_t)tfc_fnamesize(s, YES);
378 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
379 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
380 "%s: invalid blocks per xts block value", s);
382 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
383 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
384 "%s: nr of blocks per xts block is not round to %u bytes",
385 s, TFC_U(TFC_BLKSIZE));
386 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
387 "%s: nr of blocks per xts block exceeds %u bytes",
388 s, TFC_U(TFC_BLKSIZE));
390 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
392 iseek = tfc_humanfsize(s, &stoi);
393 if (!str_empty(stoi)) {
394 iseek = tfc_fnamesize(s, YES);
395 iseek = tfc_modifysize(iseek, strchr(s, ':'));
396 if (iseek == NOFSIZE) xerror(NO, YES, YES,
397 "%s: invalid iseek value", s);
399 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
400 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
402 "%s: not round to TF block size "
404 s, TFC_U(TF_BLOCK_SIZE));
405 iseek_blocks = iseek / TF_BLOCK_SIZE;
407 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
409 iseek = tfc_humanfsize(s, &stoi);
410 if (!str_empty(stoi)) {
411 iseek = tfc_fnamesize(s, YES);
412 iseek = tfc_modifysize(iseek, strchr(s, ':'));
413 if (iseek == NOFSIZE) xerror(NO, YES, YES,
414 "%s: invalid ixseek value", s);
416 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
418 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
420 iseek_blocks = tfc_humanfsize(s, &stoi);
421 if (!str_empty(stoi)) {
422 iseek_blocks = tfc_fnamesize(s, YES);
423 if (iseek_blocks == NOFSIZE)
425 "%s: invalid ictr value", s);
426 iseek_blocks /= TF_BLOCK_SIZE;
427 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
429 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
431 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
433 iseek_blocks = tfc_humanfsize(s, &stoi);
434 if (!str_empty(stoi)) {
435 iseek_blocks = tfc_fnamesize(s, YES);
436 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
437 if (iseek_blocks == NOFSIZE)
439 "%s: invalid ixctr value", s);
441 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
442 if (iseek_blocks % TF_BLOCK_SIZE)
444 "%s: not round to TF block size "
445 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
446 iseek_blocks /= TF_BLOCK_SIZE;
448 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
450 oseek = tfc_humanfsize(s, &stoi);
451 if (!str_empty(stoi)) {
452 oseek = tfc_fnamesize(s, YES);
453 oseek = tfc_modifysize(oseek, strchr(s, ':'));
454 if (oseek == NOFSIZE) xerror(NO, YES, YES,
455 "%s: invalid oseek value", s);
457 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
459 else if (!strncmp(s, "ioseek", 6) && *(s+6) == '=') {
462 iseek = tfc_humanfsize(s, &stoi);
463 if (!str_empty(stoi)) {
464 iseek = tfc_fnamesize(s, YES);
465 iseek = tfc_modifysize(iseek, strchr(s, ':'));
466 if (iseek == NOFSIZE) xerror(NO, YES, YES,
467 "%s: invalid iseek value", s);
469 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
470 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
472 "%s: not round to TF block size "
474 s, TFC_U(TF_BLOCK_SIZE));
475 iseek_blocks = iseek / TF_BLOCK_SIZE;
477 oseek = tfc_humanfsize(s, &stoi);
478 if (!str_empty(stoi)) {
479 oseek = tfc_fnamesize(s, YES);
480 oseek = tfc_modifysize(oseek, strchr(s, ':'));
481 if (oseek == NOFSIZE) xerror(NO, YES, YES,
482 "%s: invalid oseek value", s);
484 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
486 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
488 maxlen = tfc_humanfsize(s, &stoi);
489 if (!str_empty(stoi)) {
490 maxlen = tfc_fnamesize(s, YES);
491 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
492 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
493 "%s: invalid count value", s);
495 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
496 if (counter_opt == TFC_CTR_HEAD)
497 maxlen += TF_BLOCK_SIZE;
499 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
501 if (!strcmp(s, "tail")) {
502 do_ftrunc = TFC_FTRUNC_TAIL;
503 ftrunc_dfd = NOFSIZE;
506 do_ftrunc = TFC_DO_FTRUNC;
507 ftrunc_dfd = tfc_humanfsize(s, &stoi);
508 if (!str_empty(stoi)) {
509 ftrunc_dfd = tfc_fnamesize(s, YES);
510 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
511 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
512 "%s: invalid ftrunc value", s);
514 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
517 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
519 maxkeylen = tfc_humanfsize(s, &stoi);
520 if (!str_empty(stoi)) {
521 maxkeylen = tfc_fnamesize(s, YES);
522 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
523 if (maxkeylen == NOSIZE)
525 "%s: invalid key length value", s);
527 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
529 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
531 keyoffset = tfc_humanfsize(s, &stoi);
532 if (!str_empty(stoi)) {
533 keyoffset = tfc_fnamesize(s, YES);
534 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
535 if (keyoffset == NOFSIZE)
537 "%s: invalid key offset value", s);
539 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
541 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
543 ctrsz = tfc_humanfsize(s, &stoi);
544 if (!str_empty(stoi)) {
545 ctrsz = (size_t)tfc_fnamesize(s, YES);
546 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
549 "%s: invalid counter length value", s);
551 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
552 if (ctrsz > TF_BLOCK_SIZE)
553 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
555 else xerror(NO, YES, YES, "invalid option %s", s);
559 do_mac = TFC_MAC_SIGN;
560 if (strcasecmp(optarg, "mac") != 0)
561 do_mac_file = optarg;
564 do_mac = TFC_MAC_VRFY;
565 if (!strcasecmp(optarg, "drop"))
566 do_mac = TFC_MAC_DROP;
567 else if (strcasecmp(optarg, "mac") != 0)
568 do_mac_file = optarg;
572 if (do_mac != TFC_MAC_VRFY)
573 xerror(NO, YES, YES, "signature source was not specified");
574 do_mac = TFC_MAC_JUST_VRFY;
575 if (c == 'u') do_mac = TFC_MAC_JUST_VRFY2;
579 if (maxlen != NOFSIZE) {
580 if (c == 'Z') genzero_nr_bytes = maxlen;
581 else genrandom_nr_bytes = maxlen;
585 if (!strcasecmp(optarg, "cbs"))
587 else if (!strcasecmp(optarg, "ks"))
588 t = TF_FROM_BITS(TFC_KEY_BITS);
589 else if (!strcasecmp(optarg, "xks"))
590 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
591 else if (!strcasecmp(optarg, "iobs"))
594 t = tfc_humanfsize(optarg, &stoi);
595 if (!str_empty(stoi)) {
596 t = tfc_fnamesize(optarg, NO);
597 t = tfc_modifysize(t, strchr(optarg, ':'));
599 else t = tfc_modifysize(t, strchr(optarg, ':'));
601 if (c == 'Z') genzero_nr_bytes = maxlen = t;
602 else genrandom_nr_bytes = maxlen = t;
606 do_preserve_time = YES;
609 do_outfmt = TFC_OUTFMT_B64;
612 do_outfmt = TFC_OUTFMT_RAW;
615 do_outfmt = TFC_OUTFMT_HEX;
621 do_full_hexdump = NO;
628 td = strtod(optarg, &stoi);
629 status_timer = TFC_DTOUSECS(td);
630 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
638 if (!strcmp(progname, "tfbench")) {
639 if (!*(argv+optind)) usage();
641 td = strtod(*(argv+optind), &stoi);
642 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
644 "%s: invalid benchmark time in seconds", *(argv+optind));
645 bench_timer = TFC_DTOUSECS(td);
646 do_benchmark(bench_timer, td);
648 if (genrandom_nr_bytes) {
649 ctr_mode = TFC_MODE_STREAM;
650 do_edcrypt = TFC_DO_ENCRYPT;
651 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
653 if (genzero_nr_bytes) {
654 ctr_mode = TFC_MODE_PLAIN;
655 do_edcrypt = TFC_DO_PLAIN;
656 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
659 if (rawkey && password)
660 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
661 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
662 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
663 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
664 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
665 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
666 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
667 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
668 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
669 if (overwrite_source && counter_opt == TFC_CTR_RAND)
670 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
671 if (ctr_mode == TFC_MODE_PLAIN
672 && (do_edcrypt || do_mac || rawkey
673 || mackey_opt || counter_opt || counter_file))
674 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
682 memset(tfc_salt, 0, TFC_MAX_SALT);
684 if (!strcasecmp(saltf, "disable")) goto _nosalt;
686 if (!strcmp(saltf, "-")) saltfd = 0;
687 else saltfd = xopen(saltf, O_RDONLY | O_LARGEFILE);
688 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
689 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
695 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
699 if (!strcmp(mackeyf, "-")) mkfd = 0;
700 else mkfd = xopen(mackeyf, O_RDONLY | O_LARGEFILE);
702 skein_init(&sk, TFC_KEY_BITS);
709 lrem = lblock = sizeof(tmpdata);
710 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
711 _mkragain: lio = xread(mkfd, pblk, lrem);
712 if (lio == 0 && do_stop == NO) do_stop = YES;
713 if (lio != NOSIZE) ldone += lio;
715 if (errno != EIO && catch_all_errors != YES)
716 xerror(NO, NO, NO, "%s", mackeyf);
717 switch (error_action) {
718 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
719 case TFC_ERRACT_SYNC:
720 case TFC_ERRACT_LSYNC:
721 xerror(YES, NO, NO, "%s", mackeyf);
722 lio = ldone = lrem = lblock;
723 memset(tmpdata, 0, lio);
724 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
725 else lseek(mkfd, rdpos + lio, SEEK_SET);
727 default: xerror(NO, NO, NO, "%s", mackeyf); break;
730 if (lio && lio < lrem) {
736 skein_update(&sk, tmpdata, ldone);
739 skein_final(mackey, &sk);
743 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
744 memset(&getps, 0, sizeof(struct getpasswd_state));
745 getps.fd = getps.efd = -1;
746 getps.passwd = pwdask;
747 getps.pwlen = sizeof(pwdask)-1;
748 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
749 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
750 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
751 getps.flags = GETP_WAITFILL;
752 n = xgetpasswd(&getps);
753 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
754 if (n == ((size_t)-2)) xexit(1);
755 if (verbose) say_hint(pwdask, n, "MAC password hint");
756 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
760 if ((strlen(progname) <= 9)
761 && ((!strcmp(progname, "sksum"))
762 || ((!memcmp(progname, "sk", 2))
763 && (!memcmp(progname+3, "sum", 3)
764 || !memcmp(progname+4, "sum", 3)
765 || !memcmp(progname+5, "sum", 3)
766 || !memcmp(progname+6, "sum", 3)))))
767 do_sksum(progname, argv+optind);
768 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
773 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
774 if (!strcmp(argv[idx], "-")) kfd = 0;
775 else kfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
777 lio = strnlen(argv[idx], PATH_MAX);
778 memset(argv[idx], '*', lio);
785 if (do_full_key == NO && tweakf) {
788 if (!strcmp(tweakf, "-")) twfd = 0;
789 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
790 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
791 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
792 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
793 if (ldone < TF_TWEAK_SIZE)
794 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
801 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
803 sfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
804 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
805 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
808 if ((do_mac >= TFC_MAC_VRFY || do_mac <= TFC_MAC_DROP) && !do_mac_file) {
809 maxlen = tfc_fdsize(sfd);
810 if (maxlen == NOFSIZE)
812 "Cannot verify embedded MAC with non-seekable source!");
813 maxlen -= TF_FROM_BITS(macbits);
815 srcfname = argv[idx];
819 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
820 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
822 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
823 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
825 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
830 if (!strcmp(counter_file, "-")) ctrfd = 0;
831 else ctrfd = xopen(counter_file, O_RDONLY | O_LARGEFILE);
832 lio = xread(ctrfd, ctr, ctrsz);
833 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
834 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
837 else if (counter_opt == TFC_CTR_HEAD) {
840 lrem = lblock = ctrsz;
841 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
842 _ctrragain: lio = xread(sfd, pblk, lrem);
843 if (lio != NOSIZE) ldone += lio;
845 if (errno != EIO && catch_all_errors != YES)
846 xerror(NO, NO, NO, "%s", srcfname);
847 switch (error_action) {
848 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
849 case TFC_ERRACT_SYNC:
850 case TFC_ERRACT_LSYNC:
851 xerror(YES, NO, NO, "%s", srcfname);
852 lio = ldone = lrem = lblock;
854 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
855 else lseek(sfd, rdpos + lio, SEEK_SET);
857 default: xerror(NO, NO, NO, "%s", srcfname); break;
860 if (lio && lio < lrem) {
865 total_processed_src += ldone;
870 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
872 if (lseek(sfd, iseek, SEEK_SET) == -1)
873 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
876 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
878 if (verbose) tfc_esay("%s: hashing password", tfc_format_pid(progname));
880 if (rawkey == TFC_RAWKEY_KEYFILE) {
881 tfc_yesno xtskeyset = NO;
885 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
886 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
887 _keyragain: lio = xread(kfd, pblk, lrem);
888 if (lio != NOSIZE) ldone += lio;
890 if (errno != EIO && catch_all_errors != YES)
891 xerror(NO, NO, NO, "reading key");
892 switch (error_action) {
893 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
894 case TFC_ERRACT_SYNC:
895 case TFC_ERRACT_LSYNC:
896 xerror(YES, NO, NO, "reading key");
897 lio = ldone = lrem = lblock;
899 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
900 else lseek(kfd, rdpos + lio, SEEK_SET);
902 default: xerror(NO, NO, NO, "reading key"); break;
905 if (lio && lio < lrem) {
910 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
912 if (ctr_mode == TFC_MODE_XTS) {
913 if (xtskeyset == NO) {
920 else if (rawkey == TFC_RAWKEY_ASKSTR) {
921 tfc_yesno xtskeyset = NO;
923 pblk = key; n = sizeof(key);
924 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
925 getps.fd = getps.efd = -1;
926 getps.passwd = (char *)pblk;
928 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
929 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
930 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
931 getps.flags = GETP_WAITFILL;
932 n = xgetpasswd(&getps);
933 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
934 if (n == ((size_t)-2)) xexit(1);
935 if (verbose) say_hint(pblk, n, "Raw string key hint");
936 if (ctr_mode == TFC_MODE_XTS) {
937 if (xtskeyset == NO) {
938 pblk = xtskey; n = sizeof(xtskey);
944 else if (rawkey == TFC_RAWKEY_ASKHEX) {
945 tfc_yesno xtskeyset = NO;
949 memset(&getps, 0, sizeof(struct getpasswd_state));
950 getps.fd = getps.efd = -1;
951 getps.passwd = pwdask;
952 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
953 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
954 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
955 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
956 getps.flags = GETP_WAITFILL;
957 n = xgetpasswd(&getps);
958 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
959 if (n == ((size_t)-2)) xexit(1);
961 tfc_esay("Please input even number of hex digits!");
962 goto _rawkey_hex_again;
964 hex2bin(pblk, pwdask);
965 memset(pwdask, 0, sizeof(pwdask));
966 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
967 if (ctr_mode == TFC_MODE_XTS) {
968 if (xtskeyset == NO) {
971 goto _rawkey_hex_again;
976 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
977 getps.fd = getps.efd = -1;
978 getps.passwd = pwdask;
979 getps.pwlen = sizeof(pwdask)-1;
980 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
981 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
982 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
983 getps.flags = GETP_WAITFILL;
984 n = xgetpasswd(&getps);
985 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
986 if (n == ((size_t)-2)) xexit(1);
987 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
988 getps.fd = getps.efd = -1;
989 getps.passwd = pwdagain;
990 getps.pwlen = sizeof(pwdagain)-1;
991 getps.echo = "Enter it again: ";
992 getps.flags = GETP_WAITFILL;
993 n = xgetpasswd(&getps);
994 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
995 if (n == ((size_t)-2)) xexit(1);
996 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
997 tfc_esay("Passwords are different, try again");
1001 if (verbose) say_hint(pwdask, n, "Password hint");
1002 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
1003 memset(pwdask, 0, sizeof(pwdask));
1004 memset(pwdagain, 0, sizeof(pwdagain));
1007 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
1008 xerror(NO, NO, YES, "hashing key");
1012 if (tfc_saltsz > 0) {
1013 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
1014 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
1016 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
1017 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1018 memset(tfc_salt, 0, TFC_MAX_SALT);
1021 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
1022 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1027 tfc_yesno xtskeyset = NO;
1030 if (!strcmp(genkeyf, "-")) krfd = 1;
1031 else krfd = xopen(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1032 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
1033 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
1034 if (verbose && xtskeyset == NO) {
1035 tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1036 tfc_esay("%s: rawkey written to %s.", tfc_format_pid(progname), genkeyf);
1037 tfc_esay("%s: Have a nice day!", tfc_format_pid(progname));
1040 if (ctr_mode == TFC_MODE_XTS) {
1041 if (xtskeyset == NO) {
1054 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1055 if (ctr_mode < TFC_MODE_OCB) {
1056 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1057 "will be slower.", tfc_format_pid(progname));
1058 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1059 else skein_init(&sk, macbits);
1063 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1064 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1068 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1069 if (do_full_key == NO) {
1070 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1071 tf_tweak_set(key, tweak);
1073 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1075 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1077 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1078 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1079 if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1081 if (counter_opt == TFC_CTR_SHOW) {
1082 switch (do_outfmt) {
1083 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1084 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1085 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1088 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1095 if (verbose) tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1097 if (overwrite_source && srcfname) argv[idx] = srcfname;
1101 if (!strcmp(argv[idx], "-")) dfd = 1;
1102 else dfd = xxopen(YES, argv[idx], O_RDWR | O_LARGEFILE | write_flags);
1103 if (dfd == -1) dfd = xopen(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1104 dstfname = argv[idx];
1109 if (lseek(dfd, oseek, SEEK_SET) == -1)
1110 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1113 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1114 memset(&sigact, 0, sizeof(sigact));
1115 sigact.sa_flags = SA_RESTART;
1116 sigact.sa_handler = print_crypt_status;
1117 sigaction(SIGUSR1, &sigact, NULL);
1118 sigaction(SIGALRM, &sigact, NULL);
1119 if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
1120 sigact.sa_handler = change_status_width;
1121 sigaction(SIGQUIT, &sigact, NULL);
1122 sigact.sa_handler = change_status_timer;
1123 sigaction(SIGUSR2, &sigact, NULL);
1125 sigact.sa_handler = print_crypt_status;
1126 sigaction(SIGINT, &sigact, NULL);
1127 sigaction(SIGTERM, &sigact, NULL);
1128 sigaction(SIGTSTP, &sigact, NULL);
1131 sigact.sa_handler = exit_sigterm;
1132 sigaction(SIGINT, &sigact, NULL);
1133 sigaction(SIGTERM, &sigact, NULL);
1134 sigact.sa_handler = handle_sigtstp;
1135 sigaction(SIGTSTP, &sigact, NULL);
1137 memset(&sigact, 0, sizeof(struct sigaction));
1139 tfc_getcurtime(&delta_time);
1142 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1146 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1147 if (lio != NOSIZE) ldone += lio;
1148 else xerror(NO, NO, NO, "%s", dstfname);
1149 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1155 total_written_dst += ldone;
1156 total_processed_dst += ldone;
1157 delta_processed += ldone;
1160 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1162 if (do_mac == TFC_MAC_JUST_VRFY2) {
1163 rwd = tfc_fdgetpos(sfd);
1164 if (rwd == NOFSIZE) {
1165 tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", tfc_format_pid(progname));
1166 do_mac = TFC_MAC_VRFY;
1168 goto _nodecrypt_again_vrfy2;
1170 _decrypt_again_vrfy2:
1171 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1172 xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1174 total_processed_src = rwd;
1175 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1176 memset(svctr, 0, TF_BLOCK_SIZE);
1179 _nodecrypt_again_vrfy2:
1187 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1188 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1189 _ragain: lio = xread(sfd, pblk, lrem);
1191 if ((do_read_loops != 0 && sfd != 0) && (loopcnt < do_read_loops)) {
1192 lseek(sfd, 0L, SEEK_SET);
1199 if (lio != NOSIZE) ldone += lio;
1201 if (errno != EIO && catch_all_errors != YES)
1202 xerror(NO, NO, NO, "%s", srcfname);
1203 switch (error_action) {
1204 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1205 case TFC_ERRACT_SYNC:
1206 case TFC_ERRACT_LSYNC:
1207 xerror(YES, NO, NO, "%s", srcfname);
1208 lio = ldone = lrem = lblock;
1209 memset(srcblk, 0, lio);
1210 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1211 else lseek(sfd, rdpos + lio, SEEK_SET);
1213 default: xerror(NO, NO, NO, "%s", srcfname); break;
1216 if (lio && lio < lrem) {
1221 total_processed_src += ldone;
1223 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1224 size_t orig = ldone;
1225 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1226 if (ldone > blksize) ldone = blksize;
1227 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1230 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1231 skein_update(&sk, srcblk, ldone);
1233 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1234 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1235 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1236 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1237 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1238 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1239 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1240 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1241 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1242 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1243 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1244 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1245 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1246 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1248 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1249 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1250 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1251 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1253 else if (ctr_mode == TFC_MODE_PLAIN)
1254 memcpy(dstblk, srcblk, ldone);
1256 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1257 skein_update(&sk, dstblk, ldone);
1258 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1263 _wagain: lio = xwrite(dfd, pblk, lrem);
1264 if (lio != NOSIZE) ldone += lio;
1265 else xerror(NO, NO, NO, "%s", dstfname);
1266 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1272 total_written_dst += ldone;
1273 _nowrite: total_processed_dst += ldone;
1274 delta_processed += ldone;
1276 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1282 if (verbose && status_timer && do_statline_dynamic == YES && statline_was_shown == YES) tfc_esay("\n");
1285 if (do_mac >= TFC_MAC_VRFY) {
1289 lrem = lblock = TF_FROM_BITS(macbits);
1290 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1291 _macragain: lio = xread(sfd, pblk, lrem);
1292 if (lio != NOSIZE) ldone += lio;
1294 if (errno != EIO && catch_all_errors != YES)
1295 xerror(NO, NO, NO, "%s", srcfname);
1296 switch (error_action) {
1297 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1298 case TFC_ERRACT_SYNC:
1299 case TFC_ERRACT_LSYNC:
1300 xerror(YES, NO, NO, "%s", srcfname);
1301 lio = ldone = lrem = lblock;
1302 memset(macvrfy, 0, lio);
1303 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1304 else lseek(sfd, rdpos + lio, SEEK_SET);
1306 default: xerror(NO, NO, NO, "%s", srcfname); break;
1309 if (lio && lio < lrem) {
1314 total_processed_src += ldone;
1319 if (!strcmp(do_mac_file, "-")) mfd = 0;
1320 else mfd = xopen(do_mac_file, O_RDONLY | O_LARGEFILE);
1321 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1322 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1323 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1324 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1325 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1326 lio = TF_FROM_BITS(macbits);
1327 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1329 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1333 if (ldone < TF_FROM_BITS(macbits)) {
1334 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1335 "not verifying", tfc_format_pid(progname), ldone);
1340 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1341 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1343 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1344 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1345 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1346 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1347 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1348 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1350 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1352 tfc_esay("%s: signature is good", tfc_format_pid(progname));
1354 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1355 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1358 if (do_mac == TFC_MAC_JUST_VRFY2) {
1359 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", tfc_format_pid(progname));
1360 maxlen = total_processed_src - SKEIN_DIGEST_SIZE;
1361 do_mac = TFC_MAC_DROP2;
1362 goto _decrypt_again_vrfy2;
1367 tfc_esay("%s: signature is BAD: "
1368 "wrong password, key, mode, or file is not signed", tfc_format_pid(progname));
1369 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", tfc_format_pid(progname));
1374 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1375 memset(macresult, 0, sizeof(macresult));
1376 memset(tmpdata, 0, sizeof(tmpdata));
1378 else if (do_mac == TFC_MAC_SIGN) {
1379 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1380 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1382 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1383 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1384 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1385 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1386 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1387 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1388 memset(macresult, 0, sizeof(macresult));
1392 lio = lrem = TF_FROM_BITS(macbits);
1394 _macwagain: lio = xwrite(dfd, pblk, lrem);
1395 if (lio != NOSIZE) ldone += lio;
1396 else xerror(NO, NO, NO, "%s", dstfname);
1397 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1403 total_written_dst += ldone;
1404 total_processed_dst += ldone;
1405 delta_processed += ldone;
1410 if (!strcmp(do_mac_file, "-")) mfd = 1;
1411 else mfd = xopen(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1412 if (do_outfmt == TFC_OUTFMT_B64) {
1413 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1414 memset(tmpdata, 0, TFC_TMPSIZE);
1415 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1416 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1417 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1419 tmpdata[lrem] = '\n';
1422 lio = xwrite(mfd, tmpdata, lrem);
1424 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1425 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1426 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1430 memset(macvrfy, 0, sizeof(macvrfy));
1431 memset(macresult, 0, sizeof(macresult));
1432 memset(tmpdata, 0, sizeof(tmpdata));
1434 else if (do_mac == TFC_MAC_DROP2) total_processed_src += SKEIN_DIGEST_SIZE;
1436 if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(0);