X-Git-Url: https://git.distorted.org.uk/~mdw/secnet/blobdiff_plain/b7a5ecfcbac18c56d0b044975f6ed6835dd86ab4..refs/heads/mdw/xdh:/x25519.c diff --git a/x25519.c b/x25519.c index 8e9649e..85c1187 100644 --- a/x25519.c +++ b/x25519.c @@ -7,7 +7,26 @@ /*----- Licensing notice --------------------------------------------------* * - * This file is part of Catacomb. + * This file is part of secnet. + * See README for full list of copyright holders. + * + * secnet is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version d of the License, or + * (at your option) any later version. + * + * secnet 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 + * version 3 along with secnet; if not, see + * https://www.gnu.org/licenses/gpl.html. + * + * This file was originally part of Catacomb, but has been automatically + * modified for incorporation into secnet: see `import-catacomb-crypto' + * for details. * * Catacomb is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as @@ -27,7 +46,7 @@ /*----- Header files ------------------------------------------------------*/ -#include +#include "fake-mLib-bits.h" #include "montladder.h" #include "f25519.h" @@ -39,24 +58,6 @@ const octet x25519_base[32] = { 9, 0, /* ... */ }; #define A0 121665 -/*----- Key fetching ------------------------------------------------------*/ - -const key_fetchdef x25519_pubfetch[] = { - { "pub", offsetof(x25519_pub, pub), KENC_BINARY, 0 }, - { 0, 0, 0, 0 } -}; - -static const key_fetchdef priv[] = { - { "priv", offsetof(x25519_priv, priv), KENC_BINARY, 0 }, - { 0, 0, 0, 0 } -}; - -const key_fetchdef x25519_privfetch[] = { - { "pub", offsetof(x25519_priv, pub), KENC_BINARY, 0 }, - { "private", 0, KENC_STRUCT, priv }, - { 0, 0, 0, 0 } -}; - /*----- Main code ---------------------------------------------------------*/ /* --- @x25519@ --- * @@ -104,93 +105,4 @@ void x25519(octet zz[X25519_OUTSZ], f25519_store(zz, &x1); } -/*----- Test rig ----------------------------------------------------------*/ - -#ifdef TEST_RIG - -#include -#include - -#include -#include - -static int vrf_x25519(dstr dv[]) -{ - dstr dz = DSTR_INIT; - int ok = 1; - - if (dv[0].len != 32) die(1, "bad key length"); - if (dv[1].len != 32) die(1, "bad public length"); - if (dv[2].len != 32) die(1, "bad result length"); - - dstr_ensure(&dz, 32); dz.len = 32; - x25519((octet *)dz.buf, - (const octet *)dv[0].buf, - (const octet *)dv[1].buf); - if (memcmp(dz.buf, dv[2].buf, 32) != 0) { - ok = 0; - fprintf(stderr, "failed!"); - fprintf(stderr, "\n\t k = "); type_hex.dump(&dv[0], stderr); - fprintf(stderr, "\n\t p = "); type_hex.dump(&dv[1], stderr); - fprintf(stderr, "\n\twant = "); type_hex.dump(&dv[2], stderr); - fprintf(stderr, "\n\tcalc = "); type_hex.dump(&dz, stderr); - fprintf(stderr, "\n"); - } - - dstr_destroy(&dz); - return (ok); -} - -static int vrf_mct(dstr dv[]) -{ - octet b0[32], b1[32], *k = b0, *x = b1, *t; - unsigned long i, niter; - dstr d = DSTR_INIT; - int ok = 1; - - if (dv[0].len != sizeof(b0)) { fprintf(stderr, "k len\n"); exit(2); } - if (dv[1].len != sizeof(b1)) { fprintf(stderr, "x len\n"); exit(2); } - if (dv[3].len != sizeof(b0)) { fprintf(stderr, "result len\n"); exit(2); } - memcpy(b0, dv[0].buf, sizeof(b0)); - memcpy(b1, dv[1].buf, sizeof(b1)); - niter = *(unsigned long *)dv[2].buf; - dstr_ensure(&d, 32); d.len = 32; t = (octet *)d.buf; - - for (i = 0; i < niter; i++) { - x[31] &= 0x7f; - x25519(x, k, x); - t = x; x = k; k = t; - } - memcpy(d.buf, k, d.len); - - if (memcmp(d.buf, dv[3].buf, d.len) != 0) { - ok = 0; - fprintf(stderr, "failed..."); - fprintf(stderr, "\n\tinitial k = "); type_hex.dump(&dv[0], stderr); - fprintf(stderr, "\n\tinitial x = "); type_hex.dump(&dv[1], stderr); - fprintf(stderr, "\n\titerations = %lu", niter); - fprintf(stderr, "\n\texpected = "); type_hex.dump(&dv[3], stderr); - fprintf(stderr, "\n\tcalculated = "); type_hex.dump(&d, stderr); - fputc('\n', stderr); - } - - dstr_destroy(&d); - return (ok); -} - -static test_chunk tests[] = { - { "x25519", vrf_x25519, { &type_hex, &type_hex, &type_hex } }, - { "x25519-mct", vrf_mct, - { &type_hex, &type_hex, &type_ulong, &type_hex } }, - { 0, 0, { 0 } } -}; - -int main(int argc, char *argv[]) -{ - test_run(argc, argv, tests, SRCDIR "/t/x25519"); - return (0); -} - -#endif - /*----- That's all, folks -------------------------------------------------*/