Common: Put AES code within its own nested namespace

Prevents the functions from potentially clashing with other stuff in Common in the future
This commit is contained in:
Lioncash 2018-02-03 17:48:50 -05:00 committed by MerryMage
parent 40614202e7
commit d040920727
3 changed files with 24 additions and 24 deletions

View file

@ -16,18 +16,18 @@ namespace Dynarmic::BackendX64 {
using namespace Xbyak::util; using namespace Xbyak::util;
using AESFn = void(Common::AESState&, const Common::AESState&); using AESFn = void(Common::AES::State&, const Common::AES::State&);
static void EmitAESFunction(std::array<Argument, 3> args, EmitContext& ctx, BlockOfCode& code, static void EmitAESFunction(std::array<Argument, 3> args, EmitContext& ctx, BlockOfCode& code,
IR::Inst* inst, AESFn fn) { IR::Inst* inst, AESFn fn) {
constexpr u32 stack_space = static_cast<u32>(sizeof(Common::AESState)) * 2; constexpr u32 stack_space = static_cast<u32>(sizeof(Common::AES::State)) * 2;
const Xbyak::Xmm input = ctx.reg_alloc.UseXmm(args[0]); const Xbyak::Xmm input = ctx.reg_alloc.UseXmm(args[0]);
ctx.reg_alloc.EndOfAllocScope(); ctx.reg_alloc.EndOfAllocScope();
ctx.reg_alloc.HostCall(nullptr); ctx.reg_alloc.HostCall(nullptr);
code.sub(rsp, stack_space + ABI_SHADOW_SPACE); code.sub(rsp, stack_space + ABI_SHADOW_SPACE);
code.lea(code.ABI_PARAM1, ptr[rsp + ABI_SHADOW_SPACE]); code.lea(code.ABI_PARAM1, ptr[rsp + ABI_SHADOW_SPACE]);
code.lea(code.ABI_PARAM2, ptr[rsp + ABI_SHADOW_SPACE + sizeof(Common::AESState)]); code.lea(code.ABI_PARAM2, ptr[rsp + ABI_SHADOW_SPACE + sizeof(Common::AES::State)]);
code.movaps(xword[code.ABI_PARAM2], input); code.movaps(xword[code.ABI_PARAM2], input);
@ -44,13 +44,13 @@ static void EmitAESFunction(std::array<Argument, 3> args, EmitContext& ctx, Bloc
void EmitX64::EmitAESDecryptSingleRound(EmitContext& ctx, IR::Inst* inst) { void EmitX64::EmitAESDecryptSingleRound(EmitContext& ctx, IR::Inst* inst) {
auto args = ctx.reg_alloc.GetArgumentInfo(inst); auto args = ctx.reg_alloc.GetArgumentInfo(inst);
EmitAESFunction(args, ctx, code, inst, Common::DecryptSingleRound); EmitAESFunction(args, ctx, code, inst, Common::AES::DecryptSingleRound);
} }
void EmitX64::EmitAESEncryptSingleRound(EmitContext& ctx, IR::Inst* inst) { void EmitX64::EmitAESEncryptSingleRound(EmitContext& ctx, IR::Inst* inst) {
auto args = ctx.reg_alloc.GetArgumentInfo(inst); auto args = ctx.reg_alloc.GetArgumentInfo(inst);
EmitAESFunction(args, ctx, code, inst, Common::EncryptSingleRound); EmitAESFunction(args, ctx, code, inst, Common::AES::EncryptSingleRound);
} }
void EmitX64::EmitAESInverseMixColumns(EmitContext& ctx, IR::Inst* inst) { void EmitX64::EmitAESInverseMixColumns(EmitContext& ctx, IR::Inst* inst) {
@ -64,13 +64,13 @@ void EmitX64::EmitAESInverseMixColumns(EmitContext& ctx, IR::Inst* inst) {
ctx.reg_alloc.DefineValue(inst, result); ctx.reg_alloc.DefineValue(inst, result);
} else { } else {
EmitAESFunction(args, ctx, code, inst, Common::InverseMixColumns); EmitAESFunction(args, ctx, code, inst, Common::AES::InverseMixColumns);
} }
} }
void EmitX64::EmitAESMixColumns(EmitContext& ctx, IR::Inst* inst) { void EmitX64::EmitAESMixColumns(EmitContext& ctx, IR::Inst* inst) {
auto args = ctx.reg_alloc.GetArgumentInfo(inst); auto args = ctx.reg_alloc.GetArgumentInfo(inst);
EmitAESFunction(args, ctx, code, inst, Common::MixColumns); EmitAESFunction(args, ctx, code, inst, Common::AES::MixColumns);
} }
} // namespace Dynarmic::BackendX64 } // namespace Dynarmic::BackendX64

View file

@ -9,7 +9,7 @@
#include "common/aes.h" #include "common/aes.h"
#include "common/common_types.h" #include "common/common_types.h"
namespace Dynarmic::Common { namespace Dynarmic::Common::AES {
using SubstitutionTable = std::array<u8, 256>; using SubstitutionTable = std::array<u8, 256>;
@ -69,7 +69,7 @@ static constexpr u8 Multiply(u8 x, u8 y) {
((y >> 4 & 1) * xtime(xtime(xtime(xtime(x)))))); ((y >> 4 & 1) * xtime(xtime(xtime(xtime(x))))));
} }
static void ShiftRows(AESState& out_state, const AESState& state) { static void ShiftRows(State& out_state, const State& state) {
// Move zeroth row over // Move zeroth row over
out_state[0] = state[0]; out_state[0] = state[0];
out_state[4] = state[4]; out_state[4] = state[4];
@ -100,7 +100,7 @@ static void ShiftRows(AESState& out_state, const AESState& state) {
out_state[7] = temp; out_state[7] = temp;
} }
static void InverseShiftRows(AESState& out_state, const AESState& state) { static void InverseShiftRows(State& out_state, const State& state) {
// Move zeroth row over // Move zeroth row over
out_state[0] = state[0]; out_state[0] = state[0];
out_state[4] = state[4]; out_state[4] = state[4];
@ -131,7 +131,7 @@ static void InverseShiftRows(AESState& out_state, const AESState& state) {
out_state[15] = temp; out_state[15] = temp;
} }
static void SubBytes(AESState& state, const SubstitutionTable& table) { static void SubBytes(State& state, const SubstitutionTable& table) {
for (size_t i = 0; i < 4; i++) { for (size_t i = 0; i < 4; i++) {
for (size_t j = 0; j < 4; j++) { for (size_t j = 0; j < 4; j++) {
state[4 * i + j] = table[state[4 * i + j]]; state[4 * i + j] = table[state[4 * i + j]];
@ -139,17 +139,17 @@ static void SubBytes(AESState& state, const SubstitutionTable& table) {
} }
} }
void DecryptSingleRound(AESState& out_state, const AESState& state) { void DecryptSingleRound(State& out_state, const State& state) {
InverseShiftRows(out_state, state); InverseShiftRows(out_state, state);
SubBytes(out_state, inverse_substitution_box); SubBytes(out_state, inverse_substitution_box);
} }
void EncryptSingleRound(AESState& out_state, const AESState& state) { void EncryptSingleRound(State& out_state, const State& state) {
ShiftRows(out_state, state); ShiftRows(out_state, state);
SubBytes(out_state, substitution_box); SubBytes(out_state, substitution_box);
} }
void MixColumns(AESState& out_state, const AESState& state) { void MixColumns(State& out_state, const State& state) {
for (size_t i = 0; i < out_state.size(); i += 4) { for (size_t i = 0; i < out_state.size(); i += 4) {
const u8 a = state[i]; const u8 a = state[i];
const u8 b = state[i + 1]; const u8 b = state[i + 1];
@ -165,7 +165,7 @@ void MixColumns(AESState& out_state, const AESState& state) {
} }
} }
void InverseMixColumns(AESState& out_state, const AESState& state) { void InverseMixColumns(State& out_state, const State& state) {
for (size_t i = 0; i < out_state.size(); i += 4) { for (size_t i = 0; i < out_state.size(); i += 4) {
const u8 a = state[i]; const u8 a = state[i];
const u8 b = state[i + 1]; const u8 b = state[i + 1];
@ -179,4 +179,4 @@ void InverseMixColumns(AESState& out_state, const AESState& state) {
} }
} }
} // namespace Dynarmic::Common } // namespace Dynarmic::Common::AES

View file

@ -9,15 +9,15 @@
#include <array> #include <array>
#include "common/common_types.h" #include "common/common_types.h"
namespace Dynarmic::Common { namespace Dynarmic::Common::AES {
using AESState = std::array<u8, 16>; using State = std::array<u8, 16>;
// Assumes the state has already been XORed by the round key. // Assumes the state has already been XORed by the round key.
void DecryptSingleRound(AESState& out_state, const AESState& state); void DecryptSingleRound(State& out_state, const State& state);
void EncryptSingleRound(AESState& out_state, const AESState& state); void EncryptSingleRound(State& out_state, const State& state);
void MixColumns(AESState& out_state, const AESState& state); void MixColumns(State& out_state, const State& state);
void InverseMixColumns(AESState& out_state, const AESState& state); void InverseMixColumns(State& out_state, const State& state);
} // namespace Dynarmic::Common } // namespace Dynarmic::Common::AES