Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Eric Biggers and committed by
Danilo Krummrich
4f894483 e99efa8a

+5 -33
+1 -3
drivers/base/firmware_loader/Kconfig
··· 3 3 4 4 config FW_LOADER 5 5 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 8 7 default y 9 8 help 10 9 This enables the firmware loading facility in the kernel. The kernel ··· 27 28 28 29 config FW_LOADER_DEBUG 29 30 bool "Log filenames and checksums for loaded firmware" 30 - depends on CRYPTO = FW_LOADER || CRYPTO=y 31 31 depends on DYNAMIC_DEBUG 32 32 depends on FW_LOADER 33 33 default FW_LOADER
+4 -30
drivers/base/firmware_loader/main.c
··· 806 806 } 807 807 808 808 #if defined(CONFIG_FW_LOADER_DEBUG) 809 - #include <crypto/hash.h> 810 809 #include <crypto/sha2.h> 811 810 812 811 static void fw_log_firmware_info(const struct firmware *fw, const char *name, struct device *device) 813 812 { 814 - struct shash_desc *shash; 815 - struct crypto_shash *alg; 816 - u8 *sha256buf; 817 - char *outbuf; 813 + u8 digest[SHA256_DIGEST_SIZE]; 818 814 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); 844 818 } 845 819 #else 846 820 static void fw_log_firmware_info(const struct firmware *fw, const char *name,