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