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