pub struct KeyState { /* private fields */ }Expand description
Key state for HMAC key rotation support.
Corresponds to Lean: structure KeyState
Design rationale:
current: The active key used for sealing new capabilitiesepoch: Monotonically increasing counter identifying the current keyprevious: Optional previous key for validating in-flight capabilitiesprevious_epoch: Epoch of the previous key
The grace period (keeping previous key) allows capabilities that were sealed just before rotation to still be verified.
SECURITY INVARIANT: epoch is monotonically increasing (never wraps in practice).
Implementations§
Source§impl KeyState
impl KeyState
Sourcepub fn initial(key: Key) -> Self
pub fn initial(key: Key) -> Self
Create initial key state with no previous key
Corresponds to Lean: def KeyState.initial
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Create empty key state (for default/placeholder purposes)
Corresponds to Lean: def KeyState.empty
Sourcepub fn rotate(&mut self, new_key: Key)
pub fn rotate(&mut self, new_key: Key)
Rotate to a new key
Corresponds to Lean: def KeyState.rotate
The current key becomes the previous key for grace period verification. The epoch is incremented.
SECURITY: Only kernel can trigger rotation (not plugins). Uses checked arithmetic – epoch overflow is a critical error.
Sourcepub fn rotated(&self, new_key: Key) -> Self
pub fn rotated(&self, new_key: Key) -> Self
Rotate to a new key (immutable version)
Returns a new KeyState with the rotated key.
Sourcepub fn previous_epoch(&self) -> Option<u64>
pub fn previous_epoch(&self) -> Option<u64>
Get the previous epoch (if available)
Sourcepub fn epoch_valid(&self, cap_epoch: u64) -> bool
pub fn epoch_valid(&self, cap_epoch: u64) -> bool
Check if epoch is current or within grace period
Corresponds to Lean: def KeyState.epochValid
Sourcepub fn verify_seal(&self, payload: &CapPayload, tag: &SealedTag) -> bool
pub fn verify_seal(&self, payload: &CapPayload, tag: &SealedTag) -> bool
Verify a capability’s seal against the key state
Corresponds to Lean: def KeyState.verify
Strategy:
- Try current key first
- Fall back to previous key for in-flight capabilities (grace period)
Returns true if the capability verifies against either key.
Sourcepub fn verify_with_epoch(
&self,
payload: &CapPayload,
tag: &SealedTag,
cap_epoch: u64,
) -> bool
pub fn verify_with_epoch( &self, payload: &CapPayload, tag: &SealedTag, cap_epoch: u64, ) -> bool
Combined verification: seal AND epoch valid
Corresponds to Lean: def KeyState.verifyWithEpoch
Sourcepub fn clear_previous(&mut self)
pub fn clear_previous(&mut self)
Clear the previous key (end grace period)
Call this after sufficient time has passed since rotation to clear the previous key from memory.