pub struct DataEncryptionKey(/* private fields */);Expand description
Data Encryption Key (DEK) for encrypting actual record data.
Each segment (chunk of the log) has its own DEK. The DEK is wrapped by the tenant’s KEK and stored in the segment header.
§Key Hierarchy Position
MasterKeyProvider
│
└── wraps ──► KeyEncryptionKey
│
└── wraps ──► DataEncryptionKey (this type)
│
└── encrypts ──► Record data§Example
use kimberlite_crypto::encryption::{
InMemoryMasterKey, KeyEncryptionKey, DataEncryptionKey,
Nonce, encrypt, decrypt,
};
let master = InMemoryMasterKey::generate();
let (kek, _) = KeyEncryptionKey::generate_and_wrap(&master);
// Create DEK for a new segment
let (dek, wrapped_dek) = DataEncryptionKey::generate_and_wrap(&kek);
// Encrypt data
let nonce = Nonce::from_position(0);
let ciphertext = encrypt(dek.encryption_key(), &nonce, b"secret data");
// Decrypt data
let plaintext = decrypt(dek.encryption_key(), &nonce, &ciphertext).unwrap();
assert_eq!(plaintext, b"secret data");Implementations§
Source§impl DataEncryptionKey
impl DataEncryptionKey
Sourcepub fn restore(
kek: &KeyEncryptionKey,
wrapped: &WrappedKey,
) -> Result<Self, CryptoError>
pub fn restore( kek: &KeyEncryptionKey, wrapped: &WrappedKey, ) -> Result<Self, CryptoError>
Restores a DEK from its wrapped form (pure, no IO).
Use this when loading a segment’s DEK from its header.
§Arguments
kek- The KEK that originally wrapped this DEKwrapped- The wrapped DEK from the segment header
§Errors
Returns CryptoError::DecryptionError if:
- The wrapped key is corrupted
- The wrong KEK is used
Sourcepub fn encryption_key(&self) -> &EncryptionKey
pub fn encryption_key(&self) -> &EncryptionKey
Sourcepub fn generate_and_wrap(kek: &KeyEncryptionKey) -> (Self, WrappedKey)
pub fn generate_and_wrap(kek: &KeyEncryptionKey) -> (Self, WrappedKey)
Generates a new DEK and wraps it with the KEK.
Returns both the usable DEK and its wrapped form for storage. The wrapped form should be stored in the segment header.
This is the imperative shell - it handles IO (randomness) and delegates to a pure internal constructor for the actual construction.
§Arguments
kek- The Key Encryption Key to wrap this DEK
§Returns
A tuple of (usable_dek, wrapped_dek_for_storage).
§Panics
Panics if the OS CSPRNG fails (catastrophic system error).
Trait Implementations§
Source§impl Drop for DataEncryptionKey
impl Drop for DataEncryptionKey
Auto Trait Implementations§
impl Freeze for DataEncryptionKey
impl RefUnwindSafe for DataEncryptionKey
impl Send for DataEncryptionKey
impl Sync for DataEncryptionKey
impl Unpin for DataEncryptionKey
impl UnwindSafe for DataEncryptionKey
Blanket Implementations§
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
Mutably borrows from an owned value. Read more