...
 
Commits (4)
2020-01-02 Niels Möller <nisse@lysator.liu.se>
* 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.
......
......@@ -192,7 +192,9 @@ hogweed_SOURCES = sexp.c sexp-format.c \
eddsa-compress.c eddsa-decompress.c eddsa-expand.c \
eddsa-hash.c eddsa-pubkey.c eddsa-sign.c eddsa-verify.c \
ed25519-sha512.c ed25519-sha512-pubkey.c \
ed25519-sha512-sign.c ed25519-sha512-verify.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
......
......@@ -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;
......
......@@ -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)
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_ed25519_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
......
......@@ -42,6 +42,7 @@ const struct ecc_eddsa _nettle_ed25519_sha512 =
{
(nettle_hash_update_func *) sha512_update,
(nettle_hash_digest_func *) sha512_digest,
NULL, 0,
~(mp_limb_t) 7,
(mp_limb_t) 1 << (254 % GMP_NUMB_BITS),
};
/* 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);
}
......@@ -58,12 +58,15 @@ struct ecc_eddsa
/* Hash function to use */
nettle_hash_update_func *update;
nettle_hash_digest_func *digest;
const uint8_t *dom;
unsigned dom_size;
/* 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);
......@@ -80,7 +83,7 @@ _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);
......@@ -88,8 +91,9 @@ _eddsa_sign_itch (const struct ecc_curve *ecc);
void
_eddsa_sign (const struct ecc_curve *ecc,
const struct ecc_eddsa *eddsa,
const uint8_t *pub,
void *ctx,
const uint8_t *pub,
const uint8_t *k1,
const mp_limb_t *k2,
size_t length,
const uint8_t *msg,
......
......@@ -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 ecc_eddsa *eddsa,
const uint8_t *pub,
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,32 +75,51 @@ _eddsa_sign (const struct ecc_curve *ecc,
size = ecc->p.size;
nbytes = 1 + ecc->p.bit_size / 8;
eddsa->update (ctx, eddsa->dom_size, eddsa->dom);
eddsa->update (ctx, nbytes, k1);
eddsa->update (ctx, length, msg);
eddsa->digest (ctx, 2*nbytes, hash);
_eddsa_hash (&ecc->q, rp, hash);
_eddsa_hash (&ecc->q, rp, 2*nbytes, hash);
ecc->mul_g (ecc, P, rp, scratch_out);
_eddsa_compress (ecc, signature, P, scratch_out);
eddsa->update (ctx, eddsa->dom_size, eddsa->dom);
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, 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
......
......@@ -106,11 +106,12 @@ _eddsa_verify (const struct ecc_curve *ecc,
if (mpn_cmp (sp, ecc->q.m, ecc->q.size) >= 0)
return 0;
eddsa->update (ctx, eddsa->dom_size, eddsa->dom);
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, 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
}
......
......@@ -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.
......@@ -33,6 +33,7 @@
#include "eddsa.h"
#include "eddsa-internal.h"
#include "sha3.h"
static void
test_eddsa_sign (const struct ecc_curve *ecc,
......@@ -69,9 +70,8 @@ test_eddsa_sign (const struct ecc_curve *ecc,
fprintf (stderr, "\n");
abort ();
}
eddsa->update (ctx, nbytes, k1);
_eddsa_sign (ecc, eddsa, 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))
......@@ -113,6 +113,19 @@ test_ed25519_sign (const struct tstring *public,
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)
{
......@@ -151,4 +164,59 @@ 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"));
}
......@@ -33,6 +33,7 @@
#include "eddsa.h"
#include "eddsa-internal.h"
#include "sha3.h"
static void
test_eddsa (const struct ecc_curve *ecc,
......@@ -136,6 +137,17 @@ test_ed25519 (const uint8_t *pub,
pub, msg, signature);
}
static void
test_ed448 (const uint8_t *pub,
const struct tstring *msg,
const uint8_t *signature)
{
struct sha3_256_ctx ctx;
sha3_256_init (&ctx);
test_eddsa (&_nettle_curve448, &_nettle_ed448_shake256, &ctx,
pub, msg, signature);
}
void
test_main (void)
......@@ -167,4 +179,47 @@ test_main (void)
"99df1340cce54626 183144ef46887163"
"4b0a5c0033534108 e1c67c0dc99d3014"
"f01084e98c95e101 4b309b1dbb2e6704"));
/* Based on a few of the test vectors from RFC 8032 */
test_ed448 (H("5fd7449b59b461fd 2ce787ec616ad46a"
"1da1342485a70e1f 8a0ea75d80e96778"
"edf124769b46c706 1bd6783df1e50f6c"
"d1fa1abeafe82561 80"),
SHEX(""),
H("533a37f6bbe45725 1f023c0d88f976ae"
"2dfb504a843e34d2 074fd823d41a591f"
"2b233f034f628281 f2fd7a22ddd47d78"
"28c59bd0a21bfd39 80ff0d2028d4b18a"
"9df63e006c5d1c2d 345b925d8dc00b41"
"04852db99ac5c7cd da8530a113a0f4db"
"b61149f05a736326 8c71d95808ff2e65"
"2600"));
test_ed448 (H("43ba28f430cdff45 6ae531545f7ecd0a"
"c834a55d9358c037 2bfa0c6c6798c086"
"6aea01eb00742802 b8438ea4cb82169c"
"235160627b4c3a94 80"),
SHEX("03"),
H("26b8f91727bd6289 7af15e41eb43c377"
"efb9c610d48f2335 cb0bd0087810f435"
"2541b143c4b981b7 e18f62de8ccdf633"
"fc1bf037ab7cd779 805e0dbcc0aae1cb"
"cee1afb2e027df36 bc04dcecbf154336"
"c19f0af7e0a64729 05e799f1953d2a0f"
"f3348ab21aa4adaf d1d234441cf807c0"
"3a00"));
test_ed448 (H("df9705f58edbab80 2c7f8363cfe5560a"
"b1c6132c20a9f1dd 163483a26f8ac53a"
"39d6808bf4a1dfbd 261b099bb03b3fb5"
"0906cb28bd8a081f 00"),
SHEX("bd0f6a3747cd561b dddf4640a332461a"
"4a30a12a434cd0bf 40d766d9c6d458e5"
"512204a30c17d1f5 0b5079631f64eb31"
"12182da300583546 1113718d1a5ef944"),
H("554bc2480860b49e ab8532d2a533b7d5"
"78ef473eeb58c98b b2d0e1ce488a98b1"
"8dfde9b9b90775e6 7f47d4a1c3482058"
"efc9f40d2ca033a0 801b63d45b3b722e"
"f552bad3b4ccb667 da350192b61c508c"
"f7b6b5adadc2c8d9 a446ef003fb05cba"
"5f30e88e36ec2703 b349ca229c267083"
"3900"));
}