use cipher::{ consts::U1, crypto_common::{InnerInit, InnerUser}, generic_array::ArrayLength, inout::InOut, AlgorithmName, Block, BlockBackend, BlockCipher, BlockClosure, BlockEncryptMut, BlockSizeUser, ParBlocksSizeUser, }; use core::fmt; /// ECB mode encryptor. #[derive(Clone)] pub struct Encryptor where C: BlockEncryptMut + BlockCipher, { cipher: C, } impl BlockSizeUser for Encryptor where C: BlockEncryptMut + BlockCipher, { type BlockSize = C::BlockSize; } impl BlockEncryptMut for Encryptor where C: BlockEncryptMut + BlockCipher, { fn encrypt_with_backend_mut(&mut self, f: impl BlockClosure) { let Self { cipher, .. } = self; cipher.encrypt_with_backend_mut(Closure { f }) } } impl InnerUser for Encryptor where C: BlockEncryptMut + BlockCipher, { type Inner = C; } impl InnerInit for Encryptor where C: BlockEncryptMut + BlockCipher, { #[inline] fn inner_init(cipher: C) -> Self { Self { cipher } } } impl AlgorithmName for Encryptor where C: BlockEncryptMut + BlockCipher + AlgorithmName, { fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("ecb::Encryptor<")?; ::write_alg_name(f)?; f.write_str(">") } } impl fmt::Debug for Encryptor where C: BlockEncryptMut + BlockCipher + AlgorithmName, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("ecb::Encryptor<")?; ::write_alg_name(f)?; f.write_str("> { ... }") } } struct Closure where BS: ArrayLength, BC: BlockClosure, { f: BC, } impl BlockSizeUser for Closure where BS: ArrayLength, BC: BlockClosure, { type BlockSize = BS; } impl BlockClosure for Closure where BS: ArrayLength, BC: BlockClosure, { #[inline(always)] fn call>(self, backend: &mut B) { let Self { f, .. } = self; f.call(&mut Backend { backend }); } } struct Backend<'a, BS, BK> where BS: ArrayLength, BK: BlockBackend, { backend: &'a mut BK, } impl<'a, BS, BK> BlockSizeUser for Backend<'a, BS, BK> where BS: ArrayLength, BK: BlockBackend, { type BlockSize = BS; } impl<'a, BS, BK> ParBlocksSizeUser for Backend<'a, BS, BK> where BS: ArrayLength, BK: BlockBackend, { type ParBlocksSize = U1; } impl<'a, BS, BK> BlockBackend for Backend<'a, BS, BK> where BS: ArrayLength, BK: BlockBackend, { #[inline(always)] fn proc_block(&mut self, block: InOut<'_, '_, Block>) { self.backend.proc_block(block); } }