Skip to main content

DatabaseCipher

Struct DatabaseCipher 

Source
pub struct DatabaseCipher { /* private fields */ }
Expand description

Cryptographic engine managing page-level encryption and verification.

Deliberately does not implement Clone so raw key material is never silently duplicated in memory; sensitive key and salt bytes are scrubbed via zeroize on Drop.

Implementations§

Source§

impl DatabaseCipher

Source

pub fn new(key: [u8; 32], salt: [u8; 16]) -> Self

Initialize cipher with raw 256-bit key and 16-byte salt

Source

pub fn from_passphrase(passphrase: &str, salt: [u8; 16]) -> Self

Initialize cipher by deriving 256-bit key from passphrase and salt via PBKDF2

Source

pub fn from_passphrase_with_iterations( passphrase: &str, salt: [u8; 16], iterations: u32, ) -> Self

Initialize cipher with custom PBKDF2 iteration count

Source

pub fn salt(&self) -> &[u8; 16]

Return reference to database salt

Source

pub fn key(&self) -> &[u8; 32]

Return raw key bytes

Source

pub fn epoch(&self) -> u32

Return database epoch

Source

pub fn set_epoch(&mut self, epoch: u32)

Set database epoch (e.g. incremented after major checkpoints / rollbacks)

Source

pub fn derive_nonce_with_seq( &self, page_id: PageId, sequence: u32, epoch: u32, ) -> [u8; 12]

Derive unique 12-byte AEAD nonce with monotonic write sequence and epoch Formally satisfies RFC 8439 Nonce Uniqueness Requirement: Nonce = Epoch (4B) || PageId (4B) || Sequence (4B)

Source

pub fn derive_nonce(&self, page_id: PageId) -> [u8; 12]

Default 12-byte nonce for backward compatibility (sequence 0, epoch 1)

Source

pub fn generate_kcv(&self) -> Result<[u8; 16]>

Generate 16-byte Key Check Value (KCV) tag for header validation.

Returns Err if the ChaCha20-Poly1305 cipher fails, so callers always know when KCV generation is impossible rather than silently receiving a zeroed tag that would bypass authentication checks.

Source

pub fn verify_kcv(&self, stored_kcv: &[u8; 16]) -> bool

Constant-time verification of stored KCV against candidate key. Uses the audited subtle crate (ConstantTimeEq) to prevent timing side-channel leakage. Returns false (not-equal) on internal error rather than panicking.

Source

pub fn encrypt_page(&self, page_id: PageId, plaintext: &[u8]) -> Result<Vec<u8>>

Encrypt a 4,096-byte in-memory page into a 4,096-byte disk page using XChaCha20-Poly1305 with a cryptographically secure 24-byte (192-bit) random nonce from the OS CSPRNG.

Every write produces a completely distinct ciphertext even if page contents are identical, guaranteeing absolute immunity against nonce reuse attacks per RFC 8439.

Source

pub fn encrypt_page_with_nonce( &self, page_id: PageId, nonce_bytes: &[u8; 24], plaintext: &[u8], ) -> Result<Vec<u8>>

Encrypt a 4,096-byte in-memory page with an explicitly provided 24-byte nonce (useful for deterministic tests)

Source

pub fn encrypt_page_with_seq( &self, page_id: PageId, sequence: u32, epoch: u32, plaintext: &[u8], ) -> Result<Vec<u8>>

Legacy compatibility wrapper: forwards to encrypt_page_with_nonce using a 24-byte padded nonce

Source

pub fn decrypt_page(&self, page_id: PageId, disk_page: &[u8]) -> Result<Vec<u8>>

Decrypt a 4,096-byte disk page back into an in-memory page using XChaCha20-Poly1305.

Reads the 24-byte random XNonce from bytes [4056..4080] and the 16-byte Poly1305 tag from bytes [4080..4096]. Authenticates Associated Data against page_id.

Source

pub fn decrypt_page_with_seq( &self, page_id: PageId, _sequence: u32, _epoch: u32, disk_page: &[u8], ) -> Result<Vec<u8>>

Legacy compatibility wrapper: forwards directly to self-contained decrypt_page

Trait Implementations§

Source§

impl Drop for DatabaseCipher

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Zeroize for DatabaseCipher

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.