Merge pull request #14913 from jordan-woyak/md5

Common: Introduce MD5Context class and ComputeMD5 function to minimize mbedtls dependence.
This commit is contained in:
Jordan Woyak authored and GitHub committed 2026-10-22 15:05:23 -05:00
commit e1d35f666c
6 files changed
+66 -14

No files matched your search

+32
View File
@@ -9,6 +9,8 @@
#include <zlib.h>
#include <mbedtls/md5.h>
#include "Common/CPUDetect.h"
#include "Common/Intrinsics.h"
@@ -457,4 +459,34 @@ u32 ComputeCRC32(std::string_view data)
{
return ComputeCRC32(reinterpret_cast<const u8*>(data.data()), data.size());
}
struct MD5Context::Impl
{
mbedtls_md5_context context;
};
MD5Context::MD5Context() : m_impl{std::make_unique<Impl>()}
{
mbedtls_md5_init(&m_impl->context);
mbedtls_md5_starts_ret(&m_impl->context);
}
// FYI: mbedtls_md5_free is unnecessary. It just zeroes the struct.
MD5Context::~MD5Context() = default;
void MD5Context::Update(std::span<const u8> data)
{
mbedtls_md5_update_ret(&m_impl->context, data.data(), data.size());
}
void MD5Context::Finish(u8* output)
{
mbedtls_md5_finish_ret(&m_impl->context, output);
}
void ComputeMD5(std::span<const u8> input, u8* output)
{
mbedtls_md5_ret(input.data(), input.size(), output);
}
} // namespace Common
+22
View File
@@ -4,6 +4,8 @@
#pragma once
#include <cstddef>
#include <memory>
#include <span>
#include <string_view>
#include "Common/CommonTypes.h"
@@ -21,4 +23,24 @@ u32 StartCRC32();
u32 UpdateCRC32(u32 crc, const u8* data, size_t len);
u32 ComputeCRC32(const u8* data, size_t len);
u32 ComputeCRC32(std::string_view data);
class MD5Context
{
public:
MD5Context();
~MD5Context();
void Update(std::span<const u8> data);
// output should point to 16 bytes.
void Finish(u8* output);
private:
struct Impl;
std::unique_ptr<Impl> m_impl;
};
// output should point to 16 bytes.
void ComputeMD5(std::span<const u8> input, u8* output);
} // namespace Common
+4 -3
View File
@@ -12,7 +12,6 @@
#include <array>
#include <cstdio>
#include <cstring>
#include <mbedtls/md5.h>
#include <memory>
#include <optional>
#include <string>
@@ -22,10 +21,12 @@
#include <fmt/format.h>
#include "Common/Align.h"
#include "Common/BitUtils.h"
#include "Common/CommonTypes.h"
#include "Common/Crypto/SHA1.h"
#include "Common/Crypto/ec.h"
#include "Common/FileUtil.h"
#include "Common/Hash.h"
#include "Common/IOFile.h"
#include "Common/Lazy.h"
#include "Common/Logging/Log.h"
@@ -125,7 +126,7 @@ public:
header.banner[7] &= ~1;
Md5 md5_calc;
mbedtls_md5_ret(reinterpret_cast<const u8*>(&header), sizeof(Header), md5_calc.data());
Common::ComputeMD5(Common::AsU8Span(header), md5_calc.data());
header.md5 = md5_calc;
return header;
}
@@ -302,7 +303,7 @@ public:
Md5 md5_file = header.md5;
header.md5 = s_md5_blanker;
Md5 md5_calc;
mbedtls_md5_ret(reinterpret_cast<const u8*>(&header), sizeof(Header), md5_calc.data());
Common::ComputeMD5(Common::AsU8Span(header), md5_calc.data());
if (md5_file != md5_calc)
{
ERROR_LOG_FMT(CONSOLE, "MD5 mismatch\n {:016x}{:016x} != {:016x}{:016x}",
@@ -9,10 +9,10 @@
#include <string>
#include <mbedtls/aes.h>
#include <mbedtls/md5.h>
#include "Common/BitUtils.h"
#include "Common/CommonTypes.h"
#include "Common/Hash.h"
#include "Common/IOFile.h"
#include "Common/Logging/Log.h"
#include "Common/Random.h"
@@ -107,7 +107,7 @@ void SkylanderFigure::Encrypt(std::span<const u8, FIGURE_SIZE> input)
std::array<u8, BLOCK_SIZE> hash_out = {};
mbedtls_md5_ret(hash_in.data(), 0x56, hash_out.data());
Common::ComputeMD5(hash_in, hash_out.data());
mbedtls_aes_context aes_context = {};
@@ -394,7 +394,7 @@ void SkylanderFigure::DecryptFigure(std::array<u8, FIGURE_SIZE>* dest) const
std::array<u8, BLOCK_SIZE> hash_out = {};
mbedtls_md5_ret(hash_in.data(), 0x56, hash_out.data());
Common::ComputeMD5(hash_in, hash_out.data());
mbedtls_aes_context aes_context = {};
+3 -5
View File
@@ -10,7 +10,6 @@
#include <string>
#include <string_view>
#include <mbedtls/md5.h>
#include <mz.h>
#include <mz_strm.h>
#include <mz_zip.h>
@@ -1087,8 +1086,7 @@ void VolumeVerifier::SetUpHashing()
if (m_hashes_to_calculate.md5)
{
mbedtls_md5_init(&m_md5_context);
mbedtls_md5_starts_ret(&m_md5_context);
m_md5_context.emplace();
}
if (m_hashes_to_calculate.sha1)
@@ -1231,7 +1229,7 @@ void VolumeVerifier::Process()
if (m_hashes_to_calculate.md5)
{
m_md5_future = std::async(std::launch::async, [this, byte_increment] {
mbedtls_md5_update_ret(&m_md5_context, m_data.data(), byte_increment);
m_md5_context->Update(std::span{m_data}.first(byte_increment));
});
}
@@ -1324,7 +1322,7 @@ void VolumeVerifier::Finish()
if (m_hashes_to_calculate.md5)
{
m_result.hashes.md5 = std::vector<u8>(16);
mbedtls_md5_finish_ret(&m_md5_context, m_result.hashes.md5.data());
m_md5_context->Finish(m_result.hashes.md5.data());
}
if (m_hashes_to_calculate.sha1)
+2 -3
View File
@@ -10,10 +10,9 @@
#include <string>
#include <vector>
#include <mbedtls/md5.h>
#include "Common/CommonTypes.h"
#include "Common/Crypto/SHA1.h"
#include "Common/Hash.h"
#include "Core/IOS/ES/Formats.h"
#include "DiscIO/DiscScrubber.h"
#include "DiscIO/Volume.h"
@@ -175,7 +174,7 @@ private:
Hashes<bool> m_hashes_to_calculate{};
bool m_calculating_any_hash = false;
u32 m_crc32_context = 0;
mbedtls_md5_context m_md5_context{};
std::optional<Common::MD5Context> m_md5_context;
std::unique_ptr<Common::SHA1::Context> m_sha1_context;
u64 m_excess_bytes = 0;