6c1035f5 |
1 | /* -*-c-*- |
2 | * |
aec42286 |
3 | * $Id$ |
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 | |
73fb671f |
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 |
4d47e157 |
34 | * uses the field %$\gf{2^8}$%, represented by |
73fb671f |
35 | * |
4d47e157 |
36 | * %$\gf{2}[x]/(x^8 + x^4 + x^3 + x^2 + 1)$% |
73fb671f |
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 | |
6c1035f5 |
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 | |
6c1035f5 |
66 | typedef struct gfshare { |
67 | unsigned t; /* Threshold */ |
6c1035f5 |
68 | unsigned i; /* Next free slot in vector */ |
69 | size_t sz; /* Size of the secret and shares */ |
3d64a35c |
70 | octet *v; /* Vector of share information */ |
6c1035f5 |
71 | } gfshare; |
72 | |
5d4fee2a |
73 | #define GFSHARE_INIT(t, sz) { t, 0, sz, 0 } |
6c1035f5 |
74 | |
aec42286 |
75 | #define GFSHARE_INDEX(s, i) ((s)->v[(i) * ((s)->sz + 1)]) |
76 | |
6c1035f5 |
77 | /*----- Functions provided ------------------------------------------------*/ |
78 | |
79 | /* --- @gfshare_create@ --- * |
80 | * |
81 | * Arguments: @gfshare *s@ = pointer to share context to initialize |
3d64a35c |
82 | * @unsigned t@ = threshold for the system |
6c1035f5 |
83 | * @size_t sz@ = size of the secret |
84 | * |
85 | * Returns: --- |
86 | * |
87 | * Use: Initializes a sharing context. |
88 | */ |
89 | |
3d64a35c |
90 | extern void gfshare_create(gfshare */*s*/, unsigned /*t*/, size_t /*sz*/); |
6c1035f5 |
91 | |
92 | /* --- @gfshare_destroy@ --- * |
93 | * |
94 | * Arguments: @gfshare *s@ = pointer to share context to destroy |
95 | * |
96 | * Returns: --- |
97 | * |
98 | * Use: Disposes of a sharing context. The allocations for the |
99 | * individual shares and the vector @v@ are freed; the secret is |
100 | * left alone. |
101 | */ |
102 | |
103 | extern void gfshare_destroy(gfshare */*s*/); |
104 | |
105 | /* --- @gfshare_mkshares@ --- * |
106 | * |
107 | * Arguments: @gfshare *s@ = pointer to share context to fill in |
108 | * @grand *r@ = pointer to random number source |
5d4fee2a |
109 | * @const void *buf@ = pointer to the secret to share |
6c1035f5 |
110 | * |
111 | * Returns: --- |
112 | * |
3d64a35c |
113 | * Use: Initializes a sharing context to be able to create shares. |
6c1035f5 |
114 | * The context structure is expected to be mostly filled in. In |
5d4fee2a |
115 | * particular, @t@ must be initialized. If @v@ is zero, a |
116 | * vector of appropriate size is allocated. You should use the |
117 | * macro @GFSHARE_INIT@ or @gfshare_create@ to construct sharing |
118 | * contexts. |
6c1035f5 |
119 | */ |
120 | |
5d4fee2a |
121 | extern void gfshare_mkshares(gfshare */*s*/, grand */*r*/, |
122 | const void */*buf*/); |
6c1035f5 |
123 | |
3d64a35c |
124 | /* --- @gfshare_get@ --- * |
125 | * |
126 | * Arguments: @gfshare *s@ = pointer to share conext |
127 | * @unsigned x@ = share index to fetch |
128 | * @void *buf@ = pointer to output buffer |
129 | * |
ea303d6c |
130 | * Returns: --- |
3d64a35c |
131 | * |
132 | * Use: Extracts a share from the system. You may extract up to 255 |
133 | * shares from the system. Shares are indexed from 0. |
134 | */ |
135 | |
136 | extern void gfshare_get(gfshare */*s*/, unsigned /*x*/, void */*buf*/); |
137 | |
aec42286 |
138 | /* --- @gfshare_addedp@ --- * |
139 | * |
140 | * Arguments: @gfshare *s@ = pointer to sharing context |
141 | * @unsigned x@ = which share number to check |
142 | * |
143 | * Returns: Nonzero if share @x@ has been added already, zero if it |
144 | * hasn't. |
145 | */ |
146 | |
147 | extern int gfshare_addedp(gfshare */*s*/, unsigned /*x*/); |
148 | |
6c1035f5 |
149 | /* --- @gfshare_add@ --- * |
150 | * |
151 | * Arguments: @gfshare *s@ = pointer to sharing context |
152 | * @unsigned x@ = which share number this is |
3d64a35c |
153 | * @const void *y@ = the share value |
6c1035f5 |
154 | * |
155 | * Returns: Number of shares required before recovery may be performed. |
156 | * |
157 | * Use: Adds a share to the context. The context must have been |
158 | * initialized with the correct threshold @t@. |
159 | */ |
160 | |
161 | extern unsigned gfshare_add(gfshare */*s*/, |
3d64a35c |
162 | unsigned /*x*/, const void */*y*/); |
6c1035f5 |
163 | |
164 | /* --- @gfshare_combine@ --- * |
165 | * |
166 | * Arguments: @gfshare *s@ = pointer to share context |
3d64a35c |
167 | * @void *buf@ = pointer to output buffer for the secret |
6c1035f5 |
168 | * |
169 | * Returns: --- |
170 | * |
171 | * Use: Reconstructs a secret, given enough shares. |
172 | */ |
173 | |
3d64a35c |
174 | extern void gfshare_combine(gfshare */*s*/, void */*buf*/); |
6c1035f5 |
175 | |
176 | /*----- That's all, folks -------------------------------------------------*/ |
177 | |
178 | #ifdef __cplusplus |
179 | } |
180 | #endif |
181 | |
182 | #endif |