EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
crypto.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define AES_BLOCK_SIZE   16
 
#define SHA256_DIGEST_SIZE   32
 

Functions

int encrypt_buffer (const char *in, size_t in_len, char **out, size_t *out_len)
 Encrypts a buffer using AES-128 in CBC mode.
 
int decrypt_buffer (const char *in, size_t in_len, char **out, size_t *out_len)
 Decrypts a buffer using AES-128 in CBC mode.
 
int hash_string (const char *input, u8 *digest)
 Hashes a string using SHA-256.
 
bool are_hash_equals (const u8 *h1, const u8 *h2)
 Compares two SHA-256 hashes for equality.
 
void hash_to_str (const u8 *digest, char *output)
 Converts a SHA-256 hash to a hexadecimal string.
 

Macro Definition Documentation

◆ AES_BLOCK_SIZE

#define AES_BLOCK_SIZE   16

Definition at line 7 of file crypto.h.

◆ SHA256_DIGEST_SIZE

#define SHA256_DIGEST_SIZE   32

Definition at line 9 of file crypto.h.

Function Documentation

◆ are_hash_equals()

bool are_hash_equals ( const u8 *  h1,
const u8 *  h2 
)

Compares two SHA-256 hashes for equality.

Parameters
h1First hash to compare.
h2Second hash to compare.
Returns
true if the hashes are equal, false otherwise.

Definition at line 60 of file hash.c.

60 {
61 if (!h1 || !h2)
62 return false;
63
64 return (memcmp(h1, h2, SHA256_DIGEST_SIZE) == 0) ? true : false;
65}
#define SHA256_DIGEST_SIZE
Definition crypto.h:9

◆ decrypt_buffer()

int decrypt_buffer ( const char *  in,
size_t  in_len,
char **  out,
size_t *  out_len 
)

Decrypts a buffer using AES-128 in CBC mode.

Parameters
inInput buffer to decrypt.
in_lenLength of the input buffer.
outPointer to the output buffer (allocated within the function).
out_lenPointer to the length of the output buffer.
Returns
0 on success, negative error code on failure.

Definition at line 335 of file aes.c.

335 {
336 return _crypt_buffer(false, in, in_len, out, out_len);
337}
static int _crypt_buffer(bool encrypt, const char *in, size_t in_len, char **out, size_t *out_len)
Encrypts or decrypts a buffer using AES-128 in CBC mode.
Definition aes.c:186

◆ encrypt_buffer()

int encrypt_buffer ( const char *  in,
size_t  in_len,
char **  out,
size_t *  out_len 
)

Encrypts a buffer using AES-128 in CBC mode.

Parameters
inInput buffer to encrypt.
in_lenLength of the input buffer.
outPointer to the output buffer (allocated within the function).
out_lenPointer to the length of the output buffer.
Returns
0 on success, negative error code on failure.

Definition at line 322 of file aes.c.

322 {
323 return _crypt_buffer(true, in, in_len, out, out_len);
324}

◆ hash_string()

int hash_string ( const char *  input,
u8 *  digest 
)

Hashes a string using SHA-256.

Parameters
inputInput string to hash.
digestOutput buffer for the hash (must be at least SHA256_DIGEST_SIZE bytes).
Returns
0 on success, negative error code on failure.

Definition at line 17 of file hash.c.

17 {
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}

◆ hash_to_str()

void hash_to_str ( const u8 *  digest,
char *  output 
)

Converts a SHA-256 hash to a hexadecimal string.

Parameters
digestInput hash to convert.
outputOutput buffer for the hexadecimal string (must be at least SHA256_DIGEST_SIZE * 2 + 1 bytes).

Definition at line 74 of file hash.c.

74 {
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}