chiark / gitweb /
Support elliptic curve key exchange.
[tripe] / keyexch.c
index c17f4009f95d54f9c199bdfbb31ee50666ebed94..ea503583f976b50cfc536ba7fe6966487583b67f 100644 (file)
--- a/keyexch.c
+++ b/keyexch.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: keyexch.c,v 1.4 2001/06/22 19:40:36 mdw Exp $
+ * $Id: keyexch.c,v 1.11 2004/04/03 12:35:13 mdw Exp $
  *
  * Key exchange protocol
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: keyexch.c,v $
+ * Revision 1.11  2004/04/03 12:35:13  mdw
+ * Support elliptic curve key exchange.
+ *
+ * Revision 1.10  2003/10/15 09:29:38  mdw
+ * Cosmetic fix to changelog comment.
+ *
+ * Revision 1.9  2003/07/13 11:53:14  mdw
+ * Add protocol commentary.
+ *
+ * Revision 1.8  2003/07/13 11:19:49  mdw
+ * Incompatible 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:01:28  mdw
+ * Handle flags on challenge timers correctly to prevent confusing the event
+ * list.
+ *
+ * Revision 1.6  2003/04/06 10:26:35  mdw
+ * Report peer name on decrypt errors.
+ *
+ * Revision 1.5  2002/01/13 14:54:40  mdw
+ * Patch up zero-knowledge property by passing an encrypted log with a
+ * challenge, so that the prover can verify that the challenge is good.
+ *
  * Revision 1.4  2001/06/22 19:40:36  mdw
  * Support expiry of other peers' public keys.
  *
 
 #include "tripe.h"
 
+/*----- Brief protocol overview -------------------------------------------*
+ *
+ * Let %$G$% be a cyclic group; let %$g$% be a generator of %$G$%, and let
+ * %$q$% be the order of %$G$%; for a key %$K$%, let %$E_K(\cdot)$% denote
+ * application of the symmetric packet protocol to a message; let
+ * %$H(\cdot)$% be the random oracle.  Let $\alpha \inr \{0,\ldots,q - 1\}$%
+ * be Alice's private key; let %$a = g^\alpha$% be her public key; let %$b$%
+ * be Bob's public key.
+ *
+ * At the beginning of the session, Alice chooses
+ *
+ *   %$\rho_A \inr \{0, \ldots q - 1\}$%
+ *
+ * We also have:
+ *
+ * %$r_A = g^{\rho_A}$%                        Alice's challenge
+ * %$c_A = H(\cookie{cookie}, r_A)$%   Alice's cookie
+ * %$v_A = \rho_A \xor H(\cookie{expected-reply}, r_A, r_B, b^{\rho_A})$%
+ *                                     Alice's challenge check value
+ * %$r_B^\alpha = a^{\rho_B}$%         Alice's reply
+ * %$K = r_B^{\rho_A} = r_B^{\rho_A} = g^{\rho_A\rho_B}$%
+ *                                     Alice and Bob's shared secret key
+ * %$w_A = H(\cookie{switch-request}, c_A, c_B)$%
+ *                                     Alice's switch request value
+ * %$u_A = H(\cookie{switch-confirm}, c_A, c_B)$%
+ *                                     Alice's switch confirm value
+ *
+ * The messages are then:
+ *
+ * %$\cookie{kx-pre-challenge}, r_A$%
+ *     Initial greeting.  In state @KXS_CHAL@.
+ *
+ * %$\cookie{kx-cookie}, r_A, c_B$%
+ *     My table is full but I got your message.
+ *
+ * %$\cookie{kx-challenge}, r_A, c_B, v_A$%
+ *     Here's a full challenge for you to answer.
+ *
+ * %$\cookie{kx-reply}, c_A, c_B, v_A, E_K(r_B^\alpha))$%
+ *     Challenge accpeted: here's the answer.  Commit to my challenge.  Move
+ *     to @KXS_COMMIT@.
+ *
+ * %$\cookie{kx-switch}, c_A, c_B, E_K(r_B^\alpha, w_A))$%
+ *     Reply received: here's my reply.  Committed; send data; move to
+ *     @KXS_SWITCH@.
+ *
+ * %$\cookie{kx-switch-ok}, E_K(u_A))$%
+ *     Switch received.  Committed; send data; move to @KXS_SWITCH@.
+ */ 
+
 /*----- Tunable parameters ------------------------------------------------*/
 
