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
|
/*
* AES using POWER8 crypto extensions
*
* Contributed by Jeffrey Walton
*
* Further changes
* (C) 2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/aes.h>
#include <botan/cpuid.h>
#include <altivec.h>
#undef vector
#undef bool
namespace Botan {
namespace {
__vector unsigned long long LoadKey(const uint32_t* src)
{
__vector unsigned int vec = vec_vsx_ld(0, src);
if(CPUID::is_little_endian())
{
const __vector unsigned char mask = {12,13,14,15, 8,9,10,11, 4,5,6,7, 0,1,2,3};
const __vector unsigned char zero = {0};
return (__vector unsigned long long)vec_perm((__vector unsigned char)vec, zero, mask);
}
else
{
return (__vector unsigned long long)vec;
}
}
__vector unsigned char Reverse8x16(const __vector unsigned char src)
{
if(CPUID::is_little_endian())
{
const __vector unsigned char mask = {15,14,13,12, 11,10,9,8, 7,6,5,4, 3,2,1,0};
const __vector unsigned char zero = {0};
return vec_perm(src, zero, mask);
}
else
{
return src;
}
}
__vector unsigned long long LoadBlock(const uint8_t* src)
{
return (__vector unsigned long long)Reverse8x16(vec_vsx_ld(0, src));
}
void StoreBlock(const __vector unsigned long long src, uint8_t* dest)
{
vec_vsx_st(Reverse8x16((__vector unsigned char)src), 0, dest);
}
}
BOTAN_FUNC_ISA("crypto")
void AES_128::power8_encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
{
const __vector unsigned long long K0 = LoadKey(&m_EK[0]);
const __vector unsigned long long K1 = LoadKey(&m_EK[4]);
const __vector unsigned long long K2 = LoadKey(&m_EK[8]);
const __vector unsigned long long K3 = LoadKey(&m_EK[12]);
const __vector unsigned long long K4 = LoadKey(&m_EK[16]);
const __vector unsigned long long K5 = LoadKey(&m_EK[20]);
const __vector unsigned long long K6 = LoadKey(&m_EK[24]);
const __vector unsigned long long K7 = LoadKey(&m_EK[28]);
const __vector unsigned long long K8 = LoadKey(&m_EK[32]);
const __vector unsigned long long K9 = LoadKey(&m_EK[36]);
const __vector unsigned long long K10 = LoadBlock(m_ME.data());
for(size_t i = 0; i != blocks; ++i)
{
__vector unsigned long long B = LoadBlock(in);
B = vec_xor(B, K0);
B = __builtin_crypto_vcipher(B, K1);
B = __builtin_crypto_vcipher(B, K2);
B = __builtin_crypto_vcipher(B, K3);
B = __builtin_crypto_vcipher(B, K4);
B = __builtin_crypto_vcipher(B, K5);
B = __builtin_crypto_vcipher(B, K6);
B = __builtin_crypto_vcipher(B, K7);
B = __builtin_crypto_vcipher(B, K8);
B = __builtin_crypto_vcipher(B, K9);
B = __builtin_crypto_vcipherlast(B, K10);
StoreBlock(B, out);
out += 16;
in += 16;
}
}
BOTAN_FUNC_ISA("crypto")
void AES_128::power8_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
{
const __vector unsigned long long K0 = LoadBlock(m_ME.data());
const __vector unsigned long long K1 = LoadKey(&m_EK[36]);
const __vector unsigned long long K2 = LoadKey(&m_EK[32]);
const __vector unsigned long long K3 = LoadKey(&m_EK[28]);
const __vector unsigned long long K4 = LoadKey(&m_EK[24]);
const __vector unsigned long long K5 = LoadKey(&m_EK[20]);
const __vector unsigned long long K6 = LoadKey(&m_EK[16]);
const __vector unsigned long long K7 = LoadKey(&m_EK[12]);
const __vector unsigned long long K8 = LoadKey(&m_EK[8]);
const __vector unsigned long long K9 = LoadKey(&m_EK[4]);
const __vector unsigned long long K10 = LoadKey(&m_EK[0]);
for(size_t i = 0; i != blocks; ++i)
{
__vector unsigned long long B = LoadBlock(in);
B = vec_xor(B, K0);
B = __builtin_crypto_vncipher(B, K1);
B = __builtin_crypto_vncipher(B, K2);
B = __builtin_crypto_vncipher(B, K3);
B = __builtin_crypto_vncipher(B, K4);
B = __builtin_crypto_vncipher(B, K5);
B = __builtin_crypto_vncipher(B, K6);
B = __builtin_crypto_vncipher(B, K7);
B = __builtin_crypto_vncipher(B, K8);
B = __builtin_crypto_vncipher(B, K9);
B = __builtin_crypto_vncipherlast(B, K10);
StoreBlock(B, out);
out += 16;
in += 16;
}
}
BOTAN_FUNC_ISA("crypto")
void AES_192::power8_encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
{
const __vector unsigned long long K0 = LoadKey(&m_EK[0]);
const __vector unsigned long long K1 = LoadKey(&m_EK[4]);
const __vector unsigned long long K2 = LoadKey(&m_EK[8]);
const __vector unsigned long long K3 = LoadKey(&m_EK[12]);
const __vector unsigned long long K4 = LoadKey(&m_EK[16]);
const __vector unsigned long long K5 = LoadKey(&m_EK[20]);
const __vector unsigned long long K6 = LoadKey(&m_EK[24]);
const __vector unsigned long long K7 = LoadKey(&m_EK[28]);
const __vector unsigned long long K8 = LoadKey(&m_EK[32]);
const __vector unsigned long long K9 = LoadKey(&m_EK[36]);
const __vector unsigned long long K10 = LoadKey(&m_EK[40]);
const __vector unsigned long long K11 = LoadKey(&m_EK[44]);
const __vector unsigned long long K12 = LoadBlock(m_ME.data());
for(size_t i = 0; i != blocks; ++i)
{
__vector unsigned long long B = LoadBlock(in);
B = vec_xor(B, K0);
B = __builtin_crypto_vcipher(B, K1);
B = __builtin_crypto_vcipher(B, K2);
B = __builtin_crypto_vcipher(B, K3);
B = __builtin_crypto_vcipher(B, K4);
B = __builtin_crypto_vcipher(B, K5);
B = __builtin_crypto_vcipher(B, K6);
B = __builtin_crypto_vcipher(B, K7);
B = __builtin_crypto_vcipher(B, K8);
B = __builtin_crypto_vcipher(B, K9);
B = __builtin_crypto_vcipher(B, K10);
B = __builtin_crypto_vcipher(B, K11);
B = __builtin_crypto_vcipherlast(B, K12);
StoreBlock(B, out);
out += 16;
in += 16;
}
}
BOTAN_FUNC_ISA("crypto")
void AES_192::power8_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
{
const __vector unsigned long long K0 = LoadBlock(m_ME.data());
const __vector unsigned long long K1 = LoadKey(&m_EK[44]);
const __vector unsigned long long K2 = LoadKey(&m_EK[40]);
const __vector unsigned long long K3 = LoadKey(&m_EK[36]);
const __vector unsigned long long K4 = LoadKey(&m_EK[32]);
const __vector unsigned long long K5 = LoadKey(&m_EK[28]);
const __vector unsigned long long K6 = LoadKey(&m_EK[24]);
const __vector unsigned long long K7 = LoadKey(&m_EK[20]);
const __vector unsigned long long K8 = LoadKey(&m_EK[16]);
const __vector unsigned long long K9 = LoadKey(&m_EK[12]);
const __vector unsigned long long K10 = LoadKey(&m_EK[8]);
const __vector unsigned long long K11 = LoadKey(&m_EK[4]);
const __vector unsigned long long K12 = LoadKey(&m_EK[0]);
for(size_t i = 0; i != blocks; ++i)
{
__vector unsigned long long B = LoadBlock(in);
B = vec_xor(B, K0);
B = __builtin_crypto_vncipher(B, K1);
B = __builtin_crypto_vncipher(B, K2);
B = __builtin_crypto_vncipher(B, K3);
B = __builtin_crypto_vncipher(B, K4);
B = __builtin_crypto_vncipher(B, K5);
B = __builtin_crypto_vncipher(B, K6);
B = __builtin_crypto_vncipher(B, K7);
B = __builtin_crypto_vncipher(B, K8);
B = __builtin_crypto_vncipher(B, K9);
B = __builtin_crypto_vncipher(B, K10);
B = __builtin_crypto_vncipher(B, K11);
B = __builtin_crypto_vncipherlast(B, K12);
StoreBlock(B, out);
out += 16;
in += 16;
}
}
BOTAN_FUNC_ISA("crypto")
void AES_256::power8_encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
{
const __vector unsigned long long K0 = LoadKey(&m_EK[0]);
const __vector unsigned long long K1 = LoadKey(&m_EK[4]);
const __vector unsigned long long K2 = LoadKey(&m_EK[8]);
const __vector unsigned long long K3 = LoadKey(&m_EK[12]);
const __vector unsigned long long K4 = LoadKey(&m_EK[16]);
const __vector unsigned long long K5 = LoadKey(&m_EK[20]);
const __vector unsigned long long K6 = LoadKey(&m_EK[24]);
const __vector unsigned long long K7 = LoadKey(&m_EK[28]);
const __vector unsigned long long K8 = LoadKey(&m_EK[32]);
const __vector unsigned long long K9 = LoadKey(&m_EK[36]);
const __vector unsigned long long K10 = LoadKey(&m_EK[40]);
const __vector unsigned long long K11 = LoadKey(&m_EK[44]);
const __vector unsigned long long K12 = LoadKey(&m_EK[48]);
const __vector unsigned long long K13 = LoadKey(&m_EK[52]);
const __vector unsigned long long K14 = LoadBlock(m_ME.data());
for(size_t i = 0; i != blocks; ++i)
{
__vector unsigned long long B = LoadBlock(in);
B = vec_xor(B, K0);
B = __builtin_crypto_vcipher(B, K1);
B = __builtin_crypto_vcipher(B, K2);
B = __builtin_crypto_vcipher(B, K3);
B = __builtin_crypto_vcipher(B, K4);
B = __builtin_crypto_vcipher(B, K5);
B = __builtin_crypto_vcipher(B, K6);
B = __builtin_crypto_vcipher(B, K7);
B = __builtin_crypto_vcipher(B, K8);
B = __builtin_crypto_vcipher(B, K9);
B = __builtin_crypto_vcipher(B, K10);
B = __builtin_crypto_vcipher(B, K11);
B = __builtin_crypto_vcipher(B, K12);
B = __builtin_crypto_vcipher(B, K13);
B = __builtin_crypto_vcipherlast(B, K14);
StoreBlock(B, out);
out += 16;
in += 16;
}
}
BOTAN_FUNC_ISA("crypto")
void AES_256::power8_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
{
const __vector unsigned long long K0 = LoadBlock(m_ME.data());
const __vector unsigned long long K1 = LoadKey(&m_EK[52]);
const __vector unsigned long long K2 = LoadKey(&m_EK[48]);
const __vector unsigned long long K3 = LoadKey(&m_EK[44]);
const __vector unsigned long long K4 = LoadKey(&m_EK[40]);
const __vector unsigned long long K5 = LoadKey(&m_EK[36]);
const __vector unsigned long long K6 = LoadKey(&m_EK[32]);
const __vector unsigned long long K7 = LoadKey(&m_EK[28]);
const __vector unsigned long long K8 = LoadKey(&m_EK[24]);
const __vector unsigned long long K9 = LoadKey(&m_EK[20]);
const __vector unsigned long long K10 = LoadKey(&m_EK[16]);
const __vector unsigned long long K11 = LoadKey(&m_EK[12]);
const __vector unsigned long long K12 = LoadKey(&m_EK[8]);
const __vector unsigned long long K13 = LoadKey(&m_EK[4]);
const __vector unsigned long long K14 = LoadKey(&m_EK[0]);
for(size_t i = 0; i != blocks; ++i)
{
__vector unsigned long long B = LoadBlock(in);
B = vec_xor(B, K0);
B = __builtin_crypto_vncipher(B, K1);
B = __builtin_crypto_vncipher(B, K2);
B = __builtin_crypto_vncipher(B, K3);
B = __builtin_crypto_vncipher(B, K4);
B = __builtin_crypto_vncipher(B, K5);
B = __builtin_crypto_vncipher(B, K6);
B = __builtin_crypto_vncipher(B, K7);
B = __builtin_crypto_vncipher(B, K8);
B = __builtin_crypto_vncipher(B, K9);
B = __builtin_crypto_vncipher(B, K10);
B = __builtin_crypto_vncipher(B, K11);
B = __builtin_crypto_vncipher(B, K12);
B = __builtin_crypto_vncipher(B, K13);
B = __builtin_crypto_vncipherlast(B, K14);
StoreBlock(B, out);
out += 16;
in += 16;
}
}
}
|