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