/* -*-c-*-
*
- * $Id: keyset.c,v 1.5 2001/06/19 22:07:43 mdw Exp $
+ * $Id$
*
* Handling of symmetric keysets
*
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: keyset.c,v $
- * Revision 1.5 2001/06/19 22:07:43 mdw
- * Change the encrypted packet format to be non-malleable.
- *
- * Revision 1.4 2001/06/16 14:06:40 mdw
- * Quantify collision probabilities for the stated data volume bounds.
- *
- * Revision 1.3 2001/02/16 21:39:55 mdw
- * Major overhaul. Separate functions for manipulating keysets from
- * functions for manipulating keyset lists. Introduce a concept of
- * listening-only keys.
- *
- * Revision 1.2 2001/02/05 19:53:23 mdw
- * Add sequence number protection.
- *
- * Revision 1.1 2001/02/03 20:26:37 mdw
- * Initial checkin.
- *
- */
-
/*----- Header files ------------------------------------------------------*/
#include "tripe.h"
*
* For a 64-bit block cipher (e.g., Blowfish), the probability of a collision
* occurring after 32 MB is less than %$2^{-21}$%, and the probability of a
- * collision occurring after 64 MB is less than %$2^{-19}$%.
+ * collision occurring after 64 MB is less than %$2^{-19}$%. These could be
+ * adjusted dependent on the encryption scheme, but it's too much pain.
*/
#define T_EXP MIN(60) /* Expiry time for a key */
#define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now)
+#define SEQSZ 4 /* Size of sequence number packet */
+
/*----- Low-level packet encryption and decryption ------------------------*/
/* --- Encrypted data format --- *
*
- * Let %$p_i$% be the %$i$%-th plaintext message. We first compute
+ * Let %$p_i$% be the %$i$%-th plaintext message, with type %$t$%. We first
+ * compute
*
* %$c_i = \mathcal{E}\textrm{-CBC}_{K_{\text{E}}}(p_i)$%
*
* as the CBC-ciphertext of %$p_i$%, and then
*
- * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(i, c_i)$%
+ * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(t, i, c_i)$%
*
* as a MAC on the %%\emph{ciphertext}%%. The message sent is then the pair
* %$(\sigma_i, c_i)$%. This construction is provably secure in the NM-CCA
/* --- @doencrypt@ --- *
*
* Arguments: @keyset *ks@ = pointer to keyset to use
+ * @unsigned ty@ = type of message this is
* @buf *b@ = pointer to an input buffer
* @buf *bb@ = pointer to an output buffer
*
* keyset is OK to use.
*/
-static int doencrypt(keyset *ks, buf *b, buf *bb)
+static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
{
ghash *h;
- gcipher *c;
+ gcipher *c = ks->cout;
const octet *p = BCUR(b);
size_t sz = BLEFT(b);
octet *qmac, *qseq, *qiv, *qpk;
uint32 oseq;
+ size_t ivsz = GC_CLASS(c)->blksz;
+ size_t tagsz = ks->tagsz;
size_t osz, nsz;
+ octet t[4];
int rc = 0;
/* --- Allocate the required buffer space --- */
- c = ks->cout;
- if (buf_ensure(bb, MACSZ + SEQSZ + IVSZ + sz))
+ if (buf_ensure(bb, tagsz + SEQSZ + ivsz + sz))
return (0); /* Caution! */
- qmac = BCUR(bb); qseq = qmac + MACSZ; qiv = qseq + SEQSZ; qpk = qiv + IVSZ;
- BSTEP(bb, MACSZ + SEQSZ + IVSZ + sz);
-
- /* --- Encrypt the packet --- */
+ qmac = BCUR(bb); qseq = qmac + tagsz; qiv = qseq + SEQSZ; qpk = qiv + ivsz;
+ BSTEP(bb, tagsz + SEQSZ + ivsz + sz);
+ STORE32(t, ty);
oseq = ks->oseq++; STORE32(qseq, oseq);
- rand_get(RAND_GLOBAL, qiv, IVSZ);
- c->ops->setiv(c, qiv);
- c->ops->encrypt(c, p, qpk, sz);
IF_TRACING(T_KEYSET, {
trace(T_KEYSET, "keyset: encrypting packet %lu using keyset %u",
(unsigned long)oseq, ks->seq);
- trace_block(T_CRYPTO, "crypto: encrypted packet", qpk, sz);
+ trace_block(T_CRYPTO, "crypto: plaintext packet", p, sz);
})
- /* --- Now compute the MAC --- */
+ /* --- Encrypt the packet --- */
- h = ks->mout->ops->init(ks->mout);
- h->ops->hash(h, qseq, SEQSZ + IVSZ + sz);
- memcpy(qmac, h->ops->done(h, 0), MACSZ);
- h->ops->destroy(h);
+ if (ivsz) {
+ rand_get(RAND_GLOBAL, qiv, ivsz);
+ GC_SETIV(c, qiv);
+ IF_TRACING(T_KEYSET, {
+ trace_block(T_CRYPTO, "crypto: initialization vector", qiv, ivsz);
+ })
+ }
+ GC_ENCRYPT(c, p, qpk, sz);
IF_TRACING(T_KEYSET, {
- trace_block(T_CRYPTO, "crypto: computed MAC", qmac, MACSZ);
+ trace_block(T_CRYPTO, "crypto: encrypted packet", qpk, sz);
})
+ /* --- Now compute the MAC --- */
+
+ if (tagsz) {
+ h = GM_INIT(ks->mout);
+ GH_HASH(h, t, sizeof(t));
+ GH_HASH(h, qseq, SEQSZ + ivsz + sz);
+ memcpy(qmac, GH_DONE(h, 0), tagsz);
+ GH_DESTROY(h);
+ IF_TRACING(T_KEYSET, {
+ trace_block(T_CRYPTO, "crypto: computed MAC", qmac, tagsz);
+ })
+ }
+
/* --- Deduct the packet size from the key's data life --- */
osz = ks->sz_exp;
/* --- @dodecrypt@ --- *
*
* Arguments: @keyset *ks@ = pointer to keyset to use
+ * @unsigned ty@ = expected type code
* @buf *b@ = pointer to an input buffer
* @buf *bb@ = pointer to an output buffer
* @uint32 *seq@ = where to store the sequence number
* packet, and the packet's sequence number is stored in @*seq@.
*/
-static int dodecrypt(keyset *ks, buf *b, buf *bb, uint32 *seq)
+static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq)
{
const octet *pmac, *piv, *pseq, *ppk;
size_t psz = BLEFT(b);
octet *q = BCUR(bb);
ghash *h;
gcipher *c = ks->cin;
- size_t ivsz = c->ops->c->blksz;
+ size_t ivsz = GC_CLASS(c)->blksz;
+ size_t tagsz = ks->tagsz;
octet *mac;
int eq;
+ octet t[4];
/* --- Break up the packet into its components --- */
- if (psz < ivsz + 4) {
+ if (psz < ivsz + SEQSZ + tagsz) {
T( trace(T_KEYSET, "keyset: block too small for keyset %u", ks->seq); )
return (-1);
}
- sz = psz - IVSZ - SEQSZ - MACSZ;
- pmac = BCUR(b); pseq = pmac + MACSZ; piv = pseq + SEQSZ; ppk = piv + IVSZ;
+ sz = psz - ivsz - SEQSZ - tagsz;
+ pmac = BCUR(b); pseq = pmac + tagsz; piv = pseq + SEQSZ; ppk = piv + ivsz;
+ STORE32(t, ty);
- /* --- Verify the MAC on the packet --- */
-
- h = ks->min->ops->init(ks->min);
- h->ops->hash(h, pseq, SEQSZ + IVSZ + sz);
- mac = h->ops->done(h, 0);
- eq = !memcmp(mac, pmac, MACSZ);
IF_TRACING(T_KEYSET, {
trace(T_KEYSET, "keyset: decrypting using keyset %u", ks->seq);
- trace_block(T_CRYPTO, "crypto: computed MAC", mac, MACSZ);
+ trace_block(T_CRYPTO, "crypto: ciphertext packet", ppk, sz);
})
- h->ops->destroy(h);
- if (!eq) {
+
+ /* --- Verify the MAC on the packet --- */
+
+ if (tagsz) {
+ h = GM_INIT(ks->min);
+ GH_HASH(h, t, sizeof(t));
+ GH_HASH(h, pseq, SEQSZ + ivsz + sz);
+ mac = GH_DONE(h, 0);
+ eq = !memcmp(mac, pmac, tagsz);
IF_TRACING(T_KEYSET, {
- trace(T_KEYSET, "keyset: decryption failed");
- trace_block(T_CRYPTO, "crypto: expected MAC", pmac, MACSZ);
+ trace_block(T_CRYPTO, "crypto: computed MAC", mac, tagsz);
})
- return (-1);
+ GH_DESTROY(h);
+ if (!eq) {
+ IF_TRACING(T_KEYSET, {
+ trace(T_KEYSET, "keyset: incorrect MAC: decryption failed");
+ trace_block(T_CRYPTO, "crypto: expected MAC", pmac, tagsz);
+ })
+ return (-1);
+ }
}
/* --- Decrypt the packet --- */
- c->ops->setiv(c, piv);
- c->ops->decrypt(c, ppk, q, sz);
+ if (ivsz) {
+ GC_SETIV(c, piv);
+ IF_TRACING(T_KEYSET, {
+ trace_block(T_CRYPTO, "crypto: initialization vector", piv, ivsz);
+ })
+ }
+ GC_DECRYPT(c, ppk, q, sz);
if (seq)
*seq = LOAD32(pseq);
IF_TRACING(T_KEYSET, {
return (0);
}
-/* --- @dosequence@ --- *
- *
- * Arguments: @keyset *ks@ = pointer to a keyset
- * @uint32 seq@ = a sequence number from a packet
- *
- * Returns: Zero if the sequence number is OK, nonzero if it's not.
- *
- * Use: Checks a sequence number. The data in the keyset which keeps
- * track of valid sequence numbers is updated if the sequence
- * number given is good. It's assumed that the sequence number
- * has already been checked for authenticity.
- */
-
-static int dosequence(keyset *ks, uint32 seq)
-{
- uint32 seqbit;
- uint32 n;
-
- if (seq < ks->iseq) {
- a_warn("received packet has old sequence number (possible replay)");
- return (-1);
- }
- if (seq >= ks->iseq + KS_SEQWINSZ) {
- n = seq - (ks->iseq + KS_SEQWINSZ - 1);
- if (n < KS_SEQWINSZ)
- ks->iwin >>= n;
- else
- ks->iwin = 0;
- ks->iseq += n;
- }
- seqbit = 1 << (seq - ks->iseq);
- if (ks->iwin & seqbit) {
- a_warn("received packet repeats old sequence number");
- return (-1);
- }
- ks->iwin |= seqbit;
- return (0);
-}
-
/*----- Operations on a single keyset -------------------------------------*/
/* --- @ks_drop@ --- *
{
if (--ks->ref)
return;
- ks->cin->ops->destroy(ks->cin);
- ks->cout->ops->destroy(ks->cout);
- ks->min->ops->destroy(ks->min);
- ks->mout->ops->destroy(ks->mout);
+ GC_DESTROY(ks->cin);
+ GC_DESTROY(ks->cout);
+ GM_DESTROY(ks->min);
+ GM_DESTROY(ks->mout);
DESTROY(ks);
}
*
* Arguments: @const void *k@ = pointer to key material
* @size_t x, y, z@ = offsets into key material (see below)
+ * @peer *p@ = pointer to peer information
*
* Returns: A pointer to the new keyset.
*
* calling @ks_encrypt@ directly.
*/
-keyset *ks_gen(const void *k, size_t x, size_t y, size_t z)
+keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p)
{
- HASH_CTX h;
- octet buf[HASHSZ];
+ ghash *h;
+ const octet *hh;
keyset *ks = CREATE(keyset);
time_t now = time(0);
- const octet *p = k;
+ const octet *pp = k;
T( static unsigned seq = 0; )
T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); )
* This is done with macros, because it's quite tedious.
*/
-#define MINE HASH(&h, p, x)
-#define YOURS HASH(&h, p + x, y - x)
-#define OURS HASH(&h, p + y, z - y)
-
-#define IN MINE; YOURS; OURS
-#define OUT YOURS; MINE; OURS
-#define STR_IN "incoming"
-#define STR_OUT "outgoing"
-
-#define GETHASH(str, dir) do { \
- HASH_INIT(&h); \
- HASH_STRING(&h, "tripe-" str); \
- dir; \
- HASH_DONE(&h, buf); \
+#define MINE GH_HASH(h, pp, x)
+#define YOURS GH_HASH(h, pp + x, y - x)
+#define OURS GH_HASH(h, pp + y, z - y)
+
+#define HASH_in MINE; YOURS; OURS
+#define HASH_out YOURS; MINE; OURS
+#define INIT_c(k) GC_INIT(algs.c, (k), algs.cksz)
+#define INIT_m(k) GM_KEY(algs.m, (k), algs.mksz)
+#define STR_c "encryption"
+#define STR_m "integrity"
+#define STR_in "incoming"
+#define STR_out "outgoing"
+
+#define SETKEY(a, dir) do { \
+ h = GH_INIT(algs.h); \
+ HASH_STRING(h, "tripe-" STR_##a); \
+ HASH_##dir; \
+ hh = GH_DONE(h, 0); \
IF_TRACING(T_KEYSET, { \
- trace_block(T_CRYPTO, "crypto: " STR_##dir " key " str, \
- buf, sizeof(buf)); \
+ trace_block(T_CRYPTO, "crypto: " STR_##dir " key " STR_##a, \
+ hh, algs.a##ksz); \
}) \
+ ks->a##dir = INIT_##a(hh); \
+ GH_DESTROY(h); \
} while (0)
- GETHASH("encryption", IN); ks->cin = CIPHER->init(buf, sizeof(buf));
- GETHASH("integrity", IN); ks->min = MAC->key(buf, sizeof(buf));
- GETHASH("encryption", OUT); ks->cout = CIPHER->init(buf, sizeof(buf));
- GETHASH("integrity", OUT); ks->mout = MAC->key(buf, sizeof(buf));
+ SETKEY(c, in); SETKEY(c, out);
+ SETKEY(m, in); SETKEY(m, out);
#undef MINE
#undef YOURS
#undef OURS
-#undef IN
-#undef OUT
-#undef STR_IN
-#undef STR_OUT
-#undef GETHASH
+#undef STR_c
+#undef STR_m
+#undef STR_in
+#undef STR_out
+#undef INIT_c
+#undef INIT_m
+#undef HASH_in
+#undef HASH_out
+#undef SETKEY
T( ks->seq = seq++; )
+ ks->ref = 1;
ks->t_exp = now + T_EXP;
ks->sz_exp = SZ_EXP;
- ks->oseq = ks->iseq = 0;
- ks->iwin = 0;
+ ks->oseq = 0;
+ seq_reset(&ks->iseq);
ks->next = 0;
+ ks->p = p;
ks->f = KSF_LISTEN;
- BURN(buf);
+ ks->tagsz = algs.tagsz;
return (ks);
}
/* --- @ks_encrypt@ --- *
*
* Arguments: @keyset *ks@ = pointer to a keyset
+ * @unsigned ty@ = message type
* @buf *b@ = pointer to input buffer
* @buf *bb@ = pointer to output buffer
*
* used even if it's marked as not for data output.
*/
-int ks_encrypt(keyset *ks, buf *b, buf *bb)
+int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
{
time_t now = time(0);
buf_break(bb);
return (0);
}
- return (doencrypt(ks, b, bb));
+ return (doencrypt(ks, ty, b, bb));
}
/* --- @ks_decrypt@ --- *
*
* Arguments: @keyset *ks@ = pointer to a keyset
+ * @unsigned ty@ = expected type code
* @buf *b@ = pointer to an input buffer
* @buf *bb@ = pointer to an output buffer
*
* marking that it's not for encryption.
*/
-int ks_decrypt(keyset *ks, buf *b, buf *bb)
+int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
{
time_t now = time(0);
uint32 seq;
if (!KEYOK(ks, now) ||
buf_ensure(bb, BLEN(b)) ||
- dodecrypt(ks, b, bb, &seq) ||
- dosequence(ks, seq))
+ dodecrypt(ks, ty, b, bb, &seq) ||
+ seq_check(&ks->iseq, seq, "SYMM"))
return (-1);
return (0);
}
/* --- @ksl_encrypt@ --- *
*
* Arguments: @keyset **ksroot@ = pointer to keyset list head
+ * @unsigned ty@ = message type
* @buf *b@ = pointer to input buffer
* @buf *bb@ = pointer to output buffer
*
* Use: Encrypts a packet.
*/
-int ksl_encrypt(keyset **ksroot, buf *b, buf *bb)
+int ksl_encrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb)
{
time_t now = time(0);
keyset *ks = *ksroot;
ks = ks->next;
}
- return (doencrypt(ks, b, bb));
+ return (doencrypt(ks, ty, b, bb));
}
/* --- @ksl_decrypt@ --- *
*
* Arguments: @keyset **ksroot@ = pointer to keyset list head
+ * @unsigned ty@ = expected type code
* @buf *b@ = pointer to input buffer
* @buf *bb@ = pointer to output buffer
*
* Use: Decrypts a packet.
*/
-int ksl_decrypt(keyset **ksroot, buf *b, buf *bb)
+int ksl_decrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb)
{
time_t now = time(0);
keyset *ks;
for (ks = *ksroot; ks; ks = ks->next) {
if (!KEYOK(ks, now))
continue;
- if (!dodecrypt(ks, b, bb, &seq)) {
+ if (!dodecrypt(ks, ty, b, bb, &seq)) {
if (ks->f & KSF_LISTEN) {
T( trace(T_KEYSET, "keyset: implicitly activating keyset %u",
ks->seq); )
ks->f &= ~KSF_LISTEN;
}
- return (dosequence(ks, seq));
+ return (seq_check(&ks->iseq, seq, "SYMM"));
}
}
- T( trace(T_KEYSET, "keyset: no matching keys"); )
+ T( trace(T_KEYSET, "keyset: no matching keys, or incorrect MAC"); )
return (-1);
}