chiark / gitweb /
server/: Replace the Diffie--Hellman group abstraction.
[tripe] / server / keyexch.c
index 0c1ed829bddc80a73e09c2a20b6a50f802ec502b..fb0e4535136ee753bf7a972cf58add4c1b23f594 100644 (file)
@@ -97,8 +97,8 @@ static const char *const pkname[] = {
 /* --- @hashge@ --- *
  *
  * Arguments:  @ghash *h@ = pointer to hash context
- *             @group *g@ = pointer to group
- *             @ge *x@ = pointer to group element
+ *             @const dhgrp *g@ = pointer to group
+ *             @const dhge *Y@ = pointer to group element
  *
  * Returns:    ---
  *
@@ -106,12 +106,12 @@ static const char *const pkname[] = {
  *             @buf_t@.
  */
 
-static void hashge(ghash *h, group *g, ge *x)
+static void hashge(ghash *h, const dhgrp *g, const dhge *Y)
 {
   buf b;
 
   buf_init(&b, buf_t, sizeof(buf_t));
-  G_TOBUF(g, &b, x);
+  g->ops->stge(g, &b, Y, DHFMT_HASH);
   assert(BOK(&b));
   GH_HASH(h, BBASE(&b), BLEN(&b));
 }
@@ -119,7 +119,8 @@ static void hashge(ghash *h, group *g, ge *x)
 /* --- @mpmask@ --- *
  *
  * Arguments:  @buf *b@ = output buffer
- *             @mp *x@ = the plaintext integer
+ *             @const dhgrp *g@ = the group
+ *             @const dhsc *x@ = the plaintext scalar
  *             @size_t n@ = the expected size of the plaintext
  *             @gcipher *mgfc@ = mask-generating function to use
  *             @const octet *k@ = pointer to key material
@@ -127,12 +128,12 @@ static void hashge(ghash *h, group *g, ge *x)
  *
  * Returns:    ---
  *
- * Use:                Masks a multiprecision integer: returns %$x \xor H(k)$%, so
- *             it's a random oracle thing rather than an encryption thing.
- *             Breaks the output buffer on error.
+ * Use:                Masks a scalar: returns %$x \xor H(k)$%, so it's a random
+ *             oracle thing rather than an encryption thing.  Breaks the
+ *             output buffer on error.
  */
 
-static void mpmask(buf *b, mp *x, size_t n,
+static void mpmask(buf *b, const dhgrp *g, const dhsc *x, size_t n,
                   const gccipher *mgfc, const octet *k, size_t ksz)
 {
   gcipher *mgf;
@@ -141,13 +142,13 @@ static void mpmask(buf *b, mp *x, size_t n,
   if ((p = buf_get(b, n)) == 0) return;
   mgf = GC_INIT(mgfc, k, ksz);
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
-    trace(T_CRYPTO, "crypto: masking index = %s", mpstr(x));
+    trace(T_CRYPTO, "crypto: masking scalar = %s", g->ops->scstr(g, x));
     trace_block(T_CRYPTO, "crypto: masking key", k, ksz);
   }))
-  mp_storeb(x, buf_t, n);
+  if (g->ops->stsc(g, buf_t, n, x)) { buf_break(b); return; }
   GC_ENCRYPT(mgf, buf_t, p, n);
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
-    trace_block(T_CRYPTO, "crypto: index plaintext", buf_t, n);
+    trace_block(T_CRYPTO, "crypto: scalar plaintext", buf_t, n);
     trace_block(T_CRYPTO, "crypto: masked ciphertext", p, n);
   }))
   GC_DESTROY(mgf);
