0eb076b1064abdba71386f27ea74ba5d69ac9faf
3 * Secret sharing over %$\gf{2^8}$%
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/alloc.h>
36 #include <mLib/bits.h>
40 #include "gfshare-tab.h"
43 /*----- Static variables --------------------------------------------------*/
45 static const octet gflog
[] = GFSHARE_LOG
, gfexp
[] = GFSHARE_EXP
;
47 /*----- Main code ---------------------------------------------------------*/
49 /* --- @gfshare_create@ --- *
51 * Arguments: @gfshare *s@ = pointer to share context to initialize
52 * @unsigned t@ = threshold for the system
53 * @size_t sz@ = size of the secret
57 * Use: Initializes a sharing context.
60 void gfshare_create(gfshare
*s
, unsigned t
, size_t sz
)
68 /* --- @gfshare_destroy@ --- *
70 * Arguments: @gfshare *s@ = pointer to share context to destroy
74 * Use: Disposes of a sharing context. The allocations for the
75 * individual shares and the vector @v@ are freed; the secret is
79 void gfshare_destroy(gfshare
*s
)
85 /* --- @gfshare_mkshares@ --- *
87 * Arguments: @gfshare *s@ = pointer to share context to fill in
88 * @grand *r@ = pointer to random number source
89 * @const void *buf@ = pointer to the secret to share
93 * Use: Initializes a sharing context to be able to create shares.
94 * The context structure is expected to be mostly filled in. In
95 * particular, @t@ must be initialized. If @v@ is zero, a
96 * vector of appropriate size is allocated. You should use the
97 * macro @GFSHARE_INIT@ or @gfshare_create@ to construct sharing
101 void gfshare_mkshares(gfshare
*s
, grand
*r
, const void *buf
)
103 s
->v
= XS_ALLOC(s
->sz
* s
->t
);
104 r
->ops
->fill(r
, s
->v
, s
->sz
* (s
->t
- 1));
105 memcpy(s
->v
+ s
->sz
* (s
->t
- 1), buf
, s
->sz
);
108 /* --- @gfshare_get@ --- *
110 * Arguments: @gfshare *s@ = pointer to share conext
111 * @unsigned x@ = share index to fetch
112 * @void *buf@ = pointer to output buffer
116 * Use: Extracts a share from the system. You may extract up to 255
117 * shares from the system. Shares are indexed from 0.
120 void gfshare_get(gfshare
*s
, unsigned x
, void *buf
)
123 const octet
*p
= s
->v
;
124 unsigned ilog
= gflog
[x
+ 1];
126 /* --- Evaluate the polynomial at %$x = i + 1$% --- */
128 memcpy(buf
, p
, s
->sz
);
131 for (i
= 1; i
< s
->t
; i
++) {
134 for (k
= 0; k
< s
->sz
; k
++) {
137 qq
= gfexp
[ilog
+ gflog
[qq
]];
143 /* --- @gfshare_addedp@ --- *
145 * Arguments: @gfshare *s@ = pointer to sharing context
146 * @unsigned x@ = which share number to check
148 * Returns: Nonzero if share @x@ has been added already, zero if it
152 int gfshare_addedp(gfshare
*s
, unsigned x
)
156 for (i
= 0; i
< s
->i
; i
++) {
157 if (GFSHARE_INDEX(s
, i
) == x
+ 1)
163 /* --- @gfshare_add@ --- *
165 * Arguments: @gfshare *s@ = pointer to sharing context
166 * @unsigned x@ = which share number this is
167 * @const void *y@ = the share value
169 * Returns: Number of shares required before recovery may be performed.
171 * Use: Adds a share to the context. The context must have been
172 * initialized with the correct threshold @t@.
175 unsigned gfshare_add(gfshare
*s
, unsigned x
, const void *y
)
179 assert(((void)"Share context is full", s
->i
< s
->t
));
180 assert(((void)"Share already present", !gfshare_addedp(s
, x
)));
182 /* --- If no vector has been allocated, create one --- */
185 s
->v
= XS_ALLOC(s
->t
* (s
->sz
+ 1));
189 /* --- Store the share in the vector --- */
191 p
= &GFSHARE_INDEX(s
, s
->i
);
198 return (s
->t
- s
->i
);
201 /* --- @gfshare_combine@ --- *
203 * Arguments: @gfshare *s@ = pointer to share context
204 * @void *buf@ = pointer to output buffer for the secret
208 * Use: Reconstructs a secret, given enough shares.
211 void gfshare_combine(gfshare
*s
, void *buf
)
216 /* --- Sanity checking --- */
218 assert(((void)"Not enough shares yet", s
->i
== s
->t
));
220 /* --- Grind through the shares --- */
222 memset(buf
, 0, s
->sz
);
224 for (i
= 0; i
< s
->t
; i
++) {
226 octet
*q
= &GFSHARE_INDEX(s
, i
);
227 unsigned c
= 0, ci
= 0;
229 /* --- Compute the magic coefficient --- */
232 for (j
= 0; j
< s
->t
; j
++) {
235 xj
= GFSHARE_INDEX(s
, j
);
239 ci
+= gflog
[xi
^ xj
];
247 /* --- Work out another layer of the secret --- */
250 for (j
= 0; j
< s
->sz
; j
++) {
252 *p
^= gfexp
[c
+ gflog
[*q
]];
258 /*----- Test rig ----------------------------------------------------------*/
264 static int verify(grand
*r
)
266 unsigned n
= r
->ops
->range(r
, 16) + 8;
267 unsigned t
= r
->ops
->range(r
, n
- 1) + 1;
268 unsigned len
= r
->ops
->range(r
, 32) + 1;
270 octet
*v
= xmalloc(t
* len
);
271 unsigned *p
= xmalloc(n
* sizeof(unsigned));
272 octet
*sec
= xmalloc(len
), *sbuf
= xmalloc(len
);
278 for (i
= 0; i
< n
; i
++)
280 for (i
= 0; i
< t
; i
++) {
281 unsigned long j
= r
->ops
->range(r
, n
- i
);
287 r
->ops
->fill(r
, sec
, len
);
289 gfshare_create(&s
, t
, len
);
291 gfshare_mkshares(&s
, r
, sec
);
292 for (i
= 0; i
< t
; i
++)
293 gfshare_get(&s
, p
[i
], v
+ (i
* len
));
296 gfshare_create(&s
, t
, len
);
297 for (i
= 0; i
< t
; i
++)
298 gfshare_add(&s
, p
[i
], v
+ (i
* len
));
299 gfshare_combine(&s
, sbuf
);
302 if (memcmp(sec
, sbuf
, len
) != 0) {
304 fprintf(stderr
, "\nbad recombination of shares\n");
318 grand
*r
= fibrand_create(0);
322 fputs("gfshare: ", stdout
);
323 for (i
= 0; i
< 40; i
++) {
331 fputs(" ok\n", stdout
);
333 fputs(" failed\n", stdout
);
334 return (ok ? EXIT_SUCCESS
: EXIT_FAILURE
);
339 /*----- That's all, folks -------------------------------------------------*/