9e0e6293804f258d2e00cae1bfee6a578b749da7
[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                                 xexit_no_nl = YES;
647                                 verbose = NO;
648                                 do_full_hexdump = NO;
649                                 status_timer = 0;
650                                 break;
651                         case 'v':
652                                 verbose = YES;
653                                 break;
654                         case 'V':
655                                 td = strtod(optarg, &stoi);
656                                 status_timer = TFC_DTOUSECS(td);
657                                 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
658                                 break;
659                         default:
660                                 usage();
661                                 break;
662                 }
663         }
664
665         if (!strcmp(progname, "tfbench")) {
666                 if (!*(argv+optind)) usage();
667
668                 td = strtod(*(argv+optind), &stoi);
669                 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
670                         xerror(NO, YES, YES,
671                         "%s: invalid benchmark time in seconds", *(argv+optind));
672                 bench_timer = TFC_DTOUSECS(td);
673                 do_benchmark(bench_timer, td);
674         }
675         if (genrandom_nr_bytes) {
676                 ctr_mode = TFC_MODE_STREAM;
677                 do_edcrypt = TFC_DO_ENCRYPT;
678                 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
679         }
680         if (genzero_nr_bytes) {
681                 ctr_mode = TFC_MODE_PLAIN;
682                 do_edcrypt = TFC_DO_PLAIN;
683                 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
684         }
685
686         if (rawkey && password)
687                 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
688         if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
689                 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
690         if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
691                 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
692         if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
693                 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
694         if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
695                 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
696         if (overwrite_source && counter_opt == TFC_CTR_RAND)
697                 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
698         if (do_edcrypt == TFC_DO_PLAIN
699         && (do_mac || saltf || rawkey || mackey_opt || counter_opt || counter_file))
700                 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
701
702         errno = 0;
703         do_stop = NO;
704
705         if (saltf) {
706                 int saltfd;
707
708                 memset(tfc_salt, 0, TFC_MAX_SALT);
709                 tfc_saltsz = 0;
710                 if (!strcasecmp(saltf, "disable")) goto _nosalt;
711
712                 if (!strcmp(saltf, "-")) saltfd = 0;
713                 else saltfd = xopen(saltf, O_RDONLY | O_LARGEFILE);
714                 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
715                 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
716                 tfc_saltsz = lio;
717                 xclose(saltfd);
718         }
719
720 _nosalt:
721         if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
722                 int mkfd = -1;
723                 tfc_yesno do_stop;
724
725                 if (!strcmp(mackeyf, "-")) mkfd = 0;
726                 else mkfd = xopen(mackeyf, O_RDONLY | O_LARGEFILE);
727
728                 skein_init(&sk, TFC_KEY_BITS);
729
730                 do_stop = NO;
731                 while (1) {
732                         if (do_stop) break;
733                         pblk = tmpdata;
734                         ldone = 0;
735                         lrem = lblock = sizeof(tmpdata);
736                         if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
737 _mkragain:              lio = xread(mkfd, pblk, lrem);
738                         if (lio == 0 && do_stop == NO) do_stop = YES;
739                         if (lio != NOSIZE) ldone += lio;
740                         else {
741                                 if (errno != EIO && catch_all_errors != YES)
742                                         xerror(NO, NO, NO, "%s", mackeyf);
743                                 switch (error_action) {
744                                         case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
745                                         case TFC_ERRACT_SYNC:
746                                         case TFC_ERRACT_LSYNC:
747                                                 xerror(YES, NO, NO, "%s", mackeyf);
748                                                 lio = ldone = lrem = lblock;
749                                                 memset(tmpdata, 0, lio);
750                                                 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
751                                                 else lseek(mkfd, rdpos + lio, SEEK_SET);
752                                                 break;
753                                         default: xerror(NO, NO, NO, "%s", mackeyf); break;
754                                 }
755                         }
756                         if (lio && lio < lrem) {
757                                 pblk += lio;
758                                 lrem -= lio;
759                                 goto _mkragain;
760                         }
761
762                         skein_update(&sk, tmpdata, ldone);
763                 }
764
765                 skein_final(mackey, &sk);
766
767                 xclose(mkfd);
768         }
769         else if (mackey_opt == TFC_MACKEY_PASSWORD) {
770                 memset(&getps, 0, sizeof(struct getpasswd_state));
771                 getps.fd = getps.efd = -1;
772                 getps.passwd = pwdask;
773                 getps.pwlen = sizeof(pwdask)-1;
774                 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
775                 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
776                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
777                 getps.flags = GETP_WAITFILL;
778                 n = xgetpasswd(&getps);
779                 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
780                 if (n == ((size_t)-2)) xexit(1);
781                 if (verbose) say_hint(pwdask, n, "MAC password hint");
782                 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
783         }
784
785         
786         if ((strlen(progname) <= 9)
787         && ((!strcmp(progname, "sksum"))
788         || ((!memcmp(progname, "sk", 2))
789         && (!memcmp(progname+3, "sum", 3)
790         || !memcmp(progname+4, "sum", 3)
791         || !memcmp(progname+5, "sum", 3)
792         || !memcmp(progname+6, "sum", 3)))))
793                 do_sksum(progname, argv+optind);
794         if (!strcmp(progname, "base64")) do_edbase64(argv+optind);
795
796         idx = optind;
797
798         if (argv[idx]) {
799                 if ((do_edcrypt == TFC_DO_PLAIN && ctr_mode == TFC_MODE_PLAIN)
800                 || password
801                 || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
802                 if (!strcmp(argv[idx], "-")) kfd = 0;
803                 else kfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
804
805                 if (do_edcrypt == TFC_DO_PLAIN && ctr_mode == TFC_MODE_XOR) {
806                         /* out: don't erase kfname if xor */
807                         idx++;
808                         goto _nokeyfd;
809                 }
810
811                 lio = strnlen(argv[idx], PATH_MAX);
812                 memset(argv[idx], '*', lio);
813
814                 idx++;
815         }
816         else password = YES;
817
818         errno = 0;
819         if (do_full_key == NO && tweakf) {
820                 int twfd;
821
822                 if (!strcmp(tweakf, "-")) twfd = 0;
823                 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
824                 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
825                 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
826                 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
827                 if (ldone < TF_TWEAK_SIZE)
828                         xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
829                 xclose(twfd);
830         }
831
832 _nokeyfd:
833         errno = 0;
834         if (argv[idx]) {
835                 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
836                 else {
837                         sfd = xopen(argv[idx], O_RDONLY | O_LARGEFILE);
838                         if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
839                                 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
840                 }
841
842                 if ((do_mac >= TFC_MAC_VRFY || do_mac <= TFC_MAC_DROP) && !do_mac_file) {
843                         maxlen = tfc_fdsize(sfd);
844                         if (maxlen == NOFSIZE)
845                                 xerror(NO, YES, YES,
846                                 "Cannot verify embedded MAC with non-seekable source!");
847                         maxlen -= TF_FROM_BITS(macbits);
848                 }
849                 srcfname = argv[idx];
850                 idx++;
851         }
852
853         if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
854                 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
855
856         if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
857         if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
858
859         if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
860         errno = 0;
861         if (counter_file) {
862                 int ctrfd;
863
864                 if (!strcmp(counter_file, "-")) ctrfd = 0;
865                 else ctrfd = xopen(counter_file, O_RDONLY | O_LARGEFILE);
866                 lio = xread(ctrfd, ctr, ctrsz);
867                 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
868                 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
869                 xclose(ctrfd);
870         }
871         else if (counter_opt == TFC_CTR_HEAD) {
872                 pblk = ctr;
873                 ldone = 0;
874                 lrem = lblock = ctrsz;
875                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
876 _ctrragain:     lio = xread(sfd, pblk, lrem);
877                 if (lio != NOSIZE) ldone += lio;
878                 else {
879                         if (errno != EIO && catch_all_errors != YES)
880                                 xerror(NO, NO, NO, "%s", srcfname);
881                         switch (error_action) {
882                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
883                                 case TFC_ERRACT_SYNC:
884                                 case TFC_ERRACT_LSYNC:
885                                         xerror(YES, NO, NO, "%s", srcfname);
886                                         lio = ldone = lrem = lblock;
887                                         memset(ctr, 0, lio);
888                                         if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
889                                         else lseek(sfd, rdpos + lio, SEEK_SET);
890                                         break;
891                                 default: xerror(NO, NO, NO, "%s", srcfname); break;
892                         }
893                 }
894                 if (lio && lio < lrem) {
895                         pblk += lio;
896                         lrem -= lio;
897                         goto _ctrragain;
898                 }
899                 total_processed_src += ldone;
900         }
901
902 _ctrskip1:
903         if (iseek) {
904                 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
905                         iseek += ctrsz;
906                 if (lseek(sfd, iseek, SEEK_SET) == -1)
907                         xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
908         }
909
910         if (do_edcrypt == TFC_DO_PLAIN) goto _plain;
911
912         if (verbose) tfc_esay("%s: hashing password", tfc_format_pid(progname));
913
914         if (rawkey == TFC_RAWKEY_KEYFILE) {
915                 tfc_yesno xtskeyset = NO;
916
917                 pblk = key;
918 _xts2key:       ldone = 0;
919                 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
920                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
921 _keyragain:     lio = xread(kfd, pblk, lrem);
922                 if (lio != NOSIZE) ldone += lio;
923                 else {
924                         if (errno != EIO && catch_all_errors != YES)
925                                 xerror(NO, NO, NO, "reading key");
926                         switch (error_action) {
927                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
928                                 case TFC_ERRACT_SYNC:
929                                 case TFC_ERRACT_LSYNC:
930                                         xerror(YES, NO, NO, "reading key");
931                                         lio = ldone = lrem = lblock;
932                                         memset(key, 0, lio);
933                                         if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
934                                         else lseek(kfd, rdpos + lio, SEEK_SET);
935                                         break;
936                                 default: xerror(NO, NO, NO, "reading key"); break;
937                         }
938                 }
939                 if (lio && lio < lrem) {
940                         pblk += lio;
941                         lrem -= lio;
942                         goto _keyragain;
943                 }
944                 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
945
946                 if (ctr_mode == TFC_MODE_XTS) {
947                         if (xtskeyset == NO) {
948                                 pblk = xtskey;
949                                 xtskeyset = YES;
950                                 goto _xts2key;
951                         }
952                 }
953         }
954         else if (rawkey == TFC_RAWKEY_ASKSTR) {
955                 tfc_yesno xtskeyset = NO;
956
957                 pblk = key; n = sizeof(key);
958 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
959                 getps.fd = getps.efd = -1;
960                 getps.passwd = (char *)pblk;
961                 getps.pwlen = n;
962                 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
963                 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
964                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
965                 getps.flags = GETP_WAITFILL;
966                 n = xgetpasswd(&getps);
967                 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
968                 if (n == ((size_t)-2)) xexit(1);
969                 if (verbose) say_hint(pblk, n, "Raw string key hint");
970                 if (ctr_mode == TFC_MODE_XTS) {
971                         if (xtskeyset == NO) {
972                                 pblk = xtskey; n = sizeof(xtskey);
973                                 xtskeyset = YES;
974                                 goto _xts2keyaskstr;
975                         }
976                 }
977         }
978         else if (rawkey == TFC_RAWKEY_ASKHEX) {
979                 tfc_yesno xtskeyset = NO;
980
981                 pblk = key;
982 _rawkey_hex_again:
983                 memset(&getps, 0, sizeof(struct getpasswd_state));
984                 getps.fd = getps.efd = -1;
985                 getps.passwd = pwdask;
986                 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
987                 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
988                 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
989                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
990                 getps.flags = GETP_WAITFILL;
991                 n = xgetpasswd(&getps);
992                 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
993                 if (n == ((size_t)-2)) xexit(1);
994                 if (n % 2) {
995                         tfc_esay("Please input even number of hex digits!");
996                         goto _rawkey_hex_again;
997                 }
998                 hex2bin(pblk, pwdask);
999                 memset(pwdask, 0, sizeof(pwdask));
1000                 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
1001                 if (ctr_mode == TFC_MODE_XTS) {
1002                         if (xtskeyset == NO) {
1003                                 pblk = xtskey;
1004                                 xtskeyset = YES;
1005                                 goto _rawkey_hex_again;
1006                         }
1007                 }
1008         }
1009         else if (password) {
1010 _pwdagain:      memset(&getps, 0, sizeof(struct getpasswd_state));
1011                 getps.fd = getps.efd = -1;
1012                 getps.passwd = pwdask;
1013                 getps.pwlen = sizeof(pwdask)-1;
1014                 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
1015                 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
1016                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
1017                 getps.flags = GETP_WAITFILL;
1018                 n = xgetpasswd(&getps);
1019                 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
1020                 if (n == ((size_t)-2)) xexit(1);
1021                 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
1022                         getps.fd = getps.efd = -1;
1023                         getps.passwd = pwdagain;
1024                         getps.pwlen = sizeof(pwdagain)-1;
1025                         getps.echo = "Enter it again: ";
1026                         getps.flags = GETP_WAITFILL;
1027                         n = xgetpasswd(&getps);
1028                         if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
1029                         if (n == ((size_t)-2)) xexit(1);
1030                         if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
1031                                 tfc_esay("Passwords are different, try again");
1032                                 goto _pwdagain;
1033                         }
1034                 }
1035                 if (verbose) say_hint(pwdask, n, "Password hint");
1036                 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
1037                 memset(pwdask, 0, sizeof(pwdask));
1038                 memset(pwdagain, 0, sizeof(pwdagain));
1039         }
1040         else {
1041                 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
1042                         xerror(NO, NO, YES, "hashing key");
1043         }
1044
1045         if (rawkey == NO) {
1046                 if (tfc_saltsz > 0) {
1047                         memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
1048                         skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
1049                 }
1050                 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
1051                         skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1052                 memset(tfc_salt, 0, TFC_MAX_SALT);
1053         }
1054
1055         if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
1056                 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1057         }
1058
1059         if (genkeyf) {
1060                 int krfd;
1061                 tfc_yesno xtskeyset = NO;
1062
1063                 pblk = key;
1064                 if (!strcmp(genkeyf, "-")) krfd = 1;
1065                 else krfd = xopen(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1066 _xts2genkey:    if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
1067                 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
1068                 if (verbose && xtskeyset == NO) {
1069                         tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1070                         tfc_esay("%s: rawkey written to %s.", tfc_format_pid(progname), genkeyf);
1071                         tfc_esay("%s: Have a nice day!", tfc_format_pid(progname));
1072                 }
1073
1074                 if (ctr_mode == TFC_MODE_XTS) {
1075                         if (xtskeyset == NO) {
1076                                 pblk = xtskey;
1077                                 xtskeyset = YES;
1078                                 goto _xts2genkey;
1079                         }
1080                 }
1081
1082                 fchmod(krfd, 0600);
1083                 xclose(krfd);
1084                 xexit(0);
1085         }
1086
1087         if (do_mac != NO) {
1088                 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
1089                 if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
1090                         "will be slower.", tfc_format_pid(progname));
1091                 if (mackey_opt) skein_init_key(&sk, mackey, macbits);
1092                 else skein_init(&sk, macbits);
1093         }
1094
1095         if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
1096                 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1097         }
1098
1099         tf_convkey(key);
1100         if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
1101         if (do_full_key == NO) {
1102                 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
1103                 tf_tweak_set(key, tweak);
1104         }
1105         if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
1106
1107         if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
1108
1109         tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
1110         tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
1111         if (do_mac == TFC_MAC_JUST_VRFY2) memcpy(svctr, ctr, TF_BLOCK_SIZE);
1112
1113         if (counter_opt == TFC_CTR_SHOW) {
1114                 switch (do_outfmt) {
1115                         case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
1116                         case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
1117                         case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
1118                 }
1119         }
1120         else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
1121
1122 _ctrskip2:
1123         if (kfd != -1) {
1124                 xclose(kfd);
1125                 kfd = -1;
1126         }
1127         if (verbose) tfc_esay("%s: password hashing done", tfc_format_pid(progname));
1128
1129         if (overwrite_source && srcfname) argv[idx] = srcfname;
1130
1131 _plain:
1132         if (argv[idx]) {
1133                 if (!strcmp(argv[idx], "-")) dfd = 1;
1134                 else dfd = xxopen(YES, argv[idx], O_RDWR | O_LARGEFILE | write_flags);
1135                 if (dfd == -1) dfd = xopen(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1136                 dstfname = argv[idx];
1137                 idx++;
1138         }
1139
1140         if (oseek) {
1141                 if (lseek(dfd, oseek, SEEK_SET) == -1)
1142                         xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1143         }
1144
1145         for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1146         memset(&sigact, 0, sizeof(sigact));
1147         sigact.sa_flags = SA_RESTART;
1148         sigact.sa_handler = print_crypt_status;
1149         sigaction(SIGUSR1, &sigact, NULL);
1150         sigaction(SIGALRM, &sigact, NULL);
1151         if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
1152         sigact.sa_handler = change_status_width;
1153         sigaction(SIGQUIT, &sigact, NULL);
1154         sigact.sa_handler = change_status_timer;
1155         sigaction(SIGUSR2, &sigact, NULL);
1156         if (quiet == NO) {
1157                 sigact.sa_handler = print_crypt_status;
1158                 sigaction(SIGINT, &sigact, NULL);
1159                 sigaction(SIGTERM, &sigact, NULL);
1160                 sigaction(SIGTSTP, &sigact, NULL);
1161         }
1162         else {
1163                 sigact.sa_handler = exit_sigterm;
1164                 sigaction(SIGINT, &sigact, NULL);
1165                 sigaction(SIGTERM, &sigact, NULL);
1166                 sigact.sa_handler = handle_sigtstp;
1167                 sigaction(SIGTSTP, &sigact, NULL);
1168         }
1169         memset(&sigact, 0, sizeof(struct sigaction));
1170
1171         tfc_getcurtime(&delta_time);
1172
1173         errno = 0;
1174         if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1175                 pblk = ctr;
1176                 lio = lrem = ctrsz;
1177                 ldone = 0;
1178 _ctrwagain:     lio = xwrite(dfd, pblk, lrem);
1179                 if (lio != NOSIZE) ldone += lio;
1180                 else xerror(NO, NO, NO, "%s", dstfname);
1181                 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1182                 if (lio < lrem) {
1183                         pblk += lio;
1184                         lrem -= lio;
1185                         goto _ctrwagain;
1186                 }
1187                 total_written_dst += ldone;
1188                 total_processed_dst += ldone;
1189                 delta_processed += ldone;
1190         }
1191
1192         if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1193
1194         if (do_mac == TFC_MAC_JUST_VRFY2) {
1195                 rwd = tfc_fdgetpos(sfd);
1196                 if (rwd == NOFSIZE) {
1197                         tfc_esay("%s: WARNING: input is not seekable, disabling MAC testing mode", tfc_format_pid(progname));
1198                         do_mac = TFC_MAC_VRFY;
1199                 }
1200                 goto _nodecrypt_again_vrfy2;
1201
1202 _decrypt_again_vrfy2:
1203                 if (lseek(sfd, (off_t)rwd, SEEK_SET) == ((off_t)-1)) {
1204                         xerror(ignore_seek_errors, NO, YES, "MAC testing seek failed");
1205                 }
1206                 total_processed_src = rwd;
1207                 memcpy(ctr, svctr, TF_BLOCK_SIZE);
1208                 if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1209                 memset(svctr, 0, TF_BLOCK_SIZE);
1210         }
1211
1212 #define FLFD(x, y) (flipfd ? y : x)
1213 _nodecrypt_again_vrfy2:
1214         loopcnt = 1;
1215         errno = 0;
1216         do_stop = NO;
1217         flipfd = NO;
1218         while (1) {
1219                 if (do_stop) break;
1220                 if (ctr_mode == TFC_MODE_XOR) flipfd = NO;
1221                 pblk = srcblk;
1222 _nextblk:       ldone = 0;
1223                 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1224                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(FLFD(sfd, kfd));
1225 _ragain:        lio = xread(FLFD(sfd, kfd), pblk, lrem);
1226                 if (lio == 0) {
1227                         if ((do_read_loops != 0 && FLFD(sfd, kfd) != 0) && (loopcnt < do_read_loops)) {
1228                                 lseek(FLFD(sfd, kfd), 0L, SEEK_SET);
1229                                 loopcnt++;
1230                                 goto _ragain;
1231                         }
1232
1233                         do_stop = YES;
1234                 }
1235                 if (lio != NOSIZE) ldone += lio;
1236                 else {
1237                         if (errno != EIO && catch_all_errors != YES)
1238                                 xerror(NO, NO, NO, "%s", srcfname);
1239                         switch (error_action) {
1240                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1241                                 case TFC_ERRACT_SYNC:
1242                                 case TFC_ERRACT_LSYNC:
1243                                         xerror(YES, NO, NO, "%s", srcfname);
1244                                         lio = ldone = lrem = lblock;
1245                                         memset(srcblk, 0, lio);
1246                                         if (rdpos == NOFSIZE) lseek(FLFD(sfd, kfd), lio, SEEK_CUR);
1247                                         else lseek(FLFD(sfd, kfd), rdpos + lio, SEEK_SET);
1248                                         break;
1249                                 default: xerror(NO, NO, NO, "%s", srcfname); break;
1250                         }
1251                 }
1252                 if (unbuffered == NO && lio && lio < lrem) {
1253                         pblk += lio;
1254                         lrem -= lio;
1255                         goto _ragain;
1256                 }
1257                 total_processed_src += ldone;
1258
1259                 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1260                         size_t orig = ldone;
1261                         ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1262                         if (ldone > blksize) ldone = blksize;
1263                         memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1264                 }
1265
1266                 if (ctr_mode == TFC_MODE_XOR && flipfd == NO) {
1267                         if (do_stop) blksize = ldone;
1268                         flipfd = YES;
1269                         pblk = dstblk;
1270                         goto _nextblk;
1271                 }
1272
1273                 if (do_mac == TFC_MAC_SIGN) skein_update(&sk, srcblk, ldone);
1274
1275                 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1276                 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1277                 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1278                         tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1279                 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1280                         tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1281                 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1282                         tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1283                 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1284                         tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1285                 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1286                         tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1287                 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1288                         tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1289                 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_ENCRYPT)
1290                         tf_pcbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1291                 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_DECRYPT)
1292                         tf_pcbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1293
1294                 else if (ctr_mode == TFC_MODE_PLAIN)
1295                         memcpy(dstblk, srcblk, ldone);
1296
1297                 else if (ctr_mode == TFC_MODE_XOR)
1298                         xor_block(dstblk, srcblk, ldone);
1299
1300                 if (do_mac >= TFC_MAC_VRFY) skein_update(&sk, dstblk, ldone);
1301                 if (do_mac >= TFC_MAC_JUST_VRFY) goto _nowrite;
1302
1303                 pblk = dstblk;
1304                 lrem = ldone;
1305                 ldone = 0;
1306 _wagain:        lio = xwrite(dfd, pblk, lrem);
1307                 if (lio != NOSIZE) ldone += lio;
1308                 else xerror(NO, NO, NO, "%s", dstfname);
1309                 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1310                 if (lio < lrem) {
1311                         pblk += lio;
1312                         lrem -= lio;
1313                         goto _wagain;
1314                 }
1315                 total_written_dst += ldone;
1316 _nowrite:       total_processed_dst += ldone;
1317                 delta_processed += ldone;
1318
1319                 if (maxlen != NOFSIZE && total_processed_src >= maxlen) {
1320                         do_stop = YES;
1321                         break;
1322                 }
1323         }
1324
1325         if (verbose && status_timer && do_statline_dynamic == YES && statline_was_shown == YES) tfc_esay("\n");
1326
1327         errno = 0;
1328         if (do_mac >= TFC_MAC_VRFY) {
1329                 if (!do_mac_file) {
1330                         pblk = macvrfy;
1331                         ldone = 0;
1332                         lrem = lblock = TF_FROM_BITS(macbits);
1333                         if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1334 _macragain:             lio = xread(sfd, pblk, lrem);
1335                         if (lio != NOSIZE) ldone += lio;
1336                         else {
1337                                 if (errno != EIO && catch_all_errors != YES)
1338                                         xerror(NO, NO, NO, "%s", srcfname);
1339                                 switch (error_action) {
1340                                         case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1341                                         case TFC_ERRACT_SYNC:
1342                                         case TFC_ERRACT_LSYNC:
1343                                                 xerror(YES, NO, NO, "%s", srcfname);
1344                                                 lio = ldone = lrem = lblock;
1345                                                 memset(macvrfy, 0, lio);
1346                                                 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1347                                                 else lseek(sfd, rdpos + lio, SEEK_SET);
1348                                                 break;
1349                                         default: xerror(NO, NO, NO, "%s", srcfname); break;
1350                                 }
1351                         }
1352                         if (lio && lio < lrem) {
1353                                 pblk += lio;
1354                                 lrem -= lio;
1355                                 goto _macragain;
1356                         }
1357                         total_processed_src += ldone;
1358                 }
1359                 else {
1360                         int mfd;
1361
1362                         if (!strcmp(do_mac_file, "-")) mfd = 0;
1363                         else mfd = xopen(do_mac_file, O_RDONLY | O_LARGEFILE);
1364                         lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1365                         if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1366                         if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1367                                 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1368                                         sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1369                                 lio = TF_FROM_BITS(macbits);
1370                                 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1371                         }
1372                         else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1373                         xclose(mfd);
1374                 }
1375
1376                 if (ldone < TF_FROM_BITS(macbits)) {
1377                         if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1378                                 "not verifying", tfc_format_pid(progname), ldone);
1379                         exitcode = 1;
1380                         goto _shortmac;
1381                 }
1382
1383                 skein_final(macresult, &sk);
1384
1385                 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1386                 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1387                 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1388                 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1389                 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1390                 else if (ctr_mode == TFC_MODE_PCBC) tf_pcbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1391
1392                 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1393                         if (quiet == NO) {
1394                                 tfc_esay("%s: signature is good", tfc_format_pid(progname));
1395                                 if (verbose) {
1396                                         if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1397                                         else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1398                                 }
1399                         }
1400                         if (do_mac == TFC_MAC_JUST_VRFY2) {
1401                                 if (verbose) tfc_esay("%s: -u: MAC signature is valid, proceeding with decrypting it again", tfc_format_pid(progname));
1402                                 maxlen = total_processed_src - SKEIN_DIGEST_SIZE;
1403                                 do_mac = TFC_MAC_DROP2;
1404                                 goto _decrypt_again_vrfy2;
1405                         }
1406                 }
1407                 else {
1408                         if (quiet == NO) {
1409                                 tfc_esay("%s: signature is BAD: "
1410                                 "wrong password, key, mode, or file is not signed", tfc_format_pid(progname));
1411                                 if (do_mac == TFC_MAC_JUST_VRFY2) tfc_esay("%s: -u: MAC signature is invalid, not decrypting it again", tfc_format_pid(progname));
1412                         }
1413                         exitcode = 1;
1414                 }
1415
1416 _shortmac:      memset(macvrfy, 0, sizeof(macvrfy));
1417                 memset(macresult, 0, sizeof(macresult));
1418                 memset(tmpdata, 0, sizeof(tmpdata));
1419         }
1420         else if (do_mac == TFC_MAC_SIGN) {
1421                 skein_final(macresult, &sk);
1422
1423                 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1424                 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1425                 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1426                 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1427                 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1428                 else if (ctr_mode == TFC_MODE_PCBC) tf_pcbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1429                 memset(macresult, 0, sizeof(macresult));
1430
1431                 if (!do_mac_file) {
1432                         pblk = tmpdata;
1433                         lio = lrem = TF_FROM_BITS(macbits);
1434                         ldone = 0;
1435 _macwagain:             lio = xwrite(dfd, pblk, lrem);
1436                         if (lio != NOSIZE) ldone += lio;
1437                         else xerror(NO, NO, NO, "%s", dstfname);
1438                         if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1439                         if (lio < lrem) {
1440                                 pblk += lio;
1441                                 lrem -= lio;
1442                                 goto _macwagain;
1443                         }
1444                         total_written_dst += ldone;
1445                         total_processed_dst += ldone;
1446                         delta_processed += ldone;
1447                 }
1448                 else {
1449                         int mfd;
1450
1451                         if (!strcmp(do_mac_file, "-")) mfd = 1;
1452                         else mfd = xopen(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
1453                         if (do_outfmt == TFC_OUTFMT_B64) {
1454                                 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1455                                 memset(tmpdata, 0, TFC_TMPSIZE);
1456                                 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1457                                 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1458                                 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1459                                 if (lrem) {
1460                                         tmpdata[lrem] = '\n';
1461                                         lrem++;
1462                                 }
1463                                 lio = xwrite(mfd, tmpdata, lrem);
1464                         }
1465                         else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1466                         if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1467                         if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1468                         xclose(mfd);
1469                 }
1470
1471                 memset(macvrfy, 0, sizeof(macvrfy));
1472                 memset(macresult, 0, sizeof(macresult));
1473                 memset(tmpdata, 0, sizeof(tmpdata));
1474         }
1475         else if (do_mac == TFC_MAC_DROP2) total_processed_src += SKEIN_DIGEST_SIZE;
1476
1477         if (verbose || status_timer || (do_stop == YES && quiet == NO)) print_crypt_status(0);
1478
1479         xexit(exitcode);
1480         return -1;
1481 }