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:TPkzxc: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 counter_file = sksum_hashlist_file = optarg;
97 if (!strcasecmp(optarg, "ctr"))
98 ctr_mode = TFC_MODE_CTR;
99 else if (!strcasecmp(optarg, "stream"))
100 ctr_mode = TFC_MODE_STREAM;
101 else if (!strcasecmp(optarg, "cbc"))
102 ctr_mode = TFC_MODE_CBC;
103 else if (!strcasecmp(optarg, "ecb"))
104 ctr_mode = TFC_MODE_ECB;
105 else if (!strcasecmp(optarg, "xts"))
106 ctr_mode = TFC_MODE_XTS;
107 else if (!strcasecmp(optarg, "ocb"))
108 ctr_mode = TFC_MODE_OCB;
109 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
112 do_edcrypt = TFC_DO_PLAIN;
114 ctr_mode = TFC_MODE_PLAIN;
117 do_edcrypt = TFC_DO_ENCRYPT;
120 do_edcrypt = TFC_DO_DECRYPT;
123 macbits = strtoul(optarg, &stoi, 10);
124 if (macbits == 0 || !str_empty(stoi) || macbits < 8
125 || macbits > TF_MAX_BITS || macbits % 8)
126 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
129 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
130 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
133 if (!strcasecmp(optarg, "key"))
134 mackey_opt = TFC_MACKEY_RAWKEY;
135 else if (!strcasecmp(optarg, "pwd"))
136 mackey_opt = TFC_MACKEY_PASSWORD;
138 mackey_opt = TFC_MACKEY_FILE;
146 rawkey = TFC_RAWKEY_KEYFILE;
149 rawkey = TFC_RAWKEY_ASKSTR;
152 rawkey = TFC_RAWKEY_ASKHEX;
166 if (maxlen != NOFSIZE) break;
168 maxlen = tfc_humanfsize(optarg, &stoi);
169 if (!str_empty(stoi)) {
170 maxlen = tfc_fnamesize(optarg, YES);
171 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
172 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
173 "%s: invalid count value", optarg);
175 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
176 if (counter_opt == TFC_CTR_HEAD)
177 maxlen += TF_BLOCK_SIZE;
180 overwrite_source = YES;
183 if (!strcmp(optarg, "xall")) {
184 catch_all_errors = YES;
187 if (!strcmp(optarg, "xseek")) {
188 ignore_seek_errors = YES;
191 if (!strcmp(optarg, "exit"))
192 error_action = TFC_ERRACT_EXIT;
193 else if (!strncmp(optarg, "cont", 4))
194 error_action = TFC_ERRACT_CONT;
195 else if (!strcmp(optarg, "sync"))
196 error_action = TFC_ERRACT_SYNC;
197 else if (!strcmp(optarg, "lsync"))
198 error_action = TFC_ERRACT_LSYNC;
199 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
202 s = d = optarg; t = NULL;
203 while ((s = strtok_r(d, ",", &t))) {
205 if (!strcmp(s, "sync"))
206 write_flags |= O_SYNC;
207 else if (!strcmp(s, "trunc"))
208 write_flags |= O_TRUNC;
209 else if (!strcmp(s, "fsync"))
211 else if (!strcmp(s, "pad"))
213 else if (!strcmp(s, "xtime"))
214 do_preserve_time = YES;
215 else if (!strcmp(s, "gibsize"))
216 do_stats_in_gibs = YES;
217 else if (!strcmp(s, "plainstats"))
218 do_statline_dynamic = NO;
219 else if (!strcmp(s, "statless"))
221 else if (!strcmp(s, "norepeat"))
223 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
225 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
226 mac_pw_prompt = s+10;
227 else if (!strcmp(s, "shorthex"))
228 do_full_hexdump = NO;
229 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
231 blksize = (size_t)tfc_humanfsize(s, &stoi);
232 if (!str_empty(stoi)) {
233 blksize = (size_t)tfc_fnamesize(s, YES);
234 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
235 if (blksize == NOSIZE) xerror(NO, YES, YES,
236 "%s: invalid block size value", s);
238 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
239 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
240 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
241 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
242 "%s: block size exceeds %u bytes",
243 s, TFC_U(TFC_BLKSIZE));
245 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
247 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
248 if (!str_empty(stoi)) {
249 xtsblocks = (size_t)tfc_fnamesize(s, YES);
250 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
251 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
252 "%s: invalid blocks per xts block value", s);
254 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
255 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
256 "%s: nr of blocks per xts block is not round to %u bytes",
257 s, TFC_U(TFC_BLKSIZE));
258 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
259 "%s: nr of blocks per xts block exceeds %u bytes",
260 s, TFC_U(TFC_BLKSIZE));
262 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
264 iseek = tfc_humanfsize(s, &stoi);
265 if (!str_empty(stoi)) {
266 iseek = tfc_fnamesize(s, YES);
267 iseek = tfc_modifysize(iseek, strchr(s, ':'));
268 if (iseek == NOFSIZE) xerror(NO, YES, YES,
269 "%s: invalid iseek value", s);
271 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
272 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
274 "%s: not round to TF block size "
276 s, TFC_U(TF_BLOCK_SIZE));
277 iseek_blocks = iseek / TF_BLOCK_SIZE;
279 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
281 iseek = tfc_humanfsize(s, &stoi);
282 if (!str_empty(stoi)) {
283 iseek = tfc_fnamesize(s, YES);
284 iseek = tfc_modifysize(iseek, strchr(s, ':'));
285 if (iseek == NOFSIZE) xerror(NO, YES, YES,
286 "%s: invalid ixseek value", s);
288 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
290 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
292 iseek_blocks = tfc_humanfsize(s, &stoi);
293 if (!str_empty(stoi)) {
294 iseek_blocks = tfc_fnamesize(s, YES);
295 if (iseek_blocks == NOFSIZE)
297 "%s: invalid ictr value", s);
298 iseek_blocks /= TF_BLOCK_SIZE;
299 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
301 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
303 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
305 iseek_blocks = tfc_humanfsize(s, &stoi);
306 if (!str_empty(stoi)) {
307 iseek_blocks = tfc_fnamesize(s, YES);
308 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
309 if (iseek_blocks == NOFSIZE)
311 "%s: invalid ixctr value", s);
313 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
314 if (iseek_blocks % TF_BLOCK_SIZE)
316 "%s: not round to TF block size "
317 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
318 iseek_blocks /= TF_BLOCK_SIZE;
320 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
322 oseek = tfc_humanfsize(s, &stoi);
323 if (!str_empty(stoi)) {
324 oseek = tfc_fnamesize(s, YES);
325 oseek = tfc_modifysize(oseek, strchr(s, ':'));
326 if (oseek == NOFSIZE) xerror(NO, YES, YES,
327 "%s: invalid oseek value", s);
329 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
331 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
333 maxlen = tfc_humanfsize(s, &stoi);
334 if (!str_empty(stoi)) {
335 maxlen = tfc_fnamesize(s, YES);
336 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
337 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
338 "%s: invalid count value", s);
340 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
341 if (counter_opt == TFC_CTR_HEAD)
342 maxlen += TF_BLOCK_SIZE;
344 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
346 maxkeylen = tfc_humanfsize(s, &stoi);
347 if (!str_empty(stoi)) {
348 maxkeylen = tfc_fnamesize(s, YES);
349 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
350 if (maxkeylen == NOSIZE)
352 "%s: invalid key length value", s);
354 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
356 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
358 keyoffset = tfc_humanfsize(s, &stoi);
359 if (!str_empty(stoi)) {
360 keyoffset = tfc_fnamesize(s, YES);
361 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
362 if (keyoffset == NOFSIZE)
364 "%s: invalid key offset value", s);
366 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
368 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
370 ctrsz = tfc_humanfsize(s, &stoi);
371 if (!str_empty(stoi)) {
372 ctrsz = (size_t)tfc_fnamesize(s, YES);
373 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
376 "%s: invalid counter length value", s);
378 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
379 if (ctrsz > TF_BLOCK_SIZE)
380 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
382 else xerror(NO, YES, YES, "invalid option %s", s);
386 do_mac = TFC_MAC_SIGN;
387 if (strcasecmp(optarg, "mac") != 0)
388 do_mac_file = optarg;
391 do_mac = TFC_MAC_VRFY;
392 if (!strcasecmp(optarg, "drop"))
393 do_mac = TFC_MAC_DROP;
394 else if (strcasecmp(optarg, "mac") != 0)
395 do_mac_file = optarg;
398 if (do_mac != TFC_MAC_VRFY)
399 xerror(NO, YES, YES, "signature source was not specified");
400 do_mac = TFC_MAC_JUST_VRFY;
404 if (maxlen != NOFSIZE) {
405 if (c == 'Z') genzero_nr_bytes = maxlen;
406 else genrandom_nr_bytes = maxlen;
410 if (!strcasecmp(optarg, "cbs"))
412 else if (!strcasecmp(optarg, "ks"))
413 t = TF_FROM_BITS(TFC_KEY_BITS);
414 else if (!strcasecmp(optarg, "xks"))
415 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
416 else if (!strcasecmp(optarg, "iobs"))
419 t = tfc_humanfsize(optarg, &stoi);
420 if (!str_empty(stoi)) {
421 t = tfc_fnamesize(optarg, NO);
422 t = tfc_modifysize(t, strchr(optarg, ':'));
424 else t = tfc_modifysize(t, strchr(optarg, ':'));
426 if (c == 'Z') genzero_nr_bytes = maxlen = t;
427 else genrandom_nr_bytes = maxlen = t;
431 do_preserve_time = YES;
434 do_outfmt = TFC_OUTFMT_B64;
437 do_outfmt = TFC_OUTFMT_RAW;
440 do_outfmt = TFC_OUTFMT_HEX;
451 td = strtod(optarg, &stoi);
452 status_timer = TFC_DTOUSECS(td);
453 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
461 if (!strcmp(progname, "tfbench")) {
462 if (!*(argv+optind)) usage();
464 td = strtod(*(argv+optind), &stoi);
465 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
467 "%s: invalid benchmark time in seconds", *(argv+optind));
468 bench_timer = TFC_DTOUSECS(td);
469 do_benchmark(bench_timer, td);
471 if (genrandom_nr_bytes) {
472 ctr_mode = TFC_MODE_STREAM;
473 do_edcrypt = TFC_DO_ENCRYPT;
474 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
476 if (genzero_nr_bytes) {
477 ctr_mode = TFC_MODE_PLAIN;
478 do_edcrypt = TFC_DO_PLAIN;
479 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
482 if (rawkey && password)
483 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
484 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
485 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
486 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
487 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
488 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
489 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
490 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
491 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
492 if (overwrite_source && counter_opt == TFC_CTR_RAND)
493 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
494 if (tweakf && do_tfcrypt1 == NO)
495 xerror(NO, YES, YES, "Use -T with -t tweakfile to enable old tfcrypt mode!");
496 if (ctr_mode == TFC_MODE_PLAIN
497 && (do_edcrypt || do_mac || rawkey
498 || mackey_opt || counter_opt || counter_file))
499 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
507 memset(tfc_salt, 0, TFC_MAX_SALT);
509 if (!strcasecmp(saltf, "disable")) goto _nosalt;
511 if (!strcmp(saltf, "-")) saltfd = 0;
512 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
513 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
514 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
515 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
521 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
525 if (!strcmp(mackeyf, "-")) mkfd = 0;
526 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
527 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
529 skein_init(&sk, TFC_KEY_BITS);
536 lrem = lblock = sizeof(tmpdata);
537 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
538 _mkragain: lio = xread(mkfd, pblk, lrem);
539 if (lio == 0) do_stop = YES;
540 if (lio != NOSIZE) ldone += lio;
542 if (errno != EIO && catch_all_errors != YES)
543 xerror(NO, NO, NO, "%s", mackeyf);
544 switch (error_action) {
545 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
546 case TFC_ERRACT_SYNC:
547 case TFC_ERRACT_LSYNC:
548 xerror(YES, NO, NO, "%s", mackeyf);
549 lio = ldone = lrem = lblock;
550 memset(tmpdata, 0, lio);
551 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
552 else lseek(mkfd, rdpos + lio, SEEK_SET);
554 default: xerror(NO, NO, NO, "%s", mackeyf); break;
557 if (lio && lio < lrem) {
563 skein_update(&sk, tmpdata, ldone);
566 skein_final(mackey, &sk);
570 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
571 memset(&getps, 0, sizeof(struct getpasswd_state));
572 getps.fd = getps.efd = -1;
573 getps.passwd = pwdask;
574 getps.pwlen = sizeof(pwdask)-1;
575 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
576 getps.charfilter = getps_filter;
577 getps.maskchar = 'x';
578 getps.flags = GETP_WAITFILL;
579 n = xgetpasswd(&getps);
580 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
581 if (n == ((size_t)-2)) xexit(1);
582 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
584 skein(tmpdata, TF_MAX_BITS, NULL, mackey, TF_FROM_BITS(TF_MAX_BITS));
585 xor_shrink(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, tmpdata, TF_FROM_BITS(TF_MAX_BITS));
586 tfc_nfsay(stderr, "MAC password hint: ");
587 mehexdump(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, TF_SIZE_UNIT, 1);
588 memset(tmpdata, 0, sizeof(tmpdata));
593 if ((strlen(progname) <= 9)
594 && ((!strcmp(progname, "sksum"))
595 || ((!memcmp(progname, "sk", 2))
596 && (!memcmp(progname+3, "sum", 3)
597 || !memcmp(progname+4, "sum", 3)
598 || !memcmp(progname+5, "sum", 3)
599 || !memcmp(progname+6, "sum", 3)))))
600 do_sksum(progname, argv+optind);
601 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
606 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
607 if (!strcmp(argv[idx], "-")) kfd = 0;
608 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
609 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
611 lio = strnlen(argv[idx], PATH_MAX);
612 memset(argv[idx], '*', lio);
619 if (do_tfcrypt1 == YES && tweakf) {
622 if (!strcmp(tweakf, "-")) twfd = 0;
623 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
624 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
625 lio = ldone = xread(twfd, key+TF_FROM_BITS(TF_MAX_BITS)+TF_SIZE_UNIT, 2*TF_SIZE_UNIT);
626 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
627 if (ldone < 2*TF_SIZE_UNIT)
628 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, 2*TF_SIZE_UNIT);
635 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
637 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
638 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
639 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
641 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
643 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
644 if (verbose) tfc_esay("%s: disabling signature verification on "
645 "requested partial decryption.", progname);
649 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
650 maxlen = tfc_fdsize(sfd);
651 if (maxlen == NOFSIZE)
653 "Cannot verify embedded MAC with non-seekable source!");
654 maxlen -= TF_FROM_BITS(macbits);
656 srcfname = argv[idx];
660 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
661 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
663 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
664 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
666 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
671 if (!strcmp(counter_file, "-")) ctrfd = 0;
672 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
673 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
674 lio = xread(ctrfd, ctr, ctrsz);
675 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
676 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
679 else if (counter_opt == TFC_CTR_HEAD) {
682 lrem = lblock = ctrsz;
683 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
684 _ctrragain: lio = xread(sfd, pblk, lrem);
685 if (lio != NOSIZE) ldone += lio;
687 if (errno != EIO && catch_all_errors != YES)
688 xerror(NO, NO, NO, "%s", srcfname);
689 switch (error_action) {
690 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
691 case TFC_ERRACT_SYNC:
692 case TFC_ERRACT_LSYNC:
693 xerror(YES, NO, NO, "%s", srcfname);
694 lio = ldone = lrem = lblock;
696 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
697 else lseek(sfd, rdpos + lio, SEEK_SET);
699 default: xerror(NO, NO, NO, "%s", srcfname); break;
702 if (lio && lio < lrem) {
707 total_processed_src += ldone;
712 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
714 if (lseek(sfd, iseek, SEEK_SET) == -1)
715 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
718 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
720 if (verbose) tfc_esay("%s: hashing password", progname);
722 if (rawkey == TFC_RAWKEY_KEYFILE) {
723 tfc_yesno xtskeyset = NO;
727 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
728 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
729 _keyragain: lio = xread(kfd, pblk, lrem);
730 if (lio != NOSIZE) ldone += lio;
732 if (errno != EIO && catch_all_errors != YES)
733 xerror(NO, NO, NO, "reading key");
734 switch (error_action) {
735 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
736 case TFC_ERRACT_SYNC:
737 case TFC_ERRACT_LSYNC:
738 xerror(YES, NO, NO, "reading key");
739 lio = ldone = lrem = lblock;
741 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
742 else lseek(kfd, rdpos + lio, SEEK_SET);
744 default: xerror(NO, NO, NO, "reading key"); break;
747 if (lio && lio < lrem) {
752 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
754 if (ctr_mode == TFC_MODE_XTS) {
755 if (xtskeyset == NO) {
762 else if (rawkey == TFC_RAWKEY_ASKSTR) {
763 tfc_yesno xtskeyset = NO;
765 pblk = key; n = sizeof(key);
766 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
767 getps.fd = getps.efd = -1;
768 getps.passwd = (char *)pblk;
770 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
771 getps.charfilter = getps_filter;
772 getps.maskchar = 'x';
773 getps.flags = GETP_WAITFILL;
774 n = xgetpasswd(&getps);
775 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
776 if (n == ((size_t)-2)) xexit(1);
777 if (ctr_mode == TFC_MODE_XTS) {
778 if (xtskeyset == NO) {
779 pblk = xtskey; n = sizeof(xtskey);
785 else if (rawkey == TFC_RAWKEY_ASKHEX) {
786 tfc_yesno xtskeyset = NO;
790 memset(&getps, 0, sizeof(struct getpasswd_state));
791 getps.fd = getps.efd = -1;
792 getps.passwd = pwdask;
793 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
794 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
795 getps.charfilter = getps_hex_filter;
796 getps.maskchar = 'x';
797 getps.flags = GETP_WAITFILL;
798 n = xgetpasswd(&getps);
799 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
800 if (n == ((size_t)-2)) xexit(1);
802 tfc_esay("Please input even number of hex digits!");
803 goto _rawkey_hex_again;
805 hex2bin(pblk, pwdask);
806 memset(pwdask, 0, sizeof(pwdask));
807 if (ctr_mode == TFC_MODE_XTS) {
808 if (xtskeyset == NO) {
811 goto _rawkey_hex_again;
816 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
817 getps.fd = getps.efd = -1;
818 getps.passwd = pwdask;
819 getps.pwlen = sizeof(pwdask)-1;
820 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
821 getps.charfilter = getps_filter;
822 getps.maskchar = 'x';
823 getps.flags = GETP_WAITFILL;
824 n = xgetpasswd(&getps);
825 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
826 if (n == ((size_t)-2)) xexit(1);
827 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
828 getps.fd = getps.efd = -1;
829 getps.passwd = pwdagain;
830 getps.pwlen = sizeof(pwdagain)-1;
831 getps.echo = "Enter it again: ";
832 getps.flags = GETP_WAITFILL;
833 n = xgetpasswd(&getps);
834 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
835 if (n == ((size_t)-2)) xexit(1);
836 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
837 tfc_esay("Passwords are different, try again");
841 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
842 memset(pwdask, 0, sizeof(pwdask));
843 memset(pwdagain, 0, sizeof(pwdagain));
846 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
847 xerror(NO, NO, YES, "hashing key");
851 if (tfc_saltsz > 0) {
852 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
853 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
855 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
856 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
857 memset(tfc_salt, 0, TFC_MAX_SALT);
860 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
861 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
866 tfc_yesno xtskeyset = NO;
869 if (!strcmp(genkeyf, "-")) krfd = 1;
870 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
871 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
872 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
873 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
874 if (verbose && xtskeyset == NO) {
875 tfc_esay("%s: password hashing done", progname);
876 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
877 tfc_esay("%s: Have a nice day!", progname);
880 if (ctr_mode == TFC_MODE_XTS) {
881 if (xtskeyset == NO) {
893 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
894 if (verbose) tfc_esay("%s: disabling signature verification on "
895 "requested partial decryption.", progname);
900 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
901 if (ctr_mode < TFC_MODE_OCB) {
902 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
903 "will be slower.", progname);
904 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
905 else skein_init(&sk, macbits);
909 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
910 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
914 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
915 if (do_tfcrypt1 == YES) {
916 if (!tweakf) skein(key+TF_FROM_BITS(TF_MAX_BITS)+TF_SIZE_UNIT, 2*TF_UNIT_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
917 tf_key_tweak_compat(key);
919 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
920 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
921 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
923 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
925 switch (counter_opt) {
928 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
929 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
930 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
933 case TFC_CTR_RAND: tfc_getrandom(ctr, ctrsz); break;
941 if (verbose) tfc_esay("%s: password hashing done", progname);
943 if (overwrite_source && srcfname) argv[idx] = srcfname;
947 if (!strcmp(argv[idx], "-")) dfd = 1;
948 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
950 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
951 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
953 dstfname = argv[idx];
958 if (lseek(dfd, oseek, SEEK_SET) == -1)
959 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
962 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
963 memset(&sigact, 0, sizeof(sigact));
964 sigact.sa_flags = SA_RESTART;
965 sigact.sa_handler = print_crypt_status;
966 sigaction(SIGUSR1, &sigact, NULL);
967 sigaction(SIGTSTP, &sigact, NULL);
968 sigaction(SIGALRM, &sigact, NULL);
969 if (status_timer) setup_next_alarm(status_timer);
970 sigact.sa_handler = change_status_width;
971 sigaction(SIGQUIT, &sigact, NULL);
972 sigact.sa_handler = change_status_timer;
973 sigaction(SIGUSR2, &sigact, NULL);
975 sigact.sa_handler = print_crypt_status;
976 sigaction(SIGINT, &sigact, NULL);
977 sigaction(SIGTERM, &sigact, NULL);
980 sigact.sa_handler = exit_sigterm;
981 sigaction(SIGINT, &sigact, NULL);
982 sigaction(SIGTERM, &sigact, NULL);
984 memset(&sigact, 0, sizeof(struct sigaction));
986 tfc_getcurtime(&delta_time);
989 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
993 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
994 if (lio != NOSIZE) ldone += lio;
995 else xerror(NO, NO, NO, "%s", dstfname);
996 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1002 total_processed_dst += ldone;
1003 delta_processed += ldone;
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);