1 parent
fa30098129
commit
8583a479dd
11 files changed
+1763
-129
No files matched your search
@@ -0,0 +1,178 @@
|
||||
#include <Void/Security/Security.hpp>
|
||||
|
||||
#include <sodium.h>
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Security
|
||||
{
|
||||
namespace
|
||||
{
|
||||
struct PasswordLimits {
|
||||
unsigned long long operations;
|
||||
std::size_t memory;
|
||||
};
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> ensure_sodium()
|
||||
{
|
||||
static const bool initialized = sodium_init() >= 0;
|
||||
if (!initialized) {
|
||||
return Void::Result::fail(
|
||||
"Security: libsodium initialization failed");
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::optional<PasswordLimits>
|
||||
password_limits(PasswordHashStrength strength) noexcept
|
||||
{
|
||||
switch (strength) {
|
||||
case PasswordHashStrength::Interactive:
|
||||
return PasswordLimits{crypto_pwhash_OPSLIMIT_INTERACTIVE,
|
||||
crypto_pwhash_MEMLIMIT_INTERACTIVE};
|
||||
case PasswordHashStrength::Moderate:
|
||||
return PasswordLimits{crypto_pwhash_OPSLIMIT_MODERATE,
|
||||
crypto_pwhash_MEMLIMIT_MODERATE};
|
||||
case PasswordHashStrength::Sensitive:
|
||||
return PasswordLimits{crypto_pwhash_OPSLIMIT_SENSITIVE,
|
||||
crypto_pwhash_MEMLIMIT_SENSITIVE};
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::array<unsigned char, crypto_generichash_BYTES>
|
||||
token_digest(std::string_view token) noexcept
|
||||
{
|
||||
std::array<unsigned char, crypto_generichash_BYTES> digest{};
|
||||
(void)crypto_generichash(
|
||||
digest.data(), digest.size(),
|
||||
reinterpret_cast<const unsigned char*>(token.data()),
|
||||
token.size(), nullptr, 0);
|
||||
return digest;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<std::string> random_token(std::size_t entropy_bytes)
|
||||
{
|
||||
if (const auto initialized = ensure_sodium(); !initialized) {
|
||||
return Void::Result::fail(std::move(initialized).error());
|
||||
}
|
||||
if (entropy_bytes == 0) {
|
||||
return Void::Result::fail(
|
||||
"Security::random_token: entropy must be greater than zero");
|
||||
}
|
||||
if (entropy_bytes >
|
||||
(std::numeric_limits<std::size_t>::max() - 2) / 4 * 3) {
|
||||
return Void::Result::fail(
|
||||
"Security::random_token: entropy size is too large");
|
||||
}
|
||||
|
||||
std::vector<unsigned char> random(entropy_bytes);
|
||||
randombytes_buf(random.data(), random.size());
|
||||
|
||||
const auto encoded_size = sodium_base64_ENCODED_LEN(
|
||||
random.size(), sodium_base64_VARIANT_URLSAFE_NO_PADDING);
|
||||
std::string encoded(encoded_size, '\0');
|
||||
const auto* result = sodium_bin2base64(
|
||||
encoded.data(), encoded.size(), random.data(), random.size(),
|
||||
sodium_base64_VARIANT_URLSAFE_NO_PADDING);
|
||||
sodium_memzero(random.data(), random.size());
|
||||
if (result == nullptr) {
|
||||
return Void::Result::fail(
|
||||
"Security::random_token: base64 encoding failed");
|
||||
}
|
||||
encoded.resize(std::strlen(encoded.c_str()));
|
||||
return encoded;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string> hash_token(std::string_view token)
|
||||
{
|
||||
if (const auto initialized = ensure_sodium(); !initialized) {
|
||||
return Void::Result::fail(std::move(initialized).error());
|
||||
}
|
||||
|
||||
const auto digest = token_digest(token);
|
||||
std::array<char, crypto_generichash_BYTES * 2 + 1> encoded{};
|
||||
(void)sodium_bin2hex(encoded.data(), encoded.size(), digest.data(),
|
||||
digest.size());
|
||||
return std::string{encoded.data(), encoded.size() - 1};
|
||||
}
|
||||
|
||||
Void::Result::Result<bool> verify_token(std::string_view token,
|
||||
std::string_view encoded_hash)
|
||||
{
|
||||
if (const auto initialized = ensure_sodium(); !initialized) {
|
||||
return Void::Result::fail(std::move(initialized).error());
|
||||
}
|
||||
|
||||
constexpr auto encoded_length = crypto_generichash_BYTES * 2;
|
||||
if (encoded_hash.size() != encoded_length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::array<unsigned char, crypto_generichash_BYTES> expected{};
|
||||
std::size_t decoded_length = 0;
|
||||
if (sodium_hex2bin(expected.data(), expected.size(),
|
||||
encoded_hash.data(), encoded_hash.size(), nullptr,
|
||||
&decoded_length, nullptr) != 0 ||
|
||||
decoded_length != expected.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto actual = token_digest(token);
|
||||
return sodium_memcmp(actual.data(), expected.data(), actual.size()) ==
|
||||
0;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string>
|
||||
hash_password(std::string_view password, PasswordHashStrength strength)
|
||||
{
|
||||
if (const auto initialized = ensure_sodium(); !initialized) {
|
||||
return Void::Result::fail(std::move(initialized).error());
|
||||
}
|
||||
if (password.size() > crypto_pwhash_PASSWD_MAX) {
|
||||
return Void::Result::fail(
|
||||
"Security::hash_password: password is too long");
|
||||
}
|
||||
|
||||
const auto limits = password_limits(strength);
|
||||
if (!limits) {
|
||||
return Void::Result::fail(
|
||||
"Security::hash_password: invalid strength");
|
||||
}
|
||||
|
||||
std::array<char, crypto_pwhash_STRBYTES> encoded{};
|
||||
if (crypto_pwhash_str_alg(
|
||||
encoded.data(), password.data(),
|
||||
static_cast<unsigned long long>(password.size()),
|
||||
limits->operations, limits->memory,
|
||||
crypto_pwhash_ALG_ARGON2ID13) != 0) {
|
||||
return Void::Result::fail(
|
||||
"Security::hash_password: Argon2id hashing failed");
|
||||
}
|
||||
return std::string{encoded.data()};
|
||||
}
|
||||
|
||||
Void::Result::Result<bool> verify_password(std::string_view password,
|
||||
std::string_view encoded_hash)
|
||||
{
|
||||
if (const auto initialized = ensure_sodium(); !initialized) {
|
||||
return Void::Result::fail(std::move(initialized).error());
|
||||
}
|
||||
if (password.size() > crypto_pwhash_PASSWD_MAX ||
|
||||
encoded_hash.empty() ||
|
||||
encoded_hash.find('\0') != std::string_view::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string terminated_hash{encoded_hash};
|
||||
return crypto_pwhash_str_verify(
|
||||
terminated_hash.c_str(), password.data(),
|
||||
static_cast<unsigned long long>(password.size())) == 0;
|
||||
}
|
||||
|
||||
} // namespace Void::Security
|
||||
Reference in new issue
Block a user