-#define T_VALID MIN(2)
-#define T_RETRY SEC(10)
+#define T_VALID MIN(2)                 /* Challenge validity period */
+#define T_RETRY SEC(10)                        /* Challenge retransmit interval */
 
 #define ISVALID(kx, now) ((now) < (kx)->t_valid)
 
 /*----- Various utilities -------------------------------------------------*/
 
-/* --- @hashmp@ --- *
+/* --- @hashge@ --- *
  *
  * Arguments:  @HASH_CTX *r@ = pointer to hash context
- *             @mp *m@ = pointer to multiprecision integer
+ *             @ge *x@ = pointer to group element
  *
  * Returns:    ---
  *
- * Use:                Adds the hash of a multiprecision integer to the context.
- *             Corrupts @buf_t@.
+ * Use:                Adds the hash of a group element to the context.  Corrupts
+ *             @buf_t@.
  */
 
-static void hashmp(HASH_CTX *r, mp *m)
+static void hashge(HASH_CTX *r, ge *x)
 {
   buf b;
   buf_init(&b, buf_t, sizeof(buf_t));
-  buf_putmp(&b, m);
+  G_TOBUF(gg, &b, x);
   assert(BOK(&b));
   HASH(r, BBASE(&b), BLEN(&b));
 }
 
+/* --- @mpcrypt@ --- *
+ *
+ * Arguments:  @mp *d@ = the destination integer
+ *             @mp *x@ = the plaintext/ciphertext integer
+ *             @size_t sz@ = the expected size of the plaintext
+ *             @const octet *k@ = pointer to key material
+ *             @size_t ksz@ = size of the key
+ *
+ * Returns:    The encrypted/decrypted integer.
+ *
+ * Use:                Encrypts (or decrypts) a multiprecision integer.  In fact,
+ *             the title is a bit of a misnomer: we actually compute
+ *             %$x \xor H(k)$%, so it's a random oracle thing rather than an
+ *             encryption thing.
+ */
+
+static mp *mpcrypt(mp *d, mp *x, size_t sz, const octet *k, size_t ksz)
+{
+  MGF_CTX m;
+
+  MGF_INIT(&m, k, ksz, 0);
+  mp_storeb(x, buf_t, sz);
+  MGF_CRYPT(&m, buf_t, buf_t, sz);
+  return (mp_loadb(d, buf_t, sz));
+}
+
 /* --- @timer@ --- *
  *
  * Arguments:  @struct timeval *tv@ = the current time
@@ -143,8 +244,9 @@ static void kxc_destroy(kxchal *kxc)
 {
   if (kxc->f & KXF_TIMER)
     sel_rmtimer(&kxc->t);
-  mp_drop(kxc->c);
-  mp_drop(kxc->r);
+  G_DESTROY(gg, kxc->c);
+  if (kxc->r) G_DESTROY(gg, kxc->r);
+  mp_drop(kxc->ck);
   ks_drop(kxc->ks);
   DESTROY(kxc);
 }
@@ -164,6 +266,7 @@ static void kxc_stoptimer(kxchal *kxc)
 {
   if (kxc->f & KXF_TIMER)
     sel_rmtimer(&kxc->t);
+  kxc->f &= ~KXF_TIMER;
 }
 
 /* --- @kxc_new@ --- *
@@ -192,8 +295,9 @@ static kxchal *kxc_new(keyexch *kx)
   /* --- Fill in the new structure --- */
 
   kxc = CREATE(kxchal);
-  kxc->c = 0;
+  kxc->c = G_CREATE(gg);
   kxc->r = 0;
+  kxc->ck = MP_NEW;
   kxc->ks = 0;
   kxc->kx = kx;
   kxc->f = 0;
@@ -204,19 +308,19 @@ static kxchal *kxc_new(keyexch *kx)
 /* --- @kxc_bychal@ --- *
  *
  * Arguments:  @keyexch *kx@ = pointer to key exchange block
- *             @mp *c@ = challenge from remote host
+ *             @ge *c@ = challenge from remote host
  *
  * Returns:    Pointer to the challenge block, or null.
  *
  * Use:                Finds a challenge block, given its challenge.
  */
 
