From 3133ee5293a827cf823ad3b91bfc64703fa982c6 Mon Sep 17 00:00:00 2001 From: Peter Lin Date: Mon, 12 Dec 2022 22:55:06 +0800 Subject: [PATCH] Add error message when sha256 algorithm is not supported It failed silently when crypto_alloc_tfm() failed, so add an error message to inform the developer to enable sha256 algorithm support. Signed-off-by: Yu Chien Peter Lin --- examples/cryptosha256.c | 6 +++++- lkmpg.tex | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/cryptosha256.c b/examples/cryptosha256.c index be2767a..029659b 100644 --- a/examples/cryptosha256.c +++ b/examples/cryptosha256.c @@ -26,8 +26,12 @@ static int cryptosha256_init(void) struct shash_desc *shash; sha256 = crypto_alloc_shash("sha256", 0, 0); - if (IS_ERR(sha256)) + if (IS_ERR(sha256)) { + pr_err( + "%s(): Failed to allocate sha256 algorithm, enable CONFIG_CRYPTO_SHA256 and try again.\n", + __func__); return -1; + } shash = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(sha256), GFP_KERNEL); diff --git a/lkmpg.tex b/lkmpg.tex index 238921b..07390f3 100644 --- a/lkmpg.tex +++ b/lkmpg.tex @@ -1891,6 +1891,7 @@ To handle crypto stuff, the kernel has its own API enabling common methods of en Calculating and checking the hashes of things is a common operation. Here is a demonstration of how to calculate a sha256 hash within a kernel module. +To provide the sha256 algorithm support, make sure \cpp|CONFIG_CRYPTO_SHA256| is enabled in kernel. \samplec{examples/cryptosha256.c}