Skip to contentSkip to conversation

fit: Add support for SHA256 hash

PATCH New Marek Vasut 4 participants 13 messages Feb 6, 2014 – Mar 5, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 fit: Add support for SHA256 hash

This patch adds support for SHA-256 hash into the FIT image. The usage is as with the other hashing algorithms:

"

	hash at 1 {
		algo = "sha256";
	};

"

---
 common/image-fit.c |  5 +++++
 include/image.h    | 15 ++++++++++++++-
 tools/Makefile     |  2 ++
 3 files changed, 21 insertions(+), 1 deletion(-)
common/image-fit.c +5 -0
index cf4b67e..a7ecf8b 100644
@@ -22,6 +22,7 @@ DECLARE_GLOBAL_DATA_PTR;
 
 #include <bootstage.h>
 #include <sha1.h>
+#include <sha256.h>
 #include <u-boot/crc.h>
 #include <u-boot/md5.h>
 
@@ -882,6 +883,10 @@ int calculate_hash(const void *data, int data_len, const char *algo,
 		sha1_csum_wd((unsigned char *)data, data_len,
 			     (unsigned char *)value, CHUNKSZ_SHA1);
 		*value_len = 20;
+	} else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
+		sha256_csum_wd((unsigned char *)data, data_len,
+			     (unsigned char *)value, CHUNKSZ_SHA256);
+		*value_len = 32;
 	} else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
 		md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
 		*value_len = 16;
include/image.h +14 -1
index 7de2bb2..e5c76e7 100644
@@ -57,13 +57,18 @@ struct lmb;
 #  ifdef CONFIG_SPL_SHA1_SUPPORT
 #   define IMAGE_ENABLE_SHA1	1
 #  endif
+#  ifdef CONFIG_SPL_SHA256_SUPPORT
+#   define IMAGE_ENABLE_SHA256	1
+#  endif
 # else
 #  define CONFIG_CRC32		/* FIT images need CRC32 support */
 #  define CONFIG_MD5		/* and MD5 */
 #  define CONFIG_SHA1		/* and SHA1 */
+#  define CONFIG_SHA256		/* and SHA256 */
 #  define IMAGE_ENABLE_CRC32	1
 #  define IMAGE_ENABLE_MD5	1
 #  define IMAGE_ENABLE_SHA1	1
+#  define IMAGE_ENABLE_SHA256	1
 # endif
 
 #ifndef IMAGE_ENABLE_CRC32
@@ -78,6 +83,10 @@ struct lmb;
 #define IMAGE_ENABLE_SHA1	0
 #endif
 
+#ifndef IMAGE_ENABLE_SHA256
+#define IMAGE_ENABLE_SHA256	0
+#endif
+
 #endif /* CONFIG_FIT */
 
 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
@@ -345,6 +354,10 @@ extern bootm_headers_t images;
 #define CHUNKSZ_SHA1 (64 * 1024)
 #endif
 
+#ifndef CHUNKSZ_SHA256
+#define CHUNKSZ_SHA256 (64 * 1024)
+#endif
+
 #define uimage_to_cpu(x)		be32_to_cpu(x)
 #define cpu_to_uimage(x)		cpu_to_be32(x)
 
@@ -691,7 +704,7 @@ int bootz_setup(ulong image, ulong *start, ulong *end);
 #define FIT_FDT_PROP		"fdt"
 #define FIT_DEFAULT_PROP	"default"
 
-#define FIT_MAX_HASH_LEN	20	/* max(crc32_len(4), sha1_len(20)) */
+#define FIT_MAX_HASH_LEN	32	/* max(crc32_len(4), sha1_len(20), sha256_len(32)) */
 
 /* cmdline argument format parsing */
 int fit_parse_conf(const char *spec, ulong addr_curr,
tools/Makefile +2 -0
index 328cea3..e025004 100644
@@ -71,6 +71,7 @@ EXT_OBJ_FILES-y += common/image-sig.o
 EXT_OBJ_FILES-y += lib/crc32.o
 EXT_OBJ_FILES-y += lib/md5.o
 EXT_OBJ_FILES-y += lib/sha1.o
+EXT_OBJ_FILES-y += lib/sha256.o
 
 # Source files located in the tools directory
 NOPED_OBJ_FILES-y += aisimage.o
@@ -252,6 +253,7 @@ $(obj)mkimage$(SFX):	$(obj)aisimage.o \
 			$(obj)os_support.o \
 			$(obj)pblimage.o \
 			$(obj)sha1.o \
+			$(obj)sha256.o \
 			$(obj)ublimage.o \
 			$(LIBFDT_OBJS) \
 			$(RSA_OBJS)
Signature · 1 line
1.8.5.3
#1 lore raw

PATCH 2/3 fit: rsa: Add groundwork to support other hashes

Separate out the SHA1 code from the rsa-sign.c and rsa-verify.c . Each file now has a function which does the correct hashing operation instead of having the SHA-1 hashing operation hard-coded in the rest of the code. This makes adding a new hashing operating much easier and cleaner.

---
 lib/rsa/rsa-sign.c   | 45 ++++++++++++++++++++++++--
 lib/rsa/rsa-verify.c | 89 +++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 110 insertions(+), 24 deletions(-)
lib/rsa/rsa-sign.c +42 -3
index 549130e..4e11720 100644
@@ -15,6 +15,11 @@
 #include <openssl/ssl.h>
 #include <openssl/evp.h>
 
+enum rsa_hash_type {
+	RSA_HASH_SHA1,
+	RSA_HASH_UNKNOWN,
+};
+
 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
 #define HAVE_ERR_REMOVE_THREAD_STATE
 #endif
@@ -159,7 +164,19 @@ static void rsa_remove(void)
 	EVP_cleanup();
 }
 
