3 * Catcrypt key-encapsulation
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #define _FILE_OFFSET_BITS 64
34 #include <mLib/alloc.h>
35 #include <mLib/dstr.h>
36 #include <mLib/report.h>
51 #include "blowfish-cbc.h"
52 #include "chacha20-poly1305.h"
59 /*----- Bulk crypto -------------------------------------------------------*/
61 /* --- NaCl `secretbox' --- */
63 typedef struct naclbox_encctx {
69 static bulk *naclbox_init(key *k, const char *calg, const char *halg)
71 naclbox_encctx *ctx = CREATE(naclbox_encctx);
77 if ((q = key_getattr(0, k, "cipher")) != 0) calg = q;
78 if (!calg || strcmp(calg, "salsa20") == 0) ctx->cc = &salsa20;
79 else if (strcmp(calg, "salsa20/12") == 0) ctx->cc = &salsa2012;
80 else if (strcmp(calg, "salsa20/8") == 0) ctx->cc = &salsa208;
81 else if (strcmp(calg, "chacha20") == 0) ctx->cc = &chacha20;
82 else if (strcmp(calg, "chacha12") == 0) ctx->cc = &chacha12;
83 else if (strcmp(calg, "chacha8") == 0) ctx->cc = &chacha8;
86 "unknown or inappropriate encryption scheme `%s' in key `%s'",
94 static int naclbox_setup(bulk *b, gcipher *cx)
96 naclbox_encctx *ctx = (naclbox_encctx *)b;
97 octet k[SALSA20_KEYSZ];
99 GC_ENCRYPT(cx, 0, k, sizeof(k));
100 ctx->c = GC_INIT(ctx->cc, k, sizeof(k));
104 static size_t naclbox_overhead(bulk *b) { return (POLY1305_TAGSZ); }
106 static void naclbox_destroy(bulk *b)
108 naclbox_encctx *ctx = (naclbox_encctx *)b;
114 static const char *naclbox_encdoit(bulk *b, uint32 seq, buf *bb,
115 const void *p, size_t sz)
117 naclbox_encctx *ctx = (naclbox_encctx *)b;
123 STORE32(t, seq); STORE32(t + 4, 0); GC_SETIV(ctx->c, t);
124 GC_ENCRYPT(ctx->c, 0, t, POLY1305_KEYSZ + POLY1305_MASKSZ);
125 poly1305_keyinit(&ak, t, POLY1305_KEYSZ);
126 poly1305_macinit(&a, &ak, t + POLY1305_KEYSZ);
128 tag = buf_get(bb, POLY1305_TAGSZ); assert(tag);
129 ct = buf_get(bb, sz); assert(ct);
130 GC_ENCRYPT(ctx->c, p, ct, sz);
131 poly1305_hash(&a, ct, sz);
132 poly1305_done(&a, tag);
136 static const char *naclbox_decdoit(bulk *b, uint32 seq, buf *bb,
137 const void *p, size_t sz)
139 naclbox_encctx *ctx = (naclbox_encctx *)b;
144 octet *tag, *ct, *pt;
146 STORE32(t, seq); STORE32(t + 4, 0); GC_SETIV(ctx->c, t);
147 GC_ENCRYPT(ctx->c, 0, t, POLY1305_KEYSZ + POLY1305_MASKSZ);
148 poly1305_keyinit(&ak, t, POLY1305_KEYSZ);
149 poly1305_macinit(&a, &ak, t + POLY1305_KEYSZ);
151 buf_init(&bin, (/*unconst*/ void *)p, sz);
152 if ((tag = buf_get(&bin, POLY1305_TAGSZ)) == 0) return ("no tag");
153 ct = BCUR(&bin); sz = BLEFT(&bin);
154 poly1305_hash(&a, ct, sz);
155 poly1305_done(&a, t);
156 if (!ct_memeq(t, tag, POLY1305_TAGSZ)) return ("authentication failure");
157 pt = buf_get(bb, sz); assert(pt);
158 GC_DECRYPT(ctx->c, ct, pt, sz);
162 static const bulkops naclbox_encops = {
163 naclbox_init, naclbox_setup, naclbox_overhead,
164 naclbox_encdoit, naclbox_destroy
165 }, naclbox_decops = {
166 naclbox_init, naclbox_setup, naclbox_overhead,
167 naclbox_decdoit, naclbox_destroy
170 /* --- Authenticated encryption schemes --- */
172 typedef struct aead_encctx {
176 union { gaead_enc *enc; gaead_dec *dec; } ed;
181 static bulk *aead_internalinit(key *k, const gcaead *aec)
183 aead_encctx *ctx = CREATE(aead_encctx);
187 if ((ctx->nsz = keysz_pad(4, aec->noncesz)) == 0)
188 die(EXIT_FAILURE, "no suitable nonce size for `%s'", aec->name);
189 ctx->tsz = keysz(0, ctx->aec->tagsz);
194 static bulk *aead_init(key *k, const char *calg, const char *halg)
202 if ((q = key_getattr(0, k, "cipher")) != 0) calg = q;
203 if (!calg) aec = &chacha20_poly1305;
204 else if ((aec = gaead_byname(calg)) == 0)
205 die(EXIT_FAILURE, "AEAD scheme `%s' not found in key `%s'",
209 return (aead_internalinit(k, aec));
212 static int aead_commonsetup(aead_encctx *ctx, gcipher *cx)
216 n = ksz = keysz(0, ctx->aec->keysz);
217 if (n < ctx->nsz) n = ctx->nsz;
218 if (n < ctx->tsz) n = ctx->tsz;
221 GC_ENCRYPT(cx, 0, ctx->t, ksz);
222 ctx->key = GAEAD_KEY(ctx->aec, ctx->t, ksz);
226 static size_t aead_overhead(bulk *b)
227 { aead_encctx *ctx = (aead_encctx *)b; return (ctx->aec->ohd + ctx->tsz); }
229 static void aead_commondestroy(aead_encctx *ctx)
231 if (ctx->key) GAEAD_DESTROY(ctx->key);
236 static int aead_encsetup(bulk *b, gcipher *cx)
238 aead_encctx *ctx = (aead_encctx *)b;
239 ctx->ed.enc = 0; return (aead_commonsetup(ctx, cx));
242 static const char *aead_encdoit(bulk *b, uint32 seq, buf *bb,
243 const void *p, size_t sz)
245 aead_encctx *ctx = (aead_encctx *)b;
249 memset(ctx->t + 4, 0, ctx->nsz - 4); STORE32_B(ctx->t, seq);
251 ctx->ed.enc = GAEAD_ENC(ctx->key, ctx->t, ctx->nsz, 0, sz, ctx->tsz);
253 GAEAD_REINIT(ctx->ed.enc, ctx->t, ctx->nsz, 0, sz, ctx->tsz);
254 t = buf_get(bb, ctx->tsz); assert(t);
255 rc = GAEAD_ENCRYPT(ctx->ed.enc, p, sz, bb); assert(rc >= 0);
256 rc = GAEAD_DONE(ctx->ed.enc, 0, bb, t, ctx->tsz); assert(rc >= 0);
260 static void aead_encdestroy(bulk *b)
262 aead_encctx *ctx = (aead_encctx *)b;
263 if (ctx->ed.enc) GAEAD_DESTROY(ctx->ed.enc);
264 aead_commondestroy(ctx);
267 static int aead_decsetup(bulk *b, gcipher *cx)
269 aead_encctx *ctx = (aead_encctx *)b;
270 ctx->ed.dec = 0; return (aead_commonsetup(ctx, cx));
273 static const char *aead_decdoit(bulk *b, uint32 seq, buf *bb,
274 const void *p, size_t sz)
276 aead_encctx *ctx = (aead_encctx *)b;
281 memset(ctx->t + 4, 0, ctx->nsz - 4); STORE32_B(ctx->t, seq);
283 ctx->ed.dec = GAEAD_DEC(ctx->key, ctx->t, ctx->nsz, 0, sz, ctx->tsz);
285 GAEAD_REINIT(ctx->ed.enc, ctx->t, ctx->nsz, 0, sz, ctx->tsz);
287 buf_init(&bin, (/*unconst*/ void *)p, sz);
288 t = buf_get(&bin, ctx->tsz); if (!t) return ("no tag");
289 rc = GAEAD_DECRYPT(ctx->ed.dec, BCUR(&bin), BLEFT(&bin), bb);
291 rc = GAEAD_DONE(ctx->ed.dec, 0, bb, t, ctx->tsz); assert(rc >= 0);
292 if (!rc) return ("authentication failure");
296 static void aead_decdestroy(bulk *b)
298 aead_encctx *ctx = (aead_encctx *)b;
299 if (ctx->ed.dec) GAEAD_DESTROY(ctx->ed.dec);
300 aead_commondestroy(ctx);
303 static const struct bulkops aead_encops = {
304 aead_init, aead_encsetup, aead_overhead,
305 aead_encdoit, aead_encdestroy
307 aead_init, aead_decsetup, aead_overhead,
308 aead_decdoit, aead_decdestroy
311 /* --- Generic composition --- */
313 typedef struct gencomp_encctx {
319 octet *t; size_t tsz;
322 static bulk *gencomp_init(key *k, const char *calg, const char *halg)
324 gencomp_encctx *ctx = CREATE(gencomp_encctx);
326 dstr d = DSTR_INIT, t = DSTR_INIT;
330 if ((q = key_getattr(0, k, "cipher")) != 0) calg = q;
331 if (!calg) ctx->cc = &blowfish_cbc;
332 else if ((ctx->cc = gcipher_byname(calg)) == 0) {
333 die(EXIT_FAILURE, "encryption scheme `%s' not found in key `%s'",
338 if ((q = key_getattr(0, k, "mac")) == 0) {
339 dstr_putf(&d, "%s-hmac", halg);
342 if ((ctx->mc = gmac_byname(q)) == 0) {
344 "message authentication code `%s' not found in key `%s'",
351 static int gencomp_setup(bulk *b, gcipher *cx)
353 gencomp_encctx *ctx = (gencomp_encctx *)b;
359 cn = keysz(0, ctx->cc->keysz); if (cn > n) n = cn;
360 mn = keysz(0, ctx->mc->keysz); if (mn > n) n = mn;
361 ctx->t = kd = xmalloc(n); ctx->tsz = n;
362 GC_ENCRYPT(cx, 0, kd, cn);
363 ctx->c = GC_INIT(ctx->cc, kd, cn);
364 GC_ENCRYPT(cx, 0, kd, mn);
365 ctx->m = GM_KEY(ctx->mc, kd, mn);
369 static size_t gencomp_overhead(bulk *b)
371 gencomp_encctx *ctx = (gencomp_encctx *)b;
372 return (ctx->cc->blksz + ctx->mc->hashsz); }
374 static void gencomp_destroy(bulk *b)
376 gencomp_encctx *ctx = (gencomp_encctx *)b;
384 static const char *gencomp_encdoit(bulk *b, uint32 seq, buf *bb,
385 const void *p, size_t sz)
387 gencomp_encctx *ctx = (gencomp_encctx *)b;
389 ghash *h = GM_INIT(ctx->m);
392 if (ctx->cc->blksz) {
393 GC_ENCRYPT(ctx->cx, 0, ctx->t, ctx->cc->blksz);
394 GC_SETIV(ctx->c, ctx->t);
396 tag = buf_get(bb, ctx->mc->hashsz); assert(tag);
397 ct = buf_get(bb, sz); assert(ct);
398 GC_ENCRYPT(ctx->c, p, ct, sz);
405 static const char *gencomp_decdoit(bulk *b, uint32 seq, buf *bb,
406 const void *p, size_t sz)
408 gencomp_encctx *ctx = (gencomp_encctx *)b;
410 const octet *tag, *ct;
415 buf_init(&bin, (/*unconst*/ void *)p, sz);
416 if ((tag = buf_get(&bin, ctx->mc->hashsz)) == 0) return ("no tag");
417 ct = BCUR(&bin); sz = BLEFT(&bin);
418 pt = buf_get(bb, sz); assert(pt);
423 ok = ct_memeq(tag, GH_DONE(h, 0), ctx->mc->hashsz);
425 if (!ok) return ("authentication failure");
427 if (ctx->cc->blksz) {
428 GC_ENCRYPT(ctx->cx, 0, ctx->t, ctx->cc->blksz);
429 GC_SETIV(ctx->c, ctx->t);
431 GC_DECRYPT(ctx->c, ct, pt, sz);
435 static const bulkops gencomp_encops = {
436 gencomp_init, gencomp_setup, gencomp_overhead,
437 gencomp_encdoit, gencomp_destroy
438 }, gencomp_decops = {
439 gencomp_init, gencomp_setup, gencomp_overhead,
440 gencomp_decdoit, gencomp_destroy
443 const struct bulktab bulktab[] = {
444 { "gencomp", &gencomp_encops, &gencomp_decops },
445 { "naclbox", &naclbox_encops, &naclbox_decops },
446 { "aead", &aead_encops, &aead_decops },
450 /*----- Key encapsulation -------------------------------------------------*/
454 typedef struct rsa_encctx {
459 static kem *rsa_encinit(key *k, void *kd)
461 rsa_encctx *re = CREATE(rsa_encctx);
462 rsa_pubcreate(&re->rp, kd);
466 static int rsa_encdoit(kem *k, dstr *d, ghash *h)
468 rsa_encctx *re = (rsa_encctx *)k;
469 mp *x = mprand_range(MP_NEW, re->rp.rp->n, &rand_global, 0);
470 mp *y = rsa_pubop(&re->rp, MP_NEW, x);
471 size_t n = mp_octets(re->rp.rp->n);
473 mp_storeb(x, d->buf, n);
474 GH_HASH(h, d->buf, n);
475 mp_storeb(y, d->buf, n);
482 static const char *rsa_lengthcheck(mp *n)
484 if (mp_bits(n) < 1020) return ("key too short");
488 static const char *rsa_enccheck(kem *k)
490 rsa_encctx *re = (rsa_encctx *)k;
492 if ((e = rsa_lengthcheck(re->rp.rp->n)) != 0) return (e);
496 static void rsa_encdestroy(kem *k)
498 rsa_encctx *re = (rsa_encctx *)k;
499 rsa_pubdestroy(&re->rp);
503 static const kemops rsa_encops = {
504 rsa_pubfetch, sizeof(rsa_pub),
505 rsa_encinit, rsa_encdoit, rsa_enccheck, rsa_encdestroy
508 typedef struct rsa_decctx {
513 static kem *rsa_decinit(key *k, void *kd)
515 rsa_decctx *rd = CREATE(rsa_decctx);
516 rsa_privcreate(&rd->rp, kd, &rand_global);
520 static int rsa_decdoit(kem *k, dstr *d, ghash *h)
522 rsa_decctx *rd = (rsa_decctx *)k;
523 mp *x = mp_loadb(MP_NEW, d->buf, d->len);
527 if (MP_CMP(x, >=, rd->rp.rp->n)) {
531 n = mp_octets(rd->rp.rp->n);
533 x = rsa_privop(&rd->rp, x, x);
541 static const char *rsa_deccheck(kem *k)
543 rsa_decctx *rd = (rsa_decctx *)k;
545 if ((e = rsa_lengthcheck(rd->rp.rp->n)) != 0) return (e);
549 static void rsa_decdestroy(kem *k)
551 rsa_decctx *rd = (rsa_decctx *)k;
552 rsa_privdestroy(&rd->rp);
556 static const kemops rsa_decops = {
557 rsa_privfetch, sizeof(rsa_priv),
558 rsa_decinit, rsa_decdoit, rsa_deccheck, rsa_decdestroy
561 /* --- DH and EC --- */
563 typedef struct dh_encctx {
570 static dh_encctx *dh_doinit(key *k, const gprime_param *gp, mp *y,
571 group *(*makegroup)(const gprime_param *),
574 dh_encctx *de = CREATE(dh_encctx);
578 if ((de->g = makegroup(gp)) == 0)
579 die(EXIT_FAILURE, "bad %s group in key `%s'", what, t.buf);
581 de->y = G_CREATE(de->g);
582 if (G_FROMINT(de->g, de->y, y))
583 die(EXIT_FAILURE, "bad public key `%s'", t.buf);
588 static dh_encctx *ec_doinit(key *k, const char *cstr, const ec *y)
590 dh_encctx *de = CREATE(dh_encctx);
596 if ((e = ec_getinfo(&ei, cstr)) != 0 ||
597 (de->g = group_ec(&ei)) == 0)
598 die(EXIT_FAILURE, "bad elliptic curve spec in key `%s': %s", t.buf, e);
600 de->y = G_CREATE(de->g);
601 if (G_FROMEC(de->g, de->y, y))
602 die(EXIT_FAILURE, "bad public curve point `%s'", t.buf);
607 static kem *dh_encinit(key *k, void *kd)
610 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_prime, "prime");
614 static kem *bindh_encinit(key *k, void *kd)
617 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_binary, "binary");
621 static kem *ec_encinit(key *k, void *kd)
624 dh_encctx *de = ec_doinit(k, ep->cstr, &ep->p);
628 static int dh_encdoit(kem *k, dstr *d, ghash *h)
630 dh_encctx *de = (dh_encctx *)k;
631 mp *r = mprand_range(MP_NEW, de->g->r, &rand_global, 0);
632 ge *x = G_CREATE(de->g);
633 ge *y = G_CREATE(de->g);
634 size_t n = de->g->noctets;
637 G_EXP(de->g, x, de->g->g, r);
638 G_EXP(de->g, y, de->y, r);
640 buf_init(&b, d->buf, n);
641 G_TORAW(de->g, &b, y);
642 GH_HASH(h, BBASE(&b), BLEN(&b));
643 buf_init(&b, d->buf, n);
644 G_TORAW(de->g, &b, x);
645 GH_HASH(h, BBASE(&b), BLEN(&b));
653 static const char *dh_enccheck(kem *k)
655 dh_encctx *de = (dh_encctx *)k;
657 if ((e = G_CHECK(de->g, &rand_global)) != 0)
659 if (group_check(de->g, de->y))
660 return ("public key not in subgroup");
664 static void dh_encdestroy(kem *k)
666 dh_encctx *de = (dh_encctx *)k;
667 G_DESTROY(de->g, de->y);
669 G_DESTROYGROUP(de->g);
673 static const kemops dh_encops = {
674 dh_pubfetch, sizeof(dh_pub),
675 dh_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
678 static const kemops bindh_encops = {
679 dh_pubfetch, sizeof(dh_pub),
680 bindh_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
683 static const kemops ec_encops = {
684 ec_pubfetch, sizeof(ec_pub),
685 ec_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
688 static kem *dh_decinit(key *k, void *kd)
691 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_prime, "prime");
692 de->x = MP_COPY(dp->x);
696 static kem *bindh_decinit(key *k, void *kd)
699 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_binary, "binary");
700 de->x = MP_COPY(dp->x);
704 static kem *ec_decinit(key *k, void *kd)
707 dh_encctx *de = ec_doinit(k, ep->cstr, &ep->p);
708 de->x = MP_COPY(ep->x);
712 static int dh_decdoit(kem *k, dstr *d, ghash *h)
714 dh_encctx *de = (dh_encctx *)k;
715 ge *x = G_CREATE(de->g);
716 size_t n = de->g->noctets;
717 void *p = xmalloc(n);
721 buf_init(&b, d->buf, d->len);
722 if (G_FROMRAW(de->g, &b, x) || group_check(de->g, x))
724 G_EXP(de->g, x, x, de->x);
726 G_TORAW(de->g, &b, x);
727 GH_HASH(h, BBASE(&b), BLEN(&b));
728 GH_HASH(h, d->buf, d->len);
736 static const kemops dh_decops = {
737 dh_privfetch, sizeof(dh_priv),
738 dh_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
741 static const kemops bindh_decops = {
742 dh_privfetch, sizeof(dh_priv),
743 bindh_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
746 static const kemops ec_decops = {
747 ec_privfetch, sizeof(ec_priv),
748 ec_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
751 /* --- X25519 and similar schemes --- */
757 #define XDHDEF(xdh, XDH) \
759 static kem *xdh##_encinit(key *k, void *kd) { return (CREATE(kem)); } \
760 static void xdh##_encdestroy(kem *k) { DESTROY(k); } \
762 static const char *xdh##_enccheck(kem *k) \
764 xdh##_pub *kd = k->kd; \
766 if (kd->pub.sz != XDH##_PUBSZ) \
767 return ("incorrect " #XDH "public key length"); \
771 static int xdh##_encdoit(kem *k, dstr *d, ghash *h) \
773 octet t[XDH##_KEYSZ], z[XDH##_OUTSZ]; \
774 xdh##_pub *kd = k->kd; \
776 rand_get(RAND_GLOBAL, t, sizeof(t)); \
777 dstr_ensure(d, XDH##_PUBSZ); \
778 xdh((octet *)d->buf, t, xdh##_base); \
779 xdh(z, t, kd->pub.k); \
780 d->len += XDH##_PUBSZ; \
781 GH_HASH(h, d->buf, XDH##_PUBSZ); \
782 GH_HASH(h, z, XDH##_OUTSZ); \
786 static const char *xdh##_deccheck(kem *k) \
788 xdh##_priv *kd = k->kd; \
790 if (kd->priv.sz != XDH##_KEYSZ) \
791 return ("incorrect " #XDH " private key length"); \
792 if (kd->pub.sz != XDH##_PUBSZ) \
793 return ("incorrect " #XDH " public key length"); \
797 static int xdh##_decdoit(kem *k, dstr *d, ghash *h) \
799 octet z[XDH##_OUTSZ]; \
800 xdh##_priv *kd = k->kd; \
803 if (d->len != XDH##_PUBSZ) goto done; \
804 xdh(z, kd->priv.k, (const octet *)d->buf); \
805 GH_HASH(h, d->buf, XDH##_PUBSZ); \
806 GH_HASH(h, z, XDH##_OUTSZ); \
812 static const kemops xdh##_encops = { \
813 xdh##_pubfetch, sizeof(xdh##_pub), \
814 xdh##_encinit, xdh##_encdoit, xdh##_enccheck, xdh##_encdestroy \
817 static const kemops xdh##_decops = { \
818 xdh##_privfetch, sizeof(xdh##_priv), \
819 xdh##_encinit, xdh##_decdoit, xdh##_deccheck, xdh##_encdestroy \
825 /* --- Symmetric --- */
827 typedef struct symm_ctx {
833 static kem *symm_init(key *k, void *kd)
839 s = CREATE(symm_ctx);
842 s->kp.e = KENC_BINARY;
846 if ((err = key_unpack(&s->kp, kd, &d)) != 0) {
847 die(EXIT_FAILURE, "failed to unpack symmetric key `%s': %s",
848 d.buf, key_strerror(err));
854 static int symm_decdoit(kem *k, dstr *d, ghash *h)
856 symm_ctx *s = (symm_ctx *)k;
858 GH_HASH(h, s->kb.k, s->kb.sz);
859 GH_HASH(h, d->buf, d->len);
863 static int symm_encdoit(kem *k, dstr *d, ghash *h)
865 dstr_ensure(d, h->ops->c->hashsz);
866 d->len += h->ops->c->hashsz;
867 rand_get(RAND_GLOBAL, d->buf, d->len);
868 return (symm_decdoit(k, d, h));
871 static const char *symm_check(kem *k) { return (0); }
873 static void symm_destroy(kem *k)
874 { symm_ctx *s = (symm_ctx *)k; key_unpackdone(&s->kp); }
876 static const kemops symm_encops = {
878 symm_init, symm_encdoit, symm_check, symm_destroy
881 static const kemops symm_decops = {
883 symm_init, symm_decdoit, symm_check, symm_destroy
886 /* --- The switch table --- */
888 const struct kemtab kemtab[] = {
889 { "rsa", &rsa_encops, &rsa_decops },
890 { "dh", &dh_encops, &dh_decops },
891 { "bindh", &bindh_encops, &bindh_decops },
892 { "ec", &ec_encops, &ec_decops },
893 #define XDHTAB(xdh, XDH) \
894 { #xdh, &xdh##_encops, &xdh##_decops },
897 { "symm", &symm_encops, &symm_decops },
901 /* --- @getkem@ --- *
903 * Arguments: @key *k@ = the key to load
904 * @const char *app@ = application name
905 * @int wantpriv@ = nonzero if we want to decrypt
906 * @bulk **bc@ = bulk crypto context to set up
908 * Returns: A key-encapsulating thing.
913 kem *getkem(key *k, const char *app, int wantpriv, bulk **bc)
915 const char *kalg, *halg = 0, *balg = 0;
922 const struct kemtab *kt;
924 const struct bulktab *bt;
930 /* --- Setup stuff --- */
934 /* --- Get the KEM name --- *
936 * Take the attribute if it's there; otherwise use the key type.
940 if ((q = key_getattr(0, k, "kem")) != 0) {
943 } else if (strncmp(k->type, app, n) == 0 && k->type[n] == '-') {
944 dstr_puts(&d, k->type);
947 die(EXIT_FAILURE, "no KEM for key `%s'", t.buf);
950 /* --- Grab the bulk encryption scheme --- *
952 * Grab it from the KEM if it's there, but override it from the attribute.
955 if (p && (p = strchr(p, '/')) != 0) {
959 if ((q = key_getattr(0, k, "bulk")) != 0)
962 /* --- Grab the hash function --- */
964 if (p && (p = strchr(p, '/')) != 0) {
968 if ((q = key_getattr(0, k, "hash")) != 0)
971 /* --- Instantiate the KEM --- */
973 for (kt = kemtab; kt->name; kt++) {
974 if (strcmp(kt->name, kalg) == 0)
977 die(EXIT_FAILURE, "key encapsulation mechanism `%s' not found in key `%s'",
980 ko = wantpriv ? kt->decops : kt->encops;
986 kd = xmalloc(ko->kdsz);
987 kp = key_fetchinit(ko->kf, 0, kd);
988 if ((e = key_fetch(kp, k)) != 0) {
989 die(EXIT_FAILURE, "error fetching key `%s': %s",
990 t.buf, key_strerror(e));
993 kk = ko->init(k, kd);
998 /* --- Set up the bulk crypto --- */
1002 else if ((kk->hc = ghash_byname(halg)) == 0) {
1003 die(EXIT_FAILURE, "hash algorithm `%s' not found in key `%s'",
1010 for (bt = bulktab, bo = 0; bt->name; bt++) {
1011 if (strcmp(balg, bt->name) == 0)
1012 { balg = 0; goto b_found; }
1013 n = strlen(bt->name);
1014 if (strncmp(balg, bt->name, n) == 0 && balg[n] == '-')
1015 { balg += n + 1; goto b_found; }
1020 bo = wantpriv ? bt->decops : bt->encops;
1021 *bc = bo->init(k, balg, kk->hc->name);
1025 if ((q = key_getattr(0, k, "kdf")) == 0) {
1026 dstr_putf(&d, "%s-mgf", kk->hc->name);
1029 if ((kk->cxc = gcipher_byname(q)) == 0) {
1030 die(EXIT_FAILURE, "encryption scheme (KDF) `%s' not found in key `%s'",
1034 /* --- Tidy up --- */
1041 /* --- @setupkem@ --- *
1043 * Arguments: @kem *k@ = key-encapsulation thing
1044 * @dstr *d@ = key-encapsulation data
1045 * @bulk *bc@ = bulk crypto context to set up
1047 * Returns: Zero on success, nonzero on failure.
1049 * Use: Initializes all the various symmetric things from a KEM.
1052 int setupkem(kem *k, dstr *d, bulk *bc)
1060 if (k->ops->doit(k, d, h))
1062 n = keysz(GH_CLASS(h)->hashsz, k->cxc->keysz);
1066 k->cx = GC_INIT(k->cxc, kd, n);
1067 bc->ops->setup(bc, k->cx);
1075 /* --- @freekem@ --- *
1077 * Arguments: @kem *k@ = key-encapsulation thing
1081 * Use: Frees up a key-encapsulation thing.
1084 void freekem(kem *k)
1089 key_fetchdone(k->kp);
1096 /*----- That's all, folks -------------------------------------------------*/