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