3 * Shamir's secret sharing
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Notes on the sharing system ---------------------------------------*
30 * Shamir's secret-sharing system is based on polynomial interpolation modulo
31 * a prime number. It is `perfect' in that fewer participants than the
32 * threshold can derive no information about the secret by pooling their
33 * shares, and `ideal' in that the shares are the same size as the secret.
35 * This implementation stays close to the definition, in order to support
36 * other schemes for (e.g.) threshold cryptography. It is, however, rather
40 #ifndef CATACOMB_SHARE_H
41 #define CATACOMB_SHARE_H
47 /*----- Header files ------------------------------------------------------*/
49 #ifndef CATACOMB_GRAND_H
57 /*----- Data structures ---------------------------------------------------*/
59 /* --- A secret sharing context --- */
61 typedef struct share_pt
{
62 unsigned x
; /* Index of this share */
63 mp
*y
; /* Payload of this share */
66 typedef struct share
{
67 unsigned t
; /* Threshold */
68 unsigned i
; /* Next free slot in the vector */
69 mp
*p
; /* Modulus for arithmetic */
70 share_pt
*v
; /* Vector of share information */
73 #define SHARE_INIT(t) { t, 0, 0, 0 }
75 /*----- Functions provided ------------------------------------------------*/
77 /* --- @share_create@ --- *
79 * Arguments: @share *s@ = pointer to share context to initialize
80 * @unsigned t@ = threshold for the system
84 * Use: Initializes a sharing context.
87 extern void share_create(share */
*s*/
, unsigned /*t*/);
89 /* --- @share_destroy@ --- *
91 * Arguments: @share *s@ = pointer to share context to destroy
95 * Use: Disposes of a sharing context. All memory is freed, all
96 * integers are dropped.
99 extern void share_destroy(share */
*s*/
);
101 /* --- @share_mkshares@ --- *
103 * Arguments: @share *s@ = pointer to share context to fill in
104 * @grand *r@ = pointer to random number source
105 * @mp *n@ = the secret to share
109 * Use: Initializes a sharing context to be able to create shares.
110 * The context structure is expected to be mostly filled in. In
111 * particular, @t@ must be initialized. If @p@ is zero, a prime
112 * number of appropriate size is generated automatically. If
113 * @v@ is zero, a vector of appropriate size is allocated. You
114 * should use the macro @SHARE_INIT@ or @share_create@ to
115 * construct sharing contexts.
118 extern void share_mkshares(share */
*s*/
, grand */
*r*/
, mp */
*n*/
);
120 /* --- @share_get@ --- *
122 * Arguments: @share *s@ = pointer to share conext
123 * @mp *d@ = destination for the share
124 * @unsigned x@ = share index to fetch
126 * Returns: The share, as requested.
128 * Use: Extracts a share from the system. You may extract @MPW_MAX@
129 * shares, or @s->p@ shares from the system, whichever is
130 * smaller. Shares are indexed from 0.
133 extern mp
*share_get(share */
*s*/
, mp */
*d*/
, unsigned /*x*/);
135 /* --- @share_addedp@ --- *
137 * Arguments: @share *s@ = pointer to sharing context
138 * @unsigned x@ = which share number to check
140 * Returns: Nonzero if share @x@ has been added already, zero if it
144 extern int share_addedp(share */
*s*/
, unsigned /*x*/);
146 /* --- @share_add@ --- *
148 * Arguments: @share *s@ = pointer to sharing context
149 * @unsigned x@ = which share number this is
150 * @mp *y@ = the share value
152 * Returns: Number of shares required before recovery may be performed.
154 * Use: Adds a share to the context. The context must have been
155 * initialized with the correct prime @p@ and threshold @t@.
158 extern unsigned share_add(share */
*s*/
, unsigned /*x*/, mp */
*y*/
);
160 /* --- @share_combine@ --- *
162 * Arguments: @share *s@ = pointer to share context
164 * Returns: The secret, as a multiprecision integer.
166 * Use: Reconstructs a secret, given enough shares.
169 extern mp
*share_combine(share */
*s*/
);
171 /*----- That's all, folks -------------------------------------------------*/