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