Skip to contentSkip to conversation

rsa: extend rsa_verify() for UEFI secure boot

RFC New AKASHI Takahiro 4 participants 18 messages Sep 6, 2019 – Oct 28, 2019

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/

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()

 include/u-boot/rsa-mod-exp.h |   3 +
 lib/rsa/Kconfig              |  14 +
 lib/rsa/Makefile             |   3 +-
 lib/rsa/rsa-keyprop.c        | 631 +++++++++++++++++++++++++++++++++++
 lib/rsa/rsa-verify.c         |  63 +++-
 5 files changed, 705 insertions(+), 9 deletions(-)
 create mode 100644 lib/rsa/rsa-keyprop.c
Signature · 1 line
2.21.0
#1 lore raw

RFC 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.

---
 lib/rsa/Kconfig  | 7 +++++++
 lib/rsa/Makefile | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
lib/rsa/Kconfig +7 -0
index 2b33f323bccc..338c8124da59 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,12 @@ if RSA
 
 config SPL_RSA
 	bool "Use RSA Library within SPL"
+	select RSA_VERIFY
+
+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..226d8f3514a9 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_RSA_VERIFY) += rsa-verify.o rsa-checksum.o
 obj-$(CONFIG_RSA_SOFTWARE_EXP) += rsa-mod-exp.o
Signature · 1 line
2.21.0
#2 lore raw

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

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

---
  lib/rsa/Kconfig  | 7 +++++++
  lib/rsa/Makefile | 2 +-
  2 files changed, 8 insertions(+), 1 deletion(-)
lib/rsa/Kconfig +7 -0
index 2b33f323bccc..338c8124da59 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,12 @@ if RSA
 
  config SPL_RSA
  	bool "Use RSA Library within SPL"
+	select RSA_VERIFY
+
+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 +0 -0
index a51c6e1685fb..226d8f3514a9 100644

lib/rsa/Makefile line 9

Quoted 6 lines
lib/rsa/Makefile +1 -1
@@ -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_RSA_VERIFY) += rsa-verify.o rsa-checksum.o
  obj-$(CONFIG_RSA_SOFTWARE_EXP) += rsa-mod-exp.o

pine64-lts_defconfig with CONFIG_RSA=y compiles fine without this patch. But with this patch:

