X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/34e4f738bcba58e6d8c4cabbb0b3232a65b42a9d..5d01b1b9514a258c5a3c201e944f676cb2c467f0:/key-binary.c diff --git a/key-binary.c b/key-binary.c index 82a1ecb..c1ef53b 100644 --- a/key-binary.c +++ b/key-binary.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: key-binary.c,v 1.5 2004/04/01 12:50:09 mdw Exp $ + * $Id$ * * Key binary encoding * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,43 +15,18 @@ * 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: key-binary.c,v $ - * Revision 1.5 2004/04/01 12:50:09 mdw - * Add cyclic group abstraction, with test code. Separate off exponentation - * functions for better static linking. Fix a buttload of bugs on the way. - * Generally ensure that negative exponents do inversion correctly. Add - * table of standard prime-field subgroups. (Binary field subgroups are - * currently unimplemented but easy to add if anyone ever finds a good one.) - * - * Revision 1.4 2004/03/28 01:58:47 mdw - * Generate, store and retreive elliptic curve keys. - * - * Revision 1.3 2001/02/03 11:57:00 mdw - * Track mLib change: symbols no longer need to include a terminating - * null. - * - * Revision 1.2 2000/06/17 11:25:20 mdw - * Use secure memory interface from MP library. - * - * Revision 1.1 2000/02/12 18:21:02 mdw - * Overhaul of key management (again). - * - */ - /*----- Header files ------------------------------------------------------*/ #include @@ -72,17 +47,17 @@ * * Arguments: @const void *p@ = pointer to buffer to read * @size_t sz@ = size of the buffer - * @key_data *k@ = pointer to key data block to write to * - * Returns: Zero if everything worked, nonzero otherwise. + * Returns: The newly-read key data, or null if it failed. * * Use: Decodes a binary representation of a key. */ -int key_decode(const void *p, size_t sz, key_data *k) +key_data *key_decode(const void *p, size_t sz) { const octet *q = p; size_t psz; + key_data *kd; unsigned e; /* --- Parse the header information --- * @@ -95,8 +70,7 @@ int key_decode(const void *p, size_t sz, key_data *k) e = LOAD16(q); psz = LOAD16(q + 2); if (psz + 4 > sz) - return (-1); - k->e = e; + return (0); /* --- Now decide what to do --- */ @@ -106,51 +80,51 @@ int key_decode(const void *p, size_t sz, key_data *k) case KENC_BINARY: case KENC_ENCRYPT: - k->u.k.k = sub_alloc(psz); - memcpy(k->u.k.k, q + 4, psz); - k->u.k.sz = psz; + kd = key_newbinary(e, q + 4, psz); break; /* --- Multiprecision integer data --- */ case KENC_MP: - k->u.m = mp_loadb(k->e & KF_BURN ? MP_NEWSEC : MP_NEW, q + 4, psz); + kd = key_newmp(e, mp_loadb(e & KF_BURN ? MP_NEWSEC : MP_NEW, + q + 4, psz)); break; /* --- String data --- */ case KENC_STRING: - k->u.p = xmalloc(sz + 1); - memcpy(k->u.p, q + 4, sz); - k->u.p[sz] = 0; + kd = key_newraw(e); + kd->u.p = xmalloc(sz + 1); + memcpy(kd->u.p, q + 4, sz); + kd->u.p[sz] = 0; break; /* --- Elliptic curve point data --- */ case KENC_EC: { size_t xsz, ysz; - EC_CREATE(&k->u.e); + kd = key_newraw(e); + EC_CREATE(&kd->u.e); if (!sz) break; - if (sz < 2) return (-1); + if (sz < 2) return (0); xsz = LOAD16(q + 4); - if (sz < xsz + 4) return (-1); + if (sz < xsz + 4) return (0); ysz = LOAD16(q + 6 + xsz); - if (sz < xsz + ysz + 4) return (-1); - k->u.e.x = mp_loadb(MP_NEW, q + 6, xsz); - k->u.e.y = mp_loadb(MP_NEW, q + 6 + xsz, ysz); + if (sz < xsz + ysz + 4) return (0); + kd->u.e.x = mp_loadb(MP_NEW, q + 6, xsz); + kd->u.e.y = mp_loadb(MP_NEW, q + 8 + xsz, ysz); } break; /* --- Structured key data --- */ case KENC_STRUCT: { dstr d = DSTR_INIT; - key_struct *ks; - unsigned f; + key_data *nkd; - if ((k->e & ~KF_ENCMASK) || (psz & 3)) - return (-1); + if ((e & ~KF_ENCMASK) || (psz & 3)) + return (0); q += 4; - sym_create(&k->u.s); + kd = key_newstruct(); while (psz) { @@ -167,20 +141,15 @@ int key_decode(const void *p, size_t sz, key_data *k) /* --- Read the encoding and size --- */ - e = LOAD16(q); sz = (LOAD16(q + 2) + 7) & ~3; if (sz > psz) goto fail; /* --- Create a table node and fill it in --- */ - ks = sym_find(&k->u.s, d.buf, d.len, sizeof(*ks), &f); - if (f) - goto fail; - if (key_decode(q, sz, &ks->k)) { - sym_remove(&k->u.s, ks); + if ((nkd = key_decode(q, sz)) == 0) goto fail; - } + key_structsteal(kd, d.buf, nkd); psz -= sz; q += sz; } @@ -191,19 +160,20 @@ int key_decode(const void *p, size_t sz, key_data *k) fail: dstr_destroy(&d); - key_destroy(k); - return (-1); + key_drop(kd); + return (0); } break; /* --- Everything else --- */ default: - return (-1); + return (0); } /* --- OK, that was good --- */ - return (0); + kd->e = e; + return (kd); } /* --- @key_encode@ --- * @@ -217,6 +187,11 @@ int key_decode(const void *p, size_t sz, key_data *k) * Use: Encodes a key block as binary data. */ +static int ksbyname(const void *a, const void *b) { + key_struct *const *x = a, *const *y = b; + return (strcmp(SYM_NAME(*x), SYM_NAME(*y))); +} + int key_encode(key_data *k, dstr *d, const key_filter *kf) { int rc = 0; @@ -264,7 +239,7 @@ int key_encode(key_data *k, dstr *d, const key_filter *kf) case KENC_EC: { char *p; - size_t xsz, ysz; + size_t xsz = 0, ysz = 0; size_t sz; if (EC_ATINF(&k->u.e)) @@ -291,7 +266,8 @@ int key_encode(key_data *k, dstr *d, const key_filter *kf) case KENC_STRUCT: { size_t n; char *p; - key_struct *ks; + key_struct *ks, **ksv; + size_t nks, j; sym_iter i; n = d->len; @@ -299,20 +275,32 @@ int key_encode(key_data *k, dstr *d, const key_filter *kf) p = d->buf + n; STORE16(p, k->e & KF_ENCMASK); d->len += 4; - for (sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; ) { - size_t o = d->len; - DENSURE(d, 1); - *(octet *)(d->buf + d->len++) = strlen(SYM_NAME(ks)); - DPUTS(d, SYM_NAME(ks)); - while (d->len & 3) - DPUTC(d, 0); - if (key_encode(&ks->k, d, kf)) - rc = 1; - else - d->len = o; + + for (nks = 0, sym_mkiter(&i, &k->u.s); + (ks = sym_next(&i)) != 0; + nks++); + if (nks) { + ksv = xmalloc(nks * sizeof(*ksv)); + for (j = 0, sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; j++) + ksv[j] = ks; + qsort(ksv, nks, sizeof(*ksv), ksbyname); + for (j = 0; j < nks; j++) { + size_t o = d->len; + ks = ksv[j]; + DENSURE(d, 1); + *(octet *)(d->buf + d->len++) = strlen(SYM_NAME(ks)); + DPUTS(d, SYM_NAME(ks)); + while (d->len & 3) + DPUTC(d, 0); + if (key_encode(ks->k, d, kf)) + rc = 1; + else + d->len = o; + } + xfree(ksv); } if (!rc) - d->len = n; + d->len = n; else { p = d->buf + n + 2; n = d->len - n - 4;