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