12 #include <botan/aes.h>
13 #include <botan/cpuid.h>
27 inline Altivec64x2
load_key(
const uint32_t key[])
29 Altivec32x4 vec = vec_vsx_ld(0, key);
33 const Altivec8x16 mask = {12,13,14,15, 8,9,10,11, 4,5,6,7, 0,1,2,3};
34 const Altivec8x16 zero = {0};
35 return (Altivec64x2)vec_perm((Altivec8x16)vec, zero, mask);
39 return (Altivec64x2)vec;
43 inline Altivec8x16 reverse_vec(Altivec8x16 src)
47 const Altivec8x16 mask = {15,14,13,12, 11,10,9,8, 7,6,5,4, 3,2,1,0};
48 const Altivec8x16 zero = {0};
49 return vec_perm(src, zero, mask);
57 inline Altivec64x2 load_block(
const uint8_t src[])
59 return (Altivec64x2)reverse_vec(vec_vsx_ld(0, src));
62 inline void store_block(Altivec64x2 src, uint8_t dest[])
64 vec_vsx_st(reverse_vec((Altivec8x16)src), 0, dest);
67 inline void store_blocks(Altivec64x2 B0, Altivec64x2 B1,
68 Altivec64x2 B2, Altivec64x2 B3,
72 store_block(B1, out+16);
73 store_block(B2, out+16*2);
74 store_block(B3, out+16*3);
77 #define AES_XOR_4(B0, B1, B2, B3, K) do { \
78 B0 = vec_xor(B0, K); \
79 B1 = vec_xor(B1, K); \
80 B2 = vec_xor(B2, K); \
81 B3 = vec_xor(B3, K); \
84 #define AES_ENCRYPT_4(B0, B1, B2, B3, K) do { \
85 B0 = __builtin_crypto_vcipher(B0, K); \
86 B1 = __builtin_crypto_vcipher(B1, K); \
87 B2 = __builtin_crypto_vcipher(B2, K); \
88 B3 = __builtin_crypto_vcipher(B3, K); \
91 #define AES_ENCRYPT_4_LAST(B0, B1, B2, B3, K) do { \
92 B0 = __builtin_crypto_vcipherlast(B0, K); \
93 B1 = __builtin_crypto_vcipherlast(B1, K); \
94 B2 = __builtin_crypto_vcipherlast(B2, K); \
95 B3 = __builtin_crypto_vcipherlast(B3, K); \
98 #define AES_DECRYPT_4(B0, B1, B2, B3, K) do { \
99 B0 = __builtin_crypto_vncipher(B0, K); \
100 B1 = __builtin_crypto_vncipher(B1, K); \
101 B2 = __builtin_crypto_vncipher(B2, K); \
102 B3 = __builtin_crypto_vncipher(B3, K); \
105 #define AES_DECRYPT_4_LAST(B0, B1, B2, B3, K) do { \
106 B0 = __builtin_crypto_vncipherlast(B0, K); \
107 B1 = __builtin_crypto_vncipherlast(B1, K); \
108 B2 = __builtin_crypto_vncipherlast(B2, K); \
109 B3 = __builtin_crypto_vncipherlast(B3, K); \
115 void AES_128::power8_encrypt_n(const uint8_t in[], uint8_t out[],
size_t blocks)
const
117 const Altivec64x2 K0 =
load_key(&m_EK[0]);
118 const Altivec64x2 K1 =
load_key(&m_EK[4]);
119 const Altivec64x2 K2 =
load_key(&m_EK[8]);
120 const Altivec64x2 K3 =
load_key(&m_EK[12]);
121 const Altivec64x2 K4 =
load_key(&m_EK[16]);
122 const Altivec64x2 K5 =
load_key(&m_EK[20]);
123 const Altivec64x2 K6 =
load_key(&m_EK[24]);
124 const Altivec64x2 K7 =
load_key(&m_EK[28]);
125 const Altivec64x2 K8 =
load_key(&m_EK[32]);
126 const Altivec64x2 K9 =
load_key(&m_EK[36]);
127 const Altivec64x2 K10 = load_block(m_ME.data());
131 Altivec64x2 B0 = load_block(in);
132 Altivec64x2 B1 = load_block(in+16);
133 Altivec64x2 B2 = load_block(in+16*2);
134 Altivec64x2 B3 = load_block(in+16*3);
148 store_blocks(B0, B1, B2, B3, out);
155 for(
size_t i = 0; i != blocks; ++i)
157 Altivec64x2 B = load_block(in);
160 B = __builtin_crypto_vcipher(B, K1);
161 B = __builtin_crypto_vcipher(B, K2);
162 B = __builtin_crypto_vcipher(B, K3);
163 B = __builtin_crypto_vcipher(B, K4);
164 B = __builtin_crypto_vcipher(B, K5);
165 B = __builtin_crypto_vcipher(B, K6);
166 B = __builtin_crypto_vcipher(B, K7);
167 B = __builtin_crypto_vcipher(B, K8);
168 B = __builtin_crypto_vcipher(B, K9);
169 B = __builtin_crypto_vcipherlast(B, K10);
179 void AES_128::power8_decrypt_n(const uint8_t in[], uint8_t out[],
size_t blocks)
const
181 const Altivec64x2 K0 = load_block(m_ME.data());
182 const Altivec64x2 K1 =
load_key(&m_EK[36]);
183 const Altivec64x2 K2 =
load_key(&m_EK[32]);
184 const Altivec64x2 K3 =
load_key(&m_EK[28]);
185 const Altivec64x2 K4 =
load_key(&m_EK[24]);
186 const Altivec64x2 K5 =
load_key(&m_EK[20]);
187 const Altivec64x2 K6 =
load_key(&m_EK[16]);
188 const Altivec64x2 K7 =
load_key(&m_EK[12]);
189 const Altivec64x2 K8 =
load_key(&m_EK[8]);
190 const Altivec64x2 K9 =
load_key(&m_EK[4]);
191 const Altivec64x2 K10 =
load_key(&m_EK[0]);
195 Altivec64x2 B0 = load_block(in);
196 Altivec64x2 B1 = load_block(in+16);
197 Altivec64x2 B2 = load_block(in+16*2);
198 Altivec64x2 B3 = load_block(in+16*3);
212 store_blocks(B0, B1, B2, B3, out);
219 for(
size_t i = 0; i != blocks; ++i)
221 Altivec64x2 B = load_block(in);
224 B = __builtin_crypto_vncipher(B, K1);
225 B = __builtin_crypto_vncipher(B, K2);
226 B = __builtin_crypto_vncipher(B, K3);
227 B = __builtin_crypto_vncipher(B, K4);
228 B = __builtin_crypto_vncipher(B, K5);
229 B = __builtin_crypto_vncipher(B, K6);
230 B = __builtin_crypto_vncipher(B, K7);
231 B = __builtin_crypto_vncipher(B, K8);
232 B = __builtin_crypto_vncipher(B, K9);
233 B = __builtin_crypto_vncipherlast(B, K10);
243 void AES_192::power8_encrypt_n(const uint8_t in[], uint8_t out[],
size_t blocks)
const
245 const Altivec64x2 K0 =
load_key(&m_EK[0]);
246 const Altivec64x2 K1 =
load_key(&m_EK[4]);
247 const Altivec64x2 K2 =
load_key(&m_EK[8]);
248 const Altivec64x2 K3 =
load_key(&m_EK[12]);
249 const Altivec64x2 K4 =
load_key(&m_EK[16]);
250 const Altivec64x2 K5 =
load_key(&m_EK[20]);
251 const Altivec64x2 K6 =
load_key(&m_EK[24]);
252 const Altivec64x2 K7 =
load_key(&m_EK[28]);
253 const Altivec64x2 K8 =
load_key(&m_EK[32]);
254 const Altivec64x2 K9 =
load_key(&m_EK[36]);
255 const Altivec64x2 K10 =
load_key(&m_EK[40]);
256 const Altivec64x2 K11 =
load_key(&m_EK[44]);
257 const Altivec64x2 K12 = load_block(m_ME.data());
261 Altivec64x2 B0 = load_block(in);
262 Altivec64x2 B1 = load_block(in+16);
263 Altivec64x2 B2 = load_block(in+16*2);
264 Altivec64x2 B3 = load_block(in+16*3);
280 store_blocks(B0, B1, B2, B3, out);
287 for(
size_t i = 0; i != blocks; ++i)
289 Altivec64x2 B = load_block(in);
292 B = __builtin_crypto_vcipher(B, K1);
293 B = __builtin_crypto_vcipher(B, K2);
294 B = __builtin_crypto_vcipher(B, K3);
295 B = __builtin_crypto_vcipher(B, K4);
296 B = __builtin_crypto_vcipher(B, K5);
297 B = __builtin_crypto_vcipher(B, K6);
298 B = __builtin_crypto_vcipher(B, K7);
299 B = __builtin_crypto_vcipher(B, K8);
300 B = __builtin_crypto_vcipher(B, K9);
301 B = __builtin_crypto_vcipher(B, K10);
302 B = __builtin_crypto_vcipher(B, K11);
303 B = __builtin_crypto_vcipherlast(B, K12);
313 void AES_192::power8_decrypt_n(const uint8_t in[], uint8_t out[],
size_t blocks)
const
315 const Altivec64x2 K0 = load_block(m_ME.data());
316 const Altivec64x2 K1 =
load_key(&m_EK[44]);
317 const Altivec64x2 K2 =
load_key(&m_EK[40]);
318 const Altivec64x2 K3 =
load_key(&m_EK[36]);
319 const Altivec64x2 K4 =
load_key(&m_EK[32]);
320 const Altivec64x2 K5 =
load_key(&m_EK[28]);
321 const Altivec64x2 K6 =
load_key(&m_EK[24]);
322 const Altivec64x2 K7 =
load_key(&m_EK[20]);
323 const Altivec64x2 K8 =
load_key(&m_EK[16]);
324 const Altivec64x2 K9 =
load_key(&m_EK[12]);
325 const Altivec64x2 K10 =
load_key(&m_EK[8]);
326 const Altivec64x2 K11 =
load_key(&m_EK[4]);
327 const Altivec64x2 K12 =
load_key(&m_EK[0]);
331 Altivec64x2 B0 = load_block(in);
332 Altivec64x2 B1 = load_block(in+16);
333 Altivec64x2 B2 = load_block(in+16*2);
334 Altivec64x2 B3 = load_block(in+16*3);
350 store_blocks(B0, B1, B2, B3, out);
357 for(
size_t i = 0; i != blocks; ++i)
359 Altivec64x2 B = load_block(in);
362 B = __builtin_crypto_vncipher(B, K1);
363 B = __builtin_crypto_vncipher(B, K2);
364 B = __builtin_crypto_vncipher(B, K3);
365 B = __builtin_crypto_vncipher(B, K4);
366 B = __builtin_crypto_vncipher(B, K5);
367 B = __builtin_crypto_vncipher(B, K6);
368 B = __builtin_crypto_vncipher(B, K7);
369 B = __builtin_crypto_vncipher(B, K8);
370 B = __builtin_crypto_vncipher(B, K9);
371 B = __builtin_crypto_vncipher(B, K10);
372 B = __builtin_crypto_vncipher(B, K11);
373 B = __builtin_crypto_vncipherlast(B, K12);
383 void AES_256::power8_encrypt_n(const uint8_t in[], uint8_t out[],
size_t blocks)
const
385 const Altivec64x2 K0 =
load_key(&m_EK[0]);
386 const Altivec64x2 K1 =
load_key(&m_EK[4]);
387 const Altivec64x2 K2 =
load_key(&m_EK[8]);
388 const Altivec64x2 K3 =
load_key(&m_EK[12]);
389 const Altivec64x2 K4 =
load_key(&m_EK[16]);
390 const Altivec64x2 K5 =
load_key(&m_EK[20]);
391 const Altivec64x2 K6 =
load_key(&m_EK[24]);
392 const Altivec64x2 K7 =
load_key(&m_EK[28]);
393 const Altivec64x2 K8 =
load_key(&m_EK[32]);
394 const Altivec64x2 K9 =
load_key(&m_EK[36]);
395 const Altivec64x2 K10 =
load_key(&m_EK[40]);
396 const Altivec64x2 K11 =
load_key(&m_EK[44]);
397 const Altivec64x2 K12 =
load_key(&m_EK[48]);
398 const Altivec64x2 K13 =
load_key(&m_EK[52]);
399 const Altivec64x2 K14 = load_block(m_ME.data());
403 Altivec64x2 B0 = load_block(in);
404 Altivec64x2 B1 = load_block(in+16);
405 Altivec64x2 B2 = load_block(in+16*2);
406 Altivec64x2 B3 = load_block(in+16*3);
424 store_blocks(B0, B1, B2, B3, out);
431 for(
size_t i = 0; i != blocks; ++i)
433 Altivec64x2 B = load_block(in);
436 B = __builtin_crypto_vcipher(B, K1);
437 B = __builtin_crypto_vcipher(B, K2);
438 B = __builtin_crypto_vcipher(B, K3);
439 B = __builtin_crypto_vcipher(B, K4);
440 B = __builtin_crypto_vcipher(B, K5);
441 B = __builtin_crypto_vcipher(B, K6);
442 B = __builtin_crypto_vcipher(B, K7);
443 B = __builtin_crypto_vcipher(B, K8);
444 B = __builtin_crypto_vcipher(B, K9);
445 B = __builtin_crypto_vcipher(B, K10);
446 B = __builtin_crypto_vcipher(B, K11);
447 B = __builtin_crypto_vcipher(B, K12);
448 B = __builtin_crypto_vcipher(B, K13);
449 B = __builtin_crypto_vcipherlast(B, K14);
459 void AES_256::power8_decrypt_n(const uint8_t in[], uint8_t out[],
size_t blocks)
const
461 const Altivec64x2 K0 = load_block(m_ME.data());
462 const Altivec64x2 K1 =
load_key(&m_EK[52]);
463 const Altivec64x2 K2 =
load_key(&m_EK[48]);
464 const Altivec64x2 K3 =
load_key(&m_EK[44]);
465 const Altivec64x2 K4 =
load_key(&m_EK[40]);
466 const Altivec64x2 K5 =
load_key(&m_EK[36]);
467 const Altivec64x2 K6 =
load_key(&m_EK[32]);
468 const Altivec64x2 K7 =
load_key(&m_EK[28]);
469 const Altivec64x2 K8 =
load_key(&m_EK[24]);
470 const Altivec64x2 K9 =
load_key(&m_EK[20]);
471 const Altivec64x2 K10 =
load_key(&m_EK[16]);
472 const Altivec64x2 K11 =
load_key(&m_EK[12]);
473 const Altivec64x2 K12 =
load_key(&m_EK[8]);
474 const Altivec64x2 K13 =
load_key(&m_EK[4]);
475 const Altivec64x2 K14 =
load_key(&m_EK[0]);
479 Altivec64x2 B0 = load_block(in);
480 Altivec64x2 B1 = load_block(in+16);
481 Altivec64x2 B2 = load_block(in+16*2);
482 Altivec64x2 B3 = load_block(in+16*3);
500 store_blocks(B0, B1, B2, B3, out);
507 for(
size_t i = 0; i != blocks; ++i)
509 Altivec64x2 B = load_block(in);
512 B = __builtin_crypto_vncipher(B, K1);
513 B = __builtin_crypto_vncipher(B, K2);
514 B = __builtin_crypto_vncipher(B, K3);
515 B = __builtin_crypto_vncipher(B, K4);
516 B = __builtin_crypto_vncipher(B, K5);
517 B = __builtin_crypto_vncipher(B, K6);
518 B = __builtin_crypto_vncipher(B, K7);
519 B = __builtin_crypto_vncipher(B, K8);
520 B = __builtin_crypto_vncipher(B, K9);
521 B = __builtin_crypto_vncipher(B, K10);
522 B = __builtin_crypto_vncipher(B, K11);
523 B = __builtin_crypto_vncipher(B, K12);
524 B = __builtin_crypto_vncipher(B, K13);
525 B = __builtin_crypto_vncipherlast(B, K14);
536 #undef AES_ENCRYPT_4_LAST
538 #undef AES_DECRYPT_4_LAST
__vector unsigned char Altivec8x16
#define BOTAN_FUNC_ISA(isa)
#define AES_XOR_4(B0, B1, B2, B3, K)
__vector unsigned int Altivec32x4
static bool is_little_endian()
#define AES_ENCRYPT_4_LAST(B0, B1, B2, B3, K)
#define AES_ENCRYPT_4(B0, B1, B2, B3, K)
std::unique_ptr< Private_Key > load_key(DataSource &source, std::function< std::string()> get_pass)
__vector unsigned long long Altivec64x2
#define AES_DECRYPT_4(B0, B1, B2, B3, K)
#define AES_DECRYPT_4_LAST(B0, B1, B2, B3, K)