1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate::crypto::aes;
use crate::crypto::rc;
use crate::result::*;
use core::ptr;
use core::arch::aarch64;
use core::arch::asm;

unsafe fn increment_ctr(ctr: aarch64::uint8x16_t) -> aarch64::uint8x16_t {
    let mut inc = aarch64::vdupq_n_u8(0);
    let mut high = 0u64;
    let mut low = 0u64;

    asm!(
        "mov {high}, {ctr:v}.d[0]",
        "mov {low}, {ctr:v}.d[1]",
        "rev {high}, {high}",
        "rev {low}, {low}",
        "adds {low}, {low}, 1",
        "cinc {high}, {high}, cs",
        "rev {high}, {high}",
        "rev {low}, {low}",
        "mov {inc:v}.d[0], {high}",
        "mov {inc:v}.d[1], {low}",

        inc = inout(vreg) inc,
        high = inout(reg) high,
        low = inout(reg) low,
        ctr = in(vreg) ctr
    );

    // Silence warnings of the last assigned value not being read
    let _ = high;
    let _ = low;

    inc
}

pub struct Context {
    aes_ctx: aes::a128::Context,
    ctr: [u8; aes::BLOCK_SIZE],
    enc_ctr_buffer: [u8; aes::BLOCK_SIZE],
    buffer_offset: usize
}

impl Context {
    pub fn new(key: &[u8], ctr: &[u8]) -> Result<Self> {
        let mut ctx = Self {
            aes_ctx: aes::a128::Context::new(key, true)?,
            ctr: [0; aes::BLOCK_SIZE],
            enc_ctr_buffer: [0; aes::BLOCK_SIZE],
            buffer_offset: 0
        };

        ctx.reset_ctr(ctr)?;

        Ok(ctx)
    }

    pub fn reset_ctr(&mut self, ctr: &[u8]) -> Result<()> {
        result_return_unless!(ctr.len() == aes::BLOCK_SIZE, rc::ResultInvalidSize);

        unsafe {
            ptr::copy(ctr.as_ptr(), self.ctr.as_mut_ptr(), self.ctr.len());
        }
        self.enc_ctr_buffer = [0; aes::BLOCK_SIZE];
        self.buffer_offset = 0;
        Ok(())
    }

