use std::{
io::{Read, Write},
marker::PhantomData,
};
use crate::Result;
#[cfg(feature = "aes_ctr")]
use aes::{Aes128, Aes256};
#[cfg(feature = "chacha20")]
use chacha20::ChaCha20;
use cipher::{KeyIvInit, StreamCipher};
#[cfg(feature = "aes_ctr")]
use ctr::Ctr128BE;
#[cfg(feature = "salsa20")]
use salsa20::Salsa20;
use crate::RwBuilder;
#[derive(Debug)]
#[must_use]
pub struct Builder<B, C, K, N>
where
B: RwBuilder,
C: StreamCipher,
{
wrapped: B,
key: K,
nonce: N,
_marker: PhantomData<C>,
}
impl<B, C, K, N> Builder<B, C, K, N>
where
B: RwBuilder,
C: StreamCipher,
{
pub const fn new(builder: B, key: K, nonce: N) -> Self {
Self { wrapped: builder, key, nonce, _marker: PhantomData }
}
}
#[cfg(feature = "chacha20")]
pub type ChaCha20Key = chacha20::Key;
#[cfg(feature = "chacha20")]
pub type ChaCha20Nonce = [u8; 12];
#[cfg(feature = "chacha20")]
pub type ChaCha20Builder<B> = Builder<B, ChaCha20, ChaCha20Key, ChaCha20Nonce>;
#[cfg(feature = "salsa20")]
pub type Salsa20Key = salsa20::Key;
#[cfg(feature = "salsa20")]
pub type Salsa20Nonce = salsa20::Nonce;
#[cfg(feature = "salsa20")]
pub type Salsa20Builder<B> = Builder<B, Salsa20, Salsa20Key, Salsa20Nonce>;
#[cfg(feature = "aes_ctr")]
pub type Aes128Ctr = Ctr128BE<Aes128>;
#[cfg(feature = "aes_ctr")]
pub type Aes256Ctr = Ctr128BE<Aes256>;
#[cfg(feature = "aes_ctr")]
pub type Aes128Key = [u8; 16];
#[cfg(feature = "aes_ctr")]
pub type Aes256Key = [u8; 32];
#[cfg(feature = "aes_ctr")]
pub type AesNonce = [u8; 16];
#[cfg(feature = "aes_ctr")]
pub type Aes128CtrBuilder<B> = Builder<B, Aes128Ctr, Aes128Key, AesNonce>;
#[cfg(feature = "aes_ctr")]
pub type Aes256CtrBuilder<B> = Builder<B, Aes256Ctr, Aes256Key, AesNonce>;
trait CipherFactory<C> {
fn create_cipher(&self) -> C;
}
#[cfg(feature = "chacha20")]
impl<B> CipherFactory<ChaCha20> for Builder<B, ChaCha20, ChaCha20Key, ChaCha20Nonce>
where
B: RwBuilder,
{
fn create_cipher(&self) -> ChaCha20 {
ChaCha20::new(&self.key, (&self.nonce).into())
}
}
#[cfg(feature = "salsa20")]
impl<B> CipherFactory<Salsa20> for Builder<B, Salsa20, Salsa20Key, Salsa20Nonce>
where
B: RwBuilder,
{
fn create_cipher(&self) -> Salsa20 {
Salsa20::new(&self.key, &self.nonce)
}
}
#[cfg(feature = "aes_ctr")]
impl<B> CipherFactory<Aes128Ctr> for Builder<B, Aes128Ctr, Aes128Key, AesNonce>
where
B: RwBuilder,
{
fn create_cipher(&self) -> Aes128Ctr {
Aes128Ctr::new((&self.key).into(), (&self.nonce).into())
}
}
#[cfg(feature = "aes_ctr")]
impl<B> CipherFactory<Aes256Ctr> for Builder<B, Aes256Ctr, Aes256Key, AesNonce>
where
B: RwBuilder,
{
fn create_cipher(&self) -> Aes256Ctr {
Aes256Ctr::new((&self.key).into(), (&self.nonce).into())
}
}
impl<B, C, K, N> RwBuilder for Builder<B, C, K, N>
where
B: RwBuilder,
C: StreamCipher,
Self: CipherFactory<C>,
{
type Reader = Reader<B::Reader, C>;
type Writer = Writer<B::Writer, C>;
fn reader(&self) -> Result<Self::Reader> {
let reader = self.wrapped.reader()?;
let cipher = self.create_cipher();
Ok(Reader { cipher, reader })
}
fn writer(&self) -> Result<Self::Writer> {
let writer = self.wrapped.writer()?;
let cipher = self.create_cipher();
Ok(Writer { cipher, writer })
}
}
#[derive(Debug)]
pub struct Reader<R, C>
where
R: Read,
C: StreamCipher,
{
cipher: C,
reader: R,
}
impl<R, C> Read for Reader<R, C>
where
R: Read,
C: StreamCipher,
{
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let bytes_read = self.reader.read(buf)?;
self.cipher.try_apply_keystream(buf).map_err(std::io::Error::other)?;
Ok(bytes_read)
}
}
#[derive(Debug)]
pub struct Writer<W, C>
where
W: Write,
C: StreamCipher,
{
cipher: C,
writer: W,
}
impl<W, C> Write for Writer<W, C>
where
W: Write,
C: StreamCipher,
{
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let mut buffer = buf.to_owned();
self.cipher.try_apply_keystream(buffer.as_mut_slice()).map_err(std::io::Error::other)?;
self.writer.write(buffer.as_slice())
}
fn flush(&mut self) -> std::io::Result<()> {
self.writer.flush()
}
}