Skip to contentSkip to conversation

crypto: sunxi: add CE ECDSA verifier

PATCH New James Hilliard 1 participant 1 message Jun 26

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

PATCH 1/1 crypto: sunxi: add CE ECDSA verifier

The Allwinner sunxi Crypto Engine found on the H616 includes an ECC engine which can verify ECDSA signatures. Add a UCLASS_ECDSA provider so FIT signature verification can use the hardware block from U-Boot proper and SPL.

The CE takes explicit curve parameters, so provide parameter tables for secp224r1, prime256v1, secp384r1 and secp521r1 using the curves accepted by U-Boot's FIT ECDSA parser. Extend the generic ECDSA signing and verification plumbing for the additional algorithm names and for non-32-bit coordinate sizes.

The CE input buffer uses fixed-width curve fields. Reuse the parameter packing logic for the message digest as well, so wider digests are truncated to the leftmost curve-width bytes according to ECDSA rules.

---
 MAINTAINERS                    |   1 +
 doc/mkimage.1                  |   3 +
 doc/usage/fit/signature.rst    |   3 +-
 drivers/crypto/Kconfig         |  26 ++
 drivers/crypto/Makefile        |   1 +
 drivers/crypto/sunxi_ecdsa.c   | 612 +++++++++++++++++++++++++++++++++
 include/u-boot/ecdsa.h         |  16 +
 include/u-boot/fdt-libcrypto.h |   4 +-
 lib/ecdsa/Kconfig              |   2 +-
 lib/ecdsa/ecdsa-libcrypto.c    | 109 +++---
 lib/ecdsa/ecdsa-verify.c       |  33 +-
 lib/fdt-libcrypto.c            |  57 +--
 tools/image-sig-host.c         |   7 +
 13 files changed, 764 insertions(+), 110 deletions(-)
 create mode 100644 drivers/crypto/sunxi_ecdsa.c
MAINTAINERS +1 -0
index 370bcff56c1..d62252e1d1b 100644
@@ -770,6 +770,7 @@ F:	arch/arm/include/asm/arch-sunxi/
 F:	arch/arm/mach-sunxi/
 F:	board/sunxi/
 F:	drivers/clk/sunxi/
+F:	drivers/crypto/sunxi_ecdsa.c
 F:	drivers/phy/allwinner/
 F:	drivers/pinctrl/sunxi/
 F:	drivers/video/sunxi/
doc/mkimage.1 +3 -0
index 9a2d07cee75..b6ba88dd314 100644
@@ -459,7 +459,10 @@ lb.
 rsa2048
 rsa3072
 rsa4096
+ecdsa224
 ecdsa256
+ecdsa384
+secp521r1
 .TE
 .RE
 .
doc/usage/fit/signature.rst +2 -1
index da08cc75c3a..4f35f44bc8a 100644
@@ -132,7 +132,8 @@ rsa,n0-inverse
 For ECDSA the following are mandatory:
 
 ecdsa,curve
-    Name of ECDSA curve (e.g. "prime256v1")
+    Name of ECDSA curve, such as "secp224r1", "prime256v1",
+    "secp384r1", or "secp521r1"
 
 ecdsa,x-point
     Public key X coordinate as a big-endian multi-word integer
drivers/crypto/Kconfig +26 -0
index 0d58e3910fe..9b0a4f18e24 100644
@@ -6,6 +6,32 @@ source "drivers/crypto/aes/Kconfig"
 
 source "drivers/crypto/fsl/Kconfig"
 
+config SUNXI_ECDSA
+	bool "Allwinner sunxi CE ECDSA verifier"
+	depends on ARCH_SUNXI
+	depends on ECDSA_VERIFY
+	depends on CLK && DM_RESET
+	help
+	  Select this option to enable ECDSA signature verification using
+	  the Crypto Engine found in Allwinner sunxi SoCs. FIT image
+	  signatures can then be checked by the hardware accelerator in
+	  U-Boot proper. Digests wider than the selected curve are
+	  truncated according to ECDSA rules.
+
+config SPL_SUNXI_ECDSA
+	bool "Allwinner sunxi CE ECDSA verifier in SPL"
+	depends on ARCH_SUNXI
+	depends on SPL_DM
+	depends on SPL_OF_CONTROL
+	depends on SPL_ECDSA_VERIFY
+	select SPL_CRYPTO
+	help
+	  Select this option to enable ECDSA signature verification in SPL
+	  using the Crypto Engine found in Allwinner sunxi SoCs. This allows
+	  SPL FIT image signatures to be checked by the hardware accelerator
+	  before U-Boot proper is loaded. Digests wider than the selected
+	  curve are truncated according to ECDSA rules.
+
 source "drivers/crypto/aspeed/Kconfig"
 
 source "drivers/crypto/nuvoton/Kconfig"
drivers/crypto/Makefile +1 -0
index e4a4482b7f3..88291ae235f 100644
@@ -4,6 +4,7 @@
 #	http://www.samsung.com
 
 obj-$(CONFIG_EXYNOS_ACE_SHA)	+= ace_sha.o
+obj-$(CONFIG_$(PHASE_)SUNXI_ECDSA) += sunxi_ecdsa.o
 obj-y += aes/
 obj-y += rsa_mod_exp/
 obj-y += fsl/
drivers/crypto/sunxi_ecdsa.c new file +612 -0

612 lines not on this page · raw message

include/u-boot/ecdsa.h +16 -0
index f0ac0f327e9..e8b01cd0258 100644
@@ -8,6 +8,7 @@
 
 #include <errno.h>
 #include <image.h>
