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