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 xstrlcat(s, d, PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX);
69 xstrlcat(s, "/.tfcrypt.defs", PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX);
70 read_defaults(s, YES);
71 memset(s, 0, PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX);
74 while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:TPkzxc:l:qedn:vV:pwE:O:S:AmM:R:Z:WHD:")) != -1) {
77 read_defaults(optarg, NO);
86 if (!strcasecmp(optarg, "show"))
87 counter_opt = TFC_CTR_SHOW;
88 else if (!strcasecmp(optarg, "head"))
89 counter_opt = TFC_CTR_HEAD;
90 else if (!strcasecmp(optarg, "rand"))
91 counter_opt = TFC_CTR_RAND;
92 else counter_file = sksum_hashlist_file = optarg;
95 if (!strcasecmp(optarg, "ctr"))
96 ctr_mode = TFC_MODE_CTR;
97 else if (!strcasecmp(optarg, "stream"))
98 ctr_mode = TFC_MODE_STREAM;
99 else if (!strcasecmp(optarg, "cbc"))
100 ctr_mode = TFC_MODE_CBC;
101 else if (!strcasecmp(optarg, "ecb"))
102 ctr_mode = TFC_MODE_ECB;
103 else if (!strcasecmp(optarg, "xts"))
104 ctr_mode = TFC_MODE_XTS;
105 else if (!strcasecmp(optarg, "ocb"))
106 ctr_mode = TFC_MODE_OCB;
107 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
110 do_edcrypt = TFC_DO_PLAIN;
112 ctr_mode = TFC_MODE_PLAIN;
115 do_edcrypt = TFC_DO_ENCRYPT;
118 do_edcrypt = TFC_DO_DECRYPT;
121 macbits = strtoul(optarg, &stoi, 10);
122 if (macbits == 0 || !str_empty(stoi) || macbits < 8
123 || macbits > TF_MAX_BITS || macbits % 8)
124 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
127 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
128 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
131 if (!strcasecmp(optarg, "key"))
132 mackey_opt = TFC_MACKEY_RAWKEY;
133 else if (!strcasecmp(optarg, "pwd"))
134 mackey_opt = TFC_MACKEY_PASSWORD;
136 mackey_opt = TFC_MACKEY_FILE;
144 rawkey = TFC_RAWKEY_KEYFILE;
147 rawkey = TFC_RAWKEY_ASKSTR;
150 rawkey = TFC_RAWKEY_ASKHEX;
164 if (maxlen != NOFSIZE) break;
166 maxlen = tfc_humanfsize(optarg, &stoi);
167 if (!str_empty(stoi)) {
168 maxlen = tfc_fnamesize(optarg, YES);
169 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
170 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
171 "%s: invalid count value", optarg);
173 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
174 if (counter_opt == TFC_CTR_HEAD)
175 maxlen += TF_BLOCK_SIZE;
178 overwrite_source = YES;
181 if (!strcmp(optarg, "xall")) {
182 catch_all_errors = YES;
185 if (!strcmp(optarg, "xseek")) {
186 ignore_seek_errors = YES;
189 if (!strcmp(optarg, "exit"))
190 error_action = TFC_ERRACT_EXIT;
191 else if (!strncmp(optarg, "cont", 4))
192 error_action = TFC_ERRACT_CONT;
193 else if (!strcmp(optarg, "sync"))
194 error_action = TFC_ERRACT_SYNC;
195 else if (!strcmp(optarg, "lsync"))
196 error_action = TFC_ERRACT_LSYNC;
197 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
200 s = d = optarg; t = NULL;
201 while ((s = strtok_r(d, ",", &t))) {
203 if (!strcmp(s, "sync"))
204 write_flags |= O_SYNC;
205 else if (!strcmp(s, "trunc"))
206 write_flags |= O_TRUNC;
207 else if (!strcmp(s, "fsync"))
209 else if (!strcmp(s, "pad"))
211 else if (!strcmp(s, "xtime"))
212 do_preserve_time = YES;
213 else if (!strcmp(s, "gibsize"))
214 do_stats_in_gibs = YES;
215 else if (!strcmp(s, "plainstats"))
216 do_statline_dynamic = NO;
217 else if (!strcmp(s, "statless"))
219 else if (!strcmp(s, "norepeat"))
221 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
223 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
224 mac_pw_prompt = s+10;
225 else if (!strcmp(s, "shorthex"))
226 do_full_hexdump = NO;
227 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
229 blksize = (size_t)tfc_humanfsize(s, &stoi);
230 if (!str_empty(stoi)) {
231 blksize = (size_t)tfc_fnamesize(s, YES);
232 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
233 if (blksize == NOSIZE) xerror(NO, YES, YES,
234 "%s: invalid block size value", s);
236 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
237 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
238 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
239 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
240 "%s: block size exceeds %u bytes",
241 s, TFC_U(TFC_BLKSIZE));
243 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
245 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
246 if (!str_empty(stoi)) {
247 xtsblocks = (size_t)tfc_fnamesize(s, YES);
248 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
249 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
250 "%s: invalid blocks per xts block value", s);
252 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
253 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
254 "%s: nr of blocks per xts block is not round to %u bytes",
255 s, TFC_U(TFC_BLKSIZE));
256 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
257 "%s: nr of blocks per xts block exceeds %u bytes",
258 s, TFC_U(TFC_BLKSIZE));
260 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
262 iseek = tfc_humanfsize(s, &stoi);
263 if (!str_empty(stoi)) {
264 iseek = tfc_fnamesize(s, YES);
265 iseek = tfc_modifysize(iseek, strchr(s, ':'));
266 if (iseek == NOFSIZE) xerror(NO, YES, YES,
267 "%s: invalid iseek value", s);
269 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
270 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
272 "%s: not round to TF block size "
274 s, TFC_U(TF_BLOCK_SIZE));
275 iseek_blocks = iseek / TF_BLOCK_SIZE;
277 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
279 iseek = tfc_humanfsize(s, &stoi);
280 if (!str_empty(stoi)) {
281 iseek = tfc_fnamesize(s, YES);
282 iseek = tfc_modifysize(iseek, strchr(s, ':'));
283 if (iseek == NOFSIZE) xerror(NO, YES, YES,
284 "%s: invalid ixseek value", s);
286 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
288 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
290 iseek_blocks = tfc_humanfsize(s, &stoi);
291 if (!str_empty(stoi)) {
292 iseek_blocks = tfc_fnamesize(s, YES);
293 if (iseek_blocks == NOFSIZE)
295 "%s: invalid ictr value", s);
296 iseek_blocks /= TF_BLOCK_SIZE;
297 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
299 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
301 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
303 iseek_blocks = tfc_humanfsize(s, &stoi);
304 if (!str_empty(stoi)) {
305 iseek_blocks = tfc_fnamesize(s, YES);
306 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
307 if (iseek_blocks == NOFSIZE)
309 "%s: invalid ixctr value", s);
311 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
312 if (iseek_blocks % TF_BLOCK_SIZE)
314 "%s: not round to TF block size "
315 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
316 iseek_blocks /= TF_BLOCK_SIZE;
318 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
320 oseek = tfc_humanfsize(s, &stoi);
321 if (!str_empty(stoi)) {
322 oseek = tfc_fnamesize(s, YES);
323 oseek = tfc_modifysize(oseek, strchr(s, ':'));
324 if (oseek == NOFSIZE) xerror(NO, YES, YES,
325 "%s: invalid oseek value", s);
327 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
329 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
331 maxlen = tfc_humanfsize(s, &stoi);
332 if (!str_empty(stoi)) {
333 maxlen = tfc_fnamesize(s, YES);
334 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
335 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
336 "%s: invalid count value", s);
338 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
339 if (counter_opt == TFC_CTR_HEAD)
340 maxlen += TF_BLOCK_SIZE;
342 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
344 maxkeylen = tfc_humanfsize(s, &stoi);
345 if (!str_empty(stoi)) {
346 maxkeylen = tfc_fnamesize(s, YES);
347 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
348 if (maxkeylen == NOSIZE)
350 "%s: invalid key length value", s);
352 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
354 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
356 keyoffset = tfc_humanfsize(s, &stoi);
357 if (!str_empty(stoi)) {
358 keyoffset = tfc_fnamesize(s, YES);
359 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
360 if (keyoffset == NOFSIZE)
362 "%s: invalid key offset value", s);
364 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
366 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
368 ctrsz = tfc_humanfsize(s, &stoi);
369 if (!str_empty(stoi)) {
370 ctrsz = (size_t)tfc_fnamesize(s, YES);
371 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
374 "%s: invalid counter length value", s);
376 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
377 if (ctrsz > TF_BLOCK_SIZE)
378 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
380 else xerror(NO, YES, YES, "invalid option %s", s);
384 do_mac = TFC_MAC_SIGN;
385 if (strcasecmp(optarg, "mac") != 0)
386 do_mac_file = optarg;
389 do_mac = TFC_MAC_VRFY;
390 if (!strcasecmp(optarg, "drop"))
391 do_mac = TFC_MAC_DROP;
392 else if (strcasecmp(optarg, "mac") != 0)
393 do_mac_file = optarg;
396 if (do_mac != TFC_MAC_VRFY)
397 xerror(NO, YES, YES, "signature source was not specified");
398 do_mac = TFC_MAC_JUST_VRFY;
402 if (maxlen != NOFSIZE) {
403 if (c == 'Z') genzero_nr_bytes = maxlen;
404 else genrandom_nr_bytes = maxlen;
408 if (!strcasecmp(optarg, "cbs"))
410 else if (!strcasecmp(optarg, "ks"))
411 t = TF_FROM_BITS(TFC_KEY_BITS);
412 else if (!strcasecmp(optarg, "xks"))
413 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
414 else if (!strcasecmp(optarg, "iobs"))
417 t = tfc_humanfsize(optarg, &stoi);
418 if (!str_empty(stoi)) {
419 t = tfc_fnamesize(optarg, NO);
420 t = tfc_modifysize(t, strchr(optarg, ':'));
422 else t = tfc_modifysize(t, strchr(optarg, ':'));
424 if (c == 'Z') genzero_nr_bytes = maxlen = t;
425 else genrandom_nr_bytes = maxlen = t;
429 do_preserve_time = YES;
432 do_outfmt = TFC_OUTFMT_B64;
435 do_outfmt = TFC_OUTFMT_RAW;
438 do_outfmt = TFC_OUTFMT_HEX;
449 td = strtod(optarg, &stoi);
450 status_timer = TFC_DTOUSECS(td);
451 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
459 if (!strcmp(progname, "tfbench")) {
460 if (!*(argv+optind)) usage();
462 td = strtod(*(argv+optind), &stoi);
463 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
465 "%s: invalid benchmark time in seconds", *(argv+optind));
466 bench_timer = TFC_DTOUSECS(td);
467 do_benchmark(bench_timer, td);
469 if (genrandom_nr_bytes) {
470 ctr_mode = TFC_MODE_STREAM;
471 do_edcrypt = TFC_DO_ENCRYPT;
472 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
474 if (genzero_nr_bytes) {
475 ctr_mode = TFC_MODE_PLAIN;
476 do_edcrypt = TFC_DO_PLAIN;
477 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
480 if (rawkey && password)
481 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
482 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
483 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
484 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
485 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
486 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
487 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
488 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
489 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
490 if (overwrite_source && counter_opt == TFC_CTR_RAND)
491 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
492 if (tweakf && do_tfcrypt1 == NO)
493 xerror(NO, YES, YES, "Use -T with -t tweakfile to enable old tfcrypt mode!");
494 if (ctr_mode == TFC_MODE_PLAIN
495 && (do_edcrypt || do_mac || rawkey
496 || mackey_opt || counter_opt || counter_file))
497 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
505 memset(tfc_salt, 0, TFC_MAX_SALT);
507 if (!strcasecmp(saltf, "disable")) goto _nosalt;
509 if (!strcmp(saltf, "-")) saltfd = 0;
510 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
511 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
512 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
513 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
519 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
523 if (!strcmp(mackeyf, "-")) mkfd = 0;
524 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
525 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
527 skein_init(&sk, TFC_KEY_BITS);
534 lrem = lblock = sizeof(tmpdata);
535 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
536 _mkragain: lio = xread(mkfd, pblk, lrem);
537 if (lio == 0) do_stop = YES;
538 if (lio != NOSIZE) ldone += lio;
540 if (errno != EIO && catch_all_errors != YES)
541 xerror(NO, NO, NO, "%s", mackeyf);
542 switch (error_action) {
543 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
544 case TFC_ERRACT_SYNC:
545 case TFC_ERRACT_LSYNC:
546 xerror(YES, NO, NO, "%s", mackeyf);
547 lio = ldone = lrem = lblock;
548 memset(tmpdata, 0, lio);
549 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
550 else lseek(mkfd, rdpos + lio, SEEK_SET);
552 default: xerror(NO, NO, NO, "%s", mackeyf); break;
555 if (lio && lio < lrem) {
561 skein_update(&sk, tmpdata, ldone);
564 skein_final(mackey, &sk);
568 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
569 memset(&getps, 0, sizeof(struct getpasswd_state));
570 getps.fd = getps.efd = -1;
571 getps.passwd = pwdask;
572 getps.pwlen = sizeof(pwdask)-1;
573 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
574 getps.charfilter = getps_filter;
575 getps.maskchar = 'x';
576 getps.flags = GETP_WAITFILL;
577 n = xgetpasswd(&getps);
578 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
579 if (n == ((size_t)-2)) xexit(1);
580 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
582 skein(tmpdata, TF_MAX_BITS, NULL, mackey, TF_FROM_BITS(TF_MAX_BITS));
583 xor_shrink(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, tmpdata, TF_FROM_BITS(TF_MAX_BITS));
584 tfc_nfsay(stderr, "MAC password hint: ");
585 mehexdump(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, TF_SIZE_UNIT, 1);
586 memset(tmpdata, 0, sizeof(tmpdata));
591 if ((strlen(progname) <= 9)
592 && ((!strcmp(progname, "sksum"))
593 || ((!memcmp(progname, "sk", 2))
594 && (!memcmp(progname+3, "sum", 3)
595 || !memcmp(progname+4, "sum", 3)
596 || !memcmp(progname+5, "sum", 3)
597 || !memcmp(progname+6, "sum", 3)))))
598 do_sksum(progname, argv+optind);
599 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
604 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
605 if (!strcmp(argv[idx], "-")) kfd = 0;
606 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
607 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
609 lio = strnlen(argv[idx], PATH_MAX);
610 memset(argv[idx], '*', lio);
617 if (do_tfcrypt1 == YES && tweakf) {
620 if (!strcmp(tweakf, "-")) twfd = 0;
621 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
622 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
623 lio = ldone = xread(twfd, key+TF_FROM_BITS(TF_MAX_BITS)+TF_SIZE_UNIT, 2*TF_SIZE_UNIT);
624 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
625 if (ldone < 2*TF_SIZE_UNIT)
626 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, 2*TF_SIZE_UNIT);
633 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
635 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
636 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
637 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
639 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
641 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
642 if (verbose) tfc_esay("%s: disabling signature verification on "
643 "requested partial decryption.", progname);
647 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
648 maxlen = tfc_fdsize(sfd);
649 if (maxlen == NOFSIZE)
651 "Cannot verify embedded MAC with non-seekable source!");
652 maxlen -= TF_FROM_BITS(macbits);
654 srcfname = argv[idx];
658 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
659 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
661 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
662 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
664 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
669 if (!strcmp(counter_file, "-")) ctrfd = 0;
670 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
671 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
672 lio = xread(ctrfd, ctr, ctrsz);
673 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
674 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
677 else if (counter_opt == TFC_CTR_HEAD) {
680 lrem = lblock = ctrsz;
681 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
682 _ctrragain: lio = xread(sfd, pblk, lrem);
683 if (lio != NOSIZE) ldone += lio;
685 if (errno != EIO && catch_all_errors != YES)
686 xerror(NO, NO, NO, "%s", srcfname);
687 switch (error_action) {
688 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
689 case TFC_ERRACT_SYNC:
690 case TFC_ERRACT_LSYNC:
691 xerror(YES, NO, NO, "%s", srcfname);
692 lio = ldone = lrem = lblock;
694 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
695 else lseek(sfd, rdpos + lio, SEEK_SET);
697 default: xerror(NO, NO, NO, "%s", srcfname); break;
700 if (lio && lio < lrem) {
705 total_processed_src += ldone;
710 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
712 if (lseek(sfd, iseek, SEEK_SET) == -1)
713 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
716 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
718 if (verbose) tfc_esay("%s: hashing password", progname);
720 if (rawkey == TFC_RAWKEY_KEYFILE) {
721 tfc_yesno xtskeyset = NO;
725 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
726 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
727 _keyragain: lio = xread(kfd, pblk, lrem);
728 if (lio != NOSIZE) ldone += lio;
730 if (errno != EIO && catch_all_errors != YES)
731 xerror(NO, NO, NO, "reading key");
732 switch (error_action) {
733 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
734 case TFC_ERRACT_SYNC:
735 case TFC_ERRACT_LSYNC:
736 xerror(YES, NO, NO, "reading key");
737 lio = ldone = lrem = lblock;
739 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
740 else lseek(kfd, rdpos + lio, SEEK_SET);
742 default: xerror(NO, NO, NO, "reading key"); break;
745 if (lio && lio < lrem) {
750 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
752 if (ctr_mode == TFC_MODE_XTS) {
753 if (xtskeyset == NO) {
760 else if (rawkey == TFC_RAWKEY_ASKSTR) {
761 tfc_yesno xtskeyset = NO;
763 pblk = key; n = sizeof(key);
764 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
765 getps.fd = getps.efd = -1;
766 getps.passwd = (char *)pblk;
768 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
769 getps.charfilter = getps_filter;
770 getps.maskchar = 'x';
771 getps.flags = GETP_WAITFILL;
772 n = xgetpasswd(&getps);
773 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
774 if (n == ((size_t)-2)) xexit(1);
775 if (ctr_mode == TFC_MODE_XTS) {
776 if (xtskeyset == NO) {
777 pblk = xtskey; n = sizeof(xtskey);
783 else if (rawkey == TFC_RAWKEY_ASKHEX) {
784 tfc_yesno xtskeyset = NO;
788 memset(&getps, 0, sizeof(struct getpasswd_state));
789 getps.fd = getps.efd = -1;
790 getps.passwd = pwdask;
791 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
792 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
793 getps.charfilter = getps_hex_filter;
794 getps.maskchar = 'x';
795 getps.flags = GETP_WAITFILL;
796 n = xgetpasswd(&getps);
797 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
798 if (n == ((size_t)-2)) xexit(1);
800 tfc_esay("Please input even number of hex digits!");
801 goto _rawkey_hex_again;
803 hex2bin(pblk, pwdask);
804 memset(pwdask, 0, sizeof(pwdask));
805 if (ctr_mode == TFC_MODE_XTS) {
806 if (xtskeyset == NO) {
809 goto _rawkey_hex_again;
814 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
815 getps.fd = getps.efd = -1;
816 getps.passwd = pwdask;
817 getps.pwlen = sizeof(pwdask)-1;
818 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
819 getps.charfilter = getps_filter;
820 getps.maskchar = 'x';
821 getps.flags = GETP_WAITFILL;
822 n = xgetpasswd(&getps);
823 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
824 if (n == ((size_t)-2)) xexit(1);
825 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
826 getps.fd = getps.efd = -1;
827 getps.passwd = pwdagain;
828 getps.pwlen = sizeof(pwdagain)-1;
829 getps.echo = "Enter it again: ";
830 getps.flags = GETP_WAITFILL;
831 n = xgetpasswd(&getps);
832 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
833 if (n == ((size_t)-2)) xexit(1);
834 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
835 tfc_esay("Passwords are different, try again");
839 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
840 memset(pwdask, 0, sizeof(pwdask));
841 memset(pwdagain, 0, sizeof(pwdagain));
844 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
845 xerror(NO, NO, YES, "hashing key");
849 if (tfc_saltsz > 0) {
850 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
851 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
853 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
854 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
855 memset(tfc_salt, 0, TFC_MAX_SALT);
858 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
859 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
864 tfc_yesno xtskeyset = NO;
867 if (!strcmp(genkeyf, "-")) krfd = 1;
868 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
869 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
870 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
871 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
872 if (verbose && xtskeyset == NO) {
873 tfc_esay("%s: password hashing done", progname);
874 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
875 tfc_esay("%s: Have a nice day!", progname);
878 if (ctr_mode == TFC_MODE_XTS) {
879 if (xtskeyset == NO) {
891 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
892 if (verbose) tfc_esay("%s: disabling signature verification on "
893 "requested partial decryption.", progname);
898 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
899 if (ctr_mode < TFC_MODE_OCB) {
900 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
901 "will be slower.", progname);
902 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
903 else skein_init(&sk, macbits);
907 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
908 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
912 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
913 if (do_tfcrypt1 == YES) {
914 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));
915 tf_key_tweak_compat(key);
917 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
918 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
919 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
921 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
923 switch (counter_opt) {
926 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
927 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
928 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
931 case TFC_CTR_RAND: tfc_getrandom(ctr, ctrsz); break;
939 if (verbose) tfc_esay("%s: password hashing done", progname);
941 if (overwrite_source && srcfname) argv[idx] = srcfname;
945 if (!strcmp(argv[idx], "-")) dfd = 1;
946 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
948 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
949 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
951 dstfname = argv[idx];
956 if (lseek(dfd, oseek, SEEK_SET) == -1)
957 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
960 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
961 memset(&sigact, 0, sizeof(sigact));
962 sigact.sa_flags = SA_RESTART;
963 sigact.sa_handler = print_crypt_status;
964 sigaction(SIGUSR1, &sigact, NULL);
965 sigaction(SIGTSTP, &sigact, NULL);
966 sigaction(SIGALRM, &sigact, NULL);
967 if (status_timer) setup_next_alarm(status_timer);
968 sigact.sa_handler = change_status_width;
969 sigaction(SIGQUIT, &sigact, NULL);
970 sigact.sa_handler = change_status_timer;
971 sigaction(SIGUSR2, &sigact, NULL);
973 sigact.sa_handler = print_crypt_status;
974 sigaction(SIGINT, &sigact, NULL);
975 sigaction(SIGTERM, &sigact, NULL);
978 sigact.sa_handler = exit_sigterm;
979 sigaction(SIGINT, &sigact, NULL);
980 sigaction(SIGTERM, &sigact, NULL);
982 memset(&sigact, 0, sizeof(struct sigaction));
984 tfc_getcurtime(&delta_time);
987 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
991 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
992 if (lio != NOSIZE) ldone += lio;
993 else xerror(NO, NO, NO, "%s", dstfname);
994 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1000 total_processed_dst += ldone;
1001 delta_processed += ldone;
1010 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1011 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1012 _ragain: lio = xread(sfd, pblk, lrem);
1013 if (lio == 0) do_stop = TFC_STOP_BEGAN;
1014 if (lio != NOSIZE) ldone += lio;
1016 if (errno != EIO && catch_all_errors != YES)
1017 xerror(NO, NO, NO, "%s", srcfname);
1018 switch (error_action) {
1019 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1020 case TFC_ERRACT_SYNC:
1021 case TFC_ERRACT_LSYNC:
1022 xerror(YES, NO, NO, "%s", srcfname);
1023 lio = ldone = lrem = lblock;
1024 memset(srcblk, 0, lio);
1025 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1026 else lseek(sfd, rdpos + lio, SEEK_SET);
1028 default: xerror(NO, NO, NO, "%s", srcfname); break;
1031 if (lio && lio < lrem) {
1036 total_processed_src += ldone;
1038 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1039 size_t orig = ldone;
1040 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1041 if (ldone > blksize) ldone = blksize;
1042 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1045 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1046 skein_update(&sk, srcblk, ldone);
1048 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1049 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1050 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1051 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1052 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1053 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1054 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1055 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1056 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1057 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1058 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1059 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1060 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1061 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1063 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1064 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1065 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1066 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1068 else if (ctr_mode == TFC_MODE_PLAIN)
1069 memcpy(dstblk, srcblk, ldone);
1071 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1072 skein_update(&sk, dstblk, ldone);
1073 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1078 _wagain: lio = xwrite(dfd, pblk, lrem);
1079 if (lio != NOSIZE) ldone += lio;
1080 else xerror(NO, NO, NO, "%s", dstfname);
1081 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1087 _nowrite: total_processed_dst += ldone;
1088 delta_processed += ldone;
1090 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1093 if (do_stop == TFC_STOP_FULL) goto _nomac;
1096 if (do_mac >= TFC_MAC_VRFY) {
1100 lrem = lblock = TF_FROM_BITS(macbits);
1101 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1102 _macragain: lio = xread(sfd, pblk, lrem);
1103 if (lio != NOSIZE) ldone += lio;
1105 if (errno != EIO && catch_all_errors != YES)
1106 xerror(NO, NO, NO, "%s", srcfname);
1107 switch (error_action) {
1108 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1109 case TFC_ERRACT_SYNC:
1110 case TFC_ERRACT_LSYNC:
1111 xerror(YES, NO, NO, "%s", srcfname);
1112 lio = ldone = lrem = lblock;
1113 memset(macvrfy, 0, lio);
1114 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1115 else lseek(sfd, rdpos + lio, SEEK_SET);
1117 default: xerror(NO, NO, NO, "%s", srcfname); break;
1120 if (lio && lio < lrem) {
1125 total_processed_src += ldone;
1130 if (!strcmp(do_mac_file, "-")) mfd = 0;
1131 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1132 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1133 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1134 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1135 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1136 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1137 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1138 lio = TF_FROM_BITS(macbits);
1139 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1141 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1145 if (ldone < TF_FROM_BITS(macbits)) {
1146 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1147 "not verifying", progname, ldone);
1152 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1153 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1155 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1156 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1157 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1158 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1159 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1160 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1162 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1164 tfc_esay("%s: signature is good", progname);
1166 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1167 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1172 if (quiet == NO) tfc_esay("%s: signature is BAD: "
1173 "wrong password, key, mode, or file is not signed", progname);
1177 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1178 memset(macresult, 0, sizeof(macresult));
1179 memset(tmpdata, 0, sizeof(tmpdata));
1182 else if (do_mac == TFC_MAC_SIGN) {
1183 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1184 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1186 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1187 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1188 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1189 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1190 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1191 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1192 memset(macresult, 0, sizeof(macresult));
1196 lio = lrem = TF_FROM_BITS(macbits);
1198 _macwagain: lio = xwrite(dfd, pblk, lrem);
1199 if (lio != NOSIZE) ldone += lio;
1200 else xerror(NO, NO, NO, "%s", dstfname);
1201 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1207 total_processed_dst += ldone;
1208 delta_processed += ldone;
1213 if (!strcmp(do_mac_file, "-")) mfd = 1;
1214 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1215 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1216 if (do_outfmt == TFC_OUTFMT_B64) {
1217 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1218 memset(tmpdata, 0, TFC_TMPSIZE);
1219 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1220 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1221 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1223 tmpdata[lrem] = '\n';
1226 lio = xwrite(mfd, tmpdata, lrem);
1228 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1229 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1230 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1234 memset(macvrfy, 0, sizeof(macvrfy));
1235 memset(macresult, 0, sizeof(macresult));
1236 memset(tmpdata, 0, sizeof(tmpdata));
1240 if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1242 if (do_preserve_time) fcopy_matime(dfd, &s_stat);