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