use cipher::{ crypto_common::{InnerInit, InnerUser}, generic_array::ArrayLength, inout::InOut, AlgorithmName, Block, BlockBackend, BlockCipher, BlockClosure, BlockDecryptMut, BlockSizeUser, ParBlocks, ParBlocksSizeUser, }; use core::fmt; /// ECB mode decryptor. #[derive(Clone)] pub struct Decryptor where C: BlockDecryptMut + BlockCipher, { cipher: C, } impl BlockSizeUser for Decryptor where C: BlockDecryptMut + BlockCipher, { type BlockSize = C::BlockSize; } impl BlockDecryptMut for Decryptor where C: BlockDecryptMut + BlockCipher, { fn decrypt_with_backend_mut(&mut self, f: impl BlockClosure) { let Self { cipher } = self; cipher.decrypt_with_backend_mut(Closure { f }) } } impl InnerUser for Decryptor where C: BlockDecryptMut + BlockCipher, { type Inner = C; } impl InnerInit for Decryptor where C: BlockDecryptMut + BlockCipher, { #[inline] fn inner_init(cipher: C) -> Self { Self { cipher } } } impl AlgorithmName for Decryptor where C: BlockDecryptMut + BlockCipher + AlgorithmName, { fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("ecb::Decryptor<")?; ::write_alg_name(f)?; f.write_str(">") } } impl fmt::Debug for Decryptor where C: BlockDecryptMut + BlockCipher + AlgorithmName, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("ecb::Decryptor<")?; ::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 = BK::ParBlocksSize; } 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); } #[inline(always)] fn proc_par_blocks(&mut self, blocks: InOut<'_, '_, ParBlocks>) { self.backend.proc_par_blocks(blocks); } }