Files
Library/include/Void/Security/Security.hpp
T

89 lines
3.5 KiB
C++

#pragma once
#include <Void/Functional/Result.hpp>
#include <Void/Security/Types.hpp>
#include <array>
#include <cstddef>
#include <span>
#include <string>
#include <string_view>
#include <vector>
namespace Void::Security
{
/// Cost presets supplied by libsodium for Argon2id password hashing.
/// Moderate is the default for stored account passwords; Interactive is
/// useful where latency and memory are constrained, including tests.
enum class PasswordHashStrength { Interactive, Moderate, Sensitive };
inline constexpr std::size_t default_token_entropy_bytes = 32;
inline constexpr std::size_t secret_key_bytes = 32;
inline constexpr std::size_t secret_nonce_bytes = 24;
inline constexpr std::size_t secret_authentication_bytes = 16;
using SecretKey = std::array<unsigned char, secret_key_bytes>;
/// Wipe a mutable byte buffer with sodium_memzero.
void wipe_bytes(std::span<unsigned char> buffer) noexcept;
/// Constant-time comparison using sodium_memcmp.
[[nodiscard]] bool
constant_time_equal(std::span<const unsigned char> lhs,
std::span<const unsigned char> rhs) noexcept;
/// Generate a URL-safe, unpadded token containing `entropy_bytes` of
/// cryptographically secure randomness.
[[nodiscard]] Void::Result::Result<std::string>
random_token(std::size_t entropy_bytes = default_token_entropy_bytes);
/// Produce a deterministic lowercase hexadecimal BLAKE2b-256 digest.
/// Store this digest instead of the bearer token itself.
[[nodiscard]] Void::Result::Result<std::string>
hash_token(std::string_view token);
/// Verify a token digest without data-dependent comparison timing.
/// Malformed digests are treated as a non-match.
[[nodiscard]] Void::Result::Result<bool>
verify_token(std::string_view token, std::string_view encoded_hash);
/// Hash a password using libsodium's Argon2id encoded-string format.
/// The returned string contains the algorithm, parameters, salt, and hash.
[[nodiscard]] Void::Result::Result<std::string> hash_password(
std::string_view password,
PasswordHashStrength strength = PasswordHashStrength::Moderate);
/// Parse Argon2id cost parameters (`$argon2id$v=19$m=...,t=...,p=...$...`)
/// without executing Argon2id or allocating memory.
[[nodiscard]] Void::Result::Result<Argon2idCost>
parse_argon2id_cost(std::string_view encoded_hash);
/// Verify a password against an encoded Argon2id hash after enforcing input
/// caps and operator-approved memory/iteration/parallelism cost budgets.
[[nodiscard]] Void::Result::Result<bool>
verify_password_budgeted(std::string_view password,
std::string_view encoded_hash,
const PasswordVerifyBudget& budget = {});
/// Verify a password against a libsodium Argon2id encoded hash.
/// Malformed hashes are treated as a non-match.
[[nodiscard]] Void::Result::Result<bool>
verify_password(std::string_view password,
std::string_view encoded_hash);
/// Generate a cryptographically random key for local secret encryption.
[[nodiscard]] Void::Result::Result<SecretKey> random_secret_key();
/// Encrypt and authenticate bytes using the legacy local secret format
/// (24-byte nonce || crypto_secretbox_easy combined MAC + ciphertext).
[[nodiscard]] Void::Result::Result<std::vector<unsigned char>>
seal_secret(std::span<const unsigned char> plaintext,
const SecretKey& key);
/// Authenticate and decrypt an envelope produced by seal_secret.
[[nodiscard]] Void::Result::Result<std::vector<unsigned char>>
open_secret(std::span<const unsigned char> envelope,
const SecretKey& key);
} // namespace Void::Security