Skip to contentSkip to conversation

rsa: extend rsa_verify() for UEFI secure boot

PATCH Superseded AKASHI Takahiro 4 participants 20 messages Oct 9, 2019 – Oct 30, 2019

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover rsa: extend rsa_verify() 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#2.
# Please let me know if this is not appropriate.

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

[1] https://bearssl.org/

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 (3):
lib: rsa: decouple rsa from FIT image verification
lib: rsa: generate additional parameters for public key
lib: rsa: add rsa_verify_with_pkey()

 Kconfig                      |   1 +
 common/Makefile              |   3 +-
 common/image-fit-sig.c       | 417 +++++++++++++++++++++++++
 common/image-fit.c           |   6 +-
 common/image-sig.c           | 396 ------------------------
 include/image.h              |  14 +-
 include/u-boot/rsa-mod-exp.h |   3 +
 lib/rsa/Kconfig              |  12 +
 lib/rsa/Makefile             |   2 +-
 lib/rsa/rsa-keyprop.c        | 585 +++++++++++++++++++++++++++++++++++
 lib/rsa/rsa-verify.c         |  65 +++-
 tools/Makefile               |   2 +-
 12 files changed, 1095 insertions(+), 411 deletions(-)
 create mode 100644 common/image-fit-sig.c
 create mode 100644 lib/rsa/rsa-keyprop.c
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/3 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                |   1 +
 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        |  12 ++
 lib/rsa/Makefile       |   2 +-
 lib/rsa/rsa-verify.c   |   2 +
 tools/Makefile         |   2 +-
 10 files changed, 452 insertions(+), 403 deletions(-)
 create mode 100644 common/image-fit-sig.c
Kconfig +1 -0
index 1f0904f7045e..cf885fc2750f 100644
@@ -416,6 +416,7 @@ config SPL_FIT_SIGNATURE
 	depends on SPL_DM
 	select SPL_FIT
 	select SPL_RSA
+	select SPL_RSA_VERIFY
 
 config SPL_LOAD_FIT
 	bool "Enable SPL loading U-Boot as a FIT (basic fitImage features)"
common/Makefile +2 -1
index 302d8beaf356..5457f4738a02 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_RSA_VERIFY) += 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 5c63c769de27..2d78f0b47da2 100644
@@ -1216,7 +1216,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";
@@ -1238,7 +1238,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,
@@ -1870,7 +1870,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 +13 -1
index c1065c06f9bd..e37011b7e463 100644
@@ -18,6 +18,15 @@
 #include "compiler.h"
 #include <asm/byteorder.h>
 #include <stdbool.h>
+#if 1
+#ifndef USE_HOSTCC
+/*
+ * We should not include kconfig.h> on host, otherwise it may cause
+ * build errors due to unexpected CONFIG_* defined.
+ */
+#include <linux/kconfig.h>
+#endif
+#endif
 
 /* Define this to avoid #ifdefs later on */
 struct lmb;
@@ -1098,14 +1107,17 @@ 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
 
 #ifdef USE_HOSTCC
lib/rsa/Kconfig +12 -0
index 2b33f323bccc..62b7ab9c5e5c 100644
@@ -1,5 +1,6 @@
 config RSA
 	bool "Use RSA Library"
+	select RSA_VERIFY
 	select RSA_FREESCALE_EXP if FSL_CAAM && !ARCH_MX7 && !ARCH_MX6 && !ARCH_MX5
 	select RSA_SOFTWARE_EXP if !RSA_FREESCALE_EXP
 	help
@@ -17,6 +18,17 @@ if RSA
 
 config SPL_RSA
 	bool "Use RSA Library within SPL"
+	select RSA_VERIFY
+
+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"
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 +2 -0
index 287fcc4d234d..1df42f28c64a 100644
@@ -270,6 +270,7 @@ out:
 }
 #endif
 
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE) || defined(CONFIG_RSA_VERIFY_WITH_PKEY)
 /**
  * rsa_verify_key() - Verify a signature against some data using RSA Key
  *
@@ -341,6 +342,7 @@ static int rsa_verify_key(struct image_sign_info *info,
 
 	return 0;
 }
+#endif
 
 /**
  * rsa_verify_with_keynode() - Verify a signature against some data using
tools/Makefile +1 -1
index 24581adccd4b..1c46d699ec61 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.21.0
#2 lore raw

PATCH 1/3 lib: rsa: decouple rsa from FIT image verification

Hi Takahiro,

Quoted 20 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.

---
 Kconfig                |   1 +
 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        |  12 ++
 lib/rsa/Makefile       |   2 +-
 lib/rsa/rsa-verify.c   |   2 +
 tools/Makefile         |   2 +-
 10 files changed, 452 insertions(+), 403 deletions(-)
 create mode 100644 common/image-fit-sig.c

[..]

Quoted 7 lines
include/image.h +0 -0
index c1065c06f9bd..e37011b7e463 100644
@@ -18,6 +18,15 @@
 #include "compiler.h"
 #include <asm/byteorder.h>

include/image.h line 29

Quoted 10 lines
include/image.h +9 -0
 #include <stdbool.h>
+#if 1
+#ifndef USE_HOSTCC
+/*
+ * We should not include kconfig.h> on host, otherwise it may cause
+ * build errors due to unexpected CONFIG_* defined.
+ */
+#include <linux/kconfig.h>
+#endif
+#endif

Can we remove all this somehow? We seem to have got this far without including kconfig.h

Other than that it looks good.

- Simon

#3 lore raw

PATCH 1/3 lib: rsa: decouple rsa from FIT image verification

Quoted 38 lines · Simon Glass

Hi Takahiro,

Quoted 20 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.

---
 Kconfig                |   1 +
 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        |  12 ++
 lib/rsa/Makefile       |   2 +-
 lib/rsa/rsa-verify.c   |   2 +
 tools/Makefile         |   2 +-
 10 files changed, 452 insertions(+), 403 deletions(-)
 create mode 100644 common/image-fit-sig.c

[..]

Quoted 12 lines
include/image.h +4 -0
index c1065c06f9bd..e37011b7e463 100644
@@ -18,6 +18,15 @@
 #include "compiler.h"
 #include <asm/byteorder.h>
 #include <stdbool.h>
+#if 1
+#ifndef USE_HOSTCC
+/*
+ * We should not include kconfig.h> on host, otherwise it may cause
Quoted 8 lines

include/image.h line 29

Quoted 5 lines
include/image.h +5 -0
+ * build errors due to unexpected CONFIG_* defined.
+ */
+#include <linux/kconfig.h>
+#endif
+#endif

Can we remove all this somehow? We seem to have got this far without including kconfig.h

I don't remember why I added this #ifndef here. As I can't reproduce an issue, I will remove it.

-Takahiro Akashi

Quoted 4 lines

Other than that it looks good.

- Simon

#4 lore raw

PATCH 2/3 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 |   3 +
 lib/rsa/Kconfig              |   7 +
 lib/rsa/Makefile             |   1 +
 lib/rsa/rsa-keyprop.c        | 585 +++++++++++++++++++++++++++++++++++
 4 files changed, 596 insertions(+)
 create mode 100644 lib/rsa/rsa-keyprop.c
include/u-boot/rsa-mod-exp.h +3 -0
index 8a428c4b6a1a..ca189292d869 100644
@@ -26,6 +26,9 @@ struct key_prop {
 	uint32_t exp_len;	/* Exponent length in number of uint8_t */
 };
 
+struct key_prop *rsa_gen_key_prop(const void *key, uint32_t keylen);
+void rsa_free_key_prop(struct key_prop *prop);
+
 /**
  * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw
  *
lib/rsa/Kconfig +7 -0
index 62b7ab9c5e5c..d1743d7a4c47 100644
@@ -30,6 +30,13 @@ config RSA_VERIFY
 	help
 	  Add RSA signature verification support.
 
+config RSA_VERIFY_WITH_PKEY
+	bool "Execute RSA verification without key parameters from FDT"
+	depends on RSA
+	help
+	  This options enables RSA signature verification without
+	  using public key parameters which is embedded control FDT.
+
 config RSA_SOFTWARE_EXP
 	bool "Enable driver for RSA Modular Exponentiation in software"
 	depends on DM
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 +585 -0

585 lines not on this page · raw message

Signature · 1 line
2.21.0
#5 lore raw

PATCH 2/3 lib: rsa: generate additional parameters for public key

Hi Takahiro,

Quoted 29 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.

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 |   3 +
 lib/rsa/Kconfig              |   7 +
 lib/rsa/Makefile             |   1 +
 lib/rsa/rsa-keyprop.c        | 585 +++++++++++++++++++++++++++++++++++
 4 files changed, 596 insertions(+)
 create mode 100644 lib/rsa/rsa-keyprop.c
include/u-boot/rsa-mod-exp.h +0 -0
index 8a428c4b6a1a..ca189292d869 100644

include/u-boot/rsa-mod-exp.h struct key_prop { · line 30

Quoted 5 lines
include/u-boot/rsa-mod-exp.h +2 -0
@@ -26,6 +26,9 @@ struct key_prop {
        uint32_t exp_len;       /* Exponent length in number of uint8_t */
 };
 
+struct key_prop *rsa_gen_key_prop(const void *key, uint32_t keylen);
+void rsa_free_key_prop(struct key_prop *prop);

Please add full function comments.

