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