13#include <botan/internal/aes.h>
15#include <botan/internal/ct_utils.h>
16#include <botan/internal/isa_extn.h>
17#include <botan/internal/simd_4x32.h>
26 if constexpr(std::endian::native == std::endian::little) {
34 if constexpr(std::endian::native == std::endian::little) {
43 SIMD_4x32(0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C),
44 SIMD_4x32(0x0F0A0500, 0x030E0904, 0x07020D08, 0x0B06010C),
45 SIMD_4x32(0x0B020900, 0x0F060D04, 0x030A0108, 0x070E050C),
46 SIMD_4x32(0x070A0D00, 0x0B0E0104, 0x0F020508, 0x0306090C),
49 return shuffle(x, vperm_sr[r]);
54 return lo_nibs_mask & x;
59 return (x.shr<4>() & lo_nibs_mask);
66 return shuffle(k_ipt1, low_nibs(B)) ^ shuffle(k_ipt2, high_nibs(B)) ^ K;
78 const SIMD_4x32 t5 = Bl ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, Bh));
79 const SIMD_4x32 t6 = Bh ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, Bl));
81 return std::make_pair(t5, t6);
88 const SIMD_4x32 mc_forward[4] = {
SIMD_4x32(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D),
89 SIMD_4x32(0x04070605, 0x080B0A09, 0x0C0F0E0D, 0x00030201),
90 SIMD_4x32(0x080B0A09, 0x0C0F0E0D, 0x00030201, 0x04070605),
91 SIMD_4x32(0x0C0F0E0D, 0x00030201, 0x04070605, 0x080B0A09)};
93 SIMD_4x32(0x02010003, 0x06050407, 0x0A09080B, 0x0E0D0C0F),
94 SIMD_4x32(0x0E0D0C0F, 0x02010003, 0x06050407, 0x0A09080B),
95 SIMD_4x32(0x0A09080B, 0x0E0D0C0F, 0x02010003, 0x06050407),
96 SIMD_4x32(0x06050407, 0x0A09080B, 0x0E0D0C0F, 0x02010003),
101 const auto [t5, t6] = aes_decompose_kinv(B);
103 const SIMD_4x32 t7 = masked_shuffle(sb1t, t6) ^ masked_shuffle(sb1u, t5) ^ K;
104 const SIMD_4x32 t8 = masked_shuffle(sb2t, t6) ^ masked_shuffle(sb2u, t5) ^ shuffle(t7, mc_forward[r % 4]);
106 return shuffle(t8, mc_forward[r % 4]) ^ shuffle(t7, mc_backward[r % 4]) ^ t8;
113 const auto [t5, t6] = aes_decompose_kinv(B);
115 return shiftrows(masked_shuffle(sbou, t5) ^ masked_shuffle(sbot, t6) ^ K, r % 4);
122 return shuffle(k_dipt1, low_nibs(B)) ^ shuffle(k_dipt2, high_nibs(B)) ^ K;
127 SIMD_4x32(0x0C0F0E0D, 0x00030201, 0x04070605, 0x080B0A09),
128 SIMD_4x32(0x080B0A09, 0x0C0F0E0D, 0x00030201, 0x04070605),
129 SIMD_4x32(0x04070605, 0x080B0A09, 0x0C0F0E0D, 0x00030201),
130 SIMD_4x32(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D),
142 const auto [t5, t6] = aes_decompose_kinv(B);
146 const SIMD_4x32 t8 = masked_shuffle(sb9t, t6) ^ masked_shuffle(sb9u, t5) ^ K;
147 const SIMD_4x32 t9 = shuffle(t8, mc) ^ masked_shuffle(sbdu, t5) ^ masked_shuffle(sbdt, t6);
148 const SIMD_4x32 t12 = shuffle(t9, mc) ^ masked_shuffle(sbbu, t5) ^ masked_shuffle(sbbt, t6);
149 return shuffle(t12, mc) ^ masked_shuffle(sbeu, t5) ^ masked_shuffle(sbet, t6);
156 const uint32_t which_sr = ((((r - 1) << 4) ^ 48) & 48) / 16;
158 const auto [t5, t6] = aes_decompose_kinv(B);
160 const SIMD_4x32 x = masked_shuffle(sboud, t5) ^ masked_shuffle(sbotd, t6) ^ K;
161 return shiftrows(x, which_sr);
164void BOTAN_FN_ISA_SIMD_4X32
165vperm_encrypt_blocks(
const uint8_t in[], uint8_t out[],
size_t blocks,
const SIMD_4x32 K[],
size_t rounds) {
168 const size_t blocks2 = blocks - (blocks % 2);
170 for(
size_t i = 0; i != blocks2; i += 2) {
174 B0 = aes_enc_first_round(B0, K[0]);
175 B1 = aes_enc_first_round(B1, K[0]);
177 for(
size_t r = 1; r != rounds; ++r) {
178 B0 = aes_enc_round(B0, K[r], r);
179 B1 = aes_enc_round(B1, K[r], r);
182 B0 = aes_enc_last_round(B0, K[rounds], rounds);
183 B1 = aes_enc_last_round(B1, K[rounds], rounds);
185 B0.store_le(out + i * 16);
186 B1.store_le(out + (i + 1) * 16);
189 for(
size_t i = blocks2; i < blocks; ++i) {
192 B = aes_enc_first_round(B, K[0]);
194 for(
size_t r = 1; r != rounds; ++r) {
195 B = aes_enc_round(B, K[r], r);
198 B = aes_enc_last_round(B, K[rounds], rounds);
199 B.store_le(out + i * 16);
206void BOTAN_FN_ISA_SIMD_4X32
207vperm_decrypt_blocks(
const uint8_t in[], uint8_t out[],
size_t blocks,
const SIMD_4x32 K[],
size_t rounds) {
210 const size_t blocks2 = blocks - (blocks % 2);
212 for(
size_t i = 0; i != blocks2; i += 2) {
216 B0 = aes_dec_first_round(B0, K[0]);
217 B1 = aes_dec_first_round(B1, K[0]);
219 for(
size_t r = 1; r != rounds; ++r) {
220 B0 = aes_dec_round(B0, K[r], r);
221 B1 = aes_dec_round(B1, K[r], r);
224 B0 = aes_dec_last_round(B0, K[rounds], rounds);
225 B1 = aes_dec_last_round(B1, K[rounds], rounds);
227 B0.store_le(out + i * 16);
228 B1.store_le(out + (i + 1) * 16);
231 for(
size_t i = blocks2; i < blocks; ++i) {
234 B = aes_dec_first_round(B, K[0]);
236 for(
size_t r = 1; r != rounds; ++r) {
237 B = aes_dec_round(B, K[r], r);
240 B = aes_dec_last_round(B, K[rounds], rounds);
241 B.store_le(out + i * 16);
250void BOTAN_FN_ISA_SIMD_4X32 AES_128::vperm_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
251 const SIMD_4x32 K[11] = {
265 return vperm_encrypt_blocks(in, out, blocks, K, 10);
268void BOTAN_FN_ISA_SIMD_4X32 AES_128::vperm_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
269 const SIMD_4x32 K[11] = {
283 return vperm_decrypt_blocks(in, out, blocks, K, 10);
286void BOTAN_FN_ISA_SIMD_4X32 AES_192::vperm_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
287 const SIMD_4x32 K[13] = {
303 return vperm_encrypt_blocks(in, out, blocks, K, 12);
306void BOTAN_FN_ISA_SIMD_4X32 AES_192::vperm_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
307 const SIMD_4x32 K[13] = {
323 return vperm_decrypt_blocks(in, out, blocks, K, 12);
326void BOTAN_FN_ISA_SIMD_4X32 AES_256::vperm_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
327 const SIMD_4x32 K[15] = {
345 return vperm_encrypt_blocks(in, out, blocks, K, 14);
348void BOTAN_FN_ISA_SIMD_4X32 AES_256::vperm_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
349 const SIMD_4x32 K[15] = {
367 return vperm_decrypt_blocks(in, out, blocks, K, 14);
373 return shuffle(table_1, low_nibs(input)) ^ shuffle(table_2, high_nibs(input));
377 return aes_enc_first_round(input,
SIMD_4x32());
381 const SIMD_4x32 mc_forward0(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D);
385 t = shuffle(t, mc_forward0);
386 t2 = t ^ t2 ^ shuffle(t, mc_forward0);
387 return shiftrows(t2, round_no % 4);
390SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 aes_schedule_mangle_dec(
SIMD_4x32 k, uint8_t round_no) {
391 const SIMD_4x32 mc_forward0(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D);
394 SIMD_4x32(0x7ED9A700, 0xB6116FC8, 0x82255BFC, 0x4AED9334),
395 SIMD_4x32(0x27143300, 0x45765162, 0xE9DAFDCE, 0x8BB89FAC),
396 SIMD_4x32(0xCCA86400, 0x27438FEB, 0xADC90561, 0x4622EE8A),
397 SIMD_4x32(0x4F92DD00, 0x815C13CE, 0xBD602FF2, 0x73AEE13C),
398 SIMD_4x32(0x01C6C700, 0x03C4C502, 0xFA3D3CFB, 0xF83F3EF9),
399 SIMD_4x32(0x38CFF700, 0xEE1921D6, 0x7384BC4B, 0xA5526A9D),
400 SIMD_4x32(0x53732000, 0xE3C390B0, 0x10306343, 0xA080D3F3),
401 SIMD_4x32(0x036982E8, 0xA0CA214B, 0x8CE60D67, 0x2F45AEC4),
404 SIMD_4x32 t = aes_schedule_transform(k, dsk[0], dsk[1]);
405 SIMD_4x32 output = shuffle(t, mc_forward0);
407 t = aes_schedule_transform(t, dsk[2], dsk[3]);
408 output = shuffle(t ^ output, mc_forward0);
410 t = aes_schedule_transform(t, dsk[4], dsk[5]);
411 output = shuffle(t ^ output, mc_forward0);
413 t = aes_schedule_transform(t, dsk[6], dsk[7]);
414 output = shuffle(t ^ output, mc_forward0);
416 return shiftrows(output, round_no % 4);
419SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 aes_schedule_mangle_last(
SIMD_4x32 k, uint8_t round_no) {
420 const SIMD_4x32 out_tr1(0xD6B66000, 0xFF9F4929, 0xDEBE6808, 0xF7974121);
421 const SIMD_4x32 out_tr2(0x50BCEC00, 0x01EDBD51, 0xB05C0CE0, 0xE10D5DB1);
424 return aes_schedule_transform(k, out_tr1, out_tr2);
428 const SIMD_4x32 deskew1(0x47A4E300, 0x07E4A340, 0x5DBEF91A, 0x1DFEB95A);
429 const SIMD_4x32 deskew2(0x83EA6900, 0x5F36B5DC, 0xF49D1E77, 0x2841C2AB);
432 return aes_schedule_transform(k, deskew1, deskew2);
443 const auto [t5, t6] = aes_decompose_kinv(input1);
445 return smeared ^ masked_shuffle(sb1u, t5) ^ masked_shuffle(sb1t, t6);
450 SIMD_4x32(0x00000070, 0x00000000, 0x00000000, 0x00000000),
451 SIMD_4x32(0x0000002A, 0x00000000, 0x00000000, 0x00000000),
452 SIMD_4x32(0x00000098, 0x00000000, 0x00000000, 0x00000000),
453 SIMD_4x32(0x00000008, 0x00000000, 0x00000000, 0x00000000),
454 SIMD_4x32(0x0000004D, 0x00000000, 0x00000000, 0x00000000),
455 SIMD_4x32(0x0000007C, 0x00000000, 0x00000000, 0x00000000),
456 SIMD_4x32(0x0000007D, 0x00000000, 0x00000000, 0x00000000),
457 SIMD_4x32(0x00000081, 0x00000000, 0x00000000, 0x00000000),
458 SIMD_4x32(0x0000001F, 0x00000000, 0x00000000, 0x00000000),
459 SIMD_4x32(0x00000083, 0x00000000, 0x00000000, 0x00000000),
464 return aes_schedule_round(shuffle(input1, shuffle3333_15), input2 ^ rcon[rc]);
468 const SIMD_4x32 shuffle3332 =
SIMD_4x32(0x0B0A0908, 0x0F0E0D0C, 0x0F0E0D0C, 0x0F0E0D0C);
469 const SIMD_4x32 shuffle2000 =
SIMD_4x32(0x03020100, 0x03020100, 0x03020100, 0x0B0A0908);
471 const SIMD_4x32 zero_top_half(0, 0, 0xFFFFFFFF, 0xFFFFFFFF);
473 return y ^ shuffle(x, shuffle3332) ^ shuffle(y, shuffle2000);
480void BOTAN_FN_ISA_SIMD_4X32 AES_128::vperm_key_schedule(
const uint8_t keyb[],
size_t ) {
486 shiftrows(key, 2).store_le(&m_DK[4 * 10]);
488 key = aes_schedule_transform_init(key);
489 key.store_le(&m_EK[0]);
491 for(
size_t i = 1; i != 10; ++i) {
492 key = aes_schedule_round_rcon(i - 1, key, key);
494 aes_schedule_mangle(key, (12 - i) % 4).store_le(&m_EK[4 * i]);
496 aes_schedule_mangle_dec(key, (10 - i) % 4).store_le(&m_DK[4 * (10 - i)]);
499 key = aes_schedule_round_rcon(9, key, key);
500 aes_schedule_mangle_last(key, 2).store_le(&m_EK[4 * 10]);
501 aes_schedule_mangle_last_dec(key).store_le(&m_DK[0]);
504void BOTAN_FN_ISA_SIMD_4X32 AES_192::vperm_key_schedule(
const uint8_t keyb[],
size_t ) {
511 shiftrows(key1, 0).store_le(&m_DK[12 * 4]);
513 key1 = aes_schedule_transform_init(key1);
514 key2 = aes_schedule_transform_init(key2);
516 key1.store_le(&m_EK[0]);
518 for(
size_t i = 0; i != 4; ++i) {
521 key2 = aes_schedule_round_rcon(2 * i, key2, key1);
524 aes_schedule_mangle(key2t, (i + 3) % 4).store_le(&m_EK[4 * (3 * i + 1)]);
525 aes_schedule_mangle_dec(key2t, (i + 3) % 4).store_le(&m_DK[4 * (11 - 3 * i)]);
527 t = aes_schedule_192_smear(key2, t);
529 aes_schedule_mangle(t, (i + 2) % 4).store_le(&m_EK[4 * (3 * i + 2)]);
530 aes_schedule_mangle_dec(t, (i + 2) % 4).store_le(&m_DK[4 * (10 - 3 * i)]);
532 key2 = aes_schedule_round_rcon(2 * i + 1, t, key2);
535 aes_schedule_mangle_last(key2, (i + 1) % 4).store_le(&m_EK[4 * (3 * i + 3)]);
536 aes_schedule_mangle_last_dec(key2).store_le(&m_DK[4 * (9 - 3 * i)]);
538 aes_schedule_mangle(key2, (i + 1) % 4).store_le(&m_EK[4 * (3 * i + 3)]);
539 aes_schedule_mangle_dec(key2, (i + 1) % 4).store_le(&m_DK[4 * (9 - 3 * i)]);
543 key2 = aes_schedule_192_smear(key2, t);
547void BOTAN_FN_ISA_SIMD_4X32 AES_256::vperm_key_schedule(
const uint8_t keyb[],
size_t ) {
554 shiftrows(key1, 2).store_le(&m_DK[4 * 14]);
556 key1 = aes_schedule_transform_init(key1);
557 key2 = aes_schedule_transform_init(key2);
559 key1.store_le(&m_EK[0]);
560 aes_schedule_mangle(key2, 3).store_le(&m_EK[4]);
562 aes_schedule_mangle_dec(key2, 1).store_le(&m_DK[4 * 13]);
566 for(
size_t i = 2; i != 14; i += 2) {
567 const SIMD_4x32 k_t = key2;
568 key1 = key2 = aes_schedule_round_rcon((i / 2) - 1, key2, key1);
570 aes_schedule_mangle(key2, i % 4).store_le(&m_EK[4 * i]);
571 aes_schedule_mangle_dec(key2, (i + 2) % 4).store_le(&m_DK[4 * (14 - i)]);
573 key2 = aes_schedule_round(shuffle(key2, shuffle3333), k_t);
575 aes_schedule_mangle(key2, (i - 1) % 4).store_le(&m_EK[4 * (i + 1)]);
576 aes_schedule_mangle_dec(key2, (i + 1) % 4).store_le(&m_DK[4 * (13 - i)]);
579 key2 = aes_schedule_round_rcon(6, key2, key1);
581 aes_schedule_mangle_last(key2, 2).store_le(&m_EK[4 * 14]);
582 aes_schedule_mangle_last_dec(key2).store_le(&m_DK[0]);
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 load_le(const void *in) noexcept
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 byte_shuffle(const SIMD_4x32 &tbl, const SIMD_4x32 &idx)
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 alignr8(const SIMD_4x32 &a, const SIMD_4x32 &b)
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 splat(uint32_t B) noexcept
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 masked_byte_shuffle(const SIMD_4x32 &tbl, const SIMD_4x32 &idx)
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 splat_u8(uint8_t B) noexcept
BOTAN_FN_ISA_SIMD_4X32 SIMD_4x32 bswap() const noexcept
SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 shift_elems_left() const noexcept
#define BOTAN_FORCE_INLINE
constexpr void unpoison(const T *p, size_t n)
constexpr void poison(const T *p, size_t n)