pub struct KeyManager { /* private fields */ }Expand description
Manages the rolling window of LogEncryptionKeys used by an
crate::encryption::EntryEncryptor.
The “current” key is always used for encryption; on rotation, the old
current key is moved into history and a new key takes its place.
history is bounded by retention (oldest entries are dropped first
once the bound is exceeded). Decryption looks up the right key by the
KeyVersion embedded in the payload.
retention of 1 means only the current key is kept; rotating then
immediately invalidates the previous key. retention of N means at
most N - 1 historical keys plus the current key are retained at any
time (so we can decrypt entries from the most recent N versions).
retention is silently clamped to >= 1 at construction time.
Implementations§
Source§impl KeyManager
impl KeyManager
Sourcepub fn new(initial: LogEncryptionKey, retention: usize) -> Self
pub fn new(initial: LogEncryptionKey, retention: usize) -> Self
Build a new KeyManager with initial as the current key at
KeyVersion 1.
retention is clamped to >= 1; that is, at minimum the current
key is always kept. retention = 3 means current + 2 historical
keys are retained.
Sourcepub fn rotate(&mut self, new_key: LogEncryptionKey) -> KeyVersion
pub fn rotate(&mut self, new_key: LogEncryptionKey) -> KeyVersion
Rotate to a new master key, returning the new current version.
The previous current key is moved into history. When the
combined size of (current + history) exceeds retention, the
oldest historical entry is dropped.
Sourcepub fn current(&self) -> (KeyVersion, &LogEncryptionKey)
pub fn current(&self) -> (KeyVersion, &LogEncryptionKey)
The current key, paired with its version.
Sourcepub fn lookup(&self, version: KeyVersion) -> Option<&LogEncryptionKey>
pub fn lookup(&self, version: KeyVersion) -> Option<&LogEncryptionKey>
Look up the key with version, falling back to historical entries.
Returns None if version is older than the retained window
(already pruned) or has never existed.
Sourcepub fn version_count(&self) -> usize
pub fn version_count(&self) -> usize
Number of versions currently retained (current + history).
Always >= 1 because the current key is always present, so
KeyManager does not expose an is_empty method.