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