Skip to contentSkip to conversation

rsa: extend rsa_verify() for UEFI secure boot

PATCH Superseded AKASHI Takahiro 2 participants 17 messages Feb 21, 2020 – Mar 12, 2020

lore
Share & tools
lore
#0 lore raw

v7 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 v7 (Feb 21, 2020)
* rebased to v2020.04-rc2
* no functional change

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                    |  21 +-
 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(+), 436 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 v7 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        |  14 +-
 lib/rsa/Kconfig        |  10 +
 lib/rsa/Makefile       |   2 +-
 lib/rsa/rsa-verify.c   |  78 +++++---
 tools/Makefile         |   2 +-
 11 files changed, 501 insertions(+), 438 deletions(-)
 create mode 100644 common/image-fit-sig.c
Kconfig +4 -0
index 66148ce47790..e2387b2ff8e7 100644
@@ -354,6 +354,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
@@ -442,6 +444,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 4bc3df4e1bb2..77746f747d49 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 896e4af91d4c..702f2396cf46 100644
@@ -112,7 +112,8 @@ obj-$(CONFIG_ANDROID_BOOT_IMAGE) += image-android.o image-android-dt.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_$(SPL_TPL_)FIT_CIPHER) += image-cipher.o
 obj-$(CONFIG_IO_TRACE) += iotrace.o
 obj-y += memsize.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 f3bb00c98a55..9357e66e1f9e 100644
@@ -1269,7 +1269,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";
@@ -1291,7 +1291,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,
@@ -1949,7 +1949,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 -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'
  *
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 99be724b82a5..3b9ae90369a8 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
 FIT_CIPHER_OBJS-$(CONFIG_FIT_CIPHER) := common/image-cipher.o
 
 # The following files are synced with upstream DTC.
Signature · 1 line
2.24.0
#2 lore raw

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

Quoted 7 lines · AKASHI Takahiro

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.

Applied to u-boot/next, 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/20200312/4f038a1a/attachment.sig>
#3 lore raw

PATCH v7 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
#4 lore raw

PATCH v7 2/7 rsa: add CONFIG_RSA_VERIFY_WITH_PKEY config

Quoted 6 lines · 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.

Applied to u-boot/next, 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/20200312/ba477324/attachment.sig>
#5 lore raw

PATCH v7 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 eb7aa5622aa3..ceede0d4385e 100644
@@ -1170,6 +1170,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
#6 lore raw

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

Quoted 11 lines · 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.

Applied to u-boot/next, 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/20200312/6b50fe25/attachment.sig>
#7 lore raw

PATCH v7 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
#8 lore raw

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

Quoted 7 lines · 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.

Quoted 8 lines

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.

Applied to u-boot/next, 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/20200312/84e4c9d1/attachment.sig>
#9 lore raw

PATCH v7 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 3dd30c8b8bc7..80e817314b58 100644
@@ -18,9 +18,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
 
@@ -271,7 +284,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
  *
@@ -345,6 +358,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
@@ -435,6 +491,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
#10 lore raw

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

Quoted 10 lines · 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.

Applied to u-boot/next, 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/20200312/4796feed/attachment-0001.sig>
#11 lore raw

PATCH v7 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 230068d5a001..ce9ae4a9bed9 100644
@@ -8,4 +8,5 @@ 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
 obj-$(CONFIG_AES) += test_aes.o
test/lib/rsa.c new file +206 -0

206 lines not on this page · raw message

Signature · 1 line
2.24.0
#12 lore raw

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

Quoted 7 lines · 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.

Applied to u-boot/next, 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/20200312/83419147/attachment.sig>
#13 lore raw

PATCH v7 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 941b1fd2c745..e96982402ba6 100644
@@ -200,6 +200,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 7b02b8de7cce..4e001fc650dc 100644
@@ -226,6 +226,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 0049da3d4831..aaa916e077d4 100644
@@ -175,6 +175,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 f55692c2b552..4d6ccd88bf12 100644
@@ -198,6 +198,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
#14 lore raw

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

Quoted 5 lines · 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.

Applied to u-boot/next, 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/20200312/3f2b53bd/attachment.sig>
#15 lore raw

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

In reply to AKASHI Takahiro
Quoted 16 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.

Quoted 7 lines

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]

This has been applied a long while back.

And for the record, without http://patchwork.ozlabs.org/patch/1239098/
applied sandbox fails to build. I had said I would take care of that specific issue, so I'm just noting it here. I'm kicking off a larger test now.

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/20200221/3593699f/attachment.sig>
#16 lore raw

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

Quoted 23 lines · Tom Rini
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 4 lines
Quoted 2 lines

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

This has been applied a long while back.

Yes, I forgot to remove this line.

Quoted 4 lines

And for the record, without http://patchwork.ozlabs.org/patch/1239098/
applied sandbox fails to build. I had said I would take care of that specific issue, so I'm just noting it here. I'm kicking off a larger test now.

Thank you!
-Takahiro Akashi

Quoted 2 lines
Signature · 1 line
Tom