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