3 * $Id: cc-sig.c,v 1.1 2004/04/17 09:58:37 mdw Exp $
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
34 #include <mLib/report.h>
49 /*----- Main code ---------------------------------------------------------*/
51 /* --- RSA PKCS1 --- */
53 typedef struct rsap1_sigctx {
59 static sig *rsap1_siginit(key *k, void *kd, const gchash *hc)
61 rsap1_sigctx *rs = CREATE(rsap1_sigctx);
62 rsa_privcreate(&rs->rp, kd, &rand_global);
63 rs->p1.r = &rand_global;
65 rs->p1.epsz = strlen(hc->name) + 1;
70 static int rsap1_sigdoit(sig *s, dstr *d)
72 rsap1_sigctx *rs = (rsap1_sigctx *)s;
74 mp *m = rsa_sign(&rs->rp, MP_NEW,
75 GH_DONE(s->h, 0), GH_CLASS(s->h)->hashsz,
76 pkcs1_sigencode, &rs->p1);
78 n = mp_octets(rs->rp.rp->n); dstr_ensure(d, n); mp_storeb(m, d->buf, n);
79 d->len += n; mp_drop(m);
83 static const char *rsa_lengthcheck(mp *n)
85 if (mp_bits(n) < 1024) return ("key too short");
89 static const char *rsap1_sigcheck(sig *s)
91 rsap1_sigctx *rs = (rsap1_sigctx *)s;
93 if ((e = rsa_lengthcheck(rs->rp.rp->n)) != 0) return (e);
97 static void rsap1_sigdestroy(sig *s)
99 rsap1_sigctx *rs = (rsap1_sigctx *)s;
100 rsa_privdestroy(&rs->rp);
104 static const sigops rsap1_sig = {
105 rsa_privfetch, sizeof(rsa_priv),
106 rsap1_siginit, rsap1_sigdoit, rsap1_sigcheck, rsap1_sigdestroy
109 typedef struct rsap1_vrfctx {
115 static sig *rsap1_vrfinit(key *k, void *kd, const gchash *hc)
117 rsap1_vrfctx *rv = CREATE(rsap1_vrfctx);
118 rsa_pubcreate(&rv->rp, kd);
119 rv->p1.r = &rand_global;
120 rv->p1.ep = hc->name;
121 rv->p1.epsz = strlen(hc->name) + 1;
126 static int rsap1_vrfdoit(sig *s, dstr *d)
128 rsap1_vrfctx *rv = (rsap1_vrfctx *)s;
129 mp *m = mp_loadb(MP_NEW, d->buf, d->len);
130 int rc = rsa_verify(&rv->rp, m,
131 GH_DONE(s->h, 0), GH_CLASS(s->h)->hashsz,
132 0, pkcs1_sigdecode, &rv->p1);
137 static const char *rsap1_vrfcheck(sig *s)
139 rsap1_vrfctx *rv = (rsap1_vrfctx *)s;
141 if ((e = rsa_lengthcheck(rv->rp.rp->n)) != 0) return (e);
145 static void rsap1_vrfdestroy(sig *s)
147 rsap1_vrfctx *rv = (rsap1_vrfctx *)s;
148 rsa_pubdestroy(&rv->rp);
152 static const sigops rsap1_vrf = {
153 rsa_pubfetch, sizeof(rsa_pub),
154 rsap1_vrfinit, rsap1_vrfdoit, rsap1_vrfcheck, rsap1_vrfdestroy
157 /* --- RSA PSS --- */
159 static const gccipher *getmgf(key *k, const gchash *hc)
165 if ((mm = key_getattr(0, k, "mgf")) == 0) {
166 dstr_putf(&d, "%s-mgf", hc->name);
169 if ((gc = gcipher_byname(mm)) == 0)
170 die(EXIT_FAILURE, "unknown encryption scheme `%s'", mm);
175 typedef struct rsapss_sigctx {
181 static sig *rsapss_siginit(key *k, void *kd, const gchash *hc)
183 rsapss_sigctx *rs = CREATE(rsapss_sigctx);
184 rsa_privcreate(&rs->rp, kd, &rand_global);
185 rs->p.r = &rand_global;
186 rs->p.cc = getmgf(k, hc);
188 rs->p.ssz = hc->hashsz;
189 rsa_privdestroy(&rs->rp);
193 static int rsapss_sigdoit(sig *s, dstr *d)
195 rsapss_sigctx *rs = (rsapss_sigctx *)s;
197 mp *m = rsa_sign(&rs->rp, MP_NEW,
198 GH_DONE(s->h, 0), GH_CLASS(s->h)->hashsz,
201 n = mp_octets(rs->rp.rp->n); dstr_ensure(d, n); mp_storeb(m, d->buf, n);
202 d->len += n; mp_drop(m);
206 static const char *rsapss_sigcheck(sig *s)
208 rsapss_sigctx *rs = (rsapss_sigctx *)s;
210 if ((e = rsa_lengthcheck(rs->rp.rp->n)) != 0) return (e);
214 static void rsapss_sigdestroy(sig *s)
216 rsapss_sigctx *rs = (rsapss_sigctx *)s;
217 rsa_privdestroy(&rs->rp);
221 static const sigops rsapss_sig = {
222 rsa_privfetch, sizeof(rsa_priv),
223 rsapss_siginit, rsapss_sigdoit, rsapss_sigcheck, rsapss_sigdestroy
226 typedef struct rsapss_vrfctx {
232 static sig *rsapss_vrfinit(key *k, void *kd, const gchash *hc)
234 rsapss_vrfctx *rv = CREATE(rsapss_vrfctx);
235 rsa_pubcreate(&rv->rp, kd);
236 rv->p.r = &rand_global;
237 rv->p.cc = getmgf(k, hc);
239 rv->p.ssz = hc->hashsz;
243 static int rsapss_vrfdoit(sig *s, dstr *d)
245 rsapss_vrfctx *rv = (rsapss_vrfctx *)s;
246 mp *m = mp_loadb(MP_NEW, d->buf, d->len);
247 int rc = rsa_verify(&rv->rp, m,
248 GH_DONE(s->h, 0), GH_CLASS(s->h)->hashsz,
249 0, pss_decode, &rv->p);
254 static const char *rsapss_vrfcheck(sig *s)
256 rsapss_vrfctx *rv = (rsapss_vrfctx *)s;
258 if ((e = rsa_lengthcheck(rv->rp.rp->n)) != 0) return (e);
262 static void rsapss_vrfdestroy(sig *s)
264 rsapss_vrfctx *rv = (rsapss_vrfctx *)s;
265 rsa_pubdestroy(&rv->rp);
269 static const sigops rsapss_vrf = {
270 rsa_pubfetch, sizeof(rsa_pub),
271 rsapss_vrfinit, rsapss_vrfdoit, rsapss_vrfcheck, rsapss_vrfdestroy
274 /* --- DSA and ECDSA --- */
276 typedef struct dsa_sigctx {
281 static void dsa_initcommon(dsa_sigctx *ds, const gchash *hc,
284 ds->g.r = &rand_global;
290 static dsa_sigctx *dsa_doinit(key *k, const gprime_param *gp,
291 mp *y, const gchash *hc)
293 dsa_sigctx *ds = CREATE(dsa_sigctx);
297 if ((ds->g.g = group_prime(gp)) == 0)
298 die(EXIT_FAILURE, "bad prime group in key `%s'", t.buf);
299 ds->g.p = G_CREATE(ds->g.g);
300 if (G_FROMINT(ds->g.g, ds->g.p, y))
301 die(EXIT_FAILURE, "bad public key in key `%s'", t.buf);
302 dsa_initcommon(ds, hc, t.buf);
307 static dsa_sigctx *ecdsa_doinit(key *k, const char *cstr,
308 ec *y, const gchash *hc)
310 dsa_sigctx *ds = CREATE(dsa_sigctx);
316 if ((e = ec_getinfo(&ei, cstr)) != 0)
317 die(EXIT_FAILURE, "bad curve in key `%s': %s", t.buf, e);
318 ds->g.g = group_ec(&ei);
319 ds->g.p = G_CREATE(ds->g.g);
320 if (G_FROMEC(ds->g.g, ds->g.p, y))
321 die(EXIT_FAILURE, "bad public key in key `%s'", t.buf);
322 dsa_initcommon(ds, hc, t.buf);
327 static sig *dsa_siginit(key *k, void *kd, const gchash *hc)
330 dsa_sigctx *ds = dsa_doinit(k, &dp->dp, dp->y, hc);
331 ds->g.u = MP_COPY(dp->x);
335 static sig *ecdsa_siginit(key *k, void *kd, const gchash *hc)
338 dsa_sigctx *ds = ecdsa_doinit(k, ep->cstr, &ep->p, hc);
339 ds->g.u = MP_COPY(ep->x);
343 static int dsa_sigdoit(sig *s, dstr *d)
345 dsa_sigctx *ds = (dsa_sigctx *)s;
346 gdsa_sig ss = GDSA_SIG_INIT;
347 size_t n = mp_octets(ds->g.g->r);
349 gdsa_sign(&ds->g, &ss, GH_DONE(ds->s.h, 0), 0);
350 dstr_ensure(d, 2 * n);
351 mp_storeb(ss.r, d->buf, n);
352 mp_storeb(ss.s, d->buf + n, n);
354 mp_drop(ss.r); mp_drop(ss.s);
358 static const char *dsa_sigcheck(sig *s)
360 dsa_sigctx *ds = (dsa_sigctx *)s;
362 if ((e = G_CHECK(ds->g.g, &rand_global)) != 0)
364 if (group_check(ds->g.g, ds->g.p))
365 return ("public key not in subgroup");
369 static void dsa_sigdestroy(sig *s)
371 dsa_sigctx *ds = (dsa_sigctx *)s;
372 G_DESTROY(ds->g.g, ds->g.p);
374 G_DESTROYGROUP(ds->g.g);
377 static const sigops dsa_sig = {
378 dh_privfetch, sizeof(dh_priv),
379 dsa_siginit, dsa_sigdoit, dsa_sigcheck, dsa_sigdestroy
382 static const sigops ecdsa_sig = {
383 ec_privfetch, sizeof(ec_priv),
384 ecdsa_siginit, dsa_sigdoit, dsa_sigcheck, dsa_sigdestroy
387 static sig *dsa_vrfinit(key *k, void *kd, const gchash *hc)
390 dsa_sigctx *ds = dsa_doinit(k, &dp->dp, dp->y, hc);
394 static sig *ecdsa_vrfinit(key *k, void *kd, const gchash *hc)
397 dsa_sigctx *ds = ecdsa_doinit(k, ep->cstr, &ep->p, hc);
401 static int dsa_vrfdoit(sig *s, dstr *d)
403 dsa_sigctx *ds = (dsa_sigctx *)s;
408 ss.r = mp_loadb(MP_NEW, d->buf, n);
409 ss.s = mp_loadb(MP_NEW, d->buf + n, d->len - n);
410 rc = gdsa_verify(&ds->g, &ss, GH_DONE(ds->s.h, 0));
411 mp_drop(ss.r); mp_drop(ss.s);
415 static const sigops dsa_vrf = {
416 dh_pubfetch, sizeof(dh_pub),
417 dsa_vrfinit, dsa_vrfdoit, dsa_sigcheck, dsa_sigdestroy
420 static const sigops ecdsa_vrf = {
421 ec_pubfetch, sizeof(ec_pub),
422 ecdsa_vrfinit, dsa_vrfdoit, dsa_sigcheck, dsa_sigdestroy
425 /* --- KCDSA and ECKCDSA --- */
427 static void kcdsa_privkey(dsa_sigctx *ds, mp *x)
428 { ds->g.u = mp_modinv(MP_NEW, x, ds->g.g->r); }
430 static void kcdsa_sethash(dsa_sigctx *ds, const gchash *hc)
431 { ds->s.h = gkcdsa_beginhash(&ds->g); }
433 static sig *kcdsa_siginit(key *k, void *kd, const gchash *hc)
436 dsa_sigctx *ds = dsa_doinit(k, &dp->dp, dp->y, hc);
437 kcdsa_privkey(ds, dp->x);
438 kcdsa_sethash(ds, hc);
442 static sig *eckcdsa_siginit(key *k, void *kd, const gchash *hc)
445 dsa_sigctx *ds = ecdsa_doinit(k, ep->cstr, &ep->p, hc);
446 kcdsa_privkey(ds, ep->x);
447 kcdsa_sethash(ds, hc);
451 static int kcdsa_sigdoit(sig *s, dstr *d)
453 dsa_sigctx *ds = (dsa_sigctx *)s;
454 gkcdsa_sig ss = GKCDSA_SIG_INIT;
455 size_t hsz = ds->g.h->hashsz, n = mp_octets(ds->g.g->r);
457 gkcdsa_sign(&ds->g, &ss, GH_DONE(ds->s.h, 0), 0);
458 dstr_ensure(d, hsz + n);
459 memcpy(d->buf, ss.r, hsz);
460 mp_storeb(ss.s, d->buf + hsz, n);
462 xfree(ss.r); mp_drop(ss.s);
466 static const sigops kcdsa_sig = {
467 dh_privfetch, sizeof(dh_priv),
468 kcdsa_siginit, kcdsa_sigdoit, dsa_sigcheck, dsa_sigdestroy
471 static const sigops eckcdsa_sig = {
472 ec_privfetch, sizeof(ec_priv),
473 eckcdsa_siginit, kcdsa_sigdoit, dsa_sigcheck, dsa_sigdestroy
476 static sig *kcdsa_vrfinit(key *k, void *kd, const gchash *hc)
479 dsa_sigctx *ds = dsa_doinit(k, &dp->dp, dp->y, hc);
480 kcdsa_sethash(ds, hc);
484 static sig *eckcdsa_vrfinit(key *k, void *kd, const gchash *hc)
487 dsa_sigctx *ds = ecdsa_doinit(k, ep->cstr, &ep->p, hc);
488 kcdsa_sethash(ds, hc);
492 static int kcdsa_vrfdoit(sig *s, dstr *d)
494 dsa_sigctx *ds = (dsa_sigctx *)s;
496 size_t hsz = ds->g.h->hashsz, n = d->len - hsz;
501 ss.r = (octet *)d->buf;
502 ss.s = mp_loadb(MP_NEW, d->buf + hsz, n);
503 rc = gkcdsa_verify(&ds->g, &ss, GH_DONE(ds->s.h, 0));
508 static const sigops kcdsa_vrf = {
509 dh_pubfetch, sizeof(dh_pub),
510 kcdsa_vrfinit, kcdsa_vrfdoit, dsa_sigcheck, dsa_sigdestroy
513 static const sigops eckcdsa_vrf = {
514 ec_pubfetch, sizeof(ec_pub),
515 eckcdsa_vrfinit, kcdsa_vrfdoit, dsa_sigcheck, dsa_sigdestroy
518 /* --- The switch table --- */
520 static const struct sigtab {
522 const sigops *signops;
523 const sigops *verifyops;
526 { "rsapkcs1", &rsap1_sig, &rsap1_vrf, &sha },
527 { "rsapss", &rsapss_sig, &rsapss_vrf, &sha },
528 { "dsa", &dsa_sig, &dsa_vrf, &sha },
529 { "ecdsa", &ecdsa_sig, &ecdsa_vrf, &sha },
530 { "kcdsa", &kcdsa_sig, &kcdsa_vrf, &has160 },
531 { "eckcdsa", &eckcdsa_sig, &eckcdsa_vrf, &has160 },
535 /* --- @getsig@ --- *
537 * Arguments: @key *k@ = the key to load
538 * @const char *app@ = application name
539 * @int wantpriv@ = nonzero if we want to sign
541 * Returns: A signature-making thing.
543 * Use: Loads a key and starts hashing.
546 sig *getsig(key *k, const char *app, int wantpriv)
548 const char *salg, *halg = 0;
555 const struct sigtab *st;
562 /* --- Setup stuff --- */
566 /* --- Get the signature algorithm --- *
568 * Take the attribute if it's there; otherwise use the key type.
572 if ((q = key_getattr(0, k, "sig")) != 0) {
575 } else if (strncmp(k->type, app, n) == 0 && k->type[n] == '-') {
576 dstr_puts(&d, k->type);
579 die(EXIT_FAILURE, "no signature algorithm for key `%s'", t.buf);
581 /* --- Grab the hash algorithm --- *
583 * Grab it from the signature algorithm if it's there. But override that
584 * from the attribute.
588 if ((p = strchr(p, '/')) != 0) {
592 if ((q = key_getattr(0, k, "hash")) != 0)
595 /* --- Look up the algorithms in the table --- */
597 for (st = sigtab; st->name; st++) {
598 if (strcmp(st->name, salg) == 0)
601 die(EXIT_FAILURE, "signature algorithm `%s' not found in key `%s'",
607 if ((ch = ghash_byname(halg)) == 0) {
608 die(EXIT_FAILURE, "hash algorithm `%s' not found in key `%s'",
612 so = wantpriv ? st->signops : st->verifyops;
614 /* --- Load the key --- */
616 kd = xmalloc(so->kdsz);
617 kp = key_fetchinit(so->kf, 0, kd);
618 if ((e = key_fetch(kp, k)) != 0)
619 die(EXIT_FAILURE, "error fetching key `%s': %s", t.buf, key_strerror(e));
620 s = so->init(k, kd, ch);
627 /* --- Free stuff up --- */
634 /* --- @freesig@ --- *
636 * Arguments: @sig *s@ = signature-making thing
640 * Use: Frees up a signature-making thing
646 key_fetchdone(s->kp);
651 /*----- That's all, folks -------------------------------------------------*/