nhmk/examples/cryptosha256.c
linD026 9289bfe59c
Fix the warnings raised by Sparse (#92)
Sparse[1] is a semantic parser, capable of finding out the potential
problems of Linux kernel code. This patch fixed the warnings.

[1] https://www.kernel.org/doc/html/latest/dev-tools/sparse.html
2021-09-04 17:53:29 +08:00

65 lines
1.4 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))
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");