lkmpg/examples/cryptosha256.c

69 lines
1.5 KiB
C
Raw Normal View History

/*
* cryptosha256.c
*/
2021-07-22 06:35:24 +08:00
#include <crypto/internal/hash.h>
#include <linux/module.h>
2021-07-22 06:35:24 +08:00
#define SHA256_LENGTH 32
static void show_hash_result(char *plaintext, char *hash_sha256)
2021-07-22 06:35:24 +08:00
{
int i;
char str[SHA256_LENGTH * 2 + 1];
2021-07-22 06:35:24 +08:00
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;
2021-07-22 06:35:24 +08:00
pr_info("%s\n", str);
}
static int __init cryptosha256_init(void)
2021-07-22 06:35:24 +08:00
{
char *plaintext = "This is a test";
2021-07-22 06:35:24 +08:00
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__);
2021-07-22 06:35:24 +08:00
return -1;
}
2021-07-22 06:35:24 +08:00
shash = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(sha256),
GFP_KERNEL);
2021-07-22 06:35:24 +08:00
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 __exit cryptosha256_exit(void)
{
}
2021-07-22 06:35:24 +08:00
module_init(cryptosha256_init);
module_exit(cryptosha256_exit);
MODULE_DESCRIPTION("sha256 hash test");
MODULE_LICENSE("GPL");