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