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