-
Notifications
You must be signed in to change notification settings - Fork 4
/
aes_ni.c
349 lines (339 loc) · 12.2 KB
/
aes_ni.c
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
#include "aes_ni.h"
inline __m128i AES_128_ASSIST(__m128i temp1, __m128i temp2)
{
__m128i temp3;
temp2 = _mm_shuffle_epi32(temp2, 0xff);
temp3 = _mm_slli_si128(temp1, 0x4);
temp1 = _mm_xor_si128(temp1, temp3);
temp3 = _mm_slli_si128(temp3, 0x4);
temp1 = _mm_xor_si128(temp1, temp3);
temp3 = _mm_slli_si128(temp3, 0x4);
temp1 = _mm_xor_si128(temp1, temp3);
temp1 = _mm_xor_si128(temp1, temp2);
return temp1;
}
void AES_128_Key_Expansion(const unsigned char *userkey, unsigned char *key)
{
__m128i temp1, temp2;
__m128i *Key_Schedule = (__m128i *)key;
temp1 = _mm_loadu_si128((__m128i *)userkey);
Key_Schedule[0] = temp1;
temp2 = _mm_aeskeygenassist_si128(temp1, 0x1);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[1] = temp1;
temp2 = _mm_aeskeygenassist_si128(temp1, 0x2);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[2] = temp1;
temp2 = _mm_aeskeygenassist_si128(temp1, 0x4);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[3] = temp1;
temp2 = _mm_aeskeygenassist_si128(temp1, 0x8);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[4] = temp1;
temp2 = _mm_aeskeygenassist_si128(temp1, 0x10);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[5] = temp1;
temp2 = _mm_aeskeygenassist_si128(temp1, 0x20);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[6] = temp1;
temp2 = _mm_aeskeygenassist_si128(temp1, 0x40);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[7] = temp1;
temp2 = _mm_aeskeygenassist_si128(temp1, 0x80);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[8] = temp1;
temp2 = _mm_aeskeygenassist_si128(temp1, 0x1b);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[9] = temp1;
temp2 = _mm_aeskeygenassist_si128(temp1, 0x36);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[10] = temp1;
}
inline void KEY_192_ASSIST(__m128i *temp1, __m128i *temp2, __m128i *temp3)
{
__m128i temp4;
*temp2 = _mm_shuffle_epi32(*temp2, 0x55);
temp4 = _mm_slli_si128(*temp1, 0x4);
*temp1 = _mm_xor_si128(*temp1, temp4);
temp4 = _mm_slli_si128(temp4, 0x4);
*temp1 = _mm_xor_si128(*temp1, temp4);
temp4 = _mm_slli_si128(temp4, 0x4);
*temp1 = _mm_xor_si128(*temp1, temp4);
*temp1 = _mm_xor_si128(*temp1, *temp2);
*temp2 = _mm_shuffle_epi32(*temp1, 0xff);
temp4 = _mm_slli_si128(*temp3, 0x4);
*temp3 = _mm_xor_si128(*temp3, temp4);
*temp3 = _mm_xor_si128(*temp3, *temp2);
}
void AES_192_Key_Expansion(const unsigned char *userkey, unsigned char *key)
{
__m128i temp1, temp2, temp3, temp4;
__m128i *Key_Schedule = (__m128i *)key;
temp1 = _mm_loadu_si128((__m128i *)userkey);
temp3 = _mm_loadu_si128((__m128i *)(userkey + 16));
Key_Schedule[0] = temp1;
Key_Schedule[1] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3, 0x1);
KEY_192_ASSIST(&temp1, &temp2, &temp3);
Key_Schedule[1] = (__m128i)_mm_shuffle_pd((__m128d)Key_Schedule[1],
(__m128d)temp1, 0);
Key_Schedule[2] = (__m128i)_mm_shuffle_pd((__m128d)temp1, (__m128d)temp3, 1);
temp2 = _mm_aeskeygenassist_si128(temp3, 0x2);
KEY_192_ASSIST(&temp1, &temp2, &temp3);
Key_Schedule[3] = temp1;
Key_Schedule[4] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3, 0x4);
KEY_192_ASSIST(&temp1, &temp2, &temp3);
Key_Schedule[4] = (__m128i)_mm_shuffle_pd((__m128d)Key_Schedule[4],
(__m128d)temp1, 0);
Key_Schedule[5] = (__m128i)_mm_shuffle_pd((__m128d)temp1, (__m128d)temp3, 1);
temp2 = _mm_aeskeygenassist_si128(temp3, 0x8);
KEY_192_ASSIST(&temp1, &temp2, &temp3);
Key_Schedule[6] = temp1;
Key_Schedule[7] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3, 0x10);
KEY_192_ASSIST(&temp1, &temp2, &temp3);
Key_Schedule[7] = (__m128i)_mm_shuffle_pd((__m128d)Key_Schedule[7],
(__m128d)temp1, 0);
Key_Schedule[8] = (__m128i)_mm_shuffle_pd((__m128d)temp1, (__m128d)temp3, 1);
temp2 = _mm_aeskeygenassist_si128(temp3, 0x20);
KEY_192_ASSIST(&temp1, &temp2, &temp3);
Key_Schedule[9] = temp1;
Key_Schedule[10] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3, 0x40);
KEY_192_ASSIST(&temp1, &temp2, &temp3);
Key_Schedule[10] = (__m128i)_mm_shuffle_pd((__m128d)Key_Schedule[10],
(__m128d)temp1, 0);
Key_Schedule[11] = (__m128i)_mm_shuffle_pd((__m128d)temp1, (__m128d)temp3, 1);
temp2 = _mm_aeskeygenassist_si128(temp3, 0x80);
KEY_192_ASSIST(&temp1, &temp2, &temp3);
Key_Schedule[12] = temp1;
}
inline void KEY_256_ASSIST_1(__m128i *temp1, __m128i *temp2)
{
__m128i temp4;
*temp2 = _mm_shuffle_epi32(*temp2, 0xff);
temp4 = _mm_slli_si128(*temp1, 0x4);
*temp1 = _mm_xor_si128(*temp1, temp4);
temp4 = _mm_slli_si128(temp4, 0x4);
*temp1 = _mm_xor_si128(*temp1, temp4);
temp4 = _mm_slli_si128(temp4, 0x4);
*temp1 = _mm_xor_si128(*temp1, temp4);
*temp1 = _mm_xor_si128(*temp1, *temp2);
}
inline void KEY_256_ASSIST_2(__m128i *temp1, __m128i *temp3)
{
__m128i temp2, temp4;
temp4 = _mm_aeskeygenassist_si128(*temp1, 0x0);
temp2 = _mm_shuffle_epi32(temp4, 0xaa);
temp4 = _mm_slli_si128(*temp3, 0x4);
*temp3 = _mm_xor_si128(*temp3, temp4);
temp4 = _mm_slli_si128(temp4, 0x4);
*temp3 = _mm_xor_si128(*temp3, temp4);
temp4 = _mm_slli_si128(temp4, 0x4);
*temp3 = _mm_xor_si128(*temp3, temp4);
*temp3 = _mm_xor_si128(*temp3, temp2);
}
void AES_256_Key_Expansion(const unsigned char *userkey, unsigned char *key)
{
__m128i temp1, temp2, temp3;
__m128i *Key_Schedule = (__m128i *)key;
temp1 = _mm_loadu_si128((__m128i *)userkey);
temp3 = _mm_loadu_si128((__m128i *)(userkey + 16));
Key_Schedule[0] = temp1;
Key_Schedule[1] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3, 0x01);
KEY_256_ASSIST_1(&temp1, &temp2);
Key_Schedule[2] = temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
Key_Schedule[3] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3, 0x02);
KEY_256_ASSIST_1(&temp1, &temp2);
Key_Schedule[4] = temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
Key_Schedule[5] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3, 0x04);
KEY_256_ASSIST_1(&temp1, &temp2);
Key_Schedule[6] = temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
Key_Schedule[7] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3, 0x08);
KEY_256_ASSIST_1(&temp1, &temp2);
Key_Schedule[8] = temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
Key_Schedule[9] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3, 0x10);
KEY_256_ASSIST_1(&temp1, &temp2);
Key_Schedule[10] = temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
Key_Schedule[11] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3, 0x20);
KEY_256_ASSIST_1(&temp1, &temp2);
Key_Schedule[12] = temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
Key_Schedule[13] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3, 0x40);
KEY_256_ASSIST_1(&temp1, &temp2);
Key_Schedule[14] = temp1;
}
int AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key)
{
if (!userKey || !key)
return -1;
if (bits == 128)
{
AES_128_Key_Expansion(userKey, key->KEY);
key->nr = 10;
return 0;
}
else if (bits == 192)
{
AES_192_Key_Expansion(userKey, key->KEY);
key->nr = 12;
return 0;
}
else if (bits == 256)
{
AES_256_Key_Expansion(userKey, key->KEY);
key->nr = 14;
return 0;
}
return -2;
}
int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key)
{
int i, nr;
;
AES_KEY temp_key;
__m128i *Key_Schedule = (__m128i *)key->KEY;
__m128i *Temp_Key_Schedule = (__m128i *)temp_key.KEY;
if (!userKey || !key)
return -1;
if (AES_set_encrypt_key(userKey, bits, &temp_key) == -2)
return -2;
nr = temp_key.nr;
key->nr = nr;
Key_Schedule[nr] = Temp_Key_Schedule[0];
Key_Schedule[nr - 1] = _mm_aesimc_si128(Temp_Key_Schedule[1]);
Key_Schedule[nr - 2] = _mm_aesimc_si128(Temp_Key_Schedule[2]);
Key_Schedule[nr - 3] = _mm_aesimc_si128(Temp_Key_Schedule[3]);
Key_Schedule[nr - 4] = _mm_aesimc_si128(Temp_Key_Schedule[4]);
Key_Schedule[nr - 5] = _mm_aesimc_si128(Temp_Key_Schedule[5]);
Key_Schedule[nr - 6] = _mm_aesimc_si128(Temp_Key_Schedule[6]);
Key_Schedule[nr - 7] = _mm_aesimc_si128(Temp_Key_Schedule[7]);
Key_Schedule[nr - 8] = _mm_aesimc_si128(Temp_Key_Schedule[8]);
Key_Schedule[nr - 9] = _mm_aesimc_si128(Temp_Key_Schedule[9]);
if (nr > 10)
{
Key_Schedule[nr - 10] = _mm_aesimc_si128(Temp_Key_Schedule[10]);
Key_Schedule[nr - 11] = _mm_aesimc_si128(Temp_Key_Schedule[11]);
}
if (nr > 12)
{
Key_Schedule[nr - 12] = _mm_aesimc_si128(Temp_Key_Schedule[12]);
Key_Schedule[nr - 13] = _mm_aesimc_si128(Temp_Key_Schedule[13]);
}
Key_Schedule[0] = Temp_Key_Schedule[nr];
return 0;
}
void AES_ECB_encrypt(const unsigned char *in, //pointer to the PLAINTEXT
unsigned char *out, //pointer to the CIPHERTEXT buffer
unsigned long length, //text length in bytes
const char *key, //pointer to the expanded key schedule
int number_of_rounds) //number of AES rounds 10,12 or 14
{
__m128i tmp;
int i, j;
if (length % 16)
length = length / 16 + 1;
else
length = length / 16;
for (i = 0; i < length; i++)
{
tmp = _mm_loadu_si128(&((__m128i *)in)[i]);
tmp = _mm_xor_si128(tmp, ((__m128i *)key)[0]);
for (j = 1; j < number_of_rounds; j++)
{
tmp = _mm_aesenc_si128(tmp, ((__m128i *)key)[j]);
}
tmp = _mm_aesenclast_si128(tmp, ((__m128i *)key)[j]);
_mm_storeu_si128(&((__m128i *)out)[i], tmp);
}
}
void AES_ECB_decrypt(const unsigned char *in, //pointer to the CIPHERTEXT
unsigned char *out, //pointer to the DECRYPTED TEXT buffer
unsigned long length, //text length in bytes
const char *key, //pointer to the expanded key schedule
int number_of_rounds) //number of AES rounds 10,12 or 14
{
__m128i tmp;
int i, j;
if (length % 16)
length = length / 16 + 1;
else
length = length / 16;
for (i = 0; i < length; i++)
{
tmp = _mm_loadu_si128(&((__m128i *)in)[i]);
tmp = _mm_xor_si128(tmp, ((__m128i *)key)[0]);
for (j = 1; j < number_of_rounds; j++)
{
tmp = _mm_aesdec_si128(tmp, ((__m128i *)key)[j]);
}
tmp = _mm_aesdeclast_si128(tmp, ((__m128i *)key)[j]);
_mm_storeu_si128(&((__m128i *)out)[i], tmp);
}
}
void AES_CBC_encrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
unsigned char *key,
int number_of_rounds)
{
__m128i feedback, data;
int i, j;
if (length % 16)
length = length / 16 + 1;
else
length /= 16;
feedback = _mm_loadu_si128((__m128i *)ivec);
for (i = 0; i < length; i++)
{
data = _mm_loadu_si128(&((__m128i *)in)[i]);
feedback = _mm_xor_si128(data, feedback);
feedback = _mm_xor_si128(feedback, ((__m128i *)key)[0]);
for (j = 1; j < number_of_rounds; j++)
feedback = _mm_aesenc_si128(feedback, ((__m128i *)key)[j]);
feedback = _mm_aesenclast_si128(feedback, ((__m128i *)key)[j]);
_mm_storeu_si128(&((__m128i *)out)[i], feedback);
}
}
void AES_CBC_decrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
unsigned char *key,
int number_of_rounds)
{
__m128i data, feedback, last_in;
int i, j;
if (length % 16)
length = length / 16 + 1;
else
length /= 16;
feedback = _mm_loadu_si128((__m128i *)ivec);
for (i = 0; i < length; i++)
{
last_in = _mm_loadu_si128(&((__m128i *)in)[i]);
data = _mm_xor_si128(last_in, ((__m128i *)key)[0]);
for (j = 1; j < number_of_rounds; j++)
{
data = _mm_aesdec_si128(data, ((__m128i *)key)[j]);
}
data = _mm_aesdeclast_si128(data, ((__m128i *)key)[j]);
data = _mm_xor_si128(data, feedback);
_mm_storeu_si128(&((__m128i *)out)[i], data);
feedback = last_in;
}
}