2 * tfcrypt -- high security Threefish encryption tool.
4 * tfcrypt is copyrighted:
5 * Copyright (C) 2012-2018 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 int getps_filter(struct getpasswd_state *getps, char chr, size_t pos)
34 getps->retn = ((size_t)-2);
40 static int getps_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
43 getps->retn = ((size_t)-2);
46 if (chr >= '0' && chr <= '9') return 1;
47 if (chr >= 'a' && chr <= 'f') return 1;
48 if (chr >= 'A' && chr <= 'F') return 1;
49 if (chr == '\x7f' || chr == '\x08'
50 || chr == '\x15' || chr == '\x17') return 1;
54 static inline int isctrlchr(int c)
57 if (c >= 0 && c <= 31) return 1;
58 if (c == 127) return 1;
62 static int getps_plain_filter(struct getpasswd_state *getps, char chr, size_t pos)
66 x = getps_filter(getps, chr, pos);
69 if (pos < getps->pwlen && !isctrlchr(chr))
70 write(getps->efd, &chr, sizeof(char));
74 static int getps_plain_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
78 x = getps_hex_filter(getps, chr, pos);
81 if (pos < getps->pwlen && !isctrlchr(chr))
82 write(getps->efd, &chr, sizeof(char));
86 static void make_hint(void *hint, size_t szhint, const void *data, size_t szdata)
88 char t[TF_FROM_BITS(TF_MAX_BITS)];
90 skein(t, TF_MAX_BITS, NULL, data, szdata);
91 xor_shrink(hint, szhint, t, sizeof(t));
92 memset(t, 0, sizeof(t));
95 static void raw_say_hint(void *hint, size_t szhint, const void *data, size_t szdata, const char *prompt)
97 make_hint(hint, szhint, data, szdata);
98 if (prompt) tfc_nfsay(stderr, "%s: ", prompt);
99 mehexdump(hint, szhint, szhint, 1);
100 memset(hint, 0, szhint);
103 static void say_hint(const void *data, size_t szdata, const char *prompt)
105 char t[TF_SIZE_UNIT];
106 raw_say_hint(t, TF_SIZE_UNIT, data, szdata, prompt);
107 /* t[] is erased (automatically) */
110 int main(int argc, char **argv)
114 char *s, *d, *t, *stoi;
117 progname = basename(argv[0]);
119 if (!isatty(2)) do_statline_dynamic = NO;
124 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
125 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
126 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
127 read_defaults(s, YES);
132 while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:Pkzxc:l:qedn:vV:pwE:O:S:AmM:R:Z:WHD:")) != -1) {
135 read_defaults(optarg, NO);
144 if (!strcasecmp(optarg, "show"))
145 counter_opt = TFC_CTR_SHOW;
146 else if (!strcasecmp(optarg, "head"))
147 counter_opt = TFC_CTR_HEAD;
148 else if (!strcasecmp(optarg, "rand"))
149 counter_opt = TFC_CTR_RAND;
150 else if (!strcasecmp(optarg, "zero"))
151 counter_opt = TFC_CTR_ZERO;
152 else counter_file = sksum_hashlist_file = optarg;
155 if (!strcasecmp(optarg, "ctr"))
156 ctr_mode = TFC_MODE_CTR;
157 else if (!strcasecmp(optarg, "stream"))
158 ctr_mode = TFC_MODE_STREAM;
159 else if (!strcasecmp(optarg, "cbc"))
160 ctr_mode = TFC_MODE_CBC;
161 else if (!strcasecmp(optarg, "ecb"))
162 ctr_mode = TFC_MODE_ECB;
163 else if (!strcasecmp(optarg, "xts"))
164 ctr_mode = TFC_MODE_XTS;
165 else if (!strcasecmp(optarg, "ocb"))
166 ctr_mode = TFC_MODE_OCB;
167 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
170 do_edcrypt = TFC_DO_PLAIN;
172 ctr_mode = TFC_MODE_PLAIN;
175 do_edcrypt = TFC_DO_ENCRYPT;
178 do_edcrypt = TFC_DO_DECRYPT;
181 macbits = strtoul(optarg, &stoi, 10);
182 if (macbits == 0 || !str_empty(stoi) || macbits < 8
183 || macbits > TF_MAX_BITS || macbits % 8)
184 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
187 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
188 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
191 if (!strcasecmp(optarg, "key"))
192 mackey_opt = TFC_MACKEY_RAWKEY;
193 else if (!strcasecmp(optarg, "pwd"))
194 mackey_opt = TFC_MACKEY_PASSWORD;
196 mackey_opt = TFC_MACKEY_FILE;
204 rawkey = TFC_RAWKEY_KEYFILE;
207 rawkey = TFC_RAWKEY_ASKSTR;
210 rawkey = TFC_RAWKEY_ASKHEX;
221 if (maxlen != NOFSIZE) break;
223 maxlen = tfc_humanfsize(optarg, &stoi);
224 if (!str_empty(stoi)) {
225 maxlen = tfc_fnamesize(optarg, YES);
226 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
227 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
228 "%s: invalid count value", optarg);
230 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
231 if (counter_opt == TFC_CTR_HEAD)
232 maxlen += TF_BLOCK_SIZE;
235 overwrite_source = YES;
238 if (!strcmp(optarg, "xall")) {
239 catch_all_errors = YES;
242 if (!strcmp(optarg, "xseek")) {
243 ignore_seek_errors = YES;
246 if (!strcmp(optarg, "exit"))
247 error_action = TFC_ERRACT_EXIT;
248 else if (!strncmp(optarg, "cont", 4))
249 error_action = TFC_ERRACT_CONT;
250 else if (!strcmp(optarg, "sync"))
251 error_action = TFC_ERRACT_SYNC;
252 else if (!strcmp(optarg, "lsync"))
253 error_action = TFC_ERRACT_LSYNC;
254 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
257 s = d = optarg; t = NULL;
258 while ((s = strtok_r(d, ",", &t))) {
260 if (!strcmp(s, "sync"))
261 write_flags |= O_SYNC;
262 else if (!strcmp(s, "trunc"))
263 write_flags |= O_TRUNC;
264 else if (!strcmp(s, "fsync"))
266 else if (!strcmp(s, "pad"))
268 else if (!strcmp(s, "xtime"))
269 do_preserve_time = YES;
270 else if (!strcmp(s, "gibsize"))
271 do_stats_in_gibs = YES;
272 else if (!strcmp(s, "plainstats"))
273 do_statline_dynamic = NO;
274 else if (!strcmp(s, "statless"))
276 else if (!strcmp(s, "norepeat"))
278 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
280 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
281 mac_pw_prompt = s+10;
282 else if (!strcmp(s, "shorthex"))
283 do_full_hexdump = NO;
284 else if (!strcmp(s, "fullkey"))
286 else if (!strcmp(s, "showsecrets"))
288 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
290 blksize = (size_t)tfc_humanfsize(s, &stoi);
291 if (!str_empty(stoi)) {
292 blksize = (size_t)tfc_fnamesize(s, YES);
293 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
294 if (blksize == NOSIZE) xerror(NO, YES, YES,
295 "%s: invalid block size value", s);
297 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
298 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
299 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
300 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
301 "%s: block size exceeds %u bytes",
302 s, TFC_U(TFC_BLKSIZE));
304 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
306 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
307 if (!str_empty(stoi)) {
308 xtsblocks = (size_t)tfc_fnamesize(s, YES);
309 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
310 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
311 "%s: invalid blocks per xts block value", s);
313 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
314 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
315 "%s: nr of blocks per xts block is not round to %u bytes",
316 s, TFC_U(TFC_BLKSIZE));
317 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
318 "%s: nr of blocks per xts block exceeds %u bytes",
319 s, TFC_U(TFC_BLKSIZE));
321 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
323 iseek = tfc_humanfsize(s, &stoi);
324 if (!str_empty(stoi)) {
325 iseek = tfc_fnamesize(s, YES);
326 iseek = tfc_modifysize(iseek, strchr(s, ':'));
327 if (iseek == NOFSIZE) xerror(NO, YES, YES,
328 "%s: invalid iseek value", s);
330 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
331 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
333 "%s: not round to TF block size "
335 s, TFC_U(TF_BLOCK_SIZE));
336 iseek_blocks = iseek / TF_BLOCK_SIZE;
338 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
340 iseek = tfc_humanfsize(s, &stoi);
341 if (!str_empty(stoi)) {
342 iseek = tfc_fnamesize(s, YES);
343 iseek = tfc_modifysize(iseek, strchr(s, ':'));
344 if (iseek == NOFSIZE) xerror(NO, YES, YES,
345 "%s: invalid ixseek value", s);
347 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
349 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
351 iseek_blocks = tfc_humanfsize(s, &stoi);
352 if (!str_empty(stoi)) {
353 iseek_blocks = tfc_fnamesize(s, YES);
354 if (iseek_blocks == NOFSIZE)
356 "%s: invalid ictr value", s);
357 iseek_blocks /= TF_BLOCK_SIZE;
358 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
360 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
362 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
364 iseek_blocks = tfc_humanfsize(s, &stoi);
365 if (!str_empty(stoi)) {
366 iseek_blocks = tfc_fnamesize(s, YES);
367 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
368 if (iseek_blocks == NOFSIZE)
370 "%s: invalid ixctr value", s);
372 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
373 if (iseek_blocks % TF_BLOCK_SIZE)
375 "%s: not round to TF block size "
376 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
377 iseek_blocks /= TF_BLOCK_SIZE;
379 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
381 oseek = tfc_humanfsize(s, &stoi);
382 if (!str_empty(stoi)) {
383 oseek = tfc_fnamesize(s, YES);
384 oseek = tfc_modifysize(oseek, strchr(s, ':'));
385 if (oseek == NOFSIZE) xerror(NO, YES, YES,
386 "%s: invalid oseek value", s);
388 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
390 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
392 maxlen = tfc_humanfsize(s, &stoi);
393 if (!str_empty(stoi)) {
394 maxlen = tfc_fnamesize(s, YES);
395 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
396 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
397 "%s: invalid count value", s);
399 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
400 if (counter_opt == TFC_CTR_HEAD)
401 maxlen += TF_BLOCK_SIZE;
403 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
405 ftrunc_dfd = tfc_humanfsize(s, &stoi);
406 if (!str_empty(stoi)) {
407 ftrunc_dfd = tfc_fnamesize(s, YES);
408 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
409 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
410 "%s: invalid ftrunc value", s);
412 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
414 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
416 maxkeylen = tfc_humanfsize(s, &stoi);
417 if (!str_empty(stoi)) {
418 maxkeylen = tfc_fnamesize(s, YES);
419 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
420 if (maxkeylen == NOSIZE)
422 "%s: invalid key length value", s);
424 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
426 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
428 keyoffset = tfc_humanfsize(s, &stoi);
429 if (!str_empty(stoi)) {
430 keyoffset = tfc_fnamesize(s, YES);
431 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
432 if (keyoffset == NOFSIZE)
434 "%s: invalid key offset value", s);
436 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
438 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
440 ctrsz = tfc_humanfsize(s, &stoi);
441 if (!str_empty(stoi)) {
442 ctrsz = (size_t)tfc_fnamesize(s, YES);
443 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
446 "%s: invalid counter length value", s);
448 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
449 if (ctrsz > TF_BLOCK_SIZE)
450 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
452 else xerror(NO, YES, YES, "invalid option %s", s);
456 do_mac = TFC_MAC_SIGN;
457 if (strcasecmp(optarg, "mac") != 0)
458 do_mac_file = optarg;
461 do_mac = TFC_MAC_VRFY;
462 if (!strcasecmp(optarg, "drop"))
463 do_mac = TFC_MAC_DROP;
464 else if (strcasecmp(optarg, "mac") != 0)
465 do_mac_file = optarg;
468 if (do_mac != TFC_MAC_VRFY)
469 xerror(NO, YES, YES, "signature source was not specified");
470 do_mac = TFC_MAC_JUST_VRFY;
474 if (maxlen != NOFSIZE) {
475 if (c == 'Z') genzero_nr_bytes = maxlen;
476 else genrandom_nr_bytes = maxlen;
480 if (!strcasecmp(optarg, "cbs"))
482 else if (!strcasecmp(optarg, "ks"))
483 t = TF_FROM_BITS(TFC_KEY_BITS);
484 else if (!strcasecmp(optarg, "xks"))
485 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
486 else if (!strcasecmp(optarg, "iobs"))
489 t = tfc_humanfsize(optarg, &stoi);
490 if (!str_empty(stoi)) {
491 t = tfc_fnamesize(optarg, NO);
492 t = tfc_modifysize(t, strchr(optarg, ':'));
494 else t = tfc_modifysize(t, strchr(optarg, ':'));
496 if (c == 'Z') genzero_nr_bytes = maxlen = t;
497 else genrandom_nr_bytes = maxlen = t;
501 do_preserve_time = YES;
504 do_outfmt = TFC_OUTFMT_B64;
507 do_outfmt = TFC_OUTFMT_RAW;
510 do_outfmt = TFC_OUTFMT_HEX;
515 do_full_hexdump = NO;
522 td = strtod(optarg, &stoi);
523 status_timer = TFC_DTOUSECS(td);
524 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
532 if (!strcmp(progname, "tfbench")) {
533 if (!*(argv+optind)) usage();
535 td = strtod(*(argv+optind), &stoi);
536 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
538 "%s: invalid benchmark time in seconds", *(argv+optind));
539 bench_timer = TFC_DTOUSECS(td);
540 do_benchmark(bench_timer, td);
542 if (genrandom_nr_bytes) {
543 ctr_mode = TFC_MODE_STREAM;
544 do_edcrypt = TFC_DO_ENCRYPT;
545 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
547 if (genzero_nr_bytes) {
548 ctr_mode = TFC_MODE_PLAIN;
549 do_edcrypt = TFC_DO_PLAIN;
550 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
553 if (rawkey && password)
554 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
555 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
556 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
557 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
558 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
559 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
560 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
561 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
562 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
563 if (overwrite_source && counter_opt == TFC_CTR_RAND)
564 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
565 if (ctr_mode == TFC_MODE_PLAIN
566 && (do_edcrypt || do_mac || rawkey
567 || mackey_opt || counter_opt || counter_file))
568 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
576 memset(tfc_salt, 0, TFC_MAX_SALT);
578 if (!strcasecmp(saltf, "disable")) goto _nosalt;
580 if (!strcmp(saltf, "-")) saltfd = 0;
581 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
582 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
583 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
584 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
590 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
594 if (!strcmp(mackeyf, "-")) mkfd = 0;
595 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
596 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
598 skein_init(&sk, TFC_KEY_BITS);
605 lrem = lblock = sizeof(tmpdata);
606 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
607 _mkragain: lio = xread(mkfd, pblk, lrem);
608 if (lio == 0) do_stop = YES;
609 if (lio != NOSIZE) ldone += lio;
611 if (errno != EIO && catch_all_errors != YES)
612 xerror(NO, NO, NO, "%s", mackeyf);
613 switch (error_action) {
614 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
615 case TFC_ERRACT_SYNC:
616 case TFC_ERRACT_LSYNC:
617 xerror(YES, NO, NO, "%s", mackeyf);
618 lio = ldone = lrem = lblock;
619 memset(tmpdata, 0, lio);
620 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
621 else lseek(mkfd, rdpos + lio, SEEK_SET);
623 default: xerror(NO, NO, NO, "%s", mackeyf); break;
626 if (lio && lio < lrem) {
632 skein_update(&sk, tmpdata, ldone);
635 skein_final(mackey, &sk);
639 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
640 memset(&getps, 0, sizeof(struct getpasswd_state));
641 getps.fd = getps.efd = -1;
642 getps.passwd = pwdask;
643 getps.pwlen = sizeof(pwdask)-1;
644 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
645 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
646 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
647 getps.flags = GETP_WAITFILL;
648 n = xgetpasswd(&getps);
649 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
650 if (n == ((size_t)-2)) xexit(1);
651 if (verbose) say_hint(pwdask, n, "MAC password hint");
652 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
656 if ((strlen(progname) <= 9)
657 && ((!strcmp(progname, "sksum"))
658 || ((!memcmp(progname, "sk", 2))
659 && (!memcmp(progname+3, "sum", 3)
660 || !memcmp(progname+4, "sum", 3)
661 || !memcmp(progname+5, "sum", 3)
662 || !memcmp(progname+6, "sum", 3)))))
663 do_sksum(progname, argv+optind);
664 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
669 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
670 if (!strcmp(argv[idx], "-")) kfd = 0;
671 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
672 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
674 lio = strnlen(argv[idx], PATH_MAX);
675 memset(argv[idx], '*', lio);
682 if (do_full_key == NO && tweakf) {
685 if (!strcmp(tweakf, "-")) twfd = 0;
686 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
687 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
688 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
689 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
690 if (ldone < TF_TWEAK_SIZE)
691 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
698 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
700 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
701 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
702 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
704 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
706 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
707 if (verbose) tfc_esay("%s: disabling signature verification on "
708 "requested partial decryption.", progname);
712 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
713 maxlen = tfc_fdsize(sfd);
714 if (maxlen == NOFSIZE)
716 "Cannot verify embedded MAC with non-seekable source!");
717 maxlen -= TF_FROM_BITS(macbits);
719 srcfname = argv[idx];
723 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
724 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
726 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
727 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
729 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
734 if (!strcmp(counter_file, "-")) ctrfd = 0;
735 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
736 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
737 lio = xread(ctrfd, ctr, ctrsz);
738 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
739 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
742 else if (counter_opt == TFC_CTR_HEAD) {
745 lrem = lblock = ctrsz;
746 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
747 _ctrragain: lio = xread(sfd, pblk, lrem);
748 if (lio != NOSIZE) ldone += lio;
750 if (errno != EIO && catch_all_errors != YES)
751 xerror(NO, NO, NO, "%s", srcfname);
752 switch (error_action) {
753 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
754 case TFC_ERRACT_SYNC:
755 case TFC_ERRACT_LSYNC:
756 xerror(YES, NO, NO, "%s", srcfname);
757 lio = ldone = lrem = lblock;
759 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
760 else lseek(sfd, rdpos + lio, SEEK_SET);
762 default: xerror(NO, NO, NO, "%s", srcfname); break;
765 if (lio && lio < lrem) {
770 total_processed_src += ldone;
775 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
777 if (lseek(sfd, iseek, SEEK_SET) == -1)
778 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
781 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
783 if (verbose) tfc_esay("%s: hashing password", progname);
785 if (rawkey == TFC_RAWKEY_KEYFILE) {
786 tfc_yesno xtskeyset = NO;
790 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
791 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
792 _keyragain: lio = xread(kfd, pblk, lrem);
793 if (lio != NOSIZE) ldone += lio;
795 if (errno != EIO && catch_all_errors != YES)
796 xerror(NO, NO, NO, "reading key");
797 switch (error_action) {
798 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
799 case TFC_ERRACT_SYNC:
800 case TFC_ERRACT_LSYNC:
801 xerror(YES, NO, NO, "reading key");
802 lio = ldone = lrem = lblock;
804 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
805 else lseek(kfd, rdpos + lio, SEEK_SET);
807 default: xerror(NO, NO, NO, "reading key"); break;
810 if (lio && lio < lrem) {
815 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
817 if (ctr_mode == TFC_MODE_XTS) {
818 if (xtskeyset == NO) {
825 else if (rawkey == TFC_RAWKEY_ASKSTR) {
826 tfc_yesno xtskeyset = NO;
828 pblk = key; n = sizeof(key);
829 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
830 getps.fd = getps.efd = -1;
831 getps.passwd = (char *)pblk;
833 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
834 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
835 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
836 getps.flags = GETP_WAITFILL;
837 n = xgetpasswd(&getps);
838 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
839 if (n == ((size_t)-2)) xexit(1);
840 if (verbose) say_hint(pblk, n, "Raw string key hint");
841 if (ctr_mode == TFC_MODE_XTS) {
842 if (xtskeyset == NO) {
843 pblk = xtskey; n = sizeof(xtskey);
849 else if (rawkey == TFC_RAWKEY_ASKHEX) {
850 tfc_yesno xtskeyset = NO;
854 memset(&getps, 0, sizeof(struct getpasswd_state));
855 getps.fd = getps.efd = -1;
856 getps.passwd = pwdask;
857 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
858 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
859 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
860 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
861 getps.flags = GETP_WAITFILL;
862 n = xgetpasswd(&getps);
863 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
864 if (n == ((size_t)-2)) xexit(1);
866 tfc_esay("Please input even number of hex digits!");
867 goto _rawkey_hex_again;
869 hex2bin(pblk, pwdask);
870 memset(pwdask, 0, sizeof(pwdask));
871 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
872 if (ctr_mode == TFC_MODE_XTS) {
873 if (xtskeyset == NO) {
876 goto _rawkey_hex_again;
881 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
882 getps.fd = getps.efd = -1;
883 getps.passwd = pwdask;
884 getps.pwlen = sizeof(pwdask)-1;
885 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
886 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
887 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
888 getps.flags = GETP_WAITFILL;
889 n = xgetpasswd(&getps);
890 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
891 if (n == ((size_t)-2)) xexit(1);
892 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
893 getps.fd = getps.efd = -1;
894 getps.passwd = pwdagain;
895 getps.pwlen = sizeof(pwdagain)-1;
896 getps.echo = "Enter it again: ";
897 getps.flags = GETP_WAITFILL;
898 n = xgetpasswd(&getps);
899 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
900 if (n == ((size_t)-2)) xexit(1);
901 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
902 tfc_esay("Passwords are different, try again");
906 if (verbose) say_hint(pwdask, n, "Password hint");
907 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
908 memset(pwdask, 0, sizeof(pwdask));
909 memset(pwdagain, 0, sizeof(pwdagain));
912 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
913 xerror(NO, NO, YES, "hashing key");
917 if (tfc_saltsz > 0) {
918 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
919 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
921 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
922 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
923 memset(tfc_salt, 0, TFC_MAX_SALT);
926 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
927 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
932 tfc_yesno xtskeyset = NO;
935 if (!strcmp(genkeyf, "-")) krfd = 1;
936 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
937 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
938 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
939 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
940 if (verbose && xtskeyset == NO) {
941 tfc_esay("%s: password hashing done", progname);
942 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
943 tfc_esay("%s: Have a nice day!", progname);
946 if (ctr_mode == TFC_MODE_XTS) {
947 if (xtskeyset == NO) {
959 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
960 if (verbose) tfc_esay("%s: disabling signature verification on "
961 "requested partial decryption.", progname);
966 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
967 if (ctr_mode < TFC_MODE_OCB) {
968 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
969 "will be slower.", progname);
970 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
971 else skein_init(&sk, macbits);
975 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
976 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
980 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
981 if (do_full_key == NO) {
982 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
983 tf_tweak_set(key, tweak);
985 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
987 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
989 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
990 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
992 if (counter_opt == TFC_CTR_SHOW) {
994 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
995 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
996 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
999 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1006 if (verbose) tfc_esay("%s: password hashing done", progname);
1008 if (overwrite_source && srcfname) argv[idx] = srcfname;
1012 if (!strcmp(argv[idx], "-")) dfd = 1;
1013 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
1015 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1016 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
1018 dstfname = argv[idx];
1023 if (lseek(dfd, oseek, SEEK_SET) == -1)
1024 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1027 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1028 memset(&sigact, 0, sizeof(sigact));
1029 sigact.sa_flags = SA_RESTART;
1030 sigact.sa_handler = print_crypt_status;
1031 sigaction(SIGUSR1, &sigact, NULL);
1032 sigaction(SIGTSTP, &sigact, NULL);
1033 sigaction(SIGALRM, &sigact, NULL);
1034 if (status_timer) setup_next_alarm(status_timer);
1035 sigact.sa_handler = change_status_width;
1036 sigaction(SIGQUIT, &sigact, NULL);
1037 sigact.sa_handler = change_status_timer;
1038 sigaction(SIGUSR2, &sigact, NULL);
1040 sigact.sa_handler = print_crypt_status;
1041 sigaction(SIGINT, &sigact, NULL);
1042 sigaction(SIGTERM, &sigact, NULL);
1045 sigact.sa_handler = exit_sigterm;
1046 sigaction(SIGINT, &sigact, NULL);
1047 sigaction(SIGTERM, &sigact, NULL);
1049 memset(&sigact, 0, sizeof(struct sigaction));
1051 tfc_getcurtime(&delta_time);
1054 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1058 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1059 if (lio != NOSIZE) ldone += lio;
1060 else xerror(NO, NO, NO, "%s", dstfname);
1061 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1067 total_processed_dst += ldone;
1068 delta_processed += ldone;
1071 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1079 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1080 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1081 _ragain: lio = xread(sfd, pblk, lrem);
1082 if (lio == 0) do_stop = TFC_STOP_BEGAN;
1083 if (lio != NOSIZE) ldone += lio;
1085 if (errno != EIO && catch_all_errors != YES)
1086 xerror(NO, NO, NO, "%s", srcfname);
1087 switch (error_action) {
1088 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1089 case TFC_ERRACT_SYNC:
1090 case TFC_ERRACT_LSYNC:
1091 xerror(YES, NO, NO, "%s", srcfname);
1092 lio = ldone = lrem = lblock;
1093 memset(srcblk, 0, lio);
1094 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1095 else lseek(sfd, rdpos + lio, SEEK_SET);
1097 default: xerror(NO, NO, NO, "%s", srcfname); break;
1100 if (lio && lio < lrem) {
1105 total_processed_src += ldone;
1107 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1108 size_t orig = ldone;
1109 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1110 if (ldone > blksize) ldone = blksize;
1111 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1114 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1115 skein_update(&sk, srcblk, ldone);
1117 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1118 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1119 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1120 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1121 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1122 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1123 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1124 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1125 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1126 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1127 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1128 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1129 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1130 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1132 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1133 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1134 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1135 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1137 else if (ctr_mode == TFC_MODE_PLAIN)
1138 memcpy(dstblk, srcblk, ldone);
1140 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1141 skein_update(&sk, dstblk, ldone);
1142 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1147 _wagain: lio = xwrite(dfd, pblk, lrem);
1148 if (lio != NOSIZE) ldone += lio;
1149 else xerror(NO, NO, NO, "%s", dstfname);
1150 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1156 _nowrite: total_processed_dst += ldone;
1157 delta_processed += ldone;
1159 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1162 if (do_stop == TFC_STOP_FULL) goto _nomac;
1165 if (do_mac >= TFC_MAC_VRFY) {
1169 lrem = lblock = TF_FROM_BITS(macbits);
1170 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1171 _macragain: lio = xread(sfd, pblk, lrem);
1172 if (lio != NOSIZE) ldone += lio;
1174 if (errno != EIO && catch_all_errors != YES)
1175 xerror(NO, NO, NO, "%s", srcfname);
1176 switch (error_action) {
1177 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1178 case TFC_ERRACT_SYNC:
1179 case TFC_ERRACT_LSYNC:
1180 xerror(YES, NO, NO, "%s", srcfname);
1181 lio = ldone = lrem = lblock;
1182 memset(macvrfy, 0, lio);
1183 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1184 else lseek(sfd, rdpos + lio, SEEK_SET);
1186 default: xerror(NO, NO, NO, "%s", srcfname); break;
1189 if (lio && lio < lrem) {
1194 total_processed_src += ldone;
1199 if (!strcmp(do_mac_file, "-")) mfd = 0;
1200 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1201 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1202 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1203 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1204 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1205 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1206 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1207 lio = TF_FROM_BITS(macbits);
1208 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1210 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1214 if (ldone < TF_FROM_BITS(macbits)) {
1215 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1216 "not verifying", progname, ldone);
1221 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1222 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1224 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1225 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1226 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1227 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1228 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1229 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1231 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1233 tfc_esay("%s: signature is good", progname);
1235 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1236 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1241 if (quiet == NO) tfc_esay("%s: signature is BAD: "
1242 "wrong password, key, mode, or file is not signed", progname);
1246 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1247 memset(macresult, 0, sizeof(macresult));
1248 memset(tmpdata, 0, sizeof(tmpdata));
1251 else if (do_mac == TFC_MAC_SIGN) {
1252 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1253 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1255 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1256 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1257 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1258 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1259 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1260 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1261 memset(macresult, 0, sizeof(macresult));
1265 lio = lrem = TF_FROM_BITS(macbits);
1267 _macwagain: lio = xwrite(dfd, pblk, lrem);
1268 if (lio != NOSIZE) ldone += lio;
1269 else xerror(NO, NO, NO, "%s", dstfname);
1270 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1276 total_processed_dst += ldone;
1277 delta_processed += ldone;
1282 if (!strcmp(do_mac_file, "-")) mfd = 1;
1283 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1284 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1285 if (do_outfmt == TFC_OUTFMT_B64) {
1286 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1287 memset(tmpdata, 0, TFC_TMPSIZE);
1288 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1289 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1290 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1292 tmpdata[lrem] = '\n';
1295 lio = xwrite(mfd, tmpdata, lrem);
1297 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1298 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1299 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1303 memset(macvrfy, 0, sizeof(macvrfy));
1304 memset(macresult, 0, sizeof(macresult));
1305 memset(tmpdata, 0, sizeof(tmpdata));
1309 if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1311 if (do_preserve_time) fcopy_matime(dfd, &s_stat);
1313 if (ftrunc_dfd != NOFSIZE) if (ftruncate(dfd, (off_t)ftrunc_dfd) == -1) xerror(YES, NO, YES, "ftruncate(%d)", dfd);