#include "tripe.h"
-/*----- Key groups --------------------------------------------------------*/
-
-/* The key-loading functions here must fill in the kdata slot @g@ and
- * either @kpriv@ or @kpub@ as appropriate. The caller will take care of
- * determining @kpub@ given a private key, and of ensuring that @kpriv@ is
- * null for a public key.
- */
-
-typedef struct kgops {
- const char *ty;
- int (*loadpriv)(key_data *, kdata *, dstr *, dstr *);
- int (*loadpub)(key_data *, kdata *, dstr *, dstr *);
-} kgops;
-
-/* --- Diffie-Hellman --- */
-
-static int kgdh_priv(key_data *d, kdata *kd, dstr *t, dstr *e)
-{
- key_packstruct kps[DH_PRIVFETCHSZ];
- key_packdef *kp;
- dh_priv dp;
- int rc;
-
- kp = key_fetchinit(dh_privfetch, kps, &dp);
- if ((rc = key_unpack(kp, d, t)) != 0) {
- a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
- goto fail_0;
- }
- kd->g = group_prime(&dp.dp);
- kd->kpriv = MP_COPY(dp.x);
- rc = 0;
- goto done;
-fail_0:
- rc = -1;
-done:
- key_fetchdone(kp);
- return (rc);
-}
-
-static int kgdh_pub(key_data *d, kdata *kd, dstr *t, dstr *e)
-{
- key_packstruct kps[DH_PUBFETCHSZ];
- key_packdef *kp;
- dh_pub dp;
- int rc;
-
- kp = key_fetchinit(dh_pubfetch, kps, &dp);
- if ((rc = key_unpack(kp, d, t)) != 0) {
- a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
- goto fail_0;
- }
- kd->g = group_prime(&dp.dp);
- kd->kpub = G_CREATE(kd->g);
- if (G_FROMINT(kd->g, kd->kpub, dp.y)) {
- a_format(e, "bad-public-vector", A_END);
- goto fail_1;
- }
- rc = 0;
- goto done;
-fail_1:
- G_DESTROY(kd->g, kd->kpub);
- G_DESTROYGROUP(kd->g);
-fail_0:
- rc = -1;
-done:
- key_fetchdone(kp);
- return (rc);
-}
-
-static const kgops kgdh_ops = { "dh", kgdh_priv, kgdh_pub };
-
-/* --- Elliptic curve --- */
-
-static int kgec_priv(key_data *d, kdata *kd, dstr *t, dstr *e)
-{
- key_packstruct kps[EC_PRIVFETCHSZ];
- key_packdef *kp;
- ec_priv ep;
- ec_info ei;
- const char *err;
- int rc;
-
- kp = key_fetchinit(ec_privfetch, kps, &ep);
- if ((rc = key_unpack(kp, d, t)) != 0) {
- a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
- goto fail_0;
- }
- if ((err = ec_getinfo(&ei, ep.cstr)) != 0) {
- a_format(e, "decode-failed", "%s", err, A_END);
- goto fail_0;
- }
- kd->g = group_ec(&ei);
- kd->kpriv = MP_COPY(ep.x);
- rc = 0;
- goto done;
-fail_0:
- rc = -1;
-done:
- key_fetchdone(kp);
- return (rc);
-}
-
-static int kgec_pub(key_data *d, kdata *kd, dstr *t, dstr *e)
-{
- key_packstruct kps[EC_PUBFETCHSZ];
- key_packdef *kp;
- ec_pub ep;
- ec_info ei;
- const char *err;
- int rc;
-
- kp = key_fetchinit(ec_pubfetch, kps, &ep);
- if ((rc = key_unpack(kp, d, t)) != 0) {
- a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
- goto fail_0;
- }
- if ((err = ec_getinfo(&ei, ep.cstr)) != 0) {
- a_format(e, "decode-failed", "%s", err, A_END);
- goto fail_0;
- }
- kd->g = group_ec(&ei);
- kd->kpub = G_CREATE(kd->g);
- if (G_FROMEC(kd->g, kd->kpub, &ep.p)) {
- a_format(e, "bad-public-vector", A_END);
- goto fail_1;
- }
- rc = 0;
- goto done;
-fail_1:
- G_DESTROY(kd->g, kd->kpub);
- G_DESTROYGROUP(kd->g);
-fail_0:
- rc = -1;
-done:
- key_fetchdone(kp);
- return (rc);
-}
-
-static const kgops kgec_ops = { "ec", kgec_priv, kgec_pub };
-
-/* --- Table of supported key types --- */
-
-static const kgops *kgtab[] = { &kgdh_ops, &kgec_ops, 0 };
-
/*----- Algswitch stuff ---------------------------------------------------*/
/* --- @algs_get@ --- *
*
* Arguments: @algswitch *a@ = where to put the algorithms
- * @dstr *e@ = where to write errror tokens
+ * @dstr *e@ = where to write error tokens
* @key_file *kf@ = key file
* @key *k@ = key to inspect
*
static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
{
const char *p;
- char *q, *qq;
- dstr d = DSTR_INIT;
+ const bulkops *bops;
+ dstr d = DSTR_INIT, dd = DSTR_INIT;
int rc = -1;
- /* --- Symmetric encryption for bulk data --- */
-
- if ((p = key_getattr(kf, k, "cipher")) == 0) p = "blowfish-cbc";
- if ((a->c = gcipher_byname(p)) == 0) {
- a_format(e, "unknown-cipher", "%s", p, A_END);
- goto done;
- }
-
/* --- Hash function --- */
if ((p = key_getattr(kf, k, "hash")) == 0) p = "rmd160";
goto done;
}
- /* --- Message authentication for bulk data --- */
+ /* --- Bulk crypto transform --- */
- if ((p = key_getattr(kf, k, "mac")) != 0) {
- dstr_reset(&d);
- dstr_puts(&d, p);
- if ((q = strchr(d.buf, '/')) != 0)
- *q++ = 0;
- if ((a->m = gmac_byname(d.buf)) == 0) {
- a_format(e, "unknown-mac", "%s", d.buf, A_END);
- goto done;
- }
- if (!q)
- a->tagsz = a->m->hashsz;
- else {
- unsigned long n = strtoul(q, &qq, 0);
- if (*qq) {
- a_format(e, "bad-tag-length-string", "%s", q, A_END);
- goto done;
- }
- if (n%8 || n/8 > a->m->hashsz) {
- a_format(e, "bad-tag-length", "%lu", n, A_END);
- goto done;
- }
- a->tagsz = n/8;
- }
- } else {
- dstr_reset(&d);
- dstr_putf(&d, "%s-hmac", a->h->name);
- if ((a->m = gmac_byname(d.buf)) == 0) {
- a_format(e, "no-hmac-for-hash", "%s", a->h->name, A_END);
- goto done;
- }
- a->tagsz = a->h->hashsz/2;
+ if ((p = key_getattr(kf, k, "bulk")) == 0) p = "v0";
+ for (bops = bulktab; bops->name && strcmp(p, bops->name) != 0; bops++);
+ if (!bops->name) {
+ a_format(e, "unknown-bulk-transform", "%s", p, A_END);
+ goto done;
}
+ if ((a->bulk = bops->getalgs(a, e, kf, k)) == 0) goto done;
+ a->bulk->ops = bops;
+
+ /* --- All done --- */
rc = 0;
done:
dstr_destroy(&d);
+ dstr_destroy(&dd);
return (rc);
}
*
* Arguments: @algswitch *a@ = a choice of algorithms
* @dstr *e@ = where to write error tokens
- * @const group *g@ = the group we're working in
+ * @const dhgrp *grp@ = the group we're working in
*
* Returns: Zero if OK; nonzero on error.
*
* for use by @keyset@ functions.
*/
-static int algs_check(algswitch *a, dstr *e, const group *g)
+static int algs_check(algswitch *a, dstr *e, const dhgrp *grp)
{
- /* --- Derive the key sizes --- *
- *
- * Must ensure that we have non-empty keys. This isn't ideal, but it
- * provides a handy sanity check. Also must be based on a 64- or 128-bit
- * block cipher or we can't do the data expiry properly.
- */
-
a->hashsz = a->h->hashsz;
- if ((a->cksz = keysz(a->hashsz, a->c->keysz)) == 0) {
- a_format(e, "cipher", "%s", a->c->name,
- "no-key-size", "%lu", (unsigned long)a->hashsz,
- A_END);
- return (-1);
- }
- if ((a->mksz = keysz(a->hashsz, a->m->keysz)) == 0) {
- a_format(e, "mac", "%s", a->m->name,
- "no-key-size", "%lu", (unsigned long)a->hashsz,
- A_END);
- return (-1);
- }
-
- /* --- Derive the data limit --- */
-
- if (a->c->blksz < 16) a->expsz = MEG(64);
- else a->expsz = MEG(2048);
-
- /* --- Ensure the MGF accepts hashes as keys --- */
if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz) {
a_format(e, "mgf", "%s", a->mgf->name,
return (-1);
}
- /* --- All ship-shape and Bristol-fashion --- */
+ if (a->bulk->ops->checkalgs(a->bulk, a, e)) return (-1);
return (0);
}
{
const algswitch *a = &kdx->algs, *aa = &kdy->algs;
- return (group_samep(kdx->g, kdy->g) && a->c == aa->c &&
+ return (kdx->grp->ops == kdy->grp->ops &&
+ kdx->grp->ops->samegrpp(kdx->grp, kdy->grp) &&
a->mgf == aa->mgf && a->h == aa->h &&
- a->m == aa->m && a->tagsz == aa->tagsz);
+ a->bulk->ops == aa->bulk->ops &&
+ a->bulk->ops->samealgsp(a->bulk, aa->bulk));
}
/*----- Key data and key nodes --------------------------------------------*/
typedef struct keyhalf {
const char *kind;
- int (*load)(const kgops *, key_data *, kdata *, dstr *, dstr *);
+ int (*load)(key_file *, key *, key_data *,
+ const dhops *, kdata *, dstr *, dstr *);
const char *kr;
key_file *kf;
fwatch w;
/* --- @kh_loadpub@, @kh_loadpriv@ --- *
*
- * Arguments: @const kgops *ko@ = key-group operations for key type
- * @key_data *d@ = key data object as stored in keyring
+ * Arguments: @const dhops *dh@ = Diffie--Hellman operations for key type
+ * @key_file *kf@ = key file from which the key was loaded
+ * @key *k@ = the key object we're loading
+ * @key_data *d@ = the key data to load
* @kdata *kd@ = our key-data object to fill in
* @dstr *t@ = the key tag name
* @dstr *e@ = a string to write error tokens to
* Returns: Zero on success, @-1@ on error.
*
* Use: These functions handle the main difference between public and
- * private key halves. They are responsible for setting @g@,
- * @kpriv@ and @kpub@ appropriately in all keys, handling the
- * mismatch between the largely half-indifferent calling code
- * and the group-specific loading functions.
+ * private key halves. They are responsible for setting @grp@,
+ * @k@ and @K@ appropriately in all keys, handling the mismatch
+ * between the largely half-indifferent calling code and the
+ * group-specific loading functions.
*
* The function @kh_loadpriv@ is also responsible for checking
* the group for goodness. We don't bother checking public
* checked.
*/
-static int kh_loadpub(const kgops *ko, key_data *d, kdata *kd,
- dstr *t, dstr *e)
+static int kh_loadpub(key_file *kf, key *k, key_data *d,
+ const dhops *dh, kdata *kd, dstr *t, dstr *e)
{
int rc;
- if ((rc = ko->loadpub(d, kd, t, e)) != 0)
+ if ((rc = dh->ldpub(kf, k, d, kd, t, e)) != 0)
goto fail_0;
- if (group_check(kd->g, kd->kpub)) {
- a_format(e, "bad-public-group-element");
+ kd->grp->ops = dh;
+ if (kd->grp->ops->checkge(kd->grp, kd->K)) {
+ a_format(e, "bad-public-group-element", A_END);
goto fail_1;
}
- kd->kpriv = 0;
return (0);
fail_1:
- G_DESTROY(kd->g, kd->kpub);
- G_DESTROYGROUP(kd->g);
+ kd->grp->ops->freege(kd->grp, kd->K);
+ kd->grp->ops->freegrp(kd->grp);
fail_0:
return (-1);
}
-static int kh_loadpriv(const kgops *ko, key_data *d, kdata *kd,
- dstr *t, dstr *e)
+static int kh_loadpriv(key_file *kf, key *k, key_data *d,
+ const dhops *dh, kdata *kd, dstr *t, dstr *e)
{
int rc;
const char *err;
+ dhge *K;
+ int ok;
- if ((rc = ko->loadpriv(d, kd, t, e)) != 0)
+ if ((rc = dh->ldpriv(kf, k, d, kd, t, e)) != 0)
goto fail_0;
- if ((err = G_CHECK(kd->g, &rand_global)) != 0) {
+ kd->grp->ops = dh;
+ if ((err = kd->grp->ops->checkgrp(kd->grp)) != 0) {
a_format(e, "bad-group", "%s", err, A_END);
goto fail_1;
}
- kd->kpub = G_CREATE(kd->g);
- G_EXP(kd->g, kd->kpub, kd->g->g, kd->kpriv);
+ K = kd->grp->ops->mul(kd->grp, kd->k, 0);
+ ok = kd->grp->ops->eq(kd->grp, kd->K, K);
+ kd->grp->ops->freege(kd->grp, K);
+ if (!ok) {
+ a_format(e, "incorrect-public-key", A_END);
+ goto fail_1;
+ }
return (0);
fail_1:
- mp_drop(kd->kpriv);
- G_DESTROYGROUP(kd->g);
+ kd->grp->ops->freesc(kd->grp, kd->k);
+ kd->grp->ops->freege(kd->grp, kd->K);
+ kd->grp->ops->freegrp(kd->grp);
fail_0:
return (-1);
}
if (key_open(kf, kh->kr, KOPEN_READ, keymoan, kh)) {
a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", kh->kr,
- "read-error", "?ERRNO", A_END);
+ "io-error", "?ERRNO", A_END);
DESTROY(kf);
return (-1);
} else {
key_data **d;
kdata *kd;
const char *ty;
- const kgops **ko;
+ const dhops *dh;
+ T( const dhgrp *g; )
/* --- Find the key and grab its tag --- */
if (!ty && strncmp(k->type, "tripe-", 6) == 0) ty = k->type + 6;
if (!ty) ty = "dh";
- for (ko = kgtab; *ko; ko++)
- if (strcmp((*ko)->ty, ty) == 0) goto foundko;
+ for (dh = dhtab; dh->name; dh++)
+ if (strcmp(dh->name, ty) == 0) goto founddh;
a_warn("KEYMGMT", "%s-keyring", kh->kind,
"%s", kh->kr, "key", "%s", t.buf,
"unknown-group-type", "%s", ty, A_END);
goto fail_0;
-foundko:
+founddh:
kd = CREATE(kdata);
- if (kh->load(*ko, *d, kd, &t, &e)) {
+ if (kh->load(kh->kf, k, *d, dh, kd, &t, &e)) {
a_warn("KEYMGMT", "%s-keyring", kh->kind,
- "%s", kh->kr, "key" "%s", t.buf,
+ "%s", kh->kr, "key", "%s", t.buf,
"*%s", e.buf, A_END);
goto fail_1;
}
if (algs_get(&kd->algs, &e, kh->kf, k) ||
- (kd->kpriv && algs_check(&kd->algs, &e, kd->g))) {
+ (kd->k && algs_check(&kd->algs, &e, kd->grp))) {
a_warn("KEYMGMT", "%s-keyring", kh->kind,
"%s", kh->kr, "key", "%s", t.buf,
"*%s", e.buf, A_END);
}
kd->tag = xstrdup(t.buf);
- kd->indexsz = mp_octets(kd->g->r);
kd->ref = 1;
kd->kn = 0;
kd->t_exp = k->exp;
IF_TRACING(T_KEYMGMT, {
trace(T_KEYMGMT, "keymgmt: loaded %s key `%s'", kh->kind, t.buf);
IF_TRACING(T_CRYPTO, {
- trace(T_CRYPTO, "crypto: r = %s", mpstr(kd->g->r));
- trace(T_CRYPTO, "crypto: h = %s", mpstr(kd->g->h));
- if (kd->kpriv)
- trace(T_CRYPTO, "crypto: x = %s", mpstr(kd->kpriv));
- trace(T_CRYPTO, "crypto: cipher = %s", kd->algs.c->name);
- trace(T_CRYPTO, "crypto: mgf = %s", kd->algs.mgf->name);
- trace(T_CRYPTO, "crypto: hash = %s", kd->algs.h->name);
- trace(T_CRYPTO, "crypto: mac = %s/%lu",
- kd->algs.m->name, (unsigned long)kd->algs.tagsz * 8);
+ g = kd->grp;
+ g->ops->tracegrp(g);
+ if (kd->k)
+ trace(T_CRYPTO, "crypto: k = %s", g->ops->scstr(g, kd->k));
+ trace(T_CRYPTO, "crypto: K = %s", g->ops->gestr(g, kd->K));
+ kd->algs.bulk->ops->tracealgs(kd->algs.bulk);
})
})
goto done;
fail_2:
- if (kd->kpriv) mp_drop(kd->kpriv);
- G_DESTROY(kd->g, kd->kpub);
- G_DESTROYGROUP(kd->g);
+ if (kd->k) kd->grp->ops->freesc(kd->grp, kd->k);
+ kd->grp->ops->freege(kd->grp, kd->K);
+ kd->grp->ops->freegrp(kd->grp);
fail_1:
DESTROY(kd);
fail_0:
kn->f &= ~KNF_BROKEN;
if (kd->t_exp == kn->kd->t_exp &&
km_samealgsp(kd, kn->kd) &&
- G_EQ(kd->g, kd->kpub, kn->kd->kpub)) {
+ kd->grp->ops->eq(kd->grp, kd->K, kn->kd->K)) {
T( trace(T_KEYMGMT, "keymgmt: key `%s' unchanged", SYM_NAME(kn)); )
continue;
}
void km_unref(kdata *kd)
{
if (--kd->ref) return;
- if (kd->kpriv) mp_drop(kd->kpriv);
- G_DESTROY(kd->g, kd->kpub);
+ if (kd->k) kd->grp->ops->freesc(kd->grp, kd->k);
+ kd->grp->ops->freege(kd->grp, kd->K);
+ kd->grp->ops->freegrp(kd->grp);
xfree(kd->tag);
- G_DESTROYGROUP(kd->g);
+ DESTROY(kd);
}
/*----- That's all, folks -------------------------------------------------*/