From: James Hilliard <james.hilliard1@gmail.com>
To: Svyatoslav Ryhel <clamor95@gmail.com>,
Ion Agorria <ion@agorria.com>,
u-boot@lists.denx.de, Aspeed BMC SW team <BMC-SW@aspeedtech.com>,
Joel Stanley <joel@jms.id.au>
Cc: Chen-Yu Tsai <wens@kernel.org>,
Samuel Holland <samuel@sholland.org>,
Tom Rini <trini@konsulko.com>, Simon Glass <sjg@chromium.org>,
Thierry Reding <treding@nvidia.com>,
Quentin Schulz <quentin.schulz@cherry.de>,
Marek Vasut <marek.vasut+renesas@mailbox.org>,
Rasmus Villemoes <ravi@prevas.dk>,
Aristo Chen <aristo.chen@canonical.com>,
Anton Ivanov <anton@binarly.io>,
Daniel Golle <daniel@makrotopia.org>,
Francois Berder <fberder@outlook.fr>,
Peng Fan <peng.fan@nxp.com>,
Neil Armstrong <neil.armstrong@linaro.org>,
Randolph Sapp <rs@ti.com>, Jonas Karlman <jonas@kwiboo.se>,
Wolfgang Wallner <wolfgang.wallner@at.abb.com>,
Alexey Charkov <alchark@gmail.com>,
Ilias Apalodimas <ilias.apalodimas@linaro.org>,
Heiko Schocher <hs@nabladev.com>,
"Kory Maincent (TI.com)" <kory.maincent@bootlin.com>,
Anshul Dalal <anshuld@ti.com>, Johan Jonker <jbx6244@gmail.com>,
Francesco Valla <francesco@valla.it>,
Heinrich Schuchardt <xypron.glpk@gmx.de>,
Michael Walle <mwalle@kernel.org>,
Andre Przywara <andre.przywara@arm.com>,
Lukasz Majewski <lukma@denx.de>,
Richard Genoud <richard.genoud@bootlin.com>,
Michael Trimarchi <michael@amarulasolutions.com>,
E Shattow <e@freeshell.de>,
Enric Balletbo i Serra <eballetbo@kernel.org>,
Mattijs Korpershoek <mkorpershoek@kernel.org>,
Lucas Dietrich <ld.adecy@gmail.com>,
David Lechner <dlechner@baylibre.com>,
Julien Stephan <jstephan@baylibre.com>,
Kuan-Wei Chiu <visitorckw@gmail.com>,
Bastien Curutchet <bastien.curutchet@bootlin.com>,
Raymond Mao <raymond.mao@riscstar.com>,
Ryan Chen <ryan_chen@aspeedtech.com>,
Chia-Wei Wang <chiawei_wang@aspeedtech.com>,
"Lucien.Jheng" <lucienzx159@gmail.com>,
Mateusz Furdyna <mateusz.furdyna@nokia.com>,
Dinesh Maniyam <dinesh.maniyam@altera.com>,
Heiko Stuebner <heiko@sntech.de>,
James Hilliard <james.hilliard1@gmail.com>
Subject: [PATCH v4 02/14] crypto: hash: use DM providers from hash command
Date: Mon, 13 Jul 2026 00:42:59 -0600 [thread overview]
Message-ID: <20260713-submit-ce-series-v2-v4-2-ff7edc705b8a@gmail.com> (raw)
In-Reply-To: <20260713-submit-ce-series-v2-v4-0-ff7edc705b8a@gmail.com>
The hash command currently always uses the software implementation for
the selected algorithm, even when driver-model hash providers are
available.
Add a hash_digest_wd_lookup() helper which probes UCLASS_HASH devices in
order and uses the first provider supporting the requested algorithm.
Continue past unavailable providers and unsupported operations, but
propagate a hard digest failure once a provider accepts the operation.
Remember probe failures so they are not silently hidden by software
fallback when no later provider succeeds.
Use the helper from the hash command and retain its software fallback
when no usable provider is present. Add sandbox tests covering provider
fallback and hard-error propagation.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
Changes v3 -> v4:
- New patch
- Try all registered hash providers instead of only device zero
- Add provider-selection and error-propagation tests
- Reserve -EINVAL for hard errors
- Use -EOPNOTSUPP for unsupported algorithms
---
common/hash.c | 21 ++++++
drivers/crypto/aspeed/aspeed_hace.c | 2 +-
drivers/crypto/aspeed/cptra_sha.c | 2 +-
drivers/crypto/hash/hash-uclass.c | 39 +++++++++-
include/u-boot/hash.h | 24 +++++-
test/dm/Makefile | 1 +
test/dm/hash.c | 143 ++++++++++++++++++++++++++++++++++++
7 files changed, 226 insertions(+), 6 deletions(-)
diff --git a/common/hash.c b/common/hash.c
index 71c4bef5826..5cbb4926c1d 100644
--- a/common/hash.c
+++ b/common/hash.c
@@ -11,6 +11,7 @@
#ifndef USE_HOSTCC
#include <command.h>
+#include <dm.h>
#include <env.h>
#include <log.h>
#include <malloc.h>
@@ -20,6 +21,7 @@
#include <asm/global_data.h>
#include <asm/io.h>
#include <linux/errno.h>
+#include <u-boot/hash.h>
#else
#include "mkimage.h"
#include <linux/compiler_attributes.h>
@@ -614,7 +616,26 @@ int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
return CMD_RET_FAILURE;
buf = map_sysmem(addr, len);
+ if (CONFIG_IS_ENABLED(DM_HASH)) {
+ enum HASH_ALGO hash_algo;
+ int ret;
+
+ hash_algo = hash_algo_lookup_by_name(algo_name);
+ if (hash_algo != HASH_ALGO_INVALID) {
+ ret = hash_digest_wd_lookup(hash_algo, buf, len,
+ output,
+ algo->chunk_size);
+ if (ret && ret != -ENODEV && ret != -EOPNOTSUPP) {
+ unmap_sysmem(buf);
+ free(output);
+ return CMD_RET_FAILURE;
+ }
+ if (!ret)
+ goto done;
+ }
+ }
algo->hash_func_ws(buf, len, output, algo->chunk_size);
+done:
unmap_sysmem(buf);
/* Try to avoid code bloat when verify is not needed */
diff --git a/drivers/crypto/aspeed/aspeed_hace.c b/drivers/crypto/aspeed/aspeed_hace.c
index 22b5008a296..2469f53472f 100644
--- a/drivers/crypto/aspeed/aspeed_hace.c
+++ b/drivers/crypto/aspeed/aspeed_hace.c
@@ -160,7 +160,7 @@ static int aspeed_hace_init(struct udevice *dev, enum HASH_ALGO algo, void **ctx
free_n_out:
free(hace_ctx);
- return -EINVAL;
+ return -EOPNOTSUPP;
}
static int aspeed_hace_update(struct udevice *dev, void *ctx, const void *ibuf, uint32_t ilen)
diff --git a/drivers/crypto/aspeed/cptra_sha.c b/drivers/crypto/aspeed/cptra_sha.c
index f57778e160d..0dc00f306f1 100644
--- a/drivers/crypto/aspeed/cptra_sha.c
+++ b/drivers/crypto/aspeed/cptra_sha.c
@@ -68,7 +68,7 @@ static int cptra_sha_init(struct udevice *dev, enum HASH_ALGO algo, void **ctxp)
cs_ctx->dgst_len = 64;
break;
default:
- rc = -EINVAL;
+ rc = -EOPNOTSUPP;
goto free_n_out;
};
diff --git a/drivers/crypto/hash/hash-uclass.c b/drivers/crypto/hash/hash-uclass.c
index 5d9f1e0d59b..30929412856 100644
--- a/drivers/crypto/hash/hash-uclass.c
+++ b/drivers/crypto/hash/hash-uclass.c
@@ -73,8 +73,8 @@ int hash_digest(struct udevice *dev, enum HASH_ALGO algo,
}
int hash_digest_wd(struct udevice *dev, enum HASH_ALGO algo,
- const void *ibuf, const uint32_t ilen,
- void *obuf, uint32_t chunk_sz)
+ const void *ibuf, const uint32_t ilen,
+ void *obuf, uint32_t chunk_sz)
{
struct hash_ops *ops = (struct hash_ops *)device_get_ops(dev);
@@ -84,6 +84,41 @@ int hash_digest_wd(struct udevice *dev, enum HASH_ALGO algo,
return ops->hash_digest_wd(dev, algo, ibuf, ilen, obuf, chunk_sz);
}
+static bool hash_op_unsupported(int ret)
+{
+ return ret == -ENOSYS || ret == -EOPNOTSUPP;
+}
+
+int hash_digest_wd_lookup(enum HASH_ALGO algo, const void *ibuf,
+ const u32 ilen, void *obuf, u32 chunk_sz)
+{
+ struct udevice *dev;
+ int first_probe_err = 0;
+ bool found = false;
+ int ret;
+
+ for (ret = uclass_first_device_check(UCLASS_HASH, &dev); dev;
+ ret = uclass_next_device_check(&dev)) {
+ found = true;
+ if (ret) {
+ if (!first_probe_err)
+ first_probe_err = ret;
+ continue;
+ }
+
+ ret = hash_digest_wd(dev, algo, ibuf, ilen, obuf, chunk_sz);
+ if (!ret)
+ return 0;
+ if (!hash_op_unsupported(ret))
+ return ret;
+ }
+
+ if (first_probe_err)
+ return first_probe_err;
+
+ return found ? -EOPNOTSUPP : -ENODEV;
+}
+
int hash_init(struct udevice *dev, enum HASH_ALGO algo, void **ctxp)
{
struct hash_ops *ops = (struct hash_ops *)device_get_ops(dev);
diff --git a/include/u-boot/hash.h b/include/u-boot/hash.h
index f9d47a99a77..a6ba08a8591 100644
--- a/include/u-boot/hash.h
+++ b/include/u-boot/hash.h
@@ -19,6 +19,8 @@ enum HASH_ALGO {
HASH_ALGO_INVALID = 0xffffffff,
};
+struct udevice;
+
/* general APIs for hash algo information */
enum HASH_ALGO hash_algo_lookup_by_name(const char *name);
ssize_t hash_algo_digest_size(enum HASH_ALGO algo);
@@ -29,8 +31,26 @@ int hash_digest(struct udevice *dev, enum HASH_ALGO algo,
const void *ibuf, const uint32_t ilen,
void *obuf);
int hash_digest_wd(struct udevice *dev, enum HASH_ALGO algo,
- const void *ibuf, const uint32_t ilen,
- void *obuf, uint32_t chunk_sz);
+ const void *ibuf, const uint32_t ilen,
+ void *obuf, uint32_t chunk_sz);
+/**
+ * hash_digest_wd_lookup() - Hash with the first provider supporting an algorithm
+ *
+ * Probe each hash device in order and use the first one which supports the
+ * requested algorithm. Probe failures are remembered while later providers are
+ * tried. Once a provider accepts an operation, hard failures are returned
+ * without trying another provider.
+ *
+ * @algo: Hash algorithm
+ * @ibuf: Input buffer
+ * @ilen: Input buffer length
+ * @obuf: Output buffer
+ * @chunk_sz: Watchdog scheduling interval
+ * Return: 0 on success, -ENODEV if there are no providers, -EOPNOTSUPP if no
+ * provider supports @algo, or another negative error from a provider
+ */
+int hash_digest_wd_lookup(enum HASH_ALGO algo, const void *ibuf,
+ const u32 ilen, void *obuf, u32 chunk_sz);
int hash_init(struct udevice *dev, enum HASH_ALGO algo, void **ctxp);
int hash_update(struct udevice *dev, void *ctx, const void *ibuf, const uint32_t ilen);
int hash_finish(struct udevice *dev, void *ctx, void *obuf);
diff --git a/test/dm/Makefile b/test/dm/Makefile
index 0e3c63568dd..b6e8f0bd248 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -46,6 +46,7 @@ obj-$(CONFIG_DMA) += dma.o
obj-$(CONFIG_VIDEO_MIPI_DSI) += dsi_host.o
obj-$(CONFIG_DM_DSA) += dsa.o
obj-$(CONFIG_ECDSA_VERIFY) += ecdsa.o
+obj-$(CONFIG_DM_HASH) += hash.o
obj-$(CONFIG_EFI_MEDIA_SANDBOX) += efi_media.o
obj-$(CONFIG_DM_ETH) += eth.o
obj-$(CONFIG_EXTCON) += extcon.o
diff --git a/test/dm/hash.c b/test/dm/hash.c
new file mode 100644
index 00000000000..fe949e33de5
--- /dev/null
+++ b/test/dm/hash.c
@@ -0,0 +1,143 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for driver-model hash-provider selection
+ *
+ * Copyright (C) 2026 James Hilliard
+ */
+
+#include <dm.h>
+#include <dm/device-internal.h>
+#include <dm/root.h>
+#include <dm/test.h>
+#include <dm/uclass-internal.h>
+#include <u-boot/hash.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+static int unsupported_calls;
+static int success_calls;
+static int hard_error_calls;
+
+static int hash_test_unsupported(struct udevice *dev, enum HASH_ALGO algo,
+ const void *ibuf, const uint32_t ilen,
+ void *obuf, uint32_t chunk_sz)
+{
+ unsupported_calls++;
+
+ return -EOPNOTSUPP;
+}
+
+static int hash_test_success(struct udevice *dev, enum HASH_ALGO algo,
+ const void *ibuf, const uint32_t ilen,
+ void *obuf, uint32_t chunk_sz)
+{
+ success_calls++;
+ memset(obuf, 0x5a, hash_algo_digest_size(algo));
+
+ return 0;
+}
+
+static int hash_test_hard_error(struct udevice *dev, enum HASH_ALGO algo,
+ const void *ibuf, const uint32_t ilen,
+ void *obuf, uint32_t chunk_sz)
+{
+ hard_error_calls++;
+
+ return -EINVAL;
+}
+
+static const struct hash_ops hash_test_unsupported_ops = {
+ .hash_digest_wd = hash_test_unsupported,
+};
+
+static const struct hash_ops hash_test_success_ops = {
+ .hash_digest_wd = hash_test_success,
+};
+
+static const struct hash_ops hash_test_hard_error_ops = {
+ .hash_digest_wd = hash_test_hard_error,
+};
+
+U_BOOT_DRIVER(hash_test_unsupported_drv) = {
+ .name = "hash_test_unsupported",
+ .id = UCLASS_HASH,
+ .ops = &hash_test_unsupported_ops,
+};
+
+U_BOOT_DRIVER(hash_test_success_drv) = {
+ .name = "hash_test_success",
+ .id = UCLASS_HASH,
+ .ops = &hash_test_success_ops,
+};
+
+U_BOOT_DRIVER(hash_test_hard_error_drv) = {
+ .name = "hash_test_hard_error",
+ .id = UCLASS_HASH,
+ .ops = &hash_test_hard_error_ops,
+};
+
+static int hash_test_unbind_all(void)
+{
+ struct udevice *dev;
+ int ret;
+
+ for (;;) {
+ ret = uclass_find_first_device(UCLASS_HASH, &dev);
+ if (ret || !dev)
+ return ret;
+ if (device_active(dev)) {
+ ret = device_remove(dev, DM_REMOVE_NORMAL);
+ if (ret)
+ return ret;
+ }
+ ret = device_unbind(dev);
+ if (ret)
+ return ret;
+ }
+}
+
+static int hash_test_bind(const struct driver *drv, const char *name)
+{
+ struct udevice *dev;
+
+ return device_bind(dm_root(), drv, name, 0, ofnode_null(), &dev);
+}
+
+static int dm_test_hash_provider_selection(struct unit_test_state *uts)
+{
+ u8 digest[32];
+ int ret;
+
+ ut_assertok(hash_test_unbind_all());
+ ut_assertok(hash_test_bind(DM_DRIVER_GET(hash_test_unsupported_drv),
+ "hash-unsupported"));
+ ut_assertok(hash_test_bind(DM_DRIVER_GET(hash_test_success_drv),
+ "hash-success"));
+
+ unsupported_calls = 0;
+ success_calls = 0;
+ memset(digest, 0, sizeof(digest));
+ ret = hash_digest_wd_lookup(HASH_ALGO_SHA256, "test", 4, digest, 4);
+ ut_assertok(ret);
+ ut_asserteq(1, unsupported_calls);
+ ut_asserteq(1, success_calls);
+ for (int i = 0; i < sizeof(digest); i++)
+ ut_asserteq(0x5a, digest[i]);
+
+ ut_assertok(hash_test_unbind_all());
+ ut_assertok(hash_test_bind(DM_DRIVER_GET(hash_test_hard_error_drv),
+ "hash-hard-error"));
+ ut_assertok(hash_test_bind(DM_DRIVER_GET(hash_test_success_drv),
+ "hash-success"));
+
+ hard_error_calls = 0;
+ success_calls = 0;
+ ret = hash_digest_wd_lookup(HASH_ALGO_SHA256, "test", 4, digest, 4);
+ ut_asserteq(-EINVAL, ret);
+ ut_asserteq(1, hard_error_calls);
+ ut_asserteq(0, success_calls);
+
+ return 0;
+}
+
+DM_TEST(dm_test_hash_provider_selection, UTF_SCAN_FDT);
--
2.53.0
next prev parent reply other threads:[~2026-07-13 13:21 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-13 6:42 [PATCH v4 00/14] crypto: allwinner: enable sun8i-ce FIT crypto James Hilliard
2026-07-13 6:42 ` [PATCH v4 01/14] cmd: aes: fix DM operation handling James Hilliard
2026-07-13 6:42 ` James Hilliard [this message]
2026-07-13 6:43 ` [PATCH v4 03/14] crypto: aes: allow DM AES in SPL James Hilliard
2026-07-13 6:43 ` [PATCH v4 04/14] crypto: hash: allow DM hash " James Hilliard
2026-07-13 6:43 ` [PATCH v4 05/14] boot: image: try all DM hash providers James Hilliard
2026-07-13 6:43 ` [PATCH v4 06/14] crypto: aes: fix software key-size handling James Hilliard
2026-07-13 6:43 ` [PATCH v4 07/14] crypto: aes: add software-key provider dispatch James Hilliard
2026-07-13 6:43 ` [PATCH v4 08/14] boot: image: add FIT decrypt-to-buffer helper James Hilliard
2026-07-13 6:43 ` [PATCH v4 09/14] spl: fit: support encrypted payloads James Hilliard
2026-07-13 6:43 ` [PATCH v4 10/14] clk: sunxi: add H6/H616 CE gates and reset James Hilliard
2026-07-13 6:43 ` [PATCH v4 11/14] lib: ecdsa: support additional curve sizes James Hilliard
2026-07-13 6:43 ` [PATCH v4 12/14] crypto: allwinner: add sun8i-ce AES driver James Hilliard
2026-07-13 6:43 ` [PATCH v4 13/14] crypto: allwinner: add sun8i-ce ECDSA verifier James Hilliard
2026-07-13 6:43 ` [PATCH v4 14/14] crypto: allwinner: add sun8i-ce hash driver James Hilliard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260713-submit-ce-series-v2-v4-2-ff7edc705b8a@gmail.com \
--to=james.hilliard1@gmail.com \
--cc=BMC-SW@aspeedtech.com \
--cc=alchark@gmail.com \
--cc=andre.przywara@arm.com \
--cc=anshuld@ti.com \
--cc=anton@binarly.io \
--cc=aristo.chen@canonical.com \
--cc=bastien.curutchet@bootlin.com \
--cc=chiawei_wang@aspeedtech.com \
--cc=clamor95@gmail.com \
--cc=daniel@makrotopia.org \
--cc=dinesh.maniyam@altera.com \
--cc=dlechner@baylibre.com \
--cc=e@freeshell.de \
--cc=eballetbo@kernel.org \
--cc=fberder@outlook.fr \
--cc=francesco@valla.it \
--cc=heiko@sntech.de \
--cc=hs@nabladev.com \
--cc=ilias.apalodimas@linaro.org \
--cc=ion@agorria.com \
--cc=jbx6244@gmail.com \
--cc=joel@jms.id.au \
--cc=jonas@kwiboo.se \
--cc=jstephan@baylibre.com \
--cc=kory.maincent@bootlin.com \
--cc=ld.adecy@gmail.com \
--cc=lucienzx159@gmail.com \
--cc=lukma@denx.de \
--cc=marek.vasut+renesas@mailbox.org \
--cc=mateusz.furdyna@nokia.com \
--cc=michael@amarulasolutions.com \
--cc=mkorpershoek@kernel.org \
--cc=mwalle@kernel.org \
--cc=neil.armstrong@linaro.org \
--cc=peng.fan@nxp.com \
--cc=quentin.schulz@cherry.de \
--cc=ravi@prevas.dk \
--cc=raymond.mao@riscstar.com \
--cc=richard.genoud@bootlin.com \
--cc=rs@ti.com \
--cc=ryan_chen@aspeedtech.com \
--cc=samuel@sholland.org \
--cc=sjg@chromium.org \
--cc=treding@nvidia.com \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
--cc=visitorckw@gmail.com \
--cc=wens@kernel.org \
--cc=wolfgang.wallner@at.abb.com \
--cc=xypron.glpk@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox