Skip to contentSkip to conversation

add software ecdsa support

RFC PATCH Superseded Philippe Reynes 4 participants 16 messages Feb 2 – Feb 15 Crypto API

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

cover add software ecdsa support

This serie adds the support of ecdsa with software using mbedtls. So boards without ecdsa hardware may also use signature with ecdsa.

To add the support of ecdsa with mbedtls, I have:
- enabled ecdsa in mbedtls
- add a function sw_ecdsa_verify that uses mbedtls
- add a driver sw_ecdsa that call sw_ecdsa_verify

I have tested this code with sandbox, and I have followed those steps:

0) build u-boot using sandbox_defconfig and adding those options:
CONFIG_ECDSA_SW=y
CONFIG_ECDSA_MBEDTLS=y
CONFIG_ECDSA=y
CONFIG_ECDSA_VERIFY=y

1) add a signature node to an its file

	signature-256 {
		algo = "sha256,ecdsa256";
		key-name-hint = "private-key-256";
	};

2) generate an ecdsa key
openssl ecparam -name prime256v1 -genkey -noout -out private-key-256.pem

3) create the itb file
./tools/mkimage -f <file.its> -k . -K arch/sandbox/dts/test.dtb <file.itb>

4) launch sandbox u-boot

./u-boot -d arch/sandbox/dts/test.dtb

5) on sandbox u-boot prompt, load the itb and launch bootm on it

=> host load hostfs - 1000000 uboot-ecdsa.itb
4628674 bytes read in 1 ms (4.3 GiB/s)
=> bootm 1000000
...
...
Verifying Hash Integrity ... sha256,ecdsa256:private-key-256+ OK

I have tested with success ecdsa256 and ecdsa384, but there is an issue with secp521r1.

Philippe Reynes (4):
mbedtls: enable support of ecc
ecdsa: initial support of ecdsa using mbedtls
test: lib: sw_ecdsa: add initial test
drivers: crypto: add software ecdsa support

 drivers/crypto/Kconfig             |   2 +
 drivers/crypto/Makefile            |   1 +
 drivers/crypto/ecdsa/Kconfig       |   6 +
 drivers/crypto/ecdsa/Makefile      |   6 +
 drivers/crypto/ecdsa/ecdsa-sw.c    |  33 +++
 include/crypto/internal/sw_ecdsa.h |  14 +
 lib/mbedtls/Kconfig                |   8 +
 lib/mbedtls/Makefile               |  10 +
 lib/mbedtls/mbedtls_def_config.h   |  18 ++
 lib/mbedtls/sw_ecdsa.c             |  94 ++++++
 test/lib/Makefile                  |   1 +
 test/lib/sw_ecdsa.c                | 445 +++++++++++++++++++++++++++++
 12 files changed, 638 insertions(+)
 create mode 100644 drivers/crypto/ecdsa/Kconfig
 create mode 100644 drivers/crypto/ecdsa/Makefile
 create mode 100644 drivers/crypto/ecdsa/ecdsa-sw.c
 create mode 100644 include/crypto/internal/sw_ecdsa.h
 create mode 100644 lib/mbedtls/sw_ecdsa.c
 create mode 100644 test/lib/sw_ecdsa.c
Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH 1/4 mbedtls: enable support of ecc

Enables the support of ecc in mbedtls.

---
 lib/mbedtls/Kconfig              |  8 ++++++++
 lib/mbedtls/Makefile             |  7 +++++++
 lib/mbedtls/mbedtls_def_config.h | 18 ++++++++++++++++++
 3 files changed, 33 insertions(+)
lib/mbedtls/Kconfig +8 -0
index 789721ee6cd..90c007df850 100644
@@ -157,6 +157,7 @@ config MBEDTLS_LIB_CRYPTO
 	select SHA256_MBEDTLS if SHA256
 	select SHA512_MBEDTLS if SHA512
 	select SHA384_MBEDTLS if SHA384
+	select ECDSA_MBEDTLS if ECDSA
 	help
 	  Enable MbedTLS native crypto libraries.
 	  Mutually incompatible with MBEDTLS_LIB_HASHING_ALT.
@@ -231,6 +232,13 @@ config HKDF_MBEDTLS
 	  This option enables support of key derivation using HKDF algorithm
 	  with MbedTLS crypto library.
 
+config ECDSA_MBEDTLS
+	bool "Enable ECDSA support with MbedTLS crypto library"
+	depends on MBEDTLS_LIB_CRYPTO && ECDSA
+	help
+	  This option enables support of ECDSA with the MbedTLS crypto
+	  library.
+
 endif # MBEDTLS_LIB_CRYPTO
 
 config MBEDTLS_LIB_X509
lib/mbedtls/Makefile +7 -0
index c5b445bd85c..54a893609cf 100644
@@ -35,6 +35,11 @@ mbedtls_lib_crypto-$(CONFIG_$(PHASE_)SHA512_MBEDTLS) += \
 	$(MBEDTLS_LIB_DIR)/sha512.o
 mbedtls_lib_crypto-$(CONFIG_$(PHASE_)HKDF_MBEDTLS) += \
 	$(MBEDTLS_LIB_DIR)/hkdf.o
+mbedtls_lib_crypto-$(CONFIG_$(PHASE_)ECDSA_MBEDTLS) += \
+	$(MBEDTLS_LIB_DIR)/ecdsa.o \
+	$(MBEDTLS_LIB_DIR)/ecp.o \
+	$(MBEDTLS_LIB_DIR)/ecp_curves.o \
+	$(MBEDTLS_LIB_DIR)/ecp_curves_new.o
 
 # MbedTLS X509 library
 obj-$(CONFIG_$(XPL_)MBEDTLS_LIB_X509) += mbedtls_lib_x509.o
@@ -52,6 +57,8 @@ mbedtls_lib_x509-$(CONFIG_$(PHASE_)ASYMMETRIC_PUBLIC_KEY_MBEDTLS) += \
 	$(MBEDTLS_LIB_DIR)/pk.o \
 	$(MBEDTLS_LIB_DIR)/pk_wrap.o \
 	$(MBEDTLS_LIB_DIR)/pkparse.o
