3 * $Id: keyset.c,v 1.6 2003/04/06 10:26:35 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.6 2003/04/06 10:26:35 mdw
33 * Report peer name on decrypt errors.
35 * Revision 1.5 2001/06/19 22:07:43 mdw
36 * Change the encrypted packet format to be non-malleable.
38 * Revision 1.4 2001/06/16 14:06:40 mdw
39 * Quantify collision probabilities for the stated data volume bounds.
41 * Revision 1.3 2001/02/16 21:39:55 mdw
42 * Major overhaul. Separate functions for manipulating keysets from
43 * functions for manipulating keyset lists. Introduce a concept of
44 * listening-only keys.
46 * Revision 1.2 2001/02/05 19:53:23 mdw
47 * Add sequence number protection.
49 * Revision 1.1 2001/02/03 20:26:37 mdw
54 /*----- Header files ------------------------------------------------------*/
58 /*----- Tunable parameters ------------------------------------------------*/
60 /* --- Note on size limits --- *
62 * For a 64-bit block cipher (e.g., Blowfish), the probability of a collision
63 * occurring after 32 MB is less than %$2^{-21}$%, and the probability of a
64 * collision occurring after 64 MB is less than %$2^{-19}$%.
67 #define T_EXP MIN(60) /* Expiry time for a key */
68 #define T_REGEN MIN(45) /* Regeneration time for a key */
69 #define SZ_EXP MEG(64) /* Expiry data size for a key */
70 #define SZ_REGEN MEG(32) /* Data size threshold for regen */
72 /*----- Handy macros ------------------------------------------------------*/
74 #define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now)
76 /*----- Low-level packet encryption and decryption ------------------------*/
78 /* --- Encrypted data format --- *
80 * Let %$p_i$% be the %$i$%-th plaintext message. We first compute
82 * %$c_i = \mathcal{E}\textrm{-CBC}_{K_{\text{E}}}(p_i)$%
84 * as the CBC-ciphertext of %$p_i$%, and then
86 * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(i, c_i)$%
88 * as a MAC on the %%\emph{ciphertext}%%. The message sent is then the pair
89 * %$(\sigma_i, c_i)$%. This construction is provably secure in the NM-CCA
90 * sense (assuming that the cipher is IND-CPA, and the MAC is SUF-CMA)
91 * [Bellare and Namprempre].
93 * This also ensures that, assuming the key is good, we have a secure channel
94 * [Krawczyk]. Actually, [Krawczyk] shows that, if the cipher is either a
95 * simple stream cipher or a block cipher in CBC mode, we can use the MAC-
96 * then-encrypt scheme and still have a secure channel. However, I like the
97 * NM-CCA guarantee from [Bellare and Namprempre]. I'm less worried about
98 * the Horton Principle [Wagner and Schneier].
101 /* --- @doencrypt@ --- *
103 * Arguments: @keyset *ks@ = pointer to keyset to use
104 * @buf *b@ = pointer to an input buffer
105 * @buf *bb@ = pointer to an output buffer
107 * Returns: Zero if OK, nonzero if a new key is required.
109 * Use: Encrypts a message with the given key. We assume that the
110 * keyset is OK to use.
113 static int doencrypt(keyset
*ks
, buf
*b
, buf
*bb
)
117 const octet
*p
= BCUR(b
);
118 size_t sz
= BLEFT(b
);
119 octet
*qmac
, *qseq
, *qiv
, *qpk
;
124 /* --- Allocate the required buffer space --- */
127 if (buf_ensure(bb
, MACSZ
+ SEQSZ
+ IVSZ
+ sz
))
128 return (0); /* Caution! */
129 qmac
= BCUR(bb
); qseq
= qmac
+ MACSZ
; qiv
= qseq
+ SEQSZ
; qpk
= qiv
+ IVSZ
;
130 BSTEP(bb
, MACSZ
+ SEQSZ
+ IVSZ
+ sz
);
132 /* --- Encrypt the packet --- */
134 oseq
= ks
->oseq
++; STORE32(qseq
, oseq
);
135 rand_get(RAND_GLOBAL
, qiv
, IVSZ
);
136 c
->ops
->setiv(c
, qiv
);
137 c
->ops
->encrypt(c
, p
, qpk
, sz
);
138 IF_TRACING(T_KEYSET
, {
139 trace(T_KEYSET
, "keyset: encrypting packet %lu using keyset %u",
140 (unsigned long)oseq
, ks
->seq
);
141 trace_block(T_CRYPTO
, "crypto: encrypted packet", qpk
, sz
);
144 /* --- Now compute the MAC --- */
146 h
= ks
->mout
->ops
->init(ks
->mout
);
147 h
->ops
->hash(h
, qseq
, SEQSZ
+ IVSZ
+ sz
);
148 memcpy(qmac
, h
->ops
->done(h
, 0), MACSZ
);
150 IF_TRACING(T_KEYSET
, {
151 trace_block(T_CRYPTO
, "crypto: computed MAC", qmac
, MACSZ
);
154 /* --- Deduct the packet size from the key's data life --- */
161 if (osz
>= SZ_REGEN
&& nsz
< SZ_REGEN
) {
162 T( trace(T_KEYSET
, "keyset: keyset %u data regen limit exceeded -- "
163 "forcing exchange", ks
->seq
); )
170 /* --- @dodecrypt@ --- *
172 * Arguments: @keyset *ks@ = pointer to keyset to use
173 * @buf *b@ = pointer to an input buffer
174 * @buf *bb@ = pointer to an output buffer
175 * @uint32 *seq@ = where to store the sequence number
177 * Returns: Zero if OK, nonzero if it failed.
179 * Use: Attempts to decrypt a message with the given key. No other
180 * checking (e.g., sequence number checks) is performed. We
181 * assume that the keyset is OK to use, and that there is
182 * sufficient output buffer space reserved. If the decryption
183 * is successful, the buffer pointer is moved past the decrypted
184 * packet, and the packet's sequence number is stored in @*seq@.
187 static int dodecrypt(keyset
*ks
, buf
*b
, buf
*bb
, uint32
*seq
)
189 const octet
*pmac
, *piv
, *pseq
, *ppk
;
190 size_t psz
= BLEFT(b
);
194 gcipher
*c
= ks
->cin
;
195 size_t ivsz
= c
->ops
->c
->blksz
;
199 /* --- Break up the packet into its components --- */
201 if (psz
< ivsz
+ 4) {
202 T( trace(T_KEYSET
, "keyset: block too small for keyset %u", ks
->seq
); )
205 sz
= psz
- IVSZ
- SEQSZ
- MACSZ
;
206 pmac
= BCUR(b
); pseq
= pmac
+ MACSZ
; piv
= pseq
+ SEQSZ
; ppk
= piv
+ IVSZ
;
208 /* --- Verify the MAC on the packet --- */
210 h
= ks
->min
->ops
->init(ks
->min
);
211 h
->ops
->hash(h
, pseq
, SEQSZ
+ IVSZ
+ sz
);
212 mac
= h
->ops
->done(h
, 0);
213 eq
= !memcmp(mac
, pmac
, MACSZ
);
214 IF_TRACING(T_KEYSET
, {
215 trace(T_KEYSET
, "keyset: decrypting using keyset %u", ks
->seq
);
216 trace_block(T_CRYPTO
, "crypto: computed MAC", mac
, MACSZ
);
220 a_warn("incorrect MAC on packet from `%s'", p_name(ks
->p
));
221 IF_TRACING(T_KEYSET
, {
222 trace(T_KEYSET
, "keyset: decryption failed");
223 trace_block(T_CRYPTO
, "crypto: expected MAC", pmac
, MACSZ
);
228 /* --- Decrypt the packet --- */
230 c
->ops
->setiv(c
, piv
);
231 c
->ops
->decrypt(c
, ppk
, q
, sz
);
234 IF_TRACING(T_KEYSET
, {
235 trace(T_KEYSET
, "keyset: decrypted OK (sequence = %lu)",
236 (unsigned long)LOAD32(pseq
));
237 trace_block(T_CRYPTO
, "crypto: decrypted packet", q
, sz
);
243 /* --- @dosequence@ --- *
245 * Arguments: @keyset *ks@ = pointer to a keyset
246 * @uint32 seq@ = a sequence number from a packet
248 * Returns: Zero if the sequence number is OK, nonzero if it's not.
250 * Use: Checks a sequence number. The data in the keyset which keeps
251 * track of valid sequence numbers is updated if the sequence
252 * number given is good. It's assumed that the sequence number
253 * has already been checked for authenticity.
256 static int dosequence(keyset
*ks
, uint32 seq
)
261 if (seq
< ks
->iseq
) {
262 a_warn("received packet has old sequence number (possible replay)");
265 if (seq
>= ks
->iseq
+ KS_SEQWINSZ
) {
266 n
= seq
- (ks
->iseq
+ KS_SEQWINSZ
- 1);
273 seqbit
= 1 << (seq
- ks
->iseq
);
274 if (ks
->iwin
& seqbit
) {
275 a_warn("received packet repeats old sequence number");
282 /*----- Operations on a single keyset -------------------------------------*/
284 /* --- @ks_drop@ --- *
286 * Arguments: @keyset *ks@ = pointer to a keyset
290 * Use: Decrements a keyset's reference counter. If the counter hits
291 * zero, the keyset is freed.
294 void ks_drop(keyset
*ks
)
298 ks
->cin
->ops
->destroy(ks
->cin
);
299 ks
->cout
->ops
->destroy(ks
->cout
);
300 ks
->min
->ops
->destroy(ks
->min
);
301 ks
->mout
->ops
->destroy(ks
->mout
);
305 /* --- @ks_gen@ --- *
307 * Arguments: @const void *k@ = pointer to key material
308 * @size_t x, y, z@ = offsets into key material (see below)
309 * @peer *p@ = pointer to peer information
311 * Returns: A pointer to the new keyset.
313 * Use: Derives a new keyset from the given key material. The
314 * offsets @x@, @y@ and @z@ separate the key material into three
315 * parts. Between the @k@ and @k + x@ is `my' contribution to
316 * the key material; between @k + x@ and @k + y@ is `your'
317 * contribution; and between @k + y@ and @k + z@ is a shared
318 * value we made together. These are used to construct two
319 * pairs of symmetric keys. Each pair consists of an encryption
320 * key and a message authentication key. One pair is used for
321 * outgoing messages, the other for incoming messages.
323 * The new key is marked so that it won't be selected for output
324 * by @ksl_encrypt@. You can still encrypt data with it by
325 * calling @ks_encrypt@ directly.
328 keyset
*ks_gen(const void *k
, size_t x
, size_t y
, size_t z
, peer
*p
)
332 keyset
*ks
= CREATE(keyset
);
333 time_t now
= time(0);
335 T( static unsigned seq
= 0; )
337 T( trace(T_KEYSET
, "keyset: adding new keyset %u", seq
); )
339 /* --- Construct the various keys --- *
341 * This is done with macros, because it's quite tedious.
344 #define MINE HASH(&h, pp, x)
345 #define YOURS HASH(&h, pp + x, y - x)
346 #define OURS HASH(&h, pp + y, z - y)
348 #define IN MINE; YOURS; OURS
349 #define OUT YOURS; MINE; OURS
350 #define STR_IN "incoming"
351 #define STR_OUT "outgoing"
353 #define GETHASH(str, dir) do { \
355 HASH_STRING(&h, "tripe-" str); \
357 HASH_DONE(&h, buf); \
358 IF_TRACING(T_KEYSET, { \
359 trace_block(T_CRYPTO, "crypto: " STR_##dir " key " str, \
364 GETHASH("encryption", IN
); ks
->cin
= CIPHER
->init(buf
, sizeof(buf
));
365 GETHASH("integrity", IN
); ks
->min
= MAC
->key(buf
, sizeof(buf
));
366 GETHASH("encryption", OUT
); ks
->cout
= CIPHER
->init(buf
, sizeof(buf
));
367 GETHASH("integrity", OUT
); ks
->mout
= MAC
->key(buf
, sizeof(buf
));
378 T( ks
->seq
= seq
++; )
379 ks
->t_exp
= now
+ T_EXP
;
381 ks
->oseq
= ks
->iseq
= 0;
390 /* --- @ks_tregen@ --- *
392 * Arguments: @keyset *ks@ = pointer to a keyset
394 * Returns: The time at which moves ought to be made to replace this key.
397 time_t ks_tregen(keyset
*ks
) { return (ks
->t_exp
- T_EXP
+ T_REGEN
); }
399 /* --- @ks_activate@ --- *
401 * Arguments: @keyset *ks@ = pointer to a keyset
405 * Use: Activates a keyset, so that it can be used for encrypting
409 void ks_activate(keyset
*ks
)
411 if (ks
->f
& KSF_LISTEN
) {
412 T( trace(T_KEYSET
, "keyset: activating keyset %u", ks
->seq
); )
413 ks
->f
&= ~KSF_LISTEN
;
417 /* --- @ks_encrypt@ --- *
419 * Arguments: @keyset *ks@ = pointer to a keyset
420 * @buf *b@ = pointer to input buffer
421 * @buf *bb@ = pointer to output buffer
423 * Returns: Zero if OK, nonzero if the key needs replacing. If the
424 * encryption failed, the output buffer is broken and zero is
427 * Use: Encrypts a block of data using the key. Note that the `key
428 * ought to be replaced' notification is only ever given once
429 * for each key. Also note that this call forces a keyset to be
430 * used even if it's marked as not for data output.
433 int ks_encrypt(keyset
*ks
, buf
*b
, buf
*bb
)
435 time_t now
= time(0);
437 if (!KEYOK(ks
, now
)) {
441 return (doencrypt(ks
, b
, bb
));
444 /* --- @ks_decrypt@ --- *
446 * Arguments: @keyset *ks@ = pointer to a keyset
447 * @buf *b@ = pointer to an input buffer
448 * @buf *bb@ = pointer to an output buffer
450 * Returns: Zero on success, or nonzero if there was some problem.
452 * Use: Attempts to decrypt a message using a given key. Note that
453 * requesting decryption with a key directly won't clear a
454 * marking that it's not for encryption.
457 int ks_decrypt(keyset
*ks
, buf
*b
, buf
*bb
)
459 time_t now
= time(0);
462 if (!KEYOK(ks
, now
) ||
463 buf_ensure(bb
, BLEN(b
)) ||
464 dodecrypt(ks
, b
, bb
, &seq
) ||
470 /*----- Keyset list handling ----------------------------------------------*/
472 /* --- @ksl_free@ --- *
474 * Arguments: @keyset **ksroot@ = pointer to keyset list head
478 * Use: Frees (releases references to) all of the keys in a keyset.
481 void ksl_free(keyset
**ksroot
)
484 for (ks
= *ksroot
; ks
; ks
= ksn
) {
491 /* --- @ksl_link@ --- *
493 * Arguments: @keyset **ksroot@ = pointer to keyset list head
494 * @keyset *ks@ = pointer to a keyset
498 * Use: Links a keyset into a list. A keyset can only be on one list
499 * at a time. Bad things happen otherwise.
502 void ksl_link(keyset
**ksroot
, keyset
*ks
)
504 assert(!(ks
->f
& KSF_LINK
));
511 /* --- @ksl_prune@ --- *
513 * Arguments: @keyset **ksroot@ = pointer to keyset list head
517 * Use: Prunes the keyset list by removing keys which mustn't be used
521 void ksl_prune(keyset
**ksroot
)
523 time_t now
= time(0);
526 keyset
*ks
= *ksroot
;
528 if (ks
->t_exp
<= now
) {
529 T( trace(T_KEYSET
, "keyset: expiring keyset %u (time limit reached)",
532 } else if (ks
->sz_exp
== 0) {
533 T( trace(T_KEYSET
, "keyset: expiring keyset %u (data limit reached)",
548 /* --- @ksl_encrypt@ --- *
550 * Arguments: @keyset **ksroot@ = pointer to keyset list head
551 * @buf *b@ = pointer to input buffer
552 * @buf *bb@ = pointer to output buffer
554 * Returns: Nonzero if a new key is needed.
556 * Use: Encrypts a packet.
559 int ksl_encrypt(keyset
**ksroot
, buf
*b
, buf
*bb
)
561 time_t now
= time(0);
562 keyset
*ks
= *ksroot
;
566 T( trace(T_KEYSET
, "keyset: no suitable keysets found"); )
570 if (KEYOK(ks
, now
) && !(ks
->f
& KSF_LISTEN
))
575 return (doencrypt(ks
, b
, bb
));
578 /* --- @ksl_decrypt@ --- *
580 * Arguments: @keyset **ksroot@ = pointer to keyset list head
581 * @buf *b@ = pointer to input buffer
582 * @buf *bb@ = pointer to output buffer
584 * Returns: Nonzero if the packet couldn't be decrypted.
586 * Use: Decrypts a packet.
589 int ksl_decrypt(keyset
**ksroot
, buf
*b
, buf
*bb
)
591 time_t now
= time(0);
595 if (buf_ensure(bb
, BLEN(b
)))
598 for (ks
= *ksroot
; ks
; ks
= ks
->next
) {
601 if (!dodecrypt(ks
, b
, bb
, &seq
)) {
602 if (ks
->f
& KSF_LISTEN
) {
603 T( trace(T_KEYSET
, "keyset: implicitly activating keyset %u",
605 ks
->f
&= ~KSF_LISTEN
;
607 return (dosequence(ks
, seq
));
610 T( trace(T_KEYSET
, "keyset: no matching keys"); )
614 /*----- That's all, folks -------------------------------------------------*/