common: Move all cryptographic function to common/crypto

This commit is contained in:
MerryMage 2018-07-29 08:48:28 +01:00
parent 5dc23e49d7
commit f9c6d5e1a0
10 changed files with 36 additions and 34 deletions

View file

@ -8,14 +8,16 @@ add_library(dynarmic
../include/dynarmic/A64/config.h ../include/dynarmic/A64/config.h
../include/dynarmic/A64/exclusive_monitor.h ../include/dynarmic/A64/exclusive_monitor.h
common/address_range.h common/address_range.h
common/aes.cpp
common/aes.h
common/assert.h common/assert.h
common/bit_util.h common/bit_util.h
common/cast_util.h common/cast_util.h
common/common_types.h common/common_types.h
common/crc32.cpp common/crypto/aes.cpp
common/crc32.h common/crypto/aes.h
common/crypto/crc32.cpp
common/crypto/crc32.h
common/crypto/sm4.cpp
common/crypto/sm4.h
common/fp/fpcr.h common/fp/fpcr.h
common/fp/fpsr.h common/fp/fpsr.h
common/fp/fused.cpp common/fp/fused.cpp
@ -67,8 +69,6 @@ add_library(dynarmic
common/mp/vllift.h common/mp/vllift.h
common/safe_ops.h common/safe_ops.h
common/scope_exit.h common/scope_exit.h
common/sm4.cpp
common/sm4.h
common/string_util.h common/string_util.h
common/u128.cpp common/u128.cpp
common/u128.h common/u128.h

View file

@ -7,27 +7,28 @@
#include "backend_x64/abi.h" #include "backend_x64/abi.h"
#include "backend_x64/block_of_code.h" #include "backend_x64/block_of_code.h"
#include "backend_x64/emit_x64.h" #include "backend_x64/emit_x64.h"
#include "common/aes.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/crypto/aes.h"
#include "frontend/ir/microinstruction.h" #include "frontend/ir/microinstruction.h"
#include "frontend/ir/opcodes.h" #include "frontend/ir/opcodes.h"
namespace Dynarmic::BackendX64 { namespace Dynarmic::BackendX64 {
using namespace Xbyak::util; using namespace Xbyak::util;
namespace AES = Common::Crypto::AES;
using AESFn = void(Common::AES::State&, const Common::AES::State&); using AESFn = void(AES::State&, const 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::AES::State)) * 2; constexpr u32 stack_space = static_cast<u32>(sizeof(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::AES::State)]); code.lea(code.ABI_PARAM2, ptr[rsp + ABI_SHADOW_SPACE + sizeof(AES::State)]);
code.movaps(xword[code.ABI_PARAM2], input); code.movaps(xword[code.ABI_PARAM2], input);
@ -44,13 +45,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::AES::DecryptSingleRound); EmitAESFunction(args, ctx, code, inst, 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::AES::EncryptSingleRound); EmitAESFunction(args, ctx, code, inst, AES::EncryptSingleRound);
} }
void EmitX64::EmitAESInverseMixColumns(EmitContext& ctx, IR::Inst* inst) { void EmitX64::EmitAESInverseMixColumns(EmitContext& ctx, IR::Inst* inst) {
@ -63,13 +64,13 @@ void EmitX64::EmitAESInverseMixColumns(EmitContext& ctx, IR::Inst* inst) {
ctx.reg_alloc.DefineValue(inst, data); ctx.reg_alloc.DefineValue(inst, data);
} else { } else {
EmitAESFunction(args, ctx, code, inst, Common::AES::InverseMixColumns); EmitAESFunction(args, ctx, code, inst, 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::AES::MixColumns); EmitAESFunction(args, ctx, code, inst, AES::MixColumns);
} }
} // namespace Dynarmic::BackendX64 } // namespace Dynarmic::BackendX64

View file