Quoted 9 lines
include/u-boot/rsa-mod-exp.h +1 -0
+
 /**
  * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw
  *
lib/rsa/Kconfig +0 -0
index 62b7ab9c5e5c..d1743d7a4c47 100644

lib/rsa/Kconfig config RSA_VERIFY · line 38

Quoted 9 lines
lib/rsa/Kconfig +6 -0
@@ -30,6 +30,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
+config RSA_VERIFY_WITH_PKEY
+       bool "Execute RSA verification without key parameters from FDT"
+       depends on RSA
+       help
+         This options enables RSA signature verification without
+         using public key parameters which is embedded control FDT.

How about something like...

The standard RSA-signature verification code uses ....

This does not suit the use case where ...

This option enables ...

Quoted 29 lines
lib/rsa/Kconfig +1 -0
+
 config RSA_SOFTWARE_EXP
        bool "Enable driver for RSA Modular Exponentiation in software"
        depends on DM
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 +9 -0
new file mode 100644
index 000000000000..d7d222e9bed9
@@ -0,0 +1,585 @@
+// SPDX-License-Identifier: GPL-2.0+ and MIT
+/*
+ * RSA library - generate parameters for a public key
+ *
+ * Copyright (c) 2019 Linaro Limited
+ * Author: AKASHI Takahiro
+ *
+ * Big number routines in this file come from BearSSL:
+ * Copyright (c) 2016 Thomas Pornin <[email protected]>

lib/rsa/rsa-keyprop.c line 19

Quoted 10 lines
lib/rsa/rsa-keyprop.c new file +10 -0
+ */
+
+#include <common.h>
+#include <image.h>
+#include <malloc.h>
+#include <asm/byteorder.h>
+#include <crypto/internal/rsa.h>
+#include <u-boot/rsa-mod-exp.h>
+
+static inline unsigned

Please drop the inlines unless needed. Would prefer to leave this to the compiler.

Also please keep the function name on the same line as the 'static'.

Quoted 11 lines
lib/rsa/rsa-keyprop.c new file +11 -0
+br_dec16be(const void *src)
+{
+       return be16_to_cpup(src);
+}
+
+static inline uint32_t
+br_dec32be(const void *src)
+{
+       return be32_to_cpup(src);
+}
+

lib/rsa/rsa-keyprop.c line 40

Quoted 10 lines
lib/rsa/rsa-keyprop.c new file +10 -0
+static inline void
+br_enc32be(void *dst, uint32_t x)
+{
+       __be32 tmp;
+
+       tmp = cpu_to_be32(x);
+       memcpy(dst, &tmp, sizeof(tmp));
+}
+
+/* stripped version of src/inner.h */

??

lib/rsa/rsa-keyprop.c line 47

Quoted 7 lines
lib/rsa/rsa-keyprop.c new file +7 -0
+
+static inline uint32_t
+NOT(uint32_t ctl)
+{
+       return ctl ^ 1;
+}
+

All of these functions need full comments please.

lib/rsa/rsa-keyprop.c line 55

Quoted 8 lines
lib/rsa/rsa-keyprop.c new file +8 -0
+static inline uint32_t
+MUX(uint32_t ctl, uint32_t x, uint32_t y)
+{
+       return y ^ (-ctl & (x ^ y));
+}
+
+static inline uint32_t
+EQ(uint32_t x, uint32_t y)

Should use lower case. Please run patman.

lib/rsa/rsa-keyprop.c line 62

Quoted 7 lines
lib/rsa/rsa-keyprop.c new file +7 -0
+{
+       uint32_t q;
+
+       q = x ^ y;
+       return NOT((q | -q) >> 31);
+}
+

[...]

Regards,
Simon

#6 lore raw

PATCH 2/3 lib: rsa: generate additional parameters for public key

Quoted 35 lines · Simon Glass

Hi Takahiro,

Quoted 32 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.

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 |   3 +
 lib/rsa/Kconfig              |   7 +
 lib/rsa/Makefile             |   1 +
 lib/rsa/rsa-keyprop.c        | 585 +++++++++++++++++++++++++++++++++++
 4 files changed, 596 insertions(+)
 create mode 100644 lib/rsa/rsa-keyprop.c
include/u-boot/rsa-mod-exp.h +0 -0
index 8a428c4b6a1a..ca189292d869 100644
@@ -26,6 +26,9 @@ struct key_prop {
        uint32_t exp_len;       /* Exponent length in number of uint8_t */
 };
 
Quoted 4 lines

