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;
66 while ((c = getopt(argc, argv, "s:aU:C:r:K:t:TPkzxc:l:qedn:vV:pwE:O:S:AmM:R:Z:WHD:")) != -1) {
75 if (!strcasecmp(optarg, "show"))
76 counter_opt = TFC_CTR_SHOW;
77 else if (!strcasecmp(optarg, "head"))
78 counter_opt = TFC_CTR_HEAD;
79 else if (!strcasecmp(optarg, "rand"))
80 counter_opt = TFC_CTR_RAND;
81 else counter_file = sksum_hashlist_file = optarg;
84 if (!strcasecmp(optarg, "ctr"))
85 ctr_mode = TFC_MODE_CTR;
86 else if (!strcasecmp(optarg, "stream"))
87 ctr_mode = TFC_MODE_STREAM;
88 else if (!strcasecmp(optarg, "cbc"))
89 ctr_mode = TFC_MODE_CBC;
90 else if (!strcasecmp(optarg, "ecb"))
91 ctr_mode = TFC_MODE_ECB;
92 else if (!strcasecmp(optarg, "xts"))
93 ctr_mode = TFC_MODE_XTS;
94 else if (!strcasecmp(optarg, "ocb"))
95 ctr_mode = TFC_MODE_OCB;
96 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
99 do_edcrypt = TFC_DO_PLAIN;
101 ctr_mode = TFC_MODE_PLAIN;
104 do_edcrypt = TFC_DO_ENCRYPT;
107 do_edcrypt = TFC_DO_DECRYPT;
110 macbits = strtoul(optarg, &stoi, 10);
111 if (macbits == 0 || !str_empty(stoi) || macbits < 8
112 || macbits > TF_MAX_BITS || macbits % 8)
113 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
116 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
117 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
120 if (!strcasecmp(optarg, "key"))
121 mackey_opt = TFC_MACKEY_RAWKEY;
122 else if (!strcasecmp(optarg, "pwd"))
123 mackey_opt = TFC_MACKEY_PASSWORD;
125 mackey_opt = TFC_MACKEY_FILE;
133 rawkey = TFC_RAWKEY_KEYFILE;
136 rawkey = TFC_RAWKEY_ASKSTR;
139 rawkey = TFC_RAWKEY_ASKHEX;
153 if (maxlen != NOFSIZE) break;
155 maxlen = tfc_humanfsize(optarg, &stoi);
156 if (!str_empty(stoi)) {
157 maxlen = tfc_fnamesize(optarg, YES);
158 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
159 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
160 "%s: invalid count value", optarg);
162 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
163 if (counter_opt == TFC_CTR_HEAD)
164 maxlen += TF_BLOCK_SIZE;
167 overwrite_source = YES;
170 if (!strcmp(optarg, "xall")) {
171 catch_all_errors = YES;
174 if (!strcmp(optarg, "exit"))
175 error_action = TFC_ERRACT_EXIT;
176 else if (!strncmp(optarg, "cont", 4))
177 error_action = TFC_ERRACT_CONT;
178 else if (!strcmp(optarg, "sync"))
179 error_action = TFC_ERRACT_SYNC;
180 else if (!strcmp(optarg, "lsync"))
181 error_action = TFC_ERRACT_LSYNC;
182 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
185 s = d = optarg; t = NULL;
186 while ((s = strtok_r(d, ",", &t))) {
188 if (!strcmp(s, "sync"))
189 write_flags |= O_SYNC;
190 else if (!strcmp(s, "trunc"))
191 write_flags |= O_TRUNC;
192 else if (!strcmp(s, "fsync"))
194 else if (!strcmp(s, "pad"))
196 else if (!strcmp(s, "xtime"))
197 do_preserve_time = YES;
198 else if (!strcmp(s, "gibsize"))
199 do_stats_in_gibs = YES;
200 else if (!strcmp(s, "plainstats"))
201 do_statline_dynamic = NO;
202 else if (!strcmp(s, "statless"))
204 else if (!strcmp(s, "norepeat"))
206 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
208 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
209 mac_pw_prompt = s+10;
210 else if (!strcmp(s, "shorthex"))
211 do_full_hexdump = NO;
212 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
214 blksize = (size_t)tfc_humanfsize(s, &stoi);
215 if (!str_empty(stoi)) {
216 blksize = (size_t)tfc_fnamesize(s, YES);
217 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
218 if (blksize == NOSIZE) xerror(NO, YES, YES,
219 "%s: invalid block size value", s);
221 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
222 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
223 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
224 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
225 "%s: block size exceeds %u bytes",
226 s, TFC_U(TFC_BLKSIZE));
228 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
230 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
231 if (!str_empty(stoi)) {
232 xtsblocks = (size_t)tfc_fnamesize(s, YES);
233 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
234 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
235 "%s: invalid blocks per xts block value", s);
237 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
238 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
239 "%s: nr of blocks per xts block is not round to %u bytes",
240 s, TFC_U(TFC_BLKSIZE));
241 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
242 "%s: nr of blocks per xts block exceeds %u bytes",
243 s, TFC_U(TFC_BLKSIZE));
245 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
247 iseek = tfc_humanfsize(s, &stoi);
248 if (!str_empty(stoi)) {
249 iseek = tfc_fnamesize(s, YES);
250 iseek = tfc_modifysize(iseek, strchr(s, ':'));
251 if (iseek == NOFSIZE) xerror(NO, YES, YES,
252 "%s: invalid iseek value", s);
254 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
255 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
257 "%s: not round to TF block size "
259 s, TFC_U(TF_BLOCK_SIZE));
260 iseek_blocks = iseek / TF_BLOCK_SIZE;
262 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
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 ixseek value", s);
271 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
273 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
275 iseek_blocks = tfc_humanfsize(s, &stoi);
276 if (!str_empty(stoi)) {
277 iseek_blocks = tfc_fnamesize(s, YES);
278 if (iseek_blocks == NOFSIZE)
280 "%s: invalid ictr value", s);
281 iseek_blocks /= TF_BLOCK_SIZE;
282 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
284 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
286 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
288 iseek_blocks = tfc_humanfsize(s, &stoi);
289 if (!str_empty(stoi)) {
290 iseek_blocks = tfc_fnamesize(s, YES);
291 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
292 if (iseek_blocks == NOFSIZE)
294 "%s: invalid ixctr value", s);
296 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
297 if (iseek_blocks % TF_BLOCK_SIZE)
299 "%s: not round to TF block size "
300 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
301 iseek_blocks /= TF_BLOCK_SIZE;
303 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
305 oseek = tfc_humanfsize(s, &stoi);
306 if (!str_empty(stoi)) {
307 oseek = tfc_fnamesize(s, YES);
308 oseek = tfc_modifysize(oseek, strchr(s, ':'));
309 if (oseek == NOFSIZE) xerror(NO, YES, YES,
310 "%s: invalid oseek value", s);
312 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
314 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
316 maxlen = tfc_humanfsize(s, &stoi);
317 if (!str_empty(stoi)) {
318 maxlen = tfc_fnamesize(s, YES);
319 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
320 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
321 "%s: invalid count value", s);
323 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
324 if (counter_opt == TFC_CTR_HEAD)
325 maxlen += TF_BLOCK_SIZE;
327 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
329 maxkeylen = tfc_humanfsize(s, &stoi);
330 if (!str_empty(stoi)) {
331 maxkeylen = (size_t)tfc_fnamesize(s, YES);
332 maxkeylen = (size_t)tfc_modifysize((tfc_fsize)maxkeylen, strchr(s, ':'));
333 if (maxkeylen == NOSIZE)
335 "%s: invalid key length value", s);
337 else maxkeylen = (size_t)tfc_modifysize((tfc_fsize)maxkeylen, strchr(s, ':'));
339 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
341 ctrsz = tfc_humanfsize(s, &stoi);
342 if (!str_empty(stoi)) {
343 ctrsz = (size_t)tfc_fnamesize(s, YES);
344 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
347 "%s: invalid counter length value", s);
349 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
350 if (ctrsz > TF_BLOCK_SIZE)
351 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
353 else xerror(NO, YES, YES, "invalid option %s", s);
357 do_mac = TFC_MAC_SIGN;
358 if (strcasecmp(optarg, "mac") != 0)
359 do_mac_file = optarg;
362 do_mac = TFC_MAC_VRFY;
363 if (!strcasecmp(optarg, "drop"))
364 do_mac = TFC_MAC_DROP;
365 else if (strcasecmp(optarg, "mac") != 0)
366 do_mac_file = optarg;
369 if (do_mac != TFC_MAC_VRFY)
370 xerror(NO, YES, YES, "signature source was not specified");
371 do_mac = TFC_MAC_JUST_VRFY;
375 if (maxlen != NOFSIZE) {
376 if (c == 'Z') genzero_nr_bytes = maxlen;
377 else genrandom_nr_bytes = maxlen;
381 if (!strcasecmp(optarg, "cbs"))
383 else if (!strcasecmp(optarg, "ks"))
384 t = TF_FROM_BITS(TFC_KEY_BITS);
385 else if (!strcasecmp(optarg, "xks"))
386 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
387 else if (!strcasecmp(optarg, "iobs"))
390 t = tfc_humanfsize(optarg, &stoi);
391 if (!str_empty(stoi)) {
392 t = tfc_fnamesize(optarg, NO);
393 t = tfc_modifysize(t, strchr(optarg, ':'));
395 else t = tfc_modifysize(t, strchr(optarg, ':'));
397 if (c == 'Z') genzero_nr_bytes = maxlen = t;
398 else genrandom_nr_bytes = maxlen = t;
402 do_preserve_time = YES;
405 do_outfmt = TFC_OUTFMT_B64;
408 do_outfmt = TFC_OUTFMT_RAW;
411 do_outfmt = TFC_OUTFMT_HEX;
422 td = strtod(optarg, &stoi);
423 status_timer = TFC_DTOUSECS(td);
424 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
432 if (!strcmp(progname, "tfbench")) {
433 if (!*(argv+optind)) usage();
435 td = strtod(*(argv+optind), &stoi);
436 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
438 "%s: invalid benchmark time in seconds", *(argv+optind));
439 bench_timer = TFC_DTOUSECS(td);
440 do_benchmark(bench_timer, td);
442 if (genrandom_nr_bytes) {
443 ctr_mode = TFC_MODE_STREAM;
444 do_edcrypt = TFC_DO_ENCRYPT;
445 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
447 if (genzero_nr_bytes) {
448 ctr_mode = TFC_MODE_PLAIN;
449 do_edcrypt = TFC_DO_PLAIN;
450 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
453 if (rawkey && password)
454 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
455 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
456 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
457 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
458 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
459 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
460 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
461 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
462 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
463 if (overwrite_source && counter_opt == TFC_CTR_RAND)
464 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
465 if (tweakf && do_tfcrypt1 == NO)
466 xerror(NO, YES, YES, "Use -T with -t tweakfile to enable old tfcrypt mode!");
467 if (ctr_mode == TFC_MODE_PLAIN
468 && (do_edcrypt || do_mac || rawkey
469 || mackey_opt || counter_opt || counter_file))
470 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
478 memset(tfc_salt, 0, TFC_MAX_SALT);
480 if (!strcasecmp(saltf, "disable")) goto _nosalt;
482 if (!strcmp(saltf, "-")) saltfd = 0;
483 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
484 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
485 lio = read(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
486 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
492 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
496 if (!strcmp(mackeyf, "-")) mkfd = 0;
497 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
498 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
500 skein_init(&sk, TFC_KEY_BITS);
507 lrem = lblock = sizeof(tmpdata);
508 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
509 _mkragain: lio = read(mkfd, pblk, lrem);
510 if (lio == 0) do_stop = YES;
511 if (lio != NOSIZE) ldone += lio;
513 if (errno != EIO && catch_all_errors != YES)
514 xerror(NO, NO, NO, "%s", mackeyf);
515 switch (error_action) {
516 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
517 case TFC_ERRACT_SYNC:
518 case TFC_ERRACT_LSYNC:
519 xerror(YES, NO, NO, "%s", mackeyf);
520 lio = ldone = lrem = lblock;
521 memset(tmpdata, 0, lio);
522 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
523 else lseek(mkfd, rdpos + lio, SEEK_SET);
525 default: xerror(NO, NO, NO, "%s", mackeyf); break;
528 if (lio && lio < lrem) {
534 skein_update(&sk, tmpdata, ldone);
537 skein_final(mackey, &sk);
541 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
542 memset(&getps, 0, sizeof(struct getpasswd_state));
543 getps.fd = getps.efd = -1;
544 getps.passwd = pwdask;
545 getps.pwlen = sizeof(pwdask)-1;
546 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
547 getps.charfilter = getps_filter;
548 getps.maskchar = 'x';
549 getps.flags = GETP_WAITFILL;
550 n = xgetpasswd(&getps);
551 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
552 if (n == ((size_t)-2)) xexit(1);
553 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
555 skein(tmpdata, TF_MAX_BITS, NULL, mackey, TF_FROM_BITS(TF_MAX_BITS));
556 xor_shrink(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, tmpdata, TF_FROM_BITS(TF_MAX_BITS));
557 tfc_nfsay(stderr, "MAC password hint: ");
558 mehexdump(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, TF_SIZE_UNIT, 1);
559 memset(tmpdata, 0, sizeof(tmpdata));
564 if ((strlen(progname) <= 9)
565 && ((!strcmp(progname, "sksum"))
566 || ((!memcmp(progname, "sk", 2))
567 && (!memcmp(progname+3, "sum", 3)
568 || !memcmp(progname+4, "sum", 3)
569 || !memcmp(progname+5, "sum", 3)
570 || !memcmp(progname+6, "sum", 3)))))
571 do_sksum(progname, argv+optind);
572 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
577 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
578 if (!strcmp(argv[idx], "-")) kfd = 0;
579 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
580 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
582 lio = strnlen(argv[idx], PATH_MAX);
583 memset(argv[idx], '*', lio);
590 if (do_tfcrypt1 == YES && tweakf) {
593 if (!strcmp(tweakf, "-")) twfd = 0;
594 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
595 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
596 lio = ldone = read(twfd, key+TF_FROM_BITS(TF_MAX_BITS)+TF_SIZE_UNIT, 2*TF_SIZE_UNIT);
597 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
598 if (ldone < 2*TF_SIZE_UNIT)
599 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, 2*TF_SIZE_UNIT);
606 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
608 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
609 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
610 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
612 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
614 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
615 if (verbose) tfc_esay("%s: disabling signature verification on "
616 "requested partial decryption.", progname);
620 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
621 maxlen = tfc_fdsize(sfd);
622 if (maxlen == NOFSIZE)
624 "Cannot verify embedded MAC with non-seekable source!");
625 maxlen -= TF_FROM_BITS(macbits);
627 srcfname = argv[idx];
631 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
632 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
634 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
635 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
637 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
642 if (!strcmp(counter_file, "-")) ctrfd = 0;
643 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
644 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
645 lio = read(ctrfd, ctr, ctrsz);
646 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
647 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
650 else if (counter_opt == TFC_CTR_HEAD) {
653 lrem = lblock = ctrsz;
654 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
655 _ctrragain: lio = read(sfd, pblk, lrem);
656 if (lio != NOSIZE) ldone += lio;
658 if (errno != EIO && catch_all_errors != YES)
659 xerror(NO, NO, NO, "%s", srcfname);
660 switch (error_action) {
661 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
662 case TFC_ERRACT_SYNC:
663 case TFC_ERRACT_LSYNC:
664 xerror(YES, NO, NO, "%s", srcfname);
665 lio = ldone = lrem = lblock;
667 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
668 else lseek(sfd, rdpos + lio, SEEK_SET);
670 default: xerror(NO, NO, NO, "%s", srcfname); break;
673 if (lio && lio < lrem) {
678 total_processed_src += ldone;
683 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
685 if (lseek(sfd, iseek, SEEK_SET) == -1)
686 xerror(YES, NO, NO, "%s: seek failed", srcfname);
689 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
691 if (verbose) tfc_esay("%s: hashing password", progname);
693 if (rawkey == TFC_RAWKEY_KEYFILE) {
694 tfc_yesno xtskeyset = NO;
698 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
699 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
700 _keyragain: lio = read(kfd, pblk, lrem);
701 if (lio != NOSIZE) ldone += lio;
703 if (errno != EIO && catch_all_errors != YES)
704 xerror(NO, NO, NO, "reading key");
705 switch (error_action) {
706 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
707 case TFC_ERRACT_SYNC:
708 case TFC_ERRACT_LSYNC:
709 xerror(YES, NO, NO, "reading key");
710 lio = ldone = lrem = lblock;
712 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
713 else lseek(kfd, rdpos + lio, SEEK_SET);
715 default: xerror(NO, NO, NO, "reading key"); break;
718 if (lio && lio < lrem) {
723 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
725 if (ctr_mode == TFC_MODE_XTS) {
726 if (xtskeyset == NO) {
733 else if (rawkey == TFC_RAWKEY_ASKSTR) {
734 tfc_yesno xtskeyset = NO;
736 pblk = key; n = sizeof(key);
737 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
738 getps.fd = getps.efd = -1;
739 getps.passwd = (char *)pblk;
741 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
742 getps.charfilter = getps_filter;
743 getps.maskchar = 'x';
744 getps.flags = GETP_WAITFILL;
745 n = xgetpasswd(&getps);
746 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
747 if (n == ((size_t)-2)) xexit(1);
748 if (ctr_mode == TFC_MODE_XTS) {
749 if (xtskeyset == NO) {
750 pblk = xtskey; n = sizeof(xtskey);
756 else if (rawkey == TFC_RAWKEY_ASKHEX) {
757 tfc_yesno xtskeyset = NO;
761 memset(&getps, 0, sizeof(struct getpasswd_state));
762 getps.fd = getps.efd = -1;
763 getps.passwd = pwdask;
764 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
765 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
766 getps.charfilter = getps_hex_filter;
767 getps.maskchar = 'x';
768 getps.flags = GETP_WAITFILL;
769 n = xgetpasswd(&getps);
770 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
771 if (n == ((size_t)-2)) xexit(1);
773 tfc_esay("Please input even number of hex digits!");
774 goto _rawkey_hex_again;
776 hex2bin(pblk, pwdask);
777 memset(pwdask, 0, sizeof(pwdask));
778 if (ctr_mode == TFC_MODE_XTS) {
779 if (xtskeyset == NO) {
782 goto _rawkey_hex_again;
787 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
788 getps.fd = getps.efd = -1;
789 getps.passwd = pwdask;
790 getps.pwlen = sizeof(pwdask)-1;
791 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
792 getps.charfilter = getps_filter;
793 getps.maskchar = 'x';
794 getps.flags = GETP_WAITFILL;
795 n = xgetpasswd(&getps);
796 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
797 if (n == ((size_t)-2)) xexit(1);
798 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
799 getps.fd = getps.efd = -1;
800 getps.passwd = pwdagain;
801 getps.pwlen = sizeof(pwdagain)-1;
802 getps.echo = "Enter it again: ";
803 getps.flags = GETP_WAITFILL;
804 n = xgetpasswd(&getps);
805 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
806 if (n == ((size_t)-2)) xexit(1);
807 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
808 tfc_esay("Passwords are different, try again");
812 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
813 memset(pwdask, 0, sizeof(pwdask));
814 memset(pwdagain, 0, sizeof(pwdagain));
817 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, maxkeylen) != YES)
818 xerror(NO, NO, YES, "hashing key");
822 if (tfc_saltsz > 0) {
823 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
824 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
826 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
827 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
828 memset(tfc_salt, 0, TFC_MAX_SALT);
831 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
832 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
837 tfc_yesno xtskeyset = NO;
840 if (!strcmp(genkeyf, "-")) krfd = 1;
841 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
842 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
843 _xts2genkey: if (write(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == -1) xerror(NO, NO, YES, "%s", genkeyf);
844 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
845 if (verbose && xtskeyset == NO) {
846 tfc_esay("%s: password hashing done", progname);
847 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
848 tfc_esay("%s: Have a nice day!", progname);
851 if (ctr_mode == TFC_MODE_XTS) {
852 if (xtskeyset == NO) {
864 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
865 if (verbose) tfc_esay("%s: disabling signature verification on "
866 "requested partial decryption.", progname);
871 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
872 if (ctr_mode < TFC_MODE_OCB) {
873 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
874 "will be slower.", progname);
875 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
876 else skein_init(&sk, macbits);
880 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
881 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
885 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
886 if (do_tfcrypt1 == YES) {
887 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));
888 tf_key_tweak_compat(key);
890 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
891 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
892 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
893 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
895 switch (counter_opt) {
898 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
899 case TFC_OUTFMT_RAW: write(2, ctr, ctrsz); break;
900 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
903 case TFC_CTR_RAND: tfc_getrandom(ctr, ctrsz); break;
911 if (verbose) tfc_esay("%s: password hashing done", progname);
913 if (overwrite_source && srcfname) argv[idx] = srcfname;
917 if (!strcmp(argv[idx], "-")) dfd = 1;
918 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
920 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
921 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
923 dstfname = argv[idx];
928 if (lseek(dfd, oseek, SEEK_SET) == -1)
929 xerror(YES, NO, NO, "%s: seek failed", dstfname);
932 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
933 memset(&sigact, 0, sizeof(sigact));
934 sigact.sa_flags = SA_RESTART;
935 sigact.sa_handler = print_crypt_status;
936 sigaction(SIGUSR1, &sigact, NULL);
937 sigaction(SIGTSTP, &sigact, NULL);
938 sigaction(SIGALRM, &sigact, NULL);
939 if (status_timer) setup_next_alarm(status_timer);
940 sigact.sa_handler = change_status_width;
941 sigaction(SIGQUIT, &sigact, NULL);
942 sigact.sa_handler = change_status_timer;
943 sigaction(SIGUSR2, &sigact, NULL);
945 sigact.sa_handler = print_crypt_status;
946 sigaction(SIGINT, &sigact, NULL);
947 sigaction(SIGTERM, &sigact, NULL);
950 sigact.sa_handler = exit_sigterm;
951 sigaction(SIGINT, &sigact, NULL);
952 sigaction(SIGTERM, &sigact, NULL);
954 memset(&sigact, 0, sizeof(struct sigaction));
956 tfc_getcurtime(&delta_time);
959 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
963 _ctrwagain: lio = write(dfd, pblk, lrem);
964 if (lio != NOSIZE) ldone += lio;
965 else xerror(NO, NO, NO, "%s", dstfname);
966 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
972 total_processed_dst += ldone;
973 delta_processed += ldone;
982 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
983 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
984 _ragain: lio = read(sfd, pblk, lrem);
985 if (lio == 0) do_stop = TFC_STOP_BEGAN;
986 if (lio != NOSIZE) ldone += lio;
988 if (errno != EIO && catch_all_errors != YES)
989 xerror(NO, NO, NO, "%s", srcfname);
990 switch (error_action) {
991 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
992 case TFC_ERRACT_SYNC:
993 case TFC_ERRACT_LSYNC:
994 xerror(YES, NO, NO, "%s", srcfname);
995 lio = ldone = lrem = lblock;
996 memset(srcblk, 0, lio);
997 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
998 else lseek(sfd, rdpos + lio, SEEK_SET);
1000 default: xerror(NO, NO, NO, "%s", srcfname); break;
1003 if (lio && lio < lrem) {
1008 total_processed_src += ldone;
1010 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1011 size_t orig = ldone;
1012 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1013 if (ldone > blksize) ldone = blksize;
1014 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1017 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1018 skein_update(&sk, srcblk, ldone);
1020 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1021 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1022 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1023 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1024 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1025 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1026 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1027 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1028 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1029 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1030 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1031 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1032 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1033 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1035 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1036 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1037 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1038 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1040 else if (ctr_mode == TFC_MODE_PLAIN)
1041 memcpy(dstblk, srcblk, ldone);
1043 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1044 skein_update(&sk, dstblk, ldone);
1045 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1050 _wagain: lio = write(dfd, pblk, lrem);
1051 if (lio != NOSIZE) ldone += lio;
1052 else xerror(NO, NO, NO, "%s", dstfname);
1053 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1059 _nowrite: total_processed_dst += ldone;
1060 delta_processed += ldone;
1062 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1065 if (do_stop == TFC_STOP_FULL) goto _nomac;
1068 if (do_mac >= TFC_MAC_VRFY) {
1072 lrem = lblock = TF_FROM_BITS(macbits);
1073 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1074 _macragain: lio = read(sfd, pblk, lrem);
1075 if (lio != NOSIZE) ldone += lio;
1077 if (errno != EIO && catch_all_errors != YES)
1078 xerror(NO, NO, NO, "%s", srcfname);
1079 switch (error_action) {
1080 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1081 case TFC_ERRACT_SYNC:
1082 case TFC_ERRACT_LSYNC:
1083 xerror(YES, NO, NO, "%s", srcfname);
1084 lio = ldone = lrem = lblock;
1085 memset(macvrfy, 0, lio);
1086 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1087 else lseek(sfd, rdpos + lio, SEEK_SET);
1089 default: xerror(NO, NO, NO, "%s", srcfname); break;
1092 if (lio && lio < lrem) {
1097 total_processed_src += ldone;
1102 if (!strcmp(do_mac_file, "-")) mfd = 0;
1103 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1104 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1105 lio = ldone = read(mfd, tmpdata, sizeof(tmpdata));
1106 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1107 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1108 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1109 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1110 lio = TF_FROM_BITS(macbits);
1111 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1113 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1117 if (ldone < TF_FROM_BITS(macbits)) {
1118 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1119 "not verifying", progname, ldone);
1124 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1125 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1127 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1128 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1129 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1130 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1131 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1132 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1134 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1136 tfc_esay("%s: signature is good", progname);
1138 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1139 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1144 if (quiet == NO) tfc_esay("%s: signature is BAD: "
1145 "wrong password, key, mode, or file is not signed", progname);
1149 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1150 memset(macresult, 0, sizeof(macresult));
1151 memset(tmpdata, 0, sizeof(tmpdata));
1154 else if (do_mac == TFC_MAC_SIGN) {
1155 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1156 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1158 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1159 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1160 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1161 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1162 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1163 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1164 memset(macresult, 0, sizeof(macresult));
1168 lio = lrem = TF_FROM_BITS(macbits);
1170 _macwagain: lio = write(dfd, pblk, lrem);
1171 if (lio != NOSIZE) ldone += lio;
1172 else xerror(NO, NO, NO, "%s", dstfname);
1173 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1179 total_processed_dst += ldone;
1180 delta_processed += ldone;
1185 if (!strcmp(do_mac_file, "-")) mfd = 1;
1186 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1187 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1188 if (do_outfmt == TFC_OUTFMT_B64) {
1189 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1190 memset(tmpdata, 0, TFC_TMPSIZE);
1191 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1192 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1193 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1195 tmpdata[lrem] = '\n';
1198 lio = write(mfd, tmpdata, lrem);
1200 else lio = write(mfd, tmpdata, TF_FROM_BITS(macbits));
1201 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1202 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1206 memset(macvrfy, 0, sizeof(macvrfy));
1207 memset(macresult, 0, sizeof(macresult));
1208 memset(tmpdata, 0, sizeof(tmpdata));
1212 if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1214 if (do_preserve_time) fcopy_matime(dfd, &s_stat);