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 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;
168 if (maxlen != NOFSIZE) break;
170 maxlen = tfc_humanfsize(optarg, &stoi);
171 if (!str_empty(stoi)) {
172 maxlen = tfc_fnamesize(optarg, YES);
173 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
174 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
175 "%s: invalid count value", optarg);
177 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
178 if (counter_opt == TFC_CTR_HEAD)
179 maxlen += TF_BLOCK_SIZE;
182 overwrite_source = YES;
185 if (!strcmp(optarg, "xall")) {
186 catch_all_errors = YES;
189 if (!strcmp(optarg, "xseek")) {
190 ignore_seek_errors = YES;
193 if (!strcmp(optarg, "exit"))
194 error_action = TFC_ERRACT_EXIT;
195 else if (!strncmp(optarg, "cont", 4))
196 error_action = TFC_ERRACT_CONT;
197 else if (!strcmp(optarg, "sync"))
198 error_action = TFC_ERRACT_SYNC;
199 else if (!strcmp(optarg, "lsync"))
200 error_action = TFC_ERRACT_LSYNC;
201 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
204 s = d = optarg; t = NULL;
205 while ((s = strtok_r(d, ",", &t))) {
207 if (!strcmp(s, "sync"))
208 write_flags |= O_SYNC;
209 else if (!strcmp(s, "trunc"))
210 write_flags |= O_TRUNC;
211 else if (!strcmp(s, "fsync"))
213 else if (!strcmp(s, "pad"))
215 else if (!strcmp(s, "xtime"))
216 do_preserve_time = YES;
217 else if (!strcmp(s, "gibsize"))
218 do_stats_in_gibs = YES;
219 else if (!strcmp(s, "plainstats"))
220 do_statline_dynamic = NO;
221 else if (!strcmp(s, "statless"))
223 else if (!strcmp(s, "norepeat"))
225 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
227 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
228 mac_pw_prompt = s+10;
229 else if (!strcmp(s, "shorthex"))
230 do_full_hexdump = NO;
231 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
233 blksize = (size_t)tfc_humanfsize(s, &stoi);
234 if (!str_empty(stoi)) {
235 blksize = (size_t)tfc_fnamesize(s, YES);
236 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
237 if (blksize == NOSIZE) xerror(NO, YES, YES,
238 "%s: invalid block size value", s);
240 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
241 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
242 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
243 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
244 "%s: block size exceeds %u bytes",
245 s, TFC_U(TFC_BLKSIZE));
247 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
249 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
250 if (!str_empty(stoi)) {
251 xtsblocks = (size_t)tfc_fnamesize(s, YES);
252 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
253 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
254 "%s: invalid blocks per xts block value", s);
256 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
257 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
258 "%s: nr of blocks per xts block is not round to %u bytes",
259 s, TFC_U(TFC_BLKSIZE));
260 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
261 "%s: nr of blocks per xts block exceeds %u bytes",
262 s, TFC_U(TFC_BLKSIZE));
264 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
266 iseek = tfc_humanfsize(s, &stoi);
267 if (!str_empty(stoi)) {
268 iseek = tfc_fnamesize(s, YES);
269 iseek = tfc_modifysize(iseek, strchr(s, ':'));
270 if (iseek == NOFSIZE) xerror(NO, YES, YES,
271 "%s: invalid iseek value", s);
273 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
274 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
276 "%s: not round to TF block size "
278 s, TFC_U(TF_BLOCK_SIZE));
279 iseek_blocks = iseek / TF_BLOCK_SIZE;
281 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
283 iseek = tfc_humanfsize(s, &stoi);
284 if (!str_empty(stoi)) {
285 iseek = tfc_fnamesize(s, YES);
286 iseek = tfc_modifysize(iseek, strchr(s, ':'));
287 if (iseek == NOFSIZE) xerror(NO, YES, YES,
288 "%s: invalid ixseek value", s);
290 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
292 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
294 iseek_blocks = tfc_humanfsize(s, &stoi);
295 if (!str_empty(stoi)) {
296 iseek_blocks = tfc_fnamesize(s, YES);
297 if (iseek_blocks == NOFSIZE)
299 "%s: invalid ictr value", s);
300 iseek_blocks /= TF_BLOCK_SIZE;
301 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
303 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
305 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
307 iseek_blocks = tfc_humanfsize(s, &stoi);
308 if (!str_empty(stoi)) {
309 iseek_blocks = tfc_fnamesize(s, YES);
310 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
311 if (iseek_blocks == NOFSIZE)
313 "%s: invalid ixctr value", s);
315 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
316 if (iseek_blocks % TF_BLOCK_SIZE)
318 "%s: not round to TF block size "
319 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
320 iseek_blocks /= TF_BLOCK_SIZE;
322 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
324 oseek = tfc_humanfsize(s, &stoi);
325 if (!str_empty(stoi)) {
326 oseek = tfc_fnamesize(s, YES);
327 oseek = tfc_modifysize(oseek, strchr(s, ':'));
328 if (oseek == NOFSIZE) xerror(NO, YES, YES,
329 "%s: invalid oseek value", s);
331 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
333 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
335 maxlen = tfc_humanfsize(s, &stoi);
336 if (!str_empty(stoi)) {
337 maxlen = tfc_fnamesize(s, YES);
338 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
339 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
340 "%s: invalid count value", s);
342 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
343 if (counter_opt == TFC_CTR_HEAD)
344 maxlen += TF_BLOCK_SIZE;
346 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
348 maxkeylen = tfc_humanfsize(s, &stoi);
349 if (!str_empty(stoi)) {
350 maxkeylen = tfc_fnamesize(s, YES);
351 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
352 if (maxkeylen == NOSIZE)
354 "%s: invalid key length value", s);
356 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
358 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
360 keyoffset = tfc_humanfsize(s, &stoi);
361 if (!str_empty(stoi)) {
362 keyoffset = tfc_fnamesize(s, YES);
363 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
364 if (keyoffset == NOFSIZE)
366 "%s: invalid key offset value", s);
368 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
370 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
372 ctrsz = tfc_humanfsize(s, &stoi);
373 if (!str_empty(stoi)) {
374 ctrsz = (size_t)tfc_fnamesize(s, YES);
375 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
378 "%s: invalid counter length value", s);
380 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
381 if (ctrsz > TF_BLOCK_SIZE)
382 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
384 else xerror(NO, YES, YES, "invalid option %s", s);
388 do_mac = TFC_MAC_SIGN;
389 if (strcasecmp(optarg, "mac") != 0)
390 do_mac_file = optarg;
393 do_mac = TFC_MAC_VRFY;
394 if (!strcasecmp(optarg, "drop"))
395 do_mac = TFC_MAC_DROP;
396 else if (strcasecmp(optarg, "mac") != 0)
397 do_mac_file = optarg;
400 if (do_mac != TFC_MAC_VRFY)
401 xerror(NO, YES, YES, "signature source was not specified");
402 do_mac = TFC_MAC_JUST_VRFY;
406 if (maxlen != NOFSIZE) {
407 if (c == 'Z') genzero_nr_bytes = maxlen;
408 else genrandom_nr_bytes = maxlen;
412 if (!strcasecmp(optarg, "cbs"))
414 else if (!strcasecmp(optarg, "ks"))
415 t = TF_FROM_BITS(TFC_KEY_BITS);
416 else if (!strcasecmp(optarg, "xks"))
417 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
418 else if (!strcasecmp(optarg, "iobs"))
421 t = tfc_humanfsize(optarg, &stoi);
422 if (!str_empty(stoi)) {
423 t = tfc_fnamesize(optarg, NO);
424 t = tfc_modifysize(t, strchr(optarg, ':'));
426 else t = tfc_modifysize(t, strchr(optarg, ':'));
428 if (c == 'Z') genzero_nr_bytes = maxlen = t;
429 else genrandom_nr_bytes = maxlen = t;
433 do_preserve_time = YES;
436 do_outfmt = TFC_OUTFMT_B64;
439 do_outfmt = TFC_OUTFMT_RAW;
442 do_outfmt = TFC_OUTFMT_HEX;
453 td = strtod(optarg, &stoi);
454 status_timer = TFC_DTOUSECS(td);
455 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
463 if (!strcmp(progname, "tfbench")) {
464 if (!*(argv+optind)) usage();
466 td = strtod(*(argv+optind), &stoi);
467 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
469 "%s: invalid benchmark time in seconds", *(argv+optind));
470 bench_timer = TFC_DTOUSECS(td);
471 do_benchmark(bench_timer, td);
473 if (genrandom_nr_bytes) {
474 ctr_mode = TFC_MODE_STREAM;
475 do_edcrypt = TFC_DO_ENCRYPT;
476 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
478 if (genzero_nr_bytes) {
479 ctr_mode = TFC_MODE_PLAIN;
480 do_edcrypt = TFC_DO_PLAIN;
481 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
484 if (rawkey && password)
485 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
486 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
487 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
488 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
489 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
490 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
491 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
492 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
493 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
494 if (overwrite_source && counter_opt == TFC_CTR_RAND)
495 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
496 if (tweakf && do_tfcrypt1 == NO)
497 xerror(NO, YES, YES, "Use -T with -t tweakfile to enable old tfcrypt mode!");
498 if (ctr_mode == TFC_MODE_PLAIN
499 && (do_edcrypt || do_mac || rawkey
500 || mackey_opt || counter_opt || counter_file))
501 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
509 memset(tfc_salt, 0, TFC_MAX_SALT);
511 if (!strcasecmp(saltf, "disable")) goto _nosalt;
513 if (!strcmp(saltf, "-")) saltfd = 0;
514 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
515 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
516 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
517 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
523 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
527 if (!strcmp(mackeyf, "-")) mkfd = 0;
528 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
529 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
531 skein_init(&sk, TFC_KEY_BITS);
538 lrem = lblock = sizeof(tmpdata);
539 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
540 _mkragain: lio = xread(mkfd, pblk, lrem);
541 if (lio == 0) do_stop = YES;
542 if (lio != NOSIZE) ldone += lio;
544 if (errno != EIO && catch_all_errors != YES)
545 xerror(NO, NO, NO, "%s", mackeyf);
546 switch (error_action) {
547 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
548 case TFC_ERRACT_SYNC:
549 case TFC_ERRACT_LSYNC:
550 xerror(YES, NO, NO, "%s", mackeyf);
551 lio = ldone = lrem = lblock;
552 memset(tmpdata, 0, lio);
553 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
554 else lseek(mkfd, rdpos + lio, SEEK_SET);
556 default: xerror(NO, NO, NO, "%s", mackeyf); break;
559 if (lio && lio < lrem) {
565 skein_update(&sk, tmpdata, ldone);
568 skein_final(mackey, &sk);
572 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
573 memset(&getps, 0, sizeof(struct getpasswd_state));
574 getps.fd = getps.efd = -1;
575 getps.passwd = pwdask;
576 getps.pwlen = sizeof(pwdask)-1;
577 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
578 getps.charfilter = getps_filter;
579 getps.maskchar = 'x';
580 getps.flags = GETP_WAITFILL;
581 n = xgetpasswd(&getps);
582 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
583 if (n == ((size_t)-2)) xexit(1);
584 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
586 skein(tmpdata, TF_MAX_BITS, NULL, mackey, TF_FROM_BITS(TF_MAX_BITS));
587 xor_shrink(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, tmpdata, TF_FROM_BITS(TF_MAX_BITS));
588 tfc_nfsay(stderr, "MAC password hint: ");
589 mehexdump(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, TF_SIZE_UNIT, 1);
590 memset(tmpdata, 0, sizeof(tmpdata));
595 if ((strlen(progname) <= 9)
596 && ((!strcmp(progname, "sksum"))
597 || ((!memcmp(progname, "sk", 2))
598 && (!memcmp(progname+3, "sum", 3)
599 || !memcmp(progname+4, "sum", 3)
600 || !memcmp(progname+5, "sum", 3)
601 || !memcmp(progname+6, "sum", 3)))))
602 do_sksum(progname, argv+optind);
603 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
608 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
609 if (!strcmp(argv[idx], "-")) kfd = 0;
610 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
611 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
613 lio = strnlen(argv[idx], PATH_MAX);
614 memset(argv[idx], '*', lio);
621 if (do_tfcrypt1 == YES && tweakf) {
624 if (!strcmp(tweakf, "-")) twfd = 0;
625 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
626 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
627 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
628 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
629 if (ldone < TF_TWEAK_SIZE)
630 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
637 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
639 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
640 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
641 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
643 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
645 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
646 if (verbose) tfc_esay("%s: disabling signature verification on "
647 "requested partial decryption.", progname);
651 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
652 maxlen = tfc_fdsize(sfd);
653 if (maxlen == NOFSIZE)
655 "Cannot verify embedded MAC with non-seekable source!");
656 maxlen -= TF_FROM_BITS(macbits);
658 srcfname = argv[idx];
662 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
663 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
665 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
666 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
668 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
673 if (!strcmp(counter_file, "-")) ctrfd = 0;
674 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
675 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
676 lio = xread(ctrfd, ctr, ctrsz);
677 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
678 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
681 else if (counter_opt == TFC_CTR_HEAD) {
684 lrem = lblock = ctrsz;
685 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
686 _ctrragain: lio = xread(sfd, pblk, lrem);
687 if (lio != NOSIZE) ldone += lio;
689 if (errno != EIO && catch_all_errors != YES)
690 xerror(NO, NO, NO, "%s", srcfname);
691 switch (error_action) {
692 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
693 case TFC_ERRACT_SYNC:
694 case TFC_ERRACT_LSYNC:
695 xerror(YES, NO, NO, "%s", srcfname);
696 lio = ldone = lrem = lblock;
698 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
699 else lseek(sfd, rdpos + lio, SEEK_SET);
701 default: xerror(NO, NO, NO, "%s", srcfname); break;
704 if (lio && lio < lrem) {
709 total_processed_src += ldone;
714 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
716 if (lseek(sfd, iseek, SEEK_SET) == -1)
717 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
720 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
722 if (verbose) tfc_esay("%s: hashing password", progname);
724 if (rawkey == TFC_RAWKEY_KEYFILE) {
725 tfc_yesno xtskeyset = NO;
729 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
730 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
731 _keyragain: lio = xread(kfd, pblk, lrem);
732 if (lio != NOSIZE) ldone += lio;
734 if (errno != EIO && catch_all_errors != YES)
735 xerror(NO, NO, NO, "reading key");
736 switch (error_action) {
737 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
738 case TFC_ERRACT_SYNC:
739 case TFC_ERRACT_LSYNC:
740 xerror(YES, NO, NO, "reading key");
741 lio = ldone = lrem = lblock;
743 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
744 else lseek(kfd, rdpos + lio, SEEK_SET);
746 default: xerror(NO, NO, NO, "reading key"); break;
749 if (lio && lio < lrem) {
754 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
756 if (ctr_mode == TFC_MODE_XTS) {
757 if (xtskeyset == NO) {
764 else if (rawkey == TFC_RAWKEY_ASKSTR) {
765 tfc_yesno xtskeyset = NO;
767 pblk = key; n = sizeof(key);
768 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
769 getps.fd = getps.efd = -1;
770 getps.passwd = (char *)pblk;
772 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
773 getps.charfilter = getps_filter;
774 getps.maskchar = 'x';
775 getps.flags = GETP_WAITFILL;
776 n = xgetpasswd(&getps);
777 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
778 if (n == ((size_t)-2)) xexit(1);
779 if (ctr_mode == TFC_MODE_XTS) {
780 if (xtskeyset == NO) {
781 pblk = xtskey; n = sizeof(xtskey);
787 else if (rawkey == TFC_RAWKEY_ASKHEX) {
788 tfc_yesno xtskeyset = NO;
792 memset(&getps, 0, sizeof(struct getpasswd_state));
793 getps.fd = getps.efd = -1;
794 getps.passwd = pwdask;
795 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
796 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
797 getps.charfilter = getps_hex_filter;
798 getps.maskchar = 'x';
799 getps.flags = GETP_WAITFILL;
800 n = xgetpasswd(&getps);
801 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
802 if (n == ((size_t)-2)) xexit(1);
804 tfc_esay("Please input even number of hex digits!");
805 goto _rawkey_hex_again;
807 hex2bin(pblk, pwdask);
808 memset(pwdask, 0, sizeof(pwdask));
809 if (ctr_mode == TFC_MODE_XTS) {
810 if (xtskeyset == NO) {
813 goto _rawkey_hex_again;
818 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
819 getps.fd = getps.efd = -1;
820 getps.passwd = pwdask;
821 getps.pwlen = sizeof(pwdask)-1;
822 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
823 getps.charfilter = getps_filter;
824 getps.maskchar = 'x';
825 getps.flags = GETP_WAITFILL;
826 n = xgetpasswd(&getps);
827 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
828 if (n == ((size_t)-2)) xexit(1);
829 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
830 getps.fd = getps.efd = -1;
831 getps.passwd = pwdagain;
832 getps.pwlen = sizeof(pwdagain)-1;
833 getps.echo = "Enter it again: ";
834 getps.flags = GETP_WAITFILL;
835 n = xgetpasswd(&getps);
836 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
837 if (n == ((size_t)-2)) xexit(1);
838 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
839 tfc_esay("Passwords are different, try again");
843 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
844 memset(pwdask, 0, sizeof(pwdask));
845 memset(pwdagain, 0, sizeof(pwdagain));
848 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
849 xerror(NO, NO, YES, "hashing key");
853 if (tfc_saltsz > 0) {
854 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
855 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
857 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
858 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
859 memset(tfc_salt, 0, TFC_MAX_SALT);
862 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
863 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
868 tfc_yesno xtskeyset = NO;
871 if (!strcmp(genkeyf, "-")) krfd = 1;
872 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
873 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
874 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
875 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
876 if (verbose && xtskeyset == NO) {
877 tfc_esay("%s: password hashing done", progname);
878 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
879 tfc_esay("%s: Have a nice day!", progname);
882 if (ctr_mode == TFC_MODE_XTS) {
883 if (xtskeyset == NO) {
895 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
896 if (verbose) tfc_esay("%s: disabling signature verification on "
897 "requested partial decryption.", progname);
902 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
903 if (ctr_mode < TFC_MODE_OCB) {
904 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
905 "will be slower.", progname);
906 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
907 else skein_init(&sk, macbits);
911 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
912 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
916 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
917 if (do_tfcrypt1 == YES) {
918 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
919 tf_tweak_set(key, tweak);
921 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
922 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
923 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
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;
934 case TFC_CTR_ZERO: memset(ctr, 0, ctrsz); break;
942 if (verbose) tfc_esay("%s: password hashing done", progname);
944 if (overwrite_source && srcfname) argv[idx] = srcfname;
948 if (!strcmp(argv[idx], "-")) dfd = 1;
949 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
951 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
952 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
954 dstfname = argv[idx];
959 if (lseek(dfd, oseek, SEEK_SET) == -1)
960 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
963 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
964 memset(&sigact, 0, sizeof(sigact));
965 sigact.sa_flags = SA_RESTART;
966 sigact.sa_handler = print_crypt_status;
967 sigaction(SIGUSR1, &sigact, NULL);
968 sigaction(SIGTSTP, &sigact, NULL);
969 sigaction(SIGALRM, &sigact, NULL);
970 if (status_timer) setup_next_alarm(status_timer);
971 sigact.sa_handler = change_status_width;
972 sigaction(SIGQUIT, &sigact, NULL);
973 sigact.sa_handler = change_status_timer;
974 sigaction(SIGUSR2, &sigact, NULL);
976 sigact.sa_handler = print_crypt_status;
977 sigaction(SIGINT, &sigact, NULL);
978 sigaction(SIGTERM, &sigact, NULL);
981 sigact.sa_handler = exit_sigterm;
982 sigaction(SIGINT, &sigact, NULL);
983 sigaction(SIGTERM, &sigact, NULL);
985 memset(&sigact, 0, sizeof(struct sigaction));
987 tfc_getcurtime(&delta_time);
990 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
994 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
995 if (lio != NOSIZE) ldone += lio;
996 else xerror(NO, NO, NO, "%s", dstfname);
997 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1003 total_processed_dst += ldone;
1004 delta_processed += ldone;
1007 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1015 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1016 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1017 _ragain: lio = xread(sfd, pblk, lrem);
1018 if (lio == 0) do_stop = TFC_STOP_BEGAN;
1019 if (lio != NOSIZE) ldone += lio;
1021 if (errno != EIO && catch_all_errors != YES)
1022 xerror(NO, NO, NO, "%s", srcfname);
1023 switch (error_action) {
1024 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1025 case TFC_ERRACT_SYNC:
1026 case TFC_ERRACT_LSYNC:
1027 xerror(YES, NO, NO, "%s", srcfname);
1028 lio = ldone = lrem = lblock;
1029 memset(srcblk, 0, lio);
1030 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1031 else lseek(sfd, rdpos + lio, SEEK_SET);
1033 default: xerror(NO, NO, NO, "%s", srcfname); break;
1036 if (lio && lio < lrem) {
1041 total_processed_src += ldone;
1043 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1044 size_t orig = ldone;
1045 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1046 if (ldone > blksize) ldone = blksize;
1047 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1050 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1051 skein_update(&sk, srcblk, ldone);
1053 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1054 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1055 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1056 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1057 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1058 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1059 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1060 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1061 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1062 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1063 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1064 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1065 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1066 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1068 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1069 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1070 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1071 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1073 else if (ctr_mode == TFC_MODE_PLAIN)
1074 memcpy(dstblk, srcblk, ldone);
1076 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1077 skein_update(&sk, dstblk, ldone);
1078 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1083 _wagain: lio = xwrite(dfd, pblk, lrem);
1084 if (lio != NOSIZE) ldone += lio;
1085 else xerror(NO, NO, NO, "%s", dstfname);
1086 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1092 _nowrite: total_processed_dst += ldone;
1093 delta_processed += ldone;
1095 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1098 if (do_stop == TFC_STOP_FULL) goto _nomac;
1101 if (do_mac >= TFC_MAC_VRFY) {
1105 lrem = lblock = TF_FROM_BITS(macbits);
1106 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1107 _macragain: lio = xread(sfd, pblk, lrem);
1108 if (lio != NOSIZE) ldone += lio;
1110 if (errno != EIO && catch_all_errors != YES)
1111 xerror(NO, NO, NO, "%s", srcfname);
1112 switch (error_action) {
1113 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1114 case TFC_ERRACT_SYNC:
1115 case TFC_ERRACT_LSYNC:
1116 xerror(YES, NO, NO, "%s", srcfname);
1117 lio = ldone = lrem = lblock;
1118 memset(macvrfy, 0, lio);
1119 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1120 else lseek(sfd, rdpos + lio, SEEK_SET);
1122 default: xerror(NO, NO, NO, "%s", srcfname); break;
1125 if (lio && lio < lrem) {
1130 total_processed_src += ldone;
1135 if (!strcmp(do_mac_file, "-")) mfd = 0;
1136 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1137 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1138 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1139 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1140 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1141 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1142 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1143 lio = TF_FROM_BITS(macbits);
1144 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1146 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1150 if (ldone < TF_FROM_BITS(macbits)) {
1151 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1152 "not verifying", progname, ldone);
1157 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1158 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1160 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1161 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1162 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1163 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1164 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1165 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1167 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1169 tfc_esay("%s: signature is good", progname);
1171 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1172 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1177 if (quiet == NO) tfc_esay("%s: signature is BAD: "
1178 "wrong password, key, mode, or file is not signed", progname);
1182 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1183 memset(macresult, 0, sizeof(macresult));
1184 memset(tmpdata, 0, sizeof(tmpdata));
1187 else if (do_mac == TFC_MAC_SIGN) {
1188 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1189 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1191 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1192 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1193 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1194 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1195 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1196 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1197 memset(macresult, 0, sizeof(macresult));
1201 lio = lrem = TF_FROM_BITS(macbits);
1203 _macwagain: lio = xwrite(dfd, pblk, lrem);
1204 if (lio != NOSIZE) ldone += lio;
1205 else xerror(NO, NO, NO, "%s", dstfname);
1206 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1212 total_processed_dst += ldone;
1213 delta_processed += ldone;
1218 if (!strcmp(do_mac_file, "-")) mfd = 1;
1219 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1220 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1221 if (do_outfmt == TFC_OUTFMT_B64) {
1222 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1223 memset(tmpdata, 0, TFC_TMPSIZE);
1224 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1225 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1226 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1228 tmpdata[lrem] = '\n';
1231 lio = xwrite(mfd, tmpdata, lrem);
1233 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1234 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1235 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1239 memset(macvrfy, 0, sizeof(macvrfy));
1240 memset(macresult, 0, sizeof(macresult));
1241 memset(tmpdata, 0, sizeof(tmpdata));
1245 if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1247 if (do_preserve_time) fcopy_matime(dfd, &s_stat);