RotatingStrongBox

Struct RotatingStrongBox 

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

A StrongBox variant that uses a different set of keys for each period of time.

This box is primarily useful when you’re encrypting data that only has to be valid for a relatively short period of time (such as browser cookies and other shortish-lived tokens). It is particularly beneficial if you could potentially do a lot (ie “many billions”) of encryptions over the lifetime of the root keys (because, for various tedious reasons, the risk of a cryptographic break increases based on the number of different encryptions done by one key).

To use it, you need to specify the “root” encryption and decryption keys, as with any other strong box, but also the Duration for which key is valid, and also the number of previous time periods which you want to still be able to decrypt.

The way it works is that time is divided up into periods, each of which has a Duration of lifespan, specified when the RotatingStrongBox is created. When an encryption operation is performed, the current period is determined (by looking at the clock and divided by lifespan), the encryption key for the current period is derived from the “root” encryption key, and the data is encrypted as normal with that “current encryption key”. So far, so good.

When a ciphertext is presented for decryption, things get a bit more involved. The RotatingStrongBox needs to figure out which key to use for decryption, by deriving the decryption keys from the “root” decryption keys specified when the RotatingStrongBox was created, both for the time period at the time the decryption happens, as well as the previous time periods, up to the limit specified by backtrack. If the decryption key for the ciphertext is one of those keys, then decryption happens as normal. Otherwise, you’re out of luck, and the decryption fails.

Since deriving lots of keys can start to take a little bit of time, the set of decryption keys is cached, and also shared amongst all the clones of a given RotatingStrongBox. The maximum amount of memory that will be used by the cache (and the amount of time needed to derive all the keys) is determined by the number of separate decryption keys, multiplied by the number of backtrack periods allowed. Each key is relatively small, so don’t worry too much, but also don’t go “oh, I’ll make my key lifespan 30 seconds and cache keys for 10 years” without being ready for a certain amount of bloat.

Trait Implementations§

Source§

impl Clone for RotatingStrongBox

Source§

fn clone(&self) -> RotatingStrongBox

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RotatingStrongBox

Source§

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

Formats the value using the given formatter. Read more
Source§

impl StrongBox for RotatingStrongBox

Source§

fn encrypt( &self, plaintext: impl AsRef<[u8]>, ctx: impl AsRef<[u8]> + Debug, ) -> Result<Vec<u8>, Error>

Encrypt secret data using the StrongBox’s encryption key, within the StrongBox’s specified context. Read more
Source§

fn decrypt( &self, ciphertext: impl AsRef<[u8]>, ctx: impl AsRef<[u8]> + Debug, ) -> Result<Vec<u8>, Error>

Decrypt a ciphertext, using any valid key for the StrongBox, and validate that the ciphertext was encrypted with the specified context. Read more
Source§

impl UnwindSafe for RotatingStrongBox

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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

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