From 9dfa69446d8a16b5ea06607d39c5e9248cf3c6a6 Mon Sep 17 00:00:00 2001 From: jakcron Date: Wed, 5 Jul 2017 18:57:14 +1000 Subject: [PATCH] [nx] Add mined keys and NcaHeader --- lib/nx/NXCrypto.h | 120 ++++++++++++++++++++++++++++++++ lib/nx/NcaHeader.cpp | 143 ++++++++++++++++++++++++++++++++++++++ lib/nx/NcaHeader.h | 124 +++++++++++++++++++++++++++++++++ lib/nx/nx.vcxproj | 9 ++- lib/nx/nx.vcxproj.filters | 13 ++++ 5 files changed, 407 insertions(+), 2 deletions(-) create mode 100644 lib/nx/NXCrypto.h create mode 100644 lib/nx/NcaHeader.cpp create mode 100644 lib/nx/NcaHeader.h diff --git a/lib/nx/NXCrypto.h b/lib/nx/NXCrypto.h new file mode 100644 index 0000000..56390fe --- /dev/null +++ b/lib/nx/NXCrypto.h @@ -0,0 +1,120 @@ +#pragma once + +// AES +namespace nx +{ + namespace crypto + { + /* + Keys mentioned in Content Archive Authoring library + private RsaKey m_NcaHeader1SignKey; + private RsaKey m_AcidSignKey; + private RsaKey m_NrrCertificateSignKey; + private AesKey m_ProdKeyAreaEncryptionKey; + private AesKey m_ProdNcaHeaderEncryptionKek; + private RsaKey m_ProdXciHeaderSignKey; + private AesKey m_ProdXciInitialDataEncryptionKey; + private AesKey m_ProdTitleKeyGenarateKey; + private AesKey m_ProdETicketCommonKey; + private RsaKey m_ProdETicketSignKey; + */ + namespace aes + { + + /* + public static AesKey GetNcaExternalContentKey(IHashCalculator hashCalculator, ulong titleId) + { + Trace.Assert(hashCalculator != null, "IHashCalculator is null."); + byte[] array = BitConverter.GetBytes(titleId).Reverse().ToArray(); + byte[] numArray = new byte[hashCalculator.HashSize]; + hashCalculator.CalculateHash(array, 0, (int)array.Length, numArray, 0); + return new AesKey(((IEnumerable)numArray).Where((byte name, int index) => index < 16).ToArray()); + } + */ + enum Aes128KeyId + { + NcaContentKey, + NcaHeader, + XciInitialData, + XciHeader, + ETicketCommonKey + }; + + enum HmacSha256KeyId + { + TitleKeyGenarateKey + }; + + // aes128-cbc keys + u8 xci_header_key[16] = { 0x01, 0xc5, 0x8f, 0xe7, 0x2d, 0x13, 0x5a, 0xb2, 0x9a, 0x3f, 0x69, 0x33, 0x95, 0x74, 0xb1 }; + u8 eticket_common_key[16] = { 0x55, 0xA3, 0xF8, 0x72, 0xBD, 0xC8, 0x0C, 0x55, 0x5A, 0x65, 0x43, 0x81, 0x13, 0x9E, 0x15, 0x3B }; // lol this 3ds dev common key + + // aes128-xts keys (seem to use 512 block sizes, aka 0x200 blocks) + u8 nca_body_key[2][16] = + { + { 0xE5, 0x64, 0xDB, 0xFE, 0x52, 0x93, 0x3A, 0x65, 0x3C, 0x8B, 0x5E, 0xF8, 0x2C, 0x9D, 0xF4, 0xB5 }, + { 0x60, 0x7B, 0x77, 0x3E, 0x31, 0xE9, 0x6A, 0x8F, 0x8E, 0x44, 0x5C, 0x98, 0x71, 0xC0, 0x57, 0xDB }, + }; + + u8 nca_header_key[2][16] = + { + { 0xCB, 0x9A, 0x93, 0x9F, 0x82, 0x72, 0x54, 0x4A, 0x74, 0x5D, 0x28, 0x46, 0x9D, 0xCC, 0x38, 0x12 }, + { 0x06, 0x31, 0x27, 0x06, 0xAE, 0x62, 0x56, 0x8C, 0x5B, 0x7E, 0xE6, 0x9F, 0x7E, 0x01, 0x02, 0x24 }, + }; + + u8 nca_otherkey[2][16] = + { + { 0x5A, 0x3E, 0xD8, 0x4F, 0xDE, 0xC0, 0xD8, 0x26, 0x31, 0xF7, 0xE2, 0x5D, 0x19, 0x7B, 0xF5, 0xD0 }, + { 0x1C, 0x9B, 0x7B, 0xFA, 0xF6, 0x28, 0x18, 0x3D, 0x71, 0xF6, 0x4D, 0x73, 0xF1, 0x50, 0xB9, 0xD2 } + }; + // aeskey, related to m_KeyAreaEncryptionKeyList (first in list?) + u8 unk_aes_key[0x10] = { 0x3A, 0x7C, 0x3E, 0x38, 0x4A, 0x8F, 0x22, 0xFF, 0x4B, 0x21, 0x57, 0x19, 0xB7, 0x81, 0xAD, 0x0C }; + } + + // RSA + namespace rsa + { + enum Rsa2048PssKeyId // sign/validate + { + NcaHeader1, // also reference as "sign key data" + Acid, + NrrCertificate + }; + + enum Rsa2048Pkcs1KeyId // sign/validate + { + XciHeader, + ETicket // XS00000020 or XS00000021 ident + }; + + enum RsaOaepKeyId // encrypt/decrypt + { + XciKeyArea + }; + + /* + in regards to etickets + %llx....string too long.invalid string position.CT..TW..NG..VP..PC..%08X....Contents of ticket at 0x%0*lx:.. Ticket ID = 0x%I64x... Version = %hd... Rights ID..... %02hhx..... Device ID = 0x%I64x... Issuer = %s.... SigType = 0x%0x..... AccountId = 0x%x.. LicenseType = %hhd.... CommonKeyId = %hhd.... PropertyMask = 0x%hx.. Title Key (encrypted and may be personalized):. .... sectTotalSize %d.. sectHdrOffset %d.. nSectHdrs %hd..... nSectHdrEntrySize %hd.....V1 Section Header %d:... sectOffset %d..... nRecords %d... recordSize %d..... sectionSize %d....PERMANENT...SUBSCRIPTION....CONTENT.CONTENT CONSUMPTION.ACCESS TITLE....Unknown.LIMITED RESOURCE.... sectionType %s.... V1 Section Records:... refId ..%02hhx .. refIdAttr %x... limit %x.... offset %x... accessMask:..... .... index %hx... code %hx.... accessTitleId %I64x..... accessTitleMask %I64x... Unknown Record Type %hx............ + */ + + // unknown key "EncryptionKeyData" + u8 unk_rsa_key_mod[0x100] = { 0x85, 0xF8, 0x8C, 0x41, 0x8A, 0x2D, 0x96, 0xB3, 0xF3, 0x92, 0xD0, 0x6C, 0x9D, 0x90, 0x96, 0x7F, 0x1A, 0x78, 0xD8, 0x17, 0x7A, 0xB8, 0xEC, 0x0E, 0x6E, 0x10, 0x55, 0xC8, 0xCA, 0x7F, 0x18, 0xC7, 0x18, 0x94, 0x75, 0x0D, 0xBA, 0x19, 0x44, 0xDE, 0xFA, 0x93, 0xC8, 0xCB, 0x5F, 0xCA, 0x9B, 0x09, 0x3C, 0x2E, 0x2D, 0x12, 0xB2, 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06, 0x30, 0x5B, 0xCA, 0xF4, 0xEB, 0x72, 0x20, 0xD3, 0x15, 0x15, 0xC0, 0xC7, 0x1A, 0x08, 0xAE, 0x6E, 0xB2, 0x02, 0x43, 0xE9 }; + + // rsa2048-oaep-sha256 key + u8 xci_keyarea_labledigest[0x20] = { 0xB1, 0x7B, 0x5F, 0x65, 0x0E, 0x75, 0x90, 0xED, 0xFE, 0x3A, 0xF0, 0x75, 0x0B, 0x92, 0x05, 0xB3, 0x0E, 0x7D, 0x49, 0x89, 0x24, 0xD8, 0xE3, 0x85, 0xCB, 0x40, 0xE0, 0xE7, 0xF8, 0xDE, 0xF0, 0x77 }; + u8 xci_keyarea_mod[0x100] = { 0xA6, 0x1A, 0x3F, 0x5E, 0x16, 0x94, 0x4D, 0x5F, 0x88, 0x07, 0x45, 0x92, 0x42, 0x02, 0x5C, 0xF8, 0xF8, 0x01, 0xA6, 0x8F, 0xF0, 0x77, 0x76, 0x06, 0x06, 0x4B, 0x67, 0x02, 0x35, 0x2F, 0xC2, 0xF6, 0xCD, 0x74, 0xBC, 0x57, 0x36, 0xA0, 0x61, 0x1D, 0xD6, 0x66, 0x35, 0x79, 0x6E, 0xB7, 0xFF, 0xC1, 0x05, 0xC6, 0x59, 0xD9, 0x1E, 0xC9, 0xC3, 0x1F, 0x5B, 0x17, 0xA9, 0x24, 0x46, 0xB3, 0xAB, 0x1D, 0x7B, 0x79, 0xA8, 0xF2, 0x80, 0xC8, 0x6F, 0xA7, 0x9B, 0x2D, 0xE8, 0xBF, 0x91, 0xD7, 0xD4, 0xA2, 0x07, 0x53, 0x98, 0x82, 0x7D, 0x91, 0x8A, 0x68, 0x79, 0x9B, 0xB6, 0x8F, 0xA7, 0x05, 0x7B, 0x2B, 0x6C, 0x09, 0x2A, 0x58, 0x8A, 0x21, 0xE5, 0xB6, 0xCD, 0xAC, 0xCB, 0x84, 0x5F, 0x22, 0x16, 0x82, 0x54, 0x14, 0x03, 0xEB, 0xBB, 0x86, 0xEC, 0x59, 0x36, 0x38, 0xF8, 0x41, 0x41, 0xCA, 0x23, 0x8A, 0xF2, 0x98, 0x01, 0xAD, 0x14, 0xB6, 0x65, 0x15, 0x3B, 0x63, 0x42, 0xC1, 0x3E, 0x55, 0x8C, 0xC2, 0x38, 0x4E, 0xC0, 0x57, 0x5E, 0xFA, 0x2A, 0x2C, 0xC7, 0x9C, 0xBE, 0x5C, 0xD7, 0x56, 0x4D, 0xCF, 0x36, 0x31, 0x93, 0xD2, 0xB1, 0x0A, 0x8C, 0x7C, 0x7F, 0xFE, 0x46, 0x20, 0xE3, 0x91, 0x7C, 0x63, 0x3F, 0x24, 0x81, 0xFF, 0x4C, 0x68, 0x4F, 0xBE, 0x16, 0x68, 0x66, 0x1C, 0x90, 0xCD, 0xFE, 0x8D, 0xC5, 0x63, 0x3B, 0x2D, 0x5C, 0x25, 0x95, 0xA8, 0x4B, 0x3C, 0xAF, 0x57, 0x07, 0x84, 0x31, 0x37, 0xFA, 0x63, 0x98, 0x1B, 0xAC, 0xB2, 0x81, 0xB1, 0x29, 0x67, 0x0A, 0x31, 0x3D, 0x7E, 0x2D, 0x7F, 0xFE, 0xA5, 0x29, 0x3D, 0xBF, 0xD3, 0xCC, 0x9D, 0x73, 0x75, 0x40, 0x8B, 0x79, 0x98, 0x39, 0x8A, 0xAD, 0x90, 0x65, 0x82, 0x90, 0x09, 0xA4, 0x74, 0xA2, 0x3F, 0xEE, 0xC7, 0x75, 0x06, 0x29, 0x85 }; + + } + } +} diff --git a/lib/nx/NcaHeader.cpp b/lib/nx/NcaHeader.cpp new file mode 100644 index 0000000..092fde8 --- /dev/null +++ b/lib/nx/NcaHeader.cpp @@ -0,0 +1,143 @@ +#include "NcaHeader.h" +#include + +void NcaHeader::exportBinary() +{ + // TODO: implement export +} + +void NcaHeader::importBinary(const u8 * bytes) +{ + clearVariables(); + + binary_blob_.alloc(sizeof(sNcaHeader)); + memcpy(binary_blob_.data(), bytes, sizeof(sNcaHeader)); + + sNcaHeader* hdr = (sNcaHeader*)binary_blob_.data(); + + if (memcmp(hdr->signature(), kNcaSig.c_str(), 4) != 0) + { + throw fnd::Exception(kModuleName, "NCA header corrupt"); + } + + block_size_ = hdr->block_size(); + nca_size_ = hdr->nca_size(); + program_id_ = hdr->program_id(); + unk0_ = hdr->unk0(); + + for (size_t i = 0; i < kSectionNum; i++) + { + // determine section index + u8 section = kSectionNum - 1 - i; + + // skip sections that don't exist + if (hdr->section(section).start() == 0 && hdr->section(section).end() == 0) continue; + + // add high level struct + sections_.push_back({ hdr->section(section).start(), hdr->section(section).end(), blockNumToSize(hdr->section(section).start()), blockNumToSize(hdr->section(section).end()- hdr->section(section).start()), hdr->section(section).key_type(), hdr->section_hash(section) }); + } + + for (size_t i = 0; i < kAesKeyNum; i++) + { + aes_keys_.push_back(hdr->aes_key(i)); + } +} + +u64 NcaHeader::getNcaSize() const +{ + return nca_size_; +} + +void NcaHeader::setNcaSize(u64 size) +{ + nca_size_ = size; +} + +u64 NcaHeader::getProgramId() const +{ + return program_id_; +} + +void NcaHeader::setProgramId(u64 program_id) +{ + program_id_ = program_id; +} + +u32 NcaHeader::getUnk() const +{ + return unk0_; +} + +const std::vector& NcaHeader::getSections() const +{ + return sections_; +} + +void NcaHeader::addSection(const sSection & section) +{ + if (sections_.size() >= kSectionNum) + { + throw fnd::Exception(kModuleName, "Too many NCA sections"); + } + sections_.push_back(section); +} + +const std::vector& NcaHeader::getAesKeys() const +{ + return aes_keys_; +} + +void NcaHeader::addKey(const crypto::aes::sAes128Key & key) +{ + if (aes_keys_.size() >= kAesKeyNum) + { + throw fnd::Exception(kModuleName, "Too many NCA aes keys"); + } + + aes_keys_.push_back(key); +} + +void NcaHeader::clearVariables() +{ + block_size_ = 0; + nca_size_ = 0; + program_id_ = 0; + unk0_ = 0; + sections_.clear(); + aes_keys_.clear(); +} + +u64 NcaHeader::blockNumToSize(u32 block_num) const +{ + return block_num*block_size_; +} + +u32 NcaHeader::sizeToBlockNum(u64 real_size) const +{ + return align(real_size, block_size_)/block_size_; +} + +NcaHeader::NcaHeader() +{ + clearVariables(); +} + +NcaHeader::NcaHeader(const NcaHeader & other) +{ + importBinary(other.getBytes()); +} + +NcaHeader::NcaHeader(const u8 * bytes) +{ + importBinary(bytes); +} + +const u8 * NcaHeader::getBytes() const +{ + return binary_blob_.data(); +} + +size_t NcaHeader::getSize() const +{ + return binary_blob_.size(); +} \ No newline at end of file diff --git a/lib/nx/NcaHeader.h b/lib/nx/NcaHeader.h new file mode 100644 index 0000000..c71a233 --- /dev/null +++ b/lib/nx/NcaHeader.h @@ -0,0 +1,124 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +class NcaHeader +{ +public: + struct sSection + { + u32 start_blk; + u32 end_blk; + u64 offset; + u64 size; + u8 key_type; + crypto::sha::sSha256Hash hash; + }; + + NcaHeader(); + NcaHeader(const NcaHeader& other); + NcaHeader(const u8* bytes); + + // to be used after export + const u8* getBytes() const; + size_t getSize() const; + + // export/import binary + void exportBinary(); + void importBinary(const u8* bytes); + + // variables + u64 getNcaSize() const; + void setNcaSize(u64 size); + u64 getProgramId() const; + void setProgramId(u64 program_id); + u32 getUnk() const; + const std::vector& getSections() const; + void addSection(const sSection& section); + const std::vector& getAesKeys() const; + void addKey(const crypto::aes::sAes128Key& key); + +private: + const std::string kModuleName = "NCA_HEADER"; + const std::string kNcaSig = "NCA2"; + static const size_t kSectionNum = 4; + static const size_t kAesKeyNum = 4; + +#pragma pack (push, 1) + + struct sNcaHeader + { + private: + u8 signature_[4]; + u8 reserved_0[2]; + u16 block_size_; + u64 nca_size_; + u64 program_id_; + u8 reserved_1[4]; + u32 unk_0_; + u8 reserved_2[0x20]; + struct sNcaSection + { + private: + u32 start_; // block units + u32 end_; // block units + u8 key_type_; + u8 reserved[7]; + public: + u32 start() const { return le_word(start_); } + void set_start(u32 offset) { start_ = le_word(offset); } + + u32 end() const { return le_word(end_); } + void set_end(u32 offset) { end_ = le_word(offset); } + + u8 key_type() const { return key_type_; } + void set_key_type(u8 key_type) { key_type_ = key_type; } + } section_[kSectionNum]; + crypto::sha::sSha256Hash section_hash_[kSectionNum]; + crypto::aes::sAes128Key aes_keys_[kAesKeyNum]; + public: + const char* signature() const { return (const char*)signature_; } + void set_signature(const char* signature) { memcpy(signature_, signature, 4); } + + u16 block_size() const { return le_hword(block_size_); } + void set_block_size(u16 block_size) { block_size_ = le_hword(block_size); } + + u64 nca_size() const { return le_dword(nca_size_); } + void set_nca_size(u64 nca_size) { nca_size_ = le_dword(nca_size); } + + u64 program_id() const { return le_dword(program_id_); } + void set_program_id(u64 program_id) { program_id_ = le_dword(program_id); } + + u32 unk0() const { return le_word(unk_0_); } + void set_unk0(u32 val) { unk_0_ = le_word(val); } + + const sNcaSection& section(u8 index) const { return section_[index%kSectionNum]; } + sNcaSection& section(u8 index) { return section_[index%kSectionNum]; } + + const crypto::sha::sSha256Hash& section_hash(u8 index) const { return section_hash_[index%kSectionNum]; } + crypto::sha::sSha256Hash& section_hash(u8 index) { return section_hash_[index%kSectionNum]; } + + const crypto::aes::sAes128Key& aes_key(u8 index) const { return aes_keys_[index%kAesKeyNum]; } + crypto::aes::sAes128Key& aes_key(u8 index) { return aes_keys_[index%kAesKeyNum]; } + }; +#pragma pack (pop) + + // binary + fnd::MemoryBlob binary_blob_; + + // data + u16 block_size_; + u64 nca_size_; + u64 program_id_; + u32 unk0_; + std::vector sections_; + std::vector aes_keys_; + + void clearVariables(); + u64 blockNumToSize(u32 block_num) const; + u32 sizeToBlockNum(u64 real_size) const; +}; diff --git a/lib/nx/nx.vcxproj b/lib/nx/nx.vcxproj index 523c070..d525d0f 100644 --- a/lib/nx/nx.vcxproj +++ b/lib/nx/nx.vcxproj @@ -18,6 +18,13 @@ x64 + + + + + + + 15.0 {91BA9E79-8242-4F7D-B997-0DFEC95EA22B} @@ -111,8 +118,6 @@ true - - diff --git a/lib/nx/nx.vcxproj.filters b/lib/nx/nx.vcxproj.filters index 6a1782f..8885ede 100644 --- a/lib/nx/nx.vcxproj.filters +++ b/lib/nx/nx.vcxproj.filters @@ -14,4 +14,17 @@ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + Header Files + + + Header Files + + + + + Source Files + + \ No newline at end of file