Skip to contentSkip to conversation

Enable ECDSA FIT verification for stm32mp

PATCH In review Alexandru Gagniuc 5 participants 25 messages May 17, 2021 – Aug 16, 2021

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v5 cover Enable ECDSA FIT verification for stm32mp

This series is based on the following:
"[PATCH 00/18] image: Reduce #ifdef abuse in image code"

This rebase is awesome because we don't need to change #defines in common code or rely on hidden #ifdefs.

Q: Will there be a software-only implementation of ECDSA ?
A: That is the goal, so that we can have more extensive testing with
the sandbox. I don not have the bandwidth to implement it. There has been an initial poer of software ecdsa here:

       https://github.com/timr11/u-boot/tree/ecdsa-vrf-1

Q: Can more code be shared with the RSA verification path?
A: Probably yes. Mostly having to do with parsing the "/signature"
node and "key-name-hint"s in the u-boot FDT. Although there isn't any copypasted RSA code, or code with substantial similarity.

Changes since v4:
- Use U_BOOT_CRYPTO_ALGO() to add ECDSA to .u_boot_list
- No need to #define IMAGE_ENABLE_VERIFY_ECDSA
- Use ut_asserteq(x, -ENODEV) instead of ut_assert(x == -ENODEV)

Changes since v3:
- Remove unused ecdsa_check_key() function

