Commit | Line | Data |
---|---|---|
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 | ||
31 | /*----- Tunable parameters ------------------------------------------------*/ | |
32 | ||
d132c651 | 33 | /* --- Note on size limits --- * |
34 | * | |
35 | * For a 64-bit block cipher (e.g., Blowfish), the probability of a collision | |
36 | * occurring after 32 MB is less than %$2^{-21}$%, and the probability of a | |
b5c45da1 | 37 | * collision occurring after 64 MB is less than %$2^{-19}$%. These could be |
38 | * adjusted dependent on the encryption scheme, but it's too much pain. | |
d132c651 | 39 | */ |
40 | ||
426c0bc6 | 41 | #define T_EXP MIN(60) /* Expiry time for a key */ |
42 | #define T_REGEN MIN(45) /* Regeneration time for a key */ | |
43 | #define SZ_EXP MEG(64) /* Expiry data size for a key */ | |
44 | #define SZ_REGEN MEG(32) /* Data size threshold for regen */ | |
410c8acf | 45 | |
46 | /*----- Handy macros ------------------------------------------------------*/ | |
47 | ||
48 | #define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now) | |
49 | ||
b5c45da1 | 50 | #define SEQSZ 4 /* Size of sequence number packet */ |
51 | ||
426c0bc6 | 52 | /*----- Low-level packet encryption and decryption ------------------------*/ |
410c8acf | 53 | |
59d670e7 | 54 | /* --- Encrypted data format --- * |
55 | * | |
7ed14135 | 56 | * Let %$p_i$% be the %$i$%-th plaintext message, with type %$t$%. We first |
e04c2d50 | 57 | * compute |
59d670e7 | 58 | * |
59 | * %$c_i = \mathcal{E}\textrm{-CBC}_{K_{\text{E}}}(p_i)$% | |
60 | * | |
61 | * as the CBC-ciphertext of %$p_i$%, and then | |
62 | * | |
7ed14135 | 63 | * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(t, i, c_i)$% |
59d670e7 | 64 | * |
65 | * as a MAC on the %%\emph{ciphertext}%%. The message sent is then the pair | |
66 | * %$(\sigma_i, c_i)$%. This construction is provably secure in the NM-CCA | |
67 | * sense (assuming that the cipher is IND-CPA, and the MAC is SUF-CMA) | |
68 | * [Bellare and Namprempre]. | |
69 | * | |
70 | * This also ensures that, assuming the key is good, we have a secure channel | |
71 | * [Krawczyk]. Actually, [Krawczyk] shows that, if the cipher is either a | |
72 | * simple stream cipher or a block cipher in CBC mode, we can use the MAC- | |
73 | * then-encrypt scheme and still have a secure channel. However, I like the | |
74 | * NM-CCA guarantee from [Bellare and Namprempre]. I'm less worried about | |
75 | * the Horton Principle [Wagner and Schneier]. | |
76 | */ | |
77 | ||
426c0bc6 | 78 | /* --- @doencrypt@ --- * |
410c8acf | 79 | * |
426c0bc6 | 80 | * Arguments: @keyset *ks@ = pointer to keyset to use |
7ed14135 | 81 | * @unsigned ty@ = type of message this is |
426c0bc6 | 82 | * @buf *b@ = pointer to an input buffer |
83 | * @buf *bb@ = pointer to an output buffer | |
410c8acf | 84 | * |
a50f9a0e MW |
85 | * Returns: Zero if OK; @KSERR_REGEN@ if it's time to generate new keys. |
86 | * Also returns zero if there was insufficient buffer space, but | |
87 | * the buffer is broken in this case. | |
410c8acf | 88 | * |
426c0bc6 | 89 | * Use: Encrypts a message with the given key. We assume that the |
90 | * keyset is OK to use. | |
410c8acf | 91 | */ |
92 | ||
7ed14135 | 93 | static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
410c8acf | 94 | { |
426c0bc6 | 95 | ghash *h; |
b5c45da1 | 96 | gcipher *c = ks->cout; |
426c0bc6 | 97 | const octet *p = BCUR(b); |
98 | size_t sz = BLEFT(b); | |
59d670e7 | 99 | octet *qmac, *qseq, *qiv, *qpk; |
426c0bc6 | 100 | uint32 oseq; |
b5c45da1 | 101 | size_t ivsz = GC_CLASS(c)->blksz; |
102 | size_t tagsz = ks->tagsz; | |
426c0bc6 | 103 | size_t osz, nsz; |
7ed14135 | 104 | octet t[4]; |
426c0bc6 | 105 | int rc = 0; |
106 | ||
107 | /* --- Allocate the required buffer space --- */ | |
108 | ||
b5c45da1 | 109 | if (buf_ensure(bb, tagsz + SEQSZ + ivsz + sz)) |
426c0bc6 | 110 | return (0); /* Caution! */ |
b5c45da1 | 111 | qmac = BCUR(bb); qseq = qmac + tagsz; qiv = qseq + SEQSZ; qpk = qiv + ivsz; |
112 | BSTEP(bb, tagsz + SEQSZ + ivsz + sz); | |
7ed14135 | 113 | STORE32(t, ty); |
426c0bc6 | 114 | |
426c0bc6 | 115 | oseq = ks->oseq++; STORE32(qseq, oseq); |
426c0bc6 | 116 | IF_TRACING(T_KEYSET, { |
117 | trace(T_KEYSET, "keyset: encrypting packet %lu using keyset %u", | |
118 | (unsigned long)oseq, ks->seq); | |
b5c45da1 | 119 | trace_block(T_CRYPTO, "crypto: plaintext packet", p, sz); |
426c0bc6 | 120 | }) |
59d670e7 | 121 | |
b5c45da1 | 122 | /* --- Encrypt the packet --- */ |
59d670e7 | 123 | |
b5c45da1 | 124 | if (ivsz) { |
125 | rand_get(RAND_GLOBAL, qiv, ivsz); | |
126 | GC_SETIV(c, qiv); | |
127 | IF_TRACING(T_KEYSET, { | |
128 | trace_block(T_CRYPTO, "crypto: initialization vector", qiv, ivsz); | |
129 | }) | |
130 | } | |
131 | GC_ENCRYPT(c, p, qpk, sz); | |
426c0bc6 | 132 | IF_TRACING(T_KEYSET, { |
b5c45da1 | 133 | trace_block(T_CRYPTO, "crypto: encrypted packet", qpk, sz); |
426c0bc6 | 134 | }) |
135 | ||
b5c45da1 | 136 | /* --- Now compute the MAC --- */ |
137 | ||
138 | if (tagsz) { | |
139 | h = GM_INIT(ks->mout); | |
140 | GH_HASH(h, t, sizeof(t)); | |
141 | GH_HASH(h, qseq, SEQSZ + ivsz + sz); | |
142 | memcpy(qmac, GH_DONE(h, 0), tagsz); | |
143 | GH_DESTROY(h); | |
144 | IF_TRACING(T_KEYSET, { | |
145 | trace_block(T_CRYPTO, "crypto: computed MAC", qmac, tagsz); | |
146 | }) | |
147 | } | |
148 | ||
426c0bc6 | 149 | /* --- Deduct the packet size from the key's data life --- */ |
150 | ||
151 | osz = ks->sz_exp; | |
152 | if (osz > sz) | |
153 | nsz = osz - sz; | |
154 | else | |
155 | nsz = 0; | |
156 | if (osz >= SZ_REGEN && nsz < SZ_REGEN) { | |
157 | T( trace(T_KEYSET, "keyset: keyset %u data regen limit exceeded -- " | |
158 | "forcing exchange", ks->seq); ) | |
a50f9a0e | 159 | rc = KSERR_REGEN; |
426c0bc6 | 160 | } |
161 | ks->sz_exp = nsz; | |
e04c2d50 | 162 | return (rc); |
410c8acf | 163 | } |
164 | ||
426c0bc6 | 165 | /* --- @dodecrypt@ --- * |
410c8acf | 166 | * |
426c0bc6 | 167 | * Arguments: @keyset *ks@ = pointer to keyset to use |
7ed14135 | 168 | * @unsigned ty@ = expected type code |
426c0bc6 | 169 | * @buf *b@ = pointer to an input buffer |
170 | * @buf *bb@ = pointer to an output buffer | |
171 | * @uint32 *seq@ = where to store the sequence number | |
410c8acf | 172 | * |
a50f9a0e | 173 | * Returns: Zero on success; @KSERR_DECRYPT@ on failure. |
410c8acf | 174 | * |
426c0bc6 | 175 | * Use: Attempts to decrypt a message with the given key. No other |
176 | * checking (e.g., sequence number checks) is performed. We | |
177 | * assume that the keyset is OK to use, and that there is | |
178 | * sufficient output buffer space reserved. If the decryption | |
179 | * is successful, the buffer pointer is moved past the decrypted | |
180 | * packet, and the packet's sequence number is stored in @*seq@. | |
410c8acf | 181 | */ |
182 | ||
7ed14135 | 183 | static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq) |
410c8acf | 184 | { |
59d670e7 | 185 | const octet *pmac, *piv, *pseq, *ppk; |
426c0bc6 | 186 | size_t psz = BLEFT(b); |
187 | size_t sz; | |
188 | octet *q = BCUR(bb); | |
189 | ghash *h; | |
190 | gcipher *c = ks->cin; | |
b5c45da1 | 191 | size_t ivsz = GC_CLASS(c)->blksz; |
192 | size_t tagsz = ks->tagsz; | |
426c0bc6 | 193 | octet *mac; |
194 | int eq; | |
7ed14135 | 195 | octet t[4]; |
426c0bc6 | 196 | |
197 | /* --- Break up the packet into its components --- */ | |
198 | ||
b5c45da1 | 199 | if (psz < ivsz + SEQSZ + tagsz) { |
426c0bc6 | 200 | T( trace(T_KEYSET, "keyset: block too small for keyset %u", ks->seq); ) |
a50f9a0e | 201 | return (KSERR_DECRYPT); |
410c8acf | 202 | } |
b5c45da1 | 203 | sz = psz - ivsz - SEQSZ - tagsz; |
204 | pmac = BCUR(b); pseq = pmac + tagsz; piv = pseq + SEQSZ; ppk = piv + ivsz; | |
7ed14135 | 205 | STORE32(t, ty); |
426c0bc6 | 206 | |
426c0bc6 | 207 | IF_TRACING(T_KEYSET, { |
208 | trace(T_KEYSET, "keyset: decrypting using keyset %u", ks->seq); | |
b5c45da1 | 209 | trace_block(T_CRYPTO, "crypto: ciphertext packet", ppk, sz); |
426c0bc6 | 210 | }) |
b5c45da1 | 211 | |
212 | /* --- Verify the MAC on the packet --- */ | |
213 | ||
214 | if (tagsz) { | |
215 | h = GM_INIT(ks->min); | |
216 | GH_HASH(h, t, sizeof(t)); | |
217 | GH_HASH(h, pseq, SEQSZ + ivsz + sz); | |
218 | mac = GH_DONE(h, 0); | |
219 | eq = !memcmp(mac, pmac, tagsz); | |
426c0bc6 | 220 | IF_TRACING(T_KEYSET, { |
b5c45da1 | 221 | trace_block(T_CRYPTO, "crypto: computed MAC", mac, tagsz); |
426c0bc6 | 222 | }) |
b5c45da1 | 223 | GH_DESTROY(h); |
224 | if (!eq) { | |
225 | IF_TRACING(T_KEYSET, { | |
226 | trace(T_KEYSET, "keyset: incorrect MAC: decryption failed"); | |
227 | trace_block(T_CRYPTO, "crypto: expected MAC", pmac, tagsz); | |
228 | }) | |
a50f9a0e | 229 | return (KSERR_DECRYPT); |
b5c45da1 | 230 | } |
426c0bc6 | 231 | } |
59d670e7 | 232 | |
233 | /* --- Decrypt the packet --- */ | |
234 | ||
b5c45da1 | 235 | if (ivsz) { |
236 | GC_SETIV(c, piv); | |
237 | IF_TRACING(T_KEYSET, { | |
238 | trace_block(T_CRYPTO, "crypto: initialization vector", piv, ivsz); | |
239 | }) | |
240 | } | |
241 | GC_DECRYPT(c, ppk, q, sz); | |
426c0bc6 | 242 | if (seq) |
243 | *seq = LOAD32(pseq); | |
244 | IF_TRACING(T_KEYSET, { | |
245 | trace(T_KEYSET, "keyset: decrypted OK (sequence = %lu)", | |
246 | (unsigned long)LOAD32(pseq)); | |
247 | trace_block(T_CRYPTO, "crypto: decrypted packet", q, sz); | |
248 | }) | |
249 | BSTEP(bb, sz); | |
250 | return (0); | |
410c8acf | 251 | } |
252 | ||
426c0bc6 | 253 | /*----- Operations on a single keyset -------------------------------------*/ |
254 | ||
255 | /* --- @ks_drop@ --- * | |
256 | * | |
257 | * Arguments: @keyset *ks@ = pointer to a keyset | |
258 | * | |
259 | * Returns: --- | |
260 | * | |
261 | * Use: Decrements a keyset's reference counter. If the counter hits | |
262 | * zero, the keyset is freed. | |
263 | */ | |
264 | ||
265 | void ks_drop(keyset *ks) | |
266 | { | |
267 | if (--ks->ref) | |
268 | return; | |
b5c45da1 | 269 | GC_DESTROY(ks->cin); |
270 | GC_DESTROY(ks->cout); | |
271 | GM_DESTROY(ks->min); | |
272 | GM_DESTROY(ks->mout); | |
426c0bc6 | 273 | DESTROY(ks); |
410c8acf | 274 | } |
275 | ||
276 | /* --- @ks_gen@ --- * | |
277 | * | |
426c0bc6 | 278 | * Arguments: @const void *k@ = pointer to key material |
279 | * @size_t x, y, z@ = offsets into key material (see below) | |
e04c2d50 | 280 | * @peer *p@ = pointer to peer information |
410c8acf | 281 | * |
426c0bc6 | 282 | * Returns: A pointer to the new keyset. |
410c8acf | 283 | * |
426c0bc6 | 284 | * Use: Derives a new keyset from the given key material. The |
285 | * offsets @x@, @y@ and @z@ separate the key material into three | |
286 | * parts. Between the @k@ and @k + x@ is `my' contribution to | |
287 | * the key material; between @k + x@ and @k + y@ is `your' | |
288 | * contribution; and between @k + y@ and @k + z@ is a shared | |
289 | * value we made together. These are used to construct two | |
290 | * pairs of symmetric keys. Each pair consists of an encryption | |
291 | * key and a message authentication key. One pair is used for | |
292 | * outgoing messages, the other for incoming messages. | |
293 | * | |
294 | * The new key is marked so that it won't be selected for output | |
295 | * by @ksl_encrypt@. You can still encrypt data with it by | |
296 | * calling @ks_encrypt@ directly. | |
410c8acf | 297 | */ |
298 | ||
9466fafa | 299 | keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p) |
410c8acf | 300 | { |
b5c45da1 | 301 | ghash *h; |
302 | const octet *hh; | |
410c8acf | 303 | keyset *ks = CREATE(keyset); |
304 | time_t now = time(0); | |
9466fafa | 305 | const octet *pp = k; |
410c8acf | 306 | T( static unsigned seq = 0; ) |
307 | ||
308 | T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); ) | |
309 | ||
426c0bc6 | 310 | /* --- Construct the various keys --- * |
311 | * | |
312 | * This is done with macros, because it's quite tedious. | |
313 | */ | |
314 | ||
b5c45da1 | 315 | #define MINE GH_HASH(h, pp, x) |
316 | #define YOURS GH_HASH(h, pp + x, y - x) | |
317 | #define OURS GH_HASH(h, pp + y, z - y) | |
318 | ||
319 | #define HASH_in MINE; YOURS; OURS | |
320 | #define HASH_out YOURS; MINE; OURS | |
321 | #define INIT_c(k) GC_INIT(algs.c, (k), algs.cksz) | |
322 | #define INIT_m(k) GM_KEY(algs.m, (k), algs.mksz) | |
323 | #define STR_c "encryption" | |
324 | #define STR_m "integrity" | |
325 | #define STR_in "incoming" | |
326 | #define STR_out "outgoing" | |
327 | ||
328 | #define SETKEY(a, dir) do { \ | |
329 | h = GH_INIT(algs.h); \ | |
330 | HASH_STRING(h, "tripe-" STR_##a); \ | |
331 | HASH_##dir; \ | |
332 | hh = GH_DONE(h, 0); \ | |
410c8acf | 333 | IF_TRACING(T_KEYSET, { \ |
b5c45da1 | 334 | trace_block(T_CRYPTO, "crypto: " STR_##dir " key " STR_##a, \ |
335 | hh, algs.a##ksz); \ | |
410c8acf | 336 | }) \ |
b5c45da1 | 337 | ks->a##dir = INIT_##a(hh); \ |
338 | GH_DESTROY(h); \ | |
410c8acf | 339 | } while (0) |
340 | ||
b5c45da1 | 341 | SETKEY(c, in); SETKEY(c, out); |
342 | SETKEY(m, in); SETKEY(m, out); | |
426c0bc6 | 343 | |
344 | #undef MINE | |
345 | #undef YOURS | |
346 | #undef OURS | |
b5c45da1 | 347 | #undef STR_c |
348 | #undef STR_m | |
349 | #undef STR_in | |
350 | #undef STR_out | |
351 | #undef INIT_c | |
352 | #undef INIT_m | |
353 | #undef HASH_in | |
354 | #undef HASH_out | |
355 | #undef SETKEY | |
410c8acf | 356 | |
357 | T( ks->seq = seq++; ) | |
e945d6e4 | 358 | ks->ref = 1; |
426c0bc6 | 359 | ks->t_exp = now + T_EXP; |
360 | ks->sz_exp = SZ_EXP; | |
37941236 | 361 | ks->oseq = 0; |
362 | seq_reset(&ks->iseq); | |
426c0bc6 | 363 | ks->next = 0; |
9466fafa | 364 | ks->p = p; |
426c0bc6 | 365 | ks->f = KSF_LISTEN; |
b5c45da1 | 366 | ks->tagsz = algs.tagsz; |
426c0bc6 | 367 | return (ks); |
368 | } | |
369 | ||
370 | /* --- @ks_tregen@ --- * | |
371 | * | |
372 | * Arguments: @keyset *ks@ = pointer to a keyset | |
373 | * | |
374 | * Returns: The time at which moves ought to be made to replace this key. | |
375 | */ | |
376 | ||
377 | time_t ks_tregen(keyset *ks) { return (ks->t_exp - T_EXP + T_REGEN); } | |
378 | ||
379 | /* --- @ks_activate@ --- * | |
380 | * | |
381 | * Arguments: @keyset *ks@ = pointer to a keyset | |
382 | * | |
383 | * Returns: --- | |
384 | * | |
385 | * Use: Activates a keyset, so that it can be used for encrypting | |
386 | * outgoing messages. | |
387 | */ | |
388 | ||
389 | void ks_activate(keyset *ks) | |
390 | { | |
391 | if (ks->f & KSF_LISTEN) { | |
392 | T( trace(T_KEYSET, "keyset: activating keyset %u", ks->seq); ) | |
393 | ks->f &= ~KSF_LISTEN; | |
394 | } | |
410c8acf | 395 | } |
396 | ||
397 | /* --- @ks_encrypt@ --- * | |
398 | * | |
426c0bc6 | 399 | * Arguments: @keyset *ks@ = pointer to a keyset |
7ed14135 | 400 | * @unsigned ty@ = message type |
426c0bc6 | 401 | * @buf *b@ = pointer to input buffer |
402 | * @buf *bb@ = pointer to output buffer | |
403 | * | |
a50f9a0e MW |
404 | * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a |
405 | * new key; @KSERR_NOKEYS@ if the key is not usable. Also | |
406 | * returns zero if there was insufficient buffer (but the output | |
407 | * buffer is broken in this case). | |
426c0bc6 | 408 | * |
409 | * Use: Encrypts a block of data using the key. Note that the `key | |
410 | * ought to be replaced' notification is only ever given once | |
411 | * for each key. Also note that this call forces a keyset to be | |
412 | * used even if it's marked as not for data output. | |
413 | */ | |
414 | ||
7ed14135 | 415 | int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
426c0bc6 | 416 | { |
417 | time_t now = time(0); | |
418 | ||
419 | if (!KEYOK(ks, now)) { | |
420 | buf_break(bb); | |
a50f9a0e | 421 | return (KSERR_NOKEYS); |
426c0bc6 | 422 | } |
7ed14135 | 423 | return (doencrypt(ks, ty, b, bb)); |
426c0bc6 | 424 | } |
425 | ||
426 | /* --- @ks_decrypt@ --- * | |
427 | * | |
428 | * Arguments: @keyset *ks@ = pointer to a keyset | |
7ed14135 | 429 | * @unsigned ty@ = expected type code |
426c0bc6 | 430 | * @buf *b@ = pointer to an input buffer |
431 | * @buf *bb@ = pointer to an output buffer | |
432 | * | |
a50f9a0e MW |
433 | * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns |
434 | * zero if there was insufficient buffer (but the output buffer | |
435 | * is broken in this case). | |
426c0bc6 | 436 | * |
437 | * Use: Attempts to decrypt a message using a given key. Note that | |
438 | * requesting decryption with a key directly won't clear a | |
439 | * marking that it's not for encryption. | |
440 | */ | |
441 | ||
7ed14135 | 442 | int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
426c0bc6 | 443 | { |
444 | time_t now = time(0); | |
445 | uint32 seq; | |
446 | ||
447 | if (!KEYOK(ks, now) || | |
448 | buf_ensure(bb, BLEN(b)) || | |
7ed14135 | 449 | dodecrypt(ks, ty, b, bb, &seq) || |
f43df819 | 450 | seq_check(&ks->iseq, seq, "SYMM")) |
a50f9a0e | 451 | return (KSERR_DECRYPT); |
426c0bc6 | 452 | return (0); |
453 | } | |
454 | ||
455 | /*----- Keyset list handling ----------------------------------------------*/ | |
456 | ||
457 | /* --- @ksl_free@ --- * | |
458 | * | |
459 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
460 | * | |
461 | * Returns: --- | |
462 | * | |
463 | * Use: Frees (releases references to) all of the keys in a keyset. | |
464 | */ | |
465 | ||
466 | void ksl_free(keyset **ksroot) | |
467 | { | |
468 | keyset *ks, *ksn; | |
469 | for (ks = *ksroot; ks; ks = ksn) { | |
470 | ksn = ks->next; | |
471 | ks->f &= ~KSF_LINK; | |
472 | ks_drop(ks); | |
473 | } | |
474 | } | |
475 | ||
476 | /* --- @ksl_link@ --- * | |
477 | * | |
478 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
479 | * @keyset *ks@ = pointer to a keyset | |
480 | * | |
481 | * Returns: --- | |
482 | * | |
483 | * Use: Links a keyset into a list. A keyset can only be on one list | |
484 | * at a time. Bad things happen otherwise. | |
485 | */ | |
486 | ||
487 | void ksl_link(keyset **ksroot, keyset *ks) | |
488 | { | |
489 | assert(!(ks->f & KSF_LINK)); | |
490 | ks->next = *ksroot; | |
491 | *ksroot = ks; | |
492 | ks->f |= KSF_LINK; | |
493 | ks->ref++; | |
494 | } | |
495 | ||
496 | /* --- @ksl_prune@ --- * | |
497 | * | |
498 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
499 | * | |
500 | * Returns: --- | |
501 | * | |
502 | * Use: Prunes the keyset list by removing keys which mustn't be used | |
503 | * any more. | |
504 | */ | |
505 | ||
506 | void ksl_prune(keyset **ksroot) | |
507 | { | |
508 | time_t now = time(0); | |
509 | ||
510 | while (*ksroot) { | |
511 | keyset *ks = *ksroot; | |
512 | ||
513 | if (ks->t_exp <= now) { | |
514 | T( trace(T_KEYSET, "keyset: expiring keyset %u (time limit reached)", | |
515 | ks->seq); ) | |
516 | goto kill; | |
517 | } else if (ks->sz_exp == 0) { | |
518 | T( trace(T_KEYSET, "keyset: expiring keyset %u (data limit reached)", | |
519 | ks->seq); ) | |
520 | goto kill; | |
521 | } else { | |
522 | ksroot = &ks->next; | |
523 | continue; | |
524 | } | |
525 | ||
526 | kill: | |
527 | *ksroot = ks->next; | |
528 | ks->f &= ~KSF_LINK; | |
529 | ks_drop(ks); | |
530 | } | |
531 | } | |
532 | ||
533 | /* --- @ksl_encrypt@ --- * | |
534 | * | |
410c8acf | 535 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
7ed14135 | 536 | * @unsigned ty@ = message type |
410c8acf | 537 | * @buf *b@ = pointer to input buffer |
538 | * @buf *bb@ = pointer to output buffer | |
539 | * | |
a50f9a0e MW |
540 | * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a |
541 | * new key; @KSERR_NOKEYS@ if there are no suitable keys | |
542 | * available. Also returns zero if there was insufficient | |
543 | * buffer space (but the output buffer is broken in this case). | |
410c8acf | 544 | * |
545 | * Use: Encrypts a packet. | |
546 | */ | |
547 | ||
7ed14135 | 548 | int ksl_encrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) |
410c8acf | 549 | { |
550 | time_t now = time(0); | |
426c0bc6 | 551 | keyset *ks = *ksroot; |
410c8acf | 552 | |
410c8acf | 553 | for (;;) { |
554 | if (!ks) { | |
426c0bc6 | 555 | T( trace(T_KEYSET, "keyset: no suitable keysets found"); ) |
410c8acf | 556 | buf_break(bb); |
a50f9a0e | 557 | return (KSERR_NOKEYS); |
410c8acf | 558 | } |
426c0bc6 | 559 | if (KEYOK(ks, now) && !(ks->f & KSF_LISTEN)) |
410c8acf | 560 | break; |
561 | ks = ks->next; | |
562 | } | |
563 | ||
7ed14135 | 564 | return (doencrypt(ks, ty, b, bb)); |
410c8acf | 565 | } |
566 | ||
426c0bc6 | 567 | /* --- @ksl_decrypt@ --- * |
410c8acf | 568 | * |
569 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
7ed14135 | 570 | * @unsigned ty@ = expected type code |
410c8acf | 571 | * @buf *b@ = pointer to input buffer |
572 | * @buf *bb@ = pointer to output buffer | |
573 | * | |
a50f9a0e MW |
574 | * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns |
575 | * zero if there was insufficient buffer (but the output buffer | |
576 | * is broken in this case). | |
410c8acf | 577 | * |
578 | * Use: Decrypts a packet. | |
579 | */ | |
580 | ||
7ed14135 | 581 | int ksl_decrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) |
410c8acf | 582 | { |
583 | time_t now = time(0); | |
410c8acf | 584 | keyset *ks; |
426c0bc6 | 585 | uint32 seq; |
410c8acf | 586 | |
426c0bc6 | 587 | if (buf_ensure(bb, BLEN(b))) |
a50f9a0e | 588 | return (KSERR_DECRYPT); |
09585a65 | 589 | |
410c8acf | 590 | for (ks = *ksroot; ks; ks = ks->next) { |
410c8acf | 591 | if (!KEYOK(ks, now)) |
592 | continue; | |
7ed14135 | 593 | if (!dodecrypt(ks, ty, b, bb, &seq)) { |
426c0bc6 | 594 | if (ks->f & KSF_LISTEN) { |
595 | T( trace(T_KEYSET, "keyset: implicitly activating keyset %u", | |
596 | ks->seq); ) | |
597 | ks->f &= ~KSF_LISTEN; | |
598 | } | |
a50f9a0e MW |
599 | if (seq_check(&ks->iseq, seq, "SYMM")) |
600 | return (KSERR_DECRYPT); | |
601 | else | |
602 | return (0); | |
410c8acf | 603 | } |
410c8acf | 604 | } |
e945d6e4 | 605 | T( trace(T_KEYSET, "keyset: no matching keys, or incorrect MAC"); ) |
a50f9a0e | 606 | return (KSERR_DECRYPT); |
410c8acf | 607 | } |
608 | ||
609 | /*----- That's all, folks -------------------------------------------------*/ |