Skip to contentSkip to conversation

rsa: Split the rsa-verify

RFC PATCH Superseded Ruchika Gupta 2 participants 5 messages Dec 18, 2014 – Dec 19, 2014

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/4 rsa: Split the rsa-verify

Public exponentiation which is required in rsa verify functionality is currently tightly integrated with verification code in rsa_verify.c. Currently this implementation is software based. Some platforms having support of the exponentiation in hardware. To enable the rsa verify functionality to use the Modular exponentiation if present in hardware, the patch-set splits the file into two files:

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 formal (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 exponentiation from rsa-verify.c to this file. The file is compiled if "CONFIG_RSA_MOD_EXP_SW" is defined. In general if both CONFIG_FIT_SIGNATURE and CONFIG_RSA are defined, CONFIG_RSA_MOD_EXP_SW gets automatically defined.

Platforms having hardware implementation for rsa_mod_exp can add a define "CONFIG_RSA_MOD_EXP_HW" to their config files. Adding this defined, undefs the CONFIG_RSA_MOD_EXP_SW and hardware implementation of mod_exp gets compiled.

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

 Kconfig                      |   2 +
 include/config_fallbacks.h   |   8 ++
 include/u-boot/rsa-mod-exp.h |  34 +++++
 lib/Kconfig                  |  15 +++
 lib/rsa/Makefile             |   1 +
 lib/rsa/rsa-mod-exp.c        | 308 +++++++++++++++++++++++++++++++++++++++++++
 lib/rsa/rsa-verify.c         | 307 +++++-------------------------------------
 tools/Makefile               |   2 +-
 8 files changed, 399 insertions(+), 278 deletions(-)
 create mode 100644 include/u-boot/rsa-mod-exp.h
 create mode 100644 lib/rsa/rsa-mod-exp.c
Kconfig +2 -0
index 153ee2b..82b14bd 100644
@@ -101,6 +101,7 @@ config TPL
 	help
 	  If you want to build TPL as well as the normal image and SPL, say Y.
 
+
 config FIT
 	bool "Support Flattened Image Tree"
 	depends on !SPL_BUILD
@@ -118,6 +119,7 @@ config FIT_VERBOSE
 config FIT_SIGNATURE
 	bool "Enabel 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.
include/config_fallbacks.h +8 -0
index ddfe045..b46bef9 100644
@@ -83,6 +83,14 @@
 #define CONFIG_SYS_PBSIZE	(CONFIG_SYS_CBSIZE + 128)
 #endif
 
+/* This can be removed once all platforms move to defconfig 
+ * option to enable FIT_SIGNATURE 
+ */
+#if defined(CONFIG_FIT_SIGNATURE) && defined(CONFIG_RSA) && \
+	!defined(CONFIG_RSA_MOD_EXP_HW) && !defined(CONFIG_RSA_MOD_EXP_SW)
+#define CONFIG_RSA_MOD_EXP_SW
+#endif
+
 #ifndef CONFIG_FIT_SIGNATURE
 #define CONFIG_IMAGE_FORMAT_LEGACY
 #endif
include/u-boot/rsa-mod-exp.h new file +34 -0
new file mode 100644
index 0000000..0d2ccd6
@@ -0,0 +1,34 @@
+/*
+ * 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 */
+};
+
+int rsa_mod_exp(const uint8_t *sig, uint32_t sig_len,
+		struct key_prop *node, uint8_t *out);
+
+#endif
lib/Kconfig +15 -0
index 8460439..a921bb3 100644
@@ -27,4 +27,19 @@ 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
+
+if RSA
+
+config RSA_MOD_EXP_SW
+	depends on RSA
+	bool "Implement RSA Modular Exponentiation in software"
+	default y
+	help
+	  Modular Exponentiation support in SW
+
+endif
 endmenu
