3 * Handling of symmetric keysets
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE 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 General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Tunable parameters ------------------------------------------------*/
33 #define T_EXP MIN(60) /* Expiry time for a key */
34 #define T_REGEN MIN(45) /* Regeneration time for a key */
36 /*----- Handy macros ------------------------------------------------------*/
38 #define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now)
40 #define SEQSZ 4 /* Size of sequence number packet */
42 /*----- Low-level packet encryption and decryption ------------------------*/
44 /* --- Encrypted data format --- *
46 * Let %$p_i$% be the %$i$%-th plaintext message, with type %$t$%. We first
49 * %$c_i = \mathcal{E}\textrm{-CBC}_{K_{\text{E}}}(p_i)$%
51 * as the CBC-ciphertext of %$p_i$%, and then
53 * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(t, i, c_i)$%
55 * as a MAC on the %%\emph{ciphertext}%%. The message sent is then the pair
56 * %$(\sigma_i, c_i)$%. This construction is provably secure in the NM-CCA
57 * sense (assuming that the cipher is IND-CPA, and the MAC is SUF-CMA)
58 * [Bellare and Namprempre].
60 * This also ensures that, assuming the key is good, we have a secure channel
61 * [Krawczyk]. Actually, [Krawczyk] shows that, if the cipher is either a
62 * simple stream cipher or a block cipher in CBC mode, we can use the MAC-
63 * then-encrypt scheme and still have a secure channel. However, I like the
64 * NM-CCA guarantee from [Bellare and Namprempre]. I'm less worried about
65 * the Horton Principle [Wagner and Schneier].
68 /* --- @doencrypt@ --- *
70 * Arguments: @keyset *ks@ = pointer to keyset to use
71 * @unsigned ty@ = type of message this is
72 * @buf *b@ = pointer to an input buffer
73 * @buf *bb@ = pointer to an output buffer
75 * Returns: Zero if OK; @KSERR_REGEN@ if it's time to generate new keys.
76 * Also returns zero if there was insufficient buffer space, but
77 * the buffer is broken in this case.
79 * Use: Encrypts a message with the given key. We assume that the
80 * keyset is OK to use.
83 static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
86 gcipher *c = ks->cout;
87 const octet *p = BCUR(b);
89 octet *qmac, *qseq, *qiv, *qpk;
91 size_t ivsz = GC_CLASS(c)->blksz;
92 size_t tagsz = ks->tagsz;
97 /* --- Allocate the required buffer space --- */
99 if (buf_ensure(bb, tagsz + SEQSZ + ivsz + sz))
100 return (0); /* Caution! */
101 qmac = BCUR(bb); qseq = qmac + tagsz; qiv = qseq + SEQSZ; qpk = qiv + ivsz;
102 BSTEP(bb, tagsz + SEQSZ + ivsz + sz);
105 oseq = ks->oseq++; STORE32(qseq, oseq);
106 IF_TRACING(T_KEYSET, {
107 trace(T_KEYSET, "keyset: encrypting packet %lu using keyset %u",
108 (unsigned long)oseq, ks->seq);
109 trace_block(T_CRYPTO, "crypto: plaintext packet", p, sz);
112 /* --- Encrypt the packet --- */
115 rand_get(RAND_GLOBAL, qiv, ivsz);
117 IF_TRACING(T_KEYSET, {
118 trace_block(T_CRYPTO, "crypto: initialization vector", qiv, ivsz);
121 GC_ENCRYPT(c, p, qpk, sz);
122 IF_TRACING(T_KEYSET, {
123 trace_block(T_CRYPTO, "crypto: encrypted packet", qpk, sz);
126 /* --- Now compute the MAC --- */
129 h = GM_INIT(ks->mout);
130 GH_HASH(h, t, sizeof(t));
131 GH_HASH(h, qseq, SEQSZ + ivsz + sz);
132 memcpy(qmac, GH_DONE(h, 0), tagsz);
134 IF_TRACING(T_KEYSET, {
135 trace_block(T_CRYPTO, "crypto: computed MAC", qmac, tagsz);
139 /* --- Deduct the packet size from the key's data life --- */
146 if (osz >= ks->sz_regen && ks->sz_regen > nsz) {
147 T( trace(T_KEYSET, "keyset: keyset %u data regen limit exceeded -- "
148 "forcing exchange", ks->seq); )
155 /* --- @dodecrypt@ --- *
157 * Arguments: @keyset *ks@ = pointer to keyset to use
158 * @unsigned ty@ = expected type code
159 * @buf *b@ = pointer to an input buffer
160 * @buf *bb@ = pointer to an output buffer
161 * @uint32 *seq@ = where to store the sequence number
163 * Returns: Zero on success; @KSERR_DECRYPT@ on failure.
165 * Use: Attempts to decrypt a message with the given key. No other
166 * checking (e.g., sequence number checks) is performed. We
167 * assume that the keyset is OK to use, and that there is
168 * sufficient output buffer space reserved. If the decryption
169 * is successful, the buffer pointer is moved past the decrypted
170 * packet, and the packet's sequence number is stored in @*seq@.
173 static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq)
175 const octet *pmac, *piv, *pseq, *ppk;
176 size_t psz = BLEFT(b);
180 gcipher *c = ks->cin;
181 size_t ivsz = GC_CLASS(c)->blksz;
182 size_t tagsz = ks->tagsz;
187 /* --- Break up the packet into its components --- */
189 if (psz < ivsz + SEQSZ + tagsz) {
190 T( trace(T_KEYSET, "keyset: block too small for keyset %u", ks->seq); )
191 return (KSERR_MALFORMED);
193 sz = psz - ivsz - SEQSZ - tagsz;
194 pmac = BCUR(b); pseq = pmac + tagsz; piv = pseq + SEQSZ; ppk = piv + ivsz;
197 IF_TRACING(T_KEYSET, {
198 trace(T_KEYSET, "keyset: decrypting using keyset %u", ks->seq);
199 trace_block(T_CRYPTO, "crypto: ciphertext packet", ppk, sz);
202 /* --- Verify the MAC on the packet --- */
205 h = GM_INIT(ks->min);
206 GH_HASH(h, t, sizeof(t));
207 GH_HASH(h, pseq, SEQSZ + ivsz + sz);
209 eq = !memcmp(mac, pmac, tagsz);
210 IF_TRACING(T_KEYSET, {
211 trace_block(T_CRYPTO, "crypto: computed MAC", mac, tagsz);
215 IF_TRACING(T_KEYSET, {
216 trace(T_KEYSET, "keyset: incorrect MAC: decryption failed");
217 trace_block(T_CRYPTO, "crypto: expected MAC", pmac, tagsz);
219 return (KSERR_DECRYPT);
223 /* --- Decrypt the packet --- */
227 IF_TRACING(T_KEYSET, {
228 trace_block(T_CRYPTO, "crypto: initialization vector", piv, ivsz);
231 GC_DECRYPT(c, ppk, q, sz);
234 IF_TRACING(T_KEYSET, {
235 trace(T_KEYSET, "keyset: decrypted OK (sequence = %lu)",
236 (unsigned long)LOAD32(pseq));
237 trace_block(T_CRYPTO, "crypto: decrypted packet", q, sz);
243 /*----- Operations on a single keyset -------------------------------------*/
245 /* --- @ks_drop@ --- *
247 * Arguments: @keyset *ks@ = pointer to a keyset
251 * Use: Decrements a keyset's reference counter. If the counter hits
252 * zero, the keyset is freed.
255 void ks_drop(keyset *ks)
260 GC_DESTROY(ks->cout);
262 GM_DESTROY(ks->mout);
266 /* --- @ks_gen@ --- *
268 * Arguments: @const void *k@ = pointer to key material
269 * @size_t x, y, z@ = offsets into key material (see below)
270 * @peer *p@ = pointer to peer information
272 * Returns: A pointer to the new keyset.
274 * Use: Derives a new keyset from the given key material. The
275 * offsets @x@, @y@ and @z@ separate the key material into three
276 * parts. Between the @k@ and @k + x@ is `my' contribution to
277 * the key material; between @k + x@ and @k + y@ is `your'
278 * contribution; and between @k + y@ and @k + z@ is a shared
279 * value we made together. These are used to construct two
280 * pairs of symmetric keys. Each pair consists of an encryption
281 * key and a message authentication key. One pair is used for
282 * outgoing messages, the other for incoming messages.
284 * The new key is marked so that it won't be selected for output
285 * by @ksl_encrypt@. You can still encrypt data with it by
286 * calling @ks_encrypt@ directly.
289 keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p)
293 keyset *ks = CREATE(keyset);
294 time_t now = time(0);
296 T( static unsigned seq = 0; )
298 T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); )
300 /* --- Construct the various keys --- *
302 * This is done with macros, because it's quite tedious.
305 #define MINE GH_HASH(h, pp, x)
306 #define YOURS GH_HASH(h, pp + x, y - x)
307 #define OURS GH_HASH(h, pp + y, z - y)
309 #define HASH_in MINE; YOURS; OURS
310 #define HASH_out YOURS; MINE; OURS
311 #define INIT_c(k) GC_INIT(algs.c, (k), algs.cksz)
312 #define INIT_m(k) GM_KEY(algs.m, (k), algs.mksz)
313 #define STR_c "encryption"
314 #define STR_m "integrity"
315 #define STR_in "incoming"
316 #define STR_out "outgoing"
318 #define SETKEY(a, dir) do { \
319 h = GH_INIT(algs.h); \
320 HASH_STRING(h, "tripe-" STR_##a); \
322 hh = GH_DONE(h, 0); \
323 IF_TRACING(T_KEYSET, { \
324 trace_block(T_CRYPTO, "crypto: " STR_##dir " key " STR_##a, \
327 ks->a##dir = INIT_##a(hh); \
331 SETKEY(c, in); SETKEY(c, out);
332 SETKEY(m, in); SETKEY(m, out);
347 T( ks->seq = seq++; )
349 ks->t_exp = now + T_EXP;
350 ks->sz_exp = algs.expsz;
351 ks->sz_regen = algs.expsz/2;
353 seq_reset(&ks->iseq);
357 ks->tagsz = algs.tagsz;
361 /* --- @ks_tregen@ --- *
363 * Arguments: @keyset *ks@ = pointer to a keyset
365 * Returns: The time at which moves ought to be made to replace this key.
368 time_t ks_tregen(keyset *ks) { return (ks->t_exp - T_EXP + T_REGEN); }
370 /* --- @ks_activate@ --- *
372 * Arguments: @keyset *ks@ = pointer to a keyset
376 * Use: Activates a keyset, so that it can be used for encrypting
380 void ks_activate(keyset *ks)
382 if (ks->f & KSF_LISTEN) {
383 T( trace(T_KEYSET, "keyset: activating keyset %u", ks->seq); )
384 ks->f &= ~KSF_LISTEN;
388 /* --- @ks_encrypt@ --- *
390 * Arguments: @keyset *ks@ = pointer to a keyset
391 * @unsigned ty@ = message type
392 * @buf *b@ = pointer to input buffer
393 * @buf *bb@ = pointer to output buffer
395 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
396 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
397 * returns zero if there was insufficient buffer (but the output
398 * buffer is broken in this case).
400 * Use: Encrypts a block of data using the key. Note that the `key
401 * ought to be replaced' notification is only ever given once
402 * for each key. Also note that this call forces a keyset to be
403 * used even if it's marked as not for data output.
406 int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
408 time_t now = time(0);
410 if (!KEYOK(ks, now)) {
412 return (KSERR_NOKEYS);
414 return (doencrypt(ks, ty, b, bb));
417 /* --- @ks_decrypt@ --- *
419 * Arguments: @keyset *ks@ = pointer to a keyset
420 * @unsigned ty@ = expected type code
421 * @buf *b@ = pointer to an input buffer
422 * @buf *bb@ = pointer to an output buffer
424 * Returns: Zero on success; @KSERR_...@ on failure. Also returns
425 * zero if there was insufficient buffer (but the output buffer
426 * is broken in this case).
428 * Use: Attempts to decrypt a message using a given key. Note that
429 * requesting decryption with a key directly won't clear a
430 * marking that it's not for encryption.
433 int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
435 time_t now = time(0);
439 if (!KEYOK(ks, now)) return (KSERR_DECRYPT);
440 if (buf_ensure(bb, BLEN(b))) return (0);
441 if ((err = dodecrypt(ks, ty, b, bb, &seq)) != 0) return (err);
442 if (seq_check(&ks->iseq, seq, "SYMM")) return (KSERR_SEQ);
446 /*----- Keyset list handling ----------------------------------------------*/
448 /* --- @ksl_free@ --- *
450 * Arguments: @keyset **ksroot@ = pointer to keyset list head
454 * Use: Frees (releases references to) all of the keys in a keyset.
457 void ksl_free(keyset **ksroot)
460 for (ks = *ksroot; ks; ks = ksn) {
467 /* --- @ksl_link@ --- *
469 * Arguments: @keyset **ksroot@ = pointer to keyset list head
470 * @keyset *ks@ = pointer to a keyset
474 * Use: Links a keyset into a list. A keyset can only be on one list
475 * at a time. Bad things happen otherwise.
478 void ksl_link(keyset **ksroot, keyset *ks)
480 assert(!(ks->f & KSF_LINK));
487 /* --- @ksl_prune@ --- *
489 * Arguments: @keyset **ksroot@ = pointer to keyset list head
493 * Use: Prunes the keyset list by removing keys which mustn't be used
497 void ksl_prune(keyset **ksroot)
499 time_t now = time(0);
502 keyset *ks = *ksroot;
504 if (ks->t_exp <= now) {
505 T( trace(T_KEYSET, "keyset: expiring keyset %u (time limit reached)",
508 } else if (ks->sz_exp == 0) {
509 T( trace(T_KEYSET, "keyset: expiring keyset %u (data limit reached)",
524 /* --- @ksl_encrypt@ --- *
526 * Arguments: @keyset **ksroot@ = pointer to keyset list head
527 * @unsigned ty@ = message type
528 * @buf *b@ = pointer to input buffer
529 * @buf *bb@ = pointer to output buffer
531 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
532 * new key; @KSERR_NOKEYS@ if there are no suitable keys
533 * available. Also returns zero if there was insufficient
534 * buffer space (but the output buffer is broken in this case).
536 * Use: Encrypts a packet.
539 int ksl_encrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb)
541 time_t now = time(0);
542 keyset *ks = *ksroot;
546 T( trace(T_KEYSET, "keyset: no suitable keysets found"); )
548 return (KSERR_NOKEYS);
550 if (KEYOK(ks, now) && !(ks->f & KSF_LISTEN))
555 return (doencrypt(ks, ty, b, bb));
558 /* --- @ksl_decrypt@ --- *
560 * Arguments: @keyset **ksroot@ = pointer to keyset list head
561 * @unsigned ty@ = expected type code
562 * @buf *b@ = pointer to input buffer
563 * @buf *bb@ = pointer to output buffer
565 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
566 * zero if there was insufficient buffer (but the output buffer
567 * is broken in this case).
569 * Use: Decrypts a packet.
572 int ksl_decrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb)
574 time_t now = time(0);
579 if (buf_ensure(bb, BLEN(b)))
582 for (ks = *ksroot; ks; ks = ks->next) {
585 if ((err = dodecrypt(ks, ty, b, bb, &seq)) == 0) {
586 if (ks->f & KSF_LISTEN) {
587 T( trace(T_KEYSET, "keyset: implicitly activating keyset %u",
589 ks->f &= ~KSF_LISTEN;
591 if (seq_check(&ks->iseq, seq, "SYMM"))
596 if (err != KSERR_DECRYPT) return (err);
598 T( trace(T_KEYSET, "keyset: no matching keys, or incorrect MAC"); )
599 return (KSERR_DECRYPT);
602 /*----- That's all, folks -------------------------------------------------*/