service: nfp: Actually write correct crc

This commit is contained in:
Narr the Reg 2023-03-14 22:00:38 -06:00
parent 0c7149d222
commit 026fe2e4f4
6 changed files with 44 additions and 23 deletions

View File

@ -585,6 +585,8 @@ Ver3StoreData MiiManager::ConvertCharInfoToV3(const CharInfo& mii) const {
mii_v3.appearance_bits11.mole_x_position.Assign(mii.mole_x); mii_v3.appearance_bits11.mole_x_position.Assign(mii.mole_x);
mii_v3.appearance_bits11.mole_y_position.Assign(mii.mole_y); mii_v3.appearance_bits11.mole_y_position.Assign(mii.mole_y);
mii_v3.crc = GenerateCrc16(&mii_v3, sizeof(Ver3StoreData) - sizeof(u16));
// TODO: Validate mii_v3 data // TODO: Validate mii_v3 data
return mii_v3; return mii_v3;

View File

@ -365,7 +365,8 @@ struct Ver3StoreData {
} appearance_bits11; } appearance_bits11;
std::array<u16_le, 0xA> author_name; std::array<u16_le, 0xA> author_name;
INSERT_PADDING_BYTES(0x4); INSERT_PADDING_BYTES(0x2);
u16_be crc;
}; };
static_assert(sizeof(Ver3StoreData) == 0x60, "Ver3StoreData is an invalid size"); static_assert(sizeof(Ver3StoreData) == 0x60, "Ver3StoreData is an invalid size");

View File

