@@@ test copyright dates
[secnet] / xdh-test.c
CommitLineData
0bcb8184 1/*
a4c6c9ba 2 * xdh-test.c: test harness for elliptic-curve Diffie--Hellman
0bcb8184
MW
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
0bcb8184 33#include <stdio.h>
0bcb8184
MW
34
35#include "secnet.h"
36
0bcb8184
MW
37#include "x25519.h"
38#include "x448.h"
39
a4c6c9ba
MW
40#define x25519_KEYSZ X25519_KEYSZ
41#define x448_KEYSZ X448_KEYSZ
42
0bcb8184
MW
43#define GLUE(x, y) GLUE_(x, y)
44#define GLUE_(x, y) x##y
a4c6c9ba 45#define XDHOP(op) GLUE(XDH, _##op)
0bcb8184 46
a4c6c9ba
MW
47#define REG_MEMBERS \
48 uint8_t k[XDHOP(KEYSZ)];
49#include "crypto-test.h"
0bcb8184
MW
50
51enum {
a4c6c9ba
MW
52 RZ, NROUT,
53 RN = NROUT, RX, RY, NREG
0bcb8184
MW
54};
55
a4c6c9ba
MW
56static void parse_key(union regval *v, char *p)
57 { parse_hex(v->k, sizeof(v->k), p); }
0bcb8184 58
a4c6c9ba
MW
59static void dump_key(FILE *fp, const union regval *v)
60 { dump_hex(fp, v->k, sizeof(v->k)); }
0bcb8184 61
a4c6c9ba
MW
62static int eq_key(const union regval *v0, const union regval *v1)
63 { return (memcmp(v0->k, v1->k, sizeof(v0->k)) == 0); }
0bcb8184 64
a4c6c9ba
MW
65static const struct regty regty_key = {
66 trivial_regty_init,
67 parse_key,
68 dump_key,
69 eq_key,
70 trivial_regty_release
71};
0bcb8184 72
a4c6c9ba
MW
73static 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); }
0bcb8184 75
a4c6c9ba 76static void test_xdhmct(struct reg *out, const struct reg *in, void *ctx)
0bcb8184 77{
a4c6c9ba 78 uint8_t b0[XDHOP(KEYSZ)], b1[XDHOP(KEYSZ)], *x = b0, *y = b1, *t;
0bcb8184
MW
79 unsigned long i, n;
80
a4c6c9ba
MW
81 memcpy(b0, in[RX].v.k, sizeof(b0));
82 memcpy(b1, in[RY].v.k, sizeof(b1));
83 n = in[RN].v.u;
0bcb8184 84 for (i = 0; i < n; i++) {
a4c6c9ba
MW
85 XDH(y, x, y);
86 t = y; y = x; x = t;
0bcb8184 87 }
a4c6c9ba 88 memcpy(out[RZ].v.k, x, sizeof(b0));
0bcb8184 89}
a4c6c9ba
MW
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 }
0bcb8184 94static const struct regdef
a4c6c9ba
MW
95 xdh_regs[] = { REG_X, REG_Y, REG_Z, REGLIST_END },
96 xdhmct_regs[] = { REG_X, REG_Y, REG_N, REG_Z, REGLIST_END };
0bcb8184
MW
97
98static const struct test tests[] = {
a4c6c9ba
MW
99 { STRING(XDH), run_test, xdh_regs, test_xdh },
100 { STRING(XDH) "-mct", run_test, xdhmct_regs, test_xdhmct },
0bcb8184
MW
101 { 0 }
102};
103
0bcb8184 104int main(void)
a4c6c9ba 105 { return run_test_suite(NROUT, NREG, sizeof(struct reg), tests, stdin); }