+mbedtls_lib_x509-$(CONFIG_$(PHASE_)ECDSA_MBEDTLS) += \
+	$(MBEDTLS_LIB_DIR)/pk_ecc.o
 mbedtls_lib_x509-$(CONFIG_$(PHASE_)X509_CERTIFICATE_PARSER_MBEDTLS) += \
 	$(MBEDTLS_LIB_DIR)/x509_crl.o \
 	$(MBEDTLS_LIB_DIR)/x509_crt.o
lib/mbedtls/mbedtls_def_config.h +18 -0
index dda3f4dd6e4..8f366c744a5 100644
@@ -60,6 +60,24 @@
 #define MBEDTLS_HKDF_C
 #endif
 
+#if CONFIG_IS_ENABLED(ECDSA)
+#define MBEDTLS_ECDSA_C
+#define MBEDTLS_ECP_C
+#define MBEDTLS_BIGNUM_C
+#define MBEDTLS_ECP_DP_SECP256K1_ENABLED
+#define MBEDTLS_ECP_DP_SECP192R1_ENABLED
+#define MBEDTLS_ECP_DP_SECP224R1_ENABLED
+#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
+#define MBEDTLS_ECP_DP_SECP384R1_ENABLED
+#define MBEDTLS_ECP_DP_SECP521R1_ENABLED
+#define MBEDTLS_ECP_DP_SECP192K1_ENABLED
+#define MBEDTLS_ECP_DP_SECP224K1_ENABLED
+#define MBEDTLS_ECP_DP_SECP256K1_ENABLED
+#define MBEDTLS_ECP_DP_BP256R1_ENABLED
+#define MBEDTLS_ECP_DP_BP384R1_ENABLED
+#define MBEDTLS_ECP_DP_BP512R1_ENABLED
+#endif
+
 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
 
 #if CONFIG_IS_ENABLED(X509_CERTIFICATE_PARSER)
Signature · 1 line
2.43.0
#2 lore raw

Re: [RFC PATCH 1/4] mbedtls: enable support of ecc

Hi Philippe,

Quoted 23 lines · Philippe Reynes

Enables the support of ecc in mbedtls.

---
 lib/mbedtls/Kconfig              |  8 ++++++++
 lib/mbedtls/Makefile             |  7 +++++++
 lib/mbedtls/mbedtls_def_config.h | 18 ++++++++++++++++++
 3 files changed, 33 insertions(+)
lib/mbedtls/Kconfig +1 -0
index 789721ee6cd..90c007df850 100644
@@ -157,6 +157,7 @@ config MBEDTLS_LIB_CRYPTO
        select SHA256_MBEDTLS if SHA256
        select SHA512_MBEDTLS if SHA512
        select SHA384_MBEDTLS if SHA384
+       select ECDSA_MBEDTLS if ECDSA
        help
          Enable MbedTLS native crypto libraries.
          Mutually incompatible with MBEDTLS_LIB_HASHING_ALT.

lib/mbedtls/Kconfig config HKDF_MBEDTLS · line 241

Quoted 10 lines
lib/mbedtls/Kconfig +7 -0
@@ -231,6 +232,13 @@ config HKDF_MBEDTLS
          This option enables support of key derivation using HKDF algorithm
          with MbedTLS crypto library.
 
+config ECDSA_MBEDTLS
+       bool "Enable ECDSA support with MbedTLS crypto library"
+       depends on MBEDTLS_LIB_CRYPTO && ECDSA
+       help
+         This option enables support of ECDSA with the MbedTLS crypto
+         library.
+

ECDSA config should be under MBEDTLS_LIB_X509 instead og MBEDTLS_LIB_CRYPTO.

Quoted 8 lines
lib/mbedtls/Kconfig +0 -0
 endif # MBEDTLS_LIB_CRYPTO
 
 config MBEDTLS_LIB_X509
lib/mbedtls/Makefile +0 -0
index c5b445bd85c..54a893609cf 100644

lib/mbedtls/Makefile mbedtls_lib_crypto-$(CONFIG_$(PHASE_)SHA512_MBEDTLS) += \ · line 42

Quoted 8 lines
lib/mbedtls/Makefile +5 -0
@@ -35,6 +35,11 @@ mbedtls_lib_crypto-$(CONFIG_$(PHASE_)SHA512_MBEDTLS) += \
        $(MBEDTLS_LIB_DIR)/sha512.o
 mbedtls_lib_crypto-$(CONFIG_$(PHASE_)HKDF_MBEDTLS) += \
        $(MBEDTLS_LIB_DIR)/hkdf.o
+mbedtls_lib_crypto-$(CONFIG_$(PHASE_)ECDSA_MBEDTLS) += \
+       $(MBEDTLS_LIB_DIR)/ecdsa.o \
+       $(MBEDTLS_LIB_DIR)/ecp.o \
+       $(MBEDTLS_LIB_DIR)/ecp_curves.o \
+       $(MBEDTLS_LIB_DIR)/ecp_curves_new.o

Currently these ecp*.o are imported by MBEDTLS_LIB_TLS, refactoring is required to avoid duplications when multiple kconfigs are selected.

lib/mbedtls/Makefile mbedtls_lib_x509-$(CONFIG_$(PHASE_)ASYMMETRIC_PUBLIC_KEY_MBEDTLS) += \ · line 61

Quoted 9 lines
lib/mbedtls/Makefile +2 -0
 # MbedTLS X509 library
 obj-$(CONFIG_$(XPL_)MBEDTLS_LIB_X509) += mbedtls_lib_x509.o
@@ -52,6 +57,8 @@ mbedtls_lib_x509-$(CONFIG_$(PHASE_)ASYMMETRIC_PUBLIC_KEY_MBEDTLS) += \
        $(MBEDTLS_LIB_DIR)/pk.o \
        $(MBEDTLS_LIB_DIR)/pk_wrap.o \
        $(MBEDTLS_LIB_DIR)/pkparse.o
+mbedtls_lib_x509-$(CONFIG_$(PHASE_)ECDSA_MBEDTLS) += \
+       $(MBEDTLS_LIB_DIR)/pk_ecc.o

ditto.

Quoted 19 lines
lib/mbedtls/Makefile +0 -0
 mbedtls_lib_x509-$(CONFIG_$(PHASE_)X509_CERTIFICATE_PARSER_MBEDTLS) += \
        $(MBEDTLS_LIB_DIR)/x509_crl.o \
        $(MBEDTLS_LIB_DIR)/x509_crt.o
