use crate::crypto::cipher::{Cipher, InternalCipherState};
use crate::error::NoiseError;
const MAX_N_VALUE: u64 = u64::MAX;
#[derive(Debug)]
pub struct CipherState<C: Cipher> {
k: Option<C::State>,
n: u64,
}
impl<C: Cipher> CipherState<C> {
pub fn initialize_key(key: Option<&[u8; 32]>) -> Self {
Self {
k: key.map(|k| C::init(k)),
n: 0,
}
}
pub fn has_key(&self) -> bool {
self.k.is_some()
}
pub fn set_nonce(&mut self, nonce: u64) {
self.n = nonce
}
pub fn encrypt_with_ad(
&mut self,
ad: &[u8],
pt_buf: &[u8],
buf: &mut [u8],
) -> Result<usize, NoiseError> {
if self.n + 1 == MAX_N_VALUE {
return Err(NoiseError::MaxNValue);
}
let size = if let Some(c) = &self.k {
let size = c
.encrypt(self.n, ad, pt_buf, buf)
.map_err(|_| NoiseError::InvalidInput("Couldn't encrypt the message"))?;
self.n += 1;
size
} else {
buf[..pt_buf.len()].copy_from_slice(pt_buf);
pt_buf.len()
};
Ok(size)
}
pub fn decrypt_with_ad(
&mut self,
ad: &[u8],
ct_buf: &[u8],
buf: &mut [u8],
) -> Result<(), NoiseError> {
if self.n + 1 == MAX_N_VALUE {
return Err(NoiseError::MaxNValue);
}
if let Some(c) = &self.k {
c.decrypt(self.n, ad, ct_buf, buf)
.map_err(|_| NoiseError::InvalidInput("Couldn't decrypt the message"))?;
self.n += 1;
} else {
buf[..ct_buf.len()].copy_from_slice(ct_buf);
}
Ok(())
}
pub fn rekey(&mut self) -> Result<(), NoiseError> {
if let Some(c) = &mut self.k {
c.rekey().map_err(|_| NoiseError::InvalidInput("Couldn't do the rekey"))?
} else {
return Err(NoiseError::Rekey("Cannot rekey a k that isn't set."));
}
Ok(())
}
pub fn from_parts(k: Option<C::State>, n: u64) -> Self {
Self { k, n }
}
pub fn into_parts(self) -> (Option<C::State>, u64) {
(self.k, self.n)
}
}