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;
647 do_full_hexdump = NO;
654 td = strtod(optarg, &stoi);
655 status_timer = TFC_DTOUSECS(td);
656 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
664 if (!strcmp(progname, "tfbench")) {
665 if (!*(argv+optind)) usage();
667 td = strtod(*(argv+optind), &stoi);
668 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
670 "%s: invalid benchmark time in seconds", *(argv+optind));
671 bench_timer = TFC_DTOUSECS(td);
672 do_benchmark(bench_timer, td);
674 if (genrandom_nr_bytes) {
675 ctr_mode = TFC_MODE_STREAM;
676 do_edcrypt = TFC_DO_ENCRYPT;
677 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
679 if (genzero_nr_bytes) {
680 ctr_mode = TFC_MODE_PLAIN;
681 do_edcrypt = TFC_DO_PLAIN;
682 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
685 if (rawkey && password)
686 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
687 if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
688 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
689 if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
690 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
691 if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
692 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
693 if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
694 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
695 if (overwrite_source && counter_opt == TFC_CTR_RAND)
696 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
697 if (do_edcrypt == TFC_DO_PLAIN
698 && (do_mac || saltf || rawkey || mackey_opt || counter_opt || counter_file))
699 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
707 memset(tfc_salt, 0, TFC_MAX_SALT);
709 if (!strcasecmp(saltf, "disable")) goto _nosalt;
711 if (!strcmp(saltf, "-")) saltfd = 0;
712 else saltfd = xopen(saltf, O_RDONLY | O_LARGEFILE);
713 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
714 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
720 if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
724 if (!strcmp(mackeyf, "-")) mkfd = 0;
725 else mkfd = xopen(mackeyf, O_RDONLY | O_LARGEFILE);
727 skein_init(&sk, TFC_KEY_BITS);
734 lrem = lblock = sizeof(tmpdata);
735 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
736 _mkragain: lio = xread(mkfd, pblk, lrem);
737 if (lio == 0 && do_stop == NO) do_stop = YES;
738 if (lio != NOSIZE) ldone += lio;
740 if (errno != EIO && catch_all_errors != YES)
741 xerror(NO, NO, NO, "%s", mackeyf);
742 switch (error_action) {
743 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
744 case TFC_ERRACT_SYNC:
745 case TFC_ERRACT_LSYNC:
746 xerror(YES, NO, NO, "%s", mackeyf);
747 lio = ldone = lrem = lblock;
748 memset(tmpdata, 0, lio);
749 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
750 else lseek(mkfd, rdpos + lio, SEEK_SET);
752 default: xerror(NO, NO, NO, "%s", mackeyf); break;
755 if (lio && lio < lrem) {
761 skein_update(&sk, tmpdata, ldone);
764 skein_final(mackey, &sk);
768 else if (mackey_opt == TFC_MACKEY_PASSWORD) {
769 memset(&getps, 0, sizeof(struct getpasswd_state));
770 getps.fd = getps.efd = -1;
771 getps.passwd = pwdask;
772 getps.pwlen = sizeof(pwdask)-1;
773 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
774 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
775 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
776 getps.flags = GETP_WAITFILL;
777 n = xgetpasswd(&getps);
778 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
779 if (n == ((size_t)-2)) xexit(1);
780 if (verbose) say_hint(pwdask, n, "MAC password hint");
781 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
785 if ((strlen(progname) <= 9)
786 && ((!strcmp(progname, "sksum"))
787 || ((!memcmp(progname, "sk", 2))
788 && (!memcmp(progname+3, "sum", 3)
789 || !memcmp(progname+4, "sum", 3)
790 || !memcmp(progname+5, "sum", 3)
791 || !memcmp(progname+6, "sum", 3)))))
792 do_sksum(progname, argv+optind);
793 if (!strcmp(progname, "base64")) do_edbase64(argv+optind);
798 if ((do_edcrypt == TFC_DO_PLAIN && ctr_mode == TFC_MODE_PLAIN)
800 || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
801 if (!strcmp(argv[idx], "-")) kfd = 0;
802 else kfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
804 if (do_edcrypt == TFC_DO_PLAIN && ctr_mode == TFC_MODE_XOR) {
805 /* out: don't erase kfname if xor */
810 lio = strnlen(argv[idx], PATH_MAX);
811 memset(argv[idx], '*', lio);
818 if (do_full_key == NO && tweakf) {
821 if (!strcmp(tweakf, "-")) twfd = 0;
822 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
823 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
824 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
825 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
826 if (ldone < TF_TWEAK_SIZE)
827 xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
834 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
836 sfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
837 if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
838 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
841 if ((do_mac >= TFC_MAC_VRFY || do_mac <= TFC_MAC_DROP) && !do_mac_file) {
842 maxlen = tfc_fdsize(sfd);
843 if (maxlen == NOFSIZE)
845 "Cannot verify embedded MAC with non-seekable source!");
846 maxlen -= TF_FROM_BITS(macbits);
848 srcfname = argv[idx];
852 if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
853 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
855 if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
856 if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
858 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
863 if (!strcmp(counter_file, "-")) ctrfd = 0;
864 else ctrfd = xopen(counter_file, O_RDONLY | O_LARGEFILE);
865 lio = xread(ctrfd, ctr, ctrsz);
866 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
867 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
870 else if (counter_opt == TFC_CTR_HEAD) {
873 lrem = lblock = ctrsz;
874 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
875 _ctrragain: lio = xread(sfd, pblk, lrem);
876 if (lio != NOSIZE) ldone += lio;
878 if (errno != EIO && catch_all_errors != YES)
879 xerror(NO, NO, NO, "%s", srcfname);
880 switch (error_action) {
881 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
882 case TFC_ERRACT_SYNC:
883 case TFC_ERRACT_LSYNC:
884 xerror(YES, NO, NO, "%s", srcfname);
885 lio = ldone = lrem = lblock;
887 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
888 else lseek(sfd, rdpos + lio, SEEK_SET);
890 default: xerror(NO, NO, NO, "%s", srcfname); break;
893 if (lio && lio < lrem) {
898 total_processed_src += ldone;
903 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
905 if (lseek(sfd, iseek, SEEK_SET) == -1)
906 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
909 if (do_edcrypt == TFC_DO_PLAIN) goto _plain;
911 if (verbose) tfc_esay("%s: hashing password", tfc_format_pid(progname));
913 if (rawkey == TFC_RAWKEY_KEYFILE) {
914 tfc_yesno xtskeyset = NO;
918 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
919 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
920 _keyragain: lio = xread(kfd, pblk, lrem);
921 if (lio != NOSIZE) ldone += lio;
923 if (errno != EIO && catch_all_errors != YES)
924 xerror(NO, NO, NO, "reading key");
925 switch (error_action) {
926 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
927 case TFC_ERRACT_SYNC:
928 case TFC_ERRACT_LSYNC:
929 xerror(YES, NO, NO, "reading key");
930 lio = ldone = lrem = lblock;
932 if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
933 else lseek(kfd, rdpos + lio, SEEK_SET);
935 default: xerror(NO, NO, NO, "reading key"); break;
938 if (lio && lio < lrem) {
943 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
945 if (ctr_mode == TFC_MODE_XTS) {
946 if (xtskeyset == NO) {
953 else if (rawkey == TFC_RAWKEY_ASKSTR) {
954 tfc_yesno xtskeyset = NO;
956 pblk = key; n = sizeof(key);
957 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
958 getps.fd = getps.efd = -1;
959 getps.passwd = (char *)pblk;
961 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
962 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
963 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
964 getps.flags = GETP_WAITFILL;
965 n = xgetpasswd(&getps);
966 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
967 if (n == ((size_t)-2)) xexit(1);
968 if (verbose) say_hint(pblk, n, "Raw string key hint");
969 if (ctr_mode == TFC_MODE_XTS) {
970 if (xtskeyset == NO) {
971 pblk = xtskey; n = sizeof(xtskey);
977 else if (rawkey == TFC_RAWKEY_ASKHEX) {
978 tfc_yesno xtskeyset = NO;
982 memset(&getps, 0, sizeof(struct getpasswd_state));
983 getps.fd = getps.efd = -1;
984 getps.passwd = pwdask;
985 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
986 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
987 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
988 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
989 getps.flags = GETP_WAITFILL;
990 n = xgetpasswd(&getps);
991 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
992 if (n == ((size_t)-2)) xexit(1);
994 tfc_esay("Please input even number of hex digits!");
995 goto _rawkey_hex_again;
997 hex2bin(pblk, pwdask);
998 memset(pwdask, 0, sizeof(pwdask));
999 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
1000 if (ctr_mode == TFC_MODE_XTS) {
1001 if (xtskeyset == NO) {
1004 goto _rawkey_hex_again;
1008 else if (password) {
1009 _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state));
1010 getps.fd = getps.efd = -1;
1011 getps.passwd = pwdask;
1012 getps.pwlen = sizeof(pwdask)-1;
1013 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
1014 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
1015 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
1016 getps.flags = GETP_WAITFILL;
1017 n = xgetpasswd(&getps);
1018 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
1019 if (n == ((size_t)-2)) xexit(1);
1020 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
1021 getps.fd = getps.efd = -1;
1022 getps.passwd = pwdagain;
1023 getps.pwlen = sizeof(pwdagain)-1;
1024 getps.echo = "Enter it again: ";
1025 getps.flags = GETP_WAITFILL;
1026 n = xgetpasswd(&getps);
1027 if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
1028 if (n == ((size_t)-2)) xexit(1);
1029 if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
1030 tfc_esay("Passwords are different, try again");
1034 if (verbose) say_hint(pwdask, n, "Password hint");
1035 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
1036 memset(pwdask, 0, sizeof(pwdask));
1037 memset(pwdagain, 0, sizeof(pwdagain));
1040 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
1041 xerror(NO, NO, YES, "hashing key");
1045 if (tfc_saltsz > 0) {
1046 memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
1047 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
1049 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
1050 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1051 memset(tfc_salt, 0, TFC_MAX_SALT);
1054 if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
1055 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1060 tfc_yesno xtskeyset = NO;
1063 if (!strcmp(genkeyf, "-")) krfd = 1;
1064 else krfd = xopen(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1065 _xts2genkey: if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
1066 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
1067 if (verbose && xtskeyset == NO) {
1068 tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1069 tfc_esay("%s: rawkey written to %s.", tfc_format_pid(progname), genkeyf);
1070 tfc_esay("%s: Have a nice day!", tfc_format_pid(progname));
1073 if (ctr_mode == TFC_MODE_XTS) {
1074 if (xtskeyset == NO) {
1087 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1088 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1089 "will be slower.", tfc_format_pid(progname));
1090 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1091 else skein_init(&sk, macbits);
1094 if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1095 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1099 if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1100 if (do_full_key == NO) {
1101 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1102 tf_tweak_set(key, tweak);
1104 if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1106 if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1108 tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1109 tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1110 if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1112 if (counter_opt == TFC_CTR_SHOW) {
1113 switch (do_outfmt) {
1114 case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1115 case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1116 case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1119 else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1126 if (verbose) tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1128 if (overwrite_source && srcfname) argv[idx] = srcfname;
1132 if (!strcmp(argv[idx], "-")) dfd = 1;
1133 else dfd = xxopen(YES, argv[idx], O_RDWR | O_LARGEFILE | write_flags);
1134 if (dfd == -1) dfd = xopen(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1135 dstfname = argv[idx];
1140 if (lseek(dfd, oseek, SEEK_SET) == -1)
1141 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1144 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1145 memset(&sigact, 0, sizeof(sigact));
1146 sigact.sa_flags = SA_RESTART;
1147 sigact.sa_handler = print_crypt_status;
1148 sigaction(SIGUSR1, &sigact, NULL);
1149 sigaction(SIGALRM, &sigact, NULL);
1150 if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
1151 sigact.sa_handler = change_status_width;
1152 sigaction(SIGQUIT, &sigact, NULL);
1153 sigact.sa_handler = change_status_timer;
1154 sigaction(SIGUSR2, &sigact, NULL);
1156 sigact.sa_handler = print_crypt_status;
1157 sigaction(SIGINT, &sigact, NULL);
1158 sigaction(SIGTERM, &sigact, NULL);
1159 sigaction(SIGTSTP, &sigact, NULL);
1162 sigact.sa_handler = exit_sigterm;
1163 sigaction(SIGINT, &sigact, NULL);
1164 sigaction(SIGTERM, &sigact, NULL);
1165 sigact.sa_handler = handle_sigtstp;
1166 sigaction(SIGTSTP, &sigact, NULL);
1168 memset(&sigact, 0, sizeof(struct sigaction));
1170 tfc_getcurtime(&delta_time);
1173 if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1177 _ctrwagain: lio = xwrite(dfd, pblk, lrem);
1178 if (lio != NOSIZE) ldone += lio;
1179 else xerror(NO, NO, NO, "%s", dstfname);
1180 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1186 total_written_dst += ldone;
1187 total_processed_dst += ldone;
1188 delta_processed += ldone;
1191 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1193 if (do_mac == TFC_MAC_JUST_VRFY2) {
1194 rwd = tfc_fdgetpos(sfd);
1195 if (rwd == NOFSIZE) {
1196 tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", tfc_format_pid(progname));
1197 do_mac = TFC_MAC_VRFY;
1199 goto _nodecrypt_again_vrfy2;
1201 _decrypt_again_vrfy2:
1202 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1203 xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1205 total_processed_src = rwd;
1206 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1207 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1208 memset(svctr, 0, TF_BLOCK_SIZE);
1211 #define FLFD(x, y) (flipfd ? y : x)
1212 _nodecrypt_again_vrfy2:
1219 if (ctr_mode == TFC_MODE_XOR) flipfd = NO;
1221 _nextblk: ldone = 0;
1222 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1223 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(FLFD(sfd, kfd));
1224 _ragain: lio = xread(FLFD(sfd, kfd), pblk, lrem);
1226 if ((do_read_loops != 0 && FLFD(sfd, kfd) != 0) && (loopcnt < do_read_loops)) {
1227 lseek(FLFD(sfd, kfd), 0L, SEEK_SET);
1234 if (lio != NOSIZE) ldone += lio;
1236 if (errno != EIO && catch_all_errors != YES)
1237 xerror(NO, NO, NO, "%s", srcfname);
1238 switch (error_action) {
1239 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1240 case TFC_ERRACT_SYNC:
1241 case TFC_ERRACT_LSYNC:
1242 xerror(YES, NO, NO, "%s", srcfname);
1243 lio = ldone = lrem = lblock;
1244 memset(srcblk, 0, lio);
1245 if (rdpos == NOFSIZE) lseek(FLFD(sfd, kfd), lio, SEEK_CUR);
1246 else lseek(FLFD(sfd, kfd), rdpos + lio, SEEK_SET);
1248 default: xerror(NO, NO, NO, "%s", srcfname); break;
1251 if (unbuffered == NO && lio && lio < lrem) {
1256 total_processed_src += ldone;
1258 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1259 size_t orig = ldone;
1260 ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1261 if (ldone > blksize) ldone = blksize;
1262 memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1265 if (ctr_mode == TFC_MODE_XOR && flipfd == NO) {
1266 if (do_stop) blksize = ldone;
1272 if (do_mac == TFC_MAC_SIGN) skein_update(&sk, srcblk, ldone);
1274 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1275 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1276 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1277 tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1278 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1279 tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1280 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1281 tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1282 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1283 tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1284 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1285 tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1286 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1287 tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1288 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_ENCRYPT)
1289 tf_pcbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1290 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_DECRYPT)
1291 tf_pcbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1293 else if (ctr_mode == TFC_MODE_PLAIN)
1294 memcpy(dstblk, srcblk, ldone);
1296 else if (ctr_mode == TFC_MODE_XOR)
1297 xor_block(dstblk, srcblk, ldone);
1299 if (do_mac >= TFC_MAC_VRFY) skein_update(&sk, dstblk, ldone);
1300 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1305 _wagain: lio = xwrite(dfd, pblk, lrem);
1306 if (lio != NOSIZE) ldone += lio;
1307 else xerror(NO, NO, NO, "%s", dstfname);
1308 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1314 total_written_dst += ldone;
1315 _nowrite: total_processed_dst += ldone;
1316 delta_processed += ldone;
1318 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1324 if (verbose && status_timer && do_statline_dynamic == YES && statline_was_shown == YES) tfc_esay("\n");
1327 if (do_mac >= TFC_MAC_VRFY) {
1331 lrem = lblock = TF_FROM_BITS(macbits);
1332 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1333 _macragain: lio = xread(sfd, pblk, lrem);
1334 if (lio != NOSIZE) ldone += lio;
1336 if (errno != EIO && catch_all_errors != YES)
1337 xerror(NO, NO, NO, "%s", srcfname);
1338 switch (error_action) {
1339 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1340 case TFC_ERRACT_SYNC:
1341 case TFC_ERRACT_LSYNC:
1342 xerror(YES, NO, NO, "%s", srcfname);
1343 lio = ldone = lrem = lblock;
1344 memset(macvrfy, 0, lio);
1345 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1346 else lseek(sfd, rdpos + lio, SEEK_SET);
1348 default: xerror(NO, NO, NO, "%s", srcfname); break;
1351 if (lio && lio < lrem) {
1356 total_processed_src += ldone;
1361 if (!strcmp(do_mac_file, "-")) mfd = 0;
1362 else mfd = xopen(do_mac_file, O_RDONLY | O_LARGEFILE);
1363 lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1364 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1365 if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1366 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1367 sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1368 lio = TF_FROM_BITS(macbits);
1369 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1371 else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1375 if (ldone < TF_FROM_BITS(macbits)) {
1376 if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1377 "not verifying", tfc_format_pid(progname), ldone);
1382 skein_final(macresult, &sk);
1384 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1385 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1386 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1387 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1388 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1389 else if (ctr_mode == TFC_MODE_PCBC) tf_pcbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1391 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1393 tfc_esay("%s: signature is good", tfc_format_pid(progname));
1395 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1396 else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1399 if (do_mac == TFC_MAC_JUST_VRFY2) {
1400 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", tfc_format_pid(progname));
1401 maxlen = total_processed_src - SKEIN_DIGEST_SIZE;
1402 do_mac = TFC_MAC_DROP2;
1403 goto _decrypt_again_vrfy2;
1408 tfc_esay("%s: signature is BAD: "
1409 "wrong password, key, mode, or file is not signed", tfc_format_pid(progname));
1410 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", tfc_format_pid(progname));
1415 _shortmac: memset(macvrfy, 0, sizeof(macvrfy));
1416 memset(macresult, 0, sizeof(macresult));
1417 memset(tmpdata, 0, sizeof(tmpdata));
1419 else if (do_mac == TFC_MAC_SIGN) {
1420 skein_final(macresult, &sk);
1422 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1423 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1424 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1425 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1426 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1427 else if (ctr_mode == TFC_MODE_PCBC) tf_pcbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1428 memset(macresult, 0, sizeof(macresult));
1432 lio = lrem = TF_FROM_BITS(macbits);
1434 _macwagain: lio = xwrite(dfd, pblk, lrem);
1435 if (lio != NOSIZE) ldone += lio;
1436 else xerror(NO, NO, NO, "%s", dstfname);
1437 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1443 total_written_dst += ldone;
1444 total_processed_dst += ldone;
1445 delta_processed += ldone;
1450 if (!strcmp(do_mac_file, "-")) mfd = 1;
1451 else mfd = xopen(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1452 if (do_outfmt == TFC_OUTFMT_B64) {
1453 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1454 memset(tmpdata, 0, TFC_TMPSIZE);
1455 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1456 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1457 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1459 tmpdata[lrem] = '\n';
1462 lio = xwrite(mfd, tmpdata, lrem);
1464 else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1465 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1466 if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1470 memset(macvrfy, 0, sizeof(macvrfy));
1471 memset(macresult, 0, sizeof(macresult));
1472 memset(tmpdata, 0, sizeof(tmpdata));
1474 else if (do_mac == TFC_MAC_DROP2) total_processed_src += SKEIN_DIGEST_SIZE;
1476 if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(TFC_SIGLAST);