| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 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 | |
| 29 | /*----- Header files ------------------------------------------------------*/ |
| 30 | |
| 31 | #include "tripe.h" |
| 32 | |
| 33 | /*----- Tunable parameters ------------------------------------------------*/ |
| 34 | |
| 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 |
| 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. |
| 41 | */ |
| 42 | |
| 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 */ |
| 47 | |
| 48 | /*----- Handy macros ------------------------------------------------------*/ |
| 49 | |
| 50 | #define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now) |
| 51 | |
| 52 | #define SEQSZ 4 /* Size of sequence number packet */ |
| 53 | |
| 54 | /*----- Low-level packet encryption and decryption ------------------------*/ |
| 55 | |
| 56 | /* --- Encrypted data format --- * |
| 57 | * |
| 58 | * Let %$p_i$% be the %$i$%-th plaintext message, with type %$t$%. We first |
| 59 | * compute |
| 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 | * |
| 65 | * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(t, i, c_i)$% |
| 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 | |
| 80 | /* --- @doencrypt@ --- * |
| 81 | * |
| 82 | * Arguments: @keyset *ks@ = pointer to keyset to use |
| 83 | * @unsigned ty@ = type of message this is |
| 84 | * @buf *b@ = pointer to an input buffer |
| 85 | * @buf *bb@ = pointer to an output buffer |
| 86 | * |
| 87 | * Returns: Zero if OK, nonzero if a new key is required. |
| 88 | * |
| 89 | * Use: Encrypts a message with the given key. We assume that the |
| 90 | * keyset is OK to use. |
| 91 | */ |
| 92 | |
| 93 | static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
| 94 | { |
| 95 | ghash *h; |
| 96 | gcipher *c = ks->cout; |
| 97 | const octet *p = BCUR(b); |
| 98 | size_t sz = BLEFT(b); |
| 99 | octet *qmac, *qseq, *qiv, *qpk; |
| 100 | uint32 oseq; |
| 101 | size_t ivsz = GC_CLASS(c)->blksz; |
| 102 | size_t tagsz = ks->tagsz; |
| 103 | size_t osz, nsz; |
| 104 | octet t[4]; |
| 105 | int rc = 0; |
| 106 | |
| 107 | /* --- Allocate the required buffer space --- */ |
| 108 | |
| 109 | if (buf_ensure(bb, tagsz + SEQSZ + ivsz + sz)) |
| 110 | return (0); /* Caution! */ |
| 111 | qmac = BCUR(bb); qseq = qmac + tagsz; qiv = qseq + SEQSZ; qpk = qiv + ivsz; |
| 112 | BSTEP(bb, tagsz + SEQSZ + ivsz + sz); |
| 113 | STORE32(t, ty); |
| 114 | |
| 115 | oseq = ks->oseq++; STORE32(qseq, oseq); |
| 116 | IF_TRACING(T_KEYSET, { |
| 117 | trace(T_KEYSET, "keyset: encrypting packet %lu using keyset %u", |
| 118 | (unsigned long)oseq, ks->seq); |
| 119 | trace_block(T_CRYPTO, "crypto: plaintext packet", p, sz); |
| 120 | }) |
| 121 | |
| 122 | /* --- Encrypt the packet --- */ |
| 123 | |
| 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); |
| 132 | IF_TRACING(T_KEYSET, { |
| 133 | trace_block(T_CRYPTO, "crypto: encrypted packet", qpk, sz); |
| 134 | }) |
| 135 | |
| 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 | |
| 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); |
| 163 | } |
| 164 | |
| 165 | /* --- @dodecrypt@ --- * |
| 166 | * |
| 167 | * Arguments: @keyset *ks@ = pointer to keyset to use |
| 168 | * @unsigned ty@ = expected type code |
| 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 |
| 172 | * |
| 173 | * Returns: Zero if OK, nonzero if it failed. |
| 174 | * |
| 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@. |
| 181 | */ |
| 182 | |
| 183 | static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq) |
| 184 | { |
| 185 | const octet *pmac, *piv, *pseq, *ppk; |
| 186 | size_t psz = BLEFT(b); |
| 187 | size_t sz; |
| 188 | octet *q = BCUR(bb); |
| 189 | ghash *h; |
| 190 | gcipher *c = ks->cin; |
| 191 | size_t ivsz = GC_CLASS(c)->blksz; |
| 192 | size_t tagsz = ks->tagsz; |
| 193 | octet *mac; |
| 194 | int eq; |
| 195 | octet t[4]; |
| 196 | |
| 197 | /* --- Break up the packet into its components --- */ |
| 198 | |
| 199 | if (psz < ivsz + SEQSZ + tagsz) { |
| 200 | T( trace(T_KEYSET, "keyset: block too small for keyset %u", ks->seq); ) |
| 201 | return (-1); |
| 202 | } |
| 203 | sz = psz - ivsz - SEQSZ - tagsz; |
| 204 | pmac = BCUR(b); pseq = pmac + tagsz; piv = pseq + SEQSZ; ppk = piv + ivsz; |
| 205 | STORE32(t, ty); |
| 206 | |
| 207 | IF_TRACING(T_KEYSET, { |
| 208 | trace(T_KEYSET, "keyset: decrypting using keyset %u", ks->seq); |
| 209 | trace_block(T_CRYPTO, "crypto: ciphertext packet", ppk, sz); |
| 210 | }) |
| 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); |
| 220 | IF_TRACING(T_KEYSET, { |
| 221 | trace_block(T_CRYPTO, "crypto: computed MAC", mac, tagsz); |
| 222 | }) |
| 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 | } |
| 231 | } |
| 232 | |
| 233 | /* --- Decrypt the packet --- */ |
| 234 | |
| 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); |
| 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); |
| 251 | } |
| 252 | |
| 253 | /* --- @dosequence@ --- * |
| 254 | * |
| 255 | * Arguments: @keyset *ks@ = pointer to a keyset |
| 256 | * @uint32 seq@ = a sequence number from a packet |
| 257 | * |
| 258 | * Returns: Zero if the sequence number is OK, nonzero if it's not. |
| 259 | * |
| 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. |
| 264 | */ |
| 265 | |
| 266 | static int dosequence(keyset *ks, uint32 seq) |
| 267 | { |
| 268 | uint32 seqbit; |
| 269 | uint32 n; |
| 270 | |
| 271 | if (seq < ks->iseq) { |
| 272 | a_warn("SYMM replay old-sequence"); |
| 273 | return (-1); |
| 274 | } |
| 275 | if (seq >= ks->iseq + KS_SEQWINSZ) { |
| 276 | n = seq - (ks->iseq + KS_SEQWINSZ - 1); |
| 277 | if (n < KS_SEQWINSZ) |
| 278 | ks->iwin >>= n; |
| 279 | else |
| 280 | ks->iwin = 0; |
| 281 | ks->iseq += n; |
| 282 | } |
| 283 | seqbit = 1 << (seq - ks->iseq); |
| 284 | if (ks->iwin & seqbit) { |
| 285 | a_warn("SYMM replay duplicated-sequence"); |
| 286 | return (-1); |
| 287 | } |
| 288 | ks->iwin |= seqbit; |
| 289 | return (0); |
| 290 | } |
| 291 | |
| 292 | /*----- Operations on a single keyset -------------------------------------*/ |
| 293 | |
| 294 | /* --- @ks_drop@ --- * |
| 295 | * |
| 296 | * Arguments: @keyset *ks@ = pointer to a keyset |
| 297 | * |
| 298 | * Returns: --- |
| 299 | * |
| 300 | * Use: Decrements a keyset's reference counter. If the counter hits |
| 301 | * zero, the keyset is freed. |
| 302 | */ |
| 303 | |
| 304 | void ks_drop(keyset *ks) |
| 305 | { |
| 306 | if (--ks->ref) |
| 307 | return; |
| 308 | GC_DESTROY(ks->cin); |
| 309 | GC_DESTROY(ks->cout); |
| 310 | GM_DESTROY(ks->min); |
| 311 | GM_DESTROY(ks->mout); |
| 312 | DESTROY(ks); |
| 313 | } |
| 314 | |
| 315 | /* --- @ks_gen@ --- * |
| 316 | * |
| 317 | * Arguments: @const void *k@ = pointer to key material |
| 318 | * @size_t x, y, z@ = offsets into key material (see below) |
| 319 | * @peer *p@ = pointer to peer information |
| 320 | * |
| 321 | * Returns: A pointer to the new keyset. |
| 322 | * |
| 323 | * Use: Derives a new keyset from the given key material. The |
| 324 | * offsets @x@, @y@ and @z@ separate the key material into three |
| 325 | * parts. Between the @k@ and @k + x@ is `my' contribution to |
| 326 | * the key material; between @k + x@ and @k + y@ is `your' |
| 327 | * contribution; and between @k + y@ and @k + z@ is a shared |
| 328 | * value we made together. These are used to construct two |
| 329 | * pairs of symmetric keys. Each pair consists of an encryption |
| 330 | * key and a message authentication key. One pair is used for |
| 331 | * outgoing messages, the other for incoming messages. |
| 332 | * |
| 333 | * The new key is marked so that it won't be selected for output |
| 334 | * by @ksl_encrypt@. You can still encrypt data with it by |
| 335 | * calling @ks_encrypt@ directly. |
| 336 | */ |
| 337 | |
| 338 | keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p) |
| 339 | { |
| 340 | ghash *h; |
| 341 | const octet *hh; |
| 342 | keyset *ks = CREATE(keyset); |
| 343 | time_t now = time(0); |
| 344 | const octet *pp = k; |
| 345 | T( static unsigned seq = 0; ) |
| 346 | |
| 347 | T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); ) |
| 348 | |
| 349 | /* --- Construct the various keys --- * |
| 350 | * |
| 351 | * This is done with macros, because it's quite tedious. |
| 352 | */ |
| 353 | |
| 354 | #define MINE GH_HASH(h, pp, x) |
| 355 | #define YOURS GH_HASH(h, pp + x, y - x) |
| 356 | #define OURS GH_HASH(h, pp + y, z - y) |
| 357 | |
| 358 | #define HASH_in MINE; YOURS; OURS |
| 359 | #define HASH_out YOURS; MINE; OURS |
| 360 | #define INIT_c(k) GC_INIT(algs.c, (k), algs.cksz) |
| 361 | #define INIT_m(k) GM_KEY(algs.m, (k), algs.mksz) |
| 362 | #define STR_c "encryption" |
| 363 | #define STR_m "integrity" |
| 364 | #define STR_in "incoming" |
| 365 | #define STR_out "outgoing" |
| 366 | |
| 367 | #define SETKEY(a, dir) do { \ |
| 368 | h = GH_INIT(algs.h); \ |
| 369 | HASH_STRING(h, "tripe-" STR_##a); \ |
| 370 | HASH_##dir; \ |
| 371 | hh = GH_DONE(h, 0); \ |
| 372 | IF_TRACING(T_KEYSET, { \ |
| 373 | trace_block(T_CRYPTO, "crypto: " STR_##dir " key " STR_##a, \ |
| 374 | hh, algs.a##ksz); \ |
| 375 | }) \ |
| 376 | ks->a##dir = INIT_##a(hh); \ |
| 377 | GH_DESTROY(h); \ |
| 378 | } while (0) |
| 379 | |
| 380 | SETKEY(c, in); SETKEY(c, out); |
| 381 | SETKEY(m, in); SETKEY(m, out); |
| 382 | |
| 383 | #undef MINE |
| 384 | #undef YOURS |
| 385 | #undef OURS |
| 386 | #undef STR_c |
| 387 | #undef STR_m |
| 388 | #undef STR_in |
| 389 | #undef STR_out |
| 390 | #undef INIT_c |
| 391 | #undef INIT_m |
| 392 | #undef HASH_in |
| 393 | #undef HASH_out |
| 394 | #undef SETKEY |
| 395 | |
| 396 | T( ks->seq = seq++; ) |
| 397 | ks->ref = 1; |
| 398 | ks->t_exp = now + T_EXP; |
| 399 | ks->sz_exp = SZ_EXP; |
| 400 | ks->oseq = ks->iseq = 0; |
| 401 | ks->iwin = 0; |
| 402 | ks->next = 0; |
| 403 | ks->p = p; |
| 404 | ks->f = KSF_LISTEN; |
| 405 | ks->tagsz = algs.tagsz; |
| 406 | return (ks); |
| 407 | } |
| 408 | |
| 409 | /* --- @ks_tregen@ --- * |
| 410 | * |
| 411 | * Arguments: @keyset *ks@ = pointer to a keyset |
| 412 | * |
| 413 | * Returns: The time at which moves ought to be made to replace this key. |
| 414 | */ |
| 415 | |
| 416 | time_t ks_tregen(keyset *ks) { return (ks->t_exp - T_EXP + T_REGEN); } |
| 417 | |
| 418 | /* --- @ks_activate@ --- * |
| 419 | * |
| 420 | * Arguments: @keyset *ks@ = pointer to a keyset |
| 421 | * |
| 422 | * Returns: --- |
| 423 | * |
| 424 | * Use: Activates a keyset, so that it can be used for encrypting |
| 425 | * outgoing messages. |
| 426 | */ |
| 427 | |
| 428 | void ks_activate(keyset *ks) |
| 429 | { |
| 430 | if (ks->f & KSF_LISTEN) { |
| 431 | T( trace(T_KEYSET, "keyset: activating keyset %u", ks->seq); ) |
| 432 | ks->f &= ~KSF_LISTEN; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* --- @ks_encrypt@ --- * |
| 437 | * |
| 438 | * Arguments: @keyset *ks@ = pointer to a keyset |
| 439 | * @unsigned ty@ = message type |
| 440 | * @buf *b@ = pointer to input buffer |
| 441 | * @buf *bb@ = pointer to output buffer |
| 442 | * |
| 443 | * Returns: Zero if OK, nonzero if the key needs replacing. If the |
| 444 | * encryption failed, the output buffer is broken and zero is |
| 445 | * returned. |
| 446 | * |
| 447 | * Use: Encrypts a block of data using the key. Note that the `key |
| 448 | * ought to be replaced' notification is only ever given once |
| 449 | * for each key. Also note that this call forces a keyset to be |
| 450 | * used even if it's marked as not for data output. |
| 451 | */ |
| 452 | |
| 453 | int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
| 454 | { |
| 455 | time_t now = time(0); |
| 456 | |
| 457 | if (!KEYOK(ks, now)) { |
| 458 | buf_break(bb); |
| 459 | return (0); |
| 460 | } |
| 461 | return (doencrypt(ks, ty, b, bb)); |
| 462 | } |
| 463 | |
| 464 | /* --- @ks_decrypt@ --- * |
| 465 | * |
| 466 | * Arguments: @keyset *ks@ = pointer to a keyset |
| 467 | * @unsigned ty@ = expected type code |
| 468 | * @buf *b@ = pointer to an input buffer |
| 469 | * @buf *bb@ = pointer to an output buffer |
| 470 | * |
| 471 | * Returns: Zero on success, or nonzero if there was some problem. |
| 472 | * |
| 473 | * Use: Attempts to decrypt a message using a given key. Note that |
| 474 | * requesting decryption with a key directly won't clear a |
| 475 | * marking that it's not for encryption. |
| 476 | */ |
| 477 | |
| 478 | int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb) |
| 479 | { |
| 480 | time_t now = time(0); |
| 481 | uint32 seq; |
| 482 | |
| 483 | if (!KEYOK(ks, now) || |
| 484 | buf_ensure(bb, BLEN(b)) || |
| 485 | dodecrypt(ks, ty, b, bb, &seq) || |
| 486 | dosequence(ks, seq)) |
| 487 | return (-1); |
| 488 | return (0); |
| 489 | } |
| 490 | |
| 491 | /*----- Keyset list handling ----------------------------------------------*/ |
| 492 | |
| 493 | /* --- @ksl_free@ --- * |
| 494 | * |
| 495 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 496 | * |
| 497 | * Returns: --- |
| 498 | * |
| 499 | * Use: Frees (releases references to) all of the keys in a keyset. |
| 500 | */ |
| 501 | |
| 502 | void ksl_free(keyset **ksroot) |
| 503 | { |
| 504 | keyset *ks, *ksn; |
| 505 | for (ks = *ksroot; ks; ks = ksn) { |
| 506 | ksn = ks->next; |
| 507 | ks->f &= ~KSF_LINK; |
| 508 | ks_drop(ks); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /* --- @ksl_link@ --- * |
| 513 | * |
| 514 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 515 | * @keyset *ks@ = pointer to a keyset |
| 516 | * |
| 517 | * Returns: --- |
| 518 | * |
| 519 | * Use: Links a keyset into a list. A keyset can only be on one list |
| 520 | * at a time. Bad things happen otherwise. |
| 521 | */ |
| 522 | |
| 523 | void ksl_link(keyset **ksroot, keyset *ks) |
| 524 | { |
| 525 | assert(!(ks->f & KSF_LINK)); |
| 526 | ks->next = *ksroot; |
| 527 | *ksroot = ks; |
| 528 | ks->f |= KSF_LINK; |
| 529 | ks->ref++; |
| 530 | } |
| 531 | |
| 532 | /* --- @ksl_prune@ --- * |
| 533 | * |
| 534 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 535 | * |
| 536 | * Returns: --- |
| 537 | * |
| 538 | * Use: Prunes the keyset list by removing keys which mustn't be used |
| 539 | * any more. |
| 540 | */ |
| 541 | |
| 542 | void ksl_prune(keyset **ksroot) |
| 543 | { |
| 544 | time_t now = time(0); |
| 545 | |
| 546 | while (*ksroot) { |
| 547 | keyset *ks = *ksroot; |
| 548 | |
| 549 | if (ks->t_exp <= now) { |
| 550 | T( trace(T_KEYSET, "keyset: expiring keyset %u (time limit reached)", |
| 551 | ks->seq); ) |
| 552 | goto kill; |
| 553 | } else if (ks->sz_exp == 0) { |
| 554 | T( trace(T_KEYSET, "keyset: expiring keyset %u (data limit reached)", |
| 555 | ks->seq); ) |
| 556 | goto kill; |
| 557 | } else { |
| 558 | ksroot = &ks->next; |
| 559 | continue; |
| 560 | } |
| 561 | |
| 562 | kill: |
| 563 | *ksroot = ks->next; |
| 564 | ks->f &= ~KSF_LINK; |
| 565 | ks_drop(ks); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /* --- @ksl_encrypt@ --- * |
| 570 | * |
| 571 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 572 | * @unsigned ty@ = message type |
| 573 | * @buf *b@ = pointer to input buffer |
| 574 | * @buf *bb@ = pointer to output buffer |
| 575 | * |
| 576 | * Returns: Nonzero if a new key is needed. |
| 577 | * |
| 578 | * Use: Encrypts a packet. |
| 579 | */ |
| 580 | |
| 581 | int ksl_encrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) |
| 582 | { |
| 583 | time_t now = time(0); |
| 584 | keyset *ks = *ksroot; |
| 585 | |
| 586 | for (;;) { |
| 587 | if (!ks) { |
| 588 | T( trace(T_KEYSET, "keyset: no suitable keysets found"); ) |
| 589 | buf_break(bb); |
| 590 | return (-1); |
| 591 | } |
| 592 | if (KEYOK(ks, now) && !(ks->f & KSF_LISTEN)) |
| 593 | break; |
| 594 | ks = ks->next; |
| 595 | } |
| 596 | |
| 597 | return (doencrypt(ks, ty, b, bb)); |
| 598 | } |
| 599 | |
| 600 | /* --- @ksl_decrypt@ --- * |
| 601 | * |
| 602 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 603 | * @unsigned ty@ = expected type code |
| 604 | * @buf *b@ = pointer to input buffer |
| 605 | * @buf *bb@ = pointer to output buffer |
| 606 | * |
| 607 | * Returns: Nonzero if the packet couldn't be decrypted. |
| 608 | * |
| 609 | * Use: Decrypts a packet. |
| 610 | */ |
| 611 | |
| 612 | int ksl_decrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) |
| 613 | { |
| 614 | time_t now = time(0); |
| 615 | keyset *ks; |
| 616 | uint32 seq; |
| 617 | |
| 618 | if (buf_ensure(bb, BLEN(b))) |
| 619 | return (-1); |
| 620 | |
| 621 | for (ks = *ksroot; ks; ks = ks->next) { |
| 622 | if (!KEYOK(ks, now)) |
| 623 | continue; |
| 624 | if (!dodecrypt(ks, ty, b, bb, &seq)) { |
| 625 | if (ks->f & KSF_LISTEN) { |
| 626 | T( trace(T_KEYSET, "keyset: implicitly activating keyset %u", |
| 627 | ks->seq); ) |
| 628 | ks->f &= ~KSF_LISTEN; |
| 629 | } |
| 630 | return (dosequence(ks, seq)); |
| 631 | } |
| 632 | } |
| 633 | T( trace(T_KEYSET, "keyset: no matching keys, or incorrect MAC"); ) |
| 634 | return (-1); |
| 635 | } |
| 636 | |
| 637 | /*----- That's all, folks -------------------------------------------------*/ |