Commit 39af4a6a authored by Niels Möller's avatar Niels Möller

Merge branch 'curve448' into master

parents bfe24f58 a5e0d463
...@@ -49,6 +49,7 @@ core ...@@ -49,6 +49,7 @@ core
/ecc-384.h /ecc-384.h
/ecc-521.h /ecc-521.h
/ecc-25519.h /ecc-25519.h
/ecc-448.h
/version.h /version.h
/nettle.aux /nettle.aux
/nettle.cp /nettle.cp
......
2019-12-13 Niels Möller <nisse@lysator.liu.se>
* Merge curve448 implementation.
2019-12-09 Niels Möller <nisse@lysator.liu.se>
* ecc-internal.h: Revert itch macro changes. We now have
h_to_a_itch <= mul_itch, mul_g_itch. Add asserts at a few places
relying on this.
(ECC_ECDSA_KEYGEN_ITCH, ECC_MAX): Delete macros.
(ECC_ECDSA_SIGN_ITCH): Revert previous change.
* ecc-448.c (ecc_mod_pow_446m224m1): Reduce scratch space from 9*n
to 6*n.
(ECC_448_INV_ITCH, ECC_448_SQRT_ITCH): Reduce accordingly.
* curve448-mul.c (curve448_mul): Reduce allocation from 14*n to 12*n.
2019-12-08 Niels Möller <nisse@lysator.liu.se>
* x86_64/ecc-curve448-modp.asm (nettle_ecc_curve448_modp): New
assembly function.
* ecc-448.c (ecc_448_modp) [HAVE_NATIVE_ecc_curve448_modp]: Use
native nettle_ecc_curve448_modp if available.
* configure.ac (asm_hogweed_optional_list): Add ecc-curve448-modp.asm.
(HAVE_NATIVE_ecc_curve448_modp): New config.h define.
2019-12-03 Niels Möller <nisse@lysator.liu.se>
* ecc-448.c (ecc_448_modp) [GMP_NUMB_BITS == 64]: New function.
2019-12-01 Niels Möller <nisse@lysator.liu.se>
Curve 448 support contributed by Daiki Ueno.
* eccdata.c (enum ecc_type): Add ECC_TYPE_EDWARDS.
(ecc_add): Support untwisted edwards curves.
(ecc_curve_init): Add curve448 parameters.
* ecc-internal.h (ECC_ECDSA_KEYGEN_ITCH): New macro.
(ECC_ECDSA_SIGN_ITCH): Increased from 12*size to 13*size.
(ECC_MAX): New macro.
* ecc-448.c: New file.
(ecc_mod_pow_2k, ecc_mod_pow_446m224m1, ecc_448_inv)
(ecc_448_zero_p, ecc_448_sqrt): New functions.
(_nettle_curve448): New curve definition.
* curve448.h (CURVE448_SIZE): New constant.
(curve448_mul_g, curve448_mul): Declare new public functions.
* ecc-eh-to-a.c (ecc_eh_to_a): Update assert to allow the curve448
Edwards curve.
* curve448-mul.c (curve448_mul): New file and function.
* curve448-mul-g.c (curve448_mul_g): New file and function.
* curve448-eh-to-x.c (curve448_eh_to_x): New file and function.
* ecc-dup-eh.c (ecc_dup_eh_untwisted): New function.
* ecc-add-ehh.c (ecc_add_ehh_untwisted): New function.
* ecc-add-eh.c (ecc_add_eh_untwisted): New function.
* ecc-point.c (ecc_point_set): Add point validation for curve448.
* ecc-point-mul.c (ecc_point_mul): Allow h_to_a_itch larger than
mul_itch.
* ecc-point-mul-g.c (ecc_point_mul_g): Allow h_to_a_itch
larger than mul_g_itch. Switch from TMP_DECL/_ALLOC/_FREE to
gmp_alloc_limbs/gmp_free_limbs.
* ecdsa-keygen.c (ecdsa_generate_keypair): Use
ECC_ECDSA_KEYGEN_ITCH.
* Makefile.in (hogweed_SOURCES): Add ecc-448.c, curve448-mul-g.c,
curve448-mul.c, and curve448-eh-to-x.c.
(HEADERS): Add curve448.h.
(ecc-448.h): New generated file.
* testsuite/testutils.c (ecc_curves): Add _nettle_curve448 to list
of tested curves.
(test_ecc_mul_a): Add curve448.
* testsuite/ecdsa-keygen-test.c (ecc_valid_p): Add curve448 support.
* testsuite/ecdh-test.c (test_main): Add tests for (non-standard)
curve448 diffie-hellman.
* testsuite/ecc-add-test.c (test_main): Update for testing of curve448.
* testsuite/ecc-dup-test.c (test_main): Likewise.
* testsuite/ecc-mul-a-test.c (test_main): Likewise. Also increase
scratch allocation for h_to_a_itch.
* testsuite/ecc-mul-g-test.c (test_main): Likewise.
* testsuite/curve448-dh-test.c: Test for curve448.
* testsuite/Makefile.in (TS_HOGWEED_SOURCES): Add curve448-dh-test.c.
* examples/ecc-benchmark.c: Add curve448 to list of benchmarked
curves.
* nettle.texinfo (Curve 25519 and Curve 448): Add docs.
2019-12-07 Niels Möller <nisse@lysator.liu.se> 2019-12-07 Niels Möller <nisse@lysator.liu.se>
* ecc-eh-to-a.c (ecc_eh_to_a): Require op == 0, delete code only * ecc-eh-to-a.c (ecc_eh_to_a): Require op == 0, delete code only
......
...@@ -175,7 +175,7 @@ hogweed_SOURCES = sexp.c sexp-format.c \ ...@@ -175,7 +175,7 @@ hogweed_SOURCES = sexp.c sexp-format.c \
ecc-mod.c ecc-mod-inv.c \ ecc-mod.c ecc-mod-inv.c \
ecc-mod-arith.c ecc-pp1-redc.c ecc-pm1-redc.c \ ecc-mod-arith.c ecc-pp1-redc.c ecc-pm1-redc.c \
ecc-192.c ecc-224.c ecc-256.c ecc-384.c ecc-521.c \ ecc-192.c ecc-224.c ecc-256.c ecc-384.c ecc-521.c \
ecc-25519.c \ ecc-25519.c ecc-448.c \
ecc-size.c ecc-j-to-a.c ecc-a-to-j.c \ ecc-size.c ecc-j-to-a.c ecc-a-to-j.c \
ecc-dup-jj.c ecc-add-jja.c ecc-add-jjj.c \ ecc-dup-jj.c ecc-add-jja.c ecc-add-jjj.c \
ecc-eh-to-a.c \ ecc-eh-to-a.c \
...@@ -186,6 +186,7 @@ hogweed_SOURCES = sexp.c sexp-format.c \ ...@@ -186,6 +186,7 @@ hogweed_SOURCES = sexp.c sexp-format.c \
ecc-ecdsa-sign.c ecdsa-sign.c \ ecc-ecdsa-sign.c ecdsa-sign.c \
ecc-ecdsa-verify.c ecdsa-verify.c ecdsa-keygen.c \ ecc-ecdsa-verify.c ecdsa-verify.c ecdsa-keygen.c \
curve25519-mul-g.c curve25519-mul.c curve25519-eh-to-x.c \ curve25519-mul-g.c curve25519-mul.c curve25519-eh-to-x.c \
curve448-mul-g.c curve448-mul.c curve448-eh-to-x.c \
eddsa-compress.c eddsa-decompress.c eddsa-expand.c \ eddsa-compress.c eddsa-decompress.c eddsa-expand.c \
eddsa-hash.c eddsa-pubkey.c eddsa-sign.c eddsa-verify.c \ eddsa-hash.c eddsa-pubkey.c eddsa-sign.c eddsa-verify.c \
ed25519-sha512-pubkey.c \ ed25519-sha512-pubkey.c \
...@@ -196,7 +197,7 @@ OPT_SOURCES = fat-x86_64.c fat-arm.c mini-gmp.c ...@@ -196,7 +197,7 @@ OPT_SOURCES = fat-x86_64.c fat-arm.c mini-gmp.c
HEADERS = aes.h arcfour.h arctwo.h asn1.h blowfish.h \ HEADERS = aes.h arcfour.h arctwo.h asn1.h blowfish.h \
base16.h base64.h bignum.h buffer.h camellia.h cast128.h \ base16.h base64.h bignum.h buffer.h camellia.h cast128.h \
cbc.h ccm.h cfb.h chacha.h chacha-poly1305.h ctr.h \ cbc.h ccm.h cfb.h chacha.h chacha-poly1305.h ctr.h \
curve25519.h des.h dsa.h dsa-compat.h eax.h \ curve25519.h curve448.h des.h dsa.h dsa-compat.h eax.h \
ecc-curve.h ecc.h ecdsa.h eddsa.h \ ecc-curve.h ecc.h ecdsa.h eddsa.h \
gcm.h gost28147.h gosthash94.h hmac.h \ gcm.h gost28147.h gosthash94.h hmac.h \
knuth-lfib.h hkdf.h \ knuth-lfib.h hkdf.h \
...@@ -387,6 +388,9 @@ ecc-521.h: eccdata.stamp ...@@ -387,6 +388,9 @@ ecc-521.h: eccdata.stamp
ecc-25519.h: eccdata.stamp ecc-25519.h: eccdata.stamp
./eccdata$(EXEEXT_FOR_BUILD) 255 11 6 $(NUMB_BITS) > $@T && mv $@T $@ ./eccdata$(EXEEXT_FOR_BUILD) 255 11 6 $(NUMB_BITS) > $@T && mv $@T $@
ecc-448.h: eccdata.stamp
./eccdata$(EXEEXT_FOR_BUILD) 448 38 6 $(NUMB_BITS) > $@T && mv $@T $@
eccdata.stamp: eccdata.c eccdata.stamp: eccdata.c
$(MAKE) eccdata$(EXEEXT_FOR_BUILD) $(MAKE) eccdata$(EXEEXT_FOR_BUILD)
echo stamp > eccdata.stamp echo stamp > eccdata.stamp
...@@ -397,6 +401,7 @@ ecc-256.$(OBJEXT): ecc-256.h ...@@ -397,6 +401,7 @@ ecc-256.$(OBJEXT): ecc-256.h
ecc-384.$(OBJEXT): ecc-384.h ecc-384.$(OBJEXT): ecc-384.h
ecc-521.$(OBJEXT): ecc-521.h ecc-521.$(OBJEXT): ecc-521.h
ecc-25519.$(OBJEXT): ecc-25519.h ecc-25519.$(OBJEXT): ecc-25519.h
ecc-448.$(OBJEXT): ecc-448.h
.asm.$(OBJEXT): $(srcdir)/asm.m4 machine.m4 config.m4 .asm.$(OBJEXT): $(srcdir)/asm.m4 machine.m4 config.m4
$(M4) $(srcdir)/asm.m4 machine.m4 config.m4 $< >$*.s $(M4) $(srcdir)/asm.m4 machine.m4 config.m4 $< >$*.s
...@@ -650,6 +655,7 @@ distcheck: dist ...@@ -650,6 +655,7 @@ distcheck: dist
clean-here: clean-here:
-rm -f $(TARGETS) *.$(OBJEXT) *.s *.so *.dll *.a \ -rm -f $(TARGETS) *.$(OBJEXT) *.s *.so *.dll *.a \
ecc-192.h ecc-224.h ecc-256.h ecc-384.h ecc-521.h ecc-25519.h \ ecc-192.h ecc-224.h ecc-256.h ecc-384.h ecc-521.h ecc-25519.h \
ecc-448.h \
aesdata$(EXEEXT_FOR_BUILD) \ aesdata$(EXEEXT_FOR_BUILD) \
desdata$(EXEEXT_FOR_BUILD) \ desdata$(EXEEXT_FOR_BUILD) \
twofishdata$(EXEEXT_FOR_BUILD) \ twofishdata$(EXEEXT_FOR_BUILD) \
......
...@@ -476,7 +476,8 @@ asm_nettle_optional_list="gcm-hash8.asm cpuid.asm \ ...@@ -476,7 +476,8 @@ asm_nettle_optional_list="gcm-hash8.asm cpuid.asm \
asm_hogweed_optional_list="" asm_hogweed_optional_list=""
if test "x$enable_public_key" = "xyes" ; then if test "x$enable_public_key" = "xyes" ; then
asm_hogweed_optional_list="ecc-192-modp.asm ecc-224-modp.asm \ asm_hogweed_optional_list="ecc-192-modp.asm ecc-224-modp.asm \
ecc-25519-modp.asm ecc-256-redc.asm ecc-384-modp.asm ecc-521-modp.asm" ecc-256-redc.asm ecc-384-modp.asm ecc-521-modp.asm \
ecc-25519-modp.asm ecc-curve448-modp.asm"
fi fi
OPT_NETTLE_OBJS="" OPT_NETTLE_OBJS=""
...@@ -580,6 +581,7 @@ AH_VERBATIM([HAVE_NATIVE], ...@@ -580,6 +581,7 @@ AH_VERBATIM([HAVE_NATIVE],
#undef HAVE_NATIVE_ecc_256_redc #undef HAVE_NATIVE_ecc_256_redc
#undef HAVE_NATIVE_ecc_384_modp #undef HAVE_NATIVE_ecc_384_modp
#undef HAVE_NATIVE_ecc_384_redc #undef HAVE_NATIVE_ecc_384_redc
#undef HAVE_NATIVE_ecc_curve448_modp
#undef HAVE_NATIVE_ecc_521_modp #undef HAVE_NATIVE_ecc_521_modp
#undef HAVE_NATIVE_ecc_521_redc #undef HAVE_NATIVE_ecc_521_redc
#undef HAVE_NATIVE_gcm_hash8 #undef HAVE_NATIVE_gcm_hash8
......
/* curve448-eh-to-x.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 "curve448.h"
#include "ecc.h"
#include "ecc-internal.h"
/* Transform a point on the edwards448 Edwards curve to the curve448
Montgomery curve, and return the x coordinate. */
void
curve448_eh_to_x (mp_limb_t *xp, const mp_limb_t *p, mp_limb_t *scratch)
{
#define vp (p + ecc->p.size)
#define t0 scratch
#define t1 (scratch + ecc->p.size)
#define t2 (scratch + 2*ecc->p.size)
const struct ecc_curve *ecc = &_nettle_curve448;
mp_limb_t cy;
/* If u = U/W and v = V/W are the coordinates of the point on
edwards448 we get the curve448 x coordinate as
x = v^2 / u^2 = (V/W)^2 / (U/W)^2 = (V/U)^2
*/
/* Needs a total of 9*size storage. */
ecc->p.invert (&ecc->p, t0, p, t1 + ecc->p.size);
ecc_modp_mul (ecc, t1, t0, vp);
ecc_modp_mul (ecc, t2, t1, t1);
cy = mpn_sub_n (xp, t2, ecc->p.m, ecc->p.size);
cnd_copy (cy, xp, t2, ecc->p.size);
#undef vp
#undef t0
#undef t1
#undef t2
}
/* curve448-mul-g.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 "curve448.h"
#include "ecc.h"
#include "ecc-internal.h"
/* Intended to be compatible with NaCl's crypto_scalarmult_base. */
void
curve448_mul_g (uint8_t *r, const uint8_t *n)
{
const struct ecc_curve *ecc = &_nettle_curve448;
uint8_t t[CURVE448_SIZE];
mp_limb_t *scratch;
mp_size_t itch;
#define ng scratch
#define x (scratch + 3*ecc->p.size)
#define scratch_out (scratch + 4*ecc->p.size)
memcpy (t, n, sizeof(t));
t[0] &= ~3;
t[CURVE448_SIZE-1] = (t[CURVE448_SIZE-1] & 0x7f) | 0x80;
itch = 5*ecc->p.size + ecc->mul_g_itch;
scratch = gmp_alloc_limbs (itch);
mpn_set_base256_le (x, ecc->p.size, t, CURVE448_SIZE);
ecc_mul_g_eh (ecc, ng, x, scratch_out);
curve448_eh_to_x (x, ng, scratch_out);
mpn_get_base256_le (r, CURVE448_SIZE, x, ecc->p.size);
gmp_free_limbs (scratch, itch);
#undef ng
#undef x
#undef scratch_out
}
/* curve448-mul.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 <assert.h>
#include <string.h>
#include "curve448.h"
#include "ecc.h"
#include "ecc-internal.h"
/* Intended to be compatible with NaCl's crypto_scalarmult. */
void
curve448_mul (uint8_t *q, const uint8_t *n, const uint8_t *p)
{
const struct ecc_curve *ecc = &_nettle_curve448;
mp_size_t itch;
mp_limb_t *scratch;
int i;
mp_limb_t cy;
/* FIXME: Could save some more scratch space, e.g., by letting BB
overlap C, D, and CB overlap A, D. And possibly reusing some of
x2, z2, x3, z3. */
#define x1 scratch
#define x2 (scratch + ecc->p.size)
#define z2 (scratch + 2*ecc->p.size)
#define x3 (scratch + 3*ecc->p.size)
#define z3 (scratch + 4*ecc->p.size)
#define A (scratch + 5*ecc->p.size)
#define B (scratch + 6*ecc->p.size)
#define C (scratch + 7*ecc->p.size)
#define D (scratch + 8*ecc->p.size)
#define AA (scratch + 9*ecc->p.size)
#define BB (scratch + 10*ecc->p.size)
#define E (scratch + 10*ecc->p.size) /* Overlap BB */
#define DA (scratch + 9*ecc->p.size) /* Overlap AA */
#define CB (scratch + 10*ecc->p.size) /* Overlap BB */
#define a24 39081
itch = ecc->p.size * 12;
assert (ecc->p.invert_itch + 5*ecc->p.size <= itch);
scratch = gmp_alloc_limbs (itch);
/* Note that 255 % GMP_NUMB_BITS == 0 isn't supported, so x1 always
holds at least 256 bits. */
mpn_set_base256_le (x1, ecc->p.size, p, CURVE448_SIZE);
/* Initialize, x2 = x1, z2 = 1 */
mpn_copyi (x2, x1, ecc->p.size);
z2[0] = 1;
mpn_zero (z2+1, ecc->p.size - 1);
/* Get x3, z3 from doubling. Since bit 447 is forced to 1. */
ecc_modp_add (ecc, A, x2, z2);
ecc_modp_sub (ecc, B, x2, z2);
ecc_modp_sqr (ecc, AA, A);
ecc_modp_sqr (ecc, BB, B);
ecc_modp_mul (ecc, x3, AA, BB);
ecc_modp_sub (ecc, E, AA, BB);
ecc_modp_addmul_1 (ecc, AA, E, a24);
ecc_modp_mul (ecc, z3, E, AA);
for (i = 446; i >= 2; i--)
{
int bit = (n[i/8] >> (i & 7)) & 1;
cnd_swap (bit, x2, x3, 2*ecc->p.size);
/* Formulas from RFC 7748. We compute new coordinates in
memory-address order, since mul and sqr clobbers higher
limbs. */
ecc_modp_add (ecc, A, x2, z2);
ecc_modp_sub (ecc, B, x2, z2);
ecc_modp_sqr (ecc, AA, A);
ecc_modp_sqr (ecc, BB, B);
ecc_modp_mul (ecc, x2, AA, BB);
ecc_modp_sub (ecc, E, AA, BB); /* Last use of BB */
ecc_modp_addmul_1 (ecc, AA, E, a24);
ecc_modp_add (ecc, C, x3, z3);
ecc_modp_sub (ecc, D, x3, z3);
ecc_modp_mul (ecc, z2, E, AA); /* Last use of E and AA */
ecc_modp_mul (ecc, DA, D, A); /* Last use of D, A. FIXME: could
let CB overlap. */
ecc_modp_mul (ecc, CB, C, B);
ecc_modp_add (ecc, C, DA, CB);
ecc_modp_sqr (ecc, x3, C);
ecc_modp_sub (ecc, C, DA, CB);
ecc_modp_sqr (ecc, DA, C);
ecc_modp_mul (ecc, z3, DA, x1);
/* FIXME: Could be combined with the loop's initial cnd_swap. */
cnd_swap (bit, x2, x3, 2*ecc->p.size);
}
/* Do the 2 low zero bits, just duplicating x2 */
for ( ; i >= 0; i--)
{
ecc_modp_add (ecc, A, x2, z2);
ecc_modp_sub (ecc, B, x2, z2);
ecc_modp_sqr (ecc, AA, A);
ecc_modp_sqr (ecc, BB, B);
ecc_modp_mul (ecc, x2, AA, BB);
ecc_modp_sub (ecc, E, AA, BB);
ecc_modp_addmul_1 (ecc, AA, E, a24);
ecc_modp_mul (ecc, z2, E, AA);
}
ecc->p.invert (&ecc->p, x3, z2, z3 + ecc->p.size);
ecc_modp_mul (ecc, z3, x2, x3);
cy = mpn_sub_n (x2, z3, ecc->p.m, ecc->p.size);
cnd_copy (cy, x2, z3, ecc->p.size);
mpn_get_base256_le (q, CURVE448_SIZE, x2, ecc->p.size);
gmp_free_limbs (scratch, itch);
}
/* curve448.h
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/.
*/
#ifndef NETTLE_CURVE448_H
#define NETTLE_CURVE448_H
#include "nettle-types.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Name mangling */
#define curve448_mul_g nettle_curve448_mul_g
#define curve448_mul nettle_curve448_mul
#define CURVE448_SIZE 56
void
curve448_mul_g (uint8_t *q, const uint8_t *n);
void
curve448_mul (uint8_t *q, const uint8_t *n, const uint8_t *p);
#ifdef __cplusplus
}
#endif
#endif /* NETTLE_CURVE448_H */
/* ecc-448.c
Arithmetic and tables for curve448,
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 <assert.h>
#include "ecc.h"
#include "ecc-internal.h"
#define USE_REDC 0
#include "ecc-448.h"
#if HAVE_NATIVE_ecc_curve448_modp
#define ecc_448_modp nettle_ecc_curve448_modp
void
ecc_448_modp (const struct ecc_modulo *m, mp_limb_t *rp);
#elif GMP_NUMB_BITS == 64
static void
ecc_448_modp(const struct ecc_modulo *m, mp_limb_t *rp)
{
/* Let B = 2^64, b = 2^32 = sqrt(B).
p = B^7 - b B^3 - 1 ==> B^7 = b B^3 + 1
We use this to reduce
{r_{13}, ..., r_0} =
{r_6,...,r_0}
+ {r_{10},...,r_7}
+ 2 {r_{13},r_{12}, r_{11}} B^4
+ b {r_{10},...,r_7,r_{13},r_{12},r_{11} (mod p)
or
+----+----+----+----+----+----+----+
|r_6 |r_5 |r_4 |r_3 |r_2 |r_1 |r_0 |
+----+----+----+----+----+----+----+
|r_10|r_9 |r_8 |r_7 |
+----+----+----+----+----+----+----+
2 * |r_13|r_12|r_11|
+----+----+----+----+----+----+----+
+ b * |r_10|r_9 |r_8 |r_7 |r_13|r_12|r_11|
-------+----+----+----+----+----+----+----+
c_7 |r_6 |r_5 |r_4 |r_3 |r_2 |r_1 |r_0 |
+----+----+----+----+----+----+----+
*/
mp_limb_t c3, c4, c7;
mp_limb_t *tp = rp + 7;
c4 = mpn_add_n (rp, rp, rp + 7, 4);
c7 = mpn_addmul_1 (rp + 4, rp + 11, 3, 2);
c3 = mpn_addmul_1 (rp, rp + 11, 3, (mp_limb_t) 1 << 32);
c7 += mpn_addmul_1 (rp + 3, rp + 7, 4, (mp_limb_t) 1 << 32);
tp[0] = c7;
tp[1] = tp[2] = 0;
tp[3] = c3 + (c7 << 32);
tp[4] = c4 + (c7 >> 32) + (tp[3] < c3);
tp[5] = tp[6] = 0;
c7 = mpn_add_n (rp, rp, tp, 7);
c7 = cnd_add_n (c7, rp, m->B, 7);
assert (c7 == 0);
}
#else
#define ecc_448_modp ecc_mod
#endif
/* Needs 2*ecc->size limbs at rp, and 2*ecc->size additional limbs of
scratch space. No overlap allowed. */
static void
ecc_mod_pow_2k (const struct ecc_modulo *m,
mp_limb_t *rp, const mp_limb_t *xp,
unsigned k, mp_limb_t *tp)
{
if (k & 1)
{
ecc_mod_sqr (m, rp, xp);
k--;
}
else
{
ecc_mod_sqr (m, tp, xp);
ecc_mod_sqr (m, rp, tp);
k -= 2;
}
while (k > 0)
{
ecc_mod_sqr (m, tp, rp);
ecc_mod_sqr (m, rp, tp);
k -= 2;
}
}
/* Computes a^{(p-3)/4} = a^{2^446-2^222-1} mod m. Needs 6 * 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)
{
#define t0 scratch
#define t1 (scratch + 2*ECC_LIMB_SIZE)
#define t2 (scratch + 4*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_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_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_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} */
ecc_mod_mul (p, rp, t0, t1); /* a^{2^446-2^222-1} */
#undef t0
#undef t1
#undef t2
}
/* Needs 6*ECC_LIMB_SIZE scratch space. */
#define ECC_448_INV_ITCH (6*ECC_LIMB_SIZE)
static void ecc_448_inv (const struct ecc_modulo *p,
mp_limb_t *rp, const mp_limb_t *ap,
mp_limb_t *scratch)
{
#define t0 scratch
ecc_mod_pow_446m224m1 (p, rp, ap, scratch); /* a^{2^446-2^222-1} */
ecc_mod_sqr (p, t0, rp); /* a^{2^447-2^223-2} */
ecc_mod_sqr (p, rp, t0); /* a^{2^448-2^224-4} */
ecc_mod_mul (p, t0, ap, rp); /* a^{2^448-2^224-3} */
mpn_copyi (rp, t0, ECC_LIMB_SIZE); /* FIXME: Eliminate copy? */
#undef t0
}
/* First, do a canonical reduction, then check if zero */
static int
ecc_448_zero_p (const struct ecc_modulo *p, mp_limb_t *xp)
{
mp_limb_t cy;
mp_limb_t w;
mp_size_t i;
cy = mpn_sub_n (xp, xp, p->m, ECC_LIMB_SIZE);
cnd_add_n (cy, xp, p->m, ECC_LIMB_SIZE);
for (i = 0, w = 0; i < ECC_LIMB_SIZE; i++)
w |= xp[i];
return w == 0;
}