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