lib/mbedtls/mbedtls_def_config.h +8 -0
index dda3f4dd6e4..8f366c744a5 100644
@@ -60,6 +60,24 @@
 #define MBEDTLS_HKDF_C
 #endif
 
+#if CONFIG_IS_ENABLED(ECDSA)
+#define MBEDTLS_ECDSA_C
+#define MBEDTLS_ECP_C
+#define MBEDTLS_BIGNUM_C
+#define MBEDTLS_ECP_DP_SECP256K1_ENABLED
+#define MBEDTLS_ECP_DP_SECP192R1_ENABLED
+#define MBEDTLS_ECP_DP_SECP224R1_ENABLED
+#define MBEDTLS_ECP_DP_SECP256R1_ENABLED

lib/mbedtls/mbedtls_def_config.h line 80

Quoted 10 lines
lib/mbedtls/mbedtls_def_config.h +10 -0
+#define MBEDTLS_ECP_DP_SECP384R1_ENABLED
+#define MBEDTLS_ECP_DP_SECP521R1_ENABLED
+#define MBEDTLS_ECP_DP_SECP192K1_ENABLED
+#define MBEDTLS_ECP_DP_SECP224K1_ENABLED
+#define MBEDTLS_ECP_DP_SECP256K1_ENABLED
+#define MBEDTLS_ECP_DP_BP256R1_ENABLED
+#define MBEDTLS_ECP_DP_BP384R1_ENABLED
+#define MBEDTLS_ECP_DP_BP512R1_ENABLED
+#endif
+

Currently they are enabled by MBEDTLS_LIB_TLS, refactoring is required to select ECDSA when MBEDTLS_LIB_TLS is on, to avoid duplicated defining macros here.

Regards,
Raymond

Quoted 6 lines
lib/mbedtls/mbedtls_def_config.h +0 -1
 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
 
 #if CONFIG_IS_ENABLED(X509_CERTIFICATE_PARSER)
--

2.43.0

#3 lore raw

RFC PATCH 2/4 ecdsa: initial support of ecdsa using mbedtls

In reply to Philippe Reynes

Adds an initial support of ecdsa verify using mbedtls.

---
 include/crypto/internal/sw_ecdsa.h | 14 +++++
 lib/mbedtls/Makefile               |  3 +
 lib/mbedtls/sw_ecdsa.c             | 94 ++++++++++++++++++++++++++++++
 3 files changed, 111 insertions(+)
 create mode 100644 include/crypto/internal/sw_ecdsa.h
 create mode 100644 lib/mbedtls/sw_ecdsa.c
include/crypto/internal/sw_ecdsa.h new file +14 -0
new file mode 100644
index 00000000000..b1ca31da0f8
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2026, Philippe Reynes <[email protected]>
+ */
+#ifndef _SW_ECDSA
+#define _SW_ECDSA
+
+struct ecdsa_public_key;
+
+int sw_ecdsa_verify(const struct ecdsa_public_key *pubkey,
+		    const void *hash, size_t hash_len,
+		    const void *signature, size_t sig_len);
+
+#endif
lib/mbedtls/Makefile +3 -0
index 54a893609cf..a5331313a60 100644
@@ -11,6 +11,9 @@ obj-$(CONFIG_$(PHASE_)SHA1_MBEDTLS) += sha1.o
 obj-$(CONFIG_$(PHASE_)SHA256_MBEDTLS) += sha256.o
 obj-$(CONFIG_$(PHASE_)SHA512_MBEDTLS) += sha512.o
 
+# shim layer for crypto
+obj-$(CONFIG_$(PHASE_)ECDSA_MBEDTLS) += sw_ecdsa.o
+
 # x509 libraries
 obj-$(CONFIG_$(PHASE_)ASYMMETRIC_PUBLIC_KEY_MBEDTLS) += \
 	public_key.o
lib/mbedtls/sw_ecdsa.c new file +94 -0

94 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

RFC PATCH 3/4 test: lib: sw_ecdsa: add initial test

In reply to Philippe Reynes

Adds tests to check that the function sw_ecdsa_verify using mbedtls is valid.

---
 test/lib/Makefile   |   1 +
 test/lib/sw_ecdsa.c | 445 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 446 insertions(+)
 create mode 100644 test/lib/sw_ecdsa.c
test/lib/Makefile +1 -0
index f25383a40e5..8837446968b 100644
@@ -8,6 +8,7 @@ obj-$(CONFIG_$(PHASE_)UT_COMPRESSION) += compression.o
 ifeq ($(CONFIG_XPL_BUILD),)
 obj-y += abuf.o
 obj-y += alist.o
+obj-$(CONFIG_ECDSA_MBEDTLS) += sw_ecdsa.o
 obj-$(CONFIG_EFI_LOADER) += efi_device_path.o efi_memory.o
 obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o
 ifdef CONFIG_RISCV
test/lib/sw_ecdsa.c new file +445 -0

445 lines not on this page · raw message

Signature · 1 line
2.43.0
#5 lore raw

RFC PATCH 4/4 drivers: crypto: add software ecdsa support

In reply to Philippe Reynes

Add an software ecdsa driver so it is
now possible to use ecdsa signature on
board without ecdsa hardware support.

---
 drivers/crypto/Kconfig          |  2 ++
 drivers/crypto/Makefile         |  1 +
 drivers/crypto/ecdsa/Kconfig    |  6 ++++++
 drivers/crypto/ecdsa/Makefile   |  6 ++++++
 drivers/crypto/ecdsa/ecdsa-sw.c | 33 +++++++++++++++++++++++++++++++++
 5 files changed, 48 insertions(+)
 create mode 100644 drivers/crypto/ecdsa/Kconfig
 create mode 100644 drivers/crypto/ecdsa/Makefile
 create mode 100644 drivers/crypto/ecdsa/ecdsa-sw.c
drivers/crypto/Kconfig +2 -0
index 0d58e3910fe..cf49e5c0f7e 100644
@@ -12,4 +12,6 @@ source "drivers/crypto/nuvoton/Kconfig"
 
 source "drivers/crypto/tegra/Kconfig"
 
