|
|
b507f4 |
From 932ea29845da1ae350d9c056cb2cb0379a66d642 Mon Sep 17 00:00:00 2001
|
|
|
b507f4 |
From: Daiki Ueno <dueno@redhat.com>
|
|
|
b507f4 |
Date: Tue, 30 Mar 2021 09:22:47 +0200
|
|
|
b507f4 |
Subject: [PATCH] Port upstream hardening of EC scaler multiplication
|
|
|
b507f4 |
|
|
|
b507f4 |
Some internal functions used in point multiplications are known to
|
|
|
b507f4 |
misbehave if the scaler is out-of-range. This performs canonical
|
|
|
b507f4 |
reduction on scalers, before point multiplication.
|
|
|
b507f4 |
|
|
|
b507f4 |
Signed-off-by: Daiki Ueno <dueno@redhat.com>
|
|
|
b507f4 |
---
|
|
|
b507f4 |
ecc-ecdsa-sign.c | 7 +++++--
|
|
|
b507f4 |
ecc-ecdsa-verify.c | 14 ++++++++++++--
|
|
|
b507f4 |
eddsa-hash.c | 9 +++++++--
|
|
|
b507f4 |
3 files changed, 24 insertions(+), 6 deletions(-)
|
|
|
b507f4 |
|
|
|
b507f4 |
diff --git a/ecc-ecdsa-sign.c b/ecc-ecdsa-sign.c
|
|
|
b507f4 |
index 3b9e9cc1..45062528 100644
|
|
|
b507f4 |
--- a/ecc-ecdsa-sign.c
|
|
|
b507f4 |
+++ b/ecc-ecdsa-sign.c
|
|
|
b507f4 |
@@ -62,6 +62,8 @@ ecc_ecdsa_sign (const struct ecc_curve *ecc,
|
|
|
b507f4 |
mp_limb_t *rp, mp_limb_t *sp,
|
|
|
b507f4 |
mp_limb_t *scratch)
|
|
|
b507f4 |
{
|
|
|
b507f4 |
+ mp_limb_t cy;
|
|
|
b507f4 |
+
|
|
|
b507f4 |
#define P scratch
|
|
|
b507f4 |
#define kinv scratch /* Needs 5*ecc->p.size for computation */
|
|
|
b507f4 |
#define hp (scratch + ecc->p.size) /* NOTE: ecc->p.size + 1 limbs! */
|
|
|
b507f4 |
@@ -91,8 +93,9 @@ ecc_ecdsa_sign (const struct ecc_curve *ecc,
|
|
|
b507f4 |
ecc_modq_mul (ecc, tp, zp, rp);
|
|
|
b507f4 |
ecc_modq_add (ecc, hp, hp, tp);
|
|
|
b507f4 |
ecc_modq_mul (ecc, tp, hp, kinv);
|
|
|
b507f4 |
-
|
|
|
b507f4 |
- mpn_copyi (sp, tp, ecc->p.size);
|
|
|
b507f4 |
+ /* Ensure canonical reduction. */
|
|
|
b507f4 |
+ cy = mpn_sub_n (sp, tp, ecc->q.m, ecc->q.size);
|
|
|
b507f4 |
+ cnd_copy (cy, sp, tp, ecc->q.size);
|
|
|
b507f4 |
#undef P
|
|
|
b507f4 |
#undef hp
|
|
|
b507f4 |
#undef kinv
|
|
|
b507f4 |
diff --git a/ecc-ecdsa-verify.c b/ecc-ecdsa-verify.c
|
|
|
b507f4 |
index d7f5b684..6b8acb07 100644
|
|
|
b507f4 |
--- a/ecc-ecdsa-verify.c
|
|
|
b507f4 |
+++ b/ecc-ecdsa-verify.c
|
|
|
b507f4 |
@@ -75,6 +75,8 @@ ecc_ecdsa_verify (const struct ecc_curve *ecc,
|
|
|
b507f4 |
const mp_limb_t *rp, const mp_limb_t *sp,
|
|
|
b507f4 |
mp_limb_t *scratch)
|
|
|
b507f4 |
{
|
|
|
b507f4 |
+ mp_limb_t cy;
|
|
|
b507f4 |
+
|
|
|
b507f4 |
/* Procedure, according to RFC 6090, "KT-I". q denotes the group
|
|
|
b507f4 |
order.
|
|
|
b507f4 |
|
|
|
b507f4 |
@@ -98,6 +100,7 @@ ecc_ecdsa_verify (const struct ecc_curve *ecc,
|
|
|
b507f4 |
#define P1 (scratch + 4*ecc->p.size)
|
|
|
b507f4 |
#define sinv (scratch)
|
|
|
b507f4 |
#define hp (scratch + ecc->p.size)
|
|
|
b507f4 |
+#define tp (scratch + 4*ecc->p.size)
|
|
|
b507f4 |
|
|
|
b507f4 |
if (! (ecdsa_in_range (ecc, rp)
|
|
|
b507f4 |
&& ecdsa_in_range (ecc, sp)))
|
|
|
b507f4 |
@@ -112,10 +115,16 @@ ecc_ecdsa_verify (const struct ecc_curve *ecc,
|
|
|
b507f4 |
|
|
|
b507f4 |
/* u1 = h / s, P1 = u1 * G */
|
|
|
b507f4 |
ecc_hash (&ecc->q, hp, length, digest);
|
|
|
b507f4 |
- ecc_modq_mul (ecc, u1, hp, sinv);
|
|
|
b507f4 |
+ ecc_modq_mul (ecc, tp, hp, sinv);
|
|
|
b507f4 |
+ /* Ensure canonical reduction. */
|
|
|
b507f4 |
+ cy = mpn_sub_n (u1, tp, ecc->q.m, ecc->q.size);
|
|
|
b507f4 |
+ cnd_copy (cy, u1, tp, ecc->q.size);
|
|
|
b507f4 |
|
|
|
b507f4 |
/* u2 = r / s, P2 = u2 * Y */
|
|
|
b507f4 |
- ecc_modq_mul (ecc, u2, rp, sinv);
|
|
|
b507f4 |
+ ecc_modq_mul (ecc, hp, rp, sinv);
|
|
|
b507f4 |
+ /* Ensure canonical reduction. */
|
|
|
b507f4 |
+ cy = mpn_sub_n (u2, hp, ecc->q.m, ecc->q.size);
|
|
|
b507f4 |
+ cnd_copy (cy, u2, hp, ecc->q.size);
|
|
|
b507f4 |
|
|
|
b507f4 |
/* Total storage: 5*ecc->p.size + ecc->mul_itch */
|
|
|
b507f4 |
ecc->mul (ecc, P2, u2, pp, u2 + ecc->p.size);
|
|
|
b507f4 |
@@ -154,4 +163,5 @@ ecc_ecdsa_verify (const struct ecc_curve *ecc,
|
|
|
b507f4 |
#undef u2
|
|
|
b507f4 |
#undef hp
|
|
|
b507f4 |
#undef u1
|
|
|
b507f4 |
+#undef tp
|
|
|
b507f4 |
}
|
|
|
b507f4 |
diff --git a/eddsa-hash.c b/eddsa-hash.c
|
|
|
b507f4 |
index 4fb79f1b..53c6fc49 100644
|
|
|
b507f4 |
--- a/eddsa-hash.c
|
|
|
b507f4 |
+++ b/eddsa-hash.c
|
|
|
b507f4 |
@@ -45,7 +45,12 @@ void
|
|
|
b507f4 |
_eddsa_hash (const struct ecc_modulo *m,
|
|
|
b507f4 |
mp_limb_t *rp, const uint8_t *digest)
|
|
|
b507f4 |
{
|
|
|
b507f4 |
+ mp_limb_t cy;
|
|
|
b507f4 |
+
|
|
|
b507f4 |
size_t nbytes = 1 + m->bit_size / 8;
|
|
|
b507f4 |
- mpn_set_base256_le (rp, 2*m->size, digest, 2*nbytes);
|
|
|
b507f4 |
- m->mod (m, rp);
|
|
|
b507f4 |
+ mpn_set_base256_le (rp + m->size, 2*m->size, digest, 2*nbytes);
|
|
|
b507f4 |
+ m->mod (m, rp + m->size);
|
|
|
b507f4 |
+ /* Ensure canonical reduction. */
|
|
|
b507f4 |
+ cy = mpn_sub_n (rp, rp + m->size, m->m, m->size);
|
|
|
b507f4 |
+ cnd_copy (cy, rp, rp + m->size, m->size);
|
|
|
b507f4 |
}
|
|
|
b507f4 |
--
|
|
|
b507f4 |
2.30.2
|
|
|
b507f4 |
|