2 * crypto-test.c: common test vector processing
5 * This file is Free Software. It was originally written for secnet.
7 * Copyright 2017 Mark Wooding
9 * You may redistribute secnet as a whole and/or modify it under the
10 * terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 3, or (at your option) any
14 * You may redistribute this file and/or modify it under the terms of
15 * the GNU General Public License as published by the Free Software
16 * Foundation; either version 2, or (at your option) any later
19 * This software is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this software; if not, see
26 * https://www.gnu.org/licenses/gpl.html.
40 #include "crypto-test.h"
42 /*----- Utilities ---------------------------------------------------------*/
44 static void *xmalloc(size_t sz)
51 fprintf(stderr, "out of memory!\n");
57 static void *xrealloc(void *p, size_t sz)
61 if (!sz) { free(p); return 0; }
62 else if (!p) return xmalloc(sz);
65 fprintf(stderr, "out of memory!\n");
73 void bail(const char *msg, ...)
77 fprintf(stderr, "unexpected error (line %d): ", lno);
78 vfprintf(stderr, msg, ap);
88 #define LINEBUF_INIT { 0, 0 };
90 static int read_line(struct linebuf *b, FILE *fp)
95 ch = getc(fp); if (ch == EOF) return EOF;
98 b->sz = b->sz ? 2*b->sz : 64;
99 b->p = xrealloc(b->p, b->sz);
101 if (ch == EOF || ch == '\n') { b->p[n++] = 0; return 0; }
107 void parse_hex(uint8_t *b, size_t sz, char *p)
109 size_t n = strlen(p);
113 if (n%2) bail("bad hex (odd number of nibbles)");
114 else if (n/2 != sz) bail("bad hex (want %zu bytes, found %zu)", sz, n/2);
116 for (i = 0; i < 2; i++) {
117 if (!isxdigit((unsigned char)p[i]))
118 bail("bad hex digit `%c'", p[i]);
123 *b++ = strtoul(bb, 0, 16); sz--;
127 void dump_hex(FILE *fp, const uint8_t *b, size_t sz)
128 { while (sz--) fprintf(fp, "%02x", *b++); fputc('\n', fp); }
130 void trivial_regty_init(union regval *v) { ; }
131 void trivial_regty_release(union regval *v) { ; }
133 /* Define some global variables we shouldn't need.
135 * Annoyingly, `secnet.h' declares static pointers and initializes them to
136 * point to some external variables. At `-O0', GCC doesn't optimize these
137 * away, so there's a link-time dependency on these variables. Define them
138 * here, so that `f25519.c' and `f448.c' can find them.
140 * (Later GCC has `-Og', which optimizes without making debugging a
141 * nightmare, but I'm not running that version here. Note that `serpent.c'
142 * doesn't have this problem because it defines its own word load and store
143 * operations to cope with its endian weirdness, whereas the field arithmetic
144 * uses `unaligned.h' which manages to include `secnet.h'.)
147 struct timeval tv_now_global;
149 /* Bletch. util.c is a mess of layers. */
150 int consttime_memeq(const void *s1in, const void *s2in, size_t n)
152 const uint8_t *s1=s1in, *s2=s2in;
153 register volatile uint8_t accumulator=0;
156 accumulator |= (*s1++ ^ *s2++);
158 accumulator |= accumulator >> 4; /* constant-time */
159 accumulator |= accumulator >> 2; /* boolean canonicalisation */
160 accumulator |= accumulator >> 1;
166 /*----- Built-in types ----------------------------------------------------*/
168 /* Signed integer. */
170 static void parse_int(union regval *v, char *p)
175 v->i = strtol(p, &q, 0);
176 if (*q || errno) bail("bad integer `%s'", p);
179 static void dump_int(FILE *fp, const union regval *v)
180 { fprintf(fp, "%ld\n", v->i); }
182 static int eq_int(const union regval *v0, const union regval *v1)
183 { return (v0->i == v1->i); }
185 const struct regty regty_int = {
190 trivial_regty_release
193 /* Unsigned integer. */
195 static void parse_uint(union regval *v, char *p)
200 v->u = strtoul(p, &q, 0);
201 if (*q || errno) bail("bad integer `%s'", p);
204 static void dump_uint(FILE *fp, const union regval *v)
205 { fprintf(fp, "%lu\n", v->u); }
207 static int eq_uint(const union regval *v0, const union regval *v1)
208 { return (v0->u == v1->u); }
210 const struct regty regty_uint = {
215 trivial_regty_release
218 /* Byte string, as hex. */
220 void allocate_bytes(union regval *v, size_t sz)
221 { v->bytes.p = xmalloc(sz); v->bytes.sz = sz; }
223 static void init_bytes(union regval *v) { v->bytes.p = 0; v->bytes.sz = 0; }
225 static void parse_bytes(union regval *v, char *p)
227 size_t n = strlen(p);
229 allocate_bytes(v, n/2);
230 parse_hex(v->bytes.p, v->bytes.sz, p);
233 static void dump_bytes(FILE *fp, const union regval *v)
234 { dump_hex(fp, v->bytes.p, v->bytes.sz); }
236 static int eq_bytes(const union regval *v0, const union regval *v1)
238 return v0->bytes.sz == v1->bytes.sz &&
239 !memcmp(v0->bytes.p, v1->bytes.p, v0->bytes.sz);
242 static void release_bytes(union regval *v) { free(v->bytes.p); }
244 const struct regty regty_bytes = {
252 /*----- Core test machinery -----------------------------------------------*/
254 /* Say that a register is `reset' by releasing and then re-initializing it.
255 * While there is a current test, all of that test's registers are
256 * initialized. The input registers are reset at the end of `check', ready
257 * for the next test to load new values. The output registers are reset at
258 * the end of `check_test_output', so that a test runner can run a test
259 * multiple times against the same test input, but with different context
263 #define REG(rvec, i) \
264 ((struct reg *)((unsigned char *)state->rvec + (i)*state->regsz))
266 void check_test_output(struct test_state *state, const struct test *test)
268 const struct regdef *def;
269 struct reg *reg, *in, *out;
273 for (def = test->regs; def->name; def++) {
274 if (def->i >= state->nrout) continue;
275 in = REG(in, def->i); out = REG(out, def->i);
276 if (!def->ty->eq(&in->v, &out->v)) ok = 0;
281 printf("failed test `%s'\n", test->name);
282 for (def = test->regs; def->name; def++) {
283 in = REG(in, def->i);
284 if (def->i >= state->nrout) {
285 printf("\t input `%s' = ", def->name);
286 def->ty->dump(stdout, &in->v);
288 out = REG(out, def->i);
289 match = def->ty->eq(&in->v, &out->v);
290 printf("\t%s `%s' = ",
291 match ? " output" : "expected", def->name);
292 def->ty->dump(stdout, &in->v);
294 printf("\tcomputed `%s' = ", def->name);
295 def->ty->dump(stdout, &out->v);
302 for (def = test->regs; def->name; def++) {
303 if (def->i >= state->nrout) continue;
304 reg = REG(out, def->i);
305 def->ty->release(®->v); def->ty->init(®->v);
309 void run_test(struct test_state *state, const struct test *test)
311 test->fn(state->out, state->in, 0);
312 check_test_output(state, test);
315 static void check(struct test_state *state, const struct test *test)
317 const struct regdef *def, *miss = 0;
322 for (def = test->regs; def->name; def++) {
323 reg = REG(in, def->i);
324 if (reg->f®F_LIVE) any = 1;
325 else if (!miss && !(def->f®F_OPT)) miss = def;
329 bail("register `%s' not set in test `%s'", def->name, test->name);
331 test->run(state, test);
333 for (def = test->regs; def->name; def++) {
334 reg = REG(in, def->i);
335 reg->f = 0; def->ty->release(®->v); def->ty->init(®->v);
339 int run_test_suite(unsigned nrout, unsigned nreg, size_t regsz,
340 const struct test *tests, FILE *fp)
342 struct linebuf buf = LINEBUF_INIT;
343 struct test_state state[1];
344 const struct test *test;
345 const struct regdef *def;
352 for (test = tests; test->name; test++)
353 for (def = test->regs; def->name; def++)
354 assert(def->i < nreg);
356 state->in = xmalloc(nreg*regsz);
357 state->out = xmalloc(nrout*regsz);
358 state->nrout = nrout;
360 state->regsz = regsz;
361 state->win = state->lose = 0;
365 while (!read_line(&buf, fp)) {
367 p = buf.p; n = strlen(buf.p);
369 while (isspace((unsigned char)*p)) p++;
370 if (*p == '#') continue;
371 if (!*p) { check(state, test); continue; }
374 while (*p && !isspace((unsigned char)*p)) p++;
377 if (!strcmp(q, "test")) {
378 if (!*p) bail("missing argument");
381 for (def = test->regs; def->name; def++) {
382 def->ty->release(®(in, def->i)->v);
383 if (def->i < state->nrout)
384 def->ty->release(®(out, def->i)->v);
387 for (test = tests; test->name; test++)
388 if (!strcmp(p, test->name)) goto found_test;
389 bail("unknown test `%s'", p);
391 for (def = test->regs; def->name; def++) {
392 reg = REG(in, def->i);
393 reg->f = 0; def->ty->init(®->v);
394 if (def->i < state->nrout) {
395 reg = REG(out, def->i);
396 reg->f = 0; def->ty->init(®->v);
402 if (!test) bail("no current test");
403 for (def = test->regs; def->name; def++)
404 if (!strcmp(q, def->name)) goto found_reg;
405 bail("unknown register `%s' in test `%s'", q, test->name);
407 reg = REG(in, def->i);
408 if (reg->f®F_LIVE) bail("register `%s' already set", def->name);
409 def->ty->parse(®->v, p); reg->f |= REGF_LIVE;
413 total = state->win + state->lose;
415 printf("PASSED all %d test%s\n", state->win, total == 1 ? "" : "s");
417 printf("FAILED %d of %d test%s\n", state->lose, total,
418 total == 1 ? "" : "s");
419 return state->lose ? 1 : 0;