-static kxchal *kxc_bychal(keyexch *kx, mp *c)
+static kxchal *kxc_bychal(keyexch *kx, ge *c)
 {
   unsigned i;
 
   for (i = 0; i < kx->nr; i++) {
-    if (MP_EQ(c, kx->r[i]->c))
+    if (G_EQ(gg, c, kx->r[i]->c))
       return (kx->r[i]);
   }
   return (0);
@@ -273,11 +377,11 @@ static void kxc_answer(keyexch *kx, kxchal *kxc)
   /* --- Build the reply packet --- */
 
   if (!kxc->r)
-    buf_putmp(b, kx->c);
+    G_TOBUF(gg, b, kx->c);
   else
     buf_put(b, kx->hc, HASHSZ);
   buf_put(b, kxc->hc, HASHSZ);
-  buf_put(b, kxc->hrx, HASHSZ);
+  buf_putmp(b, kxc->ck);
 
   /* --- Maybe send an actual reply, if we have one --- */
 
@@ -287,9 +391,9 @@ static void kxc_answer(keyexch *kx, kxchal *kxc)
   } else {
     T( trace(T_KEYEXCH, "keyexch: sending reply to `%s'", p_name(kx->p)); )
     buf_init(&bb, buf_i, sizeof(buf_i));
-    buf_putmp(&bb, kxc->r);
+    G_TOBUF(gg, &bb, kxc->r);
     buf_flip(&bb);
-    ks_encrypt(kxc->ks, &bb, b);
+    ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_REPLY, &bb, b);
   }
 
   /* --- Update the statistics --- */
@@ -315,35 +419,49 @@ static void kxc_answer(keyexch *kx, kxchal *kxc)
 /* --- @getreply@ --- *
  *
  * Arguments:  @keyexch *kx@ = pointer to key exchange context
- *             @mp *c@ = a challenge
- *             @const octet *hrx@ = the supplied expected-reply hash
+ *             @ge *c@ = a challenge
+ *             @mp *ck@ = the supplied expected-reply check value
  *
  * Returns:    A pointer to the reply, or null if the reply-hash was wrong.
  *
  * Use:                Computes replies to challenges.
  */
 
-static mp *getreply(keyexch *kx, mp *c, const octet *hrx)
+static ge *getreply(keyexch *kx, ge *c, mp *ck)
 {
-  mp *r = mpmont_exp(&mg, MP_NEW, c, kpriv.x);
+  ge *r = G_CREATE(gg);
+  ge *y = G_CREATE(gg);
+  mp *a = MP_NEW;
   HASH_CTX h;
-  octet buf[HASHSZ];
+  octet buf[HASHSZ];  
+  int ok;
 
+  G_EXP(gg, r, c, kpriv);
   HASH_INIT(&h);
   HASH_STRING(&h, "tripe-expected-reply");
-  hashmp(&h, c);
-  hashmp(&h, kx->c);
-  hashmp(&h, r);
+  hashge(&h, c);
+  hashge(&h, kx->c);
+  hashge(&h, r);
   HASH_DONE(&h, buf);
+
+  a = mpcrypt(MP_NEW, ck, mp_octets(gg->r), buf, sizeof(buf));
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
-    trace(T_CRYPTO, "crypto: computed reply = %s", mpstr(r));
+    trace(T_CRYPTO, "crypto: computed reply = %s", gestr(gg, r));
     trace_block(T_CRYPTO, "crypto: computed reply hash", buf, HASHSZ);
+    trace(T_CRYPTO, "crypto: recovered log = %s", mpstr(a));
   }))
-  if (memcmp(buf, hrx, HASHSZ) != 0) {
-    a_warn("invalid expected-reply hash from `%s'", p_name(kx->p));
-    mp_drop(r);
-    return (0);
+  G_EXP(gg, y, gg->g, a);
+  ok = G_EQ(gg, y, c);
+  if (!ok) {
+    a_warn("invalid expected-reply check from `%s'", p_name(kx->p));
+    IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
+      trace(T_CRYPTO, "crypto: computed challenge = %s", gestr(gg, y));
+    }))
+    G_DESTROY(gg, r);
+    r = 0;
   }
