Skip to contentSkip to conversation

crypto: add md5 to common hash functions

PATCH New Ben Whitten 3 participants 6 messages Jun 6, 2018 – Jun 28, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 crypto: add md5 to common hash functions

The md5 function was missing from the common hash functions

---
 common/hash.c        | 8 ++++++++
 include/image.h      | 1 +
 include/u-boot/md5.h | 7 ++++---
 lib/Makefile         | 1 +
 lib/md5.c            | 4 ++--
 5 files changed, 16 insertions(+), 5 deletions(-)
common/hash.c +8 -0
index ef14651..d2f4b3f 100644
@@ -168,6 +168,14 @@ static struct hash_algo hash_algo[] = {
 		.hash_update	= hash_update_crc32,
 		.hash_finish	= hash_finish_crc32,
 	},
+#ifdef CONFIG_MD5
+	{
+		.name		= "md5",
+		.digest_size	= 16,
+		.chunk_size	= CHUNKSZ_MD5,
+		.hash_func_ws 	= md5_wd,
+	},
+#endif
 };
 
 /* Try to minimize code size for boards that don't want much hashing */
include/image.h +1 -0
index 9522ee4..a5a5807 100644
@@ -32,6 +32,7 @@ struct fdt_region;
 #define CONFIG_FIT_ENABLE_SHA256_SUPPORT
 #define CONFIG_SHA1
 #define CONFIG_SHA256
+#define CONFIG_MD5
 
 #define IMAGE_ENABLE_IGNORE	0
 #define IMAGE_INDENT_STRING	""
