Rearrange the file tree.
[u/mdw/catacomb] / misc / gfshare.c
1 /* -*-c-*-
2 *
3 * Secret sharing over %$\gf{2^8}$%
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <assert.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include <mLib/alloc.h>
36 #include <mLib/bits.h>
37
38 #include "arena.h"
39 #include "gfshare.h"
40 #include "gfshare-tab.h"
41 #include "grand.h"
42
43 /*----- Static variables --------------------------------------------------*/
44
45 static const octet gflog[] = GFSHARE_LOG, gfexp[] = GFSHARE_EXP;
46
47 /*----- Main code ---------------------------------------------------------*/
48
49 /* --- @gfshare_create@ --- *
50 *
51 * Arguments: @gfshare *s@ = pointer to share context to initialize
52 * @unsigned t@ = threshold for the system
53 * @size_t sz@ = size of the secret
54 *
55 * Returns: ---
56 *
57 * Use: Initializes a sharing context.
58 */
59
60 void gfshare_create(gfshare *s, unsigned t, size_t sz)
61 {
62 s->t = t;
63 s->i = 0;
64 s->sz = sz;
65 s->v = 0;
66 }
67
68 /* --- @gfshare_destroy@ --- *
69 *
70 * Arguments: @gfshare *s@ = pointer to share context to destroy
71 *
72 * Returns: ---
73 *
74 * Use: Disposes of a sharing context. The allocations for the
75 * individual shares and the vector @v@ are freed; the secret is
76 * left alone.
77 */
78
79 void gfshare_destroy(gfshare *s)
80 {
81 if (s->v)
82 XS_FREE(s->v);
83 }
84
85 /* --- @gfshare_mkshares@ --- *
86 *
87 * Arguments: @gfshare *s@ = pointer to share context to fill in
88 * @grand *r@ = pointer to random number source
89 * @const void *buf@ = pointer to the secret to share
90 *
91 * Returns: ---
92 *
93 * Use: Initializes a sharing context to be able to create shares.
94 * The context structure is expected to be mostly filled in. In
95 * particular, @t@ must be initialized. If @v@ is zero, a
96 * vector of appropriate size is allocated. You should use the
97 * macro @GFSHARE_INIT@ or @gfshare_create@ to construct sharing
98 * contexts.
99 */
100
101 void gfshare_mkshares(gfshare *s, grand *r, const void *buf)
102 {
103 s->v = XS_ALLOC(s->sz * s->t);
104 r->ops->fill(r, s->v, s->sz * (s->t - 1));
105 memcpy(s->v + s->sz * (s->t - 1), buf, s->sz);
106 }
107
108 /* --- @gfshare_get@ --- *
109 *
110 * Arguments: @gfshare *s@ = pointer to share conext
111 * @unsigned x@ = share index to fetch
112 * @void *buf@ = pointer to output buffer
113 *
114 * Returns: ---
115 *
116 * Use: Extracts a share from the system. You may extract up to 255
117 * shares from the system. Shares are indexed from 0.
118 */
119
120 void gfshare_get(gfshare *s, unsigned x, void *buf)
121 {
122 unsigned i;
123 const octet *p = s->v;
124 unsigned ilog = gflog[x + 1];
125
126 /* --- Evaluate the polynomial at %$x = i + 1$% --- */
127
128 memcpy(buf, p, s->sz);
129 p += s->sz;
130
131 for (i = 1; i < s->t; i++) {
132 octet *q = buf;
133 unsigned k;
134 for (k = 0; k < s->sz; k++) {
135 unsigned qq = *q;
136 if (qq)
137 qq = gfexp[ilog + gflog[qq]];
138 *q++ = qq ^ *p++;
139 }
140 }
141 }
142
143 /* --- @gfshare_addedp@ --- *
144 *
145 * Arguments: @gfshare *s@ = pointer to sharing context
146 * @unsigned x@ = which share number to check
147 *
148 * Returns: Nonzero if share @x@ has been added already, zero if it
149 * hasn't.
150 */
151
152 int gfshare_addedp(gfshare *s, unsigned x)
153 {
154 unsigned i;
155
156 for (i = 0; i < s->i; i++) {
157 if (GFSHARE_INDEX(s, i) == x + 1)
158 return (1);
159 }
160 return (0);
161 }
162
163 /* --- @gfshare_add@ --- *
164 *
165 * Arguments: @gfshare *s@ = pointer to sharing context
166 * @unsigned x@ = which share number this is
167 * @const void *y@ = the share value
168 *
169 * Returns: Number of shares required before recovery may be performed.
170 *
171 * Use: Adds a share to the context. The context must have been
172 * initialized with the correct threshold @t@.
173 */
174
175 unsigned gfshare_add(gfshare *s, unsigned x, const void *y)
176 {
177 octet *p;
178
179 assert(((void)"Share context is full", s->i < s->t));
180 assert(((void)"Share already present", !gfshare_addedp(s, x)));
181
182 /* --- If no vector has been allocated, create one --- */
183
184 if (!s->v) {
185 s->v = XS_ALLOC(s->t * (s->sz + 1));
186 s->i = 0;
187 }
188
189 /* --- Store the share in the vector --- */
190
191 p = &GFSHARE_INDEX(s, s->i);
192 *p++ = x + 1;
193 memcpy(p, y, s->sz);
194 s->i++;
195
196 /* --- Done --- */
197
198 return (s->t - s->i);
199 }
200
201 /* --- @gfshare_combine@ --- *
202 *
203 * Arguments: @gfshare *s@ = pointer to share context
204 * @void *buf@ = pointer to output buffer for the secret
205 *
206 * Returns: ---
207 *
208 * Use: Reconstructs a secret, given enough shares.
209 */
210
211 void gfshare_combine(gfshare *s, void *buf)
212 {
213 unsigned i, j;
214 unsigned xi, xj;
215
216 /* --- Sanity checking --- */
217
218 assert(((void)"Not enough shares yet", s->i == s->t));
219
220 /* --- Grind through the shares --- */
221
222 memset(buf, 0, s->sz);
223
224 for (i = 0; i < s->t; i++) {
225 octet *p = buf;
226 octet *q = &GFSHARE_INDEX(s, i);
227 unsigned c = 0, ci = 0;
228
229 /* --- Compute the magic coefficient --- */
230
231 xi = *q++;
232 for (j = 0; j < s->t; j++) {
233 if (i == j)
234 continue;
235 xj = GFSHARE_INDEX(s, j);
236 c += gflog[xj];
237 if (c >= 0xff)
238 c -= 0xff;
239 ci += gflog[xi ^ xj];
240 if (ci >= 0xff)
241 ci -= 0xff;
242 }
243 if (ci > c)
244 c += 0xff;
245 c -= ci;
246
247 /* --- Work out another layer of the secret --- */
248
249 p = buf;
250 for (j = 0; j < s->sz; j++) {
251 if (*q)
252 *p ^= gfexp[c + gflog[*q]];
253 p++, q++;
254 }
255 }
256 }
257
258 /*----- Test rig ----------------------------------------------------------*/
259
260 #ifdef TEST_RIG
261
262 #include "fibrand.h"
263
264 static int verify(grand *r)
265 {
266 unsigned n = r->ops->range(r, 16) + 8;
267 unsigned t = r->ops->range(r, n - 1) + 1;
268 unsigned len = r->ops->range(r, 32) + 1;
269
270 octet *v = xmalloc(t * len);
271 unsigned *p = xmalloc(n * sizeof(unsigned));
272 octet *sec = xmalloc(len), *sbuf = xmalloc(len);
273 gfshare s;
274 unsigned i;
275
276 int ok = 1;
277
278 for (i = 0; i < n; i++)
279 p[i] = i;
280 for (i = 0; i < t; i++) {
281 unsigned long j = r->ops->range(r, n - i);
282 unsigned x = p[i];
283 p[i] = p[i + j];
284 p[i + j] = x;
285 }
286
287 r->ops->fill(r, sec, len);
288
289 gfshare_create(&s, t, len);
290
291 gfshare_mkshares(&s, r, sec);
292 for (i = 0; i < t; i++)
293 gfshare_get(&s, p[i], v + (i * len));
294 gfshare_destroy(&s);
295
296 gfshare_create(&s, t, len);
297 for (i = 0; i < t; i++)
298 gfshare_add(&s, p[i], v + (i * len));
299 gfshare_combine(&s, sbuf);
300 gfshare_destroy(&s);
301
302 if (memcmp(sec, sbuf, len) != 0) {
303 ok = 0;
304 fprintf(stderr, "\nbad recombination of shares\n");
305 };
306
307 xfree(sec);
308 xfree(sbuf);
309
310 xfree(v);
311 xfree(p);
312
313 return (ok);
314 }
315
316 int main(void)
317 {
318 grand *r = fibrand_create(0);
319 unsigned i;
320 int ok = 1;
321
322 fputs("gfshare: ", stdout);
323 for (i = 0; i < 40; i++) {
324 if (!verify(r))
325 ok = 0;
326 fputc('.', stdout);
327 fflush(stdout);
328 }
329
330 if (ok)
331 fputs(" ok\n", stdout);
332 else
333 fputs(" failed\n", stdout);
334 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
335 }
336
337 #endif
338
339 /*----- That's all, folks -------------------------------------------------*/