+  mp_drop(a);
+  G_DESTROY(gg, y);
   return (r);
 }
 
@@ -360,10 +478,12 @@ static mp *getreply(keyexch *kx, mp *c, const octet *hrx)
 
 static int dochallenge(keyexch *kx, unsigned msg, buf *b)
 {
-  mp *c = 0;
-  const octet *hc = 0, *hrx = 0;
+  ge *c = G_CREATE(gg);
+  mp *ck = MP_NEW;
+  const octet *hc = 0;
   kxchal *kxc;
   HASH_CTX h;
+  octet buf[HASHSZ];
 
   /* --- Ensure that we're in a sensible state --- */
 
@@ -374,18 +494,18 @@ static int dochallenge(keyexch *kx, unsigned msg, buf *b)
 
   /* --- Unpack the packet --- */
 
-  if ((c = buf_getmp(b)) == 0 ||
+  if (G_FROMBUF(gg, b, c) ||
       (msg >= KX_COOKIE && (hc = buf_get(b, HASHSZ)) == 0) ||
-      (msg >= KX_CHAL && (hrx = buf_get(b, HASHSZ)) == 0) ||
+      (msg >= KX_CHAL && (ck = buf_getmp(b)) == 0) ||
       BLEFT(b)) {
     a_warn("malformed packet from `%s'", p_name(kx->p));
     goto bad;
   }
 
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
-    trace(T_CRYPTO, "crypto: challenge = %s", mpstr(c));
+    trace(T_CRYPTO, "crypto: challenge = %s", gestr(gg, c));
     if (hc) trace_block(T_CRYPTO, "crypto: cookie", hc, HASHSZ);
-    if (hrx) trace_block(T_CRYPTO, "crypto: response hash", hrx, HASHSZ);
+    if (ck) trace(T_CRYPTO, "crypto: check value = %s", mpstr(ck));
   }))
 
   /* --- First, handle a bare challenge --- *
@@ -396,10 +516,10 @@ static int dochallenge(keyexch *kx, unsigned msg, buf *b)
   if (!hc && kx->nr >= KX_THRESH) {
     T( trace(T_KEYEXCH, "keyexch: too many challenges -- sending cookie"); )
     b = p_txstart(kx->p, MSG_KEYEXCH | KX_COOKIE);
-    buf_putmp(b, kx->c);
+    G_TOBUF(gg, b, kx->c);
     HASH_INIT(&h);
     HASH_STRING(&h, "tripe-cookie");
-    hashmp(&h, c);
+    hashge(&h, c);
     HASH_DONE(&h, buf_get(b, HASHSZ));
     p_txend(kx->p);
     goto tidy;
@@ -419,7 +539,7 @@ static int dochallenge(keyexch *kx, unsigned msg, buf *b)
 
   if ((kxc = kxc_bychal(kx, c)) == 0) {
     size_t x, y, z;
-    mp *r;
+    ge *r;
 
     /* --- Be careful here --- *
      *
@@ -428,56 +548,62 @@ static int dochallenge(keyexch *kx, unsigned msg, buf *b)
      * it.
      */
 
-    if (!hrx)
+    if (!ck)
       kxc = kxc_new(kx);       
     else {
-      if ((r = getreply(kx, c, hrx)) == 0)
+      if ((r = getreply(kx, c, ck)) == 0)
        goto bad;
       kxc = kxc_new(kx);
       kxc->r = r;
     }
-    kxc->c = mp_copy(c);
+    kxc->c = G_CREATE(gg);
+    G_COPY(gg, kxc->c, c);
 
     /* --- Work out the cookie for this challenge --- */
 
     HASH_INIT(&h);
     HASH_STRING(&h, "tripe-cookie");
-    hashmp(&h, kxc->c);
+    hashge(&h, kxc->c);
     HASH_DONE(&h, kxc->hc);    
 
     /* --- Compute the expected-reply hash --- */
 
     HASH_INIT(&h);
     HASH_STRING(&h, "tripe-expected-reply");
