X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/1cf8e9fbb768ed0498d4534528419f31a4893975..3d64a35c9b151e739eb6a8712915810ea574eac8:/gfshare.c diff --git a/gfshare.c b/gfshare.c index a0b102d..0af17cc 100644 --- a/gfshare.c +++ b/gfshare.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: gfshare.c,v 1.3 2000/06/22 18:04:13 mdw Exp $ + * $Id: gfshare.c,v 1.4 2000/06/24 18:29:05 mdw Exp $ * * Secret sharing over %$\gf(2^8)$% * @@ -30,6 +30,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: gfshare.c,v $ + * Revision 1.4 2000/06/24 18:29:05 mdw + * Interface change: allow shares to be extracted from a context on demand, + * rather than building them all up-front. + * * Revision 1.3 2000/06/22 18:04:13 mdw * Improve secret reconstruction -- compute coefficients as needed rather * than making a big array of them. @@ -66,7 +70,7 @@ static octet gflog[] = GFSHARE_LOG, gfexp[] = GFSHARE_EXP; /* --- @gfshare_create@ --- * * * Arguments: @gfshare *s@ = pointer to share context to initialize - * @unsigned t, n@ = threshold parameters for the system + * @unsigned t@ = threshold for the system * @size_t sz@ = size of the secret * * Returns: --- @@ -74,10 +78,9 @@ static octet gflog[] = GFSHARE_LOG, gfexp[] = GFSHARE_EXP; * Use: Initializes a sharing context. */ -void gfshare_create(gfshare *s, unsigned t, unsigned n, size_t sz) +void gfshare_create(gfshare *s, unsigned t, size_t sz) { s->t = t; - s->n = n; s->i = 0; s->sz = sz; s->s = 0; @@ -97,24 +100,8 @@ void gfshare_create(gfshare *s, unsigned t, unsigned n, size_t sz) 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); - } + if (s->v) + XS_FREE(s->v); } /* --- @gfshare_mkshares@ --- * @@ -124,66 +111,61 @@ void gfshare_destroy(gfshare *s) * * Returns: --- * - * Use: Generates @c->n@ secret shares, such that any @c->t@ of them - * may be used to recover the secret. - * + * Use: Initializes a sharing context to be able to create shares. * 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. + * particular, @t@ 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]; + s->v = XS_ALLOC(s->sz * s->t); + r->ops->fill(r, s->v, s->sz * (s->t - 1)); + memcpy(s->v + s->sz * (s->t - 1), s->s, s->sz); +} - /* --- Evaluate the polynomial at %$x = i + 1$% --- */ +/* --- @gfshare_get@ --- * + * + * Arguments: @gfshare *s@ = pointer to share conext + * @unsigned x@ = share index to fetch + * @void *buf@ = pointer to output buffer + * + * Returns: The share, as requested. + * + * Use: Extracts a share from the system. You may extract up to 255 + * shares from the system. Shares are indexed from 0. + */ - 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++; - } +void gfshare_get(gfshare *s, unsigned x, void *buf) +{ + unsigned i; + const octet *p = s->v; + unsigned ilog = gflog[x + 1]; + + /* --- Evaluate the polynomial at %$x = i + 1$% --- */ + + memcpy(buf, p, s->sz); + p += s->sz; + + for (i = 1; i < s->t; i++) { + octet *q = buf; + 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 + * @const void *y@ = the share value * * Returns: Number of shares required before recovery may be performed. * @@ -191,12 +173,14 @@ void gfshare_mkshares(gfshare *s, grand *r) * initialized with the correct threshold @t@. */ -unsigned gfshare_add(gfshare *s, unsigned x, const octet *y) +unsigned gfshare_add(gfshare *s, unsigned x, const void *y) { + octet *p; + /* --- If no vector has been allocated, create one --- */ if (!s->v) { - s->v = xmalloc(s->t * sizeof(gfshare_pt)); + s->v = XS_ALLOC(s->t * (s->sz + 1)); s->i = 0; } @@ -204,9 +188,9 @@ unsigned gfshare_add(gfshare *s, unsigned x, const octet *y) /* --- 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); + p = s->v + s->i * (s->sz + 1); + *p++ = x + 1; + memcpy(p, y, s->sz); s->i++; /* --- Done --- */ @@ -217,16 +201,17 @@ 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 + * @void *buf@ = pointer to output buffer for the secret * * Returns: --- * * Use: Reconstructs a secret, given enough shares. */ -void gfshare_combine(gfshare *s, octet *buf) +void gfshare_combine(gfshare *s, void *buf) { unsigned i, j; + unsigned xi, xj; /* --- Sanity checking --- */ @@ -237,17 +222,21 @@ void gfshare_combine(gfshare *s, octet *buf) memset(buf, 0, s->sz); for (i = 0; i < s->t; i++) { + octet *p = buf; + octet *q = s->v + i * (s->sz + 1); unsigned c = 0, ci = 0; /* --- Compute the magic coefficient --- */ + xi = *q++; for (j = 0; j < s->t; j++) { if (i == j) continue; - c += gflog[s->v[j].x + 1]; + xj = s->v[j * (s->sz + 1)]; + c += gflog[xj]; if (c >= 0xff) c -= 0xff; - ci += gflog[(s->v[i].x + 1) ^ (s->v[j].x + 1)]; + ci += gflog[xi ^ xj]; if (ci >= 0xff) ci -= 0xff; } @@ -257,9 +246,11 @@ void gfshare_combine(gfshare *s, octet *buf) /* --- Work out another layer of the secret --- */ + p = buf; for (j = 0; j < s->sz; j++) { - if (s->v[i].y[j]) - buf[j] ^= gfexp[c + gflog[s->v[i].y[j]]]; + if (*q) + *p ^= gfexp[c + gflog[*q]]; + p++, q++; } } } @@ -276,7 +267,7 @@ static int verify(grand *r) 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)); + octet *v = xmalloc(t * len); unsigned *p = xmalloc(n * sizeof(unsigned)); octet *sec = xmalloc(len), *sbuf = xmalloc(len); gfshare s; @@ -295,21 +286,17 @@ static int verify(grand *r) r->ops->fill(r, sec, len); - gfshare_create(&s, t, n, len); + gfshare_create(&s, t, 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); - } + for (i = 0; i < t; i++) + gfshare_get(&s, p[i], v + (i * 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_create(&s, t, len); + for (i = 0; i < t; i++) + gfshare_add(&s, p[i], v + (i * len)); gfshare_combine(&s, sbuf); gfshare_destroy(&s); @@ -321,8 +308,6 @@ static int verify(grand *r) xfree(sec); xfree(sbuf); - for (i = 0; i < t; i++) - xfree(v[i].y); xfree(v); xfree(p);