3 * $Id: gfshare.c,v 1.5 2000/06/24 19:11:47 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.5 2000/06/24 19:11:47 mdw
34 * Fix daft error in the comment for @gfshare_get@.
36 * Revision 1.4 2000/06/24 18:29:05 mdw
37 * Interface change: allow shares to be extracted from a context on demand,
38 * rather than building them all up-front.
40 * Revision 1.3 2000/06/22 18:04:13 mdw
41 * Improve secret reconstruction -- compute coefficients as needed rather
42 * than making a big array of them.
44 * Revision 1.2 2000/06/18 23:12:15 mdw
45 * Change typesetting of Galois Field names.
47 * Revision 1.1 2000/06/17 10:56:30 mdw
48 * Fast but nonstandard secret sharing system.
52 /*----- Header files ------------------------------------------------------*/
59 #include <mLib/alloc.h>
60 #include <mLib/bits.h>
64 #include "gfshare-tab.h"
67 /*----- Static variables --------------------------------------------------*/
69 static octet gflog
[] = GFSHARE_LOG
, gfexp
[] = GFSHARE_EXP
;
71 /*----- Main code ---------------------------------------------------------*/
73 /* --- @gfshare_create@ --- *
75 * Arguments: @gfshare *s@ = pointer to share context to initialize
76 * @unsigned t@ = threshold for the system
77 * @size_t sz@ = size of the secret
81 * Use: Initializes a sharing context.
84 void gfshare_create(gfshare
*s
, unsigned t
, size_t sz
)
93 /* --- @gfshare_destroy@ --- *
95 * Arguments: @gfshare *s@ = pointer to share context to destroy
99 * Use: Disposes of a sharing context. The allocations for the
100 * individual shares and the vector @v@ are freed; the secret is
104 void gfshare_destroy(gfshare
*s
)
110 /* --- @gfshare_mkshares@ --- *
112 * Arguments: @gfshare *s@ = pointer to share context to fill in
113 * @grand *r@ = pointer to random number source
117 * Use: Initializes a sharing context to be able to create shares.
118 * The context structure is expected to be mostly filled in. In
119 * particular, @t@ and @s@ must be initialized. If @v@ is zero,
120 * a vector of appropriate size is allocated. You should use
121 * the macro @GFSHARE_INIT@ or @gfshare_create@ to construct
125 void gfshare_mkshares(gfshare
*s
, grand
*r
)
127 s
->v
= XS_ALLOC(s
->sz
* s
->t
);
128 r
->ops
->fill(r
, s
->v
, s
->sz
* (s
->t
- 1));
129 memcpy(s
->v
+ s
->sz
* (s
->t
- 1), s
->s
, s
->sz
);
132 /* --- @gfshare_get@ --- *
134 * Arguments: @gfshare *s@ = pointer to share conext
135 * @unsigned x@ = share index to fetch
136 * @void *buf@ = pointer to output buffer
140 * Use: Extracts a share from the system. You may extract up to 255
141 * shares from the system. Shares are indexed from 0.
144 void gfshare_get(gfshare
*s
, unsigned x
, void *buf
)
147 const octet
*p
= s
->v
;
148 unsigned ilog
= gflog
[x
+ 1];
150 /* --- Evaluate the polynomial at %$x = i + 1$% --- */
152 memcpy(buf
, p
, s
->sz
);
155 for (i
= 1; i
< s
->t
; i
++) {
158 for (k
= 0; k
< s
->sz
; k
++) {
161 qq
= gfexp
[ilog
+ gflog
[qq
]];
167 /* --- @gfshare_add@ --- *
169 * Arguments: @gfshare *s@ = pointer to sharing context
170 * @unsigned x@ = which share number this is
171 * @const void *y@ = the share value
173 * Returns: Number of shares required before recovery may be performed.
175 * Use: Adds a share to the context. The context must have been
176 * initialized with the correct threshold @t@.
179 unsigned gfshare_add(gfshare
*s
, unsigned x
, const void *y
)
183 /* --- If no vector has been allocated, create one --- */
186 s
->v
= XS_ALLOC(s
->t
* (s
->sz
+ 1));
190 assert(((void)"Share context is full", s
->i
< s
->t
));
192 /* --- Store the share in the vector --- */
194 p
= s
->v
+ s
->i
* (s
->sz
+ 1);
201 return (s
->t
- s
->i
);
204 /* --- @gfshare_combine@ --- *
206 * Arguments: @gfshare *s@ = pointer to share context
207 * @void *buf@ = pointer to output buffer for the secret
211 * Use: Reconstructs a secret, given enough shares.
214 void gfshare_combine(gfshare
*s
, void *buf
)
219 /* --- Sanity checking --- */
221 assert(((void)"Not enough shares yet", s
->i
== s
->t
));
223 /* --- Grind through the shares --- */
225 memset(buf
, 0, s
->sz
);
227 for (i
= 0; i
< s
->t
; i
++) {
229 octet
*q
= s
->v
+ i
* (s
->sz
+ 1);
230 unsigned c
= 0, ci
= 0;
232 /* --- Compute the magic coefficient --- */
235 for (j
= 0; j
< s
->t
; j
++) {
238 xj
= s
->v
[j
* (s
->sz
+ 1)];
242 ci
+= gflog
[xi
^ xj
];
250 /* --- Work out another layer of the secret --- */
253 for (j
= 0; j
< s
->sz
; j
++) {
255 *p
^= gfexp
[c
+ gflog
[*q
]];
261 /*----- Test rig ----------------------------------------------------------*/
267 static int verify(grand
*r
)
269 unsigned n
= r
->ops
->range(r
, 16) + 8;
270 unsigned t
= r
->ops
->range(r
, n
- 1) + 1;
271 unsigned len
= r
->ops
->range(r
, 32) + 1;
273 octet
*v
= xmalloc(t
* len
);
274 unsigned *p
= xmalloc(n
* sizeof(unsigned));
275 octet
*sec
= xmalloc(len
), *sbuf
= xmalloc(len
);
281 for (i
= 0; i
< n
; i
++)
283 for (i
= 0; i
< t
; i
++) {
284 unsigned long j
= r
->ops
->range(r
, n
- i
);
290 r
->ops
->fill(r
, sec
, len
);
292 gfshare_create(&s
, t
, len
);
295 gfshare_mkshares(&s
, r
);
296 for (i
= 0; i
< t
; i
++)
297 gfshare_get(&s
, p
[i
], v
+ (i
* len
));
300 gfshare_create(&s
, t
, len
);
301 for (i
= 0; i
< t
; i
++)
302 gfshare_add(&s
, p
[i
], v
+ (i
* len
));
303 gfshare_combine(&s
, sbuf
);
306 if (memcmp(sec
, sbuf
, len
) != 0) {
308 fprintf(stderr
, "\nbad recombination of shares\n");
322 grand
*r
= fibrand_create(0);
326 fputs("gfshare: ", stdout
);
327 for (i
= 0; i
< 40; i
++) {
335 fputs(" ok\n", stdout
);
337 fputs(" failed\n", stdout
);
338 return (ok ? EXIT_SUCCESS
: EXIT_FAILURE
);
343 /*----- That's all, folks -------------------------------------------------*/