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