+source "drivers/crypto/ecdsa/Kconfig"
+
 endmenu
drivers/crypto/Makefile +1 -0
index e4a4482b7f3..8170e4cae9c 100644
@@ -4,6 +4,7 @@
 #	http://www.samsung.com
 
 obj-$(CONFIG_EXYNOS_ACE_SHA)	+= ace_sha.o
+obj-$(CONFIG_ECDSA) += ecdsa/
 obj-y += aes/
 obj-y += rsa_mod_exp/
 obj-y += fsl/
drivers/crypto/ecdsa/Kconfig new file +6 -0
new file mode 100644
index 00000000000..308824d8421
@@ -0,0 +1,6 @@
+config ECDSA_SW
+	bool "Enable driver for ECDSA in software"
+	depends on ECDSA_MBEDTLS
+	help
+	  Enable driver for ECDSA operations in software. Currently
+	  it supports multiple ECDSA algorithm.
drivers/crypto/ecdsa/Makefile new file +6 -0
new file mode 100644
index 00000000000..8f9e5a767f8
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2026 Philippe Reynes <[email protected]>
+#
+
+obj-$(CONFIG_ECDSA_SW) += ecdsa-sw.o
drivers/crypto/ecdsa/ecdsa-sw.c new file +33 -0
new file mode 100644
index 00000000000..0d526371ecb
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2026 Philippe Reynes <[email protected]>
+ */
+#include <dm/device.h>
+#include <linux/types.h>
+#include <u-boot/ecdsa.h>
+#include <crypto/ecdsa-uclass.h>
+#include <dm/platdata.h>
+#include <crypto/internal/sw_ecdsa.h>
+
+static int ops_sw_ecdsa_verify(__always_unused struct udevice *dev,
+			       const struct ecdsa_public_key *pubkey,
+			       const void *hash, size_t hash_len,
+			       const void *signature, size_t sig_len)
+{
+	return sw_ecdsa_verify(pubkey, hash, hash_len, signature, sig_len);
+}
+
+static const struct ecdsa_ops sw_ecdsa_ops = {
+	.verify = ops_sw_ecdsa_verify,
+};
+
+U_BOOT_DRIVER(sw_ecdsa) = {
+	.name	= "sw_ecdsa",
+	.id	= UCLASS_ECDSA,
+	.ops	= &sw_ecdsa_ops,
+	.flags	= DM_FLAG_PRE_RELOC,
+};
+
+U_BOOT_DRVINFO(sw_ecdsa) = {
+	.name = "sw_ecdsa",
+};
Signature · 1 line
2.43.0
#6 lore raw
In reply to Philippe Reynes

Hi Philippe,

Quoted 55 lines · Philippe Reynes

This serie adds the support of ecdsa with software using mbedtls. So boards without ecdsa hardware may also use signature with ecdsa.

To add the support of ecdsa with mbedtls, I have:
- enabled ecdsa in mbedtls
- add a function sw_ecdsa_verify that uses mbedtls
- add a driver sw_ecdsa that call sw_ecdsa_verify

I have tested this code with sandbox, and I have followed those steps:

0) build u-boot using sandbox_defconfig and adding those options:
CONFIG_ECDSA_SW=y
CONFIG_ECDSA_MBEDTLS=y
CONFIG_ECDSA=y
CONFIG_ECDSA_VERIFY=y

1) add a signature node to an its file

        signature-256 {
                algo = "sha256,ecdsa256";
                key-name-hint = "private-key-256";
        };

2) generate an ecdsa key
openssl ecparam -name prime256v1 -genkey -noout -out private-key-256.pem

3) create the itb file
./tools/mkimage -f <file.its> -k . -K arch/sandbox/dts/test.dtb <file.itb>

4) launch sandbox u-boot

./u-boot -d arch/sandbox/dts/test.dtb

5) on sandbox u-boot prompt, load the itb and launch bootm on it

=> host load hostfs - 1000000 uboot-ecdsa.itb
4628674 bytes read in 1 ms (4.3 GiB/s)
=> bootm 1000000
...
...
Verifying Hash Integrity ... sha256,ecdsa256:private-key-256+ OK

I have tested with success ecdsa256 and ecdsa384, but there is an issue with secp521r1.

Philippe Reynes (4):
mbedtls: enable support of ecc
ecdsa: initial support of ecdsa using mbedtls
test: lib: sw_ecdsa: add initial test
drivers: crypto: add software ecdsa support

Quoted 10 lines
 drivers/crypto/Kconfig             |   2 +
 drivers/crypto/Makefile            |   1 +
 drivers/crypto/ecdsa/Kconfig       |   6 +
 drivers/crypto/ecdsa/Makefile      |   6 +
 drivers/crypto/ecdsa/ecdsa-sw.c    |  33 +++
 include/crypto/internal/sw_ecdsa.h |  14 +
 lib/mbedtls/Kconfig                |   8 +
 lib/mbedtls/Makefile               |  10 +
 lib/mbedtls/mbedtls_def_config.h   |  18 ++
 lib/mbedtls/sw_ecdsa.c             |  94 ++++++

Rename it without the "sw_", from the perspective of MbedTLS, HW acceleration is controlled by MBEDTLS_ECDSA_###_ALT, so the interface itself does not imply SW.

Regards,
Raymond

Quoted 13 lines
 test/lib/Makefile                  |   1 +
 test/lib/sw_ecdsa.c                | 445 +++++++++++++++++++++++++++++
 12 files changed, 638 insertions(+)
 create mode 100644 drivers/crypto/ecdsa/Kconfig
 create mode 100644 drivers/crypto/ecdsa/Makefile
 create mode 100644 drivers/crypto/ecdsa/ecdsa-sw.c
 create mode 100644 include/crypto/internal/sw_ecdsa.h
 create mode 100644 lib/mbedtls/sw_ecdsa.c
 create mode 100644 test/lib/sw_ecdsa.c

--
2.43.0

#7 lore raw
In reply to Philippe Reynes
Quoted 44 lines · Philippe Reynes

This serie adds the support of ecdsa with software using mbedtls. So boards without ecdsa hardware may also use signature with ecdsa.

