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>
48 #include "blowfish-cbc.h"
55 /*----- Bulk crypto -------------------------------------------------------*/
57 /* --- NaCl `secretbox' --- */
59 typedef struct naclbox_encctx {
65 static bulk *naclbox_init(key *k, const char *calg, const char *halg)
67 naclbox_encctx *ctx = CREATE(naclbox_encctx);
73 if ((q = key_getattr(0, k, "cipher")) != 0) calg = q;
74 if (!calg || strcmp(calg, "salsa20") == 0) ctx->cc = &salsa20;
75 else if (strcmp(calg, "salsa20/12") == 0) ctx->cc = &salsa2012;
76 else if (strcmp(calg, "salsa20/8") == 0) ctx->cc = &salsa208;
77 else if (strcmp(calg, "chacha20") == 0) ctx->cc = &chacha20;
78 else if (strcmp(calg, "chacha12") == 0) ctx->cc = &chacha12;
79 else if (strcmp(calg, "chacha8") == 0) ctx->cc = &chacha8;
82 "unknown or inappropriate encryption scheme `%s' in key `%s'",
90 static int naclbox_setup(bulk *b, gcipher *cx)
92 naclbox_encctx *ctx = (naclbox_encctx *)b;
93 octet k[SALSA20_KEYSZ];
95 GC_ENCRYPT(cx, 0, k, sizeof(k));
96 ctx->c = GC_INIT(ctx->cc, k, sizeof(k));
100 static size_t naclbox_overhead(bulk *b) { return (POLY1305_TAGSZ); }
102 static void naclbox_destroy(bulk *b)
104 naclbox_encctx *ctx = (naclbox_encctx *)b;
110 static const char *naclbox_encdoit(bulk *b, uint32 seq, buf *bb,
111 const void *p, size_t sz)
113 naclbox_encctx *ctx = (naclbox_encctx *)b;
119 STORE32(t, seq); STORE32(t + 4, 0); GC_SETIV(ctx->c, t);
120 GC_ENCRYPT(ctx->c, 0, t, POLY1305_KEYSZ + POLY1305_MASKSZ);
121 poly1305_keyinit(&ak, t, POLY1305_KEYSZ);
122 poly1305_macinit(&a, &ak, t + POLY1305_KEYSZ);
124 tag = buf_get(bb, POLY1305_TAGSZ); assert(tag);
125 ct = buf_get(bb, sz); assert(ct);
126 GC_ENCRYPT(ctx->c, p, ct, sz);
127 poly1305_hash(&a, ct, sz);
128 poly1305_done(&a, tag);
132 static const char *naclbox_decdoit(bulk *b, uint32 seq, buf *bb,
133 const void *p, size_t sz)
135 naclbox_encctx *ctx = (naclbox_encctx *)b;
140 octet *tag, *ct, *pt;
142 STORE32(t, seq); STORE32(t + 4, 0); GC_SETIV(ctx->c, t);
143 GC_ENCRYPT(ctx->c, 0, t, POLY1305_KEYSZ + POLY1305_MASKSZ);
144 poly1305_keyinit(&ak, t, POLY1305_KEYSZ);
145 poly1305_macinit(&a, &ak, t + POLY1305_KEYSZ);
147 buf_init(&bin, (/*unconst*/ void *)p, sz);
148 if ((tag = buf_get(&bin, POLY1305_TAGSZ)) == 0) return ("no tag");
149 ct = BCUR(&bin); sz = BLEFT(&bin);
150 poly1305_hash(&a, ct, sz);
151 poly1305_done(&a, t);
152 if (!ct_memeq(t, tag, POLY1305_TAGSZ)) return ("authentication failure");
153 pt = buf_get(bb, sz); assert(pt);
154 GC_DECRYPT(ctx->c, ct, pt, sz);
158 static const bulkops naclbox_encops = {
159 naclbox_init, naclbox_setup, naclbox_overhead,
160 naclbox_encdoit, naclbox_destroy
161 }, naclbox_decops = {
162 naclbox_init, naclbox_setup, naclbox_overhead,
163 naclbox_decdoit, naclbox_destroy
166 /* --- Generic composition --- */
168 typedef struct gencomp_encctx {
174 octet *t; size_t tsz;
177 static bulk *gencomp_init(key *k, const char *calg, const char *halg)
179 gencomp_encctx *ctx = CREATE(gencomp_encctx);
181 dstr d = DSTR_INIT, t = DSTR_INIT;
185 if ((q = key_getattr(0, k, "cipher")) != 0) calg = q;
186 if (!calg) ctx->cc = &blowfish_cbc;
187 else if ((ctx->cc = gcipher_byname(calg)) == 0) {
188 die(EXIT_FAILURE, "encryption scheme `%s' not found in key `%s'",
193 if ((q = key_getattr(0, k, "mac")) == 0) {
194 dstr_putf(&d, "%s-hmac", halg);
197 if ((ctx->mc = gmac_byname(q)) == 0) {
199 "message authentication code `%s' not found in key `%s'",
206 static int gencomp_setup(bulk *b, gcipher *cx)
208 gencomp_encctx *ctx = (gencomp_encctx *)b;
214 cn = keysz(0, ctx->cc->keysz); if (cn > n) n = cn;
215 mn = keysz(0, ctx->mc->keysz); if (mn > n) n = mn;
216 ctx->t = kd = xmalloc(n); ctx->tsz = n;
217 GC_ENCRYPT(cx, 0, kd, cn);
218 ctx->c = GC_INIT(ctx->cc, kd, cn);
219 GC_ENCRYPT(cx, 0, kd, mn);
220 ctx->m = GM_KEY(ctx->mc, kd, mn);
224 static size_t gencomp_overhead(bulk *b)
226 gencomp_encctx *ctx = (gencomp_encctx *)b;
227 return (ctx->cc->blksz + ctx->mc->hashsz); }
229 static void gencomp_destroy(bulk *b)
231 gencomp_encctx *ctx = (gencomp_encctx *)b;
239 static const char *gencomp_encdoit(bulk *b, uint32 seq, buf *bb,
240 const void *p, size_t sz)
242 gencomp_encctx *ctx = (gencomp_encctx *)b;
244 ghash *h = GM_INIT(ctx->m);
247 if (ctx->cc->blksz) {
248 GC_ENCRYPT(ctx->cx, 0, ctx->t, ctx->cc->blksz);
249 GC_SETIV(ctx->c, ctx->t);
251 tag = buf_get(bb, ctx->mc->hashsz); assert(tag);
252 ct = buf_get(bb, sz); assert(ct);
253 GC_ENCRYPT(ctx->c, p, ct, sz);
260 static const char *gencomp_decdoit(bulk *b, uint32 seq, buf *bb,
261 const void *p, size_t sz)
263 gencomp_encctx *ctx = (gencomp_encctx *)b;
265 const octet *tag, *ct;
270 buf_init(&bin, (/*unconst*/ void *)p, sz);
271 if ((tag = buf_get(&bin, ctx->mc->hashsz)) == 0) return ("no tag");
272 ct = BCUR(&bin); sz = BLEFT(&bin);
273 pt = buf_get(bb, sz); assert(pt);
278 ok = ct_memeq(tag, GH_DONE(h, 0), ctx->mc->hashsz);
280 if (!ok) return ("authentication failure");
282 if (ctx->cc->blksz) {
283 GC_ENCRYPT(ctx->cx, 0, ctx->t, ctx->cc->blksz);
284 GC_SETIV(ctx->c, ctx->t);
286 GC_DECRYPT(ctx->c, ct, pt, sz);
290 static const bulkops gencomp_encops = {
291 gencomp_init, gencomp_setup, gencomp_overhead,
292 gencomp_encdoit, gencomp_destroy
293 }, gencomp_decops = {
294 gencomp_init, gencomp_setup, gencomp_overhead,
295 gencomp_decdoit, gencomp_destroy
298 const struct bulktab bulktab[] = {
299 { "gencomp", &gencomp_encops, &gencomp_decops },
300 { "naclbox", &naclbox_encops, &naclbox_decops },
304 /*----- Key encapsulation -------------------------------------------------*/
308 typedef struct rsa_encctx {
313 static kem *rsa_encinit(key *k, void *kd)
315 rsa_encctx *re = CREATE(rsa_encctx);
316 rsa_pubcreate(&re->rp, kd);
320 static int rsa_encdoit(kem *k, dstr *d, ghash *h)
322 rsa_encctx *re = (rsa_encctx *)k;
323 mp *x = mprand_range(MP_NEW, re->rp.rp->n, &rand_global, 0);
324 mp *y = rsa_pubop(&re->rp, MP_NEW, x);
325 size_t n = mp_octets(re->rp.rp->n);
327 mp_storeb(x, d->buf, n);
328 GH_HASH(h, d->buf, n);
329 mp_storeb(y, d->buf, n);
336 static const char *rsa_lengthcheck(mp *n)
338 if (mp_bits(n) < 1020) return ("key too short");
342 static const char *rsa_enccheck(kem *k)
344 rsa_encctx *re = (rsa_encctx *)k;
346 if ((e = rsa_lengthcheck(re->rp.rp->n)) != 0) return (e);
350 static void rsa_encdestroy(kem *k)
352 rsa_encctx *re = (rsa_encctx *)k;
353 rsa_pubdestroy(&re->rp);
357 static const kemops rsa_encops = {
358 rsa_pubfetch, sizeof(rsa_pub),
359 rsa_encinit, rsa_encdoit, rsa_enccheck, rsa_encdestroy
362 typedef struct rsa_decctx {
367 static kem *rsa_decinit(key *k, void *kd)
369 rsa_decctx *rd = CREATE(rsa_decctx);
370 rsa_privcreate(&rd->rp, kd, &rand_global);
374 static int rsa_decdoit(kem *k, dstr *d, ghash *h)
376 rsa_decctx *rd = (rsa_decctx *)k;
377 mp *x = mp_loadb(MP_NEW, d->buf, d->len);
381 if (MP_CMP(x, >=, rd->rp.rp->n)) {
385 n = mp_octets(rd->rp.rp->n);
387 x = rsa_privop(&rd->rp, x, x);
395 static const char *rsa_deccheck(kem *k)
397 rsa_decctx *rd = (rsa_decctx *)k;
399 if ((e = rsa_lengthcheck(rd->rp.rp->n)) != 0) return (e);
403 static void rsa_decdestroy(kem *k)
405 rsa_decctx *rd = (rsa_decctx *)k;
406 rsa_privdestroy(&rd->rp);
410 static const kemops rsa_decops = {
411 rsa_privfetch, sizeof(rsa_priv),
412 rsa_decinit, rsa_decdoit, rsa_deccheck, rsa_decdestroy
415 /* --- DH and EC --- */
417 typedef struct dh_encctx {
424 static dh_encctx *dh_doinit(key *k, const gprime_param *gp, mp *y,
425 group *(*makegroup)(const gprime_param *),
428 dh_encctx *de = CREATE(dh_encctx);
432 if ((de->g = makegroup(gp)) == 0)
433 die(EXIT_FAILURE, "bad %s group in key `%s'", what, t.buf);
435 de->y = G_CREATE(de->g);
436 if (G_FROMINT(de->g, de->y, y))
437 die(EXIT_FAILURE, "bad public key `%s'", t.buf);
442 static dh_encctx *ec_doinit(key *k, const char *cstr, const ec *y)
444 dh_encctx *de = CREATE(dh_encctx);
450 if ((e = ec_getinfo(&ei, cstr)) != 0 ||
451 (de->g = group_ec(&ei)) == 0)
452 die(EXIT_FAILURE, "bad elliptic curve spec in key `%s': %s", t.buf, e);
454 de->y = G_CREATE(de->g);
455 if (G_FROMEC(de->g, de->y, y))
456 die(EXIT_FAILURE, "bad public curve point `%s'", t.buf);
461 static kem *dh_encinit(key *k, void *kd)
464 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_prime, "prime");
468 static kem *bindh_encinit(key *k, void *kd)
471 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_binary, "binary");
475 static kem *ec_encinit(key *k, void *kd)
478 dh_encctx *de = ec_doinit(k, ep->cstr, &ep->p);
482 static int dh_encdoit(kem *k, dstr *d, ghash *h)
484 dh_encctx *de = (dh_encctx *)k;
485 mp *r = mprand_range(MP_NEW, de->g->r, &rand_global, 0);
486 ge *x = G_CREATE(de->g);
487 ge *y = G_CREATE(de->g);
488 size_t n = de->g->noctets;
491 G_EXP(de->g, x, de->g->g, r);
492 G_EXP(de->g, y, de->y, r);
494 buf_init(&b, d->buf, n);
495 G_TORAW(de->g, &b, y);
496 GH_HASH(h, BBASE(&b), BLEN(&b));
497 buf_init(&b, d->buf, n);
498 G_TORAW(de->g, &b, x);
499 GH_HASH(h, BBASE(&b), BLEN(&b));
507 static const char *dh_enccheck(kem *k)
509 dh_encctx *de = (dh_encctx *)k;
511 if ((e = G_CHECK(de->g, &rand_global)) != 0)
513 if (group_check(de->g, de->y))
514 return ("public key not in subgroup");
518 static void dh_encdestroy(kem *k)
520 dh_encctx *de = (dh_encctx *)k;
521 G_DESTROY(de->g, de->y);
523 G_DESTROYGROUP(de->g);
527 static const kemops dh_encops = {
528 dh_pubfetch, sizeof(dh_pub),
529 dh_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
532 static const kemops bindh_encops = {
533 dh_pubfetch, sizeof(dh_pub),
534 bindh_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
537 static const kemops ec_encops = {
538 ec_pubfetch, sizeof(ec_pub),
539 ec_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
542 static kem *dh_decinit(key *k, void *kd)
545 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_prime, "prime");
546 de->x = MP_COPY(dp->x);
550 static kem *bindh_decinit(key *k, void *kd)
553 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_binary, "binary");
554 de->x = MP_COPY(dp->x);
558 static kem *ec_decinit(key *k, void *kd)
561 dh_encctx *de = ec_doinit(k, ep->cstr, &ep->p);
562 de->x = MP_COPY(ep->x);
566 static int dh_decdoit(kem *k, dstr *d, ghash *h)
568 dh_encctx *de = (dh_encctx *)k;
569 ge *x = G_CREATE(de->g);
570 size_t n = de->g->noctets;
571 void *p = xmalloc(n);
575 buf_init(&b, d->buf, d->len);
576 if (G_FROMRAW(de->g, &b, x) || group_check(de->g, x))
578 G_EXP(de->g, x, x, de->x);
580 G_TORAW(de->g, &b, x);
581 GH_HASH(h, BBASE(&b), BLEN(&b));
582 GH_HASH(h, d->buf, d->len);
590 static const kemops dh_decops = {
591 dh_privfetch, sizeof(dh_priv),
592 dh_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
595 static const kemops bindh_decops = {
596 dh_privfetch, sizeof(dh_priv),
597 bindh_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
600 static const kemops ec_decops = {
601 ec_privfetch, sizeof(ec_priv),
602 ec_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
605 /* --- Symmetric --- */
607 typedef struct symm_ctx {
613 static kem *symm_init(key *k, void *kd)
619 s = CREATE(symm_ctx);
622 s->kp.e = KENC_BINARY;
626 if ((err = key_unpack(&s->kp, kd, &d)) != 0) {
627 die(EXIT_FAILURE, "failed to unpack symmetric key `%s': %s",
628 d.buf, key_strerror(err));
634 static int symm_decdoit(kem *k, dstr *d, ghash *h)
636 symm_ctx *s = (symm_ctx *)k;
638 GH_HASH(h, s->kb.k, s->kb.sz);
639 GH_HASH(h, d->buf, d->len);
643 static int symm_encdoit(kem *k, dstr *d, ghash *h)
645 dstr_ensure(d, h->ops->c->hashsz);
646 d->len += h->ops->c->hashsz;
647 rand_get(RAND_GLOBAL, d->buf, d->len);
648 return (symm_decdoit(k, d, h));
651 static const char *symm_check(kem *k) { return (0); }
653 static void symm_destroy(kem *k)
654 { symm_ctx *s = (symm_ctx *)k; key_unpackdone(&s->kp); }
656 static const kemops symm_encops = {
658 symm_init, symm_encdoit, symm_check, symm_destroy
661 static const kemops symm_decops = {
663 symm_init, symm_decdoit, symm_check, symm_destroy
666 /* --- The switch table --- */
668 const struct kemtab kemtab[] = {
669 { "rsa", &rsa_encops, &rsa_decops },
670 { "dh", &dh_encops, &dh_decops },
671 { "bindh", &bindh_encops, &bindh_decops },
672 { "ec", &ec_encops, &ec_decops },
673 { "symm", &symm_encops, &symm_decops },
677 /* --- @getkem@ --- *
679 * Arguments: @key *k@ = the key to load
680 * @const char *app@ = application name
681 * @int wantpriv@ = nonzero if we want to decrypt
682 * @bulk **bc@ = bulk crypto context to set up
684 * Returns: A key-encapsulating thing.
689 kem *getkem(key *k, const char *app, int wantpriv, bulk **bc)
691 const char *kalg, *halg = 0, *balg = 0;
698 const struct kemtab *kt;
700 const struct bulktab *bt;
706 /* --- Setup stuff --- */
710 /* --- Get the KEM name --- *
712 * Take the attribute if it's there; otherwise use the key type.
716 if ((q = key_getattr(0, k, "kem")) != 0) {
719 } else if (strncmp(k->type, app, n) == 0 && k->type[n] == '-') {
720 dstr_puts(&d, k->type);
723 die(EXIT_FAILURE, "no KEM for key `%s'", t.buf);
726 /* --- Grab the bulk encryption scheme --- *
728 * Grab it from the KEM if it's there, but override it from the attribute.
731 if (p && (p = strchr(p, '/')) != 0) {
735 if ((q = key_getattr(0, k, "bulk")) != 0)
738 /* --- Grab the hash function --- */
740 if (p && (p = strchr(p, '/')) != 0) {
744 if ((q = key_getattr(0, k, "hash")) != 0)
747 /* --- Instantiate the KEM --- */
749 for (kt = kemtab; kt->name; kt++) {
750 if (strcmp(kt->name, kalg) == 0)
753 die(EXIT_FAILURE, "key encapsulation mechanism `%s' not found in key `%s'",
756 ko = wantpriv ? kt->decops : kt->encops;
762 kd = xmalloc(ko->kdsz);
763 kp = key_fetchinit(ko->kf, 0, kd);
764 if ((e = key_fetch(kp, k)) != 0) {
765 die(EXIT_FAILURE, "error fetching key `%s': %s",
766 t.buf, key_strerror(e));
769 kk = ko->init(k, kd);
774 /* --- Set up the bulk crypto --- */
778 else if ((kk->hc = ghash_byname(halg)) == 0) {
779 die(EXIT_FAILURE, "hash algorithm `%s' not found in key `%s'",
784 if ((q = key_getattr(0, k, "kdf")) == 0) {
785 dstr_putf(&d, "%s-mgf", kk->hc->name);
788 if ((kk->cxc = gcipher_byname(q)) == 0) {
789 die(EXIT_FAILURE, "encryption scheme (KDF) `%s' not found in key `%s'",
796 for (bt = bulktab, bo = 0; bt->name; bt++) {
797 if (strcmp(balg, bt->name) == 0)
798 { balg = 0; goto b_found; }
799 n = strlen(bt->name);
800 if (strncmp(balg, bt->name, n) == 0 && balg[n] == '-')
801 { balg += n + 1; goto b_found; }
806 bo = wantpriv ? bt->decops : bt->encops;
807 *bc = bo->init(k, balg, kk->hc->name);
810 /* --- Tidy up --- */
817 /* --- @setupkem@ --- *
819 * Arguments: @kem *k@ = key-encapsulation thing
820 * @dstr *d@ = key-encapsulation data
821 * @bulk *bc@ = bulk crypto context to set up
823 * Returns: Zero on success, nonzero on failure.
825 * Use: Initializes all the various symmetric things from a KEM.
828 int setupkem(kem *k, dstr *d, bulk *bc)
836 if (k->ops->doit(k, d, h))
838 n = keysz(GH_CLASS(h)->hashsz, k->cxc->keysz);
842 k->cx = GC_INIT(k->cxc, kd, n);
843 bc->ops->setup(bc, k->cx);
851 /* --- @freekem@ --- *
853 * Arguments: @kem *k@ = key-encapsulation thing
857 * Use: Frees up a key-encapsulation thing.
865 key_fetchdone(k->kp);
872 /*----- That's all, folks -------------------------------------------------*/