3 * Handling of symmetric keysets
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Tunable parameters ------------------------------------------------*/
33 /* --- Note on size limits --- *
35 * For a 64-bit block cipher (e.g., Blowfish), the probability of a collision
36 * occurring after 32 MB is less than %$2^{-21}$%, and the probability of a
37 * collision occurring after 64 MB is less than %$2^{-19}$%. These could be
38 * adjusted dependent on the encryption scheme, but it's too much pain.
41 #define T_EXP MIN(60) /* Expiry time for a key */
42 #define T_REGEN MIN(45) /* Regeneration time for a key */
43 #define SZ_EXP MEG(64) /* Expiry data size for a key */
44 #define SZ_REGEN MEG(32) /* Data size threshold for regen */
46 /*----- Handy macros ------------------------------------------------------*/
48 #define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now)
50 #define SEQSZ 4 /* Size of sequence number packet */
52 /*----- Low-level packet encryption and decryption ------------------------*/
54 /* --- Encrypted data format --- *
56 * Let %$p_i$% be the %$i$%-th plaintext message, with type %$t$%. We first
59 * %$c_i = \mathcal{E}\textrm{-CBC}_{K_{\text{E}}}(p_i)$%
61 * as the CBC-ciphertext of %$p_i$%, and then
63 * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(t, i, c_i)$%
65 * as a MAC on the %%\emph{ciphertext}%%. The message sent is then the pair
66 * %$(\sigma_i, c_i)$%. This construction is provably secure in the NM-CCA
67 * sense (assuming that the cipher is IND-CPA, and the MAC is SUF-CMA)
68 * [Bellare and Namprempre].
70 * This also ensures that, assuming the key is good, we have a secure channel
71 * [Krawczyk]. Actually, [Krawczyk] shows that, if the cipher is either a
72 * simple stream cipher or a block cipher in CBC mode, we can use the MAC-
73 * then-encrypt scheme and still have a secure channel. However, I like the
74 * NM-CCA guarantee from [Bellare and Namprempre]. I'm less worried about
75 * the Horton Principle [Wagner and Schneier].
78 /* --- @doencrypt@ --- *
80 * Arguments: @keyset *ks@ = pointer to keyset to use
81 * @unsigned ty@ = type of message this is
82 * @buf *b@ = pointer to an input buffer
83 * @buf *bb@ = pointer to an output buffer
85 * Returns: Zero if OK, nonzero if a new key is required.
87 * Use: Encrypts a message with the given key. We assume that the
88 * keyset is OK to use.
91 static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
94 gcipher *c = ks->cout;
95 const octet *p = BCUR(b);
97 octet *qmac, *qseq, *qiv, *qpk;
99 size_t ivsz = GC_CLASS(c)->blksz;
100 size_t tagsz = ks->tagsz;
105 /* --- Allocate the required buffer space --- */
107 if (buf_ensure(bb, tagsz + SEQSZ + ivsz + sz))
108 return (0); /* Caution! */
109 qmac = BCUR(bb); qseq = qmac + tagsz; qiv = qseq + SEQSZ; qpk = qiv + ivsz;
110 BSTEP(bb, tagsz + SEQSZ + ivsz + sz);
113 oseq = ks->oseq++; STORE32(qseq, oseq);
114 IF_TRACING(T_KEYSET, {
115 trace(T_KEYSET, "keyset: encrypting packet %lu using keyset %u",
116 (unsigned long)oseq, ks->seq);
117 trace_block(T_CRYPTO, "crypto: plaintext packet", p, sz);
120 /* --- Encrypt the packet --- */
123 rand_get(RAND_GLOBAL, qiv, ivsz);
125 IF_TRACING(T_KEYSET, {
126 trace_block(T_CRYPTO, "crypto: initialization vector", qiv, ivsz);
129 GC_ENCRYPT(c, p, qpk, sz);
130 IF_TRACING(T_KEYSET, {
131 trace_block(T_CRYPTO, "crypto: encrypted packet", qpk, sz);
134 /* --- Now compute the MAC --- */
137 h = GM_INIT(ks->mout);
138 GH_HASH(h, t, sizeof(t));
139 GH_HASH(h, qseq, SEQSZ + ivsz + sz);
140 memcpy(qmac, GH_DONE(h, 0), tagsz);
142 IF_TRACING(T_KEYSET, {
143 trace_block(T_CRYPTO, "crypto: computed MAC", qmac, tagsz);
147 /* --- Deduct the packet size from the key's data life --- */
154 if (osz >= SZ_REGEN && nsz < SZ_REGEN) {
155 T( trace(T_KEYSET, "keyset: keyset %u data regen limit exceeded -- "
156 "forcing exchange", ks->seq); )
163 /* --- @dodecrypt@ --- *
165 * Arguments: @keyset *ks@ = pointer to keyset to use
166 * @unsigned ty@ = expected type code
167 * @buf *b@ = pointer to an input buffer
168 * @buf *bb@ = pointer to an output buffer
169 * @uint32 *seq@ = where to store the sequence number
171 * Returns: Zero if OK, nonzero if it failed.
173 * Use: Attempts to decrypt a message with the given key. No other
174 * checking (e.g., sequence number checks) is performed. We
175 * assume that the keyset is OK to use, and that there is
176 * sufficient output buffer space reserved. If the decryption
177 * is successful, the buffer pointer is moved past the decrypted
178 * packet, and the packet's sequence number is stored in @*seq@.
181 static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq)
183 const octet *pmac, *piv, *pseq, *ppk;
184 size_t psz = BLEFT(b);
188 gcipher *c = ks->cin;
189 size_t ivsz = GC_CLASS(c)->blksz;
190 size_t tagsz = ks->tagsz;
195 /* --- Break up the packet into its components --- */
197 if (psz < ivsz + SEQSZ + tagsz) {
198 T( trace(T_KEYSET, "keyset: block too small for keyset %u", ks->seq); )
201 sz = psz - ivsz - SEQSZ - tagsz;
202 pmac = BCUR(b); pseq = pmac + tagsz; piv = pseq + SEQSZ; ppk = piv + ivsz;
205 IF_TRACING(T_KEYSET, {
206 trace(T_KEYSET, "keyset: decrypting using keyset %u", ks->seq);
207 trace_block(T_CRYPTO, "crypto: ciphertext packet", ppk, sz);
210 /* --- Verify the MAC on the packet --- */
213 h = GM_INIT(ks->min);
214 GH_HASH(h, t, sizeof(t));
215 GH_HASH(h, pseq, SEQSZ + ivsz + sz);
217 eq = !memcmp(mac, pmac, tagsz);
218 IF_TRACING(T_KEYSET, {
219 trace_block(T_CRYPTO, "crypto: computed MAC", mac, tagsz);
223 IF_TRACING(T_KEYSET, {
224 trace(T_KEYSET, "keyset: incorrect MAC: decryption failed");
225 trace_block(T_CRYPTO, "crypto: expected MAC", pmac, tagsz);
231 /* --- Decrypt the packet --- */
235 IF_TRACING(T_KEYSET, {
236 trace_block(T_CRYPTO, "crypto: initialization vector", piv, ivsz);
239 GC_DECRYPT(c, ppk, q, sz);
242 IF_TRACING(T_KEYSET, {
243 trace(T_KEYSET, "keyset: decrypted OK (sequence = %lu)",
244 (unsigned long)LOAD32(pseq));
245 trace_block(T_CRYPTO, "crypto: decrypted packet", q, sz);
251 /*----- Operations on a single keyset -------------------------------------*/
253 /* --- @ks_drop@ --- *
255 * Arguments: @keyset *ks@ = pointer to a keyset
259 * Use: Decrements a keyset's reference counter. If the counter hits
260 * zero, the keyset is freed.
263 void ks_drop(keyset *ks)
268 GC_DESTROY(ks->cout);
270 GM_DESTROY(ks->mout);
274 /* --- @ks_gen@ --- *
276 * Arguments: @const void *k@ = pointer to key material
277 * @size_t x, y, z@ = offsets into key material (see below)
278 * @peer *p@ = pointer to peer information
280 * Returns: A pointer to the new keyset.
282 * Use: Derives a new keyset from the given key material. The
283 * offsets @x@, @y@ and @z@ separate the key material into three
284 * parts. Between the @k@ and @k + x@ is `my' contribution to
285 * the key material; between @k + x@ and @k + y@ is `your'
286 * contribution; and between @k + y@ and @k + z@ is a shared
287 * value we made together. These are used to construct two
288 * pairs of symmetric keys. Each pair consists of an encryption
289 * key and a message authentication key. One pair is used for
290 * outgoing messages, the other for incoming messages.
292 * The new key is marked so that it won't be selected for output
293 * by @ksl_encrypt@. You can still encrypt data with it by
294 * calling @ks_encrypt@ directly.
297 keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p)
301 keyset *ks = CREATE(keyset);
302 time_t now = time(0);
304 T( static unsigned seq = 0; )
306 T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); )
308 /* --- Construct the various keys --- *
310 * This is done with macros, because it's quite tedious.
313 #define MINE GH_HASH(h, pp, x)
314 #define YOURS GH_HASH(h, pp + x, y - x)
315 #define OURS GH_HASH(h, pp + y, z - y)
317 #define HASH_in MINE; YOURS; OURS
318 #define HASH_out YOURS; MINE; OURS
319 #define INIT_c(k) GC_INIT(algs.c, (k), algs.cksz)
320 #define INIT_m(k) GM_KEY(algs.m, (k), algs.mksz)
321 #define STR_c "encryption"
322 #define STR_m "integrity"
323 #define STR_in "incoming"
324 #define STR_out "outgoing"
326 #define SETKEY(a, dir) do { \
327 h = GH_INIT(algs.h); \
328 HASH_STRING(h, "tripe-" STR_##a); \
330 hh = GH_DONE(h, 0); \
331 IF_TRACING(T_KEYSET, { \
332 trace_block(T_CRYPTO, "crypto: " STR_##dir " key " STR_##a, \
335 ks->a##dir = INIT_##a(hh); \
339 SETKEY(c, in); SETKEY(c, out);
340 SETKEY(m, in); SETKEY(m, out);
355 T( ks->seq = seq++; )
357 ks->t_exp = now + T_EXP;
360 seq_reset(&ks->iseq);
364 ks->tagsz = algs.tagsz;
368 /* --- @ks_tregen@ --- *
370 * Arguments: @keyset *ks@ = pointer to a keyset
372 * Returns: The time at which moves ought to be made to replace this key.
375 time_t ks_tregen(keyset *ks) { return (ks->t_exp - T_EXP + T_REGEN); }
377 /* --- @ks_activate@ --- *
379 * Arguments: @keyset *ks@ = pointer to a keyset
383 * Use: Activates a keyset, so that it can be used for encrypting
387 void ks_activate(keyset *ks)
389 if (ks->f & KSF_LISTEN) {
390 T( trace(T_KEYSET, "keyset: activating keyset %u", ks->seq); )
391 ks->f &= ~KSF_LISTEN;
395 /* --- @ks_encrypt@ --- *
397 * Arguments: @keyset *ks@ = pointer to a keyset
398 * @unsigned ty@ = message type
399 * @buf *b@ = pointer to input buffer
400 * @buf *bb@ = pointer to output buffer
402 * Returns: Zero if OK, nonzero if the key needs replacing. If the
403 * encryption failed, the output buffer is broken and zero is
406 * Use: Encrypts a block of data using the key. Note that the `key
407 * ought to be replaced' notification is only ever given once
408 * for each key. Also note that this call forces a keyset to be
409 * used even if it's marked as not for data output.
412 int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
414 time_t now = time(0);
416 if (!KEYOK(ks, now)) {
420 return (doencrypt(ks, ty, b, bb));
423 /* --- @ks_decrypt@ --- *
425 * Arguments: @keyset *ks@ = pointer to a keyset
426 * @unsigned ty@ = expected type code
427 * @buf *b@ = pointer to an input buffer
428 * @buf *bb@ = pointer to an output buffer
430 * Returns: Zero on success, or nonzero if there was some problem.
432 * Use: Attempts to decrypt a message using a given key. Note that
433 * requesting decryption with a key directly won't clear a
434 * marking that it's not for encryption.
437 int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
439 time_t now = time(0);
442 if (!KEYOK(ks, now) ||
443 buf_ensure(bb, BLEN(b)) ||
444 dodecrypt(ks, ty, b, bb, &seq) ||
445 seq_check(&ks->iseq, seq, "SYMM"))
450 /*----- Keyset list handling ----------------------------------------------*/
452 /* --- @ksl_free@ --- *
454 * Arguments: @keyset **ksroot@ = pointer to keyset list head
458 * Use: Frees (releases references to) all of the keys in a keyset.
461 void ksl_free(keyset **ksroot)
464 for (ks = *ksroot; ks; ks = ksn) {
471 /* --- @ksl_link@ --- *
473 * Arguments: @keyset **ksroot@ = pointer to keyset list head
474 * @keyset *ks@ = pointer to a keyset
478 * Use: Links a keyset into a list. A keyset can only be on one list
479 * at a time. Bad things happen otherwise.
482 void ksl_link(keyset **ksroot, keyset *ks)
484 assert(!(ks->f & KSF_LINK));
491 /* --- @ksl_prune@ --- *
493 * Arguments: @keyset **ksroot@ = pointer to keyset list head
497 * Use: Prunes the keyset list by removing keys which mustn't be used
501 void ksl_prune(keyset **ksroot)
503 time_t now = time(0);
506 keyset *ks = *ksroot;
508 if (ks->t_exp <= now) {
509 T( trace(T_KEYSET, "keyset: expiring keyset %u (time limit reached)",
512 } else if (ks->sz_exp == 0) {
513 T( trace(T_KEYSET, "keyset: expiring keyset %u (data limit reached)",
528 /* --- @ksl_encrypt@ --- *
530 * Arguments: @keyset **ksroot@ = pointer to keyset list head
531 * @unsigned ty@ = message type
532 * @buf *b@ = pointer to input buffer
533 * @buf *bb@ = pointer to output buffer
535 * Returns: Nonzero if a new key is needed.
537 * Use: Encrypts a packet.
540 int ksl_encrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb)
542 time_t now = time(0);
543 keyset *ks = *ksroot;
547 T( trace(T_KEYSET, "keyset: no suitable keysets found"); )
551 if (KEYOK(ks, now) && !(ks->f & KSF_LISTEN))
556 return (doencrypt(ks, ty, b, bb));
559 /* --- @ksl_decrypt@ --- *
561 * Arguments: @keyset **ksroot@ = pointer to keyset list head
562 * @unsigned ty@ = expected type code
563 * @buf *b@ = pointer to input buffer
564 * @buf *bb@ = pointer to output buffer
566 * Returns: Nonzero if the packet couldn't be decrypted.
568 * Use: Decrypts a packet.
571 int ksl_decrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb)
573 time_t now = time(0);
577 if (buf_ensure(bb, BLEN(b)))
580 for (ks = *ksroot; ks; ks = ks->next) {
583 if (!dodecrypt(ks, ty, b, bb, &seq)) {
584 if (ks->f & KSF_LISTEN) {
585 T( trace(T_KEYSET, "keyset: implicitly activating keyset %u",
587 ks->f &= ~KSF_LISTEN;
589 return (seq_check(&ks->iseq, seq, "SYMM"));
592 T( trace(T_KEYSET, "keyset: no matching keys, or incorrect MAC"); )
596 /*----- That's all, folks -------------------------------------------------*/