include/u-boot/rsa-mod-exp.h struct key_prop { · line 30

Quoted 2 lines
include/u-boot/rsa-mod-exp.h +2 -0
+struct key_prop *rsa_gen_key_prop(const void *key, uint32_t keylen);
+void rsa_free_key_prop(struct key_prop *prop);

Please add full function comments.

Sure.

Quoted 21 lines
Quoted 9 lines
include/u-boot/rsa-mod-exp.h +1 -0
+
 /**
  * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw
  *
lib/rsa/Kconfig +0 -0
index 62b7ab9c5e5c..d1743d7a4c47 100644

lib/rsa/Kconfig config RSA_VERIFY · line 38

Quoted 9 lines
lib/rsa/Kconfig +6 -0
@@ -30,6 +30,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
+config RSA_VERIFY_WITH_PKEY
+       bool "Execute RSA verification without key parameters from FDT"
+       depends on RSA
+       help
+         This options enables RSA signature verification without
+         using public key parameters which is embedded control FDT.

How about something like...

Quoted 5 lines

The standard RSA-signature verification code uses ....

This does not suit the use case where ...

This option enables ...

I will try to improve ...

Quoted 34 lines
Quoted 34 lines
lib/rsa/Kconfig +1 -0
+
 config RSA_SOFTWARE_EXP
        bool "Enable driver for RSA Modular Exponentiation in software"
        depends on DM
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 +14 -0
new file mode 100644
index 000000000000..d7d222e9bed9
@@ -0,0 +1,585 @@
+// SPDX-License-Identifier: GPL-2.0+ and MIT
+/*
+ * RSA library - generate parameters for a public key
+ *
+ * Copyright (c) 2019 Linaro Limited
+ * Author: AKASHI Takahiro
+ *
+ * Big number routines in this file come from BearSSL:
+ * Copyright (c) 2016 Thomas Pornin <[email protected]>
+ */
+
+#include <common.h>
+#include <image.h>
+#include <malloc.h>
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 19

Quoted 5 lines
lib/rsa/rsa-keyprop.c new file +5 -0
+#include <asm/byteorder.h>
+#include <crypto/internal/rsa.h>
+#include <u-boot/rsa-mod-exp.h>
+
+static inline unsigned

Please drop the inlines unless needed. Would prefer to leave this to the compiler.

Okay.

Quoted 1 line

Also please keep the function name on the same line as the 'static'.

Okay.

Quoted 15 lines
Quoted 15 lines
lib/rsa/rsa-keyprop.c new file +15 -0
+br_dec16be(const void *src)
+{
+       return be16_to_cpup(src);
+}
+
+static inline uint32_t
+br_dec32be(const void *src)
+{
+       return be32_to_cpup(src);
+}
+
+static inline void
+br_enc32be(void *dst, uint32_t x)
+{
+       __be32 tmp;
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 40

Quoted 6 lines
lib/rsa/rsa-keyprop.c new file +6 -0
+
+       tmp = cpu_to_be32(x);
+       memcpy(dst, &tmp, sizeof(tmp));
+}
+
+/* stripped version of src/inner.h */

??

I mean that only a small subset of BearSSL's src/inner.h was imported here.

Quoted 9 lines

lib/rsa/rsa-keyprop.c line 47

Quoted 7 lines
lib/rsa/rsa-keyprop.c new file +7 -0
+
+static inline uint32_t
+NOT(uint32_t ctl)
+{
+       return ctl ^ 1;
+}
+

All of these functions need full comments please.

All the functions imported from BearSSL?
Please note that all the functions, except rsa_gen_key_prop() rsa_free_key_prop(), are just imported without changes and all are declared as static.

Quoted 10 lines

lib/rsa/rsa-keyprop.c line 55

Quoted 8 lines
lib/rsa/rsa-keyprop.c new file +8 -0
+static inline uint32_t
+MUX(uint32_t ctl, uint32_t x, uint32_t y)
+{
+       return y ^ (-ctl & (x ^ y));
+}
+
+static inline uint32_t
+EQ(uint32_t x, uint32_t y)

Should use lower case. Please run patman.

I would like to honer the original code(BearSSL)'s notation including upper/lower cases for minimizing differences.

Quoted 12 lines

lib/rsa/rsa-keyprop.c line 62

Quoted 7 lines
lib/rsa/rsa-keyprop.c new file +7 -0
+{
+       uint32_t q;
+
+       q = x ^ y;
+       return NOT((q | -q) >> 31);
+}
+

[...]

Regards,
Simon

#7 lore raw

PATCH 2/3 lib: rsa: generate additional parameters for public key

Hi,

Quoted 174 lines · AKASHI Takahiro
Quoted 35 lines · Simon Glass

Hi Takahiro,

Quoted 32 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.

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 |   3 +
 lib/rsa/Kconfig              |   7 +
 lib/rsa/Makefile             |   1 +
 lib/rsa/rsa-keyprop.c        | 585 +++++++++++++++++++++++++++++++++++
 4 files changed, 596 insertions(+)
 create mode 100644 lib/rsa/rsa-keyprop.c
include/u-boot/rsa-mod-exp.h +0 -0
index 8a428c4b6a1a..ca189292d869 100644
@@ -26,6 +26,9 @@ struct key_prop {
        uint32_t exp_len;       /* Exponent length in number of uint8_t */
 };
 
Quoted 4 lines

include/u-boot/rsa-mod-exp.h struct key_prop { · line 30

Quoted 2 lines
include/u-boot/rsa-mod-exp.h +2 -0
+struct key_prop *rsa_gen_key_prop(const void *key, uint32_t keylen);
+void rsa_free_key_prop(struct key_prop *prop);

Please add full function comments.

Sure.

Quoted 21 lines
Quoted 9 lines
include/u-boot/rsa-mod-exp.h +1 -0
+
 /**
  * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw
  *
lib/rsa/Kconfig +0 -0
index 62b7ab9c5e5c..d1743d7a4c47 100644

lib/rsa/Kconfig config RSA_VERIFY · line 38

Quoted 9 lines
lib/rsa/Kconfig +6 -0
@@ -30,6 +30,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
+config RSA_VERIFY_WITH_PKEY
+       bool "Execute RSA verification without key parameters from FDT"
+       depends on RSA
+       help
+         This options enables RSA signature verification without
+         using public key parameters which is embedded control FDT.

How about something like...

Quoted 5 lines

The standard RSA-signature verification code uses ....

This does not suit the use case where ...

This option enables ...

I will try to improve ...

Quoted 34 lines
Quoted 34 lines
lib/rsa/Kconfig +1 -0
+
 config RSA_SOFTWARE_EXP
        bool "Enable driver for RSA Modular Exponentiation in software"
        depends on DM
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 +14 -0
new file mode 100644
index 000000000000..d7d222e9bed9
@@ -0,0 +1,585 @@
+// SPDX-License-Identifier: GPL-2.0+ and MIT
+/*
+ * RSA library - generate parameters for a public key
+ *
+ * Copyright (c) 2019 Linaro Limited
+ * Author: AKASHI Takahiro
+ *
+ * Big number routines in this file come from BearSSL:
+ * Copyright (c) 2016 Thomas Pornin <[email protected]>
+ */
+
+#include <common.h>
+#include <image.h>
+#include <malloc.h>
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 19

Quoted 5 lines
lib/rsa/rsa-keyprop.c new file +5 -0
+#include <asm/byteorder.h>
+#include <crypto/internal/rsa.h>
+#include <u-boot/rsa-mod-exp.h>
+
+static inline unsigned

Please drop the inlines unless needed. Would prefer to leave this to the compiler.

Okay.

Quoted 1 line

Also please keep the function name on the same line as the 'static'.

Okay.

Quoted 15 lines
Quoted 15 lines
lib/rsa/rsa-keyprop.c new file +15 -0
+br_dec16be(const void *src)
+{
+       return be16_to_cpup(src);
+}
+
+static inline uint32_t
+br_dec32be(const void *src)
+{
+       return be32_to_cpup(src);
+}
+
+static inline void
+br_enc32be(void *dst, uint32_t x)
+{
+       __be32 tmp;
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 40

Quoted 6 lines
lib/rsa/rsa-keyprop.c new file +6 -0
+
+       tmp = cpu_to_be32(x);
+       memcpy(dst, &tmp, sizeof(tmp));
+}
+
+/* stripped version of src/inner.h */

??

I mean that only a small subset of BearSSL's src/inner.h was imported here.

Quoted 9 lines

lib/rsa/rsa-keyprop.c line 47

Quoted 7 lines
lib/rsa/rsa-keyprop.c new file +7 -0
+
+static inline uint32_t
+NOT(uint32_t ctl)
+{
+       return ctl ^ 1;
+}
+

All of these functions need full comments please.

All the functions imported from BearSSL?
Please note that all the functions, except rsa_gen_key_prop() rsa_free_key_prop(), are just imported without changes and all are declared as static.

Quoted 10 lines

lib/rsa/rsa-keyprop.c line 55

Quoted 8 lines
lib/rsa/rsa-keyprop.c new file +8 -0
+static inline uint32_t
+MUX(uint32_t ctl, uint32_t x, uint32_t y)
+{
+       return y ^ (-ctl & (x ^ y));
+}
+
+static inline uint32_t
+EQ(uint32_t x, uint32_t y)

Should use lower case. Please run patman.

Quoted 2 lines

I would like to honer the original code(BearSSL)'s notation including upper/lower cases for minimizing differences.

There is precedent for this. It's just that I'm not sure the code is complicated enough to worry about keeping the comment-free single-char-variable style?

+Tom Rini I assume you are OK with this?

Regards,
Simon

#8 lore raw

PATCH 2/3 lib: rsa: generate additional parameters for public key

Quoted 178 lines · Simon Glass

Hi,

Quoted 174 lines · AKASHI Takahiro
Quoted 35 lines · Simon Glass

Hi Takahiro,

Quoted 32 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.

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 |   3 +
 lib/rsa/Kconfig              |   7 +
 lib/rsa/Makefile             |   1 +
 lib/rsa/rsa-keyprop.c        | 585 +++++++++++++++++++++++++++++++++++
 4 files changed, 596 insertions(+)
 create mode 100644 lib/rsa/rsa-keyprop.c
include/u-boot/rsa-mod-exp.h +0 -0
index 8a428c4b6a1a..ca189292d869 100644
@@ -26,6 +26,9 @@ struct key_prop {
        uint32_t exp_len;       /* Exponent length in number of uint8_t */
 };
 
Quoted 4 lines

include/u-boot/rsa-mod-exp.h struct key_prop { · line 30

Quoted 2 lines
include/u-boot/rsa-mod-exp.h +2 -0
+struct key_prop *rsa_gen_key_prop(const void *key, uint32_t keylen);
+void rsa_free_key_prop(struct key_prop *prop);

Please add full function comments.

Sure.

Quoted 21 lines
Quoted 9 lines
include/u-boot/rsa-mod-exp.h +1 -0
+
 /**
  * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw
  *
lib/rsa/Kconfig +0 -0
index 62b7ab9c5e5c..d1743d7a4c47 100644

lib/rsa/Kconfig config RSA_VERIFY · line 38

Quoted 9 lines
lib/rsa/Kconfig +6 -0
@@ -30,6 +30,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
+config RSA_VERIFY_WITH_PKEY
+       bool "Execute RSA verification without key parameters from FDT"
+       depends on RSA
+       help
+         This options enables RSA signature verification without
+         using public key parameters which is embedded control FDT.

How about something like...

Quoted 5 lines

The standard RSA-signature verification code uses ....

This does not suit the use case where ...

This option enables ...

I will try to improve ...

Quoted 34 lines
Quoted 34 lines
lib/rsa/Kconfig +1 -0
+
 config RSA_SOFTWARE_EXP
        bool "Enable driver for RSA Modular Exponentiation in software"
        depends on DM
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 +14 -0
new file mode 100644
index 000000000000..d7d222e9bed9
@@ -0,0 +1,585 @@
+// SPDX-License-Identifier: GPL-2.0+ and MIT
+/*
+ * RSA library - generate parameters for a public key
+ *
+ * Copyright (c) 2019 Linaro Limited
+ * Author: AKASHI Takahiro
+ *
+ * Big number routines in this file come from BearSSL:
+ * Copyright (c) 2016 Thomas Pornin <[email protected]>
+ */
+
+#include <common.h>
+#include <image.h>
+#include <malloc.h>
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 19

Quoted 5 lines
lib/rsa/rsa-keyprop.c new file +5 -0
+#include <asm/byteorder.h>
+#include <crypto/internal/rsa.h>
+#include <u-boot/rsa-mod-exp.h>
+
+static inline unsigned

Please drop the inlines unless needed. Would prefer to leave this to the compiler.

Okay.

Quoted 1 line

Also please keep the function name on the same line as the 'static'.

Okay.

Quoted 15 lines
Quoted 15 lines
lib/rsa/rsa-keyprop.c new file +15 -0
+br_dec16be(const void *src)
+{
+       return be16_to_cpup(src);
+}
+
+static inline uint32_t
+br_dec32be(const void *src)
+{
+       return be32_to_cpup(src);
+}
+
+static inline void
+br_enc32be(void *dst, uint32_t x)
+{
+       __be32 tmp;
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 40

Quoted 6 lines
lib/rsa/rsa-keyprop.c new file +6 -0
+
+       tmp = cpu_to_be32(x);
+       memcpy(dst, &tmp, sizeof(tmp));
+}
+
+/* stripped version of src/inner.h */

??

I mean that only a small subset of BearSSL's src/inner.h was imported here.

Quoted 9 lines

lib/rsa/rsa-keyprop.c line 47

Quoted 7 lines
lib/rsa/rsa-keyprop.c new file +7 -0
+
+static inline uint32_t
+NOT(uint32_t ctl)
+{
+       return ctl ^ 1;
+}
+

All of these functions need full comments please.

All the functions imported from BearSSL?
Please note that all the functions, except rsa_gen_key_prop() rsa_free_key_prop(), are just imported without changes and all are declared as static.

Quoted 10 lines

lib/rsa/rsa-keyprop.c line 55

Quoted 8 lines
lib/rsa/rsa-keyprop.c new file +8 -0
+static inline uint32_t
+MUX(uint32_t ctl, uint32_t x, uint32_t y)
+{
+       return y ^ (-ctl & (x ^ y));
+}
+
+static inline uint32_t
+EQ(uint32_t x, uint32_t y)

Should use lower case. Please run patman.

Quoted 8 lines
Quoted 2 lines

I would like to honer the original code(BearSSL)'s notation including upper/lower cases for minimizing differences.

There is precedent for this. It's just that I'm not sure the code is complicated enough to worry about keeping the comment-free single-char-variable style?

+Tom Rini I assume you are OK with this?

When we import code from another project that we expect to re-sync we do what we need to do to keep the overall re-sync pain down, even we we otherwise may disagree on style points.

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

PATCH 2/3 lib: rsa: generate additional parameters for public key

Hi,

Quoted 187 lines · Tom Rini
Quoted 185 lines · Simon Glass

Hi,

Quoted 174 lines · AKASHI Takahiro
Quoted 35 lines · Simon Glass

Hi Takahiro,

Quoted 32 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.

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 |   3 +
 lib/rsa/Kconfig              |   7 +
 lib/rsa/Makefile             |   1 +
 lib/rsa/rsa-keyprop.c        | 585 +++++++++++++++++++++++++++++++++++
 4 files changed, 596 insertions(+)
 create mode 100644 lib/rsa/rsa-keyprop.c
include/u-boot/rsa-mod-exp.h +0 -0
index 8a428c4b6a1a..ca189292d869 100644
@@ -26,6 +26,9 @@ struct key_prop {
        uint32_t exp_len;       /* Exponent length in number of uint8_t */
 };
 
Quoted 4 lines

include/u-boot/rsa-mod-exp.h struct key_prop { · line 30

Quoted 2 lines
include/u-boot/rsa-mod-exp.h +2 -0
+struct key_prop *rsa_gen_key_prop(const void *key, uint32_t keylen);
+void rsa_free_key_prop(struct key_prop *prop);

Please add full function comments.

Sure.

Quoted 21 lines
Quoted 9 lines
include/u-boot/rsa-mod-exp.h +1 -0
+
 /**
  * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw
  *
lib/rsa/Kconfig +0 -0
index 62b7ab9c5e5c..d1743d7a4c47 100644

lib/rsa/Kconfig config RSA_VERIFY · line 38

Quoted 9 lines
lib/rsa/Kconfig +6 -0
@@ -30,6 +30,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
+config RSA_VERIFY_WITH_PKEY
+       bool "Execute RSA verification without key parameters from FDT"
+       depends on RSA
+       help
+         This options enables RSA signature verification without
+         using public key parameters which is embedded control FDT.

How about something like...

Quoted 5 lines

The standard RSA-signature verification code uses ....

This does not suit the use case where ...

This option enables ...

I will try to improve ...

Quoted 34 lines
Quoted 34 lines
lib/rsa/Kconfig +1 -0
+
 config RSA_SOFTWARE_EXP
        bool "Enable driver for RSA Modular Exponentiation in software"
        depends on DM
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 +14 -0
new file mode 100644
index 000000000000..d7d222e9bed9
@@ -0,0 +1,585 @@
+// SPDX-License-Identifier: GPL-2.0+ and MIT
+/*
+ * RSA library - generate parameters for a public key
+ *
+ * Copyright (c) 2019 Linaro Limited
+ * Author: AKASHI Takahiro
+ *
+ * Big number routines in this file come from BearSSL:
+ * Copyright (c) 2016 Thomas Pornin <[email protected]>
+ */
+
+#include <common.h>
+#include <image.h>
+#include <malloc.h>
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 19

Quoted 5 lines
lib/rsa/rsa-keyprop.c new file +5 -0
+#include <asm/byteorder.h>
+#include <crypto/internal/rsa.h>
+#include <u-boot/rsa-mod-exp.h>
+
+static inline unsigned

Please drop the inlines unless needed. Would prefer to leave this to the compiler.

Okay.

Quoted 1 line

Also please keep the function name on the same line as the 'static'.

Okay.

Quoted 15 lines
Quoted 15 lines
lib/rsa/rsa-keyprop.c new file +15 -0
+br_dec16be(const void *src)
+{
+       return be16_to_cpup(src);
+}
+
+static inline uint32_t
+br_dec32be(const void *src)
+{
+       return be32_to_cpup(src);
+}
+
+static inline void
+br_enc32be(void *dst, uint32_t x)
+{
+       __be32 tmp;
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 40

Quoted 6 lines
lib/rsa/rsa-keyprop.c new file +6 -0
+
+       tmp = cpu_to_be32(x);
+       memcpy(dst, &tmp, sizeof(tmp));
+}
+
+/* stripped version of src/inner.h */

??

I mean that only a small subset of BearSSL's src/inner.h was imported here.

Quoted 9 lines

lib/rsa/rsa-keyprop.c line 47

Quoted 7 lines
lib/rsa/rsa-keyprop.c new file +7 -0
+
+static inline uint32_t
+NOT(uint32_t ctl)
+{
+       return ctl ^ 1;
+}
+

All of these functions need full comments please.

All the functions imported from BearSSL?
Please note that all the functions, except rsa_gen_key_prop() rsa_free_key_prop(), are just imported without changes and all are declared as static.

Quoted 10 lines

lib/rsa/rsa-keyprop.c line 55

Quoted 8 lines
lib/rsa/rsa-keyprop.c new file +8 -0
+static inline uint32_t
+MUX(uint32_t ctl, uint32_t x, uint32_t y)
+{
+       return y ^ (-ctl & (x ^ y));
+}
+
+static inline uint32_t
+EQ(uint32_t x, uint32_t y)

Should use lower case. Please run patman.

Quoted 2 lines

I would like to honer the original code(BearSSL)'s notation including upper/lower cases for minimizing differences.

There is precedent for this. It's just that I'm not sure the code is complicated enough to worry about keeping the comment-free single-char-variable style?

Quoted 5 lines
Quoted 1 line

+Tom Rini I assume you are OK with this?

When we import code from another project that we expect to re-sync we do what we need to do to keep the overall re-sync pain down, even we we otherwise may disagree on style points.

OK ta.

Regards,
Simon

#10 lore raw

PATCH 2/3 lib: rsa: generate additional parameters for public key

Simon, Tom,

Quoted 190 lines · Simon Glass

Hi,

Quoted 187 lines · Tom Rini
Quoted 185 lines · Simon Glass

Hi,

Quoted 174 lines · AKASHI Takahiro
Quoted 35 lines · Simon Glass

Hi Takahiro,

Quoted 32 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.

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 |   3 +
 lib/rsa/Kconfig              |   7 +
 lib/rsa/Makefile             |   1 +
 lib/rsa/rsa-keyprop.c        | 585 +++++++++++++++++++++++++++++++++++
 4 files changed, 596 insertions(+)
 create mode 100644 lib/rsa/rsa-keyprop.c
include/u-boot/rsa-mod-exp.h +0 -0
index 8a428c4b6a1a..ca189292d869 100644
@@ -26,6 +26,9 @@ struct key_prop {
        uint32_t exp_len;       /* Exponent length in number of uint8_t */
 };
 
Quoted 4 lines

include/u-boot/rsa-mod-exp.h struct key_prop { · line 30

Quoted 2 lines
include/u-boot/rsa-mod-exp.h +2 -0
+struct key_prop *rsa_gen_key_prop(const void *key, uint32_t keylen);
+void rsa_free_key_prop(struct key_prop *prop);

Please add full function comments.

Sure.

Quoted 21 lines
Quoted 9 lines
include/u-boot/rsa-mod-exp.h +1 -0
+
 /**
  * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw
  *
lib/rsa/Kconfig +0 -0
index 62b7ab9c5e5c..d1743d7a4c47 100644

lib/rsa/Kconfig config RSA_VERIFY · line 38

Quoted 9 lines
lib/rsa/Kconfig +6 -0
@@ -30,6 +30,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
+config RSA_VERIFY_WITH_PKEY
+       bool "Execute RSA verification without key parameters from FDT"
+       depends on RSA
+       help
+         This options enables RSA signature verification without
+         using public key parameters which is embedded control FDT.

How about something like...

Quoted 5 lines

The standard RSA-signature verification code uses ....

This does not suit the use case where ...

This option enables ...

I will try to improve ...

Quoted 34 lines
Quoted 34 lines
lib/rsa/Kconfig +1 -0
+
 config RSA_SOFTWARE_EXP
        bool "Enable driver for RSA Modular Exponentiation in software"
        depends on DM
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 +14 -0
new file mode 100644
index 000000000000..d7d222e9bed9
@@ -0,0 +1,585 @@
+// SPDX-License-Identifier: GPL-2.0+ and MIT
+/*
+ * RSA library - generate parameters for a public key
+ *
+ * Copyright (c) 2019 Linaro Limited
+ * Author: AKASHI Takahiro
+ *
+ * Big number routines in this file come from BearSSL:
+ * Copyright (c) 2016 Thomas Pornin <[email protected]>
+ */
+
+#include <common.h>
+#include <image.h>
+#include <malloc.h>
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 19

Quoted 5 lines
lib/rsa/rsa-keyprop.c new file +5 -0
+#include <asm/byteorder.h>
+#include <crypto/internal/rsa.h>
+#include <u-boot/rsa-mod-exp.h>
+
+static inline unsigned

Please drop the inlines unless needed. Would prefer to leave this to the compiler.

Okay.

Quoted 1 line

Also please keep the function name on the same line as the 'static'.

Okay.

Quoted 15 lines
Quoted 15 lines
lib/rsa/rsa-keyprop.c new file +15 -0
+br_dec16be(const void *src)
+{
+       return be16_to_cpup(src);
+}
+
+static inline uint32_t
+br_dec32be(const void *src)
+{
+       return be32_to_cpup(src);
+}
+
+static inline void
+br_enc32be(void *dst, uint32_t x)
+{
+       __be32 tmp;
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 40

Quoted 6 lines
lib/rsa/rsa-keyprop.c new file +6 -0
+
+       tmp = cpu_to_be32(x);
+       memcpy(dst, &tmp, sizeof(tmp));
+}
+
+/* stripped version of src/inner.h */

??

I mean that only a small subset of BearSSL's src/inner.h was imported here.

Quoted 9 lines

lib/rsa/rsa-keyprop.c line 47

Quoted 7 lines
lib/rsa/rsa-keyprop.c new file +7 -0
+
+static inline uint32_t
+NOT(uint32_t ctl)
+{
+       return ctl ^ 1;
+}
+

All of these functions need full comments please.

All the functions imported from BearSSL?
Please note that all the functions, except rsa_gen_key_prop() rsa_free_key_prop(), are just imported without changes and all are declared as static.

Quoted 10 lines

lib/rsa/rsa-keyprop.c line 55

Quoted 8 lines
lib/rsa/rsa-keyprop.c new file +8 -0
+static inline uint32_t
+MUX(uint32_t ctl, uint32_t x, uint32_t y)
+{
+       return y ^ (-ctl & (x ^ y));
+}
+
+static inline uint32_t
+EQ(uint32_t x, uint32_t y)

Should use lower case. Please run patman.

Quoted 2 lines

I would like to honer the original code(BearSSL)'s notation including upper/lower cases for minimizing differences.

There is precedent for this. It's just that I'm not sure the code is complicated enough to worry about keeping the comment-free single-char-variable style?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

+Tom Rini I assume you are OK with this?

When we import code from another project that we expect to re-sync we do what we need to do to keep the overall re-sync pain down, even we we otherwise may disagree on style points.

OK ta.

Thank you for your understanding.
Yes, this patch is small but if I had to modify my "import x509/pkcs7 parsers from linux" patch, it would be quite painful.

-Takahiro Akashi

Quoted 2 lines

Regards,
Simon

#11 lore raw

PATCH 2/3 lib: rsa: generate additional parameters for public key

Hi AKASHI,

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

There is precedent for this. It's just that I'm not sure the code is complicated enough to worry about keeping the comment-free single-char-variable style?

+Tom Rini I assume you are OK with this?

When we import code from another project that we expect to re-sync we do what we need to do to keep the overall re-sync pain down, even we we otherwise may disagree on style points.

Quoted 5 lines
Quoted 1 line

OK ta.

Thank you for your understanding.
Yes, this patch is small but if I had to modify my "import x509/pkcs7 parsers from linux" patch, it would be quite painful.

Quoted 6 lines

-Takahiro Akashi

Quoted 2 lines

Regards,
Simon

#12 lore raw

PATCH 3/3 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/Kconfig      |  7 -----
 lib/rsa/Makefile     |  1 -
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 55 insertions(+), 16 deletions(-)
lib/rsa/Kconfig +0 -7
index d1743d7a4c47..62b7ab9c5e5c 100644
@@ -30,13 +30,6 @@ config RSA_VERIFY
 	help
 	  Add RSA signature verification support.
 
-config RSA_VERIFY_WITH_PKEY
-	bool "Execute RSA verification without key parameters from FDT"
-	depends on RSA
-	help
-	  This options enables RSA signature verification without
-	  using public key parameters which is embedded control FDT.
-
 config RSA_SOFTWARE_EXP
 	bool "Enable driver for RSA Modular Exponentiation in software"
 	depends on DM
lib/rsa/Makefile +0 -1
index 14ed3cb4012b..c07305188e0c 100644
@@ -6,5 +6,4 @@
 # 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-verify.c +55 -8

112 lines not on this page · raw message

Signature · 1 line
2.21.0
#13 lore raw

PATCH 3/3 lib: rsa: add rsa_verify_with_pkey()

Quoted 9 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.

Is this code tested by test/py/tests/test_vboot.py? Or is there another unit test?

Quoted 39 lines
---
  lib/rsa/Kconfig      |  7 -----
  lib/rsa/Makefile     |  1 -
  lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
  3 files changed, 55 insertions(+), 16 deletions(-)
lib/rsa/Kconfig +0 -7
index d1743d7a4c47..62b7ab9c5e5c 100644
@@ -30,13 +30,6 @@ config RSA_VERIFY
  	help
  	  Add RSA signature verification support.
 
-config RSA_VERIFY_WITH_PKEY
-	bool "Execute RSA verification without key parameters from FDT"
-	depends on RSA
-	help
-	  This options enables RSA signature verification without
-	  using public key parameters which is embedded control FDT.
-
  config RSA_SOFTWARE_EXP
  	bool "Enable driver for RSA Modular Exponentiation in software"
  	depends on DM
lib/rsa/Makefile +0 -1
index 14ed3cb4012b..c07305188e0c 100644
@@ -6,5 +6,4 @@
  # 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-verify.c +0 -0
index 1df42f28c64a..ce79984b30f9 100644

lib/rsa/rsa-verify.c line 25

Quoted 9 lines
lib/rsa/rsa-verify.c +3 -0
@@ -17,9 +17,14 @@
  #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__ /* for host tools */
+#undef CONFIG_RSA_VERIFY_WITH_PKEY

Where should it have been defined?

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 352

Quoted 10 lines
lib/rsa/rsa-verify.c +3 -0
+#endif
+
  /* Default public exponent for backward compatibility */
  #define RSA_DEFAULT_PUBEXP	65537
 
@@ -344,6 +349,34 @@ static int rsa_verify_key(struct image_sign_info *info,
  }
  #endif
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Where would CONFIG_RSA_VERIFY_WITH_PKEY be defined? You just a removed it from Kconfig.

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 354

Quoted 2 lines
lib/rsa/rsa-verify.c +2 -0
+/**
+ * rsa_verify_with_pkey()

The short text for the function is missing.

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 355

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+ *

Please, describe the parameters.

Quoted 65 lines
lib/rsa/rsa-verify.c +29 -8
+ */
+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 */
+	prop = rsa_gen_key_prop(info->key, info->keylen);
+	if (!prop) {
+		debug("Generating necessary parameter for decoding failed\n");
+		return -EACCES;
+	}
+
+	ret = rsa_verify_key(info, prop, sig, sig_len, hash,
+			     info->crypto->key_len);
+
+	rsa_free_key_prop(prop);
+
+	return ret;
+}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
  /**
   * rsa_verify_with_keynode() - Verify a signature against some data using
   * information in node with prperties of RSA Key like modulus, exponent etc.
@@ -397,18 +430,21 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
 
  	return ret;
  }
+#endif
 
  int rsa_verify(struct image_sign_info *info,
  	       const struct image_region region[], int region_count,
  	       uint8_t *sig, uint sig_len)
  {
-	const void *blob = info->fdt_blob;
  	/* Reserve memory for maximum checksum-length */
  	uint8_t hash[info->crypto->key_len];
+	int ret = -EACCES;
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	const void *blob = info->fdt_blob;
  	int ndepth, noffset;
  	int sig_node, node;
  	char name[100];
-	int ret;
+#endif
 
  	/*
  	 * Verify that the checksum-length does not exceed the
@@ -421,12 +457,6 @@ int rsa_verify(struct image_sign_info *info,
  		return -EINVAL;
  	}
 
-	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
-	if (sig_node < 0) {
-		debug("%s: No signature node found\n", __func__);
-		return -ENOENT;
-	}
-
  	/* Calculate checksum with checksum-algorithm */
  	ret = info->checksum->calculate(info->checksum->name,
  					region, region_count, hash);

lib/rsa/rsa-verify.c int rsa_verify(struct image_sign_info *info, · line 468

Quoted 4 lines
lib/rsa/rsa-verify.c +1 -0
@@ -435,6 +465,22 @@ int rsa_verify(struct image_sign_info *info,
  		return -EINVAL;
  	}
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Where should this have been defined?

Best regards

Heinrich

Quoted 27 lines
lib/rsa/rsa-verify.c +16 -0
+	if (!info->fdt_blob) {
+		/* don't rely on fdt properties */
+		ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
+
+		return ret;
+	}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
+	if (sig_node < 0) {
+		debug("%s: No signature node found\n", __func__);
+		return -ENOENT;
+	}
+
  	/* See if we must use a particular key */
  	if (info->required_keynode != -1) {
  		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
@@ -461,6 +507,7 @@ int rsa_verify(struct image_sign_info *info,
  				break;
  		}
  	}
+#endif
 
  	return ret;
  }
#14 lore raw

PATCH 3/3 lib: rsa: add rsa_verify_with_pkey()

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

Quoted 7 lines
Quoted 4 lines

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

Is this code tested by test/py/tests/test_vboot.py? Or is there another unit test?

I haven't run vboot test yet.
For rsa_verify_with_pkey(), as with vtest for FIT signature, we can test it with my "UEFI secure boot" pytest.

Quoted 46 lines
Quoted 46 lines
---
 lib/rsa/Kconfig      |  7 -----
 lib/rsa/Makefile     |  1 -
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 55 insertions(+), 16 deletions(-)
lib/rsa/Kconfig +0 -7
index d1743d7a4c47..62b7ab9c5e5c 100644
@@ -30,13 +30,6 @@ config RSA_VERIFY
 	help
 	  Add RSA signature verification support.
 
-config RSA_VERIFY_WITH_PKEY
-	bool "Execute RSA verification without key parameters from FDT"
-	depends on RSA
-	help
-	  This options enables RSA signature verification without
-	  using public key parameters which is embedded control FDT.
-
 config RSA_SOFTWARE_EXP
 	bool "Enable driver for RSA Modular Exponentiation in software"
 	depends on DM
lib/rsa/Makefile +0 -1
index 14ed3cb4012b..c07305188e0c 100644
@@ -6,5 +6,4 @@
 # 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-verify.c +1 -0
index 1df42f28c64a..ce79984b30f9 100644
@@ -17,9 +17,14 @@
 #include "mkimage.h"
 #include <fdt_support.h>
 #endif
+#include <linux/kconfig.h>
 #include <u-boot/rsa-mod-exp.h>
 #include <u-boot/rsa.h>
 
Quoted 4 lines

lib/rsa/rsa-verify.c line 25

Quoted 2 lines
lib/rsa/rsa-verify.c +2 -0
+#ifndef __UBOOT__ /* for host tools */
+#undef CONFIG_RSA_VERIFY_WITH_PKEY

Where should it have been defined?

defined in patch#2

Quoted 5 lines
Quoted 5 lines
lib/rsa/rsa-verify.c +2 -0
+#endif
+
 /* Default public exponent for backward compatibility */
 #define RSA_DEFAULT_PUBEXP	65537
 
Quoted 8 lines

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 352

Quoted 5 lines
lib/rsa/rsa-verify.c +1 -0
@@ -344,6 +349,34 @@ static int rsa_verify_key(struct image_sign_info *info,
 }
 #endif
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Where would CONFIG_RSA_VERIFY_WITH_PKEY be defined? You just a removed it from Kconfig.

Because rsa-keyprop.c be compiled only under CONFIG_RSA_VERIFY_WITH_PKEY per Simon's comment.

Quoted 8 lines

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 354

Quoted 2 lines
lib/rsa/rsa-verify.c +2 -0
+/**
+ * rsa_verify_with_pkey()

The short text for the function is missing.

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 355

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+ *

Please, describe the parameters.

Sure.

-Takahiro Akashi

Quoted 104 lines
Quoted 65 lines
lib/rsa/rsa-verify.c +29 -8
+ */
+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 */
+	prop = rsa_gen_key_prop(info->key, info->keylen);
+	if (!prop) {
+		debug("Generating necessary parameter for decoding failed\n");
+		return -EACCES;
+	}
+
+	ret = rsa_verify_key(info, prop, sig, sig_len, hash,
+			     info->crypto->key_len);
+
+	rsa_free_key_prop(prop);
+
+	return ret;
+}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
 /**
  * rsa_verify_with_keynode() - Verify a signature against some data using
  * information in node with prperties of RSA Key like modulus, exponent etc.
@@ -397,18 +430,21 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
 
 	return ret;
 }
+#endif
 
 int rsa_verify(struct image_sign_info *info,
 	       const struct image_region region[], int region_count,
 	       uint8_t *sig, uint sig_len)
 {
-	const void *blob = info->fdt_blob;
 	/* Reserve memory for maximum checksum-length */
 	uint8_t hash[info->crypto->key_len];
+	int ret = -EACCES;
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	const void *blob = info->fdt_blob;
 	int ndepth, noffset;
 	int sig_node, node;
 	char name[100];
-	int ret;
+#endif
 
 	/*
 	 * Verify that the checksum-length does not exceed the
@@ -421,12 +457,6 @@ int rsa_verify(struct image_sign_info *info,
 		return -EINVAL;
 	}
 
-	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
-	if (sig_node < 0) {
-		debug("%s: No signature node found\n", __func__);
-		return -ENOENT;
-	}
-
 	/* Calculate checksum with checksum-algorithm */
 	ret = info->checksum->calculate(info->checksum->name,
 					region, region_count, hash);

lib/rsa/rsa-verify.c int rsa_verify(struct image_sign_info *info, · line 468

Quoted 4 lines
lib/rsa/rsa-verify.c +1 -0
@@ -435,6 +465,22 @@ int rsa_verify(struct image_sign_info *info,
 		return -EINVAL;
 	}
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Where should this have been defined?

Best regards

Heinrich

Quoted 27 lines
lib/rsa/rsa-verify.c +16 -0
+	if (!info->fdt_blob) {
+		/* don't rely on fdt properties */
+		ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
+
+		return ret;
+	}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
+	if (sig_node < 0) {
+		debug("%s: No signature node found\n", __func__);
+		return -ENOENT;
+	}
+
 	/* See if we must use a particular key */
 	if (info->required_keynode != -1) {
 		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
@@ -461,6 +507,7 @@ int rsa_verify(struct image_sign_info *info,
 				break;
 		}
 	}
+#endif
 
 	return ret;
 }
#15 lore raw

PATCH 3/3 lib: rsa: add rsa_verify_with_pkey()

Quoted 10 lines · AKASHI Takahiro
Quoted 9 lines · Heinrich Schuchardt
Quoted 8 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.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Is this code tested by test/py/tests/test_vboot.py? Or is there another unit test?

I haven't run vboot test yet.

I would have assumed that you to through Travis CI before submitting.

Quoted 2 lines

For rsa_verify_with_pkey(), as with vtest for FIT signature, we can test it with my "UEFI secure boot" pytest.

I can't see such a test in this patch series. So how I can test the changes before merging?

Best regards

Heinrich

Quoted 189 lines
Quoted 47 lines
Quoted 47 lines
---
  lib/rsa/Kconfig      |  7 -----
  lib/rsa/Makefile     |  1 -
  lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
  3 files changed, 55 insertions(+), 16 deletions(-)
lib/rsa/Kconfig +0 -7
index d1743d7a4c47..62b7ab9c5e5c 100644
@@ -30,13 +30,6 @@ config RSA_VERIFY
  	help
  	  Add RSA signature verification support.
 
-config RSA_VERIFY_WITH_PKEY
-	bool "Execute RSA verification without key parameters from FDT"
-	depends on RSA
-	help
-	  This options enables RSA signature verification without
-	  using public key parameters which is embedded control FDT.
-
  config RSA_SOFTWARE_EXP
  	bool "Enable driver for RSA Modular Exponentiation in software"
  	depends on DM
lib/rsa/Makefile +0 -1
index 14ed3cb4012b..c07305188e0c 100644
@@ -6,5 +6,4 @@
  # 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-verify.c +1 -0
index 1df42f28c64a..ce79984b30f9 100644
@@ -17,9 +17,14 @@
  #include "mkimage.h"
  #include <fdt_support.h>
  #endif
+#include <linux/kconfig.h>
  #include <u-boot/rsa-mod-exp.h>
  #include <u-boot/rsa.h>
 
Quoted 4 lines

lib/rsa/rsa-verify.c line 25

Quoted 2 lines
lib/rsa/rsa-verify.c +2 -0
+#ifndef __UBOOT__ /* for host tools */
+#undef CONFIG_RSA_VERIFY_WITH_PKEY

Where should it have been defined?

defined in patch#2

Quoted 5 lines
Quoted 5 lines
lib/rsa/rsa-verify.c +2 -0
+#endif
+
  /* Default public exponent for backward compatibility */
  #define RSA_DEFAULT_PUBEXP	65537
 
Quoted 8 lines

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 352

Quoted 5 lines
lib/rsa/rsa-verify.c +1 -0
@@ -344,6 +349,34 @@ static int rsa_verify_key(struct image_sign_info *info,
  }
  #endif
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Where would CONFIG_RSA_VERIFY_WITH_PKEY be defined? You just a removed it from Kconfig.

Because rsa-keyprop.c be compiled only under CONFIG_RSA_VERIFY_WITH_PKEY per Simon's comment.

Quoted 8 lines

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 354

Quoted 2 lines
lib/rsa/rsa-verify.c +2 -0
+/**
+ * rsa_verify_with_pkey()

The short text for the function is missing.

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 355

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+ *

Please, describe the parameters.

Sure.

-Takahiro Akashi

Quoted 104 lines
Quoted 65 lines
lib/rsa/rsa-verify.c +29 -8
+ */
+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 */
+	prop = rsa_gen_key_prop(info->key, info->keylen);
+	if (!prop) {
+		debug("Generating necessary parameter for decoding failed\n");
+		return -EACCES;
+	}
+
+	ret = rsa_verify_key(info, prop, sig, sig_len, hash,
+			     info->crypto->key_len);
+
+	rsa_free_key_prop(prop);
+
+	return ret;
+}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
  /**
   * rsa_verify_with_keynode() - Verify a signature against some data using
   * information in node with prperties of RSA Key like modulus, exponent etc.
@@ -397,18 +430,21 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
 
  	return ret;
  }
+#endif
 
  int rsa_verify(struct image_sign_info *info,
  	       const struct image_region region[], int region_count,
  	       uint8_t *sig, uint sig_len)
  {
-	const void *blob = info->fdt_blob;
  	/* Reserve memory for maximum checksum-length */
  	uint8_t hash[info->crypto->key_len];
+	int ret = -EACCES;
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	const void *blob = info->fdt_blob;
  	int ndepth, noffset;
  	int sig_node, node;
  	char name[100];
-	int ret;
+#endif
 
  	/*
  	 * Verify that the checksum-length does not exceed the
@@ -421,12 +457,6 @@ int rsa_verify(struct image_sign_info *info,
  		return -EINVAL;
  	}
 
-	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
-	if (sig_node < 0) {
-		debug("%s: No signature node found\n", __func__);
-		return -ENOENT;
-	}
-
  	/* Calculate checksum with checksum-algorithm */
  	ret = info->checksum->calculate(info->checksum->name,
  					region, region_count, hash);

lib/rsa/rsa-verify.c int rsa_verify(struct image_sign_info *info, · line 468

Quoted 4 lines
lib/rsa/rsa-verify.c +1 -0
@@ -435,6 +465,22 @@ int rsa_verify(struct image_sign_info *info,
  		return -EINVAL;
  	}
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Where should this have been defined?

Best regards

Heinrich

Quoted 27 lines
lib/rsa/rsa-verify.c +16 -0
+	if (!info->fdt_blob) {
+		/* don't rely on fdt properties */
+		ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
+
+		return ret;
+	}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
+	if (sig_node < 0) {
+		debug("%s: No signature node found\n", __func__);
+		return -ENOENT;
+	}
+
  	/* See if we must use a particular key */
  	if (info->required_keynode != -1) {
  		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
@@ -461,6 +507,7 @@ int rsa_verify(struct image_sign_info *info,
  				break;
  		}
  	}
+#endif
 
  	return ret;
  }
#16 lore raw

PATCH 3/3 lib: rsa: add rsa_verify_with_pkey()

Quoted 18 lines · Heinrich Schuchardt
Quoted 10 lines · AKASHI Takahiro
Quoted 9 lines · Heinrich Schuchardt
Quoted 8 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.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Is this code tested by test/py/tests/test_vboot.py? Or is there another unit test?

I haven't run vboot test yet.

Quoted 7 lines

I would have assumed that you to through Travis CI before submitting.

Quoted 2 lines

For rsa_verify_with_pkey(), as with vtest for FIT signature, we can test it with my "UEFI secure boot" pytest.

I can't see such a test in this patch series. So how I can test the changes before merging?

The only solution that I can give you is that I would submit rsa patch with my "UEFI secure boot" patch.

-Takahiro Akashi

Quoted 194 lines

Best regards

Heinrich

Quoted 189 lines
Quoted 47 lines
Quoted 47 lines
---
 lib/rsa/Kconfig      |  7 -----
 lib/rsa/Makefile     |  1 -
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 55 insertions(+), 16 deletions(-)
lib/rsa/Kconfig +0 -7
index d1743d7a4c47..62b7ab9c5e5c 100644
@@ -30,13 +30,6 @@ config RSA_VERIFY
 	help
 	  Add RSA signature verification support.
 
-config RSA_VERIFY_WITH_PKEY
-	bool "Execute RSA verification without key parameters from FDT"
-	depends on RSA
-	help
-	  This options enables RSA signature verification without
-	  using public key parameters which is embedded control FDT.
-
 config RSA_SOFTWARE_EXP
 	bool "Enable driver for RSA Modular Exponentiation in software"
 	depends on DM
lib/rsa/Makefile +0 -1
index 14ed3cb4012b..c07305188e0c 100644
@@ -6,5 +6,4 @@
 # 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-verify.c +1 -0
index 1df42f28c64a..ce79984b30f9 100644
@@ -17,9 +17,14 @@
 #include "mkimage.h"
 #include <fdt_support.h>
 #endif
+#include <linux/kconfig.h>
 #include <u-boot/rsa-mod-exp.h>
 #include <u-boot/rsa.h>
 
Quoted 4 lines

lib/rsa/rsa-verify.c line 25

Quoted 2 lines
lib/rsa/rsa-verify.c +2 -0
+#ifndef __UBOOT__ /* for host tools */
+#undef CONFIG_RSA_VERIFY_WITH_PKEY

Where should it have been defined?

defined in patch#2

Quoted 5 lines
Quoted 5 lines
lib/rsa/rsa-verify.c +2 -0
+#endif
+
 /* Default public exponent for backward compatibility */
 #define RSA_DEFAULT_PUBEXP	65537
 
Quoted 8 lines

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 352

Quoted 5 lines
lib/rsa/rsa-verify.c +1 -0
@@ -344,6 +349,34 @@ static int rsa_verify_key(struct image_sign_info *info,
 }
 #endif
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Where would CONFIG_RSA_VERIFY_WITH_PKEY be defined? You just a removed it from Kconfig.

Because rsa-keyprop.c be compiled only under CONFIG_RSA_VERIFY_WITH_PKEY per Simon's comment.

Quoted 8 lines

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 354

Quoted 2 lines
lib/rsa/rsa-verify.c +2 -0
+/**
+ * rsa_verify_with_pkey()

The short text for the function is missing.

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 355

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+ *

Please, describe the parameters.

Sure.

-Takahiro Akashi

Quoted 104 lines
Quoted 65 lines
lib/rsa/rsa-verify.c +29 -8
+ */
+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 */
+	prop = rsa_gen_key_prop(info->key, info->keylen);
+	if (!prop) {
+		debug("Generating necessary parameter for decoding failed\n");
+		return -EACCES;
+	}
+
+	ret = rsa_verify_key(info, prop, sig, sig_len, hash,
+			     info->crypto->key_len);
+
+	rsa_free_key_prop(prop);
+
+	return ret;
+}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
 /**
  * rsa_verify_with_keynode() - Verify a signature against some data using
  * information in node with prperties of RSA Key like modulus, exponent etc.
@@ -397,18 +430,21 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
 
 	return ret;
 }
+#endif
 
 int rsa_verify(struct image_sign_info *info,
 	       const struct image_region region[], int region_count,
 	       uint8_t *sig, uint sig_len)
 {
-	const void *blob = info->fdt_blob;
 	/* Reserve memory for maximum checksum-length */
 	uint8_t hash[info->crypto->key_len];
+	int ret = -EACCES;
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	const void *blob = info->fdt_blob;
 	int ndepth, noffset;
 	int sig_node, node;
 	char name[100];
-	int ret;
+#endif
 
 	/*
 	 * Verify that the checksum-length does not exceed the
@@ -421,12 +457,6 @@ int rsa_verify(struct image_sign_info *info,
 		return -EINVAL;
 	}
 
-	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
-	if (sig_node < 0) {
-		debug("%s: No signature node found\n", __func__);
-		return -ENOENT;
-	}
-
 	/* Calculate checksum with checksum-algorithm */
 	ret = info->checksum->calculate(info->checksum->name,
 					region, region_count, hash);

lib/rsa/rsa-verify.c int rsa_verify(struct image_sign_info *info, · line 468

Quoted 4 lines
lib/rsa/rsa-verify.c +1 -0
@@ -435,6 +465,22 @@ int rsa_verify(struct image_sign_info *info,
 		return -EINVAL;
 	}
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Where should this have been defined?

Best regards

Heinrich

Quoted 27 lines
lib/rsa/rsa-verify.c +16 -0
+	if (!info->fdt_blob) {
+		/* don't rely on fdt properties */
+		ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
+
+		return ret;
+	}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
+	if (sig_node < 0) {
+		debug("%s: No signature node found\n", __func__);
+		return -ENOENT;
+	}
+
 	/* See if we must use a particular key */
 	if (info->required_keynode != -1) {
 		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
@@ -461,6 +507,7 @@ int rsa_verify(struct image_sign_info *info,
 				break;
 		}
 	}
+#endif
 
 	return ret;
 }
#17 lore raw

PATCH 3/3 lib: rsa: add rsa_verify_with_pkey()

Quoted 21 lines · AKASHI Takahiro
Quoted 20 lines · Heinrich Schuchardt
Quoted 10 lines · AKASHI Takahiro
Quoted 9 lines · Heinrich Schuchardt
Quoted 8 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.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Is this code tested by test/py/tests/test_vboot.py? Or is there another unit test?

I haven't run vboot test yet.

I would have assumed that you to through Travis CI before submitting.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

For rsa_verify_with_pkey(), as with vtest for FIT signature, we can test it with my "UEFI secure boot" pytest.

I can't see such a test in this patch series. So how I can test the changes before merging?

The only solution that I can give you is that I would submit rsa patch with my "UEFI secure boot" patch.

I added a very simple function test in "ut" command but will defer sending a new version until after v2 of "x509 parser" patch
as I found there is some dependency on public key parser in rsa_keyprop.c.

-Takahiro Akashi

Quoted 196 lines
Quoted 196 lines

Best regards

Heinrich

Quoted 189 lines
Quoted 47 lines
Quoted 47 lines
---
 lib/rsa/Kconfig      |  7 -----
 lib/rsa/Makefile     |  1 -
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 55 insertions(+), 16 deletions(-)
lib/rsa/Kconfig +0 -7
index d1743d7a4c47..62b7ab9c5e5c 100644
@@ -30,13 +30,6 @@ config RSA_VERIFY
 	help
 	  Add RSA signature verification support.
 
-config RSA_VERIFY_WITH_PKEY
-	bool "Execute RSA verification without key parameters from FDT"
-	depends on RSA
-	help
-	  This options enables RSA signature verification without
-	  using public key parameters which is embedded control FDT.
-
 config RSA_SOFTWARE_EXP
 	bool "Enable driver for RSA Modular Exponentiation in software"
 	depends on DM
lib/rsa/Makefile +0 -1
index 14ed3cb4012b..c07305188e0c 100644
@@ -6,5 +6,4 @@
 # 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-verify.c +1 -0
index 1df42f28c64a..ce79984b30f9 100644
@@ -17,9 +17,14 @@
 #include "mkimage.h"
 #include <fdt_support.h>
 #endif
+#include <linux/kconfig.h>
 #include <u-boot/rsa-mod-exp.h>
 #include <u-boot/rsa.h>
 
Quoted 4 lines

lib/rsa/rsa-verify.c line 25

Quoted 2 lines
lib/rsa/rsa-verify.c +2 -0
+#ifndef __UBOOT__ /* for host tools */
+#undef CONFIG_RSA_VERIFY_WITH_PKEY

Where should it have been defined?

defined in patch#2

Quoted 5 lines
Quoted 5 lines
lib/rsa/rsa-verify.c +2 -0
+#endif
+
 /* Default public exponent for backward compatibility */
 #define RSA_DEFAULT_PUBEXP	65537
 
Quoted 8 lines

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 352

Quoted 5 lines
lib/rsa/rsa-verify.c +1 -0
@@ -344,6 +349,34 @@ static int rsa_verify_key(struct image_sign_info *info,
 }
 #endif
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Where would CONFIG_RSA_VERIFY_WITH_PKEY be defined? You just a removed it from Kconfig.

Because rsa-keyprop.c be compiled only under CONFIG_RSA_VERIFY_WITH_PKEY per Simon's comment.

Quoted 8 lines

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 354

Quoted 2 lines
lib/rsa/rsa-verify.c +2 -0
+/**
+ * rsa_verify_with_pkey()

The short text for the function is missing.

lib/rsa/rsa-verify.c static int rsa_verify_key(struct image_sign_info *info, · line 355

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+ *

Please, describe the parameters.

Sure.

-Takahiro Akashi

Quoted 104 lines
Quoted 65 lines
lib/rsa/rsa-verify.c +29 -8
+ */
+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 */
+	prop = rsa_gen_key_prop(info->key, info->keylen);
+	if (!prop) {
+		debug("Generating necessary parameter for decoding failed\n");
+		return -EACCES;
+	}
+
+	ret = rsa_verify_key(info, prop, sig, sig_len, hash,
+			     info->crypto->key_len);
+
+	rsa_free_key_prop(prop);
+
+	return ret;
+}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
 /**
  * rsa_verify_with_keynode() - Verify a signature against some data using
  * information in node with prperties of RSA Key like modulus, exponent etc.
@@ -397,18 +430,21 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
 
 	return ret;
 }
+#endif
 
 int rsa_verify(struct image_sign_info *info,
 	       const struct image_region region[], int region_count,
 	       uint8_t *sig, uint sig_len)
 {
-	const void *blob = info->fdt_blob;
 	/* Reserve memory for maximum checksum-length */
 	uint8_t hash[info->crypto->key_len];
