2017-07-05 08:58:33 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <crypto/aes.h>
|
2017-07-06 03:30:50 +00:00
|
|
|
#include <fnd/io.h>
|
2017-08-05 13:09:50 +00:00
|
|
|
#include <fnd/MemoryBlob.h>
|
2018-04-07 08:01:11 +00:00
|
|
|
#include <fnd/SimpleTextOutput.h>
|
2017-07-05 08:58:33 +00:00
|
|
|
#include <nx/NXCrypto.h>
|
|
|
|
#include <nx/NcaHeader.h>
|
2018-04-24 05:24:20 +00:00
|
|
|
#include <nx/NcaUtils.h>
|
2018-04-07 08:01:11 +00:00
|
|
|
#include <nx/PfsHeader.h>
|
2017-07-05 08:58:33 +00:00
|
|
|
#include <inttypes.h>
|
2017-07-18 14:17:32 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <direct.h>
|
|
|
|
#else
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2018-03-21 12:31:39 +00:00
|
|
|
std::string kFormatVersionStr[]
|
|
|
|
{
|
|
|
|
"NCA2",
|
|
|
|
"NCA3"
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string kDistributionTypeStr[]
|
|
|
|
{
|
|
|
|
"Download",
|
|
|
|
"Game Card"
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string kContentTypeStr[]
|
|
|
|
{
|
|
|
|
"Program",
|
|
|
|
"Meta",
|
|
|
|
"Control",
|
|
|
|
"Manual",
|
|
|
|
"Data"
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string kEncryptionTypeStr[]
|
|
|
|
{
|
|
|
|
"Auto",
|
|
|
|
"None",
|
|
|
|
"AesXts",
|
|
|
|
"AesCtr",
|
2018-04-07 08:01:11 +00:00
|
|
|
"AesCtrEx"
|
2018-03-21 12:31:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string kHashTypeStr[]
|
|
|
|
{
|
|
|
|
"Auto",
|
|
|
|
"UNKNOWN_1",
|
|
|
|
"HierarchicalSha256",
|
|
|
|
"HierarchicalIntegrity"
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string kFormatTypeStr[]
|
|
|
|
{
|
|
|
|
"RomFs",
|
|
|
|
"PartitionFs"
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string kKaekIndexStr[]
|
|
|
|
{
|
|
|
|
"Application",
|
|
|
|
"Ocean",
|
|
|
|
"System"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
enum KeysetType
|
|
|
|
{
|
|
|
|
KEYSET_DEV,
|
|
|
|
KEYSET_PROD
|
|
|
|
};
|
|
|
|
|
|
|
|
static const byte_t* kNcaHeaderKey[2][2] =
|
|
|
|
{
|
|
|
|
{ crypto::aes::nx::dev::nca_header_key[0], crypto::aes::nx::dev::nca_header_key[1] },
|
|
|
|
{ crypto::aes::nx::prod::nca_header_key[0], crypto::aes::nx::prod::nca_header_key[1] }
|
|
|
|
};
|
|
|
|
|
2018-04-24 05:24:20 +00:00
|
|
|
inline size_t sectorToOffset(size_t sector_index) { return nx::NcaUtils::sectorToOffset(sector_index); }
|
2017-07-05 08:58:33 +00:00
|
|
|
|
2018-03-22 05:29:52 +00:00
|
|
|
void initNcaCtr(byte_t ctr[crypto::aes::kAesBlockSize], uint32_t generation)
|
2017-07-05 08:58:33 +00:00
|
|
|
{
|
|
|
|
memset(ctr, 0, crypto::aes::kAesBlockSize);
|
|
|
|
for (size_t i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
ctr[7 - i] = (generation >> i * 8) & 0xff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 08:01:11 +00:00
|
|
|
void decryptNcaHeader(byte_t header[nx::nca::kHeaderSize], const byte_t* key[2])
|
2018-03-18 09:08:42 +00:00
|
|
|
{
|
2018-04-24 05:24:20 +00:00
|
|
|
crypto::aes::sAesXts128Key a;
|
|
|
|
a.set(key[0],key[1]);
|
|
|
|
nx::NcaUtils::decryptNcaHeader(header, header, a);
|
2017-07-18 14:17:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 12:31:39 +00:00
|
|
|
bool testNcaHeaderKey(const byte_t* header_src, const byte_t* key[2])
|
2017-07-18 14:17:32 +00:00
|
|
|
{
|
2018-03-21 12:31:39 +00:00
|
|
|
bool validKey = false;
|
2018-04-07 08:01:11 +00:00
|
|
|
byte_t header_dec[nx::nca::kSectorSize];
|
2018-03-22 05:29:52 +00:00
|
|
|
byte_t tweak[crypto::aes::kAesBlockSize];
|
2018-03-18 09:08:42 +00:00
|
|
|
|
2018-03-21 12:31:39 +00:00
|
|
|
// try key
|
|
|
|
crypto::aes::AesXtsMakeTweak(tweak, 1);
|
2018-04-07 08:01:11 +00:00
|
|
|
crypto::aes::AesXtsDecryptSector(header_src + sectorToOffset(1), nx::nca::kSectorSize, key[0], key[1], tweak, header_dec);
|
|
|
|
if (memcmp(header_dec, nx::nca::kNca2Sig.c_str(), 4) == 0 || memcmp(header_dec, nx::nca::kNca3Sig.c_str(), 4) == 0)
|
2018-03-21 12:31:39 +00:00
|
|
|
{
|
|
|
|
validKey = true;
|
|
|
|
}
|
2018-03-18 09:08:42 +00:00
|
|
|
|
2018-03-21 12:31:39 +00:00
|
|
|
return validKey;
|
|
|
|
}
|
2018-03-18 09:08:42 +00:00
|
|
|
|
2018-03-21 12:31:39 +00:00
|
|
|
KeysetType getKeysetFromNcaHeader(const byte_t* header_src)
|
2018-03-18 09:08:42 +00:00
|
|
|
{
|
2018-03-21 12:31:39 +00:00
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
if (testNcaHeaderKey(header_src, kNcaHeaderKey[i]) == true)
|
|
|
|
{
|
|
|
|
return (KeysetType)i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw fnd::Exception("Failed to determine NCA header key");
|
|
|
|
}
|
2017-07-18 14:17:32 +00:00
|
|
|
|
2018-04-07 08:01:11 +00:00
|
|
|
void printHeader(const byte_t* header)
|
|
|
|
{
|
|
|
|
nx::NcaHeader hdr;
|
|
|
|
hdr.importBinary(header + sectorToOffset(1), nx::nca::kSectorSize);
|
|
|
|
|
|
|
|
printf("[NCA Header]\n");
|
|
|
|
printf(" Format Type: %s\n", kFormatVersionStr[hdr.getFormatVersion()].c_str());
|
|
|
|
printf(" Dist. Type: %s\n", kDistributionTypeStr[hdr.getDistributionType()].c_str());
|
|
|
|
printf(" Content Type: %s\n", kContentTypeStr[hdr.getContentType()].c_str());
|
|
|
|
printf(" Key Generation: %d\n", hdr.getKeyGeneration());
|
|
|
|
printf(" Kaek Index: %s (%d)\n", kKaekIndexStr[hdr.getKaekIndex()].c_str(), hdr.getKaekIndex());
|
|
|
|
printf(" Size: 0x%" PRIx64 "\n", hdr.getContentSize());
|
|
|
|
printf(" ProgID: 0x%016" PRIx64 "\n", hdr.getProgramId());
|
|
|
|
printf(" Content Index: %" PRIu32 "\n", hdr.getContentIndex());
|
|
|
|
uint32_t ver = hdr.getSdkAddonVersion();
|
|
|
|
printf(" SdkAddon Ver.: v%d.%d.%d (v%" PRIu32 ")\n", (ver>>24 & 0xff),(ver>>16 & 0xff),(ver>>8 & 0xff), ver);
|
|
|
|
printf(" RightsId: ");
|
|
|
|
fnd::SimpleTextOutput::hexDump(hdr.getRightsId(), 0x10);
|
|
|
|
printf("\n");
|
|
|
|
printf(" Encrypted Key Area:\n");
|
|
|
|
crypto::aes::sAes128Key zero_key;
|
|
|
|
memset(zero_key.key, 0, sizeof(zero_key));
|
|
|
|
for (size_t i = 0; i < hdr.getEncAesKeys().getSize(); i++)
|
|
|
|
{
|
|
|
|
if (hdr.getEncAesKeys()[i] != zero_key)
|
|
|
|
{
|
|
|
|
printf(" %2lu: ", i);
|
|
|
|
fnd::SimpleTextOutput::hexDump(hdr.getEncAesKeys()[i].key, crypto::aes::kAes128KeySize);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" Sections:\n");
|
|
|
|
for (size_t i = 0; i < hdr.getPartitions().getSize(); i++)
|
|
|
|
{
|
|
|
|
const nx::NcaHeader::sPartition& partition = hdr.getPartitions()[i];
|
|
|
|
printf(" %lu:\n", i);
|
|
|
|
//printf(" Start Blk: %" PRId32 "\n", partition.start_blk);
|
|
|
|
//printf(" End Blk: %" PRId32 "\n", partition.end_blk);
|
|
|
|
printf(" Index: %d\n", partition.index);
|
|
|
|
printf(" Offset: 0x%" PRIx64 "\n", partition.offset);
|
|
|
|
printf(" Size: 0x%" PRIx64 "\n", partition.size);
|
|
|
|
|
|
|
|
|
|
|
|
size_t sector_index = 2 + partition.index;
|
|
|
|
|
|
|
|
crypto::sha::sSha256Hash ncaFsHeaderHash;
|
|
|
|
crypto::sha::Sha256(header + sectorToOffset(sector_index), nx::nca::kSectorSize, ncaFsHeaderHash.bytes);
|
|
|
|
if (partition.hash.compare(ncaFsHeaderHash) == false)
|
|
|
|
{
|
|
|
|
throw fnd::Exception("ncatool", "NcaFsHeader has bad sha256 hash");
|
|
|
|
}
|
|
|
|
|
|
|
|
const nx::sNcaFsHeader* fsHdr = (const nx::sNcaFsHeader*)(header + sectorToOffset(sector_index));
|
|
|
|
printf(" FsHeader:\n");
|
|
|
|
printf(" Version: 0x%d\n", fsHdr->version.get());
|
|
|
|
printf(" Format Type: %s\n", kFormatTypeStr[fsHdr->format_type].c_str());
|
|
|
|
printf(" Hash Type: %s\n", kHashTypeStr[fsHdr->hash_type].c_str());
|
|
|
|
printf(" Enc. Type: %s\n", kEncryptionTypeStr[fsHdr->encryption_type].c_str());
|
2018-04-24 05:24:20 +00:00
|
|
|
if (fsHdr->hash_type == nx::nca::HASH_HIERARCHICAL_INTERGRITY)
|
2018-04-07 08:01:11 +00:00
|
|
|
{
|
2018-04-24 05:24:20 +00:00
|
|
|
const nx::sIvfcHeader* hash_hdr = (const nx::sIvfcHeader*)(header + sectorToOffset(sector_index) + sizeof(nx::sNcaFsHeader));
|
|
|
|
//printf(" HashHierarchicalIntegrity Header:\n");
|
|
|
|
//printf(" ")
|
|
|
|
|
2018-04-07 08:01:11 +00:00
|
|
|
}
|
2018-04-24 05:24:20 +00:00
|
|
|
else if (fsHdr->hash_type == nx::nca::HASH_HIERARCHICAL_SHA256)
|
2018-04-07 08:01:11 +00:00
|
|
|
{
|
2018-04-24 05:24:20 +00:00
|
|
|
const nx::sHierarchicalSha256Header* hash_hdr = (const nx::sHierarchicalSha256Header*)(header + sectorToOffset(sector_index) + sizeof(nx::sNcaFsHeader));
|
|
|
|
printf(" HashHierarchicalSha256 Header:\n");
|
|
|
|
printf(" Master Hash: ");
|
|
|
|
fnd::SimpleTextOutput::hexDump(hash_hdr->master_hash, 0x20);
|
|
|
|
printf(" HashBlockSize: 0x%x\n", hash_hdr->hash_block_size.get());
|
|
|
|
printf(" Unknown: 0x%x\n", hash_hdr->unk_0x02.get());
|
|
|
|
printf(" HashDataOffset: 0x%" PRIx64 "\n", hash_hdr->hash_data.offset.get());
|
|
|
|
printf(" HashDataSize: 0x%" PRIx64 "\n", hash_hdr->hash_data.size.get());
|
|
|
|
printf(" HashTargetOffset: 0x%" PRIx64 "\n", hash_hdr->hash_target.offset.get());
|
|
|
|
printf(" HashTargetSize: 0x%" PRIx64 "\n", hash_hdr->hash_target.size.get());
|
2018-04-07 08:01:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 08:58:33 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
printf("usage: ncatool <nca file>\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:55:58 +00:00
|
|
|
try
|
2017-07-05 08:58:33 +00:00
|
|
|
{
|
2017-07-06 10:55:58 +00:00
|
|
|
fnd::MemoryBlob nca;
|
2018-04-07 08:01:11 +00:00
|
|
|
fnd::io::readFile(argv[1], 0x0, nx::nca::kHeaderSize, nca);
|
2017-07-05 08:58:33 +00:00
|
|
|
|
2018-03-21 12:31:39 +00:00
|
|
|
KeysetType keyset = getKeysetFromNcaHeader(nca.getBytes());
|
2018-03-18 09:08:42 +00:00
|
|
|
|
2018-03-21 12:31:39 +00:00
|
|
|
decryptNcaHeader(nca.getBytes(), kNcaHeaderKey[keyset]);
|
2017-07-05 08:58:33 +00:00
|
|
|
|
2018-04-07 08:01:11 +00:00
|
|
|
printHeader(nca.getBytes());
|
2017-07-06 10:55:58 +00:00
|
|
|
} catch (const fnd::Exception& e)
|
|
|
|
{
|
2017-07-07 09:57:38 +00:00
|
|
|
printf("%s\n",e.what());
|
2017-07-05 08:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|