crypto: atmel-ecc - fix missing ECDH public key validation
Subsystems (3)
PATCH v4 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 software validation; only req->src_len is checked.
Validate the point in software before it reaches the I2C command, the same check crypto_ecdh_shared_secret() performs for the generic "ecdh-nist-p256" implementation: call ecc_is_pubkey_valid_partial() and reject the request if it fails.
---
v4: Clarified that the missing check is software validation, since the device may still validate internally in hardware. Reordered the crypto/ includes alphabetically. Dropped the curve-NULL check (ecc_get_curve(ECC_CURVE_NIST_P256) never returns NULL) and the length check (the caller already validates req->src_len). Validate directly from the already-populated ECDH command buffer (work_data->cmd.data) instead of a second sg_copy_to_buffer() from the scatterlist, avoiding the extra copy. (Thorsten Blum)
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, reading X and Y directly out of work_data->cmd.data, which atmel_i2c_init_ecdh_cmd() already documents and populates as big-endian X (32 bytes) || big-endian Y (32 bytes) -- the same layout crypto_ecdh_shared_secret() expects from req->src before this driver ever sees it.
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 | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-)
drivers/crypto/atmel-ecc.c +5 -3
@@ -17,8 +17,10 @@#include <linux/of.h> #include <linux/scatterlist.h> #include <linux/slab.h> #include <linux/workqueue.h> -#include <crypto/internal/kpp.h> -#include <crypto/ecdh.h> -#include <crypto/kpp.h> +#include <crypto/ecc_curve.h> +#include <crypto/ecdh.h> +#include <crypto/internal/ecc.h> +#include <crypto/internal/kpp.h> +#include <crypto/kpp.h> #include "atmel-i2c.h"
@@ -146,6 +148,20 @@ static int atmel_ecdh_generate_public_key(struct kpp_request *req)
return 0;
}
+static int atmel_ecdh_peer_pubkey_valid(const u8 *pubkey_buf)
+{
+ const struct ecc_curve *curve = ecc_get_curve(ECC_CURVE_NIST_P256);
+ u64 x[ECC_CURVE_NIST_P256_DIGITS], y[ECC_CURVE_NIST_P256_DIGITS];
+ struct ecc_point pk;
+
+ 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);
@@ -181,8 +197,14 @@ static int atmel_ecdh_compute_shared_secret(struct kpp_request *req)
kfree(work_data);
return ret;
}
+ ret = atmel_ecdh_peer_pubkey_valid(work_data->cmd.data);
+ if (ret) {
+ kfree(work_data);
+ return ret;
+ }
+
atmel_i2c_enqueue(work_data, atmel_ecdh_done, req);
return -EINPROGRESS;
}
--
2.51.2
Thorsten Blum
maintainerv4 fails to apply with the following error:
patch fragment without header at line 71: @@ -146,6 +148,20 @@ static int atmel_ecdh_generate_public_key(struct kpp_request *req)
More comments below.
Quoted 12 lines · Ryan Prinz
atmel_ecdh_compute_shared_secret()passes the peer's ECDH public point to the ATECC508A/608A/608B secure element without software validation; onlyreq->src_lenis checked.Validate the point in software before it reaches the I2C command, the same check
crypto_ecdh_shared_secret()performs for the generic "ecdh-nist-p256" implementation: callecc_is_pubkey_valid_partial()and reject the request if it fails.Fixes 11105693fa05 ("crypto: atmel-ecc - introduce Microchip / Atmel ECC driver")Signed-off-by Ryan Prinz <[email protected]>---
[...]
Quoted 1 line
This has not been tested on hardware.
[...]
Quoted 4 lines
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.
The Fixes: tag should be fine, but please drop "I have verified the vulnerability, the fix, and the test results myself". Your notes say it wasn't tested on hardware, and there's no evidence the device accepts a bad key.
Quoted 6 lines
drivers/crypto/atmel-ecc.c +0
-0@@ -17,8 +17,10 @@#include <linux/of.h> #include <linux/scatterlist.h> #include <linux/slab.h>
drivers/crypto/atmel-ecc.c line 26
Quoted 10 lines
drivers/crypto/atmel-ecc.c +5
-3#include <linux/workqueue.h> -#include <crypto/internal/kpp.h> -#include <crypto/ecdh.h> -#include <crypto/kpp.h> +#include <crypto/ecc_curve.h> +#include <crypto/ecdh.h> +#include <crypto/internal/ecc.h> +#include <crypto/internal/kpp.h> +#include <crypto/kpp.h> #include "atmel-i2c.h"
Sorry, I should have been clearer about the grouping. I'd keep <crypto/internal/*> as a group before the other <crypto/*> includes, with each group sorted. That also keeps the diff small:
+#include <crypto/internal/ecc.h> #include <crypto/internal/kpp.h> +#include <crypto/ecc_curve.h> #include <crypto/ecdh.h> #include <crypto/kpp.h>
Quoted 1 line
+static int
atmel_ecdh_peer_pubkey_valid(const u8 *pubkey_buf)
The function name reads like a predicate, but it returns 0 or an errno. atmel_ecdh_validate_peer_pubkey() would be clearer imo.
Thanks,
Thorsten