+	int ret = -EACCES;
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	const void *blob = info->fdt_blob;
 	int ndepth, noffset;
 	int sig_node, node;
 	char name[100];
-	int ret;
+#endif
 
 	/*
 	 * Verify that the checksum-length does not exceed the
@@ -421,12 +457,6 @@ int rsa_verify(struct image_sign_info *info,
 		return -EINVAL;
 	}
 
-	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
-	if (sig_node < 0) {
-		debug("%s: No signature node found\n", __func__);
-		return -ENOENT;
-	}
-
 	/* Calculate checksum with checksum-algorithm */
 	ret = info->checksum->calculate(info->checksum->name,
 					region, region_count, hash);

lib/rsa/rsa-verify.c int rsa_verify(struct image_sign_info *info, · line 468

Quoted 4 lines
lib/rsa/rsa-verify.c +1 -0
@@ -435,6 +465,22 @@ int rsa_verify(struct image_sign_info *info,
 		return -EINVAL;
 	}
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Where should this have been defined?

Best regards

Heinrich

Quoted 27 lines
lib/rsa/rsa-verify.c +16 -0
+	if (!info->fdt_blob) {
+		/* don't rely on fdt properties */
+		ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
+
+		return ret;
+	}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
+	if (sig_node < 0) {
+		debug("%s: No signature node found\n", __func__);
+		return -ENOENT;
+	}
+
 	/* See if we must use a particular key */
 	if (info->required_keynode != -1) {
 		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
@@ -461,6 +507,7 @@ int rsa_verify(struct image_sign_info *info,
 				break;
 		}
 	}
+#endif
 
 	return ret;
 }
