Struct RTSecret

Source
pub struct RTSecret<T: Zeroize, MEC: ChooseMinimallyRepresentableUInt>(/* private fields */);
Expand description

A runtime secret with optional zeroization for the type T and exposure count tracking. It is the runtime version of Secret<T, MEC, EC>.

Implementations§

Source§

impl<T: Zeroize, MEC: ChooseMinimallyRepresentableUInt> RTSecret<T, MEC>

Source

pub const fn new(t: T) -> Self

Creates a new RTSecret with the provided secret value t.

§Parameters
  • t: The secret value.
§Returns

The newly created RTSecret.

Source

pub fn new_with(f: impl FnOnce() -> T) -> Self

Creates a new RTSecret with the provided secret value returned by the closure f.

§Parameters
  • f: A closure that returns the secret value.
§Returns

The newly created RTSecret.

Source

pub fn exposure_count( &self, ) -> <MEC as ChooseMinimallyRepresentableUInt>::Output

Retrieves the current exposure count of the secret and returns it as an unsigned integer.

Note: The actual unsigned integer type returned depends on the type-level value of the type parameter MEC, it is the minimal representable Rust’s unsigned integer type that can represent the value. e.g. if MEC is typenum::consts::U67, then the returned type is u8.

Trait Implementations§

Source§

impl<T, MEC> Clone for RTSecret<T, MEC>

Available on crate feature cloneable-secret only.
Source§

fn clone(&self) -> Self

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<T, MEC> Debug for RTSecret<T, MEC>

Available on crate feature debug-secret only.
Source§

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

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

impl<T: Zeroize, MEC: ChooseMinimallyRepresentableUInt> Drop for RTSecret<T, MEC>

Source§

fn drop(&mut self)

Zeroizes the secret value when dropped if the zeroize feature is enabled.

Source§

impl<'secret, T: Zeroize, MEC: ChooseMinimallyRepresentableUInt + Unsigned + IsGreater<U0, Output = True> + Debug> RTExposeSecret<'secret, &'secret T> for RTSecret<T, MEC>

Source§

fn expose_secret<ReturnType, ClosureType>( &self, scope: ClosureType, ) -> ReturnType
where for<'brand> ClosureType: FnOnce(RTExposedSecret<'brand, &'brand T>) -> ReturnType,

Exposes the secret with runtime checking that the exposure count is not more than the maximally allowed exposure count represented by the type parameter MEC. Note: It is impossible to return the ‘exposed secret’ as the return value of the closure.

Example:

use sosecrets_rs::{
    prelude::typenum::U2,
    runtime::{secret::RTSecret, traits::RTExposeSecret},
};
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

struct A {
    inner: i32,
}

#[cfg(feature = "zeroize")]
impl Zeroize for A {
    fn zeroize(&mut self) {
        self.inner.zeroize()
    }
}

let secret_one = RTSecret::<A, U2>::new(A { inner: 69 });
let returned_value = secret_one.expose_secret(|exposed_secret| A { inner: (*exposed_secret).inner + 1});
assert_eq!(returned_value.inner, 70);

Example (this does NOT compile):

use sosecrets_rs::{
    prelude::typenum::U2,
    runtime::{secret::RTSecret, traits::RTExposeSecret},
};
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

struct A {
    inner: i32,
}

#[cfg(feature = "zeroize")]
impl Zeroize for A {
    fn zeroize(&mut self) {
        self.inner.zeroize()
    }
}

let secret_one = RTSecret::<A, U2>::new(A { inner: 69 });
let _ = secret_one.expose_secret(|exposed_secret| exposed_secret);
let _ = secret_one.expose_secret(|exposed_secret| *exposed_secret); // Only if T is not `Copy`
§Parameters
  • self.
  • scope: A closure that takes the exposed secret and returns a value of the ReturnType.
§Panics

This function panics only if the secret is exposed more than the maximally allowed exposure count represented by the type parameter MEC.

§Returns

A value of type ReturnType which is the type of the returned value from the closure named scope.

Source§

fn try_expose_secret<ReturnType, ClosureType>( &self, scope: ClosureType, ) -> Result<ReturnType, ExposeSecretError<MEC>>
where for<'brand> ClosureType: FnOnce(RTExposedSecret<'brand, &'brand T>) -> ReturnType,

Return the Result containing Ok(scope(exposed_secret)), with runtime checking that the exposure count is not more than the maximally allowed exposure count represented by the type parameter MEC. Note: It is impossible to return the ‘exposed secret’ as the return value of the closure.

Example:

use sosecrets_rs::{
    prelude::{typenum::U2, RTSecret},
    runtime::traits::RTExposeSecret,
};
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

struct A {
    inner: i32,
}

#[cfg(feature = "zeroize")]
impl Zeroize for A {
    fn zeroize(&mut self) {
        self.inner.zeroize()
    }
}

let secret_one = RTSecret::<A, U2>::new(A { inner: 69 });
let returned_value = secret_one.try_expose_secret(|exposed_secret| A { inner: (*exposed_secret).inner + 1});
assert!(returned_value.is_ok());

Example (this example will not compile):

use sosecrets_rs::{
    prelude::typenum::U2,
    runtime::{secret::RTSecret, traits::RTExposeSecret},
};
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

struct A {
    inner: i32,
}

#[cfg(feature = "zeroize")]
impl Zeroize for A {
    fn zeroize(&mut self) {
        self.inner.zeroize()
    }
}

let secret_one = RTSecret::<A, U2>::new(A { inner: 69 });
let _ = secret_one.try_expose_secret(|exposed_secret| exposed_secret);
let _ = secret_one.try_expose_secret(|exposed_secret| *exposed_secret); // Only if T is not `Copy`
§Parameters
  • self.
  • scope: A closure that takes the exposed secret and returns a value of the ReturnType.
§Returns
  • Ok: The value returned by the closure.
  • Err: If the exposure count exceeds the maximum allowed, returns an ExposeSecretError.
Source§

type Error = ExposeSecretError<MEC>

The type representing the Error variant as part of the Result returned type in try_expose_secret.
Source§

type Exposed<'brand> = RTExposedSecret<'brand, &'brand T> where 'secret: 'brand

The type representing the exposed secret.

Auto Trait Implementations§

§

impl<T, MEC> !Freeze for RTSecret<T, MEC>

§

impl<T, MEC> !RefUnwindSafe for RTSecret<T, MEC>

§

impl<T, MEC> Send for RTSecret<T, MEC>

§

impl<T, MEC> !Sync for RTSecret<T, MEC>

§

impl<T, MEC> Unpin for RTSecret<T, MEC>

§

impl<T, MEC> UnwindSafe for RTSecret<T, MEC>

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, 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.