71dfe576 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: share.c,v 1.7 2004/04/08 01:36:15 mdw Exp $ |
71dfe576 |
4 | * |
5 | * Shamir's secret sharing |
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 | |
71dfe576 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include <stdio.h> |
33 | #include <stdlib.h> |
34 | #include <string.h> |
35 | |
36 | #include "grand.h" |
37 | #include "mp.h" |
38 | #include "mpint.h" |
ad74e5b4 |
39 | #include "mpbarrett.h" |
71dfe576 |
40 | #include "mprand.h" |
41 | #include "pgen.h" |
42 | #include "rabin.h" |
43 | #include "share.h" |
44 | |
45 | /*----- Main code ---------------------------------------------------------*/ |
46 | |
47 | /* --- @share_create@ --- * |
48 | * |
49 | * Arguments: @share *s@ = pointer to share context to initialize |
3d64a35c |
50 | * @unsigned t@ = threshold for the system |
71dfe576 |
51 | * |
52 | * Returns: --- |
53 | * |
54 | * Use: Initializes a sharing context. |
55 | */ |
56 | |
3d64a35c |
57 | void share_create(share *s, unsigned t) |
71dfe576 |
58 | { |
59 | s->t = t; |
71dfe576 |
60 | s->i = 0; |
71dfe576 |
61 | s->p = 0; |
62 | s->v = 0; |
63 | } |
64 | |
65 | /* --- @share_destroy@ --- * |
66 | * |
67 | * Arguments: @share *s@ = pointer to share context to destroy |
68 | * |
69 | * Returns: --- |
70 | * |
71 | * Use: Disposes of a sharing context. All memory is freed, all |
72 | * integers are dropped. |
73 | */ |
74 | |
75 | void share_destroy(share *s) |
76 | { |
71dfe576 |
77 | unsigned i; |
78 | |
79 | /* --- Dispose of the share vector --- */ |
80 | |
81 | if (s->v) { |
eab06f16 |
82 | for (i = 0; i < s->t; i++) |
83 | mp_drop(s->v[i].y); |
71dfe576 |
84 | xfree(s->v); |
85 | } |
86 | |
87 | /* --- Other stuff --- */ |
88 | |
eab06f16 |
89 | mp_drop(s->p); |
71dfe576 |
90 | } |
91 | |
92 | /* --- @share_mkshares@ --- * |
93 | * |
94 | * Arguments: @share *s@ = pointer to share context to fill in |
95 | * @grand *r@ = pointer to random number source |
5d4fee2a |
96 | * @mp *n@ = the secret to share |
71dfe576 |
97 | * |
98 | * Returns: --- |
99 | * |
3d64a35c |
100 | * Use: Initializes a sharing context to be able to create shares. |
71dfe576 |
101 | * The context structure is expected to be mostly filled in. In |
5d4fee2a |
102 | * particular, @t@ must be initialized. If @p@ is zero, a prime |
103 | * number of appropriate size is generated automatically. If |
104 | * @v@ is zero, a vector of appropriate size is allocated. You |
105 | * should use the macro @SHARE_INIT@ or @share_create@ to |
106 | * construct sharing contexts. |
71dfe576 |
107 | */ |
108 | |
5d4fee2a |
109 | void share_mkshares(share *s, grand *r, mp *n) |
71dfe576 |
110 | { |
71dfe576 |
111 | unsigned i; |
71dfe576 |
112 | |
113 | /* --- If there's no prime, construct one --- */ |
114 | |
115 | if (!s->p) { |
116 | pgen_filterctx pf; |
117 | rabin pr; |
118 | mp *p; |
5d4fee2a |
119 | unsigned bits = (mp_octets(n) + 1) * 8; |
71dfe576 |
120 | |
121 | pf.step = 2; |
122 | p = mprand(MP_NEW, bits, r, 1); |
123 | s->p = pgen("p", p, p, 0, 0, 0, pgen_filter, &pf, |
124 | rabin_iters(bits), pgen_test, &pr); |
125 | } |
126 | |
3d64a35c |
127 | /* --- Construct the polynomial --- */ |
71dfe576 |
128 | |
3d64a35c |
129 | if (!s->v) |
130 | s->v = xmalloc(s->t * sizeof(share_pt)); |
71dfe576 |
131 | for (i = 0; i < s->t - 1; i++) |
3d64a35c |
132 | s->v[i].y = mprand_range(MP_NEWSEC, s->p, r, 0); |
5d4fee2a |
133 | s->v[s->t - 1].y = mp_copy(n); |
3d64a35c |
134 | } |
71dfe576 |
135 | |
3d64a35c |
136 | /* --- @share_get@ --- * |
137 | * |
138 | * Arguments: @share *s@ = pointer to share conext |
139 | * @mp *d@ = destination for the share |
140 | * @unsigned x@ = share index to fetch |
141 | * |
142 | * Returns: The share, as requested. |
143 | * |
144 | * Use: Extracts a share from the system. You may extract @MPW_MAX@ |
145 | * shares, or @s->p@ shares from the system, whichever is |
146 | * smaller. Shares are indexed from 0. |
147 | */ |
71dfe576 |
148 | |
3d64a35c |
149 | mp *share_get(share *s, mp *d, unsigned x) |
150 | { |
151 | mpbarrett mb; |
152 | mpw uw = x + 1; |
153 | mp u; |
154 | unsigned i; |
71dfe576 |
155 | |
3d64a35c |
156 | /* --- Various bits of initialization --- */ |
71dfe576 |
157 | |
3d64a35c |
158 | mp_build(&u, &uw, &uw + 1); |
eab06f16 |
159 | mp_drop(d); |
71dfe576 |
160 | |
3d64a35c |
161 | /* --- Evaluate the polynomial at %$x = i + 1$% --- */ |
71dfe576 |
162 | |
3d64a35c |
163 | d = MP_ZERO; |
164 | mpbarrett_create(&mb, s->p); |
165 | for (i = 0; i < s->t; i++) { |
166 | d = mp_mul(d, d, &u); |
167 | d = mp_add(d, d, s->v[i].y); |
168 | d = mpbarrett_reduce(&mb, d, d); |
71dfe576 |
169 | } |
ad74e5b4 |
170 | mpbarrett_destroy(&mb); |
71dfe576 |
171 | |
3d64a35c |
172 | return (d); |
71dfe576 |
173 | } |
174 | |
175 | /* --- @share_add@ --- * |
176 | * |
177 | * Arguments: @share *s@ = pointer to sharing context |
178 | * @unsigned x@ = which share number this is |
179 | * @mp *y@ = the share value |
180 | * |
181 | * Returns: Number of shares required before recovery may be performed. |
182 | * |
183 | * Use: Adds a share to the context. The context must have been |
184 | * initialized with the correct prime @p@ and threshold @t@. |
185 | */ |
186 | |
187 | unsigned share_add(share *s, unsigned x, mp *y) |
188 | { |
189 | /* --- If no vector has been allocated, create one --- */ |
190 | |
191 | if (!s->v) { |
3d64a35c |
192 | unsigned i; |
71dfe576 |
193 | s->v = xmalloc(s->t * sizeof(share_pt)); |
194 | s->i = 0; |
3d64a35c |
195 | for (i = 0; i < s->t; i++) |
196 | s->v[i].y = 0; |
71dfe576 |
197 | } |
198 | |
199 | assert(((void)"Share context is full", s->i < s->t)); |
200 | |
201 | /* --- Store the share in the vector --- */ |
202 | |
3d64a35c |
203 | s->v[s->i].x = x + 1; |
71dfe576 |
204 | s->v[s->i].y = mp_copy(y); |
205 | s->i++; |
206 | |
207 | /* --- Done --- */ |
208 | |
209 | return (s->t - s->i); |
210 | } |
211 | |
212 | /* --- @share_combine@ --- * |
213 | * |
214 | * Arguments: @share *s@ = pointer to share context |
215 | * |
216 | * Returns: The secret, as a multiprecision integer. |
217 | * |
218 | * Use: Reconstructs a secret, given enough shares. |
219 | */ |
220 | |
221 | mp *share_combine(share *s) |
222 | { |
223 | mp *a = MP_ZERO; |
ad74e5b4 |
224 | mpbarrett mb; |
71dfe576 |
225 | unsigned i, j; |
ad74e5b4 |
226 | mp ii, jj; |
227 | mpw iiw, jjw; |
71dfe576 |
228 | mp *m = MP_NEW; |
229 | |
230 | /* --- Sanity checking --- */ |
231 | |
232 | assert(((void)"Not enough shares yet", s->i == s->t)); |
233 | |
234 | /* --- Initialization --- */ |
235 | |
ad74e5b4 |
236 | mpbarrett_create(&mb, s->p); |
237 | mp_build(&ii, &iiw, &iiw + 1); |
238 | mp_build(&jj, &jjw, &jjw + 1); |
71dfe576 |
239 | |
240 | /* --- Grind through the shares --- */ |
241 | |
242 | for (i = 0; i < s->t; i++) { |
ad74e5b4 |
243 | mp *c = MP_ONE; |
71dfe576 |
244 | |
3d64a35c |
245 | iiw = s->v[i].x; |
71dfe576 |
246 | for (j = 0; j < s->t; j++) { |
247 | if (i == j) |
248 | continue; |
3d64a35c |
249 | jjw = s->v[j].x; |
71dfe576 |
250 | if (s->v[j].x >= s->v[i].x) |
ad74e5b4 |
251 | m = mp_sub(m, &jj, &ii); |
71dfe576 |
252 | else { |
ad74e5b4 |
253 | m = mp_sub(m, &ii, &jj); |
71dfe576 |
254 | m = mp_sub(m, s->p, m); |
255 | } |
b817bfc6 |
256 | m = mp_modinv(m, m, s->p); |
ad74e5b4 |
257 | c = mp_mul(c, c, &jj); |
258 | c = mpbarrett_reduce(&mb, c, c); |
259 | c = mp_mul(c, c, m); |
260 | c = mpbarrett_reduce(&mb, c, c); |
71dfe576 |
261 | } |
ad74e5b4 |
262 | c = mp_mul(c, c, s->v[i].y); |
263 | c = mpbarrett_reduce(&mb, c, c); |
71dfe576 |
264 | a = mp_add(a, a, c); |
265 | mp_drop(c); |
266 | } |
267 | |
ad74e5b4 |
268 | a = mpbarrett_reduce(&mb, a, a); |
eab06f16 |
269 | mp_drop(m); |
ad74e5b4 |
270 | mpbarrett_destroy(&mb); |
71dfe576 |
271 | return (a); |
272 | } |
273 | |
274 | /*----- Test rig ----------------------------------------------------------*/ |
275 | |
276 | #ifdef TEST_RIG |
277 | |
278 | #include "fibrand.h" |
279 | |
280 | static int verify(grand *r) |
281 | { |
282 | unsigned n = r->ops->range(r, 16) + 8; |
283 | unsigned t = r->ops->range(r, n - 1) + 1; |
284 | unsigned len = r->ops->range(r, 160); |
285 | |
3d64a35c |
286 | mp **v = xmalloc(t * sizeof(mp *)); |
71dfe576 |
287 | unsigned *p = xmalloc(n * sizeof(unsigned)); |
288 | mp *sec = mprand(MP_NEW, len, r, 0); |
289 | share s; |
290 | mp *pp; |
291 | mp *ss; |
292 | unsigned i; |
293 | |
294 | int ok = 1; |
295 | |
296 | for (i = 0; i < n; i++) |
297 | p[i] = i; |
298 | for (i = 0; i < t; i++) { |
299 | unsigned long j = r->ops->range(r, n - i); |
300 | unsigned x = p[i]; |
301 | p[i] = p[i + j]; |
302 | p[i + j] = x; |
303 | } |
304 | |
3d64a35c |
305 | share_create(&s, t); |
5d4fee2a |
306 | share_mkshares(&s, r, sec); |
3d64a35c |
307 | for (i = 0; i < t; i++) |
308 | v[i] = share_get(&s, MP_NEW, p[i]); |
71dfe576 |
309 | pp = mp_copy(s.p); |
310 | share_destroy(&s); |
3d64a35c |
311 | assert(mparena_count(MPARENA_GLOBAL) + mparena_count(MPARENA_SECURE) == t + 2); |
71dfe576 |
312 | |
3d64a35c |
313 | share_create(&s, t); |
71dfe576 |
314 | s.p = pp; |
3d64a35c |
315 | for (i = 0; i < t; i++) |
316 | share_add(&s, p[i], v[i]); |
71dfe576 |
317 | ss = share_combine(&s); |
318 | share_destroy(&s); |
319 | |
22bab86c |
320 | if (!MP_EQ(sec, ss)) { |
71dfe576 |
321 | ok = 0; |
322 | fprintf(stderr, "\nbad recombination of shares\n"); |
323 | }; |
324 | |
325 | mp_drop(sec); |
326 | mp_drop(ss); |
327 | |
328 | for (i = 0; i < t; i++) |
3d64a35c |
329 | mp_drop(v[i]); |
71dfe576 |
330 | |
331 | xfree(v); |
332 | xfree(p); |
333 | |
3d64a35c |
334 | assert(mparena_count(MPARENA_GLOBAL) + mparena_count(MPARENA_SECURE) == 0); |
71dfe576 |
335 | return (ok); |
336 | } |
337 | |
338 | int main(void) |
339 | { |
340 | grand *r = fibrand_create(0); |
341 | unsigned i; |
342 | int ok = 1; |
343 | |
344 | fputs("share: ", stdout); |
345 | for (i = 0; i < 40; i++) { |
346 | if (!verify(r)) |
347 | ok = 0; |
348 | fputc('.', stdout); |
349 | fflush(stdout); |
350 | } |
351 | |
352 | if (ok) |
353 | fputs(" ok\n", stdout); |
354 | else |
355 | fputs(" failed\n", stdout); |
356 | return (ok ? EXIT_SUCCESS : EXIT_FAILURE); |
357 | } |
358 | |
359 | #endif |
360 | |
361 | /*----- That's all, folks -------------------------------------------------*/ |