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