To add the support of ecdsa with mbedtls, I have:
- enabled ecdsa in mbedtls
- add a function sw_ecdsa_verify that uses mbedtls
- add a driver sw_ecdsa that call sw_ecdsa_verify

I have tested this code with sandbox, and I have followed those steps:

0) build u-boot using sandbox_defconfig and adding those options:
CONFIG_ECDSA_SW=y
CONFIG_ECDSA_MBEDTLS=y
CONFIG_ECDSA=y
CONFIG_ECDSA_VERIFY=y

1) add a signature node to an its file

	signature-256 {
		algo = "sha256,ecdsa256";
		key-name-hint = "private-key-256";
	};

2) generate an ecdsa key
openssl ecparam -name prime256v1 -genkey -noout -out private-key-256.pem

3) create the itb file
./tools/mkimage -f <file.its> -k . -K arch/sandbox/dts/test.dtb <file.itb>

4) launch sandbox u-boot

./u-boot -d arch/sandbox/dts/test.dtb

5) on sandbox u-boot prompt, load the itb and launch bootm on it

=> host load hostfs - 1000000 uboot-ecdsa.itb
4628674 bytes read in 1 ms (4.3 GiB/s)
=> bootm 1000000
...
...
Verifying Hash Integrity ... sha256,ecdsa256:private-key-256+ OK

Quoted 2 lines

I have tested with success ecdsa256 and ecdsa384, but there is an issue with secp521r1.

This is good to see. Please work on adding this to CI automatically (which may involve doing something like the sandbox_trace builds/tests rather than just being part of the default sandbox suite).

Signature · 1 line
Tom
#8 lore raw
In reply to Philippe Reynes
Quoted 9 lines · Philippe Reynes

This serie adds the support of ecdsa with software using mbedtls. So boards without ecdsa hardware may also use signature with ecdsa.

To add the support of ecdsa with mbedtls, I have:
- enabled ecdsa in mbedtls
- add a function sw_ecdsa_verify that uses mbedtls
- add a driver sw_ecdsa that call sw_ecdsa_verify

Quoted 8 lines

I have tested this code with sandbox, and I have
followed those steps:

0) build u-boot using sandbox_defconfig and adding those options:
CONFIG_ECDSA_SW=y
CONFIG_ECDSA_MBEDTLS=y
CONFIG_ECDSA=y
CONFIG_ECDSA_VERIFY=y

I did "git am" on top of the master branch as of the current eb1562cc3e4c5130c76db1c1ea57156322362a7c and tried to build it as follows:

make rpi_4_defconfig
scripts/config -e FIT_SIGNATURE -e ECDSA -e SHA256 -e ECDSA_VERIFY \
-d BOOTSTD \
-e MBEDTLS_LIB -e MBEDTLS_LIB_CRYPTO -e ECDSA_MBEDTLS -e ECDSA_SW \
-e SHA256_MBEDTLS -e SHA256_SMALLER -e MBEDTLS_LIB_X509 -d HKDF_MBEDTLS \
-e ASN1_DECODER -e ASN1_DECODER_MBEDTLS \
-d LEGACY_HASHING_AND_CRYPTO &&
make -j$(nproc) CROSS_COMPILE=aarch64-linux-gnu-

No matter which variations of this I try (starting with -e HDKF_MBEDTLS), the build would fail with an #error in lib/mbedtls/external/mbedtls/include/mbedtls/check_config.h because MBEDTLS_ECDSA_C is defined but neither MBEDTLS_ASN1_PARSE_C nor MBEDTLS_ASN1_WRITE_C are defined.

By disabling that check I found out that the functions ecdsa_signature_to_asn1() and mbedtls_ecdsa_read_signature_restartable() really depend on these.

I diagnosed this by executing
make V=1 CROSS_COMPILE=aarch64-linux-gnu-

Then, I edited the compiler command line by replacing "-o *.o -c" with "-E -dD", and redirected the standard output into a file. In that file I found that lib/mbedtls/mbedtls_def_config.h is defining MBEDTLS_ECDSA_C and would also define the ASN1 symbols if CONFIG_ASN1_DECODER were enabled:

#if CONFIG_IS_ENABLED(ASN1_DECODER)
#define MBEDTLS_OID_C
#define MBEDTLS_ASN1_PARSE_C
#define MBEDTLS_ASN1_WRITE_C
#endif

Something is wiping that out from my .config, also when I execute "make syncconfig" after the scripts/config. When I search for ASN1_DECODER in "make menuconfig", it mentions a large number of other configuration options.

Can someone help me to enable CONFIG_ASN1_DECODER in this configuration?

With best regards,

Marko

#9 lore raw

Hi Marko,

Quoted 58 lines · Marko Mäkelä
Quoted 9 lines · Philippe Reynes

This serie adds the support of ecdsa with software using mbedtls. So boards without ecdsa hardware may also use signature with ecdsa.

To add the support of ecdsa with mbedtls, I have:
- enabled ecdsa in mbedtls
- add a function sw_ecdsa_verify that uses mbedtls
- add a driver sw_ecdsa that call sw_ecdsa_verify

Quoted 8 lines

I have tested this code with sandbox, and I have
followed those steps:

0) build u-boot using sandbox_defconfig and adding those options:
CONFIG_ECDSA_SW=y
CONFIG_ECDSA_MBEDTLS=y
CONFIG_ECDSA=y
CONFIG_ECDSA_VERIFY=y

I did "git am" on top of the master branch as of the current eb1562cc3e4c5130c76db1c1ea57156322362a7c and tried to build it as follows:

make rpi_4_defconfig
scripts/config -e FIT_SIGNATURE -e ECDSA -e SHA256 -e ECDSA_VERIFY \

(quoted diff) +0 -5
-d BOOTSTD \
-e MBEDTLS_LIB -e MBEDTLS_LIB_CRYPTO -e ECDSA_MBEDTLS -e ECDSA_SW \
-e SHA256_MBEDTLS -e SHA256_SMALLER -e MBEDTLS_LIB_X509 -d HKDF_MBEDTLS \
-e ASN1_DECODER -e ASN1_DECODER_MBEDTLS \
-d LEGACY_HASHING_AND_CRYPTO &&

make -j$(nproc) CROSS_COMPILE=aarch64-linux-gnu-

