linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Berger <stefanb@linux.ibm.com>
To: herbert@gondor.apana.org.au, davem@davemloft.net,
	linux-crypto@vger.kernel.org
Cc: linux-kernel@vger.kernel.org,
	Stefan Berger <stefanb@linux.ibm.com>,
	stable@vger.kernel.org
Subject: [PATCH] ecdsa: Fix incorrect usage of vli_cmp
Date: Thu, 21 Apr 2022 14:57:40 -0400	[thread overview]
Message-ID: <20220421185740.799271-1-stefanb@linux.ibm.com> (raw)

Fix incorrect usage of vli_cmp when calculating the value of res.x. For
signature verification to succeed, res.x must be the same as the r
component of the signature which is in the range of [1..n-1] with 'n'
being the order of the curve. Therefore, when res.x equals n calculate
res.x = res.x - n as well. Signature verification could have previously
unnecessarily failed in extremely rare cases.

Fixes: 4e6602916bc6 ("crypto: ecdsa - Add support for ECDSA signature verification")
Cc: <stable@vger.kernel.org>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 crypto/ecdsa.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/crypto/ecdsa.c b/crypto/ecdsa.c
index b3a8a6b572ba..674ab9275366 100644
--- a/crypto/ecdsa.c
+++ b/crypto/ecdsa.c
@@ -120,8 +120,8 @@ static int _ecdsa_verify(struct ecc_ctx *ctx, const u64 *hash, const u64 *r, con
 	/* res = u1*G + u2 * pub_key */
 	ecc_point_mult_shamir(&res, u1, &curve->g, u2, &ctx->pub_key, curve);
 
-	/* res.x = res.x mod n (if res.x > order) */
-	if (unlikely(vli_cmp(res.x, curve->n, ndigits) == 1))
+	/* res.x = res.x mod n (if res.x >= order) */
+	if (unlikely(vli_cmp(res.x, curve->n, ndigits) >= 0))
 		/* faster alternative for NIST p384, p256 & p192 */
 		vli_sub(res.x, res.x, curve->n, ndigits);
 
-- 
2.34.1


             reply	other threads:[~2022-04-21 18:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-21 18:57 Stefan Berger [this message]
2022-04-26 17:53 ` [PATCH] ecdsa: Fix incorrect usage of vli_cmp Stefan Berger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220421185740.799271-1-stefanb@linux.ibm.com \
    --to=stefanb@linux.ibm.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).