From 259237ce583f48ffa4b1a03ed098de3ea1c27fd0 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Sun, 29 Jul 2018 08:48:28 +0100 Subject: [PATCH] common: Move all cryptographic function to common/crypto --- src/CMakeLists.txt | 12 ++++++------ src/backend_x64/emit_x64_aes.cpp | 17 +++++++++-------- src/backend_x64/emit_x64_crc32.cpp | 7 ++++--- src/backend_x64/emit_x64_sm4.cpp | 4 ++-- src/common/{ => crypto}/aes.cpp | 6 +++--- src/common/{ => crypto}/aes.h | 4 ++-- src/common/{ => crypto}/crc32.cpp | 6 +++--- src/common/{ => crypto}/crc32.h | 4 ++-- src/common/{ => crypto}/sm4.cpp | 6 +++--- src/common/{ => crypto}/sm4.h | 4 ++-- 10 files changed, 36 insertions(+), 34 deletions(-) rename src/common/{ => crypto}/aes.cpp (98%) rename src/common/{ => crypto}/aes.h (87%) rename src/common/{ => crypto}/crc32.cpp (98%) rename src/common/{ => crypto}/crc32.h (92%) rename src/common/{ => crypto}/sm4.cpp (94%) rename src/common/{ => crypto}/sm4.h (77%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2e18b2cf..14acf9ff 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,14 +8,16 @@ add_library(dynarmic ../include/dynarmic/A64/config.h ../include/dynarmic/A64/exclusive_monitor.h common/address_range.h - common/aes.cpp - common/aes.h common/assert.h common/bit_util.h common/cast_util.h common/common_types.h - common/crc32.cpp - common/crc32.h + common/crypto/aes.cpp + 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/fpsr.h common/fp/fused.cpp @@ -67,8 +69,6 @@ add_library(dynarmic common/mp/vllift.h common/safe_ops.h common/scope_exit.h - common/sm4.cpp - common/sm4.h common/string_util.h common/u128.cpp common/u128.h diff --git a/src/backend_x64/emit_x64_aes.cpp b/src/backend_x64/emit_x64_aes.cpp index e24b4938..175ab111 100644 --- a/src/backend_x64/emit_x64_aes.cpp +++ b/src/backend_x64/emit_x64_aes.cpp @@ -7,27 +7,28 @@ #include "backend_x64/abi.h" #include "backend_x64/block_of_code.h" #include "backend_x64/emit_x64.h" -#include "common/aes.h" #include "common/common_types.h" +#include "common/crypto/aes.h" #include "frontend/ir/microinstruction.h" #include "frontend/ir/opcodes.h" namespace Dynarmic::BackendX64 { 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 args, EmitContext& ctx, BlockOfCode& code, IR::Inst* inst, AESFn fn) { - constexpr u32 stack_space = static_cast(sizeof(Common::AES::State)) * 2; + constexpr u32 stack_space = static_cast(sizeof(AES::State)) * 2; const Xbyak::Xmm input = ctx.reg_alloc.UseXmm(args[0]); ctx.reg_alloc.EndOfAllocScope(); ctx.reg_alloc.HostCall(nullptr); code.sub(rsp, stack_space + 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); @@ -44,13 +45,13 @@ static void EmitAESFunction(std::array args, EmitContext& ctx, Bloc void EmitX64::EmitAESDecryptSingleRound(EmitContext& ctx, IR::Inst* 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) { 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) { @@ -63,13 +64,13 @@ void EmitX64::EmitAESInverseMixColumns(EmitContext& ctx, IR::Inst* inst) { ctx.reg_alloc.DefineValue(inst, data); } else { - EmitAESFunction(args, ctx, code, inst, Common::AES::InverseMixColumns); + EmitAESFunction(args, ctx, code, inst, AES::InverseMixColumns); } } void EmitX64::EmitAESMixColumns(EmitContext& ctx, IR::Inst* 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 diff --git a/src/backend_x64/emit_x64_crc32.cpp b/src/backend_x64/emit_x64_crc32.cpp index 71b6347e..e24a72ed 100644 --- a/src/backend_x64/emit_x64_crc32.cpp +++ b/src/backend_x64/emit_x64_crc32.cpp @@ -10,13 +10,14 @@ #include "backend_x64/block_of_code.h" #include "backend_x64/emit_x64.h" #include "common/common_types.h" -#include "common/crc32.h" +#include "common/crypto/crc32.h" #include "frontend/ir/microinstruction.h" #include "frontend/ir/opcodes.h" namespace Dynarmic::BackendX64 { using namespace Xbyak::util; +namespace CRC32 = Common::Crypto::CRC32; static void EmitCRC32Castagnoli(BlockOfCode& code, EmitContext& ctx, IR::Inst* inst, const int data_size) { auto args = ctx.reg_alloc.GetArgumentInfo(inst); @@ -29,7 +30,7 @@ static void EmitCRC32Castagnoli(BlockOfCode& code, EmitContext& ctx, IR::Inst* i } else { ctx.reg_alloc.HostCall(inst, args[0], args[1], {}); 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], {}); 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) { diff --git a/src/backend_x64/emit_x64_sm4.cpp b/src/backend_x64/emit_x64_sm4.cpp index a0165ac4..ca589893 100644 --- a/src/backend_x64/emit_x64_sm4.cpp +++ b/src/backend_x64/emit_x64_sm4.cpp @@ -7,7 +7,7 @@ #include "backend_x64/block_of_code.h" #include "backend_x64/emit_x64.h" #include "common/common_types.h" -#include "common/sm4.h" +#include "common/crypto/sm4.h" #include "frontend/ir/microinstruction.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); ctx.reg_alloc.HostCall(inst, args[0]); - code.CallFunction(&Common::SM4::AccessSubstitutionBox); + code.CallFunction(&Common::Crypto::SM4::AccessSubstitutionBox); } } // namespace Dynarmic::BackendX64 diff --git a/src/common/aes.cpp b/src/common/crypto/aes.cpp similarity index 98% rename from src/common/aes.cpp rename to src/common/crypto/aes.cpp index d6a8f5ab..13cbde1b 100644 --- a/src/common/aes.cpp +++ b/src/common/crypto/aes.cpp @@ -6,10 +6,10 @@ #include -#include "common/aes.h" #include "common/common_types.h" +#include "common/crypto/aes.h" -namespace Dynarmic::Common::AES { +namespace Dynarmic::Common::Crypto::AES { using SubstitutionTable = std::array; @@ -179,4 +179,4 @@ void InverseMixColumns(State& out_state, const State& state) { } } -} // namespace Dynarmic::Common::AES +} // namespace Dynarmic::Common::Crypto::AES diff --git a/src/common/aes.h b/src/common/crypto/aes.h similarity index 87% rename from src/common/aes.h rename to src/common/crypto/aes.h index 1f58d4b4..d6ff14b3 100644 --- a/src/common/aes.h +++ b/src/common/crypto/aes.h @@ -9,7 +9,7 @@ #include #include "common/common_types.h" -namespace Dynarmic::Common::AES { +namespace Dynarmic::Common::Crypto::AES { using State = std::array; @@ -20,4 +20,4 @@ void EncryptSingleRound(State& out_state, const State& state); void MixColumns(State& out_state, const State& state); void InverseMixColumns(State& out_state, const State& state); -} // namespace Dynarmic::Common::AES +} // namespace Dynarmic::Common::Crypto::AES diff --git a/src/common/crc32.cpp b/src/common/crypto/crc32.cpp similarity index 98% rename from src/common/crc32.cpp rename to src/common/crypto/crc32.cpp index f88adf95..eb2522b2 100644 --- a/src/common/crc32.cpp +++ b/src/common/crypto/crc32.cpp @@ -7,9 +7,9 @@ #include #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; @@ -167,4 +167,4 @@ u32 ComputeCRC32ISO(u32 crc, u64 value, int length) { return ComputeCRC32(iso_table, crc, value, length); } -} // namespace Dynarmic::Common +} // namespace Dynarmic::Common::Crypto::CRC32 diff --git a/src/common/crc32.h b/src/common/crypto/crc32.h similarity index 92% rename from src/common/crc32.h rename to src/common/crypto/crc32.h index d497eebc..6e9bd3d3 100644 --- a/src/common/crc32.h +++ b/src/common/crypto/crc32.h @@ -8,7 +8,7 @@ #include "common/common_types.h" -namespace Dynarmic::Common { +namespace Dynarmic::Common::Crypto::CRC32 { /** * 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); -} // namespace Dynarmic::Common +} // namespace Dynarmic::Common::Crypto::CRC32 diff --git a/src/common/sm4.cpp b/src/common/crypto/sm4.cpp similarity index 94% rename from src/common/sm4.cpp rename to src/common/crypto/sm4.cpp index cf1e4361..d2c144f3 100644 --- a/src/common/sm4.cpp +++ b/src/common/crypto/sm4.cpp @@ -6,10 +6,10 @@ #include -#include "common/sm4.h" #include "common/common_types.h" +#include "common/crypto/sm4.h" -namespace Dynarmic::Common::SM4 { +namespace Dynarmic::Common::Crypto::SM4 { using SubstitutionTable = std::array; @@ -52,4 +52,4 @@ u8 AccessSubstitutionBox(u8 index) { return substitution_box[index]; } -} // namespace Dynarmic::Common::SM4 +} // namespace Dynarmic::Common::Crypto::SM4 diff --git a/src/common/sm4.h b/src/common/crypto/sm4.h similarity index 77% rename from src/common/sm4.h rename to src/common/crypto/sm4.h index 89e89cec..d6e8d371 100644 --- a/src/common/sm4.h +++ b/src/common/crypto/sm4.h @@ -8,8 +8,8 @@ #include "common/common_types.h" -namespace Dynarmic::Common::SM4 { +namespace Dynarmic::Common::Crypto::SM4 { u8 AccessSubstitutionBox(u8 index); -} // namespace Dynarmic::Common::SM4 +} // namespace Dynarmic::Common::Crypto::SM4