From: mdw Date: Sat, 17 Jun 2000 10:56:30 +0000 (+0000) Subject: Fast but nonstandard secret sharing system. X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/6c1035f55a83af8083af17432f2b4eb5afdb39dd Fast but nonstandard secret sharing system. --- diff --git a/gfshare-mktab.c b/gfshare-mktab.c new file mode 100644 index 0000000..709e619 --- /dev/null +++ b/gfshare-mktab.c @@ -0,0 +1,116 @@ +/* -*-c-*- + * + * $Id: gfshare-mktab.c,v 1.1 2000/06/17 10:56:30 mdw Exp $ + * + * Generate tables for %$\gf(2^8)$% multiplication + * + * (c) 2000 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: gfshare-mktab.c,v $ + * Revision 1.1 2000/06/17 10:56:30 mdw + * Fast but nonstandard secret sharing system. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include +#include + +#include + +/*----- Magic numbers -----------------------------------------------------*/ + +#define MOD 0x11d + +/*----- Main code ---------------------------------------------------------*/ + +int main(int argc, char *argv[]) +{ + octet log[256], alog[256]; + unsigned x; + unsigned i; + + x = 1; + for (i = 0; i < 255; i++) { + alog[i] = x; + log[x] = i; + x <<= 1; + if (x & 0x100) + x ^= MOD; + } + log[0] = 0; + alog[255] = 1; + + fputs("\ +/* -*-c-*-\n\ + *\n\ + * Log tables for secret sharing in %$\\gf(2^8)$% [generated]\n\ + */\n\ +\n\ +#ifndef GFSHARE_TAB_H\n\ +#define GFSHARE_TAB_H\n\ +\n\ +#define GFSHARE_LOG { \\\n\ + ", stdout); + + for (i = 0; i < 256; i++) { + printf("0x%02x", log[i]); + if (i == 255) + puts(" \\\n}\n"); + else if (i % 8 == 7) + fputs(", \\\n ", stdout); + else + fputs(", ", stdout); + } + + fputs("\ +#define GFSHARE_EXP { \\\n\ + ", stdout); + + for (i = 0; i < 510; i++) { + printf("0x%02x", alog[i % 255]); + if (i == 509) + puts(" \\\n}\n"); + else if (i % 8 == 7) + fputs(", \\\n ", stdout); + else + fputs(", ", stdout); + } + + /* --- Done --- */ + + fputs("#endif\n", stdout); + + if (fclose(stdout)) { + fprintf(stderr, "error writing data\n"); + exit(EXIT_FAILURE); + } + + return (0); +} + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/gfshare.c b/gfshare.c new file mode 100644 index 0000000..cdc00d2 --- /dev/null +++ b/gfshare.c @@ -0,0 +1,352 @@ +/* -*-c-*- + * + * $Id: gfshare.c,v 1.1 2000/06/17 10:56:30 mdw Exp $ + * + * Secret sharing over %$gf(2^8)$% + * + * (c) 2000 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: gfshare.c,v $ + * Revision 1.1 2000/06/17 10:56:30 mdw + * Fast but nonstandard secret sharing system. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include +#include +#include + +#include +#include + +#include "arena.h" +#include "gfshare.h" +#include "gfshare-tab.h" +#include "grand.h" + +/*----- Static variables --------------------------------------------------*/ + +static octet gflog[] = GFSHARE_LOG, gfexp[] = GFSHARE_EXP; + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @gfshare_create@ --- * + * + * Arguments: @gfshare *s@ = pointer to share context to initialize + * @unsigned t, n@ = threshold parameters for the system + * @size_t sz@ = size of the secret + * + * Returns: --- + * + * Use: Initializes a sharing context. + */ + +void gfshare_create(gfshare *s, unsigned t, unsigned n, size_t sz) +{ + s->t = t; + s->n = n; + s->i = 0; + s->sz = sz; + s->s = 0; + s->v = 0; +} + +/* --- @gfshare_destroy@ --- * + * + * Arguments: @gfshare *s@ = pointer to share context to destroy + * + * Returns: --- + * + * Use: Disposes of a sharing context. The allocations for the + * individual shares and the vector @v@ are freed; the secret is + * left alone. + */ + +void gfshare_destroy(gfshare *s) +{ + unsigned n; + unsigned i; + + /* --- Dispose of the share vector --- */ + + if (s->v) { + if (s->i) + n = s->i; + else if (s->n) + n = s->n; + else + n = s->t; + for (i = 0; i < n; i++) { + if (s->v[i].y) + XS_FREE(s->v[i].y); + } + xfree(s->v); + } +} + +/* --- @gfshare_mkshares@ --- * + * + * Arguments: @gfshare *s@ = pointer to share context to fill in + * @grand *r@ = pointer to random number source + * + * Returns: --- + * + * Use: Generates @c->n@ secret shares, such that any @c->t@ of them + * may be used to recover the secret. + * + * The context structure is expected to be mostly filled in. In + * particular, @t@, @n@, @ssz@ and @s@ must be initialized. If + * @v@ is zero, a vector of appropriate size is allocated. You + * should use the macro @GFSHARE_INIT@ or @gfshare_create@ to + * construct sharing contexts. + */ + +void gfshare_mkshares(gfshare *s, grand *r) +{ + octet *v; + unsigned i; + + /* --- Construct the coefficients --- */ + + v = XS_ALLOC(s->sz * s->t); + r->ops->fill(r, v, s->sz * (s->t - 1)); + memcpy(v + s->sz * (s->t - 1), s->s, s->sz); + + + /* --- Construct the shares --- */ + + if (!s->v) + s->v = xmalloc(s->n * sizeof(gfshare_pt)); + + for (i = 0; i < s->n; i++) { + unsigned j; + const octet *p = v; + unsigned ilog = gflog[i + 1]; + + /* --- Evaluate the polynomial at %$x = i + 1$% --- */ + + s->v[i].y = XS_ALLOC(s->sz); + memcpy(s->v[i].y, p, s->sz); + p += s->sz; + for (j = 1; j < s->t; j++) { + octet *q = s->v[i].y; + unsigned k; + for (k = 0; k < s->sz; k++) { + unsigned qq = *q; + if (qq) + qq = gfexp[ilog + gflog[qq]]; + *q++ = qq ^ *p++; + } + } + s->v[i].x = i; + } + + /* --- Dispose of various bits of old rubbish --- */ + + XS_FREE(v); +} + +/* --- @gfshare_add@ --- * + * + * Arguments: @gfshare *s@ = pointer to sharing context + * @unsigned x@ = which share number this is + * @const octet *y@ = the share value + * + * Returns: Number of shares required before recovery may be performed. + * + * Use: Adds a share to the context. The context must have been + * initialized with the correct threshold @t@. + */ + +unsigned gfshare_add(gfshare *s, unsigned x, const octet *y) +{ + /* --- If no vector has been allocated, create one --- */ + + if (!s->v) { + s->v = xmalloc(s->t * sizeof(gfshare_pt)); + s->i = 0; + } + + assert(((void)"Share context is full", s->i < s->t)); + + /* --- Store the share in the vector --- */ + + s->v[s->i].x = x; + s->v[s->i].y = XS_ALLOC(s->sz); + memcpy(s->v[s->i].y, y, s->sz); + s->i++; + + /* --- Done --- */ + + return (s->t - s->i); +} + +/* --- @gfshare_combine@ --- * + * + * Arguments: @gfshare *s@ = pointer to share context + * @octet *buf@ = pointer to output buffer for the secret + * + * Returns: --- + * + * Use: Reconstructs a secret, given enough shares. + */ + +void gfshare_combine(gfshare *s, octet *buf) +{ + unsigned i, j; + octet *v; + + /* --- Sanity checking --- */ + + assert(((void)"Not enough shares yet", s->i == s->t)); + + /* --- Precomputation of coefficients --- */ + + v = XS_ALLOC(s->t); + + for (i = 0; i < s->t; i++) { + unsigned c = 0, ci = 0; + for (j = 0; j < s->t; j++) { + if (i == j) + continue; + c += gflog[s->v[j].x + 1]; + if (c >= 0xff) + c -= 0xff; + ci += gflog[(s->v[i].x + 1) ^ (s->v[j].x + 1)]; + if (ci >= 0xff) + ci -= 0xff; + } + if (ci > c) + c += 0xff; + c -= ci; + v[i] = c; + } + + /* --- Grind through the shares --- */ + + for (i = 0; i < s->sz; i++) { + unsigned x = 0; + for (j = 0; j < s->t; j++) { + if (s->v[j].y[i]) + x ^= gfexp[v[j] + gflog[s->v[j].y[i]]]; + } + buf[i] = x; + } + + XS_FREE(v); +} + +/*----- Test rig ----------------------------------------------------------*/ + +#ifdef TEST_RIG + +#include "fibrand.h" + +static int verify(grand *r) +{ + unsigned n = r->ops->range(r, 16) + 8; + unsigned t = r->ops->range(r, n - 1) + 1; + unsigned len = r->ops->range(r, 32) + 1; + + gfshare_pt *v = xmalloc(t * sizeof(gfshare_pt)); + unsigned *p = xmalloc(n * sizeof(unsigned)); + octet *sec = xmalloc(len), *sbuf = xmalloc(len); + gfshare s; + unsigned i; + + int ok = 1; + + for (i = 0; i < n; i++) + p[i] = i; + for (i = 0; i < t; i++) { + unsigned long j = r->ops->range(r, n - i); + unsigned x = p[i]; + p[i] = p[i + j]; + p[i + j] = x; + } + + r->ops->fill(r, sec, len); + + gfshare_create(&s, t, n, len); + s.s = sec; + + gfshare_mkshares(&s, r); + for (i = 0; i < t; i++) { + v[i].x = s.v[p[i]].x; + v[i].y = xmalloc(len); + memcpy(v[i].y, s.v[p[i]].y, len); + } + gfshare_destroy(&s); + + gfshare_create(&s, t, n, len); + for (i = 0; i < t; i++) { + gfshare_add(&s, v[i].x, v[i].y); + } + gfshare_combine(&s, sbuf); + gfshare_destroy(&s); + + if (memcmp(sec, sbuf, len) != 0) { + ok = 0; + fprintf(stderr, "\nbad recombination of shares\n"); + }; + + xfree(sec); + xfree(sbuf); + + for (i = 0; i < t; i++) + xfree(v[i].y); + xfree(v); + xfree(p); + + return (ok); +} + +int main(void) +{ + grand *r = fibrand_create(0); + unsigned i; + int ok = 1; + + fputs("gfshare: ", stdout); + for (i = 0; i < 40; i++) { + if (!verify(r)) + ok = 0; + fputc('.', stdout); + fflush(stdout); + } + + if (ok) + fputs(" ok\n", stdout); + else + fputs(" failed\n", stdout); + return (ok ? EXIT_SUCCESS : EXIT_FAILURE); +} + +#endif + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/gfshare.h b/gfshare.h new file mode 100644 index 0000000..b0aca05 --- /dev/null +++ b/gfshare.h @@ -0,0 +1,154 @@ +/* -*-c-*- + * + * $Id: gfshare.h,v 1.1 2000/06/17 10:56:30 mdw Exp $ + * + * Secret sharing over %$\gf(2^8)$% + * + * (c) 2000 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: gfshare.h,v $ + * Revision 1.1 2000/06/17 10:56:30 mdw + * Fast but nonstandard secret sharing system. + * + */ + +#ifndef CATACOMB_GFSHARE_H +#define CATACOMB_GFSHARE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#include + +#ifndef CATACOMB_GRAND_H +# include "grand.h" +#endif + +/*----- Data structures ---------------------------------------------------*/ + +/* --- A secret sharing context --- */ + +typedef struct gfshare_pt { + octet x; /* %$x$%-coordinate of the share */ + octet *y; /* Pointer to share payload */ +} gfshare_pt; + +typedef struct gfshare { + unsigned t; /* Threshold */ + unsigned n; /* The number of shares to make */ + unsigned i; /* Next free slot in vector */ + size_t sz; /* Size of the secret and shares */ + octet *s; /* The secret */ + gfshare_pt *v; /* Vector of share information */ +} gfshare; + +#define GFSHARE_INIT(t, n, sz) { t, n, 0, sz, 0, 0 } + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @gfshare_create@ --- * + * + * Arguments: @gfshare *s@ = pointer to share context to initialize + * @unsigned t, n@ = threshold parameters for the system + * @size_t sz@ = size of the secret + * + * Returns: --- + * + * Use: Initializes a sharing context. + */ + +extern void gfshare_create(gfshare */*s*/, unsigned /*t*/, unsigned /*n*/, + size_t /*sz*/); + +/* --- @gfshare_destroy@ --- * + * + * Arguments: @gfshare *s@ = pointer to share context to destroy + * + * Returns: --- + * + * Use: Disposes of a sharing context. The allocations for the + * individual shares and the vector @v@ are freed; the secret is + * left alone. + */ + +extern void gfshare_destroy(gfshare */*s*/); + +/* --- @gfshare_mkshares@ --- * + * + * Arguments: @gfshare *s@ = pointer to share context to fill in + * @grand *r@ = pointer to random number source + * + * Returns: --- + * + * Use: Generates @c->n@ secret shares, such that any @c->t@ of them + * may be used to recover the secret. + * + * The context structure is expected to be mostly filled in. In + * particular, @t@, @n@, @ssz@ and @s@ must be initialized. If + * @v@ is zero, a vector of appropriate size is allocated. You + * should use the macro @GFSHARE_INIT@ or @gfshare_create@ to + * construct sharing contexts. + */ + +extern void gfshare_mkshares(gfshare */*s*/, grand */*r*/); + +/* --- @gfshare_add@ --- * + * + * Arguments: @gfshare *s@ = pointer to sharing context + * @unsigned x@ = which share number this is + * @const octet *y@ = the share value + * + * Returns: Number of shares required before recovery may be performed. + * + * Use: Adds a share to the context. The context must have been + * initialized with the correct threshold @t@. + */ + +extern unsigned gfshare_add(gfshare */*s*/, + unsigned /*x*/, const octet */*y*/); + +/* --- @gfshare_combine@ --- * + * + * Arguments: @gfshare *s@ = pointer to share context + * @octet *buf@ = pointer to output buffer for the secret + * + * Returns: --- + * + * Use: Reconstructs a secret, given enough shares. + */ + +extern void gfshare_combine(gfshare */*s*/, octet */*buf*/); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif