use crate::types::nonce::ChaCha20Compatible;
use crate::types::Nonce;
use byteorder::{ByteOrder, LittleEndian};
use dcrypt_common::security::{EphemeralSecret, SecretBuffer};
use zeroize::{Zeroize, ZeroizeOnDrop};
pub const CHACHA20_KEY_SIZE: usize = 32;
pub const CHACHA20_NONCE_SIZE: usize = 12;
pub const CHACHA20_BLOCK_SIZE: usize = 64;
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct ChaCha20 {
state: [u32; 16],
buffer: [u8; CHACHA20_BLOCK_SIZE],
position: usize,
counter: u32,
}
impl ChaCha20 {
pub fn new<const N: usize>(key: &[u8; CHACHA20_KEY_SIZE], nonce: &Nonce<N>) -> Self
where
Nonce<N>: ChaCha20Compatible,
{
let key_buf = SecretBuffer::new(*key);
Self::with_counter_secure(&key_buf, nonce, 0)
}
pub fn with_counter<const N: usize>(
key: &[u8; CHACHA20_KEY_SIZE],
nonce: &Nonce<N>,
counter: u32,
) -> Self
where
Nonce<N>: ChaCha20Compatible,
{
let key_buf = SecretBuffer::new(*key);
Self::with_counter_secure(&key_buf, nonce, counter)
}
fn with_counter_secure<const N: usize>(
key: &SecretBuffer<CHACHA20_KEY_SIZE>,
nonce: &Nonce<N>,
counter: u32,
) -> Self
where
Nonce<N>: ChaCha20Compatible,
{
let mut state = [0u32; 16];
state[0] = 0x61707865;
state[1] = 0x3320646e;
state[2] = 0x79622d32;
state[3] = 0x6b206574;
let key_bytes = key.as_ref();
for i in 0..8 {
state[4 + i] = LittleEndian::read_u32(&key_bytes[i * 4..]);
}
state[12] = counter;
let nonce_bytes = nonce.as_ref();
state[13] = LittleEndian::read_u32(&nonce_bytes[0..4]);
state[14] = LittleEndian::read_u32(&nonce_bytes[4..8]);
state[15] = LittleEndian::read_u32(&nonce_bytes[8..12]);
Self {
state,
buffer: [0; CHACHA20_BLOCK_SIZE],
position: CHACHA20_BLOCK_SIZE, counter,
}
}
#[inline]
fn quarter_round(state: &mut [u32], a: usize, b: usize, c: usize, d: usize) {
state[a] = state[a].wrapping_add(state[b]);
state[d] ^= state[a];
state[d] = state[d].rotate_left(16);
state[c] = state[c].wrapping_add(state[d]);
state[b] ^= state[c];
state[b] = state[b].rotate_left(12);
state[a] = state[a].wrapping_add(state[b]);
state[d] ^= state[a];
state[d] = state[d].rotate_left(8);
state[c] = state[c].wrapping_add(state[d]);
state[b] ^= state[c];
state[b] = state[b].rotate_left(7);
}
fn generate_keystream(&mut self) {
let mut working_state = self.state;
working_state[12] = self.counter;
for _ in 0..10 {
Self::quarter_round(&mut working_state, 0, 4, 8, 12);
Self::quarter_round(&mut working_state, 1, 5, 9, 13);
Self::quarter_round(&mut working_state, 2, 6, 10, 14);
Self::quarter_round(&mut working_state, 3, 7, 11, 15);
Self::quarter_round(&mut working_state, 0, 5, 10, 15);
Self::quarter_round(&mut working_state, 1, 6, 11, 12);
Self::quarter_round(&mut working_state, 2, 7, 8, 13);
Self::quarter_round(&mut working_state, 3, 4, 9, 14);
}
let mut output_state = EphemeralSecret::new([0u32; 16]);
for i in 0..16 {
let original_val = if i == 12 { self.counter } else { self.state[i] };
output_state[i] = working_state[i].wrapping_add(original_val);
}
for i in 0..16 {
LittleEndian::write_u32(&mut self.buffer[i * 4..], output_state[i]);
}
self.position = 0;
self.counter = self.counter.wrapping_add(1);
}
pub fn process(&mut self, data: &mut [u8]) {
for byte in data.iter_mut() {
if self.position >= CHACHA20_BLOCK_SIZE {
self.generate_keystream();
}
*byte ^= self.buffer[self.position];
self.position += 1;
}
}
pub fn encrypt(&mut self, data: &mut [u8]) {
self.process(data);
}
pub fn decrypt(&mut self, data: &mut [u8]) {
self.process(data);
}
pub fn keystream(&mut self, output: &mut [u8]) {
for byte in output.iter_mut() {
*byte = 0;
}
self.position = CHACHA20_BLOCK_SIZE;
self.process(output);
}
pub fn seek(&mut self, block_offset: u32) {
self.counter = block_offset.wrapping_add(1);
self.position = CHACHA20_BLOCK_SIZE;
self.buffer.zeroize();
}
pub fn reset(&mut self) {
self.counter = self.state[12]; self.position = CHACHA20_BLOCK_SIZE; self.buffer.zeroize(); }
}
#[cfg(test)]
mod tests;