5 * Secret sharing over %$\gf{2^8}$%
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/alloc.h>
38 #include <mLib/bits.h>
42 #include "gfshare-tab.h"
45 /*----- Static variables --------------------------------------------------*/
47 static const octet gflog[] = GFSHARE_LOG, gfexp[] = GFSHARE_EXP;
49 /*----- Main code ---------------------------------------------------------*/
51 /* --- @gfshare_create@ --- *
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
59 * Use: Initializes a sharing context.
62 void gfshare_create(gfshare *s, unsigned t, size_t sz)
70 /* --- @gfshare_destroy@ --- *
72 * Arguments: @gfshare *s@ = pointer to share context to destroy
76 * Use: Disposes of a sharing context. The allocations for the
77 * individual shares and the vector @v@ are freed; the secret is
81 void gfshare_destroy(gfshare *s)
87 /* --- @gfshare_mkshares@ --- *
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
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
103 void gfshare_mkshares(gfshare *s, grand *r, const void *buf)
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);
110 /* --- @gfshare_get@ --- *
112 * Arguments: @gfshare *s@ = pointer to share conext
113 * @unsigned x@ = share index to fetch
114 * @void *buf@ = pointer to output buffer
118 * Use: Extracts a share from the system. You may extract up to 255
119 * shares from the system. Shares are indexed from 0.
122 void gfshare_get(gfshare *s, unsigned x, void *buf)
125 const octet *p = s->v;
126 unsigned ilog = gflog[x + 1];
128 /* --- Evaluate the polynomial at %$x = i + 1$% --- */
130 memcpy(buf, p, s->sz);
133 for (i = 1; i < s->t; i++) {
136 for (k = 0; k < s->sz; k++) {
139 qq = gfexp[ilog + gflog[qq]];
145 /* --- @gfshare_addedp@ --- *
147 * Arguments: @gfshare *s@ = pointer to sharing context
148 * @unsigned x@ = which share number to check
150 * Returns: Nonzero if share @x@ has been added already, zero if it
154 int gfshare_addedp(gfshare *s, unsigned x)
158 for (i = 0; i < s->i; i++) {
159 if (GFSHARE_INDEX(s, i) == x + 1)
165 /* --- @gfshare_add@ --- *
167 * Arguments: @gfshare *s@ = pointer to sharing context
168 * @unsigned x@ = which share number this is
169 * @const void *y@ = the share value
171 * Returns: Number of shares required before recovery may be performed.
173 * Use: Adds a share to the context. The context must have been
174 * initialized with the correct threshold @t@.
177 unsigned gfshare_add(gfshare *s, unsigned x, const void *y)
181 assert(((void)"Share context is full", s->i < s->t));
182 assert(((void)"Share already present", !gfshare_addedp(s, x)));
184 /* --- If no vector has been allocated, create one --- */
187 s->v = XS_ALLOC(s->t * (s->sz + 1));
191 /* --- Store the share in the vector --- */
193 p = &GFSHARE_INDEX(s, s->i);
200 return (s->t - s->i);
203 /* --- @gfshare_combine@ --- *
205 * Arguments: @gfshare *s@ = pointer to share context
206 * @void *buf@ = pointer to output buffer for the secret
210 * Use: Reconstructs a secret, given enough shares.
213 void gfshare_combine(gfshare *s, void *buf)
218 /* --- Sanity checking --- */
220 assert(((void)"Not enough shares yet", s->i == s->t));
222 /* --- Grind through the shares --- */
224 memset(buf, 0, s->sz);
226 for (i = 0; i < s->t; i++) {
228 octet *q = &GFSHARE_INDEX(s, i);
229 unsigned c = 0, ci = 0;
231 /* --- Compute the magic coefficient --- */
234 for (j = 0; j < s->t; j++) {
237 xj = GFSHARE_INDEX(s, j);
241 ci += gflog[xi ^ xj];
249 /* --- Work out another layer of the secret --- */
252 for (j = 0; j < s->sz; j++) {
254 *p ^= gfexp[c + gflog[*q]];
260 /*----- Test rig ----------------------------------------------------------*/
266 static int verify(grand *r)
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;
272 octet *v = xmalloc(t * len);
273 unsigned *p = xmalloc(n * sizeof(unsigned));
274 octet *sec = xmalloc(len), *sbuf = xmalloc(len);
280 for (i = 0; i < n; i++)
282 for (i = 0; i < t; i++) {
283 unsigned long j = r->ops->range(r, n - i);
289 r->ops->fill(r, sec, len);
291 gfshare_create(&s, t, len);
293 gfshare_mkshares(&s, r, sec);
294 for (i = 0; i < t; i++)
295 gfshare_get(&s, p[i], v + (i * len));
298 gfshare_create(&s, t, len);
299 for (i = 0; i < t; i++)
300 gfshare_add(&s, p[i], v + (i * len));
301 gfshare_combine(&s, sbuf);
304 if (memcmp(sec, sbuf, len) != 0) {
306 fprintf(stderr, "\nbad recombination of shares\n");
320 grand *r = fibrand_create(0);
324 fputs("gfshare: ", stdout);
325 for (i = 0; i < 40; i++) {
333 fputs(" ok\n", stdout);
335 fputs(" failed\n", stdout);
336 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
341 /*----- That's all, folks -------------------------------------------------*/