mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Jonas Rebmann <jre@pengutronix.de>
To: Sascha Hauer <s.hauer@pengutronix.de>,
	 "open list:BAREBOX" <barebox@lists.infradead.org>
Cc: Lukas Wunner <lukas@wunner.de>,
	Ahmad Fatoum <a.fatoum@pengutronix.de>,
	 Jonas Rebmann <jre@pengutronix.de>,
	 Herbert Xu <herbert@gondor.apana.org.au>
Subject: [PATCH 3/4] crypto: ecdsa - Harden against integer overflows in DIV_ROUND_UP()
Date: Mon, 18 May 2026 09:56:15 +0200	[thread overview]
Message-ID: <20260518-barebox-port-ecc-v1-3-25509bd37030@pengutronix.de> (raw)
In-Reply-To: <20260518-barebox-port-ecc-v1-0-25509bd37030@pengutronix.de>

From: Lukas Wunner <lukas@wunner.de>

Herbert notes that DIV_ROUND_UP() may overflow unnecessarily if an ecdsa
implementation's ->key_size() callback returns an unusually large value.
Herbert instead suggests (for a division by 8):

  X / 8 + !!(X & 7)

Based on this formula, introduce a generic DIV_ROUND_UP_POW2() macro and
use it in lieu of DIV_ROUND_UP() for ->key_size() return values.

Additionally, use the macro in ecc_digits_from_bytes(), whose "nbytes"
parameter is a ->key_size() return value in some instances, or a
user-specified ASN.1 length in the case of ecdsa_get_signature_rs().

Link: https://lore.kernel.org/r/Z3iElsILmoSu6FuC@gondor.apana.org.au/
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
(cherry picked from linux commit b16510a530d1e6ab9683f04f8fb34f2e0f538275)
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
 crypto/ecc.c         |  4 ++--
 include/linux/math.h | 12 ++++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/crypto/ecc.c b/crypto/ecc.c
index 01003d8a38..a228ae1a66 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -1,4 +1,4 @@
-// SPDX-Comment: Origin-URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/crypto/ecc.c?id=1dcf865d3bf5bff45e93cb2410911b3428dacb78
+// SPDX-Comment: Origin-URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/crypto/ecc.c?id=b16510a530d1e6ab9683f04f8fb34f2e0f538275
 /*
  * Copyright (c) 2013, 2014 Kenneth MacKay. All rights reserved.
  * Copyright (c) 2019 Vitaly Chikunov <vt@altlinux.org>
@@ -59,7 +59,7 @@ EXPORT_SYMBOL(ecc_get_curve);
 void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
 			   u64 *out, unsigned int ndigits)
 {
-	int diff = ndigits - DIV_ROUND_UP(nbytes, sizeof(u64));
+	int diff = ndigits - DIV_ROUND_UP_POW2(nbytes, sizeof(u64));
 	unsigned int o = nbytes & 7;
 	__be64 msd = 0;
 
diff --git a/include/linux/math.h b/include/linux/math.h
index e09ecaeab6..c7198ddb68 100644
--- a/include/linux/math.h
+++ b/include/linux/math.h
@@ -33,6 +33,18 @@
  */
 #define round_down(x, y) ((x) & ~__round_mask(x, y))
 
+/**
+ * DIV_ROUND_UP_POW2 - divide and round up
+ * @n: numerator
+ * @d: denominator (must be a power of 2)
+ *
+ * Divides @n by @d and rounds up to next multiple of @d (which must be a power
+ * of 2). Avoids integer overflows that may occur with __KERNEL_DIV_ROUND_UP().
+ * Performance is roughly equivalent to __KERNEL_DIV_ROUND_UP().
+ */
+#define DIV_ROUND_UP_POW2(n, d) \
+	((n) / (d) + !!((n) & ((d) - 1)))
+
 #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
 
 #define DIV_ROUND_DOWN_ULL(ll, d) \

-- 
2.54.0.129.g3edf2eeba9




  parent reply	other threads:[~2026-05-18 10:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-18  7:56 [PATCH 0/4] crypto: Port ecc.c fixes from kernel Jonas Rebmann
2026-05-18  7:56 ` [PATCH 1/4] crypto: ecc: Add Origin-URL to document kernel revision Jonas Rebmann
2026-05-18  7:56 ` [PATCH 2/4] crypto: ecc - Fix off-by-one missing to clear most significant digit Jonas Rebmann
2026-05-18  7:56 ` Jonas Rebmann [this message]
2026-05-18  7:56 ` [PATCH 4/4] crypto: ecc - Streamline alloc_point and remove {alloc,free}_digits_space Jonas Rebmann
2026-05-18 13:25 ` [PATCH 0/4] crypto: Port ecc.c fixes from kernel Sascha Hauer

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=20260518-barebox-port-ecc-v1-3-25509bd37030@pengutronix.de \
    --to=jre@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=lukas@wunner.de \
    --cc=s.hauer@pengutronix.de \
    /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