    unsafe fn crypt_blocks(&mut self, src: *const u8, dst: *mut u8, block_count: usize) {
        let round_key_0 = aarch64::vld1q_u8(self.aes_ctx.round_keys[0].as_ptr());
        let round_key_1 = aarch64::vld1q_u8(self.aes_ctx.round_keys[1].as_ptr());
        let round_key_2 = aarch64::vld1q_u8(self.aes_ctx.round_keys[2].as_ptr());
        let round_key_3 = aarch64::vld1q_u8(self.aes_ctx.round_keys[3].as_ptr());
        let round_key_4 = aarch64::vld1q_u8(self.aes_ctx.round_keys[4].as_ptr());
        let round_key_5 = aarch64::vld1q_u8(self.aes_ctx.round_keys[5].as_ptr());
        let round_key_6 = aarch64::vld1q_u8(self.aes_ctx.round_keys[6].as_ptr());
        let round_key_7 = aarch64::vld1q_u8(self.aes_ctx.round_keys[7].as_ptr());
        let round_key_8 = aarch64::vld1q_u8(self.aes_ctx.round_keys[8].as_ptr());
        let round_key_9 = aarch64::vld1q_u8(self.aes_ctx.round_keys[9].as_ptr());
        let round_key_10 = aarch64::vld1q_u8(self.aes_ctx.round_keys[10].as_ptr());

        let mut ctr0 = aarch64::vld1q_u8(self.ctr.as_ptr());

        let mut high = 0u64;
        let mut low = 0u64;

        let mut cur_src = src;
        let mut cur_dst = dst;
        let mut cur_count = block_count; 
        if cur_count >= 3 {
            let mut ctr1 = increment_ctr(ctr0);
            let mut ctr2 = increment_ctr(ctr1);
            let mut high_tmp = 0u64;
            let mut low_tmp = 0u64;

            while cur_count >= 3 {
                let block0 = aarch64::vld1q_u8(cur_src);
                cur_src = cur_src.add(aes::BLOCK_SIZE);
                let block1 = aarch64::vld1q_u8(cur_src);
                cur_src = cur_src.add(aes::BLOCK_SIZE);
                let block2 = aarch64::vld1q_u8(cur_src);
                cur_src = cur_src.add(aes::BLOCK_SIZE);

                let mut tmp0 = ctr0;
                let mut tmp1 = ctr1;
                let mut tmp2 = ctr2;

                // ...
                asm!(
                    "aese {tmp0:v}.16b, {round_key_0:v}.16b",
                    "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                    "mov {high}, {ctr2:v}.d[0]",

                    "aese {tmp1:v}.16b, {round_key_0:v}.16b",
                    "aesmc {tmp1:v}.16b, {tmp1:v}.16b",

                    "mov {low}, {ctr2:v}.d[1]",

                    "aese {tmp2:v}.16b, {round_key_0:v}.16b",
                    "aesmc {tmp2:v}.16b, {tmp2:v}.16b",

                    "rev {high}, {high}",

                    "aese {tmp0:v}.16b, {round_key_1:v}.16b",
                    "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                    "rev {low}, {low}",

                    "aese {tmp1:v}.16b, {round_key_1:v}.16b",
                    "aesmc {tmp1:v}.16b, {tmp1:v}.16b",

                    "adds {low}, {low}, 1",

                    "aese {tmp2:v}.16b, {round_key_1:v}.16b",
                    "aesmc {tmp2:v}.16b, {tmp2:v}.16b",

                    "cinc {high}, {high}, cs",

                    "aese {tmp0:v}.16b, {round_key_2:v}.16b",
                    "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                    "rev {high_tmp}, {high}",

                    
                    "aese {tmp1:v}.16b, {round_key_2:v}.16b",
                    "aesmc {tmp1:v}.16b, {tmp1:v}.16b",

                    "rev {low_tmp}, {low}",

                    "aese {tmp2:v}.16b, {round_key_2:v}.16b",
                    "aesmc {tmp2:v}.16b, {tmp2:v}.16b",

                    "mov {ctr0:v}.d[0], {high_tmp}",

                    "aese {tmp0:v}.16b, {round_key_3:v}.16b",
                    "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                    "mov {ctr0:v}.d[1], {low_tmp}",

                    "aese {tmp1:v}.16b, {round_key_3:v}.16b",
                    "aesmc {tmp1:v}.16b, {tmp1:v}.16b",

                    "adds {low}, {low}, 1",

                    "aese {tmp2:v}.16b, {round_key_3:v}.16b",
                    "aesmc {tmp2:v}.16b, {tmp2:v}.16b",

                    "cinc {high}, {high}, cs",

                    "aese {tmp0:v}.16b, {round_key_4:v}.16b",
                    "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                    "rev {high_tmp}, {high}",

                    "aese {tmp1:v}.16b, {round_key_4:v}.16b",
                    "aesmc {tmp1:v}.16b, {tmp1:v}.16b",

                    "rev {low_tmp}, {low}",

                    "aese {tmp2:v}.16b, {round_key_4:v}.16b",
                    "aesmc {tmp2:v}.16b, {tmp2:v}.16b",

                    "mov {ctr1:v}.d[0], {high_tmp}",

                    "aese {tmp0:v}.16b, {round_key_5:v}.16b",
                    "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                    "mov {ctr1:v}.d[1], {low_tmp}",

                    "aese {tmp1:v}.16b, {round_key_5:v}.16b",
                    "aesmc {tmp1:v}.16b, {tmp1:v}.16b",

                    "adds {low}, {low}, 1",

                    "aese {tmp2:v}.16b, {round_key_5:v}.16b",
                    "aesmc {tmp2:v}.16b, {tmp2:v}.16b",

                    "cinc {high}, {high}, cs",

                    "aese {tmp0:v}.16b, {round_key_6:v}.16b",
                    "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                    "rev {high_tmp}, {high}",

                    "aese {tmp1:v}.16b, {round_key_6:v}.16b",
                    "aesmc {tmp1:v}.16b, {tmp1:v}.16b",

                    "rev {low_tmp}, {low}",

                    "aese {tmp2:v}.16b, {round_key_6:v}.16b",
                    "aesmc {tmp2:v}.16b, {tmp2:v}.16b",

                    "mov {ctr2:v}.d[0], {high_tmp}",

                    "aese {tmp0:v}.16b, {round_key_7:v}.16b",
                    "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                    "mov {ctr2:v}.d[1], {low_tmp}",

                    "aese {tmp1:v}.16b, {round_key_7:v}.16b",
                    "aesmc {tmp1:v}.16b, {tmp1:v}.16b",

                    "aese {tmp2:v}.16b, {round_key_7:v}.16b",
                    "aesmc {tmp2:v}.16b, {tmp2:v}.16b",

                    "aese {tmp0:v}.16b, {round_key_8:v}.16b",
                    "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                    "aese {tmp1:v}.16b, {round_key_8:v}.16b",
                    "aesmc {tmp1:v}.16b, {tmp1:v}.16b",

                    "aese {tmp2:v}.16b, {round_key_8:v}.16b",
                    "aesmc {tmp2:v}.16b, {tmp2:v}.16b",

                    "aese {tmp0:v}.16b, {round_key_9:v}.16b",
                    
                    "aese {tmp1:v}.16b, {round_key_9:v}.16b",
                    
                    "aese {tmp2:v}.16b, {round_key_9:v}.16b",

                    "eor {tmp0:v}.16b, {tmp0:v}.16b, {round_key_10:v}.16b",

                    "eor {tmp1:v}.16b, {tmp1:v}.16b, {round_key_10:v}.16b",

                    "eor {tmp2:v}.16b, {tmp2:v}.16b, {round_key_10:v}.16b",

                    round_key_0 = in(vreg) round_key_0,
                    round_key_1 = in(vreg) round_key_1,
                    round_key_2 = in(vreg) round_key_2,
                    round_key_3 = in(vreg) round_key_3,
                    round_key_4 = in(vreg) round_key_4,
                    round_key_5 = in(vreg) round_key_5,
                    round_key_6 = in(vreg) round_key_6,
                    round_key_7 = in(vreg) round_key_7,
                    round_key_8 = in(vreg) round_key_8,
                    round_key_9 = in(vreg) round_key_9,
                    round_key_10 = in(vreg) round_key_10,

                    tmp0 = inout(vreg) tmp0,
                    tmp1 = inout(vreg) tmp1,
                    tmp2 = inout(vreg) tmp2,

                    high = inout(reg) high,
                    low = inout(reg) low,
                    high_tmp = inout(reg) high_tmp,
                    low_tmp = inout(reg) low_tmp,

                    ctr0 = inout(vreg) ctr0,
                    ctr1 = inout(vreg) ctr1,
                    ctr2 = inout(vreg) ctr2
                );

                tmp0 = aarch64::veorq_u8(block0, tmp0);
                tmp1 = aarch64::veorq_u8(block1, tmp1);
                tmp2 = aarch64::veorq_u8(block2, tmp2);

                aarch64::vst1q_u8(cur_dst, tmp0);
                cur_dst = cur_dst.add(aes::BLOCK_SIZE);
                aarch64::vst1q_u8(cur_dst, tmp1);
                cur_dst = cur_dst.add(aes::BLOCK_SIZE);
                aarch64::vst1q_u8(cur_dst, tmp2);
                cur_dst = cur_dst.add(aes::BLOCK_SIZE);

                cur_count -= 3;
            }
        }

        while cur_count >= 1 {
            let block0 = aarch64::vld1q_u8(cur_src);
            cur_src = cur_src.add(aes::BLOCK_SIZE);

            let mut tmp0 = ctr0;

            asm!(
                "aese {tmp0:v}.16b, {round_key_0:v}.16b",
                "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                "mov {high}, {ctr0:v}.d[0]",

                "aese {tmp0:v}.16b, {round_key_1:v}.16b",
                "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                "mov {low}, {ctr0:v}.d[1]",

                "aese {tmp0:v}.16b, {round_key_2:v}.16b",
                "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                "rev {high}, {high}",

                "aese {tmp0:v}.16b, {round_key_3:v}.16b",
                "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                "rev {low}, {low}",

                "aese {tmp0:v}.16b, {round_key_4:v}.16b",
                "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                "adds {low}, {low}, 1",

                "aese {tmp0:v}.16b, {round_key_5:v}.16b",
                "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                "cinc {high}, {high}, cs",

                "aese {tmp0:v}.16b, {round_key_6:v}.16b",
                "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                "rev {high}, {high}",

                "aese {tmp0:v}.16b, {round_key_7:v}.16b",
                "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                "rev {low}, {low}",

                "aese {tmp0:v}.16b, {round_key_8:v}.16b",
                "aesmc {tmp0:v}.16b, {tmp0:v}.16b",

                "mov {ctr0:v}.d[0], {high}",

                "aese {tmp0:v}.16b, {round_key_9:v}.16b",

                "mov {ctr0:v}.d[1], {low}",
        
                "eor {tmp0:v}.16b, {tmp0:v}.16b, {round_key_10:v}.16b",

                round_key_0 = in(vreg) round_key_0,
                round_key_1 = in(vreg) round_key_1,
                round_key_2 = in(vreg) round_key_2,
                round_key_3 = in(vreg) round_key_3,
                round_key_4 = in(vreg) round_key_4,
                round_key_5 = in(vreg) round_key_5,
                round_key_6 = in(vreg) round_key_6,
                round_key_7 = in(vreg) round_key_7,
                round_key_8 = in(vreg) round_key_8,
                round_key_9 = in(vreg) round_key_9,
                round_key_10 = in(vreg) round_key_10,

                tmp0 = inout(vreg) tmp0,

                high = inout(reg) high,
                low = inout(reg) low,

                ctr0 = inout(vreg) ctr0
            );
            
            tmp0 = aarch64::veorq_u8(block0, tmp0);

            aarch64::vst1q_u8(cur_dst, tmp0);
            cur_dst = cur_dst.add(aes::BLOCK_SIZE);

            cur_count -= 1;
        }

        aarch64::vst1q_u8(self.ctr.as_mut_ptr(), ctr0);
    }

