chiark / gitweb /
server/: Make bulk crypto transforms responsible for algorithm selection.
[tripe] / server / keyset.c
index 94fcae022175a7d242e6b4ee694fcb42d5021bb2..1d7817c846df87c1cc78735c8638d4ad7bbbea1d 100644 (file)
@@ -90,8 +90,9 @@ static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
 
   /* --- Apply the bulk-crypto transformation --- */
 
-  rc = ks->bulk->encrypt(ks, ty, b, bb);
+  rc = ks->bulk->ops->encrypt(ks->bulk, ty, b, bb, ks->oseq);
   if (rc || !BOK(bb)) return (rc);
+  ks->oseq++;
 
   /* --- Do the necessary accounting for data volume --- */
 
@@ -139,7 +140,7 @@ static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq)
     trace_block(T_CRYPTO, "crypto: ciphertext packet", BCUR(b), BLEFT(b));
   })
 
-  rc = ks->bulk->decrypt(ks, ty, b, bb, seq);
+  rc = ks->bulk->ops->decrypt(ks->bulk, ty, b, bb, seq);
   if (rc) return (rc);
 
   IF_TRACING(T_KEYSET, {
@@ -164,22 +165,64 @@ static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq)
 
 void ks_drop(keyset *ks)
 {
-  if (--ks->ref)
-    return;
-
-#define DROP(dir, a, drop) do { if (ks->dir.a) drop(ks->dir.a); } while (0)
-#define DROP_DIR(dir) do {                                             \
-  DROP(dir, c, GC_DESTROY);                                            \
-  DROP(dir, m, GM_DESTROY);                                            \
-} while (0)
+  if (--ks->ref) return;
+  ks->bulk->ops->freectx(ks->bulk);
+  DESTROY(ks);
+}
 
-  DROP_DIR(in);
-  DROP_DIR(out);
+/* --- @ks_derivekey@ --- *
+ *
+ * Arguments:  @octet *k@ = pointer to an output buffer of at least
+ *                     @MAXHASHSZ@ bytes
+ *             @size_t ksz@ = actual size wanted (for tracing)
+ *             @const struct rawkey *rk@ = a raw key, as passed into
+ *                     @genkeys@
+ *             @int dir@ = direction for the key (@DIR_IN@ or @DIR_OUT@)
+ *             @const char *what@ = label for the key (input to derivation)
+ *
+ * Returns:    ---
+ *
+ * Use:                Derives a session key, for use on incoming or outgoing data.
+ *             This function is part of a private protocol between @ks_gen@
+ *             and the bulk crypto transform @genkeys@ operation.
+ */
 
-#undef DROP
-#undef DROP_DIR
+struct rawkey {
+  const gchash *hc;
+  const octet *k;
+  size_t x, y, z;
+};
 
-  DESTROY(ks);
+void ks_derivekey(octet *k, size_t ksz, const struct rawkey *rk,
+                 int dir, const char *what)
+{
+  const gchash *hc = rk->hc;
+  ghash *h;
+
+  assert(ksz <= hc->hashsz);
+  assert(hc->hashsz <= MAXHASHSZ);
+  h = GH_INIT(hc);
+  GH_HASH(h, "tripe-", 6); GH_HASH(h, what, strlen(what) + 1);
+  switch (dir) {
+    case DIR_IN:
+      GH_HASH(h, rk->k, rk->x);
+      GH_HASH(h, rk->k + rk->x, rk->y - rk->x);
+      break;
+    case DIR_OUT:
+      GH_HASH(h, rk->k + rk->x, rk->y - rk->x);
+      GH_HASH(h, rk->k, rk->x);
+      break;
+    default:
+      abort();
+  }
+  GH_HASH(h, rk->k + rk->y, rk->z - rk->y);
+  GH_DONE(h, k);
+  GH_DESTROY(h);
+  IF_TRACING(T_KEYSET, { IF_TRACING(T_CRYPTO, {
+    char _buf[32];
+    sprintf(_buf, "crypto: %s key %s", dir ? "incoming" : "outgoing", what);
+    trace_block(T_CRYPTO, _buf, k, ksz);
+  }) })
 }
 
 /* --- @ks_gen@ --- *
@@ -205,67 +248,30 @@ void ks_drop(keyset *ks)
  *             calling @ks_encrypt@ directly.
  */
 
-static void gen_dir(const algswitch *algs, struct ksdir *ksd,
-                   const char *whichdir,
-                   const octet *from, size_t fromsz,
-                   const octet *to, size_t tosz,
-                   const octet *both, size_t bothsz)
-{
-#define SETKEY(what, a, init) do {                                     \
-  ghash *_h;                                                           \
-  octet *_hh;                                                          \
-                                                                       \
-  if (!algs->a)                                                                \
-    ksd->a = 0;                                                                \
-  else {                                                               \
-    _h = GH_INIT(algs->h);                                             \
-    HASH_STRING(_h, "tripe-" what);                                    \
-    GH_HASH(_h, from, fromsz);                                         \
-    GH_HASH(_h, to, tosz);                                             \
-    GH_HASH(_h, both, bothsz);                                         \
-    _hh = GH_DONE(_h, 0);                                              \
-    IF_TRACING(T_KEYSET, { IF_TRACING(T_CRYPTO, {                      \
-      char _buf[32];                                                   \
-      sprintf(_buf, "crypto: %s key " what, whichdir);                 \
-      trace_block(T_CRYPTO, _buf, _hh, algs->a##ksz);                  \
-    }) })                                                              \
-    ksd->a = init(algs->a, _hh, algs->a##ksz);                         \
-    GH_DESTROY(_h);                                                    \
-  }                                                                    \
-} while (0)
-
-  SETKEY("encryption", c, GC_INIT);
-  SETKEY("integrity", m, GM_KEY);
-  SETKEY("blkc", b, GC_INIT);
-
-#undef SETKEY
-}
-
 keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p)
 {
   keyset *ks = CREATE(keyset);
   time_t now = time(0);
-  const octet *pp = k;
   const algswitch *algs = &p->kx.kpriv->algs;
+  struct rawkey rk;
   T( static unsigned seq = 0; )
 
   T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); )
 
-  gen_dir(algs, &ks->in, "incoming", pp, x, pp + x, y - x, pp + y, z - y);
-  gen_dir(algs, &ks->out, "outgoing", pp + x, y - x, pp, x, pp + y, z - y);
+  rk.hc = algs->h; rk.k = k; rk.x = x; rk.y = y; rk.z = z;
+  ks->bulk = algs->bulk->ops->genkeys(algs->bulk, &rk);
+  ks->bulk->ops = algs->bulk->ops;
 
   T( ks->seq = seq++; )
-  ks->bulk = algs->bulk;
   ks->ref = 1;
   ks->t_exp = now + T_EXP;
-  ks->sz_exp = algs->expsz;
-  ks->sz_regen = algs->expsz/2;
+  ks->sz_exp = algs->bulk->ops->expsz(algs->bulk);
+  ks->sz_regen = ks->sz_exp/2;
   ks->oseq = 0;
   seq_reset(&ks->iseq);
   ks->next = 0;
   ks->p = p;
   ks->f = KSF_LISTEN;
-  ks->tagsz = algs->tagsz;
   return (ks);
 }