From 4b4f883aef8fbc4f8a8b6c842df61eb0d5253f45 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 11 Nov 2018 22:33:31 -0500 Subject: [PATCH 1/3] csrng: Use std::mt19937 engine for random number generation --- src/core/hle/service/spl/module.cpp | 9 +++++++-- src/core/hle/service/spl/module.h | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/core/hle/service/spl/module.cpp b/src/core/hle/service/spl/module.cpp index 44a6717d0..69c260408 100644 --- a/src/core/hle/service/spl/module.cpp +++ b/src/core/hle/service/spl/module.cpp @@ -3,18 +3,23 @@ // Refer to the license.txt file included. #include +#include #include +#include +#include #include #include "common/logging/log.h" #include "core/hle/ipc_helpers.h" #include "core/hle/service/spl/csrng.h" #include "core/hle/service/spl/module.h" #include "core/hle/service/spl/spl.h" +#include "core/settings.h" namespace Service::SPL { Module::Interface::Interface(std::shared_ptr module, const char* name) - : ServiceFramework(name), module(std::move(module)) {} + : ServiceFramework(name), module(std::move(module)), + rng(Settings::values.rng_seed.value_or(std::time(nullptr))) {} Module::Interface::~Interface() = default; @@ -24,7 +29,7 @@ void Module::Interface::GetRandomBytes(Kernel::HLERequestContext& ctx) { std::size_t size = ctx.GetWriteBufferSize(); std::vector data(size); - std::generate(data.begin(), data.end(), std::rand); + std::generate(data.begin(), data.end(), rng); ctx.WriteBuffer(data); diff --git a/src/core/hle/service/spl/module.h b/src/core/hle/service/spl/module.h index 48fda6099..afa1f0295 100644 --- a/src/core/hle/service/spl/module.h +++ b/src/core/hle/service/spl/module.h @@ -4,6 +4,7 @@ #pragma once +#include #include "core/hle/service/service.h" namespace Service::SPL { @@ -19,6 +20,9 @@ public: protected: std::shared_ptr module; + + private: + std::mt19937 rng; }; }; From 2a16fd7ffc1ec4e2dc90480c04f9867fdeec6a98 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 11 Nov 2018 22:34:23 -0500 Subject: [PATCH 2/3] settings: Add config option to set RNG seed --- src/core/settings.h | 2 + src/yuzu/configuration/config.cpp | 12 ++ src/yuzu/configuration/configure_system.cpp | 17 ++ src/yuzu/configuration/configure_system.ui | 228 +++++++++++--------- src/yuzu_cmd/config.cpp | 7 + src/yuzu_cmd/default_ini.h | 5 + 6 files changed, 171 insertions(+), 100 deletions(-) diff --git a/src/core/settings.h b/src/core/settings.h index a8954647f..83a1a7069 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -6,6 +6,7 @@ #include #include +#include #include #include "common/common_types.h" @@ -114,6 +115,7 @@ struct Values { // System bool use_docked_mode; bool enable_nfc; + std::optional rng_seed; s32 current_user; s32 language_index; diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index d4fd60a73..d3b7fa59d 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -134,6 +134,14 @@ void Config::ReadValues() { Service::Account::MAX_USERS - 1); Settings::values.language_index = qt_config->value("language_index", 1).toInt(); + + const auto enabled = qt_config->value("rng_seed_enabled", false).toBool(); + if (enabled) { + Settings::values.rng_seed = qt_config->value("rng_seed", 0).toULongLong(); + } else { + Settings::values.rng_seed = std::nullopt; + } + qt_config->endGroup(); qt_config->beginGroup("Miscellaneous"); @@ -272,6 +280,10 @@ void Config::SaveValues() { qt_config->setValue("current_user", Settings::values.current_user); qt_config->setValue("language_index", Settings::values.language_index); + + qt_config->setValue("rng_seed_enabled", Settings::values.rng_seed.has_value()); + qt_config->setValue("rng_seed", Settings::values.rng_seed.value_or(0)); + qt_config->endGroup(); qt_config->beginGroup("Miscellaneous"); diff --git a/src/yuzu/configuration/configure_system.cpp b/src/yuzu/configuration/configure_system.cpp index 4803d43bb..b001266f6 100644 --- a/src/yuzu/configuration/configure_system.cpp +++ b/src/yuzu/configuration/configure_system.cpp @@ -137,6 +137,12 @@ ConfigureSystem::ConfigureSystem(QWidget* parent) connect(ui->pm_remove, &QPushButton::pressed, this, &ConfigureSystem::DeleteUser); connect(ui->pm_set_image, &QPushButton::pressed, this, &ConfigureSystem::SetUserImage); + connect(ui->rng_seed_checkbox, &QCheckBox::stateChanged, this, [this](bool checked) { + ui->rng_seed_edit->setEnabled(checked); + if (!checked) + ui->rng_seed_edit->setText("0000000000000000"); + }); + scene = new QGraphicsScene; ui->current_user_icon->setScene(scene); @@ -155,6 +161,11 @@ void ConfigureSystem::setConfiguration() { PopulateUserList(); UpdateCurrentUser(); + + ui->rng_seed_checkbox->setChecked(Settings::values.rng_seed.has_value()); + ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.has_value()); + ui->rng_seed_edit->setText( + QString::fromStdString(fmt::format("{:016X}", Settings::values.rng_seed.value_or(0)))); } void ConfigureSystem::PopulateUserList() { @@ -195,6 +206,12 @@ void ConfigureSystem::applyConfiguration() { return; Settings::values.language_index = ui->combo_language->currentIndex(); + + if (ui->rng_seed_checkbox->isChecked()) + Settings::values.rng_seed = ui->rng_seed_edit->text().toULongLong(nullptr, 16); + else + Settings::values.rng_seed = std::nullopt; + Settings::Apply(); } diff --git a/src/yuzu/configuration/configure_system.ui b/src/yuzu/configuration/configure_system.ui index 020b32a37..d0fcd0163 100644 --- a/src/yuzu/configuration/configure_system.ui +++ b/src/yuzu/configuration/configure_system.ui @@ -6,7 +6,7 @@ 0 0 - 360 + 366 483 @@ -22,98 +22,6 @@ System Settings - - - - Language - - - - - - - Birthday - - - - - - - Console ID: - - - - - - - - - - January - - - - - February - - - - - March - - - - - April - - - - - May - - - - - June - - - - - July - - - - - August - - - - - September - - - - - October - - - - - November - - - - - December - - - - - - - - - @@ -206,6 +114,13 @@ + + + + Console ID: + + + @@ -213,6 +128,100 @@ + + + + Birthday + + + + + + + + + + January + + + + + February + + + + + March + + + + + April + + + + + May + + + + + June + + + + + July + + + + + August + + + + + September + + + + + October + + + + + November + + + + + December + + + + + + + + + + + + + + 0 + 0 + + + + Qt::RightToLeft + + + Regenerate + + + @@ -232,19 +241,38 @@ - - + + + + Language + + + + + + + RNG Seed + + + + + - + 0 0 - - Qt::RightToLeft + + + Lucida Console + - - Regenerate + + HHHHHHHHHHHHHHHH + + + 16 diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp index b456266a6..f3134d4cb 100644 --- a/src/yuzu_cmd/config.cpp +++ b/src/yuzu_cmd/config.cpp @@ -132,6 +132,13 @@ void Config::ReadValues() { Settings::values.current_user = std::clamp( sdl2_config->GetInteger("System", "current_user", 0), 0, Service::Account::MAX_USERS - 1); + const auto enabled = sdl2_config->GetBoolean("System", "rng_seed_enabled", false); + if (enabled) { + Settings::values.rng_seed = sdl2_config->GetInteger("System", "rng_seed", 0); + } else { + Settings::values.rng_seed = std::nullopt; + } + // Miscellaneous Settings::values.log_filter = sdl2_config->Get("Miscellaneous", "log_filter", "*:Trace"); Settings::values.use_dev_keys = sdl2_config->GetBoolean("Miscellaneous", "use_dev_keys", false); diff --git a/src/yuzu_cmd/default_ini.h b/src/yuzu_cmd/default_ini.h index e0b223cd6..dd6644d79 100644 --- a/src/yuzu_cmd/default_ini.h +++ b/src/yuzu_cmd/default_ini.h @@ -178,6 +178,11 @@ use_docked_mode = # 1 (default): Yes, 0 : No enable_nfc = +# Sets the seed for the RNG generator built into the switch +# rng_seed will be ignored and randomly generated if rng_seed_enabled is false +rng_seed_enabled = +rng_seed = + # Sets the account username, max length is 32 characters # yuzu (default) username = yuzu From cb1e63ef09a5cd775e916f6f4482cde3ea256f44 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Mon, 12 Nov 2018 21:46:21 -0500 Subject: [PATCH 3/3] svc: Return random seed for svcGetInfo RandomEntropy --- src/core/hle/kernel/svc.cpp | 3 ++- src/yuzu/configuration/configure_system.cpp | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 7e8e87c33..0bfe1e3be 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -34,6 +34,7 @@ #include "core/hle/lock.h" #include "core/hle/result.h" #include "core/hle/service/service.h" +#include "core/settings.h" namespace Kernel { namespace { @@ -558,7 +559,7 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) *result = 0; break; case GetInfoType::RandomEntropy: - *result = 0; + *result = Settings::values.rng_seed.value_or(0); break; case GetInfoType::ASLRRegionBaseAddr: *result = vm_manager.GetASLRRegionBaseAddress(); diff --git a/src/yuzu/configuration/configure_system.cpp b/src/yuzu/configuration/configure_system.cpp index b001266f6..67f07ecb1 100644 --- a/src/yuzu/configuration/configure_system.cpp +++ b/src/yuzu/configuration/configure_system.cpp @@ -140,7 +140,7 @@ ConfigureSystem::ConfigureSystem(QWidget* parent) connect(ui->rng_seed_checkbox, &QCheckBox::stateChanged, this, [this](bool checked) { ui->rng_seed_edit->setEnabled(checked); if (!checked) - ui->rng_seed_edit->setText("0000000000000000"); + ui->rng_seed_edit->setText(QStringLiteral("0000000000000000")); }); scene = new QGraphicsScene; @@ -164,8 +164,11 @@ void ConfigureSystem::setConfiguration() { ui->rng_seed_checkbox->setChecked(Settings::values.rng_seed.has_value()); ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.has_value()); - ui->rng_seed_edit->setText( - QString::fromStdString(fmt::format("{:016X}", Settings::values.rng_seed.value_or(0)))); + + const auto rng_seed = QString("%1") + .arg(Settings::values.rng_seed.value_or(0), 16, 16, QLatin1Char{'0'}) + .toUpper(); + ui->rng_seed_edit->setText(rng_seed); } void ConfigureSystem::PopulateUserList() {