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, "aU:C:r:K:t:TPkzxc:l:qedn:vV:pwE:O:S:AmM:R:Z:WHD:")) != -1) {
72 if (!strcasecmp(optarg, "show"))
73 counter_opt = TFC_CTR_SHOW;
74 else if (!strcasecmp(optarg, "head"))
75 counter_opt = TFC_CTR_HEAD;
76 else if (!strcasecmp(optarg, "rand"))
77 counter_opt = TFC_CTR_RAND;
78 else counter_file = sksum_hashlist_file = optarg;
81 if (!strcasecmp(optarg, "ctr"))
82 ctr_mode = TFC_MODE_CTR;
83 else if (!strcasecmp(optarg, "stream"))
84 ctr_mode = TFC_MODE_STREAM;
85 else if (!strcasecmp(optarg, "cbc"))
86 ctr_mode = TFC_MODE_CBC;
87 else if (!strcasecmp(optarg, "ecb"))
88 ctr_mode = TFC_MODE_ECB;
89 else if (!strcasecmp(optarg, "xts"))
90 ctr_mode = TFC_MODE_XTS;
91 else if (!strcasecmp(optarg, "ocb"))
92 ctr_mode = TFC_MODE_OCB;
93 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
96 do_edcrypt = TFC_DO_PLAIN;
98 ctr_mode = TFC_MODE_PLAIN;
101 do_edcrypt = TFC_DO_ENCRYPT;
104 do_edcrypt = TFC_DO_DECRYPT;
107 macbits = strtoul(optarg, &stoi, 10);
108 if (macbits == 0 || !str_empty(stoi) || macbits < 8
109 || macbits > TF_MAX_BITS || macbits % 8)
110 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
113 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
114 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
117 if (!strcasecmp(optarg, "key"))
118 mackey_opt = TFC_MACKEY_RAWKEY;
119 else if (!strcasecmp(optarg, "pwd"))
120 mackey_opt = TFC_MACKEY_PASSWORD;
122 mackey_opt = TFC_MACKEY_FILE;
130 rawkey = TFC_RAWKEY_KEYFILE;
133 rawkey = TFC_RAWKEY_ASKSTR;
136 rawkey = TFC_RAWKEY_ASKHEX;
149 if (maxlen != NOFSIZE) break;
151 maxlen = tfc_humanfsize(optarg, &stoi);
152 if (!str_empty(stoi)) {
153 maxlen = tfc_fnamesize(optarg, YES);
154 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
155 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
156 "%s: invalid count value", optarg);
158 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
159 if (counter_opt == TFC_CTR_HEAD)
160 maxlen += TF_BLOCK_SIZE;
163 overwrite_source = YES;
166 if (!strcmp(optarg, "xall")) {
167 catch_all_errors = YES;
170 if (!strcmp(optarg, "exit"))
171 error_action = TFC_ERRACT_EXIT;
172 else if (!strncmp(optarg, "cont", 4))
173 error_action = TFC_ERRACT_CONT;
174 else if (!strcmp(optarg, "sync"))
175 error_action = TFC_ERRACT_SYNC;
176 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
179 s = d = optarg; t = NULL;
180 while ((s = strtok_r(d, ",", &t))) {
182 if (!strcmp(s, "sync"))
183 write_flags |= O_SYNC;
184 else if (!strcmp(s, "trunc"))
185 write_flags |= O_TRUNC;
186 else if (!strcmp(s, "fsync"))
188 else if (!strcmp(s, "pad"))
190 else if (!strcmp(s, "xtime"))
191 do_preserve_time = YES;
192 else if (!strcmp(s, "gibsize"))
193 do_stats_in_gibs = YES;
194 else if (!strcmp(s, "plainstats"))
195 do_statline_dynamic = NO;
196 else if (!strcmp(s, "statless"))
198 else if (!strcmp(s, "norepeat"))
200 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
202 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
203 mac_pw_prompt = s+10;
204 else if (!strcmp(s, "shorthex"))
205 do_full_hexdump = NO;
206 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
208 blksize = (size_t)tfc_humanfsize(s, &stoi);
209 if (!str_empty(stoi)) {
210 blksize = (size_t)tfc_fnamesize(s, YES);
211 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
212 if (blksize == NOSIZE) xerror(NO, YES, YES,
213 "%s: invalid block size value", s);
215 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
216 if (blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
217 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
218 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
219 "%s: block size exceeds %u bytes",
220 s, TFC_U(TFC_BLKSIZE));
222 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
224 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
225 if (!str_empty(stoi)) {
226 xtsblocks = (size_t)tfc_fnamesize(s, YES);
227 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
228 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
229 "%s: invalid blocks per xts block value", s);
231 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
232 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
233 "%s: nr of blocks per xts block is not round to %u bytes",
234 s, TFC_U(TFC_BLKSIZE));
235 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
236 "%s: nr of blocks per xts block exceeds %u bytes",
237 s, TFC_U(TFC_BLKSIZE));
239 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
241 iseek = tfc_humanfsize(s, &stoi);
242 if (!str_empty(stoi)) {
243 iseek = tfc_fnamesize(s, YES);
244 iseek = tfc_modifysize(iseek, strchr(s, ':'));
245 if (iseek == NOFSIZE) xerror(NO, YES, YES,
246 "%s: invalid iseek value", s);
248 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
249 if (ctr_mode != TFC_MODE_PLAIN && iseek % TF_BLOCK_SIZE)
251 "%s: not round to TF block size "
253 s, TFC_U(TF_BLOCK_SIZE));
254 iseek_blocks = iseek / TF_BLOCK_SIZE;
256 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
258 iseek = tfc_humanfsize(s, &stoi);
259 if (!str_empty(stoi)) {
260 iseek = tfc_fnamesize(s, YES);
261 iseek = tfc_modifysize(iseek, strchr(s, ':'));
262 if (iseek == NOFSIZE) xerror(NO, YES, YES,
263 "%s: invalid ixseek value", s);
265 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
267 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
269 iseek_blocks = tfc_humanfsize(s, &stoi);
270 if (!str_empty(stoi)) {
271 iseek_blocks = tfc_fnamesize(s, YES);
272 if (iseek_blocks == NOFSIZE)
274 "%s: invalid ictr value", s);
275 iseek_blocks /= TF_BLOCK_SIZE;
276 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
278 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
280 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
282 iseek_blocks = tfc_humanfsize(s, &stoi);
283 if (!str_empty(stoi)) {
284 iseek_blocks = tfc_fnamesize(s, YES);
285 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
286 if (iseek_blocks == NOFSIZE)
288 "%s: invalid ixctr value", s);
290 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
291 if (iseek_blocks % TF_BLOCK_SIZE)
293 "%s: not round to TF block size "
294 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
295 iseek_blocks /= TF_BLOCK_SIZE;
297 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
299 oseek = tfc_humanfsize(s, &stoi);
300 if (!str_empty(stoi)) {
301 oseek = tfc_fnamesize(s, YES);
302 oseek = tfc_modifysize(oseek, strchr(s, ':'));
303 if (oseek == NOFSIZE) xerror(NO, YES, YES,
304 "%s: invalid oseek value", s);
306 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
308 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
310 maxlen = tfc_humanfsize(s, &stoi);
311 if (!str_empty(stoi)) {
312 maxlen = tfc_fnamesize(s, YES);
313 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
314 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
315 "%s: invalid count value", s);
317 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
318 if (counter_opt == TFC_CTR_HEAD)
319 maxlen += TF_BLOCK_SIZE;
321 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
323 maxkeylen = tfc_humanfsize(s, &stoi);
324 if (!str_empty(stoi)) {
325 maxkeylen = (size_t)tfc_fnamesize(s, YES);
326 maxkeylen = (size_t)tfc_modifysize((tfc_fsize)maxkeylen, strchr(s, ':'));
327 if (maxkeylen == NOSIZE)
329 "%s: invalid key length value", s);
331 else maxkeylen = (size_t)tfc_modifysize((tfc_fsize)maxkeylen, strchr(s, ':'));
333 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
335 ctrsz = tfc_humanfsize(s, &stoi);
336 if (!str_empty(stoi)) {
337 ctrsz = (size_t)tfc_fnamesize(s, YES);
338 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
341 "%s: invalid counter length value", s);
343 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
344 if (ctrsz > TF_BLOCK_SIZE)
345 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
347 else xerror(NO, YES, YES, "invalid option %s", s);
351 do_mac = TFC_MAC_SIGN;
352 if (strcasecmp(optarg, "mac") != 0)
353 do_mac_file = optarg;
356 do_mac = TFC_MAC_VRFY;
357 if (!strcasecmp(optarg, "drop"))
358 do_mac = TFC_MAC_DROP;
359 else if (strcasecmp(optarg, "mac") != 0)
360 do_mac_file = optarg;
363 if (do_mac != TFC_MAC_VRFY)
364 xerror(NO, YES, YES, "signature source was not specified");
365 do_mac = TFC_MAC_JUST_VRFY;
369 if (maxlen != NOFSIZE) {
370 if (c == 'Z') genzero_nr_bytes = maxlen;
371 else genrandom_nr_bytes = maxlen;
375 if (!strcasecmp(optarg, "cbs"))
377 else if (!strcasecmp(optarg, "ks"))
378 t = TF_FROM_BITS(TFC_KEY_BITS);
379 else if (!strcasecmp(optarg, "xks"))
380 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
381 else if (!strcasecmp(optarg, "iobs"))
384 t = tfc_humanfsize(optarg, &stoi);
385 if (!str_empty(stoi)) {
386 t = tfc_fnamesize(optarg, NO);
387 t = tfc_modifysize(t, strchr(optarg, ':'));
389 else t = tfc_modifysize(t, strchr(optarg, ':'));
391 if (c == 'Z') genzero_nr_bytes = maxlen = t;
392 else genrandom_nr_bytes = maxlen = t;
396 do_preserve_time = YES;
399 do_outfmt = TFC_OUTFMT_B64;
402 do_outfmt = TFC_OUTFMT_RAW;
405 do_outfmt = TFC_OUTFMT_HEX;
416 td = strtod(optarg, &stoi);
417 status_timer = TFC_DTOUSECS(td);
418 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
426 if (!strcmp(progname, "tfbench")) {
427 if (!*(argv+optind)) usage();
429 td = strtod(*(argv+optind), &stoi);
430 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
432 "%s: invalid benchmark time in seconds", *(argv+optind));
433 bench_timer = TFC_DTOUSECS(td);
434 do_benchmark(bench_timer, td);
436 if (genrandom_nr_bytes) {
437 ctr_mode = TFC_MODE_STREAM;
438 do_edcrypt = TFC_DO_ENCRYPT;
439 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
441 if (genzero_nr_bytes) {
442 ctr_mode = TFC_MODE_PLAIN;
443 do_edcrypt = TFC_DO_PLAIN;
444 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
447 if (rawkey && password)
448 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
449 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
450 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
451 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
452 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
453 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
454 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
455 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
456 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
457 if (overwrite_source && counter_opt == TFC_CTR_RAND)
458 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
459 if (tweakf && do_tfcrypt1 == NO)
460 xerror(NO, YES, YES, "Use -T with -t tweakfile to enable old tfcrypt mode!");
461 if (ctr_mode == TFC_MODE_PLAIN
462 && (do_edcrypt || do_mac || rawkey
463 || mackey_opt || counter_opt || counter_file))
464 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
469 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
473 if (!strcmp(mackeyf, "-")) mkfd = 0;
474 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
475 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
477 skein_init(&sk, TFC_KEY_BITS);
484 lrem = lblock = sizeof(tmpdata);
485 _mkragain: lio = read(mkfd, pblk, lrem);
486 if (lio == 0) do_stop = YES;
487 if (lio != NOSIZE) ldone += lio;
489 if (errno != EIO && catch_all_errors != YES)
490 xerror(NO, NO, NO, "%s", mackeyf);
491 switch (error_action) {
492 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
493 case TFC_ERRACT_SYNC:
494 xerror(YES, NO, NO, "%s", mackeyf);
495 lio = ldone = lrem = lblock;
496 memset(tmpdata, 0, lio);
497 lseek(mkfd, lio, SEEK_CUR);
499 default: xerror(NO, NO, NO, "%s", mackeyf); break;
502 if (lio && lio < lrem) {
508 skein_update(&sk, tmpdata, ldone);
511 skein_final(mackey, &sk);
515 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
516 memset(&getps, 0, sizeof(struct getpasswd_state));
517 getps.fd = getps.efd = -1;
518 getps.passwd = pwdask;
519 getps.pwlen = sizeof(pwdask)-1;
520 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
521 getps.charfilter = getps_filter;
522 getps.maskchar = 'x';
523 getps.flags = GETP_WAITFILL;
524 n = xgetpasswd(&getps);
525 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
526 if (n == ((size_t)-2)) xexit(1);
527 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
529 skein(tmpdata, TF_MAX_BITS, NULL, mackey, TF_FROM_BITS(TF_MAX_BITS));
530 xor_shrink(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, tmpdata, TF_FROM_BITS(TF_MAX_BITS));
531 tfc_nfsay(stderr, "MAC password hint: ");
532 mehexdump(tmpdata+TF_FROM_BITS(TF_MAX_BITS), TF_SIZE_UNIT, TF_SIZE_UNIT, 1);
533 memset(tmpdata, 0, sizeof(tmpdata));
538 if ((strlen(progname) <= 9)
539 && ((!strcmp(progname, "sksum"))
540 || ((!memcmp(progname, "sk", 2))
541 && (!memcmp(progname+3, "sum", 3)
542 || !memcmp(progname+4, "sum", 3)
543 || !memcmp(progname+5, "sum", 3)
544 || !memcmp(progname+6, "sum", 3)))))
545 do_sksum(progname, argv+optind);
546 if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
551 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
552 if (!strcmp(argv[idx], "-")) kfd = 0;
553 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
554 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
556 lio = strnlen(argv[idx], PATH_MAX);
557 memset(argv[idx], '*', lio);
564 if (do_tfcrypt1 == YES && tweakf) {
567 if (!strcmp(tweakf, "-")) twfd = 0;
568 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
569 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
570 lio = ldone = read(twfd, key+TF_FROM_BITS(TF_MAX_BITS)+TF_SIZE_UNIT, 2*TF_SIZE_UNIT);
571 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
572 if (ldone < 2*TF_SIZE_UNIT)
573 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, 2*TF_SIZE_UNIT);
580 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
582 sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
583 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
584 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
586 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
588 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
589 if (verbose) tfc_esay("%s: disabling signature verification on "
590 "requested partial decryption.", progname);
594 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
595 maxlen = tfc_fdsize(sfd);
596 if (maxlen == NOFSIZE)
598 "Cannot verify embedded MAC with non-seekable source!");
599 maxlen -= TF_FROM_BITS(macbits);
601 srcfname = argv[idx];
605 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
606 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
608 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
609 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
611 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
616 if (!strcmp(counter_file, "-")) ctrfd = 0;
617 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
618 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
619 lio = read(ctrfd, ctr, ctrsz);
620 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
621 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
624 else if (counter_opt == TFC_CTR_HEAD) {
627 lrem = lblock = ctrsz;
628 _ctrragain: lio = read(sfd, pblk, lrem);
629 if (lio != NOSIZE) ldone += lio;
631 if (errno != EIO && catch_all_errors != YES)
632 xerror(NO, NO, NO, "%s", srcfname);
633 switch (error_action) {
634 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
635 case TFC_ERRACT_SYNC:
636 xerror(YES, NO, NO, "%s", srcfname);
637 lio = ldone = lrem = lblock;
639 lseek(sfd, lio, SEEK_CUR);
641 default: xerror(NO, NO, NO, "%s", srcfname); break;
644 if (lio && lio < lrem) {
649 total_processed_src += ldone;
654 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
656 if (lseek(sfd, iseek, SEEK_SET) == -1)
657 xerror(YES, NO, NO, "%s: seek failed", srcfname);
660 if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
662 if (verbose) tfc_esay("%s: hashing password", progname);
664 if (rawkey == TFC_RAWKEY_KEYFILE) {
665 tfc_yesno xtskeyset = NO;
669 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
670 _keyragain: lio = read(kfd, pblk, lrem);
671 if (lio != NOSIZE) ldone += lio;
673 if (errno != EIO && catch_all_errors != YES)
674 xerror(NO, NO, NO, "reading key");
675 switch (error_action) {
676 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
677 case TFC_ERRACT_SYNC:
678 xerror(YES, NO, NO, "reading key");
679 lio = ldone = lrem = lblock;
681 lseek(kfd, lio, SEEK_CUR);
683 default: xerror(NO, NO, NO, "reading key"); break;
686 if (lio && lio < lrem) {
691 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
693 if (ctr_mode == TFC_MODE_XTS) {
694 if (xtskeyset == NO) {
701 else if (rawkey == TFC_RAWKEY_ASKSTR) {
702 tfc_yesno xtskeyset = NO;
704 pblk = key; n = sizeof(key);
705 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
706 getps.fd = getps.efd = -1;
707 getps.passwd = (char *)pblk;
709 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
710 getps.charfilter = getps_filter;
711 getps.maskchar = 'x';
712 getps.flags = GETP_WAITFILL;
713 n = xgetpasswd(&getps);
714 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
715 if (n == ((size_t)-2)) xexit(1);
716 if (ctr_mode == TFC_MODE_XTS) {
717 if (xtskeyset == NO) {
718 pblk = xtskey; n = sizeof(xtskey);
724 else if (rawkey == TFC_RAWKEY_ASKHEX) {
725 tfc_yesno xtskeyset = NO;
729 memset(&getps, 0, sizeof(struct getpasswd_state));
730 getps.fd = getps.efd = -1;
731 getps.passwd = pwdask;
732 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
733 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
734 getps.charfilter = getps_hex_filter;
735 getps.maskchar = 'x';
736 getps.flags = GETP_WAITFILL;
737 n = xgetpasswd(&getps);
738 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
739 if (n == ((size_t)-2)) xexit(1);
741 tfc_esay("Please input even number of hex digits!");
742 goto _rawkey_hex_again;
744 hex2bin(pblk, pwdask);
745 memset(pwdask, 0, sizeof(pwdask));
746 if (ctr_mode == TFC_MODE_XTS) {
747 if (xtskeyset == NO) {
750 goto _rawkey_hex_again;
755 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
756 getps.fd = getps.efd = -1;
757 getps.passwd = pwdask;
758 getps.pwlen = sizeof(pwdask)-1;
759 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
760 getps.charfilter = getps_filter;
761 getps.maskchar = 'x';
762 getps.flags = GETP_WAITFILL;
763 n = xgetpasswd(&getps);
764 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
765 if (n == ((size_t)-2)) xexit(1);
766 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
767 getps.fd = getps.efd = -1;
768 getps.passwd = pwdagain;
769 getps.pwlen = sizeof(pwdagain)-1;
770 getps.echo = "Enter it again: ";
771 getps.flags = GETP_WAITFILL;
772 n = xgetpasswd(&getps);
773 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
774 if (n == ((size_t)-2)) xexit(1);
775 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
776 tfc_esay("Passwords are different, try again");
780 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
781 memset(pwdask, 0, sizeof(pwdask));
782 memset(pwdagain, 0, sizeof(pwdagain));
785 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, maxkeylen) != YES)
786 xerror(NO, NO, YES, "hashing key");
789 if (nr_turns > 1 && rawkey == NO) {
790 for (x = 0; x < nr_turns; x++)
791 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
794 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
795 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
800 tfc_yesno xtskeyset = NO;
803 if (!strcmp(genkeyf, "-")) krfd = 1;
804 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
805 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
806 _xts2genkey: if (write(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == -1) xerror(NO, NO, YES, "%s", genkeyf);
807 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
808 if (verbose && xtskeyset == NO) {
809 tfc_esay("%s: password hashing done", progname);
810 tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
811 tfc_esay("%s: Have a nice day!", progname);
814 if (ctr_mode == TFC_MODE_XTS) {
815 if (xtskeyset == NO) {
827 if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
828 if (verbose) tfc_esay("%s: disabling signature verification on "
829 "requested partial decryption.", progname);
834 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
835 if (ctr_mode < TFC_MODE_OCB) {
836 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
837 "will be slower.", progname);
838 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
839 else skein_init(&sk, macbits);
843 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
844 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
848 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
849 if (do_tfcrypt1 == YES) {
850 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));
851 tf_key_tweak_compat(key);
853 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
854 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
855 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
856 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
858 switch (counter_opt) {
861 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
862 case TFC_OUTFMT_RAW: write(2, ctr, ctrsz); break;
863 case TFC_OUTFMT_HEX: mhexdump(ctr, ctrsz, ctrsz, YES); break;
866 case TFC_CTR_RAND: tfc_getrandom(ctr, ctrsz); break;
874 if (verbose) tfc_esay("%s: password hashing done", progname);
876 if (overwrite_source && srcfname) argv[idx] = srcfname;
880 if (!strcmp(argv[idx], "-")) dfd = 1;
881 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
883 dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
884 if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
886 dstfname = argv[idx];
891 if (lseek(dfd, oseek, SEEK_SET) == -1)
892 xerror(YES, NO, NO, "%s: seek failed", dstfname);
895 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
896 memset(&sigact, 0, sizeof(sigact));
897 sigact.sa_flags = SA_RESTART;
898 sigact.sa_handler = print_crypt_status;
899 sigaction(SIGUSR1, &sigact, NULL);
900 sigaction(SIGTSTP, &sigact, NULL);
901 sigaction(SIGALRM, &sigact, NULL);
902 if (status_timer) setup_next_alarm(status_timer);
903 sigact.sa_handler = change_status_width;
904 sigaction(SIGQUIT, &sigact, NULL);
905 sigact.sa_handler = change_status_timer;
906 sigaction(SIGUSR2, &sigact, NULL);
908 sigact.sa_handler = print_crypt_status;
909 sigaction(SIGINT, &sigact, NULL);
910 sigaction(SIGTERM, &sigact, NULL);
913 sigact.sa_handler = exit_sigterm;
914 sigaction(SIGINT, &sigact, NULL);
915 sigaction(SIGTERM, &sigact, NULL);
917 memset(&sigact, 0, sizeof(struct sigaction));
919 tfc_getcurtime(&delta_time);
922 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
926 _ctrwagain: lio = write(dfd, pblk, lrem);
927 if (lio != NOSIZE) ldone += lio;
928 else xerror(NO, NO, NO, "%s", dstfname);
929 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
935 total_processed_dst += ldone;
936 delta_processed += ldone;
945 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
946 _ragain: lio = read(sfd, pblk, lrem);
947 if (lio == 0) do_stop = TFC_STOP_BEGAN;
948 if (lio != NOSIZE) ldone += lio;
950 if (errno != EIO && catch_all_errors != YES)
951 xerror(NO, NO, NO, "%s", srcfname);
952 switch (error_action) {
953 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
954 case TFC_ERRACT_SYNC:
955 xerror(YES, NO, NO, "%s", srcfname);
956 lio = ldone = lrem = lblock;
957 memset(srcblk, 0, lio);
958 lseek(sfd, lio, SEEK_CUR);
960 default: xerror(NO, NO, NO, "%s", srcfname); break;
963 if (lio && lio < lrem) {
968 total_processed_src += ldone;
970 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
972 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
973 if (ldone > blksize) ldone = blksize;
974 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
977 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
978 skein_update(&sk, srcblk, ldone);
980 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
981 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
982 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
983 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
984 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
985 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
986 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
987 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
988 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
989 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
990 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
991 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
992 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
993 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
995 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
996 tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
997 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
998 tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1000 else if (ctr_mode == TFC_MODE_PLAIN)
1001 memcpy(dstblk, srcblk, ldone);
1003 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1004 skein_update(&sk, dstblk, ldone);
1005 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1010 _wagain: lio = write(dfd, pblk, lrem);
1011 if (lio != NOSIZE) ldone += lio;
1012 else xerror(NO, NO, NO, "%s", dstfname);
1013 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1019 _nowrite: total_processed_dst += ldone;
1020 delta_processed += ldone;
1022 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1025 if (do_stop == TFC_STOP_FULL) goto _nomac;
1028 if (do_mac >= TFC_MAC_VRFY) {
1032 lrem = lblock = TF_FROM_BITS(macbits);
1033 _macragain: lio = read(sfd, pblk, lrem);
1034 if (lio != NOSIZE) ldone += lio;
1036 if (errno != EIO && catch_all_errors != YES)
1037 xerror(NO, NO, NO, "%s", srcfname);
1038 switch (error_action) {
1039 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1040 case TFC_ERRACT_SYNC:
1041 xerror(YES, NO, NO, "%s", srcfname);
1042 lio = ldone = lrem = lblock;
1043 memset(macvrfy, 0, lio);
1044 lseek(sfd, lio, SEEK_CUR);
1046 default: xerror(NO, NO, NO, "%s", srcfname); break;
1049 if (lio && lio < lrem) {
1054 total_processed_src += ldone;
1059 if (!strcmp(do_mac_file, "-")) mfd = 0;
1060 else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1061 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1062 lio = ldone = read(mfd, tmpdata, sizeof(tmpdata));
1063 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1064 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1065 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1066 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1067 lio = TF_FROM_BITS(macbits);
1068 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1070 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1074 if (ldone < TF_FROM_BITS(macbits)) {
1075 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1076 "not verifying", progname, ldone);
1081 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1082 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1084 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1085 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1086 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1087 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1088 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1089 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1091 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1093 tfc_esay("%s: signature is good", progname);
1095 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1096 else mhexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1101 if (quiet == NO) tfc_esay("%s: signature is BAD: "
1102 "wrong password, key, mode, or file is not signed", progname);
1106 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1107 memset(macresult, 0, sizeof(macresult));
1108 memset(tmpdata, 0, sizeof(tmpdata));
1111 else if (do_mac == TFC_MAC_SIGN) {
1112 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1113 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1115 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1116 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1117 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1118 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1119 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1120 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1121 memset(macresult, 0, sizeof(macresult));
1125 lio = lrem = TF_FROM_BITS(macbits);
1127 _macwagain: lio = write(dfd, pblk, lrem);
1128 if (lio != NOSIZE) ldone += lio;
1129 else xerror(NO, NO, NO, "%s", dstfname);
1130 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1136 total_processed_dst += ldone;
1137 delta_processed += ldone;
1142 if (!strcmp(do_mac_file, "-")) mfd = 1;
1143 else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1144 if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1145 if (do_outfmt == TFC_OUTFMT_B64) {
1146 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1147 memset(tmpdata, 0, TFC_TMPSIZE);
1148 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1149 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1150 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1152 tmpdata[lrem] = '\n';
1155 lio = write(mfd, tmpdata, lrem);
1157 else lio = write(mfd, tmpdata, TF_FROM_BITS(macbits));
1158 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1159 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1163 memset(macvrfy, 0, sizeof(macvrfy));
1164 memset(macresult, 0, sizeof(macresult));
1165 memset(tmpdata, 0, sizeof(tmpdata));
1169 if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1171 if (do_preserve_time) fcopy_matime(dfd, &s_stat);