Skip to main content

Dek

Struct Dek 

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

Per-user 32-byte DEK material. The byte buffer is wiped on Drop via the zeroize crate; callers obtain a Dek from an HSM unwrap — the runtime never derives key material directly (envelope encryption).

A per-DEK monotonic counter drives the deterministic 96-bit nonce for AES-GCM / AES-GCM-SIV under the NIST SP 800-38D §8.2.1 construction (4-byte invocation field = DekConfig::replica_id + 8-byte counter, big-endian). XChaCha20-Poly1305 uses a 192-bit random nonce and leaves the counter alone. Counter exhaustion at u64::MAX surfaces PiiError::DekExhausted so the operator rotates the DEK before any nonce reuse.

replica_id is held by the DEK itself and is immutable after construction — changing it post-hoc would violate L0 A11 pure compute (replay determinism depends on stable nonce bytes). Default single-writer deployments use replica_id = 0; federation builds populate a per-replica id via Dek::with_config.

Dek is intentionally not Clone — two copies of the same key material with independent counters would collide their nonces under AES-GCM (catastrophic integrity loss). Callers must hold a single owner per key; rotation yields a fresh Dek via Dek::from_bytes whose counter starts at 0.

The interior Cell<u64> counter makes Dek implicitly !Sync, so the compiler refuses naive cross-thread sharing. If a deployment genuinely needs to share a DEK across task boundaries (e.g., an L2 async runtime feeding multiple encrypt handlers), wrap it in Arc<Mutex<Dek>> or Arc<parking_lot::Mutex<Dek>> at the caller site — the mutex guards the counter advance and keeps the deterministic-nonce invariant intact. Single-writer deployments (L0 A2 single-thread) hold a plain owned Dek without synchronisation.

Implementations§

Source§

impl Dek

Source

pub fn from_bytes(material: [u8; 32]) -> Self

Construct from a 32-byte buffer using default configuration (replica_id = 0, single-writer). The input is copied; callers remain responsible for wiping their own buffer. Counter starts at 0.

Source

pub fn with_config(material: [u8; 32], config: DekConfig) -> Self

Construct from a 32-byte buffer with an explicit DekConfig. Federation path consumes this entry point with a non-zero replica_id; single-writer deployments use Dek::from_bytes.

Source

pub fn try_from_slice(bytes: &[u8]) -> Result<Self, PiiError>

Construct from a byte slice. Rejects the call with PiiError::InvalidKeyLength when bytes.len() != 32. Counter starts at 0 with default DekConfig.

The length check is a single usize compare against the constant 32 — no byte-by-byte value comparison happens on the reject path, so there is no timing side-channel the subtle crate would mitigate. copy_from_slice runs in time dependent on the buffer length, not its contents.

Trait Implementations§

Source§

impl Debug for Dek

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Dek

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Zeroize for Dek

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§

§

impl !Freeze for Dek

§

impl !RefUnwindSafe for Dek

§

impl Send for Dek

§

impl !Sync for Dek

§

impl Unpin for Dek

§

impl UnsafeUnpin for Dek

§

impl UnwindSafe for Dek

Blanket Implementations§

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<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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.