Skip to main content

KeyRing

Struct KeyRing 

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

A set of key-encryption keys, one of them current.

Holds every KEK that can still open data, plus which one new seals use. Rotation is therefore additive: add a KEK, point current at it, and old ciphertext keeps opening under its own kek_id until it is re-sealed.

Implementations§

Source§

impl KeyRing

Source

pub fn new<I>(keys: I, current: impl Into<String>) -> Result<Self, CryptoError>
where I: IntoIterator<Item = (String, Zeroizing<Vec<u8>>)>,

Build a key ring from (id, raw 32-byte key) pairs, with current naming the KEK new seals use.

§Errors

CryptoError::InvalidKek if any key is not KEK_LEN bytes, or if current is not among the supplied ids — a ring that cannot seal is a configuration error worth failing at startup rather than at first write.

Source

pub fn current_kek_id(&self) -> &str

The id new seals are written under.

Source

pub fn seal( &self, plaintext: &[u8], binding: SecretBinding<'_>, ) -> Result<SealedSecret, CryptoError>

Encrypt plaintext under the current KEK, bound to binding.

§Errors

CryptoError::SealFailed if the RNG or the AEAD fails.

Source

pub fn open( &self, sealed: &SealedSecret, binding: SecretBinding<'_>, ) -> Result<Zeroizing<Vec<u8>>, CryptoError>

Decrypt sealed, which must have been sealed under the same binding.

The plaintext comes back in a Zeroizing buffer so it is wiped when the caller drops it rather than lingering in freed memory.

§Errors

CryptoError::UnknownKekId when no configured KEK matches — the operator removed a key that still has data under it. CryptoError::OpenFailed for a wrong key, tampered ciphertext, or a binding that does not match the one it was sealed with.

Source

pub fn reseal( &self, sealed: &SealedSecret, binding: SecretBinding<'_>, ) -> Result<SealedSecret, CryptoError>

Re-seal sealed under the current KEK. Used by rotation: open with the old key, seal with the new, same binding.

§Errors

As Self::open and Self::seal.

Source§

impl KeyRing

Source

pub fn from_spec(spec: &str, current: &str) -> Result<Self, CryptoError>

Build a ring from a deploy-time spec: id:hex,id:hex,…, with current naming which id new seals use.

Hex rather than base64 so no encoding dependency is needed for 32 bytes, and so a malformed value fails loudly instead of decoding to the wrong length. Generate one with openssl rand -hex 32.

§Errors

CryptoError::InvalidKek on a malformed entry, a key that is not KEK_LEN bytes, or a current id absent from the spec.

Trait Implementations§

Source§

impl Debug for KeyRing

Keys must never surface in a Debug, and LessSafeKey has no Debug anyway — report the ids only.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more