251 lines
7.8 KiB
C++
251 lines
7.8 KiB
C++
#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;
|
|
}
|
|
|
|
Void::Result::Result<SecretKey> random_secret_key()
|
|
{
|
|
if (const auto initialized = ensure_sodium(); !initialized) {
|
|
return Void::Result::fail(std::move(initialized).error());
|
|
}
|
|
|
|
static_assert(secret_key_bytes == crypto_secretbox_KEYBYTES);
|
|
SecretKey key{};
|
|
crypto_secretbox_keygen(key.data());
|
|
return key;
|
|
}
|
|
|
|
Void::Result::Result<std::vector<unsigned char>>
|
|
seal_secret(std::span<const unsigned char> plaintext,
|
|
const SecretKey& key)
|
|
{
|
|
if (const auto initialized = ensure_sodium(); !initialized) {
|
|
return Void::Result::fail(std::move(initialized).error());
|
|
}
|
|
static_assert(secret_nonce_bytes == crypto_secretbox_NONCEBYTES);
|
|
static_assert(secret_authentication_bytes == crypto_secretbox_MACBYTES);
|
|
if (plaintext.size() > std::numeric_limits<std::size_t>::max() -
|
|
secret_nonce_bytes -
|
|
secret_authentication_bytes) {
|
|
return Void::Result::fail(
|
|
"Security::seal_secret: plaintext is too large");
|
|
}
|
|
|
|
std::vector<unsigned char> envelope(secret_nonce_bytes +
|
|
secret_authentication_bytes +
|
|
plaintext.size());
|
|
auto* nonce = envelope.data();
|
|
auto* ciphertext = nonce + secret_nonce_bytes;
|
|
randombytes_buf(nonce, secret_nonce_bytes);
|
|
if (crypto_secretbox_easy(
|
|
ciphertext, plaintext.data(),
|
|
static_cast<unsigned long long>(plaintext.size()), nonce,
|
|
key.data()) != 0) {
|
|
return Void::Result::fail(
|
|
"Security::seal_secret: encryption failed");
|
|
}
|
|
return envelope;
|
|
}
|
|
|
|
Void::Result::Result<std::vector<unsigned char>>
|
|
open_secret(std::span<const unsigned char> envelope,
|
|
const SecretKey& key)
|
|
{
|
|
if (const auto initialized = ensure_sodium(); !initialized) {
|
|
return Void::Result::fail(std::move(initialized).error());
|
|
}
|
|
if (envelope.size() <
|
|
secret_nonce_bytes + secret_authentication_bytes) {
|
|
return Void::Result::fail(
|
|
"Security::open_secret: envelope is truncated");
|
|
}
|
|
|
|
const auto* nonce = envelope.data();
|
|
const auto* ciphertext = nonce + secret_nonce_bytes;
|
|
const auto ciphertext_size = envelope.size() - secret_nonce_bytes;
|
|
std::vector<unsigned char> plaintext(ciphertext_size -
|
|
secret_authentication_bytes);
|
|
if (crypto_secretbox_open_easy(
|
|
plaintext.data(), ciphertext,
|
|
static_cast<unsigned long long>(ciphertext_size), nonce,
|
|
key.data()) != 0) {
|
|
return Void::Result::fail(
|
|
"Security::open_secret: authentication failed");
|
|
}
|
|
return plaintext;
|
|
}
|
|
|
|
} // namespace Void::Security
|