-static int rsa_sign_with_key(RSA *rsa, const struct image_region region[],
+static const EVP_MD *rsa_sign_get_hash(enum rsa_hash_type hash)
+{
+	switch (hash) {
+	case RSA_HASH_SHA1:
+		return EVP_sha1();
+	default:	/* This must never happen. */
+		rsa_err("Invalid hash type!\n");
+		exit(1);
+	};
+}
+
+static int rsa_sign_with_key(RSA *rsa, enum rsa_hash_type hash,
+		const struct image_region region[],
 		int region_count, uint8_t **sigp, uint *sig_size)
 {
 	EVP_PKEY *key;
@@ -192,7 +209,7 @@ static int rsa_sign_with_key(RSA *rsa, const struct image_region region[],
 		goto err_create;
 	}
 	EVP_MD_CTX_init(context);
-	if (!EVP_SignInit(context, EVP_sha1())) {
+	if (!EVP_SignInit(context, rsa_sign_get_hash(hash))) {
 		ret = rsa_err("Signer setup failed");
 		goto err_sign;
 	}
@@ -228,12 +245,34 @@ err_set:
 	return ret;
 }
 
+static enum rsa_hash_type rsa_get_sha_type(struct image_sign_info *info)
+{
+	char *pos;
+	unsigned int hash_str_len;
+
+	pos = strstr(info->algo->name, ",");
+	if (!pos)
+		return -EINVAL;
+
+	hash_str_len = pos - info->algo->name;
+
+	if (!strncmp(info->algo->name, "sha1", hash_str_len))
+		return RSA_HASH_SHA1;
+	else
+		return RSA_HASH_UNKNOWN;
+}
+
 int rsa_sign(struct image_sign_info *info,
 	     const struct image_region region[], int region_count,
 	     uint8_t **sigp, uint *sig_len)
 {
 	RSA *rsa;
 	int ret;
+	enum rsa_hash_type hash;
+
+	hash = rsa_get_sha_type(info);
+	if (hash == RSA_HASH_UNKNOWN)
+		return -EINVAL;
 
 	ret = rsa_init();
 	if (ret)
@@ -242,7 +281,7 @@ int rsa_sign(struct image_sign_info *info,
 	ret = rsa_get_priv_key(info->keydir, info->keyname, &rsa);
 	if (ret)
 		goto err_priv;
-	ret = rsa_sign_with_key(rsa, region, region_count, sigp, sig_len);
+	ret = rsa_sign_with_key(rsa, hash, region, region_count, sigp, sig_len);
 	if (ret)
 		goto err_sign;
 
lib/rsa/rsa-verify.c +68 -21

161 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#2 lore raw

PATCH 2/3 fit: rsa: Add groundwork to support other hashes

Dear Marek,

Quoted 5 lines · In message <[email protected]> you wrote:

Separate out the SHA1 code from the rsa-sign.c and rsa-verify.c . Each file now has a function which does the correct hashing operation instead of having the SHA-1 hashing operation hard-coded in the rest of the code. This makes adding a new hashing operating much easier and cleaner.

...

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

Quoted 2 lines
lib/rsa/rsa-verify.c +1 -1
-						      noffset);
+						      noffset, padding, pad_len);

Line too long.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
It is easier to write an incorrect program than understand a  correct
one.
#3 lore raw

PATCH 2/3 fit: rsa: Add groundwork to support other hashes

Quoted 9 lines · Wolfgang Denk

Dear Marek,

Quoted 5 lines · In message <[email protected]> you wrote:

Separate out the SHA1 code from the rsa-sign.c and rsa-verify.c . Each file now has a function which does the correct hashing operation instead of having the SHA-1 hashing operation hard-coded in the rest of the code. This makes adding a new hashing operating much easier and cleaner.

Quoted 4 lines

...

Review

Quoted 2 lines
(quoted diff) +1 -1
-						      noffset);
+						      noffset, padding, 

pad_len);

