Skip to main content

Key

Struct Key 

Source
pub struct Key<const BYTES: usize> { /* private fields */ }
Expand description

Stores the secret key used in AEGIS encryption and decryption.

This type takes a const generic parameter (BYTES) specifying the number of key bytes. AEGIS-128[L|X] ciphers use 128 bit keys while AEGIS-256[X] ciphers use 256 bit keys. (All ciphers validate the provided Key size at compile time making it impossible to use an incorrect Key size.)

Since only 128 or 256 bit keys are valid for AEGIS ciphers, only values 16 and 32 are supported for the Key’s BYTES const generic parameter. This too is validated at compile time.

Key128 (for Key<16>) and Key256 (for Key<32>) type aliases are provided for convenience and should be preferred over raw Key usage.

Use Key::generate() to securely create random Keys instead of generating key bytes yourself and passing them to Key::new() or Key::from_bytes(). The Key::generate() method will use an appropriate cryptographically secure random number generator (CSRNG) provided by the OS.

You can access the internal key bytes with Key::expose_secret(). This method is also the only way to access secret bytes once they are stored in Key, making security audits easier.

Key uses a custom implementation of Debug that always redacts the key bytes to prevent accidental exposure of secrets through logs or other machinery.

This type is zeroized on Drop.

Note that derives for Eq and PartialEq are intentionally omitted to prevent accidental non-constant-time equality comparisons. Use Key::expose_secret() and the constant_time_eq crate if you need this. Clone is not implemented to prevent accidental secret duplication.

Implementations§

Source§

impl<const BYTES: usize> Key<BYTES>

Source

pub const BYTES: usize = BYTES

The length of Key in bytes.

Source

pub fn generate() -> Result<Self>

Create a new, random Key from a cryptographically secure random number generator (CSRNG).

§Errors
  • Returns Error::RandError if there’s an error in fetching random bytes.
Source

pub fn new(data: [u8; BYTES]) -> Result<Self>

Create a new Key from a correctly sized byte array.

Prefer Key::generate() over this method.

The bytes you provide must come from cryptographically secure random number generator (CSRNG). Do not use whatever RNG you found lying around.

§Errors
Source

pub fn from_bytes<T: AsRef<[u8]>>(data: T) -> Result<Self>

Create a new Key from any T which implements AsRef<[u8]>. Thus it’s possible to pass a &[u8], a &mut [u8], a Vec<u8>, a Box<[u8]> etc.

Prefer Key::generate() over this method.

The bytes you provide must come from cryptographically secure random number generator (CSRNG). Do not use whatever RNG you found lying around.

§Errors
Source

pub fn expose_secret(&self) -> &[u8; BYTES]

Access the secret key bytes.

This method is named expose_secret instead of a more generic as_array to make it easier to grep for in a large codebase.

Trait Implementations§

Source§

impl<const BYTES: usize> Debug for Key<BYTES>

Source§

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

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

impl<const BYTES: usize> Drop for Key<BYTES>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<const BYTES: usize> Freeze for Key<BYTES>
where [u8; BYTES]: Freeze,

§

impl<const BYTES: usize> RefUnwindSafe for Key<BYTES>

§

impl<const BYTES: usize> Send for Key<BYTES>
where [u8; BYTES]: Send,

§

impl<const BYTES: usize> Sync for Key<BYTES>
where [u8; BYTES]: Sync,

§

impl<const BYTES: usize> Unpin for Key<BYTES>
where [u8; BYTES]: Unpin,

§

impl<const BYTES: usize> UnsafeUnpin for Key<BYTES>
where [u8; BYTES]: UnsafeUnpin,

§

impl<const BYTES: usize> UnwindSafe for Key<BYTES>
where [u8; BYTES]: UnwindSafe,

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<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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.