include/u-boot/md5.h +4 -3
index e09c16a..365d125 100644
@@ -21,14 +21,15 @@ struct MD5Context {
  * Calculate and store in 'output' the MD5 digest of 'len' bytes at
  * 'input'. 'output' must have enough space to hold 16 bytes.
  */
-void md5 (unsigned char *input, int len, unsigned char output[16]);
+
+void md5 (const unsigned char *input, unsigned int len, unsigned char *output);
 
 /*
  * Calculate and store in 'output' the MD5 digest of 'len' bytes at 'input'.
  * 'output' must have enough space to hold 16 bytes. If 'chunk' Trigger the
  * watchdog every 'chunk_sz' bytes of input processed.
  */
-void md5_wd (unsigned char *input, int len, unsigned char output[16],
-		unsigned int chunk_sz);
+void md5_wd (const unsigned char *input, unsigned int len,
+		unsigned char *output, unsigned int chunk_sz);
 
 #endif /* _MD5_H */
lib/Makefile +1 -0
index d531ea5..5c4aa73 100644
@@ -78,6 +78,7 @@ obj-y += div64.o
 obj-y += hang.o
 obj-y += linux_compat.o
 obj-y += linux_string.o
+obj-$(CONFIG_$(SPL_TPL_)MD5_SUPPORT) += md5.o
 obj-y += membuff.o
 obj-$(CONFIG_REGEX) += slre.o
 obj-y += string.o
lib/md5.c +2 -2
index 2ae4a06..2278001 100644
@@ -268,7 +268,7 @@ MD5Transform(__u32 buf[4], __u32 const in[16])
  * 'input'. 'output' must have enough space to hold 16 bytes.
  */
 void
-md5 (unsigned char *input, int len, unsigned char output[16])
+md5 (const unsigned char *input, unsigned int len, unsigned char *output)
 {
 	struct MD5Context context;
 
@@ -284,7 +284,7 @@ md5 (unsigned char *input, int len, unsigned char output[16])
  * watchdog every 'chunk_sz' bytes of input processed.
  */
 void
-md5_wd (unsigned char *input, int len, unsigned char output[16],
+md5_wd (const unsigned char *input, unsigned int len, unsigned char *output,
 	unsigned int chunk_sz)
 {
 	struct MD5Context context;
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 2/3 fit: allow fit to call hardware accelerated hash

Move to calling the abstraction which allows for hardware acceleration. We also remove unneeded defines and only include objects if required.

---
 common/hash.c      |  4 ++++
 common/image-fit.c | 27 +++++++++------------------
 include/image.h    | 42 ++++++++++++++++++++++--------------------
 lib/Makefile       |  5 ++++-
 4 files changed, 39 insertions(+), 39 deletions(-)
common/hash.c +4 -0
index d2f4b3f..ceee124 100644
@@ -85,6 +85,7 @@ static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
 }
 #endif
 
+#if defined(CONFIG_CRC32)
 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
 {
 	uint32_t *ctx = malloc(sizeof(uint32_t));
@@ -110,6 +111,7 @@ static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
 	free(ctx);
 	return 0;
 }
+#endif
 
 /*
  * These are the hash algorithms we support.  If we have hardware acceleration
@@ -159,6 +161,7 @@ static struct hash_algo hash_algo[] = {
 #endif
 	},
 #endif
+#ifdef CONFIG_CRC32
 	{
 		.name		= "crc32",
 		.digest_size	= 4,
@@ -168,6 +171,7 @@ static struct hash_algo hash_algo[] = {
 		.hash_update	= hash_update_crc32,
 		.hash_finish	= hash_finish_crc32,
 	},
+#endif
 #ifdef CONFIG_MD5
 	{
 		.name		= "md5",
common/image-fit.c +9 -18
index 8c15ed1..7d8c961 100644
@@ -1082,26 +1082,17 @@ int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
 int calculate_hash(const void *data, int data_len, const char *algo,
 			uint8_t *value, int *value_len)
 {
-	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
-		*((uint32_t *)value) = crc32_wd(0, data, data_len,
-							CHUNKSZ_CRC32);
-		*((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
-		*value_len = 4;
-	} else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
-		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 = SHA256_SUM_LEN;
-	} else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
-		md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
-		*value_len = 16;
-	} else {
+	struct hash_algo *hash_algo;
+	int ret;
+
+	ret = hash_lookup_algo(algo, &hash_algo);
+	if (ret) {
 		debug("Unsupported hash alogrithm\n");
-		return -1;
 	}
+	hash_algo->hash_func_ws((unsigned char *)data, data_len,
+			(unsigned char *)value, hash_algo->chunk_size);
+	*value_len = hash_algo->digest_size;
+
 	return 0;
 }
 
include/image.h +22 -20

66 lines not on this page · raw message

lib/Makefile +4 -1
index 5c4aa73..5b40444 100644
@@ -24,6 +24,7 @@ obj-$(CONFIG_USB_TTY) += circbuf.o
 obj-y += crc7.o
 obj-y += crc8.o
 obj-y += crc16.o
+obj-$(CONFIG_CRC32) += crc32.o
 obj-$(CONFIG_ERRNO_STR) += errno_str.o
 obj-$(CONFIG_FIT) += fdtdec_common.o
 obj-$(CONFIG_TEST_FDTDEC) += fdtdec_test.o
@@ -61,6 +62,8 @@ obj-$(CONFIG_$(SPL_TPL_)OF_CONTROL) += fdtdec_common.o
 obj-$(CONFIG_$(SPL_TPL_)OF_CONTROL) += fdtdec.o
 endif
 
+
+
 ifdef CONFIG_SPL_BUILD
 obj-$(CONFIG_SPL_YMODEM_SUPPORT) += crc16.o
 obj-$(CONFIG_SPL_NET_SUPPORT) += net_utils.o
@@ -71,7 +74,7 @@ obj-y += errno.o
 obj-y += display_options.o
 CFLAGS_display_options.o := $(if $(BUILD_TAG),-DBUILD_TAG='"$(BUILD_TAG)"')
 obj-$(CONFIG_BCH) += bch.o
-obj-y += crc32.o
+obj-$(CONFIG_$(SPL_TPL_)CRC32_SUPPORT) += crc32.o
 obj-$(CONFIG_CRC32C) += crc32c.o
 obj-y += ctype.o
 obj-y += div64.o
Signature · 1 line
2.7.4
#2 lore raw

PATCH v2 2/3 fit: allow fit to call hardware accelerated hash

Quoted 4 lines · Ben Whitten

Move to calling the abstraction which allows for hardware acceleration. We also remove unneeded defines and only include objects if required.

[snip]

Quoted 19 lines
common/image-fit.c +0 -11
index 8c15ed1..7d8c961 100644
@@ -1082,26 +1082,17 @@ int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
 int calculate_hash(const void *data, int data_len, const char *algo,
 			uint8_t *value, int *value_len)
 {
-	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
-		*((uint32_t *)value) = crc32_wd(0, data, data_len,
-							CHUNKSZ_CRC32);
-		*((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
-		*value_len = 4;
-	} else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
-		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,

common/image-fit.c int fit_set_timestamp(void *fit, int noffset, time_t timestamp) · line 1088

Quoted 10 lines
common/image-fit.c +4 -6
-			       (unsigned char *)value, CHUNKSZ_SHA256);
-		*value_len = SHA256_SUM_LEN;
-	} else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
-		md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
-		*value_len = 16;
-	} else {
+	struct hash_algo *hash_algo;
+	int ret;
+
+	ret = hash_lookup_algo(algo, &hash_algo);

This is unfortunately not code equivalent. There's a little bit missing to switch this over to using SPL_CRC32_SUPPORT as well. A good test would be to make sure that a sunxi platform still builds after this (it doesn't currently with the series) and use buildman -Ssdel to compare before/after sizes and make sure it's a tiny change in size related to the code switch and not a huge change due to droppped (or added!) support. Thanks!

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

PATCH v2 2/3 fit: allow fit to call hardware accelerated hash

Hi Tom,

Quoted 35 lines

Subject: Re: [U-Boot] [PATCH v2 2/3] fit: allow fit to call hardware accelerated hash

Quoted 4 lines · Ben Whitten

Move to calling the abstraction which allows for hardware acceleration. We also remove unneeded defines and only include objects if required.

[snip]

common/image-fit.c int fit_set_timestamp(void *fit, int noffset,

Quoted 5 lines
common/image-fit.c +0 -0
index 8c15ed1..7d8c961 100644
@@ -1082,26 +1082,17 @@ int fit_set_timestamp(void *fit, int noffset,

time_t timestamp)

Quoted 8 lines
common/image-fit.c +0 -5
 int calculate_hash(const void *data, int data_len, const char *algo,
 			uint8_t *value, int *value_len)
 {
-	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
-		*((uint32_t *)value) = crc32_wd(0, data, data_len,
-							CHUNKSZ_CRC32);
-		*((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
-		*value_len = 4;

common/image-fit.c int fit_set_timestamp(void *fit, int noffset, time_t timestamp)

Quoted 10 lines
common/image-fit.c +0 -10
-	} else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
-		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 = SHA256_SUM_LEN;
-	} else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
-		md5_wd((unsigned char *)data, data_len, value,

CHUNKSZ_MD5);

Quoted 14 lines

common/image-fit.c int fit_set_timestamp(void *fit, int noffset, time_t timestamp) · line 1088

Quoted 6 lines
common/image-fit.c +4 -2
-		*value_len = 16;
-	} else {
+	struct hash_algo *hash_algo;
+	int ret;
+
+	ret = hash_lookup_algo(algo, &hash_algo);

This is unfortunately not code equivalent. There's a little bit missing to switch this over to using SPL_CRC32_SUPPORT as well. A good test would be to make sure that a sunxi platform still builds after this (it doesn't currently with the series) and use buildman -Ssdel to compare before/after sizes and make sure it's a tiny change in size related to the code switch and not a huge change due to droppped (or added!) support. Thanks!

I see that I should be including the crc32.o file regardless in non SPL builds, now the sunxi board build passes locally.
I tested a fit image with all hashes enabled and they all passed so atleast the calling of the functions seems to work. Did you spot another inequivalence? I am un-familiar with the buildman tool, but I shall give it a go as you suggest.

Thanks,
Ben

#4 lore raw

PATCH v2 2/3 fit: allow fit to call hardware accelerated hash

Quoted 52 lines · Ben Whitten

Hi Tom,

Quoted 35 lines

Subject: Re: [U-Boot] [PATCH v2 2/3] fit: allow fit to call hardware accelerated hash

Quoted 4 lines · Ben Whitten

Move to calling the abstraction which allows for hardware acceleration. We also remove unneeded defines and only include objects if required.

[snip]

common/image-fit.c int fit_set_timestamp(void *fit, int noffset,

Quoted 5 lines
common/image-fit.c +0 -0
index 8c15ed1..7d8c961 100644
@@ -1082,26 +1082,17 @@ int fit_set_timestamp(void *fit, int noffset,

time_t timestamp)

Quoted 8 lines
common/image-fit.c +0 -5
 int calculate_hash(const void *data, int data_len, const char *algo,
 			uint8_t *value, int *value_len)
 {
-	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
-		*((uint32_t *)value) = crc32_wd(0, data, data_len,
-							CHUNKSZ_CRC32);
-		*((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
-		*value_len = 4;

common/image-fit.c int fit_set_timestamp(void *fit, int noffset, time_t timestamp)

Quoted 10 lines
common/image-fit.c +0 -10
-	} else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
-		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 = SHA256_SUM_LEN;
-	} else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
-		md5_wd((unsigned char *)data, data_len, value,

CHUNKSZ_MD5);

Quoted 14 lines

common/image-fit.c int fit_set_timestamp(void *fit, int noffset, time_t timestamp) · line 1088

Quoted 6 lines
common/image-fit.c +4 -2
-		*value_len = 16;
-	} else {
+	struct hash_algo *hash_algo;
+	int ret;
+
+	ret = hash_lookup_algo(algo, &hash_algo);

This is unfortunately not code equivalent. There's a little bit missing to switch this over to using SPL_CRC32_SUPPORT as well. A good test would be to make sure that a sunxi platform still builds after this (it doesn't currently with the series) and use buildman -Ssdel to compare before/after sizes and make sure it's a tiny change in size related to the code switch and not a huge change due to droppped (or added!) support. Thanks!

Quoted 5 lines

I see that I should be including the crc32.o file regardless in non SPL builds, now the sunxi board build passes locally.
I tested a fit image with all hashes enabled and they all passed so atleast the calling of the functions seems to work. Did you spot another inequivalence? I am un-familiar with the buildman tool, but I shall give it a go as you suggest.

If the size tests above are pretty-close (as in, you can see it's just a size change due to code change and not due to now including/excluding an algorithm), I think we're there. Thanks again!

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

PATCH v2 3/3 crypto: add Atmel hardware acceleration for SHA1 & 256

In reply to Ben Whitten

We can use the hardware hash block to reduce space, particularly useful for verifying FIT signatures from SPL.

---
 drivers/crypto/Kconfig     |   5 +
 drivers/crypto/Makefile    |   1 +
 drivers/crypto/atmel_sha.c | 289 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/crypto/atmel_sha.h |  52 ++++++++
 lib/Makefile               |   2 +
 5 files changed, 349 insertions(+)
 create mode 100644 drivers/crypto/atmel_sha.c
 create mode 100644 drivers/crypto/atmel_sha.h
drivers/crypto/Kconfig +5 -0
index 1ea116b..7a20edb 100644
@@ -2,4 +2,9 @@ menu "Hardware crypto devices"
 
 source drivers/crypto/fsl/Kconfig
 
+config ATMEL_SHA
+	bool "Atmel SHA Driver support"
+	help
+	  Enables the Atmel SHA accelerator.
+
 endmenu
drivers/crypto/Makefile +1 -0
index efbd1d3..07af449 100644
@@ -4,5 +4,6 @@
 # 	http://www.samsung.com
 
 obj-$(CONFIG_EXYNOS_ACE_SHA)	+= ace_sha.o
+obj-$(CONFIG_ATMEL_SHA)	+= atmel_sha.o
 obj-y += rsa_mod_exp/
 obj-y += fsl/
drivers/crypto/atmel_sha.c new file +289 -0

289 lines not on this page · raw message

drivers/crypto/atmel_sha.h new file +52 -0

52 lines not on this page · raw message

lib/Makefile +2 -0
index 5b40444..834826c 100644
@@ -47,8 +47,10 @@ obj-y += list_sort.o
 endif
 
 obj-$(CONFIG_RSA) += rsa/
+ifneq ($(CONFIG_SHA_PROG_HW_ACCEL),y)
 obj-$(CONFIG_SHA1) += sha1.o
 obj-$(CONFIG_SHA256) += sha256.o
+endif
 
 obj-$(CONFIG_$(SPL_)ZLIB) += zlib/
 obj-$(CONFIG_$(SPL_)GZIP) += gunzip.o
Signature · 1 line
2.7.4