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