cleanup: Big pile of whitespace fixes, all at once.
[u/mdw/catacomb] / gfshare.c
CommitLineData
6c1035f5 1/* -*-c-*-
2 *
aec42286 3 * $Id$
6c1035f5 4 *
eaa515d8 5 * Secret sharing over %$\gf{2^8}$%
6c1035f5 6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
6c1035f5 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.
45c0fd36 18 *
6c1035f5 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.
45c0fd36 23 *
6c1035f5 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
6c1035f5 30/*----- Header files ------------------------------------------------------*/
31
32#include <assert.h>
33#include <stdarg.h>
34#include <stdio.h>
cb06abce 35#include <string.h>
6c1035f5 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
4e66da02 47static const octet gflog[] = GFSHARE_LOG, gfexp[] = GFSHARE_EXP;
6c1035f5 48
49/*----- Main code ---------------------------------------------------------*/
50
51/* --- @gfshare_create@ --- *
52 *
53 * Arguments: @gfshare *s@ = pointer to share context to initialize
3d64a35c 54 * @unsigned t@ = threshold for the system
6c1035f5 55 * @size_t sz@ = size of the secret
56 *
57 * Returns: ---
58 *
59 * Use: Initializes a sharing context.
60 */
61
3d64a35c 62void gfshare_create(gfshare *s, unsigned t, size_t sz)
6c1035f5 63{
64 s->t = t;
6c1035f5 65 s->i = 0;
66 s->sz = sz;
6c1035f5 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
81void gfshare_destroy(gfshare *s)
82{
3d64a35c 83 if (s->v)
84 XS_FREE(s->v);
6c1035f5 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
5d4fee2a 91 * @const void *buf@ = pointer to the secret to share
6c1035f5 92 *
93 * Returns: ---
94 *
3d64a35c 95 * Use: Initializes a sharing context to be able to create shares.
6c1035f5 96 * The context structure is expected to be mostly filled in. In
5d4fee2a 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.
6c1035f5 101 */
102
5d4fee2a 103void gfshare_mkshares(gfshare *s, grand *r, const void *buf)
6c1035f5 104{
3d64a35c 105 s->v = XS_ALLOC(s->sz * s->t);
106 r->ops->fill(r, s->v, s->sz * (s->t - 1));
5d4fee2a 107 memcpy(s->v + s->sz * (s->t - 1), buf, s->sz);
3d64a35c 108}
6c1035f5 109
3d64a35c 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 *
ea303d6c 116 * Returns: ---
3d64a35c 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 */
6c1035f5 121
3d64a35c 122void 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++;
6c1035f5 141 }
6c1035f5 142 }
6c1035f5 143}
144
aec42286 145/* --- @gfshare_addedp@ --- *
146 *
147 * Arguments: @gfshare *s@ = pointer to sharing context
148 * @unsigned x@ = which share number to check
149 *
150 * Returns: Nonzero if share @x@ has been added already, zero if it
151 * hasn't.
152 */
153
154int gfshare_addedp(gfshare *s, unsigned x)
155{
156 unsigned i;
157
158 for (i = 0; i < s->i; i++) {
159 if (GFSHARE_INDEX(s, i) == x + 1)
160 return (1);
161 }
162 return (0);
163}
164
6c1035f5 165/* --- @gfshare_add@ --- *
166 *
167 * Arguments: @gfshare *s@ = pointer to sharing context
168 * @unsigned x@ = which share number this is
3d64a35c 169 * @const void *y@ = the share value
6c1035f5 170 *
171 * Returns: Number of shares required before recovery may be performed.
172 *
173 * Use: Adds a share to the context. The context must have been
174 * initialized with the correct threshold @t@.
175 */
176
3d64a35c 177unsigned gfshare_add(gfshare *s, unsigned x, const void *y)
6c1035f5 178{
3d64a35c 179 octet *p;
180
aec42286 181 assert(((void)"Share context is full", s->i < s->t));
182 assert(((void)"Share already present", !gfshare_addedp(s, x)));
183
6c1035f5 184 /* --- If no vector has been allocated, create one --- */
185
186 if (!s->v) {
3d64a35c 187 s->v = XS_ALLOC(s->t * (s->sz + 1));
6c1035f5 188 s->i = 0;
189 }
190
6c1035f5 191 /* --- Store the share in the vector --- */
192
aec42286 193 p = &GFSHARE_INDEX(s, s->i);
3d64a35c 194 *p++ = x + 1;
195 memcpy(p, y, s->sz);
6c1035f5 196 s->i++;
197
198 /* --- Done --- */
199
200 return (s->t - s->i);
201}
202
203/* --- @gfshare_combine@ --- *
204 *
205 * Arguments: @gfshare *s@ = pointer to share context
3d64a35c 206 * @void *buf@ = pointer to output buffer for the secret
6c1035f5 207 *
208 * Returns: ---
209 *
210 * Use: Reconstructs a secret, given enough shares.
211 */
212
3d64a35c 213void gfshare_combine(gfshare *s, void *buf)
6c1035f5 214{
215 unsigned i, j;
3d64a35c 216 unsigned xi, xj;
6c1035f5 217
218 /* --- Sanity checking --- */
219
220 assert(((void)"Not enough shares yet", s->i == s->t));
221
cb06abce 222 /* --- Grind through the shares --- */
6c1035f5 223
cb06abce 224 memset(buf, 0, s->sz);
6c1035f5 225
226 for (i = 0; i < s->t; i++) {
3d64a35c 227 octet *p = buf;
aec42286 228 octet *q = &GFSHARE_INDEX(s, i);
6c1035f5 229 unsigned c = 0, ci = 0;
cb06abce 230
231 /* --- Compute the magic coefficient --- */
232
3d64a35c 233 xi = *q++;
6c1035f5 234 for (j = 0; j < s->t; j++) {
235 if (i == j)
236 continue;
aec42286 237 xj = GFSHARE_INDEX(s, j);
3d64a35c 238 c += gflog[xj];
6c1035f5 239 if (c >= 0xff)
240 c -= 0xff;
3d64a35c 241 ci += gflog[xi ^ xj];
6c1035f5 242 if (ci >= 0xff)
243 ci -= 0xff;
244 }
245 if (ci > c)
246 c += 0xff;
247 c -= ci;
6c1035f5 248
cb06abce 249 /* --- Work out another layer of the secret --- */
6c1035f5 250
3d64a35c 251 p = buf;
cb06abce 252 for (j = 0; j < s->sz; j++) {
3d64a35c 253 if (*q)
254 *p ^= gfexp[c + gflog[*q]];
255 p++, q++;
6c1035f5 256 }
6c1035f5 257 }
6c1035f5 258}
259
260/*----- Test rig ----------------------------------------------------------*/
261
262#ifdef TEST_RIG
263
264#include "fibrand.h"
265
266static int verify(grand *r)
267{
268 unsigned n = r->ops->range(r, 16) + 8;
269 unsigned t = r->ops->range(r, n - 1) + 1;
270 unsigned len = r->ops->range(r, 32) + 1;
271
3d64a35c 272 octet *v = xmalloc(t * len);
6c1035f5 273 unsigned *p = xmalloc(n * sizeof(unsigned));
274 octet *sec = xmalloc(len), *sbuf = xmalloc(len);
275 gfshare s;
276 unsigned i;
277
278 int ok = 1;
279
280 for (i = 0; i < n; i++)
281 p[i] = i;
282 for (i = 0; i < t; i++) {
283 unsigned long j = r->ops->range(r, n - i);
284 unsigned x = p[i];
285 p[i] = p[i + j];
286 p[i + j] = x;
287 }
288
289 r->ops->fill(r, sec, len);
290
3d64a35c 291 gfshare_create(&s, t, len);
6c1035f5 292
5d4fee2a 293 gfshare_mkshares(&s, r, sec);
3d64a35c 294 for (i = 0; i < t; i++)
295 gfshare_get(&s, p[i], v + (i * len));
6c1035f5 296 gfshare_destroy(&s);
297
3d64a35c 298 gfshare_create(&s, t, len);
299 for (i = 0; i < t; i++)
300 gfshare_add(&s, p[i], v + (i * len));
6c1035f5 301 gfshare_combine(&s, sbuf);
302 gfshare_destroy(&s);
303
304 if (memcmp(sec, sbuf, len) != 0) {
305 ok = 0;
306 fprintf(stderr, "\nbad recombination of shares\n");
307 };
308
309 xfree(sec);
310 xfree(sbuf);
311
6c1035f5 312 xfree(v);
313 xfree(p);
314
315 return (ok);
316}
317
318int main(void)
319{
320 grand *r = fibrand_create(0);
321 unsigned i;
322 int ok = 1;
323
324 fputs("gfshare: ", stdout);
325 for (i = 0; i < 40; i++) {
326 if (!verify(r))
327 ok = 0;
328 fputc('.', stdout);
329 fflush(stdout);
330 }
331
332 if (ok)
333 fputs(" ok\n", stdout);
334 else
335 fputs(" failed\n", stdout);
336 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
337}
338
339#endif
340
341/*----- That's all, folks -------------------------------------------------*/