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