X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/898a4e2555438ff8adb08b4d82690d08715e1048..5d01b1b9514a258c5a3c201e944f676cb2c467f0:/key-binary.c diff --git a/key-binary.c b/key-binary.c index 6726152..c1ef53b 100644 --- a/key-binary.c +++ b/key-binary.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: key-binary.c,v 1.6 2004/04/08 01:03:22 mdw Exp $ + * $Id$ * * Key binary encoding * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,46 +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.6 2004/04/08 01:03:22 mdw - * Force subkeys to be sorted in structured keys. - * - * 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 @@ -75,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 --- * @@ -98,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 --- */ @@ -109,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) { @@ -170,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) + if ((nkd = key_decode(q, sz)) == 0) goto fail; - if (key_decode(q, sz, &ks->k)) { - sym_remove(&k->u.s, ks); - goto fail; - } + key_structsteal(kd, d.buf, nkd); psz -= sz; q += sz; } @@ -194,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@ --- * @@ -308,7 +275,7 @@ 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 (nks = 0, sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; nks++); @@ -325,7 +292,7 @@ int key_encode(key_data *k, dstr *d, const key_filter *kf) DPUTS(d, SYM_NAME(ks)); while (d->len & 3) DPUTC(d, 0); - if (key_encode(&ks->k, d, kf)) + if (key_encode(ks->k, d, kf)) rc = 1; else d->len = o; @@ -333,7 +300,7 @@ int key_encode(key_data *k, dstr *d, const key_filter *kf) xfree(ksv); } if (!rc) - d->len = n; + d->len = n; else { p = d->buf + n + 2; n = d->len - n - 4;