No matter which variations of this I try (starting with -e HDKF_MBEDTLS), the build would fail with an #error in lib/mbedtls/external/mbedtls/include/mbedtls/check_config.h because MBEDTLS_ECDSA_C is defined but neither MBEDTLS_ASN1_PARSE_C nor MBEDTLS_ASN1_WRITE_C are defined.

By disabling that check I found out that the functions ecdsa_signature_to_asn1() and mbedtls_ecdsa_read_signature_restartable() really depend on these.

I diagnosed this by executing
make V=1 CROSS_COMPILE=aarch64-linux-gnu-

Then, I edited the compiler command line by replacing "-o *.o -c" with "-E -dD", and redirected the standard output into a file. In that file I found that lib/mbedtls/mbedtls_def_config.h is defining MBEDTLS_ECDSA_C and would also define the ASN1 symbols if CONFIG_ASN1_DECODER were enabled:

#if CONFIG_IS_ENABLED(ASN1_DECODER)
#define MBEDTLS_OID_C
#define MBEDTLS_ASN1_PARSE_C
#define MBEDTLS_ASN1_WRITE_C
#endif

Quoted 7 lines

Something is wiping that out from my .config, also when I execute "make syncconfig" after the scripts/config. When I search for ASN1_DECODER in "make menuconfig", it mentions a large number of other configuration options.

Can someone help me to enable CONFIG_ASN1_DECODER in this configuration?

When EFI_SECURE_BOOT is enabled, all these dependent Kconfigs will be selected automatically.

Raymond

Quoted 3 lines

With best regards,

Marko

#10 lore raw

Hi Raymond,

Quoted 1 line · Raymond Mao

Hi Marko,

[snip]

Quoted 2 lines

When EFI_SECURE_BOOT is enabled, all these dependent Kconfigs will be selected automatically.

Thank you for your help. I can confirm that the following will build the ECDSA_SW implementation:

make sandbox_defconfig
scripts/config -e ECDSA_SW
make syncconfig && grep ASN1 .config
make -j$(nproc)

The redundant "grep" step above would output the following:
CONFIG_ASN1_DECODER_MBEDTLS=y CONFIG_ASN1_COMPILER=y
CONFIG_ASN1_DECODER=y

I still can't enable those in any rpi_4_defconfig based build attempt, such as this one:

cat > configs/rpi_4a_defconfig << EOF
#include <configs/rpi_4_defconfig>
CONFIG_EFI_SECURE_BOOT=y
CONFIG_MBEDTLS_LIB=y
CONFIG_ECDSA_SW=y
CONFIG_ECDSA_MBEDTLS=y
CONFIG_ECDSA=y
CONFIG_ECDSA_VERIFY=y
EOF
make rpi_4a_defconfig
make -j$(nproc) CROSS_COMPILE=aarch64-linux-gnu-

This build fails in the same way as yesterday because none of the ASN1 options will be present in the .config file. Neither will CONFIG_EFI_SECURE_BOOT. Many EFI options were enabled, but not that one.

On a positive note, CONFIG_LEGACY_HASHING_AND_CRYPTO was disabled automatically by the above, and MBEDTLS was enabled, unlike in my earlier attempt about a month ago, using an different u-boot revision.

I also tried to enable several options that CONFIG_EFI_SECURE_BOOT would select in lib/efi_loader/Kconfig, but with no success.

Is there a way to get some diagnostics that explains why Kconfig refuses to enable a particular option?

With best regards,

Marko

#11 lore raw

Hi Marko,

Quoted 45 lines · Marko Mäkelä

Hi Raymond,

Quoted 1 line · Raymond Mao

Hi Marko,

[snip]

Quoted 2 lines

When EFI_SECURE_BOOT is enabled, all these dependent Kconfigs will be selected automatically.

Thank you for your help. I can confirm that the following will build the ECDSA_SW implementation:

make sandbox_defconfig
scripts/config -e ECDSA_SW
make syncconfig && grep ASN1 .config
make -j$(nproc)

The redundant "grep" step above would output the following:
CONFIG_ASN1_DECODER_MBEDTLS=y CONFIG_ASN1_COMPILER=y
CONFIG_ASN1_DECODER=y

I still can't enable those in any rpi_4_defconfig based build attempt, such as this one:

cat > configs/rpi_4a_defconfig << EOF
#include <configs/rpi_4_defconfig>
CONFIG_EFI_SECURE_BOOT=y
CONFIG_MBEDTLS_LIB=y
CONFIG_ECDSA_SW=y
CONFIG_ECDSA_MBEDTLS=y
CONFIG_ECDSA=y
CONFIG_ECDSA_VERIFY=y
EOF
make rpi_4a_defconfig
make -j$(nproc) CROSS_COMPILE=aarch64-linux-gnu-

This build fails in the same way as yesterday because none of the ASN1 options will be present in the .config file. Neither will CONFIG_EFI_SECURE_BOOT. Many EFI options were enabled, but not that one.

On a positive note, CONFIG_LEGACY_HASHING_AND_CRYPTO was disabled automatically by the above, and MBEDTLS was enabled, unlike in my earlier attempt about a month ago, using an different u-boot revision.

Quoted 6 lines

I also tried to enable several options that CONFIG_EFI_SECURE_BOOT would select in lib/efi_loader/Kconfig, but with no success.

Is there a way to get some diagnostics that explains why Kconfig refuses to enable a particular option?

Can you take a reference on 'sandbox_defconfig', it enables:

```
CONFIG_EFI_SECURE_BOOT=y
...
CONFIG_MBEDTLS_LIB=y
```

Then all dependent Kconfigs are selected automatically when you do make sandbox_defconfig.

Regards,
Raymond

Quoted 3 lines

With best regards,

Marko

#12 lore raw
In reply to Raymond Mao

Wed, Feb 04, 2026 at 02:28:53PM -0500, Raymond Mao wrote:
[snip]

Quoted 1 line

When EFI_SECURE_BOOT is enabled, all these dependent Kconfigs will be selected automatically.

I finally solved my problem by using "make menuconfig".

It turns out that CONFIG_FIT is not defined in rpi_4_defconfig. That is why some requested configuration was being disregarded. The build succeeds with the following:

