3 * $Id: gfshare.c,v 1.2 2000/06/18 23:12:15 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.2 2000/06/18 23:12:15 mdw
34 * Change typesetting of Galois Field names.
36 * Revision 1.1 2000/06/17 10:56:30 mdw
37 * Fast but nonstandard secret sharing system.
41 /*----- Header files ------------------------------------------------------*/
47 #include <mLib/alloc.h>
48 #include <mLib/bits.h>
52 #include "gfshare-tab.h"
55 /*----- Static variables --------------------------------------------------*/
57 static octet gflog
[] = GFSHARE_LOG
, gfexp
[] = GFSHARE_EXP
;
59 /*----- Main code ---------------------------------------------------------*/
61 /* --- @gfshare_create@ --- *
63 * Arguments: @gfshare *s@ = pointer to share context to initialize
64 * @unsigned t, n@ = threshold parameters for the system
65 * @size_t sz@ = size of the secret
69 * Use: Initializes a sharing context.
72 void gfshare_create(gfshare
*s
, unsigned t
, unsigned n
, size_t sz
)
82 /* --- @gfshare_destroy@ --- *
84 * Arguments: @gfshare *s@ = pointer to share context to destroy
88 * Use: Disposes of a sharing context. The allocations for the
89 * individual shares and the vector @v@ are freed; the secret is
93 void gfshare_destroy(gfshare
*s
)
98 /* --- Dispose of the share vector --- */
107 for (i
= 0; i
< n
; i
++) {
115 /* --- @gfshare_mkshares@ --- *
117 * Arguments: @gfshare *s@ = pointer to share context to fill in
118 * @grand *r@ = pointer to random number source
122 * Use: Generates @c->n@ secret shares, such that any @c->t@ of them
123 * may be used to recover the secret.
125 * The context structure is expected to be mostly filled in. In
126 * particular, @t@, @n@, @ssz@ and @s@ must be initialized. If
127 * @v@ is zero, a vector of appropriate size is allocated. You
128 * should use the macro @GFSHARE_INIT@ or @gfshare_create@ to
129 * construct sharing contexts.
132 void gfshare_mkshares(gfshare
*s
, grand
*r
)
137 /* --- Construct the coefficients --- */
139 v
= XS_ALLOC(s
->sz
* s
->t
);
140 r
->ops
->fill(r
, v
, s
->sz
* (s
->t
- 1));
141 memcpy(v
+ s
->sz
* (s
->t
- 1), s
->s
, s
->sz
);
144 /* --- Construct the shares --- */
147 s
->v
= xmalloc(s
->n
* sizeof(gfshare_pt
));
149 for (i
= 0; i
< s
->n
; i
++) {
152 unsigned ilog
= gflog
[i
+ 1];
154 /* --- Evaluate the polynomial at %$x = i + 1$% --- */
156 s
->v
[i
].y
= XS_ALLOC(s
->sz
);
157 memcpy(s
->v
[i
].y
, p
, s
->sz
);
159 for (j
= 1; j
< s
->t
; j
++) {
160 octet
*q
= s
->v
[i
].y
;
162 for (k
= 0; k
< s
->sz
; k
++) {
165 qq
= gfexp
[ilog
+ gflog
[qq
]];
172 /* --- Dispose of various bits of old rubbish --- */
177 /* --- @gfshare_add@ --- *
179 * Arguments: @gfshare *s@ = pointer to sharing context
180 * @unsigned x@ = which share number this is
181 * @const octet *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 octet
*y
)
191 /* --- If no vector has been allocated, create one --- */
194 s
->v
= xmalloc(s
->t
* sizeof(gfshare_pt
));
198 assert(((void)"Share context is full", s
->i
< s
->t
));
200 /* --- Store the share in the vector --- */
203 s
->v
[s
->i
].y
= XS_ALLOC(s
->sz
);
204 memcpy(s
->v
[s
->i
].y
, y
, s
->sz
);
209 return (s
->t
- s
->i
);
212 /* --- @gfshare_combine@ --- *
214 * Arguments: @gfshare *s@ = pointer to share context
215 * @octet *buf@ = pointer to output buffer for the secret
219 * Use: Reconstructs a secret, given enough shares.
222 void gfshare_combine(gfshare
*s
, octet
*buf
)
227 /* --- Sanity checking --- */
229 assert(((void)"Not enough shares yet", s
->i
== s
->t
));
231 /* --- Precomputation of coefficients --- */
235 for (i
= 0; i
< s
->t
; i
++) {
236 unsigned c
= 0, ci
= 0;
237 for (j
= 0; j
< s
->t
; j
++) {
240 c
+= gflog
[s
->v
[j
].x
+ 1];
243 ci
+= gflog
[(s
->v
[i
].x
+ 1) ^ (s
->v
[j
].x
+ 1)];
253 /* --- Grind through the shares --- */
255 for (i
= 0; i
< s
->sz
; i
++) {
257 for (j
= 0; j
< s
->t
; j
++) {
259 x
^= gfexp
[v
[j
] + gflog
[s
->v
[j
].y
[i
]]];
267 /*----- Test rig ----------------------------------------------------------*/
273 static int verify(grand
*r
)
275 unsigned n
= r
->ops
->range(r
, 16) + 8;
276 unsigned t
= r
->ops
->range(r
, n
- 1) + 1;
277 unsigned len
= r
->ops
->range(r
, 32) + 1;
279 gfshare_pt
*v
= xmalloc(t
* sizeof(gfshare_pt
));
280 unsigned *p
= xmalloc(n
* sizeof(unsigned));
281 octet
*sec
= xmalloc(len
), *sbuf
= xmalloc(len
);
287 for (i
= 0; i
< n
; i
++)
289 for (i
= 0; i
< t
; i
++) {
290 unsigned long j
= r
->ops
->range(r
, n
- i
);
296 r
->ops
->fill(r
, sec
, len
);
298 gfshare_create(&s
, t
, n
, len
);
301 gfshare_mkshares(&s
, r
);
302 for (i
= 0; i
< t
; i
++) {
303 v
[i
].x
= s
.v
[p
[i
]].x
;
304 v
[i
].y
= xmalloc(len
);
305 memcpy(v
[i
].y
, s
.v
[p
[i
]].y
, len
);
309 gfshare_create(&s
, t
, n
, len
);
310 for (i
= 0; i
< t
; i
++) {
311 gfshare_add(&s
, v
[i
].x
, v
[i
].y
);
313 gfshare_combine(&s
, sbuf
);
316 if (memcmp(sec
, sbuf
, len
) != 0) {
318 fprintf(stderr
, "\nbad recombination of shares\n");
324 for (i
= 0; i
< t
; i
++)
334 grand
*r
= fibrand_create(0);
338 fputs("gfshare: ", stdout
);
339 for (i
= 0; i
< 40; i
++) {
347 fputs(" ok\n", stdout
);
349 fputs(" failed\n", stdout
);
350 return (ok ? EXIT_SUCCESS
: EXIT_FAILURE
);
355 /*----- That's all, folks -------------------------------------------------*/