Changes since v2:
- Spell out "elliptic curve" in Kconfig (Although RSA isn't spelled out)

Changes since v1:
- Add test to make sure the UCLASS is enabled
- Fix check against wrong sig_len in ecdsa_romapi.c
- s/U_BOOT_DEVICE/U_BOOT_DRVINFO/
- Use "if(!ret)" instead of "if (ret == 0)"
- Use uclass_first_device_err() instead of uclass_first_device()
- Make sure #includes are correctly alphabetized

Alexandru Gagniuc (5):
dm: crypto: Define UCLASS API for ECDSA signature verification
lib: ecdsa: Implement UCLASS_ECDSA verification on target
arm: stm32mp1: Implement ECDSA signature verification
Kconfig: FIT_SIGNATURE should not select RSA_VERIFY
test: dm: Add test for ECDSA UCLASS support

 arch/arm/mach-stm32mp/Kconfig        |   9 ++
 arch/arm/mach-stm32mp/Makefile       |   1 +
 arch/arm/mach-stm32mp/ecdsa_romapi.c | 102 ++++++++++++++++++++
 common/Kconfig.boot                  |   8 +-
 configs/sandbox_defconfig            |   2 +
 include/crypto/ecdsa-uclass.h        |  39 ++++++++
 include/dm/uclass-id.h               |   1 +
 lib/Kconfig                          |   1 +
 lib/Makefile                         |   1 +
 lib/ecdsa/Kconfig                    |  23 +++++
 lib/ecdsa/Makefile                   |   1 +
 lib/ecdsa/ecdsa-verify.c             | 134 +++++++++++++++++++++++++++
 test/dm/Makefile                     |   1 +
 test/dm/ecdsa.c                      |  39 ++++++++
 14 files changed, 358 insertions(+), 4 deletions(-)
 create mode 100644 arch/arm/mach-stm32mp/ecdsa_romapi.c
 create mode 100644 include/crypto/ecdsa-uclass.h
 create mode 100644 lib/ecdsa/Kconfig
 create mode 100644 lib/ecdsa/Makefile
 create mode 100644 lib/ecdsa/ecdsa-verify.c
 create mode 100644 test/dm/ecdsa.c
Signature · 1 line
2.31.1
#1 lore raw

PATCH v5 1/5 dm: crypto: Define UCLASS API for ECDSA signature verification

Define a UCLASS API for verifying ECDSA signatures. Unlike UCLASS_MOD_EXP, which focuses strictly on modular exponentiation, the ECDSA class focuses on verification. This is done so that it better aligns with mach-specific implementations, such as stm32mp.

---
 include/crypto/ecdsa-uclass.h | 39 +++++++++++++++++++++++++++++++++++
 include/dm/uclass-id.h        |  1 +
 2 files changed, 40 insertions(+)
 create mode 100644 include/crypto/ecdsa-uclass.h
include/crypto/ecdsa-uclass.h new file +39 -0
new file mode 100644
index 0000000000..189843820a
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2020, Alexandru Gagniuc <[email protected]>
+ */
+
+#include <dm/device.h>
+
+/**
+ * struct ecdsa_public_key - ECDSA public key properties
+ *
+ * The struct has pointers to the (x, y) curve coordinates to an ECDSA public
+ * key, as well as the name of the ECDSA curve. The size of the key is inferred
+ * from the 'curve_name'
+ */
+struct ecdsa_public_key {
+	const char *curve_name;	/* Name of curve, e.g. "prime256v1" */
+	const void *x;		/* x coordinate of public key */
+	const void *y;		/* y coordinate of public key */
+	unsigned int size_bits;	/* key size in bits, derived from curve name */
+};
+
+struct ecdsa_ops {
+	/**
+	 * Verify signature of hash against given public key
+	 *
+	 * @dev:	ECDSA Device
+	 * @pubkey:	ECDSA public key
+	 * @hash:	Hash of binary image
+	 * @hash_len:	Length of hash in bytes
+	 * @signature:	Signature in a raw (R, S) point pair
+	 * @sig_len:	Length of signature in bytes
+	 *
+	 * This function verifies that the 'signature' of the given 'hash' was
+	 * signed by the private key corresponding to 'pubkey'.
+	 */
+	int (*verify)(struct udevice *dev, const struct ecdsa_public_key *pubkey,
+		      const void *hash, size_t hash_len,
+		      const void *signature, size_t sig_len);
+};
include/dm/uclass-id.h +1 -0
index d800f679d5..2a32809669 100644
@@ -47,6 +47,7 @@ enum uclass_id {
 	UCLASS_DSI_HOST,	/* Display Serial Interface host */
 	UCLASS_DMA,		/* Direct Memory Access */
 	UCLASS_DSA,		/* Distributed (Ethernet) Switch Architecture */
+	UCLASS_ECDSA,		/* Elliptic curve cryptographic device */
 	UCLASS_EFI,		/* EFI managed devices */
 	UCLASS_ETH,		/* Ethernet device */
 	UCLASS_ETH_PHY,		/* Ethernet PHY device */
Signature · 1 line
2.31.1
#2 lore raw

PATCH v5 1/5 dm: crypto: Define UCLASS API for ECDSA signature verification

Hi Alex,

Quoted 6 lines · Alexandru Gagniuc

Define a UCLASS API for verifying ECDSA signatures. Unlike UCLASS_MOD_EXP, which focuses strictly on modular exponentiation, the ECDSA class focuses on verification. This is done so that it better aligns with mach-specific implementations, such as stm32mp.

Quoted 6 lines
---
 include/crypto/ecdsa-uclass.h | 39 +++++++++++++++++++++++++++++++++++
 include/dm/uclass-id.h        |  1 +
 2 files changed, 40 insertions(+)
 create mode 100644 include/crypto/ecdsa-uclass.h

We cannot have a uclass with a test. You should implement at least a trivial test for this uclass, with a sandbox driver.

Regards,
Simon

#3 lore raw

PATCH v5 2/5 lib: ecdsa: Implement UCLASS_ECDSA verification on target

In reply to Alexandru Gagniuc

Implement the crypto_algo .verify() function for ecdsa256. Because it backends on UCLASS_ECDSA, this change is focused on parsing the keys from devicetree and passing this information to the specific UCLASS driver.

---
 lib/Kconfig              |   1 +
 lib/Makefile             |   1 +
 lib/ecdsa/Kconfig        |  23 +++++++
 lib/ecdsa/Makefile       |   1 +
 lib/ecdsa/ecdsa-verify.c | 134 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 160 insertions(+)
 create mode 100644 lib/ecdsa/Kconfig
 create mode 100644 lib/ecdsa/Makefile
 create mode 100644 lib/ecdsa/ecdsa-verify.c
lib/Kconfig +1 -0
index 6d2d41de30..8719a51baa 100644
@@ -295,6 +295,7 @@ config AES
 	  supported by the algorithm but only a 128-bit key is supported at
 	  present.
 
+source lib/ecdsa/Kconfig
 source lib/rsa/Kconfig
 source lib/crypto/Kconfig
 
lib/Makefile +1 -0
index 6825671955..2c7c145a27 100644
@@ -60,6 +60,7 @@ endif
 
 obj-$(CONFIG_$(SPL_)ACPIGEN) += acpi/
 obj-$(CONFIG_$(SPL_)MD5) += md5.o
+obj-$(CONFIG_ECDSA) += ecdsa/
 obj-$(CONFIG_$(SPL_)RSA) += rsa/
 obj-$(CONFIG_FIT_SIGNATURE) += hash-checksum.o
 obj-$(CONFIG_SHA1) += sha1.o
lib/ecdsa/Kconfig new file +23 -0
new file mode 100644
index 0000000000..a95c4ff581
@@ -0,0 +1,23 @@
+config ECDSA
+	bool "Enable ECDSA support"
+	depends on DM
+	help
+	  This enables the ECDSA (elliptic curve signature) algorithm for FIT
+	  image verification in U-Boot. The ECDSA algorithm is implemented
+	  using the driver model, so CONFIG_DM is required by this library.
+	  See doc/uImage.FIT/signature.txt for more details.
+	  ECDSA is enabled for mkimage regardless of this option.
+
+if ECDSA
+
+config ECDSA_VERIFY
+	bool "Enable ECDSA verification support in U-Boot."
+	help
+	  Allow ECDSA signatures to be recognized and verified in U-Boot.
+
+config SPL_ECDSA_VERIFY
+	bool "Enable ECDSA verification support in SPL"
+	help
+	  Allow ECDSA signatures to be recognized and verified in SPL.
+
+endif
lib/ecdsa/Makefile new file +1 -0
new file mode 100644
index 0000000000..771d6d3135
@@ -0,0 +1 @@
+obj-$(CONFIG_$(SPL_)ECDSA_VERIFY) += ecdsa-verify.o
lib/ecdsa/ecdsa-verify.c new file +134 -0

134 lines not on this page · raw message

Signature · 1 line
2.31.1
#4 lore raw

PATCH v5 2/5 lib: ecdsa: Implement UCLASS_ECDSA verification on target

Quoted 7 lines · Alexandru Gagniuc

Implement the crypto_algo .verify() function for ecdsa256. Because it backends on UCLASS_ECDSA, this change is focused on parsing the keys from devicetree and passing this information to the specific UCLASS driver.

Quoted 10 lines
---
 lib/Kconfig              |   1 +
 lib/Makefile             |   1 +
 lib/ecdsa/Kconfig        |  23 +++++++
 lib/ecdsa/Makefile       |   1 +
 lib/ecdsa/ecdsa-verify.c | 134 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 160 insertions(+)
 create mode 100644 lib/ecdsa/Kconfig
 create mode 100644 lib/ecdsa/Makefile
 create mode 100644 lib/ecdsa/ecdsa-verify.c
#5 lore raw

PATCH v5 3/5 arm: stm32mp1: Implement ECDSA signature verification

In reply to Alexandru Gagniuc

The STM32MP ROM provides several service. One of them is the ability to verify ecdsa256 signatures. Hook the ROM API into the ECDSA uclass.

---
 arch/arm/mach-stm32mp/Kconfig        |   9 +++
 arch/arm/mach-stm32mp/Makefile       |   1 +
 arch/arm/mach-stm32mp/ecdsa_romapi.c | 102 +++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)
 create mode 100644 arch/arm/mach-stm32mp/ecdsa_romapi.c
arch/arm/mach-stm32mp/Kconfig +9 -0
index 7c25266f33..b47fbbb97b 100644
@@ -172,6 +172,15 @@ config STM32_ETZPC
 	help
 	  Say y to enable STM32 Extended TrustZone Protection
 
+config STM32_ECDSA_VERIFY
+	bool "STM32 ECDSA verification via the ROM API"
+	depends on SPL_ECDSA_VERIFY
+	default y
+	help
+	  Say y to enable the uclass driver for ECDSA verification using the
+	  ROM API provided on STM32MP.
+	  The ROM API is only available during SPL for now.
+
 config CMD_STM32KEY
 	bool "command stm32key to fuse public key hash"
 	default y
arch/arm/mach-stm32mp/Makefile +1 -0
index aa39867080..0942092d8e 100644
@@ -10,6 +10,7 @@ obj-y += bsec.o
 
 ifdef CONFIG_SPL_BUILD
 obj-y += spl.o
+obj-$(CONFIG_STM32_ECDSA_VERIFY) += ecdsa_romapi.o
 else
 obj-y += cmd_stm32prog/
 obj-$(CONFIG_CMD_STM32KEY) += cmd_stm32key.o
arch/arm/mach-stm32mp/ecdsa_romapi.c new file +102 -0

102 lines not on this page · raw message

Signature · 1 line
2.31.1
#6 lore raw

PATCH v5 4/5 Kconfig: FIT_SIGNATURE should not select RSA_VERIFY

In reply to Alexandru Gagniuc

FIT signatures can now be implemented with ECDSA. The assumption that all FIT images are signed with RSA is no longer valid. Thus, instead of 'select'ing RSA, only 'imply' it. This doesn't change the defaults, but allows one to explicitly disable RSA support.

---
 common/Kconfig.boot | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
common/Kconfig.boot +4 -4
index 03a6e6f214..1527e3e600 100644
@@ -76,8 +76,8 @@ config FIT_SIGNATURE
 	bool "Enable signature verification of FIT uImages"
 	depends on DM
 	select HASH
-	select RSA
-	select RSA_VERIFY
+	imply RSA
+	imply RSA_VERIFY
 	select IMAGE_SIGN_INFO
 	select FIT_FULL_CHECK
 	help
@@ -186,8 +186,8 @@ config SPL_FIT_SIGNATURE
 	select SPL_FIT
 	select SPL_CRYPTO_SUPPORT
 	select SPL_HASH_SUPPORT
-	select SPL_RSA
-	select SPL_RSA_VERIFY
+	imply SPL_RSA
+	imply SPL_RSA_VERIFY
 	select SPL_IMAGE_SIGN_INFO
 	select SPL_FIT_FULL_CHECK
 
Signature · 1 line
2.31.1
#7 lore raw

PATCH v5 4/5 Kconfig: FIT_SIGNATURE should not select RSA_VERIFY

Quoted 39 lines · Alexandru Gagniuc

FIT signatures can now be implemented with ECDSA. The assumption that all FIT images are signed with RSA is no longer valid. Thus, instead of 'select'ing RSA, only 'imply' it. This doesn't change the defaults, but allows one to explicitly disable RSA support.

---
 common/Kconfig.boot | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
common/Kconfig.boot +4 -5
index 03a6e6f214..1527e3e600 100644
@@ -76,8 +76,8 @@ config FIT_SIGNATURE
        bool "Enable signature verification of FIT uImages"
        depends on DM
        select HASH
-       select RSA
-       select RSA_VERIFY
+       imply RSA
+       imply RSA_VERIFY
        select IMAGE_SIGN_INFO
        select FIT_FULL_CHECK
        help
@@ -186,8 +186,8 @@ config SPL_FIT_SIGNATURE
        select SPL_FIT
        select SPL_CRYPTO_SUPPORT
        select SPL_HASH_SUPPORT
-       select SPL_RSA
-       select SPL_RSA_VERIFY
+       imply SPL_RSA
+       imply SPL_RSA_VERIFY
        select SPL_IMAGE_SIGN_INFO
        select SPL_FIT_FULL_CHECK
 
--
Quoted 2 lines

2.31.1

Signature · 7 lines
Best regards - Freundliche Gr?sse - Meilleures salutations

Igor Opaniuk
Embedded Software Engineer
T:  +380 938364067
E: igor.opaniuk at foundries.io
W: www.foundries.io
#8 lore raw

PATCH v5 5/5 test: dm: Add test for ECDSA UCLASS support

In reply to Alexandru Gagniuc

This test verifies that ECDSA_UCLASS is implemented, and that ecdsa_verify() works as expected. The definition of "expected" is "does not find a device, and returns -ENODEV".

The lack of a hardware-independent ECDSA implementation prevents us from having one in the sandbox, for now.

---
 configs/sandbox_defconfig |  2 ++
 test/dm/Makefile          |  1 +
 test/dm/ecdsa.c           | 39 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+)
 create mode 100644 test/dm/ecdsa.c
