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