Skip to contentSkip to conversation

crypto: Add new UCLASS_HASH

PATCH In review Chia-Wei Wang 4 participants 22 messages Jul 30, 2021 – Sep 27, 2021

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

cover crypto: Add new UCLASS_HASH

This patch series proposes new UCLASS_HASH for hash devices. Thus the hash drivers (SW or HW-accelerated) can be developed in the DM-based fashion.

A purely software implemented hash driver is also added under the newly added UCLASS_HASH uclass. In addition, the FIT image hash verification is also updated to leverage the UCLASS_HASH driver if configured.

As there is widly spread use of non-DM hash functions (common/hash.c), this patch does not remove them. More patches are needed if UCLASS_HASH is established.

Chia-Wei Wang (4):
lib/md5: Export progressive APIs
dm: hash: Add new UCLASS_HASH support
crypto: hash: Add software hash DM driver
fit: Use DM hash driver if supported

 common/image-fit.c                |  30 +++
 drivers/crypto/Kconfig            |   2 +
 drivers/crypto/Makefile           |   1 +
 drivers/crypto/hash/Kconfig       |  16 ++
 drivers/crypto/hash/Makefile      |   6 +
 drivers/crypto/hash/hash-uclass.c | 121 ++++++++++++
 drivers/crypto/hash/hash_sw.c     | 301 ++++++++++++++++++++++++++++++
 include/dm/uclass-id.h            |   1 +
 include/u-boot/hash.h             |  61 ++++++
 include/u-boot/md5.h              |   4 +
 lib/md5.c                         |   6 +-
 11 files changed, 546 insertions(+), 3 deletions(-)
 create mode 100644 drivers/crypto/hash/Kconfig
 create mode 100644 drivers/crypto/hash/Makefile
 create mode 100644 drivers/crypto/hash/hash-uclass.c
 create mode 100644 drivers/crypto/hash/hash_sw.c
 create mode 100644 include/u-boot/hash.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/4 lib/md5: Export progressive APIs

Export the MD5 hash init/update/finish progressive APIs for better flexibility.

---
 include/u-boot/md5.h | 4 ++++
 lib/md5.c            | 6 +++---
 2 files changed, 7 insertions(+), 3 deletions(-)
include/u-boot/md5.h +4 -0
index e09c16a6e3..e5cb923d77 100644
@@ -17,6 +17,10 @@ struct MD5Context {
 	};
 };
 
+void MD5Init(struct MD5Context *ctx);
+void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len);
+void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
+
 /*
  * Calculate and store in 'output' the MD5 digest of 'len' bytes at
  * 'input'. 'output' must have enough space to hold 16 bytes.
lib/md5.c +3 -3
index 2ae4a06319..688b7254c6 100644
@@ -55,7 +55,7 @@ byteReverse(unsigned char *buf, unsigned longs)
  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
  * initialization constants.
  */
