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 /* --- Authenticated encryption schemes --- */
63 typedef struct aead_encctx {
67 union { gaead_enc *enc; gaead_dec *dec; } ed;
72 static bulk *aead_internalinit(key *k, const gcaead *aec)
74 aead_encctx *ctx = CREATE(aead_encctx);
78 if ((ctx->nsz = keysz_pad(4, aec->noncesz)) == 0)
79 die(EXIT_FAILURE, "no suitable nonce size for `%s'", aec->name);
80 ctx->tsz = keysz(0, ctx->aec->tagsz);
85 static bulk *aead_init(key *k, const char *calg, const char *halg)
93 if ((q = key_getattr(0, k, "cipher")) != 0) calg = q;
94 if (!calg) aec = &chacha20_poly1305;
95 else if ((aec = gaead_byname(calg)) == 0)
96 die(EXIT_FAILURE, "AEAD scheme `%s' not found in key `%s'",
100 return (aead_internalinit(k, aec));
103 static int aead_commonsetup(aead_encctx *ctx, gcipher *cx)
107 n = ksz = keysz(0, ctx->aec->keysz);
108 if (n < ctx->nsz) n = ctx->nsz;
109 if (n < ctx->tsz) n = ctx->tsz;
112 GC_ENCRYPT(cx, 0, ctx->t, ksz);
113 ctx->key = GAEAD_KEY(ctx->aec, ctx->t, ksz);
117 static size_t aead_overhead(bulk *b)
118 { aead_encctx *ctx = (aead_encctx *)b; return (ctx->aec->ohd + ctx->tsz); }
120 static void aead_commondestroy(aead_encctx *ctx)
122 if (ctx->key) GAEAD_DESTROY(ctx->key);
127 static int aead_encsetup(bulk *b, gcipher *cx)
129 aead_encctx *ctx = (aead_encctx *)b;
130 ctx->ed.enc = 0; return (aead_commonsetup(ctx, cx));
133 static const char *aead_encdoit(bulk *b, uint32 seq, buf *bb,
134 const void *p, size_t sz)
136 aead_encctx *ctx = (aead_encctx *)b;
140 memset(ctx->t + 4, 0, ctx->nsz - 4); STORE32_B(ctx->t, seq);
142 ctx->ed.enc = GAEAD_ENC(ctx->key, ctx->t, ctx->nsz, 0, sz, ctx->tsz);
144 GAEAD_REINIT(ctx->ed.enc, ctx->t, ctx->nsz, 0, sz, ctx->tsz);
145 t = buf_get(bb, ctx->tsz); assert(t);
146 rc = GAEAD_ENCRYPT(ctx->ed.enc, p, sz, bb); assert(rc >= 0);
147 rc = GAEAD_DONE(ctx->ed.enc, 0, bb, t, ctx->tsz); assert(rc >= 0);
151 static void aead_encdestroy(bulk *b)
153 aead_encctx *ctx = (aead_encctx *)b;
154 if (ctx->ed.enc) GAEAD_DESTROY(ctx->ed.enc);
155 aead_commondestroy(ctx);
158 static int aead_decsetup(bulk *b, gcipher *cx)
160 aead_encctx *ctx = (aead_encctx *)b;
161 ctx->ed.dec = 0; return (aead_commonsetup(ctx, cx));
164 static const char *aead_decdoit(bulk *b, uint32 seq, buf *bb,
165 const void *p, size_t sz)
167 aead_encctx *ctx = (aead_encctx *)b;
172 memset(ctx->t + 4, 0, ctx->nsz - 4); STORE32_B(ctx->t, seq);
174 ctx->ed.dec = GAEAD_DEC(ctx->key, ctx->t, ctx->nsz, 0, sz, ctx->tsz);
176 GAEAD_REINIT(ctx->ed.enc, ctx->t, ctx->nsz, 0, sz, ctx->tsz);
178 buf_init(&bin, (/*unconst*/ void *)p, sz);
179 t = buf_get(&bin, ctx->tsz); if (!t) return ("no tag");
180 rc = GAEAD_DECRYPT(ctx->ed.dec, BCUR(&bin), BLEFT(&bin), bb);
182 rc = GAEAD_DONE(ctx->ed.dec, 0, bb, t, ctx->tsz); assert(rc >= 0);
183 if (!rc) return ("authentication failure");
187 static void aead_decdestroy(bulk *b)
189 aead_encctx *ctx = (aead_encctx *)b;
190 if (ctx->ed.dec) GAEAD_DESTROY(ctx->ed.dec);
191 aead_commondestroy(ctx);
194 static const struct bulkops aead_encops = {
195 aead_init, aead_encsetup, aead_overhead,
196 aead_encdoit, aead_encdestroy
198 aead_init, aead_decsetup, aead_overhead,
199 aead_decdoit, aead_decdestroy
202 /* --- NaCl `secretbox' in terms of AEAD --- */
204 static bulk *naclbox_init(key *k, const char *calg, const char *halg)
212 if ((q = key_getattr(0, k, "cipher")) != 0) calg = q;
213 if (!calg || strcmp(calg, "salsa20") == 0) aec = &salsa20_naclbox;
214 else if (strcmp(calg, "salsa20/12") == 0) aec = &salsa2012_naclbox;
215 else if (strcmp(calg, "salsa20/8") == 0) aec = &salsa208_naclbox;
216 else if (strcmp(calg, "chacha20") == 0) aec = &chacha20_naclbox;
217 else if (strcmp(calg, "chacha12") == 0) aec = &chacha12_naclbox;
218 else if (strcmp(calg, "chacha8") == 0) aec = &chacha8_naclbox;
221 "unknown or inappropriate encryption scheme `%s' in key `%s'",
226 return (aead_internalinit(k, aec));
229 static const bulkops naclbox_encops = {
230 naclbox_init, aead_encsetup, aead_overhead,
231 aead_encdoit, aead_encdestroy
232 }, naclbox_decops = {
233 naclbox_init, aead_decsetup, aead_overhead,
234 aead_decdoit, aead_decdestroy
237 /* --- Generic composition --- */
239 typedef struct gencomp_encctx {
245 octet *t; size_t tsz;
248 static bulk *gencomp_init(key *k, const char *calg, const char *halg)
250 gencomp_encctx *ctx = CREATE(gencomp_encctx);
252 dstr d = DSTR_INIT, t = DSTR_INIT;
256 if ((q = key_getattr(0, k, "cipher")) != 0) calg = q;
257 if (!calg) ctx->cc = &blowfish_cbc;
258 else if ((ctx->cc = gcipher_byname(calg)) == 0) {
259 die(EXIT_FAILURE, "encryption scheme `%s' not found in key `%s'",
264 if ((q = key_getattr(0, k, "mac")) == 0) {
265 dstr_putf(&d, "%s-hmac", halg);
268 if ((ctx->mc = gmac_byname(q)) == 0) {
270 "message authentication code `%s' not found in key `%s'",
277 static int gencomp_setup(bulk *b, gcipher *cx)
279 gencomp_encctx *ctx = (gencomp_encctx *)b;
285 cn = keysz(0, ctx->cc->keysz); if (cn > n) n = cn;
286 mn = keysz(0, ctx->mc->keysz); if (mn > n) n = mn;
287 ctx->t = kd = xmalloc(n); ctx->tsz = n;
288 GC_ENCRYPT(cx, 0, kd, cn);
289 ctx->c = GC_INIT(ctx->cc, kd, cn);
290 GC_ENCRYPT(cx, 0, kd, mn);
291 ctx->m = GM_KEY(ctx->mc, kd, mn);
295 static size_t gencomp_overhead(bulk *b)
297 gencomp_encctx *ctx = (gencomp_encctx *)b;
298 return (ctx->cc->blksz + ctx->mc->hashsz); }
300 static void gencomp_destroy(bulk *b)
302 gencomp_encctx *ctx = (gencomp_encctx *)b;
310 static const char *gencomp_encdoit(bulk *b, uint32 seq, buf *bb,
311 const void *p, size_t sz)
313 gencomp_encctx *ctx = (gencomp_encctx *)b;
315 ghash *h = GM_INIT(ctx->m);
318 if (ctx->cc->blksz) {
319 GC_ENCRYPT(ctx->cx, 0, ctx->t, ctx->cc->blksz);
320 GC_SETIV(ctx->c, ctx->t);
322 tag = buf_get(bb, ctx->mc->hashsz); assert(tag);
323 ct = buf_get(bb, sz); assert(ct);
324 GC_ENCRYPT(ctx->c, p, ct, sz);
331 static const char *gencomp_decdoit(bulk *b, uint32 seq, buf *bb,
332 const void *p, size_t sz)
334 gencomp_encctx *ctx = (gencomp_encctx *)b;
336 const octet *tag, *ct;
341 buf_init(&bin, (/*unconst*/ void *)p, sz);
342 if ((tag = buf_get(&bin, ctx->mc->hashsz)) == 0) return ("no tag");
343 ct = BCUR(&bin); sz = BLEFT(&bin);
344 pt = buf_get(bb, sz); assert(pt);
349 ok = ct_memeq(tag, GH_DONE(h, 0), ctx->mc->hashsz);
351 if (!ok) return ("authentication failure");
353 if (ctx->cc->blksz) {
354 GC_ENCRYPT(ctx->cx, 0, ctx->t, ctx->cc->blksz);
355 GC_SETIV(ctx->c, ctx->t);
357 GC_DECRYPT(ctx->c, ct, pt, sz);
361 static const bulkops gencomp_encops = {
362 gencomp_init, gencomp_setup, gencomp_overhead,
363 gencomp_encdoit, gencomp_destroy
364 }, gencomp_decops = {
365 gencomp_init, gencomp_setup, gencomp_overhead,
366 gencomp_decdoit, gencomp_destroy
369 const struct bulktab bulktab[] = {
370 { "gencomp", &gencomp_encops, &gencomp_decops },
371 { "naclbox", &naclbox_encops, &naclbox_decops },
372 { "aead", &aead_encops, &aead_decops },
376 /*----- Key encapsulation -------------------------------------------------*/
380 typedef struct rsa_encctx {
385 static kem *rsa_encinit(key *k, void *kd)
387 rsa_encctx *re = CREATE(rsa_encctx);
388 rsa_pubcreate(&re->rp, kd);
392 static int rsa_encdoit(kem *k, dstr *d, ghash *h)
394 rsa_encctx *re = (rsa_encctx *)k;
395 mp *x = mprand_range(MP_NEW, re->rp.rp->n, &rand_global, 0);
396 mp *y = rsa_pubop(&re->rp, MP_NEW, x);
397 size_t n = mp_octets(re->rp.rp->n);
399 mp_storeb(x, d->buf, n);
400 GH_HASH(h, d->buf, n);
401 mp_storeb(y, d->buf, n);
408 static const char *rsa_lengthcheck(mp *n)
410 if (mp_bits(n) < 1020) return ("key too short");
414 static const char *rsa_enccheck(kem *k)
416 rsa_encctx *re = (rsa_encctx *)k;
418 if ((e = rsa_lengthcheck(re->rp.rp->n)) != 0) return (e);
422 static void rsa_encdestroy(kem *k)
424 rsa_encctx *re = (rsa_encctx *)k;
425 rsa_pubdestroy(&re->rp);
429 static const kemops rsa_encops = {
430 rsa_pubfetch, sizeof(rsa_pub),
431 rsa_encinit, rsa_encdoit, rsa_enccheck, rsa_encdestroy
434 typedef struct rsa_decctx {
439 static kem *rsa_decinit(key *k, void *kd)
441 rsa_decctx *rd = CREATE(rsa_decctx);
442 rsa_privcreate(&rd->rp, kd, &rand_global);
446 static int rsa_decdoit(kem *k, dstr *d, ghash *h)
448 rsa_decctx *rd = (rsa_decctx *)k;
449 mp *x = mp_loadb(MP_NEW, d->buf, d->len);
453 if (MP_CMP(x, >=, rd->rp.rp->n)) {
457 n = mp_octets(rd->rp.rp->n);
459 x = rsa_privop(&rd->rp, x, x);
467 static const char *rsa_deccheck(kem *k)
469 rsa_decctx *rd = (rsa_decctx *)k;
471 if ((e = rsa_lengthcheck(rd->rp.rp->n)) != 0) return (e);
475 static void rsa_decdestroy(kem *k)
477 rsa_decctx *rd = (rsa_decctx *)k;
478 rsa_privdestroy(&rd->rp);
482 static const kemops rsa_decops = {
483 rsa_privfetch, sizeof(rsa_priv),
484 rsa_decinit, rsa_decdoit, rsa_deccheck, rsa_decdestroy
487 /* --- DH and EC --- */
489 typedef struct dh_encctx {
496 static dh_encctx *dh_doinit(key *k, const gprime_param *gp, mp *y,
497 group *(*makegroup)(const gprime_param *),
500 dh_encctx *de = CREATE(dh_encctx);
504 if ((de->g = makegroup(gp)) == 0)
505 die(EXIT_FAILURE, "bad %s group in key `%s'", what, t.buf);
507 de->y = G_CREATE(de->g);
508 if (G_FROMINT(de->g, de->y, y))
509 die(EXIT_FAILURE, "bad public key `%s'", t.buf);
514 static dh_encctx *ec_doinit(key *k, const char *cstr, const ec *y)
516 dh_encctx *de = CREATE(dh_encctx);
522 if ((e = ec_getinfo(&ei, cstr)) != 0 ||
523 (de->g = group_ec(&ei)) == 0)
524 die(EXIT_FAILURE, "bad elliptic curve spec in key `%s': %s", t.buf, e);
526 de->y = G_CREATE(de->g);
527 if (G_FROMEC(de->g, de->y, y))
528 die(EXIT_FAILURE, "bad public curve point `%s'", t.buf);
533 static kem *dh_encinit(key *k, void *kd)
536 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_prime, "prime");
540 static kem *bindh_encinit(key *k, void *kd)
543 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_binary, "binary");
547 static kem *ec_encinit(key *k, void *kd)
550 dh_encctx *de = ec_doinit(k, ep->cstr, &ep->p);
554 static int dh_encdoit(kem *k, dstr *d, ghash *h)
556 dh_encctx *de = (dh_encctx *)k;
557 mp *r = mprand_range(MP_NEW, de->g->r, &rand_global, 0);
558 ge *x = G_CREATE(de->g);
559 ge *y = G_CREATE(de->g);
560 size_t n = de->g->noctets;
563 G_EXP(de->g, x, de->g->g, r);
564 G_EXP(de->g, y, de->y, r);
566 buf_init(&b, d->buf, n);
567 G_TORAW(de->g, &b, y);
568 GH_HASH(h, BBASE(&b), BLEN(&b));
569 buf_init(&b, d->buf, n);
570 G_TORAW(de->g, &b, x);
571 GH_HASH(h, BBASE(&b), BLEN(&b));
579 static const char *dh_enccheck(kem *k)
581 dh_encctx *de = (dh_encctx *)k;
583 if ((e = G_CHECK(de->g, &rand_global)) != 0)
585 if (group_check(de->g, de->y))
586 return ("public key not in subgroup");
590 static void dh_encdestroy(kem *k)
592 dh_encctx *de = (dh_encctx *)k;
593 G_DESTROY(de->g, de->y);
595 G_DESTROYGROUP(de->g);
599 static const kemops dh_encops = {
600 dh_pubfetch, sizeof(dh_pub),
601 dh_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
604 static const kemops bindh_encops = {
605 dh_pubfetch, sizeof(dh_pub),
606 bindh_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
609 static const kemops ec_encops = {
610 ec_pubfetch, sizeof(ec_pub),
611 ec_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
614 static kem *dh_decinit(key *k, void *kd)
617 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_prime, "prime");
618 de->x = MP_COPY(dp->x);
622 static kem *bindh_decinit(key *k, void *kd)
625 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_binary, "binary");
626 de->x = MP_COPY(dp->x);
630 static kem *ec_decinit(key *k, void *kd)
633 dh_encctx *de = ec_doinit(k, ep->cstr, &ep->p);
634 de->x = MP_COPY(ep->x);
638 static int dh_decdoit(kem *k, dstr *d, ghash *h)
640 dh_encctx *de = (dh_encctx *)k;
641 ge *x = G_CREATE(de->g);
642 size_t n = de->g->noctets;
643 void *p = xmalloc(n);
647 buf_init(&b, d->buf, d->len);
648 if (G_FROMRAW(de->g, &b, x) || group_check(de->g, x))
650 G_EXP(de->g, x, x, de->x);
652 G_TORAW(de->g, &b, x);
653 GH_HASH(h, BBASE(&b), BLEN(&b));
654 GH_HASH(h, d->buf, d->len);
662 static const kemops dh_decops = {
663 dh_privfetch, sizeof(dh_priv),
664 dh_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
667 static const kemops bindh_decops = {
668 dh_privfetch, sizeof(dh_priv),
669 bindh_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
672 static const kemops ec_decops = {
673 ec_privfetch, sizeof(ec_priv),
674 ec_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
677 /* --- X25519 and similar schemes --- */
683 #define XDHDEF(xdh, XDH) \
685 static kem *xdh##_encinit(key *k, void *kd) { return (CREATE(kem)); } \
686 static void xdh##_encdestroy(kem *k) { DESTROY(k); } \
688 static const char *xdh##_enccheck(kem *k) \
690 xdh##_pub *kd = k->kd; \
692 if (kd->pub.sz != XDH##_PUBSZ) \
693 return ("incorrect " #XDH "public key length"); \
697 static int xdh##_encdoit(kem *k, dstr *d, ghash *h) \
699 octet t[XDH##_KEYSZ], z[XDH##_OUTSZ]; \
700 xdh##_pub *kd = k->kd; \
702 rand_get(RAND_GLOBAL, t, sizeof(t)); \
703 dstr_ensure(d, XDH##_PUBSZ); \
704 xdh((octet *)d->buf, t, xdh##_base); \
705 xdh(z, t, kd->pub.k); \
706 d->len += XDH##_PUBSZ; \
707 GH_HASH(h, d->buf, XDH##_PUBSZ); \
708 GH_HASH(h, z, XDH##_OUTSZ); \
712 static const char *xdh##_deccheck(kem *k) \
714 xdh##_priv *kd = k->kd; \
716 if (kd->priv.sz != XDH##_KEYSZ) \
717 return ("incorrect " #XDH " private key length"); \
718 if (kd->pub.sz != XDH##_PUBSZ) \
719 return ("incorrect " #XDH " public key length"); \
723 static int xdh##_decdoit(kem *k, dstr *d, ghash *h) \
725 octet z[XDH##_OUTSZ]; \
726 xdh##_priv *kd = k->kd; \
729 if (d->len != XDH##_PUBSZ) goto done; \
730 xdh(z, kd->priv.k, (const octet *)d->buf); \
731 GH_HASH(h, d->buf, XDH##_PUBSZ); \
732 GH_HASH(h, z, XDH##_OUTSZ); \
738 static const kemops xdh##_encops = { \
739 xdh##_pubfetch, sizeof(xdh##_pub), \
740 xdh##_encinit, xdh##_encdoit, xdh##_enccheck, xdh##_encdestroy \
743 static const kemops xdh##_decops = { \
744 xdh##_privfetch, sizeof(xdh##_priv), \
745 xdh##_encinit, xdh##_decdoit, xdh##_deccheck, xdh##_encdestroy \
751 /* --- Symmetric --- */
753 typedef struct symm_ctx {
759 static kem *symm_init(key *k, void *kd)
765 s = CREATE(symm_ctx);
768 s->kp.e = KENC_BINARY;
772 if ((err = key_unpack(&s->kp, kd, &d)) != 0) {
773 die(EXIT_FAILURE, "failed to unpack symmetric key `%s': %s",
774 d.buf, key_strerror(err));
780 static int symm_decdoit(kem *k, dstr *d, ghash *h)
782 symm_ctx *s = (symm_ctx *)k;
784 GH_HASH(h, s->kb.k, s->kb.sz);
785 GH_HASH(h, d->buf, d->len);
789 static int symm_encdoit(kem *k, dstr *d, ghash *h)
791 dstr_ensure(d, h->ops->c->hashsz);
792 d->len += h->ops->c->hashsz;
793 rand_get(RAND_GLOBAL, d->buf, d->len);
794 return (symm_decdoit(k, d, h));
797 static const char *symm_check(kem *k) { return (0); }
799 static void symm_destroy(kem *k)
800 { symm_ctx *s = (symm_ctx *)k; key_unpackdone(&s->kp); }
802 static const kemops symm_encops = {
804 symm_init, symm_encdoit, symm_check, symm_destroy
807 static const kemops symm_decops = {
809 symm_init, symm_decdoit, symm_check, symm_destroy
812 /* --- The switch table --- */
814 const struct kemtab kemtab[] = {
815 { "rsa", &rsa_encops, &rsa_decops },
816 { "dh", &dh_encops, &dh_decops },
817 { "bindh", &bindh_encops, &bindh_decops },
818 { "ec", &ec_encops, &ec_decops },
819 #define XDHTAB(xdh, XDH) \
820 { #xdh, &xdh##_encops, &xdh##_decops },
823 { "symm", &symm_encops, &symm_decops },
827 /* --- @getkem@ --- *
829 * Arguments: @key *k@ = the key to load
830 * @const char *app@ = application name
831 * @int wantpriv@ = nonzero if we want to decrypt
832 * @bulk **bc@ = bulk crypto context to set up
834 * Returns: A key-encapsulating thing.
839 kem *getkem(key *k, const char *app, int wantpriv, bulk **bc)
841 const char *kalg, *halg = 0, *balg = 0;
848 const struct kemtab *kt;
850 const struct bulktab *bt;
856 /* --- Setup stuff --- */
860 /* --- Get the KEM name --- *
862 * Take the attribute if it's there; otherwise use the key type.
866 if ((q = key_getattr(0, k, "kem")) != 0) {
869 } else if (strncmp(k->type, app, n) == 0 && k->type[n] == '-') {
870 dstr_puts(&d, k->type);
873 die(EXIT_FAILURE, "no KEM for key `%s'", t.buf);
876 /* --- Grab the bulk encryption scheme --- *
878 * Grab it from the KEM if it's there, but override it from the attribute.
881 if (p && (p = strchr(p, '/')) != 0) {
885 if ((q = key_getattr(0, k, "bulk")) != 0)
888 /* --- Grab the hash function --- */
890 if (p && (p = strchr(p, '/')) != 0) {
894 if ((q = key_getattr(0, k, "hash")) != 0)
897 /* --- Instantiate the KEM --- */
899 for (kt = kemtab; kt->name; kt++) {
900 if (strcmp(kt->name, kalg) == 0)
903 die(EXIT_FAILURE, "key encapsulation mechanism `%s' not found in key `%s'",
906 ko = wantpriv ? kt->decops : kt->encops;
912 kd = xmalloc(ko->kdsz);
913 kp = key_fetchinit(ko->kf, 0, kd);
914 if ((e = key_fetch(kp, k)) != 0) {
915 die(EXIT_FAILURE, "error fetching key `%s': %s",
916 t.buf, key_strerror(e));
919 kk = ko->init(k, kd);
924 /* --- Set up the bulk crypto --- */
928 else if ((kk->hc = ghash_byname(halg)) == 0) {
929 die(EXIT_FAILURE, "hash algorithm `%s' not found in key `%s'",
936 for (bt = bulktab, bo = 0; bt->name; bt++) {
937 if (strcmp(balg, bt->name) == 0)
938 { balg = 0; goto b_found; }
939 n = strlen(bt->name);
940 if (strncmp(balg, bt->name, n) == 0 && balg[n] == '-')
941 { balg += n + 1; goto b_found; }
946 bo = wantpriv ? bt->decops : bt->encops;
947 *bc = bo->init(k, balg, kk->hc->name);
951 if ((q = key_getattr(0, k, "kdf")) == 0) {
952 dstr_putf(&d, "%s-mgf", kk->hc->name);
955 if ((kk->cxc = gcipher_byname(q)) == 0) {
956 die(EXIT_FAILURE, "encryption scheme (KDF) `%s' not found in key `%s'",
960 /* --- Tidy up --- */
967 /* --- @setupkem@ --- *
969 * Arguments: @kem *k@ = key-encapsulation thing
970 * @dstr *d@ = key-encapsulation data
971 * @bulk *bc@ = bulk crypto context to set up
973 * Returns: Zero on success, nonzero on failure.
975 * Use: Initializes all the various symmetric things from a KEM.
978 int setupkem(kem *k, dstr *d, bulk *bc)
986 if (k->ops->doit(k, d, h))
988 n = keysz(GH_CLASS(h)->hashsz, k->cxc->keysz);
992 k->cx = GC_INIT(k->cxc, kd, n);
993 bc->ops->setup(bc, k->cx);
1001 /* --- @freekem@ --- *
1003 * Arguments: @kem *k@ = key-encapsulation thing
1007 * Use: Frees up a key-encapsulation thing.
1010 void freekem(kem *k)
1015 key_fetchdone(k->kp);
1022 /*----- That's all, folks -------------------------------------------------*/