Skip to contentSkip to conversation

rsa: extend rsa_verify() for UEFI secure boot

PATCH Superseded AKASHI Takahiro 2 participants 11 messages Jan 27, 2020 – Feb 18, 2020

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover rsa: extend rsa_verify() for UEFI secure boot

# This patch set is a prerequisite for UEFI secure boot.

The current rsa_verify() requires five parameters for a RSA public key for efficiency while RSA, in theory, requires only two. In addition, those parameters are expected to come from FIT image.

So this function won't fit very well when we want to use it for the purpose of implementing UEFI secure boot, in particular, image authentication as well as variable authentication, where the essential two parameters are set to be retrieved from one of X509 certificates in signature database.

So, in this patch, additional three parameters will be calculated on the fly when rsa_verify() is called without fdt which should contain parameters above.

This calculation heavily relies on "big-number (or multi-precision) library." Therefore some routines from BearSSL[1] under MIT license are imported in this implementation. See Patch#4.
# Please let me know if this is not appropriate.

Prerequisite:
* public key parser in my "import x509/pkcs7 parser" patch[2]

# Checkpatch will complain with lots of warnings/errors, but # I intentionally don't fix them for maximum maintainability.

[1] https://bearssl.org/
[2] https://lists.denx.de/pipermail/u-boot/2019-November/390127.html

Changes in v6 (Jan 27, 2020)
* rebased to v2020.01
* change CONFIG_UT_LIB_RSA dependencies after Tom's suggestion (patch#6)
* add patch#7 to enable RSA library test on sandbox

