8#include <botan/internal/aes.h>
10#include <botan/internal/isa_extn.h>
11#include <botan/internal/simd_4x32.h>
12#include <botan/internal/simd_hwaes.h>
37template <
bool ENC,
typename... Bs>
39 ((B = hw_aes_round<ENC>(B, K)), ...);
43template <
size_t R,
bool ENC,
size_t... Is,
typename... Bs>
45 std::index_sequence<Is...> ,
48 (hw_aes_round_all<ENC>(K[Is + S], B...), ...);
56template <
size_t R,
bool ENC,
typename... Bs>
64 hw_aes_inner_rounds<R, ENC>(K, std::make_index_sequence<R - 1>{}, B...);
66 ((B = hw_aes_last_round<ENC>(B, K[R - 1 + S])), ...);
73template <
size_t R,
size_t... Is>
75 const uint32_t keys[],
76 std::index_sequence<Is...> ) {
77 ((K[Is] =
hw_aes_load_block(
reinterpret_cast<const uint8_t*
>(&keys[4 * Is]))), ...);
80template <
size_t R,
bool ENC>
81BOTAN_FN_ISA_HWAES
void hw_aes_process(
const uint8_t in[], uint8_t out[],
size_t blocks,
const uint32_t keys[]) {
83 hw_aes_load_keys<R>(K, keys, std::make_index_sequence<R + 1>{});
91 hw_aes_blocks<R, ENC>(K, B0, B1, B2, B3);
103 for(
size_t i = 0; i != blocks; ++i) {
105 hw_aes_blocks<R, ENC>(K, B);
139 const auto K = next_round_key(prev, sub_rot_w3(prev,
SIMD_4x32::splat(RC)));
153 K1 ^=
K1.shift_elems_left<1>();
155 K1.store_le(out + 4);
163 K1 = next_round_key(K1, sub_w3(K0));
164 K1.store_le(out + 4);
172 const size_t rounds = EK.size() / 4 - 1;
173 DK.resize(EK.size());
176 for(
size_t i = 1; i != rounds; ++i) {
184void BOTAN_FN_ISA_HWAES AES_128::hw_aes_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
185 hw_aes_process<10, true>(in, out, blocks, m_EK.data());
188void BOTAN_FN_ISA_HWAES AES_128::hw_aes_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
189 hw_aes_process<10, false>(in, out, blocks, m_DK.data());
192void BOTAN_FN_ISA_HWAES AES_192::hw_aes_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
193 hw_aes_process<12, true>(in, out, blocks, m_EK.data());
196void BOTAN_FN_ISA_HWAES AES_192::hw_aes_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
197 hw_aes_process<12, false>(in, out, blocks, m_DK.data());
200void BOTAN_FN_ISA_HWAES AES_256::hw_aes_encrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
201 hw_aes_process<14, true>(in, out, blocks, m_EK.data());
204void BOTAN_FN_ISA_HWAES AES_256::hw_aes_decrypt_n(
const uint8_t in[], uint8_t out[],
size_t blocks)
const {
205 hw_aes_process<14, false>(in, out, blocks, m_DK.data());
208void BOTAN_FN_ISA_HWAES AES_128::hw_aes_key_schedule(std::span<const uint8_t> key) {
212 K.store_le(m_EK.data());
213 K = aes_128_round_key<0x01>(K, &m_EK[4]);
214 K = aes_128_round_key<0x02>(K, &m_EK[8]);
215 K = aes_128_round_key<0x04>(K, &m_EK[12]);
216 K = aes_128_round_key<0x08>(K, &m_EK[16]);
217 K = aes_128_round_key<0x10>(K, &m_EK[20]);
218 K = aes_128_round_key<0x20>(K, &m_EK[24]);
219 K = aes_128_round_key<0x40>(K, &m_EK[28]);
220 K = aes_128_round_key<0x80>(K, &m_EK[32]);
221 K = aes_128_round_key<0x1B>(K, &m_EK[36]);
222 aes_128_round_key<0x36>(K, &m_EK[40]);
224 hw_aes_decryption_keys(m_EK, m_DK);
227void BOTAN_FN_ISA_HWAES AES_192::hw_aes_key_schedule(std::span<const uint8_t> key) {
233 K1.store_le(&m_EK[4]);
235 aes_192_round_keys<0x01>(K0, K1, &m_EK[6]);
236 aes_192_round_keys<0x02>(K0, K1, &m_EK[12]);
237 aes_192_round_keys<0x04>(K0, K1, &m_EK[18]);
238 aes_192_round_keys<0x08>(K0, K1, &m_EK[24]);
239 aes_192_round_keys<0x10>(K0, K1, &m_EK[30]);
240 aes_192_round_keys<0x20>(K0, K1, &m_EK[36]);
241 aes_192_round_keys<0x40>(K0, K1, &m_EK[42]);
245 K0.store_le(&m_EK[48]);
247 hw_aes_decryption_keys(m_EK, m_DK);
250void BOTAN_FN_ISA_HWAES AES_256::hw_aes_key_schedule(std::span<const uint8_t> key) {
255 K0.store_le(m_EK.data());
256 K1.store_le(&m_EK[4]);
258 aes_256_round_keys<0x01>(K0, K1, &m_EK[8]);
259 aes_256_round_keys<0x02>(K0, K1, &m_EK[16]);
260 aes_256_round_keys<0x04>(K0, K1, &m_EK[24]);
261 aes_256_round_keys<0x08>(K0, K1, &m_EK[32]);
262 aes_256_round_keys<0x10>(K0, K1, &m_EK[40]);
263 aes_256_round_keys<0x20>(K0, K1, &m_EK[48]);
267 K0.store_le(&m_EK[56]);
269 hw_aes_decryption_keys(m_EK, m_DK);
void BOTAN_FN_ISA_SIMD_4X32 store_le(uint32_t out[4]) const noexcept
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)
SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 shift_elems_right() const noexcept
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 splat(uint32_t B) noexcept
SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 shift_elems_left() const noexcept
#define BOTAN_FORCE_INLINE
SIMD_4x32 BOTAN_FN_ISA_HWAES hw_aes_enc_last_round(SIMD_4x32 B, SIMD_4x32 K)
SIMD_4x32 BOTAN_FN_ISA_HWAES hw_aes_sbox_splat(SIMD_4x32 x, SIMD_4x32 k)
SIMD_4x32 BOTAN_FN_ISA_HWAES hw_aes_dec_round_key(SIMD_4x32 K)
SIMD_4x32 BOTAN_FN_ISA_HWAES hw_aes_dec_last_round(SIMD_4x32 B, SIMD_4x32 K)
SIMD_4x32 BOTAN_FN_ISA_HWAES hw_aes_enc_round(SIMD_4x32 B, SIMD_4x32 K)
constexpr bool HwAesKeyFirst
SIMD_4x32 BOTAN_FN_ISA_HWAES hw_aes_dec_round(SIMD_4x32 B, SIMD_4x32 K)
SIMD_4x32 BOTAN_FN_ISA_HWAES hw_aes_load_block(const uint8_t in[16])
std::vector< T, secure_allocator< T > > secure_vector
void BOTAN_FN_ISA_HWAES hw_aes_store_block(SIMD_4x32 B, uint8_t out[16])