Security: add a public CSPRNG bytes API (VL-SEC-01)
This commit is contained in:
1 parent
0002e74a7d
commit
54d143be93
3 files changed
+56
No files matched your search
@@ -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