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
|
/**
* \file
* \author Egor Tensin <Egor.Tensin@gmail.com>
* \date 2015
* \copyright This file is licensed under the terms of the MIT License.
* See LICENSE.txt for details.
*/
#include <aesni/all.h>
#include <emmintrin.h>
#include <wmmintrin.h>
AesBlock128 __fastcall raw_aes256_encrypt(
AesBlock128 plain,
Aes256KeySchedule* key_schedule)
{
plain = _mm_xor_si128(plain, key_schedule->keys[0]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[1]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[2]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[3]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[4]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[5]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[6]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[7]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[8]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[9]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[10]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[11]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[12]);
plain = _mm_aesenc_si128(plain, key_schedule->keys[13]);
return _mm_aesenclast_si128(plain, key_schedule->keys[14]);
}
AesBlock128 __fastcall raw_aes256_decrypt(
AesBlock128 cipher,
Aes256KeySchedule* inverted_schedule)
{
cipher = _mm_xor_si128(cipher, inverted_schedule->keys[0]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[1]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[2]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[3]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[4]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[5]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[6]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[7]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[8]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[9]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[10]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[11]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[12]);
cipher = _mm_aesdec_si128(cipher, inverted_schedule->keys[13]);
return _mm_aesdeclast_si128(cipher, inverted_schedule->keys[14]);
}
static AesBlock128 __fastcall aes256_keygen_assist(
AesBlock128* prev_lo,
AesBlock128* prev_hi,
AesBlock128 hwgen)
{
AesBlock128 tmp = *prev_lo;
tmp = _mm_slli_si128(tmp, 4);
*prev_lo = _mm_xor_si128(*prev_lo, tmp);
tmp = _mm_slli_si128(tmp, 4);
*prev_lo = _mm_xor_si128(*prev_lo, tmp);
tmp = _mm_slli_si128(tmp, 4);
*prev_lo = _mm_xor_si128(*prev_lo, tmp);
*prev_lo = _mm_xor_si128(*prev_lo, hwgen);
*prev_hi = _mm_xor_si128(*prev_hi, *prev_lo);
*prev_lo = _mm_xor_si128(*prev_lo, *prev_hi);
*prev_hi = _mm_xor_si128(*prev_hi, *prev_lo);
return *prev_hi;
}
void __fastcall raw_aes256_expand_key_schedule(
AesBlock128 key_lo,
AesBlock128 key_hi,
Aes256KeySchedule* key_schedule)
{
AesBlock128 prev_lo, prev_hi;
AesBlock128 hwgen;
prev_lo = key_schedule->keys[0] = key_lo;
prev_hi = key_schedule->keys[1] = key_hi;
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0x01);
hwgen = _mm_shuffle_epi32(hwgen, 0xff);
key_schedule->keys[2] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0);
hwgen = _mm_shuffle_epi32(hwgen, 0xaa);
key_schedule->keys[3] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0x02);
hwgen = _mm_shuffle_epi32(hwgen, 0xff);
key_schedule->keys[4] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0);
hwgen = _mm_shuffle_epi32(hwgen, 0xaa);
key_schedule->keys[5] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0x04);
hwgen = _mm_shuffle_epi32(hwgen, 0xff);
key_schedule->keys[6] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0);
hwgen = _mm_shuffle_epi32(hwgen, 0xaa);
key_schedule->keys[7] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0x08);
hwgen = _mm_shuffle_epi32(hwgen, 0xff);
key_schedule->keys[8] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0);
hwgen = _mm_shuffle_epi32(hwgen, 0xaa);
key_schedule->keys[9] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0x10);
hwgen = _mm_shuffle_epi32(hwgen, 0xff);
key_schedule->keys[10] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0);
hwgen = _mm_shuffle_epi32(hwgen, 0xaa);
key_schedule->keys[11] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0x20);
hwgen = _mm_shuffle_epi32(hwgen, 0xff);
key_schedule->keys[12] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0);
hwgen = _mm_shuffle_epi32(hwgen, 0xaa);
key_schedule->keys[13] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
hwgen = _mm_aeskeygenassist_si128(prev_hi, 0x40);
hwgen = _mm_shuffle_epi32(hwgen, 0xff);
key_schedule->keys[14] = aes256_keygen_assist(&prev_lo, &prev_hi, hwgen);
}
void __fastcall raw_aes256_invert_key_schedule(
Aes256KeySchedule* key_schedule,
Aes256KeySchedule* inverted_schedule)
{
inverted_schedule->keys[0] = key_schedule->keys[14];
inverted_schedule->keys[1] = _mm_aesimc_si128(key_schedule->keys[13]);
inverted_schedule->keys[2] = _mm_aesimc_si128(key_schedule->keys[12]);
inverted_schedule->keys[3] = _mm_aesimc_si128(key_schedule->keys[11]);
inverted_schedule->keys[4] = _mm_aesimc_si128(key_schedule->keys[10]);
inverted_schedule->keys[5] = _mm_aesimc_si128(key_schedule->keys[9]);
inverted_schedule->keys[6] = _mm_aesimc_si128(key_schedule->keys[8]);
inverted_schedule->keys[7] = _mm_aesimc_si128(key_schedule->keys[7]);
inverted_schedule->keys[8] = _mm_aesimc_si128(key_schedule->keys[6]);
inverted_schedule->keys[9] = _mm_aesimc_si128(key_schedule->keys[5]);
inverted_schedule->keys[10] = _mm_aesimc_si128(key_schedule->keys[4]);
inverted_schedule->keys[11] = _mm_aesimc_si128(key_schedule->keys[3]);
inverted_schedule->keys[12] = _mm_aesimc_si128(key_schedule->keys[2]);
inverted_schedule->keys[13] = _mm_aesimc_si128(key_schedule->keys[1]);
inverted_schedule->keys[14] = key_schedule->keys[0];
}
|