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