@@ -155,22 +156,23 @@ static void mpmask(buf *b, mp *x, size_t n,
 
 /* --- @mpunmask@ --- *
  *
- * Arguments:  @mp *d@ = the output integer
+ * Arguments:  @const dhgrp *g@ = the group
  *             @const octet *p@ = pointer to the ciphertext
  *             @size_t n@ = the size of the ciphertext
  *             @gcipher *mgfc@ = mask-generating function to use
  *             @const octet *k@ = pointer to key material
  *             @size_t ksz@ = size of the key
  *
- * Returns:    The decrypted integer, or null.
+ * Returns:    The decrypted scalar, or null.
  *
- * Use:                Unmasks a multiprecision integer.
+ * Use:                Unmasks a scalar.
  */
 
-static mp *mpunmask(mp *d, const octet *p, size_t n,
+static dhsc *mpunmask(const dhgrp *g, const octet *p, size_t n,
                    const gccipher *mgfc, const octet *k, size_t ksz)
 {
   gcipher *mgf;
+  dhsc *x;
 
   mgf = GC_INIT(mgfc, k, ksz);
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
@@ -178,22 +180,23 @@ static mp *mpunmask(mp *d, const octet *p, size_t n,
     trace_block(T_CRYPTO, "crypto: masked ciphertext", p, n);
   }))
   GC_DECRYPT(mgf, p, buf_t, n);
-  d = mp_loadb(d, buf_t, n);
+  x = g->ops->ldsc(g, buf_t, n);
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
-    trace_block(T_CRYPTO, "crypto: index plaintext", buf_t, n);
-    trace(T_CRYPTO, "crypto: unmasked index = %s", mpstr(d));
+    trace_block(T_CRYPTO, "crypto: scalar plaintext", buf_t, n);
+    trace(T_CRYPTO, "crypto: unmasked scalar = %s",
+         x ? g->ops->scstr(g, x) : "<failed>");
   }))
   GC_DESTROY(mgf);
-  return (d);
+  return (x);
 }
 
 /* --- @hashcheck@ --- *
  *
  * Arguments:  @keyexch *kx@ = pointer to key-exchange block
- *             @ge *kpub@ = sender's public key
- *             @ge *cc@ = receiver's challenge
- *             @ge *c@ = sender's challenge
- *             @ge *y@ = reply to sender's challenge
+ *             @const dhge *K@ = sender's public key
+ *             @const dhge *CC@ = receiver's challenge
+ *             @const dhge *C@ = sender's challenge
+ *             @const dhge *Y@ = reply to sender's challenge
  *
  * Returns:    Pointer to the hash value (in @buf_t@)
  *
@@ -201,29 +204,30 @@ static mp *mpunmask(mp *d, const octet *p, size_t n,
  *             indices to prove the validity of challenges.  This computes
  *             the masking key used in challenge check values.  This is
  *             really the heart of the whole thing, since it ensures that
- *             the index can be recovered from the history of hashing
+ *             the scalar can be recovered from the history of hashing
  *             queries, which gives us (a) a proof that the authentication
  *             process is zero-knowledge, and (b) a proof that the whole
  *             key-exchange is deniable.
  */
 
-static const octet *hashcheck(keyexch *kx, ge *kpub, ge *cc, ge *c, ge *y)
+static const octet *hashcheck(keyexch *kx, const dhge *K,
+                             const dhge *CC, const dhge *C, const dhge *Y)
 {
   ghash *h = GH_INIT(kx->kpriv->algs.h);
-  group *g = kx->kpriv->g;
+  const dhgrp *g = kx->kpriv->grp;
 
   HASH_STRING(h, "tripe-expected-reply");
-  hashge(h, g, kpub);
-  hashge(h, g, cc);
-  hashge(h, g, c);
-  hashge(h, g, y);
+  hashge(h, g, K);
+  hashge(h, g, CC);
+  hashge(h, g, C);
+  hashge(h, g, Y);
   GH_DONE(h, buf_t);
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
     trace(T_CRYPTO, "crypto: computing challenge check hash");
-    trace(T_CRYPTO, "crypto: public key = %s", gestr(g, kpub));
-    trace(T_CRYPTO, "crypto: receiver challenge = %s", gestr(g, cc));
-    trace(T_CRYPTO, "crypto: sender challenge = %s", gestr(g, c));
-    trace(T_CRYPTO, "crypto: sender reply = %s", gestr(g, y));
+    trace(T_CRYPTO, "crypto: public key = %s", g->ops->gestr(g, K));
+    trace(T_CRYPTO, "crypto: receiver challenge = %s", g->ops->gestr(g, CC));
+    trace(T_CRYPTO, "crypto: sender challenge = %s", g->ops->gestr(g, C));
+    trace(T_CRYPTO, "crypto: sender reply = %s", g->ops->gestr(g, Y));
     trace_block(T_CRYPTO, "crypto: hash output", buf_t, kx->kpriv->algs.hashsz);
   }))
   GH_DESTROY(h);
