EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
passwd.c
Go to the documentation of this file.
1#include "passwd.h"
2
3#include "config.h"
4#include "io.h"
5
6// Default
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};
12
13/*
14 * @brief Load the access code hash from the configuration file.
15 *
16 * Reads the hash from PASSWD_CFG_FILE, which should contain a single line
17 * with the hash in hexadecimal format. The hash is expected to be 64 hex
18 * characters long (representing 32 bytes).
19 * @return 0 on success, negative error code on failure.
20 *
21 */
22int passwd_load(void) {
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}
51
57int passwd_verify(const char *password) {
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}
67
73int passwd_set(const char *new_password) {
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}
#define PASSWD_CFG_FILE
Definition config.h:64
#define SUCCESS
Definition config.h:5
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
void build_cfg_path(const char *fname, char *out, size_t sz)
Definition io.c:87
int _write_file(const char *path, const char *buf, size_t len)
Definition io.c:67
int _read_file(const char *path, char **out_buf)
Definition io.c:12
u8 access_code_hash[SHA256_DIGEST_SIZE]
Definition passwd.c:7
int passwd_load(void)
Definition passwd.c:22
int passwd_set(const char *new_password)
Set a new password by updating the stored hash.
Definition passwd.c:73
int passwd_verify(const char *password)
Verify the provided password against the stored hash.
Definition passwd.c:57
#define PASSWD_HASH_SIZE
Definition passwd.h:12