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;
521 td = strtod(optarg, &stoi);
522 status_timer = TFC_DTOUSECS(td);
523 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
531 if (!strcmp(progname, "tfbench")) {
532 if (!*(argv+optind)) usage();
534 td = strtod(*(argv+optind), &stoi);
535 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
537 "%s: invalid benchmark time in seconds", *(argv+optind));
538 bench_timer = TFC_DTOUSECS(td);
539 do_benchmark(bench_timer, td);
541 if (genrandom_nr_bytes) {
542 ctr_mode = TFC_MODE_STREAM;
543 do_edcrypt = TFC_DO_ENCRYPT;
544 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
546 if (genzero_nr_bytes) {
547 ctr_mode = TFC_MODE_PLAIN;
548 do_edcrypt = TFC_DO_PLAIN;
549 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
552 if (rawkey && password)
553 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
554 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
555 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
556 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
557 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
558 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
559 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
560 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
561 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
562 if (overwrite_source && counter_opt == TFC_CTR_RAND)
563 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
564 if (ctr_mode == TFC_MODE_PLAIN
565 && (do_edcrypt || do_mac || rawkey
566 || mackey_opt || counter_opt || counter_file))
567 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
575 memset(tfc_salt, 0, TFC_MAX_SALT);
577 if (!strcasecmp(saltf, "disable")) goto _nosalt;
579 if (!strcmp(saltf, "-")) saltfd = 0;
580 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
581 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
582 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
583 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
589 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
593 if (!strcmp(mackeyf, "-")) mkfd = 0;
594 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
595 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
597 skein_init(&sk, TFC_KEY_BITS);
604 lrem = lblock = sizeof(tmpdata);
605 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
606 _mkragain: lio = xread(mkfd, pblk, lrem);
607 if (lio == 0) do_stop = YES;
608 if (lio != NOSIZE) ldone += lio;
610 if (errno != EIO && catch_all_errors != YES)
611 xerror(NO, NO, NO, "%s", mackeyf);
612 switch (error_action) {
613 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
614 case TFC_ERRACT_SYNC:
615 case TFC_ERRACT_LSYNC:
616 xerror(YES, NO, NO, "%s", mackeyf);
617 lio = ldone = lrem = lblock;
618 memset(tmpdata, 0, lio);
619 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
620 else lseek(mkfd, rdpos + lio, SEEK_SET);
622 default: xerror(NO, NO, NO, "%s", mackeyf); break;
625 if (lio && lio < lrem) {
631 skein_update(&sk, tmpdata, ldone);
634 skein_final(mackey, &sk);
638 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
639 memset(&getps, 0, sizeof(struct getpasswd_state));
640 getps.fd = getps.efd = -1;
641 getps.passwd = pwdask;
642 getps.pwlen = sizeof(pwdask)-1;
643 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
644 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
645 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
646 getps.flags = GETP_WAITFILL;
647 n = xgetpasswd(&getps);
648 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
649 if (n == ((size_t)-2)) xexit(1);
650 if (verbose) say_hint(pwdask, n, "MAC password hint");
651 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
655 if ((strlen(progname) <= 9)
656 && ((!strcmp(progname, "sksum"))
657 || ((!memcmp(progname, "sk", 2))
658 && (!memcmp(progname+3, "sum", 3)
659 || !memcmp(progname+4, "sum", 3)
660 || !memcmp(progname+5, "sum", 3)
661 || !memcmp(progname+6, "sum", 3)))))
662 do_sksum(progname, argv+optind);
663 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
668 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
669 if (!strcmp(argv[idx], "-")) kfd = 0;
670 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
671 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
673 lio = strnlen(argv[idx], PATH_MAX);
674 memset(argv[idx], '*', lio);
681 if (do_full_key == NO && tweakf) {
684 if (!strcmp(tweakf, "-")) twfd = 0;
685 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
686 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
687 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
688 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
689 if (ldone < TF_TWEAK_SIZE)
690 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
697 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
699 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
700 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
701 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
703 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
705 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
706 if (verbose) tfc_esay("%s: disabling signature verification on "
707 "requested partial decryption.", progname);
711 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
712 maxlen = tfc_fdsize(sfd);
713 if (maxlen == NOFSIZE)
715 "Cannot verify embedded MAC with non-seekable source!");
716 maxlen -= TF_FROM_BITS(macbits);
718 srcfname = argv[idx];
722 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
723 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
725 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
726 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
728 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
733 if (!strcmp(counter_file, "-")) ctrfd = 0;
734 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
735 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
736 lio = xread(ctrfd, ctr, ctrsz);
737 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
738 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
741 else if (counter_opt == TFC_CTR_HEAD) {
744 lrem = lblock = ctrsz;
745 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
746 _ctrragain: lio = xread(sfd, pblk, lrem);
747 if (lio != NOSIZE) ldone += lio;
749 if (errno != EIO && catch_all_errors != YES)
750 xerror(NO, NO, NO, "%s", srcfname);
751 switch (error_action) {
752 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
753 case TFC_ERRACT_SYNC:
754 case TFC_ERRACT_LSYNC:
755 xerror(YES, NO, NO, "%s", srcfname);
756 lio = ldone = lrem = lblock;
758 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
759 else lseek(sfd, rdpos + lio, SEEK_SET);
761 default: xerror(NO, NO, NO, "%s", srcfname); break;
764 if (lio && lio < lrem) {
769 total_processed_src += ldone;
774 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
776 if (lseek(sfd, iseek, SEEK_SET) == -1)
777 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
780 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
782 if (verbose) tfc_esay("%s: hashing password", progname);
784 if (rawkey == TFC_RAWKEY_KEYFILE) {
785 tfc_yesno xtskeyset = NO;
789 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
790 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
791 _keyragain: lio = xread(kfd, pblk, lrem);
792 if (lio != NOSIZE) ldone += lio;
794 if (errno != EIO && catch_all_errors != YES)
795 xerror(NO, NO, NO, "reading key");
796 switch (error_action) {
797 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
798 case TFC_ERRACT_SYNC:
799 case TFC_ERRACT_LSYNC:
800 xerror(YES, NO, NO, "reading key");
801 lio = ldone = lrem = lblock;
803 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
804 else lseek(kfd, rdpos + lio, SEEK_SET);
806 default: xerror(NO, NO, NO, "reading key"); break;
809 if (lio && lio < lrem) {
814 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
816 if (ctr_mode == TFC_MODE_XTS) {
817 if (xtskeyset == NO) {
824 else if (rawkey == TFC_RAWKEY_ASKSTR) {
825 tfc_yesno xtskeyset = NO;
827 pblk = key; n = sizeof(key);
828 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
829 getps.fd = getps.efd = -1;
830 getps.passwd = (char *)pblk;
832 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
833 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
834 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
835 getps.flags = GETP_WAITFILL;
836 n = xgetpasswd(&getps);
837 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
838 if (n == ((size_t)-2)) xexit(1);
839 if (verbose) say_hint(pblk, n, "Raw string key hint");
840 if (ctr_mode == TFC_MODE_XTS) {
841 if (xtskeyset == NO) {
842 pblk = xtskey; n = sizeof(xtskey);
848 else if (rawkey == TFC_RAWKEY_ASKHEX) {
849 tfc_yesno xtskeyset = NO;
853 memset(&getps, 0, sizeof(struct getpasswd_state));
854 getps.fd = getps.efd = -1;
855 getps.passwd = pwdask;
856 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
857 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
858 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
859 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
860 getps.flags = GETP_WAITFILL;
861 n = xgetpasswd(&getps);
862 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
863 if (n == ((size_t)-2)) xexit(1);
865 tfc_esay("Please input even number of hex digits!");
866 goto _rawkey_hex_again;
868 hex2bin(pblk, pwdask);
869 memset(pwdask, 0, sizeof(pwdask));
870 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
871 if (ctr_mode == TFC_MODE_XTS) {
872 if (xtskeyset == NO) {
875 goto _rawkey_hex_again;
880 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
881 getps.fd = getps.efd = -1;
882 getps.passwd = pwdask;
883 getps.pwlen = sizeof(pwdask)-1;
884 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
885 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
886 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
887 getps.flags = GETP_WAITFILL;
888 n = xgetpasswd(&getps);
889 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
890 if (n == ((size_t)-2)) xexit(1);
891 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
892 getps.fd = getps.efd = -1;
893 getps.passwd = pwdagain;
894 getps.pwlen = sizeof(pwdagain)-1;
895 getps.echo = "Enter it again: ";
896 getps.flags = GETP_WAITFILL;
897 n = xgetpasswd(&getps);
898 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
899 if (n == ((size_t)-2)) xexit(1);
900 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
901 tfc_esay("Passwords are different, try again");
905 if (verbose) say_hint(pwdask, n, "Password hint");
906 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
907 memset(pwdask, 0, sizeof(pwdask));
908 memset(pwdagain, 0, sizeof(pwdagain));
911 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
912 xerror(NO, NO, YES, "hashing key");
916 if (tfc_saltsz > 0) {
917 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
918 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
920 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
921 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
922 memset(tfc_salt, 0, TFC_MAX_SALT);
925 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
926 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
931 tfc_yesno xtskeyset = NO;
934 if (!strcmp(genkeyf, "-")) krfd = 1;
935 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
936 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
937 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
938 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
939 if (verbose && xtskeyset == NO) {
940 tfc_esay("%s: password hashing done", progname);
941 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
942 tfc_esay("%s: Have a nice day!", progname);
945 if (ctr_mode == TFC_MODE_XTS) {
946 if (xtskeyset == NO) {
958 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
959 if (verbose) tfc_esay("%s: disabling signature verification on "
960 "requested partial decryption.", progname);
965 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
966 if (ctr_mode < TFC_MODE_OCB) {
967 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
968 "will be slower.", progname);
969 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
970 else skein_init(&sk, macbits);
974 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
975 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
979 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
980 if (do_full_key == NO) {
981 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
982 tf_tweak_set(key, tweak);
984 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
986 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
988 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
989 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
991 if (counter_opt == TFC_CTR_SHOW) {
993 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
994 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
995 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
998 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1005 if (verbose) tfc_esay("%s: password hashing done", progname);
1007 if (overwrite_source && srcfname) argv[idx] = srcfname;
1011 if (!strcmp(argv[idx], "-")) dfd = 1;
1012 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
1014 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1015 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
1017 dstfname = argv[idx];
1022 if (lseek(dfd, oseek, SEEK_SET) == -1)
1023 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1026 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1027 memset(&sigact, 0, sizeof(sigact));
1028 sigact.sa_flags = SA_RESTART;
1029 sigact.sa_handler = print_crypt_status;
1030 sigaction(SIGUSR1, &sigact, NULL);
1031 sigaction(SIGTSTP, &sigact, NULL);
1032 sigaction(SIGALRM, &sigact, NULL);
1033 if (status_timer) setup_next_alarm(status_timer);
1034 sigact.sa_handler = change_status_width;
1035 sigaction(SIGQUIT, &sigact, NULL);
1036 sigact.sa_handler = change_status_timer;
1037 sigaction(SIGUSR2, &sigact, NULL);
1039 sigact.sa_handler = print_crypt_status;
1040 sigaction(SIGINT, &sigact, NULL);
1041 sigaction(SIGTERM, &sigact, NULL);
1044 sigact.sa_handler = exit_sigterm;
1045 sigaction(SIGINT, &sigact, NULL);
1046 sigaction(SIGTERM, &sigact, NULL);
1048 memset(&sigact, 0, sizeof(struct sigaction));
1050 tfc_getcurtime(&delta_time);
1053 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1057 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1058 if (lio != NOSIZE) ldone += lio;
1059 else xerror(NO, NO, NO, "%s", dstfname);
1060 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1066 total_processed_dst += ldone;
1067 delta_processed += ldone;
1070 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1078 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1079 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1080 _ragain: lio = xread(sfd, pblk, lrem);
1081 if (lio == 0) do_stop = TFC_STOP_BEGAN;
1082 if (lio != NOSIZE) ldone += lio;
1084 if (errno != EIO && catch_all_errors != YES)
1085 xerror(NO, NO, NO, "%s", srcfname);
1086 switch (error_action) {
1087 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1088 case TFC_ERRACT_SYNC:
1089 case TFC_ERRACT_LSYNC:
1090 xerror(YES, NO, NO, "%s", srcfname);
1091 lio = ldone = lrem = lblock;
1092 memset(srcblk, 0, lio);
1093 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1094 else lseek(sfd, rdpos + lio, SEEK_SET);
1096 default: xerror(NO, NO, NO, "%s", srcfname); break;
1099 if (lio && lio < lrem) {
1104 total_processed_src += ldone;
1106 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1107 size_t orig = ldone;
1108 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1109 if (ldone > blksize) ldone = blksize;
1110 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1113 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1114 skein_update(&sk, srcblk, ldone);
1116 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1117 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1118 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1119 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1120 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1121 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1122 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1123 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1124 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1125 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1126 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1127 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1128 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1129 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1131 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1132 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1133 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1134 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1136 else if (ctr_mode == TFC_MODE_PLAIN)
1137 memcpy(dstblk, srcblk, ldone);
1139 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1140 skein_update(&sk, dstblk, ldone);
1141 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1146 _wagain: 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 _nowrite: total_processed_dst += ldone;
1156 delta_processed += ldone;
1158 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1161 if (do_stop == TFC_STOP_FULL) goto _nomac;
1164 if (do_mac >= TFC_MAC_VRFY) {
1168 lrem = lblock = TF_FROM_BITS(macbits);
1169 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1170 _macragain: lio = xread(sfd, pblk, lrem);
1171 if (lio != NOSIZE) ldone += lio;
1173 if (errno != EIO && catch_all_errors != YES)
1174 xerror(NO, NO, NO, "%s", srcfname);
1175 switch (error_action) {
1176 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1177 case TFC_ERRACT_SYNC:
1178 case TFC_ERRACT_LSYNC:
1179 xerror(YES, NO, NO, "%s", srcfname);
1180 lio = ldone = lrem = lblock;
1181 memset(macvrfy, 0, lio);
1182 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1183 else lseek(sfd, rdpos + lio, SEEK_SET);
1185 default: xerror(NO, NO, NO, "%s", srcfname); break;
1188 if (lio && lio < lrem) {
1193 total_processed_src += ldone;
1198 if (!strcmp(do_mac_file, "-")) mfd = 0;
1199 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1200 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1201 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1202 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1203 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1204 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1205 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1206 lio = TF_FROM_BITS(macbits);
1207 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1209 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1213 if (ldone < TF_FROM_BITS(macbits)) {
1214 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1215 "not verifying", progname, ldone);
1220 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1221 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1223 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1224 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1225 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1226 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1227 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1228 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1230 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1232 tfc_esay("%s: signature is good", progname);
1234 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1235 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1240 if (quiet == NO) tfc_esay("%s: signature is BAD: "
1241 "wrong password, key, mode, or file is not signed", progname);
1245 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1246 memset(macresult, 0, sizeof(macresult));
1247 memset(tmpdata, 0, sizeof(tmpdata));
1250 else if (do_mac == TFC_MAC_SIGN) {
1251 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1252 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1254 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1255 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1256 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1257 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1258 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1259 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1260 memset(macresult, 0, sizeof(macresult));
1264 lio = lrem = TF_FROM_BITS(macbits);
1266 _macwagain: lio = xwrite(dfd, pblk, lrem);
1267 if (lio != NOSIZE) ldone += lio;
1268 else xerror(NO, NO, NO, "%s", dstfname);
1269 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1275 total_processed_dst += ldone;
1276 delta_processed += ldone;
1281 if (!strcmp(do_mac_file, "-")) mfd = 1;
1282 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1283 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1284 if (do_outfmt == TFC_OUTFMT_B64) {
1285 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1286 memset(tmpdata, 0, TFC_TMPSIZE);
1287 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1288 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1289 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1291 tmpdata[lrem] = '\n';
1294 lio = xwrite(mfd, tmpdata, lrem);
1296 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1297 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1298 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1302 memset(macvrfy, 0, sizeof(macvrfy));
1303 memset(macresult, 0, sizeof(macresult));
1304 memset(tmpdata, 0, sizeof(tmpdata));
1308 if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1310 if (do_preserve_time) fcopy_matime(dfd, &s_stat);
1312 if (ftrunc_dfd != NOFSIZE) if (ftruncate(dfd, (off_t)ftrunc_dfd) == -1) xerror(YES, NO, YES, "ftruncate(%d)", dfd);