chiark / gitweb /
Change secret sharing interface: present the secret at share
[catacomb] / gfshare.c
1 /* -*-c-*-
2  *
3  * $Id: gfshare.c,v 1.6 2000/12/06 20:30:10 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.6  2000/12/06 20:30:10  mdw
34  * Change secret sharing interface: present the secret at share
35  * construction time.
36  *
37  * Revision 1.5  2000/06/24 19:11:47  mdw
38  * Fix daft error in the comment for @gfshare_get@.
39  *
40  * Revision 1.4  2000/06/24 18:29:05  mdw
41  * Interface change: allow shares to be extracted from a context on demand,
42  * rather than building them all up-front.
43  *
44  * Revision 1.3  2000/06/22 18:04:13  mdw
45  * Improve secret reconstruction -- compute coefficients as needed rather
46  * than making a big array of them.
47  *
48  * Revision 1.2  2000/06/18 23:12:15  mdw
49  * Change typesetting of Galois Field names.
50  *
51  * Revision 1.1  2000/06/17 10:56:30  mdw
52  * Fast but nonstandard secret sharing system.
53  *
54  */
55
56 /*----- Header files ------------------------------------------------------*/
57
58 #include <assert.h>
59 #include <stdarg.h>
60 #include <stdio.h>
61 #include <string.h>
62
63 #include <mLib/alloc.h>
64 #include <mLib/bits.h>
65
66 #include "arena.h"
67 #include "gfshare.h"
68 #include "gfshare-tab.h"
69 #include "grand.h"
70
71 /*----- Static variables --------------------------------------------------*/
72
73 static octet gflog[] = GFSHARE_LOG, gfexp[] = GFSHARE_EXP;
74
75 /*----- Main code ---------------------------------------------------------*/
76
77 /* --- @gfshare_create@ --- *
78  *
79  * Arguments:   @gfshare *s@ = pointer to share context to initialize
80  *              @unsigned t@ = threshold for the system
81  *              @size_t sz@ = size of the secret
82  *
83  * Returns:     ---
84  *
85  * Use:         Initializes a sharing context.
86  */
87
88 void gfshare_create(gfshare *s, unsigned t, size_t sz)
89 {
90   s->t = t;
91   s->i = 0;
92   s->sz = sz;
93   s->v = 0;
94 }
95
96 /* --- @gfshare_destroy@ --- *
97  *
98  * Arguments:   @gfshare *s@ = pointer to share context to destroy
99  *
100  * Returns:     ---
101  *
102  * Use:         Disposes of a sharing context.  The allocations for the
103  *              individual shares and the vector @v@ are freed; the secret is
104  *              left alone.
105  */
106
107 void gfshare_destroy(gfshare *s)
108 {
109   if (s->v)
110     XS_FREE(s->v);
111 }
112
113 /* --- @gfshare_mkshares@ --- *
114  *
115  * Arguments:   @gfshare *s@ = pointer to share context to fill in
116  *              @grand *r@ = pointer to random number source
117  *              @const void *buf@ = pointer to the secret to share
118  *
119  * Returns:     ---
120  *
121  * Use:         Initializes a sharing context to be able to create shares.
122  *              The context structure is expected to be mostly filled in.  In
123  *              particular, @t@ must be initialized.  If @v@ is zero, a
124  *              vector of appropriate size is allocated.  You should use the
125  *              macro @GFSHARE_INIT@ or @gfshare_create@ to construct sharing
126  *              contexts.
127  */
128
129 void gfshare_mkshares(gfshare *s, grand *r, const void *buf)
130 {
131   s->v = XS_ALLOC(s->sz * s->t);
132   r->ops->fill(r, s->v, s->sz * (s->t - 1));
133   memcpy(s->v + s->sz * (s->t - 1), buf, s->sz);
134 }
135
136 /* --- @gfshare_get@ --- *
137  *
138  * Arguments:   @gfshare *s@ = pointer to share conext
139  *              @unsigned x@ = share index to fetch
140  *              @void *buf@ = pointer to output buffer
141  *
142  * Returns:     ---
143  *
144  * Use:         Extracts a share from the system.  You may extract up to 255
145  *              shares from the system.  Shares are indexed from 0.
146  */
147
148 void gfshare_get(gfshare *s, unsigned x, void *buf)
149 {
150   unsigned i;
151   const octet *p = s->v;
152   unsigned ilog = gflog[x + 1];
153
154   /* --- Evaluate the polynomial at %$x = i + 1$% --- */
155
156   memcpy(buf, p, s->sz);
157   p += s->sz;
158
159   for (i = 1; i < s->t; i++) {
160     octet *q = buf;
161     unsigned k;
162     for (k = 0; k < s->sz; k++) {
163       unsigned qq = *q;
164       if (qq)
165         qq = gfexp[ilog + gflog[qq]];
166       *q++ = qq ^ *p++;
167     }
168   }
169 }
170
171 /* --- @gfshare_add@ --- *
172  *
173  * Arguments:   @gfshare *s@ = pointer to sharing context
174  *              @unsigned x@ = which share number this is
175  *              @const void *y@ = the share value
176  *
177  * Returns:     Number of shares required before recovery may be performed.
178  *
179  * Use:         Adds a share to the context.  The context must have been
180  *              initialized with the correct threshold @t@.
181  */
182
183 unsigned gfshare_add(gfshare *s, unsigned x, const void *y)
184 {
185   octet *p;
186
187   /* --- If no vector has been allocated, create one --- */
188
189   if (!s->v) {
190     s->v = XS_ALLOC(s->t * (s->sz + 1));
191     s->i = 0;
192   }
193
194   assert(((void)"Share context is full", s->i < s->t));
195
196   /* --- Store the share in the vector --- */
197
198   p = s->v + s->i * (s->sz + 1);
199   *p++ = x + 1;
200   memcpy(p, y, s->sz);
201   s->i++;
202
203   /* --- Done --- */
204
205   return (s->t - s->i);
206 }
207
208 /* --- @gfshare_combine@ --- *
209  *
210  * Arguments:   @gfshare *s@ = pointer to share context
211  *              @void *buf@ = pointer to output buffer for the secret
212  *
213  * Returns:     ---
214  *
215  * Use:         Reconstructs a secret, given enough shares.
216  */
217
218 void gfshare_combine(gfshare *s, void *buf)
219 {
220   unsigned i, j;
221   unsigned xi, xj;
222
223   /* --- Sanity checking --- */
224
225   assert(((void)"Not enough shares yet", s->i == s->t));
226
227   /* --- Grind through the shares --- */
228
229   memset(buf, 0, s->sz);
230
231   for (i = 0; i < s->t; i++) {
232     octet *p = buf;
233     octet *q = s->v + i * (s->sz + 1);
234     unsigned c = 0, ci = 0;
235
236     /* --- Compute the magic coefficient --- */
237
238     xi = *q++;
239     for (j = 0; j < s->t; j++) {
240       if (i == j)
241         continue;
242       xj = s->v[j * (s->sz + 1)];
243       c += gflog[xj];
244       if (c >= 0xff)
245         c -= 0xff;
246       ci += gflog[xi ^ xj];
247       if (ci >= 0xff)
248         ci -= 0xff;
249     }
250     if (ci > c)
251       c += 0xff;
252     c -= ci;
253
254     /* --- Work out another layer of the secret --- */
255
256     p = buf;
257     for (j = 0; j < s->sz; j++) {
258       if (*q)
259         *p ^= gfexp[c + gflog[*q]];
260       p++, q++;
261     }
262   }
263 }
264
265 /*----- Test rig ----------------------------------------------------------*/
266
267 #ifdef TEST_RIG
268
269 #include "fibrand.h"
270
271 static int verify(grand *r)
272 {
273   unsigned n = r->ops->range(r, 16) + 8;
274   unsigned t = r->ops->range(r, n - 1) + 1;
275   unsigned len = r->ops->range(r, 32) + 1;
276
277   octet *v = xmalloc(t * len);
278   unsigned *p = xmalloc(n * sizeof(unsigned));
279   octet *sec = xmalloc(len), *sbuf = xmalloc(len);
280   gfshare s;
281   unsigned i;
282
283   int ok = 1;
284
285   for (i = 0; i < n; i++)
286     p[i] = i;
287   for (i = 0; i < t; i++) {
288     unsigned long j = r->ops->range(r, n - i);
289     unsigned x = p[i];
290     p[i] = p[i + j];
291     p[i + j] = x;
292   }
293
294   r->ops->fill(r, sec, len);
295
296   gfshare_create(&s, t, len);
297
298   gfshare_mkshares(&s, r, sec);
299   for (i = 0; i < t; i++)
300     gfshare_get(&s, p[i], v + (i * len));
301   gfshare_destroy(&s);
302
303   gfshare_create(&s, t, len);
304   for (i = 0; i < t; i++)
305     gfshare_add(&s, p[i], v + (i * len));
306   gfshare_combine(&s, sbuf);
307   gfshare_destroy(&s);
308
309   if (memcmp(sec, sbuf, len) != 0) {
310     ok = 0;
311     fprintf(stderr, "\nbad recombination of shares\n");
312   };
313
314   xfree(sec);
315   xfree(sbuf);
316
317   xfree(v);
318   xfree(p);
319
320   return (ok);
321 }
322
323 int main(void)
324 {
325   grand *r = fibrand_create(0);
326   unsigned i;
327   int ok = 1;
328
329   fputs("gfshare: ", stdout);
330   for (i = 0; i < 40; i++) {
331     if (!verify(r))
332       ok = 0;
333     fputc('.', stdout);
334     fflush(stdout);
335   }
336
337   if (ok)
338     fputs(" ok\n", stdout);
339   else
340     fputs(" failed\n", stdout);
341   return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
342 }
343
344 #endif
345
346 /*----- That's all, folks -------------------------------------------------*/