Build and test the new crypto toys
[secnet] / xdh-test.c
diff --git a/xdh-test.c b/xdh-test.c
new file mode 100644 (file)
index 0000000..08efabe
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * xdh-test.c: test harness for elliptic-curve Diffie--Hellman
+ *
+ * (The implementations originally came with different test arrangements,
+ * with complicated external dependencies.  This file replicates the original
+ * tests, but without the dependencies.)
+ */
+/*
+ * This file is Free Software.  It was originally written for secnet.
+ *
+ * Copyright 2017 Mark Wooding
+ *
+ * You may redistribute secnet as a whole and/or modify it under the
+ * terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3, or (at your option) any
+ * later version.
+ *
+ * You may redistribute this file and/or modify it under the terms of
+ * the GNU General Public License as published by the Free Software
+ * Foundation; either version 2, or (at your option) any later
+ * version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this software; if not, see
+ * https://www.gnu.org/licenses/gpl.html.
+ */
+
+#include <stdio.h>
+
+#include "secnet.h"
+
+#include "x25519.h"
+#include "x448.h"
+
+#define x25519_KEYSZ X25519_KEYSZ
+#define x448_KEYSZ X448_KEYSZ
+
+#define GLUE(x, y) GLUE_(x, y)
+#define GLUE_(x, y) x##y
+#define XDHOP(op) GLUE(XDH, _##op)
+
+#define REG_MEMBERS                                                    \
+    uint8_t k[XDHOP(KEYSZ)];
+#include "crypto-test.h"
+
+enum {
+    RZ, NROUT,
+    RN = NROUT, RX, RY, NREG
+};
+
+static void parse_key(union regval *v, char *p)
+    { parse_hex(v->k, sizeof(v->k), p); }
+
+static void dump_key(FILE *fp, const union regval *v)
+    { dump_hex(fp, v->k, sizeof(v->k)); }
+
+static int eq_key(const union regval *v0, const union regval *v1)
+    { return (memcmp(v0->k, v1->k, sizeof(v0->k)) == 0); }
+
+static const struct regty regty_key = {
+    trivial_regty_init,
+    parse_key,
+    dump_key,
+    eq_key,
+    trivial_regty_release
+};
+
+static void test_xdh(struct reg *out, const struct reg *in, void *ctx)
+    { XDH(out[RZ].v.k, in[RX].v.k, in[RY].v.k); }
+
+static void test_xdhmct(struct reg *out, const struct reg *in, void *ctx)
+{
+    uint8_t b0[XDHOP(KEYSZ)], b1[XDHOP(KEYSZ)], *x = b0, *y = b1, *t;
+    unsigned long i, n;
+
+    memcpy(b0, in[RX].v.k, sizeof(b0));
+    memcpy(b1, in[RY].v.k, sizeof(b1));
+    n = in[RN].v.u;
+    for (i = 0; i < n; i++) {
+       XDH(y, x, y);
+       t = y; y = x; x = t;
+    }
+    memcpy(out[RZ].v.k, x, sizeof(b0));
+}
+#define REG_X { "x", RX, &regty_key, 0 }
+#define REG_Y { "Y", RY, &regty_key, 0 }
+#define REG_Z { "Z", RZ, &regty_key, 0 }
+#define REG_N { "n", RN, &regty_uint, 0 }
+static const struct regdef
+    xdh_regs[] = { REG_X, REG_Y, REG_Z, REGLIST_END },
+    xdhmct_regs[] = { REG_X, REG_Y, REG_N, REG_Z, REGLIST_END };
+
+static const struct test tests[] = {
+    { STRING(XDH), run_test, xdh_regs, test_xdh },
+    { STRING(XDH) "-mct", run_test, xdhmct_regs, test_xdhmct },
+    { 0 }
+};
+
+int main(void)
+    { return run_test_suite(NROUT, NREG, sizeof(struct reg), tests, stdin); }