pub struct EncryptionEngine { /* private fields */ }Expand description
Data-at-rest encryption engine.
Wraps AES-256-GCM-SIV with random nonces. Thread-safe (the cipher
is Send + Sync and nonce generation uses OS randomness).
Implementations§
Source§impl EncryptionEngine
impl EncryptionEngine
Sourcepub fn new(key: &[u8; 32]) -> Self
pub fn new(key: &[u8; 32]) -> Self
Create an encryption engine with the given 256-bit key.
The key must be exactly 32 bytes. Typically loaded from
Kubernetes Secrets or the SOCHDB_ENCRYPTION_KEY env var.
Sourcepub fn from_key(key: &EncryptionKey) -> Self
pub fn from_key(key: &EncryptionKey) -> Self
Create an encryption engine from a zeroize-on-drop EncryptionKey.
Preferred over Self::new on the live path: the caller holds the key
material in a wiping container rather than a bare [u8; 32].
Sourcepub fn disabled() -> Self
pub fn disabled() -> Self
Create a disabled (passthrough) encryption engine.
encrypt() and decrypt() are identity operations when disabled.
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Whether encryption is active.
Sourcepub fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>, EncryptionError>
pub fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>, EncryptionError>
Encrypt a plaintext block.
Returns [version(1) | nonce(12) | ciphertext+tag(N+16)].
§Performance
~4 GB/s on x86_64 with AES-NI. The overhead is the 13-byte header plus 16-byte auth tag per block.
Sourcepub fn encrypt_with_aad(
&self,
plaintext: &[u8],
aad: &[u8],
) -> Result<Vec<u8>, EncryptionError>
pub fn encrypt_with_aad( &self, plaintext: &[u8], aad: &[u8], ) -> Result<Vec<u8>, EncryptionError>
Encrypt a plaintext block, binding aad as additional authenticated data.
Returns [version(1) | nonce(12) | ciphertext+tag(N+16)]. The aad is
NOT stored in the output — it is authenticated only, and the reader MUST
reconstruct the identical aad (e.g. {format_version, db_uuid, dek_epoch, record_LSN}) from its own trusted state. Binding framing/
position as AAD is what prevents an attacker (or a corrupt/misdirected
write) from reordering, duplicating, splicing, or downgrading WAL records
— GCM-SIV alone authenticates only the isolated record body.
AAD scope is part of the on-disk format and cannot be widened later without a format break, so callers must commit to the full AAD tuple from the first encrypted byte written.
Sourcepub fn decrypt(&self, encrypted: &[u8]) -> Result<Vec<u8>, EncryptionError>
pub fn decrypt(&self, encrypted: &[u8]) -> Result<Vec<u8>, EncryptionError>
Decrypt an encrypted block produced by encrypt().
Validates the version byte and authentication tag.
Sourcepub fn decrypt_with_aad(
&self,
encrypted: &[u8],
aad: &[u8],
) -> Result<Vec<u8>, EncryptionError>
pub fn decrypt_with_aad( &self, encrypted: &[u8], aad: &[u8], ) -> Result<Vec<u8>, EncryptionError>
Decrypt an encrypted block, verifying it against the same aad the writer
bound via Self::encrypt_with_aad. A mismatched aad (e.g. a record
that was moved to a different LSN/position) fails authentication exactly
like a wrong key or tampered ciphertext — surfaced as
EncryptionError::DecryptFailed, which callers MUST treat as a hard
error, never as a clean end-of-stream.
Sourcepub fn encrypt_in_place(
&self,
buffer: &mut Vec<u8>,
) -> Result<(), EncryptionError>
pub fn encrypt_in_place( &self, buffer: &mut Vec<u8>, ) -> Result<(), EncryptionError>
Encrypt in-place for zero-copy WAL append.
Prepends the header to the buffer and encrypts the payload region. The buffer is resized to accommodate the header + auth tag.
Auto Trait Implementations§
impl Freeze for EncryptionEngine
impl RefUnwindSafe for EncryptionEngine
impl Send for EncryptionEngine
impl Sync for EncryptionEngine
impl Unpin for EncryptionEngine
impl UnsafeUnpin for EncryptionEngine
impl UnwindSafe for EncryptionEngine
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more