6c1035f5 |
1 | /* -*-c-*- |
2 | * |
eaa515d8 |
3 | * Secret sharing over %$\gf{2^8}$% |
6c1035f5 |
4 | * |
5 | * (c) 2000 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
6c1035f5 |
9 | * |
10 | * This file is part of Catacomb. |
11 | * |
12 | * Catacomb is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU Library General Public License as |
14 | * published by the Free Software Foundation; either version 2 of the |
15 | * License, or (at your option) any later version. |
45c0fd36 |
16 | * |
6c1035f5 |
17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU Library General Public License for more details. |
45c0fd36 |
21 | * |
6c1035f5 |
22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
6c1035f5 |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | #include <assert.h> |
31 | #include <stdarg.h> |
32 | #include <stdio.h> |
cb06abce |
33 | #include <string.h> |
6c1035f5 |
34 | |
35 | #include <mLib/alloc.h> |
36 | #include <mLib/bits.h> |
37 | |
38 | #include "arena.h" |
39 | #include "gfshare.h" |
40 | #include "gfshare-tab.h" |
41 | #include "grand.h" |
42 | |
43 | /*----- Static variables --------------------------------------------------*/ |
44 | |
4e66da02 |
45 | static const octet gflog[] = GFSHARE_LOG, gfexp[] = GFSHARE_EXP; |
6c1035f5 |
46 | |
47 | /*----- Main code ---------------------------------------------------------*/ |
48 | |
49 | /* --- @gfshare_create@ --- * |
50 | * |
51 | * Arguments: @gfshare *s@ = pointer to share context to initialize |
3d64a35c |
52 | * @unsigned t@ = threshold for the system |
6c1035f5 |
53 | * @size_t sz@ = size of the secret |
54 | * |
55 | * Returns: --- |
56 | * |
57 | * Use: Initializes a sharing context. |
58 | */ |
59 | |
3d64a35c |
60 | void gfshare_create(gfshare *s, unsigned t, size_t sz) |
6c1035f5 |
61 | { |
62 | s->t = t; |
6c1035f5 |
63 | s->i = 0; |
64 | s->sz = sz; |
6c1035f5 |
65 | s->v = 0; |
66 | } |
67 | |
68 | /* --- @gfshare_destroy@ --- * |
69 | * |
70 | * Arguments: @gfshare *s@ = pointer to share context to destroy |
71 | * |
72 | * Returns: --- |
73 | * |
74 | * Use: Disposes of a sharing context. The allocations for the |
75 | * individual shares and the vector @v@ are freed; the secret is |
76 | * left alone. |
77 | */ |
78 | |
79 | void gfshare_destroy(gfshare *s) |
80 | { |
3d64a35c |
81 | if (s->v) |
82 | XS_FREE(s->v); |
6c1035f5 |
83 | } |
84 | |
85 | /* --- @gfshare_mkshares@ --- * |
86 | * |
87 | * Arguments: @gfshare *s@ = pointer to share context to fill in |
88 | * @grand *r@ = pointer to random number source |
5d4fee2a |
89 | * @const void *buf@ = pointer to the secret to share |
6c1035f5 |
90 | * |
91 | * Returns: --- |
92 | * |
3d64a35c |
93 | * Use: Initializes a sharing context to be able to create shares. |
6c1035f5 |
94 | * The context structure is expected to be mostly filled in. In |
5d4fee2a |
95 | * particular, @t@ must be initialized. If @v@ is zero, a |
96 | * vector of appropriate size is allocated. You should use the |
97 | * macro @GFSHARE_INIT@ or @gfshare_create@ to construct sharing |
98 | * contexts. |
6c1035f5 |
99 | */ |
100 | |
5d4fee2a |
101 | void gfshare_mkshares(gfshare *s, grand *r, const void *buf) |
6c1035f5 |
102 | { |
3d64a35c |
103 | s->v = XS_ALLOC(s->sz * s->t); |
104 | r->ops->fill(r, s->v, s->sz * (s->t - 1)); |
5d4fee2a |
105 | memcpy(s->v + s->sz * (s->t - 1), buf, s->sz); |
3d64a35c |
106 | } |
6c1035f5 |
107 | |
3d64a35c |
108 | /* --- @gfshare_get@ --- * |
109 | * |
110 | * Arguments: @gfshare *s@ = pointer to share conext |
111 | * @unsigned x@ = share index to fetch |
112 | * @void *buf@ = pointer to output buffer |
113 | * |
ea303d6c |
114 | * Returns: --- |
3d64a35c |
115 | * |
116 | * Use: Extracts a share from the system. You may extract up to 255 |
117 | * shares from the system. Shares are indexed from 0. |
118 | */ |
6c1035f5 |
119 | |
3d64a35c |
120 | void gfshare_get(gfshare *s, unsigned x, void *buf) |
121 | { |
122 | unsigned i; |
123 | const octet *p = s->v; |
124 | unsigned ilog = gflog[x + 1]; |
125 | |
126 | /* --- Evaluate the polynomial at %$x = i + 1$% --- */ |
127 | |
128 | memcpy(buf, p, s->sz); |
129 | p += s->sz; |
130 | |
131 | for (i = 1; i < s->t; i++) { |
132 | octet *q = buf; |
133 | unsigned k; |
134 | for (k = 0; k < s->sz; k++) { |
135 | unsigned qq = *q; |
136 | if (qq) |
137 | qq = gfexp[ilog + gflog[qq]]; |
138 | *q++ = qq ^ *p++; |
6c1035f5 |
139 | } |
6c1035f5 |
140 | } |
6c1035f5 |
141 | } |
142 | |
aec42286 |
143 | /* --- @gfshare_addedp@ --- * |
144 | * |
145 | * Arguments: @gfshare *s@ = pointer to sharing context |
146 | * @unsigned x@ = which share number to check |
147 | * |
148 | * Returns: Nonzero if share @x@ has been added already, zero if it |
149 | * hasn't. |
150 | */ |
151 | |
152 | int gfshare_addedp(gfshare *s, unsigned x) |
153 | { |
154 | unsigned i; |
155 | |
156 | for (i = 0; i < s->i; i++) { |
157 | if (GFSHARE_INDEX(s, i) == x + 1) |
158 | return (1); |
159 | } |
160 | return (0); |
161 | } |
162 | |
6c1035f5 |
163 | /* --- @gfshare_add@ --- * |
164 | * |
165 | * Arguments: @gfshare *s@ = pointer to sharing context |
166 | * @unsigned x@ = which share number this is |
3d64a35c |
167 | * @const void *y@ = the share value |
6c1035f5 |
168 | * |
169 | * Returns: Number of shares required before recovery may be performed. |
170 | * |
171 | * Use: Adds a share to the context. The context must have been |
172 | * initialized with the correct threshold @t@. |
173 | */ |
174 | |
3d64a35c |
175 | unsigned gfshare_add(gfshare *s, unsigned x, const void *y) |
6c1035f5 |
176 | { |
3d64a35c |
177 | octet *p; |
178 | |
aec42286 |
179 | assert(((void)"Share context is full", s->i < s->t)); |
180 | assert(((void)"Share already present", !gfshare_addedp(s, x))); |
181 | |
6c1035f5 |
182 | /* --- If no vector has been allocated, create one --- */ |
183 | |
184 | if (!s->v) { |
3d64a35c |
185 | s->v = XS_ALLOC(s->t * (s->sz + 1)); |
6c1035f5 |
186 | s->i = 0; |
187 | } |
188 | |
6c1035f5 |
189 | /* --- Store the share in the vector --- */ |
190 | |
aec42286 |
191 | p = &GFSHARE_INDEX(s, s->i); |
3d64a35c |
192 | *p++ = x + 1; |
193 | memcpy(p, y, s->sz); |
6c1035f5 |
194 | s->i++; |
195 | |
196 | /* --- Done --- */ |
197 | |
198 | return (s->t - s->i); |
199 | } |
200 | |
201 | /* --- @gfshare_combine@ --- * |
202 | * |
203 | * Arguments: @gfshare *s@ = pointer to share context |
3d64a35c |
204 | * @void *buf@ = pointer to output buffer for the secret |
6c1035f5 |
205 | * |
206 | * Returns: --- |
207 | * |
208 | * Use: Reconstructs a secret, given enough shares. |
209 | */ |
210 | |
3d64a35c |
211 | void gfshare_combine(gfshare *s, void *buf) |
6c1035f5 |
212 | { |
213 | unsigned i, j; |
3d64a35c |
214 | unsigned xi, xj; |
6c1035f5 |
215 | |
216 | /* --- Sanity checking --- */ |
217 | |
218 | assert(((void)"Not enough shares yet", s->i == s->t)); |
219 | |
cb06abce |
220 | /* --- Grind through the shares --- */ |
6c1035f5 |
221 | |
cb06abce |
222 | memset(buf, 0, s->sz); |
6c1035f5 |
223 | |
224 | for (i = 0; i < s->t; i++) { |
3d64a35c |
225 | octet *p = buf; |
aec42286 |
226 | octet *q = &GFSHARE_INDEX(s, i); |
6c1035f5 |
227 | unsigned c = 0, ci = 0; |
cb06abce |
228 | |
229 | /* --- Compute the magic coefficient --- */ |
230 | |
3d64a35c |
231 | xi = *q++; |
6c1035f5 |
232 | for (j = 0; j < s->t; j++) { |
233 | if (i == j) |
234 | continue; |
aec42286 |
235 | xj = GFSHARE_INDEX(s, j); |
3d64a35c |
236 | c += gflog[xj]; |
6c1035f5 |
237 | if (c >= 0xff) |
238 | c -= 0xff; |
3d64a35c |
239 | ci += gflog[xi ^ xj]; |
6c1035f5 |
240 | if (ci >= 0xff) |
241 | ci -= 0xff; |
242 | } |
243 | if (ci > c) |
244 | c += 0xff; |
245 | c -= ci; |
6c1035f5 |
246 | |
cb06abce |
247 | /* --- Work out another layer of the secret --- */ |
6c1035f5 |
248 | |
3d64a35c |
249 | p = buf; |
cb06abce |
250 | for (j = 0; j < s->sz; j++) { |
3d64a35c |
251 | if (*q) |
252 | *p ^= gfexp[c + gflog[*q]]; |
253 | p++, q++; |
6c1035f5 |
254 | } |
6c1035f5 |
255 | } |
6c1035f5 |
256 | } |
257 | |
258 | /*----- Test rig ----------------------------------------------------------*/ |
259 | |
260 | #ifdef TEST_RIG |
261 | |
262 | #include "fibrand.h" |
263 | |
264 | static int verify(grand *r) |
265 | { |
266 | unsigned n = r->ops->range(r, 16) + 8; |
267 | unsigned t = r->ops->range(r, n - 1) + 1; |
268 | unsigned len = r->ops->range(r, 32) + 1; |
269 | |
3d64a35c |
270 | octet *v = xmalloc(t * len); |
6c1035f5 |
271 | unsigned *p = xmalloc(n * sizeof(unsigned)); |
272 | octet *sec = xmalloc(len), *sbuf = xmalloc(len); |
273 | gfshare s; |
274 | unsigned i; |
275 | |
276 | int ok = 1; |
277 | |
278 | for (i = 0; i < n; i++) |
279 | p[i] = i; |
280 | for (i = 0; i < t; i++) { |
281 | unsigned long j = r->ops->range(r, n - i); |
282 | unsigned x = p[i]; |
283 | p[i] = p[i + j]; |
284 | p[i + j] = x; |
285 | } |
286 | |
287 | r->ops->fill(r, sec, len); |
288 | |
3d64a35c |
289 | gfshare_create(&s, t, len); |
6c1035f5 |
290 | |
5d4fee2a |
291 | gfshare_mkshares(&s, r, sec); |
3d64a35c |
292 | for (i = 0; i < t; i++) |
293 | gfshare_get(&s, p[i], v + (i * len)); |
6c1035f5 |
294 | gfshare_destroy(&s); |
295 | |
3d64a35c |
296 | gfshare_create(&s, t, len); |
297 | for (i = 0; i < t; i++) |
298 | gfshare_add(&s, p[i], v + (i * len)); |
6c1035f5 |
299 | gfshare_combine(&s, sbuf); |
300 | gfshare_destroy(&s); |
301 | |
302 | if (memcmp(sec, sbuf, len) != 0) { |
303 | ok = 0; |
304 | fprintf(stderr, "\nbad recombination of shares\n"); |
305 | }; |
306 | |
307 | xfree(sec); |
308 | xfree(sbuf); |
309 | |
6c1035f5 |
310 | xfree(v); |
311 | xfree(p); |
312 | |
313 | return (ok); |
314 | } |
315 | |
316 | int main(void) |
317 | { |
318 | grand *r = fibrand_create(0); |
319 | unsigned i; |
320 | int ok = 1; |
321 | |
322 | fputs("gfshare: ", stdout); |
323 | for (i = 0; i < 40; i++) { |
324 | if (!verify(r)) |
325 | ok = 0; |
326 | fputc('.', stdout); |
327 | fflush(stdout); |
328 | } |
329 | |
330 | if (ok) |
331 | fputs(" ok\n", stdout); |
332 | else |
333 | fputs(" failed\n", stdout); |
334 | return (ok ? EXIT_SUCCESS : EXIT_FAILURE); |
335 | } |
336 | |
337 | #endif |
338 | |
339 | /*----- That's all, folks -------------------------------------------------*/ |