Skip to content

Commit

Permalink
VolumeVerifier: enable fast hash functions by default
Browse files Browse the repository at this point in the history
sets defaults based on cpu support.
  • Loading branch information
shuffle2 committed Aug 3, 2022
1 parent 7d2d5d9 commit d717971
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 18 deletions.
5 changes: 5 additions & 0 deletions Source/Core/Common/Crypto/SHA1.cpp
Expand Up @@ -59,6 +59,7 @@ class ContextMbed final : public Context
ASSERT(!mbedtls_sha1_finish_ret(&ctx, digest.data()));
return digest;
}
virtual bool HwAccelerated() const override { return false; }

private:
mbedtls_sha1_context ctx{};
Expand Down Expand Up @@ -249,6 +250,8 @@ class ContextX64SHA1 final : public BlockContext
return digest;
}

virtual bool HwAccelerated() const override { return true; }

std::array<__m128i, 2> state{};
};

Expand Down Expand Up @@ -363,6 +366,8 @@ class ContextNeon final : public BlockContext
return digest;
}

virtual bool HwAccelerated() const override { return true; }

State state;
};

Expand Down
1 change: 1 addition & 0 deletions Source/Core/Common/Crypto/SHA1.h
Expand Up @@ -25,6 +25,7 @@ class Context
virtual void Update(const u8* msg, size_t len) = 0;
void Update(const std::vector<u8>& msg) { return Update(msg.data(), msg.size()); }
virtual Digest Finish() = 0;
virtual bool HwAccelerated() const = 0;
};

std::unique_ptr<Context> CreateContext();
Expand Down
19 changes: 19 additions & 0 deletions Source/Core/DiscIO/VolumeVerifier.cpp
Expand Up @@ -18,6 +18,7 @@

#include "Common/Align.h"
#include "Common/Assert.h"
#include "Common/CPUDetect.h"
#include "Common/CommonPaths.h"
#include "Common/CommonTypes.h"
#include "Common/Crypto/SHA1.h"
Expand Down Expand Up @@ -374,6 +375,24 @@ VolumeVerifier::~VolumeVerifier()
WaitForAsyncOperations();
}

Hashes<bool> VolumeVerifier::GetDefaultHashesToCalculate()
{
Hashes<bool> hashes_to_calculate{.crc32 = true, .md5 = true, .sha1 = true};
// If the system can compute certain hashes faster than others, only default-enable the fast ones.
const bool sha1_hw_accel = Common::SHA1::CreateContext()->HwAccelerated();
// For crc32, we assume zlib-ng will be fast if cpu supports crc32
const bool crc32_hw_accel = cpu_info.bCRC32;
if (crc32_hw_accel || sha1_hw_accel)
{
hashes_to_calculate.crc32 = crc32_hw_accel;
// md5 has no accelerated implementation at the moment, always default to off
hashes_to_calculate.md5 = false;
// Always enable SHA1, to avoid situation where only crc32 is computed
hashes_to_calculate.sha1 = true;
}
return hashes_to_calculate;
}