Changes in v5 (Dec 17, 2019)
* modify RSA_VERIFY-related configuration to fix size growth problem on some platforms (T1042RDB_PI_NAND_SECURE_BOOT and else), adding IMAGE_SIGN_INFO config (patch #1, #2, #6)
* simplify kconfig dependencies;
- RSA_VERIFY_WITH_PKEY *selects* RSA_VERIFY instead of "depends on"

    (patch#2)

- remove some implicit dependencies (patch#6)

Changes in v4 (Nov 21, 2019)
* rebased to v2020.01-rc3
* change a function prototype of rsa_gen_key_prop() to return an error code (patch#4,#5)
* re-order include files in alphabetical order (patch#6)
* add some comments per Simon's review comments

Changes in v3 (Nov 13, 2019)
* remove RSA_VERIFY_WITH_PKEY, which is to be added in patch#2 (patch#1)
* modify unit test Kconfg due to removal of test/lib/Kconfig (patch#6)

Changes in v2 (Oct 29, 2019)
* fix build errors at Travis CI
* not include linux/kconfig.h (patch#1)
* add a separate patch for adding CONFIG_RSA_VERIFY_WITH_PKEY (patch#2)
* take a prerequisite patch from my "secure boot patch" (patch#3)
* add a dependency on RSA_PUBLIC_KEY_PARSER (patch#4)
* remove "inline" directives (patch#4)
* add function descriptions, which mostly come from BearSSL's src/inner.h (patch#4)
* improve Kconfig help text after Simon's comment (patch#5)
* add function description of rsa_verify_with_pkey() (patch#5)
* modify rsa_verify() to use "if (CONFIG_IS_ENABLED(...) " style rather than "#ifdef CONFIG_..." (patch#5)
* add function tests (patch#6)

Changes in v1 (Oct 9, 2019)
* fix a build error on pine64-lts_defconfig (reported by Heinrich) by defining FIT_IMAGE_ENABLE_VERIFY flag and adding SPL_RSA_VERIFY config (patch#1)
* remove FIT-specific code from image-sig.c and put them to new image-fit-sig.c to allow us to disable CONFIG_FIT_SIGNATURE (patch#1)
* compile rsa-keyprop.c only if necessary (i.e. if CONFIG_RSA_VERIFY_WITH_PKEY) (patch#2)
* add SPDX license identifier in rsa-keyprop.c (patch#2)
* include <common.h> instead of <stdio.h> (patch#2)
* use U-Boot's byteorder helper functions instead of BearSSL's (patch#2)

AKASHI Takahiro (7):
lib: rsa: decouple rsa from FIT image verification
rsa: add CONFIG_RSA_VERIFY_WITH_PKEY config
include: image.h: add key info to image_sign_info
lib: rsa: generate additional parameters for public key
lib: rsa: add rsa_verify_with_pkey()
test: add rsa_verify() unit test
test: enable RSA library test on sandbox

 Kconfig                            |   4 +
 common/Kconfig                     |   7 +
 common/Makefile                    |   3 +-
 common/image-fit-sig.c             | 417 +++++++++++++++++
 common/image-fit.c                 |   6 +-
 common/image-sig.c                 | 396 ----------------
 configs/sandbox64_defconfig        |   1 +
 configs/sandbox_defconfig          |   1 +
 configs/sandbox_flattree_defconfig |   1 +
 configs/sandbox_spl_defconfig      |   1 +
 include/image.h                    |  20 +-
 include/u-boot/rsa-mod-exp.h       |  23 +
 lib/rsa/Kconfig                    |  27 ++
 lib/rsa/Makefile                   |   3 +-
 lib/rsa/rsa-keyprop.c              | 725 +++++++++++++++++++++++++++++
 lib/rsa/rsa-verify.c               | 137 ++++--
 test/Kconfig                       |  10 +
 test/lib/Makefile                  |   1 +
 test/lib/rsa.c                     | 206 ++++++++
 tools/Makefile                     |   2 +-
 20 files changed, 1556 insertions(+), 435 deletions(-)
 create mode 100644 common/image-fit-sig.c
 create mode 100644 lib/rsa/rsa-keyprop.c
 create mode 100644 test/lib/rsa.c
Signature · 1 line
2.24.0
#1 lore raw

PATCH v6 1/7 lib: rsa: decouple rsa from FIT image verification

Introduce new configuration, CONFIG_RSA_VERIFY which will decouple building RSA functions from FIT verification and allow for adding a RSA-based signature verification for other file formats, in particular PE file for UEFI secure boot.

---
 Kconfig                |   4 +
 common/Kconfig         |   7 +
 common/Makefile        |   3 +-
 common/image-fit-sig.c | 417 +++++++++++++++++++++++++++++++++++++++++
 common/image-fit.c     |   6 +-
 common/image-sig.c     | 396 --------------------------------------
 include/image.h        |  13 +-
 lib/rsa/Kconfig        |  10 +
 lib/rsa/Makefile       |   2 +-
 lib/rsa/rsa-verify.c   |  78 +++++---
 tools/Makefile         |   2 +-
 11 files changed, 501 insertions(+), 437 deletions(-)
 create mode 100644 common/image-fit-sig.c
Kconfig +4 -0
index 92fc4fc135a4..63164fbda7ca 100644
@@ -352,6 +352,8 @@ config FIT_SIGNATURE
 	depends on DM
 	select HASH
 	select RSA
+	select RSA_VERIFY
+	select IMAGE_SIGN_INFO
 	help
 	  This option enables signature verification of FIT uImages,
 	  using a hash signed and verified using RSA. If
@@ -432,6 +434,8 @@ config SPL_FIT_SIGNATURE
 	depends on SPL_DM
 	select SPL_FIT
 	select SPL_RSA
+	select SPL_RSA_VERIFY
+	select IMAGE_SIGN_INFO
 
 config SPL_LOAD_FIT
 	bool "Enable SPL loading U-Boot as a FIT (basic fitImage features)"
common/Kconfig +7 -0
index a7c5ba278a65..b6e7b81dd021 100644
@@ -1044,3 +1044,10 @@ config BLOBLIST_ADDR
 endmenu
 
 source "common/spl/Kconfig"
+
+config IMAGE_SIGN_INFO
+	bool
+	select SHA1
+	select SHA256
+	help
+	  Enable image_sign_info helper functions.
common/Makefile +2 -1
index 302d8beaf356..8620b1eab766 100644
@@ -112,7 +112,8 @@ obj-$(CONFIG_ANDROID_BOOT_IMAGE) += image-android.o
 obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += image-fdt.o
 obj-$(CONFIG_$(SPL_TPL_)FIT) += image-fit.o
 obj-$(CONFIG_$(SPL_)MULTI_DTB_FIT) += boot_fit.o common_fit.o
-obj-$(CONFIG_$(SPL_TPL_)FIT_SIGNATURE) += image-sig.o
+obj-$(CONFIG_IMAGE_SIGN_INFO) += image-sig.o
+obj-$(CONFIG_$(SPL_TPL_)FIT_SIGNATURE) += image-fit-sig.o
 obj-$(CONFIG_IO_TRACE) += iotrace.o
 obj-y += memsize.o
 obj-y += stdio.o
common/image-fit-sig.c new file +417 -0

417 lines not on this page · raw message

common/image-fit.c +3 -3
index c52f94512088..e67b1cc4b8d1 100644
@@ -1217,7 +1217,7 @@ int fit_image_verify_with_data(const void *fit, int image_noffset,
 	int ret;
 
 	/* Verify all required signatures */
-	if (IMAGE_ENABLE_VERIFY &&
+	if (FIT_IMAGE_ENABLE_VERIFY &&
 	    fit_image_verify_required_sigs(fit, image_noffset, data, size,
 					   gd_fdt_blob(), &verify_all)) {
 		err_msg = "Unable to verify required signature";
@@ -1239,7 +1239,7 @@ int fit_image_verify_with_data(const void *fit, int image_noffset,
 						 &err_msg))
 				goto error;
 			puts("+ ");
-		} else if (IMAGE_ENABLE_VERIFY && verify_all &&
+		} else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
 				!strncmp(name, FIT_SIG_NODENAME,
 					strlen(FIT_SIG_NODENAME))) {
 			ret = fit_image_check_sig(fit, noffset, data,
@@ -1871,7 +1871,7 @@ int fit_image_load(bootm_headers_t *images, ulong addr,
 		if (image_type == IH_TYPE_KERNEL)
 			images->fit_uname_cfg = fit_base_uname_config;
 
-		if (IMAGE_ENABLE_VERIFY && images->verify) {
+		if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
 			puts("   Verifying Hash Integrity ... ");
 			if (fit_config_verify(fit, cfg_noffset)) {
 				puts("Bad Data Hash\n");
common/image-sig.c +0 -396

405 lines not on this page · raw message

include/image.h +9 -4
index f4d2aaf53e87..7eb0b4b53184 100644
@@ -1086,6 +1086,7 @@ int fit_conf_get_prop_node(const void *fit, int noffset,
 
 int fit_check_ramdisk(const void *fit, int os_noffset,
 		uint8_t arch, int verify);
+#endif /* IMAGE_ENABLE_FIT */
 
 int calculate_hash(const void *data, int data_len, const char *algo,
 			uint8_t *value, int *value_len);
@@ -1098,16 +1099,20 @@ int calculate_hash(const void *data, int data_len, const char *algo,
 # if defined(CONFIG_FIT_SIGNATURE)
 #  define IMAGE_ENABLE_SIGN	1
 #  define IMAGE_ENABLE_VERIFY	1
+#  define FIT_IMAGE_ENABLE_VERIFY	1
 #  include <openssl/evp.h>
 # else
 #  define IMAGE_ENABLE_SIGN	0
 #  define IMAGE_ENABLE_VERIFY	0
+#  define FIT_IMAGE_ENABLE_VERIFY	0
 # endif
 #else
 # define IMAGE_ENABLE_SIGN	0
-# define IMAGE_ENABLE_VERIFY	CONFIG_IS_ENABLED(FIT_SIGNATURE)
+# define IMAGE_ENABLE_VERIFY		CONFIG_IS_ENABLED(RSA_VERIFY)
+# define FIT_IMAGE_ENABLE_VERIFY	CONFIG_IS_ENABLED(FIT_SIGNATURE)
 #endif
 
+#if IMAGE_ENABLE_FIT
 #ifdef USE_HOSTCC
 void *image_get_host_blob(void);
 void image_set_host_blob(void *host_blob);
@@ -1121,6 +1126,7 @@ void image_set_host_blob(void *host_blob);
 #else
 #define IMAGE_ENABLE_BEST_MATCH	0
 #endif
+#endif /* IMAGE_ENABLE_FIT */
 
 /* Information passed to the signing routines */
 struct image_sign_info {
@@ -1137,7 +1143,6 @@ struct image_sign_info {
 	const char *require_keys;	/* Value for 'required' property */
 	const char *engine_id;		/* Engine to use for signing */
 };
-#endif /* Allow struct image_region to always be defined for rsa.h */
 
 /* A part of an image, used for hashing */
 struct image_region {
@@ -1145,8 +1150,6 @@ struct image_region {
 	int size;
 };
 
-#if IMAGE_ENABLE_FIT
-
 #if IMAGE_ENABLE_VERIFY
 # include <u-boot/rsa-checksum.h>
 #endif
@@ -1247,6 +1250,8 @@ struct crypto_algo *image_get_crypto_algo(const char *full_name);
  */
 struct padding_algo *image_get_padding_algo(const char *name);
 
+#if IMAGE_ENABLE_FIT
+
 /**
  * fit_image_verify_required_sigs() - Verify signatures marked as 'required'
  *
lib/rsa/Kconfig +10 -0
index 2b33f323bccc..18a075c17478 100644
@@ -18,6 +18,16 @@ if RSA
 config SPL_RSA
 	bool "Use RSA Library within SPL"
 
+config SPL_RSA_VERIFY
+	bool
+	help
+	  Add RSA signature verification support in SPL.
+
+config RSA_VERIFY
+	bool
+	help
+	  Add RSA signature verification support.
+
 config RSA_SOFTWARE_EXP
 	bool "Enable driver for RSA Modular Exponentiation in software"
 	depends on DM
lib/rsa/Makefile +1 -1
index a51c6e1685fb..c07305188e0c 100644
@@ -5,5 +5,5 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
-obj-$(CONFIG_$(SPL_)FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o
+obj-$(CONFIG_$(SPL_)RSA_VERIFY) += rsa-verify.o rsa-checksum.o
 obj-$(CONFIG_RSA_SOFTWARE_EXP) += rsa-mod-exp.o
lib/rsa/rsa-verify.c +47 -31

119 lines not on this page · raw message

tools/Makefile +1 -1
index 345bc84e48db..1f885cca7034 100644
@@ -58,7 +58,7 @@ hostprogs-$(CONFIG_FIT_SIGNATURE) += fit_info fit_check_sign
 hostprogs-$(CONFIG_CMD_BOOTEFI_SELFTEST) += file2include
 
 FIT_OBJS-$(CONFIG_FIT) := fit_common.o fit_image.o image-host.o common/image-fit.o
-FIT_SIG_OBJS-$(CONFIG_FIT_SIGNATURE) := common/image-sig.o
+FIT_SIG_OBJS-$(CONFIG_FIT_SIGNATURE) := common/image-sig.o common/image-fit-sig.o
 
 # The following files are synced with upstream DTC.
 # Use synced versions from scripts/dtc/libfdt/.
Signature · 1 line
2.24.0
#2 lore raw

PATCH v6 2/7 rsa: add CONFIG_RSA_VERIFY_WITH_PKEY config

In reply to AKASHI Takahiro

In the next couple of commits, under new CONFIG_RSA_VERIFY_WITH_PKEY, rsa_verify() will be extended to be able to perform RSA decryption without additional RSA key properties from FIT image, i.e. rr and n0inv.

---
 lib/rsa/Kconfig | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
lib/rsa/Kconfig +14 -0
index 18a075c17478..89697219db2d 100644
@@ -28,6 +28,20 @@ config RSA_VERIFY
 	help
 	  Add RSA signature verification support.
 
+config RSA_VERIFY_WITH_PKEY
+	bool "Execute RSA verification without key parameters from FDT"
+	select RSA_VERIFY
+	help
+	  The standard RSA-signature verification code (FIT_SIGNATURE) uses
+	  pre-calculated key properties, that are stored in fdt blob, in
+	  decrypting a signature.
+	  This does not suit the use case where there is no way defined to
+	  provide such additional key properties in standardized form,
+	  particularly UEFI secure boot.
+	  This options enables RSA signature verification with a public key
+	  directly specified in image_sign_info, where all the necessary
+	  key properties will be calculated on the fly in verification code.
+
 config RSA_SOFTWARE_EXP
 	bool "Enable driver for RSA Modular Exponentiation in software"
 	depends on DM
Signature · 1 line
2.24.0
#3 lore raw

PATCH v6 3/7 include: image.h: add key info to image_sign_info

In reply to AKASHI Takahiro

For FIT verification, all the properties of a public key come from "control fdt" pointed to by fdt_blob. In UEFI secure boot, on the other hand, a public key is located and retrieved from dedicated signature database stored as UEFI variables.

Added two fields may hold values of a public key if fdt_blob is NULL, and will be used in rsa_verify_with_pkey() to verify a signature in UEFI sub-system.

---
 include/image.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/image.h +7 -0
index 7eb0b4b53184..20e1f95cf2a7 100644
@@ -1142,6 +1142,13 @@ struct image_sign_info {
 	int required_keynode;		/* Node offset of key to use: -1=any */
 	const char *require_keys;	/* Value for 'required' property */
 	const char *engine_id;		/* Engine to use for signing */
+	/*
+	 * Note: the following two fields are always valid even w/o
+	 * RSA_VERIFY_WITH_PKEY in order to make sure this structure is
+	 * the same on target and host. Otherwise, vboot test may fail.
+	 */
+	const void *key;		/* Pointer to public key in DER */
+	int keylen;			/* Length of public key */
 };
 
 /* A part of an image, used for hashing */
Signature · 1 line
2.24.0
#4 lore raw

PATCH v6 4/7 lib: rsa: generate additional parameters for public key

In reply to AKASHI Takahiro

In the current implementation of FIT_SIGNATURE, five parameters for a RSA public key are required while only two of them are essential. (See rsa-mod-exp.h and uImage.FIT/signature.txt)
This is a result of considering relatively limited computer power and resources on embedded systems, while such a assumption may not be quite practical for other use cases.

In this patch, added is a function, rsa_gen_key_prop(), which will generate additional parameters for other uses, in particular UEFI secure boot, on the fly.

Note: the current code uses some "big number" routines from BearSSL for the calculation.

---
 include/u-boot/rsa-mod-exp.h |  23 ++
 lib/rsa/Kconfig              |   3 +
 lib/rsa/Makefile             |   1 +
 lib/rsa/rsa-keyprop.c        | 725 +++++++++++++++++++++++++++++++++++
 4 files changed, 752 insertions(+)
 create mode 100644 lib/rsa/rsa-keyprop.c
include/u-boot/rsa-mod-exp.h +23 -0
index 8a428c4b6a1a..1da8af1bb83d 100644
@@ -26,6 +26,29 @@ struct key_prop {
 	uint32_t exp_len;	/* Exponent length in number of uint8_t */
 };
 
+/**
+ * rsa_gen_key_prop() - Generate key properties of RSA public key
+ * @key:	Specifies key data in DER format
+ * @keylen:	Length of @key
+ * @prop:	Generated key property
+ *
+ * This function takes a blob of encoded RSA public key data in DER
+ * format, parse it and generate all the relevant properties
+ * in key_prop structure.
+ * Return a pointer to struct key_prop in @prop on success.
+ *
+ * Return:	0 on success, negative on error
+ */
+int rsa_gen_key_prop(const void *key, uint32_t keylen, struct key_prop **proc);
+
+/**
+ * rsa_free_key_prop() - Free key properties
+ * @prop:	Pointer to struct key_prop
+ *
+ * This function frees all the memories allocated by rsa_gen_key_prop().
+ */
+void rsa_free_key_prop(struct key_prop *prop);
+
 /**
  * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw
  *
lib/rsa/Kconfig +3 -0
index 89697219db2d..a90d67e5a87b 100644
@@ -31,6 +31,9 @@ config RSA_VERIFY
 config RSA_VERIFY_WITH_PKEY
 	bool "Execute RSA verification without key parameters from FDT"
 	select RSA_VERIFY
+	select ASYMMETRIC_KEY_TYPE
+	select ASYMMETRIC_PUBLIC_KEY_SUBTYPE
+	select RSA_PUBLIC_KEY_PARSER
 	help
 	  The standard RSA-signature verification code (FIT_SIGNATURE) uses
 	  pre-calculated key properties, that are stored in fdt blob, in
lib/rsa/Makefile +1 -0
index c07305188e0c..14ed3cb4012b 100644
@@ -6,4 +6,5 @@
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
 obj-$(CONFIG_$(SPL_)RSA_VERIFY) += rsa-verify.o rsa-checksum.o
+obj-$(CONFIG_RSA_VERIFY_WITH_PKEY) += rsa-keyprop.o
 obj-$(CONFIG_RSA_SOFTWARE_EXP) += rsa-mod-exp.o
lib/rsa/rsa-keyprop.c new file +725 -0

725 lines not on this page · raw message

Signature · 1 line
2.24.0
#5 lore raw

PATCH v6 5/7 lib: rsa: add rsa_verify_with_pkey()

In reply to AKASHI Takahiro

This function, and hence rsa_verify(), will perform RSA verification with two essential parameters for a RSA public key in contract of rsa_verify_with_keynode(), which requires additional three parameters stored in FIT image.

It will be used in implementing UEFI secure boot, i.e. image authentication and variable authentication.

---
 lib/rsa/rsa-verify.c | 65 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 64 insertions(+), 1 deletion(-)
lib/rsa/rsa-verify.c +64 -1
index d2fd0692fa13..1ea02c02dab3 100644
@@ -17,9 +17,22 @@
 #include "mkimage.h"
 #include <fdt_support.h>
 #endif
+#include <linux/kconfig.h>
 #include <u-boot/rsa-mod-exp.h>
 #include <u-boot/rsa.h>
 
+#ifndef __UBOOT__
+/*
+ * NOTE:
+ * Since host tools, like mkimage, make use of openssl library for
+ * RSA encryption, rsa_verify_with_pkey()/rsa_gen_key_prop() are
+ * of no use and should not be compiled in.
+ * So just turn off CONFIG_RSA_VERIFY_WITH_PKEY.
+ */
+
+#undef CONFIG_RSA_VERIFY_WITH_PKEY
+#endif
+
 /* Default public exponent for backward compatibility */
 #define RSA_DEFAULT_PUBEXP	65537
 
@@ -270,7 +283,7 @@ out:
 }
 #endif
 
-#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE) || IS_ENABLED(CONFIG_RSA_VERIFY_WITH_PKEY)
 /**
  * rsa_verify_key() - Verify a signature against some data using RSA Key
  *
@@ -344,6 +357,49 @@ static int rsa_verify_key(struct image_sign_info *info,
 }
 #endif
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY
+/**
+ * rsa_verify_with_pkey() - Verify a signature against some data using
+ * only modulus and exponent as RSA key properties.
+ * @info:	Specifies key information
+ * @hash:	Pointer to the expected hash
+ * @sig:	Signature
+ * @sig_len:	Number of bytes in signature
+ *
+ * Parse a RSA public key blob in DER format pointed to in @info and fill
+ * a key_prop structure with properties of the key. Then verify a RSA PKCS1.5
+ * signature against an expected hash using the calculated properties.
+ *
+ * Return	0 if verified, -ve on error
+ */
+static int rsa_verify_with_pkey(struct image_sign_info *info,
+				const void *hash, uint8_t *sig, uint sig_len)
+{
+	struct key_prop *prop;
+	int ret;
+
+	/* Public key is self-described to fill key_prop */
+	ret = rsa_gen_key_prop(info->key, info->keylen, &prop);
+	if (ret) {
+		debug("Generating necessary parameter for decoding failed\n");
+		return ret;
+	}
+
+	ret = rsa_verify_key(info, prop, sig, sig_len, hash,
+			     info->crypto->key_len);
+
+	rsa_free_key_prop(prop);
+
+	return ret;
+}
+#else
+static int rsa_verify_with_pkey(struct image_sign_info *info,
+				const void *hash, uint8_t *sig, uint sig_len)
+{
+	return -EACCES;
+}
+#endif
+
 #if CONFIG_IS_ENABLED(FIT_SIGNATURE)
 /**
  * rsa_verify_with_keynode() - Verify a signature against some data using
@@ -434,6 +490,13 @@ int rsa_verify(struct image_sign_info *info,
 		return -EINVAL;
 	}
 
+	if (IS_ENABLED(CONFIG_RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) {
+		/* don't rely on fdt properties */
+		ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
+
+		return ret;
+	}
+
 	if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
 		const void *blob = info->fdt_blob;
 		int ndepth, noffset;
Signature · 1 line
2.24.0
#6 lore raw

PATCH v6 6/7 test: add rsa_verify() unit test

In reply to AKASHI Takahiro

In this patch, a very simple test is added to verify that rsa_verify() using rsa_verify_with_pkey() work correctly.

To keep the code simple, all the test data, either public key and verified binary data, are embedded in the source.

---
 test/Kconfig      |  10 +++
 test/lib/Makefile |   1 +
 test/lib/rsa.c    | 206 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 217 insertions(+)
 create mode 100644 test/lib/rsa.c
test/Kconfig +10 -0
index cb51b4672102..0157e0b060ad 100644
@@ -28,6 +28,16 @@ config UT_LIB_ASN1
 	  Enables a test which exercises asn1 compiler and decoder function
 	  via various parsers.
 
+config UT_LIB_RSA
+	bool "Unit test for rsa_verify() function"
+	depends on RSA
+	depends on RSA_VERIFY_WITH_PKEY
+	select IMAGE_SIGN_INFO
+	default y
+	help
+	  Enables rsa_verify() test, currently rsa_verify_with_pkey only()
+	  only, at the 'ut lib' command.
+
 endif
 
 config UT_TIME
test/lib/Makefile +1 -0
index 72d2ec74b5f4..2bf6ef3935bb 100644
@@ -8,3 +8,4 @@ obj-y += lmb.o
 obj-y += string.o
 obj-$(CONFIG_ERRNO_STR) += test_errno_str.o
 obj-$(CONFIG_UT_LIB_ASN1) += asn1.o
+obj-$(CONFIG_UT_LIB_RSA) += rsa.o
test/lib/rsa.c new file +206 -0

206 lines not on this page · raw message

Signature · 1 line
2.24.0
#7 lore raw

PATCH v6 7/7 test: enable RSA library test on sandbox

In reply to AKASHI Takahiro

We want to always run RSA library test on sandbox build in Travis CI. Just adding CONFIG_RSA_VERIFY_WITH_PKEY would be good enough for this purpose.

---
 configs/sandbox64_defconfig        | 1 +
 configs/sandbox_defconfig          | 1 +
 configs/sandbox_flattree_defconfig | 1 +
 configs/sandbox_spl_defconfig      | 1 +
 4 files changed, 4 insertions(+)
configs/sandbox64_defconfig +1 -0
index cc536ff0ba38..add56a1d1580 100644
@@ -196,6 +196,7 @@ CONFIG_WDT_SANDBOX=y
 CONFIG_FS_CBFS=y
 CONFIG_FS_CRAMFS=y
 CONFIG_CMD_DHRYSTONE=y
+CONFIG_RSA_VERIFY_WITH_PKEY=y
 CONFIG_TPM=y
 CONFIG_LZ4=y
 CONFIG_ERRNO_STR=y
configs/sandbox_defconfig +1 -0
index 64245f7cdc4b..63bde7ec23a2 100644
@@ -223,6 +223,7 @@ CONFIG_WDT_SANDBOX=y
 CONFIG_FS_CBFS=y
 CONFIG_FS_CRAMFS=y
 CONFIG_CMD_DHRYSTONE=y
+CONFIG_RSA_VERIFY_WITH_PKEY=y
 CONFIG_TPM=y
 CONFIG_LZ4=y
 CONFIG_ERRNO_STR=y
configs/sandbox_flattree_defconfig +1 -0
index bb31b00904a4..1a7abf224b37 100644
@@ -173,6 +173,7 @@ CONFIG_VIDEO_SANDBOX_SDL=y
 CONFIG_OSD=y
 CONFIG_SANDBOX_OSD=y
 CONFIG_CMD_DHRYSTONE=y
+CONFIG_RSA_VERIFY_WITH_PKEY=y
 CONFIG_TPM=y
 CONFIG_LZ4=y
 CONFIG_ERRNO_STR=y
configs/sandbox_spl_defconfig +1 -0
index 61a8ffdef88a..c6c82c468133 100644
@@ -196,6 +196,7 @@ CONFIG_FS_CBFS=y
 CONFIG_FS_CRAMFS=y
 # CONFIG_SPL_USE_TINY_PRINTF is not set
 CONFIG_CMD_DHRYSTONE=y
+CONFIG_RSA_VERIFY_WITH_PKEY=y
 CONFIG_TPM=y
 CONFIG_LZ4=y
 CONFIG_ERRNO_STR=y
Signature · 1 line
2.24.0
#8 lore raw

v6 cover rsa: extend rsa_verify() for UEFI secure boot

In reply to AKASHI Takahiro
Quoted 21 lines · AKASHI Takahiro

# This patch set is a prerequisite for UEFI secure boot.

The current rsa_verify() requires five parameters for a RSA public key for efficiency while RSA, in theory, requires only two. In addition, those parameters are expected to come from FIT image.

So this function won't fit very well when we want to use it for the purpose of implementing UEFI secure boot, in particular, image authentication as well as variable authentication, where the essential two parameters are set to be retrieved from one of X509 certificates in signature database.

So, in this patch, additional three parameters will be calculated on the fly when rsa_verify() is called without fdt which should contain parameters above.

This calculation heavily relies on "big-number (or multi-precision) library." Therefore some routines from BearSSL[1] under MIT license are imported in this implementation. See Patch#4.
# Please let me know if this is not appropriate.

Quoted 8 lines

Prerequisite:
* public key parser in my "import x509/pkcs7 parser" patch[2]

# Checkpatch will complain with lots of warnings/errors, but # I intentionally don't fix them for maximum maintainability.

[1] https://bearssl.org/
[2] https://lists.denx.de/pipermail/u-boot/2019-November/390127.html

At this point it needs to be rebased again. There's a ton of failures in https://gitlab.denx.de/u-boot/u-boot/pipelines/2198 which is after I did
https://gitlab.denx.de/u-boot/u-boot/commit/7db0379f85995d8c7673db7b04eb36d96546c9c8
and I'll put a proper commit message on that later today and post it and CC relevant parties. It's otherwise looking good. I do want to confirm that on boards like minnowmax the slight growth in fit_image_check_sig is expected. It's only 6 bytes so it probably is and we get a larger reduction in rsa_verify all-around.

Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200214/50c7bc32/attachment.sig>
#9 lore raw

v6 cover rsa: extend rsa_verify() for UEFI secure boot

Hi Tom,

Quoted 26 lines · Tom Rini
Quoted 24 lines · AKASHI Takahiro

# This patch set is a prerequisite for UEFI secure boot.

The current rsa_verify() requires five parameters for a RSA public key for efficiency while RSA, in theory, requires only two. In addition, those parameters are expected to come from FIT image.

So this function won't fit very well when we want to use it for the purpose of implementing UEFI secure boot, in particular, image authentication as well as variable authentication, where the essential two parameters are set to be retrieved from one of X509 certificates in signature database.

So, in this patch, additional three parameters will be calculated on the fly when rsa_verify() is called without fdt which should contain parameters above.

This calculation heavily relies on "big-number (or multi-precision) library." Therefore some routines from BearSSL[1] under MIT license are imported in this implementation. See Patch#4.
# Please let me know if this is not appropriate.

Prerequisite:
* public key parser in my "import x509/pkcs7 parser" patch[2]

Quoted 8 lines
Quoted 5 lines

# Checkpatch will complain with lots of warnings/errors, but # I intentionally don't fix them for maximum maintainability.

[1] https://bearssl.org/
[2] https://lists.denx.de/pipermail/u-boot/2019-November/390127.html

At this point it needs to be rebased again. There's a ton of failures in https://gitlab.denx.de/u-boot/u-boot/pipelines/2198 which is after I

I think that you have wrongly merged my rsa extension patch here. Looking at your modified commit,
https://gitlab.denx.de/u-boot/u-boot/commit/13fb61ce20dcd65cd4ccba1554eca6343c92ed6b
there is one missing hunk from my original.
Please revert the change in include/image.h and then apply a diff attached below.

Quoted 4 lines

did
https://gitlab.denx.de/u-boot/u-boot/commit/7db0379f85995d8c7673db7b04eb36d96546c9c8
and I'll put a proper commit message on that later today and post it and CC relevant parties.

I believe that your commit above has nothing to do with my patch (and test failures).

Quoted 4 lines

It's otherwise looking good. I do want to confirm
that on boards like minnowmax the slight growth in fit_image_check_sig is expected. It's only 6 bytes so it probably is and we get a larger reduction in rsa_verify all-around.

Growth due to my patch??

Thanks,
-Takahiro Akashi

Quoted 4 lines

Thanks!

Signature · 1 line
Tom

===8<===

include/image.h +9 -5
index b316d167d8d7..eb7aa5622aa3 100644
@@ -1114,6 +1114,7 @@ int fit_conf_get_prop_node(const void *fit, int noffset,
 
 int fit_check_ramdisk(const void *fit, int os_noffset,
 		uint8_t arch, int verify);
+#endif /* IMAGE_ENABLE_FIT */
 
 int calculate_hash(const void *data, int data_len, const char *algo,
 			uint8_t *value, int *value_len);
@@ -1126,16 +1127,20 @@ int calculate_hash(const void *data, int data_len, const char *algo,
 # if defined(CONFIG_FIT_SIGNATURE)
 #  define IMAGE_ENABLE_SIGN	1
 #  define IMAGE_ENABLE_VERIFY	1
+#  define FIT_IMAGE_ENABLE_VERIFY	1
 #  include <openssl/evp.h>
 # else
 #  define IMAGE_ENABLE_SIGN	0
 #  define IMAGE_ENABLE_VERIFY	0
+#  define FIT_IMAGE_ENABLE_VERIFY	0
 # endif
 #else
 # define IMAGE_ENABLE_SIGN	0
-# define IMAGE_ENABLE_VERIFY	CONFIG_IS_ENABLED(FIT_SIGNATURE)
+# define IMAGE_ENABLE_VERIFY		CONFIG_IS_ENABLED(RSA_VERIFY)
+# define FIT_IMAGE_ENABLE_VERIFY	CONFIG_IS_ENABLED(FIT_SIGNATURE)
 #endif
 
+#if IMAGE_ENABLE_FIT
 #ifdef USE_HOSTCC
 void *image_get_host_blob(void);
 void image_set_host_blob(void *host_blob);
@@ -1149,6 +1154,7 @@ void image_set_host_blob(void *host_blob);
 #else
 #define IMAGE_ENABLE_BEST_MATCH	0
 #endif
+#endif /* IMAGE_ENABLE_FIT */
 
 /* Information passed to the signing routines */
 struct image_sign_info {
@@ -1166,16 +1172,12 @@ struct image_sign_info {
 	const char *engine_id;		/* Engine to use for signing */
 };
 
-#endif /* Allow struct image_region to always be defined for rsa.h */
-
 /* A part of an image, used for hashing */
 struct image_region {
 	const void *data;
 	int size;
 };
 
-#if IMAGE_ENABLE_FIT
-
 #if IMAGE_ENABLE_VERIFY
 # include <u-boot/rsa-checksum.h>
 #endif
@@ -1276,6 +1278,8 @@ struct crypto_algo *image_get_crypto_algo(const char *full_name);
  */
 struct padding_algo *image_get_padding_algo(const char *name);
 
+#if IMAGE_ENABLE_FIT
+
 /**
  * fit_image_verify_required_sigs() - Verify signatures marked as 'required'
  *
#10 lore raw

v6 cover rsa: extend rsa_verify() for UEFI secure boot

Quoted 35 lines · AKASHI Takahiro

Hi Tom,

Quoted 32 lines · Tom Rini
Quoted 29 lines · AKASHI Takahiro

# This patch set is a prerequisite for UEFI secure boot.

The current rsa_verify() requires five parameters for a RSA public key for efficiency while RSA, in theory, requires only two. In addition, those parameters are expected to come from FIT image.

So this function won't fit very well when we want to use it for the purpose of implementing UEFI secure boot, in particular, image authentication as well as variable authentication, where the essential two parameters are set to be retrieved from one of X509 certificates in signature database.

So, in this patch, additional three parameters will be calculated on the fly when rsa_verify() is called without fdt which should contain parameters above.

This calculation heavily relies on "big-number (or multi-precision) library." Therefore some routines from BearSSL[1] under MIT license are imported in this implementation. See Patch#4.
# Please let me know if this is not appropriate.

Prerequisite:
* public key parser in my "import x509/pkcs7 parser" patch[2]

# Checkpatch will complain with lots of warnings/errors, but # I intentionally don't fix them for maximum maintainability.

[1] https://bearssl.org/
[2] https://lists.denx.de/pipermail/u-boot/2019-November/390127.html

Quoted 8 lines
Quoted 2 lines

At this point it needs to be rebased again. There's a ton of failures in https://gitlab.denx.de/u-boot/u-boot/pipelines/2198 which is after I

I think that you have wrongly merged my rsa extension patch here. Looking at your modified commit,
https://gitlab.denx.de/u-boot/u-boot/commit/13fb61ce20dcd65cd4ccba1554eca6343c92ed6b
there is one missing hunk from my original.
Please revert the change in include/image.h and then apply a diff attached below.

Please rebase and repost the series.

Quoted 7 lines
Quoted 4 lines

did
https://gitlab.denx.de/u-boot/u-boot/commit/7db0379f85995d8c7673db7b04eb36d96546c9c8
and I'll put a proper commit message on that later today and post it and CC relevant parties.

I believe that your commit above has nothing to do with my patch (and test failures).

I'll re-confirm things then with the next post.

Quoted 6 lines
Quoted 4 lines

It's otherwise looking good. I do want to confirm
that on boards like minnowmax the slight growth in fit_image_check_sig is expected. It's only 6 bytes so it probably is and we get a larger reduction in rsa_verify all-around.

Growth due to my patch??

Unless it's something else I mis-merged, yes. But given the area this series works on, it's not unexpected growth.

Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200218/5ff2238a/attachment.sig>