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