@@ -234,7 +238,7 @@ static const octet *hashcheck(keyexch *kx, ge *kpub, ge *cc, ge *c, ge *y)
  *
  * Arguments:  @keyexch *kx@ = pointer to key exchange block
  *             @buf *b@ = output buffer for challenge
- *             @ge *c@ = peer's actual challenge
+ *             @const dhge *C@ = peer's actual challenge
  *             @const octet *hc@ = peer's challenge cookie
  *
  * Returns:    ---
@@ -242,12 +246,14 @@ static const octet *hashcheck(keyexch *kx, ge *kpub, ge *cc, ge *c, ge *y)
  * Use:                Writes a full challenge to the message buffer.
  */
 
-static void sendchallenge(keyexch *kx, buf *b, ge *c, const octet *hc)
+static void sendchallenge(keyexch *kx, buf *b,
+                         const dhge *C, const octet *hc)
 {
-  G_TOBUF(kx->kpriv->g, b, kx->c);
+  const dhgrp *g = kx->kpriv->grp;
+  g->ops->stge(g, b, kx->C, DHFMT_VAR);
   buf_put(b, hc, kx->kpriv->algs.hashsz);
-  mpmask(b, kx->alpha, kx->kpriv->indexsz, kx->kpriv->algs.mgf,
-        hashcheck(kx, kx->kpriv->kpub, c, kx->c, kx->rx),
+  mpmask(b, g, kx->a, g->scsz, kx->kpriv->algs.mgf,
+        hashcheck(kx, kx->kpriv->K, C, kx->C, kx->RX),
         kx->kpriv->algs.hashsz);
 }
 
@@ -394,10 +400,11 @@ static void rs_reset(retry *rs) { rs->t = 0; }
 
 static void kxc_destroy(kxchal *kxc)
 {
+  const dhgrp *g = kxc->kx->kpriv->grp;
   if (kxc->f & KXF_TIMER)
     sel_rmtimer(&kxc->t);
-  G_DESTROY(kxc->kx->kpriv->g, kxc->c);
-  G_DESTROY(kxc->kx->kpriv->g, kxc->r);
+  g->ops->freege(g, kxc->C);
+  g->ops->freege(g, kxc->R);
   ks_drop(kxc->ks);
   DESTROY(kxc);
 }
@@ -459,19 +466,20 @@ static kxchal *kxc_new(keyexch *kx)
 /* --- @kxc_bychal@ --- *
  *
  * Arguments:  @keyexch *kx@ = pointer to key exchange block
- *             @ge *c@ = challenge from remote host
+ *             @const dhge *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, ge *c)
+static kxchal *kxc_bychal(keyexch *kx, const dhge *C)
 {
+  const dhgrp *g = kx->kpriv->grp;
   unsigned i;
 
   for (i = 0; i < kx->nr; i++) {
-    if (G_EQ(kx->kpriv->g, c, kx->r[i]->c))
+    if (g->ops->eq(g, C, kx->r[i]->C))
       return (kx->r[i]);
   }
   return (0);
@@ -522,15 +530,16 @@ static void kxc_answer(keyexch *kx, kxchal *kxc)
 {
   stats *st = p_stats(kx->p);
   buf *b = p_txstart(kx->p, MSG_KEYEXCH | KX_REPLY);
+  const dhgrp *g = kx->kpriv->grp;
   struct timeval tv;
   buf bb;
 
   /* --- Build the reply packet --- */
 
   T( trace(T_KEYEXCH, "keyexch: sending reply to `%s'", p_name(kx->p)); )
-  sendchallenge(kx, b, kxc->c, kxc->hc);
+  sendchallenge(kx, b, kxc->C, kxc->hc);
   buf_init(&bb, buf_i, sizeof(buf_i));
-  G_TORAW(kx->kpriv->g, &bb, kxc->r);
+  g->ops->stge(g, &bb, kxc->R, DHFMT_STD);
   buf_flip(&bb);
   ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_REPLY, &bb, b);
 
