35#ifndef PACS_SECURITY_AUDIT_LOG_CIPHER_HPP
36#define PACS_SECURITY_AUDIT_LOG_CIPHER_HPP
45#include <unordered_map>
86 std::array<std::uint8_t, audit_cipher_key_bytes>
data_key{};
89 std::array<std::uint8_t, audit_cipher_mac_key_bytes>
mac_key{};
131 [[nodiscard]] VoidResult set_active(
const std::string&
key_id);
140 void forget(
const std::string&
key_id);
143 [[nodiscard]] std::size_t size()
const;
158 [[nodiscard]] VoidResult load_from_env();
172 [[nodiscard]]
static VoidResult decode_key(
173 const std::string& encoded,
175 std::size_t out_size);
179 std::unordered_map<std::string, audit_cipher_key>
keys_;
217 const std::string& plaintext)
const;
231 const std::string& encoded)
const;
237 [[nodiscard]] VoidResult verify_record(
const std::string& encoded)
const;
240 [[nodiscard]]
static bool looks_encrypted(
const std::string& line)
noexcept;
253[[nodiscard]] std::string
base64_encode(
const std::uint8_t* data,
258 std::vector<std::uint8_t>&
out);
261[[nodiscard]]
bool hex_decode(
const std::string& in,
263 std::size_t out_size);
267 const std::uint8_t* b,
268 std::size_t n)
noexcept;
std::string base64_encode(std::string_view input)
Encode data to Base64.
Holds the active encryption key and any retired keys for decryption of historical records.
std::unordered_map< std::string, audit_cipher_key > keys_
audit_key_manager()=default
Encrypts and decrypts single audit log records.
audit_key_manager & keys_
bool constant_time_equal(const std::uint8_t *a, const std::uint8_t *b, std::size_t n) noexcept
Constant-time equality on byte buffers of equal length.
bool base64_decode(const std::string &in, std::vector< std::uint8_t > &out)
Standard base64 decode; returns false on malformed input.
bool hex_decode(const std::string &in, std::uint8_t *out, std::size_t out_size)
Hex decode (lowercase or uppercase); returns false on malformed input.
constexpr std::size_t audit_cipher_mac_key_bytes
HMAC-SHA256 key length in bytes (equal to block size for full strength)
constexpr std::size_t audit_cipher_iv_bytes
GCM IV length in bytes (NIST SP 800-38D recommended 96 bits)
constexpr std::size_t audit_cipher_key_bytes
AES-256 key length in bytes.
constexpr const char * audit_cipher_format_version
constexpr std::size_t audit_cipher_tag_bytes
GCM authentication tag length in bytes.
constexpr const char * audit_cipher_format_tag
Format version string embedded in every encrypted record.
Result<T> type aliases and helpers for PACS system.
A versioned symmetric key pair (data encryption + MAC)
std::array< std::uint8_t, audit_cipher_key_bytes > data_key
256-bit AES data encryption key
void wipe() noexcept
Secure zero helper (wipes key material on destruction)
std::string key_id
Stable identifier (ASCII, non-empty, no '|' character)
std::array< std::uint8_t, audit_cipher_mac_key_bytes > mac_key
256-bit HMAC-SHA256 key