configs/sandbox_defconfig +2 -0
index 7b8603d1ef..e40bcb4b16 100644
@@ -287,3 +287,5 @@ CONFIG_TEST_FDTDEC=y
 CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
+CONFIG_ECDSA=y
+CONFIG_ECDSA_VERIFY=y
test/dm/Makefile +1 -0
index c9644617a1..3508aa1968 100644
@@ -34,6 +34,7 @@ obj-$(CONFIG_DEVRES) += devres.o
 obj-$(CONFIG_DMA) += dma.o
 obj-$(CONFIG_VIDEO_MIPI_DSI) += dsi_host.o
 obj-$(CONFIG_DM_DSA) += dsa.o
+obj-$(CONFIG_ECDSA_VERIFY) += ecdsa.o
 obj-$(CONFIG_DM_ETH) += eth.o
 ifneq ($(CONFIG_EFI_PARTITION),)
 obj-$(CONFIG_FASTBOOT_FLASH_MMC) += fastboot.o
test/dm/ecdsa.c new file +39 -0
new file mode 100644
index 0000000000..9c0007b180
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <crypto/ecdsa-uclass.h>
+#include <dm.h>
+#include <dm/test.h>
+#include <test/ut.h>
+#include <u-boot/ecdsa.h>
+
+/*
+ * Basic test of the ECDSA uclass and ecdsa_verify()
+ *
+ * ECDSA implementations in u-boot are hardware-dependent. Until we have a
+ * software implementation that can be compiled into the sandbox, all we can
+ * test is the uclass support.
+ *
+ * The uclass_get() test is redundant since ecdsa_verify() would also fail. We
+ * run both functions in order to isolate the cause more clearly. i.e. is
+ * ecdsa_verify() failing because the UCLASS is absent/broken?
+ */
+static int dm_test_ecdsa_verify(struct unit_test_state *uts)
+{
+	const struct ecdsa_ops *ops;
+	struct uclass *ucp;
+
+	const struct checksum_algo algo = {
+		.checksum_len = 256,
+	};
+
+	struct image_sign_info info = {
+		.checksum = &algo,
+	};
+
+	ut_assertok(uclass_get(UCLASS_ECDSA, &ucp));
+	ut_assertnonnull(ucp);
+	ut_asserteq(-ENODEV, ecdsa_verify(&info, NULL, 0, NULL, 0));
+
+	return 0;
+}
+DM_TEST(dm_test_ecdsa_verify, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Signature · 1 line
2.31.1
#9 lore raw

Re: [PATCH v5 5/5] test: dm: Add test for ECDSA UCLASS support

Hi Alexandru,

Quoted 73 lines · Alexandru Gagniuc

This test verifies that ECDSA_UCLASS is implemented, and that ecdsa_verify() works as expected. The definition of "expected" is "does not find a device, and returns -ENODEV".

The lack of a hardware-independent ECDSA implementation prevents us from having one in the sandbox, for now.

---
  configs/sandbox_defconfig |  2 ++
  test/dm/Makefile          |  1 +
  test/dm/ecdsa.c           | 39 +++++++++++++++++++++++++++++++++++++++
  3 files changed, 42 insertions(+)

create mode 100644 test/dm/ecdsa.c

configs/sandbox_defconfig +2 -0
index 7b8603d1ef..e40bcb4b16 100644
@@ -287,3 +287,5 @@ CONFIG_TEST_FDTDEC=y
  CONFIG_UNIT_TEST=y
  CONFIG_UT_TIME=y
  CONFIG_UT_DM=y
+CONFIG_ECDSA=y
+CONFIG_ECDSA_VERIFY=y
test/dm/Makefile +1 -0
index c9644617a1..3508aa1968 100644
@@ -34,6 +34,7 @@ obj-$(CONFIG_DEVRES) += devres.o
  obj-$(CONFIG_DMA) += dma.o
  obj-$(CONFIG_VIDEO_MIPI_DSI) += dsi_host.o
  obj-$(CONFIG_DM_DSA) += dsa.o
+obj-$(CONFIG_ECDSA_VERIFY) += ecdsa.o
  obj-$(CONFIG_DM_ETH) += eth.o
  ifneq ($(CONFIG_EFI_PARTITION),)
  obj-$(CONFIG_FASTBOOT_FLASH_MMC) += fastboot.o
test/dm/ecdsa.c new file +29 -0
new file mode 100644
index 0000000000..9c0007b180
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <crypto/ecdsa-uclass.h>
+#include <dm.h>
+#include <dm/test.h>
+#include <test/ut.h>
+#include <u-boot/ecdsa.h>
+
+/*
+ * Basic test of the ECDSA uclass and ecdsa_verify()
+ *
+ * ECDSA implementations in u-boot are hardware-dependent. Until we have a
+ * software implementation that can be compiled into the sandbox, all we can
+ * test is the uclass support.
+ *
+ * The uclass_get() test is redundant since ecdsa_verify() would also fail. We
+ * run both functions in order to isolate the cause more clearly. i.e. is
+ * ecdsa_verify() failing because the UCLASS is absent/broken?
+ */
+static int dm_test_ecdsa_verify(struct unit_test_state *uts)
+{
+	const struct ecdsa_ops *ops;
+	struct uclass *ucp;
+
+	const struct checksum_algo algo = {
+		.checksum_len = 256,
+	};
+
+	struct image_sign_info info = {

test/dm/ecdsa.c line 39

Quoted 10 lines
test/dm/ecdsa.c new file +10 -0
+		.checksum = &algo,
+	};
+
+	ut_assertok(uclass_get(UCLASS_ECDSA, &ucp));
+	ut_assertnonnull(ucp);
+	ut_asserteq(-ENODEV, ecdsa_verify(&info, NULL, 0, NULL, 0));
+
+	return 0;
+}
+DM_TEST(dm_test_ecdsa_verify, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);

When I prepare the stm32 pull request, I detect a issue with this patch in CI pipeline:

https://source.denx.de/u-boot/custodians/u-boot-stm/-/jobs/298432

+ sandbox test.py

+ sandbox with clang test.py

With the same errors:

Building current source for 1 boards (1 thread, 32 jobs per thread)
   sandbox:  +   sandbox
+test/dm/ecdsa.c:30:15: error: initializing 'struct checksum_algo *' with an expression of type 'const struct checksum_algo *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers] +                .checksum = &algo,
+                            ^~~~~
+test/dm/ecdsa.c:22:26: error: unused variable 'ops' [-Werror,-Wunused-variable]
+        const struct ecdsa_ops *ops;
+                                ^
+2 errors generated.
+make[3]: *** [scripts/Makefile.build:253: test/dm/ecdsa.o] Error 1
+make[2]: *** [scripts/Makefile.build:394: test/dm] Error 2
+make[1]: *** [Makefile:1815: test] Error 2
+make: *** [Makefile:177: sub-make] Error 2
    0    0    1 /1              sandbox

Can you correct this issue.

Thanks,

Patrick

#10 lore raw

cover Enable ECDSA FIT verification for stm32mp

This series is based on the latest master, so no patch dependencies.

Q: Will there be a software-only implementation of ECDSA ?
A: That is the goal, so that we can have more extensive testing with
the sandbox. I don not have the bandwidth to implement it. There has been an initial poer of software ecdsa here:

       https://github.com/timr11/u-boot/tree/ecdsa-vrf-1

Q: Can more code be shared with the RSA verification path?
A: Probably yes. Mostly having to do with parsing the "/signature"
node and "key-name-hint"s in the u-boot FDT. Although there isn't any copypasted RSA code, or code with substantial similarity.

Changes since v5:
- Fixed clang warning stemming from test/dm/ecdsa.c

Changes since v4:
- Use U_BOOT_CRYPTO_ALGO() to add ECDSA to .u_boot_list
- No need to #define IMAGE_ENABLE_VERIFY_ECDSA
- Use ut_asserteq(x, -ENODEV) instead of ut_assert(x == -ENODEV)

Changes since v3:
- Remove unused ecdsa_check_key() function

Changes since v2:
- Spell out "elliptic curve" in Kconfig (Although RSA isn't spelled out)

Changes since v1:
- Add test to make sure the UCLASS is enabled
- Fix check against wrong sig_len in ecdsa_romapi.c
- s/U_BOOT_DEVICE/U_BOOT_DRVINFO/
- Use "if(!ret)" instead of "if (ret == 0)"
- Use uclass_first_device_err() instead of uclass_fi

Alexandru Gagniuc (5):
dm: crypto: Define UCLASS API for ECDSA signature verification
lib: ecdsa: Implement UCLASS_ECDSA verification on target
arm: stm32mp1: Implement ECDSA signature verification
Kconfig: FIT_SIGNATURE should not select RSA_VERIFY
test: dm: Add test for ECDSA UCLASS support

 arch/arm/mach-stm32mp/Kconfig        |   9 ++
 arch/arm/mach-stm32mp/Makefile       |   1 +
 arch/arm/mach-stm32mp/ecdsa_romapi.c | 102 ++++++++++++++++++++
 common/Kconfig.boot                  |   8 +-
 configs/sandbox_defconfig            |   2 +
 include/crypto/ecdsa-uclass.h        |  39 ++++++++
 include/dm/uclass-id.h               |   1 +
 lib/Kconfig                          |   1 +
 lib/Makefile                         |   1 +
 lib/ecdsa/Kconfig                    |  23 +++++
 lib/ecdsa/Makefile                   |   1 +
 lib/ecdsa/ecdsa-verify.c             | 134 +++++++++++++++++++++++++++
 test/dm/Makefile                     |   1 +
 test/dm/ecdsa.c                      |  38 ++++++++
 14 files changed, 357 insertions(+), 4 deletions(-)
 create mode 100644 arch/arm/mach-stm32mp/ecdsa_romapi.c
 create mode 100644 include/crypto/ecdsa-uclass.h
 create mode 100644 lib/ecdsa/Kconfig
 create mode 100644 lib/ecdsa/Makefile
 create mode 100644 lib/ecdsa/ecdsa-verify.c
 create mode 100644 test/dm/ecdsa.c
Signature · 1 line
2.31.1
#11 lore raw

PATCH v5 1/5 dm: crypto: Define UCLASS API for ECDSA signature verification

Define a UCLASS API for verifying ECDSA signatures. Unlike UCLASS_MOD_EXP, which focuses strictly on modular exponentiation, the ECDSA class focuses on verification. This is done so that it better aligns with mach-specific implementations, such as stm32mp.

---
 include/crypto/ecdsa-uclass.h | 39 +++++++++++++++++++++++++++++++++++
 include/dm/uclass-id.h        |  1 +
 2 files changed, 40 insertions(+)
 create mode 100644 include/crypto/ecdsa-uclass.h
include/crypto/ecdsa-uclass.h new file +39 -0
new file mode 100644
index 0000000000..189843820a
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2020, Alexandru Gagniuc <[email protected]>
+ */
+
+#include <dm/device.h>
+
+/**
+ * struct ecdsa_public_key - ECDSA public key properties
+ *
+ * The struct has pointers to the (x, y) curve coordinates to an ECDSA public
+ * key, as well as the name of the ECDSA curve. The size of the key is inferred
+ * from the 'curve_name'
+ */
+struct ecdsa_public_key {
+	const char *curve_name;	/* Name of curve, e.g. "prime256v1" */
+	const void *x;		/* x coordinate of public key */
+	const void *y;		/* y coordinate of public key */
+	unsigned int size_bits;	/* key size in bits, derived from curve name */
+};
+
+struct ecdsa_ops {
+	/**
+	 * Verify signature of hash against given public key
+	 *
+	 * @dev:	ECDSA Device
+	 * @pubkey:	ECDSA public key
+	 * @hash:	Hash of binary image
+	 * @hash_len:	Length of hash in bytes
+	 * @signature:	Signature in a raw (R, S) point pair
+	 * @sig_len:	Length of signature in bytes
+	 *
+	 * This function verifies that the 'signature' of the given 'hash' was
+	 * signed by the private key corresponding to 'pubkey'.
+	 */
+	int (*verify)(struct udevice *dev, const struct ecdsa_public_key *pubkey,
+		      const void *hash, size_t hash_len,
+		      const void *signature, size_t sig_len);
+};
include/dm/uclass-id.h +1 -0
index 9d474533ba..e7edd409f3 100644
@@ -47,6 +47,7 @@ enum uclass_id {
 	UCLASS_DSI_HOST,	/* Display Serial Interface host */
 	UCLASS_DMA,		/* Direct Memory Access */
 	UCLASS_DSA,		/* Distributed (Ethernet) Switch Architecture */
+	UCLASS_ECDSA,		/* Elliptic curve cryptographic device */
 	UCLASS_EFI,		/* EFI managed devices */
 	UCLASS_ETH,		/* Ethernet device */
 	UCLASS_ETH_PHY,		/* Ethernet PHY device */
Signature · 1 line
2.31.1
Reviewed-by on v5 1/5
#12 lore raw

Re: [PATCH 1/5] dm: crypto: Define UCLASS API for ECDSA signature verification

Hi Alexandru,

Quoted 5 lines · Alexandru Gagniuc

Define a UCLASS API for verifying ECDSA signatures. Unlike UCLASS_MOD_EXP, which focuses strictly on modular exponentiation, the ECDSA class focuses on verification. This is done so that it better aligns with mach-specific implementations, such as stm32mp.

Quoted 7 lines
---
  include/crypto/ecdsa-uclass.h | 39 +++++++++++++++++++++++++++++++++++
  include/dm/uclass-id.h        |  1 +
  2 files changed, 40 insertions(+)

create mode 100644 include/crypto/ecdsa-uclass.h

Thanks
Patrick

#13 lore raw

Re: [PATCH 1/5] dm: crypto: Define UCLASS API for ECDSA signature verification

Hi Alexandru

Quoted 16 lines · Patrick DELAUNAY

Hi Alexandru,

Quoted 5 lines · Alexandru Gagniuc

Define a UCLASS API for verifying ECDSA signatures. Unlike UCLASS_MOD_EXP, which focuses strictly on modular exponentiation, the ECDSA class focuses on verification. This is done so that it better aligns with mach-specific implementations, such as stm32mp.

Quoted 7 lines

---
  include/crypto/ecdsa-uclass.h | 39 +++++++++++++++++++++++++++++++++++ include/dm/uclass-id.h        |  1 +

  2 files changed, 40 insertions(+)

  create mode 100644 include/crypto/ecdsa-uclass.h

Quoted 5 lines

Thanks
Patrick

Applied to u-boot-stm/master

Thanks
Patrice

#14 lore raw

PATCH v5 2/5 lib: ecdsa: Implement UCLASS_ECDSA verification on target

In reply to Alexandru Gagniuc

Implement the crypto_algo .verify() function for ecdsa256. Because it backends on UCLASS_ECDSA, this change is focused on parsing the keys from devicetree and passing this information to the specific UCLASS driver.

---
 lib/Kconfig              |   1 +
 lib/Makefile             |   1 +
 lib/ecdsa/Kconfig        |  23 +++++++
 lib/ecdsa/Makefile       |   1 +
 lib/ecdsa/ecdsa-verify.c | 134 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 160 insertions(+)
 create mode 100644 lib/ecdsa/Kconfig
 create mode 100644 lib/ecdsa/Makefile
 create mode 100644 lib/ecdsa/ecdsa-verify.c
lib/Kconfig +1 -0
index fdcf7ea405..014a2f7f77 100644
@@ -303,6 +303,7 @@ config AES
 	  supported by the algorithm but only a 128-bit key is supported at
 	  present.
 
+source lib/ecdsa/Kconfig
 source lib/rsa/Kconfig
 source lib/crypto/Kconfig
 source lib/crypt/Kconfig
lib/Makefile +1 -0
index 07c2ccd7cf..8ba745faa0 100644
@@ -60,6 +60,7 @@ endif
 
 obj-$(CONFIG_$(SPL_)ACPIGEN) += acpi/
 obj-$(CONFIG_$(SPL_)MD5) += md5.o
+obj-$(CONFIG_ECDSA) += ecdsa/
 obj-$(CONFIG_$(SPL_)RSA) += rsa/
 obj-$(CONFIG_HASH) += hash-checksum.o
 obj-$(CONFIG_SHA1) += sha1.o
lib/ecdsa/Kconfig new file +23 -0
new file mode 100644
index 0000000000..a95c4ff581
@@ -0,0 +1,23 @@
+config ECDSA
+	bool "Enable ECDSA support"
+	depends on DM
+	help
+	  This enables the ECDSA (elliptic curve signature) algorithm for FIT
+	  image verification in U-Boot. The ECDSA algorithm is implemented
+	  using the driver model, so CONFIG_DM is required by this library.
+	  See doc/uImage.FIT/signature.txt for more details.
+	  ECDSA is enabled for mkimage regardless of this option.
+
+if ECDSA
+
+config ECDSA_VERIFY
+	bool "Enable ECDSA verification support in U-Boot."
+	help
+	  Allow ECDSA signatures to be recognized and verified in U-Boot.
+
+config SPL_ECDSA_VERIFY
+	bool "Enable ECDSA verification support in SPL"
+	help
+	  Allow ECDSA signatures to be recognized and verified in SPL.
+
+endif
lib/ecdsa/Makefile new file +1 -0
new file mode 100644
index 0000000000..771d6d3135
@@ -0,0 +1 @@
+obj-$(CONFIG_$(SPL_)ECDSA_VERIFY) += ecdsa-verify.o
lib/ecdsa/ecdsa-verify.c new file +134 -0

134 lines not on this page · raw message

Signature · 1 line
2.31.1
Reviewed-by on v5 2/5
#15 lore raw

Re: [PATCH 2/5] lib: ecdsa: Implement UCLASS_ECDSA verification on target

Hi,

Quoted 14 lines · Alexandru Gagniuc

Implement the crypto_algo .verify() function for ecdsa256. Because it backends on UCLASS_ECDSA, this change is focused on parsing the keys from devicetree and passing this information to the specific UCLASS driver.

---
  lib/Kconfig              |   1 +
  lib/Makefile             |   1 +
  lib/ecdsa/Kconfig        |  23 +++++++
  lib/ecdsa/Makefile       |   1 +
  lib/ecdsa/ecdsa-verify.c | 134 +++++++++++++++++++++++++++++++++++++++
  5 files changed, 160 insertions(+)
Quoted 4 lines

create mode 100644 lib/ecdsa/Kconfig
create mode 100644 lib/ecdsa/Makefile
create mode 100644 lib/ecdsa/ecdsa-verify.c

Thanks
Patrick

#16 lore raw

PATCH v5 3/5 arm: stm32mp1: Implement ECDSA signature verification

In reply to Alexandru Gagniuc

The STM32MP ROM provides several service. One of them is the ability to verify ecdsa256 signatures. Hook the ROM API into the ECDSA uclass.

---
 arch/arm/mach-stm32mp/Kconfig        |   9 +++
 arch/arm/mach-stm32mp/Makefile       |   1 +
 arch/arm/mach-stm32mp/ecdsa_romapi.c | 102 +++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)
 create mode 100644 arch/arm/mach-stm32mp/ecdsa_romapi.c
arch/arm/mach-stm32mp/Kconfig +9 -0
index ace07fd70f..4c1eeef165 100644
@@ -172,6 +172,15 @@ config STM32_ETZPC
 	help
 	  Say y to enable STM32 Extended TrustZone Protection
 
+config STM32_ECDSA_VERIFY
+	bool "STM32 ECDSA verification via the ROM API"
+	depends on SPL_ECDSA_VERIFY
+	default y
+	help
+	  Say y to enable the uclass driver for ECDSA verification using the
+	  ROM API provided on STM32MP.
+	  The ROM API is only available during SPL for now.
+
 config CMD_STM32KEY
 	bool "command stm32key to fuse public key hash"
 	default n
arch/arm/mach-stm32mp/Makefile +1 -0
index 879c1961fe..391b47cf13 100644
@@ -11,6 +11,7 @@ obj-y += bsec.o
 ifdef CONFIG_SPL_BUILD
 obj-y += spl.o
 obj-y += tzc400.o
+obj-$(CONFIG_STM32_ECDSA_VERIFY) += ecdsa_romapi.o
 else
 obj-y += cmd_stm32prog/
 obj-$(CONFIG_CMD_STM32KEY) += cmd_stm32key.o
arch/arm/mach-stm32mp/ecdsa_romapi.c new file +102 -0

102 lines not on this page · raw message

Signature · 1 line
2.31.1
Reviewed-by on v5 3/5
#17 lore raw

Re: [PATCH 3/5] arm: stm32mp1: Implement ECDSA signature verification

Hi,

Quoted 11 lines · Alexandru Gagniuc

The STM32MP ROM provides several service. One of them is the ability to verify ecdsa256 signatures. Hook the ROM API into the ECDSA uclass.

---
  arch/arm/mach-stm32mp/Kconfig        |   9 +++
  arch/arm/mach-stm32mp/Makefile       |   1 +
  arch/arm/mach-stm32mp/ecdsa_romapi.c | 102 +++++++++++++++++++++++++++
  3 files changed, 112 insertions(+)

create mode 100644 arch/arm/mach-stm32mp/ecdsa_romapi.c

Thanks
Patrick

#18 lore raw

Re: [PATCH 3/5] arm: stm32mp1: Implement ECDSA signature verification

Hi Alexandru

Quoted 15 lines · Patrick DELAUNAY

Hi,

Quoted 11 lines · Alexandru Gagniuc

The STM32MP ROM provides several service. One of them is the ability to verify ecdsa256 signatures. Hook the ROM API into the ECDSA uclass.

---
  arch/arm/mach-stm32mp/Kconfig        |   9 +++ arch/arm/mach-stm32mp/Makefile       |   1 + arch/arm/mach-stm32mp/ecdsa_romapi.c | 102 +++++++++++++++++++++++++++

  3 files changed, 112 insertions(+)

  create mode 100644 arch/arm/mach-stm32mp/ecdsa_romapi.c

Quoted 5 lines

Thanks
Patrick

Applied to u-boot-stm/master

Thanks
Patrice

#19 lore raw

PATCH v5 4/5 Kconfig: FIT_SIGNATURE should not select RSA_VERIFY

In reply to Alexandru Gagniuc

FIT signatures can now be implemented with ECDSA. The assumption that all FIT images are signed with RSA is no longer valid. Thus, instead of 'select'ing RSA, only 'imply' it. This doesn't change the defaults, but allows one to explicitly disable RSA support.

---
 common/Kconfig.boot | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
common/Kconfig.boot +4 -4
index f39df04bbf..0d4c38402c 100644
@@ -76,8 +76,8 @@ config FIT_SIGNATURE
 	bool "Enable signature verification of FIT uImages"
 	depends on DM
 	select HASH
-	select RSA
-	select RSA_VERIFY
+	imply RSA
+	imply RSA_VERIFY
 	select IMAGE_SIGN_INFO
 	select FIT_FULL_CHECK
 	help
@@ -186,8 +186,8 @@ config SPL_FIT_SIGNATURE
 	select SPL_FIT
 	select SPL_CRYPTO
 	select SPL_HASH_SUPPORT
-	select SPL_RSA
-	select SPL_RSA_VERIFY
+	imply SPL_RSA
+	imply SPL_RSA_VERIFY
 	select SPL_IMAGE_SIGN_INFO
 	select SPL_FIT_FULL_CHECK
 
Signature · 1 line
2.31.1
Reviewed-by on v5 4/5
#20 lore raw

Re: [PATCH 4/5] Kconfig: FIT_SIGNATURE should not select RSA_VERIFY

Hi,

Quoted 5 lines · Alexandru Gagniuc

FIT signatures can now be implemented with ECDSA. The assumption that all FIT images are signed with RSA is no longer valid. Thus, instead of 'select'ing RSA, only 'imply' it. This doesn't change the defaults, but allows one to explicitly disable RSA support.

Quoted 7 lines
---
  common/Kconfig.boot | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)

Thanks
Patrick

#21 lore raw

Re: [PATCH 4/5] Kconfig: FIT_SIGNATURE should not select RSA_VERIFY

Hi Alexandru

Quoted 16 lines · Patrick DELAUNAY

Hi,

Quoted 5 lines · Alexandru Gagniuc

FIT signatures can now be implemented with ECDSA. The assumption that all FIT images are signed with RSA is no longer valid. Thus, instead of 'select'ing RSA, only 'imply' it. This doesn't change the defaults, but allows one to explicitly disable RSA support.

Quoted 7 lines

---
  common/Kconfig.boot | 8 ++++----

  1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 5 lines

Thanks
Patrick

Applied to u-boot-stm/master

Thanks
Patrice

#22 lore raw

PATCH v5 5/5 test: dm: Add test for ECDSA UCLASS support

In reply to Alexandru Gagniuc

This test verifies that ECDSA_UCLASS is implemented, and that ecdsa_verify() works as expected. The definition of "expected" is "does not find a device, and returns -ENODEV".

The lack of a hardware-independent ECDSA implementation prevents us from having one in the sandbox, for now.

---
 configs/sandbox_defconfig |  2 ++
 test/dm/Makefile          |  1 +
 test/dm/ecdsa.c           | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+)
 create mode 100644 test/dm/ecdsa.c
