3133ee5293
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 <peterlin.tw@pm.me>
69 lines
1.5 KiB
C
69 lines
1.5 KiB
C
/*
|
|
* cryptosha256.c
|
|
*/
|
|
#include <crypto/internal/hash.h>
|
|
#include <linux/module.h>
|
|
|
|
#define SHA256_LENGTH 32
|
|
|
|
static void show_hash_result(char *plaintext, char *hash_sha256)
|
|
{
|
|
int i;
|
|
char str[SHA256_LENGTH * 2 + 1];
|
|
|
|
pr_info("sha256 test for string: \"%s\"\n", plaintext);
|
|
for (i = 0; i < SHA256_LENGTH; i++)
|
|
sprintf(&str[i * 2], "%02x", (unsigned char)hash_sha256[i]);
|
|
str[i * 2] = 0;
|
|
pr_info("%s\n", str);
|
|
}
|
|
|
|
static int cryptosha256_init(void)
|
|
{
|
|
char *plaintext = "This is a test";
|
|
char hash_sha256[SHA256_LENGTH];
|
|
struct crypto_shash *sha256;
|
|
struct shash_desc *shash;
|
|
|
|
sha256 = crypto_alloc_shash("sha256", 0, 0);
|
|
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);
|
|
if (!shash)
|
|
return -ENOMEM;
|
|
|
|
shash->tfm = sha256;
|
|
|
|
if (crypto_shash_init(shash))
|
|
return -1;
|
|
|
|
if (crypto_shash_update(shash, plaintext, strlen(plaintext)))
|
|
return -1;
|
|
|
|
if (crypto_shash_final(shash, hash_sha256))
|
|
return -1;
|
|
|
|
kfree(shash);
|
|
crypto_free_shash(sha256);
|
|
|
|
show_hash_result(plaintext, hash_sha256);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void cryptosha256_exit(void)
|
|
{
|
|
}
|
|
|
|
module_init(cryptosha256_init);
|
|
module_exit(cryptosha256_exit);
|
|
|
|
MODULE_DESCRIPTION("sha256 hash test");
|
|
MODULE_LICENSE("GPL");
|