Add M_TryExactPassword

Single-use password technology that exists outside of the enumerated list
This commit is contained in:
toaster 2024-04-12 14:05:15 +01:00
parent 6c0118d491
commit 54d6914b50
2 changed files with 24 additions and 10 deletions

View file

@ -43,22 +43,21 @@ namespace
constexpr const UINT8 kRRSalt[17] = "0L4rlK}{9ay6'VJS"; constexpr const UINT8 kRRSalt[17] = "0L4rlK}{9ay6'VJS";
std::array<UINT8, M_PW_BUF_SIZE> decode_hash(std::string encoded)
{
std::array<UINT8, M_PW_BUF_SIZE> decoded;
if (modp::b64_decode(encoded).size() != decoded.size())
throw std::invalid_argument("hash is incorrectly sized");
std::copy(encoded.begin(), encoded.end(), decoded.begin());
return decoded;
}
struct Pw struct Pw
{ {
Pw(void (*cb)(), const char *encoded_hash) : cb_(cb), hash_(decode_hash(encoded_hash)) {} Pw(void (*cb)(), const char *encoded_hash) : cb_(cb), hash_(decode_hash(encoded_hash)) {}
void (*cb_)(); void (*cb_)();
const std::array<UINT8, M_PW_BUF_SIZE> hash_; const std::array<UINT8, M_PW_BUF_SIZE> hash_;
private:
static std::array<UINT8, M_PW_BUF_SIZE> decode_hash(std::string encoded)
{
std::array<UINT8, M_PW_BUF_SIZE> decoded;
if (modp::b64_decode(encoded).size() != decoded.size())
throw std::invalid_argument("hash is incorrectly sized");
std::copy(encoded.begin(), encoded.end(), decoded.begin());
return decoded;
}
}; };
std::vector<Pw> passwords; std::vector<Pw> passwords;
@ -342,6 +341,20 @@ try_password_e M_TryPassword(const char *password, boolean conditions)
return return_code; return return_code;
} }
boolean M_TryExactPassword(const char *password, const char *encodedhash)
{
// Normalize input casing
std::string key = password;
strlwr(key.data());
UINT8 key_hash[M_PW_HASH_SIZE];
M_HashPassword(key_hash, key.c_str(), kRRSalt);
auto hash = decode_hash(encodedhash);
return (memcmp(key_hash, hash.data(), M_PW_HASH_SIZE) == 0);
}
#ifdef DEVELOP #ifdef DEVELOP
void Command_Crypt_f(void) void Command_Crypt_f(void)
{ {

View file

@ -27,6 +27,7 @@ try_password_e;
void M_PasswordInit(void); void M_PasswordInit(void);
try_password_e M_TryPassword(const char *password, boolean challenges); try_password_e M_TryPassword(const char *password, boolean challenges);
boolean M_TryExactPassword(const char *password, const char *encodedhash);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"