lib/rsa/Makefile +1 -0
index a5a96cb6..ccc6060 100644
@@ -8,3 +8,4 @@
 #
 
 obj-$(CONFIG_FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o
+obj-$(CONFIG_RSA_MOD_EXP_SW) += rsa-mod-exp.o
lib/rsa/rsa-mod-exp.c new file +308 -0

308 lines not on this page · raw message

lib/rsa/rsa-verify.c +30 -277

355 lines not on this page · raw message

tools/Makefile +1 -1
index a4216a1..f67cd15 100644
@@ -60,7 +60,7 @@ 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
#1 lore raw

RFC PATCH v2 2/4 crypto/fsl: Add support for RSA Modular Exponentiation

Support added for offloading Modular Exponentiation required in RSA Verify functionality to hardware which depends on CONFIG_RSA_MOD_EXP_HW.

---
Changes in v2:
Added Kconfig option for RSA_MOD_HW

 drivers/crypto/fsl/Kconfig    |  5 +++++
 drivers/crypto/fsl/Makefile   |  1 +
 drivers/crypto/fsl/fsl_rsa.c  | 44 +++++++++++++++++++++++++++++++++++++++++++
 drivers/crypto/fsl/jobdesc.c  | 28 +++++++++++++++++++++++++++
 drivers/crypto/fsl/jobdesc.h  |  5 +++++
 drivers/crypto/fsl/rsa_caam.h | 27 ++++++++++++++++++++++++++
 lib/Kconfig                   | 11 +++++++++--
 7 files changed, 119 insertions(+), 2 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
drivers/crypto/fsl/Kconfig new file +5 -0
new file mode 100644
index 0000000..45e6683
@@ -0,0 +1,5 @@
+config FSL_CAAM
+	depends on CRYPTO
+	bool "Freescale Crypto Driver Support"
+	help
+	  HW Acceleration support for RSA based platforms
drivers/crypto/fsl/Makefile +1 -0
index 067d0a9..ef36f67 100644
@@ -8,4 +8,5 @@
 
 obj-y += sec.o
 obj-$(CONFIG_FSL_CAAM) += jr.o fsl_hash.o jobdesc.o error.o
+obj-$(CONFIG_RSA_MOD_EXP_HW) += fsl_rsa.o
 obj-$(CONFIG_CMD_BLOB) += fsl_blob.o
drivers/crypto/fsl/fsl_rsa.c new file +44 -0

44 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 +27 -0
new file mode 100644
index 0000000..d1dc7eb
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2014 Freescale Semiconductor, Inc.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef __RSA_SEC_H
+#define __RSA_SEC_H
+
+#include <common.h>
+
+/**
+ * struct pk_in_params - holder for input to 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
lib/Kconfig +9 -2
index a921bb3..b52f17f 100644
@@ -35,11 +35,18 @@ config RSA
 if RSA
 
 config RSA_MOD_EXP_SW
-	depends on RSA
+	depends on RSA && !RSA_MOD_EXP_HW
 	bool "Implement RSA Modular Exponentiation in software"
-	default y
+	default n
 	help
 	  Modular Exponentiation support in SW
 
+config RSA_MOD_EXP_HW
+	depends on RSA && FSL_CAAM
+	bool "Implement RSA Modular Exp using Freescale Specific Impl"
+	default y
+	help
+	  Modular Exponentiation support in HW
+
 endif
 endmenu
Signature · 1 line
1.8.1.4
#2 lore raw

RFC PATCH v2 3/4 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 used Progressive Hash API's.

---
Changes in v2 :
Added commit message

 common/hash.c  | 35 ++++++++++++++++++++++++++---------
 include/hash.h | 15 +++++++++++++++
 2 files changed, 41 insertions(+), 9 deletions(-)
common/hash.c +26 -9
index 12d6759..87263df 100644
@@ -20,7 +20,7 @@
 #include <asm/io.h>
 #include <asm/errno.h>
 
-#ifdef CONFIG_CMD_SHA1SUM
+#ifdef CONFIG_SHA256
 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
 {
 	sha1_context *ctx = malloc(sizeof(sha1_context));
@@ -125,12 +125,8 @@ 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 +136,6 @@ static struct hash_algo hash_algo[] = {
 		hash_update_sha1,
 		hash_finish_sha1,
 	},
-#define MULTI_HASH
 #endif
 #ifdef CONFIG_SHA256
 	{
@@ -152,7 +147,6 @@ static struct hash_algo hash_algo[] = {
 		hash_update_sha256,
 		hash_finish_sha256,
 	},
-#define MULTI_HASH
 #endif
 	{
 		"crc32",
@@ -165,6 +159,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 +309,25 @@ 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
#3 lore raw

RFC PATCH v2 4/4 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 support of hardware accelerated progressive hash API's once available.

---
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          |  3 ++-
 5 files changed, 60 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 +2 -1
index 583400f..b7b8933 100644
@@ -142,7 +142,8 @@ int rsa_verify(struct image_sign_info *info,
 	}
 
 	/* Calculate checksum with checksum-algorithm */
-	info->algo->checksum->calculate(region, region_count, hash);
+	info->algo->checksum->calculate(info->algo->checksum->name,
+					region, region_count, hash);
 
 	/* See if we must use a particular key */
 	if (info->required_keynode != -1) {
Signature · 1 line
1.8.1.4
#4 lore raw

RFC PATCH v2 1/4 rsa: Split the rsa-verify

In reply to Ruchika Gupta

Hi Ruchika,

Quoted 38 lines · Ruchika Gupta

Public exponentiation which is required in rsa verify functionality is currently tightly integrated with verification code in rsa_verify.c. Currently this implementation is software based. Some platforms having support of the exponentiation in hardware. To enable the rsa verify functionality to use the Modular exponentiation if present in hardware, the patch-set splits the file into two files:

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 formal (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 exponentiation from rsa-verify.c to this file. The file is compiled if "CONFIG_RSA_MOD_EXP_SW" is defined. In general if both CONFIG_FIT_SIGNATURE and CONFIG_RSA are defined, CONFIG_RSA_MOD_EXP_SW gets automatically defined.

Platforms having hardware implementation for rsa_mod_exp can add a define "CONFIG_RSA_MOD_EXP_HW" to their config files. Adding this defined, undefs the CONFIG_RSA_MOD_EXP_SW and hardware implementation of mod_exp gets compiled.

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

Quoted 12 lines
 Kconfig                      |   2 +
 include/config_fallbacks.h   |   8 ++
 include/u-boot/rsa-mod-exp.h |  34 +++++
 lib/Kconfig                  |  15 +++
 lib/rsa/Makefile             |   1 +
 lib/rsa/rsa-mod-exp.c        | 308 +++++++++++++++++++++++++++++++++++++++++++
 lib/rsa/rsa-verify.c         | 307 +++++-------------------------------------
 tools/Makefile               |   2 +-
 8 files changed, 399 insertions(+), 278 deletions(-)
 create mode 100644 include/u-boot/rsa-mod-exp.h
 create mode 100644 lib/rsa/rsa-mod-exp.c

Here are my comments on the whole series. In general the approach looks fine provided you move it to driver model in a follow-up series. See the 'thermal' uclass for a very simple example.

- Check your multi-line comment style
- Make sure you add a full help paragraph for new Kconfigs
- Avoid double blank lines
- First patch should just move code out of lib/rsa/rsa-verify.c
- Add a separate patch to move CONFIG_FIT_SIGNATURE to Kconfig for existing boards
- Make sure functions have comments describing their purpose, arguments, return value. Normally these go in the header file except for static functions
- For your header file guard #ifdefs, use a symbol that relates to the filename
- Word-wrap your commit messages to >70 chars (e.g. 76 or so)
- Make sure you check return values of functions that return errors
- Make sure the vboot tests still pass
make O=sandbox sandbox_defconfig all
O=b/sandbox ./test/vboot/vboot_test.sh
- Check your hash.c changes, I think you have the #ifdefs wrong.

Please then send a v3 without RFC and we'll try to get final things resolved.

Regards,
Simon