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