Interface change: allow shares to be extracted from a context on demand,
[u/mdw/catacomb] / gfshare.c
1 /* -*-c-*-
2 *
3 * $Id: gfshare.c,v 1.4 2000/06/24 18:29:05 mdw Exp $
4 *
5 * Secret sharing over %$\gf(2^8)$%
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: gfshare.c,v $
33 * Revision 1.4 2000/06/24 18:29:05 mdw
34 * Interface change: allow shares to be extracted from a context on demand,
35 * rather than building them all up-front.
36 *
37 * Revision 1.3 2000/06/22 18:04:13 mdw
38 * Improve secret reconstruction -- compute coefficients as needed rather
39 * than making a big array of them.
40 *
41 * Revision 1.2 2000/06/18 23:12:15 mdw
42 * Change typesetting of Galois Field names.
43 *
44 * Revision 1.1 2000/06/17 10:56:30 mdw
45 * Fast but nonstandard secret sharing system.
46 *
47 */
48
49 /*----- Header files ------------------------------------------------------*/
50
51 #include <assert.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <string.h>
55
56 #include <mLib/alloc.h>
57 #include <mLib/bits.h>
58
59 #include "arena.h"
60 #include "gfshare.h"
61 #include "gfshare-tab.h"
62 #include "grand.h"
63
64 /*----- Static variables --------------------------------------------------*/
65
66 static octet gflog[] = GFSHARE_LOG, gfexp[] = GFSHARE_EXP;
67
68 /*----- Main code ---------------------------------------------------------*/
69
70 /* --- @gfshare_create@ --- *
71 *
72 * Arguments: @gfshare *s@ = pointer to share context to initialize
73 * @unsigned t@ = threshold for the system
74 * @size_t sz@ = size of the secret
75 *
76 * Returns: ---
77 *
78 * Use: Initializes a sharing context.
79 */
80
81 void gfshare_create(gfshare *s, unsigned t, size_t sz)
82 {
83 s->t = t;
84 s->i = 0;
85 s->sz = sz;
86 s->s = 0;
87 s->v = 0;
88 }
89
90 /* --- @gfshare_destroy@ --- *
91 *
92 * Arguments: @gfshare *s@ = pointer to share context to destroy
93 *
94 * Returns: ---
95 *
96 * Use: Disposes of a sharing context. The allocations for the
97 * individual shares and the vector @v@ are freed; the secret is
98 * left alone.
99 */
100
101 void gfshare_destroy(gfshare *s)
102 {
103 if (s->v)
104 XS_FREE(s->v);
105 }
106
107 /* --- @gfshare_mkshares@ --- *
108 *
109 * Arguments: @gfshare *s@ = pointer to share context to fill in
110 * @grand *r@ = pointer to random number source
111 *
112 * Returns: ---
113 *
114 * Use: Initializes a sharing context to be able to create shares.
115 * The context structure is expected to be mostly filled in. In
116 * particular, @t@ and @s@ must be initialized. If @v@ is zero,
117 * a vector of appropriate size is allocated. You should use
118 * the macro @GFSHARE_INIT@ or @gfshare_create@ to construct
119 * sharing contexts.
120 */
121
122 void gfshare_mkshares(gfshare *s, grand *r)
123 {
124 s->v = XS_ALLOC(s->sz * s->t);
125 r->ops->fill(r, s->v, s->sz * (s->t - 1));
126 memcpy(s->v + s->sz * (s->t - 1), s->s, s->sz);
127 }
128
129 /* --- @gfshare_get@ --- *
130 *
131 * Arguments: @gfshare *s@ = pointer to share conext
132 * @unsigned x@ = share index to fetch
133 * @void *buf@ = pointer to output buffer
134 *
135 * Returns: The share, as requested.
136 *
137 * Use: Extracts a share from the system. You may extract up to 255
138 * shares from the system. Shares are indexed from 0.
139 */
140
141 void gfshare_get(gfshare *s, unsigned x, void *buf)
142 {
143 unsigned i;
144 const octet *p = s->v;
145 unsigned ilog = gflog[x + 1];
146
147 /* --- Evaluate the polynomial at %$x = i + 1$% --- */
148
149 memcpy(buf, p, s->sz);
150 p += s->sz;
151
152 for (i = 1; i < s->t; i++) {
153 octet *q = buf;
154 unsigned k;
155 for (k = 0; k < s->sz; k++) {
156 unsigned qq = *q;
157 if (qq)
158 qq = gfexp[ilog + gflog[qq]];
159 *q++ = qq ^ *p++;
160 }
161 }
162 }
163
164 /* --- @gfshare_add@ --- *
165 *
166 * Arguments: @gfshare *s@ = pointer to sharing context
167 * @unsigned x@ = which share number this is
168 * @const void *y@ = the share value
169 *
170 * Returns: Number of shares required before recovery may be performed.
171 *
172 * Use: Adds a share to the context. The context must have been
173 * initialized with the correct threshold @t@.
174 */
175
176 unsigned gfshare_add(gfshare *s, unsigned x, const void *y)
177 {
178 octet *p;
179
180 /* --- If no vector has been allocated, create one --- */
181
182 if (!s->v) {
183 s->v = XS_ALLOC(s->t * (s->sz + 1));
184 s->i = 0;
185 }
186
187 assert(((void)"Share context is full", s->i < s->t));
188
189 /* --- Store the share in the vector --- */
190
191 p = s->v + s->i * (s->sz + 1);
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 = s->v + i * (s->sz + 1);
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 = s->v[j * (s->sz + 1)];
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 s.s = sec;
291
292 gfshare_mkshares(&s, r);
293 for (i = 0; i < t; i++)
294 gfshare_get(&s, p[i], v + (i * len));
295 gfshare_destroy(&s);
296
297 gfshare_create(&s, t, len);
298 for (i = 0; i < t; i++)
299 gfshare_add(&s, p[i], v + (i * len));
300 gfshare_combine(&s, sbuf);
301 gfshare_destroy(&s);
302
303 if (memcmp(sec, sbuf, len) != 0) {
304 ok = 0;
305 fprintf(stderr, "\nbad recombination of shares\n");
306 };
307
308 xfree(sec);
309 xfree(sbuf);
310
311 xfree(v);
312 xfree(p);
313
314 return (ok);
315 }
316
317 int main(void)
318 {
319 grand *r = fibrand_create(0);
320 unsigned i;
321 int ok = 1;
322
323 fputs("gfshare: ", stdout);
324 for (i = 0; i < 40; i++) {
325 if (!verify(r))
326 ok = 0;
327 fputc('.', stdout);
328 fflush(stdout);
329 }
330
331 if (ok)
332 fputs(" ok\n", stdout);
333 else
334 fputs(" failed\n", stdout);
335 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
336 }
337
338 #endif
339
340 /*----- That's all, folks -------------------------------------------------*/