1use crate::Piece;
2use rayon::prelude::*;
3use std::io::Write;
4
5const BLOCK_SIZE_BITS: u32 = 24;
6const BLOCK_SIZE: usize = 3;
7const AES_SBOX: [u8; 256] = [
8 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
9 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
10 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
11 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
12 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
13 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
14 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
15 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
16 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
17 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
18 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
19 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
20 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
21 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
22 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
23 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16,
24];
25
26type Block = [u8; 3];
27
28pub struct SBoxDirect(Vec<Block>);
29
30impl SBoxDirect {
31 pub fn new() -> Self {
33 let mut result = vec![[0_u8; 3]; 2_usize.pow(BLOCK_SIZE_BITS)];
34
35 for x in 0..2_u32.pow(BLOCK_SIZE_BITS) {
36 let [.., x1, x2, x3] = x.to_be_bytes();
37 let y = u32::from_be_bytes([
38 0,
39 AES_SBOX[x1 as usize],
40 AES_SBOX[x2 as usize],
41 AES_SBOX[x3 as usize],
42 ]);
43 result[y as usize] = [x1, x2, x3];
44 }
45
46 Self(result)
47 }
48
49 fn get(&self, y: Block) -> Block {
50 let index = u32::from_be_bytes([0, y[0], y[1], y[2]]);
51 self.0[index as usize]
52 }
53}
54
55pub struct SBoxInverse();
56
57impl SBoxInverse {
58 pub fn new() -> Self {
60 Self()
61 }
62
63 fn get(&self, x: Block) -> Block {
64 [
65 AES_SBOX[x[0] as usize],
66 AES_SBOX[x[1] as usize],
67 AES_SBOX[x[2] as usize],
68 ]
69 }
70}
71
72pub fn encode_simple(piece: &mut Piece, iv: Block, breadth_iterations: usize, sbox: &SBoxDirect) {
73 for _ in 0..breadth_iterations {
74 let mut feedback = iv;
75 piece.chunks_exact_mut(BLOCK_SIZE).for_each(|mut block| {
76 feedback = sbox.get([
77 block[0] ^ feedback[0],
78 block[1] ^ feedback[1],
79 block[2] ^ feedback[2],
80 ]);
81
82 block.write_all(&feedback[..]).unwrap();
83 });
84 }
85}
86
87pub fn encode_pipelined_x4(
88 mut pieces: [&mut Piece; 4],
89 ivs: [Block; 4],
90 breadth_iterations: usize,
91 sbox: &SBoxDirect,
92) {
93 for _ in 0..breadth_iterations {
94 for (piece, iv) in pieces.iter_mut().zip(ivs.iter()) {
95 let mut feedback = *iv;
96 piece.chunks_exact_mut(BLOCK_SIZE).for_each(|mut block| {
97 feedback = sbox.get([
98 block[0] ^ feedback[0],
99 block[1] ^ feedback[1],
100 block[2] ^ feedback[2],
101 ]);
102
103 block.write_all(&feedback[..]).unwrap();
104 });
105 }
106 }
107}
108
109pub fn encode_pipelined_x8(
110 mut pieces: [&mut Piece; 8],
111 ivs: [Block; 8],
112 breadth_iterations: usize,
113 sbox: &SBoxDirect,
114) {
115 for _ in 0..breadth_iterations {
116 for (piece, iv) in pieces.iter_mut().zip(ivs.iter()) {
117 let mut feedback = *iv;
118 piece.chunks_exact_mut(BLOCK_SIZE).for_each(|mut block| {
119 feedback = sbox.get([
120 block[0] ^ feedback[0],
121 block[1] ^ feedback[1],
122 block[2] ^ feedback[2],
123 ]);
124
125 block.write_all(&feedback[..]).unwrap();
126 });
127 }
128 }
129}
130
131pub fn encode_pipelined_x16(
132 mut pieces: [&mut Piece; 16],
133 ivs: [Block; 16],
134 breadth_iterations: usize,
135 sbox: &SBoxDirect,
136) {
137 for _ in 0..breadth_iterations {
138 for (piece, iv) in pieces.iter_mut().zip(ivs.iter()) {
139 let mut feedback = *iv;
140 piece.chunks_exact_mut(BLOCK_SIZE).for_each(|mut block| {
141 feedback = sbox.get([
142 block[0] ^ feedback[0],
143 block[1] ^ feedback[1],
144 block[2] ^ feedback[2],
145 ]);
146
147 block.write_all(&feedback[..]).unwrap();
148 });
149 }
150 }
151}
152
153pub fn encode_pipelined_x32(
154 mut pieces: [&mut Piece; 32],
155 ivs: [Block; 32],
156 breadth_iterations: usize,
157 sbox: &SBoxDirect,
158) {
159 for _ in 0..breadth_iterations {
160 for (piece, iv) in pieces.iter_mut().zip(ivs.iter()) {
161 let mut feedback = *iv;
162 piece.chunks_exact_mut(BLOCK_SIZE).for_each(|mut block| {
163 feedback = sbox.get([
164 block[0] ^ feedback[0],
165 block[1] ^ feedback[1],
166 block[2] ^ feedback[2],
167 ]);
168
169 block.write_all(&feedback[..]).unwrap();
170 });
171 }
172 }
173}
174
175pub fn encode_pipelined_x64(
176 mut pieces: [&mut Piece; 64],
177 ivs: [Block; 64],
178 breadth_iterations: usize,
179 sbox: &SBoxDirect,
180) {
181 for _ in 0..breadth_iterations {
182 for (piece, iv) in pieces.iter_mut().zip(ivs.iter()) {
183 let mut feedback = *iv;
184 piece.chunks_exact_mut(BLOCK_SIZE).for_each(|mut block| {
185 feedback = sbox.get([
186 block[0] ^ feedback[0],
187 block[1] ^ feedback[1],
188 block[2] ^ feedback[2],
189 ]);
190
191 block.write_all(&feedback[..]).unwrap();
192 });
193 }
194 }
195}
196
197pub fn encode_simple_parallel(
198 pieces: &mut [Piece],
199 iv: Block,
200 breadth_iterations: usize,
201 sbox: &SBoxDirect,
202 thread_pipelining: usize,
203) {
204 pieces
205 .par_chunks_mut(thread_pipelining)
206 .for_each(|pieces: &mut [Piece]| {
207 for piece in pieces {
208 encode_simple(piece, iv, breadth_iterations, &sbox);
209 }
210 });
211}
212
213pub fn encode_pipelined_x64_parallel(
214 pieces: &mut [Piece],
215 iv: Block,
217 breadth_iterations: usize,
218 sbox: &SBoxDirect,
219) {
220 assert!(pieces.len() % 64 == 0);
221 pieces.par_chunks_mut(64).for_each(|pieces: &mut [Piece]| {
222 for _ in 0..breadth_iterations {
223 for piece in pieces.iter_mut() {
224 let mut feedback = iv;
225 piece.chunks_exact_mut(BLOCK_SIZE).for_each(|mut block| {
226 feedback = sbox.get([
227 block[0] ^ feedback[0],
228 block[1] ^ feedback[1],
229 block[2] ^ feedback[2],
230 ]);
231
232 block.write_all(&feedback[..]).unwrap();
233 });
234 }
235 }
236 });
237}
238
239pub fn decode_simple(piece: &mut Piece, iv: Block, breadth_iterations: usize, sbox: &SBoxInverse) {
240 for _ in 0..breadth_iterations {
241 let mut feedback = iv;
242 piece.chunks_exact_mut(BLOCK_SIZE).for_each(|block| {
243 let previous_feedback = feedback;
244 feedback = [block[0], block[1], block[2]];
245 let decoded = sbox.get([block[0], block[1], block[2]]);
246
247 block[0] = decoded[0] ^ previous_feedback[0];
248 block[1] = decoded[1] ^ previous_feedback[1];
249 block[2] = decoded[2] ^ previous_feedback[2];
250 });
251 }
252}
253
254pub fn decode_simple_parallel(
255 pieces: &mut [Piece],
256 iv: Block,
257 breadth_iterations: usize,
258 sbox: &SBoxInverse,
259 thread_pipelining: usize,
260) {
261 pieces
262 .par_chunks_mut(thread_pipelining)
263 .for_each(|pieces: &mut [Piece]| {
264 for piece in pieces {
265 decode_simple(piece, iv, breadth_iterations, &sbox);
266 }
267 });
268}
269
270#[cfg(test)]
271mod tests {
272 use super::*;
273 use crate::PIECE_SIZE;
274 use rand::Rng;
275
276 #[test]
277 fn test_simple() {
278 let iv = [1, 2, 3];
279 let sbox = SBoxDirect::new();
280 let sbox_inverse = SBoxInverse::new();
281 let mut input = [0u8; PIECE_SIZE];
282 rand::thread_rng().fill(&mut input[..]);
283
284 for &iterations in &[1, 10] {
285 let mut encoding = input;
286 encode_simple(&mut encoding, iv, iterations, &sbox);
287
288 assert_ne!(encoding[..], input[..]);
289
290 decode_simple(&mut encoding, iv, iterations, &sbox_inverse);
291
292 assert_eq!(encoding[..], input[..]);
293 }
294
295 for &iterations in &[1, 10] {
296 let mut encodings_1 = input;
297 let mut encodings_2 = input;
298 let mut encodings_3 = input;
299 let mut encodings_4 = input;
300 encode_pipelined_x4(
301 [
302 &mut encodings_1,
303 &mut encodings_2,
304 &mut encodings_3,
305 &mut encodings_4,
306 ],
307 [iv; 4],
308 iterations,
309 &sbox,
310 );
311
312 assert_ne!(encodings_1[..], input[..]);
313 assert_ne!(encodings_2[..], input[..]);
314 assert_ne!(encodings_3[..], input[..]);
315 assert_ne!(encodings_4[..], input[..]);
316
317 decode_simple(&mut encodings_1, iv, iterations, &sbox_inverse);
318 decode_simple(&mut encodings_2, iv, iterations, &sbox_inverse);
319 decode_simple(&mut encodings_3, iv, iterations, &sbox_inverse);
320 decode_simple(&mut encodings_4, iv, iterations, &sbox_inverse);
321 assert_eq!(encodings_1[..], input[..]);
322 assert_eq!(encodings_2[..], input[..]);
323 assert_eq!(encodings_3[..], input[..]);
324 assert_eq!(encodings_4[..], input[..]);
325 }
326
327 for &iterations in &[1, 10] {
328 let inputs = vec![input; 3];
329 let mut encodings = inputs.clone();
330 encode_simple_parallel(&mut encodings, iv, iterations, &sbox, 1);
331
332 assert_ne!(
333 encodings
334 .iter()
335 .map(|array| array.to_vec())
336 .collect::<Vec<_>>(),
337 inputs
338 .iter()
339 .map(|array| array.to_vec())
340 .collect::<Vec<_>>()
341 );
342
343 decode_simple_parallel(&mut encodings, iv, iterations, &sbox_inverse, 1);
344
345 assert_eq!(
346 encodings
347 .iter()
348 .map(|array| array.to_vec())
349 .collect::<Vec<_>>(),
350 inputs
351 .iter()
352 .map(|array| array.to_vec())
353 .collect::<Vec<_>>()
354 );
355 }
356 }
357}