Interface change: allow shares to be extracted from a context on demand,
[u/mdw/catacomb] / gfshare.h
CommitLineData
6c1035f5 1/* -*-c-*-
2 *
3d64a35c 3 * $Id: gfshare.h,v 1.4 2000/06/24 18:29:05 mdw Exp $
6c1035f5 4 *
4d47e157 5 * Secret sharing over %$\gf{2^8}$%
6c1035f5 6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: gfshare.h,v $
3d64a35c 33 * Revision 1.4 2000/06/24 18:29:05 mdw
34 * Interface change: allow shares to be extracted from a context on demand,
35 * rather than building them all up-front.
36 *
4d47e157 37 * Revision 1.3 2000/06/18 23:12:15 mdw
38 * Change typesetting of Galois Field names.
39 *
73fb671f 40 * Revision 1.2 2000/06/17 11:05:27 mdw
41 * Add a commentary on the system.
42 *
6c1035f5 43 * Revision 1.1 2000/06/17 10:56:30 mdw
44 * Fast but nonstandard secret sharing system.
45 *
46 */
47
73fb671f 48/*----- Notes on the system -----------------------------------------------*
49 *
50 * This uses a variant of Shamir's secret sharing system. Shamir's original
51 * system used polynomials modulo a large prime. This implementation instead
4d47e157 52 * uses the field %$\gf{2^8}$%, represented by
73fb671f 53 *
4d47e157 54 * %$\gf{2}[x]/(x^8 + x^4 + x^3 + x^2 + 1)$%
73fb671f 55 *
56 * and shares each byte of the secret independently. It is therefore limited
57 * to 255 players, although this probably isn't a serious limitation in
58 * practice.
59 *
60 * Share creation and reconstruction is extremely efficient. Contrast the
61 * performance of the straightforward implementation based on multiprecision
62 * arithmetic.
63 */
64
6c1035f5 65#ifndef CATACOMB_GFSHARE_H
66#define CATACOMB_GFSHARE_H
67
68#ifdef __cplusplus
69 extern "C" {
70#endif
71
72/*----- Header files ------------------------------------------------------*/
73
74#include <mLib/bits.h>
75
76#ifndef CATACOMB_GRAND_H
77# include "grand.h"
78#endif
79
80/*----- Data structures ---------------------------------------------------*/
81
82/* --- A secret sharing context --- */
83
6c1035f5 84typedef struct gfshare {
85 unsigned t; /* Threshold */
6c1035f5 86 unsigned i; /* Next free slot in vector */
87 size_t sz; /* Size of the secret and shares */
3d64a35c 88 void *s; /* The secret */
89 octet *v; /* Vector of share information */
6c1035f5 90} gfshare;
91
3d64a35c 92#define GFSHARE_INIT(t, sz) { t, 0, sz, 0, 0 }
6c1035f5 93
94/*----- Functions provided ------------------------------------------------*/
95
96/* --- @gfshare_create@ --- *
97 *
98 * Arguments: @gfshare *s@ = pointer to share context to initialize
3d64a35c 99 * @unsigned t@ = threshold for the system
6c1035f5 100 * @size_t sz@ = size of the secret
101 *
102 * Returns: ---
103 *
104 * Use: Initializes a sharing context.
105 */
106
3d64a35c 107extern void gfshare_create(gfshare */*s*/, unsigned /*t*/, size_t /*sz*/);
6c1035f5 108
109/* --- @gfshare_destroy@ --- *
110 *
111 * Arguments: @gfshare *s@ = pointer to share context to destroy
112 *
113 * Returns: ---
114 *
115 * Use: Disposes of a sharing context. The allocations for the
116 * individual shares and the vector @v@ are freed; the secret is
117 * left alone.
118 */
119
120extern void gfshare_destroy(gfshare */*s*/);
121
122/* --- @gfshare_mkshares@ --- *
123 *
124 * Arguments: @gfshare *s@ = pointer to share context to fill in
125 * @grand *r@ = pointer to random number source
126 *
127 * Returns: ---
128 *
3d64a35c 129 * Use: Initializes a sharing context to be able to create shares.
6c1035f5 130 * The context structure is expected to be mostly filled in. In
3d64a35c 131 * particular, @t@ and @s@ must be initialized. If @v@ is zero,
132 * a vector of appropriate size is allocated. You should use
133 * the macro @GFSHARE_INIT@ or @gfshare_create@ to construct
134 * sharing contexts.
6c1035f5 135 */
136
137extern void gfshare_mkshares(gfshare */*s*/, grand */*r*/);
138
3d64a35c 139/* --- @gfshare_get@ --- *
140 *
141 * Arguments: @gfshare *s@ = pointer to share conext
142 * @unsigned x@ = share index to fetch
143 * @void *buf@ = pointer to output buffer
144 *
145 * Returns: The share, as requested.
146 *
147 * Use: Extracts a share from the system. You may extract up to 255
148 * shares from the system. Shares are indexed from 0.
149 */
150
151extern void gfshare_get(gfshare */*s*/, unsigned /*x*/, void */*buf*/);
152
6c1035f5 153/* --- @gfshare_add@ --- *
154 *
155 * Arguments: @gfshare *s@ = pointer to sharing context
156 * @unsigned x@ = which share number this is
3d64a35c 157 * @const void *y@ = the share value
6c1035f5 158 *
159 * Returns: Number of shares required before recovery may be performed.
160 *
161 * Use: Adds a share to the context. The context must have been
162 * initialized with the correct threshold @t@.
163 */
164
165extern unsigned gfshare_add(gfshare */*s*/,
3d64a35c 166 unsigned /*x*/, const void */*y*/);
6c1035f5 167
168/* --- @gfshare_combine@ --- *
169 *
170 * Arguments: @gfshare *s@ = pointer to share context
3d64a35c 171 * @void *buf@ = pointer to output buffer for the secret
6c1035f5 172 *
173 * Returns: ---
174 *
175 * Use: Reconstructs a secret, given enough shares.
176 */
177
3d64a35c 178extern void gfshare_combine(gfshare */*s*/, void */*buf*/);
6c1035f5 179
180/*----- That's all, folks -------------------------------------------------*/
181
182#ifdef __cplusplus
183 }
184#endif
185
186#endif