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