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