-X: add simple XOR mode (embedded xor tool)
[tfcrypt.git] / tfcrypt.c
1 /*
2  * tfcrypt -- high security Threefish encryption tool.
3  *
4  * tfcrypt is copyrighted:
5  * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
6  *
7  * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
8  *
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:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
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.
27  */
28
29 #include "tfcrypt.h"
30
31 static tfc_byte svctr[TF_BLOCK_SIZE];
32 static tfc_fsize rwd, do_read_loops, loopcnt;
33 static tfc_yesno unbuffered;
34
35 static void open_log(const char *logfile)
36 {
37         int fd;
38         tfc_yesno ro;
39
40         if (!strcmp(logfile, "-")) return;
41
42         ro = read_only;
43         read_only = NO;
44         fd = xopen(logfile, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC);
45         read_only = ro;
46         xclose(2);
47         if (dup2(fd, 2) == -1) xexit(2);
48         xclose(fd);
49         do_statline_dynamic = NO;
50 }
51
52 static int getps_filter(struct getpasswd_state *getps, char chr, size_t pos)
53 {
54         if (chr == '\x03') {
55                 getps->retn = ((size_t)-2);
56                 return 6;
57         }
58         return 1;
59 }
60
61 static int getps_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
62 {
63         if (chr == '\x03') {
64                 getps->retn = ((size_t)-2);
65                 return 6;
66         }
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;
72         return 0;
73 }
74
75 static inline int isctrlchr(int c)
76 {
77         if (c == 9) return 0;
78         if (c >= 0 && c <= 31) return 1;
79         if (c == 127) return 1;
80         return 0;
81 }
82
83 static int getps_plain_filter(struct getpasswd_state *getps, char chr, size_t pos)
84 {
85         int x;
86
87         x = getps_filter(getps, chr, pos);
88         if (x != 1) return x;
89
90         if (pos < getps->pwlen && !isctrlchr(chr))
91                 write(getps->efd, &chr, sizeof(char));
92         return 1;
93 }
94
95 static int getps_plain_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
96 {
97         int x;
98
99         x = getps_hex_filter(getps, chr, pos);
100         if (x != 1) return x;
101
102         if (pos < getps->pwlen && !isctrlchr(chr))
103                 write(getps->efd, &chr, sizeof(char));
104         return 1;
105 }
106
107 static void make_hint(void *hint, size_t szhint, const void *data, size_t szdata)
108 {
109         char t[TF_FROM_BITS(TF_MAX_BITS)];
110
111         skein(t, TF_MAX_BITS, NULL, data, szdata);
112         xor_shrink(hint, szhint, t, sizeof(t));
113         memset(t, 0, sizeof(t));
114 }
115
116 static void raw_say_hint(void *hint, size_t szhint, const void *data, size_t szdata, const char *prompt)
117 {
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);
122 }
123
124 static void say_hint(const void *data, size_t szdata, const char *prompt)
125 {
126         char t[TF_SIZE_UNIT];
127         raw_say_hint(t, TF_SIZE_UNIT, data, szdata, prompt);
128         /* t[] is erased (automatically) */
129 }
130
131 int main(int argc, char **argv)
132 {
133         tfc_yesno flipfd;
134         int c;
135         double td;
136         char *s, *d, *t, *stoi;
137         size_t x, n;
138
139         progpid = getpid();
140         progname = basename(argv[0]);
141
142         if (!isatty(2)) do_statline_dynamic = NO;
143
144         s = (char *)srcblk;
145         d = getenv("HOME");
146         if (!d) d = "";
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);
151 _baddfname:
152         memset(s, 0, n);
153
154         if (!strcmp(progname, "iotool")) {
155                 do_edcrypt = TFC_DO_PLAIN;
156                 ctr_mode = TFC_MODE_PLAIN;
157         }
158
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 */
163                 kfd = 0;
164         }
165
166         opterr = 0;
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:")) != -1) {
168                 switch (c) {
169                         case 'L':
170                                 read_defaults(optarg, NO);
171                                 break;
172                         case 's':
173                                 saltf = optarg;
174                                 break;
175                         case 'r':
176                                 randsource = optarg;
177                                 break;
178                         case 'c':
179                                 if (!strcasecmp(optarg, "show"))
180                                         counter_opt = TFC_CTR_SHOW;
181                                 else if (!strcasecmp(optarg, "head"))
182                                         counter_opt = TFC_CTR_HEAD;
183                                 else if (!strcasecmp(optarg, "rand"))
184                                         counter_opt = TFC_CTR_RAND;
185                                 else if (!strcasecmp(optarg, "zero"))
186                                         counter_opt = TFC_CTR_ZERO;
187                                 else if (strchr(optarg, ':')) {
188                                         char *ss, chr;
189
190                                         counter_opt = TFC_CTR_SSET;
191                                         n = sizeof(ctr);
192
193                                         s = d = optarg; t = NULL;
194                                         while ((s = strtok_r(d, ",", &t))) {
195                                                 if (d) d = NULL;
196
197                                                 if (n == 0) break;
198                                                 ss = strchr(s, ':');
199                                                 if (!ss) continue;
200                                                 *ss = 0; ss++;
201                                                 chr = (char)strtoul(s, &stoi, 16);
202                                                 if (!str_empty(stoi)) continue;
203                                                 x = (size_t)strtoul(ss, &stoi, 10);
204                                                 if (!str_empty(stoi)) continue;
205                                                 if (x > n) x = n;
206                                                 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
207                                                 n -= x;
208                                         }
209                                 }
210                                 else counter_file = sksum_hashlist_file = optarg;
211                                 break;
212                         case 'C':
213                                 if (!strcasecmp(optarg, "ctr"))
214                                         ctr_mode = TFC_MODE_CTR;
215                                 else if (!strcasecmp(optarg, "stream"))
216                                         ctr_mode = TFC_MODE_STREAM;
217                                 else if (!strcasecmp(optarg, "cbc"))
218                                         ctr_mode = TFC_MODE_CBC;
219                                 else if (!strcasecmp(optarg, "pcbc"))
220                                         ctr_mode = TFC_MODE_PCBC;
221                                 else if (!strcasecmp(optarg, "ecb"))
222                                         ctr_mode = TFC_MODE_ECB;
223                                 else if (!strcasecmp(optarg, "xts"))
224                                         ctr_mode = TFC_MODE_XTS;
225                                 else xerror(NO, YES, YES, "%s: invalid mode of operation", optarg);
226                                 break;
227                         case 'P':
228                         case 'X':
229                                 do_edcrypt = TFC_DO_PLAIN;
230                                 if (c == 'X') {
231                                         ctr_mode = TFC_MODE_XOR;
232                                         /* xor: default to stdin if invoked without args */
233                                         kfd = 0;
234                                 }
235                                 else ctr_mode = TFC_MODE_PLAIN;
236                                 break;
237                         case 'e':
238                                 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
239                                 break;
240                         case 'd':
241                                 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
242                                 break;
243                         case 'D':
244                                 macbits = strtoul(optarg, &stoi, 10);
245                                 if (macbits == 0 || !str_empty(stoi) || macbits < 8
246                                 || macbits > TF_MAX_BITS || macbits % 8)
247                                         xerror(NO, YES, YES, "%s: invalid MAC bits setting", optarg);
248                                 break;
249                         case 'n':
250                                 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
251                                 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
252                                 break;
253                         case 'U':
254                                 if (!strcasecmp(optarg, "key"))
255                                         mackey_opt = TFC_MACKEY_RAWKEY;
256                                 else if (!strcasecmp(optarg, "pwd"))
257                                         mackey_opt = TFC_MACKEY_PASSWORD;
258                                 else {
259                                         mackey_opt = TFC_MACKEY_FILE;
260                                         mackeyf = optarg;
261                                 }
262                                 break;
263                         case 'p':
264                                 password = YES;
265                                 break;
266                         case 'k':
267                                 rawkey = TFC_RAWKEY_KEYFILE;
268                                 break;
269                         case 'z':
270                                 rawkey = TFC_RAWKEY_ASKSTR;
271                                 break;
272                         case 'x':
273                                 rawkey = TFC_RAWKEY_ASKHEX;
274                                 break;
275                         case 'K':
276                                 verbose = YES;
277                                 genkeyf = optarg;
278                                 break;
279                         case 't':
280                                 tweakf = optarg;
281                                 do_full_key = NO;
282                                 break;
283                         case 'l':
284                                 if (maxlen != NOFSIZE) break;
285
286                                 maxlen = tfc_humanfsize(optarg, &stoi);
287                                 if (!str_empty(stoi)) {
288                                         maxlen = tfc_fnamesize(optarg, YES);
289                                         maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
290                                         if (maxlen == NOFSIZE) xerror(NO, YES, YES,
291                                         "%s: invalid count value", optarg);
292                                 }
293                                 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
294                                 if (counter_opt == TFC_CTR_HEAD)
295                                         maxlen += TF_BLOCK_SIZE;
296                                 break;
297                         case 'w':
298                                 overwrite_source = YES;
299                                 break;
300                         case 'E':
301                                 if (!strcmp(optarg, "xall")) {
302                                         catch_all_errors = YES;
303                                         break;
304                                 }
305                                 if (!strcmp(optarg, "xseek")) {
306                                         ignore_seek_errors = YES;
307                                         break;
308                                 }
309                                 if (!strcmp(optarg, "exit"))
310                                         error_action = TFC_ERRACT_EXIT;
311                                 else if (!strncmp(optarg, "cont", 4))
312                                         error_action = TFC_ERRACT_CONT;
313                                 else if (!strcmp(optarg, "sync"))
314                                         error_action = TFC_ERRACT_SYNC;
315                                 else if (!strcmp(optarg, "lsync"))
316                                         error_action = TFC_ERRACT_LSYNC;
317                                 else xerror(NO, YES, YES, "invalid error action %s specified", optarg);
318                                 break;
319                         case 'o':
320                                 open_log(optarg);
321                                 break;
322                         case 'O':
323                                 s = d = optarg; t = NULL;
324                                 while ((s = strtok_r(d, ",", &t))) {
325                                         if (d) d = NULL;
326                                         if (!strcmp(s, "sync"))
327                                                 write_flags |= O_SYNC;
328                                         else if (!strcmp(s, "trunc"))
329                                                 write_flags |= O_TRUNC;
330                                         else if (!strcmp(s, "append"))
331                                                 write_flags |= O_APPEND;
332                                         else if (!strcmp(s, "fsync"))
333                                                 do_fsync = YES;
334                                         else if (!strcmp(s, "pad"))
335                                                 do_pad = YES;
336                                         else if (!strcmp(s, "ro"))
337                                                 read_only = YES;
338                                         else if (!strcmp(s, "rw"))
339                                                 read_only = NO;
340                                         else if (!strcmp(s, "xtime"))
341                                                 do_preserve_time = YES;
342                                         else if (!strcmp(s, "gibsize"))
343                                                 do_stats_in_gibs = YES;
344                                         else if (!strcmp(s, "plainstats"))
345                                                 do_statline_dynamic = NO;
346                                         else if (!strcmp(s, "statless"))
347                                                 do_less_stats = YES;
348                                         else if (!strcmp(s, "norepeat"))
349                                                 no_repeat = YES;
350                                         else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
351                                                 pw_prompt = s+7;
352                                         else if (!strncmp(s, "macprompt", 9) && *(s+9) == '=')
353                                                 mac_pw_prompt = s+10;
354                                         else if (!strcmp(s, "shorthex"))
355                                                 do_full_hexdump = NO;
356                                         else if (!strcmp(s, "fullkey"))
357                                                 do_full_key = YES;
358                                         else if (!strcmp(s, "showsecrets"))
359                                                 show_secrets = YES;
360                                         else if (!strcmp(s, "finished"))
361                                                 show_when_done = YES;
362                                         else if (!strcmp(s, "pid"))
363                                                 show_pid = YES;
364                                         else if (!strcmp(s, "nobuf")) {
365                                                 if (!tfc_is_freestream(ctr_mode)) xerror(NO, YES, YES,
366                                                         "cannot activate unbuffered mode for non-stream cipher mode %s!",
367                                                         tfc_modename(ctr_mode));
368                                                 else unbuffered = YES;
369                                         }
370                                         else if (!strncmp(s, "readloops", 9) && *(s+9) == '=') {
371                                                 do_read_loops = tfc_humanfsize(s+10, &stoi);
372                                                 if (!str_empty(stoi)) do_read_loops = NOSIZE;
373                                         }
374                                         else if (!strncmp(s, "logfile", 7) && *(s+7) == '=')
375                                                 open_log(s+8);
376                                         else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
377                                                 s += 5;
378                                                 blksize = (size_t)tfc_humanfsize(s, &stoi);
379                                                 if (!str_empty(stoi)) {
380                                                         blksize = (size_t)tfc_fnamesize(s, YES);
381                                                         blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
382                                                         if (blksize == NOSIZE) xerror(NO, YES, YES,
383                                                         "%s: invalid block size value", s);
384                                                 }
385                                                 else blksize = (size_t)tfc_modifysize((tfc_fsize)blksize, strchr(s, ':'));
386                                                 if (!tfc_is_freestream(ctr_mode) && blksize < TF_BLOCK_SIZE) xerror(NO, YES, YES,
387                                                         "%s: block size is lesser than TF_BLOCK_SIZE (%u bytes)", s, TFC_U(TF_BLOCK_SIZE));
388                                                 if (blksize > TFC_BLKSIZE) xerror(NO, YES, YES,
389                                                         "%s: block size exceeds %u bytes",
390                                                         s, TFC_U(TFC_BLKSIZE));
391                                         }
392                                         else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
393                                                 s += 10;
394                                                 xtsblocks = (size_t)tfc_humanfsize(s, &stoi);
395                                                 if (!str_empty(stoi)) {
396                                                         xtsblocks = (size_t)tfc_fnamesize(s, YES);
397                                                         xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
398                                                         if (xtsblocks == NOSIZE) xerror(NO, YES, YES,
399                                                         "%s: invalid blocks per xts block value", s);
400                                                 }
401                                                 else xtsblocks = (size_t)tfc_modifysize((tfc_fsize)xtsblocks, strchr(s, ':'));
402                                                 if (TFC_BLKSIZE % xtsblocks) xerror(NO, YES, YES,
403                                                         "%s: nr of blocks per xts block is not round to %u bytes",
404                                                         s, TFC_U(TFC_BLKSIZE));
405                                                 if ((xtsblocks * TF_BLOCK_SIZE) > TFC_BLKSIZE) xerror(NO, YES, YES,
406                                                         "%s: nr of blocks per xts block exceeds %u bytes",
407                                                         s, TFC_U(TFC_BLKSIZE));
408                                         }
409                                         else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
410                                                 s += 6;
411                                                 iseek = tfc_humanfsize(s, &stoi);
412                                                 if (!str_empty(stoi)) {
413                                                         iseek = tfc_fnamesize(s, YES);
414                                                         iseek = tfc_modifysize(iseek, strchr(s, ':'));
415                                                         if (iseek == NOFSIZE) xerror(NO, YES, YES,
416                                                         "%s: invalid iseek value", s);
417                                                 }
418                                                 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
419                                                 if (do_edcrypt != TFC_DO_PLAIN && iseek % TF_BLOCK_SIZE)
420                                                         xerror(NO, YES, YES,
421                                                                 "%s: not round to TF block size "
422                                                                 "of %u bytes",
423                                                                 s, TFC_U(TF_BLOCK_SIZE));
424                                                 iseek_blocks = iseek / TF_BLOCK_SIZE;
425                                         }
426                                         else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
427                                                 s += 7;
428                                                 iseek = tfc_humanfsize(s, &stoi);
429                                                 if (!str_empty(stoi)) {
430                                                         iseek = tfc_fnamesize(s, YES);
431                                                         iseek = tfc_modifysize(iseek, strchr(s, ':'));
432                                                         if (iseek == NOFSIZE) xerror(NO, YES, YES,
433                                                                 "%s: invalid ixseek value", s);
434                                                 }
435                                                 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
436                                         }
437                                         else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
438                                                 s += 5;
439                                                 iseek_blocks = tfc_humanfsize(s, &stoi);
440                                                 if (!str_empty(stoi)) {
441                                                         iseek_blocks = tfc_fnamesize(s, YES);
442                                                         if (iseek_blocks == NOFSIZE)
443                                                                 xerror(NO, YES, YES,
444                                                                 "%s: invalid ictr value", s);
445                                                         iseek_blocks /= TF_BLOCK_SIZE;
446                                                         iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
447                                                 }
448                                                 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
449                                         }
450                                         else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
451                                                 s += 6;
452                                                 iseek_blocks = tfc_humanfsize(s, &stoi);
453                                                 if (!str_empty(stoi)) {
454                                                         iseek_blocks = tfc_fnamesize(s, YES);
455                                                         iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
456                                                         if (iseek_blocks == NOFSIZE)
457                                                                 xerror(NO, YES, YES,
458                                                                 "%s: invalid ixctr value", s);
459                                                 }
460                                                 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
461                                                 if (iseek_blocks % TF_BLOCK_SIZE)
462                                                         xerror(NO, YES, YES,
463                                                         "%s: not round to TF block size "
464                                                         "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
465                                                 iseek_blocks /= TF_BLOCK_SIZE;
466                                         }
467                                         else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
468                                                 s += 6;
469                                                 oseek = tfc_humanfsize(s, &stoi);
470                                                 if (!str_empty(stoi)) {
471                                                         oseek = tfc_fnamesize(s, YES);
472                                                         oseek = tfc_modifysize(oseek, strchr(s, ':'));
473                                                         if (oseek == NOFSIZE) xerror(NO, YES, YES,
474                                                         "%s: invalid oseek value", s);
475                                                 }
476                                                 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
477                                         }
478                                         else if (!strncmp(s, "ioseek", 6) && *(s+6) == '=') {
479                                                 s += 7;
480
481                                                 iseek = tfc_humanfsize(s, &stoi);
482                                                 if (!str_empty(stoi)) {
483                                                         iseek = tfc_fnamesize(s, YES);
484                                                         iseek = tfc_modifysize(iseek, strchr(s, ':'));
485                                                         if (iseek == NOFSIZE) xerror(NO, YES, YES,
486                                                         "%s: invalid iseek value", s);
487                                                 }
488                                                 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
489                                                 if (do_edcrypt != TFC_DO_PLAIN && iseek % TF_BLOCK_SIZE)
490                                                         xerror(NO, YES, YES,
491                                                                 "%s: not round to TF block size "
492                                                                 "of %u bytes",
493                                                                 s, TFC_U(TF_BLOCK_SIZE));
494                                                 iseek_blocks = iseek / TF_BLOCK_SIZE;
495
496                                                 oseek = tfc_humanfsize(s, &stoi);
497                                                 if (!str_empty(stoi)) {
498                                                         oseek = tfc_fnamesize(s, YES);
499                                                         oseek = tfc_modifysize(oseek, strchr(s, ':'));
500                                                         if (oseek == NOFSIZE) xerror(NO, YES, YES,
501                                                         "%s: invalid oseek value", s);
502                                                 }
503                                                 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
504                                         }
505                                         else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
506                                                 s += 6;
507                                                 maxlen = tfc_humanfsize(s, &stoi);
508                                                 if (!str_empty(stoi)) {
509                                                         maxlen = tfc_fnamesize(s, YES);
510                                                         maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
511                                                         if (maxlen == NOFSIZE) xerror(NO, YES, YES,
512                                                         "%s: invalid count value", s);
513                                                 }
514                                                 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
515                                                 if (counter_opt == TFC_CTR_HEAD)
516                                                         maxlen += TF_BLOCK_SIZE;
517                                         }
518                                         else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
519                                                 s += 7;
520                                                 if (!strcmp(s, "tail")) {
521                                                         do_ftrunc = TFC_FTRUNC_TAIL;
522                                                         ftrunc_dfd = NOFSIZE;
523                                                 }
524                                                 else {
525                                                         do_ftrunc = TFC_DO_FTRUNC;
526                                                         ftrunc_dfd = tfc_humanfsize(s, &stoi);
527                                                         if (!str_empty(stoi)) {
528                                                                 ftrunc_dfd = tfc_fnamesize(s, YES);
529                                                                 ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
530                                                                 if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
531                                                                 "%s: invalid ftrunc value", s);
532                                                         }
533                                                         else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
534                                                 }
535                                         }
536                                         else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
537                                                 s += 5;
538                                                 maxkeylen = tfc_humanfsize(s, &stoi);
539                                                 if (!str_empty(stoi)) {
540                                                         maxkeylen = tfc_fnamesize(s, YES);
541                                                         maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
542                                                         if (maxkeylen == NOSIZE)
543                                                                 xerror(NO, YES, YES,
544                                                                 "%s: invalid key length value", s);
545                                                 }
546                                                 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
547                                         }
548                                         else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
549                                                 s += 5;
550                                                 keyoffset = tfc_humanfsize(s, &stoi);
551                                                 if (!str_empty(stoi)) {
552                                                         keyoffset = tfc_fnamesize(s, YES);
553                                                         keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
554                                                         if (keyoffset == NOFSIZE)
555                                                                 xerror(NO, YES, YES,
556                                                                 "%s: invalid key offset value", s);
557                                                 }
558                                                 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
559                                         }
560                                         else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
561                                                 s += 5;
562                                                 ctrsz = tfc_humanfsize(s, &stoi);
563                                                 if (!str_empty(stoi)) {
564                                                         ctrsz = (size_t)tfc_fnamesize(s, YES);
565                                                         ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
566                                                         if (ctrsz == NOSIZE)
567                                                                 xerror(NO, YES, YES,
568                                                                 "%s: invalid counter length value", s);
569                                                 }
570                                                 else ctrsz = (size_t)tfc_modifysize((tfc_fsize)ctrsz, strchr(s, ':'));
571                                                 if (ctrsz > TF_BLOCK_SIZE)
572                                                         xerror(NO, YES, YES, "%s: counter size cannot exceed TF block size", s);
573                                         }
574                                         else xerror(NO, YES, YES, "invalid option %s", s);
575                                 }
576                                 break;
577                         case 'S':
578                                 do_mac = TFC_MAC_SIGN;
579                                 if (strcasecmp(optarg, "mac") != 0)
580                                         do_mac_file = optarg;
581                                 break;
582                         case 'M':
583                                 do_mac = TFC_MAC_VRFY;
584                                 if (!strcasecmp(optarg, "drop"))
585                                         do_mac = TFC_MAC_DROP;
586                                 else if (strcasecmp(optarg, "mac") != 0)
587                                         do_mac_file = optarg;
588                                 break;
589                         case 'm':
590                         case 'u':
591                                 if (do_mac != TFC_MAC_VRFY)
592                                         xerror(NO, YES, YES, "signature source was not specified");
593                                 do_mac = TFC_MAC_JUST_VRFY;
594                                 if (c == 'u') do_mac = TFC_MAC_JUST_VRFY2;
595                                 break;
596                         case 'R':
597                         case 'Z':
598                                 if (maxlen != NOFSIZE) {
599                                         if (c == 'Z') genzero_nr_bytes = maxlen;
600                                         else genrandom_nr_bytes = maxlen;
601                                 }
602                                 else {
603                                         tfc_fsize t;
604                                         if (!strcasecmp(optarg, "cbs"))
605                                                 t = TF_BLOCK_SIZE;
606                                         else if (!strcasecmp(optarg, "ks"))
607                                                 t = TF_FROM_BITS(TFC_KEY_BITS);
608                                         else if (!strcasecmp(optarg, "xks"))
609                                                 t = TF_FROM_BITS(TFC_KEY_BITS) * 2;
610                                         else if (!strcasecmp(optarg, "iobs"))
611                                                 t = blksize;
612                                         else {
613                                                 t = tfc_humanfsize(optarg, &stoi);
614                                                 if (!str_empty(stoi)) {
615                                                         t = tfc_fnamesize(optarg, NO);
616                                                         t = tfc_modifysize(t, strchr(optarg, ':'));
617                                                 }
618                                                 else t = tfc_modifysize(t, strchr(optarg, ':'));
619                                         }
620                                         if (c == 'Z') genzero_nr_bytes = maxlen = t;
621                                         else genrandom_nr_bytes = maxlen = t;
622                                 }
623                                 break;
624                         case 'a':
625                                 do_preserve_time = YES;
626                                 break;
627                         case 'A':
628                                 do_outfmt = TFC_OUTFMT_B64;
629                                 break;
630                         case 'W':
631                                 do_outfmt = TFC_OUTFMT_RAW;
632                                 break;
633                         case 'H':
634                                 do_outfmt = TFC_OUTFMT_HEX;
635                                 break;
636                         case 'q':
637                                 quiet = YES;
638                                 xexit_no_nl = YES;
639                                 verbose = NO;
640                                 do_full_hexdump = NO;
641                                 status_timer = 0;
642                                 break;
643                         case 'v':
644                                 verbose = YES;
645                                 break;
646                         case 'V':
647                                 td = strtod(optarg, &stoi);
648                                 status_timer = TFC_DTOUSECS(td);
649                                 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
650                                 break;
651                         default:
652                                 usage();
653                                 break;
654                 }
655         }
656
657         if (!strcmp(progname, "tfbench")) {
658                 if (!*(argv+optind)) usage();
659
660                 td = strtod(*(argv+optind), &stoi);
661                 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
662                         xerror(NO, YES, YES,
663                         "%s: invalid benchmark time in seconds", *(argv+optind));
664                 bench_timer = TFC_DTOUSECS(td);
665                 do_benchmark(bench_timer, td);
666         }
667         if (genrandom_nr_bytes) {
668                 ctr_mode = TFC_MODE_STREAM;
669                 do_edcrypt = TFC_DO_ENCRYPT;
670                 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
671         }
672         if (genzero_nr_bytes) {
673                 ctr_mode = TFC_MODE_PLAIN;
674                 do_edcrypt = TFC_DO_PLAIN;
675                 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
676         }
677
678         if (rawkey && password)
679                 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
680         if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
681                 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
682         if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
683                 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
684         if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
685                 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
686         if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
687                 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
688         if (overwrite_source && counter_opt == TFC_CTR_RAND)
689                 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
690         if (do_edcrypt == TFC_DO_PLAIN
691         && (do_mac || saltf || rawkey || mackey_opt || counter_opt || counter_file))
692                 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
693
694         errno = 0;
695         do_stop = NO;
696
697         if (saltf) {
698                 int saltfd;
699
700                 memset(tfc_salt, 0, TFC_MAX_SALT);
701                 tfc_saltsz = 0;
702                 if (!strcasecmp(saltf, "disable")) goto _nosalt;
703
704                 if (!strcmp(saltf, "-")) saltfd = 0;
705                 else saltfd = xopen(saltf, O_RDONLY | O_LARGEFILE);
706                 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
707                 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
708                 tfc_saltsz = lio;
709                 xclose(saltfd);
710         }
711
712 _nosalt:
713         if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
714                 int mkfd = -1;
715                 tfc_yesno do_stop;
716
717                 if (!strcmp(mackeyf, "-")) mkfd = 0;
718                 else mkfd = xopen(mackeyf, O_RDONLY | O_LARGEFILE);
719
720                 skein_init(&sk, TFC_KEY_BITS);
721
722                 do_stop = NO;
723                 while (1) {
724                         if (do_stop) break;
725                         pblk = tmpdata;
726                         ldone = 0;
727                         lrem = lblock = sizeof(tmpdata);
728                         if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
729 _mkragain:              lio = xread(mkfd, pblk, lrem);
730                         if (lio == 0 && do_stop == NO) do_stop = YES;
731                         if (lio != NOSIZE) ldone += lio;
732                         else {
733                                 if (errno != EIO && catch_all_errors != YES)
734                                         xerror(NO, NO, NO, "%s", mackeyf);
735                                 switch (error_action) {
736                                         case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
737                                         case TFC_ERRACT_SYNC:
738                                         case TFC_ERRACT_LSYNC:
739                                                 xerror(YES, NO, NO, "%s", mackeyf);
740                                                 lio = ldone = lrem = lblock;
741                                                 memset(tmpdata, 0, lio);
742                                                 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
743                                                 else lseek(mkfd, rdpos + lio, SEEK_SET);
744                                                 break;
745                                         default: xerror(NO, NO, NO, "%s", mackeyf); break;
746                                 }
747                         }
748                         if (lio && lio < lrem) {
749                                 pblk += lio;
750                                 lrem -= lio;
751                                 goto _mkragain;
752                         }
753
754                         skein_update(&sk, tmpdata, ldone);
755                 }
756
757                 skein_final(mackey, &sk);
758
759                 xclose(mkfd);
760         }
761         else if (mackey_opt == TFC_MACKEY_PASSWORD) {
762                 memset(&getps, 0, sizeof(struct getpasswd_state));
763                 getps.fd = getps.efd = -1;
764                 getps.passwd = pwdask;
765                 getps.pwlen = sizeof(pwdask)-1;
766                 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
767                 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
768                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
769                 getps.flags = GETP_WAITFILL;
770                 n = xgetpasswd(&getps);
771                 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
772                 if (n == ((size_t)-2)) xexit(1);
773                 if (verbose) say_hint(pwdask, n, "MAC password hint");
774                 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
775         }
776
777         
778         if ((strlen(progname) <= 9)
779         && ((!strcmp(progname, "sksum"))
780         || ((!memcmp(progname, "sk", 2))
781         && (!memcmp(progname+3, "sum", 3)
782         || !memcmp(progname+4, "sum", 3)
783         || !memcmp(progname+5, "sum", 3)
784         || !memcmp(progname+6, "sum", 3)))))
785                 do_sksum(progname, argv+optind);
786         if (!strcmp(progname, "base64")) do_edbase64(argv+optind);
787
788         idx = optind;
789
790         if (argv[idx]) {
791                 if ((do_edcrypt == TFC_DO_PLAIN && ctr_mode == TFC_MODE_PLAIN)
792                 || password
793                 || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
794                 if (!strcmp(argv[idx], "-")) kfd = 0;
795                 else kfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
796
797                 if (do_edcrypt == TFC_DO_PLAIN && ctr_mode == TFC_MODE_XOR) {
798                         /* out: don't erase kfname if xor */
799                         idx++;
800                         goto _nokeyfd;
801                 }
802
803                 lio = strnlen(argv[idx], PATH_MAX);
804                 memset(argv[idx], '*', lio);
805
806                 idx++;
807         }
808         else password = YES;
809
810         errno = 0;
811         if (do_full_key == NO && tweakf) {
812                 int twfd;
813
814                 if (!strcmp(tweakf, "-")) twfd = 0;
815                 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
816                 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
817                 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
818                 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
819                 if (ldone < TF_TWEAK_SIZE)
820                         xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
821                 xclose(twfd);
822         }
823
824 _nokeyfd:
825         errno = 0;
826         if (argv[idx]) {
827                 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
828                 else {
829                         sfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
830                         if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
831                                 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
832                 }
833
834                 if ((do_mac >= TFC_MAC_VRFY || do_mac <= TFC_MAC_DROP) && !do_mac_file) {
835                         maxlen = tfc_fdsize(sfd);
836                         if (maxlen == NOFSIZE)
837                                 xerror(NO, YES, YES,
838                                 "Cannot verify embedded MAC with non-seekable source!");
839                         maxlen -= TF_FROM_BITS(macbits);
840                 }
841                 srcfname = argv[idx];
842                 idx++;
843         }
844
845         if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
846                 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
847
848         if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
849         if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
850
851         if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
852         errno = 0;
853         if (counter_file) {
854                 int ctrfd;
855
856                 if (!strcmp(counter_file, "-")) ctrfd = 0;
857                 else ctrfd = xopen(counter_file, O_RDONLY | O_LARGEFILE);
858                 lio = xread(ctrfd, ctr, ctrsz);
859                 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
860                 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
861                 xclose(ctrfd);
862         }
863         else if (counter_opt == TFC_CTR_HEAD) {
864                 pblk = ctr;
865                 ldone = 0;
866                 lrem = lblock = ctrsz;
867                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
868 _ctrragain:     lio = xread(sfd, pblk, lrem);
869                 if (lio != NOSIZE) ldone += lio;
870                 else {
871                         if (errno != EIO && catch_all_errors != YES)
872                                 xerror(NO, NO, NO, "%s", srcfname);
873                         switch (error_action) {
874                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
875                                 case TFC_ERRACT_SYNC:
876                                 case TFC_ERRACT_LSYNC:
877                                         xerror(YES, NO, NO, "%s", srcfname);
878                                         lio = ldone = lrem = lblock;
879                                         memset(ctr, 0, lio);
880                                         if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
881                                         else lseek(sfd, rdpos + lio, SEEK_SET);
882                                         break;
883                                 default: xerror(NO, NO, NO, "%s", srcfname); break;
884                         }
885                 }
886                 if (lio && lio < lrem) {
887                         pblk += lio;
888                         lrem -= lio;
889                         goto _ctrragain;
890                 }
891                 total_processed_src += ldone;
892         }
893
894 _ctrskip1:
895         if (iseek) {
896                 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
897                         iseek += ctrsz;
898                 if (lseek(sfd, iseek, SEEK_SET) == -1)
899                         xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
900         }
901
902         if (do_edcrypt == TFC_DO_PLAIN) goto _plain;
903
904         if (verbose) tfc_esay("%s: hashing password", tfc_format_pid(progname));
905
906         if (rawkey == TFC_RAWKEY_KEYFILE) {
907                 tfc_yesno xtskeyset = NO;
908
909                 pblk = key;
910 _xts2key:       ldone = 0;
911                 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
912                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
913 _keyragain:     lio = xread(kfd, pblk, lrem);
914                 if (lio != NOSIZE) ldone += lio;
915                 else {
916                         if (errno != EIO && catch_all_errors != YES)
917                                 xerror(NO, NO, NO, "reading key");
918                         switch (error_action) {
919                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
920                                 case TFC_ERRACT_SYNC:
921                                 case TFC_ERRACT_LSYNC:
922                                         xerror(YES, NO, NO, "reading key");
923                                         lio = ldone = lrem = lblock;
924                                         memset(key, 0, lio);
925                                         if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
926                                         else lseek(kfd, rdpos + lio, SEEK_SET);
927                                         break;
928                                 default: xerror(NO, NO, NO, "reading key"); break;
929                         }
930                 }
931                 if (lio && lio < lrem) {
932                         pblk += lio;
933                         lrem -= lio;
934                         goto _keyragain;
935                 }
936                 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
937
938                 if (ctr_mode == TFC_MODE_XTS) {
939                         if (xtskeyset == NO) {
940                                 pblk = xtskey;
941                                 xtskeyset = YES;
942                                 goto _xts2key;
943                         }
944                 }
945         }
946         else if (rawkey == TFC_RAWKEY_ASKSTR) {
947                 tfc_yesno xtskeyset = NO;
948
949                 pblk = key; n = sizeof(key);
950 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
951                 getps.fd = getps.efd = -1;
952                 getps.passwd = (char *)pblk;
953                 getps.pwlen = n;
954                 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
955                 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
956                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
957                 getps.flags = GETP_WAITFILL;
958                 n = xgetpasswd(&getps);
959                 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
960                 if (n == ((size_t)-2)) xexit(1);
961                 if (verbose) say_hint(pblk, n, "Raw string key hint");
962                 if (ctr_mode == TFC_MODE_XTS) {
963                         if (xtskeyset == NO) {
964                                 pblk = xtskey; n = sizeof(xtskey);
965                                 xtskeyset = YES;
966                                 goto _xts2keyaskstr;
967                         }
968                 }
969         }
970         else if (rawkey == TFC_RAWKEY_ASKHEX) {
971                 tfc_yesno xtskeyset = NO;
972
973                 pblk = key;
974 _rawkey_hex_again:
975                 memset(&getps, 0, sizeof(struct getpasswd_state));
976                 getps.fd = getps.efd = -1;
977                 getps.passwd = pwdask;
978                 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
979                 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
980                 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
981                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
982                 getps.flags = GETP_WAITFILL;
983                 n = xgetpasswd(&getps);
984                 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
985                 if (n == ((size_t)-2)) xexit(1);
986                 if (n % 2) {
987                         tfc_esay("Please input even number of hex digits!");
988                         goto _rawkey_hex_again;
989                 }
990                 hex2bin(pblk, pwdask);
991                 memset(pwdask, 0, sizeof(pwdask));
992                 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
993                 if (ctr_mode == TFC_MODE_XTS) {
994                         if (xtskeyset == NO) {
995                                 pblk = xtskey;
996                                 xtskeyset = YES;
997                                 goto _rawkey_hex_again;
998                         }
999                 }
1000         }
1001         else if (password) {
1002 _pwdagain:      memset(&getps, 0, sizeof(struct getpasswd_state));
1003                 getps.fd = getps.efd = -1;
1004                 getps.passwd = pwdask;
1005                 getps.pwlen = sizeof(pwdask)-1;
1006                 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
1007                 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
1008                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
1009                 getps.flags = GETP_WAITFILL;
1010                 n = xgetpasswd(&getps);
1011                 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
1012                 if (n == ((size_t)-2)) xexit(1);
1013                 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
1014                         getps.fd = getps.efd = -1;
1015                         getps.passwd = pwdagain;
1016                         getps.pwlen = sizeof(pwdagain)-1;
1017                         getps.echo = "Enter it again: ";
1018                         getps.flags = GETP_WAITFILL;
1019                         n = xgetpasswd(&getps);
1020                         if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
1021                         if (n == ((size_t)-2)) xexit(1);
1022                         if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
1023                                 tfc_esay("Passwords are different, try again");
1024                                 goto _pwdagain;
1025                         }
1026                 }
1027                 if (verbose) say_hint(pwdask, n, "Password hint");
1028                 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
1029                 memset(pwdask, 0, sizeof(pwdask));
1030                 memset(pwdagain, 0, sizeof(pwdagain));
1031         }
1032         else {
1033                 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
1034                         xerror(NO, NO, YES, "hashing key");
1035         }
1036
1037         if (rawkey == NO) {
1038                 if (tfc_saltsz > 0) {
1039                         memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
1040                         skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
1041                 }
1042                 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
1043                         skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1044                 memset(tfc_salt, 0, TFC_MAX_SALT);
1045         }
1046
1047         if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
1048                 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1049         }
1050
1051         if (genkeyf) {
1052                 int krfd;
1053                 tfc_yesno xtskeyset = NO;
1054
1055                 pblk = key;
1056                 if (!strcmp(genkeyf, "-")) krfd = 1;
1057                 else krfd = xopen(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1058 _xts2genkey:    if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
1059                 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
1060                 if (verbose && xtskeyset == NO) {
1061                         tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1062                         tfc_esay("%s: rawkey written to %s.", tfc_format_pid(progname), genkeyf);
1063                         tfc_esay("%s: Have a nice day!", tfc_format_pid(progname));
1064                 }
1065
1066                 if (ctr_mode == TFC_MODE_XTS) {
1067                         if (xtskeyset == NO) {
1068                                 pblk = xtskey;
1069                                 xtskeyset = YES;
1070                                 goto _xts2genkey;
1071                         }
1072                 }
1073
1074                 fchmod(krfd, 0600);
1075                 xclose(krfd);
1076                 xexit(0);
1077         }
1078
1079         if (do_mac != NO) {
1080                 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1081                 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1082                         "will be slower.", tfc_format_pid(progname));
1083                 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1084                 else skein_init(&sk, macbits);
1085         }
1086
1087         if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1088                 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1089         }
1090
1091         tf_convkey(key);
1092         if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1093         if (do_full_key == NO) {
1094                 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1095                 tf_tweak_set(key, tweak);
1096         }
1097         if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1098
1099         if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1100
1101         tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1102         tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1103         if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1104
1105         if (counter_opt == TFC_CTR_SHOW) {
1106                 switch (do_outfmt) {
1107                         case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1108                         case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1109                         case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1110                 }
1111         }
1112         else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1113
1114 _ctrskip2:
1115         if (kfd != -1) {
1116                 xclose(kfd);
1117                 kfd = -1;
1118         }
1119         if (verbose) tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1120
1121         if (overwrite_source && srcfname) argv[idx] = srcfname;
1122
1123 _plain:
1124         if (argv[idx]) {
1125                 if (!strcmp(argv[idx], "-")) dfd = 1;
1126                 else dfd = xxopen(YES, argv[idx], O_RDWR | O_LARGEFILE | write_flags);
1127                 if (dfd == -1) dfd = xopen(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1128                 dstfname = argv[idx];
1129                 idx++;
1130         }
1131
1132         if (oseek) {
1133                 if (lseek(dfd, oseek, SEEK_SET) == -1)
1134                         xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1135         }
1136
1137         for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1138         memset(&sigact, 0, sizeof(sigact));
1139         sigact.sa_flags = SA_RESTART;
1140         sigact.sa_handler = print_crypt_status;
1141         sigaction(SIGUSR1, &sigact, NULL);
1142         sigaction(SIGALRM, &sigact, NULL);
1143         if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
1144         sigact.sa_handler = change_status_width;
1145         sigaction(SIGQUIT, &sigact, NULL);
1146         sigact.sa_handler = change_status_timer;
1147         sigaction(SIGUSR2, &sigact, NULL);
1148         if (quiet == NO) {
1149                 sigact.sa_handler = print_crypt_status;
1150                 sigaction(SIGINT, &sigact, NULL);
1151                 sigaction(SIGTERM, &sigact, NULL);
1152                 sigaction(SIGTSTP, &sigact, NULL);
1153         }
1154         else {
1155                 sigact.sa_handler = exit_sigterm;
1156                 sigaction(SIGINT, &sigact, NULL);
1157                 sigaction(SIGTERM, &sigact, NULL);
1158                 sigact.sa_handler = handle_sigtstp;
1159                 sigaction(SIGTSTP, &sigact, NULL);
1160         }
1161         memset(&sigact, 0, sizeof(struct sigaction));
1162
1163         tfc_getcurtime(&delta_time);
1164
1165         errno = 0;
1166         if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1167                 pblk = ctr;
1168                 lio = lrem = ctrsz;
1169                 ldone = 0;
1170 _ctrwagain:     lio = xwrite(dfd, pblk, lrem);
1171                 if (lio != NOSIZE) ldone += lio;
1172                 else xerror(NO, NO, NO, "%s", dstfname);
1173                 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1174                 if (lio < lrem) {
1175                         pblk += lio;
1176                         lrem -= lio;
1177                         goto _ctrwagain;
1178                 }
1179                 total_written_dst += ldone;
1180                 total_processed_dst += ldone;
1181                 delta_processed += ldone;
1182         }
1183
1184         if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1185
1186         if (do_mac == TFC_MAC_JUST_VRFY2) {
1187                 rwd = tfc_fdgetpos(sfd);
1188                 if (rwd == NOFSIZE) {
1189                         tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", tfc_format_pid(progname));
1190                         do_mac = TFC_MAC_VRFY;
1191                 }
1192                 goto _nodecrypt_again_vrfy2;
1193
1194 _decrypt_again_vrfy2:
1195                 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1196                         xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1197                 }
1198                 total_processed_src = rwd;
1199                 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1200                 memset(svctr, 0, TF_BLOCK_SIZE);
1201         }
1202
1203 #define FLFD(x, y) (flipfd ? y : x)
1204 _nodecrypt_again_vrfy2:
1205         loopcnt = 1;
1206         errno = 0;
1207         do_stop = NO;
1208         flipfd = NO;
1209         while (1) {
1210                 if (do_stop) break;
1211                 if (ctr_mode == TFC_MODE_XOR) flipfd = NO;
1212                 pblk = srcblk;
1213 _nextblk:       ldone = 0;
1214                 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1215                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(FLFD(sfd, kfd));
1216 _ragain:        lio = xread(FLFD(sfd, kfd), pblk, lrem);
1217                 if (lio == 0) {
1218                         if ((do_read_loops != 0 && FLFD(sfd, kfd) != 0) && (loopcnt < do_read_loops)) {
1219                                 lseek(FLFD(sfd, kfd), 0L, SEEK_SET);
1220                                 loopcnt++;
1221                                 goto _ragain;
1222                         }
1223
1224                         do_stop = YES;
1225                 }
1226                 if (lio != NOSIZE) ldone += lio;
1227                 else {
1228                         if (errno != EIO && catch_all_errors != YES)
1229                                 xerror(NO, NO, NO, "%s", srcfname);
1230                         switch (error_action) {
1231                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1232                                 case TFC_ERRACT_SYNC:
1233                                 case TFC_ERRACT_LSYNC:
1234                                         xerror(YES, NO, NO, "%s", srcfname);
1235                                         lio = ldone = lrem = lblock;
1236                                         memset(srcblk, 0, lio);
1237                                         if (rdpos == NOFSIZE) lseek(FLFD(sfd, kfd), lio, SEEK_CUR);
1238                                         else lseek(FLFD(sfd, kfd), rdpos + lio, SEEK_SET);
1239                                         break;
1240                                 default: xerror(NO, NO, NO, "%s", srcfname); break;
1241                         }
1242                 }
1243                 if (unbuffered == NO && lio && lio < lrem) {
1244                         pblk += lio;
1245                         lrem -= lio;
1246                         goto _ragain;
1247                 }
1248                 total_processed_src += ldone;
1249
1250                 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1251                         size_t orig = ldone;
1252                         ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1253                         if (ldone > blksize) ldone = blksize;
1254                         memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1255                 }
1256
1257                 if (ctr_mode == TFC_MODE_XOR && flipfd == NO) {
1258                         if (do_stop) blksize = ldone;
1259                         flipfd = YES;
1260                         pblk = dstblk;
1261                         goto _nextblk;
1262                 }
1263
1264                 if (do_mac == TFC_MAC_SIGN) skein_update(&sk, srcblk, ldone);
1265
1266                 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1267                 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1268                 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1269                         tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1270                 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1271                         tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1272                 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1273                         tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1274                 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1275                         tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1276                 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1277                         tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1278                 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1279                         tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1280                 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_ENCRYPT)
1281                         tf_pcbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1282                 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_DECRYPT)
1283                         tf_pcbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1284
1285                 else if (ctr_mode == TFC_MODE_PLAIN)
1286                         memcpy(dstblk, srcblk, ldone);
1287
1288                 else if (ctr_mode == TFC_MODE_XOR)
1289                         xor_block(dstblk, srcblk, ldone);
1290
1291                 if (do_mac >= TFC_MAC_VRFY) skein_update(&sk, dstblk, ldone);
1292                 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1293
1294                 pblk = dstblk;
1295                 lrem = ldone;
1296                 ldone = 0;
1297 _wagain:        lio = xwrite(dfd, pblk, lrem);
1298                 if (lio != NOSIZE) ldone += lio;
1299                 else xerror(NO, NO, NO, "%s", dstfname);
1300                 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1301                 if (lio < lrem) {
1302                         pblk += lio;
1303                         lrem -= lio;
1304                         goto _wagain;
1305                 }
1306                 total_written_dst += ldone;
1307 _nowrite:       total_processed_dst += ldone;
1308                 delta_processed += ldone;
1309
1310                 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1311                         do_stop = YES;
1312                         break;
1313                 }
1314         }
1315
1316         if (verbose && status_timer && do_statline_dynamic == YES && statline_was_shown == YES) tfc_esay("\n");
1317
1318         errno = 0;
1319         if (do_mac >= TFC_MAC_VRFY) {
1320                 if (!do_mac_file) {
1321                         pblk = macvrfy;
1322                         ldone = 0;
1323                         lrem = lblock = TF_FROM_BITS(macbits);
1324                         if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1325 _macragain:             lio = xread(sfd, pblk, lrem);
1326                         if (lio != NOSIZE) ldone += lio;
1327                         else {
1328                                 if (errno != EIO && catch_all_errors != YES)
1329                                         xerror(NO, NO, NO, "%s", srcfname);
1330                                 switch (error_action) {
1331                                         case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1332                                         case TFC_ERRACT_SYNC:
1333                                         case TFC_ERRACT_LSYNC:
1334                                                 xerror(YES, NO, NO, "%s", srcfname);
1335                                                 lio = ldone = lrem = lblock;
1336                                                 memset(macvrfy, 0, lio);
1337                                                 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1338                                                 else lseek(sfd, rdpos + lio, SEEK_SET);
1339                                                 break;
1340                                         default: xerror(NO, NO, NO, "%s", srcfname); break;
1341                                 }
1342                         }
1343                         if (lio && lio < lrem) {
1344                                 pblk += lio;
1345                                 lrem -= lio;
1346                                 goto _macragain;
1347                         }
1348                         total_processed_src += ldone;
1349                 }
1350                 else {
1351                         int mfd;
1352
1353                         if (!strcmp(do_mac_file, "-")) mfd = 0;
1354                         else mfd = xopen(do_mac_file, O_RDONLY | O_LARGEFILE);
1355                         lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1356                         if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1357                         if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1358                                 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1359                                         sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1360                                 lio = TF_FROM_BITS(macbits);
1361                                 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1362                         }
1363                         else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1364                         xclose(mfd);
1365                 }
1366
1367                 if (ldone < TF_FROM_BITS(macbits)) {
1368                         if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1369                                 "not verifying", tfc_format_pid(progname), ldone);
1370                         exitcode = 1;
1371                         goto _shortmac;
1372                 }
1373
1374                 skein_final(macresult, &sk);
1375
1376                 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1377                 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1378                 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1379                 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1380                 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1381                 else if (ctr_mode == TFC_MODE_PCBC) tf_pcbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1382
1383                 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1384                         if (quiet == NO) {
1385                                 tfc_esay("%s: signature is good", tfc_format_pid(progname));
1386                                 if (verbose) {
1387                                         if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1388                                         else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1389                                 }
1390                         }
1391                         if (do_mac == TFC_MAC_JUST_VRFY2) {
1392                                 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", tfc_format_pid(progname));
1393                                 maxlen = total_processed_src - SKEIN_DIGEST_SIZE;
1394                                 do_mac = TFC_MAC_DROP2;
1395                                 goto _decrypt_again_vrfy2;
1396                         }
1397                 }
1398                 else {
1399                         if (quiet == NO) {
1400                                 tfc_esay("%s: signature is BAD: "
1401                                 "wrong password, key, mode, or file is not signed", tfc_format_pid(progname));
1402                                 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", tfc_format_pid(progname));
1403                         }
1404                         exitcode = 1;
1405                 }
1406
1407 _shortmac:      memset(macvrfy, 0, sizeof(macvrfy));
1408                 memset(macresult, 0, sizeof(macresult));
1409                 memset(tmpdata, 0, sizeof(tmpdata));
1410         }
1411         else if (do_mac == TFC_MAC_SIGN) {
1412                 skein_final(macresult, &sk);
1413
1414                 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1415                 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1416                 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1417                 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1418                 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1419                 else if (ctr_mode == TFC_MODE_PCBC) tf_pcbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1420                 memset(macresult, 0, sizeof(macresult));
1421
1422                 if (!do_mac_file) {
1423                         pblk = tmpdata;
1424                         lio = lrem = TF_FROM_BITS(macbits);
1425                         ldone = 0;
1426 _macwagain:             lio = xwrite(dfd, pblk, lrem);
1427                         if (lio != NOSIZE) ldone += lio;
1428                         else xerror(NO, NO, NO, "%s", dstfname);
1429                         if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1430                         if (lio < lrem) {
1431                                 pblk += lio;
1432                                 lrem -= lio;
1433                                 goto _macwagain;
1434                         }
1435                         total_written_dst += ldone;
1436                         total_processed_dst += ldone;
1437                         delta_processed += ldone;
1438                 }
1439                 else {
1440                         int mfd;
1441
1442                         if (!strcmp(do_mac_file, "-")) mfd = 1;
1443                         else mfd = xopen(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1444                         if (do_outfmt == TFC_OUTFMT_B64) {
1445                                 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1446                                 memset(tmpdata, 0, TFC_TMPSIZE);
1447                                 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1448                                 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1449                                 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1450                                 if (lrem) {
1451                                         tmpdata[lrem] = '\n';
1452                                         lrem++;
1453                                 }
1454                                 lio = xwrite(mfd, tmpdata, lrem);
1455                         }
1456                         else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1457                         if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1458                         if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1459                         xclose(mfd);
1460                 }
1461
1462                 memset(macvrfy, 0, sizeof(macvrfy));
1463                 memset(macresult, 0, sizeof(macresult));
1464                 memset(tmpdata, 0, sizeof(tmpdata));
1465         }
1466         else if (do_mac == TFC_MAC_DROP2) total_processed_src += SKEIN_DIGEST_SIZE;
1467
1468         if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(0);
1469
1470         xexit(exitcode);
1471         return -1;
1472 }