From c71d2c9d20eeebb985e3872e4550137209e3ce4d Mon Sep 17 00:00:00 2001 From: Niels Möller <nisse@lysator.liu.se> Date: Thu, 10 Dec 2015 18:57:34 +0100 Subject: [PATCH] Fixed miscomputation bugs in secp-256r1 modulo functions. --- ecc-256.c | 22 ++++++++++++++++++---- 1 files changed, 18 insertions(+), 4 deletions(-) diff --git a/ecc-256.c b/ecc-256.c index 4fd186e..e757985 100644 --- a/ecc-256.c +++ b/ecc-256.c @@ -106,8 +106,19 @@ ecc_256_modp (const struct ecc_modulo *p, mp_limb_t *rp) assert (q2 < 2); - /* We multiply by two low limbs of p, 2^96 - 1, so we could use - shifts rather than mul. */ + /* + n-1 n-2 n-3 n-4 + +---+---+---+---+ + | u1| u0| u low | + +---+---+---+---+ + - | q1(2^96-1)| + +-------+---+ + |q2(2^.)| + +-------+ + + We multiply by two low limbs of p, 2^96 - 1, so we could use + shifts rather than mul. + */ t = mpn_submul_1 (rp + n - 4, ecc->p, 2, q1); t += cnd_sub_n (q2, rp + n - 3, ecc->p, 1); t += (-q2) & 0xffffffff; @@ -117,7 +128,10 @@ ecc_256_modp (const struct ecc_modulo *p, mp_limb_t *rp) u0 -= t; t = (u1 < cy); u1 -= cy; - u1 += cnd_add_n (t, rp + n - 4, ecc->p, 3); + + cy = cnd_add_n (t, rp + n - 4, ecc->p, 2); + u0 += cy; + u1 += (u0 < cy); u1 -= (-t) & 0xffffffff; } rp[2] = u0; @@ -204,7 +218,7 @@ ecc_256_modq (const struct ecc_modulo *q, mp_limb_t *rp) /* Conditional add of p */ u1 += t; - u2 += (t<<32) + (u0 < t); + u2 += (t<<32) + (u1 < t); t = cnd_add_n (t, rp + n - 4, ecc->q, 2); u1 += t; -- libgit2 0.23.3