pub struct CipherState<C: Cipher> { /* private fields */ }
Expand description

A CipherState can encrypt and decrypt data.

Mostly like CipherState in the spec, but must be created with a key.

Panics

Encryption and decryption methods will panic if nonce reaches maximum u64, i.e., 2 ^ 64 - 1.

Implementations§

source§

impl<C> CipherState<C>
where C: Cipher,

source

pub fn name() -> &'static str

Name of cipher, e.g. “ChaChaPoly”.

source

pub fn new(key: &[u8], n: u64) -> Self

Create a new CipherState with a key and a nonce n.

source

pub fn rekey(&mut self)

Rekey. Set our key to REKEY(old key).

source

pub fn encrypt_ad(&mut self, authtext: &[u8], plaintext: &[u8], out: &mut [u8])

AEAD encryption.

source

pub fn encrypt_ad_in_place( &mut self, authtext: &[u8], in_out: &mut [u8], plaintext_len: usize ) -> usize

AEAD encryption in place.

source

pub fn decrypt_ad( &mut self, authtext: &[u8], ciphertext: &[u8], out: &mut [u8] ) -> Result<(), ()>

AEAD decryption.

source

pub fn decrypt_ad_in_place( &mut self, authtext: &[u8], in_out: &mut [u8], ciphertext_len: usize ) -> Result<usize, ()>

AEAD decryption in place.

source

pub fn encrypt(&mut self, plaintext: &[u8], out: &mut [u8])

Encryption.

source

pub fn encrypt_in_place( &mut self, in_out: &mut [u8], plaintext_len: usize ) -> usize

Encryption in place.

source

pub fn encrypt_vec(&mut self, plaintext: &[u8]) -> Vec<u8>

Encryption, returns ciphertext as Vec<u8>.

source

pub fn decrypt(&mut self, ciphertext: &[u8], out: &mut [u8]) -> Result<(), ()>

Decryption.

source

pub fn decrypt_in_place( &mut self, in_out: &mut [u8], ciphertext_len: usize ) -> Result<usize, ()>

Decryption in place.

source

pub fn decrypt_vec(&mut self, ciphertext: &[u8]) -> Result<Vec<u8>, ()>

Decryption, returns plaintext as Vec<u8>.

source

pub fn get_next_n(&self) -> u64

Get the next value of n. Could be used to decide on whether to re-key, etc.

source

pub fn extract(self) -> (C::Key, u64)

Get underlying cipher and nonce.

This is useful for e.g. WireGuard. Because packets may be lost or arrive out of order, they would likely want to deal with nonces themselves.

Trait Implementations§

source§

impl<C> Clone for CipherState<C>
where C: Cipher,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<C> RefUnwindSafe for CipherState<C>
where <C as Cipher>::Key: RefUnwindSafe,

§

impl<C> Send for CipherState<C>
where <C as Cipher>::Key: Send,

§

impl<C> Sync for CipherState<C>
where <C as Cipher>::Key: Sync,

§

impl<C> Unpin for CipherState<C>
where <C as Cipher>::Key: Unpin,

§

impl<C> UnwindSafe for CipherState<C>
where <C as Cipher>::Key: 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<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> ToOwned for T
where T: Clone,

§

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

§

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

§

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.