Skip to main content

Cipher

Enum Cipher 

Source
pub enum Cipher {
    XChaCha20Poly1305,
    ChaCha20Poly1305,
    Aes256Gcm,
    Aes128Gcm,
    Aes256GcmSiv,
}
Expand description

A symmetric authenticated cipher.

Every one here is an AEAD: it authenticates as well as encrypts, so there is no way to decrypt without checking integrity first. A plain cipher with no tag is not offered, because using one correctly requires bolting a MAC on in exactly the right order and getting it wrong is silent.

Variants§

§

XChaCha20Poly1305

XChaCha20-Poly1305. The default, and the one to use.

Its 192-bit nonce is large enough that a random nonce per message has no meaningful collision risk, so there is no counter to keep and no state to synchronise between processes.

§

ChaCha20Poly1305

ChaCha20-Poly1305, as used by TLS.

A 96-bit nonce, which is small enough that random nonces have a real birthday bound — safe for a few billion messages under one key, and a reason to prefer the X variant when you are not constrained by a peer.

§

Aes256Gcm

AES-256-GCM. For interoperating with something that requires it, or where hardware AES makes it measurably faster.

The same 96-bit nonce caveat as ChaCha20Poly1305, and a harsher failure: repeating a nonce under GCM leaks the authentication key, not just the plaintext relationship.

§

Aes128Gcm

AES-128-GCM. A shorter key, derived from the same 256-bit key material.

§

Aes256GcmSiv

AES-256-GCM-SIV — nonce-misuse resistant.

Repeating a nonce here reveals only that two plaintexts were equal, rather than breaking the cipher. Worth choosing when nonces come from somewhere you do not fully control.

Implementations§

Source§

impl Cipher

Source

pub const ALL: [Cipher; 5]

Every cipher, for a diagnostic listing and for tests.

Source

pub fn id(self) -> &'static str

The short identifier written into a payload.

Source

pub fn from_id(id: &str) -> Option<Self>

The cipher a payload names.

v1 is accepted as an alias for XChaCha20-Poly1305: it is what the first release wrote, before payloads named their algorithm, and those payloads still have to open.

Source

pub fn key_len(self) -> usize

The key length this cipher needs, in bytes.

Source

pub fn nonce_len(self) -> usize

The nonce length this cipher needs, in bytes.

Source

pub fn is_nonce_misuse_resistant(self) -> bool

Whether repeating a nonce is survivable.

Source

pub fn encrypt( self, key: &Key, nonce: &[u8], aad: &[u8], plain: &[u8], ) -> Result<Vec<u8>>

Encrypt plain, authenticating aad alongside it.

Source

pub fn decrypt( self, key: &Key, nonce: &[u8], aad: &[u8], sealed: &[u8], ) -> Result<Vec<u8>>

Decrypt, verifying aad.

Every failure is the same error: a caller can do nothing useful with the difference between “the tag was wrong” and “the length was wrong”, and telling them apart is how padding-oracle attacks start.

Source

pub fn nonce(self) -> Vec<u8> ⓘ

A fresh nonce of the right length for this cipher.

Trait Implementations§

Source§

impl Clone for Cipher

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Cipher

Source§

impl Debug for Cipher

Source§

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

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

impl Default for Cipher

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Cipher

Source§

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

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

impl Eq for Cipher

Source§

impl FromStr for Cipher

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(value: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Cipher

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Cipher

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> 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> 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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V