pub struct ChaCha20 { /* private fields */ }
Expand description
The ChaCha20
struct represents the ChaCha20 stream cipher.
Implementations§
Source§impl ChaCha20
impl ChaCha20
Sourcepub fn new() -> Self
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
.
Sourcepub fn next_keystream(&mut self) -> [u8; 64]
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.
pub fn keystream_at(&self, counter: u32) -> [u8; 64]
Trait Implementations§
Source§impl AlgorithmKeyIVInit for ChaCha20
impl AlgorithmKeyIVInit for ChaCha20
Source§fn init(&mut self, key: &[u8], iv: &[u8])
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
impl AlgorithmProcess for ChaCha20
Source§fn process(&mut self, bytes_in: &[u8]) -> Vec<u8> ⓘ
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
impl AlgorithmProcessInPlace for ChaCha20
Source§fn process_in_place(&self, bytes: &mut [u8])
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).
impl EncryptionAlgorithm for ChaCha20
Auto Trait Implementations§
impl Freeze for ChaCha20
impl RefUnwindSafe for ChaCha20
impl Send for ChaCha20
impl Sync for ChaCha20
impl Unpin for ChaCha20
impl UnwindSafe for ChaCha20
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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