-    hashmp(&h, kx->c);
-    hashmp(&h, kxc->c);
-    hashmp(&h, kx->rx);
-    HASH_DONE(&h, kxc->hrx);
+    hashge(&h, kx->c);
+    hashge(&h, kxc->c);
+    hashge(&h, kx->rx);
+    HASH_DONE(&h, buf);
+    kxc->ck = mpcrypt(MP_NEW, kx->alpha, mp_octets(gg->r),
+                     buf, sizeof(buf));
 
     /* --- Work out the shared key --- */
 
-    r = mpmont_exp(&mg, MP_NEW, c, kx->alpha);
+    r = G_CREATE(gg);
+    G_EXP(gg, r, c, kx->alpha);
 
     /* --- Compute the switch messages --- */
 
     HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-request");
-    hashmp(&h, kx->c); hashmp(&h, kxc->c);
+    hashge(&h, kx->c); hashge(&h, kxc->c);
     HASH_DONE(&h, kxc->hswrq_out);
     HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-confirm");
-    hashmp(&h, kx->c); hashmp(&h, kxc->c);
+    hashge(&h, kx->c); hashge(&h, kxc->c);
     HASH_DONE(&h, kxc->hswok_out);
 
     HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-request");
-    hashmp(&h, kxc->c); hashmp(&h, kx->c);
+    hashge(&h, kxc->c); hashge(&h, kx->c);
     HASH_DONE(&h, kxc->hswrq_in);
     HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-confirm");
-    hashmp(&h, kxc->c); hashmp(&h, kx->c);
+    hashge(&h, kxc->c); hashge(&h, kx->c);
     HASH_DONE(&h, kxc->hswok_in);
 
     IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
       trace_block(T_CRYPTO, "crypto: computed cookie", kxc->hc, HASHSZ);
-      trace_block(T_CRYPTO, "crypto: my reply hash", kxc->hrx, HASHSZ);
-      trace(T_CRYPTO, "crypto: shared secret = %s", mpstr(r));
+      trace_block(T_CRYPTO, "crypto: expected-reply hash",
+                 buf, HASHSZ);
+      trace(T_CRYPTO, "crypto: my reply check = %s", mpstr(kxc->ck));
+      trace(T_CRYPTO, "crypto: shared secret = %s", gestr(gg, r));
       trace_block(T_CRYPTO, "crypto: outbound switch request",
                  kxc->hswrq_out, HASHSZ);
       trace_block(T_CRYPTO, "crypto: outbound switch confirm",
@@ -491,20 +617,20 @@ static int dochallenge(keyexch *kx, unsigned msg, buf *b)
     /* --- Create a new symmetric keyset --- */
 
     buf_init(b, buf_o, sizeof(buf_o));
-    buf_putmp(b, kx->c); x = BLEN(b);
-    buf_putmp(b, kxc->c); y = BLEN(b);
-    buf_putmp(b, r); z = BLEN(b);
+    G_TOBUF(gg, b, kx->c); x = BLEN(b);
+    G_TOBUF(gg, b, kxc->c); y = BLEN(b);
+    G_TOBUF(gg, b, r); z = BLEN(b);
     assert(BOK(b));
 
-    kxc->ks = ks_gen(BBASE(b), x, y, z);
-    mp_drop(r);
+    kxc->ks = ks_gen(BBASE(b), x, y, z, kx->p);
+    G_DESTROY(gg, r);
   }
 
   /* --- Answer the challenge if we need to --- */
 
-  if (hrx && !kxc->r) {
-    mp *r;
-    if ((r = getreply(kx, c, hrx)) == 0)
+  if (ck && !kxc->r) {
+    ge *r;
+    if ((r = getreply(kx, c, ck)) == 0)
       goto bad;
     kxc->r = r;
   }
@@ -514,11 +640,13 @@ static int dochallenge(keyexch *kx, unsigned msg, buf *b)
   /* --- Tidy up and go home --- */
 
 tidy:
-  mp_drop(c);
+  G_DESTROY(gg, c);
+  mp_drop(ck);
   return (0);
 
 bad:
-  mp_drop(c);
+  G_DESTROY(gg, c);
+  mp_drop(ck);
   return (-1);
 }
 