lib/rsa/rsa-verify.c:60:5: error: redefinition of ‘padding_pkcs_15_verify’
60 | int padding_pkcs_15_verify(struct image_sign_info *info,
| ^~~~~~~~~~~~~~~~~~~~~~
In file included from lib/rsa/rsa-verify.c:21:
include/u-boot/rsa.h:118:19: note: previous definition of ‘padding_pkcs_15_verify’ was here
118 | static inline int padding_pkcs_15_verify(struct image_sign_info
*info,

       |                   ^~~~~~~~~~~~~~~~~~~~~~
lib/rsa/rsa-verify.c:399:5: error: redefinition of ‘rsa_verify’
   399 | int rsa_verify(struct image_sign_info *info,
       |     ^~~~~~~~~~

CC lib/efi_loader/efi_hii.o
In file included from lib/rsa/rsa-verify.c:21:
include/u-boot/rsa.h:111:19: note: previous definition of ‘rsa_verify’ was here
111 | static inline int rsa_verify(struct image_sign_info *info,

       |                   ^~~~~~~~~~

Best regards

Heinrich

#3 lore raw

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

Quoted 44 lines · Heinrich Schuchardt
Quoted 43 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.

---
 lib/rsa/Kconfig  | 7 +++++++
 lib/rsa/Makefile | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
lib/rsa/Kconfig +7 -0
index 2b33f323bccc..338c8124da59 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,12 @@ if RSA
 
 config SPL_RSA
 	bool "Use RSA Library within SPL"
+	select RSA_VERIFY
+
+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 +0 -0
index a51c6e1685fb..226d8f3514a9 100644
@@ -5,5 +5,5 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
Quoted 7 lines

lib/rsa/Makefile line 9

Quoted 4 lines
lib/rsa/Makefile +1 -1
-obj-$(CONFIG_$(SPL_)FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o
+obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o
 obj-$(CONFIG_RSA_SOFTWARE_EXP) += rsa-mod-exp.o

pine64-lts_defconfig with CONFIG_RSA=y compiles fine without this patch. But with this patch:

Right.
It seems that include/image.h will also have to be modified.

Thanks,
-Takahiro Akashi

Quoted 22 lines

lib/rsa/rsa-verify.c:60:5: error: redefinition of ‘padding_pkcs_15_verify’
60 | int padding_pkcs_15_verify(struct image_sign_info *info,

      |     ^~~~~~~~~~~~~~~~~~~~~~

In file included from lib/rsa/rsa-verify.c:21:
include/u-boot/rsa.h:118:19: note: previous definition of ‘padding_pkcs_15_verify’ was here
118 | static inline int padding_pkcs_15_verify(struct image_sign_info
*info,

      |                   ^~~~~~~~~~~~~~~~~~~~~~
lib/rsa/rsa-verify.c:399:5: error: redefinition of ‘rsa_verify’
  399 | int rsa_verify(struct image_sign_info *info,
      |     ^~~~~~~~~~

CC lib/efi_loader/efi_hii.o
In file included from lib/rsa/rsa-verify.c:21:
include/u-boot/rsa.h:111:19: note: previous definition of ‘rsa_verify’ was here
111 | static inline int rsa_verify(struct image_sign_info *info,

      |                   ^~~~~~~~~~

Best regards

Heinrich

#4 lore raw

RFC 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/Makefile             |   2 +-
 lib/rsa/rsa-keyprop.c        | 631 +++++++++++++++++++++++++++++++++++
 3 files changed, 635 insertions(+), 1 deletion(-)
 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/Makefile +1 -1
index 226d8f3514a9..d66eef74c514 100644
@@ -5,5 +5,5 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
-obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o
+obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o
 obj-$(CONFIG_RSA_SOFTWARE_EXP) += rsa-mod-exp.o
lib/rsa/rsa-keyprop.c new file +631 -0

631 lines not on this page · raw message

Signature · 1 line
2.21.0
#5 lore raw

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

Hi AKASHI,

Quoted 42 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/Makefile             |   2 +-
 lib/rsa/rsa-keyprop.c        | 631 +++++++++++++++++++++++++++++++++++
 3 files changed, 635 insertions(+), 1 deletion(-)
 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/Makefile +0 -0
index 226d8f3514a9..d66eef74c514 100644

lib/rsa/Makefile line 8

Quoted 5 lines
lib/rsa/Makefile +1 -1
@@ -5,5 +5,5 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
-obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o
+obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o

Can this code only be included when needed? It seems a bit large,

Quoted 10 lines
lib/rsa/Makefile +0 -0
 obj-$(CONFIG_RSA_SOFTWARE_EXP) += rsa-mod-exp.o
lib/rsa/rsa-keyprop.c new file +3 -0
new file mode 100644
index 000000000000..e650a931dff9
@@ -0,0 +1,631 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ *  RSA library - generate parameters for a public key

lib/rsa/rsa-keyprop.c line 13

Quoted 10 lines
lib/rsa/rsa-keyprop.c new file +10 -0
+ *
+ *  Copyright (c) 2019 Linaro Limited
+ *  Author: AKASHI Takahiro
+ *
+ *  Big number routines in this file come from BearSSL.
+ *  See the original copyright below.
+ *
+ * Copyright (c) 2016 Thomas Pornin <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining

Can you use SPDX?

Quoted 10 lines
lib/rsa/rsa-keyprop.c new file +10 -0
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *

lib/rsa/rsa-keyprop.c line 33

Quoted 10 lines
lib/rsa/rsa-keyprop.c new file +10 -0
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include <stdio.h>

Should this include common.h?

lib/rsa/rsa-keyprop.c line 37

Quoted 4 lines
lib/rsa/rsa-keyprop.c new file +4 -0
+
+#include <image.h>
+#include <malloc.h>
+#include <crypto/internal/rsa.h>

Hmm this seems to be for running on the host?

Regards,
Simon

#6 lore raw

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

Hi Simon,

Quoted 48 lines · Simon Glass

Hi AKASHI,

Quoted 45 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/Makefile             |   2 +-
 lib/rsa/rsa-keyprop.c        | 631 +++++++++++++++++++++++++++++++++++
 3 files changed, 635 insertions(+), 1 deletion(-)
 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/Makefile +0 -0
index 226d8f3514a9..d66eef74c514 100644
@@ -5,5 +5,5 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
Quoted 4 lines

lib/rsa/Makefile line 8

Quoted 2 lines
lib/rsa/Makefile +1 -1
-obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o
+obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o

Can this code only be included when needed? It seems a bit large,

Okay, compiled in only if CONFIG_RSA_VERIFY_WITH_PKEY.

Quoted 14 lines
Quoted 14 lines
lib/rsa/Makefile +0 -0
 obj-$(CONFIG_RSA_SOFTWARE_EXP) += rsa-mod-exp.o
lib/rsa/rsa-keyprop.c new file +7 -0
new file mode 100644
index 000000000000..e650a931dff9
@@ -0,0 +1,631 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ *  RSA library - generate parameters for a public key
+ *
+ *  Copyright (c) 2019 Linaro Limited
+ *  Author: AKASHI Takahiro
+ *
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 13

Quoted 6 lines
lib/rsa/rsa-keyprop.c new file +6 -0
+ *  Big number routines in this file come from BearSSL.
+ *  See the original copyright below.
+ *
+ * Copyright (c) 2016 Thomas Pornin <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining

Can you use SPDX?

I'm not sure this license is listed in SPDX, but will ask the author.

Quoted 21 lines
Quoted 10 lines
lib/rsa/rsa-keyprop.c new file +10 -0
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *

lib/rsa/rsa-keyprop.c line 33

Quoted 10 lines
lib/rsa/rsa-keyprop.c new file +10 -0
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include <stdio.h>
Quoted 1 line

Should this include common.h?

Okay.

Quoted 6 lines

lib/rsa/rsa-keyprop.c line 37

Quoted 4 lines
lib/rsa/rsa-keyprop.c new file +4 -0
+
+#include <image.h>
+#include <malloc.h>
+#include <crypto/internal/rsa.h>

Hmm this seems to be for running on the host?

rsa.h? No. It was imported from linux kernel to use rsa_parse_pub_key() in rsa_gen_key_prop().

Thanks,
-Takahiro Akashi

Quoted 2 lines

Regards,
Simon

#7 lore raw

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

In reply to AKASHI Takahiro
Quoted 40 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/Makefile             |   2 +-
 lib/rsa/rsa-keyprop.c        | 631 +++++++++++++++++++++++++++++++++++
 3 files changed, 635 insertions(+), 1 deletion(-)
 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/rsa-keyprop.c new file +1 -0
@@ -0,0 +1,631 @@
+

[...]

Quoted 37 lines
lib/rsa/rsa-keyprop.c new file +37 -0
+/* stripped version of src/inner.h */
+
+static inline unsigned
+br_dec16be(const void *src)
+{
+#if 0 /* BR_BE_UNALIGNED */
+	return ((const br_union_u16 *)src)->u;
+#else
+	const unsigned char *buf;
+
+	buf = src;
+	return ((unsigned)buf[0] << 8) | (unsigned)buf[1];
+#endif
+}
+
+static inline uint32_t
+br_dec32be(const void *src)
+{
+#if 0 /* BR_BE_UNALIGNED */
+	return ((const br_union_u32 *)src)->u;
+#else
+	const unsigned char *buf;
+
+	buf = src;
+	return ((uint32_t)buf[0] << 24)
+		| ((uint32_t)buf[1] << 16)
+		| ((uint32_t)buf[2] << 8)
+		| (uint32_t)buf[3];
+#endif
+}
+
+static inline void
+br_enc32be(void *dst, uint32_t x)
+{
+#if 0 /* BR_BE_UNALIGNED */
+	((br_union_u32 *)dst)->u = x;
+#else

lib/rsa/rsa-keyprop.c line 86

Quoted 10 lines
lib/rsa/rsa-keyprop.c new file +10 -0
+	unsigned char *buf;
+
+	buf = dst;
+	buf[0] = (unsigned char)(x >> 24);
+	buf[1] = (unsigned char)(x >> 16);
+	buf[2] = (unsigned char)(x >> 8);
+	buf[3] = (unsigned char)x;
+#endif
+}
+

There's no U-Boot API for the above?

lib/rsa/rsa-keyprop.c line 91

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

Ditto

Quoted 20 lines
lib/rsa/rsa-keyprop.c new file +20 -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)
+{
+	uint32_t q;
+
+	q = x ^ y;
+	return NOT((q | -q) >> 31);
+}
+
+static inline uint32_t
+NEQ(uint32_t x, uint32_t y)
+{
+	uint32_t q;

lib/rsa/rsa-keyprop.c line 121

Quoted 10 lines
lib/rsa/rsa-keyprop.c new file +10 -0
+
+	q = x ^ y;
+	return (q | -q) >> 31;
+}
+
+static inline uint32_t
+GT(uint32_t x, uint32_t y)
+{
+	/*
+	 * If both x < 2^31 and x < 2^31, then y-x will have its high

second one should be y^31

Quoted 19 lines
lib/rsa/rsa-keyprop.c new file +12 -0
+	 * bit set if x > y, cleared otherwise.
+	 *
+}
+
+struct key_prop *rsa_gen_key_prop(const void *key, uint32_t keylen)
+{
+	struct key_prop *prop;
+	struct rsa_key rsa_key;
+#define BR_MAX_RSA_SIZE 4096
+	uint32_t *n, *rr, *rrtmp;
+	int rlen, i, ret;
+	prop->n0inv = br_i32_ninv32(n[1]);
Signature · 6 lines
2.21.0

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot

Regards
/Ilias

#8 lore raw

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

Ilias,

Quoted 85 lines · Ilias Apalodimas
Quoted 40 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/Makefile             |   2 +-
 lib/rsa/rsa-keyprop.c        | 631 +++++++++++++++++++++++++++++++++++
 3 files changed, 635 insertions(+), 1 deletion(-)
 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/rsa-keyprop.c new file +1 -0
@@ -0,0 +1,631 @@
+

[...]

Quoted 41 lines
lib/rsa/rsa-keyprop.c new file +41 -0
+/* stripped version of src/inner.h */
+
+static inline unsigned
+br_dec16be(const void *src)
+{
+#if 0 /* BR_BE_UNALIGNED */
+	return ((const br_union_u16 *)src)->u;
+#else
+	const unsigned char *buf;
+
+	buf = src;
+	return ((unsigned)buf[0] << 8) | (unsigned)buf[1];
+#endif
+}
+
+static inline uint32_t
+br_dec32be(const void *src)
+{
+#if 0 /* BR_BE_UNALIGNED */
+	return ((const br_union_u32 *)src)->u;
+#else
+	const unsigned char *buf;
+
+	buf = src;
+	return ((uint32_t)buf[0] << 24)
+		| ((uint32_t)buf[1] << 16)
+		| ((uint32_t)buf[2] << 8)
+		| (uint32_t)buf[3];
+#endif
+}
+
+static inline void
+br_enc32be(void *dst, uint32_t x)
+{
+#if 0 /* BR_BE_UNALIGNED */
+	((br_union_u32 *)dst)->u = x;
+#else
+	unsigned char *buf;
+
+	buf = dst;
+	buf[0] = (unsigned char)(x >> 24);
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 86

Quoted 6 lines
lib/rsa/rsa-keyprop.c new file +6 -0
+	buf[1] = (unsigned char)(x >> 16);
+	buf[2] = (unsigned char)(x >> 8);
+	buf[3] = (unsigned char)x;
+#endif
+}
+

There's no U-Boot API for the above?

Do you mean dec32be() and enc32be()?
Yes, there are similar functions but I intentionally don't use them as I want to keep the difference between BearSSL's original code and imported one in this file to a minimum.

Anyhow, this code won't work for big-endian. We should manage it.

Quoted 32 lines

lib/rsa/rsa-keyprop.c line 91

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

Ditto

Quoted 24 lines
lib/rsa/rsa-keyprop.c new file +24 -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)
+{
+	uint32_t q;
+
+	q = x ^ y;
+	return NOT((q | -q) >> 31);
+}
+
+static inline uint32_t
+NEQ(uint32_t x, uint32_t y)
+{
+	uint32_t q;
+
+	q = x ^ y;
+	return (q | -q) >> 31;
+}
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 121

Quoted 6 lines
lib/rsa/rsa-keyprop.c new file +6 -0
+
+static inline uint32_t
+GT(uint32_t x, uint32_t y)
+{
+	/*
+	 * If both x < 2^31 and x < 2^31, then y-x will have its high

second one should be y^31

Do you mean that the second "x < 2^31" be "y < 2^31"? You're right.

Thanks,
-Takahiro Akashi

Quoted 24 lines
Quoted 20 lines
lib/rsa/rsa-keyprop.c new file +12 -0
+	 * bit set if x > y, cleared otherwise.
+	 *
+}
+
+struct key_prop *rsa_gen_key_prop(const void *key, uint32_t keylen)
+{
+	struct key_prop *prop;
+	struct rsa_key rsa_key;
+#define BR_MAX_RSA_SIZE 4096
+	uint32_t *n, *rr, *rrtmp;
+	int rlen, i, ret;
+	prop->n0inv = br_i32_ninv32(n[1]);
Signature · 6 lines
2.21.0

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot

Regards
/Ilias

#9 lore raw

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

Quoted 97 lines · AKASHI Takahiro

Ilias,

Quoted 85 lines · Ilias Apalodimas
Quoted 40 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/Makefile             |   2 +-
  lib/rsa/rsa-keyprop.c        | 631 +++++++++++++++++++++++++++++++++++
  3 files changed, 635 insertions(+), 1 deletion(-)

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/rsa-keyprop.c new file +1 -0
@@ -0,0 +1,631 @@
+

[...]

Quoted 41 lines
lib/rsa/rsa-keyprop.c new file +41 -0
+/* stripped version of src/inner.h */
+
+static inline unsigned
+br_dec16be(const void *src)
+{
+#if 0 /* BR_BE_UNALIGNED */
+	return ((const br_union_u16 *)src)->u;
+#else
+	const unsigned char *buf;
+
+	buf = src;
+	return ((unsigned)buf[0] << 8) | (unsigned)buf[1];
+#endif
+}
+
+static inline uint32_t
+br_dec32be(const void *src)
+{
+#if 0 /* BR_BE_UNALIGNED */
+	return ((const br_union_u32 *)src)->u;
+#else
+	const unsigned char *buf;
+
+	buf = src;
+	return ((uint32_t)buf[0] << 24)
+		| ((uint32_t)buf[1] << 16)
+		| ((uint32_t)buf[2] << 8)
+		| (uint32_t)buf[3];
+#endif
+}
+
+static inline void
+br_enc32be(void *dst, uint32_t x)
+{
+#if 0 /* BR_BE_UNALIGNED */
+	((br_union_u32 *)dst)->u = x;
+#else
+	unsigned char *buf;
+
+	buf = dst;
+	buf[0] = (unsigned char)(x >> 24);
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 86

Quoted 6 lines
lib/rsa/rsa-keyprop.c new file +6 -0
+	buf[1] = (unsigned char)(x >> 16);
+	buf[2] = (unsigned char)(x >> 8);
+	buf[3] = (unsigned char)x;
+#endif
+}
+

There's no U-Boot API for the above?

Quoted 6 lines

Do you mean dec32be() and enc32be()?
Yes, there are similar functions but I intentionally don't use them as I want to keep the difference between BearSSL's original code and imported one in this file to a minimum.

Anyhow, this code won't work for big-endian. We should manage it.

htonl() does the conversion both for big- and little-endian for properly aligned fields.

Tom is always unhappy when we unnecessarily enlarge the U-Boot binary.

Best regards

Heinrich

Quoted 73 lines
Quoted 33 lines

lib/rsa/rsa-keyprop.c line 91

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

Ditto

Quoted 24 lines
lib/rsa/rsa-keyprop.c new file +24 -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)
+{
+	uint32_t q;
+
+	q = x ^ y;
+	return NOT((q | -q) >> 31);
+}
+
+static inline uint32_t
+NEQ(uint32_t x, uint32_t y)
+{
+	uint32_t q;
+
+	q = x ^ y;
+	return (q | -q) >> 31;
+}
Quoted 8 lines

lib/rsa/rsa-keyprop.c line 121

Quoted 6 lines
lib/rsa/rsa-keyprop.c new file +6 -0
+
+static inline uint32_t
+GT(uint32_t x, uint32_t y)
+{
+	/*
+	 * If both x < 2^31 and x < 2^31, then y-x will have its high

second one should be y^31

Do you mean that the second "x < 2^31" be "y < 2^31"? You're right.

Thanks,
-Takahiro Akashi

Quoted 24 lines
Quoted 16 lines
lib/rsa/rsa-keyprop.c new file +12 -1
+	 * bit set if x > y, cleared otherwise.
+	 *
+}
+
+struct key_prop *rsa_gen_key_prop(const void *key, uint32_t keylen)
+{
+	struct key_prop *prop;
+	struct rsa_key rsa_key;
+#define BR_MAX_RSA_SIZE 4096
+	uint32_t *n, *rr, *rrtmp;
+	int rlen, i, ret;
+	prop->n0inv = br_i32_ninv32(n[1]);
--

2.21.0

Quoted 4 lines

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot

Regards
/Ilias

#10 lore raw

RFC 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     |  3 ++-
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 64 insertions(+), 9 deletions(-)
lib/rsa/Kconfig +7 -0
index 338c8124da59..3c1986a26f8c 100644
@@ -25,6 +25,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 +2 -1
index d66eef74c514..fd4592fd6a8a 100644
@@ -5,5 +5,6 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
-obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o
+obj-$(CONFIG_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
#11 lore raw

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

Hi AKASHI,

Quoted 21 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     |  3 ++-
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 64 insertions(+), 9 deletions(-)
lib/rsa/Kconfig +0 -0
index 338c8124da59..3c1986a26f8c 100644

lib/rsa/Kconfig config RSA_VERIFY · line 33

Quoted 9 lines
lib/rsa/Kconfig +6 -0
@@ -25,6 +25,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.

Please expand this, a lot. It is too brief.

Quoted 108 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 +2 -1
index d66eef74c514..fd4592fd6a8a 100644
@@ -5,5 +5,6 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
-obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o
+obj-$(CONFIG_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 +38 -8
index 287fcc4d234d..80eabff3e940 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
 
@@ -342,6 +347,34 @@ static int rsa_verify_key(struct image_sign_info *info,
        return 0;
 }
 
+#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.
@@ -395,18 +428,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
@@ -419,12 +455,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 466

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

Can this use if() instead of #ifdef?

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

Quoted 3 lines
lib/rsa/rsa-verify.c +3 -0
+       if (!info->fdt_blob) {
+               /* don't rely on fdt properties */
+               ret = rsa_verify_with_pkey(info, hash, sig, sig_len);

Does this support required_keynode?

Please add to the documentation for secure boot in uImage, as this seems to be a new case.

Also, how do we test this new code?

Quoted 26 lines
lib/rsa/rsa-verify.c +13 -1
+
+               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,
@@ -459,6 +505,7 @@ int rsa_verify(struct image_sign_info *info,
                                break;
                }
        }
+#endif
 
        return ret;
 }
--

2.21.0

#12 lore raw

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

Simon,

Overall, do you agree to my approach here?

Quoted 27 lines · Simon Glass

Hi AKASHI,

Quoted 24 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     |  3 ++-
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 64 insertions(+), 9 deletions(-)
lib/rsa/Kconfig +0 -0
index 338c8124da59..3c1986a26f8c 100644
@@ -25,6 +25,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
Quoted 8 lines

lib/rsa/Kconfig config RSA_VERIFY · line 33

Quoted 6 lines
lib/rsa/Kconfig +6 -0
+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.

Please expand this, a lot. It is too brief.

Will add more description.

Quoted 111 lines
Quoted 111 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 +2 -1
index d66eef74c514..fd4592fd6a8a 100644
@@ -5,5 +5,6 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
-obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o
+obj-$(CONFIG_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 +38 -8
index 287fcc4d234d..80eabff3e940 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
 
@@ -342,6 +347,34 @@ static int rsa_verify_key(struct image_sign_info *info,
        return 0;
 }
 
+#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.
@@ -395,18 +428,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
@@ -419,12 +455,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);
@@ -433,6 +463,22 @@ int rsa_verify(struct image_sign_info *info,
                return -EINVAL;
        }
 
Quoted 3 lines

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

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Can this use if() instead of #ifdef?

Do you mean
if (IS_ENABLED(CONFIG_RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) ?

Quoted 5 lines

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

Quoted 3 lines
lib/rsa/rsa-verify.c +3 -0
+       if (!info->fdt_blob) {
+               /* don't rely on fdt properties */
+               ret = rsa_verify_with_pkey(info, hash, sig, sig_len);

Does this support required_keynode?

IICU, no because required_keynode is an offset into fdt.

Quoted 2 lines

Please add to the documentation for secure boot in uImage, as this seems to be a new case.

It may work with FIT in case of no extra key parameters, I intended that the feature is used only by UEFI.
So I don't think it appropriate to add a doc under doc/uImage.FIT.

Quoted 1 line

Also, how do we test this new code?

The code is exercised and tested only through UEFI's verification code and relevant pytest (in my secure boot patch[1]). Like test_vboot?

[1] https://lists.denx.de/pipermail/u-boot/2019-September/383897.html

Thanks,
-Takahiro Akashi

Quoted 27 lines
Quoted 27 lines
lib/rsa/rsa-verify.c +13 -1
+
+               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,
@@ -459,6 +505,7 @@ int rsa_verify(struct image_sign_info *info,
                                break;
                }
        }
+#endif
 
        return ret;
 }
--

2.21.0

#13 lore raw

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

Simon,

Quoted 3 lines · AKASHI Takahiro

Simon,

Overall, do you agree to my approach here?

Ping. Do you mind my sending out v2?

-Takahiro Akashi

Quoted 210 lines · Simon Glass
Quoted 27 lines

Hi AKASHI,

Quoted 24 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     |  3 ++-
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 64 insertions(+), 9 deletions(-)
lib/rsa/Kconfig +0 -0
index 338c8124da59..3c1986a26f8c 100644
@@ -25,6 +25,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
Quoted 8 lines

lib/rsa/Kconfig config RSA_VERIFY · line 33

Quoted 6 lines
lib/rsa/Kconfig +6 -0
+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.

Please expand this, a lot. It is too brief.

Will add more description.

Quoted 111 lines
Quoted 111 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 +2 -1
index d66eef74c514..fd4592fd6a8a 100644
@@ -5,5 +5,6 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
-obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o
+obj-$(CONFIG_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 +38 -8
index 287fcc4d234d..80eabff3e940 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
 
@@ -342,6 +347,34 @@ static int rsa_verify_key(struct image_sign_info *info,
        return 0;
 }
 
+#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.
@@ -395,18 +428,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
@@ -419,12 +455,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);
@@ -433,6 +463,22 @@ int rsa_verify(struct image_sign_info *info,
                return -EINVAL;
        }
 
Quoted 3 lines

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

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Can this use if() instead of #ifdef?

Do you mean
if (IS_ENABLED(CONFIG_RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) ?

Quoted 5 lines

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

Quoted 3 lines
lib/rsa/rsa-verify.c +3 -0
+       if (!info->fdt_blob) {
+               /* don't rely on fdt properties */
+               ret = rsa_verify_with_pkey(info, hash, sig, sig_len);

Does this support required_keynode?

IICU, no because required_keynode is an offset into fdt.

Quoted 2 lines

Please add to the documentation for secure boot in uImage, as this seems to be a new case.

It may work with FIT in case of no extra key parameters, I intended that the feature is used only by UEFI.
So I don't think it appropriate to add a doc under doc/uImage.FIT.

Quoted 1 line

Also, how do we test this new code?

The code is exercised and tested only through UEFI's verification code and relevant pytest (in my secure boot patch[1]). Like test_vboot?

[1] https://lists.denx.de/pipermail/u-boot/2019-September/383897.html

Thanks,
-Takahiro Akashi

Quoted 27 lines
Quoted 27 lines
lib/rsa/rsa-verify.c +13 -1
+
+               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,
@@ -459,6 +505,7 @@ int rsa_verify(struct image_sign_info *info,
                                break;
                }
        }
+#endif
 
        return ret;
 }
--

2.21.0

#14 lore raw

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

In reply to AKASHI Takahiro

Hi Takahiro,

Quoted 120 lines · AKASHI Takahiro

Simon,

Overall, do you agree to my approach here?

Quoted 27 lines · Simon Glass

Hi AKASHI,

Quoted 24 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     |  3 ++-
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 64 insertions(+), 9 deletions(-)
lib/rsa/Kconfig +0 -0
index 338c8124da59..3c1986a26f8c 100644
@@ -25,6 +25,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
Quoted 8 lines

lib/rsa/Kconfig config RSA_VERIFY · line 33

Quoted 6 lines
lib/rsa/Kconfig +6 -0
+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.

Please expand this, a lot. It is too brief.

Will add more description.

Quoted 76 lines
Quoted 76 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 +2 -1
index d66eef74c514..fd4592fd6a8a 100644
@@ -5,5 +5,6 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
-obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o
+obj-$(CONFIG_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 +34 -0
index 287fcc4d234d..80eabff3e940 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
 
@@ -342,6 +347,34 @@ static int rsa_verify_key(struct image_sign_info *info,
        return 0;
 }
 
+#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.
@@ -395,18 +428,21 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
 
        return ret;
 }
+#endif
 
Quoted 9 lines
Quoted 9 lines

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

Quoted 9 lines
lib/rsa/rsa-verify.c +2 -1
 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)

Can you use if() instead of #if ?

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
lib/rsa/rsa-verify.c +2 -7
+       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
@@ -419,12 +455,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);
@@ -433,6 +463,22 @@ int rsa_verify(struct image_sign_info *info,
                return -EINVAL;
        }
 
Quoted 6 lines
Quoted 3 lines

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

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Can this use if() instead of #ifdef?

Do you mean
if (IS_ENABLED(CONFIG_RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) ?

Yes

Quoted 7 lines
Quoted 6 lines

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

Quoted 4 lines
lib/rsa/rsa-verify.c +3 -0
+       if (!info->fdt_blob) {
+               /* don't rely on fdt properties */
+               ret = rsa_verify_with_pkey(info, hash, sig, sig_len);

Does this support required_keynode?

Quoted 8 lines

IICU, no because required_keynode is an offset into fdt.

Quoted 2 lines

Please add to the documentation for secure boot in uImage, as this seems to be a new case.

It may work with FIT in case of no extra key parameters, I intended that the feature is used only by UEFI.
So I don't think it appropriate to add a doc under doc/uImage.FIT.

Sounds good.

Quoted 5 lines
Quoted 2 lines

Also, how do we test this new code?

The code is exercised and tested only through UEFI's verification code and relevant pytest (in my secure boot patch[1]). Like test_vboot?

OK good.

Quoted 5 lines

[1] https://lists.denx.de/pipermail/u-boot/2019-September/383897.html

Thanks,
-Takahiro Akashi

Regards,
Simon

#15 lore raw

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

Quoted 124 lines · Simon Glass

Hi Takahiro,

Quoted 120 lines · AKASHI Takahiro

Simon,

Overall, do you agree to my approach here?

Quoted 27 lines · Simon Glass

Hi AKASHI,

Quoted 24 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     |  3 ++-
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 64 insertions(+), 9 deletions(-)
lib/rsa/Kconfig +0 -0
index 338c8124da59..3c1986a26f8c 100644
@@ -25,6 +25,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
Quoted 8 lines

lib/rsa/Kconfig config RSA_VERIFY · line 33

Quoted 6 lines
lib/rsa/Kconfig +6 -0
+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.

Please expand this, a lot. It is too brief.

Will add more description.

Quoted 76 lines
Quoted 76 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 +2 -1
index d66eef74c514..fd4592fd6a8a 100644
@@ -5,5 +5,6 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
-obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o
+obj-$(CONFIG_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 +34 -0
index 287fcc4d234d..80eabff3e940 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
 
@@ -342,6 +347,34 @@ static int rsa_verify_key(struct image_sign_info *info,
        return 0;
 }
 
+#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.
@@ -395,18 +428,21 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
 
        return ret;
 }
+#endif
 
Quoted 11 lines
Quoted 9 lines
Quoted 9 lines

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

Quoted 9 lines
lib/rsa/rsa-verify.c +2 -1
 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)

Can you use if() instead of #if ?

This code is for local variable declaration, and so changing as you suggested can be difficult. Or do you accept moving this stuff further down to the next "#if CONFIG_IS_ENABLED(FIT_SIGNATURE)":
=== 8< ===

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)) {
	/*
	 * declare variables here!
	 */
	const void *blob = info->fdt_blob;
	int ndepth, noffset;
	int sig_node, node;
	char name[100];

	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 */
	...
}

=== >8 ===

Otherwise, we will have to introduce another function, rsa_verify_with_fdt?, just for this purpose.

Quoted 54 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
lib/rsa/rsa-verify.c +2 -7
+       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
@@ -419,12 +455,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);
@@ -433,6 +463,22 @@ int rsa_verify(struct image_sign_info *info,
                return -EINVAL;
        }
 
Quoted 6 lines
Quoted 3 lines

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

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+#ifdef CONFIG_RSA_VERIFY_WITH_PKEY

Can this use if() instead of #ifdef?

Do you mean
if (IS_ENABLED(CONFIG_RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) ?

Yes

Quoted 7 lines
Quoted 6 lines

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

Quoted 4 lines
lib/rsa/rsa-verify.c +3 -0
+       if (!info->fdt_blob) {
+               /* don't rely on fdt properties */
+               ret = rsa_verify_with_pkey(info, hash, sig, sig_len);

Does this support required_keynode?

Quoted 8 lines

IICU, no because required_keynode is an offset into fdt.

Quoted 2 lines

Please add to the documentation for secure boot in uImage, as this seems to be a new case.

It may work with FIT in case of no extra key parameters, I intended that the feature is used only by UEFI.
So I don't think it appropriate to add a doc under doc/uImage.FIT.

Sounds good.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Also, how do we test this new code?

The code is exercised and tested only through UEFI's verification code and relevant pytest (in my secure boot patch[1]). Like test_vboot?

OK good.

Thanks,
-Takahiro Akashi

Quoted 8 lines
Quoted 5 lines

[1] https://lists.denx.de/pipermail/u-boot/2019-September/383897.html

Thanks,
-Takahiro Akashi

Regards,
Simon

#16 lore raw

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

Hi Takahiro,

Quoted 138 lines · AKASHI Takahiro
Quoted 124 lines · Simon Glass

Hi Takahiro,

Quoted 120 lines · AKASHI Takahiro

Simon,

Overall, do you agree to my approach here?

Quoted 27 lines · Simon Glass

Hi AKASHI,

Quoted 24 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     |  3 ++-
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 64 insertions(+), 9 deletions(-)
lib/rsa/Kconfig +0 -0
index 338c8124da59..3c1986a26f8c 100644
@@ -25,6 +25,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
Quoted 8 lines

lib/rsa/Kconfig config RSA_VERIFY · line 33

Quoted 6 lines
lib/rsa/Kconfig +6 -0
+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.

Please expand this, a lot. It is too brief.

Will add more description.

Quoted 76 lines
Quoted 76 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 +2 -1
index d66eef74c514..fd4592fd6a8a 100644
@@ -5,5 +5,6 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
-obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o
+obj-$(CONFIG_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 +34 -0
index 287fcc4d234d..80eabff3e940 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
 
@@ -342,6 +347,34 @@ static int rsa_verify_key(struct image_sign_info *info,
        return 0;
 }
 
+#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.
@@ -395,18 +428,21 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
 
        return ret;
 }
+#endif
 
Quoted 11 lines
Quoted 9 lines
Quoted 9 lines

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

Quoted 9 lines
lib/rsa/rsa-verify.c +2 -1
 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)

Can you use if() instead of #if ?

Quoted 3 lines

This code is for local variable declaration, and so changing as you suggested can be difficult. Or do you accept moving this stuff further down to the next "#if CONFIG_IS_ENABLED(FIT_SIGNATURE)":

Yes if you use if() then you don't need to #ifdef the variables.

Quoted 26 lines

=== 8< ===

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)) {
        /*
         * declare variables here!
         */
        const void *blob = info->fdt_blob;
        int ndepth, noffset;
        int sig_node, node;
        char name[100];

        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 */
        ...
}
Quoted 5 lines

=== >8 ===

Otherwise, we will have to introduce another function, rsa_verify_with_fdt?, just for this purpose.

Regards,
Simon

#17 lore raw

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

Simon,

Quoted 142 lines · Simon Glass

Hi Takahiro,

Quoted 138 lines · AKASHI Takahiro
Quoted 135 lines · Simon Glass

Hi Takahiro,

Quoted 120 lines · AKASHI Takahiro

Simon,

Overall, do you agree to my approach here?

Quoted 27 lines · Simon Glass

Hi AKASHI,

Quoted 24 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     |  3 ++-
 lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 64 insertions(+), 9 deletions(-)
lib/rsa/Kconfig +0 -0
index 338c8124da59..3c1986a26f8c 100644
@@ -25,6 +25,13 @@ config RSA_VERIFY
        help
          Add RSA signature verification support.
 
Quoted 8 lines

lib/rsa/Kconfig config RSA_VERIFY · line 33

Quoted 6 lines
lib/rsa/Kconfig +6 -0
+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.

Please expand this, a lot. It is too brief.

Will add more description.

Quoted 76 lines
Quoted 76 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 +2 -1
index d66eef74c514..fd4592fd6a8a 100644
@@ -5,5 +5,6 @@
 # (C) Copyright 2000-2007
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
-obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o
+obj-$(CONFIG_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 +34 -0
index 287fcc4d234d..80eabff3e940 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
 
@@ -342,6 +347,34 @@ static int rsa_verify_key(struct image_sign_info *info,
        return 0;
 }
 
+#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.
@@ -395,18 +428,21 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
 
        return ret;
 }
+#endif
 
Quoted 9 lines
Quoted 9 lines

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

Quoted 9 lines
lib/rsa/rsa-verify.c +2 -1
 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)

Can you use if() instead of #if ?

Quoted 5 lines
Quoted 3 lines

This code is for local variable declaration, and so changing as you suggested can be difficult. Or do you accept moving this stuff further down to the next "#if CONFIG_IS_ENABLED(FIT_SIGNATURE)":

Yes if you use if() then you don't need to #ifdef the variables.

Okay, I will change the code as below.
-Takahiro Akashi

Quoted 34 lines
Quoted 26 lines

=== 8< ===

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)) {
        /*
         * declare variables here!
         */
        const void *blob = info->fdt_blob;
        int ndepth, noffset;
        int sig_node, node;
        char name[100];

        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 */
        ...
}
Quoted 5 lines

=== >8 ===

Otherwise, we will have to introduce another function, rsa_verify_with_fdt?, just for this purpose.

Regards,
Simon