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, "pcbc"))
213 ctr_mode = TFC_MODE_PCBC;
214 else if (!strcasecmp(optarg, "ecb"))
215 ctr_mode = TFC_MODE_ECB;
216 else if (!strcasecmp(optarg, "xts"))
217 ctr_mode = TFC_MODE_XTS;
218 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
221 do_edcrypt = TFC_DO_PLAIN;
223 ctr_mode = TFC_MODE_PLAIN;
226 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
229 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
232 macbits = strtoul(optarg, &stoi, 10);
233 if (macbits == 0 || !str_empty(stoi) || macbits < 8
234 || macbits > TF_MAX_BITS || macbits % 8)
235 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
238 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
239 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
242 if (!strcasecmp(optarg, "key"))
243 mackey_opt = TFC_MACKEY_RAWKEY;
244 else if (!strcasecmp(optarg, "pwd"))
245 mackey_opt = TFC_MACKEY_PASSWORD;
247 mackey_opt = TFC_MACKEY_FILE;
255 rawkey = TFC_RAWKEY_KEYFILE;
258 rawkey = TFC_RAWKEY_ASKSTR;
261 rawkey = TFC_RAWKEY_ASKHEX;
272 if (maxlen != NOFSIZE) break;
274 maxlen = tfc_humanfsize(optarg, &stoi);
275 if (!str_empty(stoi)) {
276 maxlen = tfc_fnamesize(optarg, YES);
277 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
278 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
279 "%s: invalid count value", optarg);
281 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
282 if (counter_opt == TFC_CTR_HEAD)
283 maxlen += TF_BLOCK_SIZE;
286 overwrite_source = YES;
289 if (!strcmp(optarg, "xall")) {
290 catch_all_errors = YES;
293 if (!strcmp(optarg, "xseek")) {
294 ignore_seek_errors = YES;
297 if (!strcmp(optarg, "exit"))
298 error_action = TFC_ERRACT_EXIT;
299 else if (!strncmp(optarg, "cont", 4))
300 error_action = TFC_ERRACT_CONT;
301 else if (!strcmp(optarg, "sync"))
302 error_action = TFC_ERRACT_SYNC;
303 else if (!strcmp(optarg, "lsync"))
304 error_action = TFC_ERRACT_LSYNC;
305 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
311 s = d = optarg; t = NULL;
312 while ((s = strtok_r(d, ",", &t))) {
314 if (!strcmp(s, "sync"))
315 write_flags |= O_SYNC;
316 else if (!strcmp(s, "trunc"))
317 write_flags |= O_TRUNC;
318 else if (!strcmp(s, "append"))
319 write_flags |= O_APPEND;
320 else if (!strcmp(s, "fsync"))
322 else if (!strcmp(s, "pad"))
324 else if (!strcmp(s, "ro"))
326 else if (!strcmp(s, "rw"))
328 else if (!strcmp(s, "xtime"))
329 do_preserve_time = YES;
330 else if (!strcmp(s, "gibsize"))
331 do_stats_in_gibs = YES;
332 else if (!strcmp(s, "plainstats"))
333 do_statline_dynamic = NO;
334 else if (!strcmp(s, "statless"))
336 else if (!strcmp(s, "norepeat"))
338 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
340 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
341 mac_pw_prompt = s+10;
342 else if (!strcmp(s, "shorthex"))
343 do_full_hexdump = NO;
344 else if (!strcmp(s, "fullkey"))
346 else if (!strcmp(s, "showsecrets"))
348 else if (!strcmp(s, "finished"))
349 show_when_done = YES;
350 else if (!strcmp(s, "pid"))
352 else if (!strcmp(s, "nobuf")) {
353 if (!tfc_is_freestream(ctr_mode)) xerror(NO, YES, YES,
354 "cannot activate unbuffered mode for non-stream cipher mode %s!",
355 tfc_modename(ctr_mode));
356 else unbuffered = YES;
358 else if (!strncmp(s, "readloops", 9) && *(s+9) == '=') {
359 do_read_loops = tfc_humanfsize(s+10, &stoi);
360 if (!str_empty(stoi)) do_read_loops = NOSIZE;
362 else if (!strncmp(s, "logfile", 7) && *(s+7) == '=')
364 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
366 blksize = (size_t)tfc_humanfsize(s, &stoi);
367 if (!str_empty(stoi)) {
368 blksize = (size_t)tfc_fnamesize(s, YES);
369 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
370 if (blksize == NOSIZE) xerror(NO, YES, YES,
371 "%s: invalid block size value", s);
373 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
374 if (!tfc_is_freestream(ctr_mode) && blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
375 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
376 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
377 "%s: block size exceeds %u bytes",
378 s, TFC_U(TFC_BLKSIZE));
380 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
382 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
383 if (!str_empty(stoi)) {
384 xtsblocks = (size_t)tfc_fnamesize(s, YES);
385 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
386 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
387 "%s: invalid blocks per xts block value", s);
389 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
390 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
391 "%s: nr of blocks per xts block is not round to %u bytes",
392 s, TFC_U(TFC_BLKSIZE));
393 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
394 "%s: nr of blocks per xts block exceeds %u bytes",
395 s, TFC_U(TFC_BLKSIZE));
397 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
399 iseek = tfc_humanfsize(s, &stoi);
400 if (!str_empty(stoi)) {
401 iseek = tfc_fnamesize(s, YES);
402 iseek = tfc_modifysize(iseek, strchr(s, ':'));
403 if (iseek == NOFSIZE) xerror(NO, YES, YES,
404 "%s: invalid iseek value", s);
406 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
407 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
409 "%s: not round to TF block size "
411 s, TFC_U(TF_BLOCK_SIZE));
412 iseek_blocks = iseek / TF_BLOCK_SIZE;
414 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
416 iseek = tfc_humanfsize(s, &stoi);
417 if (!str_empty(stoi)) {
418 iseek = tfc_fnamesize(s, YES);
419 iseek = tfc_modifysize(iseek, strchr(s, ':'));
420 if (iseek == NOFSIZE) xerror(NO, YES, YES,
421 "%s: invalid ixseek value", s);
423 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
425 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
427 iseek_blocks = tfc_humanfsize(s, &stoi);
428 if (!str_empty(stoi)) {
429 iseek_blocks = tfc_fnamesize(s, YES);
430 if (iseek_blocks == NOFSIZE)
432 "%s: invalid ictr value", s);
433 iseek_blocks /= TF_BLOCK_SIZE;
434 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
436 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
438 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
440 iseek_blocks = tfc_humanfsize(s, &stoi);
441 if (!str_empty(stoi)) {
442 iseek_blocks = tfc_fnamesize(s, YES);
443 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
444 if (iseek_blocks == NOFSIZE)
446 "%s: invalid ixctr value", s);
448 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
449 if (iseek_blocks % TF_BLOCK_SIZE)
451 "%s: not round to TF block size "
452 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
453 iseek_blocks /= TF_BLOCK_SIZE;
455 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
457 oseek = tfc_humanfsize(s, &stoi);
458 if (!str_empty(stoi)) {
459 oseek = tfc_fnamesize(s, YES);
460 oseek = tfc_modifysize(oseek, strchr(s, ':'));
461 if (oseek == NOFSIZE) xerror(NO, YES, YES,
462 "%s: invalid oseek value", s);
464 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
466 else if (!strncmp(s, "ioseek", 6) && *(s+6) == '=') {
469 iseek = tfc_humanfsize(s, &stoi);
470 if (!str_empty(stoi)) {
471 iseek = tfc_fnamesize(s, YES);
472 iseek = tfc_modifysize(iseek, strchr(s, ':'));
473 if (iseek == NOFSIZE) xerror(NO, YES, YES,
474 "%s: invalid iseek value", s);
476 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
477 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
479 "%s: not round to TF block size "
481 s, TFC_U(TF_BLOCK_SIZE));
482 iseek_blocks = iseek / TF_BLOCK_SIZE;
484 oseek = tfc_humanfsize(s, &stoi);
485 if (!str_empty(stoi)) {
486 oseek = tfc_fnamesize(s, YES);
487 oseek = tfc_modifysize(oseek, strchr(s, ':'));
488 if (oseek == NOFSIZE) xerror(NO, YES, YES,
489 "%s: invalid oseek value", s);
491 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
493 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
495 maxlen = tfc_humanfsize(s, &stoi);
496 if (!str_empty(stoi)) {
497 maxlen = tfc_fnamesize(s, YES);
498 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
499 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
500 "%s: invalid count value", s);
502 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
503 if (counter_opt == TFC_CTR_HEAD)
504 maxlen += TF_BLOCK_SIZE;
506 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
508 if (!strcmp(s, "tail")) {
509 do_ftrunc = TFC_FTRUNC_TAIL;
510 ftrunc_dfd = NOFSIZE;
513 do_ftrunc = TFC_DO_FTRUNC;
514 ftrunc_dfd = tfc_humanfsize(s, &stoi);
515 if (!str_empty(stoi)) {
516 ftrunc_dfd = tfc_fnamesize(s, YES);
517 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
518 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
519 "%s: invalid ftrunc value", s);
521 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
524 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
526 maxkeylen = tfc_humanfsize(s, &stoi);
527 if (!str_empty(stoi)) {
528 maxkeylen = tfc_fnamesize(s, YES);
529 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
530 if (maxkeylen == NOSIZE)
532 "%s: invalid key length value", s);
534 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
536 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
538 keyoffset = tfc_humanfsize(s, &stoi);
539 if (!str_empty(stoi)) {
540 keyoffset = tfc_fnamesize(s, YES);
541 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
542 if (keyoffset == NOFSIZE)
544 "%s: invalid key offset value", s);
546 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
548 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
550 ctrsz = tfc_humanfsize(s, &stoi);
551 if (!str_empty(stoi)) {
552 ctrsz = (size_t)tfc_fnamesize(s, YES);
553 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
556 "%s: invalid counter length value", s);
558 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
559 if (ctrsz > TF_BLOCK_SIZE)
560 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
562 else xerror(NO, YES, YES, "invalid option %s", s);
566 do_mac = TFC_MAC_SIGN;
567 if (strcasecmp(optarg, "mac") != 0)
568 do_mac_file = optarg;
571 do_mac = TFC_MAC_VRFY;
572 if (!strcasecmp(optarg, "drop"))
573 do_mac = TFC_MAC_DROP;
574 else if (strcasecmp(optarg, "mac") != 0)
575 do_mac_file = optarg;
579 if (do_mac != TFC_MAC_VRFY)
580 xerror(NO, YES, YES, "signature source was not specified");
581 do_mac = TFC_MAC_JUST_VRFY;
582 if (c == 'u') do_mac = TFC_MAC_JUST_VRFY2;
586 if (maxlen != NOFSIZE) {
587 if (c == 'Z') genzero_nr_bytes = maxlen;
588 else genrandom_nr_bytes = maxlen;
592 if (!strcasecmp(optarg, "cbs"))
594 else if (!strcasecmp(optarg, "ks"))
595 t = TF_FROM_BITS(TFC_KEY_BITS);
596 else if (!strcasecmp(optarg, "xks"))
597 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
598 else if (!strcasecmp(optarg, "iobs"))
601 t = tfc_humanfsize(optarg, &stoi);
602 if (!str_empty(stoi)) {
603 t = tfc_fnamesize(optarg, NO);
604 t = tfc_modifysize(t, strchr(optarg, ':'));
606 else t = tfc_modifysize(t, strchr(optarg, ':'));
608 if (c == 'Z') genzero_nr_bytes = maxlen = t;
609 else genrandom_nr_bytes = maxlen = t;
613 do_preserve_time = YES;
616 do_outfmt = TFC_OUTFMT_B64;
619 do_outfmt = TFC_OUTFMT_RAW;
622 do_outfmt = TFC_OUTFMT_HEX;
628 do_full_hexdump = NO;
635 td = strtod(optarg, &stoi);
636 status_timer = TFC_DTOUSECS(td);
637 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
645 if (!strcmp(progname, "tfbench")) {
646 if (!*(argv+optind)) usage();
648 td = strtod(*(argv+optind), &stoi);
649 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
651 "%s: invalid benchmark time in seconds", *(argv+optind));
652 bench_timer = TFC_DTOUSECS(td);
653 do_benchmark(bench_timer, td);
655 if (genrandom_nr_bytes) {
656 ctr_mode = TFC_MODE_STREAM;
657 do_edcrypt = TFC_DO_ENCRYPT;
658 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
660 if (genzero_nr_bytes) {
661 ctr_mode = TFC_MODE_PLAIN;
662 do_edcrypt = TFC_DO_PLAIN;
663 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
666 if (rawkey && password)
667 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
668 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
669 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
670 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
671 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
672 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
673 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
674 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
675 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
676 if (overwrite_source && counter_opt == TFC_CTR_RAND)
677 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
678 if (ctr_mode == TFC_MODE_PLAIN
679 && (do_edcrypt || do_mac || rawkey
680 || mackey_opt || counter_opt || counter_file))
681 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
689 memset(tfc_salt, 0, TFC_MAX_SALT);
691 if (!strcasecmp(saltf, "disable")) goto _nosalt;
693 if (!strcmp(saltf, "-")) saltfd = 0;
694 else saltfd = xopen(saltf, O_RDONLY | O_LARGEFILE);
695 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
696 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
702 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
706 if (!strcmp(mackeyf, "-")) mkfd = 0;
707 else mkfd = xopen(mackeyf, O_RDONLY | O_LARGEFILE);
709 skein_init(&sk, TFC_KEY_BITS);
716 lrem = lblock = sizeof(tmpdata);
717 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
718 _mkragain: lio = xread(mkfd, pblk, lrem);
719 if (lio == 0 && do_stop == NO) do_stop = YES;
720 if (lio != NOSIZE) ldone += lio;
722 if (errno != EIO && catch_all_errors != YES)
723 xerror(NO, NO, NO, "%s", mackeyf);
724 switch (error_action) {
725 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
726 case TFC_ERRACT_SYNC:
727 case TFC_ERRACT_LSYNC:
728 xerror(YES, NO, NO, "%s", mackeyf);
729 lio = ldone = lrem = lblock;
730 memset(tmpdata, 0, lio);
731 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
732 else lseek(mkfd, rdpos + lio, SEEK_SET);
734 default: xerror(NO, NO, NO, "%s", mackeyf); break;
737 if (lio && lio < lrem) {
743 skein_update(&sk, tmpdata, ldone);
746 skein_final(mackey, &sk);
750 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
751 memset(&getps, 0, sizeof(struct getpasswd_state));
752 getps.fd = getps.efd = -1;
753 getps.passwd = pwdask;
754 getps.pwlen = sizeof(pwdask)-1;
755 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
756 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
757 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
758 getps.flags = GETP_WAITFILL;
759 n = xgetpasswd(&getps);
760 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
761 if (n == ((size_t)-2)) xexit(1);
762 if (verbose) say_hint(pwdask, n, "MAC password hint");
763 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
767 if ((strlen(progname) <= 9)
768 && ((!strcmp(progname, "sksum"))
769 || ((!memcmp(progname, "sk", 2))
770 && (!memcmp(progname+3, "sum", 3)
771 || !memcmp(progname+4, "sum", 3)
772 || !memcmp(progname+5, "sum", 3)
773 || !memcmp(progname+6, "sum", 3)))))
774 do_sksum(progname, argv+optind);
775 if (!strcmp(progname, "base64")) do_edbase64(argv+optind);
780 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
781 if (!strcmp(argv[idx], "-")) kfd = 0;
782 else kfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
784 lio = strnlen(argv[idx], PATH_MAX);
785 memset(argv[idx], '*', lio);
792 if (do_full_key == NO && tweakf) {
795 if (!strcmp(tweakf, "-")) twfd = 0;
796 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
797 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
798 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
799 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
800 if (ldone < TF_TWEAK_SIZE)
801 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
808 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
810 sfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
811 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
812 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
815 if ((do_mac >= TFC_MAC_VRFY || do_mac <= TFC_MAC_DROP) && !do_mac_file) {
816 maxlen = tfc_fdsize(sfd);
817 if (maxlen == NOFSIZE)
819 "Cannot verify embedded MAC with non-seekable source!");
820 maxlen -= TF_FROM_BITS(macbits);
822 srcfname = argv[idx];
826 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
827 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
829 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
830 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
832 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
837 if (!strcmp(counter_file, "-")) ctrfd = 0;
838 else ctrfd = xopen(counter_file, O_RDONLY | O_LARGEFILE);
839 lio = xread(ctrfd, ctr, ctrsz);
840 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
841 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
844 else if (counter_opt == TFC_CTR_HEAD) {
847 lrem = lblock = ctrsz;
848 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
849 _ctrragain: lio = xread(sfd, pblk, lrem);
850 if (lio != NOSIZE) ldone += lio;
852 if (errno != EIO && catch_all_errors != YES)
853 xerror(NO, NO, NO, "%s", srcfname);
854 switch (error_action) {
855 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
856 case TFC_ERRACT_SYNC:
857 case TFC_ERRACT_LSYNC:
858 xerror(YES, NO, NO, "%s", srcfname);
859 lio = ldone = lrem = lblock;
861 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
862 else lseek(sfd, rdpos + lio, SEEK_SET);
864 default: xerror(NO, NO, NO, "%s", srcfname); break;
867 if (lio && lio < lrem) {
872 total_processed_src += ldone;
877 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
879 if (lseek(sfd, iseek, SEEK_SET) == -1)
880 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
883 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
885 if (verbose) tfc_esay("%s: hashing password", tfc_format_pid(progname));
887 if (rawkey == TFC_RAWKEY_KEYFILE) {
888 tfc_yesno xtskeyset = NO;
892 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
893 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
894 _keyragain: lio = xread(kfd, pblk, lrem);
895 if (lio != NOSIZE) ldone += lio;
897 if (errno != EIO && catch_all_errors != YES)
898 xerror(NO, NO, NO, "reading key");
899 switch (error_action) {
900 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
901 case TFC_ERRACT_SYNC:
902 case TFC_ERRACT_LSYNC:
903 xerror(YES, NO, NO, "reading key");
904 lio = ldone = lrem = lblock;
906 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
907 else lseek(kfd, rdpos + lio, SEEK_SET);
909 default: xerror(NO, NO, NO, "reading key"); break;
912 if (lio && lio < lrem) {
917 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
919 if (ctr_mode == TFC_MODE_XTS) {
920 if (xtskeyset == NO) {
927 else if (rawkey == TFC_RAWKEY_ASKSTR) {
928 tfc_yesno xtskeyset = NO;
930 pblk = key; n = sizeof(key);
931 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
932 getps.fd = getps.efd = -1;
933 getps.passwd = (char *)pblk;
935 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
936 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
937 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
938 getps.flags = GETP_WAITFILL;
939 n = xgetpasswd(&getps);
940 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
941 if (n == ((size_t)-2)) xexit(1);
942 if (verbose) say_hint(pblk, n, "Raw string key hint");
943 if (ctr_mode == TFC_MODE_XTS) {
944 if (xtskeyset == NO) {
945 pblk = xtskey; n = sizeof(xtskey);
951 else if (rawkey == TFC_RAWKEY_ASKHEX) {
952 tfc_yesno xtskeyset = NO;
956 memset(&getps, 0, sizeof(struct getpasswd_state));
957 getps.fd = getps.efd = -1;
958 getps.passwd = pwdask;
959 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
960 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
961 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
962 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
963 getps.flags = GETP_WAITFILL;
964 n = xgetpasswd(&getps);
965 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
966 if (n == ((size_t)-2)) xexit(1);
968 tfc_esay("Please input even number of hex digits!");
969 goto _rawkey_hex_again;
971 hex2bin(pblk, pwdask);
972 memset(pwdask, 0, sizeof(pwdask));
973 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
974 if (ctr_mode == TFC_MODE_XTS) {
975 if (xtskeyset == NO) {
978 goto _rawkey_hex_again;
983 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
984 getps.fd = getps.efd = -1;
985 getps.passwd = pwdask;
986 getps.pwlen = sizeof(pwdask)-1;
987 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
988 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
989 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
990 getps.flags = GETP_WAITFILL;
991 n = xgetpasswd(&getps);
992 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
993 if (n == ((size_t)-2)) xexit(1);
994 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
995 getps.fd = getps.efd = -1;
996 getps.passwd = pwdagain;
997 getps.pwlen = sizeof(pwdagain)-1;
998 getps.echo = "Enter it again: ";
999 getps.flags = GETP_WAITFILL;
1000 n = xgetpasswd(&getps);
1001 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
1002 if (n == ((size_t)-2)) xexit(1);
1003 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
1004 tfc_esay("Passwords are different, try again");
1008 if (verbose) say_hint(pwdask, n, "Password hint");
1009 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
1010 memset(pwdask, 0, sizeof(pwdask));
1011 memset(pwdagain, 0, sizeof(pwdagain));
1014 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
1015 xerror(NO, NO, YES, "hashing key");
1019 if (tfc_saltsz > 0) {
1020 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
1021 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
1023 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
1024 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1025 memset(tfc_salt, 0, TFC_MAX_SALT);
1028 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
1029 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1034 tfc_yesno xtskeyset = NO;
1037 if (!strcmp(genkeyf, "-")) krfd = 1;
1038 else krfd = xopen(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1039 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
1040 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
1041 if (verbose && xtskeyset == NO) {
1042 tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1043 tfc_esay("%s: rawkey written to %s.", tfc_format_pid(progname), genkeyf);
1044 tfc_esay("%s: Have a nice day!", tfc_format_pid(progname));
1047 if (ctr_mode == TFC_MODE_XTS) {
1048 if (xtskeyset == NO) {
1061 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1062 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1063 "will be slower.", tfc_format_pid(progname));
1064 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1065 else skein_init(&sk, macbits);
1068 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1069 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1073 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1074 if (do_full_key == NO) {
1075 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1076 tf_tweak_set(key, tweak);
1078 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1080 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1082 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1083 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1084 if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1086 if (counter_opt == TFC_CTR_SHOW) {
1087 switch (do_outfmt) {
1088 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1089 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1090 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1093 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1100 if (verbose) tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1102 if (overwrite_source && srcfname) argv[idx] = srcfname;
1106 if (!strcmp(argv[idx], "-")) dfd = 1;
1107 else dfd = xxopen(YES, argv[idx], O_RDWR | O_LARGEFILE | write_flags);
1108 if (dfd == -1) dfd = xopen(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1109 dstfname = argv[idx];
1114 if (lseek(dfd, oseek, SEEK_SET) == -1)
1115 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1118 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1119 memset(&sigact, 0, sizeof(sigact));
1120 sigact.sa_flags = SA_RESTART;
1121 sigact.sa_handler = print_crypt_status;
1122 sigaction(SIGUSR1, &sigact, NULL);
1123 sigaction(SIGALRM, &sigact, NULL);
1124 if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
1125 sigact.sa_handler = change_status_width;
1126 sigaction(SIGQUIT, &sigact, NULL);
1127 sigact.sa_handler = change_status_timer;
1128 sigaction(SIGUSR2, &sigact, NULL);
1130 sigact.sa_handler = print_crypt_status;
1131 sigaction(SIGINT, &sigact, NULL);
1132 sigaction(SIGTERM, &sigact, NULL);
1133 sigaction(SIGTSTP, &sigact, NULL);
1136 sigact.sa_handler = exit_sigterm;
1137 sigaction(SIGINT, &sigact, NULL);
1138 sigaction(SIGTERM, &sigact, NULL);
1139 sigact.sa_handler = handle_sigtstp;
1140 sigaction(SIGTSTP, &sigact, NULL);
1142 memset(&sigact, 0, sizeof(struct sigaction));
1144 tfc_getcurtime(&delta_time);
1147 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1151 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1152 if (lio != NOSIZE) ldone += lio;
1153 else xerror(NO, NO, NO, "%s", dstfname);
1154 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1160 total_written_dst += ldone;
1161 total_processed_dst += ldone;
1162 delta_processed += ldone;
1165 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1167 if (do_mac == TFC_MAC_JUST_VRFY2) {
1168 rwd = tfc_fdgetpos(sfd);
1169 if (rwd == NOFSIZE) {
1170 tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", tfc_format_pid(progname));
1171 do_mac = TFC_MAC_VRFY;
1173 goto _nodecrypt_again_vrfy2;
1175 _decrypt_again_vrfy2:
1176 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1177 xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1179 total_processed_src = rwd;
1180 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1181 memset(svctr, 0, TF_BLOCK_SIZE);
1184 _nodecrypt_again_vrfy2:
1192 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1193 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1194 _ragain: lio = xread(sfd, pblk, lrem);
1196 if ((do_read_loops != 0 && sfd != 0) && (loopcnt < do_read_loops)) {
1197 lseek(sfd, 0L, SEEK_SET);
1204 if (lio != NOSIZE) ldone += lio;
1206 if (errno != EIO && catch_all_errors != YES)
1207 xerror(NO, NO, NO, "%s", srcfname);
1208 switch (error_action) {
1209 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1210 case TFC_ERRACT_SYNC:
1211 case TFC_ERRACT_LSYNC:
1212 xerror(YES, NO, NO, "%s", srcfname);
1213 lio = ldone = lrem = lblock;
1214 memset(srcblk, 0, lio);
1215 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1216 else lseek(sfd, rdpos + lio, SEEK_SET);
1218 default: xerror(NO, NO, NO, "%s", srcfname); break;
1221 if (unbuffered == NO && lio && lio < lrem) {
1226 total_processed_src += ldone;
1228 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1229 size_t orig = ldone;
1230 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1231 if (ldone > blksize) ldone = blksize;
1232 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1235 if (do_mac == TFC_MAC_SIGN) skein_update(&sk, srcblk, ldone);
1237 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1238 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1239 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1240 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1241 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1242 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1243 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1244 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1245 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1246 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1247 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1248 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1249 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1250 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1251 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_ENCRYPT)
1252 tf_pcbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1253 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_DECRYPT)
1254 tf_pcbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1256 else if (ctr_mode == TFC_MODE_PLAIN)
1257 memcpy(dstblk, srcblk, ldone);
1259 if (do_mac >= TFC_MAC_VRFY) skein_update(&sk, dstblk, ldone);
1260 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1265 _wagain: lio = xwrite(dfd, pblk, lrem);
1266 if (lio != NOSIZE) ldone += lio;
1267 else xerror(NO, NO, NO, "%s", dstfname);
1268 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1274 total_written_dst += ldone;
1275 _nowrite: total_processed_dst += ldone;
1276 delta_processed += ldone;
1278 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1284 if (verbose && status_timer && do_statline_dynamic == YES && statline_was_shown == YES) tfc_esay("\n");
1287 if (do_mac >= TFC_MAC_VRFY) {
1291 lrem = lblock = TF_FROM_BITS(macbits);
1292 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1293 _macragain: lio = xread(sfd, pblk, lrem);
1294 if (lio != NOSIZE) ldone += lio;
1296 if (errno != EIO && catch_all_errors != YES)
1297 xerror(NO, NO, NO, "%s", srcfname);
1298 switch (error_action) {
1299 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1300 case TFC_ERRACT_SYNC:
1301 case TFC_ERRACT_LSYNC:
1302 xerror(YES, NO, NO, "%s", srcfname);
1303 lio = ldone = lrem = lblock;
1304 memset(macvrfy, 0, lio);
1305 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1306 else lseek(sfd, rdpos + lio, SEEK_SET);
1308 default: xerror(NO, NO, NO, "%s", srcfname); break;
1311 if (lio && lio < lrem) {
1316 total_processed_src += ldone;
1321 if (!strcmp(do_mac_file, "-")) mfd = 0;
1322 else mfd = xopen(do_mac_file, O_RDONLY | O_LARGEFILE);
1323 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1324 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1325 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1326 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1327 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1328 lio = TF_FROM_BITS(macbits);
1329 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1331 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1335 if (ldone < TF_FROM_BITS(macbits)) {
1336 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1337 "not verifying", tfc_format_pid(progname), ldone);
1342 skein_final(macresult, &sk);
1344 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1345 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1346 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1347 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1348 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1349 else if (ctr_mode == TFC_MODE_PCBC) tf_pcbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1351 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1353 tfc_esay("%s: signature is good", tfc_format_pid(progname));
1355 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1356 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1359 if (do_mac == TFC_MAC_JUST_VRFY2) {
1360 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", tfc_format_pid(progname));
1361 maxlen = total_processed_src - SKEIN_DIGEST_SIZE;
1362 do_mac = TFC_MAC_DROP2;
1363 goto _decrypt_again_vrfy2;
1368 tfc_esay("%s: signature is BAD: "
1369 "wrong password, key, mode, or file is not signed", tfc_format_pid(progname));
1370 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", tfc_format_pid(progname));
1375 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1376 memset(macresult, 0, sizeof(macresult));
1377 memset(tmpdata, 0, sizeof(tmpdata));
1379 else if (do_mac == TFC_MAC_SIGN) {
1380 skein_final(macresult, &sk);
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_PCBC) tf_pcbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
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);