cat > boot/rpi_4_ecdsa_defconfig << "EOF"
#include <configs/rpi_4_defconfig>
CONFIG_FIT=y
CONFIG_FIT_SIGNATURE=y
CONFIG_MBEDTLS_LIB=y
CONFIG_ECDSA=y
CONFIG_ECDSA_SW=y
CONFIG_ECDSA_VERIFY=y
CONFIG_EFI_SECURE_BOOT=y
CONFIG_EFI_LOADER=y
EOF
make rpi_4_ecdsa_defconfig
make -j$(nproc) CROSS_COMPILE=aarch64-linux-gnu-

I am yet to build an ECDSA signed fitImage of Linux and the device tree, so I did not actually test this implementation yet.

With best regards,

Marko

#13 lore raw
In reply to Philippe Reynes
Quoted 8 lines · Philippe Reynes

I have tested this code with sandbox, and I have
followed those steps:

0) build u-boot using sandbox_defconfig and adding those options:
CONFIG_ECDSA_SW=y
CONFIG_ECDSA_MBEDTLS=y
CONFIG_ECDSA=y
CONFIG_ECDSA_VERIFY=y

I believe that I was able to build an ECDSA signed fitImage of a Linux kernel. At least "dtc" shows that a signature is present, just like with my earlier attempt with RSA.

Quoted 11 lines

1) add a signature node to an its file

	signature-256 {
		algo = "sha256,ecdsa256";
		key-name-hint = "private-key-256";
	};

2) generate an ecdsa key
openssl ecparam -name prime256v1 -genkey -noout -out private-key-256.pem

3) create the itb file
./tools/mkimage -f <file.its> -k . -K arch/sandbox/dts/test.dtb <file.itb>

Step 1) is part of <file.its>, which specifies how a signed payload, such as a Linux kernel, is built in <file.itb>, right?

I assume that arch/sandbox/dts/test.dts is the source code for arch/sandbox/dts/test.dtb. Would this file correspond to the file u-boot.dtb in a non-sandbox environment (in my case, based on rpi_4_defconfig)?

For me, mkimage version 2025.01 (as shipped in Debian Sid) would crash if I ask it to write the public key to u-boot.dtb using the parameter "-K u-boot.dtb". The following statement in do_add() would hit SIGSEGV:

ret = fdt_setprop_string(fdt, key_node, FIT_KEY_REQUIRED,
info->require_keys);

The function do_add() is invoked by ecdsa_add_verify_data(). For my kernel build, I did not yet try a mkimage that is built from the latest u-boot. Should that make a difference?

For an earlier experiment with an RSA signed fitImage, I was able to do the following:

make -j$(nproc) CROSS_COMPILE=aarch64-linux-gnu- all u-boot.dtb

cp u-boot.dtb u-boot-pubkey.dtb
ALGO=$(scripts/dtc/dtc -I dtb /target/fitImage |grep -A10 signature| sed -ne "s/\s*algo = \"\(.*\)\";/\1/p") tools/fdt_add_pubkey -a "$ALGO" -n dev -k . -r conf u-boot-pubkey.dtb tools/fit_check_sign -f /target/fitImage -k u-boot-pubkey.dtb

make -j$(nproc) CROSS_COMPILE=aarch64-linux-gnu- \
EXT_DTB=u-boot-pubkey.dtb

cp -L u-boot.img /target/

With CONFIG_ECDSA, fdt_add_pubkey would SIGSEGV (unrelated to these changes) and fit_check_sign does not appear to be built.

I would appreciate some help in embedding the ECDSA public key to the u-boot image, so that I can test this.

Best regards,

Marko

#14 lore raw
Quoted 6 lines · Marko Mäkelä

For me, mkimage version 2025.01 (as shipped in Debian Sid) would crash if I ask it to write the public key to u-boot.dtb using the parameter "-K u-boot.dtb". The following statement in do_add() would hit SIGSEGV:

ret = fdt_setprop_string(fdt, key_node, FIT_KEY_REQUIRED,
info->require_keys);

The function do_add() is invoked by ecdsa_add_verify_data(). For my kernel build, I did not yet try a mkimage that is built from the latest u-boot. Should that make a difference?

Apparently, something has been fixed since the 2025.01 release. The following would work for me with a current u-boot build:

echo "/dts-v1/; / { description = \"\"; images {}; };" > public-key.its mkimage -f public-key.its public-key.dtb
mkimage -f fitImage.its -k . -K public-key.dtb fitImage

With the mkimage 2025.01 that is included in the Debian Sid u-boot-tools, I am able to build an unsigned Linux fitImage:
mkimage -f fitImage.its fitImage

Then I can invoke a freshly compiled mkimage to sign it and include the corresponding public ECDSA key in an u-boot image:
mkimage -r -k . -K u-boot.dtb -F fitImage
cat u-boot-nodtb.bin u-boot.dtb > u-boot.bin

However, this will not work on the Raspberry Pi 4, which defines CONFIG_OF_BOARD. I came up with an idea of creating a device tree overlay file instead:

tools/mkimage -r -k . -K pubkey.dtb -F fitImage
cat > signature.dtso << EOF
/dts-v1/;
/plugin/;

/ {
        fragment@0 {
                target = "/";

                __overlay__ {
EOF
dtc pubkey.dtb|grep -A12 signature >> signature.dtso
cat >> signature.dtso << EOF
		};
	};
};

EOF
dtc -o signature.dtbo signature.dtso
cat u-boot-nodtb.bin signature.dtbo > kernel8.img

Initially, I tested this with CONFIG_RSA, which I expect to work. The bootm command would start up my fitImage, but unfortunately it would do so even if I corrupt a bit of the public key. This would lead me to believe that the overlay was not loaded and the signature was not validated. I only saw messages about hash validation. I'm afraid I need a target environment where u-boot is the primary bootloader, or I must override the CONFIG_OF_BOARD and see if the u-boot.dtb approach would work.

Another point is that my initial CONFIG_ECDSA_SW build was over 4 MiB in size, while the sha256,rsa4096 experiment was only half a megabyte. I did trim the build options for the CONFIG_ECDSA_SW experiment yet.

Marko

Tested-by
#15 lore raw

Hi Philippe, hi all,

