mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ryan Prinz <rc4d5a@protonmail.com>
To: Thorsten Blum <blum@kernel.org>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
	"David S . Miller" <davem@davemloft.net>,
	linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org,
	Ryan Prinz <rc4d5a@protonmail.com>
Subject: [PATCH] crypto: atmel-ecc - fix missing ECDH public key validation
Date: Wed, 25 Nov 2026 23:18:08 +0000	[thread overview]
Message-ID: <20261125231745.1857056-1-rc4d5a@protonmail.com> (raw)

atmel_ecdh_compute_shared_secret() passes the peer's ECDH public point
into the ATECC508A/608A/608B secure element over I2C without checking
it first. The only check on req->src is on length
(req->src_len != ATMEL_ECC_PUBKEY_SIZE); the coordinates themselves are
never validated before atmel_i2c_init_ecdh_cmd() hands them to the
device.

crypto/ecc.c's ecc_is_pubkey_valid_partial() is the generic software
check for this operation: it rejects a point that is zero, out of
range, or not on the curve. crypto/ecdh.c's compute_shared_secret()
calls it before computing the shared secret. This driver's hardware
path has no equivalent check.

Both the generic implementation and this driver register under the
same crypto_kpp name, "ecdh-nist-p256". This driver registers at
cra_priority 300 (ATMEL_ECC_PRIORITY) versus the generic
implementation's 100, so any caller requesting "ecdh-nist-p256" gets
this unvalidated path whenever an Atmel/Microchip ECC device is bound.

net/bluetooth/smp.c allocates "ecdh-nist-p256" for LE Secure
Connections pairing and performs no validation of the remote device's
public key itself; it relies on the crypto_kpp backend
(net/bluetooth/ecdh_helper.c's compute_ecdh_secret() converts the
wire-format coordinates to the big-endian layout this backend expects,
but does not check their validity). Unlike this driver's HPRE
counterpart, nothing else in that path currently blocks the call from
reaching the unvalidated code: on a device with an Atmel/Microchip
secure element bound as its ECDH backend, a BLE peer can send an
invalid public key during pairing and force the resulting DHKey to
one of a small number of predictable values in a single pairing
attempt.

I don't have access to this hardware, so I checked whether the device
itself might reject an invalid point instead. The ATECC608A-TFLXTLS
datasheet (Microchip DS40002138A) documents the ECDH command (opcode
0x43) as combining the stored private key with the input X and Y
coordinates directly; its description includes no on-curve or range
check. The datasheet also lists "Public Key Validation Support" as a
device feature, but that refers to the Verify command's Validate and
ValidateExternal modes (see application note AN8932, "ATECC508A
Public Key Validation"), which validate a public key already written
to a device slot against a certificate chain before trusting it for
future signature verification. That is a provisioning-time operation
on a persistently stored key, unrelated to the ephemeral peer public
key passed as live input data to the ECDH command.

Fixed by validating the peer-supplied point before it reaches the I2C
command, reusing ecc_is_pubkey_valid_partial() the same way the
already-submitted HPRE fix does.

Fixes: 11105693fa05 ("crypto: atmel-ecc - introduce Microchip / Atmel ECC driver")
Signed-off-by: Ryan Prinz <rc4d5a@protonmail.com>
---
No Atmel/Microchip ECC hardware was available to reproduce this.
Verified by applying this patch to an unmodified kernel tree and
building drivers/crypto/atmel-ecc.o directly (make drivers/crypto/
atmel-ecc.o), confirming it compiles cleanly with no new warnings or
errors. The point validation reuses ecc_is_pubkey_valid_partial()
unmodified. I traced net/bluetooth/ecdh_helper.c's
compute_ecdh_secret() to confirm the src buffer it submits is
big-endian X (32 bytes) || big-endian Y (32 bytes) -- the same layout
crypto/ecc.c's own generic compute_shared_secret() expects and the
same layout atmel_i2c_init_ecdh_cmd() already consumes directly -- so
this driver's new validation step reads the same bytes the same way.
I could not test against the actual secure element; the datasheet
citation above is the basis for believing the device does not perform
this check itself.

AI Disclosure: An AI assistant was used to assist with the
analysis and testing. I have verified the vulnerability, the fix, and
the test results myself and take responsibility for this contribution,
per Documentation/process/coding-assistants.rst.

 drivers/crypto/atmel-ecc.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/drivers/crypto/atmel-ecc.c b/drivers/crypto/atmel-ecc.c
--- a/drivers/crypto/atmel-ecc.c
+++ b/drivers/crypto/atmel-ecc.c
@@ -19,7 +19,9 @@
 #include <linux/slab.h>
 #include <linux/workqueue.h>
 #include <crypto/internal/kpp.h>
+#include <crypto/ecc_curve.h>
 #include <crypto/ecdh.h>
+#include <crypto/internal/ecc.h>
 #include <crypto/kpp.h>
 #include "atmel-i2c.h"

@@ -146,6 +148,34 @@ static int atmel_ecdh_generate_public_key(struct kpp_request *req)
 	return 0;
 }

+static int atmel_ecdh_peer_pubkey_valid(struct scatterlist *src,
+					unsigned int src_len)
+{
+	const struct ecc_curve *curve = ecc_get_curve(ECC_CURVE_NIST_P256);
+	u8 pubkey_buf[ATMEL_ECC_PUBKEY_SIZE];
+	u64 x[ECC_CURVE_NIST_P256_DIGITS], y[ECC_CURVE_NIST_P256_DIGITS];
+	struct ecc_point pk;
+	size_t copied;
+
+	if (!curve)
+		return -EINVAL;
+
+	if (src_len != ATMEL_ECC_PUBKEY_SIZE)
+		return -EINVAL;
+
+	copied = sg_copy_to_buffer(src, sg_nents_for_len(src, src_len),
+				   pubkey_buf, src_len);
+	if (copied != src_len)
+		return -EINVAL;
+
+	ecc_swap_digits(pubkey_buf, x, ECC_CURVE_NIST_P256_DIGITS);
+	ecc_swap_digits(pubkey_buf + ATMEL_ECC_NIST_P256_N_SIZE, y,
+			ECC_CURVE_NIST_P256_DIGITS);
+	pk = ECC_POINT_INIT(x, y, ECC_CURVE_NIST_P256_DIGITS);
+
+	return ecc_is_pubkey_valid_partial(curve, &pk);
+}
+
 static int atmel_ecdh_compute_shared_secret(struct kpp_request *req)
 {
 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
@@ -166,6 +196,10 @@ static int atmel_ecdh_compute_shared_secret(struct kpp_request *req)
 	if (req->src_len != ATMEL_ECC_PUBKEY_SIZE)
 		return -EINVAL;

+	ret = atmel_ecdh_peer_pubkey_valid(req->src, req->src_len);
+	if (ret)
+		return ret;
+
 	gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL :
 							     GFP_ATOMIC;

-- 
2.51.2


                 reply	other threads:[~2026-11-25 23:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261125231745.1857056-1-rc4d5a@protonmail.com \
    --to=rc4d5a@protonmail.com \
    --cc=blum@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@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

all inboxes | Powered by JetHome®