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 | ||
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 | 76 | static 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 | |
c70a7c5c | 93 | rc = ks->bulk->ops->encrypt(ks->bulk, ty, b, bb, ks->oseq); |
a93aacce | 94 | if (rc || !BOK(bb)) return (rc); |
c70a7c5c | 95 | ks->oseq++; |
b5c45da1 | 96 | |
a93aacce | 97 | /* --- Do the necessary accounting for data volume --- */ |
426c0bc6 | 98 | |
99 | osz = ks->sz_exp; | |
a93aacce | 100 | nsz = osz > sz ? osz - sz : 0; |
383a9d71 | 101 | if (osz >= ks->sz_regen && ks->sz_regen > nsz) { |
426c0bc6 | 102 | T( trace(T_KEYSET, "keyset: keyset %u data regen limit exceeded -- " |
103 | "forcing exchange", ks->seq); ) | |
a50f9a0e | 104 | rc = KSERR_REGEN; |
426c0bc6 | 105 | } |
106 | ks->sz_exp = nsz; | |
a93aacce MW |
107 | |
108 | /* --- We're done --- */ | |
109 | ||
e04c2d50 | 110 | return (rc); |
410c8acf | 111 | } |
112 | ||
426c0bc6 | 113 | /* --- @dodecrypt@ --- * |
410c8acf | 114 | * |
426c0bc6 | 115 | * Arguments: @keyset *ks@ = pointer to keyset to use |
7ed14135 | 116 | * @unsigned ty@ = expected type code |
426c0bc6 | 117 | * @buf *b@ = pointer to an input buffer |
118 | * @buf *bb@ = pointer to an output buffer | |
119 | * @uint32 *seq@ = where to store the sequence number | |
410c8acf | 120 | * |
a50f9a0e | 121 | * Returns: Zero on success; @KSERR_DECRYPT@ on failure. |
410c8acf | 122 | * |
426c0bc6 | 123 | * Use: Attempts to decrypt a message with the given key. No other |
124 | * checking (e.g., sequence number checks) is performed. We | |
125 | * assume that the keyset is OK to use, and that there is | |
126 | * sufficient output buffer space reserved. If the decryption | |
127 | * is successful, the buffer pointer is moved past the decrypted | |
128 | * packet, and the packet's sequence number is stored in @*seq@. | |
410c8acf | 129 | */ |
130 | ||
7ed14135 | 131 | static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq) |
410c8acf | 132 | { |
a93aacce MW |
133 | const octet *q = BCUR(bb); |
134 | int rc; | |
426c0bc6 | 135 | |
426c0bc6 | 136 | IF_TRACING(T_KEYSET, { |
a93aacce MW |
137 | trace(T_KEYSET, |
138 | "keyset: try decrypting packet (type %u) using keyset %u", | |
139 | ty, ks->seq); | |
140 | trace_block(T_CRYPTO, "crypto: ciphertext packet", BCUR(b), BLEFT(b)); | |
426c0bc6 | 141 | }) |
b5c45da1 | 142 | |
c70a7c5c | 143 | rc = ks->bulk->ops->decrypt(ks->bulk, ty, b, bb, seq); |
a93aacce | 144 | if (rc) return (rc); |
59d670e7 | 145 | |
426c0bc6 | 146 | IF_TRACING(T_KEYSET, { |
147 | trace(T_KEYSET, "keyset: decrypted OK (sequence = %lu)", | |
a93aacce MW |
148 | (unsigned long)*seq); |
149 | trace_block(T_CRYPTO, "crypto: decrypted packet", q, BCUR(bb) - q); | |
426c0bc6 | 150 | }) |
426c0bc6 | 151 | return (0); |
410c8acf | 152 | } |
153 | ||
426c0bc6 | 154 | /*----- Operations on a single keyset -------------------------------------*/ |
155 | ||
156 | /* --- @ks_drop@ --- * | |
157 | * | |
158 | * Arguments: @keyset *ks@ = pointer to a keyset | |
159 | * | |
160 | * Returns: --- | |
161 | * | |
162 | * Use: Decrements a keyset's reference counter. If the counter hits | |
163 | * zero, the keyset is freed. | |
164 | */ | |
165 | ||
166 | void ks_drop(keyset *ks) | |
167 | { | |
c70a7c5c MW |
168 | if (--ks->ref) return; |
169 | ks->bulk->ops->freectx(ks->bulk); | |
170 | DESTROY(ks); | |
171 | } | |
a93aacce | 172 | |
c70a7c5c MW |
173 | /* --- @ks_derivekey@ --- * |
174 | * | |
175 | * Arguments: @octet *k@ = pointer to an output buffer of at least | |
176 | * @MAXHASHSZ@ bytes | |
177 | * @size_t ksz@ = actual size wanted (for tracing) | |
178 | * @const struct rawkey *rk@ = a raw key, as passed into | |
179 | * @genkeys@ | |
180 | * @int dir@ = direction for the key (@DIR_IN@ or @DIR_OUT@) | |
181 | * @const char *what@ = label for the key (input to derivation) | |
182 | * | |
183 | * Returns: --- | |
184 | * | |
185 | * Use: Derives a session key, for use on incoming or outgoing data. | |
186 | * This function is part of a private protocol between @ks_gen@ | |
187 | * and the bulk crypto transform @genkeys@ operation. | |
188 | */ | |
a93aacce | 189 | |
c70a7c5c MW |
190 | struct rawkey { |
191 | const gchash *hc; | |
192 | const octet *k; | |
193 | size_t x, y, z; | |
194 | }; | |
a93aacce | 195 | |
c70a7c5c MW |
196 | void ks_derivekey(octet *k, size_t ksz, const struct rawkey *rk, |
197 | int dir, const char *what) | |
198 | { | |
199 | const gchash *hc = rk->hc; | |
200 | ghash *h; | |
201 | ||
202 | assert(ksz <= hc->hashsz); | |
203 | assert(hc->hashsz <= MAXHASHSZ); | |
204 | h = GH_INIT(hc); | |
205 | GH_HASH(h, "tripe-", 6); GH_HASH(h, what, strlen(what) + 1); | |
206 | switch (dir) { | |
207 | case DIR_IN: | |
208 | GH_HASH(h, rk->k, rk->x); | |
209 | GH_HASH(h, rk->k + rk->x, rk->y - rk->x); | |
210 | break; | |
211 | case DIR_OUT: | |
212 | GH_HASH(h, rk->k + rk->x, rk->y - rk->x); | |
213 | GH_HASH(h, rk->k, rk->x); | |
214 | break; | |
215 | default: | |
216 | abort(); | |
217 | } | |
218 | GH_HASH(h, rk->k + rk->y, rk->z - rk->y); | |
219 | GH_DONE(h, k); | |
220 | GH_DESTROY(h); | |
221 | IF_TRACING(T_KEYSET, { IF_TRACING(T_CRYPTO, { | |
222 | char _buf[32]; | |
223 | sprintf(_buf, "crypto: %s key %s", dir ? "incoming" : "outgoing", what); | |
224 | trace_block(T_CRYPTO, _buf, k, ksz); | |
225 | }) }) | |
410c8acf | 226 | } |
227 | ||
228 | /* --- @ks_gen@ --- * | |
229 | * | |
426c0bc6 | 230 | * Arguments: @const void *k@ = pointer to key material |
231 | * @size_t x, y, z@ = offsets into key material (see below) | |
e04c2d50 | 232 | * @peer *p@ = pointer to peer information |
410c8acf | 233 | * |
426c0bc6 | 234 | * Returns: A pointer to the new keyset. |
410c8acf | 235 | * |
426c0bc6 | 236 | * Use: Derives a new keyset from the given key material. The |
237 | * offsets @x@, @y@ and @z@ separate the key material into three | |
238 | * parts. Between the @k@ and @k + x@ is `my' contribution to | |
239 | * the key material; between @k + x@ and @k + y@ is `your' | |
240 | * contribution; and between @k + y@ and @k + z@ is a shared | |
241 | * value we made together. These are used to construct two | |
242 | * pairs of symmetric keys. Each pair consists of an encryption | |
243 | * key and a message authentication key. One pair is used for | |
244 | * outgoing messages, the other for incoming messages. | |
245 | * | |
246 | * The new key is marked so that it won't be selected for output | |
247 | * by @ksl_encrypt@. You can still encrypt data with it by | |
248 | * calling @ks_encrypt@ directly. | |
410c8acf | 249 | */ |
250 | ||
9466fafa | 251 | keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p) |
410c8acf | 252 | { |
410c8acf | 253 | keyset *ks = CREATE(keyset); |
254 | time_t now = time(0); | |
35c8b547 | 255 | const algswitch *algs = &p->kx.kpriv->algs; |
c70a7c5c | 256 | struct rawkey rk; |
410c8acf | 257 | T( static unsigned seq = 0; ) |
258 | ||
259 | T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); ) | |
260 | ||
c70a7c5c MW |
261 | rk.hc = algs->h; rk.k = k; rk.x = x; rk.y = y; rk.z = z; |
262 | ks->bulk = algs->bulk->ops->genkeys(algs->bulk, &rk); | |
263 | ks->bulk->ops = algs->bulk->ops; | |
410c8acf | 264 | |
265 | T( ks->seq = seq++; ) | |
e945d6e4 | 266 | ks->ref = 1; |
426c0bc6 | 267 | ks->t_exp = now + T_EXP; |
c70a7c5c MW |
268 | ks->sz_exp = algs->bulk->ops->expsz(algs->bulk); |
269 | ks->sz_regen = ks->sz_exp/2; | |
37941236 | 270 | ks->oseq = 0; |
271 | seq_reset(&ks->iseq); | |
426c0bc6 | 272 | ks->next = 0; |
9466fafa | 273 | ks->p = p; |
426c0bc6 | 274 | ks->f = KSF_LISTEN; |
426c0bc6 | 275 | return (ks); |
276 | } | |
277 | ||
426c0bc6 | 278 | /* --- @ks_activate@ --- * |
279 | * | |
280 | * Arguments: @keyset *ks@ = pointer to a keyset | |
281 | * | |
282 | * Returns: --- | |
283 | * | |
284 | * Use: Activates a keyset, so that it can be used for encrypting | |
285 | * outgoing messages. | |
286 | */ | |
287 | ||
288 | void ks_activate(keyset *ks) | |
289 | { | |
290 | if (ks->f & KSF_LISTEN) { | |
291 | T( trace(T_KEYSET, "keyset: activating keyset %u", ks->seq); ) | |
292 | ks->f &= ~KSF_LISTEN; | |
293 | } | |
410c8acf | 294 | } |
295 | ||
296 | /* --- @ks_encrypt@ --- * | |
297 | * | |
426c0bc6 | 298 | * Arguments: @keyset *ks@ = pointer to a keyset |
7ed14135 | 299 | * @unsigned ty@ = message type |
426c0bc6 | 300 | * @buf *b@ = pointer to input buffer |
301 | * @buf *bb@ = pointer to output buffer | |
302 | * | |
a50f9a0e MW |
303 | * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a |
304 | * new key; @KSERR_NOKEYS@ if the key is not usable. Also | |
305 | * returns zero if there was insufficient buffer (but the output | |
306 | * buffer is broken in this case). | |
426c0bc6 | 307 | * |
308 | * Use: Encrypts a block of data using the key. Note that the `key | |
309 | * ought to be replaced' notification is only ever given once | |
310 | * for each key. Also note that this call forces a keyset to be | |
311 | * used even if it's marked as not for data output. | |
a93aacce MW |
312 | * |
313 | * The encryption transform is permitted to corrupt @buf_u@ for | |
314 | * its own purposes. Neither the source nor destination should | |
315 | * be within @buf_u@; and callers mustn't expect anything stored | |
316 | * in @buf_u@ to still | |
426c0bc6 | 317 | */ |
318 | ||
7ed14135 | 319 | int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
426c0bc6 | 320 | { |
321 | time_t now = time(0); | |
322 | ||
323 | if (!KEYOK(ks, now)) { | |
324 | buf_break(bb); | |
a50f9a0e | 325 | return (KSERR_NOKEYS); |
426c0bc6 | 326 | } |
7ed14135 | 327 | return (doencrypt(ks, ty, b, bb)); |
426c0bc6 | 328 | } |
329 | ||
330 | /* --- @ks_decrypt@ --- * | |
331 | * | |
332 | * Arguments: @keyset *ks@ = pointer to a keyset | |
7ed14135 | 333 | * @unsigned ty@ = expected type code |
426c0bc6 | 334 | * @buf *b@ = pointer to an input buffer |
335 | * @buf *bb@ = pointer to an output buffer | |
336 | * | |
12a26b8b | 337 | * Returns: Zero on success; @KSERR_...@ on failure. Also returns |
a50f9a0e MW |
338 | * zero if there was insufficient buffer (but the output buffer |
339 | * is broken in this case). | |
426c0bc6 | 340 | * |
341 | * Use: Attempts to decrypt a message using a given key. Note that | |
342 | * requesting decryption with a key directly won't clear a | |
343 | * marking that it's not for encryption. | |
a93aacce MW |
344 | * |
345 | * The decryption transform is permitted to corrupt @buf_u@ for | |
346 | * its own purposes. Neither the source nor destination should | |
347 | * be within @buf_u@; and callers mustn't expect anything stored | |
348 | * in @buf_u@ to still | |
426c0bc6 | 349 | */ |
350 | ||
7ed14135 | 351 | int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
426c0bc6 | 352 | { |
353 | time_t now = time(0); | |
354 | uint32 seq; | |
12a26b8b | 355 | int err; |
426c0bc6 | 356 | |
12a26b8b MW |
357 | if (!KEYOK(ks, now)) return (KSERR_DECRYPT); |
358 | if (buf_ensure(bb, BLEN(b))) return (0); | |
359 | if ((err = dodecrypt(ks, ty, b, bb, &seq)) != 0) return (err); | |
360 | if (seq_check(&ks->iseq, seq, "SYMM")) return (KSERR_SEQ); | |
426c0bc6 | 361 | return (0); |
362 | } | |
363 | ||
364 | /*----- Keyset list handling ----------------------------------------------*/ | |
365 | ||
366 | /* --- @ksl_free@ --- * | |
367 | * | |
368 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
369 | * | |
370 | * Returns: --- | |
371 | * | |
372 | * Use: Frees (releases references to) all of the keys in a keyset. | |
373 | */ | |
374 | ||
375 | void ksl_free(keyset **ksroot) | |
376 | { | |
377 | keyset *ks, *ksn; | |
378 | for (ks = *ksroot; ks; ks = ksn) { | |
379 | ksn = ks->next; | |
380 | ks->f &= ~KSF_LINK; | |
381 | ks_drop(ks); | |
382 | } | |
383 | } | |
384 | ||
385 | /* --- @ksl_link@ --- * | |
386 | * | |
387 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
388 | * @keyset *ks@ = pointer to a keyset | |
389 | * | |
390 | * Returns: --- | |
391 | * | |
392 | * Use: Links a keyset into a list. A keyset can only be on one list | |
393 | * at a time. Bad things happen otherwise. | |
394 | */ | |
395 | ||
396 | void ksl_link(keyset **ksroot, keyset *ks) | |
397 | { | |
398 | assert(!(ks->f & KSF_LINK)); | |
399 | ks->next = *ksroot; | |
400 | *ksroot = ks; | |
401 | ks->f |= KSF_LINK; | |
402 | ks->ref++; | |
403 | } | |
404 | ||
405 | /* --- @ksl_prune@ --- * | |
406 | * | |
407 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
408 | * | |
409 | * Returns: --- | |
410 | * | |
411 | * Use: Prunes the keyset list by removing keys which mustn't be used | |
412 | * any more. | |
413 | */ | |
414 | ||
415 | void ksl_prune(keyset **ksroot) | |
416 | { | |
417 | time_t now = time(0); | |
418 | ||
419 | while (*ksroot) { | |
420 | keyset *ks = *ksroot; | |
421 | ||
422 | if (ks->t_exp <= now) { | |
423 | T( trace(T_KEYSET, "keyset: expiring keyset %u (time limit reached)", | |
424 | ks->seq); ) | |
425 | goto kill; | |
426 | } else if (ks->sz_exp == 0) { | |
427 | T( trace(T_KEYSET, "keyset: expiring keyset %u (data limit reached)", | |
428 | ks->seq); ) | |
429 | goto kill; | |
430 | } else { | |
431 | ksroot = &ks->next; | |
432 | continue; | |
433 | } | |
434 | ||
435 | kill: | |
436 | *ksroot = ks->next; | |
437 | ks->f &= ~KSF_LINK; | |
438 | ks_drop(ks); | |
439 | } | |
440 | } | |
441 | ||
442 | /* --- @ksl_encrypt@ --- * | |
443 | * | |
410c8acf | 444 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
7ed14135 | 445 | * @unsigned ty@ = message type |
410c8acf | 446 | * @buf *b@ = pointer to input buffer |
447 | * @buf *bb@ = pointer to output buffer | |
448 | * | |
a50f9a0e MW |
449 | * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a |
450 | * new key; @KSERR_NOKEYS@ if there are no suitable keys | |
451 | * available. Also returns zero if there was insufficient | |
452 | * buffer space (but the output buffer is broken in this case). | |
410c8acf | 453 | * |
454 | * Use: Encrypts a packet. | |
455 | */ | |
456 | ||
7ed14135 | 457 | int ksl_encrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) |
410c8acf | 458 | { |
459 | time_t now = time(0); | |
426c0bc6 | 460 | keyset *ks = *ksroot; |
410c8acf | 461 | |
410c8acf | 462 | for (;;) { |
463 | if (!ks) { | |
426c0bc6 | 464 | T( trace(T_KEYSET, "keyset: no suitable keysets found"); ) |
410c8acf | 465 | buf_break(bb); |
a50f9a0e | 466 | return (KSERR_NOKEYS); |
410c8acf | 467 | } |
426c0bc6 | 468 | if (KEYOK(ks, now) && !(ks->f & KSF_LISTEN)) |
410c8acf | 469 | break; |
470 | ks = ks->next; | |
471 | } | |
472 | ||
7ed14135 | 473 | return (doencrypt(ks, ty, b, bb)); |
410c8acf | 474 | } |
475 | ||
426c0bc6 | 476 | /* --- @ksl_decrypt@ --- * |
410c8acf | 477 | * |
478 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
7ed14135 | 479 | * @unsigned ty@ = expected type code |
410c8acf | 480 | * @buf *b@ = pointer to input buffer |
481 | * @buf *bb@ = pointer to output buffer | |
482 | * | |
a50f9a0e MW |
483 | * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns |
484 | * zero if there was insufficient buffer (but the output buffer | |
485 | * is broken in this case). | |
410c8acf | 486 | * |
487 | * Use: Decrypts a packet. | |
488 | */ | |
489 | ||
7ed14135 | 490 | int ksl_decrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) |
410c8acf | 491 | { |
492 | time_t now = time(0); | |
410c8acf | 493 | keyset *ks; |
426c0bc6 | 494 | uint32 seq; |
12a26b8b | 495 | int err; |
410c8acf | 496 | |
426c0bc6 | 497 | if (buf_ensure(bb, BLEN(b))) |
12a26b8b | 498 | return (0); |
09585a65 | 499 | |
410c8acf | 500 | for (ks = *ksroot; ks; ks = ks->next) { |
410c8acf | 501 | if (!KEYOK(ks, now)) |
502 | continue; | |
12a26b8b | 503 | if ((err = dodecrypt(ks, ty, b, bb, &seq)) == 0) { |
426c0bc6 | 504 | if (ks->f & KSF_LISTEN) { |
505 | T( trace(T_KEYSET, "keyset: implicitly activating keyset %u", | |
506 | ks->seq); ) | |
507 | ks->f &= ~KSF_LISTEN; | |
508 | } | |
a50f9a0e | 509 | if (seq_check(&ks->iseq, seq, "SYMM")) |
12a26b8b | 510 | return (KSERR_SEQ); |
a50f9a0e MW |
511 | else |
512 | return (0); | |
410c8acf | 513 | } |
12a26b8b | 514 | if (err != KSERR_DECRYPT) return (err); |
410c8acf | 515 | } |
e945d6e4 | 516 | T( trace(T_KEYSET, "keyset: no matching keys, or incorrect MAC"); ) |
a50f9a0e | 517 | return (KSERR_DECRYPT); |
410c8acf | 518 | } |
519 | ||
520 | /*----- That's all, folks -------------------------------------------------*/ |