EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
hash.c
Go to the documentation of this file.
1#include <crypto/hash.h>
2#include <linux/crypto.h>
3#include <linux/err.h>
4#include <linux/slab.h>
5#include <linux/string.h>
6
7#include "crypto.h"
8
17int hash_string(const char *input, u8 *digest) {
18 struct crypto_shash *tfm; // Hash transformation handle
19 struct shash_desc *shash; // Hash descriptor
20 char *desc_buffer; // Buffer for hash descriptor
21 int desc_size, ret;
22
23 if (!input || !digest)
24 return -EINVAL;
25
26 // Allocate hash transformation for SHA-256
27 tfm = crypto_alloc_shash("sha256", 0, 0);
28 if (IS_ERR(tfm)) {
29 pr_err("Erreur allocation tfm SHA-256\n");
30 return PTR_ERR(tfm);
31 }
32
33 // Allocate memory for hash descriptor
34 desc_size = sizeof(struct shash_desc) + crypto_shash_descsize(tfm);
35 desc_buffer = kmalloc(desc_size, GFP_KERNEL);
36 if (!desc_buffer) {
37 crypto_free_shash(tfm);
38 return -ENOMEM;
39 }
40
41 shash = (struct shash_desc *)desc_buffer;
42 shash->tfm = tfm;
43
44 // Compute the hash
45 ret = crypto_shash_digest(shash, input, strlen(input), digest);
46
47 kfree(desc_buffer); // Free descriptor buffer
48 crypto_free_shash(tfm); // Free hash transformation
49
50 return ret;
51}
52
60bool are_hash_equals(const u8 *h1, const u8 *h2) {
61 if (!h1 || !h2)
62 return false;
63
64 return (memcmp(h1, h2, SHA256_DIGEST_SIZE) == 0) ? true : false;
65}
66
74void hash_to_str(const u8 *digest, char *output) {
75 int i;
76
77 if (!digest || !output) {
78 pr_err("hash_to_str: digest or output NULL\n");
79 return;
80 }
81
82 // Convert each byte of the hash to a 2-character hexadecimal representation
83 for (i = 0; i < SHA256_DIGEST_SIZE; i++)
84 sprintf(output + (i * 2), "%02x", digest[i]);
85 output[SHA256_DIGEST_SIZE * 2] = '\0'; // Null-terminate the string
86}
#define SHA256_DIGEST_SIZE
Definition crypto.h:9
bool are_hash_equals(const u8 *h1, const u8 *h2)
Compares two SHA-256 hashes for equality.
Definition hash.c:60
int hash_string(const char *input, u8 *digest)
Hashes a string using SHA-256.
Definition hash.c:17
void hash_to_str(const u8 *digest, char *output)
Converts a SHA-256 hash to a hexadecimal string.
Definition hash.c:74