Skip to contentSkip to conversation

rsa: Modify rsa lib to use hw acceleration

PATCH Superseded Ruchika Gupta 2 participants 28 messages Dec 23, 2014 – Dec 30, 2014 2 messages not in the archive

Subsystems (3)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover rsa: Modify rsa lib to use hw acceleration

The rsa-verify functionality is a two step operation involving:
1. Checksum (hash) Calculation over image regions
2. Public Key Modular exponentiation over signature to generate hash

The following patch set modifies the rsa library to use hw acceleration if available in platform.

The first two patches in the series, split the rsa-verify lib into two files:
1. rsa-verify.c
Does Verification

2. rsa-mod-exp.c
Does Modular Exponentiation

Driver Model is added for RSA Modular Exponentiation.

The patch set also has patches are related with hash lib support in RSA.

For hash, the infrastructure already exists in common/hash.c. rsa_checksum is modified to use the API's registered with the hash_algo structure. Once HW accelerated support for progressive hash is available, RSA library can easily pick it up.

Ruchika Gupta (9):
rsa: Split the rsa-verify to separate the modular exponentiation
FIT: Modify option FIT_SIGNATURE in Kconfig
DM: crypto/rsa: Add rsa Modular Exponentiation DM driver
configs: Move CONFIG_FIT_SIGNATURE to defconfig lib/rsa: Modify rsa to use DM driver if available
DM: crypto/fsl - Add Freescale rsa DM driver
lib/rsa: Add Kconfig option for HW accelerated RSA
hash: Add function to find hash_algo struct with progressive hash
rsa: Use checksum algorithms from struct hash_algo

Changes in v3:
Simon's comments incoprorated.
- Driver Model added for RSA Modular Exponentiation
- Other cosmetic changes like multiline comments etc incoporated
- CONFIG_FIT_SIGNATURE moved to defconfig file for the boards using it

I have tested it's compilation on sandbox platform. However, I don't have the sandbox board available with me to test it. The patches have been tested on freescale platform LS1020 with all the configs and tests available in test/vboot. The tests have been done with both RSA_HW as well as RSA_SW driver.

Changes in v2:
Kconfig option introduced

 Kconfig                            |   3 +-
 common/hash.c                      |  33 +++-
 common/image-sig.c                 |   6 +-
 configs/ids8313_defconfig          |   2 +
 configs/sandbox_defconfig          |   3 +
 configs/zynq_microzed_defconfig    |   3 +
 configs/zynq_zc70x_defconfig       |   3 +
 configs/zynq_zc770_xm010_defconfig |   3 +
 configs/zynq_zc770_xm012_defconfig |   3 +
 configs/zynq_zc770_xm013_defconfig |   3 +
 configs/zynq_zed_defconfig         |   3 +
 configs/zynq_zybo_defconfig        |   3 +
 drivers/crypto/Kconfig             |   3 +
 drivers/crypto/Makefile            |   1 +
 drivers/crypto/fsl/Kconfig         |   6 +
 drivers/crypto/fsl/Makefile        |   1 +
 drivers/crypto/fsl/fsl_rsa.c       |  62 +++++++
 drivers/crypto/fsl/jobdesc.c       |  28 +++
 drivers/crypto/fsl/jobdesc.h       |   5 +
 drivers/crypto/fsl/rsa_caam.h      |  27 +++
 drivers/crypto/rsa/Kconfig         |   5 +
 drivers/crypto/rsa/Makefile        |   8 +
 drivers/crypto/rsa/rsa_sw.c        |  39 ++++
 drivers/crypto/rsa/rsa_uclass.c    |  32 ++++
 include/configs/am335x_evm.h       |   5 +-
 include/configs/ids8313.h          |   3 -
 include/configs/sandbox.h          |   3 -
 include/configs/zynq-common.h      |   6 -
 include/dm/uclass-id.h             |   1 +
 include/hash.h                     |  15 ++
 include/image.h                    |   5 +-
 include/u-boot/rsa-checksum.h      |   7 +-
 include/u-boot/rsa-mod-exp.h       |  83 +++++++++
 lib/Kconfig                        |   2 +
 lib/rsa/Kconfig                    |  52 ++++++
 lib/rsa/Makefile                   |   1 +
 lib/rsa/rsa-checksum.c             |  53 +++++-
 lib/rsa/rsa-mod-exp.c              | 307 ++++++++++++++++++++++++++++++++
 lib/rsa/rsa-verify.c               | 354 +++++++++----------------------------
 tools/Makefile                     |   3 +-
 40 files changed, 872 insertions(+), 313 deletions(-)
 create mode 100644 drivers/crypto/fsl/Kconfig
 create mode 100644 drivers/crypto/fsl/fsl_rsa.c
 create mode 100644 drivers/crypto/fsl/rsa_caam.h
 create mode 100644 drivers/crypto/rsa/Kconfig
 create mode 100644 drivers/crypto/rsa/Makefile
 create mode 100644 drivers/crypto/rsa/rsa_sw.c
 create mode 100644 drivers/crypto/rsa/rsa_uclass.c
 create mode 100644 include/u-boot/rsa-mod-exp.h
 create mode 100644 lib/rsa/Kconfig
 create mode 100644 lib/rsa/rsa-mod-exp.c
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH v3 1/9 rsa: Split the rsa-verify to separate the modular exponentiation

Public exponentiation which is required in rsa verify functionality is tightly integrated with verification code in rsa_verify.c. The patch splits the file into twp separating the modular exponentiation.

1. rsa-verify.c
- The file parses device tree keys node to fill a keyprop structure. The keyprop structure can then be converted to implementation specific format.
(struct rsa_pub_key for sw implementation)
- The parsed device tree node is then passed to a generic rsa_mod_exp function.

2. rsa-mod-exp.c
Move the software specific functions related to modular exponentiation from rsa-verify.c to this file.

---
Changes in v3:
Kconfig moved to separate patch. This patch just splits the file now

Changes in v2:
Addressed few of Simon Glass's comments:
- Kconfig option added for RSA
- Comments added for new keyprop struct

 include/u-boot/rsa-mod-exp.h |  43 ++++++
 lib/rsa/Makefile             |   2 +-
 lib/rsa/rsa-mod-exp.c        | 307 ++++++++++++++++++++++++++++++++++++++++
 lib/rsa/rsa-verify.c         | 329 ++++++++-----------------------------------
 tools/Makefile               |   3 +-
 5 files changed, 408 insertions(+), 276 deletions(-)
 create mode 100644 include/u-boot/rsa-mod-exp.h
 create mode 100644 lib/rsa/rsa-mod-exp.c
include/u-boot/rsa-mod-exp.h new file +43 -0
new file mode 100644
index 0000000..59cd9ea
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2014, Ruchika Gupta.
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+*/
+
+#ifndef _RSA_MOD_EXP_H
+#define _RSA_MOD_EXP_H
+
+#include <errno.h>
+#include <image.h>
+
+/**
+ * struct key_prop - holder for a public key properties
+ *
+ * The struct has pointers to modulus (Typically called N),
+ * The inverse, R^2, exponent. These can be typecasted and
+ * used as byte arrays or converted to the required format
+ * as per requirement of RSA implementation.
+ */
+struct key_prop {
+	const void *rr;		/* R^2 can be treated as byte array */
+	const void *modulus;	/* modulus as byte array */
+	const void *public_exponent; /* public exponent as byte array */
+	uint32_t n0inv;		/* -1 / modulus[0] mod 2^32 */
+	int num_bits;		/* Key length in bits */
+	uint32_t exp_len;	/* Exponent length in number of uint8_t */
+};
+
+/**
+ * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw
+ *
+ * Operation: out[] = sig ^ exponent % modulus
+ *
+ * @sig:	RSA PKCS1.5 signature
+ * @sig_len:	Length of signature in number of bytes
+ * @node:	Node with RSA key elements like modulus, exponent, R^2, n0inv
+ * @out:	Result in form of byte array
+ */
+int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
+		struct key_prop *node, uint8_t *out);
+
+#endif
lib/rsa/Makefile +1 -1
index a5a96cb6..cc25b3c 100644
@@ -7,4 +7,4 @@
 # SPDX-License-Identifier:	GPL-2.0+
 #
 
-obj-$(CONFIG_FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o
+obj-$(CONFIG_FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o rsa-mod-exp.o
lib/rsa/rsa-mod-exp.c new file +307 -0

307 lines not on this page · raw message

lib/rsa/rsa-verify.c +55 -274

380 lines not on this page · raw message

tools/Makefile +2 -1
index a4216a1..0b981da 100644
@@ -60,7 +60,8 @@ FIT_SIG_OBJS-$(CONFIG_FIT_SIGNATURE) := common/image-sig.o
 LIBFDT_OBJS := $(addprefix lib/libfdt/, \
 			fdt.o fdt_ro.o fdt_rw.o fdt_strerror.o fdt_wip.o)
 RSA_OBJS-$(CONFIG_FIT_SIGNATURE) := $(addprefix lib/rsa/, \
-					rsa-sign.o rsa-verify.o rsa-checksum.o)
+					rsa-sign.o rsa-verify.o rsa-checksum.o \
+					rsa-mod-exp.o)
 
 # common objs for dumpimage and mkimage
 dumpimage-mkimage-objs := aisimage.o \
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH v3 1/9 rsa: Split the rsa-verify to separate the modular exponentiation

Hi Ruchika,

Quoted 27 lines · Ruchika Gupta

Public exponentiation which is required in rsa verify functionality is tightly integrated with verification code in rsa_verify.c. The patch splits the file into twp separating the modular exponentiation.

1. rsa-verify.c
- The file parses device tree keys node to fill a keyprop structure. The keyprop structure can then be converted to implementation specific format.
(struct rsa_pub_key for sw implementation)
- The parsed device tree node is then passed to a generic rsa_mod_exp function.

2. rsa-mod-exp.c
Move the software specific functions related to modular exponentiation from rsa-verify.c to this file.

---
Changes in v3:
Kconfig moved to separate patch. This patch just splits the file now

Changes in v2:
Addressed few of Simon Glass's comments:
- Kconfig option added for RSA
- Comments added for new keyprop struct

Quoted 8 lines
 include/u-boot/rsa-mod-exp.h |  43 ++++++
 lib/rsa/Makefile             |   2 +-
 lib/rsa/rsa-mod-exp.c        | 307 ++++++++++++++++++++++++++++++++++++++++
 lib/rsa/rsa-verify.c         | 329 ++++++++-----------------------------------
 tools/Makefile               |   3 +-
 5 files changed, 408 insertions(+), 276 deletions(-)
 create mode 100644 include/u-boot/rsa-mod-exp.h
 create mode 100644 lib/rsa/rsa-mod-exp.c
#3 lore raw

PATCH v3 2/9 FIT: Modify option FIT_SIGNATURE in Kconfig

In reply to Ruchika Gupta

For FIT signature based approach to work, RSA library needs to be selected. The FIT_SIGNATURE option in Kconfig is modified to automatically select RSA. Selecting RSA compiles the RSA library required for image verification.

---
Changes in v3:
New patch created for adding Kconfig option for FIT signature

 Kconfig     | 3 ++-
 lib/Kconfig | 6 ++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
Kconfig +2 -1
index 153ee2b..c2d7cb9 100644
@@ -116,8 +116,9 @@ config FIT_VERBOSE
 	depends on FIT
 
 config FIT_SIGNATURE
-	bool "Enabel signature verification of FIT uImages"
+	bool "Enable signature verification of FIT uImages"
 	depends on FIT
+	select RSA
 	help
 	  This option enables signature verification of FIT uImages,
 	  using a hash signed and verified using RSA.
lib/Kconfig +6 -0
index 8460439..602dd37 100644
@@ -27,4 +27,10 @@ config SYS_HZ
 	  get_timer() must operate in milliseconds and this option must be
 	  set to 1000.
 
+config RSA
+	bool "Use RSA Library"
+	help
+	  RSA support.This enables the RSA algorithm used for FIT image
+	  verification in U-Boot.
+
 endmenu
Signature · 1 line
1.8.1.4
#4 lore raw

PATCH v3 2/9 FIT: Modify option FIT_SIGNATURE in Kconfig

Hi Ruchika,

Quoted 5 lines · Ruchika Gupta

For FIT signature based approach to work, RSA library needs to be selected. The FIT_SIGNATURE option in Kconfig is modified to automatically select RSA. Selecting RSA compiles the RSA library required for image verification.

One nit below.

Quoted 28 lines

---
Changes in v3:
New patch created for adding Kconfig option for FIT signature

 Kconfig     | 3 ++-
 lib/Kconfig | 6 ++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
Kconfig +2 -1
index 153ee2b..c2d7cb9 100644
@@ -116,8 +116,9 @@ config FIT_VERBOSE
        depends on FIT
 
 config FIT_SIGNATURE
-       bool "Enabel signature verification of FIT uImages"
+       bool "Enable signature verification of FIT uImages"
        depends on FIT
+       select RSA
        help
          This option enables signature verification of FIT uImages,
          using a hash signed and verified using RSA.
lib/Kconfig +0 -0
index 8460439..602dd37 100644

lib/Kconfig config SYS_HZ · line 34

Quoted 8 lines
lib/Kconfig +5 -0
@@ -27,4 +27,10 @@ config SYS_HZ
          get_timer() must operate in milliseconds and this option must be
          set to 1000.
 
+config RSA
+       bool "Use RSA Library"
+       help
+         RSA support.This enables the RSA algorithm used for FIT image
+         verification in U-Boot.

Please expand this a bit - you can point to the documentation for example.

Regards,
Simon

#5 lore raw

PATCH v3 3/9 DM: crypto/rsa: Add rsa Modular Exponentiation DM driver

In reply to Ruchika Gupta

Add a new rsa uclass for performing modular exponentiation and implement the software driver basing on this uclass.

---
Changes in v3:
New patch with driver model for RSA UCLASS

 drivers/crypto/Kconfig          |  1 +
 drivers/crypto/Makefile         |  1 +
 drivers/crypto/rsa/Kconfig      |  5 +++++
 drivers/crypto/rsa/Makefile     |  8 ++++++++
 drivers/crypto/rsa/rsa_sw.c     | 39 +++++++++++++++++++++++++++++++++++++++
 drivers/crypto/rsa/rsa_uclass.c | 31 +++++++++++++++++++++++++++++++
 include/dm/uclass-id.h          |  1 +
 include/u-boot/rsa-mod-exp.h    | 40 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 126 insertions(+)
 create mode 100644 drivers/crypto/rsa/Kconfig
 create mode 100644 drivers/crypto/rsa/Makefile
 create mode 100644 drivers/crypto/rsa/rsa_sw.c
 create mode 100644 drivers/crypto/rsa/rsa_uclass.c
drivers/crypto/Kconfig +1 -0
index e69de29..75f3479 100644
@@ -0,0 +1 @@
+source drivers/crypto/rsa/Kconfig
drivers/crypto/Makefile +1 -0
index 7b79237..a2f30fc 100644
@@ -6,4 +6,5 @@
 #
 
 obj-$(CONFIG_EXYNOS_ACE_SHA)	+= ace_sha.o
+obj-y += rsa/
 obj-y += fsl/
drivers/crypto/rsa/Kconfig new file +5 -0
new file mode 100644
index 0000000..7eb90a1
@@ -0,0 +1,5 @@
+config DM_RSA
+	bool "Enable Driver Model for RSA "
+	depends on DM
+	help
+	  If you want to use driver model for RSA Modular Exponentiation, say Y.
drivers/crypto/rsa/Makefile new file +8 -0
new file mode 100644
index 0000000..fae4f8c
@@ -0,0 +1,8 @@
+#
+# (C) Copyright 2014 Freescale Semiconductor, Inc.
+#
+# SPDX-License-Identifier:	GPL-2.0+
+#
+
+obj-$(CONFIG_DM_RSA) += rsa_uclass.o
+obj-$(CONFIG_RSA_SW) += rsa_sw.o
drivers/crypto/rsa/rsa_sw.c new file +39 -0
new file mode 100644
index 0000000..5d94754
@@ -0,0 +1,39 @@
+/*
+ * (C) Copyright 2014 Freescale Semiconductor, Inc.
+ * Author: Ruchika Gupta <[email protected]>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <config.h>
+#include <common.h>
+#include <dm.h>
+#include <u-boot/rsa-mod-exp.h>
+
+int mod_exp_sw(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
+		struct key_prop *prop, uint8_t *out)
+{
+	int ret = 0;
+
+	ret = rsa_mod_exp_sw(sig, sig_len, prop, out);
+
+	if (ret) {
+		debug("%s: RSA failed to verify: %d\n", __func__, ret);
+		return ret;
+	}
+	return 0;
+}
+
+static const struct rsa_ops rsa_ops_sw = {
+	.get_mod_exp	= mod_exp_sw,
+};
+
+U_BOOT_DRIVER(fsl_rsa) = {
+	.name	= "rsa_sw",
+	.id	= UCLASS_RSA,
+	.ops	= &rsa_ops_sw,
+};
+
+U_BOOT_DEVICE(rsa_sw) = {
+	.name = "rsa_sw",
+};
drivers/crypto/rsa/rsa_uclass.c new file +31 -0
new file mode 100644
index 0000000..f4f4f39
@@ -0,0 +1,31 @@
+/*
+ * (C) Copyright 2014 Freescale Semiconductor, Inc
+ * Author: Ruchika Gupta <[email protected]>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <u-boot/rsa-mod-exp.h>
+#include <errno.h>
+#include <fdtdec.h>
+#include <malloc.h>
+#include <asm/io.h>
+#include <linux/list.h>
+
+int rsa_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
+		struct key_prop *node, uint8_t *out)
+{
+	const struct rsa_ops *ops = device_get_ops(dev);
+
+	if (!ops->get_mod_exp)
+		return -ENOSYS;
+
+	return ops->get_mod_exp(dev, sig, sig_len, node, out);
+}
+
+UCLASS_DRIVER(rsa) = {
+	.id		= UCLASS_RSA,
+	.name		= "rsa",
+};
include/dm/uclass-id.h +1 -0
index f17c3c2..659369e 100644
@@ -33,6 +33,7 @@ enum uclass_id {
 	UCLASS_I2C,		/* I2C bus */
 	UCLASS_I2C_GENERIC,	/* Generic I2C device */
 	UCLASS_I2C_EEPROM,	/* I2C EEPROM device */
+	UCLASS_RSA	,	/* RSA Mod Exp device */
 
 	UCLASS_COUNT,
 	UCLASS_INVALID = -1,
include/u-boot/rsa-mod-exp.h +40 -0
index 59cd9ea..7f7e196 100644
@@ -40,4 +40,44 @@ struct key_prop {
 int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
 		struct key_prop *node, uint8_t *out);
 
+/**
+ * rsa_mod_exp - Perform RSA Modular Exponentiation
+ *
+ * Operation: out[] = sig ^ exponent % modulus
+ *
+ * @udev:	RSA Device
+ * @sig:	RSA PKCS1.5 signature
+ * @sig_len:	Length of signature in number of bytes
+ * @node:	Node with RSA key elements like modulus, exponent, R^2, n0inv
+ * @out:	Result in form of byte array
+ */
+int rsa_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
+		struct key_prop *node, uint8_t *out);
+
+/**
+ * struct struct rsa_ops - Driver model for RSA operations
+ *
+ * The uclass interface is implemented by all crypto devices which use
+ * driver model.
+ */
+struct rsa_ops {
+	/**
+	 * Perform Modular Exponentiation
+	 *
+	 * Operation: out[] = sig ^ exponent % modulus
+	 *
+	 * @dev:	RSA Device
+	 * @sig:	RSA PKCS1.5 signature
+	 * @sig_len:	Length of signature in number of bytes
+	 * @node:	Node with RSA key elements like modulus, exponent,
+	 *		R^2, n0inv
+	 * @out:	Result in form of byte array
+	 * Returns: 0 if exponentiation is succesful, or a negative value
+	 * if it wasn't.
+	 */
+	int (*get_mod_exp)(struct udevice *dev, const uint8_t *sig,
+			   uint32_t sig_len, struct key_prop *node,
+			   uint8_t *out);
+};
+
 #endif
