-E sync: reform logic. Prequery file position, jump over error hole.
[tfcrypt.git] / tfc_skein.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 #include "tfcore.h"
31
32 void skein(void *hash, size_t bits, const void *key, const void *data, size_t szdata)
33 {
34         struct skein sk;
35
36         if (key) skein_init_key(&sk, key, bits);
37         else skein_init(&sk, bits);
38         skein_update(&sk, data, szdata);
39         skein_final(hash, &sk);
40 }
41
42 void tf_key_tweak_compat(void *key)
43 {
44         TF_UNIT_TYPE *ukey = key, c = THREEFISH_CONST;
45         size_t x;
46
47         for (x = 0; x < TF_NR_BLOCK_UNITS; x++) c ^= ukey[x];
48         ukey[x] = c;
49         ukey[TF_TWEAK_WORD3] = ukey[TF_TWEAK_WORD1] ^ ukey[TF_TWEAK_WORD2];
50 }
51
52 tfc_yesno skeinfd(void *hash, size_t bits, const void *key, int fd, tfc_fsize readto)
53 {
54         static tfc_byte skblk[TFC_BLKSIZE];
55
56         struct skein sk;
57         tfc_byte *pblk;
58         size_t ldone, lblock, lrem, lio;
59         tfc_fsize total = 0;
60         tfc_yesno stop;
61
62         if (ctr_mode == TFC_MODE_SKSUM) total_processed_src = total_processed_dst = delta_processed = 0;
63
64         if (fd == -1) goto _fail;
65         if (fd > 2 && readto == NOFSIZE) {
66                 readto = tfc_fdsize(fd);
67                 if (readto == NOFSIZE) goto _fail;
68         }
69
70         if (key) skein_init_key(&sk, key, bits);
71         else skein_init(&sk, bits);
72
73         errno = 0;
74         stop = NO;
75         while (1) {
76                 if (stop) break;
77                 pblk = skblk;
78                 lblock = lrem = blk_len_adj(readto, total, TFC_BLKSIZE);
79                 ldone = 0;
80                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(fd);
81 _again:         lio = read(fd, pblk, lrem);
82                 if (lio == 0) stop = YES;
83                 if (lio != NOSIZE) ldone += lio;
84                 else {
85                         if (errno != EIO && catch_all_errors != YES) goto _fail;
86                         switch (error_action) {
87                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "skeinfd: %d", fd); goto _again; break;
88                                 case TFC_ERRACT_SYNC:
89                                 case TFC_ERRACT_LSYNC:
90                                         xerror(YES, NO, NO, "skeinfd: %d", fd);
91                                         lio = lrem = ldone = lblock;
92                                         total += lio;
93                                         memset(skblk, 0, lio);
94                                         if (rdpos == NOFSIZE) lseek(fd, lio, SEEK_CUR);
95                                         else lseek(fd, rdpos + lio, SEEK_SET);
96                                         break;
97                                 default: goto _fail; break;
98                         }
99                 }
100                 if (lio && lio < lrem) {
101                         pblk += lio;
102                         lrem -= lio;
103                         goto _again;
104                 }
105                 total += ldone;
106                 if (ctr_mode == TFC_MODE_SKSUM) {
107                         total_processed_src = total_processed_dst = total;
108                         delta_processed += ldone;
109                 }
110                 skein_update(&sk, skblk, ldone);
111                 if (readto != NOFSIZE && total >= readto) break;
112         }
113
114         if (fd > 2) lseek(fd, (off_t)readto, SEEK_SET);
115
116         skein_final(hash, &sk);
117         if (ctr_mode == TFC_MODE_SKSUM) {
118                 if (verbose || status_timer) print_crypt_status(-1);
119                 total_processed_src = total_processed_dst = delta_processed = 0;
120         }
121         memset(skblk, 0, TFC_BLKSIZE);
122         return YES;
123
124 _fail:
125         memset(&sk, 0, sizeof(struct skein));
126         memset(hash, 0, SKEIN_DIGEST_SIZE);
127         memset(skblk, 0, TFC_BLKSIZE);
128         return NO;
129 }
130
131 void do_sksum(char *spec, char **fargv)
132 {
133         static char sksblk[TFC_BLKSIZE / 2], tmp[TFC_TMPSIZE];
134         tfc_byte hash[SKEIN_DIGEST_SIZE];
135         int fd = -1;
136         int x = 0, xx;
137         size_t bits;
138
139         if (macbits < TF_MAX_BITS) {
140                 bits = macbits;
141                 goto _dothat;
142         }
143
144         if (!strcmp(spec, "sksum")) {
145                 bits = TF_MAX_BITS;
146                 goto _dothat;
147         }
148
149         if ((sscanf(spec, "sk%zusum", &bits) < 1)) {
150                 bits = TF_MAX_BITS;
151         }
152
153         if (bits < 8 || bits > TF_MAX_BITS) {
154                 xerror(NO, YES, YES,
155                 "%u: invalid bits number specified!\n"
156                 "tfcrypt supports from 8 to %u bits, divisible by 8.",
157                 bits, TFC_U(TF_MAX_BITS));
158         }
159
160         if (!bits || bits % 8) {
161                 xerror(NO, YES, YES,
162                 "%u: invalid bits number specified!\n"
163                 "Number of bits must start from 8 and be divisible by 8.",
164                 bits, TFC_U(TF_MAX_BITS));
165         }
166
167 _dothat:
168         do_edcrypt = TFC_DO_PLAIN;
169         ctr_mode = TFC_MODE_SKSUM;
170
171         for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
172         memset(&sigact, 0, sizeof(sigact));
173         sigact.sa_flags = SA_RESTART;
174         sigact.sa_handler = print_crypt_status;
175         sigaction(SIGUSR1, &sigact, NULL);
176         sigaction(SIGTSTP, &sigact, NULL);
177         sigaction(SIGALRM, &sigact, NULL);
178         sigact.sa_handler = change_status_width;
179         sigaction(SIGQUIT, &sigact, NULL);
180         sigact.sa_handler = change_status_timer;
181         sigaction(SIGUSR2, &sigact, NULL);
182         sigact.sa_handler = exit_sigterm;
183         sigaction(SIGINT, &sigact, NULL);
184         sigaction(SIGTERM, &sigact, NULL);
185         memset(&sigact, 0, sizeof(struct sigaction));
186
187         tfc_getcurtime(&delta_time);
188
189         if (sksum_hashlist_file) {
190                 FILE *f;
191                 char *s, *d, *t, *shash, *fname;
192                 int failed = 0, totaltested = 0;
193
194                 if (!strcmp(sksum_hashlist_file, "-")) f = stdin;
195                 else f = fopen(sksum_hashlist_file, "r");
196                 if (!f) xerror(NO, NO, YES, "%s", sksum_hashlist_file);
197
198                 while (1) {
199                         memset(sksblk, 0, sizeof(sksblk));
200                         x = xfgets(sksblk, sizeof(sksblk), f);
201                         if (x == 0) break;
202
203                         s = d = sksblk; t = NULL;
204                         shash = fname = NULL;
205                         while ((s = strtok_r(d, "\t", &t))) {
206                                 if (d) d = NULL;
207
208                                 if (!shash) shash = s;
209                                 else if (shash && !fname) fname = s;
210                         }
211
212                         if (!shash || !fname) {
213                                 xerror(YES, NO, YES, "invalid string %s", sksblk);
214                                 exitcode = 2;
215                                 continue;
216                         }
217
218                         s = strchr(shash, ' ');
219                         if (s && s[1] == ' ') *s = 0;
220
221                         fd = open(fname, O_RDONLY | O_LARGEFILE);
222                         if (fd == -1) {
223                                 xerror(YES, NO, YES, "%s", fname);
224                                 exitcode = 1;
225                                 continue;
226                         }
227
228                         if (status_timer) setup_next_alarm(status_timer);
229                         if (skeinfd(hash, bits, mackey_opt ? mackey : NULL, fd, maxlen) != YES) {
230                                 xerror(YES, NO, YES, "%s", fname);
231                                 exitcode = 1;
232                                 continue;
233                         }
234                         xclose(fd);
235                         if (sksum_turns > 1) {
236                                 size_t y;
237                                 for (y = 0; y < sksum_turns; y++)
238                                         skein(hash, bits, mackey_opt ? mackey : NULL, hash, TF_FROM_BITS(bits));
239                         }
240                         if (isbase64(shash)) base64_decode(tmp, sizeof(tmp), shash, strlen(shash));
241                         else hex2bin(tmp, shash);
242
243                         if (!memcmp(hash, tmp, TF_FROM_BITS(bits))) {
244                                 tfc_say("%s: OK", fname);
245                         }
246                         else {
247                                 tfc_say("%s: FAILED", fname);
248                                 failed++;
249                         }
250                         memset(tmp, 0, sizeof(tmp));
251                         memset(sksblk, 0, sizeof(sksblk));
252                         totaltested++;
253                 }
254
255                 fclose(f);
256                 if (failed) {
257                         tfc_esay("%s: WARNING: %u of %u computed checksums did NOT match",
258                                 progname, failed, totaltested);
259                         exitcode = 1;
260                 }
261                 xexit(exitcode);
262         }
263
264         for (xx = 0; fargv[xx]; xx++);
265         if (xx == 0) {
266                 fd = 0;
267                 x = 0;
268                 goto _dohash;
269         }
270
271         for (x = 0; fargv[x] && xx; x++) {
272                 if (!strcmp(fargv[x], "-")) fd = 0;
273                 else fd = open(fargv[x], O_RDONLY | O_LARGEFILE);
274                 if (fd == -1) {
275                         xerror(YES, NO, YES, "%s", fargv[x]);
276                         exitcode = 1;
277                         continue;
278                 }
279
280 _dohash:        if (status_timer) setup_next_alarm(status_timer);
281                 if (skeinfd(hash, bits, mackey_opt ? mackey : NULL, fd, maxlen) != YES) {
282                         xerror(YES, NO, YES, "%s", fargv[x]);
283                         exitcode = 1;
284                         continue;
285                 }
286                 xclose(fd);
287                 if (sksum_turns > 1) {
288                         size_t y;
289                         for (y = 0; y < sksum_turns; y++) skein(hash, bits, mackey_opt ? mackey : NULL, hash, TF_FROM_BITS(bits));
290                 }
291                 if (do_outfmt == TFC_OUTFMT_B64) tfc_printbase64(stdout, hash, TF_FROM_BITS(bits), 0);
292                 else if (do_outfmt == TFC_OUTFMT_RAW) write(1, hash, TF_FROM_BITS(bits));
293                 else mhexdump(hash, TF_FROM_BITS(bits), TF_FROM_BITS(bits), 0);
294                 if (do_outfmt != TFC_OUTFMT_RAW) {
295                         if (quiet == NO || xx > 1) tfc_say("\t%s", fargv[x] ? fargv[x] : "-");
296                         else tfc_say("\n");
297                 }
298         }
299
300         memset(hash, 0, SKEIN_DIGEST_SIZE);
301         xexit(exitcode);
302 }