Skip to main content

KeyEncryptionKey

Struct KeyEncryptionKey 

Source
pub struct KeyEncryptionKey(/* private fields */);
Expand description

Key Encryption Key (KEK) for wrapping Data Encryption Keys.

Each tenant has one KEK. The KEK is wrapped by the master key and stored alongside tenant metadata. Deleting a tenant’s wrapped KEK renders all their data cryptographically inaccessible (GDPR “right to erasure”).

§Key Hierarchy Position

MasterKeyProvider
    │
    └── wraps ──► KeyEncryptionKey (this type)
                      │
                      └── wraps ──► DataEncryptionKey

§Example

use kimberlite_crypto::encryption::{
    InMemoryMasterKey, MasterKeyProvider, KeyEncryptionKey, DataEncryptionKey,
};

let master = InMemoryMasterKey::generate();

// Create KEK for a new tenant
let (kek, wrapped_kek) = KeyEncryptionKey::generate_and_wrap(&master);

// Store wrapped_kek.to_bytes() in tenant metadata...

// Later: restore KEK when tenant accesses data
let kek = KeyEncryptionKey::restore(&master, &wrapped_kek).unwrap();

Implementations§

Source§

impl KeyEncryptionKey

Source

pub fn restore( master: &impl MasterKeyProvider, wrapped: &WrappedKey, ) -> Result<Self, CryptoError>

Restores a KEK from its wrapped form (pure, no IO).

Use this when loading a tenant’s KEK from storage.

§Arguments
  • master - The master key provider that originally wrapped this KEK
  • wrapped - The wrapped KEK from storage
§Errors

Returns CryptoError::DecryptionError if:

  • The wrapped key is corrupted
  • The wrong master key is used
Source

pub fn wrap_dek(&self, dek_bytes: &[u8; 32]) -> WrappedKey

Wraps a Data Encryption Key for secure storage.

The wrapped DEK should be stored in the segment header.

Source

pub fn unwrap_dek(&self, wrapped: &WrappedKey) -> Result<[u8; 32], CryptoError>

Unwraps a Data Encryption Key from storage.

§Errors

Returns CryptoError::DecryptionError if:

  • The wrapped key is corrupted
  • The wrong KEK is used
Source

pub fn generate_and_wrap(master: &impl MasterKeyProvider) -> (Self, WrappedKey)

Generates a new KEK and wraps it with the master key.

Returns both the usable KEK and its wrapped form for storage. The wrapped form should be persisted alongside tenant metadata.

This is the imperative shell - it handles IO (randomness) and delegates to a pure internal constructor for the actual construction.

§Arguments
  • master - The master key provider to wrap the KEK
§Returns

A tuple of (usable_kek, wrapped_kek_for_storage).

§Panics

Panics if the OS CSPRNG fails (catastrophic system error).

Trait Implementations§

Source§

impl Drop for KeyEncryptionKey

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Zeroize for KeyEncryptionKey

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> 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V