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 | * | |
11ad66c2 MW |
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. | |
e04c2d50 | 16 | * |
11ad66c2 MW |
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. | |
e04c2d50 | 21 | * |
410c8acf | 22 | * You should have received a copy of the GNU General Public License |
11ad66c2 | 23 | * along with TrIPE. If not, see <https://www.gnu.org/licenses/>. |
410c8acf | 24 | */ |
25 | ||
410c8acf | 26 | /*----- Header files ------------------------------------------------------*/ |
27 | ||
28 | #include "tripe.h" | |
29 | ||
410c8acf | 30 | /*----- Handy macros ------------------------------------------------------*/ |
31 | ||
32 | #define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now) | |
33 | ||
426c0bc6 | 34 | /*----- Low-level packet encryption and decryption ------------------------*/ |
410c8acf | 35 | |
59d670e7 | 36 | /* --- Encrypted data format --- * |
37 | * | |
7ed14135 | 38 | * Let %$p_i$% be the %$i$%-th plaintext message, with type %$t$%. We first |
e04c2d50 | 39 | * compute |
59d670e7 | 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 | * | |
7ed14135 | 45 | * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(t, i, c_i)$% |
59d670e7 | 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 | ||
426c0bc6 | 60 | /* --- @doencrypt@ --- * |
410c8acf | 61 | * |
426c0bc6 | 62 | * Arguments: @keyset *ks@ = pointer to keyset to use |
7ed14135 | 63 | * @unsigned ty@ = type of message this is |
426c0bc6 | 64 | * @buf *b@ = pointer to an input buffer |
65 | * @buf *bb@ = pointer to an output buffer | |
410c8acf | 66 | * |
a50f9a0e MW |
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. | |
410c8acf | 70 | * |
426c0bc6 | 71 | * Use: Encrypts a message with the given key. We assume that the |
72 | * keyset is OK to use. | |
410c8acf | 73 | */ |
74 | ||
7ed14135 | 75 | static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
410c8acf | 76 | { |
a93aacce | 77 | int rc; |
426c0bc6 | 78 | size_t sz = BLEFT(b); |
426c0bc6 | 79 | size_t osz, nsz; |
426c0bc6 | 80 | |
a93aacce | 81 | /* --- Initial tracing --- */ |
426c0bc6 | 82 | |
426c0bc6 | 83 | IF_TRACING(T_KEYSET, { |
a93aacce | 84 | trace(T_KEYSET, |
41b4ea14 | 85 | "keyset: encrypting packet %lu (type 0x%02x) using keyset %u", |
a93aacce MW |
86 | (unsigned long)ks->oseq, ty, ks->seq); |
87 | trace_block(T_CRYPTO, "crypto: plaintext packet", BCUR(b), sz); | |
426c0bc6 | 88 | }) |
59d670e7 | 89 | |
a93aacce | 90 | /* --- Apply the bulk-crypto transformation --- */ |
426c0bc6 | 91 | |
c70a7c5c | 92 | rc = ks->bulk->ops->encrypt(ks->bulk, ty, b, bb, ks->oseq); |
a93aacce | 93 | if (rc || !BOK(bb)) return (rc); |
c70a7c5c | 94 | ks->oseq++; |
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 | 130 | static 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 | 136 | trace(T_KEYSET, |
41b4ea14 | 137 | "keyset: try decrypting packet (type 0x%02x) using keyset %u", |
a93aacce MW |
138 | ty, ks->seq); |
139 | trace_block(T_CRYPTO, "crypto: ciphertext packet", BCUR(b), BLEFT(b)); | |
426c0bc6 | 140 | }) |
b5c45da1 | 141 | |
c70a7c5c | 142 | rc = ks->bulk->ops->decrypt(ks->bulk, ty, b, bb, seq); |
a93aacce | 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 | ||
165 | void ks_drop(keyset *ks) | |
166 | { | |
c70a7c5c MW |
167 | if (--ks->ref) return; |
168 | ks->bulk->ops->freectx(ks->bulk); | |
169 | DESTROY(ks); | |
170 | } | |
a93aacce | 171 | |
c70a7c5c MW |
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 | */ | |
a93aacce | 188 | |
c70a7c5c MW |
189 | struct rawkey { |
190 | const gchash *hc; | |
191 | const octet *k; | |
192 | size_t x, y, z; | |
193 | }; | |
a93aacce | 194 | |
c70a7c5c MW |
195 | void 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]; | |
8111ced6 | 222 | sprintf(_buf, "crypto: %s key %s", dir ? "outgoing" : "incoming", what); |
c70a7c5c MW |
223 | trace_block(T_CRYPTO, _buf, k, ksz); |
224 | }) }) | |
410c8acf | 225 | } |
226 | ||
227 | /* --- @ks_gen@ --- * | |
228 | * | |
426c0bc6 | 229 | * Arguments: @const void *k@ = pointer to key material |
230 | * @size_t x, y, z@ = offsets into key material (see below) | |
e04c2d50 | 231 | * @peer *p@ = pointer to peer information |
410c8acf | 232 | * |
426c0bc6 | 233 | * Returns: A pointer to the new keyset. |
410c8acf | 234 | * |
426c0bc6 | 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 | |
ba487ab5 MW |
241 | * collections of symmetric keys: one for outgoing messages, the |
242 | * other for incoming messages. | |
426c0bc6 | 243 | * |
244 | * The new key is marked so that it won't be selected for output | |
245 | * by @ksl_encrypt@. You can still encrypt data with it by | |
246 | * calling @ks_encrypt@ directly. | |
410c8acf | 247 | */ |
248 | ||
9466fafa | 249 | keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p) |
410c8acf | 250 | { |
410c8acf | 251 | keyset *ks = CREATE(keyset); |
252 | time_t now = time(0); | |
35c8b547 | 253 | const algswitch *algs = &p->kx.kpriv->algs; |
c70a7c5c | 254 | struct rawkey rk; |
410c8acf | 255 | T( static unsigned seq = 0; ) |
256 | ||
257 | T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); ) | |
258 | ||
c70a7c5c MW |
259 | rk.hc = algs->h; rk.k = k; rk.x = x; rk.y = y; rk.z = z; |
260 | ks->bulk = algs->bulk->ops->genkeys(algs->bulk, &rk); | |
261 | ks->bulk->ops = algs->bulk->ops; | |
410c8acf | 262 | |
263 | T( ks->seq = seq++; ) | |
e945d6e4 | 264 | ks->ref = 1; |
426c0bc6 | 265 | ks->t_exp = now + T_EXP; |
c70a7c5c MW |
266 | ks->sz_exp = algs->bulk->ops->expsz(algs->bulk); |
267 | ks->sz_regen = ks->sz_exp/2; | |
37941236 | 268 | ks->oseq = 0; |
269 | seq_reset(&ks->iseq); | |
426c0bc6 | 270 | ks->next = 0; |
9466fafa | 271 | ks->p = p; |
426c0bc6 | 272 | ks->f = KSF_LISTEN; |
426c0bc6 | 273 | return (ks); |
274 | } | |
275 | ||
426c0bc6 | 276 | /* --- @ks_activate@ --- * |
277 | * | |
278 | * Arguments: @keyset *ks@ = pointer to a keyset | |
279 | * | |
280 | * Returns: --- | |
281 | * | |
282 | * Use: Activates a keyset, so that it can be used for encrypting | |
283 | * outgoing messages. | |
284 | */ | |
285 | ||
286 | void ks_activate(keyset *ks) | |
287 | { | |
288 | if (ks->f & KSF_LISTEN) { | |
289 | T( trace(T_KEYSET, "keyset: activating keyset %u", ks->seq); ) | |
290 | ks->f &= ~KSF_LISTEN; | |
291 | } | |
410c8acf | 292 | } |
293 | ||
294 | /* --- @ks_encrypt@ --- * | |
295 | * | |
426c0bc6 | 296 | * Arguments: @keyset *ks@ = pointer to a keyset |
7ed14135 | 297 | * @unsigned ty@ = message type |
426c0bc6 | 298 | * @buf *b@ = pointer to input buffer |
299 | * @buf *bb@ = pointer to output buffer | |
300 | * | |
a50f9a0e MW |
301 | * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a |
302 | * new key; @KSERR_NOKEYS@ if the key is not usable. Also | |
303 | * returns zero if there was insufficient buffer (but the output | |
304 | * buffer is broken in this case). | |
426c0bc6 | 305 | * |
306 | * Use: Encrypts a block of data using the key. Note that the `key | |
307 | * ought to be replaced' notification is only ever given once | |
308 | * for each key. Also note that this call forces a keyset to be | |
309 | * used even if it's marked as not for data output. | |
a93aacce MW |
310 | * |
311 | * The encryption transform is permitted to corrupt @buf_u@ for | |
312 | * its own purposes. Neither the source nor destination should | |
313 | * be within @buf_u@; and callers mustn't expect anything stored | |
314 | * in @buf_u@ to still | |
426c0bc6 | 315 | */ |
316 | ||
7ed14135 | 317 | int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
426c0bc6 | 318 | { |
319 | time_t now = time(0); | |
320 | ||
321 | if (!KEYOK(ks, now)) { | |
322 | buf_break(bb); | |
a50f9a0e | 323 | return (KSERR_NOKEYS); |
426c0bc6 | 324 | } |
7ed14135 | 325 | return (doencrypt(ks, ty, b, bb)); |
426c0bc6 | 326 | } |
327 | ||
328 | /* --- @ks_decrypt@ --- * | |
329 | * | |
330 | * Arguments: @keyset *ks@ = pointer to a keyset | |
7ed14135 | 331 | * @unsigned ty@ = expected type code |
426c0bc6 | 332 | * @buf *b@ = pointer to an input buffer |
333 | * @buf *bb@ = pointer to an output buffer | |
334 | * | |
12a26b8b | 335 | * Returns: Zero on success; @KSERR_...@ on failure. Also returns |
a50f9a0e MW |
336 | * zero if there was insufficient buffer (but the output buffer |
337 | * is broken in this case). | |
426c0bc6 | 338 | * |
339 | * Use: Attempts to decrypt a message using a given key. Note that | |
340 | * requesting decryption with a key directly won't clear a | |
341 | * marking that it's not for encryption. | |
a93aacce MW |
342 | * |
343 | * The decryption transform is permitted to corrupt @buf_u@ for | |
344 | * its own purposes. Neither the source nor destination should | |
345 | * be within @buf_u@; and callers mustn't expect anything stored | |
346 | * in @buf_u@ to still | |
426c0bc6 | 347 | */ |
348 | ||
7ed14135 | 349 | int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
426c0bc6 | 350 | { |
351 | time_t now = time(0); | |
352 | uint32 seq; | |
12a26b8b | 353 | int err; |
426c0bc6 | 354 | |
12a26b8b MW |
355 | if (!KEYOK(ks, now)) return (KSERR_DECRYPT); |
356 | if (buf_ensure(bb, BLEN(b))) return (0); | |
357 | if ((err = dodecrypt(ks, ty, b, bb, &seq)) != 0) return (err); | |
358 | if (seq_check(&ks->iseq, seq, "SYMM")) return (KSERR_SEQ); | |
426c0bc6 | 359 | return (0); |
360 | } | |
361 | ||
362 | /*----- Keyset list handling ----------------------------------------------*/ | |
363 | ||
364 | /* --- @ksl_free@ --- * | |
365 | * | |
366 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
367 | * | |
368 | * Returns: --- | |
369 | * | |
370 | * Use: Frees (releases references to) all of the keys in a keyset. | |
371 | */ | |
372 | ||
373 | void ksl_free(keyset **ksroot) | |
374 | { | |
375 | keyset *ks, *ksn; | |
376 | for (ks = *ksroot; ks; ks = ksn) { | |
377 | ksn = ks->next; | |
378 | ks->f &= ~KSF_LINK; | |
379 | ks_drop(ks); | |
380 | } | |
381 | } | |
382 | ||
383 | /* --- @ksl_link@ --- * | |
384 | * | |
385 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
386 | * @keyset *ks@ = pointer to a keyset | |
387 | * | |
388 | * Returns: --- | |
389 | * | |
390 | * Use: Links a keyset into a list. A keyset can only be on one list | |
391 | * at a time. Bad things happen otherwise. | |
392 | */ | |
393 | ||
394 | void ksl_link(keyset **ksroot, keyset *ks) | |
395 | { | |
396 | assert(!(ks->f & KSF_LINK)); | |
397 | ks->next = *ksroot; | |
398 | *ksroot = ks; | |
399 | ks->f |= KSF_LINK; | |
400 | ks->ref++; | |
401 | } | |
402 | ||
403 | /* --- @ksl_prune@ --- * | |
404 | * | |
405 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
406 | * | |
407 | * Returns: --- | |
408 | * | |
409 | * Use: Prunes the keyset list by removing keys which mustn't be used | |
410 | * any more. | |
411 | */ | |
412 | ||
413 | void ksl_prune(keyset **ksroot) | |
414 | { | |
415 | time_t now = time(0); | |
416 | ||
417 | while (*ksroot) { | |
418 | keyset *ks = *ksroot; | |
419 | ||
420 | if (ks->t_exp <= now) { | |
421 | T( trace(T_KEYSET, "keyset: expiring keyset %u (time limit reached)", | |
422 | ks->seq); ) | |
423 | goto kill; | |
424 | } else if (ks->sz_exp == 0) { | |
425 | T( trace(T_KEYSET, "keyset: expiring keyset %u (data limit reached)", | |
426 | ks->seq); ) | |
427 | goto kill; | |
428 | } else { | |
429 | ksroot = &ks->next; | |
430 | continue; | |
431 | } | |
432 | ||
433 | kill: | |
434 | *ksroot = ks->next; | |
435 | ks->f &= ~KSF_LINK; | |
436 | ks_drop(ks); | |
437 | } | |
438 | } | |
439 | ||
440 | /* --- @ksl_encrypt@ --- * | |
441 | * | |
410c8acf | 442 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
7ed14135 | 443 | * @unsigned ty@ = message type |
410c8acf | 444 | * @buf *b@ = pointer to input buffer |
445 | * @buf *bb@ = pointer to output buffer | |
446 | * | |
a50f9a0e MW |
447 | * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a |
448 | * new key; @KSERR_NOKEYS@ if there are no suitable keys | |
449 | * available. Also returns zero if there was insufficient | |
450 | * buffer space (but the output buffer is broken in this case). | |
410c8acf | 451 | * |
452 | * Use: Encrypts a packet. | |
453 | */ | |
454 | ||
7ed14135 | 455 | int ksl_encrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) |
410c8acf | 456 | { |
457 | time_t now = time(0); | |
426c0bc6 | 458 | keyset *ks = *ksroot; |
410c8acf | 459 | |
410c8acf | 460 | for (;;) { |
461 | if (!ks) { | |
426c0bc6 | 462 | T( trace(T_KEYSET, "keyset: no suitable keysets found"); ) |
410c8acf | 463 | buf_break(bb); |
a50f9a0e | 464 | return (KSERR_NOKEYS); |
410c8acf | 465 | } |
426c0bc6 | 466 | if (KEYOK(ks, now) && !(ks->f & KSF_LISTEN)) |
410c8acf | 467 | break; |
468 | ks = ks->next; | |
469 | } | |
470 | ||
7ed14135 | 471 | return (doencrypt(ks, ty, b, bb)); |
410c8acf | 472 | } |
473 | ||
426c0bc6 | 474 | /* --- @ksl_decrypt@ --- * |
410c8acf | 475 | * |
476 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
7ed14135 | 477 | * @unsigned ty@ = expected type code |
410c8acf | 478 | * @buf *b@ = pointer to input buffer |
479 | * @buf *bb@ = pointer to output buffer | |
480 | * | |
a50f9a0e MW |
481 | * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns |
482 | * zero if there was insufficient buffer (but the output buffer | |
483 | * is broken in this case). | |
410c8acf | 484 | * |
485 | * Use: Decrypts a packet. | |
486 | */ | |
487 | ||
7ed14135 | 488 | int ksl_decrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) |
410c8acf | 489 | { |
490 | time_t now = time(0); | |
410c8acf | 491 | keyset *ks; |
426c0bc6 | 492 | uint32 seq; |
12a26b8b | 493 | int err; |
410c8acf | 494 | |
426c0bc6 | 495 | if (buf_ensure(bb, BLEN(b))) |
12a26b8b | 496 | return (0); |
09585a65 | 497 | |
410c8acf | 498 | for (ks = *ksroot; ks; ks = ks->next) { |
410c8acf | 499 | if (!KEYOK(ks, now)) |
500 | continue; | |
12a26b8b | 501 | if ((err = dodecrypt(ks, ty, b, bb, &seq)) == 0) { |
426c0bc6 | 502 | if (ks->f & KSF_LISTEN) { |
503 | T( trace(T_KEYSET, "keyset: implicitly activating keyset %u", | |
504 | ks->seq); ) | |
505 | ks->f &= ~KSF_LISTEN; | |
506 | } | |
a50f9a0e | 507 | if (seq_check(&ks->iseq, seq, "SYMM")) |
12a26b8b | 508 | return (KSERR_SEQ); |
a50f9a0e MW |
509 | else |
510 | return (0); | |
410c8acf | 511 | } |
12a26b8b | 512 | if (err != KSERR_DECRYPT) return (err); |
410c8acf | 513 | } |
e945d6e4 | 514 | T( trace(T_KEYSET, "keyset: no matching keys, or incorrect MAC"); ) |
a50f9a0e | 515 | return (KSERR_DECRYPT); |
410c8acf | 516 | } |
517 | ||
518 | /*----- That's all, folks -------------------------------------------------*/ |