chiark / gitweb /
Incopatible protocol fix! Include message type code under MAC tag to prevent
[tripe] / keyset.c
index 12ade4f32a93d55dab9e6f229f397d3796afa909..d109714cc3ea4cb7fd7a490b94fd2d73eb3ac845 100644 (file)
--- a/keyset.c
+++ b/keyset.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: keyset.c,v 1.7 2003/05/17 11:00:47 mdw Exp $
+ * $Id: keyset.c,v 1.8 2003/07/13 11:19:49 mdw Exp $
  *
  * Handling of symmetric keysets
  *
  *
  * Handling of symmetric keysets
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: keyset.c,v $
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: keyset.c,v $
+ * Revision 1.8  2003/07/13 11:19:49  mdw
+ * Incopatible protocol fix!  Include message type code under MAC tag to prevent
+ * cut-and-paste from key-exchange messages to general packet transport.
+ *
  * Revision 1.7  2003/05/17 11:00:47  mdw
  * Don't make scary messages just because one key didn't work on a message:
  * only be frightened if they all fail.  Set initial keyset refcount
  * Revision 1.7  2003/05/17 11:00:47  mdw
  * Don't make scary messages just because one key didn't work on a message:
  * only be frightened if they all fail.  Set initial keyset refcount
 
 /* --- Encrypted data format --- *
  *
 
 /* --- 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
  *
  *
  *   %$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
  *
  * 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
 /* --- @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
  *
  *             @buf *b@ = pointer to an input buffer
  *             @buf *bb@ = pointer to an output buffer
  *
  *             keyset is OK to use.
  */
 
  *             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;
 {
   ghash *h;
   gcipher *c;
@@ -124,6 +130,7 @@ static int doencrypt(keyset *ks, buf *b, buf *bb)
   octet *qmac, *qseq, *qiv, *qpk;
   uint32 oseq;
   size_t osz, nsz;
   octet *qmac, *qseq, *qiv, *qpk;
   uint32 oseq;
   size_t osz, nsz;
+  octet t[4];
   int rc = 0;
 
   /* --- Allocate the required buffer space --- */
   int rc = 0;
 
   /* --- Allocate the required buffer space --- */
@@ -133,6 +140,7 @@ static int doencrypt(keyset *ks, buf *b, buf *bb)
     return (0); /* Caution! */
   qmac = BCUR(bb); qseq = qmac + MACSZ; qiv = qseq + SEQSZ; qpk = qiv + IVSZ;
   BSTEP(bb, MACSZ + SEQSZ + IVSZ + sz);
     return (0); /* Caution! */
   qmac = BCUR(bb); qseq = qmac + MACSZ; qiv = qseq + SEQSZ; qpk = qiv + IVSZ;
   BSTEP(bb, MACSZ + SEQSZ + IVSZ + sz);
+  STORE32(t, ty);
 
   /* --- Encrypt the packet --- */
 
 
   /* --- Encrypt the packet --- */
 
@@ -149,6 +157,7 @@ static int doencrypt(keyset *ks, buf *b, buf *bb)
   /* --- Now compute the MAC --- */
 
   h = ks->mout->ops->init(ks->mout);
   /* --- Now compute the MAC --- */
 
   h = ks->mout->ops->init(ks->mout);
+  h->ops->hash(h, t, sizeof(t));
   h->ops->hash(h, qseq, SEQSZ + IVSZ + sz);
   memcpy(qmac, h->ops->done(h, 0), MACSZ);
   h->ops->destroy(h);
   h->ops->hash(h, qseq, SEQSZ + IVSZ + sz);
   memcpy(qmac, h->ops->done(h, 0), MACSZ);
   h->ops->destroy(h);
@@ -175,6 +184,7 @@ static int doencrypt(keyset *ks, buf *b, buf *bb)
 /* --- @dodecrypt@ --- *
  *
  * Arguments:  @keyset *ks@ = pointer to keyset to use
 /* --- @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
  *             @buf *b@ = pointer to an input buffer
  *             @buf *bb@ = pointer to an output buffer
  *             @uint32 *seq@ = where to store the sequence number
@@ -189,7 +199,7 @@ static int doencrypt(keyset *ks, buf *b, buf *bb)
  *             packet, and the packet's sequence number is stored in @*seq@.
  */
 
  *             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);
 {
   const octet *pmac, *piv, *pseq, *ppk;
   size_t psz = BLEFT(b);
@@ -200,6 +210,7 @@ static int dodecrypt(keyset *ks, buf *b, buf *bb, uint32 *seq)
   size_t ivsz = c->ops->c->blksz;
   octet *mac;
   int eq;
   size_t ivsz = c->ops->c->blksz;
   octet *mac;
   int eq;
+  octet t[4];
 
   /* --- Break up the packet into its components --- */
 
 
   /* --- Break up the packet into its components --- */
 
@@ -209,10 +220,12 @@ static int dodecrypt(keyset *ks, buf *b, buf *bb, uint32 *seq)
   }
   sz = psz - IVSZ - SEQSZ - MACSZ;
   pmac = BCUR(b); pseq = pmac + MACSZ; piv = pseq + SEQSZ; ppk = piv + IVSZ;
   }
   sz = psz - IVSZ - SEQSZ - MACSZ;
   pmac = BCUR(b); pseq = pmac + MACSZ; piv = pseq + SEQSZ; ppk = piv + IVSZ;
