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