@@ -567,7 +576,8 @@ static void kxc_answer(keyexch *kx, kxchal *kxc)
 static int doprechallenge(keyexch *kx, buf *b)
 {
   stats *st = p_stats(kx->p);
-  ge *c = G_CREATE(kx->kpriv->g);
+  const dhgrp *g = kx->kpriv->grp;
+  dhge *C = 0;
   ghash *h;
 
   /* --- Ensure that we're in a sensible state --- */
@@ -579,11 +589,11 @@ static int doprechallenge(keyexch *kx, buf *b)
 
   /* --- Unpack the packet --- */
 
-  if (G_FROMBUF(kx->kpriv->g, b, c) || BLEFT(b))
+  if ((C = g->ops->ldge(g, b, DHFMT_VAR)) == 0 || BLEFT(b))
     goto bad;
 
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
-    trace(T_CRYPTO, "crypto: challenge = %s", gestr(kx->kpriv->g, c));
+    trace(T_CRYPTO, "crypto: challenge = %s", g->ops->gestr(g, C));
   }))
 
   /* --- Send out a full challenge by return --- */
@@ -591,8 +601,8 @@ static int doprechallenge(keyexch *kx, buf *b)
   b = p_txstart(kx->p, MSG_KEYEXCH | KX_CHAL);
   h = GH_INIT(kx->kpriv->algs.h);
   HASH_STRING(h, "tripe-cookie");
-  hashge(h, kx->kpriv->g, c);
-  sendchallenge(kx, b, c, GH_DONE(h, 0));
+  hashge(h, g, C);
+  sendchallenge(kx, b, C, GH_DONE(h, 0));
   GH_DESTROY(h);
   st->n_kxout++;
   st->sz_kxout += BLEN(b);
@@ -600,11 +610,11 @@ static int doprechallenge(keyexch *kx, buf *b)
 
   /* --- Done --- */
 
-  G_DESTROY(kx->kpriv->g, c);
+  g->ops->freege(g, C);
   return (0);
 
 bad:
-  if (c) G_DESTROY(kx->kpriv->g, c);
+  if (C) g->ops->freege(g, C);
   return (-1);
 }
 
@@ -622,15 +632,15 @@ bad:
 
 static kxchal *respond(keyexch *kx, unsigned msg, buf *b)
 {
-  group *g = kx->kpriv->g;
+  const dhgrp *g = kx->kpriv->grp;
   const algswitch *algs = &kx->kpriv->algs;
-  size_t ixsz = kx->kpriv->indexsz;
-  ge *c = G_CREATE(g);
-  ge *r = G_CREATE(g);
-  ge *cc = G_CREATE(g);
+  size_t ixsz = g->scsz;
+  dhge *C = 0;
+  dhge *R = 0;
+  dhge *CC = 0;
   const octet *hc, *ck;
   size_t x, y, z;
-  mp *cv = 0;
+  dhsc *c = 0;
   kxchal *kxc;
   ghash *h = 0;
   buf bb;
@@ -638,14 +648,14 @@ static kxchal *respond(keyexch *kx, unsigned msg, buf *b)
 
   /* --- Unpack the packet --- */
 
-  if (G_FROMBUF(g, b, c) ||
+  if ((C = g->ops->ldge(g, b, DHFMT_VAR)) == 0 ||
       (hc = buf_get(b, algs->hashsz)) == 0 ||
       (ck = buf_get(b, ixsz)) == 0) {
     a_warn("KX", "?PEER", kx->p, "invalid", "%s", pkname[msg], A_END);
     goto bad;
   }
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
-    trace(T_CRYPTO, "crypto: challenge = %s", gestr(g, c));
+    trace(T_CRYPTO, "crypto: challenge = %s", g->ops->gestr(g, C));
     trace_block(T_CRYPTO, "crypto: cookie", hc, algs->hashsz);
     trace_block(T_CRYPTO, "crypto: check-value", ck, ixsz);
   }))
