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