Signature · 1 line
1.8.1.4
#6 lore raw

PATCH v3 3/9 DM: crypto/rsa: Add rsa Modular Exponentiation DM driver

Hi Ruchika,

Quoted 45 lines · Ruchika Gupta

Add a new rsa uclass for performing modular exponentiation and implement the software driver basing on this uclass.

---
Changes in v3:
New patch with driver model for RSA UCLASS

 drivers/crypto/Kconfig          |  1 +
 drivers/crypto/Makefile         |  1 +
 drivers/crypto/rsa/Kconfig      |  5 +++++
 drivers/crypto/rsa/Makefile     |  8 ++++++++
 drivers/crypto/rsa/rsa_sw.c     | 39 +++++++++++++++++++++++++++++++++++++++
 drivers/crypto/rsa/rsa_uclass.c | 31 +++++++++++++++++++++++++++++++
 include/dm/uclass-id.h          |  1 +
 include/u-boot/rsa-mod-exp.h    | 40 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 126 insertions(+)
 create mode 100644 drivers/crypto/rsa/Kconfig
 create mode 100644 drivers/crypto/rsa/Makefile
 create mode 100644 drivers/crypto/rsa/rsa_sw.c
 create mode 100644 drivers/crypto/rsa/rsa_uclass.c
drivers/crypto/Kconfig +1 -0
index e69de29..75f3479 100644
@@ -0,0 +1 @@
+source drivers/crypto/rsa/Kconfig
drivers/crypto/Makefile +1 -0
index 7b79237..a2f30fc 100644
@@ -6,4 +6,5 @@
 #
 
 obj-$(CONFIG_EXYNOS_ACE_SHA)   += ace_sha.o
+obj-y += rsa/
 obj-y += fsl/
drivers/crypto/rsa/Kconfig new file +0 -0
new file mode 100644
index 0000000..7eb90a1

drivers/crypto/rsa/Kconfig line 5

Quoted 5 lines
drivers/crypto/rsa/Kconfig new file +5 -0
@@ -0,0 +1,5 @@
+config DM_RSA
+       bool "Enable Driver Model for RSA "
+       depends on DM
+       help
+         If you want to use driver model for RSA Modular Exponentiation, say Y.

Can you send a new patch (later if you prefer) which removes this option altogether? It should be the default. In other words, RSA should always use driver model.

Quoted 101 lines
drivers/crypto/rsa/Makefile new file +8 -0
new file mode 100644
index 0000000..fae4f8c
@@ -0,0 +1,8 @@
+#
+# (C) Copyright 2014 Freescale Semiconductor, Inc.
+#
+# SPDX-License-Identifier:     GPL-2.0+
+#
+
+obj-$(CONFIG_DM_RSA) += rsa_uclass.o
+obj-$(CONFIG_RSA_SW) += rsa_sw.o
drivers/crypto/rsa/rsa_sw.c new file +39 -0
new file mode 100644
index 0000000..5d94754
@@ -0,0 +1,39 @@
+/*
+ * (C) Copyright 2014 Freescale Semiconductor, Inc.
+ * Author: Ruchika Gupta <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <config.h>
+#include <common.h>
+#include <dm.h>
+#include <u-boot/rsa-mod-exp.h>
+
+int mod_exp_sw(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
+               struct key_prop *prop, uint8_t *out)
+{
+       int ret = 0;
+
+       ret = rsa_mod_exp_sw(sig, sig_len, prop, out);
+
+       if (ret) {
+               debug("%s: RSA failed to verify: %d\n", __func__, ret);
+               return ret;
+       }
+       return 0;
+}
+
+static const struct rsa_ops rsa_ops_sw = {
+       .get_mod_exp    = mod_exp_sw,
+};
+
+U_BOOT_DRIVER(fsl_rsa) = {
+       .name   = "rsa_sw",
+       .id     = UCLASS_RSA,
+       .ops    = &rsa_ops_sw,
+};
+
+U_BOOT_DEVICE(rsa_sw) = {
+       .name = "rsa_sw",
+};
drivers/crypto/rsa/rsa_uclass.c new file +31 -0
new file mode 100644
index 0000000..f4f4f39
@@ -0,0 +1,31 @@
+/*
+ * (C) Copyright 2014 Freescale Semiconductor, Inc
+ * Author: Ruchika Gupta <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <u-boot/rsa-mod-exp.h>
+#include <errno.h>
+#include <fdtdec.h>
+#include <malloc.h>
+#include <asm/io.h>
+#include <linux/list.h>
+
+int rsa_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
+               struct key_prop *node, uint8_t *out)
+{
+       const struct rsa_ops *ops = device_get_ops(dev);
+
+       if (!ops->get_mod_exp)
+               return -ENOSYS;
+
+       return ops->get_mod_exp(dev, sig, sig_len, node, out);
+}
+
+UCLASS_DRIVER(rsa) = {
+       .id             = UCLASS_RSA,
+       .name           = "rsa",
+};
include/dm/uclass-id.h +0 -0
index f17c3c2..659369e 100644

include/dm/uclass-id.h enum uclass_id { · line 36

Quoted 4 lines
include/dm/uclass-id.h +1 -0
@@ -33,6 +33,7 @@ enum uclass_id {
        UCLASS_I2C,             /* I2C bus */
        UCLASS_I2C_GENERIC,     /* Generic I2C device */
        UCLASS_I2C_EEPROM,      /* I2C EEPROM device */
+       UCLASS_RSA      ,       /* RSA Mod Exp device */

Funny spacing here.

Quoted 11 lines
include/dm/uclass-id.h +0 -0
        UCLASS_COUNT,
        UCLASS_INVALID = -1,
include/u-boot/rsa-mod-exp.h +0 -0
index 59cd9ea..7f7e196 100644
@@ -40,4 +40,44 @@ struct key_prop {
 int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
                struct key_prop *node, uint8_t *out);
 

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

Quoted 10 lines
include/u-boot/rsa-mod-exp.h +10 -0
+/**
+ * rsa_mod_exp - Perform RSA Modular Exponentiation
+ *
+ * Operation: out[] = sig ^ exponent % modulus
+ *
+ * @udev:      RSA Device
+ * @sig:       RSA PKCS1.5 signature
+ * @sig_len:   Length of signature in number of bytes
+ * @node:      Node with RSA key elements like modulus, exponent, R^2, n0inv
+ * @out:       Result in form of byte array

How big is this array?

Quoted 12 lines
include/u-boot/rsa-mod-exp.h +12 -0
+ */
+int rsa_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
+               struct key_prop *node, uint8_t *out);
+
+/**
+ * struct struct rsa_ops - Driver model for RSA operations
+ *
+ * The uclass interface is implemented by all crypto devices which use
+ * driver model.
+ */
+struct rsa_ops {
+       /**

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

Quoted 10 lines
include/u-boot/rsa-mod-exp.h +10 -0
+        * Perform Modular Exponentiation
+        *
+        * Operation: out[] = sig ^ exponent % modulus
+        *
+        * @dev:        RSA Device
+        * @sig:        RSA PKCS1.5 signature
+        * @sig_len:    Length of signature in number of bytes
+        * @node:       Node with RSA key elements like modulus, exponent,
+        *              R^2, n0inv
+        * @out:        Result in form of byte array

How big is this array?

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

Quoted 1 line
include/u-boot/rsa-mod-exp.h +1 -0
+        * Returns: 0 if exponentiation is succesful, or a negative value

successful

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

Quoted 3 lines
include/u-boot/rsa-mod-exp.h +3 -0
+        * if it wasn't.
+        */
+       int (*get_mod_exp)(struct udevice *dev, const uint8_t *sig,

mod_exp() is better I think, since it matches your function above.

Quoted 8 lines
include/u-boot/rsa-mod-exp.h +4 -1
+                          uint32_t sig_len, struct key_prop *node,
+                          uint8_t *out);
+};
+
 #endif
--

1.8.1.4

Regards,
Simon

#7 lore raw

PATCH v3 4/9 configs: Move CONFIG_FIT_SIGNATURE to defconfig

In reply to Ruchika Gupta

For the platforms which use,CONFIG_FIT_SIGNATURE, the required configs are moved to the platform's defconfig file. Selecting CONFIG_FIT_SIGNATURE using defconfig automatically resolves the dependencies for signature verification. The RSA library gets automatically selected and user does not have to define CONFIG_RSA manually.

---
Changes in v3:
New patch

 configs/ids8313_defconfig          | 2 ++
 configs/sandbox_defconfig          | 3 +++
 configs/zynq_microzed_defconfig    | 3 +++
 configs/zynq_zc70x_defconfig       | 3 +++
 configs/zynq_zc770_xm010_defconfig | 3 +++
 configs/zynq_zc770_xm012_defconfig | 3 +++
 configs/zynq_zc770_xm013_defconfig | 3 +++
 configs/zynq_zed_defconfig         | 3 +++
 configs/zynq_zybo_defconfig        | 3 +++
 include/configs/am335x_evm.h       | 4 ++--
 include/configs/ids8313.h          | 3 ---
 include/configs/sandbox.h          | 3 ---
 include/configs/zynq-common.h      | 6 ------
 13 files changed, 28 insertions(+), 14 deletions(-)
configs/ids8313_defconfig +2 -0
index 1c665aa..8479cd4 100644
@@ -1,4 +1,6 @@
 CONFIG_SYS_EXTRA_OPTIONS="SYS_TEXT_BASE=0xFFF00000"
 CONFIG_PPC=y
 CONFIG_MPC83xx=y
+CONFIG_FIT=y
+CONFIG_FIT_SIGNATURE=y
 CONFIG_TARGET_IDS8313=y
configs/sandbox_defconfig +3 -0
index 47d8400..0111f25 100644
@@ -1,3 +1,6 @@
 CONFIG_OF_CONTROL=y
 CONFIG_OF_HOSTFILE=y
+CONFIG_FIT=y
+CONFIG_FIT_VERBOSE=y
+CONFIG_FIT_SIGNATURE=y
 CONFIG_DEFAULT_DEVICE_TREE="sandbox"
configs/zynq_microzed_defconfig +3 -0
index 9588849..b9a6fe5 100644
@@ -3,4 +3,7 @@ CONFIG_SPL=y
 +S:CONFIG_ZYNQ=y
 +S:CONFIG_TARGET_ZYNQ_MICROZED=y
 CONFIG_OF_CONTROL=y
+CONFIG_FIT=y
+CONFIG_FIT_VERBOSE=y
+CONFIG_FIT_SIGNATURE=y
 CONFIG_DEFAULT_DEVICE_TREE="zynq-microzed"
configs/zynq_zc70x_defconfig +3 -0
index cf50730..dc8aa84 100644
@@ -4,3 +4,6 @@ CONFIG_SPL=y
 +S:CONFIG_TARGET_ZYNQ_ZC70X=y
 CONFIG_OF_CONTROL=y
 CONFIG_DEFAULT_DEVICE_TREE="zynq-zc702"
+CONFIG_FIT=y
+CONFIG_FIT_VERBOSE=y
+CONFIG_FIT_SIGNATURE=y
configs/zynq_zc770_xm010_defconfig +3 -0
index 8bb405d..2f5fa8c 100644
@@ -5,3 +5,6 @@ CONFIG_SYS_EXTRA_OPTIONS="ZC770_XM010"
 +S:CONFIG_TARGET_ZYNQ_ZC770=y
 CONFIG_OF_CONTROL=y
 CONFIG_DEFAULT_DEVICE_TREE="zynq-zc770-xm010"