configs/sandbox_defconfig +2 -0
index bcd82f76ff..8bb981d6f5 100644
@@ -299,3 +299,5 @@ CONFIG_TEST_FDTDEC=y
 CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
+CONFIG_ECDSA=y
+CONFIG_ECDSA_VERIFY=y
test/dm/Makefile +1 -0
index d5c42e7643..516f69d61c 100644
@@ -36,6 +36,7 @@ obj-$(CONFIG_DEVRES) += devres.o
 obj-$(CONFIG_DMA) += dma.o
 obj-$(CONFIG_VIDEO_MIPI_DSI) += dsi_host.o
 obj-$(CONFIG_DM_DSA) += dsa.o
+obj-$(CONFIG_ECDSA_VERIFY) += ecdsa.o
 obj-$(CONFIG_DM_ETH) += eth.o
 ifneq ($(CONFIG_EFI_PARTITION),)
 obj-$(CONFIG_FASTBOOT_FLASH_MMC) += fastboot.o
test/dm/ecdsa.c new file +38 -0
new file mode 100644
index 0000000000..da535c98b5
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <crypto/ecdsa-uclass.h>
+#include <dm.h>
+#include <dm/test.h>
+#include <test/ut.h>
+#include <u-boot/ecdsa.h>
+
+/*
+ * Basic test of the ECDSA uclass and ecdsa_verify()
+ *
+ * ECDSA implementations in u-boot are hardware-dependent. Until we have a
+ * software implementation that can be compiled into the sandbox, all we can
+ * test is the uclass support.
+ *
+ * The uclass_get() test is redundant since ecdsa_verify() would also fail. We
+ * run both functions in order to isolate the cause more clearly. i.e. is
+ * ecdsa_verify() failing because the UCLASS is absent/broken?
+ */
+static int dm_test_ecdsa_verify(struct unit_test_state *uts)
+{
+	struct uclass *ucp;
+
+	struct checksum_algo algo = {
+		.checksum_len = 256,
+	};
+
+	struct image_sign_info info = {
+		.checksum = &algo,
+	};
+
+	ut_assertok(uclass_get(UCLASS_ECDSA, &ucp));
+	ut_assertnonnull(ucp);
+	ut_asserteq(-ENODEV, ecdsa_verify(&info, NULL, 0, NULL, 0));
+
+	return 0;
+}
+DM_TEST(dm_test_ecdsa_verify, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Signature · 1 line
2.31.1
Reviewed-by on v5 5/5
#23 lore raw

Re: [PATCH 5/5] test: dm: Add test for ECDSA UCLASS support

Hi,

Quoted 7 lines · Alexandru Gagniuc

This test verifies that ECDSA_UCLASS is implemented, and that ecdsa_verify() works as expected. The definition of "expected" is "does not find a device, and returns -ENODEV".

The lack of a hardware-independent ECDSA implementation prevents us from having one in the sandbox, for now.

Quoted 9 lines
---
  configs/sandbox_defconfig |  2 ++
  test/dm/Makefile          |  1 +
  test/dm/ecdsa.c           | 38 ++++++++++++++++++++++++++++++++++++++
  3 files changed, 41 insertions(+)

create mode 100644 test/dm/ecdsa.c

Thanks
Patrick

#24 lore raw

Re: [PATCH 5/5] test: dm: Add test for ECDSA UCLASS support

Hi Alexandru

Quoted 20 lines · Patrick DELAUNAY

Hi,

Quoted 7 lines · Alexandru Gagniuc

This test verifies that ECDSA_UCLASS is implemented, and that ecdsa_verify() works as expected. The definition of "expected" is "does not find a device, and returns -ENODEV".

The lack of a hardware-independent ECDSA implementation prevents us from having one in the sandbox, for now.

Quoted 9 lines

---
  configs/sandbox_defconfig |  2 ++
  test/dm/Makefile          |  1 +
  test/dm/ecdsa.c           | 38 ++++++++++++++++++++++++++++++++++++++

  3 files changed, 41 insertions(+)

  create mode 100644 test/dm/ecdsa.c

Quoted 5 lines

Thanks
Patrick

Applied to u-boot-stm/master

Thanks
Patrice