@ -10,13 +10,14 @@
#include "backend_x64/block_of_code.h" #include "backend_x64/block_of_code.h"
#include "backend_x64/emit_x64.h" #include "backend_x64/emit_x64.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/crc32.h" #include "common/crypto/crc32.h"
#include "frontend/ir/microinstruction.h" #include "frontend/ir/microinstruction.h"
#include "frontend/ir/opcodes.h" #include "frontend/ir/opcodes.h"
namespace Dynarmic::BackendX64 { namespace Dynarmic::BackendX64 {
using namespace Xbyak::util; using namespace Xbyak::util;
namespace CRC32 = Common::Crypto::CRC32;
static void EmitCRC32Castagnoli(BlockOfCode& code, EmitContext& ctx, IR::Inst* inst, const int data_size) { static void EmitCRC32Castagnoli(BlockOfCode& code, EmitContext& ctx, IR::Inst* inst, const int data_size) {
auto args = ctx.reg_alloc.GetArgumentInfo(inst); auto args = ctx.reg_alloc.GetArgumentInfo(inst);
@ -29,7 +30,7 @@ static void EmitCRC32Castagnoli(BlockOfCode& code, EmitContext& ctx, IR::Inst* i
} else { } else {
ctx.reg_alloc.HostCall(inst, args[0], args[1], {}); ctx.reg_alloc.HostCall(inst, args[0], args[1], {});
code.mov(code.ABI_PARAM3, data_size / CHAR_BIT); code.mov(code.ABI_PARAM3, data_size / CHAR_BIT);
code.CallFunction(&Common::ComputeCRC32Castagnoli); code.CallFunction(&CRC32::ComputeCRC32Castagnoli);
} }
} }
@ -38,7 +39,7 @@ static void EmitCRC32ISO(BlockOfCode& code, EmitContext& ctx, IR::Inst* inst, co
ctx.reg_alloc.HostCall(inst, args[0], args[1], {}); ctx.reg_alloc.HostCall(inst, args[0], args[1], {});
code.mov(code.ABI_PARAM3, data_size / CHAR_BIT); code.mov(code.ABI_PARAM3, data_size / CHAR_BIT);
code.CallFunction(&Common::ComputeCRC32ISO); code.CallFunction(&CRC32::ComputeCRC32ISO);
} }
void EmitX64::EmitCRC32Castagnoli8(EmitContext& ctx, IR::Inst* inst) { void EmitX64::EmitCRC32Castagnoli8(EmitContext& ctx, IR::Inst* inst) {

View file

@ -7,7 +7,7 @@
#include "backend_x64/block_of_code.h" #include "backend_x64/block_of_code.h"
#include "backend_x64/emit_x64.h" #include "backend_x64/emit_x64.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/sm4.h" #include "common/crypto/sm4.h"
#include "frontend/ir/microinstruction.h" #include "frontend/ir/microinstruction.h"
#include "frontend/ir/opcodes.h" #include "frontend/ir/opcodes.h"
@ -17,7 +17,7 @@ void EmitX64::EmitSM4AccessSubstitutionBox(EmitContext& ctx, IR::Inst* inst) {
auto args = ctx.reg_alloc.GetArgumentInfo(inst); auto args = ctx.reg_alloc.GetArgumentInfo(inst);
ctx.reg_alloc.HostCall(inst, args[0]); ctx.reg_alloc.HostCall(inst, args[0]);
code.CallFunction(&Common::SM4::AccessSubstitutionBox); code.CallFunction(&Common::Crypto::SM4::AccessSubstitutionBox);
} }
} // namespace Dynarmic::BackendX64 } // namespace Dynarmic::BackendX64

View file

@ -6,10 +6,10 @@
#include <array> #include <array>
#include "common/aes.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/crypto/aes.h"
namespace Dynarmic::Common::AES { namespace Dynarmic::Common::Crypto::AES {
using SubstitutionTable = std::array<u8, 256>; using SubstitutionTable = std::array<u8, 256>;
@ -179,4 +179,4 @@ void InverseMixColumns(State& out_state, const State& state) {
} }
} }
} // namespace Dynarmic::Common::AES } // namespace Dynarmic::Common::Crypto::AES

View file

@ -9,7 +9,7 @@
#include <array> #include <array>
#include "common/common_types.h" #include "common/common_types.h"
namespace Dynarmic::Common::AES { namespace Dynarmic::Common::Crypto::AES {
using State = std::array<u8, 16>; using State = std::array<u8, 16>;
@ -20,4 +20,4 @@ void EncryptSingleRound(State& out_state, const State& state);
void MixColumns(State& out_state, const State& state); void MixColumns(State& out_state, const State& state);
void InverseMixColumns(State& out_state, const State& state); void InverseMixColumns(State& out_state, const State& state);
} // namespace Dynarmic::Common::AES } // namespace Dynarmic::Common::Crypto::AES

View file

@ -7,9 +7,9 @@
#include <array> #include <array>
#include "common/common_types.h" #include "common/common_types.h"
#include "common/crc32.h" #include "common/crypto/crc32.h"
namespace Dynarmic::Common { namespace Dynarmic::Common::Crypto::CRC32 {
using CRC32Table = std::array<u32, 256>; using CRC32Table = std::array<u32, 256>;
@ -167,4 +167,4 @@ u32 ComputeCRC32ISO(u32 crc, u64 value, int length) {
return ComputeCRC32(iso_table, crc, value, length); return ComputeCRC32(iso_table, crc, value, length);
} }
} // namespace Dynarmic::Common } // namespace Dynarmic::Common::Crypto::CRC32

View file

@ -8,7 +8,7 @@
#include "common/common_types.h" #include "common/common_types.h"
namespace Dynarmic::Common { namespace Dynarmic::Common::Crypto::CRC32 {
/** /**
* Computes a CRC32 value using Castagnoli polynomial (0x1EDC6F41). * Computes a CRC32 value using Castagnoli polynomial (0x1EDC6F41).
@ -38,4 +38,4 @@ u32 ComputeCRC32Castagnoli(u32 crc, u64 value, int length);
*/ */
u32 ComputeCRC32ISO(u32 crc, u64 value, int length); u32 ComputeCRC32ISO(u32 crc, u64 value, int length);
} // namespace Dynarmic::Common } // namespace Dynarmic::Common::Crypto::CRC32

View file

@ -6,10 +6,10 @@
#include <array> #include <array>
#include "common/sm4.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/crypto/sm4.h"
namespace Dynarmic::Common::SM4 { namespace Dynarmic::Common::Crypto::SM4 {
using SubstitutionTable = std::array<u8, 256>; using SubstitutionTable = std::array<u8, 256>;
@ -52,4 +52,4 @@ u8 AccessSubstitutionBox(u8 index) {
return substitution_box[index]; return substitution_box[index];
} }
} // namespace Dynarmic::Common::SM4 } // namespace Dynarmic::Common::Crypto::SM4

View file

@ -8,8 +8,8 @@
#include "common/common_types.h" #include "common/common_types.h"
namespace Dynarmic::Common::SM4 { namespace Dynarmic::Common::Crypto::SM4 {
u8 AccessSubstitutionBox(u8 index); u8 AccessSubstitutionBox(u8 index);
} // namespace Dynarmic::Common::SM4 } // namespace Dynarmic::Common::Crypto::SM4