|
|
@ -25,7 +25,8 @@ bool IsAmiiboValid(const EncryptedNTAG215File& ntag_file) { |
|
|
LOG_DEBUG(Service_NFP, "character_id=0x{0:x}", amiibo_data.model_info.character_id); |
|
|
LOG_DEBUG(Service_NFP, "character_id=0x{0:x}", amiibo_data.model_info.character_id); |
|
|
LOG_DEBUG(Service_NFP, "character_variant={}", amiibo_data.model_info.character_variant); |
|
|
LOG_DEBUG(Service_NFP, "character_variant={}", amiibo_data.model_info.character_variant); |
|
|
LOG_DEBUG(Service_NFP, "amiibo_type={}", amiibo_data.model_info.amiibo_type); |
|
|
LOG_DEBUG(Service_NFP, "amiibo_type={}", amiibo_data.model_info.amiibo_type); |
|
|
LOG_DEBUG(Service_NFP, "model_number=0x{0:x}", amiibo_data.model_info.model_number); |
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_NFP, "model_number=0x{0:x}", |
|
|
|
|
|
static_cast<u16>(amiibo_data.model_info.model_number)); |
|
|
LOG_DEBUG(Service_NFP, "series={}", amiibo_data.model_info.series); |
|
|
LOG_DEBUG(Service_NFP, "series={}", amiibo_data.model_info.series); |
|
|
LOG_DEBUG(Service_NFP, "fixed_value=0x{0:x}", amiibo_data.model_info.constant_value); |
|
|
LOG_DEBUG(Service_NFP, "fixed_value=0x{0:x}", amiibo_data.model_info.constant_value); |
|
|
|
|
|
|
|
|
@ -35,11 +36,12 @@ bool IsAmiiboValid(const EncryptedNTAG215File& ntag_file) { |
|
|
|
|
|
|
|
|
// Validate UUID
|
|
|
// Validate UUID
|
|
|
constexpr u8 CT = 0x88; // As defined in `ISO / IEC 14443 - 3`
|
|
|
constexpr u8 CT = 0x88; // As defined in `ISO / IEC 14443 - 3`
|
|
|
if ((CT ^ ntag_file.uuid[0] ^ ntag_file.uuid[1] ^ ntag_file.uuid[2]) != ntag_file.uuid[3]) { |
|
|
|
|
|
|
|
|
if ((CT ^ ntag_file.uuid.uid[0] ^ ntag_file.uuid.uid[1] ^ ntag_file.uuid.uid[2]) != |
|
|
|
|
|
ntag_file.uuid.uid[3]) { |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
if ((ntag_file.uuid[4] ^ ntag_file.uuid[5] ^ ntag_file.uuid[6] ^ ntag_file.uuid[7]) != |
|
|
|
|
|
ntag_file.uuid[8]) { |
|
|
|
|
|
|
|
|
if ((ntag_file.uuid.uid[4] ^ ntag_file.uuid.uid[5] ^ ntag_file.uuid.uid[6] ^ |
|
|
|
|
|
ntag_file.uuid.nintendo_id) != ntag_file.uuid.lock_bytes[0]) { |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -70,7 +72,8 @@ bool IsAmiiboValid(const EncryptedNTAG215File& ntag_file) { |
|
|
NTAG215File NfcDataToEncodedData(const EncryptedNTAG215File& nfc_data) { |
|
|
NTAG215File NfcDataToEncodedData(const EncryptedNTAG215File& nfc_data) { |
|
|
NTAG215File encoded_data{}; |
|
|
NTAG215File encoded_data{}; |
|
|
|
|
|
|
|
|
memcpy(encoded_data.uuid2.data(), nfc_data.uuid.data() + 0x8, sizeof(encoded_data.uuid2)); |
|
|
|
|
|
|
|
|
encoded_data.uid = nfc_data.uuid.uid; |
|
|
|
|
|
encoded_data.nintendo_id = nfc_data.uuid.nintendo_id; |
|
|
encoded_data.static_lock = nfc_data.static_lock; |
|
|
encoded_data.static_lock = nfc_data.static_lock; |
|
|
encoded_data.compability_container = nfc_data.compability_container; |
|
|
encoded_data.compability_container = nfc_data.compability_container; |
|
|
encoded_data.hmac_data = nfc_data.user_memory.hmac_data; |
|
|
encoded_data.hmac_data = nfc_data.user_memory.hmac_data; |
|
|
@ -85,7 +88,7 @@ NTAG215File NfcDataToEncodedData(const EncryptedNTAG215File& nfc_data) { |
|
|
encoded_data.hash = nfc_data.user_memory.hash; |
|
|
encoded_data.hash = nfc_data.user_memory.hash; |
|
|
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; |
|
|
memcpy(encoded_data.uuid.data(), nfc_data.uuid.data(), sizeof(encoded_data.uuid)); |
|
|
|
|
|
|
|
|
encoded_data.lock_bytes = nfc_data.uuid.lock_bytes; |
|
|
encoded_data.model_info = nfc_data.user_memory.model_info; |
|
|
encoded_data.model_info = nfc_data.user_memory.model_info; |
|
|
encoded_data.keygen_salt = nfc_data.user_memory.keygen_salt; |
|
|
encoded_data.keygen_salt = nfc_data.user_memory.keygen_salt; |
|
|
encoded_data.dynamic_lock = nfc_data.dynamic_lock; |
|
|
encoded_data.dynamic_lock = nfc_data.dynamic_lock; |
|
|
@ -99,8 +102,9 @@ NTAG215File NfcDataToEncodedData(const EncryptedNTAG215File& nfc_data) { |
|
|
EncryptedNTAG215File EncodedDataToNfcData(const NTAG215File& encoded_data) { |
|
|
EncryptedNTAG215File EncodedDataToNfcData(const NTAG215File& encoded_data) { |
|
|
EncryptedNTAG215File nfc_data{}; |
|
|
EncryptedNTAG215File nfc_data{}; |
|
|
|
|
|
|
|
|
memcpy(nfc_data.uuid.data() + 0x8, encoded_data.uuid2.data(), sizeof(encoded_data.uuid2)); |
|
|
|
|
|
memcpy(nfc_data.uuid.data(), encoded_data.uuid.data(), sizeof(encoded_data.uuid)); |
|
|
|
|
|
|
|
|
nfc_data.uuid.uid = encoded_data.uid; |
|
|
|
|
|
nfc_data.uuid.nintendo_id = encoded_data.nintendo_id; |
|
|
|
|
|
nfc_data.uuid.lock_bytes = encoded_data.lock_bytes; |
|
|
nfc_data.static_lock = encoded_data.static_lock; |
|
|
nfc_data.static_lock = encoded_data.static_lock; |
|
|
nfc_data.compability_container = encoded_data.compability_container; |
|
|
nfc_data.compability_container = encoded_data.compability_container; |
|
|
nfc_data.user_memory.hmac_data = encoded_data.hmac_data; |
|
|
nfc_data.user_memory.hmac_data = encoded_data.hmac_data; |
|
|
@ -127,10 +131,10 @@ EncryptedNTAG215File EncodedDataToNfcData(const NTAG215File& encoded_data) { |
|
|
|
|
|
|
|
|
u32 GetTagPassword(const TagUuid& uuid) { |
|
|
u32 GetTagPassword(const TagUuid& uuid) { |
|
|
// Verifiy that the generated password is correct
|
|
|
// Verifiy that the generated password is correct
|
|
|
u32 password = 0xAA ^ (uuid[1] ^ uuid[3]); |
|
|
|
|
|
password &= (0x55 ^ (uuid[2] ^ uuid[4])) << 8; |
|
|
|
|
|
password &= (0xAA ^ (uuid[3] ^ uuid[5])) << 16; |
|
|
|
|
|
password &= (0x55 ^ (uuid[4] ^ uuid[6])) << 24; |
|
|
|
|
|
|
|
|
u32 password = 0xAA ^ (uuid.uid[1] ^ uuid.uid[3]); |
|
|
|
|
|
password &= (0x55 ^ (uuid.uid[2] ^ uuid.uid[4])) << 8; |
|
|
|
|
|
password &= (0xAA ^ (uuid.uid[3] ^ uuid.uid[5])) << 16; |
|
|
|
|
|
password &= (0x55 ^ (uuid.uid[4] ^ uuid.uid[6])) << 24; |
|
|
return password; |
|
|
return password; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -138,15 +142,13 @@ HashSeed GetSeed(const NTAG215File& data) { |
|
|
HashSeed seed{ |
|
|
HashSeed seed{ |
|
|
.magic = data.write_counter, |
|
|
.magic = data.write_counter, |
|
|
.padding = {}, |
|
|
.padding = {}, |
|
|
.uuid1 = {}, |
|
|
|
|
|
.uuid2 = {}, |
|
|
|
|
|
|
|
|
.uid_1 = data.uid, |
|
|
|
|
|
.nintendo_id_1 = data.nintendo_id, |
|
|
|
|
|
.uid_2 = data.uid, |
|
|
|
|
|
.nintendo_id_2 = data.nintendo_id, |
|
|
.keygen_salt = data.keygen_salt, |
|
|
.keygen_salt = data.keygen_salt, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// Copy the first 8 bytes of uuid
|
|
|
|
|
|
memcpy(seed.uuid1.data(), data.uuid.data(), sizeof(seed.uuid1)); |
|
|
|
|
|
memcpy(seed.uuid2.data(), data.uuid.data(), sizeof(seed.uuid2)); |
|
|
|
|
|
|
|
|
|
|
|
return seed; |
|
|
return seed; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -165,8 +167,10 @@ std::vector<u8> GenerateInternalKey(const InternalKey& key, const HashSeed& seed |
|
|
output.insert(output.end(), key.magic_bytes.begin(), |
|
|
output.insert(output.end(), key.magic_bytes.begin(), |
|
|
key.magic_bytes.begin() + key.magic_length); |
|
|
key.magic_bytes.begin() + key.magic_length); |
|
|
|
|
|
|
|
|
output.insert(output.end(), seed.uuid1.begin(), seed.uuid1.end()); |
|
|
|
|
|
output.insert(output.end(), seed.uuid2.begin(), seed.uuid2.end()); |
|
|
|
|
|
|
|
|
output.insert(output.end(), seed.uid_1.begin(), seed.uid_1.end()); |
|
|
|
|
|
output.emplace_back(seed.nintendo_id_1); |
|
|
|
|
|
output.insert(output.end(), seed.uid_2.begin(), seed.uid_2.end()); |
|
|
|
|
|
output.emplace_back(seed.nintendo_id_2); |
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < sizeof(seed.keygen_salt); i++) { |
|
|
for (std::size_t i = 0; i < sizeof(seed.keygen_salt); i++) { |
|
|
output.emplace_back(static_cast<u8>(seed.keygen_salt[i] ^ key.xor_pad[i])); |
|
|
output.emplace_back(static_cast<u8>(seed.keygen_salt[i] ^ key.xor_pad[i])); |
|
|
@ -250,14 +254,15 @@ void Cipher(const DerivedKeys& keys, const NTAG215File& in_data, NTAG215File& ou |
|
|
reinterpret_cast<unsigned char*>(&out_data.settings)); |
|
|
reinterpret_cast<unsigned char*>(&out_data.settings)); |
|
|
|
|
|
|
|
|
// Copy the rest of the data directly
|
|
|
// Copy the rest of the data directly
|
|
|
out_data.uuid2 = in_data.uuid2; |
|
|
|
|
|
|
|
|
out_data.uid = in_data.uid; |
|
|
|
|
|
out_data.nintendo_id = in_data.nintendo_id; |
|
|
|
|
|
out_data.lock_bytes = in_data.lock_bytes; |
|
|
out_data.static_lock = in_data.static_lock; |
|
|
out_data.static_lock = in_data.static_lock; |
|
|
out_data.compability_container = in_data.compability_container; |
|
|
out_data.compability_container = in_data.compability_container; |
|
|
|
|
|
|
|
|
out_data.constant_value = in_data.constant_value; |
|
|
out_data.constant_value = in_data.constant_value; |
|
|
out_data.write_counter = in_data.write_counter; |
|
|
out_data.write_counter = in_data.write_counter; |
|
|
|
|
|
|
|
|
out_data.uuid = in_data.uuid; |
|
|
|
|
|
out_data.model_info = in_data.model_info; |
|
|
out_data.model_info = in_data.model_info; |
|
|
out_data.keygen_salt = in_data.keygen_salt; |
|
|
out_data.keygen_salt = in_data.keygen_salt; |
|
|
out_data.dynamic_lock = in_data.dynamic_lock; |
|
|
out_data.dynamic_lock = in_data.dynamic_lock; |
|
|
@ -309,7 +314,7 @@ bool DecodeAmiibo(const EncryptedNTAG215File& encrypted_tag_data, NTAG215File& t |
|
|
// Regenerate tag HMAC. Note: order matters, data HMAC depends on tag HMAC!
|
|
|
// Regenerate tag HMAC. Note: order matters, data HMAC depends on tag HMAC!
|
|
|
constexpr std::size_t input_length = DYNAMIC_LOCK_START - UUID_START; |
|
|
constexpr std::size_t input_length = DYNAMIC_LOCK_START - UUID_START; |
|
|
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), tag_keys.hmac_key.data(), |
|
|
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), tag_keys.hmac_key.data(), |
|
|
sizeof(HmacKey), reinterpret_cast<const unsigned char*>(&tag_data.uuid), |
|
|
|
|
|
|
|
|
sizeof(HmacKey), reinterpret_cast<const unsigned char*>(&tag_data.uid), |
|
|
input_length, reinterpret_cast<unsigned char*>(&tag_data.hmac_tag)); |
|
|
input_length, reinterpret_cast<unsigned char*>(&tag_data.hmac_tag)); |
|
|
|
|
|
|
|
|
// Regenerate data HMAC
|
|
|
// Regenerate data HMAC
|
|
|
@ -350,7 +355,7 @@ bool EncodeAmiibo(const NTAG215File& tag_data, EncryptedNTAG215File& encrypted_t |
|
|
constexpr std::size_t input_length = DYNAMIC_LOCK_START - UUID_START; |
|
|
constexpr std::size_t input_length = DYNAMIC_LOCK_START - UUID_START; |
|
|
constexpr std::size_t input_length2 = HMAC_TAG_START - WRITE_COUNTER_START; |
|
|
constexpr std::size_t input_length2 = HMAC_TAG_START - WRITE_COUNTER_START; |
|
|
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), tag_keys.hmac_key.data(), |
|
|
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), tag_keys.hmac_key.data(), |
|
|
sizeof(HmacKey), reinterpret_cast<const unsigned char*>(&tag_data.uuid), |
|
|
|
|
|
|
|
|
sizeof(HmacKey), reinterpret_cast<const unsigned char*>(&tag_data.uid), |
|
|
input_length, reinterpret_cast<unsigned char*>(&encoded_tag_data.hmac_tag)); |
|
|
input_length, reinterpret_cast<unsigned char*>(&encoded_tag_data.hmac_tag)); |
|
|
|
|
|
|
|
|
// Init mbedtls HMAC context
|
|
|
// Init mbedtls HMAC context
|
|
|
@ -364,7 +369,7 @@ bool EncodeAmiibo(const NTAG215File& tag_data, EncryptedNTAG215File& encrypted_t |
|
|
input_length2); // Data
|
|
|
input_length2); // Data
|
|
|
mbedtls_md_hmac_update(&ctx, reinterpret_cast<unsigned char*>(&encoded_tag_data.hmac_tag), |
|
|
mbedtls_md_hmac_update(&ctx, reinterpret_cast<unsigned char*>(&encoded_tag_data.hmac_tag), |
|
|
sizeof(HashData)); // Tag HMAC
|
|
|
sizeof(HashData)); // Tag HMAC
|
|
|
mbedtls_md_hmac_update(&ctx, reinterpret_cast<const unsigned char*>(&tag_data.uuid), |
|
|
|
|
|
|
|
|
mbedtls_md_hmac_update(&ctx, reinterpret_cast<const unsigned char*>(&tag_data.uid), |
|
|
input_length); |
|
|
input_length); |
|
|
mbedtls_md_hmac_finish(&ctx, reinterpret_cast<unsigned char*>(&encoded_tag_data.hmac_data)); |
|
|
mbedtls_md_hmac_finish(&ctx, reinterpret_cast<unsigned char*>(&encoded_tag_data.hmac_data)); |
|
|
|
|
|
|
|
|
|