#pragma once #include #include #include #include #include #include #include #include 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; /// Wipe a mutable byte buffer with sodium_memzero. void wipe_bytes(std::span buffer) noexcept; /// Constant-time comparison using sodium_memcmp. [[nodiscard]] bool constant_time_equal(std::span lhs, std::span rhs) noexcept; /// Generate a URL-safe, unpadded token containing `entropy_bytes` of /// cryptographically secure randomness. [[nodiscard]] Void::Result::Result 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 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 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 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 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 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 verify_password(std::string_view password, std::string_view encoded_hash); /// Generate a cryptographically random key for local secret encryption. [[nodiscard]] Void::Result::Result 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> seal_secret(std::span plaintext, const SecretKey& key); /// Authenticate and decrypt an envelope produced by seal_secret. [[nodiscard]] Void::Result::Result> open_secret(std::span envelope, const SecretKey& key); } // namespace Void::Security