diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-05-16 13:35:50 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-05-16 13:35:50 +0200 |
| commit | c589f92ed0f6e462a976c64d533c1d8a21b2a3ba (patch) | |
| tree | d08e2dffd0baff10d2660091decbb36064e16c56 /dev/kernel/src/User.cc | |
| parent | 85c3784b85e701389adfc43e7c222cc90bed953d (diff) | |
feat(kernel): User doesn't store the password directly anymore, it is hashed
under a 64-bit FNV algorithm.
why?
- Better security, so that we're sure that no one else knows about the
password.
also:
- Rename super to MGMT (Management), as it manages a NeKernel machine.
- Added a copy of cxxdrv in the nekernel source tree.
- Working on the custom manual parser for NeKernel. (PoC)
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/kernel/src/User.cc')
| -rw-r--r-- | dev/kernel/src/User.cc | 77 |
1 files changed, 18 insertions, 59 deletions
diff --git a/dev/kernel/src/User.cc b/dev/kernel/src/User.cc index 3e6aeeba..c1a5ca94 100644 --- a/dev/kernel/src/User.cc +++ b/dev/kernel/src/User.cc @@ -29,20 +29,23 @@ namespace Detail { /// \param password password to hash. /// \return the hashed password //////////////////////////////////////////////////////////// - Int32 user_standard_token_generator(Char* password, const Char* in_password, User* user, - SizeT length) { + STATIC UInt64 user_fnv_generator(const Char* password, User* user) { if (!password || !user) return 1; if (*password == 0) return 1; - kout << "user_standard_token_generator: Hashing user password...\r"; + kout << "user_fnv_generator: Hashing user password...\r"; - for (SizeT i_pass = 0UL; i_pass < length; ++i_pass) { - Char cur_chr = in_password[i_pass]; + const UInt64 FNV_OFFSET_BASIS = 0xcbf29ce484222325ULL; + const UInt64 FNV_PRIME = 0x100000001b3ULL; - password[i_pass] = cur_chr | (user->IsStdUser() ? kStdUserType : kSuperUserType); + UInt64 hash = FNV_OFFSET_BASIS; + + while (*password) { + hash ^= (Utf8Char) (*password++); + hash *= FNV_PRIME; } - kout << "user_standard_token_generator: Hashed user password.\r"; + kout << "user_fnv_generator: Hashed user password.\r"; return 0; } @@ -68,70 +71,26 @@ User::User(const UserRingKind& ring_kind, const Char* user_name) : mUserRing(rin //////////////////////////////////////////////////////////// User::~User() = default; -Bool User::Save(const UserPublicKey password_to_fill) noexcept { - if (!password_to_fill || *password_to_fill == 0) return No; - - SizeT len = rt_string_len(password_to_fill); - - UserPublicKey password = new UserPublicKeyType[len]; - - MUST_PASS(password); - - rt_set_memory(password, 0, len); - - // fill data first, generate hash. - // return false on error. - - rt_copy_memory((VoidPtr) password_to_fill, password, len); - - if (!Detail::user_standard_token_generator(password, password_to_fill, this, len)) { - delete[] password; - password = nullptr; - - return No; - } - - // then store password. - - rt_copy_memory(password, this->mUserKey, rt_string_len(password_to_fill)); +Bool User::Save(const UserPublicKey password) noexcept { + if (!password || *password == 0) return No; - delete[] password; - password = nullptr; + this->mUserFNV = Detail::user_fnv_generator(password, this); kout << "User::Save: Saved password successfully...\r"; return Yes; } -Bool User::Matches(const UserPublicKey password_to_fill) noexcept { - if (!password_to_fill || *password_to_fill) return No; - - SizeT len = rt_string_len(password_to_fill); - - Char* password = new Char[len]; - MUST_PASS(password); - - // fill data first, generate hash. - // return false on error. - - rt_copy_memory((VoidPtr) password_to_fill, password, len); - - if (!Detail::user_standard_token_generator(password, password_to_fill, this, len)) { - delete[] password; - password = nullptr; - - return No; - } - - kout << "User::Matches: Validating hashed passwords...\r"; +Bool User::Login(const UserPublicKey password) noexcept { + if (!password || !*password) return No; // now check if the password matches. - if (rt_string_cmp(password, this->mUserKey, rt_string_len(this->mUserKey)) == 0) { - kout << "User::Matches: Password matches.\r"; + if (this->mUserFNV == Detail::user_fnv_generator(password, this)) { + kout << "User::Login: Password matches.\r"; return Yes; } - kout << "User::Matches: Password doesn't match.\r"; + kout << "User::Login: Password doesn't match.\r"; return No; } |
