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>
25 if constexpr(std::endian::native == std::endian::little) {
33 if constexpr(std::endian::native == std::endian::little) {
54const SIMD_4x32 mc_forward[4] = {
SIMD_4x32(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D),
55 SIMD_4x32(0x04070605, 0x080B0A09, 0x0C0F0E0D, 0x00030201),
56 SIMD_4x32(0x080B0A09, 0x0C0F0E0D, 0x00030201, 0x04070605),
57 SIMD_4x32(0x0C0F0E0D, 0x00030201, 0x04070605, 0x080B0A09)};
60 SIMD_4x32(0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C),
61 SIMD_4x32(0x0F0A0500, 0x030E0904, 0x07020D08, 0x0B06010C),
62 SIMD_4x32(0x0B020900, 0x0F060D04, 0x030A0108, 0x070E050C),
63 SIMD_4x32(0x070A0D00, 0x0B0E0104, 0x0F020508, 0x0306090C),
67 SIMD_4x32(0x00000070, 0x00000000, 0x00000000, 0x00000000),
68 SIMD_4x32(0x0000002A, 0x00000000, 0x00000000, 0x00000000),
69 SIMD_4x32(0x00000098, 0x00000000, 0x00000000, 0x00000000),
70 SIMD_4x32(0x00000008, 0x00000000, 0x00000000, 0x00000000),
71 SIMD_4x32(0x0000004D, 0x00000000, 0x00000000, 0x00000000),
72 SIMD_4x32(0x0000007C, 0x00000000, 0x00000000, 0x00000000),
73 SIMD_4x32(0x0000007D, 0x00000000, 0x00000000, 0x00000000),
74 SIMD_4x32(0x00000081, 0x00000000, 0x00000000, 0x00000000),
75 SIMD_4x32(0x0000001F, 0x00000000, 0x00000000, 0x00000000),
76 SIMD_4x32(0x00000083, 0x00000000, 0x00000000, 0x00000000),
98 SIMD_4x32(0x0C0F0E0D, 0x00030201, 0x04070605, 0x080B0A09),
99 SIMD_4x32(0x080B0A09, 0x0C0F0E0D, 0x00030201, 0x04070605),
100 SIMD_4x32(0x04070605, 0x080B0A09, 0x0C0F0E0D, 0x00030201),
101 SIMD_4x32(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D),
105 SIMD_4x32(0x02010003, 0x06050407, 0x0A09080B, 0x0E0D0C0F),
106 SIMD_4x32(0x0E0D0C0F, 0x02010003, 0x06050407, 0x0A09080B),
107 SIMD_4x32(0x0A09080B, 0x0E0D0C0F, 0x02010003, 0x06050407),
108 SIMD_4x32(0x06050407, 0x0A09080B, 0x0E0D0C0F, 0x02010003),
114 return lo_nibs_mask & x;
118 return (x.shr<4>() & lo_nibs_mask);
122 return shuffle(k_ipt1, low_nibs(B)) ^ shuffle(k_ipt2, high_nibs(B)) ^ K;
128 const SIMD_4x32 t2 = shuffle(k_inv2, Bl);
131 const SIMD_4x32 t5 = Bl ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, Bh));
132 const SIMD_4x32 t6 = Bh ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, Bl));
134 const SIMD_4x32 t7 = masked_shuffle(sb1t, t6) ^ masked_shuffle(sb1u, t5) ^ K;
135 const SIMD_4x32 t8 = masked_shuffle(sb2t, t6) ^ masked_shuffle(sb2u, t5) ^ shuffle(t7, mc_forward[r % 4]);
137 return shuffle(t8, mc_forward[r % 4]) ^ shuffle(t7, mc_backward[r % 4]) ^ t8;
143 const SIMD_4x32 t2 = shuffle(k_inv2, Bl);
146 const SIMD_4x32 t5 = Bl ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, Bh));
147 const SIMD_4x32 t6 = Bh ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, Bl));
149 return shuffle(masked_shuffle(sbou, t5) ^ masked_shuffle(sbot, t6) ^ K, vperm_sr[r % 4]);
153 return shuffle(k_dipt1, low_nibs(B)) ^ shuffle(k_dipt2, high_nibs(B)) ^ K;
163 const SIMD_4x32 t5 = B ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, Bh));
164 const SIMD_4x32 t6 = Bh ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, B));
168 const SIMD_4x32 t8 = masked_shuffle(sb9t, t6) ^ masked_shuffle(sb9u, t5) ^ K;
169 const SIMD_4x32 t9 = shuffle(t8, mc) ^ masked_shuffle(sbdu, t5) ^ masked_shuffle(sbdt, t6);
170 const SIMD_4x32 t12 = shuffle(t9, mc) ^ masked_shuffle(sbbu, t5) ^ masked_shuffle(sbbt, t6);
171 return shuffle(t12, mc) ^ masked_shuffle(sbeu, t5) ^ masked_shuffle(sbet, t6);
175 const uint32_t which_sr = ((((r - 1) << 4) ^ 48) & 48) / 16;
183 const SIMD_4x32 t5 = B ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, Bh));
184 const SIMD_4x32 t6 = Bh ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, B));
186 const SIMD_4x32 x = masked_shuffle(sboud, t5) ^ masked_shuffle(sbotd, t6) ^ K;
187 return shuffle(x, vperm_sr[which_sr]);
190void BOTAN_FN_ISA_SIMD_4X32
191vperm_encrypt_blocks(
const uint8_t in[], uint8_t out[],
size_t blocks,
const SIMD_4x32 K[],
size_t rounds) {
194 const size_t blocks2 = blocks - (blocks % 2);
196 for(
size_t i = 0; i != blocks2; i += 2) {
200 B0 = aes_enc_first_round(B0, K[0]);
201 B1 = aes_enc_first_round(B1, K[0]);
203 for(
size_t r = 1; r != rounds; ++r) {
204 B0 = aes_enc_round(B0, K[r], r);
205 B1 = aes_enc_round(B1, K[r], r);
208 B0 = aes_enc_last_round(B0, K[rounds], rounds);
209 B1 = aes_enc_last_round(B1, K[rounds], rounds);
211 B0.store_le(out + i * 16);
212 B1.store_le(out + (i + 1) * 16);
215 for(
size_t i = blocks2; i < blocks; ++i) {
218 B = aes_enc_first_round(B, K[0]);
220 for(
size_t r = 1; r != rounds; ++r) {
221 B = aes_enc_round(B, K[r], r);
224 B = aes_enc_last_round(B, K[rounds], rounds);
225 B.store_le(out + i * 16);
232void BOTAN_FN_ISA_SIMD_4X32
233vperm_decrypt_blocks(
const uint8_t in[], uint8_t out[],
size_t blocks,
const SIMD_4x32 K[],
size_t rounds) {
236 const size_t blocks2 = blocks - (blocks % 2);
238 for(
size_t i = 0; i != blocks2; i += 2) {
242 B0 = aes_dec_first_round(B0, K[0]);
243 B1 = aes_dec_first_round(B1, K[0]);
245 for(
size_t r = 1; r != rounds; ++r) {
246 B0 = aes_dec_round(B0, K[r], r);
247 B1 = aes_dec_round(B1, K[r], r);
250 B0 = aes_dec_last_round(B0, K[rounds], rounds);
251 B1 = aes_dec_last_round(B1, K[rounds], rounds);
253 B0.store_le(out + i * 16);
254 B1.store_le(out + (i + 1) * 16);
257 for(
size_t i = blocks2; i < blocks; ++i) {
260 B = aes_dec_first_round(B, K[0]);
262 for(
size_t r = 1; r != rounds; ++r) {
263 B = aes_dec_round(B, K[r], r);
266 B = aes_dec_last_round(B, K[rounds], rounds);
267 B.store_le(out + i * 16);
276void AES_128::vperm_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
277 const SIMD_4x32 K[11] = {
291 return vperm_encrypt_blocks(in, out, blocks, K, 10);
294void AES_128::vperm_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
295 const SIMD_4x32 K[11] = {
309 return vperm_decrypt_blocks(in, out, blocks, K, 10);
312void AES_192::vperm_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
313 const SIMD_4x32 K[13] = {
329 return vperm_encrypt_blocks(in, out, blocks, K, 12);
332void AES_192::vperm_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
333 const SIMD_4x32 K[13] = {
349 return vperm_decrypt_blocks(in, out, blocks, K, 12);
352void AES_256::vperm_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
353 const SIMD_4x32 K[15] = {
371 return vperm_encrypt_blocks(in, out, blocks, K, 14);
374void AES_256::vperm_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
375 const SIMD_4x32 K[15] = {
393 return vperm_decrypt_blocks(in, out, blocks, K, 14);
399 return shuffle(table_1, low_nibs(input)) ^ shuffle(table_2, high_nibs(input));
403 const SIMD_4x32 mc_forward0(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D);
407 t = shuffle(t, mc_forward0);
408 t2 = t ^ t2 ^ shuffle(t, mc_forward0);
409 return shuffle(t2, vperm_sr[round_no % 4]);
412SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 aes_schedule_mangle_dec(
SIMD_4x32 k, uint8_t round_no) {
413 const SIMD_4x32 mc_forward0(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D);
416 SIMD_4x32(0x7ED9A700, 0xB6116FC8, 0x82255BFC, 0x4AED9334),
417 SIMD_4x32(0x27143300, 0x45765162, 0xE9DAFDCE, 0x8BB89FAC),
418 SIMD_4x32(0xCCA86400, 0x27438FEB, 0xADC90561, 0x4622EE8A),
419 SIMD_4x32(0x4F92DD00, 0x815C13CE, 0xBD602FF2, 0x73AEE13C),
420 SIMD_4x32(0x01C6C700, 0x03C4C502, 0xFA3D3CFB, 0xF83F3EF9),
421 SIMD_4x32(0x38CFF700, 0xEE1921D6, 0x7384BC4B, 0xA5526A9D),
422 SIMD_4x32(0x53732000, 0xE3C390B0, 0x10306343, 0xA080D3F3),
423 SIMD_4x32(0x036982E8, 0xA0CA214B, 0x8CE60D67, 0x2F45AEC4),
426 SIMD_4x32 t = aes_schedule_transform(k, dsk[0], dsk[1]);
427 SIMD_4x32 output = shuffle(t, mc_forward0);
429 t = aes_schedule_transform(t, dsk[2], dsk[3]);
430 output = shuffle(t ^ output, mc_forward0);
432 t = aes_schedule_transform(t, dsk[4], dsk[5]);
433 output = shuffle(t ^ output, mc_forward0);
435 t = aes_schedule_transform(t, dsk[6], dsk[7]);
436 output = shuffle(t ^ output, mc_forward0);
438 return shuffle(output, vperm_sr[round_no % 4]);
441SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 aes_schedule_mangle_last(
SIMD_4x32 k, uint8_t round_no) {
442 const SIMD_4x32 out_tr1(0xD6B66000, 0xFF9F4929, 0xDEBE6808, 0xF7974121);
443 const SIMD_4x32 out_tr2(0x50BCEC00, 0x01EDBD51, 0xB05C0CE0, 0xE10D5DB1);
445 k = shuffle(k, vperm_sr[round_no % 4]);
447 return aes_schedule_transform(k, out_tr1, out_tr2);
451 const SIMD_4x32 deskew1(0x47A4E300, 0x07E4A340, 0x5DBEF91A, 0x1DFEB95A);
452 const SIMD_4x32 deskew2(0x83EA6900, 0x5F36B5DC, 0xF49D1E77, 0x2841C2AB);
455 return aes_schedule_transform(k, deskew1, deskew2);
466 const SIMD_4x32 t2 = shuffle(k_inv2, Bl);
470 SIMD_4x32 t5 = Bl ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, Bh));
471 SIMD_4x32 t6 = Bh ^ masked_shuffle(k_inv1, t2 ^ shuffle(k_inv1, Bl));
473 return smeared ^ masked_shuffle(sb1u, t5) ^ masked_shuffle(sb1t, t6);
479 return aes_schedule_round(shuffle(input1, shuffle3333_15), input2 ^ rc);
483 const SIMD_4x32 shuffle3332 =
SIMD_4x32(0x0B0A0908, 0x0F0E0D0C, 0x0F0E0D0C, 0x0F0E0D0C);
484 const SIMD_4x32 shuffle2000 =
SIMD_4x32(0x03020100, 0x03020100, 0x03020100, 0x0B0A0908);
486 const SIMD_4x32 zero_top_half(0, 0, 0xFFFFFFFF, 0xFFFFFFFF);
488 return y ^ shuffle(x, shuffle3332) ^ shuffle(y, shuffle2000);
495void AES_128::vperm_key_schedule(
const uint8_t keyb[],
size_t ) {
501 shuffle(key, vperm_sr[2]).store_le(&m_DK[4 * 10]);
503 key = aes_schedule_transform(key, k_ipt1, k_ipt2);
504 key.store_le(&m_EK[0]);
506 for(
size_t i = 1; i != 10; ++i) {
507 key = aes_schedule_round(rcon[i - 1], key, key);
509 aes_schedule_mangle(key, (12 - i) % 4).store_le(&m_EK[4 * i]);
511 aes_schedule_mangle_dec(key, (10 - i) % 4).store_le(&m_DK[4 * (10 - i)]);
514 key = aes_schedule_round(rcon[9], key, key);
515 aes_schedule_mangle_last(key, 2).store_le(&m_EK[4 * 10]);
516 aes_schedule_mangle_last_dec(key).store_le(&m_DK[0]);
519void AES_192::vperm_key_schedule(
const uint8_t keyb[],
size_t ) {
526 shuffle(key1, vperm_sr[0]).store_le(&m_DK[12 * 4]);
528 key1 = aes_schedule_transform(key1, k_ipt1, k_ipt2);
529 key2 = aes_schedule_transform(key2, k_ipt1, k_ipt2);
531 key1.store_le(&m_EK[0]);
533 for(
size_t i = 0; i != 4; ++i) {
536 key2 = aes_schedule_round(rcon[2 * i], key2, key1);
539 aes_schedule_mangle(key2t, (i + 3) % 4).store_le(&m_EK[4 * (3 * i + 1)]);
540 aes_schedule_mangle_dec(key2t, (i + 3) % 4).store_le(&m_DK[4 * (11 - 3 * i)]);
542 t = aes_schedule_192_smear(key2, t);
544 aes_schedule_mangle(t, (i + 2) % 4).store_le(&m_EK[4 * (3 * i + 2)]);
545 aes_schedule_mangle_dec(t, (i + 2) % 4).store_le(&m_DK[4 * (10 - 3 * i)]);
547 key2 = aes_schedule_round(rcon[2 * i + 1], t, key2);
550 aes_schedule_mangle_last(key2, (i + 1) % 4).store_le(&m_EK[4 * (3 * i + 3)]);
551 aes_schedule_mangle_last_dec(key2).store_le(&m_DK[4 * (9 - 3 * i)]);
553 aes_schedule_mangle(key2, (i + 1) % 4).store_le(&m_EK[4 * (3 * i + 3)]);
554 aes_schedule_mangle_dec(key2, (i + 1) % 4).store_le(&m_DK[4 * (9 - 3 * i)]);
558 key2 = aes_schedule_192_smear(key2, t);
562void AES_256::vperm_key_schedule(
const uint8_t keyb[],
size_t ) {
569 shuffle(key1, vperm_sr[2]).store_le(&m_DK[4 * 14]);
571 key1 = aes_schedule_transform(key1, k_ipt1, k_ipt2);
572 key2 = aes_schedule_transform(key2, k_ipt1, k_ipt2);
574 key1.store_le(&m_EK[0]);
575 aes_schedule_mangle(key2, 3).store_le(&m_EK[4]);
577 aes_schedule_mangle_dec(key2, 1).store_le(&m_DK[4 * 13]);
581 for(
size_t i = 2; i != 14; i += 2) {
582 const SIMD_4x32 k_t = key2;
583 key1 = key2 = aes_schedule_round(rcon[(i / 2) - 1], key2, key1);
585 aes_schedule_mangle(key2, i % 4).store_le(&m_EK[4 * i]);
586 aes_schedule_mangle_dec(key2, (i + 2) % 4).store_le(&m_DK[4 * (14 - i)]);
588 key2 = aes_schedule_round(shuffle(key2, shuffle3333), k_t);
590 aes_schedule_mangle(key2, (i - 1) % 4).store_le(&m_EK[4 * (i + 1)]);
591 aes_schedule_mangle_dec(key2, (i + 1) % 4).store_le(&m_DK[4 * (13 - i)]);
594 key2 = aes_schedule_round(rcon[6], key2, key1);
596 aes_schedule_mangle_last(key2, 2).store_le(&m_EK[4 * 14]);
597 aes_schedule_mangle_last_dec(key2).store_le(&m_DK[0]);
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 byte_shuffle(const SIMD_4x32 &tbl, const SIMD_4x32 &idx)
static SIMD_4x32 load_le(const void *in) noexcept
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 alignr8(const SIMD_4x32 &a, const SIMD_4x32 &b)
SIMD_4x32 shift_elems_left() const noexcept
static SIMD_4x32 splat_u8(uint8_t B) noexcept
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 masked_byte_shuffle(const SIMD_4x32 &tbl, const SIMD_4x32 &idx)
BOTAN_FN_ISA_SIMD_4X32 SIMD_4x32 bswap() const noexcept
static SIMD_4x32 splat(uint32_t B) noexcept
constexpr void unpoison(const T *p, size_t n)
constexpr void poison(const T *p, size_t n)