@@ -665,7 +675,7 @@ static kxchal *respond(keyexch *kx, unsigned msg, buf *b)
    * This will also find a challenge block and, if necessary, populate it.
    */
 
-  if ((kxc = kxc_bychal(kx, c)) != 0) {
+  if ((kxc = kxc_bychal(kx, C)) != 0) {
     h = GH_INIT(algs->h);
     HASH_STRING(h, "tripe-check-hash");
     GH_HASH(h, ck, ixsz);
@@ -676,32 +686,30 @@ static kxchal *respond(keyexch *kx, unsigned msg, buf *b)
 
     /* --- Compute the reply, and check the magic --- */
 
-    G_EXP(g, r, c, kx->kpriv->kpriv);
-    if ((cv = mpunmask(MP_NEW, ck, ixsz, algs->mgf,
-                      hashcheck(kx, kx->kpub->kpub, kx->c, c, r),
-                      algs->hashsz)) == 0)
+    R = g->ops->mul(g, kx->kpriv->k, C);
+    if ((c = mpunmask(g, ck, ixsz, algs->mgf,
+                     hashcheck(kx, kx->kpub->K, kx->C, C, R),
+                     algs->hashsz)) == 0)
       goto badcheck;
     IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
-      trace(T_CRYPTO, "crypto: computed reply = %s", gestr(g, r));
-      trace(T_CRYPTO, "crypto: recovered log = %s", mpstr(cv));
+      trace(T_CRYPTO, "crypto: computed reply = %s", g->ops->gestr(g, R));
+      trace(T_CRYPTO, "crypto: recovered log = %s", g->ops->scstr(g, c));
     }))
-    if (MP_CMP(cv, >, g->r) ||
-       (G_EXP(g, cc, g->g, cv),
-        !G_EQ(g, c, cc)))
-      goto badcheck;
+    CC = g->ops->mul(g, c, 0);
+    if (!g->ops->eq(g, CC, C)) goto badcheck;
 
     /* --- Fill in a new challenge block --- */
 
     kxc = kxc_new(kx);
-    kxc->c = c; c = 0;
-    kxc->r = r; r = G_CREATE(g);
+    kxc->C = C; C = 0;
+    kxc->R = R; R = 0;
 
     h = GH_INIT(algs->h); HASH_STRING(h, "tripe-check-hash");
     GH_HASH(h, ck, ixsz);
     GH_DONE(h, kxc->ck); GH_DESTROY(h);
 
     h = GH_INIT(algs->h); HASH_STRING(h, "tripe-cookie");
-    hashge(h, g, kxc->c);
+    hashge(h, g, kxc->C);
     GH_DONE(h, kxc->hc); GH_DESTROY(h);
 
     IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
@@ -711,25 +719,25 @@ static kxchal *respond(keyexch *kx, unsigned msg, buf *b)
 
     /* --- Work out the shared key --- */
 
-    G_EXP(g, r, kxc->c, kx->alpha);
+    R = g->ops->mul(g, kx->a, kxc->C);
     IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
-      trace(T_CRYPTO, "crypto: shared secret = %s", gestr(g, r));
+      trace(T_CRYPTO, "crypto: shared secret = %s", g->ops->gestr(g, R));
     }))
 
     /* --- Compute the switch messages --- */
 
     h = GH_INIT(algs->h); HASH_STRING(h, "tripe-switch-request");
-    hashge(h, g, kx->c); hashge(h, g, kxc->c);
+    hashge(h, g, kx->C); hashge(h, g, kxc->C);
     GH_DONE(h, kxc->hswrq_out); GH_DESTROY(h);
     h = GH_INIT(algs->h); HASH_STRING(h, "tripe-switch-confirm");
-    hashge(h, g, kx->c); hashge(h, g, kxc->c);
+    hashge(h, g, kx->C); hashge(h, g, kxc->C);
     GH_DONE(h, kxc->hswok_out); GH_DESTROY(h);
 
     h = GH_INIT(algs->h); HASH_STRING(h, "tripe-switch-request");
