2021-07-22 11:25:32 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* cryptosha256.c
|
2021-07-22 11:25:32 +08:00
|
|
|
*/
|
2021-07-22 06:35:24 +08:00
|
|
|
#include <crypto/internal/hash.h>
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/module.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
#define SHA256_LENGTH 32
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
static void show_hash_result(char *plaintext, char *hash_sha256)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
int i;
|
2021-07-22 06:58:13 +08:00
|
|
|
char str[SHA256_LENGTH * 2 + 1];
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
pr_info("sha256 test for string: \"%s\"\n", plaintext);
|
2021-07-22 06:58:13 +08:00
|
|
|
for (i = 0; i < SHA256_LENGTH; i++)
|
2021-09-02 15:15:07 +08:00
|
|
|
sprintf(&str[i * 2], "%02x", (unsigned char)hash_sha256[i]);
|
2021-07-22 06:58:13 +08:00
|
|
|
str[i * 2] = 0;
|
2021-07-22 06:35:24 +08:00
|
|
|
pr_info("%s\n", str);
|
|
|
|
}
|
|
|
|
|
2021-09-04 17:53:29 +08:00
|
|
|
static int cryptosha256_init(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2021-07-22 06:58:13 +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))
|
|
|
|
return -1;
|
|
|
|
|
2021-07-22 06:58:13 +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;
|
|
|
|
}
|
|
|
|
|
2021-09-04 17:53:29 +08:00
|
|
|
static void cryptosha256_exit(void)
|
2021-09-02 15:15:07 +08:00
|
|
|
{
|
|
|
|
}
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
module_init(cryptosha256_init);
|
|
|
|
module_exit(cryptosha256_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("sha256 hash test");
|
|
|
|
MODULE_LICENSE("GPL");
|