#include #include #include #include #include #include #include #include #include namespace hex::plugin::hashes { namespace { using namespace wolv::literals; std::vector hashProviderRegion(const Region& region, prv::Provider *provider, auto &hashFunction) { auto reader = prv::ProviderReader(provider); reader.seek(region.getStartAddress()); reader.setEndAddress(region.getEndAddress()); for (u64 address = region.getStartAddress(); address < region.getEndAddress(); address += 1_MiB) { u64 readSize = std::min(1_MiB, (region.getEndAddress() - address) + 1); auto data = reader.read(address, readSize); hashFunction->TransformBytes({ data.begin(), data.end() }, address - region.getStartAddress(), data.size()); } auto result = hashFunction->TransformFinal(); auto bytes = result->GetBytes(); return { bytes.begin(), bytes.end() }; } } class HashMD5 : public ContentRegistry::Hashes::Hash { public: HashMD5() : Hash("hex.hashes.hash.md5") {} Function create(std::string name) override { return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector { auto array = crypt::md5(provider, region.address, region.size); return { array.begin(), array.end() }; }); } [[nodiscard]] nlohmann::json store() const override { return { }; } void load(const nlohmann::json &) override {} }; class HashSHA1 : public ContentRegistry::Hashes::Hash { public: HashSHA1() : Hash("hex.hashes.hash.sha1") {} Function create(std::string name) override { return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector { auto array = crypt::sha1(provider, region.address, region.size); return { array.begin(), array.end() }; }); } [[nodiscard]] nlohmann::json store() const override { return { }; } void load(const nlohmann::json &) override {} }; class HashSHA224 : public ContentRegistry::Hashes::Hash { public: HashSHA224() : Hash("hex.hashes.hash.sha224") {} Function create(std::string name) override { return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector { auto array = crypt::sha224(provider, region.address, region.size); return { array.begin(), array.end() }; }); } [[nodiscard]] nlohmann::json store() const override { return { }; } void load(const nlohmann::json &) override {} }; class HashSHA256 : public ContentRegistry::Hashes::Hash { public: HashSHA256() : Hash("hex.hashes.hash.sha256") {} Function create(std::string name) override { return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector { auto array = crypt::sha256(provider, region.address, region.size); return { array.begin(), array.end() }; }); } [[nodiscard]] nlohmann::json store() const override { return { }; } void load(const nlohmann::json &) override {} }; class HashSHA384 : public ContentRegistry::Hashes::Hash { public: HashSHA384() : Hash("hex.hashes.hash.sha384") {} Function create(std::string name) override { return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector { auto array = crypt::sha384(provider, region.address, region.size); return { array.begin(), array.end() }; }); } [[nodiscard]] nlohmann::json store() const override { return { }; } void load(const nlohmann::json &) override {} }; class HashSHA512 : public ContentRegistry::Hashes::Hash { public: HashSHA512() : Hash("hex.hashes.hash.sha512") {} Function create(std::string name) override { return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector { auto array = crypt::sha512(provider, region.address, region.size); return { array.begin(), array.end() }; }); } [[nodiscard]] nlohmann::json store() const override { return { }; } void load(const nlohmann::json &) override {} }; template class HashCRC : public ContentRegistry::Hashes::Hash { public: using CRCFunction = T(*)(prv::Provider*&, u64, size_t, u32, u32, u32, bool, bool); HashCRC(const std::string &name, const CRCFunction &crcFunction, u32 polynomial, u32 initialValue, u32 xorOut, bool reflectIn = false, bool reflectOut = false) : Hash(name), m_crcFunction(crcFunction), m_polynomial(polynomial), m_initialValue(initialValue), m_xorOut(xorOut), m_reflectIn(reflectIn), m_reflectOut(reflectOut) {} void draw() override { ImGuiExt::InputHexadecimal("hex.hashes.hash.common.poly"_lang, &this->m_polynomial); ImGuiExt::InputHexadecimal("hex.hashes.hash.common.iv"_lang, &this->m_initialValue); ImGuiExt::InputHexadecimal("hex.hashes.hash.common.xor_out"_lang, &this->m_xorOut); ImGui::NewLine(); ImGui::Checkbox("hex.hashes.hash.common.refl_in"_lang, &this->m_reflectIn); ImGui::Checkbox("hex.hashes.hash.common.refl_out"_lang, &this->m_reflectOut); } Function create(std::string name) override { return Hash::create(name, [hash = *this](const Region& region, prv::Provider *provider) -> std::vector { auto result = hash.m_crcFunction(provider, region.address, region.size, hash.m_polynomial, hash.m_initialValue, hash.m_xorOut, hash.m_reflectIn, hash.m_reflectOut); std::vector bytes(sizeof(result), 0x00); std::memcpy(bytes.data(), &result, bytes.size()); if constexpr (std::endian::native == std::endian::little) std::reverse(bytes.begin(), bytes.end()); return bytes; }); } [[nodiscard]] nlohmann::json store() const override { nlohmann::json result; result["polynomial"] = this->m_polynomial; result["initialValue"] = this->m_initialValue; result["xorOut"] = this->m_xorOut; result["reflectIn"] = this->m_reflectIn; result["reflectOut"] = this->m_reflectOut; return result; } void load(const nlohmann::json &json) override { try { this->m_polynomial = json.at("polynomial"); this->m_initialValue = json.at("initialValue"); this->m_xorOut = json.at("xorOut"); this->m_reflectIn = json.at("reflectIn"); this->m_reflectOut = json.at("reflectOut"); } catch (std::exception&) { } } private: CRCFunction m_crcFunction; u32 m_polynomial; u32 m_initialValue; u32 m_xorOut; bool m_reflectIn = false, m_reflectOut = false; }; class HashBasic : public ContentRegistry::Hashes::Hash { public: using FactoryFunction = IHash(*)(); explicit HashBasic(FactoryFunction function) : Hash(function()->GetName()), m_factoryFunction(function) {} Function create(std::string name) override { return Hash::create(name, [hash = *this](const Region& region, prv::Provider *provider) -> std::vector { IHash hashFunction = hash.m_factoryFunction(); hashFunction->Initialize(); return hashProviderRegion(region, provider, hashFunction); }); } [[nodiscard]] nlohmann::json store() const override { return { }; } void load(const nlohmann::json &) override {} private: FactoryFunction m_factoryFunction; }; class HashWithKey : public ContentRegistry::Hashes::Hash { public: using FactoryFunction = IHashWithKey(*)(); explicit HashWithKey(FactoryFunction function) : Hash(function()->GetName()), m_factoryFunction(function) {} void draw() override { ImGui::InputText("hex.hashes.hash.common.key"_lang, this->m_key, ImGuiInputTextFlags_CharsHexadecimal); } Function create(std::string name) override { return Hash::create(name, [hash = *this, key = hex::parseByteString(this->m_key)](const Region& region, prv::Provider *provider) -> std::vector { IHashWithKey hashFunction = hash.m_factoryFunction(); hashFunction->Initialize(); hashFunction->SetKey(key); return hashProviderRegion(region, provider, hashFunction); }); } [[nodiscard]] nlohmann::json store() const override { return { }; } void load(const nlohmann::json &) override {} private: FactoryFunction m_factoryFunction; std::string m_key; }; class HashInitialValue : public ContentRegistry::Hashes::Hash { public: using FactoryFunction = IHash(*)(const Int32); explicit HashInitialValue(FactoryFunction function) : Hash(function(0)->GetName()), m_factoryFunction(function) {} void draw() override { ImGuiExt::InputHexadecimal("hex.hashes.hash.common.iv"_lang, &this->m_initialValue); } Function create(std::string name) override { return Hash::create(name, [hash = *this](const Region& region, prv::Provider *provider) -> std::vector { IHash hashFunction = hash.m_factoryFunction(Int32(hash.m_initialValue)); hashFunction->Initialize(); return hashProviderRegion(region, provider, hashFunction); }); } [[nodiscard]] nlohmann::json store() const override { return { }; } void load(const nlohmann::json &) override {} private: FactoryFunction m_factoryFunction; u32 m_initialValue = 0x00; }; class HashTiger : public ContentRegistry::Hashes::Hash { public: using FactoryFunction = IHash(*)(const Int32, const HashRounds&); explicit HashTiger(std::string name, FactoryFunction function) : Hash(std::move(name)), m_factoryFunction(function) {} void draw() override { ImGui::Combo("hex.hashes.hash.common.size"_lang, &this->m_hashSize, "128 Bits\0" "160 Bits\0" "192 Bits\0"); ImGui::Combo("hex.hashes.hash.common.rounds"_lang, &this->m_hashRounds, "3 Rounds\0" "4 Rounds\0" "5 Rounds\0" "8 Rounds\0"); } Function create(std::string name) override { return Hash::create(name, [hash = *this](const Region& region, prv::Provider *provider) -> std::vector { Int32 hashSize = 16; switch (hash.m_hashSize) { case 0: hashSize = 16; break; case 1: hashSize = 20; break; case 2: hashSize = 24; break; } HashRounds hashRounds = HashRounds::Rounds3; switch (hash.m_hashRounds) { case 0: hashRounds = HashRounds::Rounds3; break; case 1: hashRounds = HashRounds::Rounds4; break; case 2: hashRounds = HashRounds::Rounds5; break; case 3: hashRounds = HashRounds::Rounds8; break; } IHash hashFunction = hash.m_factoryFunction(hashSize, hashRounds); hashFunction->Initialize(); return hashProviderRegion(region, provider, hashFunction); }); } [[nodiscard]] nlohmann::json store() const override { return { }; } void load(const nlohmann::json &) override {} private: FactoryFunction m_factoryFunction; int m_hashSize = 0, m_hashRounds = 0; }; template class HashBlake2 : public ContentRegistry::Hashes::Hash { public: using FactoryFunction = IHash(*)(T1 a_Config, T2 a_TreeConfig); explicit HashBlake2(std::string name, FactoryFunction function) : Hash(std::move(name)), m_factoryFunction(function) {} void draw() override { ImGui::InputText("hex.hashes.hash.common.salt"_lang, this->m_salt, ImGuiInputTextFlags_CharsHexadecimal); ImGui::InputText("hex.hashes.hash.common.key"_lang, this->m_key, ImGuiInputTextFlags_CharsHexadecimal); ImGui::InputText("hex.hashes.hash.common.personalization"_lang, this->m_personalization, ImGuiInputTextFlags_CharsHexadecimal); ImGui::Combo("hex.hashes.hash.common.size"_lang, &this->m_hashSize, "128 Bits\0" "160 Bits\0" "192 Bits\0" "224 Bits\0" "256 Bits\0" "288 Bits\0" "384 Bits\0" "512 Bits\0"); } Function create(std::string name) override { return Hash::create(name, [hash = *this, key = hex::parseByteString(this->m_key), salt = hex::parseByteString(this->m_salt), personalization = hex::parseByteString(this->m_personalization)](const Region& region, prv::Provider *provider) -> std::vector { u32 hashSize = 16; switch (hash.m_hashSize) { case 0: hashSize = 16; break; case 1: hashSize = 20; break; case 2: hashSize = 24; break; case 3: hashSize = 28; break; case 4: hashSize = 32; break; case 5: hashSize = 36; break; case 6: hashSize = 48; break; case 7: hashSize = 64; break; } auto config = Config::GetDefaultConfig(); config->SetKey(key); config->SetSalt(salt); config->SetPersonalization(personalization); config->SetHashSize(hashSize); IHash hashFunction = hash.m_factoryFunction(config, nullptr); hashFunction->Initialize(); return hashProviderRegion(region, provider, hashFunction); }); } [[nodiscard]] nlohmann::json store() const override { return { }; } void load(const nlohmann::json &) override {} private: FactoryFunction m_factoryFunction; std::string m_salt, m_key, m_personalization; int m_hashSize = 0; }; void registerHashes() { ContentRegistry::Hashes::add(); ContentRegistry::Hashes::add(); ContentRegistry::Hashes::add(); ContentRegistry::Hashes::add(); ContentRegistry::Hashes::add(); ContentRegistry::Hashes::add(); ContentRegistry::Hashes::add>("hex.hashes.hash.crc8", crypt::crc8, 0x07, 0x0000, 0x0000); ContentRegistry::Hashes::add>("hex.hashes.hash.crc16", crypt::crc16, 0x8005, 0x0000, 0x0000); ContentRegistry::Hashes::add>("hex.hashes.hash.crc32", crypt::crc32, 0x04C1'1DB7, 0xFFFF'FFFF, 0xFFFF'FFFF, true, true); ContentRegistry::Hashes::add>("hex.hashes.hash.crc32mpeg", crypt::crc32, 0x04C1'1DB7, 0xFFFF'FFFF, 0x0000'0000, false, false); ContentRegistry::Hashes::add>("hex.hashes.hash.crc32posix", crypt::crc32, 0x04C1'1DB7, 0x0000'0000, 0xFFFF'FFFF, false, false); ContentRegistry::Hashes::add>("hex.hashes.hash.crc32c", crypt::crc32, 0x1EDC'6F41, 0xFFFF'FFFF, 0xFFFF'FFFF, true, true); hex::ContentRegistry::Hashes::add(HashFactory::Checksum::CreateAdler32); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateAP); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateBKDR); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateBernstein); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateBernstein1); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateDEK); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateDJB); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateELF); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateFNV1a_32); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateFNV32); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateJS); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateOneAtTime); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreatePJW); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateRotating); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateRS); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateSDBM); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateShiftAndXor); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateSuperFast); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateMurmur2_32); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateMurmurHash3_x86_32); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateXXHash32); hex::ContentRegistry::Hashes::add(HashFactory::Hash32::CreateJenkins3); hex::ContentRegistry::Hashes::add(HashFactory::Hash64::CreateFNV64); hex::ContentRegistry::Hashes::add(HashFactory::Hash64::CreateFNV1a_64); hex::ContentRegistry::Hashes::add(HashFactory::Hash64::CreateMurmur2_64); hex::ContentRegistry::Hashes::add(HashFactory::Hash64::CreateSipHash64_2_4); hex::ContentRegistry::Hashes::add(HashFactory::Hash64::CreateXXHash64); hex::ContentRegistry::Hashes::add(HashFactory::Hash128::CreateSipHash128_2_4); hex::ContentRegistry::Hashes::add(HashFactory::Hash128::CreateMurmurHash3_x86_128); hex::ContentRegistry::Hashes::add(HashFactory::Hash128::CreateMurmurHash3_x64_128); hex::ContentRegistry::Hashes::add("hex.hashes.hash.tiger", HashFactory::Crypto::CreateTiger); hex::ContentRegistry::Hashes::add("hex.hashes.hash.tiger2", HashFactory::Crypto::CreateTiger2); hex::ContentRegistry::Hashes::add>("hex.hashes.hash.blake2b", HashFactory::Crypto::CreateBlake2B); hex::ContentRegistry::Hashes::add>("hex.hashes.hash.blake2s", HashFactory::Crypto::CreateBlake2S); } }