...
 
Commits (10)
2020-01-03 Niels Möller <nisse@lysator.liu.se>
Add benchmarking of ed25519, ed448 and curve448.
* examples/hogweed-benchmark.c: (struct eddsa_ctx): New struct.
(bench_eddsa_init, bench_eddsa_sign, bench_eddsa_verify)
(bench_eddsa_clear): New functions.
(struct curve_ctx): New struct, generalizing struct curve25519_ctx.
(bench_curve_init, bench_curve_mul_g, bench_curve_mul)
(bench_curve_clear): New functions.
(struct curve25519_ctx, bench_curve25519_mul_g)
(bench_curve25519_mul, bench_curve25519): Deleted.
(alg_list): Add eddsa and curve entries.
(main): Delete call to bench_curve25519.
2020-01-02 Niels Möller <nisse@lysator.liu.se>
* eddsa-internal.h (nettle_eddsa_dom_func): New typedef.
(struct ecc_eddsa): Use function pointer to represent eddsa dom
string. To avoid calling sha512_update with empty input for
ed25519.
* ed448-shake256.c (ed448_dom): New function, calling
sha3_256_update with the magic dom prefix.
(_nettle_ed448_shake256): Point to it.
* ed25519-sha512.c (_nettle_ed25519_sha512): Add do-nothing dom function.
* eddsa-sign.c (_eddsa_sign): Update to use dom function pointer.
* eddsa-verify.c (_eddsa_verify): Likewise.
* eddsa-internal.h (struct ecc_eddsa): Add magic dom string,
needed for ed448.
* ed25519-sha512.c (_nettle_ed25519_sha512): Empty dom string.
* ed448-shake256.c (_nettle_ed448_shake256): New file and
parameter struct.
* eddsa-hash.c (_eddsa_hash): Add digest_size as input argument.
Handle ed448 digests with two extra bytes. Update callers.
* eddsa-verify.c (_eddsa_verify): Hash dom string.
* eddsa-sign.c (_eddsa_sign_itch): Assert that
_eddsa_compress_itch isn't too large.
(_eddsa_sign): New argument k1, with the hash prefix. Add hashing
of this prefix and the dom string. Update callers. Fix final
reduction, it's different for ed25519, with q slightly larger than
a power of two, and ed448, with q slightly smaller.
* eddsa-pubkey.c (_eddsa_public_key_itch): Assert that
_eddsa_compress_itch isn't too large.
Implementation of ed448-shake256, based on patch by Daiki Ueno.
* ed448-shake256-pubkey.c (ed448_shake256_public_key): New file
and function.
* ed448-shake256-sign.c (ed448_shake256_sign): New file and function.
* ed448-shake256-verify.c (ed448_shake256_verify): New file and function.
* Makefile.in (hogweed_SOURCES): Add new ed448 files.
* testsuite/eddsa-verify-test.c (test_ed448): New function.
(test_main): New ed448 tests.
* testsuite/eddsa-sign-test.c (test_ed448_sign): New function.
(test_main): New ed448 tests.
* testsuite/ed448-test.c: New tests.
* testsuite/Makefile.in (TS_HOGWEED_SOURCES): Add ed448-test.c.
* nettle.texinfo (Curve 25519 and Curve 448): Document ed448.
2020-01-01 Niels Möller <nisse@lysator.liu.se>
* ecc-448.c (ecc_mod_pow_2kp1): New function.
(ecc_mod_pow_446m224m1): Reduce scratch usage from 6*n to 5*n, at
the cost of one copy operation. Also use ecc_mod_pow_2kp1 where
applicable.
(ECC_448_INV_ITCH): Reduce to 5*ECC_LIMB_SIZE.
(ECC_448_SQRT_ITCH): Reduce to 9*ECC_LIMB_SIZE.
* testsuite/eddsa-compress-test.c: Test also with curve448.
2019-12-30 Niels Möller <nisse@lysator.liu.se>
Preparation for ed448, based on patch by Daiki Ueno.
* eddsa-internal.h (struct ecc_eddsa): New struct for eddsa
parameters.
* ed25519-sha512.c (_nettle_ed25519_sha512): New parameter struct.
* eddsa-expand.c (_eddsa_expand_key): Replace input
struct nettle_hash with struct ecc_eddsa, and generalize for
ed448. Update all callers.
* eddsa-sign.c (_eddsa_sign): Likewise.
* eddsa-verify.c (_eddsa_verify): Likewise.
* eddsa-compress.c (_eddsa_compress): Store sign bit in most
significant bit of last byte, as specified by RFC 8032.
* eddsa-decompress.c (_eddsa_decompress): Corresponding update.
Also generalize to support ed448, and make validity checks
stricter.
* testsuite/eddsa-sign-test.c (test_ed25519_sign): New function.
(test_main): Use it.
* testsuite/eddsa-verify-test.c (test_ed25519): New function.
(test_main): Use it.
2019-12-28 Niels Möller <nisse@lysator.liu.se>
* bignum.h: Drop unreleted include of nettle-meta.h.
......
......@@ -191,8 +191,10 @@ hogweed_SOURCES = sexp.c sexp-format.c \
curve448-mul-g.c curve448-mul.c curve448-eh-to-x.c \
eddsa-compress.c eddsa-decompress.c eddsa-expand.c \
eddsa-hash.c eddsa-pubkey.c eddsa-sign.c eddsa-verify.c \
ed25519-sha512-pubkey.c \
ed25519-sha512-sign.c ed25519-sha512-verify.c
ed25519-sha512.c ed25519-sha512-pubkey.c \
ed25519-sha512-sign.c ed25519-sha512-verify.c \
ed448-shake256.c ed448-shake256-pubkey.c \
ed448-shake256-sign.c ed448-shake256-verify.c
OPT_SOURCES = fat-x86_64.c fat-arm.c mini-gmp.c
......
......@@ -124,37 +124,48 @@ ecc_mod_pow_2k (const struct ecc_modulo *m,
}
}
/* Computes a^{(p-3)/4} = a^{2^446-2^222-1} mod m. Needs 6 * n scratch
static void
ecc_mod_pow_2kp1 (const struct ecc_modulo *m,
mp_limb_t *rp, const mp_limb_t *xp,
unsigned k, mp_limb_t *tp)
{
ecc_mod_pow_2k (m, tp, xp, k, rp);
ecc_mod_mul (m, rp, tp, xp);
}
/* Computes a^{(p-3)/4} = a^{2^446-2^222-1} mod m. Needs 5 * n scratch
space. */
static void
ecc_mod_pow_446m224m1 (const struct ecc_modulo *p,
mp_limb_t *rp, const mp_limb_t *ap,
mp_limb_t *scratch)
{
/* Note overlap: operations writing to t0 clobber t1. */
#define t0 scratch
#define t1 (scratch + 2*ECC_LIMB_SIZE)
#define t2 (scratch + 4*ECC_LIMB_SIZE)
#define t1 (scratch + 1*ECC_LIMB_SIZE)
#define t2 (scratch + 3*ECC_LIMB_SIZE)
ecc_mod_sqr (p, rp, ap); /* a^2 */
ecc_mod_mul (p, t0, ap, rp); /* a^3 */
ecc_mod_sqr (p, rp, t0); /* a^6 */
ecc_mod_mul (p, t0, ap, rp); /* a^{2^3-1} */
ecc_mod_pow_2k (p, rp, t0, 3, t2); /* a^{2^6-2^3} */
ecc_mod_mul (p, t1, t0, rp); /* a^{2^6-1} */
ecc_mod_pow_2kp1 (p, t1, t0, 3, rp); /* a^{2^6-1} */
ecc_mod_pow_2k (p, rp, t1, 3, t2); /* a^{2^9-2^3} */
ecc_mod_mul (p, t1, t0, rp); /* a^{2^9-1} */
ecc_mod_pow_2k (p, t0, t1, 9, t2); /* a^{2^18-2^9} */
ecc_mod_mul (p, rp, t1, t0); /* a^{2^18-1} */
ecc_mod_sqr (p, t1, rp); /* a^{2^19-2} */
ecc_mod_mul (p, t0, ap, t1); /* a^{2^19-1} */
ecc_mod_pow_2k (p, t1, t0, 18, t2); /* a^{2^37-2^18} */
ecc_mod_mul (p, t0, rp, t1); /* a^{2^37-1} */
ecc_mod_pow_2k (p, t1, t0, 37, t2); /* a^{2^74-2^37} */
ecc_mod_mul (p, rp, t0, t1); /* a^{2^74-1} */
ecc_mod_mul (p, t2, t0, rp); /* a^{2^9-1} */
ecc_mod_pow_2kp1 (p, t0, t2, 9, rp); /* a^{2^18-1} */
ecc_mod_sqr (p, t1, t0); /* a^{2^19-2} */
ecc_mod_mul (p, rp, ap, t1); /* a^{2^19-1} */
ecc_mod_pow_2k (p, t1, rp, 18, t2); /* a^{2^37-2^18} */
ecc_mod_mul (p, rp, t0, t1); /* a^{2^37-1} */
mpn_copyi (t0, rp, p->size);
ecc_mod_pow_2kp1 (p, rp, t0, 37, t2); /* a^{2^74-1} */
ecc_mod_pow_2k (p, t1, rp, 37, t2); /* a^{2^111-2^37} */
ecc_mod_mul (p, rp, t0, t1); /* a^{2^111-1} */
ecc_mod_pow_2k (p, t1, rp, 111, t2); /* a^{2^222-2^111} */
ecc_mod_mul (p, t0, rp, t1); /* a^{2^222-1} */
ecc_mod_pow_2kp1 (p, t0, rp, 111, t2);/* a^{2^222-1} */
ecc_mod_sqr (p, t1, t0); /* a^{2^223-2} */
ecc_mod_mul (p, rp, ap, t1); /* a^{2^223-1} */
ecc_mod_pow_2k (p, t1, rp, 223, t2); /* a^{2^446-2^223} */
......@@ -164,8 +175,7 @@ ecc_mod_pow_446m224m1 (const struct ecc_modulo *p,
#undef t2
}
/* Needs 6*ECC_LIMB_SIZE scratch space. */
#define ECC_448_INV_ITCH (6*ECC_LIMB_SIZE)
#define ECC_448_INV_ITCH (5*ECC_LIMB_SIZE)
static void ecc_448_inv (const struct ecc_modulo *p,
mp_limb_t *rp, const mp_limb_t *ap,
......@@ -207,7 +217,7 @@ ecc_448_zero_p (const struct ecc_modulo *p, mp_limb_t *xp)
*/
/* Needs 4*n space + scratch for ecc_mod_pow_446m224m1. */
#define ECC_448_SQRT_ITCH (10*ECC_LIMB_SIZE)
#define ECC_448_SQRT_ITCH (9*ECC_LIMB_SIZE)
static int
ecc_448_sqrt(const struct ecc_modulo *p, mp_limb_t *rp,
......
......@@ -154,7 +154,7 @@ struct ecc_modulo
/* B^size mod m. Expected to have at least 32 leading zeros
(equality for secp_256r1). */
const mp_limb_t *B;
/* 2^{bit_size} - p, same value as above, but shifted. */
/* 2^{bit_size} - m, same value as above, but shifted. */
const mp_limb_t *B_shifted;
/* m +/- 1, for redc, excluding redc_size low limbs. */
const mp_limb_t *redc_mpm1;
......
......@@ -50,8 +50,8 @@ ed25519_sha512_public_key (uint8_t *pub, const uint8_t *priv)
#define k scratch
#define scratch_out (scratch + ecc->q.size)
_eddsa_expand_key (ecc, &nettle_sha512, &ctx, priv, digest, k);
sha512_init (&ctx);
_eddsa_expand_key (ecc, &_nettle_ed25519_sha512, &ctx, priv, digest, k);
_eddsa_public_key (ecc, k, pub, scratch_out);
gmp_free_limbs (scratch, itch);
......
......@@ -52,14 +52,13 @@ ed25519_sha512_sign (const uint8_t *pub,
#define scratch_out (scratch + ecc->q.size)
struct sha512_ctx ctx;
uint8_t digest[SHA512_DIGEST_SIZE];
#define k1 (digest + ED25519_KEY_SIZE)
_eddsa_expand_key (ecc, &nettle_sha512, &ctx, priv, digest, k2);
sha512_init (&ctx);
_eddsa_expand_key (ecc, &_nettle_ed25519_sha512, &ctx, priv, digest, k2);
sha512_update (&ctx, ED25519_KEY_SIZE, k1);
_eddsa_sign (ecc, &nettle_sha512, pub,
&ctx,
k2, length, msg, signature, scratch_out);
_eddsa_sign (ecc, &_nettle_ed25519_sha512, &ctx,
pub, digest + ED25519_KEY_SIZE, k2,
length, msg, signature, scratch_out);
gmp_free_limbs (scratch, itch);
#undef k1
......
......@@ -53,9 +53,11 @@ ed25519_sha512_verify (const uint8_t *pub,
int res;
#define A scratch
#define scratch_out (scratch + 3*ecc->p.size)
sha512_init (&ctx);
res = (_eddsa_decompress (ecc,
A, pub, scratch_out)
&& _eddsa_verify (ecc, &nettle_sha512,
&& _eddsa_verify (ecc, &_nettle_ed25519_sha512,
pub, A, &ctx,
length, msg, signature,
scratch_out));
......
/* ed25519-sha512.c
Copyright (C) 2019 Niels Möller
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle 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 copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "eddsa-internal.h"
#include "nettle-types.h"
#include "sha2.h"
static nettle_eddsa_dom_func ed25519_dom;
static void ed25519_dom(void *ctx UNUSED) {}
const struct ecc_eddsa _nettle_ed25519_sha512 =
{
(nettle_hash_update_func *) sha512_update,
(nettle_hash_digest_func *) sha512_digest,
ed25519_dom,
~(mp_limb_t) 7,
(mp_limb_t) 1 << (254 % GMP_NUMB_BITS),
};
/* ed448-shake256-pubkey.c
Copyright (C) 2017 Daiki Ueno
Copyright (C) 2017 Red Hat, Inc.
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle 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 copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "eddsa.h"
#include "ecc-internal.h"
#include "eddsa-internal.h"
#include "sha3.h"
void
ed448_shake256_public_key (uint8_t *pub, const uint8_t *priv)
{
const struct ecc_curve *ecc = &_nettle_curve448;
struct sha3_256_ctx ctx;
uint8_t digest[ED448_SIGNATURE_SIZE];
mp_size_t itch = ecc->q.size + _eddsa_public_key_itch (ecc);
mp_limb_t *scratch = gmp_alloc_limbs (itch);
#define k scratch
#define scratch_out (scratch + ecc->q.size)
sha3_256_init (&ctx);
_eddsa_expand_key (ecc, &_nettle_ed448_shake256, &ctx, priv, digest, k);
_eddsa_public_key (ecc, k, pub, scratch_out);
gmp_free_limbs (scratch, itch);
#undef k
#undef scratch_out
}
/* ed448-shake256-sign.c
Copyright (C) 2017 Daiki Ueno
Copyright (C) 2017 Red Hat, Inc.
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle 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 copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "eddsa.h"
#include "ecc-internal.h"
#include "eddsa-internal.h"
#include "sha3.h"
void
ed448_shake256_sign (const uint8_t *pub,
const uint8_t *priv,
size_t length, const uint8_t *msg,
uint8_t *signature)
{
const struct ecc_curve *ecc = &_nettle_curve448;
const struct ecc_eddsa *eddsa = &_nettle_ed448_shake256;
mp_size_t itch = ecc->q.size + _eddsa_sign_itch (ecc);
mp_limb_t *scratch = gmp_alloc_limbs (itch);
#define k2 scratch
#define scratch_out (scratch + ecc->q.size)
struct sha3_256_ctx ctx;
uint8_t digest[ED448_SIGNATURE_SIZE];
sha3_256_init (&ctx);
_eddsa_expand_key (ecc, eddsa, &ctx, priv, digest, k2);
_eddsa_sign (ecc, eddsa, &ctx,
pub, digest + ED448_KEY_SIZE, k2,
length, msg, signature, scratch_out);
gmp_free_limbs (scratch, itch);
#undef k1
#undef k2
#undef scratch_out
}
/* ed448-shake256-verify.c
Copyright (C) 2017 Daiki Ueno
Copyright (C) 2017 Red Hat, Inc.
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle 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 copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "eddsa.h"
#include "ecc-internal.h"
#include "eddsa-internal.h"
#include "sha3.h"
int
ed448_shake256_verify (const uint8_t *pub,
size_t length, const uint8_t *msg,
const uint8_t *signature)
{
const struct ecc_curve *ecc = &_nettle_curve448;
mp_size_t itch = 3*ecc->p.size + _eddsa_verify_itch (ecc);
mp_limb_t *scratch = gmp_alloc_limbs (itch);
struct sha3_256_ctx ctx;
int res;
#define A scratch
#define scratch_out (scratch + 3*ecc->p.size)
sha3_256_init (&ctx);
res = (_eddsa_decompress (ecc,
A, pub, scratch_out)
&& _eddsa_verify (ecc, &_nettle_ed448_shake256, pub, A,
&ctx,
length, msg, signature,
scratch_out));
gmp_free_limbs (scratch, itch);
return res;
#undef A
#undef scratch_out
}
/* ed448-shake256.c
Copyright (C) 2019 Niels Möller
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle 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 copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "eddsa-internal.h"
#include "nettle-types.h"
#include "sha3.h"
#define DOM_SIZE 10
static nettle_eddsa_dom_func ed448_dom;
static void
ed448_dom(void *ctx)
{
static const uint8_t dom[DOM_SIZE] =
{ 'S', 'i', 'g', 'E', 'd', '4', '4', '8', 0, 0};
sha3_256_update (ctx, DOM_SIZE, dom);
}
const struct ecc_eddsa _nettle_ed448_shake256 =
{
(nettle_hash_update_func *) sha3_256_update,
(nettle_hash_digest_func *) sha3_256_shake,
ed448_dom,
~(mp_limb_t) 3,
(mp_limb_t) 1 << (447 % GMP_NUMB_BITS),
};
......@@ -53,11 +53,11 @@ _eddsa_compress (const struct ecc_curve *ecc, uint8_t *r, mp_limb_t *p,
#define yp (scratch + ecc->p.size)
#define scratch_out (scratch + 2*ecc->p.size)
size_t nbytes = 1 + ecc->p.bit_size / 8;
ecc->h_to_a (ecc, 0, xp, p, scratch_out);
/* Encoding is the y coordinate and an appended "sign" bit, which is
the low bit of x. Bit order is not specified explicitly, but for
little-endian encoding, it makes most sense to append the bit
after the most significant bit of y. */
mpn_get_base256_le (r, 1 + ecc->p.bit_size / 8, yp, ecc->p.size);
r[ecc->p.bit_size / 8] += (xp[0] & 1) << (ecc->p.bit_size & 7);
the low bit of x. The sign bit is stored as the most significant
bit of the last byte. */
mpn_get_base256_le (r, nbytes, yp, ecc->p.size);
r[nbytes - 1] += (xp[0] & 1) << 7;
}
......@@ -33,6 +33,8 @@
# include "config.h"
#endif
#include <assert.h>
#include "eddsa.h"
#include "eddsa-internal.h"
......@@ -51,6 +53,8 @@ _eddsa_decompress (const struct ecc_curve *ecc, mp_limb_t *p,
mp_limb_t *scratch)
{
mp_limb_t sign, cy;
mp_size_t nlimbs;
size_t nbytes;
int res;
#define xp p
......@@ -62,23 +66,46 @@ _eddsa_decompress (const struct ecc_curve *ecc, mp_limb_t *p,
#define tp (scratch + 2*ecc->p.size)
#define scratch_out (scratch + 4*ecc->p.size)
sign = cp[ecc->p.bit_size / 8] >> (ecc->p.bit_size & 7);
if (sign > 1)
return 0;
mpn_set_base256_le (yp, ecc->p.size, cp, 1 + ecc->p.bit_size / 8);
/* Clear out the sign bit (if it fits) */
yp[ecc->p.size - 1] &= ~(mp_limb_t) 0
>> (ecc->p.size * GMP_NUMB_BITS - ecc->p.bit_size);
nbytes = 1 + ecc->p.bit_size / 8;
/* By RFC 8032, sign bit is always the most significant bit of the
last byte. */
sign = cp[nbytes-1] >> 7;
/* May need an extra limb. */
nlimbs = (nbytes * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS;
assert (nlimbs <= ecc->p.size + 1);
mpn_set_base256_le (scratch, nlimbs, cp, nbytes);
/* Clear out the sign bit */
scratch[nlimbs - 1] &=
((mp_limb_t) 1 << ((nbytes * 8 - 1) % GMP_NUMB_BITS)) - 1;
mpn_copyi (yp, scratch, ecc->p.size);
/* Check range. */
if (nlimbs > ecc->p.size)
res = (scratch[nlimbs - 1] == 0);
else
res = 1;
/* For a valid input, y < p, so subtraction should underflow. */
res &= mpn_sub_n (scratch, scratch, ecc->p.m, ecc->p.size);
ecc_modp_sqr (ecc, y2, yp);
ecc_modp_mul (ecc, vp, y2, ecc->b);
ecc_modp_sub (ecc, vp, vp, ecc->unit);
/* The sign is different between curve25519 and curve448. */
if (ecc->p.bit_size == 255)
ecc_modp_sub (ecc, up, ecc->unit, y2);
res = ecc->p.sqrt (&ecc->p, tp, up, vp, scratch_out);
else
ecc_modp_sub (ecc, up, y2, ecc->unit);
res &= ecc->p.sqrt (&ecc->p, tp, up, vp, scratch_out);
cy = mpn_sub_n (xp, tp, ecc->p.m, ecc->p.size);
cnd_copy (cy, xp, tp, ecc->p.size);
sign ^= xp[0] & 1;
mpn_sub_n (tp, ecc->p.m, xp, ecc->p.size);
cnd_copy (sign, xp, tp, ecc->p.size);
/* Fails if the square root is zero but (original) sign was 1 */
res &= mpn_sub_n (tp, xp, ecc->p.m, ecc->p.size);
return res;
}
......@@ -41,13 +41,12 @@
#include "ecc.h"
#include "ecc-internal.h"
#include "nettle-meta.h"
/* Expands a private key, generating the secret scalar K2 and leaving
the key K1 for nonce generation, at the end of the digest. */
void
_eddsa_expand_key (const struct ecc_curve *ecc,
const struct nettle_hash *H,
const struct ecc_eddsa *eddsa,
void *ctx,
const uint8_t *key,
uint8_t *digest,
......@@ -55,19 +54,19 @@ _eddsa_expand_key (const struct ecc_curve *ecc,
{
size_t nbytes = 1 + ecc->p.bit_size / 8;
assert (H->digest_size >= 2*nbytes);
H->init (ctx);
H->update (ctx, nbytes, key);
H->digest (ctx, 2*nbytes, digest);
mpn_set_base256_le (k2, ecc->p.size, digest, nbytes);
/* Clear low 3 bits */
k2[0] &= ~(mp_limb_t) 7;
/* Set bit number bit_size - 1 (bit 254 for curve25519) */
k2[(ecc->p.bit_size - 1) / GMP_NUMB_BITS]
|= (mp_limb_t) 1 << ((ecc->p.bit_size - 1) % GMP_NUMB_BITS);
/* Clear any higher bits. */
k2[ecc->p.size - 1] &= ~(mp_limb_t) 0
>> (GMP_NUMB_BITS * ecc->p.size - ecc->p.bit_size);
eddsa->update (ctx, nbytes, key);
eddsa->digest (ctx, 2*nbytes, digest);
/* For ed448, ignores the most significant byte. */
mpn_set_base256_le (k2, ecc->p.size, digest, (ecc->p.bit_size + 7) / 8);
/* Clear low c bits */
k2[0] &= eddsa->low_mask;
/* Clear higher bits. */
k2[ecc->p.size - 1] &= eddsa->high_bit - 1;
/* Set bit number bit_size - 1 (bit 254 for curve25519, bit 447 for
curve448) */
k2[ecc->p.size - 1] |= eddsa->high_bit;
}
/* eddsa-hash.c
Copyright (C) 2014 Niels Möller
Copyright (C) 2014, 2019 Niels Möller
Copyright (C) 2017 Daiki Ueno
Copyright (C) 2017 Red Hat, Inc.
This file is part of GNU Nettle.
......@@ -42,11 +44,35 @@
#include "ecc-internal.h"
#include "nettle-internal.h"
/* Convert hash digest to integer, and reduce modulo q, to m->size
limbs. Needs space for 2*m->size + 1 at rp. */
void
_eddsa_hash (const struct ecc_modulo *m,
mp_limb_t *rp, const uint8_t *digest)
mp_limb_t *rp, size_t digest_size, const uint8_t *digest)
{
size_t nbytes = 1 + m->bit_size / 8;
mpn_set_base256_le (rp, 2*m->size, digest, 2*nbytes);
mp_size_t nlimbs = (8*digest_size + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS;
mpn_set_base256_le (rp, nlimbs, digest, digest_size);
if (nlimbs > 2*m->size)
{
/* Special case for Ed448: reduce rp to 2*m->size limbs.
After decoding rp from a hash of size 2*rn:
rp = r2 || r1 || r0
where r0 and r1 have m->size limbs. Reduce this to:
rp = r1' || r0
where r1' has m->size limbs. */
mp_limb_t hi = rp[2*m->size];
assert (nlimbs == 2*m->size + 1);
hi = mpn_addmul_1 (rp + m->size, m->B, m->size, hi);
assert (hi <= 1);
hi = cnd_add_n (hi, rp + m->size, m->B, m->size);
assert (hi == 0);
}
m->mod (m, rp);
}
......@@ -33,7 +33,7 @@
#define NETTLE_EDDSA_INTERNAL_H
#include "nettle-types.h"
#include "nettle-meta.h"
#include "bignum.h"
#define _eddsa_compress _nettle_eddsa_compress
#define _eddsa_compress_itch _nettle_eddsa_compress_itch
......@@ -53,6 +53,22 @@
struct ecc_curve;
struct ecc_modulo;
typedef void nettle_eddsa_dom_func(void *ctx);
struct ecc_eddsa
{
/* Hash function to use */
nettle_hash_update_func *update;
nettle_hash_digest_func *digest;
nettle_eddsa_dom_func *dom;
/* For generating the secret scalar */
mp_limb_t low_mask;
mp_limb_t high_bit;
};
extern const struct ecc_eddsa _nettle_ed25519_sha512;
extern const struct ecc_eddsa _nettle_ed448_shake256;
mp_size_t
_eddsa_compress_itch (const struct ecc_curve *ecc);
void
......@@ -68,16 +84,17 @@ _eddsa_decompress (const struct ecc_curve *ecc, mp_limb_t *p,
void
_eddsa_hash (const struct ecc_modulo *m,
mp_limb_t *rp, const uint8_t *digest);
mp_limb_t *rp, size_t digest_size, const uint8_t *digest);
mp_size_t
_eddsa_sign_itch (const struct ecc_curve *ecc);
void
_eddsa_sign (const struct ecc_curve *ecc,
const struct nettle_hash *H,
const uint8_t *pub,
const struct ecc_eddsa *eddsa,
void *ctx,
const uint8_t *pub,
const uint8_t *k1,
const mp_limb_t *k2,
size_t length,
const uint8_t *msg,
......@@ -89,7 +106,7 @@ _eddsa_verify_itch (const struct ecc_curve *ecc);
int
_eddsa_verify (const struct ecc_curve *ecc,
const struct nettle_hash *H,
const struct ecc_eddsa *eddsa,
const uint8_t *pub,
const mp_limb_t *A,
void *ctx,
......@@ -100,7 +117,7 @@ _eddsa_verify (const struct ecc_curve *ecc,
void
_eddsa_expand_key (const struct ecc_curve *ecc,
const struct nettle_hash *H,
const struct ecc_eddsa *eddsa,
void *ctx,
const uint8_t *key,
uint8_t *digest,
......
......@@ -33,6 +33,8 @@
# include "config.h"
#endif
#include <assert.h>
#include "eddsa.h"
#include "eddsa-internal.h"
......@@ -41,6 +43,7 @@
mp_size_t
_eddsa_public_key_itch (const struct ecc_curve *ecc)
{
assert (_eddsa_compress_itch (ecc) <= ecc->mul_g_itch);
return 3*ecc->p.size + ecc->mul_g_itch;
}
......
......@@ -45,14 +45,16 @@
mp_size_t
_eddsa_sign_itch (const struct ecc_curve *ecc)
{
assert (_eddsa_compress_itch (ecc) <= ecc->mul_g_itch);
return 5*ecc->p.size + ecc->mul_g_itch;
}
void
_eddsa_sign (const struct ecc_curve *ecc,
const struct nettle_hash *H,
const uint8_t *pub,
const struct ecc_eddsa *eddsa,
void *ctx,
const uint8_t *pub,
const uint8_t *k1,
const mp_limb_t *k2,
size_t length,
const uint8_t *msg,
......@@ -61,6 +63,8 @@ _eddsa_sign (const struct ecc_curve *ecc,
{
mp_size_t size;
size_t nbytes;
mp_limb_t q, cy;
#define rp scratch
#define hp (scratch + size)
#define P (scratch + 2*size)
......@@ -71,34 +75,51 @@ _eddsa_sign (const struct ecc_curve *ecc,
size = ecc->p.size;
nbytes = 1 + ecc->p.bit_size / 8;
assert (H->digest_size >= 2 * nbytes);
eddsa->dom (ctx);
eddsa->update (ctx, nbytes, k1);
eddsa->update (ctx, length, msg);
eddsa->digest (ctx, 2*nbytes, hash);
_eddsa_hash (&ecc->q, rp, 2*nbytes, hash);
H->update (ctx, length, msg);
H->digest (ctx, 2*nbytes, hash);
_eddsa_hash (&ecc->q, rp, hash);
ecc->mul_g (ecc, P, rp, scratch_out);
_eddsa_compress (ecc, signature, P, scratch_out);
H->update (ctx, nbytes, signature);
H->update (ctx, nbytes, pub);
H->update (ctx, length, msg);
H->digest (ctx, 2*nbytes, hash);
_eddsa_hash (&ecc->q, hp, hash);
eddsa->dom (ctx);
eddsa->update (ctx, nbytes, signature);
eddsa->update (ctx, nbytes, pub);
eddsa->update (ctx, length, msg);
eddsa->digest (ctx, 2*nbytes, hash);
_eddsa_hash (&ecc->q, hp, 2*nbytes, hash);
ecc_modq_mul (ecc, sp, hp, k2);
ecc_modq_add (ecc, sp, sp, rp); /* FIXME: Can be plain add */
/* FIXME: Special code duplicated in ecc_25519_modq and ecc_eh_to_a.
Define a suitable method? */
if (ecc->p.bit_size == 255)
{
/* FIXME: Special code duplicated in ecc_25519_modq
Define a suitable method for canonical reduction? */
/* q is slightly larger than 2^252, underflow from below
mpn_submul_1 is unlikely. */
unsigned shift = 252 - GMP_NUMB_BITS * (ecc->p.size - 1);
q = sp[ecc->p.size-1] >> shift;
}
else
{
unsigned shift;
mp_limb_t cy;
assert (ecc->p.bit_size == 255);
shift = ecc->q.bit_size - 1 - GMP_NUMB_BITS * (ecc->p.size - 1);
cy = mpn_submul_1 (sp, ecc->q.m, ecc->p.size,
sp[ecc->p.size-1] >> shift);
assert (cy < 2);
cnd_add_n (cy, sp, ecc->q.m, ecc->p.size);
assert (ecc->p.bit_size == 448);
/* q is slightly smaller than 2^446 */
shift = 446 - GMP_NUMB_BITS * (ecc->p.size - 1);
/* Add one, then it's possible but unlikely that below
mpn_submul_1 does *not* underflow. */
q = (sp[ecc->p.size-1] >> shift) + 1;
}
cy = mpn_submul_1 (sp, ecc->q.m, ecc->p.size, q);
assert (cy < 2);
cy -= cnd_add_n (cy, sp, ecc->q.m, ecc->p.size);
assert (cy == 0);
mpn_get_base256_le (signature + nbytes, nbytes, sp, ecc->q.size);
#undef rp
#undef hp
......
......@@ -70,12 +70,13 @@ equal_h (const struct ecc_modulo *p,
mp_size_t
_eddsa_verify_itch (const struct ecc_curve *ecc)
{
assert (_eddsa_decompress_itch (ecc) <= ecc->mul_itch);
return 8*ecc->p.size + ecc->mul_itch;
}
int
_eddsa_verify (const struct ecc_curve *ecc,
const struct nettle_hash *H,
const struct ecc_eddsa *eddsa,
const uint8_t *pub,
const mp_limb_t *A,
void *ctx,
......@@ -105,12 +106,12 @@ _eddsa_verify (const struct ecc_curve *ecc,
if (mpn_cmp (sp, ecc->q.m, ecc->q.size) >= 0)
return 0;
H->init (ctx);
H->update (ctx, nbytes, signature);
H->update (ctx, nbytes, pub);
H->update (ctx, length, msg);
H->digest (ctx, 2*nbytes, hash);
_eddsa_hash (&ecc->q, hp, hash);
eddsa->dom (ctx);
eddsa->update (ctx, nbytes, signature);
eddsa->update (ctx, nbytes, pub);
eddsa->update (ctx, length, msg);
eddsa->digest (ctx, 2*nbytes, hash);
_eddsa_hash (&ecc->q, hp, 2*nbytes, hash);
/* Compute h A + R - s G, which should be the neutral point */
ecc->mul (ecc, P, hp, A, scratch_out);
......
......@@ -45,6 +45,9 @@ extern "C" {
#define ed25519_sha512_public_key nettle_ed25519_sha512_public_key
#define ed25519_sha512_sign nettle_ed25519_sha512_sign
#define ed25519_sha512_verify nettle_ed25519_sha512_verify
#define ed448_shake256_public_key nettle_ed448_shake256_public_key
#define ed448_shake256_sign nettle_ed448_shake256_sign
#define ed448_shake256_verify nettle_ed448_shake256_verify
#define ED25519_KEY_SIZE 32
#define ED25519_SIGNATURE_SIZE 64
......@@ -63,6 +66,22 @@ ed25519_sha512_verify (const uint8_t *pub,
size_t length, const uint8_t *msg,
const uint8_t *signature);
#define ED448_KEY_SIZE 57
#define ED448_SIGNATURE_SIZE 114
void
ed448_shake256_public_key (uint8_t *pub, const uint8_t *priv);
void
ed448_shake256_sign (const uint8_t *pub,
const uint8_t *priv,
size_t length, const uint8_t *msg,
uint8_t *signature);
int
ed448_shake256_verify (const uint8_t *pub,
size_t length, const uint8_t *msg,
const uint8_t *signature);
#ifdef __cplusplus
}
......
......@@ -47,7 +47,9 @@
#include "dsa.h"
#include "rsa.h"
#include "eddsa.h"
#include "curve25519.h"
#include "curve448.h"
#include "nettle-meta.h"
#include "sexp.h"
......@@ -385,7 +387,7 @@ struct ecdsa_ctx
{
struct ecc_point pub;
struct ecc_scalar key;
struct knuth_lfib_ctx rctx;
struct knuth_lfib_ctx lfib;
unsigned digest_size;
uint8_t *digest;
struct dsa_signature s;
......@@ -405,7 +407,7 @@ bench_ecdsa_init (unsigned size)
ctx = xalloc (sizeof(*ctx));
dsa_signature_init (&ctx->s);
knuth_lfib_init (&ctx->rctx, 17);
knuth_lfib_init (&ctx->lfib, 17);
switch (size)
{
......@@ -479,7 +481,7 @@ bench_ecdsa_init (unsigned size)
mpz_clear (z);
ecdsa_sign (&ctx->key,
&ctx->rctx, (nettle_random_func *) knuth_lfib_random,
&ctx->lfib, (nettle_random_func *) knuth_lfib_random,
ctx->digest_size, ctx->digest,
&ctx->s);
......@@ -494,7 +496,7 @@ bench_ecdsa_sign (void *p)
dsa_signature_init (&s);
ecdsa_sign (&ctx->key,
&ctx->rctx, (nettle_random_func *) knuth_lfib_random,
&ctx->lfib, (nettle_random_func *) knuth_lfib_random,
ctx->digest_size, ctx->digest,
&s);
dsa_signature_clear (&s);
......@@ -507,7 +509,7 @@ bench_ecdsa_verify (void *p)
if (! ecdsa_verify (&ctx->pub,
ctx->digest_size, ctx->digest,
&ctx->s))
die ("Internal error, _ecdsa_verify failed.\n");
die ("Internal error, ecdsa_verify failed.\n");
}
static void
......@@ -523,6 +525,72 @@ bench_ecdsa_clear (void *p)
free (ctx);
}
struct eddsa_ctx
{
uint8_t pub[ED448_KEY_SIZE];
uint8_t key[ED448_KEY_SIZE];
uint8_t signature[ED448_SIGNATURE_SIZE];
void (*sign)(const uint8_t *pub,
const uint8_t *priv,
size_t length, const uint8_t *msg,
uint8_t *signature);
int (*verify)(const uint8_t *pub,
size_t length, const uint8_t *msg,
const uint8_t *signature);
};
static void *
bench_eddsa_init (unsigned size)
{
struct knuth_lfib_ctx lfib;
struct eddsa_ctx *ctx;
knuth_lfib_init (&lfib, 17);
ctx = xalloc (sizeof(*ctx));
switch (size) {
case 255:
ctx->sign = ed25519_sha512_sign;
ctx->verify = ed25519_sha512_verify;
knuth_lfib_random (&lfib, ED25519_KEY_SIZE, ctx->key);
ed25519_sha512_public_key (ctx->pub, ctx->key);
break;
case 448:
ctx->sign = ed448_shake256_sign;
ctx->verify = ed448_shake256_verify;
knuth_lfib_random (&lfib, ED448_KEY_SIZE, ctx->key);
ed448_shake256_public_key (ctx->pub, ctx->key);
break;
default:
abort ();
}
ctx->sign (ctx->pub, ctx->key, 3, (const uint8_t *) "abc", ctx->signature);
return ctx;
}
static void
bench_eddsa_sign (void *p)
{
struct eddsa_ctx *ctx = p;
ctx->sign (ctx->pub, ctx->key, 3, (const uint8_t *) "abc", ctx->signature);
}
static void
bench_eddsa_verify (void *p)
{
struct eddsa_ctx *ctx = p;
if (!ctx->verify (ctx->pub, 3, (const uint8_t *) "abc", ctx->signature))
die ("Internal error, eddsa_verify failed.\n");
}
static void
bench_eddsa_clear (void *p)
{
free (p);
}
#if WITH_OPENSSL
struct openssl_rsa_ctx
{
......@@ -684,45 +752,59 @@ bench_openssl_ecdsa_clear (void *p)
}
#endif
struct curve25519_ctx
struct curve_ctx
{
uint8_t x[CURVE25519_SIZE];
uint8_t s[CURVE25519_SIZE];
uint8_t x[CURVE448_SIZE];
uint8_t s[CURVE448_SIZE];
void (*mul_g)(uint8_t *q, const uint8_t *n);
void (*mul)(uint8_t *q, const uint8_t *n, const uint8_t *p);
};
static void
bench_curve25519_mul_g (void *p)
static void *
bench_curve_init (unsigned size)
{
struct curve25519_ctx *ctx = p;
uint8_t q[CURVE25519_SIZE];
curve25519_mul_g (q, ctx->s);
struct knuth_lfib_ctx lfib;
struct curve_ctx *ctx = xalloc (sizeof (*ctx));
knuth_lfib_init (&lfib, 17);
switch (size)
{
case 255:
ctx->mul = curve25519_mul;
ctx->mul_g = curve25519_mul_g;
knuth_lfib_random (&lfib, sizeof(CURVE25519_SIZE), ctx->s);
break;
case 448:
ctx->mul = curve448_mul;
ctx->mul_g = curve448_mul_g;
knuth_lfib_random (&lfib, sizeof(CURVE448_SIZE), ctx->s);
break;
default:
abort ();
}
ctx->mul_g (ctx->x, ctx->s);
return ctx;
}
static void
bench_curve25519_mul (void *p)
bench_curve_mul_g (void *p)
{
struct curve25519_ctx *ctx = p;
uint8_t q[CURVE25519_SIZE];
curve25519_mul (q, ctx->s, ctx->x);
struct curve_ctx *ctx = p;
uint8_t q[CURVE448_SIZE];
ctx->mul_g (q, ctx->s);
}
static void
bench_curve25519 (void)
bench_curve_mul (void *p)
{
double mul_g;
double mul;
struct knuth_lfib_ctx lfib;
struct curve25519_ctx ctx;
knuth_lfib_init (&lfib, 2);
knuth_lfib_random (&lfib, sizeof(ctx.s), ctx.s);
curve25519_mul_g (ctx.x, ctx.s);
mul_g = time_function (bench_curve25519_mul_g, &ctx);
mul = time_function (bench_curve25519_mul, &ctx);
struct curve_ctx *ctx = p;
uint8_t q[CURVE448_SIZE];
ctx->mul (q, ctx->s, ctx->x);
}
printf("%16s %4d %9.4f %9.4f\n",
"curve25519", 255, 1e-3/mul_g, 1e-3/mul);
static void
bench_curve_clear (void *p)
{
free (p);
}
struct alg alg_list[] = {
......@@ -752,6 +834,10 @@ struct alg alg_list[] = {
{ "ecdsa (openssl)", 384, bench_openssl_ecdsa_init, bench_openssl_ecdsa_sign, bench_openssl_ecdsa_verify, bench_openssl_ecdsa_clear },
{ "ecdsa (openssl)", 521, bench_openssl_ecdsa_init, bench_openssl_ecdsa_sign, bench_openssl_ecdsa_verify, bench_openssl_ecdsa_clear },
#endif
{ "eddsa", 255, bench_eddsa_init, bench_eddsa_sign, bench_eddsa_verify, bench_eddsa_clear },
{ "eddsa", 448, bench_eddsa_init, bench_eddsa_sign, bench_eddsa_verify, bench_eddsa_clear },
{ "curve", 255, bench_curve_init, bench_curve_mul_g, bench_curve_mul, bench_curve_clear},
{ "curve", 448, bench_curve_init, bench_curve_mul_g, bench_curve_mul, bench_curve_clear },
};
#define numberof(x) (sizeof (x) / sizeof ((x)[0]))
......@@ -773,8 +859,5 @@ main (int argc, char **argv)
if (!filter || strstr (alg_list[i].name, filter))
bench_alg (&alg_list[i]);
if (!filter || strstr("curve25519", filter))
bench_curve25519();
return EXIT_SUCCESS;
}
......@@ -5213,6 +5213,31 @@ Verifies a message using the provided public key. Returns 1 if the
signature is valid, otherwise 0.
@end deftypefun
Nettle also provides Ed448, an EdDSA signature scheme based on an
Edwards curve equivalent to curve448.
@defvr Constant ED448_KEY_SIZE
The size of a private or public Ed448 key, 57 octets.
@end defvr
@defvr Constant ED448_SIGNATURE_SIZE
The size of an Ed448 signature, 114 octets.
@end defvr
@deftypefun void ed448_shake256_public_key (uint8_t *@var{pub}, const uint8_t *@var{priv})
Computes the public key corresponding to the given private key. Both
input and output are of size @code{ED448_KEY_SIZE}.
@end deftypefun
@deftypefun void ed448_shake256_sign (const uint8_t *@var{pub}, const uint8_t *@var{priv}, size_t @var{length}, const uint8_t *@var{msg}, uint8_t *@var{signature})
Signs a message using the provided key pair.
@end deftypefun
@deftypefun int ed448_shake256_verify (const uint8_t *@var{pub}, size_t @var{length}, const uint8_t *@var{msg}, const uint8_t *@var{signature})
Verifies a message using the provided public key. Returns 1 if the
signature is valid, otherwise 0.
@end deftypefun
@node Randomness, ASCII encoding, Public-key algorithms, Reference
@comment node-name, next, previous, up
@section Randomness
......
......@@ -286,6 +286,9 @@ eddsa-verify-test$(EXEEXT): eddsa-verify-test.$(OBJEXT)
ed25519-test$(EXEEXT): ed25519-test.$(OBJEXT)
$(LINK) ed25519-test.$(OBJEXT) $(TEST_OBJS) -o ed25519-test$(EXEEXT)
ed448-test$(EXEEXT): ed448-test.$(OBJEXT)
$(LINK) ed448-test.$(OBJEXT) $(TEST_OBJS) -o ed448-test$(EXEEXT)
sha1-huge-test$(EXEEXT): sha1-huge-test.$(OBJEXT)
$(LINK) sha1-huge-test.$(OBJEXT) $(TEST_OBJS) -o sha1-huge-test$(EXEEXT)
......
......@@ -53,7 +53,7 @@ TS_HOGWEED_SOURCES = sexp-test.c sexp-format-test.c \
ecdsa-sign-test.c ecdsa-verify-test.c \
ecdsa-keygen-test.c ecdh-test.c \
eddsa-compress-test.c eddsa-sign-test.c \
eddsa-verify-test.c ed25519-test.c
eddsa-verify-test.c ed25519-test.c ed448-test.c
TS_SOURCES = $(TS_NETTLE_SOURCES) $(TS_HOGWEED_SOURCES)
CXX_SOURCES = cxx-test.cxx
......
This diff is collapsed.
......@@ -38,8 +38,14 @@
void test_main (void)
{
const struct ecc_curve *ecc = &_nettle_curve25519;
gmp_randstate_t rands;
unsigned i;
gmp_randinit_default (rands);
for (i = 0; ecc_curves[i]; i++)
{
const struct ecc_curve *ecc = ecc_curves[i];
mp_size_t size, itch;
mpz_t zp, t;
mp_limb_t *s;
......@@ -51,7 +57,8 @@ void test_main (void)
uint8_t *c;
unsigned j;
gmp_randinit_default (rands);
if (!(ecc->p.bit_size == 255 || ecc->p.bit_size == 448))
continue;
size = ecc_size (ecc);
clen = 1 + ecc->p.bit_size / 8;
......@@ -68,6 +75,7 @@ void test_main (void)
itch = _eddsa_decompress_itch (ecc);
if (itch < ecc->mul_g_itch)
itch = ecc->mul_g_itch;
ASSERT (_eddsa_compress_itch (ecc) <= itch);
scratch = xalloc_limbs (itch);
......@@ -99,7 +107,7 @@ void test_main (void)
fprintf (stderr, ",\n 0x");
mpz_out_str (stderr, 16, y2);
fprintf (stderr, "\n");
abort ();
FAIL();
}
}
mpz_clear (t);
......@@ -109,5 +117,6 @@ void test_main (void)
free (pa1);
free (pa2);
free (scratch);
}
gmp_randclear (rands);
}
......@@ -33,10 +33,12 @@
#include "eddsa.h"
#include "eddsa-internal.h"
#include "sha3.h"
static void
test_eddsa_sign (const struct ecc_curve *ecc,
const struct nettle_hash *H,
const struct ecc_eddsa *eddsa,
void *ctx,
const struct tstring *public,
const struct tstring *private,
const struct tstring *msg,
......@@ -45,7 +47,6 @@ test_eddsa_sign (const struct ecc_curve *ecc,
mp_limb_t *scratch = xalloc_limbs (_eddsa_sign_itch (ecc));
size_t nbytes = 1 + ecc->p.bit_size / 8;
uint8_t *signature = xalloc (2*nbytes);
void *ctx = xalloc (H->context_size);
uint8_t *public_out = xalloc (nbytes);
uint8_t *digest = xalloc (2*nbytes);
const uint8_t *k1 = digest + nbytes;
......@@ -55,7 +56,7 @@ test_eddsa_sign (const struct ecc_curve *ecc,
ASSERT (private->length == nbytes);
ASSERT (ref->length == 2*nbytes);
_eddsa_expand_key (ecc, H, ctx, private->data,
_eddsa_expand_key (ecc, eddsa, ctx, private->data,
digest, k2);
_eddsa_public_key (ecc, k2, public_out, scratch);
......@@ -69,9 +70,8 @@ test_eddsa_sign (const struct ecc_curve *ecc,
fprintf (stderr, "\n");
abort ();
}
H->update (ctx, nbytes, k1);
_eddsa_sign (ecc, H, public->data, ctx, k2,
_eddsa_sign (ecc, eddsa, ctx,
public->data, k1, k2,
msg->length, msg->data, signature, scratch);
if (!MEMEQ (2*nbytes, signature, ref->data))
......@@ -95,18 +95,43 @@ test_eddsa_sign (const struct ecc_curve *ecc,
free (scratch);
free (signature);
free (ctx);
free (digest);
free (k2);
free (public_out);
}
void test_main (void)
static void
test_ed25519_sign (const struct tstring *public,
const struct tstring *private,
const struct tstring *msg,
const struct tstring *ref)
{
struct sha512_ctx ctx;
sha512_init (&ctx);
test_eddsa_sign (&_nettle_curve25519, &_nettle_ed25519_sha512, &ctx,
public, private, msg, ref);
}
static void
test_ed448_sign (const struct tstring *public,
const struct tstring *private,
const struct tstring *msg,
const struct tstring *ref)
{
struct sha3_256_ctx ctx;
sha3_256_init (&ctx);
test_eddsa_sign (&_nettle_curve448, &_nettle_ed448_shake256, &ctx,
public, private, msg, ref);
}
void
test_main (void)
{
/* Based on a few of the test vectors at
http://ed25519.cr.yp.to/python/sign.input */
test_eddsa_sign (&_nettle_curve25519, &nettle_sha512,
SHEX("d75a980182b10ab7 d54bfed3c964073a"
test_ed25519_sign (SHEX("d75a980182b10ab7 d54bfed3c964073a"
"0ee172f3daa62325 af021a68f707511a"),
SHEX("9d61b19deffd5a60 ba844af492ec2cc4"
"4449c5697b326919 703bac031cae7f60"),
......@@ -115,8 +140,7 @@ void test_main (void)
"84877f1eb8e5d974 d873e06522490155"
"5fb8821590a33bac c61e39701cf9b46b"
"d25bf5f0595bbe24 655141438e7a100b"));
test_eddsa_sign (&_nettle_curve25519, &nettle_sha512,
SHEX("3d4017c3e843895a 92b70aa74d1b7ebc"
test_ed25519_sign (SHEX("3d4017c3e843895a 92b70aa74d1b7ebc"
"9c982ccf2ec4968c c0cd55f12af4660c"),
SHEX("4ccd089b28ff96da 9db6c346ec114e0f"
"5b8a319f35aba624 da8cf6ed4fb8a6fb"),
......@@ -125,8 +149,7 @@ void test_main (void)
"a2b27b5416503f8f b3762223ebdb69da"
"085ac1e43e15996e 458f3613d0f11d8c"
"387b2eaeb4302aee b00d291612bb0c00"));
test_eddsa_sign (&_nettle_curve25519, &nettle_sha512,
SHEX("1ed506485b09a645 0be7c9337d9fe87e"
test_ed25519_sign (SHEX("1ed506485b09a645 0be7c9337d9fe87e"
"f99c96f8bd11cd63 1ca160d0fd73067e"),
SHEX("f215d34fe2d757cf f9cf5c05430994de"
"587987ce45cb0459 f61ec6c825c62259"),
......@@ -141,4 +164,59 @@ void test_main (void)
"99df1340cce54626 183144ef46887163"
"4b0a5c0033534108 e1c67c0dc99d3014"
"f01084e98c95e101 4b309b1dbb2e6704"));
/* Based on a few of the test vectors from RFC 8032 */
test_ed448_sign (SHEX("5fd7449b59b461fd 2ce787ec616ad46a"
"1da1342485a70e1f 8a0ea75d80e96778"
"edf124769b46c706 1bd6783df1e50f6c"
"d1fa1abeafe82561 80"),
SHEX("6c82a562cb808d10 d632be89c8513ebf"
"6c929f34ddfa8c9f 63c9960ef6e348a3"
"528c8a3fcc2f044e 39a3fc5b94492f8f"
"032e7549a20098f9 5b"),
SHEX(""),
SHEX("533a37f6bbe45725 1f023c0d88f976ae"
"2dfb504a843e34d2 074fd823d41a591f"
"2b233f034f628281 f2fd7a22ddd47d78"
"28c59bd0a21bfd39 80ff0d2028d4b18a"
"9df63e006c5d1c2d 345b925d8dc00b41"
"04852db99ac5c7cd da8530a113a0f4db"
"b61149f05a736326 8c71d95808ff2e65"
"2600"));
test_ed448_sign (SHEX("43ba28f430cdff45 6ae531545f7ecd0a"
"c834a55d9358c037 2bfa0c6c6798c086"
"6aea01eb00742802 b8438ea4cb82169c"
"235160627b4c3a94 80"),
SHEX("c4eab05d357007c6 32f3dbb48489924d"
"552b08fe0c353a0d 4a1f00acda2c463a"
"fbea67c5e8d2877c 5e3bc397a659949e"
"f8021e954e0a1227 4e"),
SHEX("03"),
SHEX("26b8f91727bd6289 7af15e41eb43c377"
"efb9c610d48f2335 cb0bd0087810f435"
"2541b143c4b981b7 e18f62de8ccdf633"
"fc1bf037ab7cd779 805e0dbcc0aae1cb"
"cee1afb2e027df36 bc04dcecbf154336"
"c19f0af7e0a64729 05e799f1953d2a0f"
"f3348ab21aa4adaf d1d234441cf807c0"
"3a00"));
test_ed448_sign (SHEX("df9705f58edbab80 2c7f8363cfe5560a"
"b1c6132c20a9f1dd 163483a26f8ac53a"
"39d6808bf4a1dfbd 261b099bb03b3fb5"
"0906cb28bd8a081f 00"),
SHEX("d65df341ad13e008 567688baedda8e9d"
"cdc17dc024974ea5 b4227b6530e339bf"
"f21f99e68ca6968f 3cca6dfe0fb9f4fa"
"b4fa135d5542ea3f 01"),
SHEX("bd0f6a3747cd561b dddf4640a332461a"
"4a30a12a434cd0bf 40d766d9c6d458e5"
"512204a30c17d1f5 0b5079631f64eb31"
"12182da300583546 1113718d1a5ef944"),
SHEX("554bc2480860b49e ab8532d2a533b7d5"
"78ef473eeb58c98b b2d0e1ce488a98b1"
"8dfde9b9b90775e6 7f47d4a1c3482058"
"efc9f40d2ca033a0 801b63d45b3b722e"
"f552bad3b4ccb667 da350192b61c508c"
"f7b6b5adadc2c8d9 a446ef003fb05cba"
"5f30e88e36ec2703 b349ca229c267083"
"3900"));
}