-    hashge(h, g, kxc->c); hashge(h, g, kx->c);
+    hashge(h, g, kxc->C); hashge(h, g, kx->C);
     GH_DONE(h, kxc->hswrq_in); GH_DESTROY(h);
     h = GH_INIT(algs->h); HASH_STRING(h, "tripe-switch-confirm");
-    hashge(h, g, kxc->c); hashge(h, g, kx->c);
+    hashge(h, g, kxc->C); hashge(h, g, kx->C);
     GH_DONE(h, kxc->hswok_in); GH_DESTROY(h);
 
     IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
@@ -746,28 +754,28 @@ static kxchal *respond(keyexch *kx, unsigned msg, buf *b)
     /* --- Create a new symmetric keyset --- */
 
     buf_init(&bb, buf_o, sizeof(buf_o));
-    G_TOBUF(g, &bb, kx->c); x = BLEN(&bb);
-    G_TOBUF(g, &bb, kxc->c); y = BLEN(&bb);
-    G_TOBUF(g, &bb, r); z = BLEN(&bb);
+    g->ops->stge(g, &bb, kx->C, DHFMT_HASH); x = BLEN(&bb);
+    g->ops->stge(g, &bb, kxc->C, DHFMT_HASH); y = BLEN(&bb);
+    g->ops->stge(g, &bb, R, DHFMT_HASH); z = BLEN(&bb);
     assert(BOK(&bb));
 
     kxc->ks = ks_gen(BBASE(&bb), x, y, z, kx->p);
   }
 
-  if (c) G_DESTROY(g, c);
-  G_DESTROY(g, cc);
-  G_DESTROY(g, r);
-  mp_drop(cv);
+  if (C) g->ops->freege(g, C);
+  if (CC) g->ops->freege(g, CC);
+  if (R) g->ops->freege(g, R);
+  if (c) g->ops->freesc(g, c);
   return (kxc);
 
 badcheck:
   a_warn("KX", "?PEER", kx->p, "bad-expected-reply-log", A_END);
   goto bad;
 bad:
-  if (c) G_DESTROY(g, c);
-  G_DESTROY(g, cc);
-  G_DESTROY(g, r);
-  mp_drop(cv);
+  if (C) g->ops->freege(g, C);
+  if (CC) g->ops->freege(g, CC);
+  if (R) g->ops->freege(g, R);
+  if (c) g->ops->freesc(g, c);
   return (0);
 }
 
@@ -818,6 +826,7 @@ static void resend(keyexch *kx)
   buf bb;
   stats *st = p_stats(kx->p);
   struct timeval tv;
+  const dhgrp *g = kx->kpriv->grp;
   buf *b;
 
   switch (kx->s) {
@@ -825,7 +834,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);
-      G_TOBUF(kx->kpriv->g, b, kx->c);
+      g->ops->stge(g, b, kx->C, DHFMT_VAR);
       break;
     case KXS_COMMIT:
       T( trace(T_KEYEXCH, "keyexch: sending switch request to `%s'",
@@ -835,7 +844,7 @@ static void resend(keyexch *kx)
       buf_put(b, kx->hc, kx->kpriv->algs.hashsz);
       buf_put(b, kxc->hc, kx->kpriv->algs.hashsz);
       buf_init(&bb, buf_i, sizeof(buf_i));
-      G_TORAW(kx->kpriv->g, &bb, kxc->r);
+      g->ops->stge(g, &bb, kxc->R, DHFMT_STD);
       buf_put(&bb, kxc->hswrq_out, kx->kpriv->algs.hashsz);
       buf_flip(&bb);
       ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCH, &bb, b);
@@ -907,26 +916,26 @@ static int decryptrest(keyexch *kx, kxchal *kxc, unsigned msg, buf *b)
 
 static int checkresponse(keyexch *kx, unsigned msg, buf *b)
 {
-  group *g = kx->kpriv->g;
-  ge *r = G_CREATE(g);
+  const dhgrp *g = kx->kpriv->grp;
+  dhge *R;
 
-  if (G_FROMRAW(g, b, r)) {
+  if ((R = g->ops->ldge(g, b, DHFMT_STD)) == 0) {
     a_warn("KX", "?PEER", kx->p, "invalid", "%s", pkname[msg], A_END);
     goto bad;
   }
   IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
-    trace(T_CRYPTO, "crypto: reply = %s", gestr(g, r));
+    trace(T_CRYPTO, "crypto: reply = %s", g->ops->gestr(g, R));
   }))