@@ -543,7 +671,7 @@ static void resend(keyexch *kx)
       T( trace(T_KEYEXCH, "keyexch: sending prechallenge to `%s'",
               p_name(kx->p)); )
       b = p_txstart(kx->p, MSG_KEYEXCH | KX_PRECHAL);
-      buf_putmp(b, kx->c);
+      G_TOBUF(gg, b, kx->c);
       break;
     case KXS_COMMIT:
       T( trace(T_KEYEXCH, "keyexch: sending switch request to `%s'",
@@ -553,10 +681,10 @@ static void resend(keyexch *kx)
       buf_put(b, kx->hc, HASHSZ);
       buf_put(b, kxc->hc, HASHSZ);
       buf_init(&bb, buf_i, sizeof(buf_i));
-      buf_putmp(&bb, kxc->r);
+      G_TOBUF(gg, &bb, kxc->r);
       buf_put(&bb, kxc->hswrq_out, HASHSZ);
       buf_flip(&bb);
-      ks_encrypt(kxc->ks, &bb, b);
+      ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCH, &bb, b);
       break;
     case KXS_SWITCH:
       T( trace(T_KEYEXCH, "keyexch: sending switch confirmation to `%s'",
@@ -566,7 +694,7 @@ static void resend(keyexch *kx)
       buf_init(&bb, buf_i, sizeof(buf_i));
       buf_put(&bb, kxc->hswok_out, HASHSZ);
       buf_flip(&bb);
-      ks_encrypt(kxc->ks, &bb, b);
+      ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCHOK, &bb, b);
       break;
     default:
       abort();
@@ -585,9 +713,10 @@ static void resend(keyexch *kx)
 /* --- @matchreply@ --- *
  *
  * Arguments:  @keyexch *kx@ = pointer to key exchange context
+ *             @unsigned ty@ = type of incoming message
  *             @const octet *hc_in@ = a hash of his challenge
  *             @const octet *hc_out@ = a hash of my challenge (cookie)
- *             @const octet *krx@ = his expected-reply hash (optional)
+ *             @mp *ck@ = his expected-reply hash (optional)
  *             @buf *b@ = encrypted remainder of the packet
  *
  * Returns:    A pointer to the challenge block if OK, or null on failure.
@@ -598,19 +727,19 @@ static void resend(keyexch *kx)
  *             challenge is returned.
  */
 
-static kxchal *matchreply(keyexch *kx, const octet *hc_in,
-                         const octet *hc_out, const octet *hrx, buf *b)
+static kxchal *matchreply(keyexch *kx, unsigned ty, const octet *hc_in,
+                         const octet *hc_out, mp *ck, buf *b)
 {
   kxchal *kxc;
   buf bb;
-  mp *r = 0;
+  ge *r = 0;
 
   /* --- Check the plaintext portions of the data --- */
 
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
     trace_block(T_CRYPTO, "crypto: challenge", hc_in, HASHSZ);
     trace_block(T_CRYPTO, "crypto: cookie", hc_out, HASHSZ);
-    if (hrx) trace_block(T_CRYPTO, "crypto: response hash", hrx, HASHSZ);
+    if (ck) trace(T_CRYPTO, "crypto: check value = %s", mpstr(ck));
   }))
   if (memcmp(hc_out, kx->hc, HASHSZ) != 0) {
     a_warn("incorrect cookie from `%s'", p_name(kx->p));
@@ -624,11 +753,11 @@ static kxchal *matchreply(keyexch *kx, const octet *hc_in,
   /* --- Maybe compute a reply for the challenge --- */
 
   if (!kxc->r) {
-    if (!hrx) {
+    if (!ck) {
       a_warn("unexpected switch request from `%s'", p_name(kx->p));
       goto bad;
     }
-    if ((r = getreply(kx, kxc->c, hrx)) == 0)
+    if ((r = getreply(kx, kxc->c, ck)) == 0)
       goto bad;
     kxc->r = r;
     r = 0;
@@ -637,30 +766,31 @@ static kxchal *matchreply(keyexch *kx, const octet *hc_in,
   /* --- Decrypt the rest of the packet --- */
 
   buf_init(&bb, buf_o, sizeof(buf_o));
-  if (ks_decrypt(kxc->ks, b, &bb)) {
+  if (ks_decrypt(kxc->ks, ty, b, &bb)) {
     a_warn("failed to decrypt reply from `%s'", p_name(kx->p));
     goto bad;
   }
   buf_init(b, BBASE(&bb), BLEN(&bb));
-  if ((r = buf_getmp(b)) == 0) {
+  r = G_CREATE(gg);
+  if (G_FROMBUF(gg, b, r)) {
     a_warn("invalid reply packet from `%s'", p_name(kx->p));
     goto bad;
   }
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
-    trace(T_CRYPTO, "crypto: reply = %s", mpstr(r));
+    trace(T_CRYPTO, "crypto: reply = %s", gestr(gg, r));
   }))
-  if (!mp_eq(r, kx->rx)) {
+  if (!G_EQ(gg, r, kx->rx)) {
     a_warn("incorrect reply from `%s'", p_name(kx->p));
     goto bad;
   }
 
   /* --- Done --- */
 
-  mp_drop(r);
+  G_DESTROY(gg, r);
   return (kxc);
 
 bad:
-  mp_drop(r);
+  if (r) G_DESTROY(gg, r);
   return (0);
 }
 
@@ -702,7 +832,8 @@ static void commit(keyexch *kx, kxchal *kxc)
 
 static int doreply(keyexch *kx, buf *b)
 {
-  const octet *hc_in, *hc_out, *hrx;
+  const octet *hc_in, *hc_out;
+  mp *ck = 0;
   kxchal *kxc;
 
   if (kx->s != KXS_CHAL && kx->s != KXS_COMMIT) {
@@ -711,11 +842,12 @@ static int doreply(keyexch *kx, buf *b)
   }
   if ((hc_in = buf_get(b, HASHSZ)) == 0 ||
       (hc_out = buf_get(b, HASHSZ)) == 0 ||
-      (hrx = buf_get(b, HASHSZ)) == 0) {
+      (ck = buf_getmp(b)) == 0) {
     a_warn("invalid reply packet from `%s'", p_name(kx->p));
     goto bad;
   }
-  if ((kxc = matchreply(kx, hc_in, hc_out, hrx, b)) == 0)
+  if ((kxc = matchreply(kx, MSG_KEYEXCH | KX_REPLY,
+                       hc_in, hc_out, ck, b)) == 0)
     goto bad;
   if (BLEFT(b)) {
     a_warn("invalid reply packet from `%s'", p_name(kx->p));
@@ -729,6 +861,7 @@ static int doreply(keyexch *kx, buf *b)
   return (0);
 
 bad:
+  mp_drop(ck);
   return (-1);
 }
 
@@ -752,7 +885,8 @@ static int doswitch(keyexch *kx, buf *b)
     a_warn("invalid switch request from `%s'", p_name(kx->p));
     goto bad;
   }
-  if ((kxc = matchreply(kx, hc_in, hc_out, 0, b)) == 0)
+  if ((kxc = matchreply(kx, MSG_KEYEXCH | KX_SWITCH,
+                       hc_in, hc_out, 0, b)) == 0)
     goto bad;
   if ((hswrq = buf_get(b, HASHSZ)) == 0 || BLEFT(b)) {
     a_warn("invalid switch request from `%s'", p_name(kx->p));
@@ -803,7 +937,7 @@ static int doswitchok(keyexch *kx, buf *b)
   }
   kxc = kx->r[0];
   buf_init(&bb, buf_o, sizeof(buf_o));
-  if (ks_decrypt(kxc->ks, b, &bb)) {
+  if (ks_decrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCHOK, b, &bb)) {
     a_warn("failed to decrypt switch confirmation from `%s'", p_name(kx->p));
     goto bad;
   }
@@ -858,8 +992,8 @@ static void stop(keyexch *kx)
   for (i = 0; i < kx->nr; i++)
     kxc_destroy(kx->r[i]);
   mp_drop(kx->alpha);
-  mp_drop(kx->c);
-  mp_drop(kx->rx);
+  G_DESTROY(gg, kx->c);
+  G_DESTROY(gg, kx->rx);
   kx->t_valid = 0;
   kx->f |= KXF_DEAD;
   kx->f &= ~KXF_TIMER;
@@ -884,23 +1018,23 @@ static void start(keyexch *kx, time_t now)
 
   kx->f &= ~KXF_DEAD;
   kx->nr = 0;
-  kx->alpha = mprand_range(MP_NEW, kpriv.dp.q, &rand_global, 0);
-  kx->c = mpmont_exp(&mg, MP_NEW, kpriv.dp.g, kx->alpha);
-  kx->rx = mpmont_exp(&mg, MP_NEW, kx->kpub.y, kx->alpha);
+  kx->alpha = mprand_range(MP_NEW, gg->r, &rand_global, 0);
+  kx->c = G_CREATE(gg); G_EXP(gg, kx->c, gg->g, kx->alpha);
+  kx->rx = G_CREATE(gg); G_EXP(gg, kx->rx, kx->kpub, kx->alpha);
   kx->s = KXS_CHAL;
   kx->t_valid = now + T_VALID;
 
   HASH_INIT(&h);
   HASH_STRING(&h, "tripe-cookie");
-  hashmp(&h, kx->c);
+  hashge(&h, kx->c);
   HASH_DONE(&h, kx->hc);
 
   IF_TRACING(T_KEYEXCH, {
     trace(T_KEYEXCH, "keyexch: creating new challenge");
     IF_TRACING(T_CRYPTO, {
       trace(T_CRYPTO, "crypto: secret = %s", mpstr(kx->alpha));
-      trace(T_CRYPTO, "crypto: challenge = %s", mpstr(kx->c));
-      trace(T_CRYPTO, "crypto: expected response = %s", mpstr(kx->rx));
+      trace(T_CRYPTO, "crypto: challenge = %s", gestr(gg, kx->c));
+      trace(T_CRYPTO, "crypto: expected response = %s", gestr(gg, kx->rx));
       trace_block(T_CRYPTO, "crypto: challenge cookie", kx->hc, HASHSZ);
     })
   })
@@ -925,7 +1059,7 @@ static int checkpub(keyexch *kx)
   if (KEY_EXPIRED(now, kx->texp_kpub)) {
     stop(kx);
     a_warn("public key for `%s' has expired", p_name(kx->p));
-    dh_pubfree(&kx->kpub);
+    G_COPY(gg, kx->kpub, gg->i);
     kx->f &= ~KXF_PUBKEY;
     return (-1);
   }
@@ -1035,8 +1169,7 @@ void kx_message(keyexch *kx, unsigned msg, buf *b)
 void kx_free(keyexch *kx)
 {
   stop(kx);
-  if (kx->f & KXF_PUBKEY)
-    dh_pubfree(&kx->kpub);
+  G_DESTROY(gg, kx->kpub);
 }
 
 /* --- @kx_newkeys@ --- *
@@ -1053,13 +1186,8 @@ void kx_free(keyexch *kx)
 
 void kx_newkeys(keyexch *kx)
 {
-  dh_pub dp;
-
-  if (km_getpubkey(p_name(kx->p), &dp, &kx->texp_kpub))
+  if (km_getpubkey(p_name(kx->p), kx->kpub, &kx->texp_kpub))
     return;
-  if (kx->f & KXF_PUBKEY)
-    dh_pubfree(&kx->kpub);
-  kx->kpub = dp;
   kx->f |= KXF_PUBKEY;
   if ((kx->f & KXF_DEAD) || kx->s != KXS_SWITCH) {
     T( trace(T_KEYEXCH, "keyexch: restarting key negotiation with `%s'",
@@ -1087,8 +1215,11 @@ int kx_init(keyexch *kx, peer *p, keyset **ks)
 {
   kx->ks = ks;
   kx->p = p;
-  if (km_getpubkey(p_name(p), &kx->kpub, &kx->texp_kpub))
+  kx->kpub = G_CREATE(gg);
+  if (km_getpubkey(p_name(p), kx->kpub, &kx->texp_kpub)) {
+    G_DESTROY(gg, kx->kpub);
     return (-1);
+  }
   kx->f = KXF_DEAD | KXF_PUBKEY;
   start(kx, time(0));
   resend(kx);