service: nfc: Seed all random values

This commit is contained in:
Narr the Reg 2023-05-09 17:49:56 -06:00
parent 1968cc7b10
commit 42e1db4b0e
2 changed files with 14 additions and 6 deletions

View File

@ -48,9 +48,6 @@ NfcDevice::NfcDevice(Core::HID::NpadIdType npad_id_, Core::System& system_,
}; };
is_controller_set = true; is_controller_set = true;
callback_key = npad_device->SetCallback(engine_callback); callback_key = npad_device->SetCallback(engine_callback);
auto& standard_steady_clock{system.GetTimeManager().GetStandardSteadyClockCore()};
current_posix_time = standard_steady_clock.GetCurrentTimePoint(system).time_point;
} }
NfcDevice::~NfcDevice() { NfcDevice::~NfcDevice() {
@ -232,6 +229,7 @@ Result NfcDevice::GetTagInfo(NFP::TagInfo& tag_info, bool is_mifare) const {
// Generate random UUID to bypass amiibo load limits // Generate random UUID to bypass amiibo load limits
if (Settings::values.random_amiibo_id) { if (Settings::values.random_amiibo_id) {
Common::TinyMT rng{}; Common::TinyMT rng{};
rng.Initialize(static_cast<u32>(GetCurrentPosixTime()));
rng.GenerateRandomBytes(uuid.data(), sizeof(UniqueSerialNumber)); rng.GenerateRandomBytes(uuid.data(), sizeof(UniqueSerialNumber));
uuid[3] = 0x88 ^ uuid[0] ^ uuid[1] ^ uuid[2]; uuid[3] = 0x88 ^ uuid[0] ^ uuid[1] ^ uuid[2];
} }
@ -415,7 +413,7 @@ Result NfcDevice::Flush() {
auto& settings = tag_data.settings; auto& settings = tag_data.settings;
const auto& current_date = GetAmiiboDate(current_posix_time); const auto& current_date = GetAmiiboDate(GetCurrentPosixTime());
if (settings.write_date.raw_date != current_date.raw_date) { if (settings.write_date.raw_date != current_date.raw_date) {
settings.write_date = current_date; settings.write_date = current_date;
UpdateSettingsCrc(); UpdateSettingsCrc();
@ -534,6 +532,7 @@ Result NfcDevice::GetModelInfo(NFP::ModelInfo& model_info) const {
} }
const auto& model_info_data = encrypted_tag_data.user_memory.model_info; const auto& model_info_data = encrypted_tag_data.user_memory.model_info;
model_info = { model_info = {
.character_id = model_info_data.character_id, .character_id = model_info_data.character_id,
.character_variant = model_info_data.character_variant, .character_variant = model_info_data.character_variant,
@ -678,6 +677,7 @@ Result NfcDevice::DeleteRegisterInfo() {
} }
Common::TinyMT rng{}; Common::TinyMT rng{};
rng.Initialize(static_cast<u32>(GetCurrentPosixTime()));
rng.GenerateRandomBytes(&tag_data.owner_mii, sizeof(tag_data.owner_mii)); rng.GenerateRandomBytes(&tag_data.owner_mii, sizeof(tag_data.owner_mii));
rng.GenerateRandomBytes(&tag_data.settings.amiibo_name, sizeof(tag_data.settings.amiibo_name)); rng.GenerateRandomBytes(&tag_data.settings.amiibo_name, sizeof(tag_data.settings.amiibo_name));
rng.GenerateRandomBytes(&tag_data.unknown, sizeof(u8)); rng.GenerateRandomBytes(&tag_data.unknown, sizeof(u8));
@ -710,7 +710,7 @@ Result NfcDevice::SetRegisterInfoPrivate(const NFP::RegisterInfoPrivate& registe
auto& settings = tag_data.settings; auto& settings = tag_data.settings;
if (tag_data.settings.settings.amiibo_initialized == 0) { if (tag_data.settings.settings.amiibo_initialized == 0) {
settings.init_date = GetAmiiboDate(current_posix_time); settings.init_date = GetAmiiboDate(GetCurrentPosixTime());
settings.write_date.raw_date = 0; settings.write_date.raw_date = 0;
} }
@ -877,6 +877,7 @@ Result NfcDevice::SetApplicationArea(std::span<const u8> data) {
} }
Common::TinyMT rng{}; Common::TinyMT rng{};
rng.Initialize(static_cast<u32>(GetCurrentPosixTime()));
std::memcpy(tag_data.application_area.data(), data.data(), data.size()); std::memcpy(tag_data.application_area.data(), data.data(), data.size());
// Fill remaining data with random numbers // Fill remaining data with random numbers
rng.GenerateRandomBytes(tag_data.application_area.data() + data.size(), rng.GenerateRandomBytes(tag_data.application_area.data() + data.size(),
@ -933,6 +934,7 @@ Result NfcDevice::RecreateApplicationArea(u32 access_id, std::span<const u8> dat
} }
Common::TinyMT rng{}; Common::TinyMT rng{};
rng.Initialize(static_cast<u32>(GetCurrentPosixTime()));
std::memcpy(tag_data.application_area.data(), data.data(), data.size()); std::memcpy(tag_data.application_area.data(), data.data(), data.size());
// Fill remaining data with random numbers // Fill remaining data with random numbers
rng.GenerateRandomBytes(tag_data.application_area.data() + data.size(), rng.GenerateRandomBytes(tag_data.application_area.data() + data.size(),
@ -982,6 +984,7 @@ Result NfcDevice::DeleteApplicationArea() {
} }
Common::TinyMT rng{}; Common::TinyMT rng{};
rng.Initialize(static_cast<u32>(GetCurrentPosixTime()));
rng.GenerateRandomBytes(tag_data.application_area.data(), sizeof(NFP::ApplicationArea)); rng.GenerateRandomBytes(tag_data.application_area.data(), sizeof(NFP::ApplicationArea));
rng.GenerateRandomBytes(&tag_data.application_id, sizeof(u64)); rng.GenerateRandomBytes(&tag_data.application_id, sizeof(u64));
rng.GenerateRandomBytes(&tag_data.application_area_id, sizeof(u32)); rng.GenerateRandomBytes(&tag_data.application_area_id, sizeof(u32));
@ -1198,6 +1201,11 @@ NFP::AmiiboDate NfcDevice::GetAmiiboDate(s64 posix_time) const {
return amiibo_date; return amiibo_date;
} }
u64 NfcDevice::GetCurrentPosixTime() const {
auto& standard_steady_clock{system.GetTimeManager().GetStandardSteadyClockCore()};
return standard_steady_clock.GetCurrentTimePoint(system).time_point;
}
u64 NfcDevice::RemoveVersionByte(u64 application_id) const { u64 NfcDevice::RemoveVersionByte(u64 application_id) const {
return application_id & ~(0xfULL << NFP::application_id_version_offset); return application_id & ~(0xfULL << NFP::application_id_version_offset);
} }

View File

@ -105,6 +105,7 @@ private:
NFP::AmiiboName GetAmiiboName(const NFP::AmiiboSettings& settings) const; NFP::AmiiboName GetAmiiboName(const NFP::AmiiboSettings& settings) const;
void SetAmiiboName(NFP::AmiiboSettings& settings, const NFP::AmiiboName& amiibo_name); void SetAmiiboName(NFP::AmiiboSettings& settings, const NFP::AmiiboName& amiibo_name);
NFP::AmiiboDate GetAmiiboDate(s64 posix_time) const; NFP::AmiiboDate GetAmiiboDate(s64 posix_time) const;
u64 GetCurrentPosixTime() const;
u64 RemoveVersionByte(u64 application_id) const; u64 RemoveVersionByte(u64 application_id) const;
void UpdateSettingsCrc(); void UpdateSettingsCrc();
void UpdateRegisterInfoCrc(); void UpdateRegisterInfoCrc();
@ -127,7 +128,6 @@ private:
bool is_data_moddified{}; bool is_data_moddified{};
bool is_app_area_open{}; bool is_app_area_open{};
bool is_plain_amiibo{}; bool is_plain_amiibo{};
s64 current_posix_time{};
NFP::MountTarget mount_target{NFP::MountTarget::None}; NFP::MountTarget mount_target{NFP::MountTarget::None};
NFP::NTAG215File tag_data{}; NFP::NTAG215File tag_data{};