Quoted 2 lines

Line too long.

I will need to cross-correlate this with Heiko's efforts, so there'll be V2 of either mine or his stuff.

Thanks for the review though.

Best regards,
Marek Vasut

#4 lore raw

PATCH 3/3 fit: rsa: Add support for SHA256 hash

In reply to Marek Vasut

Add support for "sha256,rsa2048" signature. This patch utilises the previously laid groundwork for adding other hashes.

---
 common/image-sig.c   |  8 +++++++-
 lib/rsa/rsa-sign.c   |  5 +++++
 lib/rsa/rsa-verify.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+), 1 deletion(-)
common/image-sig.c +7 -1
index 973b06d..c3d63bc 100644
@@ -23,7 +23,13 @@ struct image_sig_algo image_sig_algos[] = {
 		rsa_sign,
 		rsa_add_verify_data,
 		rsa_verify,
-	}
+	},
+	{
+		"sha256,rsa2048",
+		rsa_sign,
+		rsa_add_verify_data,
+		rsa_verify,
+	},
 };
 
 struct image_sig_algo *image_get_sig_algo(const char *name)
lib/rsa/rsa-sign.c +5 -0
index 4e11720..f1167b1 100644
@@ -17,6 +17,7 @@
 
 enum rsa_hash_type {
 	RSA_HASH_SHA1,
+	RSA_HASH_SHA256,
 	RSA_HASH_UNKNOWN,
 };
 
@@ -169,6 +170,8 @@ static const EVP_MD *rsa_sign_get_hash(enum rsa_hash_type hash)
 	switch (hash) {
 	case RSA_HASH_SHA1:
 		return EVP_sha1();
+	case RSA_HASH_SHA256:
+		return EVP_sha256();
 	default:	/* This must never happen. */
 		rsa_err("Invalid hash type!\n");
 		exit(1);
@@ -258,6 +261,8 @@ static enum rsa_hash_type rsa_get_sha_type(struct image_sign_info *info)
 
 	if (!strncmp(info->algo->name, "sha1", hash_str_len))
 		return RSA_HASH_SHA1;
+	else if (!strncmp(info->algo->name, "sha256", hash_str_len))
+		return RSA_HASH_SHA256;
 	else
 		return RSA_HASH_UNKNOWN;
 }
lib/rsa/rsa-verify.c +58 -0

82 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#5 lore raw

PATCH 3/3 fit: rsa: Add support for SHA256 hash

Hi Marek,

Quoted 4 lines · Marek Vasut

Add support for "sha256,rsa2048" signature. This patch utilises the previously laid groundwork for adding other hashes.

Does this conflict with Heiko's patch or is it the same?

Regards,
Simon

#6 lore raw

PATCH 3/3 fit: rsa: Add support for SHA256 hash

Quoted 9 lines · Simon Glass

Hi Marek,

Quoted 4 lines · Marek Vasut

Add support for "sha256,rsa2048" signature. This patch utilises the previously laid groundwork for adding other hashes.

Does this conflict with Heiko's patch or is it the same?

Heiko's patchset is superior, so I drop this one please.

Best regards,
Marek Vasut

#7 lore raw

PATCH 1/3 fit: Add support for SHA256 hash

In reply to Marek Vasut

Hello Marek,

Am 06.02.2014 04:47, schrieb Marek Vasut:

Quoted 7 lines

This patch adds support for SHA-256 hash into the FIT image. The usage is as with the other hashing algorithms:

"

	hash at 1 {
		algo = "sha256";
	};
Quoted 8 lines

"

---
  common/image-fit.c |  5 +++++
  include/image.h    | 15 ++++++++++++++-
  tools/Makefile     |  2 ++
  3 files changed, 21 insertions(+), 1 deletion(-)

seems I posted similiar patches ... you find them here:

[U-Boot,1/7] tools/image-host: fix sign-images bug
http://patchwork.ozlabs.org/patch/314125/

[U-Boot,2/7] fdt: add "fdt sign" command
http://patchwork.ozlabs.org/patch/314120/

[U-Boot,3/7] fit: add sha256 support
http://patchwork.ozlabs.org/patch/314126/

[U-Boot,4/7] rsa: add sha256-rsa2048 algorithm
http://patchwork.ozlabs.org/patch/314124/

[U-Boot,5/7] rsa: add sha256,rsa4096 algorithm
http://patchwork.ozlabs.org/patch/314121/

I reworked the comments, except one is missing, and I can post "v2" Maybe you can try this patches?

bye,
Heiko

Signature · 2 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
#8 lore raw

PATCH 1/3 fit: Add support for SHA256 hash

