Skip to main content

cipher/block/
backends.rs

1use common::{Block, BlockSizeUser, ParBlocks, ParBlocksSizeUser, typenum::Unsigned};
2use inout::{InOut, InOutBuf};
3
4/// Trait implemented by block cipher mode encryption backends.
5pub trait BlockCipherEncBackend: ParBlocksSizeUser {
6    /// Encrypt single inout block.
7    fn encrypt_block(&self, block: InOut<'_, '_, Block<Self>>);
8
9    /// Encrypt inout blocks in parallel.
10    #[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    /// Encrypt buffer of inout blocks. Length of the buffer MUST be smaller
18    /// than `Self::ParBlocksSize`.
19    #[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    /// Encrypt single block in-place.
28    #[inline(always)]
29    fn encrypt_block_inplace(&self, block: &mut Block<Self>) {
30        self.encrypt_block(block.into());
31    }
32
33    /// Encrypt blocks in parallel in-place.
34    #[inline(always)]
35    fn encrypt_par_blocks_inplace(&self, blocks: &mut ParBlocks<Self>) {
36        self.encrypt_par_blocks(blocks.into());
37    }
38
39    /// Encrypt buffer of blocks in-place. Length of the buffer MUST be smaller
40    /// than `Self::ParBlocksSize`.
41    #[inline(always)]
42    fn encrypt_tail_blocks_inplace(&self, blocks: &mut [Block<Self>]) {
43        self.encrypt_tail_blocks(blocks.into());
44    }
45}
46
47/// Trait for [`BlockCipherEncBackend`] users.
48///
49/// This trait is used to define rank-2 closures.
50pub trait BlockCipherEncClosure: BlockSizeUser {
51    /// Execute closure with the provided block cipher backend.
52    fn call<B: BlockCipherEncBackend<BlockSize = Self::BlockSize>>(self, backend: &B);
53}
54
55/// Trait implemented by block cipher decryption backends.
56pub trait BlockCipherDecBackend: ParBlocksSizeUser {
57    /// Decrypt single inout block.
58    fn decrypt_block(&self, block: InOut<'_, '_, Block<Self>>);
59
60    /// Decrypt inout blocks in parallel.
61    #[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    /// Decrypt buffer of inout blocks. Length of the buffer MUST be smaller
69    /// than `Self::ParBlocksSize`.
70    #[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    /// Decrypt single block in-place.
79    #[inline(always)]
80    fn decrypt_block_inplace(&self, block: &mut Block<Self>) {
81        self.decrypt_block(block.into());
82    }
83
84    /// Decrypt blocks in parallel in-place.
85    #[inline(always)]
86    fn decrypt_par_blocks_inplace(&self, blocks: &mut ParBlocks<Self>) {
87        self.decrypt_par_blocks(blocks.into());
88    }
89
90    /// Decrypt buffer of blocks in-place. Length of the buffer MUST be smaller
91    /// than `Self::ParBlocksSize`.
92    #[inline(always)]
93    fn decrypt_tail_blocks_inplace(&self, blocks: &mut [Block<Self>]) {
94        self.decrypt_tail_blocks(blocks.into());
95    }
96}
97
98/// Trait for [`BlockCipherDecBackend`] users.
99///
100/// This trait is used to define rank-2 closures.
101pub trait BlockCipherDecClosure: BlockSizeUser {
102    /// Execute closure with the provided block cipher backend.
103    fn call<B: BlockCipherDecBackend<BlockSize = Self::BlockSize>>(self, backend: &B);
104}
105
106/// Trait implemented by block cipher mode encryption backends.
107pub trait BlockModeEncBackend: ParBlocksSizeUser {
108    /// Encrypt single inout block.
109    fn encrypt_block(&mut self, block: InOut<'_, '_, Block<Self>>);
110
111    /// Encrypt inout blocks in parallel.
112    #[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    /// Encrypt buffer of inout blocks. Length of the buffer MUST be smaller
120    /// than `Self::ParBlocksSize`.
121    #[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    /// Encrypt single block in-place.
130    #[inline(always)]
131    fn encrypt_block_inplace(&mut self, block: &mut Block<Self>) {
132        self.encrypt_block(block.into());
133    }
134
135    /// Encrypt blocks in parallel in-place.
136    #[inline(always)]
137    fn encrypt_par_blocks_inplace(&mut self, blocks: &mut ParBlocks<Self>) {
138        self.encrypt_par_blocks(blocks.into());
139    }
140
141    /// Encrypt buffer of blocks in-place. Length of the buffer MUST be smaller
142    /// than `Self::ParBlocksSize`.
143    #[inline(always)]
144    fn encrypt_tail_blocks_inplace(&mut self, blocks: &mut [Block<Self>]) {
145        self.encrypt_tail_blocks(blocks.into());
146    }
147}
148
149/// Trait for [`BlockModeEncBackend`] users.
150///
151/// This trait is used to define rank-2 closures.
152pub trait BlockModeEncClosure: BlockSizeUser {
153    /// Execute closure with the provided block cipher backend.
154    fn call<B: BlockModeEncBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B);
155}
156
157/// Trait implemented by block cipher mode decryption backends.
158pub trait BlockModeDecBackend: ParBlocksSizeUser {
159    /// Decrypt single inout block.
160    fn decrypt_block(&mut self, block: InOut<'_, '_, Block<Self>>);
161
162    /// Decrypt inout blocks in parallel.
163    #[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    /// Decrypt buffer of inout blocks. Length of the buffer MUST be smaller
171    /// than `Self::ParBlocksSize`.
172    #[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    /// Decrypt single block in-place.
181    #[inline(always)]
182    fn decrypt_block_inplace(&mut self, block: &mut Block<Self>) {
183        self.decrypt_block(block.into());
184    }
185
186    /// Decrypt blocks in parallel in-place.
187    #[inline(always)]
188    fn decrypt_par_blocks_inplace(&mut self, blocks: &mut ParBlocks<Self>) {
189        self.decrypt_par_blocks(blocks.into());
190    }
191
192    /// Decrypt buffer of blocks in-place. Length of the buffer MUST be smaller
193    /// than `Self::ParBlocksSize`.
194    #[inline(always)]
195    fn decrypt_tail_blocks_inplace(&mut self, blocks: &mut [Block<Self>]) {
196        self.decrypt_tail_blocks(blocks.into());
197    }
198}
199
200/// Trait for [`BlockModeDecBackend`] users.
201///
202/// This trait is used to define rank-2 closures.
203pub trait BlockModeDecClosure: BlockSizeUser {
204    /// Execute closure with the provided block cipher backend.
205    fn call<B: BlockModeDecBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B);
206}