Struct Ctr

Source
pub struct Ctr<Enc> { /* private fields */ }
Expand description

Block counter mode is a block chaining mode which turns a block cipher into a stream cipher, and hence does not require a padding scheme.

The algorithm keeps a monotonically incrementing counter. The plaintext is split into blocks. Each block of plaintext is encrypted by converting the counter into bytes, converting the bytes into a block (by appending as many zero bytes as needed to reach the block size), encrypting that block with the underlying block cipher, and XORing the ciphertext block with the appropriate block of plaintext. Afterwards, the block counter is incremented, and the process is repeated until there are no blocks left.

If the last block of plaintext is shorter than the block size, the last block of ciphertext is simply truncated to the length of the remaining plaintext.

The block counter is first set to some initial value, called the nonce. Like the IV for CBC mode, the nonce does not need to be secret, but it needs to be unique.

Because the XOR operation cancels itself ($X \oplus Y \oplus Y = X$ for any $X, Y$), the decryption is exactly the same as encryption. Notably, it only relies on the encryption function of the underlying block cipher. The decryption function is never used.

The operation of counter mode essentially represents a one-time pad, where the keystream is generated using the underlying block cipher and the block counter.

Implementations§

Source§

impl<Enc, const BLOCK_SIZE: usize> Ctr<Enc>
where Enc: BlockEncrypt<EncryptionBlock = [u8; BLOCK_SIZE]>,

Source

pub fn new(enc: Enc, nonce: u64) -> Result<Self, BlockSizeTooSmall>

Trait Implementations§

Source§

impl<Enc> Cipher for Ctr<Enc>
where Enc: BlockEncrypt, Enc::EncryptionBlock: IntoIterator<Item = u8> + AsMut<[u8]> + Default, Enc::EncryptionKey: 'static + Clone,

Source§

impl<Enc> CipherDecrypt for Ctr<Enc>
where Enc: BlockEncrypt, Enc::EncryptionBlock: IntoIterator<Item = u8> + AsMut<[u8]> + Default, Enc::EncryptionKey: 'static + Clone,

Source§

type DecryptionErr = Infallible

Source§

type DecryptionKey = <Enc as BlockEncrypt>::EncryptionKey

Source§

fn decrypt( &self, data: Vec<u8>, key: Self::DecryptionKey, ) -> Result<Vec<u8>, Self::DecryptionErr>

Decrypt the ciphertext. This operation can fail, for example, if the ciphertext was not created by this cipher.
Source§

impl<Enc> CipherEncrypt for Ctr<Enc>
where Enc: BlockEncrypt, Enc::EncryptionBlock: IntoIterator<Item = u8> + AsMut<[u8]> + Default, Enc::EncryptionKey: 'static + Clone,

Source§

type EncryptionErr = Infallible

Source§

type EncryptionKey = <Enc as BlockEncrypt>::EncryptionKey

Source§

fn encrypt( &self, data: Vec<u8>, key: Self::EncryptionKey, ) -> Result<Vec<u8>, Self::EncryptionErr>

Encrypt the plaintext.
Source§

impl<Enc: Clone> Clone for Ctr<Enc>

Source§

fn clone(&self) -> Ctr<Enc>

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<Enc: Debug> Debug for Ctr<Enc>

Source§

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

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

impl<Enc> BlockMode for Ctr<Enc>
where Enc: BlockEncrypt, Enc::EncryptionBlock: IntoIterator<Item = u8> + AsMut<[u8]> + Default, Enc::EncryptionKey: 'static + Clone,

Auto Trait Implementations§

§

impl<Enc> Freeze for Ctr<Enc>
where Enc: Freeze,

§

impl<Enc> RefUnwindSafe for Ctr<Enc>
where Enc: RefUnwindSafe,

§

impl<Enc> Send for Ctr<Enc>
where Enc: Send,

§

impl<Enc> Sync for Ctr<Enc>
where Enc: Sync,

§

impl<Enc> Unpin for Ctr<Enc>
where Enc: Unpin,

§

impl<Enc> UnwindSafe for Ctr<Enc>
where Enc: 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> 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> 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.