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