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 int main(int argc, char **argv)
58 char *s, *d, *t, *stoi;
61 progname = basename(argv[0]);
63 if (!isatty(2)) do_statline_dynamic = NO;
68 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
69 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
70 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
71 read_defaults(s, YES);
76 while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:Pkzxc:l:qedn:vV:pwE:O:S:AmM:R:Z:WHD:")) != -1) {
79 read_defaults(optarg, NO);
88 if (!strcasecmp(optarg, "show"))
89 counter_opt = TFC_CTR_SHOW;
90 else if (!strcasecmp(optarg, "head"))
91 counter_opt = TFC_CTR_HEAD;
92 else if (!strcasecmp(optarg, "rand"))
93 counter_opt = TFC_CTR_RAND;
94 else if (!strcasecmp(optarg, "zero"))
95 counter_opt = TFC_CTR_ZERO;
96 else counter_file = sksum_hashlist_file = optarg;
99 if (!strcasecmp(optarg, "ctr"))
100 ctr_mode = TFC_MODE_CTR;
101 else if (!strcasecmp(optarg, "stream"))
102 ctr_mode = TFC_MODE_STREAM;
103 else if (!strcasecmp(optarg, "cbc"))
104 ctr_mode = TFC_MODE_CBC;
105 else if (!strcasecmp(optarg, "ecb"))
106 ctr_mode = TFC_MODE_ECB;
107 else if (!strcasecmp(optarg, "xts"))
108 ctr_mode = TFC_MODE_XTS;
109 else if (!strcasecmp(optarg, "ocb"))
110 ctr_mode = TFC_MODE_OCB;
111 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
114 do_edcrypt = TFC_DO_PLAIN;
116 ctr_mode = TFC_MODE_PLAIN;
119 do_edcrypt = TFC_DO_ENCRYPT;
122 do_edcrypt = TFC_DO_DECRYPT;
125 macbits = strtoul(optarg, &stoi, 10);
126 if (macbits == 0 || !str_empty(stoi) || macbits < 8
127 || macbits > TF_MAX_BITS || macbits % 8)
128 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
131 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
132 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
135 if (!strcasecmp(optarg, "key"))
136 mackey_opt = TFC_MACKEY_RAWKEY;
137 else if (!strcasecmp(optarg, "pwd"))
138 mackey_opt = TFC_MACKEY_PASSWORD;
140 mackey_opt = TFC_MACKEY_FILE;
148 rawkey = TFC_RAWKEY_KEYFILE;
151 rawkey = TFC_RAWKEY_ASKSTR;
154 rawkey = TFC_RAWKEY_ASKHEX;
165 if (maxlen != NOFSIZE) break;
167 maxlen = tfc_humanfsize(optarg, &stoi);
168 if (!str_empty(stoi)) {
169 maxlen = tfc_fnamesize(optarg, YES);
170 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
171 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
172 "%s: invalid count value", optarg);
174 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
175 if (counter_opt == TFC_CTR_HEAD)
176 maxlen += TF_BLOCK_SIZE;
179 overwrite_source = YES;
182 if (!strcmp(optarg, "xall")) {
183 catch_all_errors = YES;
186 if (!strcmp(optarg, "xseek")) {
187 ignore_seek_errors = YES;
190 if (!strcmp(optarg, "exit"))
191 error_action = TFC_ERRACT_EXIT;
192 else if (!strncmp(optarg, "cont", 4))
193 error_action = TFC_ERRACT_CONT;
194 else if (!strcmp(optarg, "sync"))
195 error_action = TFC_ERRACT_SYNC;
196 else if (!strcmp(optarg, "lsync"))
197 error_action = TFC_ERRACT_LSYNC;
198 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
201 s = d = optarg; t = NULL;
202 while ((s = strtok_r(d, ",", &t))) {
204 if (!strcmp(s, "sync"))
205 write_flags |= O_SYNC;
206 else if (!strcmp(s, "trunc"))
207 write_flags |= O_TRUNC;
208 else if (!strcmp(s, "fsync"))
210 else if (!strcmp(s, "pad"))
212 else if (!strcmp(s, "xtime"))
213 do_preserve_time = YES;
214 else if (!strcmp(s, "gibsize"))
215 do_stats_in_gibs = YES;
216 else if (!strcmp(s, "plainstats"))
217 do_statline_dynamic = NO;
218 else if (!strcmp(s, "statless"))
220 else if (!strcmp(s, "norepeat"))
222 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
224 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
225 mac_pw_prompt = s+10;
226 else if (!strcmp(s, "shorthex"))
227 do_full_hexdump = NO;
228 else if (!strcmp(s, "fullkey"))
230 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
232 blksize = (size_t)tfc_humanfsize(s, &stoi);
233 if (!str_empty(stoi)) {
234 blksize = (size_t)tfc_fnamesize(s, YES);
235 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
236 if (blksize == NOSIZE) xerror(NO, YES, YES,
237 "%s: invalid block size value", s);
239 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
240 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
241 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
242 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
243 "%s: block size exceeds %u bytes",
244 s, TFC_U(TFC_BLKSIZE));
246 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
248 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
249 if (!str_empty(stoi)) {
250 xtsblocks = (size_t)tfc_fnamesize(s, YES);
251 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
252 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
253 "%s: invalid blocks per xts block value", s);
255 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
256 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
257 "%s: nr of blocks per xts block is not round to %u bytes",
258 s, TFC_U(TFC_BLKSIZE));
259 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
260 "%s: nr of blocks per xts block exceeds %u bytes",
261 s, TFC_U(TFC_BLKSIZE));
263 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
265 iseek = tfc_humanfsize(s, &stoi);
266 if (!str_empty(stoi)) {
267 iseek = tfc_fnamesize(s, YES);
268 iseek = tfc_modifysize(iseek, strchr(s, ':'));
269 if (iseek == NOFSIZE) xerror(NO, YES, YES,
270 "%s: invalid iseek value", s);
272 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
273 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
275 "%s: not round to TF block size "
277 s, TFC_U(TF_BLOCK_SIZE));
278 iseek_blocks = iseek / TF_BLOCK_SIZE;
280 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
282 iseek = tfc_humanfsize(s, &stoi);
283 if (!str_empty(stoi)) {
284 iseek = tfc_fnamesize(s, YES);
285 iseek = tfc_modifysize(iseek, strchr(s, ':'));
286 if (iseek == NOFSIZE) xerror(NO, YES, YES,
287 "%s: invalid ixseek value", s);
289 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
291 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
293 iseek_blocks = tfc_humanfsize(s, &stoi);
294 if (!str_empty(stoi)) {
295 iseek_blocks = tfc_fnamesize(s, YES);
296 if (iseek_blocks == NOFSIZE)
298 "%s: invalid ictr value", s);
299 iseek_blocks /= TF_BLOCK_SIZE;
300 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
302 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
304 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
306 iseek_blocks = tfc_humanfsize(s, &stoi);
307 if (!str_empty(stoi)) {
308 iseek_blocks = tfc_fnamesize(s, YES);
309 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
310 if (iseek_blocks == NOFSIZE)
312 "%s: invalid ixctr value", s);
314 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
315 if (iseek_blocks % TF_BLOCK_SIZE)
317 "%s: not round to TF block size "
318 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
319 iseek_blocks /= TF_BLOCK_SIZE;
321 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
323 oseek = tfc_humanfsize(s, &stoi);
324 if (!str_empty(stoi)) {
325 oseek = tfc_fnamesize(s, YES);
326 oseek = tfc_modifysize(oseek, strchr(s, ':'));
327 if (oseek == NOFSIZE) xerror(NO, YES, YES,
328 "%s: invalid oseek value", s);
330 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
332 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
334 maxlen = tfc_humanfsize(s, &stoi);
335 if (!str_empty(stoi)) {
336 maxlen = tfc_fnamesize(s, YES);
337 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
338 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
339 "%s: invalid count value", s);
341 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
342 if (counter_opt == TFC_CTR_HEAD)
343 maxlen += TF_BLOCK_SIZE;
345 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
347 maxkeylen = tfc_humanfsize(s, &stoi);
348 if (!str_empty(stoi)) {
349 maxkeylen = tfc_fnamesize(s, YES);
350 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
351 if (maxkeylen == NOSIZE)
353 "%s: invalid key length value", s);
355 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
357 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
359 keyoffset = tfc_humanfsize(s, &stoi);
360 if (!str_empty(stoi)) {
361 keyoffset = tfc_fnamesize(s, YES);
362 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
363 if (keyoffset == NOFSIZE)
365 "%s: invalid key offset value", s);
367 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
369 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
371 ctrsz = tfc_humanfsize(s, &stoi);
372 if (!str_empty(stoi)) {
373 ctrsz = (size_t)tfc_fnamesize(s, YES);
374 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
377 "%s: invalid counter length value", s);
379 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
380 if (ctrsz > TF_BLOCK_SIZE)
381 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
383 else xerror(NO, YES, YES, "invalid option %s", s);
387 do_mac = TFC_MAC_SIGN;
388 if (strcasecmp(optarg, "mac") != 0)
389 do_mac_file = optarg;
392 do_mac = TFC_MAC_VRFY;
393 if (!strcasecmp(optarg, "drop"))
394 do_mac = TFC_MAC_DROP;
395 else if (strcasecmp(optarg, "mac") != 0)
396 do_mac_file = optarg;
399 if (do_mac != TFC_MAC_VRFY)
400 xerror(NO, YES, YES, "signature source was not specified");
401 do_mac = TFC_MAC_JUST_VRFY;
405 if (maxlen != NOFSIZE) {
406 if (c == 'Z') genzero_nr_bytes = maxlen;
407 else genrandom_nr_bytes = maxlen;
411 if (!strcasecmp(optarg, "cbs"))
413 else if (!strcasecmp(optarg, "ks"))
414 t = TF_FROM_BITS(TFC_KEY_BITS);
415 else if (!strcasecmp(optarg, "xks"))
416 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
417 else if (!strcasecmp(optarg, "iobs"))
420 t = tfc_humanfsize(optarg, &stoi);
421 if (!str_empty(stoi)) {
422 t = tfc_fnamesize(optarg, NO);
423 t = tfc_modifysize(t, strchr(optarg, ':'));
425 else t = tfc_modifysize(t, strchr(optarg, ':'));
427 if (c == 'Z') genzero_nr_bytes = maxlen = t;
428 else genrandom_nr_bytes = maxlen = t;
432 do_preserve_time = YES;
435 do_outfmt = TFC_OUTFMT_B64;
438 do_outfmt = TFC_OUTFMT_RAW;
441 do_outfmt = TFC_OUTFMT_HEX;
452 td = strtod(optarg, &stoi);
453 status_timer = TFC_DTOUSECS(td);
454 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
462 if (!strcmp(progname, "tfbench")) {
463 if (!*(argv+optind)) usage();
465 td = strtod(*(argv+optind), &stoi);
466 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
468 "%s: invalid benchmark time in seconds", *(argv+optind));
469 bench_timer = TFC_DTOUSECS(td);
470 do_benchmark(bench_timer, td);
472 if (genrandom_nr_bytes) {
473 ctr_mode = TFC_MODE_STREAM;
474 do_edcrypt = TFC_DO_ENCRYPT;
475 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
477 if (genzero_nr_bytes) {
478 ctr_mode = TFC_MODE_PLAIN;
479 do_edcrypt = TFC_DO_PLAIN;
480 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
483 if (rawkey && password)
484 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
485 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
486 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
487 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
488 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
489 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
490 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
491 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
492 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
493 if (overwrite_source && counter_opt == TFC_CTR_RAND)
494 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
495 if (ctr_mode == TFC_MODE_PLAIN
496 && (do_edcrypt || do_mac || rawkey
497 || mackey_opt || counter_opt || counter_file))
498 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
506 memset(tfc_salt, 0, TFC_MAX_SALT);
508 if (!strcasecmp(saltf, "disable")) goto _nosalt;
510 if (!strcmp(saltf, "-")) saltfd = 0;
511 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
512 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
513 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
514 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
520 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
524 if (!strcmp(mackeyf, "-")) mkfd = 0;
525 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
526 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
528 skein_init(&sk, TFC_KEY_BITS);
535 lrem = lblock = sizeof(tmpdata);
536 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
537 _mkragain: lio = xread(mkfd, pblk, lrem);
538 if (lio == 0) do_stop = YES;
539 if (lio != NOSIZE) ldone += lio;
541 if (errno != EIO && catch_all_errors != YES)
542 xerror(NO, NO, NO, "%s", mackeyf);
543 switch (error_action) {
544 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
545 case TFC_ERRACT_SYNC:
546 case TFC_ERRACT_LSYNC:
547 xerror(YES, NO, NO, "%s", mackeyf);
548 lio = ldone = lrem = lblock;
549 memset(tmpdata, 0, lio);
550 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
551 else lseek(mkfd, rdpos + lio, SEEK_SET);
553 default: xerror(NO, NO, NO, "%s", mackeyf); break;
556 if (lio && lio < lrem) {
562 skein_update(&sk, tmpdata, ldone);
565 skein_final(mackey, &sk);
569 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
570 memset(&getps, 0, sizeof(struct getpasswd_state));
571 getps.fd = getps.efd = -1;
572 getps.passwd = pwdask;
573 getps.pwlen = sizeof(pwdask)-1;
574 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
575 getps.charfilter = getps_filter;
576 getps.maskchar = 'x';
577 getps.flags = GETP_WAITFILL;
578 n = xgetpasswd(&getps);
579 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
580 if (n == ((size_t)-2)) xexit(1);
581 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
583 skein(tmpdata, TF_MAX_BITS, NULL, mackey, TF_FROM_BITS(TF_MAX_BITS));
584 xor_shrink(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, tmpdata, TF_FROM_BITS(TF_MAX_BITS));
585 tfc_nfsay(stderr, "MAC password hint: ");
586 mehexdump(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, TF_SIZE_UNIT, 1);
587 memset(tmpdata, 0, sizeof(tmpdata));
592 if ((strlen(progname) <= 9)
593 && ((!strcmp(progname, "sksum"))
594 || ((!memcmp(progname, "sk", 2))
595 && (!memcmp(progname+3, "sum", 3)
596 || !memcmp(progname+4, "sum", 3)
597 || !memcmp(progname+5, "sum", 3)
598 || !memcmp(progname+6, "sum", 3)))))
599 do_sksum(progname, argv+optind);
600 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
605 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
606 if (!strcmp(argv[idx], "-")) kfd = 0;
607 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
608 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
610 lio = strnlen(argv[idx], PATH_MAX);
611 memset(argv[idx], '*', lio);
618 if (do_full_key == NO && tweakf) {
621 if (!strcmp(tweakf, "-")) twfd = 0;
622 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
623 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
624 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
625 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
626 if (ldone < TF_TWEAK_SIZE)
627 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
634 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
636 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
637 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
638 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
640 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
642 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
643 if (verbose) tfc_esay("%s: disabling signature verification on "
644 "requested partial decryption.", progname);
648 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
649 maxlen = tfc_fdsize(sfd);
650 if (maxlen == NOFSIZE)
652 "Cannot verify embedded MAC with non-seekable source!");
653 maxlen -= TF_FROM_BITS(macbits);
655 srcfname = argv[idx];
659 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
660 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
662 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
663 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
665 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
670 if (!strcmp(counter_file, "-")) ctrfd = 0;
671 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
672 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
673 lio = xread(ctrfd, ctr, ctrsz);
674 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
675 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
678 else if (counter_opt == TFC_CTR_HEAD) {
681 lrem = lblock = ctrsz;
682 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
683 _ctrragain: lio = xread(sfd, pblk, lrem);
684 if (lio != NOSIZE) ldone += lio;
686 if (errno != EIO && catch_all_errors != YES)
687 xerror(NO, NO, NO, "%s", srcfname);
688 switch (error_action) {
689 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
690 case TFC_ERRACT_SYNC:
691 case TFC_ERRACT_LSYNC:
692 xerror(YES, NO, NO, "%s", srcfname);
693 lio = ldone = lrem = lblock;
695 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
696 else lseek(sfd, rdpos + lio, SEEK_SET);
698 default: xerror(NO, NO, NO, "%s", srcfname); break;
701 if (lio && lio < lrem) {
706 total_processed_src += ldone;
711 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
713 if (lseek(sfd, iseek, SEEK_SET) == -1)
714 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
717 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
719 if (verbose) tfc_esay("%s: hashing password", progname);
721 if (rawkey == TFC_RAWKEY_KEYFILE) {
722 tfc_yesno xtskeyset = NO;
726 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
727 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
728 _keyragain: lio = xread(kfd, pblk, lrem);
729 if (lio != NOSIZE) ldone += lio;
731 if (errno != EIO && catch_all_errors != YES)
732 xerror(NO, NO, NO, "reading key");
733 switch (error_action) {
734 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
735 case TFC_ERRACT_SYNC:
736 case TFC_ERRACT_LSYNC:
737 xerror(YES, NO, NO, "reading key");
738 lio = ldone = lrem = lblock;
740 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
741 else lseek(kfd, rdpos + lio, SEEK_SET);
743 default: xerror(NO, NO, NO, "reading key"); break;
746 if (lio && lio < lrem) {
751 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
753 if (ctr_mode == TFC_MODE_XTS) {
754 if (xtskeyset == NO) {
761 else if (rawkey == TFC_RAWKEY_ASKSTR) {
762 tfc_yesno xtskeyset = NO;
764 pblk = key; n = sizeof(key);
765 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
766 getps.fd = getps.efd = -1;
767 getps.passwd = (char *)pblk;
769 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
770 getps.charfilter = getps_filter;
771 getps.maskchar = 'x';
772 getps.flags = GETP_WAITFILL;
773 n = xgetpasswd(&getps);
774 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
775 if (n == ((size_t)-2)) xexit(1);
776 if (ctr_mode == TFC_MODE_XTS) {
777 if (xtskeyset == NO) {
778 pblk = xtskey; n = sizeof(xtskey);
784 else if (rawkey == TFC_RAWKEY_ASKHEX) {
785 tfc_yesno xtskeyset = NO;
789 memset(&getps, 0, sizeof(struct getpasswd_state));
790 getps.fd = getps.efd = -1;
791 getps.passwd = pwdask;
792 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
793 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
794 getps.charfilter = getps_hex_filter;
795 getps.maskchar = 'x';
796 getps.flags = GETP_WAITFILL;
797 n = xgetpasswd(&getps);
798 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
799 if (n == ((size_t)-2)) xexit(1);
801 tfc_esay("Please input even number of hex digits!");
802 goto _rawkey_hex_again;
804 hex2bin(pblk, pwdask);
805 memset(pwdask, 0, sizeof(pwdask));
806 if (ctr_mode == TFC_MODE_XTS) {
807 if (xtskeyset == NO) {
810 goto _rawkey_hex_again;
815 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
816 getps.fd = getps.efd = -1;
817 getps.passwd = pwdask;
818 getps.pwlen = sizeof(pwdask)-1;
819 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
820 getps.charfilter = getps_filter;
821 getps.maskchar = 'x';
822 getps.flags = GETP_WAITFILL;
823 n = xgetpasswd(&getps);
824 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
825 if (n == ((size_t)-2)) xexit(1);
826 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
827 getps.fd = getps.efd = -1;
828 getps.passwd = pwdagain;
829 getps.pwlen = sizeof(pwdagain)-1;
830 getps.echo = "Enter it again: ";
831 getps.flags = GETP_WAITFILL;
832 n = xgetpasswd(&getps);
833 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
834 if (n == ((size_t)-2)) xexit(1);
835 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
836 tfc_esay("Passwords are different, try again");
840 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
841 memset(pwdask, 0, sizeof(pwdask));
842 memset(pwdagain, 0, sizeof(pwdagain));
845 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
846 xerror(NO, NO, YES, "hashing key");
850 if (tfc_saltsz > 0) {
851 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
852 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
854 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
855 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
856 memset(tfc_salt, 0, TFC_MAX_SALT);
859 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
860 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
865 tfc_yesno xtskeyset = NO;
868 if (!strcmp(genkeyf, "-")) krfd = 1;
869 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
870 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
871 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
872 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
873 if (verbose && xtskeyset == NO) {
874 tfc_esay("%s: password hashing done", progname);
875 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
876 tfc_esay("%s: Have a nice day!", progname);
879 if (ctr_mode == TFC_MODE_XTS) {
880 if (xtskeyset == NO) {
892 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
893 if (verbose) tfc_esay("%s: disabling signature verification on "
894 "requested partial decryption.", progname);
899 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
900 if (ctr_mode < TFC_MODE_OCB) {
901 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
902 "will be slower.", progname);
903 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
904 else skein_init(&sk, macbits);
908 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
909 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
913 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
914 if (do_full_key == NO) {
915 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
916 tf_tweak_set(key, tweak);
918 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
919 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
920 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
922 switch (counter_opt) {
925 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
926 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
927 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
930 case TFC_CTR_RAND: tfc_getrandom(ctr, ctrsz); break;
931 case TFC_CTR_ZERO: memset(ctr, 0, ctrsz); break;
939 if (verbose) tfc_esay("%s: password hashing done", progname);
941 if (overwrite_source && srcfname) argv[idx] = srcfname;
945 if (!strcmp(argv[idx], "-")) dfd = 1;
946 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
948 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
949 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
951 dstfname = argv[idx];
956 if (lseek(dfd, oseek, SEEK_SET) == -1)
957 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
960 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
961 memset(&sigact, 0, sizeof(sigact));
962 sigact.sa_flags = SA_RESTART;
963 sigact.sa_handler = print_crypt_status;
964 sigaction(SIGUSR1, &sigact, NULL);
965 sigaction(SIGTSTP, &sigact, NULL);
966 sigaction(SIGALRM, &sigact, NULL);
967 if (status_timer) setup_next_alarm(status_timer);
968 sigact.sa_handler = change_status_width;
969 sigaction(SIGQUIT, &sigact, NULL);
970 sigact.sa_handler = change_status_timer;
971 sigaction(SIGUSR2, &sigact, NULL);
973 sigact.sa_handler = print_crypt_status;
974 sigaction(SIGINT, &sigact, NULL);
975 sigaction(SIGTERM, &sigact, NULL);
978 sigact.sa_handler = exit_sigterm;
979 sigaction(SIGINT, &sigact, NULL);
980 sigaction(SIGTERM, &sigact, NULL);
982 memset(&sigact, 0, sizeof(struct sigaction));
984 tfc_getcurtime(&delta_time);
987 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
991 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
992 if (lio != NOSIZE) ldone += lio;
993 else xerror(NO, NO, NO, "%s", dstfname);
994 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1000 total_processed_dst += ldone;
1001 delta_processed += ldone;
1004 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1012 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1013 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1014 _ragain: lio = xread(sfd, pblk, lrem);
1015 if (lio == 0) do_stop = TFC_STOP_BEGAN;
1016 if (lio != NOSIZE) ldone += lio;
1018 if (errno != EIO && catch_all_errors != YES)
1019 xerror(NO, NO, NO, "%s", srcfname);
1020 switch (error_action) {
1021 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1022 case TFC_ERRACT_SYNC:
1023 case TFC_ERRACT_LSYNC:
1024 xerror(YES, NO, NO, "%s", srcfname);
1025 lio = ldone = lrem = lblock;
1026 memset(srcblk, 0, lio);
1027 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1028 else lseek(sfd, rdpos + lio, SEEK_SET);
1030 default: xerror(NO, NO, NO, "%s", srcfname); break;
1033 if (lio && lio < lrem) {
1038 total_processed_src += ldone;
1040 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1041 size_t orig = ldone;
1042 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1043 if (ldone > blksize) ldone = blksize;
1044 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1047 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1048 skein_update(&sk, srcblk, ldone);
1050 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1051 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1052 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1053 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1054 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1055 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1056 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1057 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1058 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1059 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1060 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1061 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1062 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1063 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1065 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1066 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1067 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1068 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1070 else if (ctr_mode == TFC_MODE_PLAIN)
1071 memcpy(dstblk, srcblk, ldone);
1073 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1074 skein_update(&sk, dstblk, ldone);
1075 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1080 _wagain: lio = xwrite(dfd, pblk, lrem);
1081 if (lio != NOSIZE) ldone += lio;
1082 else xerror(NO, NO, NO, "%s", dstfname);
1083 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1089 _nowrite: total_processed_dst += ldone;
1090 delta_processed += ldone;
1092 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1095 if (do_stop == TFC_STOP_FULL) goto _nomac;
1098 if (do_mac >= TFC_MAC_VRFY) {
1102 lrem = lblock = TF_FROM_BITS(macbits);
1103 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1104 _macragain: lio = xread(sfd, pblk, lrem);
1105 if (lio != NOSIZE) ldone += lio;
1107 if (errno != EIO && catch_all_errors != YES)
1108 xerror(NO, NO, NO, "%s", srcfname);
1109 switch (error_action) {
1110 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1111 case TFC_ERRACT_SYNC:
1112 case TFC_ERRACT_LSYNC:
1113 xerror(YES, NO, NO, "%s", srcfname);
1114 lio = ldone = lrem = lblock;
1115 memset(macvrfy, 0, lio);
1116 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1117 else lseek(sfd, rdpos + lio, SEEK_SET);
1119 default: xerror(NO, NO, NO, "%s", srcfname); break;
1122 if (lio && lio < lrem) {
1127 total_processed_src += ldone;
1132 if (!strcmp(do_mac_file, "-")) mfd = 0;
1133 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1134 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1135 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1136 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1137 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1138 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1139 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1140 lio = TF_FROM_BITS(macbits);
1141 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1143 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1147 if (ldone < TF_FROM_BITS(macbits)) {
1148 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1149 "not verifying", progname, ldone);
1154 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1155 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1157 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1158 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1159 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1160 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1161 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1162 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1164 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1166 tfc_esay("%s: signature is good", progname);
1168 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1169 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1174 if (quiet == NO) tfc_esay("%s: signature is BAD: "
1175 "wrong password, key, mode, or file is not signed", progname);
1179 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1180 memset(macresult, 0, sizeof(macresult));
1181 memset(tmpdata, 0, sizeof(tmpdata));
1184 else if (do_mac == TFC_MAC_SIGN) {
1185 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1186 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1188 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1189 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1190 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1191 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1192 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1193 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1194 memset(macresult, 0, sizeof(macresult));
1198 lio = lrem = TF_FROM_BITS(macbits);
1200 _macwagain: lio = xwrite(dfd, pblk, lrem);
1201 if (lio != NOSIZE) ldone += lio;
1202 else xerror(NO, NO, NO, "%s", dstfname);
1203 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1209 total_processed_dst += ldone;
1210 delta_processed += ldone;
1215 if (!strcmp(do_mac_file, "-")) mfd = 1;
1216 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1217 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1218 if (do_outfmt == TFC_OUTFMT_B64) {
1219 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1220 memset(tmpdata, 0, TFC_TMPSIZE);
1221 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1222 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1223 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1225 tmpdata[lrem] = '\n';
1228 lio = xwrite(mfd, tmpdata, lrem);
1230 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1231 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1232 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1236 memset(macvrfy, 0, sizeof(macvrfy));
1237 memset(macresult, 0, sizeof(macresult));
1238 memset(tmpdata, 0, sizeof(tmpdata));
1242 if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1244 if (do_preserve_time) fcopy_matime(dfd, &s_stat);