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_init(key
*k
, const char *calg
, const char *halg
)
183 aead_encctx
*ctx
= CREATE(aead_encctx
);
189 if ((q
= key_getattr(0, k
, "cipher")) != 0) calg
= q
;
190 if (!calg
) ctx
->aec
= &chacha20_poly1305
;
191 else if ((ctx
->aec
= gaead_byname(calg
)) == 0)
192 die(EXIT_FAILURE
, "AEAD scheme `%s' not found in key `%s'",
196 if ((ctx
->nsz
= keysz_pad(4, ctx
->aec
->noncesz
)) == 0)
197 die(EXIT_FAILURE
, "no suitable nonce size for `%s'", calg
);
198 ctx
->tsz
= keysz(0, ctx
->aec
->tagsz
);
204 static int aead_commonsetup(aead_encctx
*ctx
, gcipher
*cx
)
208 n
= ksz
= keysz(0, ctx
->aec
->keysz
);
209 if (n
< ctx
->nsz
) n
= ctx
->nsz
;
210 if (n
< ctx
->tsz
) n
= ctx
->tsz
;
213 GC_ENCRYPT(cx
, 0, ctx
->t
, ksz
);
214 ctx
->key
= GAEAD_KEY(ctx
->aec
, ctx
->t
, ksz
);
218 static size_t aead_overhead(bulk
*b
)
219 { aead_encctx
*ctx
= (aead_encctx
*)b
; return (ctx
->aec
->ohd
+ ctx
->tsz
); }
221 static void aead_commondestroy(aead_encctx
*ctx
)
223 if (ctx
->key
) GAEAD_DESTROY(ctx
->key
);
228 static int aead_encsetup(bulk
*b
, gcipher
*cx
)
230 aead_encctx
*ctx
= (aead_encctx
*)b
;
231 ctx
->ed
.enc
= 0; return (aead_commonsetup(ctx
, cx
));
234 static const char *aead_encdoit(bulk
*b
, uint32 seq
, buf
*bb
,
235 const void *p
, size_t sz
)
237 aead_encctx
*ctx
= (aead_encctx
*)b
;
241 memset(ctx
->t
+ 4, 0, ctx
->nsz
- 4); STORE32_B(ctx
->t
, seq
);
243 ctx
->ed
.enc
= GAEAD_ENC(ctx
->key
, ctx
->t
, ctx
->nsz
, 0, sz
, ctx
->tsz
);
245 GAEAD_REINIT(ctx
->ed
.enc
, ctx
->t
, ctx
->nsz
, 0, sz
, ctx
->tsz
);
246 t
= buf_get(bb
, ctx
->tsz
); assert(t
);
247 rc
= GAEAD_ENCRYPT(ctx
->ed
.enc
, p
, sz
, bb
); assert(rc
>= 0);
248 rc
= GAEAD_DONE(ctx
->ed
.enc
, 0, bb
, t
, ctx
->tsz
); assert(rc
>= 0);
252 static void aead_encdestroy(bulk
*b
)
254 aead_encctx
*ctx
= (aead_encctx
*)b
;
255 if (ctx
->ed
.enc
) GAEAD_DESTROY(ctx
->ed
.enc
);
256 aead_commondestroy(ctx
);
259 static int aead_decsetup(bulk
*b
, gcipher
*cx
)
261 aead_encctx
*ctx
= (aead_encctx
*)b
;
262 ctx
->ed
.dec
= 0; return (aead_commonsetup(ctx
, cx
));
265 static const char *aead_decdoit(bulk
*b
, uint32 seq
, buf
*bb
,
266 const void *p
, size_t sz
)
268 aead_encctx
*ctx
= (aead_encctx
*)b
;
273 memset(ctx
->t
+ 4, 0, ctx
->nsz
- 4); STORE32_B(ctx
->t
, seq
);
275 ctx
->ed
.dec
= GAEAD_DEC(ctx
->key
, ctx
->t
, ctx
->nsz
, 0, sz
, ctx
->tsz
);
277 GAEAD_REINIT(ctx
->ed
.enc
, ctx
->t
, ctx
->nsz
, 0, sz
, ctx
->tsz
);
279 buf_init(&bin
, (/*unconst*/ void *)p
, sz
);
280 t
= buf_get(&bin
, ctx
->tsz
); if (!t
) return ("no tag");
281 rc
= GAEAD_DECRYPT(ctx
->ed
.dec
, BCUR(&bin
), BLEFT(&bin
), bb
);
283 rc
= GAEAD_DONE(ctx
->ed
.dec
, 0, bb
, t
, ctx
->tsz
); assert(rc
>= 0);
284 if (!rc
) return ("authentication failure");
288 static void aead_decdestroy(bulk
*b
)
290 aead_encctx
*ctx
= (aead_encctx
*)b
;
291 if (ctx
->ed
.dec
) GAEAD_DESTROY(ctx
->ed
.dec
);
292 aead_commondestroy(ctx
);
295 static const struct bulkops aead_encops
= {
296 aead_init
, aead_encsetup
, aead_overhead
,
297 aead_encdoit
, aead_encdestroy
299 aead_init
, aead_decsetup
, aead_overhead
,
300 aead_decdoit
, aead_decdestroy
303 /* --- Generic composition --- */
305 typedef struct gencomp_encctx
{
311 octet
*t
; size_t tsz
;
314 static bulk
*gencomp_init(key
*k
, const char *calg
, const char *halg
)
316 gencomp_encctx
*ctx
= CREATE(gencomp_encctx
);
318 dstr d
= DSTR_INIT
, t
= DSTR_INIT
;
322 if ((q
= key_getattr(0, k
, "cipher")) != 0) calg
= q
;
323 if (!calg
) ctx
->cc
= &blowfish_cbc
;
324 else if ((ctx
->cc
= gcipher_byname(calg
)) == 0) {
325 die(EXIT_FAILURE
, "encryption scheme `%s' not found in key `%s'",
330 if ((q
= key_getattr(0, k
, "mac")) == 0) {
331 dstr_putf(&d
, "%s-hmac", halg
);
334 if ((ctx
->mc
= gmac_byname(q
)) == 0) {
336 "message authentication code `%s' not found in key `%s'",
343 static int gencomp_setup(bulk
*b
, gcipher
*cx
)
345 gencomp_encctx
*ctx
= (gencomp_encctx
*)b
;
351 cn
= keysz(0, ctx
->cc
->keysz
); if (cn
> n
) n
= cn
;
352 mn
= keysz(0, ctx
->mc
->keysz
); if (mn
> n
) n
= mn
;
353 ctx
->t
= kd
= xmalloc(n
); ctx
->tsz
= n
;
354 GC_ENCRYPT(cx
, 0, kd
, cn
);
355 ctx
->c
= GC_INIT(ctx
->cc
, kd
, cn
);
356 GC_ENCRYPT(cx
, 0, kd
, mn
);
357 ctx
->m
= GM_KEY(ctx
->mc
, kd
, mn
);
361 static size_t gencomp_overhead(bulk
*b
)
363 gencomp_encctx
*ctx
= (gencomp_encctx
*)b
;
364 return (ctx
->cc
->blksz
+ ctx
->mc
->hashsz
); }
366 static void gencomp_destroy(bulk
*b
)
368 gencomp_encctx
*ctx
= (gencomp_encctx
*)b
;
376 static const char *gencomp_encdoit(bulk
*b
, uint32 seq
, buf
*bb
,
377 const void *p
, size_t sz
)
379 gencomp_encctx
*ctx
= (gencomp_encctx
*)b
;
381 ghash
*h
= GM_INIT(ctx
->m
);
384 if (ctx
->cc
->blksz
) {
385 GC_ENCRYPT(ctx
->cx
, 0, ctx
->t
, ctx
->cc
->blksz
);
386 GC_SETIV(ctx
->c
, ctx
->t
);
388 tag
= buf_get(bb
, ctx
->mc
->hashsz
); assert(tag
);
389 ct
= buf_get(bb
, sz
); assert(ct
);
390 GC_ENCRYPT(ctx
->c
, p
, ct
, sz
);
397 static const char *gencomp_decdoit(bulk
*b
, uint32 seq
, buf
*bb
,
398 const void *p
, size_t sz
)
400 gencomp_encctx
*ctx
= (gencomp_encctx
*)b
;
402 const octet
*tag
, *ct
;
407 buf_init(&bin
, (/*unconst*/ void *)p
, sz
);
408 if ((tag
= buf_get(&bin
, ctx
->mc
->hashsz
)) == 0) return ("no tag");
409 ct
= BCUR(&bin
); sz
= BLEFT(&bin
);
410 pt
= buf_get(bb
, sz
); assert(pt
);
415 ok
= ct_memeq(tag
, GH_DONE(h
, 0), ctx
->mc
->hashsz
);
417 if (!ok
) return ("authentication failure");
419 if (ctx
->cc
->blksz
) {
420 GC_ENCRYPT(ctx
->cx
, 0, ctx
->t
, ctx
->cc
->blksz
);
421 GC_SETIV(ctx
->c
, ctx
->t
);
423 GC_DECRYPT(ctx
->c
, ct
, pt
, sz
);
427 static const bulkops gencomp_encops
= {
428 gencomp_init
, gencomp_setup
, gencomp_overhead
,
429 gencomp_encdoit
, gencomp_destroy
430 }, gencomp_decops
= {
431 gencomp_init
, gencomp_setup
, gencomp_overhead
,
432 gencomp_decdoit
, gencomp_destroy
435 const struct bulktab bulktab
[] = {
436 { "gencomp", &gencomp_encops
, &gencomp_decops
},
437 { "naclbox", &naclbox_encops
, &naclbox_decops
},
438 { "aead", &aead_encops
, &aead_decops
},
442 /*----- Key encapsulation -------------------------------------------------*/
446 typedef struct rsa_encctx
{
451 static kem
*rsa_encinit(key
*k
, void *kd
)
453 rsa_encctx
*re
= CREATE(rsa_encctx
);
454 rsa_pubcreate(&re
->rp
, kd
);
458 static int rsa_encdoit(kem
*k
, dstr
*d
, ghash
*h
)
460 rsa_encctx
*re
= (rsa_encctx
*)k
;
461 mp
*x
= mprand_range(MP_NEW
, re
->rp
.rp
->n
, &rand_global
, 0);
462 mp
*y
= rsa_pubop(&re
->rp
, MP_NEW
, x
);
463 size_t n
= mp_octets(re
->rp
.rp
->n
);
465 mp_storeb(x
, d
->buf
, n
);
466 GH_HASH(h
, d
->buf
, n
);
467 mp_storeb(y
, d
->buf
, n
);
474 static const char *rsa_lengthcheck(mp
*n
)
476 if (mp_bits(n
) < 1020) return ("key too short");
480 static const char *rsa_enccheck(kem
*k
)
482 rsa_encctx
*re
= (rsa_encctx
*)k
;
484 if ((e
= rsa_lengthcheck(re
->rp
.rp
->n
)) != 0) return (e
);
488 static void rsa_encdestroy(kem
*k
)
490 rsa_encctx
*re
= (rsa_encctx
*)k
;
491 rsa_pubdestroy(&re
->rp
);
495 static const kemops rsa_encops
= {
496 rsa_pubfetch
, sizeof(rsa_pub
),
497 rsa_encinit
, rsa_encdoit
, rsa_enccheck
, rsa_encdestroy
500 typedef struct rsa_decctx
{
505 static kem
*rsa_decinit(key
*k
, void *kd
)
507 rsa_decctx
*rd
= CREATE(rsa_decctx
);
508 rsa_privcreate(&rd
->rp
, kd
, &rand_global
);
512 static int rsa_decdoit(kem
*k
, dstr
*d
, ghash
*h
)
514 rsa_decctx
*rd
= (rsa_decctx
*)k
;
515 mp
*x
= mp_loadb(MP_NEW
, d
->buf
, d
->len
);
519 if (MP_CMP(x
, >=, rd
->rp
.rp
->n
)) {
523 n
= mp_octets(rd
->rp
.rp
->n
);
525 x
= rsa_privop(&rd
->rp
, x
, x
);
533 static const char *rsa_deccheck(kem
*k
)
535 rsa_decctx
*rd
= (rsa_decctx
*)k
;
537 if ((e
= rsa_lengthcheck(rd
->rp
.rp
->n
)) != 0) return (e
);
541 static void rsa_decdestroy(kem
*k
)
543 rsa_decctx
*rd
= (rsa_decctx
*)k
;
544 rsa_privdestroy(&rd
->rp
);
548 static const kemops rsa_decops
= {
549 rsa_privfetch
, sizeof(rsa_priv
),
550 rsa_decinit
, rsa_decdoit
, rsa_deccheck
, rsa_decdestroy
553 /* --- DH and EC --- */
555 typedef struct dh_encctx
{
562 static dh_encctx
*dh_doinit(key
*k
, const gprime_param
*gp
, mp
*y
,
563 group
*(*makegroup
)(const gprime_param
*),
566 dh_encctx
*de
= CREATE(dh_encctx
);
570 if ((de
->g
= makegroup(gp
)) == 0)
571 die(EXIT_FAILURE
, "bad %s group in key `%s'", what
, t
.buf
);
573 de
->y
= G_CREATE(de
->g
);
574 if (G_FROMINT(de
->g
, de
->y
, y
))
575 die(EXIT_FAILURE
, "bad public key `%s'", t
.buf
);
580 static dh_encctx
*ec_doinit(key
*k
, const char *cstr
, const ec
*y
)
582 dh_encctx
*de
= CREATE(dh_encctx
);
588 if ((e
= ec_getinfo(&ei
, cstr
)) != 0 ||
589 (de
->g
= group_ec(&ei
)) == 0)
590 die(EXIT_FAILURE
, "bad elliptic curve spec in key `%s': %s", t
.buf
, e
);
592 de
->y
= G_CREATE(de
->g
);
593 if (G_FROMEC(de
->g
, de
->y
, y
))
594 die(EXIT_FAILURE
, "bad public curve point `%s'", t
.buf
);
599 static kem
*dh_encinit(key
*k
, void *kd
)
602 dh_encctx
*de
= dh_doinit(k
, &dp
->dp
, dp
->y
, group_prime
, "prime");
606 static kem
*bindh_encinit(key
*k
, void *kd
)
609 dh_encctx
*de
= dh_doinit(k
, &dp
->dp
, dp
->y
, group_binary
, "binary");
613 static kem
*ec_encinit(key
*k
, void *kd
)
616 dh_encctx
*de
= ec_doinit(k
, ep
->cstr
, &ep
->p
);
620 static int dh_encdoit(kem
*k
, dstr
*d
, ghash
*h
)
622 dh_encctx
*de
= (dh_encctx
*)k
;
623 mp
*r
= mprand_range(MP_NEW
, de
->g
->r
, &rand_global
, 0);
624 ge
*x
= G_CREATE(de
->g
);
625 ge
*y
= G_CREATE(de
->g
);
626 size_t n
= de
->g
->noctets
;
629 G_EXP(de
->g
, x
, de
->g
->g
, r
);
630 G_EXP(de
->g
, y
, de
->y
, r
);
632 buf_init(&b
, d
->buf
, n
);
633 G_TORAW(de
->g
, &b
, y
);
634 GH_HASH(h
, BBASE(&b
), BLEN(&b
));
635 buf_init(&b
, d
->buf
, n
);
636 G_TORAW(de
->g
, &b
, x
);
637 GH_HASH(h
, BBASE(&b
), BLEN(&b
));
645 static const char *dh_enccheck(kem
*k
)
647 dh_encctx
*de
= (dh_encctx
*)k
;
649 if ((e
= G_CHECK(de
->g
, &rand_global
)) != 0)
651 if (group_check(de
->g
, de
->y
))
652 return ("public key not in subgroup");
656 static void dh_encdestroy(kem
*k
)
658 dh_encctx
*de
= (dh_encctx
*)k
;
659 G_DESTROY(de
->g
, de
->y
);
661 G_DESTROYGROUP(de
->g
);
665 static const kemops dh_encops
= {
666 dh_pubfetch
, sizeof(dh_pub
),
667 dh_encinit
, dh_encdoit
, dh_enccheck
, dh_encdestroy
670 static const kemops bindh_encops
= {
671 dh_pubfetch
, sizeof(dh_pub
),
672 bindh_encinit
, dh_encdoit
, dh_enccheck
, dh_encdestroy
675 static const kemops ec_encops
= {
676 ec_pubfetch
, sizeof(ec_pub
),
677 ec_encinit
, dh_encdoit
, dh_enccheck
, dh_encdestroy
680 static kem
*dh_decinit(key
*k
, void *kd
)
683 dh_encctx
*de
= dh_doinit(k
, &dp
->dp
, dp
->y
, group_prime
, "prime");
684 de
->x
= MP_COPY(dp
->x
);
688 static kem
*bindh_decinit(key
*k
, void *kd
)
691 dh_encctx
*de
= dh_doinit(k
, &dp
->dp
, dp
->y
, group_binary
, "binary");
692 de
->x
= MP_COPY(dp
->x
);
696 static kem
*ec_decinit(key
*k
, void *kd
)
699 dh_encctx
*de
= ec_doinit(k
, ep
->cstr
, &ep
->p
);
700 de
->x
= MP_COPY(ep
->x
);
704 static int dh_decdoit(kem
*k
, dstr
*d
, ghash
*h
)
706 dh_encctx
*de
= (dh_encctx
*)k
;
707 ge
*x
= G_CREATE(de
->g
);
708 size_t n
= de
->g
->noctets
;
709 void *p
= xmalloc(n
);
713 buf_init(&b
, d
->buf
, d
->len
);
714 if (G_FROMRAW(de
->g
, &b
, x
) || group_check(de
->g
, x
))
716 G_EXP(de
->g
, x
, x
, de
->x
);
718 G_TORAW(de
->g
, &b
, x
);
719 GH_HASH(h
, BBASE(&b
), BLEN(&b
));
720 GH_HASH(h
, d
->buf
, d
->len
);
728 static const kemops dh_decops
= {
729 dh_privfetch
, sizeof(dh_priv
),
730 dh_decinit
, dh_decdoit
, dh_enccheck
, dh_encdestroy
733 static const kemops bindh_decops
= {
734 dh_privfetch
, sizeof(dh_priv
),
735 bindh_decinit
, dh_decdoit
, dh_enccheck
, dh_encdestroy
738 static const kemops ec_decops
= {
739 ec_privfetch
, sizeof(ec_priv
),
740 ec_decinit
, dh_decdoit
, dh_enccheck
, dh_encdestroy
743 /* --- X25519 and similar schemes --- */
749 #define XDHDEF(xdh, XDH) \
751 static kem *xdh##_encinit(key *k, void *kd) { return (CREATE(kem)); } \
752 static void xdh##_encdestroy(kem *k) { DESTROY(k); } \
754 static const char *xdh##_enccheck(kem *k) \
756 xdh##_pub *kd = k->kd; \
758 if (kd->pub.sz != XDH##_PUBSZ) \
759 return ("incorrect " #XDH "public key length"); \
763 static int xdh##_encdoit(kem *k, dstr *d, ghash *h) \
765 octet t[XDH##_KEYSZ], z[XDH##_OUTSZ]; \
766 xdh##_pub *kd = k->kd; \
768 rand_get(RAND_GLOBAL, t, sizeof(t)); \
769 dstr_ensure(d, XDH##_PUBSZ); \
770 xdh((octet *)d->buf, t, xdh##_base); \
771 xdh(z, t, kd->pub.k); \
772 d->len += XDH##_PUBSZ; \
773 GH_HASH(h, d->buf, XDH##_PUBSZ); \
774 GH_HASH(h, z, XDH##_OUTSZ); \
778 static const char *xdh##_deccheck(kem *k) \
780 xdh##_priv *kd = k->kd; \
782 if (kd->priv.sz != XDH##_KEYSZ) \
783 return ("incorrect " #XDH " private key length"); \
784 if (kd->pub.sz != XDH##_PUBSZ) \
785 return ("incorrect " #XDH " public key length"); \
789 static int xdh##_decdoit(kem *k, dstr *d, ghash *h) \
791 octet z[XDH##_OUTSZ]; \
792 xdh##_priv *kd = k->kd; \
795 if (d->len != XDH##_PUBSZ) goto done; \
796 xdh(z, kd->priv.k, (const octet *)d->buf); \
797 GH_HASH(h, d->buf, XDH##_PUBSZ); \
798 GH_HASH(h, z, XDH##_OUTSZ); \
804 static const kemops xdh##_encops = { \
805 xdh##_pubfetch, sizeof(xdh##_pub), \
806 xdh##_encinit, xdh##_encdoit, xdh##_enccheck, xdh##_encdestroy \
809 static const kemops xdh##_decops = { \
810 xdh##_privfetch, sizeof(xdh##_priv), \
811 xdh##_encinit, xdh##_decdoit, xdh##_deccheck, xdh##_encdestroy \
817 /* --- Symmetric --- */
819 typedef struct symm_ctx
{
825 static kem
*symm_init(key
*k
, void *kd
)
831 s
= CREATE(symm_ctx
);
834 s
->kp
.e
= KENC_BINARY
;
838 if ((err
= key_unpack(&s
->kp
, kd
, &d
)) != 0) {
839 die(EXIT_FAILURE
, "failed to unpack symmetric key `%s': %s",
840 d
.buf
, key_strerror(err
));
846 static int symm_decdoit(kem
*k
, dstr
*d
, ghash
*h
)
848 symm_ctx
*s
= (symm_ctx
*)k
;
850 GH_HASH(h
, s
->kb
.k
, s
->kb
.sz
);
851 GH_HASH(h
, d
->buf
, d
->len
);
855 static int symm_encdoit(kem
*k
, dstr
*d
, ghash
*h
)
857 dstr_ensure(d
, h
->ops
->c
->hashsz
);
858 d
->len
+= h
->ops
->c
->hashsz
;
859 rand_get(RAND_GLOBAL
, d
->buf
, d
->len
);
860 return (symm_decdoit(k
, d
, h
));
863 static const char *symm_check(kem
*k
) { return (0); }
865 static void symm_destroy(kem
*k
)
866 { symm_ctx
*s
= (symm_ctx
*)k
; key_unpackdone(&s
->kp
); }
868 static const kemops symm_encops
= {
870 symm_init
, symm_encdoit
, symm_check
, symm_destroy
873 static const kemops symm_decops
= {
875 symm_init
, symm_decdoit
, symm_check
, symm_destroy
878 /* --- The switch table --- */
880 const struct kemtab kemtab
[] = {
881 { "rsa", &rsa_encops
, &rsa_decops
},
882 { "dh", &dh_encops
, &dh_decops
},
883 { "bindh", &bindh_encops
, &bindh_decops
},
884 { "ec", &ec_encops
, &ec_decops
},
885 #define XDHTAB(xdh, XDH) \
886 { #xdh, &xdh##_encops, &xdh##_decops },
889 { "symm", &symm_encops
, &symm_decops
},
893 /* --- @getkem@ --- *
895 * Arguments: @key *k@ = the key to load
896 * @const char *app@ = application name
897 * @int wantpriv@ = nonzero if we want to decrypt
898 * @bulk **bc@ = bulk crypto context to set up
900 * Returns: A key-encapsulating thing.
905 kem
*getkem(key
*k
, const char *app
, int wantpriv
, bulk
**bc
)
907 const char *kalg
, *halg
= 0, *balg
= 0;
914 const struct kemtab
*kt
;
916 const struct bulktab
*bt
;
922 /* --- Setup stuff --- */
926 /* --- Get the KEM name --- *
928 * Take the attribute if it's there; otherwise use the key type.
932 if ((q
= key_getattr(0, k
, "kem")) != 0) {
935 } else if (strncmp(k
->type
, app
, n
) == 0 && k
->type
[n
] == '-') {
936 dstr_puts(&d
, k
->type
);
939 die(EXIT_FAILURE
, "no KEM for key `%s'", t
.buf
);
942 /* --- Grab the bulk encryption scheme --- *
944 * Grab it from the KEM if it's there, but override it from the attribute.
947 if (p
&& (p
= strchr(p
, '/')) != 0) {
951 if ((q
= key_getattr(0, k
, "bulk")) != 0)
954 /* --- Grab the hash function --- */
956 if (p
&& (p
= strchr(p
, '/')) != 0) {
960 if ((q
= key_getattr(0, k
, "hash")) != 0)
963 /* --- Instantiate the KEM --- */
965 for (kt
= kemtab
; kt
->name
; kt
++) {
966 if (strcmp(kt
->name
, kalg
) == 0)
969 die(EXIT_FAILURE
, "key encapsulation mechanism `%s' not found in key `%s'",
972 ko
= wantpriv ? kt
->decops
: kt
->encops
;
978 kd
= xmalloc(ko
->kdsz
);
979 kp
= key_fetchinit(ko
->kf
, 0, kd
);
980 if ((e
= key_fetch(kp
, k
)) != 0) {
981 die(EXIT_FAILURE
, "error fetching key `%s': %s",
982 t
.buf
, key_strerror(e
));
985 kk
= ko
->init(k
, kd
);
990 /* --- Set up the bulk crypto --- */
994 else if ((kk
->hc
= ghash_byname(halg
)) == 0) {
995 die(EXIT_FAILURE
, "hash algorithm `%s' not found in key `%s'",
1002 for (bt
= bulktab
, bo
= 0; bt
->name
; bt
++) {
1003 if (strcmp(balg
, bt
->name
) == 0)
1004 { balg
= 0; goto b_found
; }
1005 n
= strlen(bt
->name
);
1006 if (strncmp(balg
, bt
->name
, n
) == 0 && balg
[n
] == '-')
1007 { balg
+= n
+ 1; goto b_found
; }
1012 bo
= wantpriv ? bt
->decops
: bt
->encops
;
1013 *bc
= bo
->init(k
, balg
, kk
->hc
->name
);
1017 if ((q
= key_getattr(0, k
, "kdf")) == 0) {
1018 dstr_putf(&d
, "%s-mgf", kk
->hc
->name
);
1021 if ((kk
->cxc
= gcipher_byname(q
)) == 0) {
1022 die(EXIT_FAILURE
, "encryption scheme (KDF) `%s' not found in key `%s'",
1026 /* --- Tidy up --- */
1033 /* --- @setupkem@ --- *
1035 * Arguments: @kem *k@ = key-encapsulation thing
1036 * @dstr *d@ = key-encapsulation data
1037 * @bulk *bc@ = bulk crypto context to set up
1039 * Returns: Zero on success, nonzero on failure.
1041 * Use: Initializes all the various symmetric things from a KEM.
1044 int setupkem(kem
*k
, dstr
*d
, bulk
*bc
)
1052 if (k
->ops
->doit(k
, d
, h
))
1054 n
= keysz(GH_CLASS(h
)->hashsz
, k
->cxc
->keysz
);
1058 k
->cx
= GC_INIT(k
->cxc
, kd
, n
);
1059 bc
->ops
->setup(bc
, k
->cx
);
1067 /* --- @freekem@ --- *
1069 * Arguments: @kem *k@ = key-encapsulation thing
1073 * Use: Frees up a key-encapsulation thing.
1076 void freekem(kem
*k
)
1081 key_fetchdone(k
->kp
);
1088 /*----- That's all, folks -------------------------------------------------*/