Merge vl-sec-01-manual: public CSPRNG bytes API (VL-SEC-01)
This commit is contained in:
commit
a29c9475f2
3 files changed
+56
No files matched your search
@@ -37,6 +37,12 @@ namespace Void::Security
|
||||
[[nodiscard]] Void::Result::Result<std::string>
|
||||
random_token(std::size_t entropy_bytes = default_token_entropy_bytes);
|
||||
|
||||
/// Fill `buffer` with cryptographically secure random bytes from the
|
||||
/// platform CSPRNG. An empty buffer is a successful no-op; on failure the
|
||||
/// destination is wiped so no partial randomness is left behind.
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
random_bytes(std::span<unsigned char> buffer);
|
||||
|
||||
/// Produce a deterministic lowercase hexadecimal BLAKE2b-256 digest.
|
||||
/// Store this digest instead of the bearer token itself.
|
||||
[[nodiscard]] Void::Result::Result<std::string>
|
||||
|
||||
@@ -186,6 +186,24 @@ namespace Void::Security
|
||||
return encoded;
|
||||
}
|
||||
|
||||
Void::Result::Result<void> random_bytes(std::span<unsigned char> buffer)
|
||||
{
|
||||
if (const auto initialized = ensure_sodium(); !initialized) {
|
||||
return std::unexpected{initialized.error()};
|
||||
}
|
||||
if (buffer.empty()) {
|
||||
return {};
|
||||
}
|
||||
if (auto fault = Detail::maybe_inject_fault(
|
||||
FaultOperation::random_bytes, "Security::random_bytes");
|
||||
fault.has_value()) {
|
||||
wipe_bytes(buffer);
|
||||
return std::unexpected{std::move(*fault)};
|
||||
}
|
||||
randombytes_buf(buffer.data(), buffer.size());
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string> hash_token(std::string_view token)
|
||||
{
|
||||
if (const auto initialized = ensure_sodium(); !initialized) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
@@ -320,3 +321,34 @@ Void::Testing::Test security_authorization_default_deny_and_limits()
|
||||
.condition = std::move(deep)}})
|
||||
.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test security_random_bytes_fills_and_fails_closed()
|
||||
{
|
||||
using namespace Void::Security;
|
||||
|
||||
std::array<unsigned char, 32> first{};
|
||||
std::array<unsigned char, 32> second{};
|
||||
first.fill(0xAA);
|
||||
second.fill(0xAA);
|
||||
Void::Testing::require(random_bytes(first).has_value());
|
||||
Void::Testing::require(random_bytes(second).has_value());
|
||||
Void::Testing::expect(!constant_time_equal(first, second));
|
||||
Void::Testing::expect(std::any_of(first.begin(), first.end(),
|
||||
[](unsigned char b) { return b != 0xAA; }));
|
||||
|
||||
// An empty destination is a successful no-op.
|
||||
std::array<unsigned char, 0> empty{};
|
||||
Void::Testing::expect(random_bytes(empty).has_value());
|
||||
|
||||
// Injected failure wipes the destination instead of leaving partial bytes.
|
||||
{
|
||||
ScopedFaultGuard fault{FaultConfig{FaultOperation::random_bytes,
|
||||
SecurityErrorCode::backend_error, 0}};
|
||||
std::array<unsigned char, 16> output{};
|
||||
output.fill(0xCC);
|
||||
auto result = random_bytes(output);
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(std::all_of(
|
||||
output.begin(), output.end(), [](unsigned char b) { return b == 0; }));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user