+CONFIG_FIT=y
+CONFIG_FIT_VERBOSE=y
+CONFIG_FIT_SIGNATURE=y
configs/zynq_zc770_xm012_defconfig +3 -0
index 0ba5da5..a92d495 100644
@@ -5,3 +5,6 @@ CONFIG_SYS_EXTRA_OPTIONS="ZC770_XM012"
 +S:CONFIG_TARGET_ZYNQ_ZC770=y
 CONFIG_OF_CONTROL=y
 CONFIG_DEFAULT_DEVICE_TREE="zynq-zc770-xm012"
+CONFIG_FIT=y
+CONFIG_FIT_VERBOSE=y
+CONFIG_FIT_SIGNATURE=y
configs/zynq_zc770_xm013_defconfig +3 -0
index 13f8112..3a02f75 100644
@@ -5,3 +5,6 @@ CONFIG_SYS_EXTRA_OPTIONS="ZC770_XM013"
 +S:CONFIG_TARGET_ZYNQ_ZC770=y
 CONFIG_OF_CONTROL=y
 CONFIG_DEFAULT_DEVICE_TREE="zynq-zc770-xm013"
+CONFIG_FIT=y
+CONFIG_FIT_VERBOSE=y
+CONFIG_FIT_SIGNATURE=y
configs/zynq_zed_defconfig +3 -0
index eb057fa..1d816f6 100644
@@ -4,3 +4,6 @@ CONFIG_SPL=y
 +S:CONFIG_TARGET_ZYNQ_ZED=y
 CONFIG_OF_CONTROL=y
 CONFIG_DEFAULT_DEVICE_TREE="zynq-zed"
+CONFIG_FIT=y
+CONFIG_FIT_VERBOSE=y
+CONFIG_FIT_SIGNATURE=y
configs/zynq_zybo_defconfig +3 -0
index 12311cd..9183629 100644
@@ -4,3 +4,6 @@ CONFIG_SPL=y
 +S:CONFIG_TARGET_ZYNQ_ZYBO=y
 CONFIG_OF_CONTROL=y
 CONFIG_DEFAULT_DEVICE_TREE="zynq-zybo"
+CONFIG_FIT=y
+CONFIG_FIT_VERBOSE=y
+CONFIG_FIT_SIGNATURE=y
include/configs/am335x_evm.h +2 -2
index 560e3bf..cc36985 100644
@@ -23,8 +23,8 @@
 # define CONFIG_TIMESTAMP
 # define CONFIG_LZO
 # ifdef CONFIG_ENABLE_VBOOT
-#  define CONFIG_FIT_SIGNATURE
-#  define CONFIG_RSA
+# define CONFIG_FIT_SIGNATURE
+# define CONFIG_RSA
 # endif
 #endif
 
include/configs/ids8313.h +0 -3
index f084834..2384864 100644
@@ -575,12 +575,9 @@
 
 #define CONFIG_VERSION_VARIABLE
 
-#define CONFIG_FIT
-#define CONFIG_FIT_SIGNATURE
 #define CONFIG_IMAGE_FORMAT_LEGACY
 #define CONFIG_CMD_FDT
 #define CONFIG_CMD_HASH
-#define CONFIG_RSA
 #define CONFIG_SHA1
 #define CONFIG_SHA256
 
include/configs/sandbox.h +0 -3
index 657f751..6fd29b9 100644
@@ -41,9 +41,6 @@
 
 #define CONFIG_OF_LIBFDT
 #define CONFIG_LMB
-#define CONFIG_FIT
-#define CONFIG_FIT_SIGNATURE
-#define CONFIG_RSA
 #define CONFIG_CMD_FDT
 #define CONFIG_ANDROID_BOOT_IMAGE
 
include/configs/zynq-common.h +0 -6
index 87b4fff..3894517 100644
@@ -219,17 +219,11 @@
 #define CONFIG_OF_LIBFDT
 
 /* FIT support */
-#define CONFIG_FIT
-#define CONFIG_FIT_VERBOSE	1 /* enable fit_format_{error,warning}() */
 #define CONFIG_IMAGE_FORMAT_LEGACY /* enable also legacy image format */
 
 /* FDT support */
 #define CONFIG_DISPLAY_BOARDINFO_LATE
 
-/* RSA support */
-#define CONFIG_FIT_SIGNATURE
-#define CONFIG_RSA
-
 /* Extend size of kernel image for uncompression */
 #define CONFIG_SYS_BOOTM_LEN	(60 * 1024 * 1024)
 
Signature · 1 line
1.8.1.4
#8 lore raw

PATCH v3 4/9 configs: Move CONFIG_FIT_SIGNATURE to defconfig

Quoted 8 lines · Ruchika Gupta

For the platforms which use,CONFIG_FIT_SIGNATURE, the required configs are moved to the platform's defconfig file. Selecting CONFIG_FIT_SIGNATURE using defconfig automatically resolves the dependencies for signature verification. The RSA library gets automatically selected and user does not have to define CONFIG_RSA manually.

Quoted 18 lines

---
Changes in v3:
New patch

 configs/ids8313_defconfig          | 2 ++
 configs/sandbox_defconfig          | 3 +++
 configs/zynq_microzed_defconfig    | 3 +++
 configs/zynq_zc70x_defconfig       | 3 +++
 configs/zynq_zc770_xm010_defconfig | 3 +++
 configs/zynq_zc770_xm012_defconfig | 3 +++
 configs/zynq_zc770_xm013_defconfig | 3 +++
 configs/zynq_zed_defconfig         | 3 +++
 configs/zynq_zybo_defconfig        | 3 +++
 include/configs/am335x_evm.h       | 4 ++--
 include/configs/ids8313.h          | 3 ---
 include/configs/sandbox.h          | 3 ---
 include/configs/zynq-common.h      | 6 ------
 13 files changed, 28 insertions(+), 14 deletions(-)
#9 lore raw

PATCH v3 5/9 lib/rsa: Modify rsa to use DM driver if available

In reply to Ruchika Gupta

Modify rsa_verify to use the rsa driver of DM library available.The tools and the configurations which don't use Driver Model, will continue to use the same RSA sw library. The software implementation of RSA Modular Exponentation is now compiled if RSA_MOD_EXP_SW is selected.

Kconfig options are also added for rsa library.

---
Changes in v3:
New patch

 include/configs/am335x_evm.h |  1 +
 lib/Kconfig                  |  6 +-----
 lib/rsa/Kconfig              | 31 +++++++++++++++++++++++++++++++
 lib/rsa/Makefile             |  3 ++-
 lib/rsa/rsa-verify.c         | 18 ++++++++++++++++++
 5 files changed, 53 insertions(+), 6 deletions(-)
 create mode 100644 lib/rsa/Kconfig
include/configs/am335x_evm.h +1 -0
index cc36985..aa79841 100644
@@ -25,6 +25,7 @@
 # ifdef CONFIG_ENABLE_VBOOT
 # define CONFIG_FIT_SIGNATURE
 # define CONFIG_RSA
+# define CONFIG_RSA_MOD_EXP_SW
 # endif
 #endif
 
lib/Kconfig +1 -5
index 602dd37..a1f30a2 100644
@@ -27,10 +27,6 @@ config SYS_HZ
 	  get_timer() must operate in milliseconds and this option must be
 	  set to 1000.
 
-config RSA
-	bool "Use RSA Library"
-	help
-	  RSA support.This enables the RSA algorithm used for FIT image
-	  verification in U-Boot.
+source lib/rsa/Kconfig
 
 endmenu
lib/rsa/Kconfig new file +31 -0
new file mode 100644
index 0000000..8f9aa44
@@ -0,0 +1,31 @@
+config RSA
+	bool "Use RSA Library"
+	select RSA_MOD_EXP_SW if !DM
+	select DM_RSA if DM
+	help
+	  RSA support.This enables the RSA algorithm used for FIT image
+	  verification in U-Boot.
+	  See doc/uImage.FIT/signature.txt for more details.
+
+if RSA && DM_RSA
+
+config RSA_SW
+	bool "User driver Model for RSA Modular Exponentiation in software"
+	depends on DM && DM_RSA && RSA
+	select RSA_MOD_EXP_SW
+	default y
+	help
+	  Enables driver for modular exponentiation in software. This is a RSA
+	  algorithm used in FIT image verification. It required RSA Key as
+	  input.
+	  See doc/uImage.FIT/signature.txt for more details.
+
+endif
+
+config RSA_MOD_EXP_SW
+	bool
+	default n
+	help
+	  Library for SW implementation of RSA Modular Exponentiation. This
+	  library is used by the mkimage tool(not selected through this option)
+	  as well as by the RSA driver model with SW implementation.
lib/rsa/Makefile +2 -1
index cc25b3c..ccc6060 100644
@@ -7,4 +7,5 @@
 # SPDX-License-Identifier:	GPL-2.0+
 #
 