+#include <string.h>
 
 /**
  * crypto_algo API impementation for ECDSA;
@@ -64,8 +65,23 @@ int ecdsa_verify(struct image_sign_info *info,
 		 uint8_t *sig, uint sig_len);
 /** @} */
 
+#define ECDSA224_BYTES	(224 / 8)
 #define ECDSA256_BYTES	(256 / 8)
 #define ECDSA384_BYTES	(384 / 8)
 #define ECDSA521_BYTES	((521 + 7) / 8)
 
+static inline unsigned int ecdsa_curve_size(const char *curve_name)
+{
+	if (!strcmp(curve_name, "secp224r1"))
+		return 224;
+	else if (!strcmp(curve_name, "prime256v1"))
+		return 256;
+	else if (!strcmp(curve_name, "secp384r1"))
+		return 384;
+	else if (!strcmp(curve_name, "secp521r1"))
+		return 521;
+
+	return 0;
+}
+
 #endif
include/u-boot/fdt-libcrypto.h +2 -2
index b15d8a1eaf4..a7631cd0e04 100644
@@ -12,7 +12,7 @@
 /**
  * fdt_add_bignum() - Write a libcrypto BIGNUM as an FDT property
  *
- * Convert a libcrypto BIGNUM * into a big endian array of integers.
+ * Convert a libcrypto BIGNUM * into a fixed-width big endian byte array.
  *
  * @blob:	FDT blob to modify
  * @noffset:	Offset of the FDT node
@@ -22,6 +22,6 @@
  * Return: 0 if all good all working, -ve on horror
  */
 int fdt_add_bignum(void *blob, int noffset, const char *prop_name,
-		   BIGNUM *num, int num_bits);
+		   const BIGNUM *num, int num_bits);
 
 #endif /* _FDT_LIBCRYPTO_H */
lib/ecdsa/Kconfig +1 -1
index ca13b6bfa1f..f0c56278fb6 100644
@@ -17,7 +17,7 @@ config ECDSA_VERIFY
 
 config SPL_ECDSA_VERIFY
 	bool "Enable ECDSA verification support in SPL"
-	depends on SPL
+	depends on SPL && SPL_DM
 	help
 	  Allow ECDSA signatures to be recognized and verified in SPL.
 
lib/ecdsa/ecdsa-libcrypto.c +66 -43

238 lines not on this page · raw message

lib/ecdsa/ecdsa-verify.c +15 -18
index 629b662cf6c..201111bc128 100644
@@ -12,22 +12,6 @@
 #include <dm/uclass.h>
 #include <u-boot/ecdsa.h>
 
-/*
- * Derive size of an ECDSA key from the curve name
- *
- * While it's possible to extract the key size by using string manipulation,
- * use a list of known curves for the time being.
- */
-static int ecdsa_key_size(const char *curve_name)
-{
-	if (!strcmp(curve_name, "prime256v1"))
-		return 256;
-	else if (!strcmp(curve_name, "secp384r1"))
-		return 384;
-
-	return 0;
-}
-
 static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node)
 {
 	int x_len, y_len;
@@ -38,7 +22,7 @@ static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node)
 		return -ENOMSG;
 	}
 
-	key->size_bits = ecdsa_key_size(key->curve_name);
+	key->size_bits = ecdsa_curve_size(key->curve_name);
 	if (key->size_bits == 0) {
 		debug("Unknown ECDSA curve '%s'", key->curve_name);
 		return -EINVAL;
@@ -50,7 +34,8 @@ static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node)
 	if (!key->x || !key->y)
 		return -EINVAL;
 
-	if (x_len != (key->size_bits / 8) || y_len != (key->size_bits / 8)) {
+	if (x_len != (key->size_bits + 7) / 8 ||
+	    y_len != (key->size_bits + 7) / 8) {
 		printf("%s: node=%d, curve@%p x@%p+%i y@%p+%i\n", __func__,
 		       node, key->curve_name, key->x, x_len, key->y, y_len);
 		return -EINVAL;
@@ -123,6 +108,12 @@ int ecdsa_verify(struct image_sign_info *info,
 	return ecdsa_verify_hash(dev, info, hash, sig, sig_len);
 }
 
+U_BOOT_CRYPTO_ALGO(ecdsa224) = {
+	.name = "ecdsa224",
+	.key_len = ECDSA224_BYTES,
+	.verify = ecdsa_verify,
+};
+
 U_BOOT_CRYPTO_ALGO(ecdsa256) = {
 	.name = "ecdsa256",
 	.key_len = ECDSA256_BYTES,
@@ -135,6 +126,12 @@ U_BOOT_CRYPTO_ALGO(ecdsa384) = {
 	.verify = ecdsa_verify,
 };
 
+U_BOOT_CRYPTO_ALGO(secp521r1) = {
+	.name = "secp521r1",
+	.key_len = ECDSA521_BYTES,
+	.verify = ecdsa_verify,
+};
+
 /*
  * uclass definition for ECDSA API
  *
lib/fdt-libcrypto.c +12 -45

80 lines not on this page · raw message

tools/image-sig-host.c +7 -0
index 5285263c616..aeca83bd440 100644
@@ -69,6 +69,13 @@ struct crypto_algo crypto_algos[] = {
 		.add_verify_data = rsa_add_verify_data,
 		.verify = rsa_verify,
 	},
+	{
+		.name = "ecdsa224",
+		.key_len = ECDSA224_BYTES,
+		.sign = ecdsa_sign,
+		.add_verify_data = ecdsa_add_verify_data,
+		.verify = ecdsa_verify,
+	},
 	{
 		.name = "ecdsa256",
 		.key_len = ECDSA256_BYTES,
Signature · 1 line
2.53.0