Quoted 19 lines · Heiko Schocher

Hello Marek,

Am 06.02.2014 04:47, schrieb Marek Vasut:

Quoted 16 lines

This patch adds support for SHA-256 hash into the FIT image. The usage is as with the other hashing algorithms:

"

	hash at 1 {
	
		algo = "sha256";
	
	};

"

---

Quoted 6 lines
Quoted 4 lines
  common/image-fit.c |  5 +++++
  include/image.h    | 15 ++++++++++++++-
  tools/Makefile     |  2 ++
  3 files changed, 21 insertions(+), 1 deletion(-)

seems I posted similiar patches ... you find them here:

Nice, thanks for bringing this up. Please review my series and check if there's possibly something interesting in that you might pull out into yours.

Otherwise, I'm all for applying your , since you also added rsa4096.

Best regards,
Marek Vasut

#9 lore raw

PATCH 1/3 fit: Add support for SHA256 hash

Hello Marek,

Am 08.02.2014 15:18, schrieb Marek Vasut:

Quoted 25 lines · Heiko Schocher
Quoted 24 lines

Hello Marek,

Am 06.02.2014 04:47, schrieb Marek Vasut:

Quoted 20 lines

This patch adds support for SHA-256 hash into the FIT image. The usage is as with the other hashing algorithms:

"

	hash at 1 {
	
		algo = "sha256";
	
	};

"

---

   common/image-fit.c |  5 +++++
   include/image.h    | 15 ++++++++++++++-
   tools/Makefile     |  2 ++
   3 files changed, 21 insertions(+), 1 deletion(-)
Quoted 4 lines
Quoted 1 line

seems I posted similiar patches ... you find them here:

Nice, thanks for bringing this up. Please review my series and check if there's possibly something interesting in that you might pull out into yours.

I think, all your changes are also in my patchseries ...

Quoted 1 line

Otherwise, I'm all for applying your , since you also added rsa4096.

bye,
Heiko

Signature · 2 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
#10 lore raw

PATCH 1/3 fit: Add support for SHA256 hash

Quoted 28 lines · Heiko Schocher

Hello Marek,

Am 08.02.2014 15:18, schrieb Marek Vasut:

Quoted 25 lines · Heiko Schocher
Quoted 24 lines

Hello Marek,

Am 06.02.2014 04:47, schrieb Marek Vasut:

Quoted 20 lines

This patch adds support for SHA-256 hash into the FIT image. The usage is as with the other hashing algorithms:

"

	hash at 1 {
	
		algo = "sha256";
	
	};

"

---

   common/image-fit.c |  5 +++++
   include/image.h    | 15 ++++++++++++++-
   tools/Makefile     |  2 ++
   3 files changed, 21 insertions(+), 1 deletion(-)
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

seems I posted similiar patches ... you find them here:

Nice, thanks for bringing this up. Please review my series and check if there's possibly something interesting in that you might pull out into yours.

I think, all your changes are also in my patchseries ...

OK, thanks!

Best regards,
Marek Vasut

#11 lore raw

PATCH 1/3 fit: Add support for SHA256 hash

In reply to Marek Vasut

Dear Marek,

Quoted 2 lines · In message <[email protected]> you wrote:

This patch adds support for SHA-256 hash into the FIT image. The usage is as with the other hashing algorithms:

...

include/image.h int bootz_setup(ulong image, ulong *start, ulong *end); · line 707

Quoted 2 lines
include/image.h +1 -1
-#define FIT_MAX_HASH_LEN	20	/* max(crc32_len(4), sha1_len(20)) */
+#define FIT_MAX_HASH_LEN	32	/* max(crc32_len(4), sha1_len(20), sha256_len(32)) */

Line too long.

Please make sure to run your patches through checkpatch !

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
I have made mistakes, but have never made the mistake of  claiming  I
never made one.                                     - James G. Bennet
#12 lore raw

PATCH 1/3 fit: Add support for SHA256 hash

Quoted 8 lines · Wolfgang Denk

Dear Marek,

Quoted 3 lines · In message <[email protected]> you wrote:

This patch adds support for SHA-256 hash into the FIT image. The usage is

as with the other hashing algorithms:

...

Quoted 7 lines
Quoted 3 lines
(quoted diff) +1 -1
-#define FIT_MAX_HASH_LEN	20	/* max(crc32_len(4), sha1_len(20)) */
+#define FIT_MAX_HASH_LEN	32	/* max(crc32_len(4), sha1_len(20),

sha256_len(32)) */

Line too long.

Please make sure to run your patches through checkpatch !

This is weird, since all my patches should be checked upon 'git commit' via hook. Thanks for bringing this up to my attention, I will verify that.

Nonetheless, I would vouch for applying Heiko's patches instead.

Best regards,
Marek Vasut