-static void
+void
 MD5Init(struct MD5Context *ctx)
 {
 	ctx->buf[0] = 0x67452301;
@@ -71,7 +71,7 @@ MD5Init(struct MD5Context *ctx)
  * Update context to reflect the concatenation of another buffer full
  * of bytes.
  */
-static void
+void
 MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
 {
 	register __u32 t;
@@ -120,7 +120,7 @@ MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
  * Final wrapup - pad to 64-byte boundary with the bit pattern
  * 1 0* (64-bit count of bits processed, MSB-first)
  */
-static void
+void
 MD5Final(unsigned char digest[16], struct MD5Context *ctx)
 {
 	unsigned int count;
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 1/4] lib/md5: Export progressive APIs

Quoted 4 lines · Chia-Wei Wang

Export the MD5 hash init/update/finish progressive APIs for better flexibility.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#3 lore raw

PATCH 2/4 dm: hash: Add new UCLASS_HASH support

In reply to Chia-Wei Wang

Add UCLASS_HASH for hash driver development. Thus the hash drivers (SW or HW-accelerated) can be developed in the DM-based fashion.

---
 drivers/crypto/Kconfig            |   2 +
 drivers/crypto/Makefile           |   1 +
 drivers/crypto/hash/Kconfig       |   5 ++
 drivers/crypto/hash/Makefile      |   5 ++
 drivers/crypto/hash/hash-uclass.c | 121 ++++++++++++++++++++++++++++++
 include/dm/uclass-id.h            |   1 +
 include/u-boot/hash.h             |  61 +++++++++++++++
 7 files changed, 196 insertions(+)
 create mode 100644 drivers/crypto/hash/Kconfig
 create mode 100644 drivers/crypto/hash/Makefile
 create mode 100644 drivers/crypto/hash/hash-uclass.c
 create mode 100644 include/u-boot/hash.h
drivers/crypto/Kconfig +2 -0
index 1ea116be75..0082177c21 100644
@@ -1,5 +1,7 @@
 menu "Hardware crypto devices"
 
+source drivers/crypto/hash/Kconfig
+
 source drivers/crypto/fsl/Kconfig
 
 endmenu
drivers/crypto/Makefile +1 -0
index efbd1d3fca..4a12b56be6 100644
@@ -6,3 +6,4 @@
 obj-$(CONFIG_EXYNOS_ACE_SHA)	+= ace_sha.o
 obj-y += rsa_mod_exp/
 obj-y += fsl/
+obj-y += hash/
drivers/crypto/hash/Kconfig new file +5 -0
new file mode 100644
index 0000000000..e226144b9b
@@ -0,0 +1,5 @@
+config DM_HASH
+	bool "Enable Driver Model for Hash"
+	depends on DM
+	help
+	  If you want to use driver model for Hash, say Y.
drivers/crypto/hash/Makefile new file +5 -0
new file mode 100644
index 0000000000..83acf3d47b
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2021 ASPEED Technology Inc.
+
+obj-$(CONFIG_DM_HASH) += hash-uclass.o
drivers/crypto/hash/hash-uclass.c new file +121 -0

121 lines not on this page · raw message

include/dm/uclass-id.h +1 -0
index 9d474533ba..a2c05ae99e 100644
@@ -53,6 +53,7 @@ enum uclass_id {
 	UCLASS_FIRMWARE,	/* Firmware */
 	UCLASS_FS_FIRMWARE_LOADER,		/* Generic loader */
 	UCLASS_GPIO,		/* Bank of general-purpose I/O pins */
+	UCLASS_HASH,		/* Hash device */
 	UCLASS_HWSPINLOCK,	/* Hardware semaphores */
 	UCLASS_I2C,		/* I2C bus */
 	UCLASS_I2C_EEPROM,	/* I2C EEPROM device */
include/u-boot/hash.h new file +61 -0

61 lines not on this page · raw message

Signature · 1 line
2.17.1
#4 lore raw

Re: [PATCH 2/4] dm: hash: Add new UCLASS_HASH support

Quoted 5 lines · Chia-Wei Wang

Add UCLASS_HASH for hash driver development. Thus the hash drivers (SW or HW-accelerated) can be developed in the DM-based fashion.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#5 lore raw

Re: [PATCH 2/4] dm: hash: Add new UCLASS_HASH support

Hi,

Quoted 10 lines · Tom Rini
Quoted 5 lines · Chia-Wei Wang

Add UCLASS_HASH for hash driver development. Thus the hash drivers (SW or HW-accelerated) can be developed in the DM-based fashion.

Applied to u-boot/next, thanks!

Oddly enough I didn't see this patch but did see Tom's reply.

Regards,
SImon

#6 lore raw

RE: [PATCH 2/4] dm: hash: Add new UCLASS_HASH support

Hi Simon,

Quoted 9 lines

From: Simon Glass <[email protected]>
Sent: Thursday, September 23, 2021 12:19 AM

Hi,

Quoted 3 lines · Tom Rini

On Fri, Jul 30, 2021 at 09:08:03AM +0800, Chia-Wei Wang wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Add UCLASS_HASH for hash driver development. Thus the hash drivers (SW or HW-accelerated) can be developed in the DM-based fashion.

Applied to u-boot/next, thanks!

Oddly enough I didn't see this patch but did see Tom's reply.

Truly odd. You and Tom are on the '--to' list.
I also checked the content sent on U-Boot Patchwork as shown below.

---
To: <[email protected]>, <[email protected]>, <[email protected]>
Subject: [PATCH 2/4] dm: hash: Add new UCLASS_HASH support
---

Regards,
Chiawei

#7 lore raw

Re: [PATCH 2/4] dm: hash: Add new UCLASS_HASH support

Hi,

Quoted 19 lines · ChiaWei Wang

Hi Simon,

Quoted 16 lines

From: Simon Glass <[email protected]>
Sent: Thursday, September 23, 2021 12:19 AM

Hi,

Quoted 9 lines · Tom Rini
Quoted 4 lines · Chia-Wei Wang

Add UCLASS_HASH for hash driver development. Thus the hash drivers (SW or HW-accelerated) can be developed in the DM-based fashion.

Applied to u-boot/next, thanks!

Quoted 8 lines
Quoted 1 line

Oddly enough I didn't see this patch but did see Tom's reply.

Truly odd. You and Tom are on the '--to' list.
I also checked the content sent on U-Boot Patchwork as shown below.

---
To: <[email protected]>, <[email protected]>, <[email protected]>
Subject: [PATCH 2/4] dm: hash: Add new UCLASS_HASH support

Well it doesn't matter. It actually happened about 6 months ago too, I think. I don't know what causes it but I suspect a spam filter as I found a few patches in my spam folder. I'll try to train it.

Regards,
Simon

#8 lore raw

Re: [PATCH 2/4] dm: hash: Add new UCLASS_HASH support

In reply to Chia-Wei Wang

Hi,

Quoted 3 lines · Chia-Wei Wang

Add UCLASS_HASH for hash driver development. Thus the hash drivers (SW or HW-accelerated) can be developed in the DM-based fashion.

Software hashing implementations are shared tightly with host tools. With DM, there's no opportunity for code sharing with host tools. The design question that I have is "do we want to DM hashes, or do we want to DM hardware accelerators for hashes?"

I did some parallel work expose remaining hash algos via hash_lookup_algo() and hash_progressive_lookup_algo().

Quoted 20 lines
---
  drivers/crypto/Kconfig            |   2 +
  drivers/crypto/Makefile           |   1 +
  drivers/crypto/hash/Kconfig       |   5 ++
  drivers/crypto/hash/Makefile      |   5 ++
  drivers/crypto/hash/hash-uclass.c | 121 ++++++++++++++++++++++++++++++
  include/dm/uclass-id.h            |   1 +
  include/u-boot/hash.h             |  61 +++++++++++++++
  7 files changed, 196 insertions(+)

create mode 100644 drivers/crypto/hash/Kconfig create mode 100644 drivers/crypto/hash/Makefile create mode 100644 drivers/crypto/hash/hash-uclass.c create mode 100644 include/u-boot/hash.h

drivers/crypto/Kconfig +0 -0
index 1ea116be75..0082177c21 100644

drivers/crypto/Kconfig line 4

Quoted 4 lines
drivers/crypto/Kconfig +2 -0
@@ -1,5 +1,7 @@
  menu "Hardware crypto devices"
 
+source drivers/crypto/hash/Kconfig
+

Hashes are useful outside of cryptographic functions, so it seems odd to merge them in crypto. For example, CRC32 is not a hash useful in crypto, but otherwise widely used in u-boot.

[snip]

Quoted 28 lines
drivers/crypto/hash/hash-uclass.c new file +22 -0
new file mode 100644
index 0000000000..446eb9e56a
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2021 ASPEED Technology Inc.
+ * Author: ChiaWei Wang <[email protected]>
+ */
+
+#define LOG_CATEGORY UCLASS_HASH
+
+#include <common.h>
+#include <dm.h>
+#include <asm/global_data.h>
+#include <u-boot/hash.h>
+#include <errno.h>
+#include <fdtdec.h>
+#include <malloc.h>
+#include <asm/io.h>
+#include <linux/list.h>
+
+struct hash_info {
+	char *name;
+	uint32_t digest_size;
+};

drivers/crypto/hash/hash-uclass.c line 32

Quoted 10 lines
drivers/crypto/hash/hash-uclass.c new file +10 -0
+
+static const struct hash_info hash_info[HASH_ALGO_NUM] = {
+	[HASH_ALGO_CRC16_CCITT] = { "crc16-ccitt", 2 },
+	[HASH_ALGO_CRC32] = { "crc32", 4 },
+	[HASH_ALGO_MD5] = { "md5", 16 },
+	[HASH_ALGO_SHA1] = { "sha1", 20 },
+	[HASH_ALGO_SHA256] = { "sha256", 32 },
+	[HASH_ALGO_SHA384] = { "sha384", 48 },
+	[HASH_ALGO_SHA512] = { "sha512", 64},
+};

It seems a step backwards to have to enum {} our hash algos, since we already identify them by their strings (e.g. "sha256"). and then associated ops structure. The

drivers/crypto/hash/hash-uclass.c line 34

Quoted 2 lines
drivers/crypto/hash/hash-uclass.c new file +2 -0
+
+enum HASH_ALGO hash_algo_lookup_by_name(const char *name)
    string -> hash_lookup_algo() -> ops struct

Is the current way to do things. hash_algo_lookup_by_name() does the roundabout through an enum. That doesn't make sense to me.

Alex

#9 lore raw

RE: [PATCH 2/4] dm: hash: Add new UCLASS_HASH support

Hi Alex,

Quoted 9 lines

From: Alex G. <[email protected]>
Sent: Thursday, September 16, 2021 11:43 PM

Hi,

Quoted 2 lines · Chia-Wei Wang

Add UCLASS_HASH for hash driver development. Thus the hash drivers (SW or HW-accelerated) can be developed in the DM-based fashion.

Quoted 7 lines

Software hashing implementations are shared tightly with host tools. With DM, there's no opportunity for code sharing with host tools. The design question that I have is "do we want to DM hashes, or do we want to DM hardware accelerators for hashes?"

I did some parallel work expose remaining hash algos via hash_lookup_algo() and hash_progressive_lookup_algo().

DM-based approach is mainly for the U-Boot bootloader part.
A consistent, abstract interface is therefore available for vendor drivers regardless of the hashing are conducted in the SW or HW-assisted fashion. And the CONFIG_SHAxxx_HW_ACCEL/CONFIG_SHA_PROG_HW_ACCEL options can be dropped.

Most of the current DM-based, SW hash driver implementation reuses the code of hash lib. The code sharing benefit is still greatly leveraged.

Quoted 24 lines
Quoted 8 lines
---
  drivers/crypto/Kconfig            |   2 +
  drivers/crypto/Makefile           |   1 +
  drivers/crypto/hash/Kconfig       |   5 ++
  drivers/crypto/hash/Makefile      |   5 ++
  drivers/crypto/hash/hash-uclass.c | 121

++++++++++++++++++++++++++++++

Quoted 15 lines
  include/dm/uclass-id.h            |   1 +
  include/u-boot/hash.h             |  61 +++++++++++++++
  7 files changed, 196 insertions(+)

create mode 100644 drivers/crypto/hash/Kconfig create mode 100644 drivers/crypto/hash/Makefile create mode 100644 drivers/crypto/hash/hash-uclass.c create mode 100644 include/u-boot/hash.h

drivers/crypto/Kconfig index renamed from drivers/crypto/Kconfig +0 -0

1ea116be75..0082177c21 100644

drivers/crypto/Kconfig +0 -0
@@ -1,5 +1,7 @@
  menu "Hardware crypto devices"
 
Quoted 5 lines

drivers/crypto/Kconfig line 4

Quoted 2 lines
drivers/crypto/Kconfig +2 -0
+source drivers/crypto/hash/Kconfig
+

Hashes are useful outside of cryptographic functions, so it seems odd to merge them in crypto. For example, CRC32 is not a hash useful in crypto, but otherwise widely used in u-boot.

Certain systems have the hash functionality included in their crypto engine. (e.g. ARM, FSL, ASPEED, etc.) Based on this observation, the DM hash driver is placed under crypto/.
However, it is OK for me to move the hash/ out of crypto/ if a more specific place is created and preferred.

Quoted 44 lines

[snip]

Quoted 27 lines
drivers/crypto/hash/hash-uclass.c new file +20 -0
new file mode 100644
index 0000000000..446eb9e56a
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2021 ASPEED Technology Inc.
+ * Author: ChiaWei Wang <[email protected]>  */
+
+#define LOG_CATEGORY UCLASS_HASH
+
+#include <common.h>
+#include <dm.h>
+#include <asm/global_data.h>
+#include <u-boot/hash.h>
+#include <errno.h>
+#include <fdtdec.h>
+#include <malloc.h>
+#include <asm/io.h>
+#include <linux/list.h>
+
+struct hash_info {
+	char *name;
+	uint32_t digest_size;

drivers/crypto/hash/hash-uclass.c line 30

Quoted 10 lines
drivers/crypto/hash/hash-uclass.c new file +10 -0
+};
+
+static const struct hash_info hash_info[HASH_ALGO_NUM] = {
+	[HASH_ALGO_CRC16_CCITT] = { "crc16-ccitt", 2 },
+	[HASH_ALGO_CRC32] = { "crc32", 4 },
+	[HASH_ALGO_MD5] = { "md5", 16 },
+	[HASH_ALGO_SHA1] = { "sha1", 20 },
+	[HASH_ALGO_SHA256] = { "sha256", 32 },
+	[HASH_ALGO_SHA384] = { "sha384", 48 },
+	[HASH_ALGO_SHA512] = { "sha512", 64}, };

It seems a step backwards to have to enum {} our hash algos, since we already identify them by their strings (e.g. "sha256"). and then associated ops structure. The

Quoted 8 lines

drivers/crypto/hash/hash-uclass.c line 32

Quoted 2 lines
drivers/crypto/hash/hash-uclass.c new file +2 -0
+
+enum HASH_ALGO hash_algo_lookup_by_name(const char *name)
     string -> hash_lookup_algo() -> ops struct

Is the current way to do things. hash_algo_lookup_by_name() does the roundabout through an enum. That doesn't make sense to me.

The common hash-uclass.c also provides the string_to_enum conversion. Both the DM-based hash driver works on both the string-based and enum-based scenario.

Chiawei

#10 lore raw

Re: [PATCH 2/4] dm: hash: Add new UCLASS_HASH support

In reply to Alex G.

Hi,

Quoted 38 lines · Alex G.

Hi,

Quoted 3 lines · Chia-Wei Wang

Add UCLASS_HASH for hash driver development. Thus the hash drivers (SW or HW-accelerated) can be developed in the DM-based fashion.

Software hashing implementations are shared tightly with host tools. With DM, there's no opportunity for code sharing with host tools. The design question that I have is "do we want to DM hashes, or do we want to DM hardware accelerators for hashes?"

I did some parallel work expose remaining hash algos via hash_lookup_algo() and hash_progressive_lookup_algo().

Quoted 22 lines
---
  drivers/crypto/Kconfig            |   2 +
  drivers/crypto/Makefile           |   1 +
  drivers/crypto/hash/Kconfig       |   5 ++
  drivers/crypto/hash/Makefile      |   5 ++
  drivers/crypto/hash/hash-uclass.c | 121 ++++++++++++++++++++++++++++++
  include/dm/uclass-id.h            |   1 +
  include/u-boot/hash.h             |  61 +++++++++++++++
  7 files changed, 196 insertions(+)

create mode 100644 drivers/crypto/hash/Kconfig create mode 100644 drivers/crypto/hash/Makefile create mode 100644 drivers/crypto/hash/hash-uclass.c create mode 100644 include/u-boot/hash.h

drivers/crypto/Kconfig +0 -0
index 1ea116be75..0082177c21 100644
@@ -1,5 +1,7 @@
  menu "Hardware crypto devices"
 
Quoted 5 lines

drivers/crypto/Kconfig line 4

Quoted 2 lines
drivers/crypto/Kconfig +2 -0
+source drivers/crypto/hash/Kconfig
+

Hashes are useful outside of cryptographic functions, so it seems odd to merge them in crypto. For example, CRC32 is not a hash useful in crypto, but otherwise widely used in u-boot.

Are you syching to move this to drivers/hash ? I feel that hashing is close enough to crypto that it might not be worth having a new 'top-level' driver directory.

Some might say that md5 is in the same board (not useful for crypto) but it used to be. Similarly, sha1 is fading away.

Quoted 45 lines

[snip]

Quoted 28 lines
drivers/crypto/hash/hash-uclass.c new file +22 -0
new file mode 100644
index 0000000000..446eb9e56a
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2021 ASPEED Technology Inc.
+ * Author: ChiaWei Wang <[email protected]>
+ */
+
+#define LOG_CATEGORY UCLASS_HASH
+
+#include <common.h>
+#include <dm.h>
+#include <asm/global_data.h>
+#include <u-boot/hash.h>
+#include <errno.h>
+#include <fdtdec.h>
+#include <malloc.h>
+#include <asm/io.h>
+#include <linux/list.h>
+
+struct hash_info {
+     char *name;
+     uint32_t digest_size;
+};

drivers/crypto/hash/hash-uclass.c line 32

Quoted 10 lines
drivers/crypto/hash/hash-uclass.c new file +10 -0
+
+static const struct hash_info hash_info[HASH_ALGO_NUM] = {
+     [HASH_ALGO_CRC16_CCITT] = { "crc16-ccitt", 2 },
+     [HASH_ALGO_CRC32] = { "crc32", 4 },
+     [HASH_ALGO_MD5] = { "md5", 16 },
+     [HASH_ALGO_SHA1] = { "sha1", 20 },
+     [HASH_ALGO_SHA256] = { "sha256", 32 },
+     [HASH_ALGO_SHA384] = { "sha384", 48 },
+     [HASH_ALGO_SHA512] = { "sha512", 64},
+};

It seems a step backwards to have to enum {} our hash algos, since we already identify them by their strings (e.g. "sha256"). and then associated ops structure. The

Quoted 7 lines

drivers/crypto/hash/hash-uclass.c line 34

Quoted 2 lines
drivers/crypto/hash/hash-uclass.c new file +2 -0
+
+enum HASH_ALGO hash_algo_lookup_by_name(const char *name)
     string -> hash_lookup_algo() -> ops struct

Is the current way to do things. hash_algo_lookup_by_name() does the roundabout through an enum. That doesn't make sense to me.

Actually I'd like to see an enum to describe these, since looking up a string is less efficient in a bootloader. So some way of doing that seems good to me.

Of course it means we need a global list of this algos in a header file and each driver needs to indicate which one(s) it supports.

Regards,
Simon

#11 lore raw

Re: [PATCH 2/4] dm: hash: Add new UCLASS_HASH support

Quoted 10 lines · Alex G.
Quoted 9 lines · Chia-Wei Wang

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+enum HASH_ALGO hash_algo_lookup_by_name(const char *name)
      string -> hash_lookup_algo() -> ops struct

Is the current way to do things. hash_algo_lookup_by_name() does the roundabout through an enum. That doesn't make sense to me.

Quoted 6 lines

Actually I'd like to see an enum to describe these, since looking up a string is less efficient in a bootloader. So some way of doing that seems good to me.

Of course it means we need a global list of this algos in a header file and each driver needs to indicate which one(s) it supports.

Let's assume strcmp() takes a lot of efforts. For a lot of cases, the algo comes in as a string. Think about FIT and image signature verification. So we have to do the strcmp() anyway, making the enum roundabout.

#12 lore raw

Re: [PATCH 2/4] dm: hash: Add new UCLASS_HASH support

Hi Alex,

Quoted 17 lines · Alex G.
Quoted 14 lines · Alex G.
Quoted 9 lines · Chia-Wei Wang

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+enum HASH_ALGO hash_algo_lookup_by_name(const char *name)
      string -> hash_lookup_algo() -> ops struct

Is the current way to do things. hash_algo_lookup_by_name() does the roundabout through an enum. That doesn't make sense to me.

Actually I'd like to see an enum to describe these, since looking up a string is less efficient in a bootloader. So some way of doing that seems good to me.

Quoted 7 lines
Quoted 2 lines

Of course it means we need a global list of this algos in a header file and each driver needs to indicate which one(s) it supports.

Let's assume strcmp() takes a lot of efforts. For a lot of cases, the algo comes in as a string. Think about FIT and image signature verification. So we have to do the strcmp() anyway, making the enum roundabout.

The point is that in most code we can use the enum, thus saving time and space. FIT is an external interface, so using a string makes more sense there. Having said that, we could invent a new version of FIT that has a binding file and uses ints.

Regards,
SImon

#13 lore raw

PATCH 3/4 crypto: hash: Add software hash DM driver

In reply to Chia-Wei Wang

Add purely software-implmented drivers to support multiple hash operations including CRC, MD5, and SHA family.

This driver is based on the new hash uclass.

---
 drivers/crypto/hash/Kconfig   |  11 ++
 drivers/crypto/hash/Makefile  |   1 +
 drivers/crypto/hash/hash_sw.c | 301 ++++++++++++++++++++++++++++++++++
 3 files changed, 313 insertions(+)
 create mode 100644 drivers/crypto/hash/hash_sw.c
drivers/crypto/hash/Kconfig +11 -0
index e226144b9b..cd29a5c6a4 100644
@@ -3,3 +3,14 @@ config DM_HASH
 	depends on DM
 	help
 	  If you want to use driver model for Hash, say Y.
+
+config HASH_SOFTWARE
+	bool "Enable driver for Hash in software"
+	depends on DM_HASH
+	depends on MD5
+	depends on SHA1
+	depends on SHA256
+	depends on SHA512_ALGO
+	help
+	  Enable driver for hashing operations in software. Currently
+	  it support multiple hash algorithm including CRC/MD5/SHA.
drivers/crypto/hash/Makefile +1 -0
index 83acf3d47b..33d88161ed 100644
@@ -3,3 +3,4 @@
 # Copyright (c) 2021 ASPEED Technology Inc.
 
 obj-$(CONFIG_DM_HASH) += hash-uclass.o
+obj-$(CONFIG_HASH_SOFTWARE) += hash_sw.o
drivers/crypto/hash/hash_sw.c new file +301 -0

301 lines not on this page · raw message

Signature · 1 line
2.17.1
#14 lore raw

Re: [PATCH 3/4] crypto: hash: Add software hash DM driver

Quoted 6 lines · Chia-Wei Wang

Add purely software-implmented drivers to support multiple hash operations including CRC, MD5, and SHA family.

This driver is based on the new hash uclass.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#15 lore raw

Re: [PATCH 3/4] crypto: hash: Add software hash DM driver

In reply to Chia-Wei Wang
Quoted 19 lines · Chia-Wei Wang

Add purely software-implmented drivers to support multiple hash operations including CRC, MD5, and SHA family.

This driver is based on the new hash uclass.

---
  drivers/crypto/hash/Kconfig   |  11 ++
  drivers/crypto/hash/Makefile  |   1 +
  drivers/crypto/hash/hash_sw.c | 301 ++++++++++++++++++++++++++++++++++
  3 files changed, 313 insertions(+)

create mode 100644 drivers/crypto/hash/hash_sw.c

drivers/crypto/hash/Kconfig +0 -0
index e226144b9b..cd29a5c6a4 100644
@@ -3,3 +3,14 @@ config DM_HASH
  	depends on DM

drivers/crypto/hash/Kconfig config DM_HASH · line 13

Quoted 10 lines
drivers/crypto/hash/Kconfig +8 -0
  	help
  	  If you want to use driver model for Hash, say Y.
+
+config HASH_SOFTWARE
+	bool "Enable driver for Hash in software"
+	depends on DM_HASH
+	depends on MD5
+	depends on SHA1
+	depends on SHA256
+	depends on SHA512_ALGO

I would have expected a U_BOOT_DRIVER() for each hash algo, rather than a U_BOOT_DRIVER() wich encompassess all possible algos. If I'm trying to use SHA256 in SPL, I might not have the room too add SHA1 and MD5, so I'd have issues using HASH_SOFTWARE, as designed.

Quoted 18 lines
drivers/crypto/hash/hash_sw.c new file +12 -0
new file mode 100644
index 0000000000..fea9d12609
@@ -0,0 +1,301 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2021 ASPEED Technology Inc.
+ * Author: ChiaWei Wang <[email protected]>
+ */
+#include <config.h>
+#include <common.h>
+#include <dm.h>
+#include <log.h>
+#include <malloc.h>
+#include <watchdog.h>
+#include <u-boot/hash.h>

drivers/crypto/hash/hash_sw.c line 22

Quoted 10 lines
drivers/crypto/hash/hash_sw.c new file +10 -0
+#include <u-boot/crc.h>
+#include <u-boot/md5.h>
+#include <u-boot/sha1.h>
+#include <u-boot/sha256.h>
+#include <u-boot/sha512.h>
+
+/* CRC16-CCITT */
+static void hash_init_crc16_ccitt(void *ctx)
+{
+	*((uint16_t *)ctx) = 0;

Undefined behavior: Pointer aliased type-punning. I would suggest using memset(). Might not be necessarrym as expleined in the next comment.

drivers/crypto/hash/hash_sw.c line 115

Quoted 11 lines
drivers/crypto/hash/hash_sw.c new file +11 -0
+static void hash_update_crc16_ccitt(void *ctx, const void *ibuf, uint32_t ilen)
+static void hash_finish_crc16_ccitt(void *ctx, void *obuf)
+/* CRC32 */
+static void hash_init_crc32(void *ctx)
+static void hash_update_crc32(void *ctx, const void *ibuf, uint32_t ilen)
+static void hash_finish_crc32(void *ctx, void *obuf)
+/* SHA1 */
+static void hash_init_sha1(void *ctx)
+/* SHA256 */
+/* SHA384 */
+/* SHA512 */

This logic already exists in common/hash.c for hash_Lookup_algo() and hash_progressive_algo().

Alex

#16 lore raw

RE: [PATCH 3/4] crypto: hash: Add software hash DM driver

Hi Alex,

Quoted 31 lines

From: Alex G. <[email protected]>
Sent: Thursday, September 16, 2021 11:49 PM

Quoted 10 lines · Chia-Wei Wang

Add purely software-implmented drivers to support multiple hash operations including CRC, MD5, and SHA family.

This driver is based on the new hash uclass.

---
  drivers/crypto/hash/Kconfig   |  11 ++
  drivers/crypto/hash/Makefile  |   1 +
  drivers/crypto/hash/hash_sw.c | 301

++++++++++++++++++++++++++++++++++

Quoted 16 lines
  3 files changed, 313 insertions(+)

create mode 100644 drivers/crypto/hash/hash_sw.c

drivers/crypto/hash/Kconfig +5 -0
index e226144b9b..cd29a5c6a4 100644
@@ -3,3 +3,14 @@ config DM_HASH
  	depends on DM
  	help
  	  If you want to use driver model for Hash, say Y.
+
+config HASH_SOFTWARE
+	bool "Enable driver for Hash in software"
+	depends on DM_HASH
+	depends on MD5
Quoted 8 lines

drivers/crypto/hash/Kconfig config DM_HASH · line 13

Quoted 3 lines
drivers/crypto/hash/Kconfig +3 -0
+	depends on SHA1
+	depends on SHA256
+	depends on SHA512_ALGO

I would have expected a U_BOOT_DRIVER() for each hash algo, rather than a U_BOOT_DRIVER() wich encompassess all possible algos. If I'm trying to use SHA256 in SPL, I might not have the room too add SHA1 and MD5, so I'd have issues using HASH_SOFTWARE, as designed.

I agree with the SPL size issue.
A future patches to move those CONFIG_SHAxxx/CONFIG_MD5 options into the DM-based hash_sw.c could be an option. Thus the balance between the hash algos support and the code size can be managed.

Quoted 23 lines
Quoted 9 lines
(quoted diff) +0 -0

b/drivers/crypto/hash/hash_sw.c new file mode 100644 index 0000000000..fea9d12609

drivers/crypto/hash/hash_sw.c new file +2 -0
@@ -0,0 +1,301 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*

drivers/crypto/hash/hash_sw.c line 12

Quoted 10 lines
drivers/crypto/hash/hash_sw.c new file +10 -0
+ * Copyright (c) 2021 ASPEED Technology Inc.
+ * Author: ChiaWei Wang <[email protected]>  */ #include
+<config.h> #include <common.h> #include <dm.h> #include <log.h>
+#include <malloc.h> #include <watchdog.h> #include <u-boot/hash.h>
+#include <u-boot/crc.h> #include <u-boot/md5.h> #include
+<u-boot/sha1.h> #include <u-boot/sha256.h> #include <u-boot/sha512.h>
+
+/* CRC16-CCITT */
+static void hash_init_crc16_ccitt(void *ctx) {
+	*((uint16_t *)ctx) = 0;

Undefined behavior: Pointer aliased type-punning. I would suggest using memset(). Might not be necessarrym as expleined in the next comment.

Quoted 15 lines

drivers/crypto/hash/hash_sw.c line 115

Quoted 12 lines
drivers/crypto/hash/hash_sw.c new file +12 -0
+static void hash_update_crc16_ccitt(void *ctx, const void *ibuf,
+uint32_t ilen) static void hash_finish_crc16_ccitt(void *ctx, void
+*obuf)
+/* CRC32 */
+static void hash_init_crc32(void *ctx) static void
+hash_update_crc32(void *ctx, const void *ibuf, uint32_t ilen) static
+void hash_finish_crc32(void *ctx, void *obuf)
+/* SHA1 */
+static void hash_init_sha1(void *ctx)
+/* SHA256 */
+/* SHA384 */
+/* SHA512 */

This logic already exists in common/hash.c for hash_Lookup_algo() and hash_progressive_algo().

Yes.
To prevent modifying the 'static' keyword in common/hash.c while reusing the hash lib, the same logic appears in the DM-based hash_sw.c.

Chiawei

#17 lore raw

PATCH 4/4 fit: Use DM hash driver if supported

In reply to Chia-Wei Wang

Calculate hash using DM driver if supported.
For backward compatibility, the call to legacy
hash functions is reserved.

---
 common/image-fit.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
common/image-fit.c +30 -0
index d6b2c3c7ec..ec2e526356 100644
@@ -25,6 +25,10 @@
 #include <asm/io.h>
 #include <malloc.h>
 #include <asm/global_data.h>
+#ifdef CONFIG_DM_HASH
+#include <dm.h>
+#include <u-boot/hash.h>
+#endif
 DECLARE_GLOBAL_DATA_PTR;
 #endif /* !USE_HOSTCC*/
 
@@ -1214,6 +1218,31 @@ 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 !defined(USE_HOSTCC) && defined(CONFIG_DM_HASH)
+	int rc;
+	enum HASH_ALGO hash_algo;
+	struct udevice *dev;
+
+	rc = uclass_get_device(UCLASS_HASH, 0, &dev);
+	if (rc) {
+		debug("failed to get hash device, rc=%d\n", rc);
+		return -1;
+	}
+
+	hash_algo = hash_algo_lookup_by_name(algo);
+	if (hash_algo == HASH_ALGO_INVALID) {
+		debug("Unsupported hash algorithm\n");
+		return -1;
+	};
+
+	rc = hash_digest_wd(dev, hash_algo, data, data_len, value, CHUNKSZ);
+	if (rc) {
+		debug("failed to get hash value, rc=%d\n", rc);
+		return -1;
+	}
+
+	*value_len = hash_algo_digest_size(hash_algo);
+#else
 	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
 		*((uint32_t *)value) = crc32_wd(0, data, data_len,
 							CHUNKSZ_CRC32);
@@ -1242,6 +1271,7 @@ int calculate_hash(const void *data, int data_len, const char *algo,
 		debug("Unsupported hash alogrithm\n");
 		return -1;
 	}
+#endif
 	return 0;
 }
 
Signature · 1 line
2.17.1
#18 lore raw

Re: [PATCH 4/4] fit: Use DM hash driver if supported

Quoted 5 lines · Chia-Wei Wang

Calculate hash using DM driver if supported.
For backward compatibility, the call to legacy
hash functions is reserved.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#19 lore raw

Re: [PATCH 4/4] fit: Use DM hash driver if supported

In reply to Chia-Wei Wang
Quoted 25 lines · Chia-Wei Wang

Calculate hash using DM driver if supported.
For backward compatibility, the call to legacy
hash functions is reserved.

---
  common/image-fit.c | 30 ++++++++++++++++++++++++++++++
  1 file changed, 30 insertions(+)
common/image-fit.c +4 -0
index d6b2c3c7ec..ec2e526356 100644
@@ -25,6 +25,10 @@
  #include <asm/io.h>
  #include <malloc.h>
  #include <asm/global_data.h>
+#ifdef CONFIG_DM_HASH
+#include <dm.h>
+#include <u-boot/hash.h>
+#endif
  DECLARE_GLOBAL_DATA_PTR;
  #endif /* !USE_HOSTCC*/
 

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

Quoted 4 lines
common/image-fit.c +1 -0
@@ -1214,6 +1218,31 @@ 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 !defined(USE_HOSTCC) && defined(CONFIG_DM_HASH)

This file is shared in its entirety with host tools. There isn't a huge opportunity for using a DM-type approach here without #ifndef USE_HOSTCC.

Alex

Quoted 36 lines
common/image-fit.c +25 -0
+	int rc;
+	enum HASH_ALGO hash_algo;
+	struct udevice *dev;
+
+	rc = uclass_get_device(UCLASS_HASH, 0, &dev);
+	if (rc) {
+		debug("failed to get hash device, rc=%d\n", rc);
+		return -1;
+	}
+
+	hash_algo = hash_algo_lookup_by_name(algo);
+	if (hash_algo == HASH_ALGO_INVALID) {
+		debug("Unsupported hash algorithm\n");
+		return -1;
+	};
+
+	rc = hash_digest_wd(dev, hash_algo, data, data_len, value, CHUNKSZ);
+	if (rc) {
+		debug("failed to get hash value, rc=%d\n", rc);
+		return -1;
+	}
+
+	*value_len = hash_algo_digest_size(hash_algo);
+#else
  	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
  		*((uint32_t *)value) = crc32_wd(0, data, data_len,
  							CHUNKSZ_CRC32);
@@ -1242,6 +1271,7 @@ int calculate_hash(const void *data, int data_len, const char *algo,
  		debug("Unsupported hash alogrithm\n");
  		return -1;
  	}
+#endif
  	return 0;
  }
 
#20 lore raw

RE: [PATCH 4/4] fit: Use DM hash driver if supported

Hi Alex,

Quoted 30 lines

From: Alex G. <[email protected]>
Sent: Friday, September 17, 2021 12:00 AM

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

Quoted 25 lines · Chia-Wei Wang

Calculate hash using DM driver if supported.
For backward compatibility, the call to legacy hash functions is reserved.

---
  common/image-fit.c | 30 ++++++++++++++++++++++++++++++
  1 file changed, 30 insertions(+)
common/image-fit.c index renamed from common/image-fit.c +0 -0

d6b2c3c7ec..ec2e526356 100644

common/image-fit.c +4 -0
@@ -25,6 +25,10 @@
  #include <asm/io.h>
  #include <malloc.h>
  #include <asm/global_data.h>
+#ifdef CONFIG_DM_HASH
+#include <dm.h>
+#include <u-boot/hash.h>
+#endif
  DECLARE_GLOBAL_DATA_PTR;
  #endif /* !USE_HOSTCC*/
 
@@ -1214,6 +1218,31 @@ int fit_set_timestamp(void *fit, int noffset,

time_t timestamp)

Quoted 8 lines

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

Quoted 4 lines
common/image-fit.c +1 -0
  int calculate_hash(const void *data, int data_len, const char *algo,
  			uint8_t *value, int *value_len)
  {
+#if !defined(USE_HOSTCC) && defined(CONFIG_DM_HASH)

This file is shared in its entirety with host tools. There isn't a huge opportunity for using a DM-type approach here without #ifndef USE_HOSTCC.

There are still clean up missions to make U-boot bootloader to move on to the DM-based approach.

For instance, when a FIT image is verified by a hash digest, the hash is calculated by calculate_hash() in image-fit.c. However, when a FIT image is verified by a signature, the hash comes from hash_calculate() in hash-checksum.c. In my personal opinion, a consistent way to calculate hash for U-Boot bootloader would be better for maintenance.

To make this transition more smoothly, currently the USE_HOSTCC and CONFIG_DM_HASH are added in an ad-hoc way.

Chiawei

Quoted 36 lines

common/image-fit.c int calculate_hash(const void *data, int data_len,

Quoted 27 lines
common/image-fit.c +23 -0
+	int rc;
+	enum HASH_ALGO hash_algo;
+	struct udevice *dev;
+
+	rc = uclass_get_device(UCLASS_HASH, 0, &dev);
+	if (rc) {
+		debug("failed to get hash device, rc=%d\n", rc);
+		return -1;
+	}
+
+	hash_algo = hash_algo_lookup_by_name(algo);
+	if (hash_algo == HASH_ALGO_INVALID) {
+		debug("Unsupported hash algorithm\n");
+		return -1;
+	};
+
+	rc = hash_digest_wd(dev, hash_algo, data, data_len, value, CHUNKSZ);
+	if (rc) {
+		debug("failed to get hash value, rc=%d\n", rc);
+		return -1;
+	}
+
+	*value_len = hash_algo_digest_size(hash_algo); #else
  	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
  		*((uint32_t *)value) = crc32_wd(0, data, data_len,
  							CHUNKSZ_CRC32);
@@ -1242,6 +1271,7 @@ int calculate_hash(const void *data, int data_len,

const char *algo,

Quoted 8 lines
common/image-fit.c +1 -0
  		debug("Unsupported hash alogrithm\n");
  		return -1;
  	}
+#endif
  	return 0;
  }
 
#21 lore raw
In reply to Chia-Wei Wang

Hi All,

Do you have update on this patch series?
We look forward to continuing the SPL FIT booting patch for Aspeed SoCs based on this one. Any advice and suggestions are appreciated.

Chiawei

Quoted 41 lines

From: U-Boot <[email protected]> On Behalf Of Chia-Wei Wang
Sent: Friday, July 30, 2021 9:08 AM

This patch series proposes new UCLASS_HASH for hash devices. Thus the hash drivers (SW or HW-accelerated) can be developed in the DM-based fashion.

A purely software implemented hash driver is also added under the newly added UCLASS_HASH uclass. In addition, the FIT image hash verification is also updated to leverage the UCLASS_HASH driver if configured.

As there is widly spread use of non-DM hash functions (common/hash.c), this patch does not remove them. More patches are needed if UCLASS_HASH is established.

Chia-Wei Wang (4):
lib/md5: Export progressive APIs
dm: hash: Add new UCLASS_HASH support
crypto: hash: Add software hash DM driver
fit: Use DM hash driver if supported

 common/image-fit.c                |  30 +++
 drivers/crypto/Kconfig            |   2 +
 drivers/crypto/Makefile           |   1 +
 drivers/crypto/hash/Kconfig       |  16 ++
 drivers/crypto/hash/Makefile      |   6 +
 drivers/crypto/hash/hash-uclass.c | 121 ++++++++++++
 drivers/crypto/hash/hash_sw.c     | 301
++++++++++++++++++++++++++++++
 include/dm/uclass-id.h            |   1 +
 include/u-boot/hash.h             |  61 ++++++
 include/u-boot/md5.h              |   4 +
 lib/md5.c                         |   6 +-
 11 files changed, 546 insertions(+), 3 deletions(-)  create mode 100644

drivers/crypto/hash/Kconfig create mode 100644 drivers/crypto/hash/Makefile create mode 100644 drivers/crypto/hash/hash-uclass.c create mode 100644 drivers/crypto/hash/hash_sw.c create mode 100644 include/u-boot/hash.h

--
2.17.1