I finally got this to work, with one small patch (see below) which I hope you will include in some form. First, here is the relevant part of the output of a successful run:

U-Boot> load mmc 0:1 $loadaddr fitImage.signed
9748489 bytes read in 437 ms (21.3 MiB/s)
U-Boot> bootm
## Loading kernel (any) from FIT Image at 01000000 ...
Using 'conf-1' configuration
Verifying Hash Integrity ... sha256,ecdsa256:dev+ OK Trying 'kernel' kernel subimage
Verifying Hash Integrity ... sha256+ OK
## Loading fdt (any) from FIT Image at 01000000 ...
Using 'conf-1' configuration
Verifying Hash Integrity ... sha256,ecdsa256:dev+ OK Trying 'fdt' fdt subimage
Verifying Hash Integrity ... sha256+ OK
Loading fdt from 0x0193dba0 to 0x05600000
Booting using the fdt blob at 0x5600000
Working FDT set to 5600000
Uncompressing Kernel Image to 2000000
Loading Device Tree to 000000001ffef000, end 000000001ffffbb8 ... OK
Working FDT set to 1ffef000

Starting kernel ...

[    0.000000] Booting Linux on physical CPU 0x0000000000 [0x410fd083]
[    0.000000] Linux version 6.12.68-v8 ([email protected]) (aarch64-linux-gnu-gcc (Debian 14.2.0-19) 14.2.0, GNU ld (GNU Binutils for Debian) 2.44) #1 SMP PREEMPT @1770573000

After I flipped 1 bit of "ecdsa,y-point", the boot failed:

U-Boot> bootm
## Loading kernel (any) from FIT Image at 01000000 ...
Using 'conf-1' configuration
Verifying Hash Integrity ... sha256,ecdsa256:devsw_ecdsa_verify: public key is invalid (err = -19584)
- error!
Verification failed for '<NULL>' hash node in 'conf-1' config node
Failed to verify required signature 'dev'
Bad Data Hash
ERROR -2: can't get kernel image!

Quoted 1 line · Marko Mäkelä

However, this will not work on the Raspberry Pi 4, which defines CONFIG_OF_BOARD. I came up with an idea of creating a device tree overlay file instead:

I found a promising setting CONFIG_OF_OVERLAY_LIST, but apparently it has no effect on the u-boot.bin when CONFIG_OF_BOARD is enabled.

Quoted 1 line

Initially, I tested this with CONFIG_RSA, which I expect to work. The bootm command would start up my fitImage, but unfortunately it would do so even if I corrupt a bit of the public key.

After I added #define DEBUG to boot/image-fit-sig.c and lib/rsa/rsa-verify.c it became clear that the CONFIG_FIT_SIGNATURE becomes a no-op if no "signature" node can be found by u-boot.

The trick was to add the public key to the device tree that U-boot will be starting with, that is, the file bcm2711-rpi-4-b.dtb that will be preloaded by the VideoCore GPU.

Sure, this is an obviously insecure (trivial to circumvent by reverting to the stock *.dtb files), but I think it is good enough for using this piece of existing commodity hardware for development and test purposes.

Quoted 1 line

Another point is that my initial CONFIG_ECDSA_SW build was over 4 MiB in size, while the sha256,rsa4096 experiment was only half a megabyte. I did trim the build options for the CONFIG_ECDSA_SW experiment yet.

I managed to shrink the u-boot.bin to 645296 bytes, or 56320 bytes more than the CONFIG_RSA variant. However, I had to adjust some dependencies:

lib/mbedtls/Makefile +2 -0
index a5331313a60..14f4d295d2a 100644
@@ -57,6 +57,8 @@ mbedtls_lib_x509-$(CONFIG_$(PHASE_)RSA_PUBLIC_KEY_PARSER_MBEDTLS) += \
 	$(MBEDTLS_LIB_DIR)/rsa.o \
 	$(MBEDTLS_LIB_DIR)/rsa_alt_helpers.o
 mbedtls_lib_x509-$(CONFIG_$(PHASE_)ASYMMETRIC_PUBLIC_KEY_MBEDTLS) += \
+	$(MBEDTLS_LIB_DIR)/bignum.o \
+	$(MBEDTLS_LIB_DIR)/bignum_core.o \
 	$(MBEDTLS_LIB_DIR)/pk.o \
 	$(MBEDTLS_LIB_DIR)/pk_wrap.o \
 	$(MBEDTLS_LIB_DIR)/pkparse.o

This may need some fixup, so that the bignum*.o will not be added twice if also CONFIG_RSA_PUBLIC_KEY_PARSER_MBEDTLS is enabled.

Below is my build script, with 4 lines FIT related overrides because it is normally disabled on this target:

make rpi_4_defconfig
scripts/config -d BOOTSTD \
-e FIT -e FIT_SIGNATURE -d FIT_CIPHER -d FIT_VERBOSE -d FIT_BEST_MATCH \
-d FIT_PRINT -d UPDATE_TFTP -d UPDATE_FIT -d EFI_LOADER \
--set-val FIT_EXTERNAL_OFFSET 0x0 \
--set-val FIT_SIGNATURE_MAX_SIZE 0x10000000 \
-e ASYMMETRIC_KEY_TYPE -e ASYMMETRIC_PUBLIC_KEY_SUBTYPE \
-d LEGACY_HASHING_AND_CRYPTO -e MBEDTLS_LIB -e MBEDTLS_LIB_CRYPTO \
-d X509_CERTIFICATE_PARSER -d X509_CERTIFICATE_PARSER_LEGACY \
-e ECDSA -e ECDSA_SW -e ECDSA_VERIFY \
-d MD5_MBEDTLS -d HKDF_MBEDTLS -e SHA256_SMALLER \
-d RSA_PUBLIC_KEY_PARSER -d RSA_PUBLIC_KEY_PARSER_MBEDTLS \
-d X509_CERTIFICATE_PARSER_MBEDTLS -e MSCODE_PARSER
make -j$(nproc) CROSS_COMPILE=aarch64-linux-gnu-

In my CONFIG_RSA test build, the 4 lines starting with -e ECDSA were replaced by the following:

-e RSA -e RSA_VERIFY_WITH_PKEY -d RSASSA_PSS -d MSCODE_PARSER

With best regards,

Marko