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