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