pub struct ChaCha20 { /* private fields */ }
Expand description

The ChaCha20 struct represents the ChaCha20 stream cipher.

Implementations§

source§

impl ChaCha20

source

pub fn new() -> Self

Constructs a new ChaCha20 cipher instance.

This function initializes the internal state of the cipher.

Returns

A new instance of ChaCha20.

source

pub fn next_keystream(&mut self) -> [u8; 64]

Generates the next 64-byte keystream block from the ChaCha20 state.

This function advances the ChaCha20 state and produces a keystream block based on the current state. It performs a permutation of the state, increments the block counter to ensure uniqueness for subsequent calls, and then serializes the permuted state into a 64-byte array.

Returns

A 64-byte array representing the generated keystream block.

Panics

Panics if the 32-bit block counter overflows, which would only happen after a very large number of blocks (2^32-1) have been processed with the same key-nonce combination.

Example
use secured_cipher::{ChaCha20, Permutation};
 
let mut chacha20 = ChaCha20::new();
chacha20.init(&[0_u8; 32], &[0_u8; 12]);
 
let keystream_block = chacha20.next_keystream();
// `keystream_block` now contains the next 64 bytes of the keystream
Notes

The keystream generated by this function is used to encrypt or decrypt data by XORing it with the plaintext or ciphertext. Each call to this function must produce a unique keystream block. This uniqueness is guaranteed by incrementing the internal block counter.

Trait Implementations§

source§

impl Permutation for ChaCha20

source§

fn init(&mut self, key: &[u8], iv: &[u8])

Initializes the ChaCha20 cipher with a given key and IV (initialization vector).

This method sets up the cipher’s internal state which includes the ChaCha20 constants, the provided key, a zeroed block counter, and the provided IV.

Arguments
  • key - A 256-bit key represented as 32 bytes.
  • iv - A 86-bit IV (nonce) represented as 12 bytes.
source§

fn process(&mut self, bytes_in: &[u8]) -> Vec<u8>

Processes input data using the ChaCha20 cipher algorithm.

This function applies the ChaCha20 encryption or decryption process to the given input bytes. It works by generating a unique keystream for each 64-byte block of the input data and then applying an XOR operation between the data block and the keystream. This process is suitable for both encryption and decryption due to the reversible nature of the XOR operation.

Arguments
  • bytes_in - A slice of bytes representing the input data to be processed (either plaintext for encryption or ciphertext for decryption).
Returns

A Vec<u8> containing the processed data (encrypted or decrypted).

Behavior

The function divides the input data into 64-byte blocks. For each block, it generates a unique keystream using the next_keystream method. Each block of the input data is then XORed with its corresponding keystream block. This method ensures that each block is encrypted or decrypted with a different keystream, which is essential for the security of the cipher.

After processing all blocks, the function clears the internal state to prevent any residual sensitive data from remaining in memory.

Example
use secured_cipher::{ChaCha20, Permutation};
 
let mut chacha20 = ChaCha20::new();
chacha20.init(&[0_u8; 32], &[0_u8; 12]);
 
let data = b"some plaintext data"; // Data to be encrypted or decrypted
let processed_data = chacha20.process(data);
// `processed_data` now contains the encrypted or decrypted output
Notes

It’s important to use the same nonce and key for decrypting the data that were used for encryption. The output size will be equal to the input size, as ChaCha20 is a stream cipher.

source§

fn clear(&mut self)

Clears the internal counter of the 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<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

§

type Output = T

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