2 * tfcrypt -- high security Threefish encryption tool.
4 * tfcrypt is copyrighted:
5 * Copyright (C) 2012-2019 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 tfc_byte svctr[TF_BLOCK_SIZE];
32 static tfc_fsize rwd, do_read_loops, loopcnt;
33 static tfc_yesno unbuffered;
35 static void open_log(const char *logfile)
40 if (!strcmp(logfile, "-")) return;
44 fd = xopen(logfile, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC);
47 if (dup2(fd, 2) == -1) xexit(2);
49 do_statline_dynamic = NO;
52 static int getps_filter(struct getpasswd_state *getps, char chr, size_t pos)
55 getps->retn = ((size_t)-2);
61 static int getps_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
64 getps->retn = ((size_t)-2);
67 if (chr >= '0' && chr <= '9') return 1;
68 if (chr >= 'a' && chr <= 'f') return 1;
69 if (chr >= 'A' && chr <= 'F') return 1;
70 if (chr == '\x7f' || chr == '\x08'
71 || chr == '\x15' || chr == '\x17') return 1;
75 static inline int isctrlchr(int c)
78 if (c >= 0 && c <= 31) return 1;
79 if (c == 127) return 1;
83 static int getps_plain_filter(struct getpasswd_state *getps, char chr, size_t pos)
87 x = getps_filter(getps, chr, pos);
90 if (pos < getps->pwlen && !isctrlchr(chr))
91 write(getps->efd, &chr, sizeof(char));
95 static int getps_plain_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
99 x = getps_hex_filter(getps, chr, pos);
100 if (x != 1) return x;
102 if (pos < getps->pwlen && !isctrlchr(chr))
103 write(getps->efd, &chr, sizeof(char));
107 static void make_hint(void *hint, size_t szhint, const void *data, size_t szdata)
109 char t[TF_FROM_BITS(TF_MAX_BITS)];
111 skein(t, TF_MAX_BITS, NULL, data, szdata);
112 xor_shrink(hint, szhint, t, sizeof(t));
113 memset(t, 0, sizeof(t));
116 static void raw_say_hint(void *hint, size_t szhint, const void *data, size_t szdata, const char *prompt)
118 make_hint(hint, szhint, data, szdata);
119 if (prompt) tfc_nfsay(stderr, "%s: ", prompt);
120 mehexdump(hint, szhint, szhint, 1);
121 memset(hint, 0, szhint);
124 static void say_hint(const void *data, size_t szdata, const char *prompt)
126 char t[TF_SIZE_UNIT];
127 raw_say_hint(t, TF_SIZE_UNIT, data, szdata, prompt);
128 /* t[] is erased (automatically) */
131 int main(int argc, char **argv)
136 char *s, *d, *t, *stoi;
140 progname = basename(argv[0]);
142 if (!isatty(2)) do_statline_dynamic = NO;
147 n = PATH_MAX > sizeof(srcblk) ? sizeof(srcblk) : PATH_MAX;
148 if (xstrlcpy(s, d, n) >= n) goto _baddfname;
149 if (xstrlcat(s, "/.tfcrypt.defs", n) >= n) goto _baddfname;
150 read_defaults(s, YES);
154 if (!strcmp(progname, "iotool")) {
155 do_edcrypt = TFC_DO_PLAIN;
156 ctr_mode = TFC_MODE_PLAIN;
159 if (!strcmp(progname, "xor")) {
160 do_edcrypt = TFC_DO_PLAIN;
161 ctr_mode = TFC_MODE_XOR;
162 /* xor: default to stdin if invoked without args */
167 while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:PXkzxc:l:qedn:vV:pwE:o:O:S:AmuM:R:Z:WHD:gj")) != -1) {
170 read_defaults(optarg, NO);
180 if (c == 'j') ctr_mode = TFC_MODE_CTR;
181 else if (c == 'g') ctr_mode = TFC_MODE_STREAM;
182 if (do_edcrypt == TFC_DO_DECRYPT) counter_opt = TFC_CTR_HEAD;
183 else if (do_edcrypt == TFC_DO_ENCRYPT) counter_opt = TFC_CTR_RAND;
184 else xerror(NO, YES, YES, "plain mode was selected with -%c, cannot continue", c);
187 if (!strcasecmp(optarg, "show"))
188 counter_opt = TFC_CTR_SHOW;
189 else if (!strcasecmp(optarg, "head"))
190 counter_opt = TFC_CTR_HEAD;
191 else if (!strcasecmp(optarg, "rand"))
192 counter_opt = TFC_CTR_RAND;
193 else if (!strcasecmp(optarg, "zero"))
194 counter_opt = TFC_CTR_ZERO;
195 else if (strchr(optarg, ':')) {
198 counter_opt = TFC_CTR_SSET;
201 s = d = optarg; t = NULL;
202 while ((s = strtok_r(d, ",", &t))) {
209 chr = (char)strtoul(s, &stoi, 16);
210 if (!str_empty(stoi)) continue;
211 x = (size_t)strtoul(ss, &stoi, 10);
212 if (!str_empty(stoi)) continue;
214 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
218 else counter_file = sksum_hashlist_file = optarg;
221 if (!strcasecmp(optarg, "ctr"))
222 ctr_mode = TFC_MODE_CTR;
223 else if (!strcasecmp(optarg, "stream"))
224 ctr_mode = TFC_MODE_STREAM;
225 else if (!strcasecmp(optarg, "cbc"))
226 ctr_mode = TFC_MODE_CBC;
227 else if (!strcasecmp(optarg, "pcbc"))
228 ctr_mode = TFC_MODE_PCBC;
229 else if (!strcasecmp(optarg, "ecb"))
230 ctr_mode = TFC_MODE_ECB;
231 else if (!strcasecmp(optarg, "xts"))
232 ctr_mode = TFC_MODE_XTS;
233 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
237 do_edcrypt = TFC_DO_PLAIN;
239 ctr_mode = TFC_MODE_XOR;
240 /* xor: default to stdin if invoked without args */
243 else ctr_mode = TFC_MODE_PLAIN;
246 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
249 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
252 macbits = strtoul(optarg, &stoi, 10);
253 if (macbits == 0 || !str_empty(stoi) || macbits < 8
254 || macbits > TF_MAX_BITS || macbits % 8)
255 xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
258 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
259 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
262 if (!strcasecmp(optarg, "key"))
263 mackey_opt = TFC_MACKEY_RAWKEY;
264 else if (!strcasecmp(optarg, "pwd"))
265 mackey_opt = TFC_MACKEY_PASSWORD;
267 mackey_opt = TFC_MACKEY_FILE;
275 rawkey = TFC_RAWKEY_KEYFILE;
278 rawkey = TFC_RAWKEY_ASKSTR;
281 rawkey = TFC_RAWKEY_ASKHEX;
292 if (maxlen != NOFSIZE) break;
294 maxlen = tfc_humanfsize(optarg, &stoi);
295 if (!str_empty(stoi)) {
296 maxlen = tfc_fnamesize(optarg, YES);
297 maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
298 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
299 "%s: invalid count value", optarg);
301 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
302 if (counter_opt == TFC_CTR_HEAD)
303 maxlen += TF_BLOCK_SIZE;
306 overwrite_source = YES;
309 if (!strcmp(optarg, "xall")) {
310 catch_all_errors = YES;
313 if (!strcmp(optarg, "xseek")) {
314 ignore_seek_errors = YES;
317 if (!strcmp(optarg, "exit"))
318 error_action = TFC_ERRACT_EXIT;
319 else if (!strncmp(optarg, "cont", 4))
320 error_action = TFC_ERRACT_CONT;
321 else if (!strcmp(optarg, "sync"))
322 error_action = TFC_ERRACT_SYNC;
323 else if (!strcmp(optarg, "lsync"))
324 error_action = TFC_ERRACT_LSYNC;
325 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
331 s = d = optarg; t = NULL;
332 while ((s = strtok_r(d, ",", &t))) {
334 if (!strcmp(s, "sync"))
335 write_flags |= O_SYNC;
336 else if (!strcmp(s, "trunc"))
337 write_flags |= O_TRUNC;
338 else if (!strcmp(s, "append"))
339 write_flags |= O_APPEND;
340 else if (!strcmp(s, "fsync"))
342 else if (!strcmp(s, "pad"))
344 else if (!strcmp(s, "ro"))
346 else if (!strcmp(s, "rw"))
348 else if (!strcmp(s, "xtime"))
349 do_preserve_time = YES;
350 else if (!strcmp(s, "gibsize"))
351 do_stats_in_gibs = YES;
352 else if (!strcmp(s, "plainstats"))
353 do_statline_dynamic = NO;
354 else if (!strcmp(s, "statless"))
356 else if (!strcmp(s, "norepeat"))
358 else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
360 else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
361 mac_pw_prompt = s+10;
362 else if (!strcmp(s, "shorthex"))
363 do_full_hexdump = NO;
364 else if (!strcmp(s, "fullkey"))
366 else if (!strcmp(s, "showsecrets"))
368 else if (!strcmp(s, "finished"))
369 show_when_done = YES;
370 else if (!strcmp(s, "pid"))
372 else if (!strcmp(s, "nobuf")) {
373 if (!tfc_is_freestream(ctr_mode)) xerror(NO, YES, YES,
374 "cannot activate unbuffered mode for non-stream cipher mode %s!",
375 tfc_modename(ctr_mode));
376 else unbuffered = YES;
378 else if (!strncmp(s, "readloops", 9) && *(s+9) == '=') {
379 do_read_loops = tfc_humanfsize(s+10, &stoi);
380 if (!str_empty(stoi)) do_read_loops = NOSIZE;
382 else if (!strncmp(s, "logfile", 7) && *(s+7) == '=')
384 else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
386 blksize = (size_t)tfc_humanfsize(s, &stoi);
387 if (!str_empty(stoi)) {
388 blksize = (size_t)tfc_fnamesize(s, YES);
389 blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
390 if (blksize == NOSIZE) xerror(NO, YES, YES,
391 "%s: invalid block size value", s);
393 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
394 if (!tfc_is_freestream(ctr_mode) && blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
395 "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
396 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
397 "%s: block size exceeds %u bytes",
398 s, TFC_U(TFC_BLKSIZE));
400 else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
402 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
403 if (!str_empty(stoi)) {
404 xtsblocks = (size_t)tfc_fnamesize(s, YES);
405 xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
406 if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
407 "%s: invalid blocks per xts block value", s);
409 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
410 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
411 "%s: nr of blocks per xts block is not round to %u bytes",
412 s, TFC_U(TFC_BLKSIZE));
413 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
414 "%s: nr of blocks per xts block exceeds %u bytes",
415 s, TFC_U(TFC_BLKSIZE));
417 else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
419 iseek = tfc_humanfsize(s, &stoi);
420 if (!str_empty(stoi)) {
421 iseek = tfc_fnamesize(s, YES);
422 iseek = tfc_modifysize(iseek, strchr(s, ':'));
423 if (iseek == NOFSIZE) xerror(NO, YES, YES,
424 "%s: invalid iseek value", s);
426 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
427 if (do_edcrypt != TFC_DO_PLAIN && iseek % TF_BLOCK_SIZE)
429 "%s: not round to TF block size "
431 s, TFC_U(TF_BLOCK_SIZE));
432 iseek_blocks = iseek / TF_BLOCK_SIZE;
434 else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
436 iseek = tfc_humanfsize(s, &stoi);
437 if (!str_empty(stoi)) {
438 iseek = tfc_fnamesize(s, YES);
439 iseek = tfc_modifysize(iseek, strchr(s, ':'));
440 if (iseek == NOFSIZE) xerror(NO, YES, YES,
441 "%s: invalid ixseek value", s);
443 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
445 else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
447 iseek_blocks = tfc_humanfsize(s, &stoi);
448 if (!str_empty(stoi)) {
449 iseek_blocks = tfc_fnamesize(s, YES);
450 if (iseek_blocks == NOFSIZE)
452 "%s: invalid ictr value", s);
453 iseek_blocks /= TF_BLOCK_SIZE;
454 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
456 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
458 else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
460 iseek_blocks = tfc_humanfsize(s, &stoi);
461 if (!str_empty(stoi)) {
462 iseek_blocks = tfc_fnamesize(s, YES);
463 iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
464 if (iseek_blocks == NOFSIZE)
466 "%s: invalid ixctr value", s);
468 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
469 if (iseek_blocks % TF_BLOCK_SIZE)
471 "%s: not round to TF block size "
472 "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
473 iseek_blocks /= TF_BLOCK_SIZE;
475 else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
477 oseek = tfc_humanfsize(s, &stoi);
478 if (!str_empty(stoi)) {
479 oseek = tfc_fnamesize(s, YES);
480 oseek = tfc_modifysize(oseek, strchr(s, ':'));
481 if (oseek == NOFSIZE) xerror(NO, YES, YES,
482 "%s: invalid oseek value", s);
484 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
486 else if (!strncmp(s, "ioseek", 6) && *(s+6) == '=') {
489 iseek = tfc_humanfsize(s, &stoi);
490 if (!str_empty(stoi)) {
491 iseek = tfc_fnamesize(s, YES);
492 iseek = tfc_modifysize(iseek, strchr(s, ':'));
493 if (iseek == NOFSIZE) xerror(NO, YES, YES,
494 "%s: invalid iseek value", s);
496 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
497 if (do_edcrypt != TFC_DO_PLAIN && iseek % TF_BLOCK_SIZE)
499 "%s: not round to TF block size "
501 s, TFC_U(TF_BLOCK_SIZE));
502 iseek_blocks = iseek / TF_BLOCK_SIZE;
504 oseek = tfc_humanfsize(s, &stoi);
505 if (!str_empty(stoi)) {
506 oseek = tfc_fnamesize(s, YES);
507 oseek = tfc_modifysize(oseek, strchr(s, ':'));
508 if (oseek == NOFSIZE) xerror(NO, YES, YES,
509 "%s: invalid oseek value", s);
511 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
513 else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
515 maxlen = tfc_humanfsize(s, &stoi);
516 if (!str_empty(stoi)) {
517 maxlen = tfc_fnamesize(s, YES);
518 maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
519 if (maxlen == NOFSIZE) xerror(NO, YES, YES,
520 "%s: invalid count value", s);
522 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
523 if (counter_opt == TFC_CTR_HEAD)
524 maxlen += TF_BLOCK_SIZE;
526 else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
528 if (!strcmp(s, "tail")) {
529 do_ftrunc = TFC_FTRUNC_TAIL;
530 ftrunc_dfd = NOFSIZE;
533 do_ftrunc = TFC_DO_FTRUNC;
534 ftrunc_dfd = tfc_humanfsize(s, &stoi);
535 if (!str_empty(stoi)) {
536 ftrunc_dfd = tfc_fnamesize(s, YES);
537 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
538 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
539 "%s: invalid ftrunc value", s);
541 else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
544 else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
546 maxkeylen = tfc_humanfsize(s, &stoi);
547 if (!str_empty(stoi)) {
548 maxkeylen = tfc_fnamesize(s, YES);
549 maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
550 if (maxkeylen == NOSIZE)
552 "%s: invalid key length value", s);
554 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
556 else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
558 keyoffset = tfc_humanfsize(s, &stoi);
559 if (!str_empty(stoi)) {
560 keyoffset = tfc_fnamesize(s, YES);
561 keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
562 if (keyoffset == NOFSIZE)
564 "%s: invalid key offset value", s);
566 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
568 else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
570 ctrsz = tfc_humanfsize(s, &stoi);
571 if (!str_empty(stoi)) {
572 ctrsz = (size_t)tfc_fnamesize(s, YES);
573 ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
576 "%s: invalid counter length value", s);
578 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
579 if (ctrsz > TF_BLOCK_SIZE)
580 xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
582 else xerror(NO, YES, YES, "invalid option %s", s);
586 do_mac = TFC_MAC_SIGN;
587 if (strcasecmp(optarg, "mac") != 0)
588 do_mac_file = optarg;
591 do_mac = TFC_MAC_VRFY;
592 if (!strcasecmp(optarg, "drop"))
593 do_mac = TFC_MAC_DROP;
594 else if (strcasecmp(optarg, "mac") != 0)
595 do_mac_file = optarg;
599 if (do_mac != TFC_MAC_VRFY)
600 xerror(NO, YES, YES, "signature source was not specified");
601 do_mac = TFC_MAC_JUST_VRFY;
602 if (c == 'u') do_mac = TFC_MAC_JUST_VRFY2;
606 if (maxlen != NOFSIZE) {
607 if (c == 'Z') genzero_nr_bytes = maxlen;
608 else genrandom_nr_bytes = maxlen;
612 if (!strcasecmp(optarg, "cbs"))
614 else if (!strcasecmp(optarg, "ks"))
615 t = TF_FROM_BITS(TFC_KEY_BITS);
616 else if (!strcasecmp(optarg, "xks"))
617 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
618 else if (!strcasecmp(optarg, "iobs"))
621 t = tfc_humanfsize(optarg, &stoi);
622 if (!str_empty(stoi)) {
623 t = tfc_fnamesize(optarg, NO);
624 t = tfc_modifysize(t, strchr(optarg, ':'));
626 else t = tfc_modifysize(t, strchr(optarg, ':'));
628 if (c == 'Z') genzero_nr_bytes = maxlen = t;
629 else genrandom_nr_bytes = maxlen = t;
633 do_preserve_time = YES;
636 do_outfmt = TFC_OUTFMT_B64;
639 do_outfmt = TFC_OUTFMT_RAW;
642 do_outfmt = TFC_OUTFMT_HEX;
648 do_full_hexdump = NO;
655 td = strtod(optarg, &stoi);
656 status_timer = TFC_DTOUSECS(td);
657 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
665 if (!strcmp(progname, "tfbench")) {
666 if (!*(argv+optind)) usage();
668 td = strtod(*(argv+optind), &stoi);
669 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
671 "%s: invalid benchmark time in seconds", *(argv+optind));
672 bench_timer = TFC_DTOUSECS(td);
673 do_benchmark(bench_timer, td);
675 if (genrandom_nr_bytes) {
676 ctr_mode = TFC_MODE_STREAM;
677 do_edcrypt = TFC_DO_ENCRYPT;
678 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
680 if (genzero_nr_bytes) {
681 ctr_mode = TFC_MODE_PLAIN;
682 do_edcrypt = TFC_DO_PLAIN;
683 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
686 if (rawkey && password)
687 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
688 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
689 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
690 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
691 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
692 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
693 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
694 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
695 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
696 if (overwrite_source && counter_opt == TFC_CTR_RAND)
697 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
698 if (do_edcrypt == TFC_DO_PLAIN
699 && (do_mac || saltf || rawkey || mackey_opt || counter_opt || counter_file))
700 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
708 memset(tfc_salt, 0, TFC_MAX_SALT);
710 if (!strcasecmp(saltf, "disable")) goto _nosalt;
712 if (!strcmp(saltf, "-")) saltfd = 0;
713 else saltfd = xopen(saltf, O_RDONLY | O_LARGEFILE);
714 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
715 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
721 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
725 if (!strcmp(mackeyf, "-")) mkfd = 0;
726 else mkfd = xopen(mackeyf, O_RDONLY | O_LARGEFILE);
728 skein_init(&sk, TFC_KEY_BITS);
735 lrem = lblock = sizeof(tmpdata);
736 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
737 _mkragain: lio = xread(mkfd, pblk, lrem);
738 if (lio == 0 && do_stop == NO) do_stop = YES;
739 if (lio != NOSIZE) ldone += lio;
741 if (errno != EIO && catch_all_errors != YES)
742 xerror(NO, NO, NO, "%s", mackeyf);
743 switch (error_action) {
744 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
745 case TFC_ERRACT_SYNC:
746 case TFC_ERRACT_LSYNC:
747 xerror(YES, NO, NO, "%s", mackeyf);
748 lio = ldone = lrem = lblock;
749 memset(tmpdata, 0, lio);
750 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
751 else lseek(mkfd, rdpos + lio, SEEK_SET);
753 default: xerror(NO, NO, NO, "%s", mackeyf); break;
756 if (lio && lio < lrem) {
762 skein_update(&sk, tmpdata, ldone);
765 skein_final(mackey, &sk);
769 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
770 memset(&getps, 0, sizeof(struct getpasswd_state));
771 getps.fd = getps.efd = -1;
772 getps.passwd = pwdask;
773 getps.pwlen = sizeof(pwdask)-1;
774 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
775 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
776 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
777 getps.flags = GETP_WAITFILL;
778 n = xgetpasswd(&getps);
779 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
780 if (n == ((size_t)-2)) xexit(1);
781 if (verbose) say_hint(pwdask, n, "MAC password hint");
782 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
786 if ((strlen(progname) <= 9)
787 && ((!strcmp(progname, "sksum"))
788 || ((!memcmp(progname, "sk", 2))
789 && (!memcmp(progname+3, "sum", 3)
790 || !memcmp(progname+4, "sum", 3)
791 || !memcmp(progname+5, "sum", 3)
792 || !memcmp(progname+6, "sum", 3)))))
793 do_sksum(progname, argv+optind);
794 if (!strcmp(progname, "base64")) do_edbase64(argv+optind);
799 if ((do_edcrypt == TFC_DO_PLAIN && ctr_mode == TFC_MODE_PLAIN)
801 || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
802 if (!strcmp(argv[idx], "-")) kfd = 0;
803 else kfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
805 if (do_edcrypt == TFC_DO_PLAIN && ctr_mode == TFC_MODE_XOR) {
806 /* out: don't erase kfname if xor */
811 lio = strnlen(argv[idx], PATH_MAX);
812 memset(argv[idx], '*', lio);
819 if (do_full_key == NO && tweakf) {
822 if (!strcmp(tweakf, "-")) twfd = 0;
823 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
824 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
825 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
826 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
827 if (ldone < TF_TWEAK_SIZE)
828 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
835 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
837 sfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
838 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
839 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
842 if ((do_mac >= TFC_MAC_VRFY || do_mac <= TFC_MAC_DROP) && !do_mac_file) {
843 maxlen = tfc_fdsize(sfd);
844 if (maxlen == NOFSIZE)
846 "Cannot verify embedded MAC with non-seekable source!");
847 maxlen -= TF_FROM_BITS(macbits);
849 srcfname = argv[idx];
853 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
854 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
856 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
857 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
859 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
864 if (!strcmp(counter_file, "-")) ctrfd = 0;
865 else ctrfd = xopen(counter_file, O_RDONLY | O_LARGEFILE);
866 lio = xread(ctrfd, ctr, ctrsz);
867 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
868 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
871 else if (counter_opt == TFC_CTR_HEAD) {
874 lrem = lblock = ctrsz;
875 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
876 _ctrragain: lio = xread(sfd, pblk, lrem);
877 if (lio != NOSIZE) ldone += lio;
879 if (errno != EIO && catch_all_errors != YES)
880 xerror(NO, NO, NO, "%s", srcfname);
881 switch (error_action) {
882 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
883 case TFC_ERRACT_SYNC:
884 case TFC_ERRACT_LSYNC:
885 xerror(YES, NO, NO, "%s", srcfname);
886 lio = ldone = lrem = lblock;
888 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
889 else lseek(sfd, rdpos + lio, SEEK_SET);
891 default: xerror(NO, NO, NO, "%s", srcfname); break;
894 if (lio && lio < lrem) {
899 total_processed_src += ldone;
904 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
906 if (lseek(sfd, iseek, SEEK_SET) == -1)
907 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
910 if (do_edcrypt == TFC_DO_PLAIN) goto _plain;
912 if (verbose) tfc_esay("%s: hashing password", tfc_format_pid(progname));
914 if (rawkey == TFC_RAWKEY_KEYFILE) {
915 tfc_yesno xtskeyset = NO;
919 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
920 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
921 _keyragain: lio = xread(kfd, pblk, lrem);
922 if (lio != NOSIZE) ldone += lio;
924 if (errno != EIO && catch_all_errors != YES)
925 xerror(NO, NO, NO, "reading key");
926 switch (error_action) {
927 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
928 case TFC_ERRACT_SYNC:
929 case TFC_ERRACT_LSYNC:
930 xerror(YES, NO, NO, "reading key");
931 lio = ldone = lrem = lblock;
933 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
934 else lseek(kfd, rdpos + lio, SEEK_SET);
936 default: xerror(NO, NO, NO, "reading key"); break;
939 if (lio && lio < lrem) {
944 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
946 if (ctr_mode == TFC_MODE_XTS) {
947 if (xtskeyset == NO) {
954 else if (rawkey == TFC_RAWKEY_ASKSTR) {
955 tfc_yesno xtskeyset = NO;
957 pblk = key; n = sizeof(key);
958 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
959 getps.fd = getps.efd = -1;
960 getps.passwd = (char *)pblk;
962 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
963 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
964 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
965 getps.flags = GETP_WAITFILL;
966 n = xgetpasswd(&getps);
967 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
968 if (n == ((size_t)-2)) xexit(1);
969 if (verbose) say_hint(pblk, n, "Raw string key hint");
970 if (ctr_mode == TFC_MODE_XTS) {
971 if (xtskeyset == NO) {
972 pblk = xtskey; n = sizeof(xtskey);
978 else if (rawkey == TFC_RAWKEY_ASKHEX) {
979 tfc_yesno xtskeyset = NO;
983 memset(&getps, 0, sizeof(struct getpasswd_state));
984 getps.fd = getps.efd = -1;
985 getps.passwd = pwdask;
986 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
987 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
988 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
989 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
990 getps.flags = GETP_WAITFILL;
991 n = xgetpasswd(&getps);
992 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
993 if (n == ((size_t)-2)) xexit(1);
995 tfc_esay("Please input even number of hex digits!");
996 goto _rawkey_hex_again;
998 hex2bin(pblk, pwdask);
999 memset(pwdask, 0, sizeof(pwdask));
1000 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
1001 if (ctr_mode == TFC_MODE_XTS) {
1002 if (xtskeyset == NO) {
1005 goto _rawkey_hex_again;
1009 else if (password) {
1010 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
1011 getps.fd = getps.efd = -1;
1012 getps.passwd = pwdask;
1013 getps.pwlen = sizeof(pwdask)-1;
1014 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
1015 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
1016 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
1017 getps.flags = GETP_WAITFILL;
1018 n = xgetpasswd(&getps);
1019 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
1020 if (n == ((size_t)-2)) xexit(1);
1021 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
1022 getps.fd = getps.efd = -1;
1023 getps.passwd = pwdagain;
1024 getps.pwlen = sizeof(pwdagain)-1;
1025 getps.echo = "Enter it again: ";
1026 getps.flags = GETP_WAITFILL;
1027 n = xgetpasswd(&getps);
1028 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
1029 if (n == ((size_t)-2)) xexit(1);
1030 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
1031 tfc_esay("Passwords are different, try again");
1035 if (verbose) say_hint(pwdask, n, "Password hint");
1036 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
1037 memset(pwdask, 0, sizeof(pwdask));
1038 memset(pwdagain, 0, sizeof(pwdagain));
1041 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
1042 xerror(NO, NO, YES, "hashing key");
1046 if (tfc_saltsz > 0) {
1047 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
1048 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
1050 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
1051 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1052 memset(tfc_salt, 0, TFC_MAX_SALT);
1055 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
1056 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1061 tfc_yesno xtskeyset = NO;
1064 if (!strcmp(genkeyf, "-")) krfd = 1;
1065 else krfd = xopen(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1066 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
1067 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
1068 if (verbose && xtskeyset == NO) {
1069 tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1070 tfc_esay("%s: rawkey written to %s.", tfc_format_pid(progname), genkeyf);
1071 tfc_esay("%s: Have a nice day!", tfc_format_pid(progname));
1074 if (ctr_mode == TFC_MODE_XTS) {
1075 if (xtskeyset == NO) {
1088 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1089 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1090 "will be slower.", tfc_format_pid(progname));
1091 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1092 else skein_init(&sk, macbits);
1095 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1096 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1100 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1101 if (do_full_key == NO) {
1102 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1103 tf_tweak_set(key, tweak);
1105 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1107 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1109 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1110 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1111 if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1113 if (counter_opt == TFC_CTR_SHOW) {
1114 switch (do_outfmt) {
1115 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1116 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1117 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1120 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1127 if (verbose) tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1129 if (overwrite_source && srcfname) argv[idx] = srcfname;
1133 if (!strcmp(argv[idx], "-")) dfd = 1;
1134 else dfd = xxopen(YES, argv[idx], O_RDWR | O_LARGEFILE | write_flags);
1135 if (dfd == -1) dfd = xopen(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1136 dstfname = argv[idx];
1141 if (lseek(dfd, oseek, SEEK_SET) == -1)
1142 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1145 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1146 memset(&sigact, 0, sizeof(sigact));
1147 sigact.sa_flags = SA_RESTART;
1148 sigact.sa_handler = print_crypt_status;
1149 sigaction(SIGUSR1, &sigact, NULL);
1150 sigaction(SIGALRM, &sigact, NULL);
1151 if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
1152 sigact.sa_handler = change_status_width;
1153 sigaction(SIGQUIT, &sigact, NULL);
1154 sigact.sa_handler = change_status_timer;
1155 sigaction(SIGUSR2, &sigact, NULL);
1157 sigact.sa_handler = print_crypt_status;
1158 sigaction(SIGINT, &sigact, NULL);
1159 sigaction(SIGTERM, &sigact, NULL);
1160 sigaction(SIGTSTP, &sigact, NULL);
1163 sigact.sa_handler = exit_sigterm;
1164 sigaction(SIGINT, &sigact, NULL);
1165 sigaction(SIGTERM, &sigact, NULL);
1166 sigact.sa_handler = handle_sigtstp;
1167 sigaction(SIGTSTP, &sigact, NULL);
1169 memset(&sigact, 0, sizeof(struct sigaction));
1171 tfc_getcurtime(&delta_time);
1174 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1178 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1179 if (lio != NOSIZE) ldone += lio;
1180 else xerror(NO, NO, NO, "%s", dstfname);
1181 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1187 total_written_dst += ldone;
1188 total_processed_dst += ldone;
1189 delta_processed += ldone;
1192 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1194 if (do_mac == TFC_MAC_JUST_VRFY2) {
1195 rwd = tfc_fdgetpos(sfd);
1196 if (rwd == NOFSIZE) {
1197 tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", tfc_format_pid(progname));
1198 do_mac = TFC_MAC_VRFY;
1200 goto _nodecrypt_again_vrfy2;
1202 _decrypt_again_vrfy2:
1203 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1204 xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1206 total_processed_src = rwd;
1207 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1208 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1209 memset(svctr, 0, TF_BLOCK_SIZE);
1212 #define FLFD(x, y) (flipfd ? y : x)
1213 _nodecrypt_again_vrfy2:
1220 if (ctr_mode == TFC_MODE_XOR) flipfd = NO;
1222 _nextblk: ldone = 0;
1223 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1224 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(FLFD(sfd, kfd));
1225 _ragain: lio = xread(FLFD(sfd, kfd), pblk, lrem);
1227 if ((do_read_loops != 0 && FLFD(sfd, kfd) != 0) && (loopcnt < do_read_loops)) {
1228 lseek(FLFD(sfd, kfd), 0L, SEEK_SET);
1235 if (lio != NOSIZE) ldone += lio;
1237 if (errno != EIO && catch_all_errors != YES)
1238 xerror(NO, NO, NO, "%s", srcfname);
1239 switch (error_action) {
1240 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1241 case TFC_ERRACT_SYNC:
1242 case TFC_ERRACT_LSYNC:
1243 xerror(YES, NO, NO, "%s", srcfname);
1244 lio = ldone = lrem = lblock;
1245 memset(srcblk, 0, lio);
1246 if (rdpos == NOFSIZE) lseek(FLFD(sfd, kfd), lio, SEEK_CUR);
1247 else lseek(FLFD(sfd, kfd), rdpos + lio, SEEK_SET);
1249 default: xerror(NO, NO, NO, "%s", srcfname); break;
1252 if (unbuffered == NO && lio && lio < lrem) {
1257 total_processed_src += ldone;
1259 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1260 size_t orig = ldone;
1261 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1262 if (ldone > blksize) ldone = blksize;
1263 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1266 if (ctr_mode == TFC_MODE_XOR && flipfd == NO) {
1267 if (do_stop) blksize = ldone;
1273 if (do_mac == TFC_MAC_SIGN) skein_update(&sk, srcblk, ldone);
1275 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1276 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1277 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1278 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1279 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1280 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1281 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1282 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1283 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1284 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1285 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1286 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1287 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1288 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1289 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_ENCRYPT)
1290 tf_pcbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1291 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_DECRYPT)
1292 tf_pcbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1294 else if (ctr_mode == TFC_MODE_PLAIN)
1295 memcpy(dstblk, srcblk, ldone);
1297 else if (ctr_mode == TFC_MODE_XOR)
1298 xor_block(dstblk, srcblk, ldone);
1300 if (do_mac >= TFC_MAC_VRFY) skein_update(&sk, dstblk, ldone);
1301 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1306 _wagain: lio = xwrite(dfd, pblk, lrem);
1307 if (lio != NOSIZE) ldone += lio;
1308 else xerror(NO, NO, NO, "%s", dstfname);
1309 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1315 total_written_dst += ldone;
1316 _nowrite: total_processed_dst += ldone;
1317 delta_processed += ldone;
1319 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1325 if (verbose && status_timer && do_statline_dynamic == YES && statline_was_shown == YES) tfc_esay("\n");
1328 if (do_mac >= TFC_MAC_VRFY) {
1332 lrem = lblock = TF_FROM_BITS(macbits);
1333 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1334 _macragain: lio = xread(sfd, pblk, lrem);
1335 if (lio != NOSIZE) ldone += lio;
1337 if (errno != EIO && catch_all_errors != YES)
1338 xerror(NO, NO, NO, "%s", srcfname);
1339 switch (error_action) {
1340 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1341 case TFC_ERRACT_SYNC:
1342 case TFC_ERRACT_LSYNC:
1343 xerror(YES, NO, NO, "%s", srcfname);
1344 lio = ldone = lrem = lblock;
1345 memset(macvrfy, 0, lio);
1346 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1347 else lseek(sfd, rdpos + lio, SEEK_SET);
1349 default: xerror(NO, NO, NO, "%s", srcfname); break;
1352 if (lio && lio < lrem) {
1357 total_processed_src += ldone;
1362 if (!strcmp(do_mac_file, "-")) mfd = 0;
1363 else mfd = xopen(do_mac_file, O_RDONLY | O_LARGEFILE);
1364 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1365 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1366 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1367 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1368 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1369 lio = TF_FROM_BITS(macbits);
1370 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1372 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1376 if (ldone < TF_FROM_BITS(macbits)) {
1377 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1378 "not verifying", tfc_format_pid(progname), ldone);
1383 skein_final(macresult, &sk);
1385 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1386 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1387 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1388 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1389 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1390 else if (ctr_mode == TFC_MODE_PCBC) tf_pcbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1392 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1394 tfc_esay("%s: signature is good", tfc_format_pid(progname));
1396 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1397 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1400 if (do_mac == TFC_MAC_JUST_VRFY2) {
1401 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", tfc_format_pid(progname));
1402 maxlen = total_processed_src - SKEIN_DIGEST_SIZE;
1403 do_mac = TFC_MAC_DROP2;
1404 goto _decrypt_again_vrfy2;
1409 tfc_esay("%s: signature is BAD: "
1410 "wrong password, key, mode, or file is not signed", tfc_format_pid(progname));
1411 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", tfc_format_pid(progname));
1416 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1417 memset(macresult, 0, sizeof(macresult));
1418 memset(tmpdata, 0, sizeof(tmpdata));
1420 else if (do_mac == TFC_MAC_SIGN) {
1421 skein_final(macresult, &sk);
1423 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1424 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1425 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1426 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1427 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1428 else if (ctr_mode == TFC_MODE_PCBC) tf_pcbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1429 memset(macresult, 0, sizeof(macresult));
1433 lio = lrem = TF_FROM_BITS(macbits);
1435 _macwagain: lio = xwrite(dfd, pblk, lrem);
1436 if (lio != NOSIZE) ldone += lio;
1437 else xerror(NO, NO, NO, "%s", dstfname);
1438 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1444 total_written_dst += ldone;
1445 total_processed_dst += ldone;
1446 delta_processed += ldone;
1451 if (!strcmp(do_mac_file, "-")) mfd = 1;
1452 else mfd = xopen(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1453 if (do_outfmt == TFC_OUTFMT_B64) {
1454 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1455 memset(tmpdata, 0, TFC_TMPSIZE);
1456 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1457 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1458 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1460 tmpdata[lrem] = '\n';
1463 lio = xwrite(mfd, tmpdata, lrem);
1465 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1466 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1467 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1471 memset(macvrfy, 0, sizeof(macvrfy));
1472 memset(macresult, 0, sizeof(macresult));
1473 memset(tmpdata, 0, sizeof(tmpdata));
1475 else if (do_mac == TFC_MAC_DROP2) total_processed_src += SKEIN_DIGEST_SIZE;
1477 if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(0);