Skip to contentSkip to conversation

crypto: atmel-ecc - fix missing ECDH public key validation

PATCH Superseded Ryan Prinz 2 participants 4 messages Nov 25 – Nov 28

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Ryan Prinz

author
#0 lore raw

PATCH 1/1 crypto: atmel-ecc - fix missing ECDH public key validation

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")

---
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(+)
drivers/crypto/atmel-ecc.c +34 -0
@@ -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;
 
Signature · 1 line
2.51.2
maintainer
#1 lore raw

Hi Ryan,

Thanks for the patch.

Please shorten the commit message substantially and focus on the missing software validation and how this patch adds it.

Quoted 7 lines · Ryan Prinz

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.

Quoted 5 lines

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.

The function is ecdh_compute_value(). Validation then happens in crypto_ecdh_shared_secret().

Quoted 6 lines

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.

Quoted 12 lines

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.

Please drop this claim unless supported by testing or documentation.

Also, wolfSSL's public-key validation code [1] assumes that the devices validate keys internally:

/* consider key check success on HW crypto
* ex: ATECC508/608A, CryptoCell and Silabs which validate the key
* internally
*/

Thanks,
Thorsten

[1] https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/ecc.c#L12012-L12015

author
#2 lore raw

PATCH v3 1/1 crypto: atmel-ecc - fix missing ECDH public key validation

atmel_ecdh_compute_shared_secret() passes the peer's ECDH public point to the ATECC508A/608A/608B secure element without validating it; only req->src_len is checked.

Add the same check to this driver's path, before the point reaches the I2C command, reusing ecc_is_pubkey_valid_partial().

Fixes 11105693fa05 ("crypto: atmel-ecc - introduce Microchip / Atmel ECC driver")

---
v3: Shortened the commit message, moving supporting detail below this line. No functional change.

Both implementations register as crypto_kpp "ecdh-nist-p256"; this driver's higher cra_priority (300 vs. the generic implementation's
100) is why it's selected instead whenever an Atmel/Microchip ECC device is bound, so the generic validation never runs for this driver.

v2: Fixed the crypto/ecdh.c function names (ecdh_compute_value() is the registered callback; validation happens in crypto_ecdh_shared_secret()) and dropped the unsupported Bluetooth SMP reachability claim. (Thorsten Blum)

This has not been tested on hardware. The ATECC608A-TFLXTLS datasheet (Microchip DS40002138A) describes the ECDH command (opcode 0x43) combining the stored private key with the input coordinates directly, with no on-curve or range check documented. Its "Public Key Validation Support" feature is the Verify command's Validate/ValidateExternal modes (AN8932), which validate a persistently stored key against a certificate chain at provisioning time, which is a different operation from validating the ephemeral peer public key given as live input here.

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 ecdh_compute_value() expects and atmel_i2c_init_ecdh_cmd() already consumes directly.

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(+)
drivers/crypto/atmel-ecc.c +34 -0
@@ -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

maintainer
#3 lore raw

Hi Ryan,

This should probably have been submitted as v2, not v3, but let's continue with v4.

Please post each revision as a new thread to the list and not as a reply to a review.

Quoted 3 lines · Ryan Prinz

atmel_ecdh_compute_shared_secret() passes the peer's ECDH public point to the ATECC508A/608A/608B secure element without validating it; only req->src_len is checked.

Please clarify that this refers to software validation, as the device may perform its own hardware validation.

Quoted 2 lines

Add the same check to this driver's path, before the point reaches the I2C command, reusing ecc_is_pubkey_valid_partial().

Which same check? Somebody reading this later in the git history will have no idea what you're referring to without the context of earlier revisions.

[...]

drivers/crypto/atmel-ecc.c line 24

Quoted 13 lines
 drivers/crypto/atmel-ecc.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
drivers/crypto/atmel-ecc.c +2 -0
@@ -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>

Please keep the crypto includes in alphabetical order.

Quoted 8 lines
drivers/crypto/atmel-ecc.c +1 -0
 #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,

drivers/crypto/atmel-ecc.c static int atmel_ecdh_generate_public_key(struct kpp_request *req) · line 161

Quoted 10 lines
drivers/crypto/atmel-ecc.c +10 -0
+					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;

This check can be dropped as ecc_get_curve(ECC_CURVE_NIST_P256) always returns a valid pointer.

drivers/crypto/atmel-ecc.c static int atmel_ecdh_generate_public_key(struct kpp_request *req) · line 163

Quoted 2 lines
drivers/crypto/atmel-ecc.c +2 -0
+	if (src_len != ATMEL_ECC_PUBKEY_SIZE)
+		return -EINVAL;

The caller already checks the length, so this check is also redundant.

drivers/crypto/atmel-ecc.c static int atmel_ecdh_generate_public_key(struct kpp_request *req) · line 167

Quoted 2 lines
drivers/crypto/atmel-ecc.c +2 -0
+	copied = sg_copy_to_buffer(src, sg_nents_for_len(src, src_len),
+				   pubkey_buf, src_len);

Could we validate work_data->cmd.data after atmel_i2c_init_ecdh_cmd() and before enqueueing? That would avoid an extra copy.

Thanks,
Thorsten