3 * $Id: gfshare.c,v 1.8 2004/04/02 01:03:49 mdw Exp $
5 * Secret sharing over %$\gf{2^8}$%
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.8 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
36 * Revision 1.7 2001/06/16 23:42:17 mdw
39 * Revision 1.6 2000/12/06 20:30:10 mdw
40 * Change secret sharing interface: present the secret at share
43 * Revision 1.5 2000/06/24 19:11:47 mdw
44 * Fix daft error in the comment for @gfshare_get@.
46 * Revision 1.4 2000/06/24 18:29:05 mdw
47 * Interface change: allow shares to be extracted from a context on demand,
48 * rather than building them all up-front.
50 * Revision 1.3 2000/06/22 18:04:13 mdw
51 * Improve secret reconstruction -- compute coefficients as needed rather
52 * than making a big array of them.
54 * Revision 1.2 2000/06/18 23:12:15 mdw
55 * Change typesetting of Galois Field names.
57 * Revision 1.1 2000/06/17 10:56:30 mdw
58 * Fast but nonstandard secret sharing system.
62 /*----- Header files ------------------------------------------------------*/
69 #include <mLib/alloc.h>
70 #include <mLib/bits.h>
74 #include "gfshare-tab.h"
77 /*----- Static variables --------------------------------------------------*/
79 static const octet gflog
[] = GFSHARE_LOG
, gfexp
[] = GFSHARE_EXP
;
81 /*----- Main code ---------------------------------------------------------*/
83 /* --- @gfshare_create@ --- *
85 * Arguments: @gfshare *s@ = pointer to share context to initialize
86 * @unsigned t@ = threshold for the system
87 * @size_t sz@ = size of the secret
91 * Use: Initializes a sharing context.
94 void gfshare_create(gfshare
*s
, unsigned t
, size_t sz
)
102 /* --- @gfshare_destroy@ --- *
104 * Arguments: @gfshare *s@ = pointer to share context to destroy
108 * Use: Disposes of a sharing context. The allocations for the
109 * individual shares and the vector @v@ are freed; the secret is
113 void gfshare_destroy(gfshare
*s
)
119 /* --- @gfshare_mkshares@ --- *
121 * Arguments: @gfshare *s@ = pointer to share context to fill in
122 * @grand *r@ = pointer to random number source
123 * @const void *buf@ = pointer to the secret to share
127 * Use: Initializes a sharing context to be able to create shares.
128 * The context structure is expected to be mostly filled in. In
129 * particular, @t@ must be initialized. If @v@ is zero, a
130 * vector of appropriate size is allocated. You should use the
131 * macro @GFSHARE_INIT@ or @gfshare_create@ to construct sharing
135 void gfshare_mkshares(gfshare
*s
, grand
*r
, const void *buf
)
137 s
->v
= XS_ALLOC(s
->sz
* s
->t
);
138 r
->ops
->fill(r
, s
->v
, s
->sz
* (s
->t
- 1));
139 memcpy(s
->v
+ s
->sz
* (s
->t
- 1), buf
, s
->sz
);
142 /* --- @gfshare_get@ --- *
144 * Arguments: @gfshare *s@ = pointer to share conext
145 * @unsigned x@ = share index to fetch
146 * @void *buf@ = pointer to output buffer
150 * Use: Extracts a share from the system. You may extract up to 255
151 * shares from the system. Shares are indexed from 0.
154 void gfshare_get(gfshare
*s
, unsigned x
, void *buf
)
157 const octet
*p
= s
->v
;
158 unsigned ilog
= gflog
[x
+ 1];
160 /* --- Evaluate the polynomial at %$x = i + 1$% --- */
162 memcpy(buf
, p
, s
->sz
);
165 for (i
= 1; i
< s
->t
; i
++) {
168 for (k
= 0; k
< s
->sz
; k
++) {
171 qq
= gfexp
[ilog
+ gflog
[qq
]];
177 /* --- @gfshare_add@ --- *
179 * Arguments: @gfshare *s@ = pointer to sharing context
180 * @unsigned x@ = which share number this is
181 * @const void *y@ = the share value
183 * Returns: Number of shares required before recovery may be performed.
185 * Use: Adds a share to the context. The context must have been
186 * initialized with the correct threshold @t@.
189 unsigned gfshare_add(gfshare
*s
, unsigned x
, const void *y
)
193 /* --- If no vector has been allocated, create one --- */
196 s
->v
= XS_ALLOC(s
->t
* (s
->sz
+ 1));
200 assert(((void)"Share context is full", s
->i
< s
->t
));
202 /* --- Store the share in the vector --- */
204 p
= s
->v
+ s
->i
* (s
->sz
+ 1);
211 return (s
->t
- s
->i
);
214 /* --- @gfshare_combine@ --- *
216 * Arguments: @gfshare *s@ = pointer to share context
217 * @void *buf@ = pointer to output buffer for the secret
221 * Use: Reconstructs a secret, given enough shares.
224 void gfshare_combine(gfshare
*s
, void *buf
)
229 /* --- Sanity checking --- */
231 assert(((void)"Not enough shares yet", s
->i
== s
->t
));
233 /* --- Grind through the shares --- */
235 memset(buf
, 0, s
->sz
);
237 for (i
= 0; i
< s
->t
; i
++) {
239 octet
*q
= s
->v
+ i
* (s
->sz
+ 1);
240 unsigned c
= 0, ci
= 0;
242 /* --- Compute the magic coefficient --- */
245 for (j
= 0; j
< s
->t
; j
++) {
248 xj
= s
->v
[j
* (s
->sz
+ 1)];
252 ci
+= gflog
[xi
^ xj
];
260 /* --- Work out another layer of the secret --- */
263 for (j
= 0; j
< s
->sz
; j
++) {
265 *p
^= gfexp
[c
+ gflog
[*q
]];
271 /*----- Test rig ----------------------------------------------------------*/
277 static int verify(grand
*r
)
279 unsigned n
= r
->ops
->range(r
, 16) + 8;
280 unsigned t
= r
->ops
->range(r
, n
- 1) + 1;
281 unsigned len
= r
->ops
->range(r
, 32) + 1;
283 octet
*v
= xmalloc(t
* len
);
284 unsigned *p
= xmalloc(n
* sizeof(unsigned));
285 octet
*sec
= xmalloc(len
), *sbuf
= xmalloc(len
);
291 for (i
= 0; i
< n
; i
++)
293 for (i
= 0; i
< t
; i
++) {
294 unsigned long j
= r
->ops
->range(r
, n
- i
);
300 r
->ops
->fill(r
, sec
, len
);
302 gfshare_create(&s
, t
, len
);
304 gfshare_mkshares(&s
, r
, sec
);
305 for (i
= 0; i
< t
; i
++)
306 gfshare_get(&s
, p
[i
], v
+ (i
* len
));
309 gfshare_create(&s
, t
, len
);
310 for (i
= 0; i
< t
; i
++)
311 gfshare_add(&s
, p
[i
], v
+ (i
* len
));
312 gfshare_combine(&s
, sbuf
);
315 if (memcmp(sec
, sbuf
, len
) != 0) {
317 fprintf(stderr
, "\nbad recombination of shares\n");
331 grand
*r
= fibrand_create(0);
335 fputs("gfshare: ", stdout
);
336 for (i
= 0; i
< 40; i
++) {
344 fputs(" ok\n", stdout
);
346 fputs(" failed\n", stdout
);
347 return (ok ? EXIT_SUCCESS
: EXIT_FAILURE
);
352 /*----- That's all, folks -------------------------------------------------*/