410c8acf |
1 | /* -*-c-*- |
2 | * |
3cdc3f3a |
3 | * $Id$ |
410c8acf |
4 | * |
5 | * Handling of symmetric keysets |
6 | * |
7 | * (c) 2001 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Trivial IP Encryption (TrIPE). |
13 | * |
14 | * TrIPE is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * TrIPE is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with TrIPE; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | */ |
28 | |
410c8acf |
29 | /*----- Header files ------------------------------------------------------*/ |
30 | |
31 | #include "tripe.h" |
32 | |
33 | /*----- Tunable parameters ------------------------------------------------*/ |
34 | |
d132c651 |
35 | /* --- Note on size limits --- * |
36 | * |
37 | * For a 64-bit block cipher (e.g., Blowfish), the probability of a collision |
38 | * occurring after 32 MB is less than %$2^{-21}$%, and the probability of a |
b5c45da1 |
39 | * collision occurring after 64 MB is less than %$2^{-19}$%. These could be |
40 | * adjusted dependent on the encryption scheme, but it's too much pain. |
d132c651 |
41 | */ |
42 | |
426c0bc6 |
43 | #define T_EXP MIN(60) /* Expiry time for a key */ |
44 | #define T_REGEN MIN(45) /* Regeneration time for a key */ |
45 | #define SZ_EXP MEG(64) /* Expiry data size for a key */ |
46 | #define SZ_REGEN MEG(32) /* Data size threshold for regen */ |
410c8acf |
47 | |
48 | /*----- Handy macros ------------------------------------------------------*/ |
49 | |
50 | #define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now) |
51 | |
b5c45da1 |
52 | #define SEQSZ 4 /* Size of sequence number packet */ |
53 | |
426c0bc6 |
54 | /*----- Low-level packet encryption and decryption ------------------------*/ |
410c8acf |
55 | |
59d670e7 |
56 | /* --- Encrypted data format --- * |
57 | * |
7ed14135 |
58 | * Let %$p_i$% be the %$i$%-th plaintext message, with type %$t$%. We first |
59 | * compute |
59d670e7 |
60 | * |
61 | * %$c_i = \mathcal{E}\textrm{-CBC}_{K_{\text{E}}}(p_i)$% |
62 | * |
63 | * as the CBC-ciphertext of %$p_i$%, and then |
64 | * |
7ed14135 |
65 | * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(t, i, c_i)$% |
59d670e7 |
66 | * |
67 | * as a MAC on the %%\emph{ciphertext}%%. The message sent is then the pair |
68 | * %$(\sigma_i, c_i)$%. This construction is provably secure in the NM-CCA |
69 | * sense (assuming that the cipher is IND-CPA, and the MAC is SUF-CMA) |
70 | * [Bellare and Namprempre]. |
71 | * |
72 | * This also ensures that, assuming the key is good, we have a secure channel |
73 | * [Krawczyk]. Actually, [Krawczyk] shows that, if the cipher is either a |
74 | * simple stream cipher or a block cipher in CBC mode, we can use the MAC- |
75 | * then-encrypt scheme and still have a secure channel. However, I like the |
76 | * NM-CCA guarantee from [Bellare and Namprempre]. I'm less worried about |
77 | * the Horton Principle [Wagner and Schneier]. |
78 | */ |
79 | |
426c0bc6 |
80 | /* --- @doencrypt@ --- * |
410c8acf |
81 | * |
426c0bc6 |
82 | * Arguments: @keyset *ks@ = pointer to keyset to use |
7ed14135 |
83 | * @unsigned ty@ = type of message this is |
426c0bc6 |
84 | * @buf *b@ = pointer to an input buffer |
85 | * @buf *bb@ = pointer to an output buffer |
410c8acf |
86 | * |
426c0bc6 |
87 | * Returns: Zero if OK, nonzero if a new key is required. |
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); ) |
159 | rc = -1; |
160 | } |
161 | ks->sz_exp = nsz; |
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 | * |
426c0bc6 |
173 | * Returns: Zero if OK, nonzero if it failed. |
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); ) |
201 | return (-1); |
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 | }) |
229 | return (-1); |
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 | /* --- @dosequence@ --- * |
410c8acf |
254 | * |
426c0bc6 |
255 | * Arguments: @keyset *ks@ = pointer to a keyset |
256 | * @uint32 seq@ = a sequence number from a packet |
410c8acf |
257 | * |
426c0bc6 |
258 | * Returns: Zero if the sequence number is OK, nonzero if it's not. |
410c8acf |
259 | * |
426c0bc6 |
260 | * Use: Checks a sequence number. The data in the keyset which keeps |
261 | * track of valid sequence numbers is updated if the sequence |
262 | * number given is good. It's assumed that the sequence number |
263 | * has already been checked for authenticity. |
410c8acf |
264 | */ |
265 | |
426c0bc6 |
266 | static int dosequence(keyset *ks, uint32 seq) |
410c8acf |
267 | { |
37941236 |
268 | switch (seq_check(&ks->iseq, seq)) { |
269 | case SEQ_OK: break; |
270 | case SEQ_OLD: a_warn("SYMM replay old-sequence"); return (-1); |
271 | case SEQ_REPLAY: a_warn("SYMM replay duplicated-sequence"); return (-1); |
272 | default: abort(); |
426c0bc6 |
273 | } |
426c0bc6 |
274 | return (0); |
275 | } |
276 | |
277 | /*----- Operations on a single keyset -------------------------------------*/ |
278 | |
279 | /* --- @ks_drop@ --- * |
280 | * |
281 | * Arguments: @keyset *ks@ = pointer to a keyset |
282 | * |
283 | * Returns: --- |
284 | * |
285 | * Use: Decrements a keyset's reference counter. If the counter hits |
286 | * zero, the keyset is freed. |
287 | */ |
288 | |
289 | void ks_drop(keyset *ks) |
290 | { |
291 | if (--ks->ref) |
292 | return; |
b5c45da1 |
293 | GC_DESTROY(ks->cin); |
294 | GC_DESTROY(ks->cout); |
295 | GM_DESTROY(ks->min); |
296 | GM_DESTROY(ks->mout); |
426c0bc6 |
297 | DESTROY(ks); |
410c8acf |
298 | } |
299 | |
300 | /* --- @ks_gen@ --- * |
301 | * |
426c0bc6 |
302 | * Arguments: @const void *k@ = pointer to key material |
303 | * @size_t x, y, z@ = offsets into key material (see below) |
9466fafa |
304 | * @peer *p@ = pointer to peer information |
410c8acf |
305 | * |
426c0bc6 |
306 | * Returns: A pointer to the new keyset. |
410c8acf |
307 | * |
426c0bc6 |
308 | * Use: Derives a new keyset from the given key material. The |
309 | * offsets @x@, @y@ and @z@ separate the key material into three |
310 | * parts. Between the @k@ and @k + x@ is `my' contribution to |
311 | * the key material; between @k + x@ and @k + y@ is `your' |
312 | * contribution; and between @k + y@ and @k + z@ is a shared |
313 | * value we made together. These are used to construct two |
314 | * pairs of symmetric keys. Each pair consists of an encryption |
315 | * key and a message authentication key. One pair is used for |
316 | * outgoing messages, the other for incoming messages. |
317 | * |
318 | * The new key is marked so that it won't be selected for output |
319 | * by @ksl_encrypt@. You can still encrypt data with it by |
320 | * calling @ks_encrypt@ directly. |
410c8acf |
321 | */ |
322 | |
9466fafa |
323 | keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p) |
410c8acf |
324 | { |
b5c45da1 |
325 | ghash *h; |
326 | const octet *hh; |
410c8acf |
327 | keyset *ks = CREATE(keyset); |
328 | time_t now = time(0); |
9466fafa |
329 | const octet *pp = k; |
410c8acf |
330 | T( static unsigned seq = 0; ) |
331 | |
332 | T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); ) |
333 | |
426c0bc6 |
334 | /* --- Construct the various keys --- * |
335 | * |
336 | * This is done with macros, because it's quite tedious. |
337 | */ |
338 | |
b5c45da1 |
339 | #define MINE GH_HASH(h, pp, x) |
340 | #define YOURS GH_HASH(h, pp + x, y - x) |
341 | #define OURS GH_HASH(h, pp + y, z - y) |
342 | |
343 | #define HASH_in MINE; YOURS; OURS |
344 | #define HASH_out YOURS; MINE; OURS |
345 | #define INIT_c(k) GC_INIT(algs.c, (k), algs.cksz) |
346 | #define INIT_m(k) GM_KEY(algs.m, (k), algs.mksz) |
347 | #define STR_c "encryption" |
348 | #define STR_m "integrity" |
349 | #define STR_in "incoming" |
350 | #define STR_out "outgoing" |
351 | |
352 | #define SETKEY(a, dir) do { \ |
353 | h = GH_INIT(algs.h); \ |
354 | HASH_STRING(h, "tripe-" STR_##a); \ |
355 | HASH_##dir; \ |
356 | hh = GH_DONE(h, 0); \ |
410c8acf |
357 | IF_TRACING(T_KEYSET, { \ |
b5c45da1 |
358 | trace_block(T_CRYPTO, "crypto: " STR_##dir " key " STR_##a, \ |
359 | hh, algs.a##ksz); \ |
410c8acf |
360 | }) \ |
b5c45da1 |
361 | ks->a##dir = INIT_##a(hh); \ |
362 | GH_DESTROY(h); \ |
410c8acf |
363 | } while (0) |
364 | |
b5c45da1 |
365 | SETKEY(c, in); SETKEY(c, out); |
366 | SETKEY(m, in); SETKEY(m, out); |
426c0bc6 |
367 | |
368 | #undef MINE |
369 | #undef YOURS |
370 | #undef OURS |
b5c45da1 |
371 | #undef STR_c |
372 | #undef STR_m |
373 | #undef STR_in |
374 | #undef STR_out |
375 | #undef INIT_c |
376 | #undef INIT_m |
377 | #undef HASH_in |
378 | #undef HASH_out |
379 | #undef SETKEY |
410c8acf |
380 | |
381 | T( ks->seq = seq++; ) |
e945d6e4 |
382 | ks->ref = 1; |
426c0bc6 |
383 | ks->t_exp = now + T_EXP; |
384 | ks->sz_exp = SZ_EXP; |
37941236 |
385 | ks->oseq = 0; |
386 | seq_reset(&ks->iseq); |
426c0bc6 |
387 | ks->next = 0; |
9466fafa |
388 | ks->p = p; |
426c0bc6 |
389 | ks->f = KSF_LISTEN; |
b5c45da1 |
390 | ks->tagsz = algs.tagsz; |
426c0bc6 |
391 | return (ks); |
392 | } |
393 | |
394 | /* --- @ks_tregen@ --- * |
395 | * |
396 | * Arguments: @keyset *ks@ = pointer to a keyset |
397 | * |
398 | * Returns: The time at which moves ought to be made to replace this key. |
399 | */ |
400 | |
401 | time_t ks_tregen(keyset *ks) { return (ks->t_exp - T_EXP + T_REGEN); } |
402 | |
403 | /* --- @ks_activate@ --- * |
404 | * |
405 | * Arguments: @keyset *ks@ = pointer to a keyset |
406 | * |
407 | * Returns: --- |
408 | * |
409 | * Use: Activates a keyset, so that it can be used for encrypting |
410 | * outgoing messages. |
411 | */ |
412 | |
413 | void ks_activate(keyset *ks) |
414 | { |
415 | if (ks->f & KSF_LISTEN) { |
416 | T( trace(T_KEYSET, "keyset: activating keyset %u", ks->seq); ) |
417 | ks->f &= ~KSF_LISTEN; |
418 | } |
410c8acf |
419 | } |
420 | |
421 | /* --- @ks_encrypt@ --- * |
426c0bc6 |
422 | * |
423 | * Arguments: @keyset *ks@ = pointer to a keyset |
7ed14135 |
424 | * @unsigned ty@ = message type |
426c0bc6 |
425 | * @buf *b@ = pointer to input buffer |
426 | * @buf *bb@ = pointer to output buffer |
427 | * |
428 | * Returns: Zero if OK, nonzero if the key needs replacing. If the |
429 | * encryption failed, the output buffer is broken and zero is |
430 | * returned. |
431 | * |
432 | * Use: Encrypts a block of data using the key. Note that the `key |
433 | * ought to be replaced' notification is only ever given once |
434 | * for each key. Also note that this call forces a keyset to be |
435 | * used even if it's marked as not for data output. |
436 | */ |
437 | |
7ed14135 |
438 | int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
426c0bc6 |
439 | { |
440 | time_t now = time(0); |
441 | |
442 | if (!KEYOK(ks, now)) { |
443 | buf_break(bb); |
444 | return (0); |
445 | } |
7ed14135 |
446 | return (doencrypt(ks, ty, b, bb)); |
426c0bc6 |
447 | } |
448 | |
449 | /* --- @ks_decrypt@ --- * |
450 | * |
451 | * Arguments: @keyset *ks@ = pointer to a keyset |
7ed14135 |
452 | * @unsigned ty@ = expected type code |
426c0bc6 |
453 | * @buf *b@ = pointer to an input buffer |
454 | * @buf *bb@ = pointer to an output buffer |
455 | * |
456 | * Returns: Zero on success, or nonzero if there was some problem. |
457 | * |
458 | * Use: Attempts to decrypt a message using a given key. Note that |
459 | * requesting decryption with a key directly won't clear a |
460 | * marking that it's not for encryption. |
461 | */ |
462 | |
7ed14135 |
463 | int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
426c0bc6 |
464 | { |
465 | time_t now = time(0); |
466 | uint32 seq; |
467 | |
468 | if (!KEYOK(ks, now) || |
469 | buf_ensure(bb, BLEN(b)) || |
7ed14135 |
470 | dodecrypt(ks, ty, b, bb, &seq) || |
426c0bc6 |
471 | dosequence(ks, seq)) |
472 | return (-1); |
473 | return (0); |
474 | } |
475 | |
476 | /*----- Keyset list handling ----------------------------------------------*/ |
477 | |
478 | /* --- @ksl_free@ --- * |
479 | * |
480 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
481 | * |
482 | * Returns: --- |
483 | * |
484 | * Use: Frees (releases references to) all of the keys in a keyset. |
485 | */ |
486 | |
487 | void ksl_free(keyset **ksroot) |
488 | { |
489 | keyset *ks, *ksn; |
490 | for (ks = *ksroot; ks; ks = ksn) { |
491 | ksn = ks->next; |
492 | ks->f &= ~KSF_LINK; |
493 | ks_drop(ks); |
494 | } |
495 | } |
496 | |
497 | /* --- @ksl_link@ --- * |
498 | * |
499 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
500 | * @keyset *ks@ = pointer to a keyset |
501 | * |
502 | * Returns: --- |
503 | * |
504 | * Use: Links a keyset into a list. A keyset can only be on one list |
505 | * at a time. Bad things happen otherwise. |
506 | */ |
507 | |
508 | void ksl_link(keyset **ksroot, keyset *ks) |
509 | { |
510 | assert(!(ks->f & KSF_LINK)); |
511 | ks->next = *ksroot; |
512 | *ksroot = ks; |
513 | ks->f |= KSF_LINK; |
514 | ks->ref++; |
515 | } |
516 | |
517 | /* --- @ksl_prune@ --- * |
518 | * |
519 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
520 | * |
521 | * Returns: --- |
522 | * |
523 | * Use: Prunes the keyset list by removing keys which mustn't be used |
524 | * any more. |
525 | */ |
526 | |
527 | void ksl_prune(keyset **ksroot) |
528 | { |
529 | time_t now = time(0); |
530 | |
531 | while (*ksroot) { |
532 | keyset *ks = *ksroot; |
533 | |
534 | if (ks->t_exp <= now) { |
535 | T( trace(T_KEYSET, "keyset: expiring keyset %u (time limit reached)", |
536 | ks->seq); ) |
537 | goto kill; |
538 | } else if (ks->sz_exp == 0) { |
539 | T( trace(T_KEYSET, "keyset: expiring keyset %u (data limit reached)", |
540 | ks->seq); ) |
541 | goto kill; |
542 | } else { |
543 | ksroot = &ks->next; |
544 | continue; |
545 | } |
546 | |
547 | kill: |
548 | *ksroot = ks->next; |
549 | ks->f &= ~KSF_LINK; |
550 | ks_drop(ks); |
551 | } |
552 | } |
553 | |
554 | /* --- @ksl_encrypt@ --- * |
410c8acf |
555 | * |
556 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
7ed14135 |
557 | * @unsigned ty@ = message type |
410c8acf |
558 | * @buf *b@ = pointer to input buffer |
559 | * @buf *bb@ = pointer to output buffer |
560 | * |
561 | * Returns: Nonzero if a new key is needed. |
562 | * |
563 | * Use: Encrypts a packet. |
564 | */ |
565 | |
7ed14135 |
566 | int ksl_encrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) |
410c8acf |
567 | { |
568 | time_t now = time(0); |
426c0bc6 |
569 | keyset *ks = *ksroot; |
410c8acf |
570 | |
410c8acf |
571 | for (;;) { |
572 | if (!ks) { |
426c0bc6 |
573 | T( trace(T_KEYSET, "keyset: no suitable keysets found"); ) |
410c8acf |
574 | buf_break(bb); |
575 | return (-1); |
576 | } |
426c0bc6 |
577 | if (KEYOK(ks, now) && !(ks->f & KSF_LISTEN)) |
410c8acf |
578 | break; |
579 | ks = ks->next; |
580 | } |
581 | |
7ed14135 |
582 | return (doencrypt(ks, ty, b, bb)); |
410c8acf |
583 | } |
584 | |
426c0bc6 |
585 | /* --- @ksl_decrypt@ --- * |
410c8acf |
586 | * |
587 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
7ed14135 |
588 | * @unsigned ty@ = expected type code |
410c8acf |
589 | * @buf *b@ = pointer to input buffer |
590 | * @buf *bb@ = pointer to output buffer |
591 | * |
592 | * Returns: Nonzero if the packet couldn't be decrypted. |
593 | * |
594 | * Use: Decrypts a packet. |
595 | */ |
596 | |
7ed14135 |
597 | int ksl_decrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) |
410c8acf |
598 | { |
599 | time_t now = time(0); |
410c8acf |
600 | keyset *ks; |
426c0bc6 |
601 | uint32 seq; |
410c8acf |
602 | |
426c0bc6 |
603 | if (buf_ensure(bb, BLEN(b))) |
410c8acf |
604 | return (-1); |
09585a65 |
605 | |
410c8acf |
606 | for (ks = *ksroot; ks; ks = ks->next) { |
410c8acf |
607 | if (!KEYOK(ks, now)) |
608 | continue; |
7ed14135 |
609 | if (!dodecrypt(ks, ty, b, bb, &seq)) { |
426c0bc6 |
610 | if (ks->f & KSF_LISTEN) { |
611 | T( trace(T_KEYSET, "keyset: implicitly activating keyset %u", |
612 | ks->seq); ) |
613 | ks->f &= ~KSF_LISTEN; |
614 | } |
615 | return (dosequence(ks, seq)); |
410c8acf |
616 | } |
410c8acf |
617 | } |
e945d6e4 |
618 | T( trace(T_KEYSET, "keyset: no matching keys, or incorrect MAC"); ) |
410c8acf |
619 | return (-1); |
620 | } |
621 | |
622 | /*----- That's all, folks -------------------------------------------------*/ |