91 lines
3.0 KiB
C++
91 lines
3.0 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Security/SecureBuffer.hpp>
|
|
#include <Void/Security/Types.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Security
|
|
{
|
|
inline constexpr std::uint8_t kEnvelopeVersionV1 = 1;
|
|
inline constexpr std::uint8_t kEnvelopeAlgXChaCha20Poly1305Ietf = 1;
|
|
inline constexpr std::size_t kEnvelopeKeyBytes = 32;
|
|
inline constexpr std::size_t kEnvelopeNonceBytes = 24;
|
|
inline constexpr std::size_t kEnvelopeTagBytes = 16;
|
|
|
|
struct EnvelopeHeader {
|
|
std::uint8_t version{kEnvelopeVersionV1};
|
|
std::uint8_t algorithm{kEnvelopeAlgXChaCha20Poly1305Ietf};
|
|
std::string key_id{};
|
|
std::uint32_t plaintext_size{0};
|
|
};
|
|
|
|
/// Parse and validate a versioned VSEC envelope header without decrypting.
|
|
/// Never falls back to legacy seal_secret framing.
|
|
[[nodiscard]] Void::Result::Result<EnvelopeHeader>
|
|
inspect_envelope(std::span<const unsigned char> envelope);
|
|
|
|
/// Versioned AEAD key ring supporting key rotation: writes use the single
|
|
/// active key ID, reads resolve the key ID authenticated in the header, and
|
|
/// retirement requires proving no ciphertext still depends on that key ID.
|
|
class SecretKeyRing
|
|
{
|
|
public:
|
|
|
|
SecretKeyRing() = default;
|
|
SecretKeyRing(SecretKeyRing&&) noexcept = default;
|
|
SecretKeyRing& operator=(SecretKeyRing&&) noexcept = default;
|
|
SecretKeyRing(const SecretKeyRing&) = delete;
|
|
SecretKeyRing& operator=(const SecretKeyRing&) = delete;
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
add_key(std::string key_id,
|
|
std::span<const unsigned char> raw_key,
|
|
bool make_active = false,
|
|
LockPolicy lock_policy = LockPolicy::StandardWiped);
|
|
|
|
[[nodiscard]] Void::Result::Result<void> generate_and_add_key(
|
|
std::string key_id, bool make_active = true,
|
|
LockPolicy lock_policy = LockPolicy::StandardWiped);
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
set_active_key(std::string_view key_id);
|
|
|
|
/// Retire a non-active key only after the caller supplies an
|
|
/// inventory of remaining ciphertext key IDs proving none reference
|
|
/// `key_id`.
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
retire_key(std::string_view key_id,
|
|
std::span<const std::string>
|
|
remaining_ciphertext_key_ids);
|
|
|
|
[[nodiscard]] std::optional<std::string>
|
|
active_key_id() const;
|
|
[[nodiscard]] bool has_key(std::string_view key_id) const;
|
|
[[nodiscard]] std::vector<std::string> key_ids() const;
|
|
|
|
[[nodiscard]] Void::Result::Result<std::vector<unsigned char>>
|
|
seal(std::span<const unsigned char> plaintext,
|
|
std::span<const unsigned char> aad = {}) const;
|
|
|
|
[[nodiscard]] Void::Result::Result<SecureBuffer>
|
|
open(std::span<const unsigned char> envelope,
|
|
std::span<const unsigned char> aad = {},
|
|
LockPolicy out_policy = LockPolicy::StandardWiped) const;
|
|
|
|
private:
|
|
|
|
std::map<std::string, SecureBuffer, std::less<>> m_keys{};
|
|
std::string m_active_key_id{};
|
|
};
|
|
|
|
} // namespace Void::Security
|