2 * xdh-test.c: test harness for elliptic-curve Diffie--Hellman
4 * (The implementations originally came with different test arrangements,
5 * with complicated external dependencies. This file replicates the original
6 * tests, but without the dependencies.)
9 * This file is Free Software. It was originally written for secnet.
11 * Copyright 2017 Mark Wooding
13 * You may redistribute secnet as a whole and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3, or (at your option) any
18 * You may redistribute this file and/or modify it under the terms of
19 * the GNU General Public License as published by the Free Software
20 * Foundation; either version 2, or (at your option) any later
23 * This software is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this software; if not, see
30 * https://www.gnu.org/licenses/gpl.html.
40 #define x25519_KEYSZ X25519_KEYSZ
41 #define x448_KEYSZ X448_KEYSZ
43 #define GLUE(x, y) GLUE_(x, y)
44 #define GLUE_(x, y) x##y
45 #define XDHOP(op) GLUE(XDH, _##op)
48 uint8_t k[XDHOP(KEYSZ)];
49 #include "crypto-test.h"
53 RN = NROUT, RX, RY, NREG
56 static void parse_key(union regval *v, char *p)
57 { parse_hex(v->k, sizeof(v->k), p); }
59 static void dump_key(FILE *fp, const union regval *v)
60 { dump_hex(fp, v->k, sizeof(v->k)); }
62 static int eq_key(const union regval *v0, const union regval *v1)
63 { return (memcmp(v0->k, v1->k, sizeof(v0->k)) == 0); }
65 static const struct regty regty_key = {
73 static void test_xdh(struct reg *out, const struct reg *in, void *ctx)
74 { XDH(out[RZ].v.k, in[RX].v.k, in[RY].v.k); }
76 static void test_xdhmct(struct reg *out, const struct reg *in, void *ctx)
78 uint8_t b0[XDHOP(KEYSZ)], b1[XDHOP(KEYSZ)], *x = b0, *y = b1, *t;
81 memcpy(b0, in[RX].v.k, sizeof(b0));
82 memcpy(b1, in[RY].v.k, sizeof(b1));
84 for (i = 0; i < n; i++) {
88 memcpy(out[RZ].v.k, x, sizeof(b0));
90 #define REG_X { "x", RX, ®ty_key, 0 }
91 #define REG_Y { "Y", RY, ®ty_key, 0 }
92 #define REG_Z { "Z", RZ, ®ty_key, 0 }
93 #define REG_N { "n", RN, ®ty_uint, 0 }
94 static const struct regdef
95 xdh_regs[] = { REG_X, REG_Y, REG_Z, REGLIST_END },
96 xdhmct_regs[] = { REG_X, REG_Y, REG_N, REG_Z, REGLIST_END };
98 static const struct test tests[] = {
99 { STRING(XDH), run_test, xdh_regs, test_xdh },
100 { STRING(XDH) "-mct", run_test, xdhmct_regs, test_xdhmct },
105 { return run_test_suite(NROUT, NREG, sizeof(struct reg), tests, stdin); }