#18 lore raw

PATCH 3/3 lib: rsa: add rsa_verify_with_pkey()

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

---
 lib/rsa/Kconfig      |  7 -----
 lib/rsa/Makefile     |  1 -
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 55 insertions(+), 16 deletions(-)
lib/rsa/Kconfig +0 -7
index d1743d7a4c47..62b7ab9c5e5c 100644
@@ -30,13 +30,6 @@ config RSA_VERIFY
 	help
 	  Add RSA signature verification support.
 
-config RSA_VERIFY_WITH_PKEY
-	bool "Execute RSA verification without key parameters from FDT"
-	depends on RSA
-	help
-	  This options enables RSA signature verification without
-	  using public key parameters which is embedded control FDT.
-
 config RSA_SOFTWARE_EXP
 	bool "Enable driver for RSA Modular Exponentiation in software"
 	depends on DM
lib/rsa/Makefile +0 -0
index 14ed3cb4012b..c07305188e0c 100644

lib/rsa/Makefile line 9

Quoted 5 lines
lib/rsa/Makefile +0 -1
@@ -6,5 +6,4 @@
 # 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

Oops, those changes above against Kconfig and Makefile are wrong. They should not be included in this commit. By removing them, everything will work well.
# Those hunks are remnants from last-minute cleanup.