+  STORE32(t, ty);
 
   /* --- Verify the MAC on the packet --- */
 
   h = ks->min->ops->init(ks->min);
 
   /* --- Verify the MAC on the packet --- */
 
   h = ks->min->ops->init(ks->min);
+  h->ops->hash(h, t, sizeof(t));
   h->ops->hash(h, pseq, SEQSZ + IVSZ + sz);
   mac = h->ops->done(h, 0);
   eq = !memcmp(mac, pmac, MACSZ);
   h->ops->hash(h, pseq, SEQSZ + IVSZ + sz);
   mac = h->ops->done(h, 0);
   eq = !memcmp(mac, pmac, MACSZ);
@@ -422,6 +435,7 @@ void ks_activate(keyset *ks)
 /* --- @ks_encrypt@ --- *
  *
  * Arguments:  @keyset *ks@ = pointer to a keyset
 /* --- @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
  *
  *             @buf *b@ = pointer to input buffer
  *             @buf *bb@ = pointer to output buffer
  *
@@ -435,7 +449,7 @@ void ks_activate(keyset *ks)
  *             used even if it's marked as not for data output.
  */
 
  *             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);
 
 {
   time_t now = time(0);
 
@@ -443,12 +457,13 @@ int ks_encrypt(keyset *ks, buf *b, buf *bb)
     buf_break(bb);
     return (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
 }
 
 /* --- @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
  *
  *             @buf *b@ = pointer to an input buffer
  *             @buf *bb@ = pointer to an output buffer
  *
@@ -459,14 +474,14 @@ int ks_encrypt(keyset *ks, buf *b, buf *bb)
  *             marking that it's not for encryption.
  */
 
  *             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)) ||
 {
   time_t now = time(0);
   uint32 seq;
 
   if (!KEYOK(ks, now) ||
       buf_ensure(bb, BLEN(b)) ||
-      dodecrypt(ks, b, bb, &seq) ||
+      dodecrypt(ks, ty, b, bb, &seq) ||
       dosequence(ks, seq))
     return (-1);
   return (0);
       dosequence(ks, seq))
     return (-1);
   return (0);
@@ -553,6 +568,7 @@ void ksl_prune(keyset **ksroot)
 /* --- @ksl_encrypt@ --- *
  *
  * Arguments:  @keyset **ksroot@ = pointer to keyset list head
 /* --- @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
  *
  *             @buf *b@ = pointer to input buffer
  *             @buf *bb@ = pointer to output buffer
  *
@@ -561,7 +577,7 @@ void ksl_prune(keyset **ksroot)
  * Use:                Encrypts a packet.
  */
 
  * 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;
 {
   time_t now = time(0);
   keyset *ks = *ksroot;
@@ -577,12 +593,13 @@ int ksl_encrypt(keyset **ksroot, buf *b, buf *bb)
     ks = ks->next;
   }
 
     ks = ks->next;
   }
 
-  return (doencrypt(ks, b, bb));
+  return (doencrypt(ks, ty, b, bb));
 }
 
 /* --- @ksl_decrypt@ --- *
  *
  * Arguments:  @keyset **ksroot@ = pointer to keyset list head
 }
 
 /* --- @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
  *
  *             @buf *b@ = pointer to input buffer
  *             @buf *bb@ = pointer to output buffer
  *
@@ -591,7 +608,7 @@ int ksl_encrypt(keyset **ksroot, buf *b, buf *bb)
  * Use:                Decrypts a packet.
  */
 
  * 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;
 {
   time_t now = time(0);
   keyset *ks;
@@ -603,7 +620,7 @@ int ksl_decrypt(keyset **ksroot, buf *b, buf *bb)
   for (ks = *ksroot; ks; ks = ks->next) {
     if (!KEYOK(ks, now))
       continue;
   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); )
       if (ks->f & KSF_LISTEN) {
        T( trace(T_KEYSET, "keyset: implicitly activating keyset %u",
                 ks->seq); )