Skip to content

Commit 4f89448

Browse files
ebiggersDanilo Krummrich
authored andcommitted
firmware_loader: use SHA-256 library API instead of crypto_shash API
This user of SHA-256 does not support any other algorithm, so the crypto_shash abstraction provides no value. Just use the SHA-256 library API instead, which is much simpler and easier to use. Also take advantage of printk's built-in hex conversion using %*phN. Signed-off-by: Eric Biggers <ebiggers@google.com> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://lore.kernel.org/r/20250428190909.852705-1-ebiggers@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent e99efa8 commit 4f89448

File tree

2 files changed

+5
-33
lines changed

2 files changed

+5
-33
lines changed

drivers/base/firmware_loader/Kconfig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ menu "Firmware loader"
33

44
config FW_LOADER
55
tristate "Firmware loading facility" if EXPERT
6-
select CRYPTO_HASH if FW_LOADER_DEBUG
7-
select CRYPTO_SHA256 if FW_LOADER_DEBUG
6+
select CRYPTO_LIB_SHA256 if FW_LOADER_DEBUG
87
default y
98
help
109
This enables the firmware loading facility in the kernel. The kernel
@@ -28,7 +27,6 @@ config FW_LOADER
2827

2928
config FW_LOADER_DEBUG
3029
bool "Log filenames and checksums for loaded firmware"
31-
depends on CRYPTO = FW_LOADER || CRYPTO=y
3230
depends on DYNAMIC_DEBUG
3331
depends on FW_LOADER
3432
default FW_LOADER

drivers/base/firmware_loader/main.c

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -806,41 +806,15 @@ static void fw_abort_batch_reqs(struct firmware *fw)
806806
}
807807

808808
#if defined(CONFIG_FW_LOADER_DEBUG)
809-
#include <crypto/hash.h>
810809
#include <crypto/sha2.h>
811810

812811
static void fw_log_firmware_info(const struct firmware *fw, const char *name, struct device *device)
813812
{
814-
struct shash_desc *shash;
815-
struct crypto_shash *alg;
816-
u8 *sha256buf;
817-
char *outbuf;
813+
u8 digest[SHA256_DIGEST_SIZE];
818814

819-
alg = crypto_alloc_shash("sha256", 0, 0);
820-
if (IS_ERR(alg))
821-
return;
822-
823-
sha256buf = kmalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
824-
outbuf = kmalloc(SHA256_BLOCK_SIZE + 1, GFP_KERNEL);
825-
shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(alg), GFP_KERNEL);
826-
if (!sha256buf || !outbuf || !shash)
827-
goto out_free;
828-
829-
shash->tfm = alg;
830-
831-
if (crypto_shash_digest(shash, fw->data, fw->size, sha256buf) < 0)
832-
goto out_free;
833-
834-
for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
835-
sprintf(&outbuf[i * 2], "%02x", sha256buf[i]);
836-
outbuf[SHA256_BLOCK_SIZE] = 0;
837-
dev_dbg(device, "Loaded FW: %s, sha256: %s\n", name, outbuf);
838-
839-
out_free:
840-
kfree(shash);
841-
kfree(outbuf);
842-
kfree(sha256buf);
843-
crypto_free_shash(alg);
815+
sha256(fw->data, fw->size, digest);
816+
dev_dbg(device, "Loaded FW: %s, sha256: %*phN\n",
817+
name, SHA256_DIGEST_SIZE, digest);
844818
}
845819
#else
846820
static void fw_log_firmware_info(const struct firmware *fw, const char *name,

0 commit comments

Comments
 (0)