Thanks,
-Takahiro Akashi

Quoted 125 lines
lib/rsa/rsa-verify.c +55 -8
index 1df42f28c64a..ce79984b30f9 100644
@@ -17,9 +17,14 @@
 #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__ /* for host tools */
+#undef CONFIG_RSA_VERIFY_WITH_PKEY
+#endif
+
 /* Default public exponent for backward compatibility */
 #define RSA_DEFAULT_PUBEXP	65537
 
@@ -344,6 +349,34 @@ static int rsa_verify_key(struct image_sign_info *info,
 }
 #endif
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY
+/**
+ * rsa_verify_with_pkey()
+ *
+ */
+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 */
+	prop = rsa_gen_key_prop(info->key, info->keylen);
+	if (!prop) {
+		debug("Generating necessary parameter for decoding failed\n");
+		return -EACCES;
+	}
+
+	ret = rsa_verify_key(info, prop, sig, sig_len, hash,
+			     info->crypto->key_len);
+
+	rsa_free_key_prop(prop);
+
+	return ret;
+}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
 /**
  * rsa_verify_with_keynode() - Verify a signature against some data using
  * information in node with prperties of RSA Key like modulus, exponent etc.
@@ -397,18 +430,21 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
 
 	return ret;
 }
+#endif
 
 int rsa_verify(struct image_sign_info *info,
 	       const struct image_region region[], int region_count,
 	       uint8_t *sig, uint sig_len)
 {
-	const void *blob = info->fdt_blob;
 	/* Reserve memory for maximum checksum-length */
 	uint8_t hash[info->crypto->key_len];
+	int ret = -EACCES;
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	const void *blob = info->fdt_blob;
 	int ndepth, noffset;
 	int sig_node, node;
 	char name[100];
-	int ret;
+#endif
 
 	/*
 	 * Verify that the checksum-length does not exceed the
@@ -421,12 +457,6 @@ int rsa_verify(struct image_sign_info *info,
 		return -EINVAL;
 	}
 
-	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
-	if (sig_node < 0) {
-		debug("%s: No signature node found\n", __func__);
-		return -ENOENT;
-	}
-
 	/* Calculate checksum with checksum-algorithm */
 	ret = info->checksum->calculate(info->checksum->name,
 					region, region_count, hash);
@@ -435,6 +465,22 @@ int rsa_verify(struct image_sign_info *info,
 		return -EINVAL;
 	}
 
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY
+	if (!info->fdt_blob) {
+		/* don't rely on fdt properties */
+		ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
+
+		return ret;
+	}
+#endif
+
+#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
+	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
+	if (sig_node < 0) {
+		debug("%s: No signature node found\n", __func__);
+		return -ENOENT;
+	}
+
 	/* See if we must use a particular key */
 	if (info->required_keynode != -1) {
 		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
@@ -461,6 +507,7 @@ int rsa_verify(struct image_sign_info *info,
 				break;
 		}
 	}
