math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / misc / gfshare.h
CommitLineData
6c1035f5 1/* -*-c-*-
2 *
4d47e157 3 * Secret sharing over %$\gf{2^8}$%
6c1035f5 4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
6c1035f5 9 *
10 * This file is part of Catacomb.
11 *
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.
45c0fd36 16 *
6c1035f5 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.
45c0fd36 21 *
6c1035f5 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,
25 * MA 02111-1307, USA.
26 */
27
73fb671f 28/*----- Notes on the system -----------------------------------------------*
29 *
30 * This uses a variant of Shamir's secret sharing system. Shamir's original
31 * system used polynomials modulo a large prime. This implementation instead
4d47e157 32 * uses the field %$\gf{2^8}$%, represented by
73fb671f 33 *
4d47e157 34 * %$\gf{2}[x]/(x^8 + x^4 + x^3 + x^2 + 1)$%
73fb671f 35 *
36 * and shares each byte of the secret independently. It is therefore limited
37 * to 255 players, although this probably isn't a serious limitation in
38 * practice.
39 *
40 * Share creation and reconstruction is extremely efficient. Contrast the
41 * performance of the straightforward implementation based on multiprecision
42 * arithmetic.
43 */
44
6c1035f5 45#ifndef CATACOMB_GFSHARE_H
46#define CATACOMB_GFSHARE_H
47
48#ifdef __cplusplus
49 extern "C" {
50#endif
51
52/*----- Header files ------------------------------------------------------*/
53
54#include <mLib/bits.h>
55
56#ifndef CATACOMB_GRAND_H
57# include "grand.h"
58#endif
59
60/*----- Data structures ---------------------------------------------------*/
61
62/* --- A secret sharing context --- */
63
6c1035f5 64typedef struct gfshare {
65 unsigned t; /* Threshold */
6c1035f5 66 unsigned i; /* Next free slot in vector */
67 size_t sz; /* Size of the secret and shares */
3d64a35c 68 octet *v; /* Vector of share information */
6c1035f5 69} gfshare;
70
5d4fee2a 71#define GFSHARE_INIT(t, sz) { t, 0, sz, 0 }
6c1035f5 72
aec42286 73#define GFSHARE_INDEX(s, i) ((s)->v[(i) * ((s)->sz + 1)])
74
6c1035f5 75/*----- Functions provided ------------------------------------------------*/
76
77/* --- @gfshare_create@ --- *
78 *
79 * Arguments: @gfshare *s@ = pointer to share context to initialize
3d64a35c 80 * @unsigned t@ = threshold for the system
6c1035f5 81 * @size_t sz@ = size of the secret
82 *
83 * Returns: ---
84 *
85 * Use: Initializes a sharing context.
86 */
87
3d64a35c 88extern void gfshare_create(gfshare */*s*/, unsigned /*t*/, size_t /*sz*/);
6c1035f5 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
101extern 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
5d4fee2a 107 * @const void *buf@ = pointer to the secret to share
6c1035f5 108 *
109 * Returns: ---
110 *
3d64a35c 111 * Use: Initializes a sharing context to be able to create shares.
6c1035f5 112 * The context structure is expected to be mostly filled in. In
5d4fee2a 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.
6c1035f5 117 */
118
5d4fee2a 119extern void gfshare_mkshares(gfshare */*s*/, grand */*r*/,
120 const void */*buf*/);
6c1035f5 121
3d64a35c 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 *
ea303d6c 128 * Returns: ---
3d64a35c 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
134extern void gfshare_get(gfshare */*s*/, unsigned /*x*/, void */*buf*/);
135
aec42286 136/* --- @gfshare_addedp@ --- *
137 *
138 * Arguments: @gfshare *s@ = pointer to sharing context
139 * @unsigned x@ = which share number to check
140 *
141 * Returns: Nonzero if share @x@ has been added already, zero if it
142 * hasn't.
143 */
144
145extern int gfshare_addedp(gfshare */*s*/, unsigned /*x*/);
146
6c1035f5 147/* --- @gfshare_add@ --- *
148 *
149 * Arguments: @gfshare *s@ = pointer to sharing context
150 * @unsigned x@ = which share number this is
3d64a35c 151 * @const void *y@ = the share value
6c1035f5 152 *
153 * Returns: Number of shares required before recovery may be performed.
154 *
155 * Use: Adds a share to the context. The context must have been
156 * initialized with the correct threshold @t@.
157 */
158
159extern unsigned gfshare_add(gfshare */*s*/,
3d64a35c 160 unsigned /*x*/, const void */*y*/);
6c1035f5 161
162/* --- @gfshare_combine@ --- *
163 *
164 * Arguments: @gfshare *s@ = pointer to share context
3d64a35c 165 * @void *buf@ = pointer to output buffer for the secret
6c1035f5 166 *
167 * Returns: ---
168 *
169 * Use: Reconstructs a secret, given enough shares.
170 */
171
3d64a35c 172extern void gfshare_combine(gfshare */*s*/, void */*buf*/);
6c1035f5 173
174/*----- That's all, folks -------------------------------------------------*/
175
176#ifdef __cplusplus
177 }
178#endif
179
180#endif