51 broke -E logic completely, rewise it
[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:gj")) != -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 'j':
179                         case 'g':
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);
185                                 break;
186                         case '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, ':')) {
196                                         char *ss, chr;
197
198                                         counter_opt = TFC_CTR_SSET;
199                                         n = sizeof(ctr);
200
201                                         s = d = optarg; t = NULL;
202                                         while ((s = strtok_r(d, ",", &t))) {
203                                                 if (d) d = NULL;
204
205                                                 if (n == 0) break;
206                                                 ss = strchr(s, ':');
207                                                 if (!ss) continue;
208                                                 *ss = 0; ss++;
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;
213                                                 if (x > n) x = n;
214                                                 memset(ctr+(sizeof(ctr)-n), (int)chr, x);
215                                                 n -= x;
216                                         }
217                                 }
218                                 else counter_file = sksum_hashlist_file = optarg;
219                                 break;
220                         case 'C':
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);
234                                 break;
235                         case 'P':
236                         case 'X':
237                                 do_edcrypt = TFC_DO_PLAIN;
238                                 if (c == 'X') {
239                                         ctr_mode = TFC_MODE_XOR;
240                                         /* xor: default to stdin if invoked without args */
241                                         kfd = 0;
242                                 }
243                                 else ctr_mode = TFC_MODE_PLAIN;
244                                 break;
245                         case 'e':
246                                 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_ENCRYPT;
247                                 break;
248                         case 'd':
249                                 if (do_edcrypt != TFC_DO_PLAIN) do_edcrypt = TFC_DO_DECRYPT;
250                                 break;
251                         case 'D':
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);
256                                 break;
257                         case 'n':
258                                 nr_turns = sksum_turns = strtoul(optarg, &stoi, 10);
259                                 if (!str_empty(stoi)) xerror(NO, YES, YES, "%s: invalid number of turns", optarg);
260                                 break;
261                         case 'U':
262                                 if (!strcasecmp(optarg, "key"))
263                                         mackey_opt = TFC_MACKEY_RAWKEY;
264                                 else if (!strcasecmp(optarg, "pwd"))
265                                         mackey_opt = TFC_MACKEY_PASSWORD;
266                                 else {
267                                         mackey_opt = TFC_MACKEY_FILE;
268                                         mackeyf = optarg;
269                                 }
270                                 break;
271                         case 'p':
272                                 password = YES;
273                                 break;
274                         case 'k':
275                                 rawkey = TFC_RAWKEY_KEYFILE;
276                                 break;
277                         case 'z':
278                                 rawkey = TFC_RAWKEY_ASKSTR;
279                                 break;
280                         case 'x':
281                                 rawkey = TFC_RAWKEY_ASKHEX;
282                                 break;
283                         case 'K':
284                                 verbose = YES;
285                                 genkeyf = optarg;
286                                 break;
287                         case 't':
288                                 tweakf = optarg;
289                                 do_full_key = NO;
290                                 break;
291                         case 'l':
292                                 if (maxlen != NOFSIZE) break;
293
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);
300                                 }
301                                 else maxlen = tfc_modifysize(maxlen, strchr(optarg, ':'));
302                                 if (counter_opt == TFC_CTR_HEAD)
303                                         maxlen += TF_BLOCK_SIZE;
304                                 break;
305                         case 'w':
306                                 overwrite_source = YES;
307                                 break;
308                         case 'E':
309                                 if (!strcmp(optarg, "xall")) {
310                                         catch_all_errors = YES;
311                                         break;
312                                 }
313                                 if (!strcmp(optarg, "xseek")) {
314                                         ignore_seek_errors = YES;
315                                         break;
316                                 }
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);
326                                 break;
327                         case 'o':
328                                 open_log(optarg);
329                                 break;
330                         case 'O':
331                                 s = d = optarg; t = NULL;
332                                 while ((s = strtok_r(d, ",", &t))) {
333                                         if (d) d = NULL;
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"))
341                                                 do_fsync = YES;
342                                         else if (!strcmp(s, "pad"))
343                                                 do_pad = YES;
344                                         else if (!strcmp(s, "ro"))
345                                                 read_only = YES;
346                                         else if (!strcmp(s, "rw"))
347                                                 read_only = NO;
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"))
355                                                 do_less_stats = YES;
356                                         else if (!strcmp(s, "norepeat"))
357                                                 no_repeat = YES;
358                                         else if (!strncmp(s, "prompt", 6) && *(s+6) == '=')
359                                                 pw_prompt = s+7;
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"))
365                                                 do_full_key = YES;
366                                         else if (!strcmp(s, "showsecrets"))
367                                                 show_secrets = YES;
368                                         else if (!strcmp(s, "finished"))
369                                                 show_when_done = YES;
370                                         else if (!strcmp(s, "pid"))
371                                                 show_pid = YES;
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;
377                                         }
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;
381                                         }
382                                         else if (!strncmp(s, "logfile", 7) && *(s+7) == '=')
383                                                 open_log(s+8);
384                                         else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
385                                                 s += 5;
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);
392                                                 }
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));
399                                         }
400                                         else if (!strncmp(s, "xtsblocks", 9) && *(s+9) == '=') {
401                                                 s += 10;
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);
408                                                 }
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));
416                                         }
417                                         else if (!strncmp(s, "iseek", 5) && *(s+5) == '=') {
418                                                 s += 6;
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);
425                                                 }
426                                                 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
427                                                 if (do_edcrypt != TFC_DO_PLAIN && iseek % TF_BLOCK_SIZE)
428                                                         xerror(NO, YES, YES,
429                                                                 "%s: not round to TF block size "
430                                                                 "of %u bytes",
431                                                                 s, TFC_U(TF_BLOCK_SIZE));
432                                                 iseek_blocks = iseek / TF_BLOCK_SIZE;
433                                         }
434                                         else if (!strncmp(s, "ixseek", 6) && *(s+6) == '=') {
435                                                 s += 7;
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);
442                                                 }
443                                                 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
444                                         }
445                                         else if (!strncmp(s, "ictr", 4) && *(s+4) == '=') {
446                                                 s += 5;
447                                                 iseek_blocks = tfc_humanfsize(s, &stoi);
448                                                 if (!str_empty(stoi)) {
449                                                         iseek_blocks = tfc_fnamesize(s, YES);
450                                                         if (iseek_blocks == NOFSIZE)
451                                                                 xerror(NO, YES, YES,
452                                                                 "%s: invalid ictr value", s);
453                                                         iseek_blocks /= TF_BLOCK_SIZE;
454                                                         iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
455                                                 }
456                                                 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
457                                         }
458                                         else if (!strncmp(s, "ixctr", 5) && *(s+5) == '=') {
459                                                 s += 6;
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)
465                                                                 xerror(NO, YES, YES,
466                                                                 "%s: invalid ixctr value", s);
467                                                 }
468                                                 else iseek_blocks = tfc_modifysize(iseek_blocks, strchr(s, ':'));
469                                                 if (iseek_blocks % TF_BLOCK_SIZE)
470                                                         xerror(NO, YES, YES,
471                                                         "%s: not round to TF block size "
472                                                         "of %u bytes", s, TFC_U(TF_BLOCK_SIZE));
473                                                 iseek_blocks /= TF_BLOCK_SIZE;
474                                         }
475                                         else if (!strncmp(s, "oseek", 5) && *(s+5) == '=') {
476                                                 s += 6;
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);
483                                                 }
484                                                 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
485                                         }
486                                         else if (!strncmp(s, "ioseek", 6) && *(s+6) == '=') {
487                                                 s += 7;
488
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);
495                                                 }
496                                                 else iseek = tfc_modifysize(iseek, strchr(s, ':'));
497                                                 if (do_edcrypt != TFC_DO_PLAIN && iseek % TF_BLOCK_SIZE)
498                                                         xerror(NO, YES, YES,
499                                                                 "%s: not round to TF block size "
500                                                                 "of %u bytes",
501                                                                 s, TFC_U(TF_BLOCK_SIZE));
502                                                 iseek_blocks = iseek / TF_BLOCK_SIZE;
503
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);
510                                                 }
511                                                 else oseek = tfc_modifysize(oseek, strchr(s, ':'));
512                                         }
513                                         else if (!strncmp(s, "count", 5) && *(s+5) == '=') {
514                                                 s += 6;
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);
521                                                 }
522                                                 else maxlen = tfc_modifysize(maxlen, strchr(s, ':'));
523                                                 if (counter_opt == TFC_CTR_HEAD)
524                                                         maxlen += TF_BLOCK_SIZE;
525                                         }
526                                         else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
527                                                 s += 7;
528                                                 if (!strcmp(s, "tail")) {
529                                                         do_ftrunc = TFC_FTRUNC_TAIL;
530                                                         ftrunc_dfd = NOFSIZE;
531                                                 }
532                                                 else {
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);
540                                                         }
541                                                         else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
542                                                 }
543                                         }
544                                         else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
545                                                 s += 5;
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)
551                                                                 xerror(NO, YES, YES,
552                                                                 "%s: invalid key length value", s);
553                                                 }
554                                                 else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':'));
555                                         }
556                                         else if (!strncmp(s, "okey", 4) && *(s+4) == '=') {
557                                                 s += 5;
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)
563                                                                 xerror(NO, YES, YES,
564                                                                 "%s: invalid key offset value", s);
565                                                 }
566                                                 else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':'));
567                                         }
568                                         else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') {
569                                                 s += 5;
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, ':'));
574                                                         if (ctrsz == NOSIZE)
575                                                                 xerror(NO, YES, YES,
576                                                                 "%s: invalid counter length value", s);
577                                                 }
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);
581                                         }
582                                         else xerror(NO, YES, YES, "invalid option %s", s);
583                                 }
584                                 break;
585                         case 'S':
586                                 do_mac = TFC_MAC_SIGN;
587                                 if (strcasecmp(optarg, "mac") != 0)
588                                         do_mac_file = optarg;
589                                 break;
590                         case 'M':
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;
596                                 break;
597                         case 'm':
598                         case 'u':
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;
603                                 break;
604                         case 'R':
605                         case 'Z':
606                                 if (maxlen != NOFSIZE) {
607                                         if (c == 'Z') genzero_nr_bytes = maxlen;
608                                         else genrandom_nr_bytes = maxlen;
609                                 }
610                                 else {
611                                         tfc_fsize t;
612                                         if (!strcasecmp(optarg, "cbs"))
613                                                 t = TF_BLOCK_SIZE;
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"))
619                                                 t = blksize;
620                                         else {
621                                                 t = tfc_humanfsize(optarg, &stoi);
622                                                 if (!str_empty(stoi)) {
623                                                         t = tfc_fnamesize(optarg, NO);
624                                                         t = tfc_modifysize(t, strchr(optarg, ':'));
625                                                 }
626                                                 else t = tfc_modifysize(t, strchr(optarg, ':'));
627                                         }
628                                         if (c == 'Z') genzero_nr_bytes = maxlen = t;
629                                         else genrandom_nr_bytes = maxlen = t;
630                                 }
631                                 break;
632                         case 'a':
633                                 do_preserve_time = YES;
634                                 break;
635                         case 'A':
636                                 do_outfmt = TFC_OUTFMT_B64;
637                                 break;
638                         case 'W':
639                                 do_outfmt = TFC_OUTFMT_RAW;
640                                 break;
641                         case 'H':
642                                 do_outfmt = TFC_OUTFMT_HEX;
643                                 break;
644                         case 'q':
645                                 quiet = YES;
646                                 verbose = NO;
647                                 do_full_hexdump = NO;
648                                 status_timer = 0;
649                                 break;
650                         case 'v':
651                                 verbose = YES;
652                                 break;
653                         case 'V':
654                                 td = strtod(optarg, &stoi);
655                                 status_timer = TFC_DTOUSECS(td);
656                                 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
657                                 break;
658                         default:
659                                 usage();
660                                 break;
661                 }
662         }
663
664         if (!strcmp(progname, "tfbench")) {
665                 if (!*(argv+optind)) usage();
666
667                 td = strtod(*(argv+optind), &stoi);
668                 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
669                         xerror(NO, YES, YES,
670                         "%s: invalid benchmark time in seconds", *(argv+optind));
671                 bench_timer = TFC_DTOUSECS(td);
672                 do_benchmark(bench_timer, td);
673         }
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);
678         }
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);
683         }
684
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.");
700
701         errno = 0;
702         do_stop = NO;
703
704         if (saltf) {
705                 int saltfd;
706
707                 memset(tfc_salt, 0, TFC_MAX_SALT);
708                 tfc_saltsz = 0;
709                 if (!strcasecmp(saltf, "disable")) goto _nosalt;
710
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);
715                 tfc_saltsz = lio;
716                 xclose(saltfd);
717         }
718
719 _nosalt:
720         if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
721                 int mkfd = -1;
722                 tfc_yesno do_stop;
723
724                 if (!strcmp(mackeyf, "-")) mkfd = 0;
725                 else mkfd = xopen(mackeyf, O_RDONLY | O_LARGEFILE);
726
727                 skein_init(&sk, TFC_KEY_BITS);
728
729                 do_stop = NO;
730                 while (1) {
731                         if (do_stop) break;
732                         pblk = tmpdata;
733                         ldone = 0;
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;
739                         else {
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);
751                                                 break;
752                                         default: xerror(NO, NO, NO, "%s", mackeyf); break;
753                                 }
754                         }
755                         if (lio && lio < lrem) {
756                                 pblk += lio;
757                                 lrem -= lio;
758                                 goto _mkragain;
759                         }
760
761                         skein_update(&sk, tmpdata, ldone);
762                 }
763
764                 skein_final(mackey, &sk);
765
766                 xclose(mkfd);
767         }
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);
782         }
783
784         
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);
794
795         idx = optind;
796
797         if (argv[idx]) {
798                 if ((do_edcrypt == TFC_DO_PLAIN && ctr_mode == TFC_MODE_PLAIN)
799                 || password
800                 || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
801                 if (!strcmp(argv[idx], "-")) kfd = 0;
802                 else kfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
803
804                 if (do_edcrypt == TFC_DO_PLAIN && ctr_mode == TFC_MODE_XOR) {
805                         /* out: don't erase kfname if xor */
806                         idx++;
807                         goto _nokeyfd;
808                 }
809
810                 lio = strnlen(argv[idx], PATH_MAX);
811                 memset(argv[idx], '*', lio);
812
813                 idx++;
814         }
815         else password = YES;
816
817         errno = 0;
818         if (do_full_key == NO && tweakf) {
819                 int twfd;
820
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);
828                 xclose(twfd);
829         }
830
831 _nokeyfd:
832         errno = 0;
833         if (argv[idx]) {
834                 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
835                 else {
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]);
839                 }
840
841                 if ((do_mac >= TFC_MAC_VRFY || do_mac <= TFC_MAC_DROP) && !do_mac_file) {
842                         maxlen = tfc_fdsize(sfd);
843                         if (maxlen == NOFSIZE)
844                                 xerror(NO, YES, YES,
845                                 "Cannot verify embedded MAC with non-seekable source!");
846                         maxlen -= TF_FROM_BITS(macbits);
847                 }
848                 srcfname = argv[idx];
849                 idx++;
850         }
851
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!");
854
855         if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
856         if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
857
858         if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
859         errno = 0;
860         if (counter_file) {
861                 int ctrfd;
862
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);
868                 xclose(ctrfd);
869         }
870         else if (counter_opt == TFC_CTR_HEAD) {
871                 pblk = ctr;
872                 ldone = 0;
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;
877                 else {
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;
886                                         memset(ctr, 0, lio);
887                                         if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
888                                         else lseek(sfd, rdpos + lio, SEEK_SET);
889                                         break;
890                                 default: xerror(NO, NO, NO, "%s", srcfname); break;
891                         }
892                 }
893                 if (lio && lio < lrem) {
894                         pblk += lio;
895                         lrem -= lio;
896                         goto _ctrragain;
897                 }
898                 total_processed_src += ldone;
899         }
900
901 _ctrskip1:
902         if (iseek) {
903                 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
904                         iseek += ctrsz;
905                 if (lseek(sfd, iseek, SEEK_SET) == -1)
906                         xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
907         }
908
909         if (do_edcrypt == TFC_DO_PLAIN) goto _plain;
910
911         if (verbose) tfc_esay("%s: hashing password", tfc_format_pid(progname));
912
913         if (rawkey == TFC_RAWKEY_KEYFILE) {
914                 tfc_yesno xtskeyset = NO;
915
916                 pblk = key;
917 _xts2key:       ldone = 0;
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;
922                 else {
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;
931                                         memset(key, 0, lio);
932                                         if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
933                                         else lseek(kfd, rdpos + lio, SEEK_SET);
934                                         break;
935                                 default: xerror(NO, NO, NO, "reading key"); break;
936                         }
937                 }
938                 if (lio && lio < lrem) {
939                         pblk += lio;
940                         lrem -= lio;
941                         goto _keyragain;
942                 }
943                 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
944
945                 if (ctr_mode == TFC_MODE_XTS) {
946                         if (xtskeyset == NO) {
947                                 pblk = xtskey;
948                                 xtskeyset = YES;
949                                 goto _xts2key;
950                         }
951                 }
952         }
953         else if (rawkey == TFC_RAWKEY_ASKSTR) {
954                 tfc_yesno xtskeyset = NO;
955
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;
960                 getps.pwlen = n;
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);
972                                 xtskeyset = YES;
973                                 goto _xts2keyaskstr;
974                         }
975                 }
976         }
977         else if (rawkey == TFC_RAWKEY_ASKHEX) {
978                 tfc_yesno xtskeyset = NO;
979
980                 pblk = key;
981 _rawkey_hex_again:
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);
993                 if (n % 2) {
994                         tfc_esay("Please input even number of hex digits!");
995                         goto _rawkey_hex_again;
996                 }
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) {
1002                                 pblk = xtskey;
1003                                 xtskeyset = YES;
1004                                 goto _rawkey_hex_again;
1005                         }
1006                 }
1007         }
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");
1031                                 goto _pwdagain;
1032                         }
1033                 }
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));
1038         }
1039         else {
1040                 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
1041                         xerror(NO, NO, YES, "hashing key");
1042         }
1043
1044         if (rawkey == NO) {
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));
1048                 }
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);
1052         }
1053
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));
1056         }
1057
1058         if (genkeyf) {
1059                 int krfd;
1060                 tfc_yesno xtskeyset = NO;
1061
1062                 pblk = key;
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));
1071                 }
1072
1073                 if (ctr_mode == TFC_MODE_XTS) {
1074                         if (xtskeyset == NO) {
1075                                 pblk = xtskey;
1076                                 xtskeyset = YES;
1077                                 goto _xts2genkey;
1078                         }
1079                 }
1080
1081                 fchmod(krfd, 0600);
1082                 xclose(krfd);
1083                 xexit(0);
1084         }
1085
1086         if (do_mac != 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);
1092         }
1093
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));
1096         }
1097
1098         tf_convkey(key);
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);
1103         }
1104         if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1105
1106         if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1107
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);
1111
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;
1117                 }
1118         }
1119         else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1120
1121 _ctrskip2:
1122         if (kfd != -1) {
1123                 xclose(kfd);
1124                 kfd = -1;
1125         }
1126         if (verbose) tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1127
1128         if (overwrite_source && srcfname) argv[idx] = srcfname;
1129
1130 _plain:
1131         if (argv[idx]) {
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];
1136                 idx++;
1137         }
1138
1139         if (oseek) {
1140                 if (lseek(dfd, oseek, SEEK_SET) == -1)
1141                         xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1142         }
1143
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);
1155         if (quiet == NO) {
1156                 sigact.sa_handler = print_crypt_status;
1157                 sigaction(SIGINT, &sigact, NULL);
1158                 sigaction(SIGTERM, &sigact, NULL);
1159                 sigaction(SIGTSTP, &sigact, NULL);
1160         }
1161         else {
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);
1167         }
1168         memset(&sigact, 0, sizeof(struct sigaction));
1169
1170         tfc_getcurtime(&delta_time);
1171
1172         errno = 0;
1173         if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1174                 pblk = ctr;
1175                 lio = lrem = ctrsz;
1176                 ldone = 0;
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);
1181                 if (lio < lrem) {
1182                         pblk += lio;
1183                         lrem -= lio;
1184                         goto _ctrwagain;
1185                 }
1186                 total_written_dst += ldone;
1187                 total_processed_dst += ldone;
1188                 delta_processed += ldone;
1189         }
1190
1191         if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1192
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;
1198                 }
1199                 goto _nodecrypt_again_vrfy2;
1200
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");
1204                 }
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);
1209         }
1210
1211 #define FLFD(x, y) (flipfd ? y : x)
1212 _nodecrypt_again_vrfy2:
1213         loopcnt = 1;
1214         errno = 0;
1215         do_stop = NO;
1216         flipfd = NO;
1217         while (1) {
1218                 if (do_stop) break;
1219                 if (ctr_mode == TFC_MODE_XOR) flipfd = NO;
1220                 pblk = srcblk;
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);
1225                 if (lio == 0) {
1226                         if ((do_read_loops != 0 && FLFD(sfd, kfd) != 0) && (loopcnt < do_read_loops)) {
1227                                 lseek(FLFD(sfd, kfd), 0L, SEEK_SET);
1228                                 loopcnt++;
1229                                 goto _ragain;
1230                         }
1231
1232                         do_stop = YES;
1233                 }
1234                 if (lio != NOSIZE) ldone += lio;
1235                 else {
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);
1247                                         break;
1248                                 default: xerror(NO, NO, NO, "%s", srcfname); break;
1249                         }
1250                 }
1251                 if (unbuffered == NO && lio && lio < lrem) {
1252                         pblk += lio;
1253                         lrem -= lio;
1254                         goto _ragain;
1255                 }
1256                 total_processed_src += ldone;
1257
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);
1263                 }
1264
1265                 if (ctr_mode == TFC_MODE_XOR && flipfd == NO) {
1266                         if (do_stop) blksize = ldone;
1267                         flipfd = YES;
1268                         pblk = dstblk;
1269                         goto _nextblk;
1270                 }
1271
1272                 if (do_mac == TFC_MAC_SIGN) skein_update(&sk, srcblk, ldone);
1273
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);
1292
1293                 else if (ctr_mode == TFC_MODE_PLAIN)
1294                         memcpy(dstblk, srcblk, ldone);
1295
1296                 else if (ctr_mode == TFC_MODE_XOR)
1297                         xor_block(dstblk, srcblk, ldone);
1298
1299                 if (do_mac >= TFC_MAC_VRFY) skein_update(&sk, dstblk, ldone);
1300                 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1301
1302                 pblk = dstblk;
1303                 lrem = ldone;
1304                 ldone = 0;
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);
1309                 if (lio < lrem) {
1310                         pblk += lio;
1311                         lrem -= lio;
1312                         goto _wagain;
1313                 }
1314                 total_written_dst += ldone;
1315 _nowrite:       total_processed_dst += ldone;
1316                 delta_processed += ldone;
1317
1318                 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1319                         do_stop = YES;
1320                         break;
1321                 }
1322         }
1323
1324         if (verbose && status_timer && do_statline_dynamic == YES && statline_was_shown == YES) tfc_esay("\n");
1325
1326         errno = 0;
1327         if (do_mac >= TFC_MAC_VRFY) {
1328                 if (!do_mac_file) {
1329                         pblk = macvrfy;
1330                         ldone = 0;
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;
1335                         else {
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);
1347                                                 break;
1348                                         default: xerror(NO, NO, NO, "%s", srcfname); break;
1349                                 }
1350                         }
1351                         if (lio && lio < lrem) {
1352                                 pblk += lio;
1353                                 lrem -= lio;
1354                                 goto _macragain;
1355                         }
1356                         total_processed_src += ldone;
1357                 }
1358                 else {
1359                         int mfd;
1360
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));
1370                         }
1371                         else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1372                         xclose(mfd);
1373                 }
1374
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);
1378                         exitcode = 1;
1379                         goto _shortmac;
1380                 }
1381
1382                 skein_final(macresult, &sk);
1383
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));
1390
1391                 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1392                         if (quiet == NO) {
1393                                 tfc_esay("%s: signature is good", tfc_format_pid(progname));
1394                                 if (verbose) {
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);
1397                                 }
1398                         }
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;
1404                         }
1405                 }
1406                 else {
1407                         if (quiet == NO) {
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));
1411                         }
1412                         exitcode = 1;
1413                 }
1414
1415 _shortmac:      memset(macvrfy, 0, sizeof(macvrfy));
1416                 memset(macresult, 0, sizeof(macresult));
1417                 memset(tmpdata, 0, sizeof(tmpdata));
1418         }
1419         else if (do_mac == TFC_MAC_SIGN) {
1420                 skein_final(macresult, &sk);
1421
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));
1429
1430                 if (!do_mac_file) {
1431                         pblk = tmpdata;
1432                         lio = lrem = TF_FROM_BITS(macbits);
1433                         ldone = 0;
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);
1438                         if (lio < lrem) {
1439                                 pblk += lio;
1440                                 lrem -= lio;
1441                                 goto _macwagain;
1442                         }
1443                         total_written_dst += ldone;
1444                         total_processed_dst += ldone;
1445                         delta_processed += ldone;
1446                 }
1447                 else {
1448                         int mfd;
1449
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));
1458                                 if (lrem) {
1459                                         tmpdata[lrem] = '\n';
1460                                         lrem++;
1461                                 }
1462                                 lio = xwrite(mfd, tmpdata, lrem);
1463                         }
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);
1467                         xclose(mfd);
1468                 }
1469
1470                 memset(macvrfy, 0, sizeof(macvrfy));
1471                 memset(macresult, 0, sizeof(macresult));
1472                 memset(tmpdata, 0, sizeof(tmpdata));
1473         }
1474         else if (do_mac == TFC_MAC_DROP2) total_processed_src += SKEIN_DIGEST_SIZE;
1475
1476         if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(TFC_SIGLAST);
1477
1478         xexit(exitcode);
1479         return -1;
1480 }