68 lines
2.6 KiB
C++
68 lines
2.6 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Result.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>;
|
|
|
|
/// 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);
|
|
|
|
/// 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. The returned portable envelope is
|
|
/// nonce || ciphertext || authentication tag.
|
|
[[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
|