-  if (!G_EQ(g, r, kx->rx)) {
+  if (!g->ops->eq(g, R, kx->RX)) {
     a_warn("KX", "?PEER", kx->p, "incorrect", "response", A_END);
     goto bad;
   }
 
-  G_DESTROY(g, r);
+  g->ops->freege(g, R);
   return (0);
 
 bad:
-  G_DESTROY(g, r);
+  if (R) g->ops->freege(g, R);
   return (-1);
 }
 
@@ -1135,6 +1144,7 @@ bad:
 
 static void stop(keyexch *kx)
 {
+  const dhgrp *g = kx->kpriv->grp;
   unsigned i;
 
   if (kx->f & KXF_DEAD)
@@ -1144,9 +1154,9 @@ static void stop(keyexch *kx)
     sel_rmtimer(&kx->t);
   for (i = 0; i < kx->nr; i++)
     kxc_destroy(kx->r[i]);
-  mp_drop(kx->alpha);
-  G_DESTROY(kx->kpriv->g, kx->c);
-  G_DESTROY(kx->kpriv->g, kx->rx);
+  g->ops->freesc(g, kx->a);
+  g->ops->freege(g, kx->C);
+  g->ops->freege(g, kx->RX);
   kx->t_valid = 0;
   kx->f |= KXF_DEAD;
   kx->f &= ~KXF_TIMER;
@@ -1166,31 +1176,32 @@ static void stop(keyexch *kx)
 static void start(keyexch *kx, time_t now)
 {
   algswitch *algs = &kx->kpriv->algs;
-  group *g = kx->kpriv->g;
+  const dhgrp *g = kx->kpriv->grp;
   ghash *h;
 
   assert(kx->f & KXF_DEAD);
 
   kx->f &= ~(KXF_DEAD | KXF_CORK);
   kx->nr = 0;
-  kx->alpha = mprand_range(MP_NEW, g->r, &rand_global, 0);
-  kx->c = G_CREATE(g); G_EXP(g, kx->c, g->g, kx->alpha);
-  kx->rx = G_CREATE(g); G_EXP(g, kx->rx, kx->kpub->kpub, kx->alpha);
+  kx->a = g->ops->randsc(g);
+  kx->C = g->ops->mul(g, kx->a, 0);
+  kx->RX = g->ops->mul(g, kx->a, kx->kpub->K);
   kx->s = KXS_CHAL;
   kx->t_valid = now + T_VALID;
 
   h = GH_INIT(algs->h);
   HASH_STRING(h, "tripe-cookie");
-  hashge(h, g, kx->c);
+  hashge(h, g, kx->C);
   GH_DONE(h, kx->hc);
   GH_DESTROY(h);
 
   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", gestr(g, kx->c));
-      trace(T_CRYPTO, "crypto: expected response = %s", gestr(g, kx->rx));
+      trace(T_CRYPTO, "crypto: secret = %s", g->ops->scstr(g, kx->a));
+      trace(T_CRYPTO, "crypto: challenge = %s", g->ops->gestr(g, kx->C));
+      trace(T_CRYPTO, "crypto: expected response = %s",
+           g->ops->gestr(g, kx->RX));
       trace_block(T_CRYPTO, "crypto: challenge cookie",
                  kx->hc, algs->hashsz);
     })
@@ -1441,7 +1452,8 @@ void kx_newkeys(keyexch *kx)
 newkeys:
   switchp = ((kx->f & KXF_DEAD) ||
             kx->s != KXS_SWITCH ||
-            !group_samep(kx->kpriv->g, kpriv->g));
+            kpriv->grp->ops != kx->kpriv->grp->ops ||
+            !kpriv->grp->ops->samegrpp(kpriv->grp, kx->kpriv->grp));
 
   T( trace(T_KEYEXCH, "keyexch: peer `%s' adopting "
           "%s priv `%s' and %s pub `%s'; %sforcing exchange", p_name(kx->p),