@@@ test keccak and sha3
[secnet] / xdh-test.c
1 /*
2 * xdh-test.c: test harness for elliptic-curve Diffie--Hellman
3 *
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.)
7 */
8 /*
9 * This file is Free Software. It was originally written for secnet.
10 *
11 * Copyright 2017 Mark Wooding
12 *
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
16 * later version.
17 *
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
21 * version.
22 *
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.
27 *
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.
31 */
32
33 #include <stdio.h>
34
35 #include "secnet.h"
36
37 #include "x25519.h"
38 #include "x448.h"
39
40 #define x25519_KEYSZ X25519_KEYSZ
41 #define x448_KEYSZ X448_KEYSZ
42
43 #define GLUE(x, y) GLUE_(x, y)
44 #define GLUE_(x, y) x##y
45 #define XDHOP(op) GLUE(XDH, _##op)
46
47 #define REG_MEMBERS \
48 uint8_t k[XDHOP(KEYSZ)];
49 #include "crypto-test.h"
50
51 enum {
52 RZ, NROUT,
53 RN = NROUT, RX, RY, NREG
54 };
55
56 static void parse_key(union regval *v, char *p)
57 { parse_hex(v->k, sizeof(v->k), p); }
58
59 static void dump_key(FILE *fp, const union regval *v)
60 { dump_hex(fp, v->k, sizeof(v->k)); }
61
62 static int eq_key(const union regval *v0, const union regval *v1)
63 { return (memcmp(v0->k, v1->k, sizeof(v0->k)) == 0); }
64
65 static const struct regty regty_key = {
66 trivial_regty_init,
67 parse_key,
68 dump_key,
69 eq_key,
70 trivial_regty_release
71 };
72
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); }
75
76 static void test_xdhmct(struct reg *out, const struct reg *in, void *ctx)
77 {
78 uint8_t b0[XDHOP(KEYSZ)], b1[XDHOP(KEYSZ)], *x = b0, *y = b1, *t;
79 unsigned long i, n;
80
81 memcpy(b0, in[RX].v.k, sizeof(b0));
82 memcpy(b1, in[RY].v.k, sizeof(b1));
83 n = in[RN].v.u;
84 for (i = 0; i < n; i++) {
85 XDH(y, x, y);
86 t = y; y = x; x = t;
87 }
88 memcpy(out[RZ].v.k, x, sizeof(b0));
89 }
90 #define REG_X { "x", RX, &regty_key, 0 }
91 #define REG_Y { "Y", RY, &regty_key, 0 }
92 #define REG_Z { "Z", RZ, &regty_key, 0 }
93 #define REG_N { "n", RN, &regty_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 };
97
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 },
101 { 0 }
102 };
103
104 int main(void)
105 { return run_test_suite(NROUT, NREG, sizeof(struct reg), tests, stdin); }