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