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