chiark / gitweb /
server/tests.at (AWAIT_KXDONE): Ignore the correct server messages.
[tripe] / server / keyset.c
CommitLineData
410c8acf 1/* -*-c-*-
2 *
410c8acf 3 * Handling of symmetric keysets
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
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.
e04c2d50 16 *
410c8acf 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.
e04c2d50 21 *
410c8acf 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.
25 */
26
410c8acf 27/*----- Header files ------------------------------------------------------*/
28
29#include "tripe.h"
30
410c8acf 31/*----- Handy macros ------------------------------------------------------*/
32
33#define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now)
34
426c0bc6 35/*----- Low-level packet encryption and decryption ------------------------*/
410c8acf 36
59d670e7 37/* --- Encrypted data format --- *
38 *
7ed14135 39 * Let %$p_i$% be the %$i$%-th plaintext message, with type %$t$%. We first
e04c2d50 40 * compute
59d670e7 41 *
42 * %$c_i = \mathcal{E}\textrm{-CBC}_{K_{\text{E}}}(p_i)$%
43 *
44 * as the CBC-ciphertext of %$p_i$%, and then
45 *
7ed14135 46 * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(t, i, c_i)$%
59d670e7 47 *
48 * as a MAC on the %%\emph{ciphertext}%%. The message sent is then the pair
49 * %$(\sigma_i, c_i)$%. This construction is provably secure in the NM-CCA
50 * sense (assuming that the cipher is IND-CPA, and the MAC is SUF-CMA)
51 * [Bellare and Namprempre].
52 *
53 * This also ensures that, assuming the key is good, we have a secure channel
54 * [Krawczyk]. Actually, [Krawczyk] shows that, if the cipher is either a
55 * simple stream cipher or a block cipher in CBC mode, we can use the MAC-
56 * then-encrypt scheme and still have a secure channel. However, I like the
57 * NM-CCA guarantee from [Bellare and Namprempre]. I'm less worried about
58 * the Horton Principle [Wagner and Schneier].
59 */
60
426c0bc6 61/* --- @doencrypt@ --- *
410c8acf 62 *
426c0bc6 63 * Arguments: @keyset *ks@ = pointer to keyset to use
7ed14135 64 * @unsigned ty@ = type of message this is
426c0bc6 65 * @buf *b@ = pointer to an input buffer
66 * @buf *bb@ = pointer to an output buffer
410c8acf 67 *
a50f9a0e
MW
68 * Returns: Zero if OK; @KSERR_REGEN@ if it's time to generate new keys.
69 * Also returns zero if there was insufficient buffer space, but
70 * the buffer is broken in this case.
410c8acf 71 *
426c0bc6 72 * Use: Encrypts a message with the given key. We assume that the
73 * keyset is OK to use.
410c8acf 74 */
75
7ed14135 76static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
410c8acf 77{
a93aacce 78 int rc;
426c0bc6 79 size_t sz = BLEFT(b);
426c0bc6 80 size_t osz, nsz;
426c0bc6 81
a93aacce 82 /* --- Initial tracing --- */
426c0bc6 83
426c0bc6 84 IF_TRACING(T_KEYSET, {
a93aacce
MW
85 trace(T_KEYSET,
86 "keyset: encrypting packet %lu (type %u) using keyset %u",
87 (unsigned long)ks->oseq, ty, ks->seq);
88 trace_block(T_CRYPTO, "crypto: plaintext packet", BCUR(b), sz);
426c0bc6 89 })
59d670e7 90
a93aacce 91 /* --- Apply the bulk-crypto transformation --- */
426c0bc6 92
a93aacce
MW
93 rc = ks->bulk->encrypt(ks, ty, b, bb);
94 if (rc || !BOK(bb)) return (rc);
b5c45da1 95
a93aacce 96 /* --- Do the necessary accounting for data volume --- */
426c0bc6 97
98 osz = ks->sz_exp;
a93aacce 99 nsz = osz > sz ? osz - sz : 0;
383a9d71 100 if (osz >= ks->sz_regen && ks->sz_regen > nsz) {
426c0bc6 101 T( trace(T_KEYSET, "keyset: keyset %u data regen limit exceeded -- "
102 "forcing exchange", ks->seq); )
a50f9a0e 103 rc = KSERR_REGEN;
426c0bc6 104 }
105 ks->sz_exp = nsz;
a93aacce
MW
106
107 /* --- We're done --- */
108
e04c2d50 109 return (rc);
410c8acf 110}
111
426c0bc6 112/* --- @dodecrypt@ --- *
410c8acf 113 *
426c0bc6 114 * Arguments: @keyset *ks@ = pointer to keyset to use
7ed14135 115 * @unsigned ty@ = expected type code
426c0bc6 116 * @buf *b@ = pointer to an input buffer
117 * @buf *bb@ = pointer to an output buffer
118 * @uint32 *seq@ = where to store the sequence number
410c8acf 119 *
a50f9a0e 120 * Returns: Zero on success; @KSERR_DECRYPT@ on failure.
410c8acf 121 *
426c0bc6 122 * Use: Attempts to decrypt a message with the given key. No other
123 * checking (e.g., sequence number checks) is performed. We
124 * assume that the keyset is OK to use, and that there is
125 * sufficient output buffer space reserved. If the decryption
126 * is successful, the buffer pointer is moved past the decrypted
127 * packet, and the packet's sequence number is stored in @*seq@.
410c8acf 128 */
129
7ed14135 130static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq)
410c8acf 131{
a93aacce
MW
132 const octet *q = BCUR(bb);
133 int rc;
426c0bc6 134
426c0bc6 135 IF_TRACING(T_KEYSET, {
a93aacce
MW
136 trace(T_KEYSET,
137 "keyset: try decrypting packet (type %u) using keyset %u",
138 ty, ks->seq);
139 trace_block(T_CRYPTO, "crypto: ciphertext packet", BCUR(b), BLEFT(b));
426c0bc6 140 })
b5c45da1 141
a93aacce
MW
142 rc = ks->bulk->decrypt(ks, ty, b, bb, seq);
143 if (rc) return (rc);
59d670e7 144
426c0bc6 145 IF_TRACING(T_KEYSET, {
146 trace(T_KEYSET, "keyset: decrypted OK (sequence = %lu)",
a93aacce
MW
147 (unsigned long)*seq);
148 trace_block(T_CRYPTO, "crypto: decrypted packet", q, BCUR(bb) - q);
426c0bc6 149 })
426c0bc6 150 return (0);
410c8acf 151}
152
426c0bc6 153/*----- Operations on a single keyset -------------------------------------*/
154
155/* --- @ks_drop@ --- *
156 *
157 * Arguments: @keyset *ks@ = pointer to a keyset
158 *
159 * Returns: ---
160 *
161 * Use: Decrements a keyset's reference counter. If the counter hits
162 * zero, the keyset is freed.
163 */
164
165void ks_drop(keyset *ks)
166{
167 if (--ks->ref)
168 return;
a93aacce
MW
169
170#define DROP(dir, a, drop) do { if (ks->dir.a) drop(ks->dir.a); } while (0)
171#define DROP_DIR(dir) do { \
172 DROP(dir, c, GC_DESTROY); \
173 DROP(dir, m, GM_DESTROY); \
174} while (0)
175
176 DROP_DIR(in);
177 DROP_DIR(out);
178
179#undef DROP
180#undef DROP_DIR
181
426c0bc6 182 DESTROY(ks);
410c8acf 183}
184
185/* --- @ks_gen@ --- *
186 *
426c0bc6 187 * Arguments: @const void *k@ = pointer to key material
188 * @size_t x, y, z@ = offsets into key material (see below)
e04c2d50 189 * @peer *p@ = pointer to peer information
410c8acf 190 *
426c0bc6 191 * Returns: A pointer to the new keyset.
410c8acf 192 *
426c0bc6 193 * Use: Derives a new keyset from the given key material. The
194 * offsets @x@, @y@ and @z@ separate the key material into three
195 * parts. Between the @k@ and @k + x@ is `my' contribution to
196 * the key material; between @k + x@ and @k + y@ is `your'
197 * contribution; and between @k + y@ and @k + z@ is a shared
198 * value we made together. These are used to construct two
199 * pairs of symmetric keys. Each pair consists of an encryption
200 * key and a message authentication key. One pair is used for
201 * outgoing messages, the other for incoming messages.
202 *
203 * The new key is marked so that it won't be selected for output
204 * by @ksl_encrypt@. You can still encrypt data with it by
205 * calling @ks_encrypt@ directly.
410c8acf 206 */
207
a93aacce
MW
208static void gen_dir(const algswitch *algs, struct ksdir *ksd,
209 const char *whichdir,
210 const octet *from, size_t fromsz,
211 const octet *to, size_t tosz,
212 const octet *both, size_t bothsz)
213{
214#define SETKEY(what, a, init) do { \
215 ghash *_h; \
216 octet *_hh; \
217 \
218 if (!algs->a) \
219 ksd->a = 0; \
220 else { \
221 _h = GH_INIT(algs->h); \
222 HASH_STRING(_h, "tripe-" what); \
223 GH_HASH(_h, from, fromsz); \
224 GH_HASH(_h, to, tosz); \
225 GH_HASH(_h, both, bothsz); \
226 _hh = GH_DONE(_h, 0); \
227 IF_TRACING(T_KEYSET, { IF_TRACING(T_CRYPTO, { \
228 char _buf[32]; \
229 sprintf(_buf, "crypto: %s key " what, whichdir); \
230 trace_block(T_CRYPTO, _buf, _hh, algs->a##ksz); \
231 }) }) \
232 ksd->a = init(algs->a, _hh, algs->a##ksz); \
233 GH_DESTROY(_h); \
234 } \
235} while (0)
236
237 SETKEY("encryption", c, GC_INIT);
238 SETKEY("integrity", m, GM_KEY);
b87bffcb 239 SETKEY("blkc", b, GC_INIT);
a93aacce
MW
240
241#undef SETKEY
242}
243
9466fafa 244keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p)
410c8acf 245{
410c8acf 246 keyset *ks = CREATE(keyset);
247 time_t now = time(0);
9466fafa 248 const octet *pp = k;
35c8b547 249 const algswitch *algs = &p->kx.kpriv->algs;
410c8acf 250 T( static unsigned seq = 0; )
251
252 T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); )
253
a93aacce
MW
254 gen_dir(algs, &ks->in, "incoming", pp, x, pp + x, y - x, pp + y, z - y);
255 gen_dir(algs, &ks->out, "outgoing", pp + x, y - x, pp, x, pp + y, z - y);
410c8acf 256
257 T( ks->seq = seq++; )
a93aacce 258 ks->bulk = algs->bulk;
e945d6e4 259 ks->ref = 1;
426c0bc6 260 ks->t_exp = now + T_EXP;
35c8b547
MW
261 ks->sz_exp = algs->expsz;
262 ks->sz_regen = algs->expsz/2;
37941236 263 ks->oseq = 0;
264 seq_reset(&ks->iseq);
426c0bc6 265 ks->next = 0;
9466fafa 266 ks->p = p;
426c0bc6 267 ks->f = KSF_LISTEN;
35c8b547 268 ks->tagsz = algs->tagsz;
426c0bc6 269 return (ks);
270}
271
426c0bc6 272/* --- @ks_activate@ --- *
273 *
274 * Arguments: @keyset *ks@ = pointer to a keyset
275 *
276 * Returns: ---
277 *
278 * Use: Activates a keyset, so that it can be used for encrypting
279 * outgoing messages.
280 */
281
282void ks_activate(keyset *ks)
283{
284 if (ks->f & KSF_LISTEN) {
285 T( trace(T_KEYSET, "keyset: activating keyset %u", ks->seq); )
286 ks->f &= ~KSF_LISTEN;
287 }
410c8acf 288}
289
290/* --- @ks_encrypt@ --- *
291 *
426c0bc6 292 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 293 * @unsigned ty@ = message type
426c0bc6 294 * @buf *b@ = pointer to input buffer
295 * @buf *bb@ = pointer to output buffer
296 *
a50f9a0e
MW
297 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
298 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
299 * returns zero if there was insufficient buffer (but the output
300 * buffer is broken in this case).
426c0bc6 301 *
302 * Use: Encrypts a block of data using the key. Note that the `key
303 * ought to be replaced' notification is only ever given once
304 * for each key. Also note that this call forces a keyset to be
305 * used even if it's marked as not for data output.
a93aacce
MW
306 *
307 * The encryption transform is permitted to corrupt @buf_u@ for
308 * its own purposes. Neither the source nor destination should
309 * be within @buf_u@; and callers mustn't expect anything stored
310 * in @buf_u@ to still
426c0bc6 311 */
312
7ed14135 313int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
426c0bc6 314{
315 time_t now = time(0);
316
317 if (!KEYOK(ks, now)) {
318 buf_break(bb);
a50f9a0e 319 return (KSERR_NOKEYS);
426c0bc6 320 }
7ed14135 321 return (doencrypt(ks, ty, b, bb));
426c0bc6 322}
323
324/* --- @ks_decrypt@ --- *
325 *
326 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 327 * @unsigned ty@ = expected type code
426c0bc6 328 * @buf *b@ = pointer to an input buffer
329 * @buf *bb@ = pointer to an output buffer
330 *
12a26b8b 331 * Returns: Zero on success; @KSERR_...@ on failure. Also returns
a50f9a0e
MW
332 * zero if there was insufficient buffer (but the output buffer
333 * is broken in this case).
426c0bc6 334 *
335 * Use: Attempts to decrypt a message using a given key. Note that
336 * requesting decryption with a key directly won't clear a
337 * marking that it's not for encryption.
a93aacce
MW
338 *
339 * The decryption transform is permitted to corrupt @buf_u@ for
340 * its own purposes. Neither the source nor destination should
341 * be within @buf_u@; and callers mustn't expect anything stored
342 * in @buf_u@ to still
426c0bc6 343 */
344
7ed14135 345int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
426c0bc6 346{
347 time_t now = time(0);
348 uint32 seq;
12a26b8b 349 int err;
426c0bc6 350
12a26b8b
MW
351 if (!KEYOK(ks, now)) return (KSERR_DECRYPT);
352 if (buf_ensure(bb, BLEN(b))) return (0);
353 if ((err = dodecrypt(ks, ty, b, bb, &seq)) != 0) return (err);
354 if (seq_check(&ks->iseq, seq, "SYMM")) return (KSERR_SEQ);
426c0bc6 355 return (0);
356}
357
358/*----- Keyset list handling ----------------------------------------------*/
359
360/* --- @ksl_free@ --- *
361 *
362 * Arguments: @keyset **ksroot@ = pointer to keyset list head
363 *
364 * Returns: ---
365 *
366 * Use: Frees (releases references to) all of the keys in a keyset.
367 */
368
369void ksl_free(keyset **ksroot)
370{
371 keyset *ks, *ksn;
372 for (ks = *ksroot; ks; ks = ksn) {
373 ksn = ks->next;
374 ks->f &= ~KSF_LINK;
375 ks_drop(ks);
376 }
377}
378
379/* --- @ksl_link@ --- *
380 *
381 * Arguments: @keyset **ksroot@ = pointer to keyset list head
382 * @keyset *ks@ = pointer to a keyset
383 *
384 * Returns: ---
385 *
386 * Use: Links a keyset into a list. A keyset can only be on one list
387 * at a time. Bad things happen otherwise.
388 */
389
390void ksl_link(keyset **ksroot, keyset *ks)
391{
392 assert(!(ks->f & KSF_LINK));
393 ks->next = *ksroot;
394 *ksroot = ks;
395 ks->f |= KSF_LINK;
396 ks->ref++;
397}
398
399/* --- @ksl_prune@ --- *
400 *
401 * Arguments: @keyset **ksroot@ = pointer to keyset list head
402 *
403 * Returns: ---
404 *
405 * Use: Prunes the keyset list by removing keys which mustn't be used
406 * any more.
407 */
408
409void ksl_prune(keyset **ksroot)
410{
411 time_t now = time(0);
412
413 while (*ksroot) {
414 keyset *ks = *ksroot;
415
416 if (ks->t_exp <= now) {
417 T( trace(T_KEYSET, "keyset: expiring keyset %u (time limit reached)",
418 ks->seq); )
419 goto kill;
420 } else if (ks->sz_exp == 0) {
421 T( trace(T_KEYSET, "keyset: expiring keyset %u (data limit reached)",
422 ks->seq); )
423 goto kill;
424 } else {
425 ksroot = &ks->next;
426 continue;
427 }
428
429 kill:
430 *ksroot = ks->next;
431 ks->f &= ~KSF_LINK;
432 ks_drop(ks);
433 }
434}
435
436/* --- @ksl_encrypt@ --- *
437 *
410c8acf 438 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 439 * @unsigned ty@ = message type
410c8acf 440 * @buf *b@ = pointer to input buffer
441 * @buf *bb@ = pointer to output buffer
442 *
a50f9a0e
MW
443 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
444 * new key; @KSERR_NOKEYS@ if there are no suitable keys
445 * available. Also returns zero if there was insufficient
446 * buffer space (but the output buffer is broken in this case).
410c8acf 447 *
448 * Use: Encrypts a packet.
449 */
450
7ed14135 451int ksl_encrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb)
410c8acf 452{
453 time_t now = time(0);
426c0bc6 454 keyset *ks = *ksroot;
410c8acf 455
410c8acf 456 for (;;) {
457 if (!ks) {
426c0bc6 458 T( trace(T_KEYSET, "keyset: no suitable keysets found"); )
410c8acf 459 buf_break(bb);
a50f9a0e 460 return (KSERR_NOKEYS);
410c8acf 461 }
426c0bc6 462 if (KEYOK(ks, now) && !(ks->f & KSF_LISTEN))
410c8acf 463 break;
464 ks = ks->next;
465 }
466
7ed14135 467 return (doencrypt(ks, ty, b, bb));
410c8acf 468}
469
426c0bc6 470/* --- @ksl_decrypt@ --- *
410c8acf 471 *
472 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 473 * @unsigned ty@ = expected type code
410c8acf 474 * @buf *b@ = pointer to input buffer
475 * @buf *bb@ = pointer to output buffer
476 *
a50f9a0e
MW
477 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
478 * zero if there was insufficient buffer (but the output buffer
479 * is broken in this case).
410c8acf 480 *
481 * Use: Decrypts a packet.
482 */
483
7ed14135 484int ksl_decrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb)
410c8acf 485{
486 time_t now = time(0);
410c8acf 487 keyset *ks;
426c0bc6 488 uint32 seq;
12a26b8b 489 int err;
410c8acf 490
426c0bc6 491 if (buf_ensure(bb, BLEN(b)))
12a26b8b 492 return (0);
09585a65 493
410c8acf 494 for (ks = *ksroot; ks; ks = ks->next) {
410c8acf 495 if (!KEYOK(ks, now))
496 continue;
12a26b8b 497 if ((err = dodecrypt(ks, ty, b, bb, &seq)) == 0) {
426c0bc6 498 if (ks->f & KSF_LISTEN) {
499 T( trace(T_KEYSET, "keyset: implicitly activating keyset %u",
500 ks->seq); )
501 ks->f &= ~KSF_LISTEN;
502 }
a50f9a0e 503 if (seq_check(&ks->iseq, seq, "SYMM"))
12a26b8b 504 return (KSERR_SEQ);
a50f9a0e
MW
505 else
506 return (0);
410c8acf 507 }
12a26b8b 508 if (err != KSERR_DECRYPT) return (err);
410c8acf 509 }
e945d6e4 510 T( trace(T_KEYSET, "keyset: no matching keys, or incorrect MAC"); )
a50f9a0e 511 return (KSERR_DECRYPT);
410c8acf 512}
513
514/*----- That's all, folks -------------------------------------------------*/