1use common::{Block, BlockSizeUser, ParBlocks, ParBlocksSizeUser, typenum::Unsigned};
2use inout::{InOut, InOutBuf};
3
4pub trait BlockCipherEncBackend: ParBlocksSizeUser {
6 fn encrypt_block(&self, block: InOut<'_, '_, Block<Self>>);
8
9 #[inline(always)]
11 fn encrypt_par_blocks(&self, mut blocks: InOut<'_, '_, ParBlocks<Self>>) {
12 for i in 0..Self::ParBlocksSize::USIZE {
13 self.encrypt_block(blocks.get(i));
14 }
15 }
16
17 #[inline(always)]
20 fn encrypt_tail_blocks(&self, blocks: InOutBuf<'_, '_, Block<Self>>) {
21 assert!(blocks.len() < Self::ParBlocksSize::USIZE);
22 for block in blocks {
23 self.encrypt_block(block);
24 }
25 }
26
27 #[inline(always)]
29 fn encrypt_block_inplace(&self, block: &mut Block<Self>) {
30 self.encrypt_block(block.into());
31 }
32
33 #[inline(always)]
35 fn encrypt_par_blocks_inplace(&self, blocks: &mut ParBlocks<Self>) {
36 self.encrypt_par_blocks(blocks.into());
37 }
38
39 #[inline(always)]
42 fn encrypt_tail_blocks_inplace(&self, blocks: &mut [Block<Self>]) {
43 self.encrypt_tail_blocks(blocks.into());
44 }
45}
46
47pub trait BlockCipherEncClosure: BlockSizeUser {
51 fn call<B: BlockCipherEncBackend<BlockSize = Self::BlockSize>>(self, backend: &B);
53}
54
55pub trait BlockCipherDecBackend: ParBlocksSizeUser {
57 fn decrypt_block(&self, block: InOut<'_, '_, Block<Self>>);
59
60 #[inline(always)]
62 fn decrypt_par_blocks(&self, mut blocks: InOut<'_, '_, ParBlocks<Self>>) {
63 for i in 0..Self::ParBlocksSize::USIZE {
64 self.decrypt_block(blocks.get(i));
65 }
66 }
67
68 #[inline(always)]
71 fn decrypt_tail_blocks(&self, blocks: InOutBuf<'_, '_, Block<Self>>) {
72 assert!(blocks.len() < Self::ParBlocksSize::USIZE);
73 for block in blocks {
74 self.decrypt_block(block);
75 }
76 }
77
78 #[inline(always)]
80 fn decrypt_block_inplace(&self, block: &mut Block<Self>) {
81 self.decrypt_block(block.into());
82 }
83
84 #[inline(always)]
86 fn decrypt_par_blocks_inplace(&self, blocks: &mut ParBlocks<Self>) {
87 self.decrypt_par_blocks(blocks.into());
88 }
89
90 #[inline(always)]
93 fn decrypt_tail_blocks_inplace(&self, blocks: &mut [Block<Self>]) {
94 self.decrypt_tail_blocks(blocks.into());
95 }
96}
97
98pub trait BlockCipherDecClosure: BlockSizeUser {
102 fn call<B: BlockCipherDecBackend<BlockSize = Self::BlockSize>>(self, backend: &B);
104}
105
106pub trait BlockModeEncBackend: ParBlocksSizeUser {
108 fn encrypt_block(&mut self, block: InOut<'_, '_, Block<Self>>);
110
111 #[inline(always)]
113 fn encrypt_par_blocks(&mut self, mut blocks: InOut<'_, '_, ParBlocks<Self>>) {
114 for i in 0..Self::ParBlocksSize::USIZE {
115 self.encrypt_block(blocks.get(i));
116 }
117 }
118
119 #[inline(always)]
122 fn encrypt_tail_blocks(&mut self, blocks: InOutBuf<'_, '_, Block<Self>>) {
123 assert!(blocks.len() < Self::ParBlocksSize::USIZE);
124 for block in blocks {
125 self.encrypt_block(block);
126 }
127 }
128
129 #[inline(always)]
131 fn encrypt_block_inplace(&mut self, block: &mut Block<Self>) {
132 self.encrypt_block(block.into());
133 }
134
135 #[inline(always)]
137 fn encrypt_par_blocks_inplace(&mut self, blocks: &mut ParBlocks<Self>) {
138 self.encrypt_par_blocks(blocks.into());
139 }
140
141 #[inline(always)]
144 fn encrypt_tail_blocks_inplace(&mut self, blocks: &mut [Block<Self>]) {
145 self.encrypt_tail_blocks(blocks.into());
146 }
147}
148
149pub trait BlockModeEncClosure: BlockSizeUser {
153 fn call<B: BlockModeEncBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B);
155}
156
157pub trait BlockModeDecBackend: ParBlocksSizeUser {
159 fn decrypt_block(&mut self, block: InOut<'_, '_, Block<Self>>);
161
162 #[inline(always)]
164 fn decrypt_par_blocks(&mut self, mut blocks: InOut<'_, '_, ParBlocks<Self>>) {
165 for i in 0..Self::ParBlocksSize::USIZE {
166 self.decrypt_block(blocks.get(i));
167 }
168 }
169
170 #[inline(always)]
173 fn decrypt_tail_blocks(&mut self, blocks: InOutBuf<'_, '_, Block<Self>>) {
174 assert!(blocks.len() < Self::ParBlocksSize::USIZE);
175 for block in blocks {
176 self.decrypt_block(block);
177 }
178 }
179
180 #[inline(always)]
182 fn decrypt_block_inplace(&mut self, block: &mut Block<Self>) {
183 self.decrypt_block(block.into());
184 }
185
186 #[inline(always)]
188 fn decrypt_par_blocks_inplace(&mut self, blocks: &mut ParBlocks<Self>) {
189 self.decrypt_par_blocks(blocks.into());
190 }
191
192 #[inline(always)]
195 fn decrypt_tail_blocks_inplace(&mut self, blocks: &mut [Block<Self>]) {
196 self.decrypt_tail_blocks(blocks.into());
197 }
198}
199
200pub trait BlockModeDecClosure: BlockSizeUser {
204 fn call<B: BlockModeDecBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B);
206}