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