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