From 722bcdbcd988adf40eb0dc6163e4a4c3e7e383f3 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Thu, 26 Sep 2019 09:13:22 +0100 Subject: [PATCH] crypto test code for ed448 Signed-off-by: Ian Jackson --- ed448-test.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 ed448-test.c diff --git a/ed448-test.c b/ed448-test.c new file mode 100644 index 0000000..b240ace --- /dev/null +++ b/ed448-test.c @@ -0,0 +1,121 @@ +/* + * ed448-test.c: test harness for elliptic curve signatures + * + * (The implementations originally came with different test arrangements, + * with complicated external dependencies. This file replicates the original + * tests, but without the dependencies.) + */ +/* + * This file is Free Software. It was originally written for secnet. + * + * Copyright 2019 Mark Wooding + * + * You may redistribute secnet as a whole and/or modify it under the + * terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3, or (at your option) any + * later version. + * + * You may redistribute this file and/or modify it under the terms of + * the GNU General Public License as published by the Free Software + * Foundation; either version 2, or (at your option) any later + * version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; if not, see + * https://www.gnu.org/licenses/gpl.html. + */ + +#include + +#include "secnet.h" + +#include "sha3.h" +#include "ed448.h" + +#include "crypto-test.h" + +enum { + RSIGOUT, RAOUT = RSIGOUT, RRC = RSIGOUT, NROUT, + RA = NROUT, RPH, RCTX, RM, RSIGIN, NREG +}; + +static void test_pubkey(struct reg *out, const struct reg *in, void *ctx) +{ + allocate_bytes(&out[RAOUT].v, ED448_PUBSZ); + ed448_pubkey(out[RAOUT].v.bytes.p, + in[RA].v.bytes.p, in[RA].v.bytes.sz); +} + +static void test_sign(struct reg *out, const struct reg *in, void *ctx) +{ + octet K[ED448_PUBSZ]; + const octet *m = in[RM].v.bytes.p; size_t msz = in[RM].v.bytes.sz; + octet h[64]; + shake_ctx hctx; + + if (in[RPH].v.i) { + shake256_init(&hctx); + shake_hash(&hctx, m, msz); + shake_done(&hctx, h, sizeof(h)); + m = h; msz = sizeof(h); + } + + allocate_bytes(&out[RSIGOUT].v, ED448_SIGSZ); + ed448_pubkey(K, in[RA].v.bytes.p, in[RA].v.bytes.sz); + ed448_sign(out[RSIGOUT].v.bytes.p, + in[RA].v.bytes.p, in[RA].v.bytes.sz, K, + in[RPH].v.i, + in[RCTX].v.bytes.p, in[RCTX].v.bytes.sz, + m, msz); +} + +static void test_verify(struct reg *out, const struct reg *in, void *ctx) +{ + const octet *m = in[RM].v.bytes.p; size_t msz = in[RM].v.bytes.sz; + octet h[64]; + shake_ctx hctx; + + if (in[RPH].v.i) { + shake256_init(&hctx); + shake_hash(&hctx, m, msz); + shake_done(&hctx, h, sizeof(h)); + m = h; msz = sizeof(h); + } + + out[RRC].v.i = ed448_verify(in[RA].v.bytes.p, + in[RPH].v.i, + in[RCTX].v.bytes.p, in[RCTX].v.bytes.sz, + m, msz, in[RSIGIN].v.bytes.p); +} + +#define REG_A { "a", RA, ®ty_bytes, 0 } +#define REG_BIGA { "A", RA, ®ty_bytes, 0 } +#define REG_PH { "ph", RPH, ®ty_int, 0 } +#define REG_CTX { "ctx", RCTX, ®ty_bytes, 0 } +#define REG_M { "m", RM, ®ty_bytes, 0 } +#define REG_SIGIN { "sig", RSIGIN, ®ty_bytes, 0 } + +#define REG_SIGOUT { "sig", RSIGOUT, ®ty_bytes, 0 } +#define REG_AOUT { "A", RAOUT, ®ty_bytes, 0 } +#define REG_RC { "rc", RRC, ®ty_int, 0 } +static const struct regdef + pubkey_regs[] = { REG_A, REG_AOUT, REGLIST_END }, + sign_regs[] = { REG_A, REG_PH, REG_CTX, + REG_M, REG_SIGOUT, REGLIST_END }, + verify_regs[] = { REG_BIGA, REG_PH, REG_CTX, + REG_M, REG_SIGIN, REG_RC, REGLIST_END }; + +static const struct test tests[] = { + { "pubkey", run_test, pubkey_regs, test_pubkey }, + { "sign", run_test, sign_regs, test_sign }, + { "verify", run_test, verify_regs, test_verify }, + { 0 } +}; + +int main(void) + { return run_test_suite(NROUT, NREG, sizeof(struct reg), tests, stdin); } -- 2.30.2