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 SIMD_4x32(0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C),
27 SIMD_4x32(0x0F0A0500, 0x030E0904, 0x07020D08, 0x0B06010C),
28 SIMD_4x32(0x0B020900, 0x0F060D04, 0x030A0108, 0x070E050C),
29 SIMD_4x32(0x070A0D00, 0x0B0E0104, 0x0F020508, 0x0306090C),
37 return lo_nibs_mask & x;
42 return (x.shr<4>() & lo_nibs_mask);
64 return std::make_pair(t5, t6);
71 const SIMD_4x32 mc_forward[4] = {
SIMD_4x32(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D),
72 SIMD_4x32(0x04070605, 0x080B0A09, 0x0C0F0E0D, 0x00030201),
73 SIMD_4x32(0x080B0A09, 0x0C0F0E0D, 0x00030201, 0x04070605),
74 SIMD_4x32(0x0C0F0E0D, 0x00030201, 0x04070605, 0x080B0A09)};
76 SIMD_4x32(0x02010003, 0x06050407, 0x0A09080B, 0x0E0D0C0F),
77 SIMD_4x32(0x0E0D0C0F, 0x02010003, 0x06050407, 0x0A09080B),
78 SIMD_4x32(0x0A09080B, 0x0E0D0C0F, 0x02010003, 0x06050407),
79 SIMD_4x32(0x06050407, 0x0A09080B, 0x0E0D0C0F, 0x02010003),
84 const auto [t5, t6] = aes_decompose_kinv(B);
97 const auto [t5, t6] = aes_decompose_kinv(B);
111 SIMD_4x32(0x0C0F0E0D, 0x00030201, 0x04070605, 0x080B0A09),
112 SIMD_4x32(0x080B0A09, 0x0C0F0E0D, 0x00030201, 0x04070605),
113 SIMD_4x32(0x04070605, 0x080B0A09, 0x0C0F0E0D, 0x00030201),
114 SIMD_4x32(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D),
126 const auto [t5, t6] = aes_decompose_kinv(B);
143 const uint32_t which_sr = ((((r - 1) << 4) ^ 48) & 48) / 16;
145 const auto [t5, t6] = aes_decompose_kinv(B);
148 return shiftrows(x, which_sr);
151void BOTAN_FN_ISA_SIMD_4X32
152vperm_encrypt_blocks(
const uint8_t in[], uint8_t out[],
size_t blocks,
const SIMD_4x32 K[],
size_t rounds) {
155 const size_t blocks2 = blocks - (blocks % 2);
157 for(
size_t i = 0; i != blocks2; i += 2) {
161 B0 = aes_enc_first_round(B0, K[0]);
162 B1 = aes_enc_first_round(B1, K[0]);
164 for(
size_t r = 1; r != rounds; ++r) {
165 B0 = aes_enc_round(B0, K[r], r);
166 B1 = aes_enc_round(B1, K[r], r);
169 B0 = aes_enc_last_round(B0, K[rounds], rounds);
170 B1 = aes_enc_last_round(B1, K[rounds], rounds);
172 B0.store_le(out + i * 16);
173 B1.store_le(out + (i + 1) * 16);
176 for(
size_t i = blocks2; i < blocks; ++i) {
179 B = aes_enc_first_round(B, K[0]);
181 for(
size_t r = 1; r != rounds; ++r) {
182 B = aes_enc_round(B, K[r], r);
185 B = aes_enc_last_round(B, K[rounds], rounds);
186 B.store_le(out + i * 16);
193void BOTAN_FN_ISA_SIMD_4X32
194vperm_decrypt_blocks(
const uint8_t in[], uint8_t out[],
size_t blocks,
const SIMD_4x32 K[],
size_t rounds) {
197 const size_t blocks2 = blocks - (blocks % 2);
199 for(
size_t i = 0; i != blocks2; i += 2) {
203 B0 = aes_dec_first_round(B0, K[0]);
204 B1 = aes_dec_first_round(B1, K[0]);
206 for(
size_t r = 1; r != rounds; ++r) {
207 B0 = aes_dec_round(B0, K[r], r);
208 B1 = aes_dec_round(B1, K[r], r);
211 B0 = aes_dec_last_round(B0, K[rounds], rounds);
212 B1 = aes_dec_last_round(B1, K[rounds], rounds);
214 B0.store_le(out + i * 16);
215 B1.store_le(out + (i + 1) * 16);
218 for(
size_t i = blocks2; i < blocks; ++i) {
221 B = aes_dec_first_round(B, K[0]);
223 for(
size_t r = 1; r != rounds; ++r) {
224 B = aes_dec_round(B, K[r], r);
227 B = aes_dec_last_round(B, K[rounds], rounds);
228 B.store_le(out + i * 16);
237void BOTAN_FN_ISA_SIMD_4X32 AES_128::vperm_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
238 const SIMD_4x32 K[11] = {
252 return vperm_encrypt_blocks(in, out, blocks, K, 10);
255void BOTAN_FN_ISA_SIMD_4X32 AES_128::vperm_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
256 const SIMD_4x32 K[11] = {
270 return vperm_decrypt_blocks(in, out, blocks, K, 10);
273void BOTAN_FN_ISA_SIMD_4X32 AES_192::vperm_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
274 const SIMD_4x32 K[13] = {
290 return vperm_encrypt_blocks(in, out, blocks, K, 12);
293void BOTAN_FN_ISA_SIMD_4X32 AES_192::vperm_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
294 const SIMD_4x32 K[13] = {
310 return vperm_decrypt_blocks(in, out, blocks, K, 12);
313void BOTAN_FN_ISA_SIMD_4X32 AES_256::vperm_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
314 const SIMD_4x32 K[15] = {
332 return vperm_encrypt_blocks(in, out, blocks, K, 14);
335void BOTAN_FN_ISA_SIMD_4X32 AES_256::vperm_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
336 const SIMD_4x32 K[15] = {
354 return vperm_decrypt_blocks(in, out, blocks, K, 14);
364 return aes_enc_first_round(input,
SIMD_4x32());
368 const SIMD_4x32 mc_forward0(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D);
374 return shiftrows(t2, round_no % 4);
377SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 aes_schedule_mangle_dec(
SIMD_4x32 k, uint8_t round_no) {
378 const SIMD_4x32 mc_forward0(0x00030201, 0x04070605, 0x080B0A09, 0x0C0F0E0D);
381 SIMD_4x32(0x7ED9A700, 0xB6116FC8, 0x82255BFC, 0x4AED9334),
382 SIMD_4x32(0x27143300, 0x45765162, 0xE9DAFDCE, 0x8BB89FAC),
383 SIMD_4x32(0xCCA86400, 0x27438FEB, 0xADC90561, 0x4622EE8A),
384 SIMD_4x32(0x4F92DD00, 0x815C13CE, 0xBD602FF2, 0x73AEE13C),
385 SIMD_4x32(0x01C6C700, 0x03C4C502, 0xFA3D3CFB, 0xF83F3EF9),
386 SIMD_4x32(0x38CFF700, 0xEE1921D6, 0x7384BC4B, 0xA5526A9D),
387 SIMD_4x32(0x53732000, 0xE3C390B0, 0x10306343, 0xA080D3F3),
388 SIMD_4x32(0x036982E8, 0xA0CA214B, 0x8CE60D67, 0x2F45AEC4),
391 SIMD_4x32 t = aes_schedule_transform(k, dsk[0], dsk[1]);
394 t = aes_schedule_transform(t, dsk[2], dsk[3]);
397 t = aes_schedule_transform(t, dsk[4], dsk[5]);
400 t = aes_schedule_transform(t, dsk[6], dsk[7]);
403 return shiftrows(output, round_no % 4);
406SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 aes_schedule_mangle_last(
SIMD_4x32 k, uint8_t round_no) {
407 const SIMD_4x32 out_tr1(0xD6B66000, 0xFF9F4929, 0xDEBE6808, 0xF7974121);
408 const SIMD_4x32 out_tr2(0x50BCEC00, 0x01EDBD51, 0xB05C0CE0, 0xE10D5DB1);
411 return aes_schedule_transform(k, out_tr1, out_tr2);
415 const SIMD_4x32 deskew1(0x47A4E300, 0x07E4A340, 0x5DBEF91A, 0x1DFEB95A);
416 const SIMD_4x32 deskew2(0x83EA6900, 0x5F36B5DC, 0xF49D1E77, 0x2841C2AB);
419 return aes_schedule_transform(k, deskew1, deskew2);
430 const auto [t5, t6] = aes_decompose_kinv(input1);
437 SIMD_4x32(0x00000070, 0x00000000, 0x00000000, 0x00000000),
438 SIMD_4x32(0x0000002A, 0x00000000, 0x00000000, 0x00000000),
439 SIMD_4x32(0x00000098, 0x00000000, 0x00000000, 0x00000000),
440 SIMD_4x32(0x00000008, 0x00000000, 0x00000000, 0x00000000),
441 SIMD_4x32(0x0000004D, 0x00000000, 0x00000000, 0x00000000),
442 SIMD_4x32(0x0000007C, 0x00000000, 0x00000000, 0x00000000),
443 SIMD_4x32(0x0000007D, 0x00000000, 0x00000000, 0x00000000),
444 SIMD_4x32(0x00000081, 0x00000000, 0x00000000, 0x00000000),
445 SIMD_4x32(0x0000001F, 0x00000000, 0x00000000, 0x00000000),
446 SIMD_4x32(0x00000083, 0x00000000, 0x00000000, 0x00000000),
455 const SIMD_4x32 shuffle3332 =
SIMD_4x32(0x0B0A0908, 0x0F0E0D0C, 0x0F0E0D0C, 0x0F0E0D0C);
456 const SIMD_4x32 shuffle2000 =
SIMD_4x32(0x03020100, 0x03020100, 0x03020100, 0x0B0A0908);
458 const SIMD_4x32 zero_top_half(0, 0, 0xFFFFFFFF, 0xFFFFFFFF);
467void BOTAN_FN_ISA_SIMD_4X32 AES_128::vperm_key_schedule(
const uint8_t keyb[],
size_t ) {
473 shiftrows(key, 2).store_le(&m_DK[4 * 10]);
475 key = aes_schedule_transform_init(key);
476 key.store_le(&m_EK[0]);
478 for(
size_t i = 1; i != 10; ++i) {
479 key = aes_schedule_round_rcon(i - 1, key, key);
481 aes_schedule_mangle(key, (12 - i) % 4).store_le(&m_EK[4 * i]);
483 aes_schedule_mangle_dec(key, (10 - i) % 4).store_le(&m_DK[4 * (10 - i)]);
486 key = aes_schedule_round_rcon(9, key, key);
487 aes_schedule_mangle_last(key, 2).store_le(&m_EK[4 * 10]);
488 aes_schedule_mangle_last_dec(key).store_le(&m_DK[0]);
491void BOTAN_FN_ISA_SIMD_4X32 AES_192::vperm_key_schedule(
const uint8_t keyb[],
size_t ) {
498 shiftrows(key1, 0).store_le(&m_DK[12 * 4]);
500 key1 = aes_schedule_transform_init(key1);
501 key2 = aes_schedule_transform_init(key2);
503 key1.store_le(&m_EK[0]);
505 for(
size_t i = 0; i != 4; ++i) {
508 key2 = aes_schedule_round_rcon(2 * i, key2, key1);
511 aes_schedule_mangle(key2t, (i + 3) % 4).store_le(&m_EK[4 * (3 * i + 1)]);
512 aes_schedule_mangle_dec(key2t, (i + 3) % 4).store_le(&m_DK[4 * (11 - 3 * i)]);
514 t = aes_schedule_192_smear(key2, t);
516 aes_schedule_mangle(t, (i + 2) % 4).store_le(&m_EK[4 * (3 * i + 2)]);
517 aes_schedule_mangle_dec(t, (i + 2) % 4).store_le(&m_DK[4 * (10 - 3 * i)]);
519 key2 = aes_schedule_round_rcon(2 * i + 1, t, key2);
522 aes_schedule_mangle_last(key2, (i + 1) % 4).store_le(&m_EK[4 * (3 * i + 3)]);
523 aes_schedule_mangle_last_dec(key2).store_le(&m_DK[4 * (9 - 3 * i)]);
525 aes_schedule_mangle(key2, (i + 1) % 4).store_le(&m_EK[4 * (3 * i + 3)]);
526 aes_schedule_mangle_dec(key2, (i + 1) % 4).store_le(&m_DK[4 * (9 - 3 * i)]);
530 key2 = aes_schedule_192_smear(key2, t);
534void BOTAN_FN_ISA_SIMD_4X32 AES_256::vperm_key_schedule(
const uint8_t keyb[],
size_t ) {
541 shiftrows(key1, 2).store_le(&m_DK[4 * 14]);
543 key1 = aes_schedule_transform_init(key1);
544 key2 = aes_schedule_transform_init(key2);
546 key1.store_le(&m_EK[0]);
547 aes_schedule_mangle(key2, 3).store_le(&m_EK[4]);
549 aes_schedule_mangle_dec(key2, 1).store_le(&m_DK[4 * 13]);
553 for(
size_t i = 2; i != 14; i += 2) {
554 const SIMD_4x32 k_t = key2;
555 key1 = key2 = aes_schedule_round_rcon((i / 2) - 1, key2, key1);
557 aes_schedule_mangle(key2, i % 4).store_le(&m_EK[4 * i]);
558 aes_schedule_mangle_dec(key2, (i + 2) % 4).store_le(&m_DK[4 * (14 - i)]);
562 aes_schedule_mangle(key2, (i - 1) % 4).store_le(&m_EK[4 * (i + 1)]);
563 aes_schedule_mangle_dec(key2, (i + 1) % 4).store_le(&m_DK[4 * (13 - i)]);
566 key2 = aes_schedule_round_rcon(6, key2, key1);
568 aes_schedule_mangle_last(key2, 2).store_le(&m_EK[4 * 14]);
569 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
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)