-obj-$(CONFIG_FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o rsa-mod-exp.o
+obj-$(CONFIG_FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o
+obj-$(CONFIG_RSA_MOD_EXP_SW) += rsa-mod-exp.o
lib/rsa/rsa-verify.c +18 -0
index f8bc086..27f10ef 100644
@@ -12,6 +12,7 @@
 #include <asm/errno.h>
 #include <asm/types.h>
 #include <asm/unaligned.h>
+#include <dm.h>
 #else
 #include "fdt_host.h"
 #include "mkimage.h"
@@ -43,6 +44,9 @@ static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
 	const uint8_t *padding;
 	int pad_len;
 	int ret;
+#if defined(CONFIG_DM_RSA) && !defined(USE_HOSTCC)
+	struct udevice *rsa_dev;
+#endif
 
 	if (!prop || !sig || !hash || !algo)
 		return -EIO;
@@ -63,11 +67,25 @@ static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
 
 	uint8_t buf[sig_len];
 
+#if defined(CONFIG_DM_RSA) && !defined(USE_HOSTCC)
+	ret = uclass_get_device(UCLASS_RSA, 0, &rsa_dev);
+	if (!ret) {
+		ret = rsa_mod_exp(rsa_dev, sig, sig_len, prop, buf);
+		if (ret) {
+			debug("Error in Modular exponentation\n");
+			return ret;
+		}
+	} else {
+		printf("RSA: Can't find Mod Exp implemnetation\n");
+		return -EINVAL;
+	}
+#else
 	ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
 	if (ret) {
 		debug("Error in Modular exponentation\n");
 		return ret;
 	}
+#endif
 
 	padding = algo->rsa_padding;
 	pad_len = algo->pad_len - algo->checksum_len;
Signature · 1 line
1.8.1.4
#10 lore raw

PATCH v3 5/9 lib/rsa: Modify rsa to use DM driver if available

Hi Ruchika,

Quoted 26 lines · Ruchika Gupta

Modify rsa_verify to use the rsa driver of DM library available.The tools and the configurations which don't use Driver Model, will continue to use the same RSA sw library. The software implementation of RSA Modular Exponentation is now compiled if RSA_MOD_EXP_SW is selected.

Kconfig options are also added for rsa library.

---
Changes in v3:
New patch

 include/configs/am335x_evm.h |  1 +
 lib/Kconfig                  |  6 +-----
 lib/rsa/Kconfig              | 31 +++++++++++++++++++++++++++++++
 lib/rsa/Makefile             |  3 ++-
 lib/rsa/rsa-verify.c         | 18 ++++++++++++++++++
 5 files changed, 53 insertions(+), 6 deletions(-)
 create mode 100644 lib/rsa/Kconfig
include/configs/am335x_evm.h +0 -0
index cc36985..aa79841 100644

include/configs/am335x_evm.h line 28

Quoted 4 lines
include/configs/am335x_evm.h +1 -0
@@ -25,6 +25,7 @@
 # ifdef CONFIG_ENABLE_VBOOT
 # define CONFIG_FIT_SIGNATURE
 # define CONFIG_RSA
+# define CONFIG_RSA_MOD_EXP_SW

This should go in am335x_boneblack_vboot_defconfig I think.

Quoted 102 lines
include/configs/am335x_evm.h +0 -0
 # endif
 #endif
 
lib/Kconfig +1 -5
index 602dd37..a1f30a2 100644
@@ -27,10 +27,6 @@ config SYS_HZ
          get_timer() must operate in milliseconds and this option must be
          set to 1000.
 
-config RSA
-       bool "Use RSA Library"
-       help
-         RSA support.This enables the RSA algorithm used for FIT image
-         verification in U-Boot.
+source lib/rsa/Kconfig
 
 endmenu
lib/rsa/Kconfig new file +31 -0
new file mode 100644
index 0000000..8f9aa44
@@ -0,0 +1,31 @@
+config RSA
+       bool "Use RSA Library"
+       select RSA_MOD_EXP_SW if !DM
+       select DM_RSA if DM
+       help
+         RSA support.This enables the RSA algorithm used for FIT image
+         verification in U-Boot.
+         See doc/uImage.FIT/signature.txt for more details.
+
+if RSA && DM_RSA
+
+config RSA_SW
+       bool "User driver Model for RSA Modular Exponentiation in software"
+       depends on DM && DM_RSA && RSA
+       select RSA_MOD_EXP_SW
+       default y
+       help
+         Enables driver for modular exponentiation in software. This is a RSA
+         algorithm used in FIT image verification. It required RSA Key as
+         input.
+         See doc/uImage.FIT/signature.txt for more details.
+
+endif
+
+config RSA_MOD_EXP_SW
+       bool
+       default n
+       help
+         Library for SW implementation of RSA Modular Exponentiation. This
+         library is used by the mkimage tool(not selected through this option)
+         as well as by the RSA driver model with SW implementation.
lib/rsa/Makefile +2 -1
index cc25b3c..ccc6060 100644
@@ -7,4 +7,5 @@
 # SPDX-License-Identifier:     GPL-2.0+
 #
 
-obj-$(CONFIG_FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o rsa-mod-exp.o
+obj-$(CONFIG_FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o
+obj-$(CONFIG_RSA_MOD_EXP_SW) += rsa-mod-exp.o
lib/rsa/rsa-verify.c +13 -0
index f8bc086..27f10ef 100644
@@ -12,6 +12,7 @@
 #include <asm/errno.h>
 #include <asm/types.h>
 #include <asm/unaligned.h>
+#include <dm.h>
 #else
 #include "fdt_host.h"
 #include "mkimage.h"
@@ -43,6 +44,9 @@ static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
        const uint8_t *padding;
        int pad_len;
        int ret;
+#if defined(CONFIG_DM_RSA) && !defined(USE_HOSTCC)
+       struct udevice *rsa_dev;
+#endif
 
        if (!prop || !sig || !hash || !algo)
                return -EIO;
@@ -63,11 +67,25 @@ static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
 
        uint8_t buf[sig_len];
 
+#if defined(CONFIG_DM_RSA) && !defined(USE_HOSTCC)
+       ret = uclass_get_device(UCLASS_RSA, 0, &rsa_dev);
+       if (!ret) {
+               ret = rsa_mod_exp(rsa_dev, sig, sig_len, prop, buf);
+               if (ret) {
+                       debug("Error in Modular exponentation\n");
+                       return ret;
+               }
+       } else {

lib/rsa/rsa-verify.c static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig, · line 88

Quoted 10 lines
lib/rsa/rsa-verify.c +5 -0
+               printf("RSA: Can't find Mod Exp implemnetation\n");
+               return -EINVAL;
+       }
+#else
        ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
        if (ret) {
                debug("Error in Modular exponentation\n");
                return ret;
        }
+#endif

This should use the uclass regardless I think. The software implementation should just be a driver like the hardware implementation.

Quoted 6 lines
lib/rsa/rsa-verify.c +0 -1
        padding = algo->rsa_padding;
        pad_len = algo->pad_len - algo->checksum_len;
--

1.8.1.4

Regards,
Simon

A message that is not in the archive (sent off-list or not yet received): BY1PR0301MB1288C126892D064BE49D4E6FEF540@BY1PR0301MB1288.namprd03.prod.outlook.com

#12 lore raw

PATCH v3 5/9 lib/rsa: Modify rsa to use DM driver if available

Hi Ruchika,

Quoted 46 lines · Ruchika Gupta

Hi Simon,

Quoted 33 lines

-----Original Message-----
From: sjg at google.com [mailto:sjg at google.com] On Behalf Of Simon Glass
Sent: Wednesday, December 24, 2014 6:19 AM
To: Gupta Ruchika-R66431
Cc: U-Boot Mailing List; Sun York-R58495
Subject: Re: [PATCH 5/9] [v3] lib/rsa: Modify rsa to use DM driver if available

Hi Ruchika,

Quoted 21 lines · Ruchika Gupta

Modify rsa_verify to use the rsa driver of DM library available.The tools and the configurations which don't use Driver Model, will continue to use the same RSA sw library. The software implementation of RSA Modular Exponentation is now compiled if RSA_MOD_EXP_SW is selected.

Kconfig options are also added for rsa library.

---
Changes in v3:
New patch

 include/configs/am335x_evm.h |  1 +
 lib/Kconfig                  |  6 +-----
 lib/rsa/Kconfig              | 31 +++++++++++++++++++++++++++++++
 lib/rsa/Makefile             |  3 ++-
 lib/rsa/rsa-verify.c         | 18 ++++++++++++++++++
 5 files changed, 53 insertions(+), 6 deletions(-)  create mode 100644

lib/rsa/Kconfig

Quoted 11 lines

include/configs/am335x_evm.h line 28

Quoted 9 lines
(quoted diff) +0 -0

b/include/configs/am335x_evm.h index cc36985..aa79841 100644

include/configs/am335x_evm.h +1 -0
@@ -25,6 +25,7 @@
 # ifdef CONFIG_ENABLE_VBOOT
 # define CONFIG_FIT_SIGNATURE
 # define CONFIG_RSA
+# define CONFIG_RSA_MOD_EXP_SW

This should go in am335x_boneblack_vboot_defconfig I think.

Quoted 1 line

I didn?t move it in the defconfig as it was conditionally defined under CONFIG_ENABLE_VBOOT

Ah OK I see.

Quoted 120 lines
Quoted 112 lines
Quoted 33 lines
include/configs/am335x_evm.h +0 -0
 # endif
 #endif
 
lib/Kconfig +1 -5
@@ -27,10 +27,6 @@ config SYS_HZ
          get_timer() must operate in milliseconds and this option must be
          set to 1000.
 
-config RSA
-       bool "Use RSA Library"
-       help
-         RSA support.This enables the RSA algorithm used for FIT image
-         verification in U-Boot.
+source lib/rsa/Kconfig
 
 endmenu
lib/rsa/Kconfig new file +8 -0
index 0000000..8f9aa44
@@ -0,0 +1,31 @@
+config RSA
+       bool "Use RSA Library"
+       select RSA_MOD_EXP_SW if !DM
+       select DM_RSA if DM
+       help
+         RSA support.This enables the RSA algorithm used for FIT image
+         verification in U-Boot.
+         See doc/uImage.FIT/signature.txt for more details.

lib/rsa/Kconfig line 18

Quoted 10 lines
lib/rsa/Kconfig new file +10 -0
+
+if RSA && DM_RSA
+
+config RSA_SW
+       bool "User driver Model for RSA Modular Exponentiation in software"
+       depends on DM && DM_RSA && RSA
+       select RSA_MOD_EXP_SW
+       default y
+       help
+         Enables driver for modular exponentiation in software. This is a

RSA

lib/rsa/Kconfig

Quoted 12 lines
lib/rsa/Kconfig new file +12 -0
+         algorithm used in FIT image verification. It required RSA Key as
+         input.
+         See doc/uImage.FIT/signature.txt for more details.
+
+endif
+
+config RSA_MOD_EXP_SW
+       bool
+       default n
+       help
+         Library for SW implementation of RSA Modular Exponentiation. This
+         library is used by the mkimage tool(not selected through this

option)

lib/rsa/rsa-verify.c static int rsa_verify_key(struct key_prop *prop, const

Quoted 26 lines
lib/rsa/Kconfig new file +1 -0
+         as well as by the RSA driver model with SW implementation.
lib/rsa/Makefile index renamed from lib/rsa/Makefile +0 -0

cc25b3c..ccc6060 100644

lib/rsa/Makefile +0 -1
@@ -7,4 +7,5 @@
 # SPDX-License-Identifier:     GPL-2.0+
 #
 
-obj-$(CONFIG_FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o

rsa-mod-exp.o

lib/rsa/Makefile +2 -0
+obj-$(CONFIG_FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o
+obj-$(CONFIG_RSA_MOD_EXP_SW) += rsa-mod-exp.o
lib/rsa/rsa-verify.c index renamed from lib/rsa/rsa-verify.c +0 -0

f8bc086..27f10ef 100644

lib/rsa/rsa-verify.c +1 -0
@@ -12,6 +12,7 @@
 #include <asm/errno.h>
 #include <asm/types.h>
 #include <asm/unaligned.h>
+#include <dm.h>
 #else
 #include "fdt_host.h"
 #include "mkimage.h"
@@ -43,6 +44,9 @@ static int rsa_verify_key(struct key_prop *prop, const

uint8_t *sig,

Quoted 28 lines
lib/rsa/rsa-verify.c +16 -0
        const uint8_t *padding;
        int pad_len;
        int ret;
+#if defined(CONFIG_DM_RSA) && !defined(USE_HOSTCC)
+       struct udevice *rsa_dev;
+#endif
 
        if (!prop || !sig || !hash || !algo)
                return -EIO;
@@ -63,11 +67,25 @@ static int rsa_verify_key(struct key_prop *prop,
 const uint8_t *sig,
 
        uint8_t buf[sig_len];
 
+#if defined(CONFIG_DM_RSA) && !defined(USE_HOSTCC)
+       ret = uclass_get_device(UCLASS_RSA, 0, &rsa_dev);
+       if (!ret) {
+               ret = rsa_mod_exp(rsa_dev, sig, sig_len, prop, buf);
+               if (ret) {
+                       debug("Error in Modular exponentation\n");
+                       return ret;
+               }
+       } else {
+               printf("RSA: Can't find Mod Exp implemnetation\n");
+               return -EINVAL;
+       }
+#else
        ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
Quoted 8 lines

lib/rsa/rsa-verify.c static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig, · line 88

Quoted 5 lines
lib/rsa/rsa-verify.c +1 -0
        if (ret) {
                debug("Error in Modular exponentation\n");
                return ret;
        }
+#endif

This should use the uclass regardless I think. The software implementation should just be a driver like the hardware implementation.

Quoted 1 line

I have already added software implementation as a driver in the previous patch. I have kept it here for the tools (mkimage) and the platforms which don?t use CONFIG_DM by default.

OK.
[ship]

Regards,
Simon

#13 lore raw

PATCH v3 6/9 DM: crypto/fsl - Add Freescale rsa DM driver

In reply to Ruchika Gupta

Driver added for RSA Modular Exponentiation using Freescale Hardware Accelerator CAAM. The driver used uclass rsa.

---
Changes in v3:
Moved to integrate with RSA UCLASS

 drivers/crypto/Kconfig        |  2 ++
 drivers/crypto/fsl/Kconfig    |  6 +++++
 drivers/crypto/fsl/Makefile   |  1 +
 drivers/crypto/fsl/fsl_rsa.c  | 62 +++++++++++++++++++++++++++++++++++++++++++
 drivers/crypto/fsl/jobdesc.c  | 28 +++++++++++++++++++
 drivers/crypto/fsl/jobdesc.h  |  5 ++++
 drivers/crypto/fsl/rsa_caam.h | 28 +++++++++++++++++++
 7 files changed, 132 insertions(+)
 create mode 100644 drivers/crypto/fsl/Kconfig
 create mode 100644 drivers/crypto/fsl/fsl_rsa.c
 create mode 100644 drivers/crypto/fsl/rsa_caam.h
drivers/crypto/Kconfig +2 -0
index 75f3479..4100df1 100644
@@ -1 +1,3 @@
 source drivers/crypto/rsa/Kconfig
+
+source drivers/crypto/fsl/Kconfig
drivers/crypto/fsl/Kconfig new file +6 -0
new file mode 100644
index 0000000..86b2f2f
@@ -0,0 +1,6 @@
+config FSL_CAAM
+	bool "Freescale Crypto Driver Support"
+	help
+	  Enables the Freescale's Cryptographic Accelerator and Assurance
+	  Module (CAAM), also known as the SEC version 4 (SEC4). The driver uses
+	  Job Ring as interface to communicate with CAAM.
drivers/crypto/fsl/Makefile +1 -0
index 067d0a9..10d10c9 100644
@@ -9,3 +9,4 @@
 obj-y += sec.o
 obj-$(CONFIG_FSL_CAAM) += jr.o fsl_hash.o jobdesc.o error.o
 obj-$(CONFIG_CMD_BLOB) += fsl_blob.o
+obj-$(CONFIG_FSL_RSA) += fsl_rsa.o
drivers/crypto/fsl/fsl_rsa.c new file +62 -0

62 lines not on this page · raw message

drivers/crypto/fsl/jobdesc.c +28 -0
index 1386bae..cc0dced 100644
@@ -11,6 +11,7 @@
 #include <common.h>
 #include "desc_constr.h"
 #include "jobdesc.h"
+#include "rsa_caam.h"
 
 #define KEY_BLOB_SIZE			32
 #define MAC_SIZE			16
@@ -123,3 +124,30 @@ void inline_cnstr_jobdesc_rng_instantiation(uint32_t *desc)
 	append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
 			 OP_ALG_RNG4_SK);
 }
+
+/* Change key size to bytes form bits in calling function*/
+void inline_cnstr_jobdesc_pkha_rsaexp(uint32_t *desc,
+				      struct pk_in_params *pkin, uint8_t *out,
+				      uint32_t out_siz)
+{
+	dma_addr_t dma_addr_e, dma_addr_a, dma_addr_n, dma_addr_out;
+
+	dma_addr_e = virt_to_phys((void *)pkin->e);
+	dma_addr_a = virt_to_phys((void *)pkin->a);
+	dma_addr_n = virt_to_phys((void *)pkin->n);
+	dma_addr_out = virt_to_phys((void *)out);
+
+	init_job_desc(desc, 0);
+	append_key(desc, dma_addr_e, pkin->e_siz, KEY_DEST_PKHA_E | CLASS_1);
+
+	append_fifo_load(desc, dma_addr_a,
+			 pkin->a_siz, LDST_CLASS_1_CCB | FIFOLD_TYPE_PK_A);
+
+	append_fifo_load(desc, dma_addr_n,
+			 pkin->n_siz, LDST_CLASS_1_CCB | FIFOLD_TYPE_PK_N);
+
+	append_operation(desc, OP_TYPE_PK | OP_ALG_PK | OP_ALG_PKMODE_MOD_EXPO);
+
+	append_fifo_store(desc, dma_addr_out, out_siz,
+			  LDST_CLASS_1_CCB | FIFOST_TYPE_PKHA_B);
+}
drivers/crypto/fsl/jobdesc.h +5 -0
index 3cf7226..84b3edd 100644
@@ -10,6 +10,7 @@
 
 #include <common.h>
 #include <asm/io.h>
+#include "rsa_caam.h"
 
 #define KEY_IDNFR_SZ_BYTES		16
 
@@ -26,4 +27,8 @@ void inline_cnstr_jobdesc_blob_decap(uint32_t *desc, uint8_t *key_idnfr,
 				     uint32_t out_sz);
 
 void inline_cnstr_jobdesc_rng_instantiation(uint32_t *desc);
+
+void inline_cnstr_jobdesc_pkha_rsaexp(uint32_t *desc,
+				      struct pk_in_params *pkin, uint8_t *out,
+				      uint32_t out_siz);
 #endif
drivers/crypto/fsl/rsa_caam.h new file +28 -0
new file mode 100644
index 0000000..4ff87ef
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2014 Freescale Semiconductor, Inc.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef __RSA_CAAM_H
+#define __RSA_CAAM_H
+
+#include <common.h>
+
+/**
+ * struct pk_in_params - holder for input to PKHA block in CAAM
+ * These parameters are required to perform Modular Exponentiation
+ * using PKHA Block in CAAM
+ */
+struct pk_in_params {
+	const uint8_t *e;	/* public exponent as byte array */
+	uint32_t e_siz;		/* size of e[] in number of bytes */
+	const uint8_t *n;	/* modulus as byte array */
+	uint32_t n_siz;		/* size of n[] in number of bytes */
+	const uint8_t *a;		/* Signature as byte array */
+	uint32_t a_siz;		/* size of a[] in number of bytes */
+	uint8_t *b;		/* Result exp. modulus in number of bytes */
+	uint32_t b_siz;		/* size of b[] in number of bytes */
+};
+
+#endif
Signature · 1 line
1.8.1.4
#14 lore raw

PATCH v3 6/9 DM: crypto/fsl - Add Freescale rsa DM driver

Quoted 8 lines · Ruchika Gupta

Driver added for RSA Modular Exponentiation using Freescale Hardware Accelerator CAAM. The driver used uclass rsa.

---
Changes in v3:
Moved to integrate with RSA UCLASS

Quoted 13 lines
 drivers/crypto/Kconfig        |  2 ++
 drivers/crypto/fsl/Kconfig    |  6 +++++
 drivers/crypto/fsl/Makefile   |  1 +
 drivers/crypto/fsl/fsl_rsa.c  | 62 +++++++++++++++++++++++++++++++++++++++++++
 drivers/crypto/fsl/jobdesc.c  | 28 +++++++++++++++++++
 drivers/crypto/fsl/jobdesc.h  |  5 ++++
 drivers/crypto/fsl/rsa_caam.h | 28 +++++++++++++++++++
 7 files changed, 132 insertions(+)
 create mode 100644 drivers/crypto/fsl/Kconfig
 create mode 100644 drivers/crypto/fsl/fsl_rsa.c
 create mode 100644 drivers/crypto/fsl/rsa_caam.h
#15 lore raw

PATCH v3 7/9 lib/rsa: Add Kconfig option for HW accelerated RSA

In reply to Ruchika Gupta

Kconfig option added for devices which support RSA Verification (Modular Exponentiation) operation in hardware

---
Changes in v3:
New patch

 lib/rsa/Kconfig | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
lib/rsa/Kconfig +22 -1
index 8f9aa44..fc8a1e7 100644
@@ -11,7 +11,7 @@ if RSA && DM_RSA
 
 config RSA_SW
 	bool "User driver Model for RSA Modular Exponentiation in software"
-	depends on DM && DM_RSA && RSA
+	depends on DM && DM_RSA && RSA && !RSA_HW
 	select RSA_MOD_EXP_SW
 	default y
 	help
@@ -20,6 +20,27 @@ config RSA_SW
 	  input.
 	  See doc/uImage.FIT/signature.txt for more details.
 
+menuconfig RSA_HW
+	bool "Use crypto devices to implement RSA Modular Exponentiation"
+	default y
+	help
+	  Say Y here to get to see options for hardware crypto devices and
+	  processors. This option alone does not enable the crypto device.
+
+	  If you say N,all options in this submenu will be skipped and disabled.
+
+if RSA_HW
+
+config FSL_RSA
+	bool "Implement RSA Modular Exponentiation with FSL crypto accelerator"
+	depends on DM && DM_RSA && RSA && FSL_CAAM
+	default y
+	help
+	  Enables driver for RSA modular exponentiation using Freescale's
+	  cryptographic accelerator - CAAM.
+
+endif
+
 endif
 
 config RSA_MOD_EXP_SW
Signature · 1 line
1.8.1.4
#16 lore raw

PATCH v3 7/9 lib/rsa: Add Kconfig option for HW accelerated RSA

Hi Ruchika,

Quoted 11 lines · Ruchika Gupta

Kconfig option added for devices which support RSA Verification (Modular Exponentiation) operation in hardware

---
Changes in v3:
New patch

 lib/rsa/Kconfig | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

I think this needs tweaking such that RSA_SW is a driver (use RSA_SOFTWARE might be better).

For your freescale one, it should be RSA_FREESCALE_EXP or similar. We might want to support multiple such devices. Don't use RSA_HW - that's not a descriptive name.

lib/rsa/Kconfig if RSA && DM_RSA · line 14

Quoted 11 lines
lib/rsa/Kconfig +1 -1
index 8f9aa44..fc8a1e7 100644
@@ -11,7 +11,7 @@ if RSA && DM_RSA
 
 config RSA_SW
        bool "User driver Model for RSA Modular Exponentiation in software"
-       depends on DM && DM_RSA && RSA
+       depends on DM && DM_RSA && RSA && !RSA_HW

You should drop the last term since it should be possible to have both software and hardware.

Quoted 5 lines
lib/rsa/Kconfig +0 -0
        select RSA_MOD_EXP_SW
        default y
        help
@@ -20,6 +20,27 @@ config RSA_SW
          input.

lib/rsa/Kconfig config RSA_SW · line 30

Quoted 10 lines
lib/rsa/Kconfig +8 -0
          See doc/uImage.FIT/signature.txt for more details.
 
+menuconfig RSA_HW
+       bool "Use crypto devices to implement RSA Modular Exponentiation"
+       default y
+       help
+         Say Y here to get to see options for hardware crypto devices and
+         processors. This option alone does not enable the crypto device.
+
+         If you say N,all options in this submenu will be skipped and disabled.

Space after N

Quoted 17 lines
lib/rsa/Kconfig +13 -1
+
+if RSA_HW
+
+config FSL_RSA
+       bool "Implement RSA Modular Exponentiation with FSL crypto accelerator"
+       depends on DM && DM_RSA && RSA && FSL_CAAM
+       default y
+       help
+         Enables driver for RSA modular exponentiation using Freescale's
+         cryptographic accelerator - CAAM.
+
+endif
+
 endif
 
 config RSA_MOD_EXP_SW
--
Quoted 2 lines

1.8.1.4

Regards,
Simon

#17 lore raw

PATCH v3 7/9 lib/rsa: Add Kconfig option for HW accelerated RSA

Hi Simon,

Quoted 24 lines

-----Original Message-----
From: sjg at google.com [mailto:sjg at google.com] On Behalf Of Simon Glass
Sent: Wednesday, December 24, 2014 6:20 AM
To: Gupta Ruchika-R66431
Cc: U-Boot Mailing List; Sun York-R58495
Subject: Re: [PATCH 7/9] [v3] lib/rsa: Add Kconfig option for HW accelerated RSA

Hi Ruchika,

Quoted 11 lines · Ruchika Gupta

Kconfig option added for devices which support RSA Verification (Modular Exponentiation) operation in hardware

---
Changes in v3:
New patch

 lib/rsa/Kconfig | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
Quoted 6 lines

I think this needs tweaking such that RSA_SW is a driver (use RSA_SOFTWARE might be better).

For your freescale one, it should be RSA_FREESCALE_EXP or similar. We might want to support multiple such devices. Don't use RSA_HW - that's not a descriptive name.

If we are using driver Model, can multiple devices be selected at the same time for the RSA_UCLASS ? I am little confused.

Quoted 8 lines
Quoted 8 lines
lib/rsa/Kconfig index 8f9aa44..fc8a1e7 renamed from lib/rsa/Kconfig +0 -0

100644

lib/rsa/Kconfig +0 -0
@@ -11,7 +11,7 @@ if RSA && DM_RSA
 
Quoted 7 lines

lib/rsa/Kconfig if RSA && DM_RSA · line 14

Quoted 4 lines
lib/rsa/Kconfig +1 -1
 config RSA_SW
        bool "User driver Model for RSA Modular Exponentiation in software"
-       depends on DM && DM_RSA && RSA
+       depends on DM && DM_RSA && RSA && !RSA_HW

You should drop the last term since it should be possible to have both software and hardware.

This option selects the driver model for RSA software implementation suing RSA uclass. If I define both I get this error :

drivers/crypto/fsl/built-in.o:(.u_boot_list_2_driver_2_fsl_rsa+0x0): multiple definition of `_u_boot_list_2_driver_2_fsl_rsa' drivers/crypto/rsa/built-in.o:(.u_boot_list_2_driver_2_fsl_rsa+0x0): first defined here

Quoted 36 lines
Quoted 6 lines
lib/rsa/Kconfig +0 -0
        select RSA_MOD_EXP_SW
        default y
        help
@@ -20,6 +20,27 @@ config RSA_SW
          input.

lib/rsa/Kconfig config RSA_SW · line 30

Quoted 10 lines
lib/rsa/Kconfig +8 -0
          See doc/uImage.FIT/signature.txt for more details.
 
+menuconfig RSA_HW
+       bool "Use crypto devices to implement RSA Modular Exponentiation"
+       default y
+       help
+         Say Y here to get to see options for hardware crypto devices and
+         processors. This option alone does not enable the crypto device.
+
+         If you say N,all options in this submenu will be skipped and

disabled.

Space after N

lib/rsa/Kconfig config RSA_SW

Quoted 5 lines
lib/rsa/Kconfig +5 -0
+
+if RSA_HW
+
+config FSL_RSA
+       bool "Implement RSA Modular Exponentiation with FSL crypto

accelerator"

Quoted 10 lines
lib/rsa/Kconfig +8 -0
+       depends on DM && DM_RSA && RSA && FSL_CAAM
+       default y
+       help
+         Enables driver for RSA modular exponentiation using Freescale's
+         cryptographic accelerator - CAAM.
+
+endif
+
 endif
 
Quoted 7 lines
Quoted 4 lines
lib/rsa/Kconfig +0 -1
 config RSA_MOD_EXP_SW
--

1.8.1.4

Regards,
Simon

Regards,
Ruchika

#18 lore raw

PATCH v3 7/9 lib/rsa: Add Kconfig option for HW accelerated RSA

Hi Ruchika,

Quoted 26 lines · Ruchika Gupta

Hi Simon,

Quoted 24 lines

-----Original Message-----
From: sjg at google.com [mailto:sjg at google.com] On Behalf Of Simon Glass
Sent: Wednesday, December 24, 2014 6:20 AM
To: Gupta Ruchika-R66431
Cc: U-Boot Mailing List; Sun York-R58495
Subject: Re: [PATCH 7/9] [v3] lib/rsa: Add Kconfig option for HW accelerated RSA

Hi Ruchika,

Quoted 11 lines · Ruchika Gupta

Kconfig option added for devices which support RSA Verification (Modular Exponentiation) operation in hardware

---
Changes in v3:
New patch

 lib/rsa/Kconfig | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
Quoted 7 lines
Quoted 6 lines

I think this needs tweaking such that RSA_SW is a driver (use RSA_SOFTWARE might be better).

For your freescale one, it should be RSA_FREESCALE_EXP or similar. We might want to support multiple such devices. Don't use RSA_HW - that's not a descriptive name.

If we are using driver Model, can multiple devices be selected at the same time for the RSA_UCLASS ? I am little confused.

Yes, they can be accessed using uclass_get_device_by_seq() if they are numbered in the aliases:

aliases {
mod_exp0 = "/path/to/hardware-node";
mod_exp1 = "/path/to/software-node";
}

or uclass_get_device() if there is no sequence numbering.

Quoted 16 lines
Quoted 9 lines
Quoted 9 lines
lib/rsa/Kconfig index 8f9aa44..fc8a1e7 renamed from lib/rsa/Kconfig +0 -0

100644

lib/rsa/Kconfig +0 -0
@@ -11,7 +11,7 @@ if RSA && DM_RSA
 
Quoted 7 lines

lib/rsa/Kconfig if RSA && DM_RSA · line 14

Quoted 4 lines
lib/rsa/Kconfig +1 -1
 config RSA_SW
        bool "User driver Model for RSA Modular Exponentiation in software"
-       depends on DM && DM_RSA && RSA
+       depends on DM && DM_RSA && RSA && !RSA_HW

You should drop the last term since it should be possible to have both software and hardware.

Quoted 4 lines

This option selects the driver model for RSA software implementation suing RSA uclass. If I define both I get this error :

drivers/crypto/fsl/built-in.o:(.u_boot_list_2_driver_2_fsl_rsa+0x0): multiple definition of `_u_boot_list_2_driver_2_fsl_rsa' drivers/crypto/rsa/built-in.o:(.u_boot_list_2_driver_2_fsl_rsa+0x0): first defined here

Make sure you use a different name - it seems like you are using fsl_rsa for both.

Quoted 37 lines
Quoted 37 lines
Quoted 7 lines
lib/rsa/Kconfig +0 -0
        select RSA_MOD_EXP_SW
        default y
        help
@@ -20,6 +20,27 @@ config RSA_SW
          input.

lib/rsa/Kconfig config RSA_SW · line 30

Quoted 10 lines
lib/rsa/Kconfig +8 -0
          See doc/uImage.FIT/signature.txt for more details.
 
+menuconfig RSA_HW
+       bool "Use crypto devices to implement RSA Modular Exponentiation"
+       default y
+       help
+         Say Y here to get to see options for hardware crypto devices and
+         processors. This option alone does not enable the crypto device.
+
+         If you say N,all options in this submenu will be skipped and

disabled.

Space after N

lib/rsa/Kconfig config RSA_SW

Quoted 5 lines
lib/rsa/Kconfig +5 -0
+
+if RSA_HW
+
+config FSL_RSA
+       bool "Implement RSA Modular Exponentiation with FSL crypto

accelerator"

Quoted 10 lines
lib/rsa/Kconfig +8 -0
+       depends on DM && DM_RSA && RSA && FSL_CAAM
+       default y
+       help
+         Enables driver for RSA modular exponentiation using Freescale's
+         cryptographic accelerator - CAAM.
+
+endif
+
 endif
 
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
lib/rsa/Kconfig +0 -1
 config RSA_MOD_EXP_SW
--

1.8.1.4

Regards,
Simon

#19 lore raw

PATCH v3 8/9 hash: Add function to find hash_algo struct with progressive hash

In reply to Ruchika Gupta

The hash_algo structure has some implementations in which progressive hash API's are not defined. These are basically the hardware based implementations of SHA. An API is added to find the algo which has progressive hash API's defined. This can then be integrated with RSA checksum library which uses Progressive Hash API's.

---
Changes in v3 :
Corrected ifdef for SHA1

Changes in v2 :
Added commit message

 common/hash.c  | 33 ++++++++++++++++++++++++---------
 include/hash.h | 15 +++++++++++++++
 2 files changed, 39 insertions(+), 9 deletions(-)
common/hash.c +24 -9
index 12d6759..ea1ec60 100644
@@ -20,7 +20,7 @@
 #include <asm/io.h>
 #include <asm/errno.h>
 
-#ifdef CONFIG_CMD_SHA1SUM
+#ifdef CONFIG_SHA1
 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
 {
 	sha1_context *ctx = malloc(sizeof(sha1_context));
@@ -125,12 +125,7 @@ static struct hash_algo hash_algo[] = {
 		CHUNKSZ_SHA256,
 	},
 #endif
-	/*
-	 * This is CONFIG_CMD_SHA1SUM instead of CONFIG_SHA1 since otherwise
-	 * it bloats the code for boards which use SHA1 but not the 'hash'
-	 * or 'sha1sum' commands.
-	 */
-#ifdef CONFIG_CMD_SHA1SUM
+#ifdef CONFIG_SHA1
 	{
 		"sha1",
 		SHA1_SUM_LEN,
@@ -140,7 +135,6 @@ static struct hash_algo hash_algo[] = {
 		hash_update_sha1,
 		hash_finish_sha1,
 	},
-#define MULTI_HASH
 #endif
 #ifdef CONFIG_SHA256
 	{
@@ -152,7 +146,6 @@ static struct hash_algo hash_algo[] = {
 		hash_update_sha256,
 		hash_finish_sha256,
 	},
-#define MULTI_HASH
 #endif
 	{
 		"crc32",
@@ -165,6 +158,10 @@ static struct hash_algo hash_algo[] = {
 	},
 };
 
+#if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM)
+#define MULTI_HASH
+#endif
+
 #if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)
 #define MULTI_HASH
 #endif
@@ -311,6 +308,24 @@ int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
 	return -EPROTONOSUPPORT;
 }
 
+int hash_progressive_lookup_algo(const char *algo_name,
+				 struct hash_algo **algop)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
+		if (!strcmp(algo_name, hash_algo[i].name)) {
+			if (hash_algo[i].hash_init) {
+				*algop = &hash_algo[i];
+				return 0;
+			}
+		}
+	}
+
+	debug("Unknown hash algorithm '%s'\n", algo_name);
+	return -EPROTONOSUPPORT;
+}
+
 void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
 {
 	int i;
include/hash.h +15 -0
index d8ec4f0..059f84e 100644
@@ -128,6 +128,21 @@ int hash_block(const char *algo_name, const void *data, unsigned int len,
 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop);
 
 /**
+ * hash_progressive_lookup_algo() - Look up the hash_algo struct with progressive
+ *				    hash support for an algorithm
+ *
+ * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
+ * algorithm is not available with progressive hash support.
+ *
+ * @algo_name: Hash algorithm to look up
+ * @algop: Pointer to the hash_algo struct if found
+ *
+ * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
+ */
+int hash_progressive_lookup_algo(const char *algo_name,
+				 struct hash_algo **algop);
+
+/**
  * hash_show() - Print out a hash algorithm and value
  *
  * You will get a message like this (without a newline@the end):
Signature · 1 line
1.8.1.4
#20 lore raw

PATCH v3 8/9 hash: Add function to find hash_algo struct with progressive hash

Hi Ruchika,

Quoted 24 lines · Ruchika Gupta

The hash_algo structure has some implementations in which progressive hash API's are not defined. These are basically the hardware based implementations of SHA. An API is added to find the algo which has progressive hash API's defined. This can then be integrated with RSA checksum library which uses Progressive Hash API's.

---
Changes in v3 :
Corrected ifdef for SHA1

Changes in v2 :
Added commit message

 common/hash.c  | 33 ++++++++++++++++++++++++---------
 include/hash.h | 15 +++++++++++++++
 2 files changed, 39 insertions(+), 9 deletions(-)
common/hash.c +0 -0
index 12d6759..ea1ec60 100644

common/hash.c line 23

Quoted 5 lines
common/hash.c +1 -1
@@ -20,7 +20,7 @@
 #include <asm/io.h>
 #include <asm/errno.h>
 
-#ifdef CONFIG_CMD_SHA1SUM
+#ifdef CONFIG_SHA1

I'm still not sure about this. I suspect this will bloat the code for boards that use CONFIG_SHA1 (most) but not CONFIG_CMD_SHA1SUM. You could check that, but I went through some contortions to make sure that the hash API was not compiled in when not needed.

common/hash.c static struct hash_algo hash_algo[] = { · line 132

Quoted 12 lines
common/hash.c +0 -5
 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
 {
        sha1_context *ctx = malloc(sizeof(sha1_context));
@@ -125,12 +125,7 @@ static struct hash_algo hash_algo[] = {
                CHUNKSZ_SHA256,
        },
 #endif
-       /*
-        * This is CONFIG_CMD_SHA1SUM instead of CONFIG_SHA1 since otherwise
-        * it bloats the code for boards which use SHA1 but not the 'hash'
-        * or 'sha1sum' commands.
-        */

This is the comment referring to the above.

Is it possible to leave this logic as it is?

Quoted 62 lines
common/hash.c +23 -3
-#ifdef CONFIG_CMD_SHA1SUM
+#ifdef CONFIG_SHA1
        {
                "sha1",
                SHA1_SUM_LEN,
@@ -140,7 +135,6 @@ static struct hash_algo hash_algo[] = {
                hash_update_sha1,
                hash_finish_sha1,
        },
-#define MULTI_HASH
 #endif
 #ifdef CONFIG_SHA256
        {
@@ -152,7 +146,6 @@ static struct hash_algo hash_algo[] = {
                hash_update_sha256,
                hash_finish_sha256,
        },
-#define MULTI_HASH
 #endif
        {
                "crc32",
@@ -165,6 +158,10 @@ static struct hash_algo hash_algo[] = {
        },
 };
 
+#if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM)
+#define MULTI_HASH
+#endif
+
 #if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)
 #define MULTI_HASH
 #endif
@@ -311,6 +308,24 @@ int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
        return -EPROTONOSUPPORT;
 }
 
+int hash_progressive_lookup_algo(const char *algo_name,
+                                struct hash_algo **algop)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
+               if (!strcmp(algo_name, hash_algo[i].name)) {
+                       if (hash_algo[i].hash_init) {
+                               *algop = &hash_algo[i];
+                               return 0;
+                       }
+               }
+       }
+
+       debug("Unknown hash algorithm '%s'\n", algo_name);
+       return -EPROTONOSUPPORT;
+}
+
 void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
 {
        int i;
include/hash.h +0 -0
index d8ec4f0..059f84e 100644

include/hash.h int hash_block(const char *algo_name, const void *data, unsigned int len, · line 132

Quoted 5 lines
include/hash.h +2 -0
@@ -128,6 +128,21 @@ int hash_block(const char *algo_name, const void *data, unsigned int len,
 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop);
 
 /**
+ * hash_progressive_lookup_algo() - Look up the hash_algo struct with progressive
+ *                                 hash support for an algorithm

Try to get that on one line if you can.

Quoted 17 lines
include/hash.h +13 -1
+ *
+ * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
+ * algorithm is not available with progressive hash support.
+ *
+ * @algo_name: Hash algorithm to look up
+ * @algop: Pointer to the hash_algo struct if found
+ *
+ * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
+ */
+int hash_progressive_lookup_algo(const char *algo_name,
+                                struct hash_algo **algop);
+
+/**
  * hash_show() - Print out a hash algorithm and value
  *
  * You will get a message like this (without a newline at the end):
--
Quoted 2 lines

1.8.1.4

Regards,
Simon

#21 lore raw

PATCH v3 8/9 hash: Add function to find hash_algo struct with progressive hash

Hi Simon,

Quoted 39 lines

-----Original Message-----
From: sjg at google.com [mailto:sjg at google.com] On Behalf Of Simon Glass
Sent: Wednesday, December 24, 2014 6:20 AM
To: Gupta Ruchika-R66431
Cc: U-Boot Mailing List; Sun York-R58495
Subject: Re: [PATCH 8/9] [v3] hash: Add function to find hash_algo struct with progressive hash

Hi Ruchika,

Quoted 27 lines · Ruchika Gupta

The hash_algo structure has some implementations in which progressive hash API's are not defined. These are basically the hardware based implementations of SHA. An API is added to find the algo which has progressive hash API's defined. This can then be integrated with RSA checksum library which uses Progressive Hash API's.

---
Changes in v3 :
Corrected ifdef for SHA1

Changes in v2 :
Added commit message

common/hash.c | 33 ++++++++++++++++++++++++--------- include/hash.h
| 15 +++++++++++++++

 2 files changed, 39 insertions(+), 9 deletions(-)
common/hash.c index 12d6759..ea1ec60 renamed from common/hash.c +0 -0

100644

common/hash.c +0 -0
@@ -20,7 +20,7 @@
 #include <asm/io.h>
 #include <asm/errno.h>
 
Quoted 7 lines

common/hash.c line 23

Quoted 2 lines
common/hash.c +1 -1
-#ifdef CONFIG_CMD_SHA1SUM
+#ifdef CONFIG_SHA1

I'm still not sure about this. I suspect this will bloat the code for boards that use CONFIG_SHA1 (most) but not CONFIG_CMD_SHA1SUM. You could check that, but I went through some contortions to make sure that the hash API was not compiled in when not needed.

Since we will be using this API now in RSA checksum, defining CONFIG_SHA1 should allow the compilation of this structure. Asking user to enable CONFIG_CMD_SHA1SUM for using rsa-checksum doesn?t look right. Please suggest.

Quoted 102 lines

common/hash.c static struct hash_algo hash_algo[] = {

Quoted 9 lines
common/hash.c +1 -2
 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)  {
        sha1_context *ctx = malloc(sizeof(sha1_context)); @@ -125,12
+125,7 @@ static struct hash_algo hash_algo[] = {
                CHUNKSZ_SHA256,
        },
 #endif
-       /*
-        * This is CONFIG_CMD_SHA1SUM instead of CONFIG_SHA1 since

otherwise

common/hash.c static struct hash_algo hash_algo[] = { · line 132

Quoted 3 lines
common/hash.c +0 -3
-        * it bloats the code for boards which use SHA1 but not the 'hash'
-        * or 'sha1sum' commands.
-        */

This is the comment referring to the above.

Is it possible to leave this logic as it is?

Quoted 59 lines
common/hash.c +4 -3
-#ifdef CONFIG_CMD_SHA1SUM
+#ifdef CONFIG_SHA1
        {
                "sha1",
                SHA1_SUM_LEN,
@@ -140,7 +135,6 @@ static struct hash_algo hash_algo[] = {
                hash_update_sha1,
                hash_finish_sha1,
        },
-#define MULTI_HASH
 #endif
 #ifdef CONFIG_SHA256
        {
@@ -152,7 +146,6 @@ static struct hash_algo hash_algo[] = {
                hash_update_sha256,
                hash_finish_sha256,
        },
-#define MULTI_HASH
 #endif
        {
                "crc32",
@@ -165,6 +158,10 @@ static struct hash_algo hash_algo[] = {
        },
 };
 
+#if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) #define
+MULTI_HASH #endif
+
 #if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)  #define

MULTI_HASH #endif @@ -311,6 +308,24 @@ int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)

        return -EPROTONOSUPPORT;

}

common/hash.c +17 -0
+int hash_progressive_lookup_algo(const char *algo_name,
+                                struct hash_algo **algop) {
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
+               if (!strcmp(algo_name, hash_algo[i].name)) {
+                       if (hash_algo[i].hash_init) {
+                               *algop = &hash_algo[i];
+                               return 0;
+                       }
+               }
+       }
+
+       debug("Unknown hash algorithm '%s'\n", algo_name);
+       return -EPROTONOSUPPORT;
+}
+
 void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t
*output)  {
        int i;
include/hash.h index d8ec4f0..059f84e renamed from include/hash.h +0 -0

100644

include/hash.h int hash_block(const char *algo_name, const void · line 132

Quoted 5 lines
include/hash.h +1 -0
@@ -128,6 +128,21 @@ int hash_block(const char *algo_name, const void
 *data, unsigned int len,  int hash_lookup_algo(const char *algo_name,
 struct hash_algo **algop);
 
 /**
+ * hash_progressive_lookup_algo() - Look up the hash_algo struct with

progressive

include/hash.h int hash_block(const char *algo_name, const void *data, unsigned int len, · line 132

Quoted 1 line
include/hash.h +1 -0
+ *                                 hash support for an algorithm

Try to get that on one line if you can.

Quoted 15 lines
include/hash.h +14 -0
+ *
+ * The function returns the pointer to the struct or -EPROTONOSUPPORT
+if the
+ * algorithm is not available with progressive hash support.
+ *
+ * @algo_name: Hash algorithm to look up
+ * @algop: Pointer to the hash_algo struct if found
+ *
+ * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
+ */
+int hash_progressive_lookup_algo(const char *algo_name,
+                                struct hash_algo **algop);
+
+/**
  * hash_show() - Print out a hash algorithm and value
Quoted 8 lines
Quoted 5 lines

*
* You will get a message like this (without a newline at the end):
--
1.8.1.4

Regards,
Simon

Regards,
Ruchika

#22 lore raw

PATCH v3 8/9 hash: Add function to find hash_algo struct with progressive hash

+Wolfgang

Hi Ruchika,

Quoted 49 lines · Ruchika Gupta

Hi Simon,

Quoted 39 lines

-----Original Message-----
From: sjg at google.com [mailto:sjg at google.com] On Behalf Of Simon Glass
Sent: Wednesday, December 24, 2014 6:20 AM
To: Gupta Ruchika-R66431
Cc: U-Boot Mailing List; Sun York-R58495
Subject: Re: [PATCH 8/9] [v3] hash: Add function to find hash_algo struct with progressive hash

Hi Ruchika,

Quoted 27 lines · Ruchika Gupta

The hash_algo structure has some implementations in which progressive hash API's are not defined. These are basically the hardware based implementations of SHA. An API is added to find the algo which has progressive hash API's defined. This can then be integrated with RSA checksum library which uses Progressive Hash API's.

---
Changes in v3 :
Corrected ifdef for SHA1

Changes in v2 :
Added commit message

common/hash.c | 33 ++++++++++++++++++++++++--------- include/hash.h
| 15 +++++++++++++++

 2 files changed, 39 insertions(+), 9 deletions(-)
common/hash.c index 12d6759..ea1ec60 renamed from common/hash.c +0 -0

100644

common/hash.c +0 -0
@@ -20,7 +20,7 @@
 #include <asm/io.h>
 #include <asm/errno.h>
 
Quoted 7 lines

common/hash.c line 23

Quoted 2 lines
common/hash.c +1 -1
-#ifdef CONFIG_CMD_SHA1SUM
+#ifdef CONFIG_SHA1

I'm still not sure about this. I suspect this will bloat the code for boards that use CONFIG_SHA1 (most) but not CONFIG_CMD_SHA1SUM. You could check that, but I went through some contortions to make sure that the hash API was not compiled in when not needed.

Quoted 1 line

Since we will be using this API now in RSA checksum, defining CONFIG_SHA1 should allow the compilation of this structure. Asking user to enable CONFIG_CMD_SHA1SUM for using rsa-checksum doesn?t look right. Please suggest.

Agreed it doesn't, it was just a code size hack. Wolfgang might be able to chime in with thoughts here (+Cc).

But still, do you need to change it? After all, CONFIG_CMD_SHA1SUM should be a superest for CONFIG_SHA1.

[snip]

Regards,
Simon

#23 lore raw

PATCH v3 8/9 hash: Add function to find hash_algo struct with progressive hash

Hi Simon,

Quoted 66 lines

-----Original Message-----
From: sjg at google.com [mailto:sjg at google.com] On Behalf Of Simon Glass
Sent: Tuesday, December 30, 2014 2:44 AM
To: Gupta Ruchika-R66431
Cc: U-Boot Mailing List; Sun York-R58495; Wolfgang Denk
Subject: Re: [PATCH 8/9] [v3] hash: Add function to find hash_algo struct with progressive hash

+Wolfgang

Hi Ruchika,

Quoted 51 lines · Ruchika Gupta

Hi Simon,

Quoted 41 lines

-----Original Message-----
From: sjg at google.com [mailto:sjg at google.com] On Behalf Of Simon Glass
Sent: Wednesday, December 24, 2014 6:20 AM
To: Gupta Ruchika-R66431
Cc: U-Boot Mailing List; Sun York-R58495
Subject: Re: [PATCH 8/9] [v3] hash: Add function to find hash_algo struct with progressive hash

Hi Ruchika,

Quoted 28 lines · Ruchika Gupta

The hash_algo structure has some implementations in which progressive hash API's are not defined. These are basically the hardware based implementations of SHA. An API is added to find the algo which has progressive hash API's defined. This can then be integrated with RSA checksum library which uses Progressive Hash API's.

---
Changes in v3 :
Corrected ifdef for SHA1

Changes in v2 :
Added commit message

 common/hash.c  | 33 ++++++++++++++++++++++++---------
include/hash.h
| 15 +++++++++++++++
 2 files changed, 39 insertions(+), 9 deletions(-)
common/hash.c index 12d6759..ea1ec60 renamed from common/hash.c +0 -0

100644

common/hash.c +0 -0
@@ -20,7 +20,7 @@
 #include <asm/io.h>
 #include <asm/errno.h>
 
Quoted 7 lines

common/hash.c line 23

Quoted 2 lines
common/hash.c +1 -1
-#ifdef CONFIG_CMD_SHA1SUM
+#ifdef CONFIG_SHA1

I'm still not sure about this. I suspect this will bloat the code for boards that use CONFIG_SHA1 (most) but not CONFIG_CMD_SHA1SUM. You could check that, but I went through some contortions to make sure that the hash API was not compiled in when not needed.

Quoted 1 line

Since we will be using this API now in RSA checksum, defining CONFIG_SHA1

Quoted 8 lines

should allow the compilation of this structure. Asking user to enable CONFIG_CMD_SHA1SUM for using rsa-checksum doesn?t look right. Please suggest.

Agreed it doesn't, it was just a code size hack. Wolfgang might be able to chime in with thoughts here (+Cc).

But still, do you need to change it? After all, CONFIG_CMD_SHA1SUM should be a superest for CONFIG_SHA1.

With CONFIG_FIT_SIGNATURE, CONFIG_SHA1 and CONFIG_SHA256 get automatically defined in include/image.h. I need to use the structure hash_algos to find the functions to be used for algo SHA1. If I leave this as it is, it would mean that I will have to modify include/image.h to define CONFIG_CMD_SHA1SUM for FIT signatures. I am not sure whether that would be the right thing to do.

Quoted 5 lines

[snip]

Regards,
Simon

Regards,
Ruchika

#24 lore raw

PATCH v3 9/9 rsa: Use checksum algorithms from struct hash_algo

In reply to Ruchika Gupta

Currently the hash functions used in RSA are called directly from the sha1 and sha256 libraries. Change the RSA checksum library to use the progressive hash API's registered with struct hash_algo. This will allow the checksum library to use the hardware accelerated progressive hash API's once available.

---
Changes in v3:
Modified rsa-verify to check for return from checksum function

Changes in v2:
Added generic function hash_calculate. Pass an additional argument as name of algorithm.

 common/image-sig.c            |  6 ++---
 include/image.h               |  5 ++--
 include/u-boot/rsa-checksum.h |  7 +++---
 lib/rsa/rsa-checksum.c        | 53 +++++++++++++++++++++++++++++++++++++++----
 lib/rsa/rsa-verify.c          |  7 +++++-
 5 files changed, 64 insertions(+), 14 deletions(-)
common/image-sig.c +3 -3
index 8601eda..2c9f0cd 100644
@@ -38,7 +38,7 @@ struct checksum_algo checksum_algos[] = {
 #if IMAGE_ENABLE_SIGN
 		EVP_sha1,
 #endif
-		sha1_calculate,
+		hash_calculate,
 		padding_sha1_rsa2048,
 	},
 	{
@@ -48,7 +48,7 @@ struct checksum_algo checksum_algos[] = {
 #if IMAGE_ENABLE_SIGN
 		EVP_sha256,
 #endif
-		sha256_calculate,
+		hash_calculate,
 		padding_sha256_rsa2048,
 	},
 	{
@@ -58,7 +58,7 @@ struct checksum_algo checksum_algos[] = {
 #if IMAGE_ENABLE_SIGN
 		EVP_sha256,
 #endif
-		sha256_calculate,
+		hash_calculate,
 		padding_sha256_rsa4096,
 	}
 
include/image.h +3 -2
index af30d60..ec55f23 100644
@@ -926,8 +926,9 @@ struct checksum_algo {
 #if IMAGE_ENABLE_SIGN
 	const EVP_MD *(*calculate_sign)(void);
 #endif
-	void (*calculate)(const struct image_region region[],
-			  int region_count, uint8_t *checksum);
+	int (*calculate)(const char *name,
+			 const struct image_region region[],
+			 int region_count, uint8_t *checksum);
 	const uint8_t *rsa_padding;
 };
 
include/u-boot/rsa-checksum.h +3 -4
index c996fb3..c546c80 100644
@@ -16,9 +16,8 @@ extern const uint8_t padding_sha256_rsa4096[];
 extern const uint8_t padding_sha256_rsa2048[];
 extern const uint8_t padding_sha1_rsa2048[];
 
-void sha256_calculate(const struct image_region region[], int region_count,
-		      uint8_t *checksum);
-void sha1_calculate(const struct image_region region[], int region_count,
-		    uint8_t *checksum);
+int hash_calculate(const char *name,
+		   const struct image_region region[], int region_count,
+		   uint8_t *checksum);
 
 #endif
lib/rsa/rsa-checksum.c +49 -4

81 lines not on this page · raw message

lib/rsa/rsa-verify.c +6 -1
index 27f10ef..0028304 100644
@@ -188,7 +188,12 @@ int rsa_verify(struct image_sign_info *info,
 	}
 
 	/* Calculate checksum with checksum-algorithm */
-	info->algo->checksum->calculate(region, region_count, hash);
+	ret = info->algo->checksum->calculate(info->algo->checksum->name,
+					region, region_count, hash);
+	if (ret < 0) {
+		debug("%s: Error in checksum calculation\n", __func__);
+		return -EINVAL;
+	}
 
 	/* See if we must use a particular key */
 	if (info->required_keynode != -1) {
Signature · 1 line
1.8.1.4
#25 lore raw

PATCH v3 9/9 rsa: Use checksum algorithms from struct hash_algo

Hi Ruchika,

Quoted 75 lines · Ruchika Gupta

Currently the hash functions used in RSA are called directly from the sha1 and sha256 libraries. Change the RSA checksum library to use the progressive hash API's registered with struct hash_algo. This will allow the checksum library to use the hardware accelerated progressive hash API's once available.

---
Changes in v3:
Modified rsa-verify to check for return from checksum function

Changes in v2:
Added generic function hash_calculate. Pass an additional argument as name of algorithm.

 common/image-sig.c            |  6 ++---
 include/image.h               |  5 ++--
 include/u-boot/rsa-checksum.h |  7 +++---
 lib/rsa/rsa-checksum.c        | 53 +++++++++++++++++++++++++++++++++++++++----
 lib/rsa/rsa-verify.c          |  7 +++++-
 5 files changed, 64 insertions(+), 14 deletions(-)
common/image-sig.c +3 -3
index 8601eda..2c9f0cd 100644
@@ -38,7 +38,7 @@ struct checksum_algo checksum_algos[] = {
 #if IMAGE_ENABLE_SIGN
                EVP_sha1,
 #endif
-               sha1_calculate,
+               hash_calculate,
                padding_sha1_rsa2048,
        },
        {
@@ -48,7 +48,7 @@ struct checksum_algo checksum_algos[] = {
 #if IMAGE_ENABLE_SIGN
                EVP_sha256,
 #endif
-               sha256_calculate,
+               hash_calculate,
                padding_sha256_rsa2048,
        },
        {
@@ -58,7 +58,7 @@ struct checksum_algo checksum_algos[] = {
 #if IMAGE_ENABLE_SIGN
                EVP_sha256,
 #endif
-               sha256_calculate,
+               hash_calculate,
                padding_sha256_rsa4096,
        }
 
include/image.h +3 -2
index af30d60..ec55f23 100644
@@ -926,8 +926,9 @@ struct checksum_algo {
 #if IMAGE_ENABLE_SIGN
        const EVP_MD *(*calculate_sign)(void);
 #endif
-       void (*calculate)(const struct image_region region[],
-                         int region_count, uint8_t *checksum);
+       int (*calculate)(const char *name,
+                        const struct image_region region[],
+                        int region_count, uint8_t *checksum);
        const uint8_t *rsa_padding;
 };
 
include/u-boot/rsa-checksum.h +0 -0
index c996fb3..c546c80 100644
@@ -16,9 +16,8 @@ extern const uint8_t padding_sha256_rsa4096[];
 extern const uint8_t padding_sha256_rsa2048[];

include/u-boot/rsa-checksum.h extern const uint8_t padding_sha256_rsa4096[]; · line 22

Quoted 10 lines
include/u-boot/rsa-checksum.h +3 -4
 extern const uint8_t padding_sha1_rsa2048[];
 
-void sha256_calculate(const struct image_region region[], int region_count,
-                     uint8_t *checksum);
-void sha1_calculate(const struct image_region region[], int region_count,
-                   uint8_t *checksum);
+int hash_calculate(const char *name,
+                  const struct image_region region[], int region_count,
+                  uint8_t *checksum);
 

This could use a function comment.

Quoted 42 lines
include/u-boot/rsa-checksum.h +0 -0
 #endif
lib/rsa/rsa-checksum.c +19 -3
index 8d8b59f..7f1909a 100644
@@ -10,12 +10,13 @@
 #include <asm/byteorder.h>
 #include <asm/errno.h>
 #include <asm/unaligned.h>
+#include <hash.h>
 #else
 #include "fdt_host.h"
-#endif
-#include <u-boot/rsa.h>
 #include <u-boot/sha1.h>
 #include <u-boot/sha256.h>
+#endif
+#include <u-boot/rsa.h>
 
 /* PKCS 1.5 paddings as described in the RSA PKCS#1 v2.1 standard. */
 
@@ -136,7 +137,33 @@ const uint8_t padding_sha256_rsa4096[RSA4096_BYTES - SHA256_SUM_LEN] = {
        0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
 };
 
-void sha1_calculate(const struct image_region region[], int region_count,
+#ifndef USE_HOSTCC
+int hash_calculate(const char *name,
+                   const struct image_region region[],
+                   int region_count, uint8_t *checksum)
+{
+       struct hash_algo *algo;
+       int ret = 0;
+       void *ctx;
+       uint32_t i;
+       i = 0;
+
+       ret = hash_progressive_lookup_algo(name, &algo);
+       if (ret)
+               return ret;
+
+       algo->hash_init(algo, &ctx);

lib/rsa/rsa-checksum.c const uint8_t padding_sha256_rsa4096[RSA4096_BYTES - SHA256_SUM_LEN] = { · line 165

Quoted 10 lines
lib/rsa/rsa-checksum.c +10 -0
+       for (i = 0; i < region_count - 1; i++)
+               algo->hash_update(algo, ctx, region[i].data, region[i].size, 0);
+
+       algo->hash_update(algo, ctx, region[i].data, region[i].size, 1);
+       algo->hash_finish(algo, ctx, checksum, algo->digest_size);
+
+       return 0;
+}
+
+#else

The above looks good, but what is happening here? Why do you need to do something different for USE_HOSTCC?

Quoted 57 lines
lib/rsa/rsa-checksum.c +20 -1
+int sha1_calculate(const struct image_region region[], int region_count,
                    uint8_t *checksum)
 {
        sha1_context ctx;
@@ -147,9 +174,11 @@ void sha1_calculate(const struct image_region region[], int region_count,
        for (i = 0; i < region_count; i++)
                sha1_update(&ctx, region[i].data, region[i].size);
        sha1_finish(&ctx, checksum);
+
+       return 0;
 }
 
-void sha256_calculate(const struct image_region region[], int region_count,
+int sha256_calculate(const struct image_region region[], int region_count,
                      uint8_t *checksum)
 {
        sha256_context ctx;
@@ -160,4 +189,20 @@ void sha256_calculate(const struct image_region region[], int region_count,
        for (i = 0; i < region_count; i++)
                sha256_update(&ctx, region[i].data, region[i].size);
        sha256_finish(&ctx, checksum);
+
+       return 0;
 }
+
+int hash_calculate(const char *name,
+                  const struct image_region region[], int region_count,
+                  uint8_t *checksum)
+{
+       if (!strcmp(name, "sha1"))
+               sha1_calculate(region, region_count, checksum);
+
+       if (!strcmp(name, "sha256"))
+               sha256_calculate(region, region_count, checksum);
+
+       return 0;
+}
+#endif
lib/rsa/rsa-verify.c +6 -2
index 27f10ef..0028304 100644
@@ -188,7 +188,12 @@ int rsa_verify(struct image_sign_info *info,
        }
 
        /* Calculate checksum with checksum-algorithm */
-       info->algo->checksum->calculate(region, region_count, hash);
+       ret = info->algo->checksum->calculate(info->algo->checksum->name,
+                                       region, region_count, hash);
+       if (ret < 0) {
+               debug("%s: Error in checksum calculation\n", __func__);
+               return -EINVAL;
+       }
 
        /* See if we must use a particular key */
        if (info->required_keynode != -1) {
--
Quoted 2 lines

1.8.1.4

Regards,
Simon

A message that is not in the archive (sent off-list or not yet received): BY1PR0301MB1288E92E4FEF74B81F040302EF510@BY1PR0301MB1288.namprd03.prod.outlook.com

#27 lore raw

PATCH v3 9/9 rsa: Use checksum algorithms from struct hash_algo

Sorry. Resending as message bounced on uboot mailing list.

Hi Simon,

Quoted 149 lines

-----Original Message-----
From: sjg at google.com [mailto:sjg at google.com] On Behalf Of Simon Glass
Sent: Wednesday, December 24, 2014 6:20 AM
To: Gupta Ruchika-R66431
Cc: U-Boot Mailing List; Sun York-R58495
Subject: Re: [PATCH 9/9] [v3] rsa: Use checksum algorithms from struct hash_algo

Hi Ruchika,

Quoted 4 lines · Ruchika Gupta

Currently the hash functions used in RSA are called directly from the sha1 and sha256 libraries. Change the RSA checksum library to use the progressive hash API's registered with struct hash_algo. This will allow the checksum library to use the hardware accelerated progressive hash

API's once available.

Quoted 7 lines

---
Changes in v3:
Modified rsa-verify to check for return from checksum function

Quoted 8 lines

Changes in v2:
Added generic function hash_calculate. Pass an additional argument as name of algorithm.

 common/image-sig.c            |  6 ++---
 include/image.h               |  5 ++--
 include/u-boot/rsa-checksum.h |  7 +++---
 lib/rsa/rsa-checksum.c        | 53

+++++++++++++++++++++++++++++++++++++++----

Quoted 56 lines
 lib/rsa/rsa-verify.c          |  7 +++++-
 5 files changed, 64 insertions(+), 14 deletions(-)
common/image-sig.c index renamed from common/image-sig.c +0 -0

8601eda..2c9f0cd 100644

common/image-sig.c +0 -0
@@ -38,7 +38,7 @@ struct checksum_algo checksum_algos[] = {  #if

IMAGE_ENABLE_SIGN

                EVP_sha1,

#endif

common/image-sig.c +1 -1
-               sha1_calculate,
+               hash_calculate,
                padding_sha1_rsa2048,
        },
        {
@@ -48,7 +48,7 @@ struct checksum_algo checksum_algos[] = {  #if

IMAGE_ENABLE_SIGN

                EVP_sha256,

#endif

common/image-sig.c +1 -1
-               sha256_calculate,
+               hash_calculate,
                padding_sha256_rsa2048,
        },
        {
@@ -58,7 +58,7 @@ struct checksum_algo checksum_algos[] = {  #if

IMAGE_ENABLE_SIGN

                EVP_sha256,

#endif

common/image-sig.c +1 -1
-               sha256_calculate,
+               hash_calculate,
                padding_sha256_rsa4096,
        }
include/image.h index af30d60..ec55f23 renamed from include/image.h +0 -0

100644

include/image.h +3 -2
@@ -926,8 +926,9 @@ struct checksum_algo {  #if IMAGE_ENABLE_SIGN
        const EVP_MD *(*calculate_sign)(void);  #endif
-       void (*calculate)(const struct image_region region[],
-                         int region_count, uint8_t *checksum);
+       int (*calculate)(const char *name,
+                        const struct image_region region[],
+                        int region_count, uint8_t *checksum);
        const uint8_t *rsa_padding;
 };
 
(quoted diff) +0 -0

b/include/u-boot/rsa-checksum.h index c996fb3..c546c80 100644

include/u-boot/rsa-checksum.h +0 -0
@@ -16,9 +16,8 @@ extern const uint8_t padding_sha256_rsa4096[];

extern const uint8_t padding_sha256_rsa2048[]; extern const uint8_t padding_sha1_rsa2048[];

Quoted 1 line

-void sha256_calculate(const struct image_region region[], int

region_count,

include/u-boot/rsa-checksum.h extern const uint8_t padding_sha256_rsa4096[]; · line 21

Quoted 7 lines
include/u-boot/rsa-checksum.h +3 -3
-                     uint8_t *checksum);
-void sha1_calculate(const struct image_region region[], int region_count,
-                   uint8_t *checksum);
+int hash_calculate(const char *name,
+                  const struct image_region region[], int region_count,
+                  uint8_t *checksum);

This could use a function comment.

lib/rsa/rsa-checksum.c const uint8_t padding_sha256_rsa4096[RSA4096_BYTES -

Quoted 22 lines

#endif

lib/rsa/rsa-checksum.c index renamed from lib/rsa/rsa-checksum.c +0 -0

8d8b59f..7f1909a 100644

lib/rsa/rsa-checksum.c +3 -2
@@ -10,12 +10,13 @@
 #include <asm/byteorder.h>
 #include <asm/errno.h>
 #include <asm/unaligned.h>
+#include <hash.h>
 #else
 #include "fdt_host.h"
-#endif
-#include <u-boot/rsa.h>
 #include <u-boot/sha1.h>
 #include <u-boot/sha256.h>
+#endif
+#include <u-boot/rsa.h>
 
 /* PKCS 1.5 paddings as described in the RSA PKCS#1 v2.1 standard. */
 
@@ -136,7 +137,33 @@ const uint8_t padding_sha256_rsa4096[RSA4096_BYTES -

SHA256_SUM_LEN] = {

Quoted 25 lines
lib/rsa/rsa-checksum.c +21 -1
        0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20  };
 
-void sha1_calculate(const struct image_region region[], int
 region_count,
+#ifndef USE_HOSTCC
+int hash_calculate(const char *name,
+                   const struct image_region region[],
+                   int region_count, uint8_t *checksum) {
+       struct hash_algo *algo;
+       int ret = 0;
+       void *ctx;
+       uint32_t i;
+       i = 0;
+
+       ret = hash_progressive_lookup_algo(name, &algo);
+       if (ret)
+               return ret;
+
+       algo->hash_init(algo, &ctx);
+       for (i = 0; i < region_count - 1; i++)
+               algo->hash_update(algo, ctx, region[i].data,
+ region[i].size, 0);
+
+       algo->hash_update(algo, ctx, region[i].data, region[i].size, 1);
+       algo->hash_finish(algo, ctx, checksum, algo->digest_size);
Quoted 8 lines

lib/rsa/rsa-checksum.c const uint8_t padding_sha256_rsa4096[RSA4096_BYTES - SHA256_SUM_LEN] = { · line 165

Quoted 5 lines
lib/rsa/rsa-checksum.c +5 -0
+
+       return 0;
+}
+
+#else

The above looks good, but what is happening here? Why do you need to do something different for USE_HOSTCC?

The hash_algo struct is defined in common/hash.c which doesn?t get compiled for tools. That is why I did it differently for USE_HOSTCC

Quoted 58 lines

lib/rsa/rsa-checksum.c void sha1_calculate(const struct image_region

Quoted 6 lines
lib/rsa/rsa-checksum.c +2 -0
+int sha1_calculate(const struct image_region region[], int
+region_count,
                    uint8_t *checksum)  {
        sha1_context ctx;
@@ -147,9 +174,11 @@ void sha1_calculate(const struct image_region

region[], int region_count,

lib/rsa/rsa-checksum.c void sha256_calculate(const struct image_region

Quoted 14 lines
lib/rsa/rsa-checksum.c +4 -1
        for (i = 0; i < region_count; i++)
                sha1_update(&ctx, region[i].data, region[i].size);
        sha1_finish(&ctx, checksum);
+
+       return 0;
 }
 
-void sha256_calculate(const struct image_region region[], int
 region_count,
+int sha256_calculate(const struct image_region region[], int
+region_count,
                      uint8_t *checksum)  {
        sha256_context ctx;
@@ -160,4 +189,20 @@ void sha256_calculate(const struct image_region

region[], int region_count,

Quoted 36 lines
lib/rsa/rsa-checksum.c +16 -0
        for (i = 0; i < region_count; i++)
                sha256_update(&ctx, region[i].data, region[i].size);
        sha256_finish(&ctx, checksum);
+
+       return 0;
 }
+
+int hash_calculate(const char *name,
+                  const struct image_region region[], int region_count,
+                  uint8_t *checksum)
+{
+       if (!strcmp(name, "sha1"))
+               sha1_calculate(region, region_count, checksum);
+
+       if (!strcmp(name, "sha256"))
+               sha256_calculate(region, region_count, checksum);
+
+       return 0;
+}
+#endif
lib/rsa/rsa-verify.c index renamed from lib/rsa/rsa-verify.c +0 -0

27f10ef..0028304 100644

lib/rsa/rsa-verify.c +6 -1
@@ -188,7 +188,12 @@ int rsa_verify(struct image_sign_info *info,
        }
 
        /* Calculate checksum with checksum-algorithm */
-       info->algo->checksum->calculate(region, region_count, hash);
+       ret = info->algo->checksum->calculate(info->algo->checksum->name,
+                                       region, region_count, hash);
+       if (ret < 0) {
+               debug("%s: Error in checksum calculation\n", __func__);
+               return -EINVAL;
+       }
 
Quoted 8 lines
Quoted 5 lines
lib/rsa/rsa-verify.c +0 -1
        /* See if we must use a particular key */
        if (info->required_keynode != -1) {
--

1.8.1.4

Regards,
Simon

Regards,
Ruchika

#28 lore raw

PATCH v3 9/9 rsa: Use checksum algorithms from struct hash_algo

In reply to a message not in the archive

Hi Ruchika,

Quoted 159 lines · Ruchika Gupta

Hi Simon,

Quoted 149 lines

-----Original Message-----
From: sjg at google.com [mailto:sjg at google.com] On Behalf Of Simon Glass
Sent: Wednesday, December 24, 2014 6:20 AM
To: Gupta Ruchika-R66431
Cc: U-Boot Mailing List; Sun York-R58495
Subject: Re: [PATCH 9/9] [v3] rsa: Use checksum algorithms from struct hash_algo

Hi Ruchika,

Quoted 4 lines · Ruchika Gupta

Currently the hash functions used in RSA are called directly from the sha1 and sha256 libraries. Change the RSA checksum library to use the progressive hash API's registered with struct hash_algo. This will allow the checksum library to use the hardware accelerated progressive hash

API's once available.

Quoted 7 lines

---
Changes in v3:
Modified rsa-verify to check for return from checksum function

Quoted 8 lines

Changes in v2:
Added generic function hash_calculate. Pass an additional argument as name of algorithm.

 common/image-sig.c            |  6 ++---
 include/image.h               |  5 ++--
 include/u-boot/rsa-checksum.h |  7 +++---
 lib/rsa/rsa-checksum.c        | 53

+++++++++++++++++++++++++++++++++++++++----

Quoted 56 lines
 lib/rsa/rsa-verify.c          |  7 +++++-
 5 files changed, 64 insertions(+), 14 deletions(-)
common/image-sig.c index renamed from common/image-sig.c +0 -0

8601eda..2c9f0cd 100644

common/image-sig.c +0 -0
@@ -38,7 +38,7 @@ struct checksum_algo checksum_algos[] = {  #if

IMAGE_ENABLE_SIGN

                EVP_sha1,

#endif

common/image-sig.c +1 -1
-               sha1_calculate,
+               hash_calculate,
                padding_sha1_rsa2048,
        },
        {
@@ -48,7 +48,7 @@ struct checksum_algo checksum_algos[] = {  #if

IMAGE_ENABLE_SIGN

                EVP_sha256,

#endif

common/image-sig.c +1 -1
-               sha256_calculate,
+               hash_calculate,
                padding_sha256_rsa2048,
        },
        {
@@ -58,7 +58,7 @@ struct checksum_algo checksum_algos[] = {  #if

IMAGE_ENABLE_SIGN

                EVP_sha256,

#endif

common/image-sig.c +1 -1
-               sha256_calculate,
+               hash_calculate,
                padding_sha256_rsa4096,
        }
include/image.h index af30d60..ec55f23 renamed from include/image.h +0 -0

100644

include/image.h +3 -2
@@ -926,8 +926,9 @@ struct checksum_algo {  #if IMAGE_ENABLE_SIGN
        const EVP_MD *(*calculate_sign)(void);  #endif
-       void (*calculate)(const struct image_region region[],
-                         int region_count, uint8_t *checksum);
+       int (*calculate)(const char *name,
+                        const struct image_region region[],
+                        int region_count, uint8_t *checksum);
        const uint8_t *rsa_padding;
 };
 
(quoted diff) +0 -0

b/include/u-boot/rsa-checksum.h index c996fb3..c546c80 100644

include/u-boot/rsa-checksum.h +0 -0
@@ -16,9 +16,8 @@ extern const uint8_t padding_sha256_rsa4096[];

extern const uint8_t padding_sha256_rsa2048[]; extern const uint8_t padding_sha1_rsa2048[];

Quoted 1 line

-void sha256_calculate(const struct image_region region[], int

region_count,

include/u-boot/rsa-checksum.h extern const uint8_t padding_sha256_rsa4096[]; · line 21

Quoted 7 lines
include/u-boot/rsa-checksum.h +3 -3
-                     uint8_t *checksum);
-void sha1_calculate(const struct image_region region[], int region_count,
-                   uint8_t *checksum);
+int hash_calculate(const char *name,
+                  const struct image_region region[], int region_count,
+                  uint8_t *checksum);

This could use a function comment.

lib/rsa/rsa-checksum.c const uint8_t padding_sha256_rsa4096[RSA4096_BYTES -

Quoted 22 lines

#endif

lib/rsa/rsa-checksum.c index renamed from lib/rsa/rsa-checksum.c +0 -0

8d8b59f..7f1909a 100644

lib/rsa/rsa-checksum.c +3 -2
@@ -10,12 +10,13 @@
 #include <asm/byteorder.h>
 #include <asm/errno.h>
 #include <asm/unaligned.h>
+#include <hash.h>
 #else
 #include "fdt_host.h"
-#endif
-#include <u-boot/rsa.h>
 #include <u-boot/sha1.h>
 #include <u-boot/sha256.h>
+#endif
+#include <u-boot/rsa.h>
 
 /* PKCS 1.5 paddings as described in the RSA PKCS#1 v2.1 standard. */
 
@@ -136,7 +137,33 @@ const uint8_t padding_sha256_rsa4096[RSA4096_BYTES -

SHA256_SUM_LEN] = {

Quoted 25 lines
lib/rsa/rsa-checksum.c +21 -1
        0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20  };
 
-void sha1_calculate(const struct image_region region[], int
 region_count,
+#ifndef USE_HOSTCC
+int hash_calculate(const char *name,
+                   const struct image_region region[],
+                   int region_count, uint8_t *checksum) {
+       struct hash_algo *algo;
+       int ret = 0;
+       void *ctx;
+       uint32_t i;
+       i = 0;
+
+       ret = hash_progressive_lookup_algo(name, &algo);
+       if (ret)
+               return ret;
+
+       algo->hash_init(algo, &ctx);
+       for (i = 0; i < region_count - 1; i++)
+               algo->hash_update(algo, ctx, region[i].data,
+ region[i].size, 0);
+
+       algo->hash_update(algo, ctx, region[i].data, region[i].size, 1);
+       algo->hash_finish(algo, ctx, checksum, algo->digest_size);
Quoted 8 lines

lib/rsa/rsa-checksum.c const uint8_t padding_sha256_rsa4096[RSA4096_BYTES - SHA256_SUM_LEN] = { · line 165

Quoted 5 lines
lib/rsa/rsa-checksum.c +5 -0
+
+       return 0;
+}
+
+#else

The above looks good, but what is happening here? Why do you need to do something different for USE_HOSTCC?

Quoted 1 line

The hash_algo struct is defined in common/hash.c which doesn?t get compiled for tools. That is why I did it differently for USE_HOSTCC

I wonder if we should compile hash.c for tools? Would that be easier or harder?

[snip]

Regards,
Simon

#29 lore raw

PATCH v3 9/9 rsa: Use checksum algorithms from struct hash_algo

Hi Simon,

Quoted 177 lines

-----Original Message-----
From: sjg at google.com [mailto:sjg at google.com] On Behalf Of Simon Glass
Sent: Tuesday, December 30, 2014 2:42 AM
To: Gupta Ruchika-R66431
Cc: U-Boot Mailing List; Sun York-R58495
Subject: Re: [PATCH 9/9] [v3] rsa: Use checksum algorithms from struct hash_algo

Hi Ruchika,

Quoted 98 lines · Ruchika Gupta

Hi Simon,

Quoted 96 lines

-----Original Message-----
From: sjg at google.com [mailto:sjg at google.com] On Behalf Of Simon Glass
Sent: Wednesday, December 24, 2014 6:20 AM
To: Gupta Ruchika-R66431
Cc: U-Boot Mailing List; Sun York-R58495
Subject: Re: [PATCH 9/9] [v3] rsa: Use checksum algorithms from struct hash_algo

Hi Ruchika,

Quoted 6 lines · Ruchika Gupta

Currently the hash functions used in RSA are called directly from the
sha1 and sha256 libraries. Change the RSA checksum library to use the progressive hash API's registered with struct hash_algo. This will allow the checksum library to use the hardware accelerated progressive hash

API's once available.

Quoted 7 lines

---
Changes in v3:
Modified rsa-verify to check for return from checksum function

Quoted 8 lines

Changes in v2:
Added generic function hash_calculate. Pass an additional argument as name of algorithm.

 common/image-sig.c            |  6 ++---
 include/image.h               |  5 ++--
 include/u-boot/rsa-checksum.h |  7 +++---
 lib/rsa/rsa-checksum.c        | 53

+++++++++++++++++++++++++++++++++++++++----

Quoted 56 lines
 lib/rsa/rsa-verify.c          |  7 +++++-
 5 files changed, 64 insertions(+), 14 deletions(-)
common/image-sig.c index renamed from common/image-sig.c +0 -0

8601eda..2c9f0cd 100644

common/image-sig.c +0 -0
@@ -38,7 +38,7 @@ struct checksum_algo checksum_algos[] = {  #if

IMAGE_ENABLE_SIGN

                EVP_sha1,

#endif

common/image-sig.c +1 -1
-               sha1_calculate,
+               hash_calculate,
                padding_sha1_rsa2048,
        },
        {
@@ -48,7 +48,7 @@ struct checksum_algo checksum_algos[] = {  #if

IMAGE_ENABLE_SIGN

                EVP_sha256,

#endif

common/image-sig.c +1 -1
-               sha256_calculate,
+               hash_calculate,
                padding_sha256_rsa2048,
        },
        {
@@ -58,7 +58,7 @@ struct checksum_algo checksum_algos[] = {  #if

IMAGE_ENABLE_SIGN

                EVP_sha256,

#endif

common/image-sig.c +1 -1
-               sha256_calculate,
+               hash_calculate,
                padding_sha256_rsa4096,
        }
include/image.h index renamed from include/image.h +0 -0

af30d60..ec55f23
100644

include/image.h +3 -2
@@ -926,8 +926,9 @@ struct checksum_algo {  #if IMAGE_ENABLE_SIGN
        const EVP_MD *(*calculate_sign)(void);  #endif
-       void (*calculate)(const struct image_region region[],
-                         int region_count, uint8_t *checksum);
+       int (*calculate)(const char *name,
+                        const struct image_region region[],
+                        int region_count, uint8_t *checksum);
        const uint8_t *rsa_padding;  };
 
(quoted diff) +0 -0

b/include/u-boot/rsa-checksum.h index c996fb3..c546c80 100644

include/u-boot/rsa-checksum.h +0 -0
@@ -16,9 +16,8 @@ extern const uint8_t padding_sha256_rsa4096[];

extern const uint8_t padding_sha256_rsa2048[]; extern const uint8_t padding_sha1_rsa2048[];

Quoted 1 line

-void sha256_calculate(const struct image_region region[], int

region_count,

Quoted 2 lines
include/u-boot/rsa-checksum.h +0 -2
-                     uint8_t *checksum);
-void sha1_calculate(const struct image_region region[], int

region_count,

Quoted 66 lines
Quoted 66 lines

include/u-boot/rsa-checksum.h extern const uint8_t padding_sha256_rsa4096[]; · line 21

Quoted 5 lines
include/u-boot/rsa-checksum.h +3 -1
-                   uint8_t *checksum);
+int hash_calculate(const char *name,
+                  const struct image_region region[], int region_count,
+                  uint8_t *checksum);

This could use a function comment.

Quoted 23 lines

#endif

lib/rsa/rsa-checksum.c index renamed from lib/rsa/rsa-checksum.c +0 -0

8d8b59f..7f1909a 100644

lib/rsa/rsa-checksum.c +3 -2
@@ -10,12 +10,13 @@
 #include <asm/byteorder.h>
 #include <asm/errno.h>
 #include <asm/unaligned.h>
+#include <hash.h>
 #else
 #include "fdt_host.h"
-#endif
-#include <u-boot/rsa.h>
 #include <u-boot/sha1.h>
 #include <u-boot/sha256.h>
+#endif
+#include <u-boot/rsa.h>
 
 /* PKCS 1.5 paddings as described in the RSA PKCS#1 v2.1 standard.
 */
@@ -136,7 +137,33 @@ const uint8_t
Quoted 1 line

padding_sha256_rsa4096[RSA4096_BYTES -

SHA256_SUM_LEN] = {

Quoted 20 lines
        0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20  };

-void sha1_calculate(const struct image_region region[], int region_count,

lib/rsa/rsa-checksum.c +16 -0
+#ifndef USE_HOSTCC
+int hash_calculate(const char *name,
+                   const struct image_region region[],
+                   int region_count, uint8_t *checksum) {
+       struct hash_algo *algo;
+       int ret = 0;
+       void *ctx;
+       uint32_t i;
+       i = 0;
+
+       ret = hash_progressive_lookup_algo(name, &algo);
+       if (ret)
+               return ret;
+
+       algo->hash_init(algo, &ctx);
+       for (i = 0; i < region_count - 1; i++)

lib/rsa/rsa-checksum.c const uint8_t padding_sha256_rsa4096[RSA4096_BYTES - SHA256_SUM_LEN] = { · line 165

Quoted 10 lines
lib/rsa/rsa-checksum.c +10 -0
+               algo->hash_update(algo, ctx, region[i].data,
+ region[i].size, 0);
+
+       algo->hash_update(algo, ctx, region[i].data, region[i].size, 1);
+       algo->hash_finish(algo, ctx, checksum, algo->digest_size);
+
+       return 0;
+}
+
+#else

The above looks good, but what is happening here? Why do you need to do something different for USE_HOSTCC?

Quoted 5 lines
Quoted 2 lines

The hash_algo struct is defined in common/hash.c which doesn?t get compiled for tools. That is why I did it differently for USE_HOSTCC

I wonder if we should compile hash.c for tools? Would that be easier or harder?

I had tried doing that but it gave me loads of compilation errors. I thought it would be better to leave it for now.

Quoted 5 lines

[snip]

Regards,
Simon

Regards,
Ruchika