71dfe576 |
1 | /* -*-c-*- |
2 | * |
3d64a35c |
3 | * $Id: share.h,v 1.2 2000/06/24 18:29:05 mdw Exp $ |
71dfe576 |
4 | * |
5 | * Shamir's secret sharing |
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: share.h,v $ |
3d64a35c |
33 | * Revision 1.2 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 | * |
71dfe576 |
37 | * Revision 1.1 2000/06/17 12:09:38 mdw |
38 | * Shamir's secret sharing system. |
39 | * |
40 | */ |
41 | |
42 | /*----- Notes on the sharing system ---------------------------------------* |
43 | * |
44 | * Shamir's secret-sharing system is based on polynomial interpolation modulo |
45 | * a prime number. It is `perfect' in that fewer participants than the |
46 | * threshold can derive no information about the secret by pooling their |
47 | * shares, and `ideal' in that the shares are the same size as the secret. |
48 | * |
49 | * This implementation stays close to the definition, in order to support |
50 | * other schemes for (e.g.) threshold cryptography. It is, however, rather |
51 | * slow. |
52 | */ |
53 | |
54 | #ifndef CATACOMB_SHARE_H |
55 | #define CATACOMB_SHARE_H |
56 | |
57 | #ifdef __cplusplus |
58 | extern "C" { |
59 | #endif |
60 | |
61 | /*----- Header files ------------------------------------------------------*/ |
62 | |
63 | #ifndef CATACOMB_GRAND_H |
64 | # include "grand.h" |
65 | #endif |
66 | |
67 | #ifndef CATACOMB_MP_H |
68 | # include "mp.h" |
69 | #endif |
70 | |
71 | /*----- Data structures ---------------------------------------------------*/ |
72 | |
73 | /* --- A secret sharing context --- */ |
74 | |
75 | typedef struct share_pt { |
76 | unsigned x; /* Index of this share */ |
77 | mp *y; /* Payload of this share */ |
78 | } share_pt; |
79 | |
80 | typedef struct share { |
81 | unsigned t; /* Threshold */ |
71dfe576 |
82 | unsigned i; /* Next free slot in the vector */ |
83 | mp *s; /* The secret */ |
84 | mp *p; /* Modulus for arithmetic */ |
85 | share_pt *v; /* Vector of share information */ |
86 | } share; |
87 | |
3d64a35c |
88 | #define SHARE_INIT(t) { t, 0, 0, 0, 0 } |
71dfe576 |
89 | |
90 | /*----- Functions provided ------------------------------------------------*/ |
91 | |
92 | /* --- @share_create@ --- * |
93 | * |
94 | * Arguments: @share *s@ = pointer to share context to initialize |
3d64a35c |
95 | * @unsigned t@ = threshold for the system |
71dfe576 |
96 | * |
97 | * Returns: --- |
98 | * |
99 | * Use: Initializes a sharing context. |
100 | */ |
101 | |
3d64a35c |
102 | extern void share_create(share */*s*/, unsigned /*t*/); |
71dfe576 |
103 | |
104 | /* --- @share_destroy@ --- * |
105 | * |
106 | * Arguments: @share *s@ = pointer to share context to destroy |
107 | * |
108 | * Returns: --- |
109 | * |
110 | * Use: Disposes of a sharing context. All memory is freed, all |
111 | * integers are dropped. |
112 | */ |
113 | |
114 | extern void share_destroy(share */*s*/); |
115 | |
116 | /* --- @share_mkshares@ --- * |
117 | * |
118 | * Arguments: @share *s@ = pointer to share context to fill in |
119 | * @grand *r@ = pointer to random number source |
120 | * |
121 | * Returns: --- |
122 | * |
3d64a35c |
123 | * Use: Initializes a sharing context to be able to create shares. |
71dfe576 |
124 | * The context structure is expected to be mostly filled in. In |
3d64a35c |
125 | * particular, @t@ and @s@ must be initialized. If @p@ is zero, |
126 | * a prime number of appropriate size is generated |
71dfe576 |
127 | * automatically. If @v@ is zero, a vector of appropriate size |
128 | * is allocated. You should use the macro @SHARE_INIT@ or |
129 | * @share_create@ to construct sharing contexts. |
130 | */ |
131 | |
132 | extern void share_mkshares(share */*s*/, grand */*r*/); |
133 | |
3d64a35c |
134 | /* --- @share_get@ --- * |
135 | * |
136 | * Arguments: @share *s@ = pointer to share conext |
137 | * @mp *d@ = destination for the share |
138 | * @unsigned x@ = share index to fetch |
139 | * |
140 | * Returns: The share, as requested. |
141 | * |
142 | * Use: Extracts a share from the system. You may extract @MPW_MAX@ |
143 | * shares, or @s->p@ shares from the system, whichever is |
144 | * smaller. Shares are indexed from 0. |
145 | */ |
146 | |
147 | extern mp *share_get(share */*s*/, mp */*d*/, unsigned /*x*/); |
148 | |
71dfe576 |
149 | /* --- @share_add@ --- * |
150 | * |
151 | * Arguments: @share *s@ = pointer to sharing context |
152 | * @unsigned x@ = which share number this is |
153 | * @mp *y@ = the share value |
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 prime @p@ and threshold @t@. |
159 | */ |
160 | |
161 | extern unsigned share_add(share */*s*/, unsigned /*x*/, mp */*y*/); |
162 | |
163 | /* --- @share_combine@ --- * |
164 | * |
165 | * Arguments: @share *s@ = pointer to share context |
166 | * |
167 | * Returns: The secret, as a multiprecision integer. |
168 | * |
169 | * Use: Reconstructs a secret, given enough shares. |
170 | */ |
171 | |
172 | extern mp *share_combine(share */*s*/); |
173 | |
174 | /*----- That's all, folks -------------------------------------------------*/ |
175 | |
176 | #ifdef __cplusplus |
177 | } |
178 | #endif |
179 | |
180 | #endif |