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
|
/*
* AES using ARMv8
* Contributed by Jeffrey Walton
*
* Further changes
* (C) 2017,2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
/* Modified and adapted for VeraCrypt */
#include "Common/Tcdefs.h"
#include "Aes_hw_cpu.h"
#if !defined(_UEFI)
#include <memory.h>
#include <stdlib.h>
#endif
#include "cpu.h"
#include "misc.h"
#if CRYPTOPP_ARM_AES_AVAILABLE
#include <arm_neon.h>
// Single block encryption operations
VC_INLINE void aes_enc_block(uint8x16_t* B, uint8x16_t K)
{
*B = vaesmcq_u8(vaeseq_u8(*B, K));
}
VC_INLINE void aes_enc_block_last(uint8x16_t* B, uint8x16_t K, uint8x16_t K2)
{
*B = veorq_u8(vaeseq_u8(*B, K), K2);
}
// 4-block parallel encryption operations
VC_INLINE void aes_enc_4_blocks(uint8x16_t* B0, uint8x16_t* B1,
uint8x16_t* B2, uint8x16_t* B3, uint8x16_t K)
{
*B0 = vaesmcq_u8(vaeseq_u8(*B0, K));
*B1 = vaesmcq_u8(vaeseq_u8(*B1, K));
*B2 = vaesmcq_u8(vaeseq_u8(*B2, K));
*B3 = vaesmcq_u8(vaeseq_u8(*B3, K));
}
VC_INLINE void aes_enc_4_blocks_last(uint8x16_t* B0, uint8x16_t* B1,
uint8x16_t* B2, uint8x16_t* B3,
uint8x16_t K, uint8x16_t K2)
{
*B0 = veorq_u8(vaeseq_u8(*B0, K), K2);
*B1 = veorq_u8(vaeseq_u8(*B1, K), K2);
*B2 = veorq_u8(vaeseq_u8(*B2, K), K2);
*B3 = veorq_u8(vaeseq_u8(*B3, K), K2);
}
// Single block decryption operations
VC_INLINE void aes_dec_block(uint8x16_t* B, uint8x16_t K)
{
*B = vaesimcq_u8(vaesdq_u8(*B, K));
}
VC_INLINE void aes_dec_block_last(uint8x16_t* B, uint8x16_t K, uint8x16_t K2)
{
*B = veorq_u8(vaesdq_u8(*B, K), K2);
}
// 4-block parallel decryption operations
VC_INLINE void aes_dec_4_blocks(uint8x16_t* B0, uint8x16_t* B1,
uint8x16_t* B2, uint8x16_t* B3, uint8x16_t K)
{
*B0 = vaesimcq_u8(vaesdq_u8(*B0, K));
*B1 = vaesimcq_u8(vaesdq_u8(*B1, K));
*B2 = vaesimcq_u8(vaesdq_u8(*B2, K));
*B3 = vaesimcq_u8(vaesdq_u8(*B3, K));
}
VC_INLINE void aes_dec_4_blocks_last(uint8x16_t* B0, uint8x16_t* B1,
uint8x16_t* B2, uint8x16_t* B3,
uint8x16_t K, uint8x16_t K2)
{
*B0 = veorq_u8(vaesdq_u8(*B0, K), K2);
*B1 = veorq_u8(vaesdq_u8(*B1, K), K2);
*B2 = veorq_u8(vaesdq_u8(*B2, K), K2);
*B3 = veorq_u8(vaesdq_u8(*B3, K), K2);
}
VC_INLINE void aes256_hw_encrypt_blocks(uint8 buffer[], size_t blocks, const uint8* ks)
{
const uint8x16_t K0 = vld1q_u8(ks + 0 * 16);
const uint8x16_t K1 = vld1q_u8(ks + 1 * 16);
const uint8x16_t K2 = vld1q_u8(ks + 2 * 16);
const uint8x16_t K3 = vld1q_u8(ks + 3 * 16);
const uint8x16_t K4 = vld1q_u8(ks + 4 * 16);
const uint8x16_t K5 = vld1q_u8(ks + 5 * 16);
const uint8x16_t K6 = vld1q_u8(ks + 6 * 16);
const uint8x16_t K7 = vld1q_u8(ks + 7 * 16);
const uint8x16_t K8 = vld1q_u8(ks + 8 * 16);
const uint8x16_t K9 = vld1q_u8(ks + 9 * 16);
const uint8x16_t K10 = vld1q_u8(ks + 10 * 16);
const uint8x16_t K11 = vld1q_u8(ks + 11 * 16);
const uint8x16_t K12 = vld1q_u8(ks + 12 * 16);
const uint8x16_t K13 = vld1q_u8(ks + 13 * 16);
const uint8x16_t K14 = vld1q_u8(ks + 14 * 16);
while(blocks >= 4) {
uint8x16_t B0 = vld1q_u8(buffer);
uint8x16_t B1 = vld1q_u8(buffer + 16);
uint8x16_t B2 = vld1q_u8(buffer + 32);
uint8x16_t B3 = vld1q_u8(buffer + 48);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K0);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K1);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K2);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K3);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K4);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K5);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K6);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K7);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K8);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K9);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K10);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K11);
aes_enc_4_blocks(&B0, &B1, &B2, &B3, K12);
aes_enc_4_blocks_last(&B0, &B1, &B2, &B3, K13, K14);
vst1q_u8(buffer, B0);
vst1q_u8(buffer + 16, B1);
vst1q_u8(buffer + 32, B2);
vst1q_u8(buffer + 48, B3);
buffer += 16 * 4;
blocks -= 4;
}
for(size_t i = 0; i != blocks; ++i) {
uint8x16_t B = vld1q_u8(buffer + 16 * i);
aes_enc_block(&B, K0);
aes_enc_block(&B, K1);
aes_enc_block(&B, K2);
aes_enc_block(&B, K3);
aes_enc_block(&B, K4);
aes_enc_block(&B, K5);
aes_enc_block(&B, K6);
aes_enc_block(&B, K7);
aes_enc_block(&B, K8);
aes_enc_block(&B, K9);
aes_enc_block(&B, K10);
aes_enc_block(&B, K11);
aes_enc_block(&B, K12);
aes_enc_block_last(&B, K13, K14);
vst1q_u8(buffer + 16 * i, B);
}
}
VC_INLINE void aes256_hw_encrypt_block(uint8 buffer[], const uint8* ks)
{
const uint8x16_t K0 = vld1q_u8(ks + 0 * 16);
const uint8x16_t K1 = vld1q_u8(ks + 1 * 16);
const uint8x16_t K2 = vld1q_u8(ks + 2 * 16);
const uint8x16_t K3 = vld1q_u8(ks + 3 * 16);
const uint8x16_t K4 = vld1q_u8(ks + 4 * 16);
const uint8x16_t K5 = vld1q_u8(ks + 5 * 16);
const uint8x16_t K6 = vld1q_u8(ks + 6 * 16);
const uint8x16_t K7 = vld1q_u8(ks + 7 * 16);
const uint8x16_t K8 = vld1q_u8(ks + 8 * 16);
const uint8x16_t K9 = vld1q_u8(ks + 9 * 16);
const uint8x16_t K10 = vld1q_u8(ks + 10 * 16);
const uint8x16_t K11 = vld1q_u8(ks + 11 * 16);
const uint8x16_t K12 = vld1q_u8(ks + 12 * 16);
const uint8x16_t K13 = vld1q_u8(ks + 13 * 16);
const uint8x16_t K14 = vld1q_u8(ks + 14 * 16);
uint8x16_t B = vld1q_u8(buffer);
aes_enc_block(&B, K0);
aes_enc_block(&B, K1);
aes_enc_block(&B, K2);
aes_enc_block(&B, K3);
aes_enc_block(&B, K4);
aes_enc_block(&B, K5);
aes_enc_block(&B, K6);
aes_enc_block(&B, K7);
aes_enc_block(&B, K8);
aes_enc_block(&B, K9);
aes_enc_block(&B, K10);
aes_enc_block(&B, K11);
aes_enc_block(&B, K12);
aes_enc_block_last(&B, K13, K14);
vst1q_u8(buffer, B);
}
VC_INLINE void aes256_hw_decrypt_blocks(uint8 buffer[], size_t blocks, const uint8* ks)
{
const uint8x16_t K0 = vld1q_u8(ks + 0 * 16);
const uint8x16_t K1 = vld1q_u8(ks + 1 * 16);
const uint8x16_t K2 = vld1q_u8(ks + 2 * 16);
const uint8x16_t K3 = vld1q_u8(ks + 3 * 16);
const uint8x16_t K4 = vld1q_u8(ks + 4 * 16);
const uint8x16_t K5 = vld1q_u8(ks + 5 * 16);
const uint8x16_t K6 = vld1q_u8(ks + 6 * 16);
const uint8x16_t K7 = vld1q_u8(ks + 7 * 16);
const uint8x16_t K8 = vld1q_u8(ks + 8 * 16);
const uint8x16_t K9 = vld1q_u8(ks + 9 * 16);
const uint8x16_t K10 = vld1q_u8(ks + 10 * 16);
const uint8x16_t K11 = vld1q_u8(ks + 11 * 16);
const uint8x16_t K12 = vld1q_u8(ks + 12 * 16);
const uint8x16_t K13 = vld1q_u8(ks + 13 * 16);
const uint8x16_t K14 = vld1q_u8(ks + 14 * 16);
while(blocks >= 4) {
uint8x16_t B0 = vld1q_u8(buffer);
uint8x16_t B1 = vld1q_u8(buffer + 16);
uint8x16_t B2 = vld1q_u8(buffer + 32);
uint8x16_t B3 = vld1q_u8(buffer + 48);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K0);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K1);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K2);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K3);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K4);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K5);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K6);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K7);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K8);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K9);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K10);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K11);
aes_dec_4_blocks(&B0, &B1, &B2, &B3, K12);
aes_dec_4_blocks_last(&B0, &B1, &B2, &B3, K13, K14);
vst1q_u8(buffer, B0);
vst1q_u8(buffer + 16, B1);
vst1q_u8(buffer + 32, B2);
vst1q_u8(buffer + 48, B3);
buffer += 16 * 4;
blocks -= 4;
}
for(size_t i = 0; i != blocks; ++i) {
uint8x16_t B = vld1q_u8(buffer + 16 * i);
aes_dec_block(&B, K0);
aes_dec_block(&B, K1);
aes_dec_block(&B, K2);
aes_dec_block(&B, K3);
aes_dec_block(&B, K4);
aes_dec_block(&B, K5);
aes_dec_block(&B, K6);
aes_dec_block(&B, K7);
aes_dec_block(&B, K8);
aes_dec_block(&B, K9);
aes_dec_block(&B, K10);
aes_dec_block(&B, K11);
aes_dec_block(&B, K12);
aes_dec_block_last(&B, K13, K14);
vst1q_u8(buffer + 16 * i, B);
}
}
VC_INLINE void aes256_hw_decrypt_block(uint8 buffer[], const uint8* ks)
{
const uint8x16_t K0 = vld1q_u8(ks + 0 * 16);
const uint8x16_t K1 = vld1q_u8(ks + 1 * 16);
const uint8x16_t K2 = vld1q_u8(ks + 2 * 16);
const uint8x16_t K3 = vld1q_u8(ks + 3 * 16);
const uint8x16_t K4 = vld1q_u8(ks + 4 * 16);
const uint8x16_t K5 = vld1q_u8(ks + 5 * 16);
const uint8x16_t K6 = vld1q_u8(ks + 6 * 16);
const uint8x16_t K7 = vld1q_u8(ks + 7 * 16);
const uint8x16_t K8 = vld1q_u8(ks + 8 * 16);
const uint8x16_t K9 = vld1q_u8(ks + 9 * 16);
const uint8x16_t K10 = vld1q_u8(ks + 10 * 16);
const uint8x16_t K11 = vld1q_u8(ks + 11 * 16);
const uint8x16_t K12 = vld1q_u8(ks + 12 * 16);
const uint8x16_t K13 = vld1q_u8(ks + 13 * 16);
const uint8x16_t K14 = vld1q_u8(ks + 14 * 16);
uint8x16_t B = vld1q_u8(buffer);
aes_dec_block(&B, K0);
aes_dec_block(&B, K1);
aes_dec_block(&B, K2);
aes_dec_block(&B, K3);
aes_dec_block(&B, K4);
aes_dec_block(&B, K5);
aes_dec_block(&B, K6);
aes_dec_block(&B, K7);
aes_dec_block(&B, K8);
aes_dec_block(&B, K9);
aes_dec_block(&B, K10);
aes_dec_block(&B, K11);
aes_dec_block(&B, K12);
aes_dec_block_last(&B, K13, K14);
vst1q_u8(buffer, B);
}
void aes_hw_cpu_decrypt (const uint8 *ks, uint8 *data)
{
aes256_hw_decrypt_block(data, ks);
}
void aes_hw_cpu_decrypt_32_blocks (const uint8 *ks, uint8 *data)
{
aes256_hw_decrypt_blocks(data, 32, ks);
}
void aes_hw_cpu_encrypt (const uint8 *ks, uint8 *data)
{
aes256_hw_encrypt_block(data, ks);
}
void aes_hw_cpu_encrypt_32_blocks (const uint8 *ks, uint8 *data)
{
aes256_hw_encrypt_blocks(data, 32, ks);
}
#endif
|