+#endif
 
 	return ret;
 }
Signature · 1 line
2.21.0
#19 lore raw

cover rsa: extend rsa_verify() for UEFI secure boot

In reply to AKASHI Takahiro
Quoted 14 lines · AKASHI Takahiro

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#2.
# Please let me know if this is not appropriate.

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

This patch series does not even compile:
https://travis-ci.org/xypron2/u-boot/builds/596983699

+common/image-sig.c:20:22: error: array type has incomplete element type 'struct checksum_algo'
+ struct checksum_algo checksum_algos[] = {
+ ^~~~~~~~~~~~~~
+common/image-sig.c:22:3: error: field name not in record or union initializer
+ .name = "sha1",
+ ^

Before resubmitting, please, run the whole series through Travis CI or Gitlab CI.

Best regards

Heinrich

Quoted 36 lines

[1] https://bearssl.org/

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 (3):
lib: rsa: decouple rsa from FIT image verification
lib: rsa: generate additional parameters for public key
lib: rsa: add rsa_verify_with_pkey()

 Kconfig                      |   1 +
 common/Makefile              |   3 +-
 common/image-fit-sig.c       | 417 +++++++++++++++++++++++++
 common/image-fit.c           |   6 +-
 common/image-sig.c           | 396 ------------------------
 include/image.h              |  14 +-
 include/u-boot/rsa-mod-exp.h |   3 +
 lib/rsa/Kconfig              |  12 +
 lib/rsa/Makefile             |   2 +-
 lib/rsa/rsa-keyprop.c        | 585 +++++++++++++++++++++++++++++++++++
 lib/rsa/rsa-verify.c         |  65 +++-
 tools/Makefile               |   2 +-
 12 files changed, 1095 insertions(+), 411 deletions(-)
 create mode 100644 common/image-fit-sig.c
 create mode 100644 lib/rsa/rsa-keyprop.c