.gitignore: Ignore `auto-version' script.
[u/mdw/catacomb] / cc-kem.c
CommitLineData
5c3f75ec 1/* -*-c-*-
2 *
c65df279 3 * $Id$
5c3f75ec 4 *
5 * Catcrypt key-encapsulation
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
5c3f75ec 11 *
12 * This file is part of Catacomb.
13 *
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.
45c0fd36 18 *
5c3f75ec 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.
45c0fd36 23 *
5c3f75ec 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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Header files ------------------------------------------------------*/
31
cd6eca43
MW
32#define _FILE_OFFSET_BITS 64
33
5c3f75ec 34#include <stdlib.h>
35
36#include <mLib/alloc.h>
37#include <mLib/dstr.h>
38#include <mLib/report.h>
39#include <mLib/sub.h>
40
41#include "mprand.h"
42#include "rand.h"
43
44#include "ec.h"
45#include "ec-keys.h"
46#include "dh.h"
47#include "rsa.h"
48
49#include "rmd160.h"
50#include "blowfish-cbc.h"
51
52#include "cc.h"
53
54/*----- Key encapsulation -------------------------------------------------*/
55
56/* --- RSA --- */
57
58typedef struct rsa_encctx {
59 kem k;
60 rsa_pubctx rp;
61} rsa_encctx;
62
63static kem *rsa_encinit(key *k, void *kd)
64{
65 rsa_encctx *re = CREATE(rsa_encctx);
66 rsa_pubcreate(&re->rp, kd);
67 return (&re->k);
68}
69
70static int rsa_encdoit(kem *k, dstr *d, ghash *h)
71{
72 rsa_encctx *re = (rsa_encctx *)k;
73 mp *x = mprand_range(MP_NEW, re->rp.rp->n, &rand_global, 0);
74 mp *y = rsa_pubop(&re->rp, MP_NEW, x);
75 size_t n = mp_octets(re->rp.rp->n);
76 dstr_ensure(d, n);
77 mp_storeb(x, d->buf, n);
78 GH_HASH(h, d->buf, n);
79 mp_storeb(y, d->buf, n);
80 d->len += n;
81 mp_drop(x);
82 mp_drop(y);
83 return (0);
84}
85
86static const char *rsa_lengthcheck(mp *n)
87{
88 if (mp_bits(n) < 1020) return ("key too short");
89 return (0);
90}
91
92static const char *rsa_enccheck(kem *k)
93{
94 rsa_encctx *re = (rsa_encctx *)k;
95 const char *e;
96 if ((e = rsa_lengthcheck(re->rp.rp->n)) != 0) return (e);
97 return (0);
98}
99
100static void rsa_encdestroy(kem *k)
101{
102 rsa_encctx *re = (rsa_encctx *)k;
103 rsa_pubdestroy(&re->rp);
104 DESTROY(re);
105}
106
107static const kemops rsa_encops = {
108 rsa_pubfetch, sizeof(rsa_pub),
109 rsa_encinit, rsa_encdoit, rsa_enccheck, rsa_encdestroy
110};
111
112typedef struct rsa_decctx {
113 kem k;
114 rsa_privctx rp;
115} rsa_decctx;
116
117static kem *rsa_decinit(key *k, void *kd)
118{
119 rsa_decctx *rd = CREATE(rsa_decctx);
120 rsa_privcreate(&rd->rp, kd, &rand_global);
121 return (&rd->k);
122}
123
124static int rsa_decdoit(kem *k, dstr *d, ghash *h)
125{
126 rsa_decctx *rd = (rsa_decctx *)k;
127 mp *x = mp_loadb(MP_NEW, d->buf, d->len);
128 size_t n;
129 char *p;
130
131 if (MP_CMP(x, >=, rd->rp.rp->n)) {
132 mp_drop(x);
133 return (-1);
134 }
135 n = mp_octets(rd->rp.rp->n);
136 p = xmalloc(n);
137 x = rsa_privop(&rd->rp, x, x);
138 mp_storeb(x, p, n);
139 GH_HASH(h, p, n);
140 mp_drop(x);
141 xfree(p);
142 return (0);
143}
144
145static const char *rsa_deccheck(kem *k)
146{
147 rsa_decctx *rd = (rsa_decctx *)k;
148 const char *e;
149 if ((e = rsa_lengthcheck(rd->rp.rp->n)) != 0) return (e);
150 return (0);
151}
152
153static void rsa_decdestroy(kem *k)
154{
155 rsa_decctx *rd = (rsa_decctx *)k;
156 rsa_privdestroy(&rd->rp);
157 DESTROY(rd);
158}
159
160static const kemops rsa_decops = {
161 rsa_privfetch, sizeof(rsa_priv),
162 rsa_decinit, rsa_decdoit, rsa_deccheck, rsa_decdestroy
163};
164
165/* --- DH and EC --- */
166
167typedef struct dh_encctx {
168 kem k;
169 group *g;
170 mp *x;
171 ge *y;
172} dh_encctx;
173
45c0fd36 174static dh_encctx *dh_doinit(key *k, const gprime_param *gp, mp *y,
3688eb75 175 group *(*makegroup)(const gprime_param *),
176 const char *what)
5c3f75ec 177{
178 dh_encctx *de = CREATE(dh_encctx);
179 dstr t = DSTR_INIT;
180
181 key_fulltag(k, &t);
3688eb75 182 if ((de->g = makegroup(gp)) == 0)
183 die(EXIT_FAILURE, "bad %s group in key `%s'", what, t.buf);
5c3f75ec 184 de->x = MP_NEW;
185 de->y = G_CREATE(de->g);
186 if (G_FROMINT(de->g, de->y, y))
187 die(EXIT_FAILURE, "bad public key `%s'", t.buf);
188 dstr_destroy(&t);
189 return (de);
190}
191
192static dh_encctx *ec_doinit(key *k, const char *cstr, const ec *y)
193{
194 dh_encctx *de = CREATE(dh_encctx);
195 ec_info ei;
196 const char *e;
197 dstr t = DSTR_INIT;
198
199 key_fulltag(k, &t);
200 if ((e = ec_getinfo(&ei, cstr)) != 0 ||
201 (de->g = group_ec(&ei)) == 0)
202 die(EXIT_FAILURE, "bad elliptic curve spec in key `%s': %s", t.buf, e);
203 de->x = MP_NEW;
204 de->y = G_CREATE(de->g);
205 if (G_FROMEC(de->g, de->y, y))
206 die(EXIT_FAILURE, "bad public curve point `%s'", t.buf);
207 dstr_destroy(&t);
208 return (de);
209}
210
211static kem *dh_encinit(key *k, void *kd)
212{
213 dh_pub *dp = kd;
3688eb75 214 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_prime, "prime");
215 return (&de->k);
216}
217
218static kem *bindh_encinit(key *k, void *kd)
219{
220 dh_pub *dp = kd;
221 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_binary, "binary");
5c3f75ec 222 return (&de->k);
223}
224
225static kem *ec_encinit(key *k, void *kd)
226{
227 ec_pub *ep = kd;
228 dh_encctx *de = ec_doinit(k, ep->cstr, &ep->p);
229 return (&de->k);
230}
231
232static int dh_encdoit(kem *k, dstr *d, ghash *h)
233{
234 dh_encctx *de = (dh_encctx *)k;
235 mp *r = mprand_range(MP_NEW, de->g->r, &rand_global, 0);
236 ge *x = G_CREATE(de->g);
237 ge *y = G_CREATE(de->g);
238 size_t n = de->g->noctets;
239 buf b;
45c0fd36 240
5c3f75ec 241 G_EXP(de->g, x, de->g->g, r);
242 G_EXP(de->g, y, de->y, r);
243 dstr_ensure(d, n);
244 buf_init(&b, d->buf, n);
245 G_TORAW(de->g, &b, y);
246 GH_HASH(h, BBASE(&b), BLEN(&b));
247 buf_init(&b, d->buf, n);
248 G_TORAW(de->g, &b, x);
249 GH_HASH(h, BBASE(&b), BLEN(&b));
250 d->len += BLEN(&b);
251 mp_drop(r);
252 G_DESTROY(de->g, x);
253 G_DESTROY(de->g, y);
254 return (0);
255}
256
257static const char *dh_enccheck(kem *k)
258{
259 dh_encctx *de = (dh_encctx *)k;
260 const char *e;
261 if ((e = G_CHECK(de->g, &rand_global)) != 0)
262 return (0);
263 if (group_check(de->g, de->y))
264 return ("public key not in subgroup");
265 return (0);
266}
267
268static void dh_encdestroy(kem *k)
269{
270 dh_encctx *de = (dh_encctx *)k;
271 G_DESTROY(de->g, de->y);
272 mp_drop(de->x);
273 G_DESTROYGROUP(de->g);
6f313264 274 DESTROY(de);
5c3f75ec 275}
276
277static const kemops dh_encops = {
278 dh_pubfetch, sizeof(dh_pub),
279 dh_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
280};
281
3688eb75 282static const kemops bindh_encops = {
283 dh_pubfetch, sizeof(dh_pub),
284 bindh_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
285};
286
5c3f75ec 287static const kemops ec_encops = {
288 ec_pubfetch, sizeof(ec_pub),
289 ec_encinit, dh_encdoit, dh_enccheck, dh_encdestroy
290};
291
292static kem *dh_decinit(key *k, void *kd)
293{
294 dh_priv *dp = kd;
3688eb75 295 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_prime, "prime");
296 de->x = MP_COPY(dp->x);
297 return (&de->k);
298}
299
300static kem *bindh_decinit(key *k, void *kd)
301{
302 dh_priv *dp = kd;
303 dh_encctx *de = dh_doinit(k, &dp->dp, dp->y, group_binary, "binary");
5c3f75ec 304 de->x = MP_COPY(dp->x);
305 return (&de->k);
306}
307
308static kem *ec_decinit(key *k, void *kd)
309{
310 ec_priv *ep = kd;
311 dh_encctx *de = ec_doinit(k, ep->cstr, &ep->p);
312 de->x = MP_COPY(ep->x);
313 return (&de->k);
314}
315
316static int dh_decdoit(kem *k, dstr *d, ghash *h)
317{
318 dh_encctx *de = (dh_encctx *)k;
319 ge *x = G_CREATE(de->g);
320 size_t n = de->g->noctets;
321 void *p = xmalloc(n);
322 buf b;
323 int rc = -1;
324
325 buf_init(&b, d->buf, d->len);
326 if (G_FROMRAW(de->g, &b, x) || group_check(de->g, x))
327 goto done;
328 G_EXP(de->g, x, x, de->x);
329 buf_init(&b, p, n);
330 G_TORAW(de->g, &b, x);
331 GH_HASH(h, BBASE(&b), BLEN(&b));
332 GH_HASH(h, d->buf, d->len);
333 rc = 0;
334done:
335 G_DESTROY(de->g, x);
336 xfree(p);
337 return (rc);
338}
339
340static const kemops dh_decops = {
341 dh_privfetch, sizeof(dh_priv),
342 dh_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
343};
344
3688eb75 345static const kemops bindh_decops = {
346 dh_privfetch, sizeof(dh_priv),
347 bindh_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
348};
349
5c3f75ec 350static const kemops ec_decops = {
351 ec_privfetch, sizeof(ec_priv),
352 ec_decinit, dh_decdoit, dh_enccheck, dh_encdestroy
353};
354
02dfbd5b
MW
355/* --- Symmetric --- */
356
357typedef struct symm_ctx {
358 kem k;
359 key_packdef kp;
360 key_bin kb;
361} symm_ctx;
362
363static kem *symm_init(key *k, void *kd)
364{
365 symm_ctx *s;
366 dstr d = DSTR_INIT;
367 int err;
368
369 s = CREATE(symm_ctx);
370
371 key_fulltag(k, &d);
372 s->kp.e = KENC_BINARY;
373 s->kp.p = &s->kb;
374 s->kp.kd = 0;
375
376 if ((err = key_unpack(&s->kp, kd, &d)) != 0) {
377 die(EXIT_FAILURE, "failed to unpack symmetric key `%s': %s",
378 d.buf, key_strerror(err));
379 }
380 dstr_destroy(&d);
381 return (&s->k);
382}
383
384static int symm_decdoit(kem *k, dstr *d, ghash *h)
385{
386 symm_ctx *s = (symm_ctx *)k;
387
388 GH_HASH(h, s->kb.k, s->kb.sz);
389 GH_HASH(h, d->buf, d->len);
390 return (0);
391}
392
393static int symm_encdoit(kem *k, dstr *d, ghash *h)
394{
395 dstr_ensure(d, h->ops->c->hashsz);
396 d->len += h->ops->c->hashsz;
397 rand_get(RAND_GLOBAL, d->buf, d->len);
398 return (symm_decdoit(k, d, h));
399}
400
401static const char *symm_check(kem *k) { return (0); }
402
403static void symm_destroy(kem *k)
404 { symm_ctx *s = (symm_ctx *)k; key_unpackdone(&s->kp); }
405
406static const kemops symm_encops = {
407 0, 0,
408 symm_init, symm_encdoit, symm_check, symm_destroy
409};
410
411static const kemops symm_decops = {
412 0, 0,
413 symm_init, symm_decdoit, symm_check, symm_destroy
414};
415
5c3f75ec 416/* --- The switch table --- */
417
c65df279 418const struct kemtab kemtab[] = {
5c3f75ec 419 { "rsa", &rsa_encops, &rsa_decops },
420 { "dh", &dh_encops, &dh_decops },
3688eb75 421 { "bindh", &bindh_encops, &bindh_decops },
5c3f75ec 422 { "ec", &ec_encops, &ec_decops },
02dfbd5b 423 { "symm", &symm_encops, &symm_decops },
5c3f75ec 424 { 0, 0, 0 }
425};
426
427/* --- @getkem@ --- *
428 *
429 * Arguments: @key *k@ = the key to load
430 * @const char *app@ = application name
431 * @int wantpriv@ = nonzero if we want to decrypt
432 *
433 * Returns: A key-encapsulating thing.
434 *
435 * Use: Loads a key.
436 */
437
438kem *getkem(key *k, const char *app, int wantpriv)
439{
440 const char *kalg, *halg = 0, *calg = 0;
441 dstr d = DSTR_INIT;
442 dstr t = DSTR_INIT;
443 size_t n;
444 char *p = 0;
445 const char *q;
446 kem *kk;
447 const struct kemtab *kt;
448 const kemops *ko;
449 void *kd;
450 int e;
451 key_packdef *kp;
452
453 /* --- Setup stuff --- */
454
455 key_fulltag(k, &t);
456
457 /* --- Get the KEM name --- *
458 *
459 * Take the attribute if it's there; otherwise use the key type.
460 */
461
462 n = strlen(app);
463 if ((q = key_getattr(0, k, "kem")) != 0) {
464 dstr_puts(&d, q);
465 p = d.buf;
466 } else if (strncmp(k->type, app, n) == 0 && k->type[n] == '-') {
467 dstr_puts(&d, k->type);
468 p = d.buf + n + 1;
469 } else
470 die(EXIT_FAILURE, "no KEM for key `%s'", t.buf);
471 kalg = p;
472
473 /* --- Grab the encryption scheme --- *
474 *
475 * Grab it from the KEM if it's there, but override it from the attribute.
476 */
477
478 if (p && (p = strchr(p, '/')) != 0) {
479 *p++ = 0;
480 calg = p;
481 }
482 if ((q = key_getattr(0, k, "cipher")) != 0)
483 calg = q;
484
485 /* --- Grab the hash function --- */
486
487 if (p && (p = strchr(p, '/')) != 0) {
488 *p++ = 0;
489 halg = p;
490 }
491 if ((q = key_getattr(0, k, "hash")) != 0)
492 halg = q;
493
494 /* --- Instantiate the KEM --- */
495
496 for (kt = kemtab; kt->name; kt++) {
497 if (strcmp(kt->name, kalg) == 0)
498 goto k_found;
499 }
500 die(EXIT_FAILURE, "key encapsulation mechanism `%s' not found in key `%s'",
501 kalg, t.buf);
502k_found:;
503 ko = wantpriv ? kt->decops : kt->encops;
02dfbd5b
MW
504 if (!ko->kf) {
505 kd = k->k;
506 key_incref(kd);
78ec50fa 507 kp = 0;
02dfbd5b
MW
508 } else {
509 kd = xmalloc(ko->kdsz);
510 kp = key_fetchinit(ko->kf, 0, kd);
511 if ((e = key_fetch(kp, k)) != 0) {
512 die(EXIT_FAILURE, "error fetching key `%s': %s",
513 t.buf, key_strerror(e));
514 }
515 }
5c3f75ec 516 kk = ko->init(k, kd);
517 kk->kp = kp;
518 kk->ops = ko;
519 kk->kd = kd;
520
521 /* --- Set up the algorithms --- */
522
523 if (!halg)
524 kk->h = &rmd160;
525 else if ((kk->h = ghash_byname(halg)) == 0) {
526 die(EXIT_FAILURE, "hash algorithm `%s' not found in key `%s'",
527 halg, t.buf);
528 }
529
530 if (!calg)
531 kk->c = &blowfish_cbc;
532 else if ((kk->c = gcipher_byname(calg)) == 0) {
533 die(EXIT_FAILURE, "encryption scheme `%s' not found in key `%s'",
534 calg, t.buf);
535 }
536
537 dstr_reset(&d);
538 if ((q = key_getattr(0, k, "kdf")) == 0) {
539 dstr_putf(&d, "%s-mgf", kk->h->name);
540 q = d.buf;
541 }
542 if ((kk->cx = gcipher_byname(q)) == 0) {
543 die(EXIT_FAILURE, "encryption scheme (KDF) `%s' not found in key `%s'",
544 q, t.buf);
545 }
45c0fd36 546
5c3f75ec 547 dstr_reset(&d);
548 if ((q = key_getattr(0, k, "mac")) == 0) {
549 dstr_putf(&d, "%s-hmac", kk->h->name);
550 q = d.buf;
551 }
552 if ((kk->m = gmac_byname(q)) == 0) {
553 die(EXIT_FAILURE,
554 "message authentication code `%s' not found in key `%s'",
555 q, t.buf);
556 }
557
558 /* --- Tidy up --- */
559
560 dstr_destroy(&d);
561 dstr_destroy(&t);
562 return (kk);
563}
564
565/* --- @setupkem@ --- *
566 *
567 * Arguments: @kem *k@ = key-encapsulation thing
568 * @dstr *d@ = key-encapsulation data
569 * @gcipher **cx@ = key-expansion function (for IVs)
570 * @gcipher **c@ = where to put initialized encryption scheme
571 * @gmac **m@ = where to put initialized MAC
572 *
573 * Returns: Zero on success, nonzero on failure.
574 *
575 * Use: Initializes all the various symmetric things from a KEM.
576 */
577
578int setupkem(kem *k, dstr *d, gcipher **cx, gcipher **c, gmac **m)
579{
580 octet *kd;
581 size_t n, cn, mn;
582 ghash *h;
c65df279 583 int rc = -1;
5c3f75ec 584
585 h = GH_INIT(k->h);
586 if (k->ops->doit(k, d, h))
587 goto done;
588 n = keysz(GH_CLASS(h)->hashsz, k->cx->keysz);
589 if (!n)
590 goto done;
591 kd = GH_DONE(h, 0);
592 *cx = GC_INIT(k->cx, kd, n);
593
594 cn = keysz(0, k->c->keysz); n = cn;
595 mn = keysz(0, k->m->keysz); if (mn > n) n = mn;
596 kd = xmalloc(n);
597 GC_ENCRYPT(*cx, 0, kd, cn);
598 *c = GC_INIT(k->c, kd, cn);
599 GC_ENCRYPT(*cx, 0, kd, mn);
600 *m = GM_KEY(k->m, kd, mn);
601 xfree(kd);
602
603 rc = 0;
604done:
605 GH_DESTROY(h);
606 return (rc);
607}
608
609/* --- @freekem@ --- *
610 *
611 * Arguments: @kem *k@ = key-encapsulation thing
612 *
613 * Returns: ---
614 *
615 * Use: Frees up a key-encapsulation thing.
616 */
617
618void freekem(kem *k)
619{
02dfbd5b
MW
620 if (!k->ops->kf)
621 key_drop(k->kd);
622 else {
623 key_fetchdone(k->kp);
624 xfree(k->kd);
625 }
5c3f75ec 626 k->ops->destroy(k);
627}
628
629/*----- That's all, folks -------------------------------------------------*/