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