Skip to main content

WrappedKey

Struct WrappedKey 

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

An encryption key wrapped (encrypted) by another key.

Used in the key hierarchy to protect DEKs with KEKs, and KEKs with the master key. The wrapped key can be safely stored on disk — it cannot be unwrapped without the wrapping key.

WrappedKey layout (60 bytes):
┌────────────────────────────┬────────────────────────────┬──────────────┐
│  nonce (12 bytes)          │  encrypted key (32 bytes)  │  tag (16)    │
└────────────────────────────┴────────────────────────────┴──────────────┘

§Security

  • The wrapped ciphertext is encrypted, not raw key material
  • The nonce is not secret (it’s stored alongside the ciphertext)
  • ZeroizeOnDrop is not needed since this contains no plaintext secrets
  • The wrapping key (KEK/MK) is what must be protected

§Example

use kimberlite_crypto::encryption::{EncryptionKey, WrappedKey, KEY_LENGTH};

// KEK wraps a DEK
let kek = EncryptionKey::generate();
let dek_bytes: [u8; KEY_LENGTH] = [0x42; KEY_LENGTH]; // In practice, use generate()

let wrapped = WrappedKey::new(&kek, &dek_bytes);

// Store wrapped.to_bytes() on disk...

// Later, unwrap with the same KEK
let unwrapped = wrapped.unwrap_key(&kek).unwrap();
assert_eq!(dek_bytes, unwrapped);

Implementations§

Source§

impl WrappedKey

Source

pub fn new(wrapping_key: &EncryptionKey, key_to_wrap: &[u8; 32]) -> Self

Wraps (encrypts) a key using the provided wrapping key.

Generates a random nonce and encrypts the key material using AES-256-GCM. The nonce is stored alongside the ciphertext for later unwrapping.

§Arguments
  • wrapping_key - The key used to encrypt (KEK or master key)
  • key_to_wrap - The 32-byte key material to protect (DEK or KEK)
§Returns

A WrappedKey that can be serialized and stored safely.

Source

pub fn unwrap_key( &self, wrapping_key: &EncryptionKey, ) -> Result<[u8; 32], CryptoError>

Unwraps (decrypts) the key using the provided wrapping key.

Verifies the authentication tag and returns the original key material if the wrapping key is correct.

§Arguments
  • wrapping_key - The key used during wrapping (must match)
§Errors

Returns CryptoError::DecryptionError if:

  • The wrapping key is incorrect
  • The wrapped data has been tampered with
§Returns

The original 32-byte key material.

Source

pub fn to_bytes(&self) -> [u8; 60]

Serializes the wrapped key to bytes for storage.

The format is: nonce (12 bytes) || ciphertext (48 bytes).

§Returns

A 60-byte array suitable for storing on disk or in a database.

Source

pub fn from_bytes(bytes: &[u8; 60]) -> Self

Deserializes a wrapped key from bytes.

§Arguments
§Returns

A WrappedKey that can be unwrapped with the original wrapping key.

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