summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Woyak <jordan.woyak@gmail.com>2026-10-22 15:05:23 -0500
committerGitHub <noreply@github.com>2026-10-22 15:05:23 -0500
commite1d35f666cc35f6ead55d32ea2679df44d04da72 (patch)
treedbcb08cbc40cb0f3e630a5c63201876a7a1e467a
parent10d958e66bb5d7574166a10f57839a288c5a7c64 (diff)
parent7e46e85e8fb3cff68f05bc160c7ac3ed77b1414b (diff)
Merge pull request #14913 from jordan-woyak/md5
Common: Introduce MD5Context class and ComputeMD5 function to minimize mbedtls dependence.
-rw-r--r--Source/Core/Common/Hash.cpp32
-rw-r--r--Source/Core/Common/Hash.h22
-rw-r--r--Source/Core/Core/HW/WiiSave.cpp7
-rw-r--r--Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.cpp6
-rw-r--r--Source/Core/DiscIO/VolumeVerifier.cpp8
-rw-r--r--Source/Core/DiscIO/VolumeVerifier.h5
6 files changed, 66 insertions, 14 deletions
diff --git a/Source/Core/Common/Hash.cpp b/Source/Core/Common/Hash.cpp
index f5b9da1b20..6e2ee8fb2b 100644
--- a/Source/Core/Common/Hash.cpp
+++ b/Source/Core/Common/Hash.cpp
@@ -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
diff --git a/Source/Core/Common/Hash.h b/Source/Core/Common/Hash.h
index 6166b74bd9..531dcd94e1 100644
--- a/Source/Core/Common/Hash.h
+++ b/Source/Core/Common/Hash.h
@@ -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
diff --git a/Source/Core/Core/HW/WiiSave.cpp b/Source/Core/Core/HW/WiiSave.cpp
index 58a6cf57c5..c90d49a5b8 100644
--- a/Source/Core/Core/HW/WiiSave.cpp
+++ b/Source/Core/Core/HW/WiiSave.cpp
@@ -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}",
diff --git a/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.cpp b/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.cpp
index 60edb515ed..7db61999bd 100644
--- a/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.cpp
+++ b/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.cpp
@@ -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 = {};
diff --git a/Source/Core/DiscIO/VolumeVerifier.cpp b/Source/Core/DiscIO/VolumeVerifier.cpp
index 3031f14a0f..a8122f6711 100644
--- a/Source/Core/DiscIO/VolumeVerifier.cpp
+++ b/Source/Core/DiscIO/VolumeVerifier.cpp
@@ -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)
diff --git a/Source/Core/DiscIO/VolumeVerifier.h b/Source/Core/DiscIO/VolumeVerifier.h
index d057fc83d3..141954741d 100644
--- a/Source/Core/DiscIO/VolumeVerifier.h
+++ b/Source/Core/DiscIO/VolumeVerifier.h
@@ -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;