1use cipher::{
2 crypto_common::{InnerInit, InnerUser},
3 generic_array::ArrayLength,
4 inout::InOut,
5 AlgorithmName, Block, BlockBackend, BlockCipher, BlockClosure, BlockDecryptMut, BlockSizeUser,
6 ParBlocks, ParBlocksSizeUser,
7};
8use core::fmt;
9
10#[derive(Clone)]
12pub struct Decryptor<C>
13where
14 C: BlockDecryptMut + BlockCipher,
15{
16 cipher: C,
17}
18
19impl<C> BlockSizeUser for Decryptor<C>
20where
21 C: BlockDecryptMut + BlockCipher,
22{
23 type BlockSize = C::BlockSize;
24}
25
26impl<C> BlockDecryptMut for Decryptor<C>
27where
28 C: BlockDecryptMut + BlockCipher,
29{
30 fn decrypt_with_backend_mut(&mut self, f: impl BlockClosure<BlockSize = Self::BlockSize>) {
31 let Self { cipher } = self;
32 cipher.decrypt_with_backend_mut(Closure { f })
33 }
34}
35
36impl<C> InnerUser for Decryptor<C>
37where
38 C: BlockDecryptMut + BlockCipher,
39{
40 type Inner = C;
41}
42
43impl<C> InnerInit for Decryptor<C>
44where
45 C: BlockDecryptMut + BlockCipher,
46{
47 #[inline]
48 fn inner_init(cipher: C) -> Self {
49 Self { cipher }
50 }
51}
52
53impl<C> AlgorithmName for Decryptor<C>
54where
55 C: BlockDecryptMut + BlockCipher + AlgorithmName,
56{
57 fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 f.write_str("ecb::Decryptor<")?;
59 <C as AlgorithmName>::write_alg_name(f)?;
60 f.write_str(">")
61 }
62}
63
64impl<C> fmt::Debug for Decryptor<C>
65where
66 C: BlockDecryptMut + BlockCipher + AlgorithmName,
67{
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 f.write_str("ecb::Decryptor<")?;
70 <C as AlgorithmName>::write_alg_name(f)?;
71 f.write_str("> { ... }")
72 }
73}
74
75struct Closure<BS, BC>
76where
77 BS: ArrayLength<u8>,
78 BC: BlockClosure<BlockSize = BS>,
79{
80 f: BC,
81}
82
83impl<BS, BC> BlockSizeUser for Closure<BS, BC>
84where
85 BS: ArrayLength<u8>,
86 BC: BlockClosure<BlockSize = BS>,
87{
88 type BlockSize = BS;
89}
90
91impl<BS, BC> BlockClosure for Closure<BS, BC>
92where
93 BS: ArrayLength<u8>,
94 BC: BlockClosure<BlockSize = BS>,
95{
96 #[inline(always)]
97 fn call<B: BlockBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B) {
98 let Self { f } = self;
99 f.call(&mut Backend { backend });
100 }
101}
102
103struct Backend<'a, BS, BK>
104where
105 BS: ArrayLength<u8>,
106 BK: BlockBackend<BlockSize = BS>,
107{
108 backend: &'a mut BK,
109}
110
111impl<'a, BS, BK> BlockSizeUser for Backend<'a, BS, BK>
112where
113 BS: ArrayLength<u8>,
114 BK: BlockBackend<BlockSize = BS>,
115{
116 type BlockSize = BS;
117}
118
119impl<'a, BS, BK> ParBlocksSizeUser for Backend<'a, BS, BK>
120where
121 BS: ArrayLength<u8>,
122 BK: BlockBackend<BlockSize = BS>,
123{
124 type ParBlocksSize = BK::ParBlocksSize;
125}
126
127impl<'a, BS, BK> BlockBackend for Backend<'a, BS, BK>
128where
129 BS: ArrayLength<u8>,
130 BK: BlockBackend<BlockSize = BS>,
131{
132 #[inline(always)]
133 fn proc_block(&mut self, block: InOut<'_, '_, Block<Self>>) {
134 self.backend.proc_block(block);
135 }
136
137 #[inline(always)]
138 fn proc_par_blocks(&mut self, blocks: InOut<'_, '_, ParBlocks<Self>>) {
139 self.backend.proc_par_blocks(blocks);
140 }
141}