Add password hints to every password asking function.
[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                                 status_timer = 0;
516                                 break;
517                         case 'v':
518                                 verbose = YES;
519                                 break;
520                         case 'V':
521                                 td = strtod(optarg, &stoi);
522                                 status_timer = TFC_DTOUSECS(td);
523                                 if (status_timer <= TFC_DTOUSECS(0) || !str_empty(stoi)) status_timer = 0;
524                                 break;
525                         default:
526                                 usage();
527                                 break;
528                 }
529         }
530
531         if (!strcmp(progname, "tfbench")) {
532                 if (!*(argv+optind)) usage();
533
534                 td = strtod(*(argv+optind), &stoi);
535                 if (td <= TFC_DTOUSECS(0) || !str_empty(stoi))
536                         xerror(NO, YES, YES,
537                         "%s: invalid benchmark time in seconds", *(argv+optind));
538                 bench_timer = TFC_DTOUSECS(td);
539                 do_benchmark(bench_timer, td);
540         }
541         if (genrandom_nr_bytes) {
542                 ctr_mode = TFC_MODE_STREAM;
543                 do_edcrypt = TFC_DO_ENCRYPT;
544                 gen_write_bytes(*(argv+optind), oseek, genrandom_nr_bytes);
545         }
546         if (genzero_nr_bytes) {
547                 ctr_mode = TFC_MODE_PLAIN;
548                 do_edcrypt = TFC_DO_PLAIN;
549                 gen_write_bytes(*(argv+optind), oseek, genzero_nr_bytes);
550         }
551
552         if (rawkey && password)
553                 xerror(NO, YES, YES, "Cannot use rawkey and hashing password!");
554         if (do_edcrypt == TFC_DO_ENCRYPT && do_mac >= TFC_MAC_VRFY)
555                 xerror(NO, YES, YES, "Cannot encrypt and verify signature!");
556         if (do_edcrypt == TFC_DO_DECRYPT && do_mac == TFC_MAC_SIGN)
557                 xerror(NO, YES, YES, "Cannot decrypt and calculate signature!");
558         if (do_edcrypt == TFC_DO_DECRYPT && counter_opt == TFC_CTR_RAND)
559                 xerror(NO, YES, YES, "Cannot decrypt and embed a generated CTR into file!");
560         if (do_edcrypt == TFC_DO_ENCRYPT && counter_opt == TFC_CTR_HEAD)
561                 xerror(NO, YES, YES, "Cannot encrypt and read CTR from source!");
562         if (overwrite_source && counter_opt == TFC_CTR_RAND)
563                 xerror(NO, YES, YES, "Cannot embed a CTR into file when overwriting it!");
564         if (ctr_mode == TFC_MODE_PLAIN
565         && (do_edcrypt || do_mac || rawkey
566         || mackey_opt || counter_opt || counter_file))
567                 xerror(NO, YES, YES, "Encryption facility is disabled when in plain IO mode.");
568
569         errno = 0;
570         do_stop = NO;
571
572         if (saltf) {
573                 int saltfd;
574
575                 memset(tfc_salt, 0, TFC_MAX_SALT);
576                 tfc_saltsz = 0;
577                 if (!strcasecmp(saltf, "disable")) goto _nosalt;
578
579                 if (!strcmp(saltf, "-")) saltfd = 0;
580                 else saltfd = open(saltf, O_RDONLY | O_LARGEFILE);
581                 if (saltfd == -1) xerror(NO, NO, YES, "%s", saltf);
582                 lio = xread(saltfd, tfc_salt, TFC_MAX_SALT - TF_FROM_BITS(TFC_KEY_BITS));
583                 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", saltf);
584                 tfc_saltsz = lio;
585                 xclose(saltfd);
586         }
587
588 _nosalt:
589         if (mackey_opt == TFC_MACKEY_FILE && mackeyf) {
590                 int mkfd = -1;
591                 tfc_yesno do_stop;
592
593                 if (!strcmp(mackeyf, "-")) mkfd = 0;
594                 else mkfd = open(mackeyf, O_RDONLY | O_LARGEFILE);
595                 if (mkfd == -1) xerror(NO, NO, YES, "%s", mackeyf);
596
597                 skein_init(&sk, TFC_KEY_BITS);
598
599                 do_stop = NO;
600                 while (1) {
601                         if (do_stop) break;
602                         pblk = tmpdata;
603                         ldone = 0;
604                         lrem = lblock = sizeof(tmpdata);
605                         if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(mkfd);
606 _mkragain:              lio = xread(mkfd, pblk, lrem);
607                         if (lio == 0) do_stop = YES;
608                         if (lio != NOSIZE) ldone += lio;
609                         else {
610                                 if (errno != EIO && catch_all_errors != YES)
611                                         xerror(NO, NO, NO, "%s", mackeyf);
612                                 switch (error_action) {
613                                         case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", mackeyf); goto _mkragain; break;
614                                         case TFC_ERRACT_SYNC:
615                                         case TFC_ERRACT_LSYNC:
616                                                 xerror(YES, NO, NO, "%s", mackeyf);
617                                                 lio = ldone = lrem = lblock;
618                                                 memset(tmpdata, 0, lio);
619                                                 if (rdpos == NOFSIZE) lseek(mkfd, lio, SEEK_CUR);
620                                                 else lseek(mkfd, rdpos + lio, SEEK_SET);
621                                                 break;
622                                         default: xerror(NO, NO, NO, "%s", mackeyf); break;
623                                 }
624                         }
625                         if (lio && lio < lrem) {
626                                 pblk += lio;
627                                 lrem -= lio;
628                                 goto _mkragain;
629                         }
630
631                         skein_update(&sk, tmpdata, ldone);
632                 }
633
634                 skein_final(mackey, &sk);
635
636                 xclose(mkfd);
637         }
638         else if (mackey_opt == TFC_MACKEY_PASSWORD) {
639                 memset(&getps, 0, sizeof(struct getpasswd_state));
640                 getps.fd = getps.efd = -1;
641                 getps.passwd = pwdask;
642                 getps.pwlen = sizeof(pwdask)-1;
643                 getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
644                 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
645                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
646                 getps.flags = GETP_WAITFILL;
647                 n = xgetpasswd(&getps);
648                 if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
649                 if (n == ((size_t)-2)) xexit(1);
650                 if (verbose) say_hint(pwdask, n, "MAC password hint");
651                 skein(mackey, TF_MAX_BITS, NULL, pwdask, n);
652         }
653
654         
655         if ((strlen(progname) <= 9)
656         && ((!strcmp(progname, "sksum"))
657         || ((!memcmp(progname, "sk", 2))
658         && (!memcmp(progname+3, "sum", 3)
659         || !memcmp(progname+4, "sum", 3)
660         || !memcmp(progname+5, "sum", 3)
661         || !memcmp(progname+6, "sum", 3)))))
662                 do_sksum(progname, argv+optind);
663         if (!strcmp(progname, "tfbase64")) do_edbase64(argv+optind);
664
665         idx = optind;
666
667         if (argv[idx]) {
668                 if (password || rawkey > TFC_RAWKEY_KEYFILE) goto _nokeyfd;
669                 if (!strcmp(argv[idx], "-")) kfd = 0;
670                 else kfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
671                 if (kfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
672
673                 lio = strnlen(argv[idx], PATH_MAX);
674                 memset(argv[idx], '*', lio);
675
676                 idx++;
677         }
678         else password = YES;
679
680         errno = 0;
681         if (do_full_key == NO && tweakf) {
682                 int twfd;
683
684                 if (!strcmp(tweakf, "-")) twfd = 0;
685                 else twfd = open(tweakf, O_RDONLY | O_LARGEFILE);
686                 if (twfd == -1) xerror(NO, NO, YES, "%s", tweakf);
687                 lio = ldone = xread(twfd, tweak, TF_TWEAK_SIZE);
688                 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", tweakf);
689                 if (ldone < TF_TWEAK_SIZE)
690                         xerror(NO, NO, YES, "%s: %zu bytes tweak required", tweakf, TF_TWEAK_SIZE);
691                 xclose(twfd);
692         }
693
694 _nokeyfd:
695         errno = 0;
696         if (argv[idx]) {
697                 if (!strcmp(argv[idx], "-") && kfd) sfd = 0;
698                 else {
699                         sfd = open(argv[idx], O_RDONLY | O_LARGEFILE);
700                         if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
701                                 xerror(YES, NO, YES, "stat(%s)", argv[idx]);
702                 }
703                 if (sfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
704
705                 if (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO && maxlen != NOFSIZE) {
706                         if (verbose) tfc_esay("%s: disabling signature verification on "
707                                 "requested partial decryption.", progname);
708                         do_mac = NO;
709                 }
710
711                 if ((do_mac >= TFC_MAC_VRFY || do_mac == TFC_MAC_DROP) && !do_mac_file) {
712                         maxlen = tfc_fdsize(sfd);
713                         if (maxlen == NOFSIZE)
714                                 xerror(NO, YES, YES,
715                                 "Cannot verify embedded MAC with non-seekable source!");
716                         maxlen -= TF_FROM_BITS(macbits);
717                 }
718                 srcfname = argv[idx];
719                 idx++;
720         }
721
722         if (!do_mac_file && (do_mac >= TFC_MAC_VRFY && sfd == 0))
723                 xerror(NO, YES, YES, "Cannot verify embedded MAC with non-seekable source!");
724
725         if (ctrsz == NOSIZE) ctrsz = TF_BLOCK_SIZE;
726         if (ctrsz > TF_BLOCK_SIZE) ctrsz = TF_BLOCK_SIZE;
727
728         if (ctr_mode == TFC_MODE_ECB) goto _ctrskip1;
729         errno = 0;
730         if (counter_file) {
731                 int ctrfd;
732
733                 if (!strcmp(counter_file, "-")) ctrfd = 0;
734                 else ctrfd = open(counter_file, O_RDONLY | O_LARGEFILE);
735                 if (ctrfd == -1) xerror(NO, NO, YES, "%s", counter_file);
736                 lio = xread(ctrfd, ctr, ctrsz);
737                 if (lio == NOSIZE) xerror(NO, NO, YES, "%s", counter_file);
738                 if (lio < ctrsz) xerror(NO, YES, YES, "counter file is too small (%zu)!", lio);
739                 xclose(ctrfd);
740         }
741         else if (counter_opt == TFC_CTR_HEAD) {
742                 pblk = ctr;
743                 ldone = 0;
744                 lrem = lblock = ctrsz;
745                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
746 _ctrragain:     lio = xread(sfd, pblk, lrem);
747                 if (lio != NOSIZE) ldone += lio;
748                 else {
749                         if (errno != EIO && catch_all_errors != YES)
750                                 xerror(NO, NO, NO, "%s", srcfname);
751                         switch (error_action) {
752                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ctrragain; break;
753                                 case TFC_ERRACT_SYNC:
754                                 case TFC_ERRACT_LSYNC:
755                                         xerror(YES, NO, NO, "%s", srcfname);
756                                         lio = ldone = lrem = lblock;
757                                         memset(ctr, 0, lio);
758                                         if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
759                                         else lseek(sfd, rdpos + lio, SEEK_SET);
760                                         break;
761                                 default: xerror(NO, NO, NO, "%s", srcfname); break;
762                         }
763                 }
764                 if (lio && lio < lrem) {
765                         pblk += lio;
766                         lrem -= lio;
767                         goto _ctrragain;
768                 }
769                 total_processed_src += ldone;
770         }
771
772 _ctrskip1:
773         if (iseek) {
774                 if (counter_opt == TFC_CTR_HEAD && ctr_mode != TFC_MODE_ECB)
775                         iseek += ctrsz;
776                 if (lseek(sfd, iseek, SEEK_SET) == -1)
777                         xerror(ignore_seek_errors, NO, NO, "%s: seek failed", srcfname);
778         }
779
780         if (ctr_mode == TFC_MODE_PLAIN) goto _plain;
781
782         if (verbose) tfc_esay("%s: hashing password", progname);
783
784         if (rawkey == TFC_RAWKEY_KEYFILE) {
785                 tfc_yesno xtskeyset = NO;
786
787                 pblk = key;
788 _xts2key:       ldone = 0;
789                 lrem = lblock = TF_FROM_BITS(TFC_KEY_BITS);
790                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(kfd);
791 _keyragain:     lio = xread(kfd, pblk, lrem);
792                 if (lio != NOSIZE) ldone += lio;
793                 else {
794                         if (errno != EIO && catch_all_errors != YES)
795                                 xerror(NO, NO, NO, "reading key");
796                         switch (error_action) {
797                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "reading key"); goto _keyragain; break;
798                                 case TFC_ERRACT_SYNC:
799                                 case TFC_ERRACT_LSYNC:
800                                         xerror(YES, NO, NO, "reading key");
801                                         lio = ldone = lrem = lblock;
802                                         memset(key, 0, lio);
803                                         if (rdpos == NOFSIZE) lseek(kfd, lio, SEEK_CUR);
804                                         else lseek(kfd, rdpos + lio, SEEK_SET);
805                                         break;
806                                 default: xerror(NO, NO, NO, "reading key"); break;
807                         }
808                 }
809                 if (lio && lio < lrem) {
810                         pblk += lio;
811                         lrem -= lio;
812                         goto _keyragain;
813                 }
814                 if (ldone < lblock) xerror(NO, YES, YES, "rawkey too small! (%zu)", ldone);
815
816                 if (ctr_mode == TFC_MODE_XTS) {
817                         if (xtskeyset == NO) {
818                                 pblk = xtskey;
819                                 xtskeyset = YES;
820                                 goto _xts2key;
821                         }
822                 }
823         }
824         else if (rawkey == TFC_RAWKEY_ASKSTR) {
825                 tfc_yesno xtskeyset = NO;
826
827                 pblk = key; n = sizeof(key);
828 _xts2keyaskstr: memset(&getps, 0, sizeof(struct getpasswd_state));
829                 getps.fd = getps.efd = -1;
830                 getps.passwd = (char *)pblk;
831                 getps.pwlen = n;
832                 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
833                 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
834                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
835                 getps.flags = GETP_WAITFILL;
836                 n = xgetpasswd(&getps);
837                 if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
838                 if (n == ((size_t)-2)) xexit(1);
839                 if (verbose) say_hint(pblk, n, "Raw string key hint");
840                 if (ctr_mode == TFC_MODE_XTS) {
841                         if (xtskeyset == NO) {
842                                 pblk = xtskey; n = sizeof(xtskey);
843                                 xtskeyset = YES;
844                                 goto _xts2keyaskstr;
845                         }
846                 }
847         }
848         else if (rawkey == TFC_RAWKEY_ASKHEX) {
849                 tfc_yesno xtskeyset = NO;
850
851                 pblk = key;
852 _rawkey_hex_again:
853                 memset(&getps, 0, sizeof(struct getpasswd_state));
854                 getps.fd = getps.efd = -1;
855                 getps.passwd = pwdask;
856                 getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
857                 getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
858                 getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
859                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
860                 getps.flags = GETP_WAITFILL;
861                 n = xgetpasswd(&getps);
862                 if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
863                 if (n == ((size_t)-2)) xexit(1);
864                 if (n % 2) {
865                         tfc_esay("Please input even number of hex digits!");
866                         goto _rawkey_hex_again;
867                 }
868                 hex2bin(pblk, pwdask);
869                 memset(pwdask, 0, sizeof(pwdask));
870                 if (verbose) say_hint(pblk, n/2, "Raw hex key hint");
871                 if (ctr_mode == TFC_MODE_XTS) {
872                         if (xtskeyset == NO) {
873                                 pblk = xtskey;
874                                 xtskeyset = YES;
875                                 goto _rawkey_hex_again;
876                         }
877                 }
878         }
879         else if (password) {
880 _pwdagain:      memset(&getps, 0, sizeof(struct getpasswd_state));
881                 getps.fd = getps.efd = -1;
882                 getps.passwd = pwdask;
883                 getps.pwlen = sizeof(pwdask)-1;
884                 getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
885                 getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
886                 getps.maskchar = (show_secrets == YES) ? 0 : 'x';
887                 getps.flags = GETP_WAITFILL;
888                 n = xgetpasswd(&getps);
889                 if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
890                 if (n == ((size_t)-2)) xexit(1);
891                 if (do_edcrypt == TFC_DO_ENCRYPT && no_repeat == NO) {
892                         getps.fd = getps.efd = -1;
893                         getps.passwd = pwdagain;
894                         getps.pwlen = sizeof(pwdagain)-1;
895                         getps.echo = "Enter it again: ";
896                         getps.flags = GETP_WAITFILL;
897                         n = xgetpasswd(&getps);
898                         if (n == NOSIZE) xerror(NO, NO, YES, "getting password again");
899                         if (n == ((size_t)-2)) xexit(1);
900                         if (strncmp(pwdask, pwdagain, sizeof(pwdagain)-1) != 0) {
901                                 tfc_esay("Passwords are different, try again");
902                                 goto _pwdagain;
903                         }
904                 }
905                 if (verbose) say_hint(pwdask, n, "Password hint");
906                 skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, pwdask, n);
907                 memset(pwdask, 0, sizeof(pwdask));
908                 memset(pwdagain, 0, sizeof(pwdagain));
909         }
910         else {
911                 if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES)
912                         xerror(NO, NO, YES, "hashing key");
913         }
914
915         if (rawkey == NO) {
916                 if (tfc_saltsz > 0) {
917                         memcpy(tfc_salt+tfc_saltsz, key, TF_FROM_BITS(TFC_KEY_BITS));
918                         skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, tfc_salt, tfc_saltsz+TF_FROM_BITS(TFC_KEY_BITS));
919                 }
920                 if (nr_turns > 1) for (x = 0; x < nr_turns; x++)
921                         skein(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
922                 memset(tfc_salt, 0, TFC_MAX_SALT);
923         }
924
925         if (ctr_mode == TFC_MODE_XTS && rawkey == NO) {
926                 skein(xtskey, TF_NR_KEY_BITS, mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
927         }
928
929         if (genkeyf) {
930                 int krfd;
931                 tfc_yesno xtskeyset = NO;
932
933                 pblk = key;
934                 if (!strcmp(genkeyf, "-")) krfd = 1;
935                 else krfd = open(genkeyf, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
936                 if (krfd == -1) xerror(NO, NO, YES, "%s", genkeyf);
937 _xts2genkey:    if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerror(NO, NO, YES, "%s", genkeyf);
938                 if (do_fsync && fsync(krfd) == -1) xerror(NO, NO, YES, "%s", genkeyf);
939                 if (verbose && xtskeyset == NO) {
940                         tfc_esay("%s: password hashing done", progname);
941                         tfc_esay("%s: rawkey written to %s.", progname, genkeyf);
942                         tfc_esay("%s: Have a nice day!", progname);
943                 }
944
945                 if (ctr_mode == TFC_MODE_XTS) {
946                         if (xtskeyset == NO) {
947                                 pblk = xtskey;
948                                 xtskeyset = YES;
949                                 goto _xts2genkey;
950                         }
951                 }
952
953                 fchmod(krfd, 0600);
954                 xclose(krfd);
955                 xexit(0);
956         }
957
958         if (iseek_blocks && (do_edcrypt == TFC_DO_DECRYPT && do_mac != NO)) {
959                 if (verbose) tfc_esay("%s: disabling signature verification on "
960                         "requested partial decryption.", progname);
961                 do_mac = NO;
962         }
963
964         if (do_mac != NO) {
965                 if (mackey_opt == TFC_MACKEY_RAWKEY) skein(mackey, TF_MAX_BITS, key, key, TF_FROM_BITS(TFC_KEY_BITS));
966                 if (ctr_mode < TFC_MODE_OCB) {
967                         if (verbose) tfc_esay("%s: doing MAC calculation, processing speed "
968                                 "will be slower.", progname);
969                         if (mackey_opt) skein_init_key(&sk, mackey, macbits);
970                         else skein_init(&sk, macbits);
971                 }
972         }
973
974         if (!counter_file && counter_opt <= TFC_CTR_SHOW && ctr_mode != TFC_MODE_ECB) {
975                 skein(ctr, TF_TO_BITS(ctrsz), mackey_opt ? mackey : NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
976         }
977
978         tf_convkey(key);
979         if (ctr_mode == TFC_MODE_XTS) tf_convkey(xtskey);
980         if (do_full_key == NO) {
981                 if (!tweakf) skein(tweak, TF_NR_TWEAK_BITS, NULL, key, TF_FROM_BITS(TFC_KEY_BITS));
982                 tf_tweak_set(key, tweak);
983         }
984         if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
985
986         if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
987
988         tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
989         tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
990
991         if (counter_opt == TFC_CTR_SHOW) {
992                 switch (do_outfmt) {
993                         case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
994                         case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
995                         case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
996                 }
997         }
998         else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
999
1000 _ctrskip2:
1001         if (kfd != -1) {
1002                 xclose(kfd);
1003                 kfd = -1;
1004         }
1005         if (verbose) tfc_esay("%s: password hashing done", progname);
1006
1007         if (overwrite_source && srcfname) argv[idx] = srcfname;
1008
1009 _plain:
1010         if (argv[idx]) {
1011                 if (!strcmp(argv[idx], "-")) dfd = 1;
1012                 else dfd = open(argv[idx], O_RDWR | O_LARGEFILE | write_flags, 0666);
1013                 if (dfd == -1) {
1014                         dfd = open(argv[idx], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1015                         if (dfd == -1) xerror(NO, NO, YES, "%s", argv[idx]);
1016                 }
1017                 dstfname = argv[idx];
1018                 idx++;
1019         }
1020
1021         if (oseek) {
1022                 if (lseek(dfd, oseek, SEEK_SET) == -1)
1023                         xerror(ignore_seek_errors, NO, NO, "%s: seek failed", dstfname);
1024         }
1025
1026         for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
1027         memset(&sigact, 0, sizeof(sigact));
1028         sigact.sa_flags = SA_RESTART;
1029         sigact.sa_handler = print_crypt_status;
1030         sigaction(SIGUSR1, &sigact, NULL);
1031         sigaction(SIGTSTP, &sigact, NULL);
1032         sigaction(SIGALRM, &sigact, NULL);
1033         if (status_timer) setup_next_alarm(status_timer);
1034         sigact.sa_handler = change_status_width;
1035         sigaction(SIGQUIT, &sigact, NULL);
1036         sigact.sa_handler = change_status_timer;
1037         sigaction(SIGUSR2, &sigact, NULL);
1038         if (quiet == NO) {
1039                 sigact.sa_handler = print_crypt_status;
1040                 sigaction(SIGINT, &sigact, NULL);
1041                 sigaction(SIGTERM, &sigact, NULL);
1042         }
1043         else {
1044                 sigact.sa_handler = exit_sigterm;
1045                 sigaction(SIGINT, &sigact, NULL);
1046                 sigaction(SIGTERM, &sigact, NULL);
1047         }
1048         memset(&sigact, 0, sizeof(struct sigaction));
1049
1050         tfc_getcurtime(&delta_time);
1051
1052         errno = 0;
1053         if (counter_opt == TFC_CTR_RAND && ctr_mode != TFC_MODE_ECB) {
1054                 pblk = ctr;
1055                 lio = lrem = ctrsz;
1056                 ldone = 0;
1057 _ctrwagain:     lio = xwrite(dfd, pblk, lrem);
1058                 if (lio != NOSIZE) ldone += lio;
1059                 else xerror(NO, NO, NO, "%s", dstfname);
1060                 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1061                 if (lio < lrem) {
1062                         pblk += lio;
1063                         lrem -= lio;
1064                         goto _ctrwagain;
1065                 }
1066                 total_processed_dst += ldone;
1067                 delta_processed += ldone;
1068         }
1069
1070         if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
1071
1072         errno = 0;
1073         do_stop = NO;
1074         while (1) {
1075                 if (do_stop) break;
1076                 pblk = srcblk;
1077                 ldone = 0;
1078                 lrem = lblock = blk_len_adj(maxlen, total_processed_src, blksize);
1079                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1080 _ragain:        lio = xread(sfd, pblk, lrem);
1081                 if (lio == 0) do_stop = TFC_STOP_BEGAN;
1082                 if (lio != NOSIZE) ldone += lio;
1083                 else {
1084                         if (errno != EIO && catch_all_errors != YES)
1085                                 xerror(NO, NO, NO, "%s", srcfname);
1086                         switch (error_action) {
1087                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _ragain; break;
1088                                 case TFC_ERRACT_SYNC:
1089                                 case TFC_ERRACT_LSYNC:
1090                                         xerror(YES, NO, NO, "%s", srcfname);
1091                                         lio = ldone = lrem = lblock;
1092                                         memset(srcblk, 0, lio);
1093                                         if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1094                                         else lseek(sfd, rdpos + lio, SEEK_SET);
1095                                         break;
1096                                 default: xerror(NO, NO, NO, "%s", srcfname); break;
1097                         }
1098                 }
1099                 if (lio && lio < lrem) {
1100                         pblk += lio;
1101                         lrem -= lio;
1102                         goto _ragain;
1103                 }
1104                 total_processed_src += ldone;
1105
1106                 if (do_pad && (ldone % TF_BLOCK_SIZE)) {
1107                         size_t orig = ldone;
1108                         ldone += (TF_BLOCK_SIZE - (ldone % TF_BLOCK_SIZE));
1109                         if (ldone > blksize) ldone = blksize;
1110                         memset(srcblk+orig, 0, sizeof(srcblk)-orig);
1111                 }
1112
1113                 if (do_mac == TFC_MAC_SIGN && ctr_mode < TFC_MODE_OCB)
1114                         skein_update(&sk, srcblk, ldone);
1115
1116                 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, dstblk, srcblk, ldone);
1117                 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
1118                 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
1119                         tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1120                 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
1121                         tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, xtsblocks);
1122                 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
1123                         tf_ecb_encrypt(key, dstblk, srcblk, ldone);
1124                 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
1125                         tf_ecb_decrypt(key, dstblk, srcblk, ldone);
1126                 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
1127                         tf_cbc_encrypt(key, ctr, dstblk, srcblk, ldone);
1128                 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
1129                         tf_cbc_decrypt(key, ctr, dstblk, srcblk, ldone);
1130
1131                 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_ENCRYPT)
1132                         tf_ocb_encrypt(key, ctr, dstblk, do_mac == TFC_MAC_SIGN ? macresult : NULL, srcblk, ldone, xtsblocks);
1133                 else if (ctr_mode == TFC_MODE_OCB && do_edcrypt == TFC_DO_DECRYPT)
1134                         tf_ocb_decrypt(key, ctr, dstblk, do_mac >= TFC_MAC_VRFY ? macresult : NULL, srcblk, ldone, xtsblocks);
1135
1136                 else if (ctr_mode == TFC_MODE_PLAIN)
1137                         memcpy(dstblk, srcblk, ldone);
1138
1139                 if (do_mac >= TFC_MAC_VRFY && ctr_mode < TFC_MODE_OCB)
1140                         skein_update(&sk, dstblk, ldone);
1141                 if (do_mac == TFC_MAC_JUST_VRFY) goto _nowrite;
1142
1143                 pblk = dstblk;
1144                 lrem = ldone;
1145                 ldone = 0;
1146 _wagain:        lio = xwrite(dfd, pblk, lrem);
1147                 if (lio != NOSIZE) ldone += lio;
1148                 else xerror(NO, NO, NO, "%s", dstfname);
1149                 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1150                 if (lio < lrem) {
1151                         pblk += lio;
1152                         lrem -= lio;
1153                         goto _wagain;
1154                 }
1155 _nowrite:       total_processed_dst += ldone;
1156                 delta_processed += ldone;
1157
1158                 if (maxlen != NOFSIZE && total_processed_src >= maxlen) break;
1159         }
1160
1161         if (do_stop == TFC_STOP_FULL) goto _nomac;
1162
1163         errno = 0;
1164         if (do_mac >= TFC_MAC_VRFY) {
1165                 if (!do_mac_file) {
1166                         pblk = macvrfy;
1167                         ldone = 0;
1168                         lrem = lblock = TF_FROM_BITS(macbits);
1169                         if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
1170 _macragain:             lio = xread(sfd, pblk, lrem);
1171                         if (lio != NOSIZE) ldone += lio;
1172                         else {
1173                                 if (errno != EIO && catch_all_errors != YES)
1174                                         xerror(NO, NO, NO, "%s", srcfname);
1175                                 switch (error_action) {
1176                                         case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", srcfname); goto _macragain; break;
1177                                         case TFC_ERRACT_SYNC:
1178                                         case TFC_ERRACT_LSYNC:
1179                                                 xerror(YES, NO, NO, "%s", srcfname);
1180                                                 lio = ldone = lrem = lblock;
1181                                                 memset(macvrfy, 0, lio);
1182                                                 if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
1183                                                 else lseek(sfd, rdpos + lio, SEEK_SET);
1184                                                 break;
1185                                         default: xerror(NO, NO, NO, "%s", srcfname); break;
1186                                 }
1187                         }
1188                         if (lio && lio < lrem) {
1189                                 pblk += lio;
1190                                 lrem -= lio;
1191                                 goto _macragain;
1192                         }
1193                         total_processed_src += ldone;
1194                 }
1195                 else {
1196                         int mfd;
1197
1198                         if (!strcmp(do_mac_file, "-")) mfd = 0;
1199                         else mfd = open(do_mac_file, O_RDONLY | O_LARGEFILE);
1200                         if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1201                         lio = ldone = xread(mfd, tmpdata, sizeof(tmpdata));
1202                         if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1203                         if (!memcmp(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN)) {
1204                                 memmove(tmpdata, tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN,
1205                                         sizeof(tmpdata)-TFC_ASCII_TFC_MAC_FOURCC_LEN);
1206                                 lio = TF_FROM_BITS(macbits);
1207                                 base64_decode((char *)macvrfy, lio, (char *)tmpdata, sizeof(tmpdata));
1208                         }
1209                         else memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1210                         xclose(mfd);
1211                 }
1212
1213                 if (ldone < TF_FROM_BITS(macbits)) {
1214                         if (quiet == NO) tfc_esay("%s: short signature (%zu), "
1215                                 "not verifying", progname, ldone);
1216                         exitcode = 1;
1217                         goto _shortmac;
1218                 }
1219
1220                 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1221                 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1222
1223                 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1224                 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1225                 else if (ctr_mode == TFC_MODE_XTS) tf_xts_decrypt(key, xtskey, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1226                 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_decrypt(key, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1227                 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_decrypt(key, ctr, tmpdata, macvrfy, TF_FROM_BITS(macbits));
1228                 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_decrypt(key, ctr, tmpdata, NULL, macvrfy, TF_FROM_BITS(macbits), xtsblocks);
1229
1230                 if (!memcmp(tmpdata, macresult, TF_FROM_BITS(macbits))) {
1231                         if (quiet == NO) {
1232                                 tfc_esay("%s: signature is good", progname);
1233                                 if (verbose) {
1234                                         if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stderr, macresult, TF_FROM_BITS(macbits), YES);
1235                                         else mehexdump(macresult, TF_FROM_BITS(macbits), TF_FROM_BITS(macbits), YES);
1236                                 }
1237                         }
1238                 }
1239                 else {
1240                         if (quiet == NO) tfc_esay("%s: signature is BAD: "
1241                                 "wrong password, key, mode, or file is not signed", progname);
1242                         exitcode = 1;
1243                 }
1244
1245 _shortmac:      memset(macvrfy, 0, sizeof(macvrfy));
1246                 memset(macresult, 0, sizeof(macresult));
1247                 memset(tmpdata, 0, sizeof(tmpdata));
1248         }
1249
1250         else if (do_mac == TFC_MAC_SIGN) {
1251                 if (ctr_mode < TFC_MODE_OCB) skein_final(macresult, &sk);
1252                 else skein(macresult, macbits, mackey, macresult, TF_FROM_BITS(macbits));
1253
1254                 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1255                 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, tmpdata, macresult, TF_FROM_BITS(macbits));
1256                 else if (ctr_mode == TFC_MODE_XTS) tf_xts_encrypt(key, xtskey, ctr, tmpdata, macresult, TF_FROM_BITS(macbits), xtsblocks);
1257                 else if (ctr_mode == TFC_MODE_ECB) tf_ecb_encrypt(key, tmpdata, macresult, TF_FROM_BITS(macbits));
1258                 else if (ctr_mode == TFC_MODE_CBC) tf_cbc_encrypt(key, ctr, tmpdata, macresult, TF_FROM_BITS(macbits));
1259                 else if (ctr_mode == TFC_MODE_OCB) tf_ocb_encrypt(key, ctr, tmpdata, NULL, macresult, TF_FROM_BITS(macbits), xtsblocks);
1260                 memset(macresult, 0, sizeof(macresult));
1261
1262                 if (!do_mac_file) {
1263                         pblk = tmpdata;
1264                         lio = lrem = TF_FROM_BITS(macbits);
1265                         ldone = 0;
1266 _macwagain:             lio = xwrite(dfd, pblk, lrem);
1267                         if (lio != NOSIZE) ldone += lio;
1268                         else xerror(NO, NO, NO, "%s", dstfname);
1269                         if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", dstfname);
1270                         if (lio < lrem) {
1271                                 pblk += lio;
1272                                 lrem -= lio;
1273                                 goto _macwagain;
1274                         }
1275                         total_processed_dst += ldone;
1276                         delta_processed += ldone;
1277                 }
1278                 else {
1279                         int mfd;
1280
1281                         if (!strcmp(do_mac_file, "-")) mfd = 1;
1282                         else mfd = open(do_mac_file, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
1283                         if (mfd == -1) xerror(YES, NO, NO, "%s", do_mac_file);
1284                         if (do_outfmt == TFC_OUTFMT_B64) {
1285                                 memcpy(macvrfy, tmpdata, TF_FROM_BITS(macbits));
1286                                 memset(tmpdata, 0, TFC_TMPSIZE);
1287                                 memcpy(tmpdata, TFC_ASCII_TFC_MAC_FOURCC, TFC_ASCII_TFC_MAC_FOURCC_LEN);
1288                                 base64_encode((char *)tmpdata+TFC_ASCII_TFC_MAC_FOURCC_LEN, (char *)macvrfy, TF_FROM_BITS(macbits));
1289                                 lrem = strnlen((char *)tmpdata, sizeof(tmpdata));
1290                                 if (lrem) {
1291                                         tmpdata[lrem] = '\n';
1292                                         lrem++;
1293                                 }
1294                                 lio = xwrite(mfd, tmpdata, lrem);
1295                         }
1296                         else lio = xwrite(mfd, tmpdata, TF_FROM_BITS(macbits));
1297                         if (lio == NOSIZE) xerror(NO, NO, YES, "%s", do_mac_file);
1298                         if (do_fsync && fsync(mfd) == -1) xerror(NO, NO, YES, "%s", do_mac_file);
1299                         xclose(mfd);
1300                 }
1301
1302                 memset(macvrfy, 0, sizeof(macvrfy));
1303                 memset(macresult, 0, sizeof(macresult));
1304                 memset(tmpdata, 0, sizeof(tmpdata));
1305         }
1306
1307 _nomac:
1308         if (verbose || status_timer || do_stop == TFC_STOP_FULL) print_crypt_status(0);
1309
1310         if (do_preserve_time) fcopy_matime(dfd, &s_stat);
1311         xclose(sfd);
1312         if (ftrunc_dfd != NOFSIZE) if (ftruncate(dfd, (off_t)ftrunc_dfd) == -1) xerror(YES, NO, YES, "ftruncate(%d)", dfd);
1313         xclose(dfd);
1314
1315         xexit(exitcode);
1316         return -1;
1317 }