feat(security): add SecureBuffer, VSEC envelopes, authorization, and SecurityAsync Authenticator

This commit is contained in:
faumaray committed 2026-11-25 20:44:31 +03:00
1 parent eb42228604
commit 713d2dc656
20 files changed
+4070 -43

No files matched your search

+152
View File
@@ -0,0 +1,152 @@
#include <Void/Security/SecureBuffer.hpp>
#include <sodium.h>
#include <atomic>
#include <cstring>
#include <new>
#include <utility>
namespace Void::Security
{
namespace
{
std::atomic<std::uint64_t> g_total_wiped_bytes{0};
[[nodiscard]] Void::Result::Result<void> ensure_sodium_buf()
{
if (auto fault = Detail::maybe_inject_fault(
FaultOperation::sodium_init, "Security::SecureBuffer");
fault.has_value()) {
return std::unexpected{std::move(*fault)};
}
static const bool ok = sodium_init() >= 0;
if (!ok) {
return std::unexpected{make_security_error(
SecurityErrorCode::backend_error,
"Security::SecureBuffer: libsodium initialization failed")};
}
return {};
}
} // namespace
SecureBuffer::~SecureBuffer() noexcept { clear(); }
SecureBuffer::SecureBuffer(SecureBuffer&& other) noexcept
: m_data(std::exchange(other.m_data, nullptr)),
m_size(std::exchange(other.m_size, 0)),
m_locked(std::exchange(other.m_locked, false))
{
}
SecureBuffer& SecureBuffer::operator=(SecureBuffer&& other) noexcept
{
if (this != &other) {
clear();
m_data = std::exchange(other.m_data, nullptr);
m_size = std::exchange(other.m_size, 0);
m_locked = std::exchange(other.m_locked, false);
}
return *this;
}
void SecureBuffer::clear() noexcept
{
if (m_data != nullptr) {
if (m_size > 0) {
sodium_memzero(m_data, m_size);
g_total_wiped_bytes.fetch_add(m_size,
std::memory_order_relaxed);
}
if (m_locked) {
sodium_free(m_data);
} else {
delete[] m_data;
}
m_data = nullptr;
m_size = 0;
m_locked = false;
}
}
std::uint64_t SecureBuffer::total_wiped_bytes() noexcept
{
return g_total_wiped_bytes.load(std::memory_order_relaxed);
}
Void::Result::Result<SecureBuffer>
SecureBuffer::allocate(std::size_t size, LockPolicy policy)
{
if (size > SecurityLimits::kMaxSecretPlaintextBytes) {
return std::unexpected{make_security_error(
SecurityErrorCode::input_too_large,
"Security::SecureBuffer: allocation exceeds 16 MiB cap")};
}
if (auto init = ensure_sodium_buf(); !init.has_value()) {
return std::unexpected{std::move(init.error())};
}
if (size == 0) {
return SecureBuffer{};
}
if (policy == LockPolicy::LockedRequired ||
policy == LockPolicy::LockedOptional) {
auto fault = Detail::maybe_inject_fault(
FaultOperation::locked_alloc,
"Security::SecureBuffer::allocate");
void* raw = fault.has_value() ? nullptr : sodium_malloc(size);
if (raw != nullptr) {
sodium_memzero(raw, size);
SecureBuffer buf;
buf.m_data = static_cast<unsigned char*>(raw);
buf.m_size = size;
buf.m_locked = true;
return buf;
}
if (policy == LockPolicy::LockedRequired) {
return std::unexpected{make_security_error(
SecurityErrorCode::locked_memory_unavailable,
"Security::SecureBuffer: required locked allocation "
"failed")};
}
}
unsigned char* raw = new (std::nothrow) unsigned char[size];
if (raw == nullptr) {
return std::unexpected{make_security_error(
SecurityErrorCode::backend_error,
"Security::SecureBuffer: allocation failed")};
}
sodium_memzero(raw, size);
SecureBuffer buf;
buf.m_data = raw;
buf.m_size = size;
buf.m_locked = false;
return buf;
}
Void::Result::Result<SecureBuffer>
SecureBuffer::from_bytes(std::span<const unsigned char> data,
LockPolicy policy)
{
auto buf = allocate(data.size(), policy);
if (!buf.has_value()) {
return std::unexpected{std::move(buf.error())};
}
if (!data.empty()) {
std::memcpy(buf->m_data, data.data(), data.size());
}
return buf;
}
Void::Result::Result<SecureBuffer>
SecureBuffer::from_string(std::string_view text, LockPolicy policy)
{
return from_bytes(
std::span<const unsigned char>{
reinterpret_cast<const unsigned char*>(text.data()),
text.size()},
policy);
}
} // namespace Void::Security