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