/*
- * xdh-test.c: test harness elliptic-curve Diffie--Hellman
+ * 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
* https://www.gnu.org/licenses/gpl.html.
*/
-#include <assert.h>
-#include <errno.h>
-#include <ctype.h>
-#include <stdarg.h>
-#include <stdint.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
#include "secnet.h"
-#include "f25519.h"
-#include "fgoldi.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 FIELDOP(op) GLUE(FIELD, _##op)
-
-#define f25519_FESZ 32u
-#define fgoldi_FESZ 56u
-
-/* Define some global variables we shouldn't need.
- *
- * Annoyingly, `secnet.h' declares static pointers and initializes them to
- * point to some external variables. At `-O0', GCC doesn't optimize these
- * away, so there's a link-time dependency on these variables. Define them
- * here, so that `f25519.c' and `f448.c' can find them.
- *
- * (Later GCC has `-Og', which optimizes without making debugging a
- * nightmare, but I'm not running that version here. Note that `serpent.c'
- * doesn't have this problem because it defines its own word load and store
- * operations to cope with its endian weirdness, whereas the field arithmetic
- * uses `unaligned.h' which manages to include `secnet.h'.)
- */
-uint64_t now_global;
-struct timeval tv_now_global;
-
-union reg {
- long i;
- unsigned long u;
- uint8_t fe[FIELDOP(FESZ)];
-};
-
-struct regty {
- void (*parse)(union reg *r, char *p);
- void (*dump)(FILE *fp, const union reg *r);
- int (*eq)(const union reg *r, const union reg *rr);
-};
-
-struct regdef {
- const char *name;
- const struct regty *ty;
- int r;
-};
-
-struct test {
- const char *name;
- void (*fn)(union reg *out, const union reg *in);
- const struct regdef *regs;
-};
-
-static int lno;
-
-static void bail(const char *msg, ...)
-{
- va_list ap;
- va_start(ap, msg);
- fprintf(stderr, "unexpected error (line %d): ", lno);
- vfprintf(stderr, msg, ap);
- va_end(ap);
- fputc('\n', stderr);
- exit(3);
-}
-
-static void parse_hex(uint8_t *b, size_t sz, char *p)
-{
- size_t n = strlen(p);
- unsigned i;
- char bb[3];
-
- if (n%2) bail("bad hex (odd number of nibbles)");
- else if (n/2 != sz) bail("bad hex (want %zu bytes, found %zu", sz, n/2);
- while (sz) {
- for (i = 0; i < 2; i++) {
- if (!isxdigit((unsigned char)p[i]))
- bail("bad hex digit `%c'", p[i]);
- bb[i] = p[i];
- }
- bb[2] = 0;
- p += 2;
- *b++ = strtoul(bb, 0, 16); sz--;
- }
-}
-
-static void dump_hex(FILE *fp, const uint8_t *b, size_t sz)
- { while (sz--) fprintf(fp, "%02x", *b++); fputc('\n', fp); }
-
-static void parse_int(union reg *r, char *p)
-{
- char *q;
-
- errno = 0;
- r->i = strtol(p, &q, 0);
- if (*q || errno) bail("bad integer `%s'", p);
-}
-
-static void dump_int(FILE *fp, const union reg *r)
- { fprintf(fp, "%ld\n", r->i); }
-
-static int eq_int(const union reg *r, const union reg *rr)
- { return (r->i == rr->i); }
-
-static const struct regty regty_int = { parse_int, dump_int, eq_int };
+#define XDHOP(op) GLUE(XDH, _##op)
-static void parse_uint(union reg *r, char *p)
-{
- char *q;
-
- errno = 0;
- r->u = strtoul(p, &q, 0);
- if (*q || errno) bail("bad integer `%s'", p);
-}
-
-static void dump_uint(FILE *fp, const union reg *r)
- { fprintf(fp, "%lu\n", r->u); }
-
-static int eq_uint(const union reg *r, const union reg *rr)
- { return (r->u == rr->u); }
-
-static const struct regty regty_uint = { parse_uint, dump_uint, eq_uint };
-
-static void parse_fe(union reg *r, char *p)
- { parse_hex(r->fe, sizeof(r->fe), p); }
-
-static void dump_fe(FILE *fp, const union reg *r)
- { dump_hex(fp, r->fe, sizeof(r->fe)); }
-
-static int eq_fe(const union reg *r, const union reg *rr)
- { return (memcmp(r->fe, rr->fe, sizeof(r->fe)) == 0); }
-
-static const struct regty regty_fe = { parse_fe, dump_fe, eq_fe };
+#define REG_MEMBERS \
+ uint8_t k[XDHOP(KEYSZ)];
+#include "crypto-test.h"
enum {
- RZ, RXX = RZ, RYY, NROUT,
- RX = NROUT, RY, RA = RY, RK = RY, RM, RN = RM, NREG
+ RZ, NROUT,
+ RN = NROUT, RX, RY, NREG
};
-#define BINOP(op) \
- static void test_##op(union reg *out, const union reg *in) \
- { \
- FIELD x, y, z; \
- \
- FIELDOP(load)(&x, in[RX].fe); \
- FIELDOP(load)(&y, in[RY].fe); \
- FIELDOP(op)(&z, &x, &y); \
- FIELDOP(store)(out[RZ].fe, &z); \
- }
-
-#define UNOP(op) \
- static void test_##op(union reg *out, const union reg *in) \
- { \
- FIELD x, z; \
- \
- FIELDOP(load)(&x, in[RX].fe); \
- FIELDOP(op)(&z, &x); \
- FIELDOP(store)(out[RZ].fe, &z); \
- }
-
-BINOP(add)
-BINOP(sub)
-BINOP(mul)
-UNOP(sqr)
-UNOP(inv)
-
-static void test_mulconst(union reg *out, const union reg *in)
-{
- FIELD x, z;
+static void parse_key(union regval *v, char *p)
+ { parse_hex(v->k, sizeof(v->k), p); }
- FIELDOP(load)(&x, in[RX].fe);
- FIELDOP(mulconst)(&z, &x, in[RA].i);
- FIELDOP(store)(out[RZ].fe, &z);
-}
+static void dump_key(FILE *fp, const union regval *v)
+ { dump_hex(fp, v->k, sizeof(v->k)); }
-static void test_condswap(union reg *out, const union reg *in)
-{
- FIELD x, y;
+static int eq_key(const union regval *v0, const union regval *v1)
+ { return (memcmp(v0->k, v1->k, sizeof(v0->k)) == 0); }
- FIELDOP(load)(&x, in[RX].fe);
- FIELDOP(load)(&y, in[RY].fe);
- FIELDOP(condswap)(&x, &y, in[RM].u);
- FIELDOP(store)(out[RXX].fe, &x);
- FIELDOP(store)(out[RYY].fe, &y);
-}
+static const struct regty regty_key = {
+ trivial_regty_init,
+ parse_key,
+ dump_key,
+ eq_key,
+ trivial_regty_release
+};
-static void test_xdh(union reg *out, const union reg *in)
- { XDH(out[RZ].fe, in[RK].fe, in[RX].fe); }
+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(union reg *out, const union reg *in)
+static void test_xdhmct(struct reg *out, const struct reg *in, void *ctx)
{
- uint8_t b0[FIELDOP(FESZ)], b1[FIELDOP(FESZ)], *k = b0, *x = b1, *t;
+ uint8_t b0[XDHOP(KEYSZ)], b1[XDHOP(KEYSZ)], *x = b0, *y = b1, *t;
unsigned long i, n;
- memcpy(b0, in[RK].fe, sizeof(b0));
- memcpy(b1, in[RX].fe, sizeof(b1));
- n = in[RM].u;
+ 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(x, k, x);
- t = x; x = k; k = t;
+ XDH(y, x, y);
+ t = y; y = x; x = t;
}
- memcpy(out[RZ].fe, k, sizeof(b0));
+ memcpy(out[RZ].v.k, x, sizeof(b0));
}
-
-#define REG_X { "x", ®ty_fe, RX }
-#define REG_Y { "y", ®ty_fe, RY }
-#define REG_A { "a", ®ty_int, RA }
-#define REG_M { "m", ®ty_uint, RM }
-#define REG_XX { "xx", ®ty_fe, RXX }
-#define REG_YY { "yy", ®ty_fe, RYY }
-#define REG_Z { "z", ®ty_fe, RZ }
-#define REG_BIGX { "X", ®ty_fe, RX }
-#define REG_BIGZ { "Z", ®ty_fe, RZ }
-#define REG_K { "k", ®ty_fe, RK }
-#define REG_N { "n", ®ty_uint, RN }
-#define REGLIST_END { 0 }
+#define REG_X { "x", RX, ®ty_key, 0 }
+#define REG_Y { "Y", RY, ®ty_key, 0 }
+#define REG_Z { "Z", RZ, ®ty_key, 0 }
+#define REG_N { "n", RN, ®ty_uint, 0 }
static const struct regdef
- unop_regs[] = { REG_X, REG_Z, REGLIST_END },
- binop_regs[] = { REG_X, REG_Y, REG_Z, REGLIST_END },
- mulconst_regs[] = { REG_X, REG_A, REG_Z, REGLIST_END },
- condswap_regs[] = { REG_X, REG_Y, REG_M, REG_XX, REG_YY, REGLIST_END },
- xdh_regs[] = { REG_K, REG_BIGX, REG_BIGZ, REGLIST_END },
- xdhmct_regs[] = { REG_K, REG_BIGX, REG_N, REG_BIGZ, REGLIST_END };
+ 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[] = {
- { "add", test_add, binop_regs },
- { "sub", test_sub, binop_regs },
- { "condswap", test_condswap, condswap_regs },
- { "mulconst", test_mulconst, mulconst_regs },
- { "mul", test_mul, binop_regs },
- { "sqr", test_sqr, unop_regs },
- { "inv", test_inv, unop_regs },
- { "xdh", test_xdh, xdh_regs },
- { "xdh-mct", test_xdhmct, xdhmct_regs },
+ { STRING(XDH), run_test, xdh_regs, test_xdh },
+ { STRING(XDH) "-mct", run_test, xdhmct_regs, test_xdhmct },
{ 0 }
};
-static int check(const struct test *test, unsigned iat,
- union reg *out, const union reg *in)
-{
- const struct regdef *rdef;
- int ok = 1;
-
- if (!iat) return (0);
- assert(test);
- for (rdef = test->regs; rdef->name; rdef++)
- if (!(iat & (1 << rdef->r)))
- bail("register `%s' not set", rdef->name);
- test->fn(out, in);
- for (rdef = test->regs; rdef->name; rdef++) {
- if (rdef->r >= NROUT) continue;
- if (!rdef->ty->eq(&in[rdef->r], &out[rdef->r])) ok = 0;
- }
- if (ok) return (0);
-
- fprintf(stderr, "failed `%s'\n", test->name);
- for (rdef = test->regs; rdef->name; rdef++) {
- if (rdef->r >= NROUT) {
- fprintf(stderr, "\tinput `%s' = ", rdef->name);
- rdef->ty->dump(stderr, &in[rdef->r]);
- } else {
- fprintf(stderr, "\texpected `%s' = ", rdef->name);
- rdef->ty->dump(stderr, &in[rdef->r]);
- fprintf(stderr, "\tcomputed `%s' = ", rdef->name);
- rdef->ty->dump(stderr, &out[rdef->r]);
- }
- }
- return (-1);
-}
-
int main(void)
-{
- char linebuf[256];
- char *p;
- const char *q;
- size_t n;
- unsigned iat = 0, rbit;
- const struct test *test = 0;
- union reg in[NREG], out[NROUT];
- const struct regdef *rdef;
- int rc = 0;
-
- lno = 0;
- while (fgets(linebuf, sizeof(linebuf), stdin)) {
- lno++;
- n = strlen(linebuf); assert(n);
- if (linebuf[n - 1] != '\n') bail("line too long");
- linebuf[n - 1] = 0;
- p = linebuf;
- while (isspace((unsigned char)*p)) p++;
- if (*p == '#') continue;
- if (!*p) {
- if (check(test, iat, out, in)) rc = 2;
- iat = 0;
- continue;
- }
- q = p;
- while (*p && !isspace((unsigned char)*p)) p++;
- if (!*p) bail("missing argument");
- *p++ = 0;
- while (isspace((unsigned char)*p)) p++;
- if (strcmp(q, "test") == 0) {
- if (check(test, iat, out, in)) rc = 2;
- iat = 0;
- for (test = tests; test->name; test++)
- if (strcmp(p, test->name) == 0) goto found_test;
- bail("unknown test `%s'", p);
- found_test:
- continue;
- }
- if (!test) bail("no test defined yet");
- for (rdef = test->regs; rdef->name; rdef++)
- if (strcmp(q, rdef->name) == 0) goto found_reg;
- bail("unknown register `%s'", q);
- found_reg:
- rbit = 1 << rdef->r;
- if (iat & rbit)
- bail("register `%s' already set", rdef->name);
- rdef->ty->parse(&in[rdef->r], p);
- iat |= rbit;
- }
- if (check(test, iat, out, in)) rc = 2;
- return (rc);
-}
+ { return run_test_suite(NROUT, NREG, sizeof(struct reg), tests, stdin); }