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