void VolumeVerifier::Start()
{
ASSERT(!m_started);
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DiscIO/VolumeVerifier.h
Expand Up @@ -127,6 +127,7 @@ class VolumeVerifier final
VolumeVerifier(const Volume& volume, bool redump_verification, Hashes<bool> hashes_to_calculate);
~VolumeVerifier();

static Hashes<bool> GetDefaultHashesToCalculate();
void Start();
void Process();
u64 GetBytesProcessed() const;
Expand Down
8 changes: 7 additions & 1 deletion Source/Core/DolphinQt/Config/VerifyWidget.cpp
Expand Up @@ -74,11 +74,18 @@ void VerifyWidget::CreateWidgets()
std::tie(m_md5_checkbox, m_md5_line_edit) = AddHashLine(m_hash_layout, tr("MD5:"));
std::tie(m_sha1_checkbox, m_sha1_line_edit) = AddHashLine(m_hash_layout, tr("SHA-1:"));

const auto default_to_calculate = DiscIO::VolumeVerifier::GetDefaultHashesToCalculate();
m_crc32_checkbox->setChecked(default_to_calculate.crc32);
m_md5_checkbox->setChecked(default_to_calculate.md5);
m_sha1_checkbox->setChecked(default_to_calculate.sha1);

m_redump_layout = new QFormLayout;
if (DiscIO::IsDisc(m_volume->GetVolumeType()))
{
std::tie(m_redump_checkbox, m_redump_line_edit) =
AddHashLine(m_redump_layout, tr("Redump.org Status:"));
m_redump_checkbox->setChecked(CanVerifyRedump());
UpdateRedumpEnabled();
}
else
{
Expand All @@ -98,7 +105,6 @@ std::pair<QCheckBox*, QLineEdit*> VerifyWidget::AddHashLine(QFormLayout* layout,
QLineEdit* line_edit = new QLineEdit(this);
line_edit->setReadOnly(true);
QCheckBox* checkbox = new QCheckBox(tr("Calculate"), this);
checkbox->setChecked(true);

QHBoxLayout* hbox_layout = new QHBoxLayout;
hbox_layout->addWidget(line_edit);
Expand Down
41 changes: 26 additions & 15 deletions Source/Core/DolphinTool/VerifyCommand.cpp
Expand Up @@ -57,11 +57,22 @@ int VerifyCommand::Main(const std::vector<std::string>& args)
algorithm = static_cast<const char*>(options.get("algorithm"));
}

bool enable_crc32 = algorithm == std::nullopt || algorithm == "crc32";
bool enable_md5 = algorithm == std::nullopt || algorithm == "md5";
bool enable_sha1 = algorithm == std::nullopt || algorithm == "sha1";
DiscIO::Hashes<bool> hashes_to_calculate{};
if (algorithm == std::nullopt)
{
hashes_to_calculate = DiscIO::VolumeVerifier::GetDefaultHashesToCalculate();
}
else
{
if (algorithm == "crc32")
hashes_to_calculate.crc32 = true;
else if (algorithm == "md5")
hashes_to_calculate.md5 = true;
else if (algorithm == "sha1")
hashes_to_calculate.sha1 = true;
}

if (!enable_crc32 && !enable_md5 && !enable_sha1)
if (!hashes_to_calculate.crc32 && !hashes_to_calculate.md5 && !hashes_to_calculate.sha1)
{
// optparse should protect from this
std::cerr << "Error: No algorithms selected for the operation" << std::endl;
Expand All @@ -78,7 +89,7 @@ int VerifyCommand::Main(const std::vector<std::string>& args)

// Verify the volume
const std::optional<DiscIO::VolumeVerifier::Result> result =
VerifyVolume(volume, enable_crc32, enable_md5, enable_sha1);
VerifyVolume(volume, hashes_to_calculate);
if (!result)
{
std::cerr << "Error: Unable to verify volume" << std::endl;
Expand All @@ -91,15 +102,15 @@ int VerifyCommand::Main(const std::vector<std::string>& args)
}
else
{
if (enable_crc32 && !result->hashes.crc32.empty())
if (hashes_to_calculate.crc32 && !result->hashes.crc32.empty())
std::cout << HashToHexString(result->hashes.crc32) << std::endl;
else if (enable_md5 && !result->hashes.md5.empty())
else if (hashes_to_calculate.md5 && !result->hashes.md5.empty())
std::cout << HashToHexString(result->hashes.md5) << std::endl;
else if (enable_sha1 && !result->hashes.sha1.empty())
else if (hashes_to_calculate.sha1 && !result->hashes.sha1.empty())
std::cout << HashToHexString(result->hashes.sha1) << std::endl;
else
{
std::cerr << "Error: No hash available" << std::endl;
std::cerr << "Error: No hash computed" << std::endl;
return 1;
}
}
Expand All @@ -112,17 +123,17 @@ void VerifyCommand::PrintFullReport(const std::optional<DiscIO::VolumeVerifier::
if (!result->hashes.crc32.empty())
std::cout << "CRC32: " << HashToHexString(result->hashes.crc32) << std::endl;
else
std::cout << "CRC32 not available" << std::endl;
std::cout << "CRC32 not computed" << std::endl;

if (!result->hashes.md5.empty())
std::cout << "MD5: " << HashToHexString(result->hashes.md5) << std::endl;
else
std::cout << "MD5 not available" << std::endl;
std::cout << "MD5 not computed" << std::endl;

if (!result->hashes.sha1.empty())
std::cout << "SHA1: " << HashToHexString(result->hashes.sha1) << std::endl;
else
std::cout << "SHA1 not available" << std::endl;
std::cout << "SHA1 not computed" << std::endl;

std::cout << "Problems Found: " << (result->problems.size() > 0 ? "Yes" : "No") << std::endl;

Expand Down Expand Up @@ -156,13 +167,13 @@ void VerifyCommand::PrintFullReport(const std::optional<DiscIO::VolumeVerifier::
}

std::optional<DiscIO::VolumeVerifier::Result>
VerifyCommand::VerifyVolume(std::shared_ptr<DiscIO::VolumeDisc> volume, bool enable_crc32,
bool enable_md5, bool enable_sha1)
VerifyCommand::VerifyVolume(std::shared_ptr<DiscIO::VolumeDisc> volume,
const DiscIO::Hashes<bool>& hashes_to_calculate)
{
if (!volume)
return std::nullopt;

DiscIO::VolumeVerifier verifier(*volume, false, {enable_crc32, enable_md5, enable_sha1});
DiscIO::VolumeVerifier verifier(*volume, false, hashes_to_calculate);

verifier.Start();
while (verifier.GetBytesProcessed() != verifier.GetTotalBytes())
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/DolphinTool/VerifyCommand.h
Expand Up @@ -23,8 +23,8 @@ class VerifyCommand final : public Command
void PrintFullReport(const std::optional<DiscIO::VolumeVerifier::Result> result);

std::optional<DiscIO::VolumeVerifier::Result>
VerifyVolume(std::shared_ptr<DiscIO::VolumeDisc> volume, bool enable_crc32, bool enable_md5,
bool enable_sha1);
VerifyVolume(std::shared_ptr<DiscIO::VolumeDisc> volume,
const DiscIO::Hashes<bool>& hashes_to_calculate);

std::string HashToHexString(const std::vector<u8>& hash);
};
Expand Down

0 comments on commit d717971

Please sign in to comment.