@ -89,7 +89,7 @@ NTAG215File NfcDataToEncodedData(const EncryptedNTAG215File& nfc_data) {
encoded_data.application_id_byte = nfc_data.user_memory.application_id_byte; encoded_data.application_id_byte = nfc_data.user_memory.application_id_byte;
encoded_data.unknown = nfc_data.user_memory.unknown; encoded_data.unknown = nfc_data.user_memory.unknown;
encoded_data.unknown2 = nfc_data.user_memory.unknown2; encoded_data.unknown2 = nfc_data.user_memory.unknown2;
encoded_data.application_area_crc = nfc_data.user_memory.application_area_crc; encoded_data.register_info_crc = nfc_data.user_memory.register_info_crc;
encoded_data.application_area = nfc_data.user_memory.application_area; encoded_data.application_area = nfc_data.user_memory.application_area;
encoded_data.hmac_tag = nfc_data.user_memory.hmac_tag; encoded_data.hmac_tag = nfc_data.user_memory.hmac_tag;
encoded_data.lock_bytes = nfc_data.uuid.lock_bytes; encoded_data.lock_bytes = nfc_data.uuid.lock_bytes;
@ -123,7 +123,7 @@ EncryptedNTAG215File EncodedDataToNfcData(const NTAG215File& encoded_data) {
nfc_data.user_memory.application_id_byte = encoded_data.application_id_byte; nfc_data.user_memory.application_id_byte = encoded_data.application_id_byte;
nfc_data.user_memory.unknown = encoded_data.unknown; nfc_data.user_memory.unknown = encoded_data.unknown;
nfc_data.user_memory.unknown2 = encoded_data.unknown2; nfc_data.user_memory.unknown2 = encoded_data.unknown2;
nfc_data.user_memory.application_area_crc = encoded_data.application_area_crc; nfc_data.user_memory.register_info_crc = encoded_data.register_info_crc;
nfc_data.user_memory.application_area = encoded_data.application_area; nfc_data.user_memory.application_area = encoded_data.application_area;
nfc_data.user_memory.hmac_tag = encoded_data.hmac_tag; nfc_data.user_memory.hmac_tag = encoded_data.hmac_tag;
nfc_data.user_memory.model_info = encoded_data.model_info; nfc_data.user_memory.model_info = encoded_data.model_info;

View File

@ -448,7 +448,7 @@ Result NfpDevice::DeleteRegisterInfo() {
rng.GenerateRandomBytes(&tag_data.unknown, sizeof(u8)); rng.GenerateRandomBytes(&tag_data.unknown, sizeof(u8));
rng.GenerateRandomBytes(&tag_data.unknown2[0], sizeof(u32)); rng.GenerateRandomBytes(&tag_data.unknown2[0], sizeof(u32));
rng.GenerateRandomBytes(&tag_data.unknown2[1], sizeof(u32)); rng.GenerateRandomBytes(&tag_data.unknown2[1], sizeof(u32));
rng.GenerateRandomBytes(&tag_data.application_area_crc, sizeof(u32)); rng.GenerateRandomBytes(&tag_data.register_info_crc, sizeof(u32));
rng.GenerateRandomBytes(&tag_data.settings.init_date, sizeof(u32)); rng.GenerateRandomBytes(&tag_data.settings.init_date, sizeof(u32));
tag_data.settings.settings.font_region.Assign(0); tag_data.settings.settings.font_region.Assign(0);
tag_data.settings.settings.amiibo_initialized.Assign(0); tag_data.settings.settings.amiibo_initialized.Assign(0);
@ -486,9 +486,7 @@ Result NfpDevice::SetRegisterInfoPrivate(const AmiiboName& amiibo_name) {
settings.settings.font_region.Assign(0); settings.settings.font_region.Assign(0);
settings.settings.amiibo_initialized.Assign(1); settings.settings.amiibo_initialized.Assign(1);
// TODO: this is a mix of tag.file input UpdateRegisterInfoCrc();
std::array<u8, 0x7e> unknown_input{};
tag_data.application_area_crc = CalculateCrc(unknown_input);
return Flush(); return Flush();
} }
@ -716,9 +714,7 @@ Result NfpDevice::RecreateApplicationArea(u32 access_id, std::span<const u8> dat
tag_data.application_area_id = access_id; tag_data.application_area_id = access_id;
tag_data.unknown = {}; tag_data.unknown = {};
// TODO: this is a mix of tag_data input UpdateRegisterInfoCrc();
std::array<u8, 0x7e> unknown_input{};
tag_data.application_area_crc = CalculateCrc(unknown_input);
return Flush(); return Flush();
} }
@ -838,6 +834,29 @@ void NfpDevice::UpdateSettingsCrc() {
settings.crc = CalculateCrc(unknown_input); settings.crc = CalculateCrc(unknown_input);
} }
void NfpDevice::UpdateRegisterInfoCrc() {
#pragma pack(push, 1)
struct CrcData {
Mii::Ver3StoreData mii;
u8 application_id_byte;
u8 unknown;
std::array<u32, 0x7> unknown2;
};
static_assert(sizeof(CrcData) == 0x7e, "CrcData is an invalid size");
#pragma pack(pop)
const CrcData crc_data{
.mii = tag_data.owner_mii,
.application_id_byte = tag_data.application_id_byte,
.unknown = tag_data.unknown,
.unknown2 = tag_data.unknown2,
};
std::array<u8, sizeof(CrcData)> data{};
memcpy(data.data(), &crc_data, sizeof(CrcData));
tag_data.register_info_crc = CalculateCrc(data);
}
u32 NfpDevice::CalculateCrc(std::span<const u8> data) { u32 NfpDevice::CalculateCrc(std::span<const u8> data) {
constexpr u32 magic = 0xedb88320; constexpr u32 magic = 0xedb88320;
u32 crc = 0xffffffff; u32 crc = 0xffffffff;
@ -847,17 +866,15 @@ u32 NfpDevice::CalculateCrc(std::span<const u8> data) {
} }
for (u8 input : data) { for (u8 input : data) {
u32 temp = (crc ^ input) >> 1; crc ^= input;
if (((crc ^ input) & 1) != 0) { crc = crc >> 1 ^ ((crc & 1) ? magic : 0x0);
temp = temp ^ magic; crc = crc >> 1 ^ ((crc & 1) ? magic : 0x0);
} crc = crc >> 1 ^ ((crc & 1) ? magic : 0x0);
crc = crc >> 1 ^ ((crc & 1) ? magic : 0x0);
for (std::size_t step = 0; step < 7; ++step) { crc = crc >> 1 ^ ((crc & 1) ? magic : 0x0);
crc = temp >> 1; crc = crc >> 1 ^ ((crc & 1) ? magic : 0x0);
if ((temp & 1) != 0) { crc = crc >> 1 ^ ((crc & 1) ? magic : 0x0);
crc = temp >> 1 ^ magic; crc = crc >> 1 ^ ((crc & 1) ? magic : 0x0);
}
}
} }
return ~crc; return ~crc;

View File

@ -80,6 +80,7 @@ private:
AmiiboDate GetAmiiboDate(s64 posix_time) const; AmiiboDate GetAmiiboDate(s64 posix_time) const;
u64 RemoveVersionByte(u64 application_id) const; u64 RemoveVersionByte(u64 application_id) const;
void UpdateSettingsCrc(); void UpdateSettingsCrc();
void UpdateRegisterInfoCrc();
u32 CalculateCrc(std::span<const u8>); u32 CalculateCrc(std::span<const u8>);
bool is_controller_set{}; bool is_controller_set{};

View File

@ -260,7 +260,7 @@ struct EncryptedAmiiboFile {
u8 application_id_byte; u8 application_id_byte;
u8 unknown; u8 unknown;
std::array<u32, 0x7> unknown2; std::array<u32, 0x7> unknown2;
u32_be application_area_crc; u32_be register_info_crc;
ApplicationArea application_area; // Encrypted Game data ApplicationArea application_area; // Encrypted Game data
}; };
static_assert(sizeof(EncryptedAmiiboFile) == 0x1F8, "AmiiboFile is an invalid size"); static_assert(sizeof(EncryptedAmiiboFile) == 0x1F8, "AmiiboFile is an invalid size");
@ -281,7 +281,7 @@ struct NTAG215File {
u8 application_id_byte; u8 application_id_byte;
u8 unknown; u8 unknown;
std::array<u32, 0x7> unknown2; std::array<u32, 0x7> unknown2;
u32_be application_area_crc; u32_be register_info_crc;
ApplicationArea application_area; // Encrypted Game data ApplicationArea application_area; // Encrypted Game data
HashData hmac_tag; // Hash HashData hmac_tag; // Hash
UniqueSerialNumber uid; // Unique serial number UniqueSerialNumber uid; // Unique serial number