use crate::BlockCipherMode;
use core::fmt::{Display, Formatter};
use tc_block_cipher::{BlockCipher, BlockCipherInit, CipherDirection};
pub struct EcbBlockCipher<C> {
cipher: C,
}
impl<C> EcbBlockCipher<C> {
pub const fn new(cipher: C) -> Self {
Self { cipher }
}
}
impl<C: Display> Display for EcbBlockCipher<C> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
self.cipher.fmt(f)?;
f.write_str("/ECB")
}
}
impl<C: BlockCipher> BlockCipher for EcbBlockCipher<C> {
type Error = C::Error;
fn block_size(&self) -> usize {
self.cipher.block_size()
}
fn process_block(&mut self, input: &[u8], output: &mut [u8]) -> Result<usize, Self::Error> {
self.cipher.process_block(input, output)
}
}
impl<C, P: ?Sized> BlockCipherInit<P> for EcbBlockCipher<C>
where
C: BlockCipherInit<P>,
{
type Error = <C as BlockCipherInit<P>>::Error;
fn init(
&mut self,
direction: CipherDirection,
params: &P,
) -> Result<(), <Self as BlockCipherInit<P>>::Error> {
self.cipher.init(direction, params)
}
}
impl<C: BlockCipher> BlockCipherMode for EcbBlockCipher<C> {
type Cipher = C;
fn underlying_cipher(&self) -> &Self::Cipher {
&self.cipher
}
fn is_partial_block_okay(&self) -> bool {
false
}
fn reset(&mut self) {}
}