| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: gfshare.h,v 1.6 2000/12/06 20:30:10 mdw Exp $ |
| 4 | * |
| 5 | * Secret sharing over %$\gf{2^8}$% |
| 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: gfshare.h,v $ |
| 33 | * Revision 1.6 2000/12/06 20:30:10 mdw |
| 34 | * Change secret sharing interface: present the secret at share |
| 35 | * construction time. |
| 36 | * |
| 37 | * Revision 1.5 2000/06/24 19:11:47 mdw |
| 38 | * Fix daft error in the comment for @gfshare_get@. |
| 39 | * |
| 40 | * Revision 1.4 2000/06/24 18:29:05 mdw |
| 41 | * Interface change: allow shares to be extracted from a context on demand, |
| 42 | * rather than building them all up-front. |
| 43 | * |
| 44 | * Revision 1.3 2000/06/18 23:12:15 mdw |
| 45 | * Change typesetting of Galois Field names. |
| 46 | * |
| 47 | * Revision 1.2 2000/06/17 11:05:27 mdw |
| 48 | * Add a commentary on the system. |
| 49 | * |
| 50 | * Revision 1.1 2000/06/17 10:56:30 mdw |
| 51 | * Fast but nonstandard secret sharing system. |
| 52 | * |
| 53 | */ |
| 54 | |
| 55 | /*----- Notes on the system -----------------------------------------------* |
| 56 | * |
| 57 | * This uses a variant of Shamir's secret sharing system. Shamir's original |
| 58 | * system used polynomials modulo a large prime. This implementation instead |
| 59 | * uses the field %$\gf{2^8}$%, represented by |
| 60 | * |
| 61 | * %$\gf{2}[x]/(x^8 + x^4 + x^3 + x^2 + 1)$% |
| 62 | * |
| 63 | * and shares each byte of the secret independently. It is therefore limited |
| 64 | * to 255 players, although this probably isn't a serious limitation in |
| 65 | * practice. |
| 66 | * |
| 67 | * Share creation and reconstruction is extremely efficient. Contrast the |
| 68 | * performance of the straightforward implementation based on multiprecision |
| 69 | * arithmetic. |
| 70 | */ |
| 71 | |
| 72 | #ifndef CATACOMB_GFSHARE_H |
| 73 | #define CATACOMB_GFSHARE_H |
| 74 | |
| 75 | #ifdef __cplusplus |
| 76 | extern "C" { |
| 77 | #endif |
| 78 | |
| 79 | /*----- Header files ------------------------------------------------------*/ |
| 80 | |
| 81 | #include <mLib/bits.h> |
| 82 | |
| 83 | #ifndef CATACOMB_GRAND_H |
| 84 | # include "grand.h" |
| 85 | #endif |
| 86 | |
| 87 | /*----- Data structures ---------------------------------------------------*/ |
| 88 | |
| 89 | /* --- A secret sharing context --- */ |
| 90 | |
| 91 | typedef struct gfshare { |
| 92 | unsigned t; /* Threshold */ |
| 93 | unsigned i; /* Next free slot in vector */ |
| 94 | size_t sz; /* Size of the secret and shares */ |
| 95 | octet *v; /* Vector of share information */ |
| 96 | } gfshare; |
| 97 | |
| 98 | #define GFSHARE_INIT(t, sz) { t, 0, sz, 0 } |
| 99 | |
| 100 | /*----- Functions provided ------------------------------------------------*/ |
| 101 | |
| 102 | /* --- @gfshare_create@ --- * |
| 103 | * |
| 104 | * Arguments: @gfshare *s@ = pointer to share context to initialize |
| 105 | * @unsigned t@ = threshold for the system |
| 106 | * @size_t sz@ = size of the secret |
| 107 | * |
| 108 | * Returns: --- |
| 109 | * |
| 110 | * Use: Initializes a sharing context. |
| 111 | */ |
| 112 | |
| 113 | extern void gfshare_create(gfshare */*s*/, unsigned /*t*/, size_t /*sz*/); |
| 114 | |
| 115 | /* --- @gfshare_destroy@ --- * |
| 116 | * |
| 117 | * Arguments: @gfshare *s@ = pointer to share context to destroy |
| 118 | * |
| 119 | * Returns: --- |
| 120 | * |
| 121 | * Use: Disposes of a sharing context. The allocations for the |
| 122 | * individual shares and the vector @v@ are freed; the secret is |
| 123 | * left alone. |
| 124 | */ |
| 125 | |
| 126 | extern void gfshare_destroy(gfshare */*s*/); |
| 127 | |
| 128 | /* --- @gfshare_mkshares@ --- * |
| 129 | * |
| 130 | * Arguments: @gfshare *s@ = pointer to share context to fill in |
| 131 | * @grand *r@ = pointer to random number source |
| 132 | * @const void *buf@ = pointer to the secret to share |
| 133 | * |
| 134 | * Returns: --- |
| 135 | * |
| 136 | * Use: Initializes a sharing context to be able to create shares. |
| 137 | * The context structure is expected to be mostly filled in. In |
| 138 | * particular, @t@ must be initialized. If @v@ is zero, a |
| 139 | * vector of appropriate size is allocated. You should use the |
| 140 | * macro @GFSHARE_INIT@ or @gfshare_create@ to construct sharing |
| 141 | * contexts. |
| 142 | */ |
| 143 | |
| 144 | extern void gfshare_mkshares(gfshare */*s*/, grand */*r*/, |
| 145 | const void */*buf*/); |
| 146 | |
| 147 | /* --- @gfshare_get@ --- * |
| 148 | * |
| 149 | * Arguments: @gfshare *s@ = pointer to share conext |
| 150 | * @unsigned x@ = share index to fetch |
| 151 | * @void *buf@ = pointer to output buffer |
| 152 | * |
| 153 | * Returns: --- |
| 154 | * |
| 155 | * Use: Extracts a share from the system. You may extract up to 255 |
| 156 | * shares from the system. Shares are indexed from 0. |
| 157 | */ |
| 158 | |
| 159 | extern void gfshare_get(gfshare */*s*/, unsigned /*x*/, void */*buf*/); |
| 160 | |
| 161 | /* --- @gfshare_add@ --- * |
| 162 | * |
| 163 | * Arguments: @gfshare *s@ = pointer to sharing context |
| 164 | * @unsigned x@ = which share number this is |
| 165 | * @const void *y@ = the share value |
| 166 | * |
| 167 | * Returns: Number of shares required before recovery may be performed. |
| 168 | * |
| 169 | * Use: Adds a share to the context. The context must have been |
| 170 | * initialized with the correct threshold @t@. |
| 171 | */ |
| 172 | |
| 173 | extern unsigned gfshare_add(gfshare */*s*/, |
| 174 | unsigned /*x*/, const void */*y*/); |
| 175 | |
| 176 | /* --- @gfshare_combine@ --- * |
| 177 | * |
| 178 | * Arguments: @gfshare *s@ = pointer to share context |
| 179 | * @void *buf@ = pointer to output buffer for the secret |
| 180 | * |
| 181 | * Returns: --- |
| 182 | * |
| 183 | * Use: Reconstructs a secret, given enough shares. |
| 184 | */ |
| 185 | |
| 186 | extern void gfshare_combine(gfshare */*s*/, void */*buf*/); |
| 187 | |
| 188 | /*----- That's all, folks -------------------------------------------------*/ |
| 189 | |
| 190 | #ifdef __cplusplus |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | #endif |