3 * $Id: keyset.c,v 1.4 2001/06/16 14:06:40 mdw Exp $
5 * Handling of symmetric keysets
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.4 2001/06/16 14:06:40 mdw
33 * Quantify collision probabilities for the stated data volume bounds.
35 * Revision 1.3 2001/02/16 21:39:55 mdw
36 * Major overhaul. Separate functions for manipulating keysets from
37 * functions for manipulating keyset lists. Introduce a concept of
38 * listening-only keys.
40 * Revision 1.2 2001/02/05 19:53:23 mdw
41 * Add sequence number protection.
43 * Revision 1.1 2001/02/03 20:26:37 mdw
48 /*----- Header files ------------------------------------------------------*/
52 /*----- Tunable parameters ------------------------------------------------*/
54 /* --- Note on size limits --- *
56 * For a 64-bit block cipher (e.g., Blowfish), the probability of a collision
57 * occurring after 32 MB is less than %$2^{-21}$%, and the probability of a
58 * collision occurring after 64 MB is less than %$2^{-19}$%.
61 #define T_EXP MIN(60) /* Expiry time for a key */
62 #define T_REGEN MIN(45) /* Regeneration time for a key */
63 #define SZ_EXP MEG(64) /* Expiry data size for a key */
64 #define SZ_REGEN MEG(32) /* Data size threshold for regen */
66 /*----- Handy macros ------------------------------------------------------*/
68 #define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now)
70 /*----- Low-level packet encryption and decryption ------------------------*/
72 /* --- @doencrypt@ --- *
74 * Arguments: @keyset *ks@ = pointer to keyset to use
75 * @buf *b@ = pointer to an input buffer
76 * @buf *bb@ = pointer to an output buffer
78 * Returns: Zero if OK, nonzero if a new key is required.
80 * Use: Encrypts a message with the given key. We assume that the
81 * keyset is OK to use.
84 static int doencrypt(keyset *ks, buf *b, buf *bb)
89 const octet *p = BCUR(b);
91 octet *qiv, *qseq, *qpk;
96 /* --- Allocate the required buffer space --- */
99 ivsz = c->ops->c->blksz;
100 if (buf_ensure(bb, ivsz + 4 + sz))
101 return (0); /* Caution! */
102 qiv = BCUR(bb); qseq = qiv + ivsz; qpk = qseq + 4;
103 BSTEP(bb, ivsz + 4 + sz);
105 /* --- MAC and encrypt the packet --- */
107 oseq = ks->oseq++; STORE32(qseq, oseq);
108 h = ks->mout->ops->init(ks->mout);
109 h->ops->hash(h, qseq, 4);
110 h->ops->hash(h, p, sz);
111 memcpy(qiv, h->ops->done(h, 0), ivsz);
113 IF_TRACING(T_KEYSET, {
114 trace(T_KEYSET, "keyset: encrypting packet %lu using keyset %u",
115 (unsigned long)oseq, ks->seq);
116 trace_block(T_CRYPTO, "crypto: computed MAC", qiv, ivsz);
118 c->ops->setiv(c, qiv);
119 c->ops->encrypt(c, p, qpk, sz);
120 IF_TRACING(T_KEYSET, {
121 trace_block(T_CRYPTO, "crypto: encrypted packet", qpk, sz);
124 /* --- Deduct the packet size from the key's data life --- */
131 if (osz >= SZ_REGEN && nsz < SZ_REGEN) {
132 T( trace(T_KEYSET, "keyset: keyset %u data regen limit exceeded -- "
133 "forcing exchange", ks->seq); )
140 /* --- @dodecrypt@ --- *
142 * Arguments: @keyset *ks@ = pointer to keyset to use
143 * @buf *b@ = pointer to an input buffer
144 * @buf *bb@ = pointer to an output buffer
145 * @uint32 *seq@ = where to store the sequence number
147 * Returns: Zero if OK, nonzero if it failed.
149 * Use: Attempts to decrypt a message with the given key. No other
150 * checking (e.g., sequence number checks) is performed. We
151 * assume that the keyset is OK to use, and that there is
152 * sufficient output buffer space reserved. If the decryption
153 * is successful, the buffer pointer is moved past the decrypted
154 * packet, and the packet's sequence number is stored in @*seq@.
157 static int dodecrypt(keyset *ks, buf *b, buf *bb, uint32 *seq)
159 const octet *piv, *pseq, *ppk;
160 size_t psz = BLEFT(b);
164 gcipher *c = ks->cin;
165 size_t ivsz = c->ops->c->blksz;
169 /* --- Break up the packet into its components --- */
171 if (psz < ivsz + 4) {
172 T( trace(T_KEYSET, "keyset: block too small for keyset %u", ks->seq); )
176 piv = BCUR(b); pseq = piv + ivsz; ppk = pseq + 4;
178 /* --- Attempt to decrypt the packet --- */
180 c->ops->setiv(c, piv);
181 c->ops->decrypt(c, ppk, q, sz);
182 h = ks->min->ops->init(ks->min);
183 h->ops->hash(h, pseq, 4);
184 h->ops->hash(h, q, sz);
185 mac = h->ops->done(h, 0);
186 eq = !memcmp(mac, piv, ivsz);
187 IF_TRACING(T_KEYSET, {
188 trace(T_KEYSET, "keyset: decrypting using keyset %u", ks->seq);
189 trace_block(T_CRYPTO, "crypto: computed MAC", mac, ivsz);
193 IF_TRACING(T_KEYSET, {
194 trace(T_KEYSET, "keyset: decryption failed");
195 trace_block(T_CRYPTO, "crypto: expected MAC", piv, ivsz);
196 trace_block(T_CRYPTO, "crypto: invalid packet", q, sz);
202 IF_TRACING(T_KEYSET, {
203 trace(T_KEYSET, "keyset: decrypted OK (sequence = %lu)",
204 (unsigned long)LOAD32(pseq));
205 trace_block(T_CRYPTO, "crypto: decrypted packet", q, sz);
211 /* --- @dosequence@ --- *
213 * Arguments: @keyset *ks@ = pointer to a keyset
214 * @uint32 seq@ = a sequence number from a packet
216 * Returns: Zero if the sequence number is OK, nonzero if it's not.
218 * Use: Checks a sequence number. The data in the keyset which keeps
219 * track of valid sequence numbers is updated if the sequence
220 * number given is good. It's assumed that the sequence number
221 * has already been checked for authenticity.
224 static int dosequence(keyset *ks, uint32 seq)
229 if (seq < ks->iseq) {
230 a_warn("received packet has old sequence number (possible replay)");
233 if (seq >= ks->iseq + KS_SEQWINSZ) {
234 n = seq - (ks->iseq + KS_SEQWINSZ - 1);
241 seqbit = 1 << (seq - ks->iseq);
242 if (ks->iwin & seqbit) {
243 a_warn("received packet repeats old sequence number");
250 /*----- Operations on a single keyset -------------------------------------*/
252 /* --- @ks_drop@ --- *
254 * Arguments: @keyset *ks@ = pointer to a keyset
258 * Use: Decrements a keyset's reference counter. If the counter hits
259 * zero, the keyset is freed.
262 void ks_drop(keyset *ks)
266 ks->cin->ops->destroy(ks->cin);
267 ks->cout->ops->destroy(ks->cout);
268 ks->min->ops->destroy(ks->min);
269 ks->mout->ops->destroy(ks->mout);
273 /* --- @ks_gen@ --- *
275 * Arguments: @const void *k@ = pointer to key material
276 * @size_t x, y, z@ = offsets into key material (see below)
278 * Returns: A pointer to the new keyset.
280 * Use: Derives a new keyset from the given key material. The
281 * offsets @x@, @y@ and @z@ separate the key material into three
282 * parts. Between the @k@ and @k + x@ is `my' contribution to
283 * the key material; between @k + x@ and @k + y@ is `your'
284 * contribution; and between @k + y@ and @k + z@ is a shared
285 * value we made together. These are used to construct two
286 * pairs of symmetric keys. Each pair consists of an encryption
287 * key and a message authentication key. One pair is used for
288 * outgoing messages, the other for incoming messages.
290 * The new key is marked so that it won't be selected for output
291 * by @ksl_encrypt@. You can still encrypt data with it by
292 * calling @ks_encrypt@ directly.
295 keyset *ks_gen(const void *k, size_t x, size_t y, size_t z)
299 keyset *ks = CREATE(keyset);
300 time_t now = time(0);
302 T( static unsigned seq = 0; )
304 T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); )
306 /* --- Construct the various keys --- *
308 * This is done with macros, because it's quite tedious.
311 #define MINE HASH(&h, p, x)
312 #define YOURS HASH(&h, p + x, y - x)
313 #define OURS HASH(&h, p + y, z - y)
315 #define IN MINE; YOURS; OURS
316 #define OUT YOURS; MINE; OURS
317 #define STR_IN "incoming"
318 #define STR_OUT "outgoing"
320 #define GETHASH(str, dir) do { \
322 HASH_STRING(&h, "tripe-" str); \
324 HASH_DONE(&h, buf); \
325 IF_TRACING(T_KEYSET, { \
326 trace_block(T_CRYPTO, "crypto: " STR_##dir " key " str, \
331 GETHASH("encryption", IN); ks->cin = CIPHER->init(buf, sizeof(buf));
332 GETHASH("integrity", IN); ks->min = MAC->key(buf, sizeof(buf));
333 GETHASH("encryption", OUT); ks->cout = CIPHER->init(buf, sizeof(buf));
334 GETHASH("integrity", OUT); ks->mout = MAC->key(buf, sizeof(buf));
345 T( ks->seq = seq++; )
346 ks->t_exp = now + T_EXP;
348 ks->oseq = ks->iseq = 0;
356 /* --- @ks_tregen@ --- *
358 * Arguments: @keyset *ks@ = pointer to a keyset
360 * Returns: The time at which moves ought to be made to replace this key.
363 time_t ks_tregen(keyset *ks) { return (ks->t_exp - T_EXP + T_REGEN); }
365 /* --- @ks_activate@ --- *
367 * Arguments: @keyset *ks@ = pointer to a keyset
371 * Use: Activates a keyset, so that it can be used for encrypting
375 void ks_activate(keyset *ks)
377 if (ks->f & KSF_LISTEN) {
378 T( trace(T_KEYSET, "keyset: activating keyset %u", ks->seq); )
379 ks->f &= ~KSF_LISTEN;
383 /* --- @ks_encrypt@ --- *
385 * Arguments: @keyset *ks@ = pointer to a keyset
386 * @buf *b@ = pointer to input buffer
387 * @buf *bb@ = pointer to output buffer
389 * Returns: Zero if OK, nonzero if the key needs replacing. If the
390 * encryption failed, the output buffer is broken and zero is
393 * Use: Encrypts a block of data using the key. Note that the `key
394 * ought to be replaced' notification is only ever given once
395 * for each key. Also note that this call forces a keyset to be
396 * used even if it's marked as not for data output.
399 int ks_encrypt(keyset *ks, buf *b, buf *bb)
401 time_t now = time(0);
403 if (!KEYOK(ks, now)) {
407 return (doencrypt(ks, b, bb));
410 /* --- @ks_decrypt@ --- *
412 * Arguments: @keyset *ks@ = pointer to a keyset
413 * @buf *b@ = pointer to an input buffer
414 * @buf *bb@ = pointer to an output buffer
416 * Returns: Zero on success, or nonzero if there was some problem.
418 * Use: Attempts to decrypt a message using a given key. Note that
419 * requesting decryption with a key directly won't clear a
420 * marking that it's not for encryption.
423 int ks_decrypt(keyset *ks, buf *b, buf *bb)
425 time_t now = time(0);
428 if (!KEYOK(ks, now) ||
429 buf_ensure(bb, BLEN(b)) ||
430 dodecrypt(ks, b, bb, &seq) ||
436 /*----- Keyset list handling ----------------------------------------------*/
438 /* --- @ksl_free@ --- *
440 * Arguments: @keyset **ksroot@ = pointer to keyset list head
444 * Use: Frees (releases references to) all of the keys in a keyset.
447 void ksl_free(keyset **ksroot)
450 for (ks = *ksroot; ks; ks = ksn) {
457 /* --- @ksl_link@ --- *
459 * Arguments: @keyset **ksroot@ = pointer to keyset list head
460 * @keyset *ks@ = pointer to a keyset
464 * Use: Links a keyset into a list. A keyset can only be on one list
465 * at a time. Bad things happen otherwise.
468 void ksl_link(keyset **ksroot, keyset *ks)
470 assert(!(ks->f & KSF_LINK));
477 /* --- @ksl_prune@ --- *
479 * Arguments: @keyset **ksroot@ = pointer to keyset list head
483 * Use: Prunes the keyset list by removing keys which mustn't be used
487 void ksl_prune(keyset **ksroot)
489 time_t now = time(0);
492 keyset *ks = *ksroot;
494 if (ks->t_exp <= now) {
495 T( trace(T_KEYSET, "keyset: expiring keyset %u (time limit reached)",
498 } else if (ks->sz_exp == 0) {
499 T( trace(T_KEYSET, "keyset: expiring keyset %u (data limit reached)",
514 /* --- @ksl_encrypt@ --- *
516 * Arguments: @keyset **ksroot@ = pointer to keyset list head
517 * @buf *b@ = pointer to input buffer
518 * @buf *bb@ = pointer to output buffer
520 * Returns: Nonzero if a new key is needed.
522 * Use: Encrypts a packet.
525 int ksl_encrypt(keyset **ksroot, buf *b, buf *bb)
527 time_t now = time(0);
528 keyset *ks = *ksroot;
532 T( trace(T_KEYSET, "keyset: no suitable keysets found"); )
536 if (KEYOK(ks, now) && !(ks->f & KSF_LISTEN))
541 return (doencrypt(ks, b, bb));
544 /* --- @ksl_decrypt@ --- *
546 * Arguments: @keyset **ksroot@ = pointer to keyset list head
547 * @buf *b@ = pointer to input buffer
548 * @buf *bb@ = pointer to output buffer
550 * Returns: Nonzero if the packet couldn't be decrypted.
552 * Use: Decrypts a packet.
555 int ksl_decrypt(keyset **ksroot, buf *b, buf *bb)
557 time_t now = time(0);
561 if (buf_ensure(bb, BLEN(b)))
564 for (ks = *ksroot; ks; ks = ks->next) {
567 if (!dodecrypt(ks, b, bb, &seq)) {
568 if (ks->f & KSF_LISTEN) {
569 T( trace(T_KEYSET, "keyset: implicitly activating keyset %u",
571 ks->f &= ~KSF_LISTEN;
573 return (dosequence(ks, seq));
576 T( trace(T_KEYSET, "keyset: no matching keys"); )
580 /*----- That's all, folks -------------------------------------------------*/