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