    pub fn crypt(&mut self, src: &[u8], dst: &mut [u8]) -> Result<()> {
        let mut cur_src = src.as_ptr();
        let mut cur_dst = dst.as_mut_ptr();

        result_return_unless!(src.len() == dst.len(), rc::ResultInvalidSize);
        let mut cur_size = src.len();

        if self.buffer_offset > 0 {
            let needed = aes::BLOCK_SIZE - self.buffer_offset;
            let copyable = cur_size.min(needed);

            unsafe {
                for i in 0..copyable {
                    *cur_dst.add(i) = *cur_src.add(i) ^ self.enc_ctr_buffer[self.buffer_offset + i];
                }

                cur_dst = cur_dst.add(copyable);
                cur_src = cur_src.add(copyable);
                self.buffer_offset += copyable;
                cur_size -= copyable;

                if self.buffer_offset == aes::BLOCK_SIZE {
                    self.buffer_offset = 0;
                }
            }
        }

        unsafe {
            if cur_size >= aes::BLOCK_SIZE {
                let block_count = cur_size / aes::BLOCK_SIZE;
                self.crypt_blocks(cur_src, cur_dst, block_count);

                let blocks_size = block_count * aes::BLOCK_SIZE;
                cur_size -= blocks_size;
                cur_src = cur_src.add(blocks_size);
                cur_dst = cur_dst.add(blocks_size);
            }

            if cur_size > 0 {
                ptr::copy(cur_src, self.enc_ctr_buffer.as_mut_ptr(), cur_size);
                ptr::write_bytes(self.enc_ctr_buffer.as_mut_ptr().add(cur_size), 0, aes::BLOCK_SIZE - cur_size);

                let enc_ctr_buf_ptr = self.enc_ctr_buffer.as_ptr();
                self.crypt_blocks(enc_ctr_buf_ptr, enc_ctr_buf_ptr as *mut u8, 1);

                ptr::copy(self.enc_ctr_buffer.as_ptr(), cur_dst, cur_size);
                self.buffer_offset = cur_size;
            }
        }

        Ok(())
    }
}