Struct ChaCha20

Source
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, AlgorithmKeyIVInit};

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.

Source

pub fn keystream_at(&self, counter: u32) -> [u8; 64]

Trait Implementations§

Source§

impl AlgorithmKeyIVInit 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§

impl AlgorithmProcess for ChaCha20

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).

§Example
use secured_cipher::{ChaCha20, algorithm::{AlgorithmKeyIVInit, AlgorithmProcess}};

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§

impl AlgorithmProcessInPlace for ChaCha20

Source§

fn process_in_place(&self, bytes: &mut [u8])

Processes input data using the ChaCha20 cipher algorithm.

This function applies the ChaCha20 encryption or decryption process to the given input bytes.

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

impl Clone for ChaCha20

Source§

fn clone(&self) -> ChaCha20

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 EncryptionAlgorithm for ChaCha20

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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, 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.