partition_data_manager.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. // NOTE TO FUTURE MAINTAINERS:
  5. // When a new version of switch cryptography is released,
  6. // hash the new keyblob source and master key and add the hashes to
  7. // the arrays below.
  8. #include <algorithm>
  9. #include <array>
  10. #include <cctype>
  11. #include <cstring>
  12. #include <mbedtls/sha256.h>
  13. #include "common/assert.h"
  14. #include "common/common_funcs.h"
  15. #include "common/common_types.h"
  16. #include "common/hex_util.h"
  17. #include "common/logging/log.h"
  18. #include "common/string_util.h"
  19. #include "common/swap.h"
  20. #include "core/crypto/key_manager.h"
  21. #include "core/crypto/partition_data_manager.h"
  22. #include "core/crypto/xts_encryption_layer.h"
  23. #include "core/file_sys/kernel_executable.h"
  24. #include "core/file_sys/vfs.h"
  25. #include "core/file_sys/vfs_offset.h"
  26. #include "core/file_sys/vfs_vector.h"
  27. #include "core/loader/loader.h"
  28. using Common::AsArray;
  29. namespace Core::Crypto {
  30. struct Package2Header {
  31. std::array<u8, 0x100> signature;
  32. Key128 header_ctr;
  33. std::array<Key128, 4> section_ctr;
  34. u32_le magic;
  35. u32_le base_offset;
  36. INSERT_PADDING_BYTES(4);
  37. u8 version_max;
  38. u8 version_min;
  39. INSERT_PADDING_BYTES(2);
  40. std::array<u32_le, 4> section_size;
  41. std::array<u32_le, 4> section_offset;
  42. std::array<SHA256Hash, 4> section_hash;
  43. };
  44. static_assert(sizeof(Package2Header) == 0x200, "Package2Header has incorrect size.");
  45. // clang-format off
  46. constexpr std::array source_hashes{
  47. AsArray("B24BD293259DBC7AC5D63F88E60C59792498E6FC5443402C7FFE87EE8B61A3F0"), // keyblob_mac_key_source
  48. AsArray("7944862A3A5C31C6720595EFD302245ABD1B54CCDCF33000557681E65C5664A4"), // master_key_source
  49. AsArray("21E2DF100FC9E094DB51B47B9B1D6E94ED379DB8B547955BEF8FE08D8DD35603"), // package2_key_source
  50. AsArray("FC02B9D37B42D7A1452E71444F1F700311D1132E301A83B16062E72A78175085"), // aes_kek_generation_source
  51. AsArray("FBD10056999EDC7ACDB96098E47E2C3606230270D23281E671F0F389FC5BC585"), // aes_key_generation_source
  52. AsArray("C48B619827986C7F4E3081D59DB2B460C84312650E9A8E6B458E53E8CBCA4E87"), // titlekek_source
  53. AsArray("04AD66143C726B2A139FB6B21128B46F56C553B2B3887110304298D8D0092D9E"), // key_area_key_application_source
  54. AsArray("FD434000C8FF2B26F8E9A9D2D2C12F6BE5773CBB9DC86300E1BD99F8EA33A417"), // key_area_key_ocean_source
  55. AsArray("1F17B1FD51AD1C2379B58F152CA4912EC2106441E51722F38700D5937A1162F7"), // key_area_key_system_source
  56. AsArray("6B2ED877C2C52334AC51E59ABFA7EC457F4A7D01E46291E9F2EAA45F011D24B7"), // sd_card_kek_source
  57. AsArray("D482743563D3EA5DCDC3B74E97C9AC8A342164FA041A1DC80F17F6D31E4BC01C"), // sd_card_save_key_source
  58. AsArray("2E751CECF7D93A2B957BD5FFCB082FD038CC2853219DD3092C6DAB9838F5A7CC"), // sd_card_nca_key_source
  59. AsArray("1888CAED5551B3EDE01499E87CE0D86827F80820EFB275921055AA4E2ABDFFC2"), // header_kek_source
  60. AsArray("8F783E46852DF6BE0BA4E19273C4ADBAEE16380043E1B8C418C4089A8BD64AA6"), // header_key_source
  61. AsArray("D1757E52F1AE55FA882EC690BC6F954AC46A83DC22F277F8806BD55577C6EED7"), // rsa_kek_seed3
  62. AsArray("FC02B9D37B42D7A1452E71444F1F700311D1132E301A83B16062E72A78175085"), // rsa_kek_mask0
  63. };
  64. // clang-format on
  65. // clang-format off
  66. constexpr std::array keyblob_source_hashes{
  67. AsArray("8A06FE274AC491436791FDB388BCDD3AB9943BD4DEF8094418CDAC150FD73786"), // keyblob_key_source_00
  68. AsArray("2D5CAEB2521FEF70B47E17D6D0F11F8CE2C1E442A979AD8035832C4E9FBCCC4B"), // keyblob_key_source_01
  69. AsArray("61C5005E713BAE780641683AF43E5F5C0E03671117F702F401282847D2FC6064"), // keyblob_key_source_02
  70. AsArray("8E9795928E1C4428E1B78F0BE724D7294D6934689C11B190943923B9D5B85903"), // keyblob_key_source_03
  71. AsArray("95FA33AF95AFF9D9B61D164655B32710ED8D615D46C7D6CC3CC70481B686B402"), // keyblob_key_source_04
  72. AsArray("3F5BE7B3C8B1ABD8C10B4B703D44766BA08730562C172A4FE0D6B866B3E2DB3E"), // keyblob_key_source_05
  73. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_06
  74. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_07
  75. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_08
  76. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_09
  77. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_0A
  78. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_0B
  79. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_0C
  80. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_0D
  81. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_0E
  82. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_0F
  83. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_10
  84. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_11
  85. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_12
  86. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_13
  87. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_14
  88. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_15
  89. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_16
  90. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_17
  91. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_18
  92. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_19
  93. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_1A
  94. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_1B
  95. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_1C
  96. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_1D
  97. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_1E
  98. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // keyblob_key_source_1F
  99. };
  100. // clang-format on
  101. // clang-format off
  102. constexpr std::array master_key_hashes{
  103. AsArray("0EE359BE3C864BB0782E1D70A718A0342C551EED28C369754F9C4F691BECF7CA"), // master_key_00
  104. AsArray("4FE707B7E4ABDAF727C894AAF13B1351BFE2AC90D875F73B2E20FA94B9CC661E"), // master_key_01
  105. AsArray("79277C0237A2252EC3DFAC1F7C359C2B3D121E9DB15BB9AB4C2B4408D2F3AE09"), // master_key_02
  106. AsArray("4F36C565D13325F65EE134073C6A578FFCB0008E02D69400836844EAB7432754"), // master_key_03
  107. AsArray("75FF1D95D26113550EE6FCC20ACB58E97EDEB3A2FF52543ED5AEC63BDCC3DA50"), // master_key_04
  108. AsArray("EBE2BCD6704673EC0F88A187BB2AD9F1CC82B718C389425941BDC194DC46B0DD"), // master_key_05
  109. AsArray("9497E6779F5D840F2BBA1DE4E95BA1D6F21EFC94717D5AE5CA37D7EC5BD37A19"), // master_key_06
  110. AsArray("4EC96B8CB01B8DCE382149443430B2B6EBCB2983348AFA04A25E53609DABEDF6"), // master_key_07
  111. AsArray("2998E2E23609BC2675FF062A2D64AF5B1B78DFF463B24119D64A1B64F01B2D51"), // master_key_08
  112. AsArray("9D486A98067C44B37CF173D3BF577891EB6081FF6B4A166347D9DBBF7025076B"), // master_key_09
  113. AsArray("4EC5A237A75A083A9C5F6CF615601522A7F822D06BD4BA32612C9CEBBB29BD45"), // master_key_0A
  114. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_0B
  115. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_0C
  116. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_0D
  117. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_0E
  118. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_0F
  119. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_10
  120. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_11
  121. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_12
  122. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_13
  123. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_14
  124. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_15
  125. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_16
  126. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_17
  127. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_18
  128. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_19
  129. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_1A
  130. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_1B
  131. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_1C
  132. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_1D
  133. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_1E
  134. AsArray("0000000000000000000000000000000000000000000000000000000000000000"), // master_key_1F
  135. };
  136. // clang-format on
  137. static constexpr u8 CalculateMaxKeyblobSourceHash() {
  138. const auto is_zero = [](const auto& data) {
  139. // TODO: Replace with std::all_of whenever mingw decides to update their
  140. // libraries to include the constexpr variant of it.
  141. for (const auto element : data) {
  142. if (element != 0) {
  143. return false;
  144. }
  145. }
  146. return true;
  147. };
  148. for (s8 i = 0x1F; i >= 0; --i) {
  149. if (!is_zero(keyblob_source_hashes[i])) {
  150. return static_cast<u8>(i + 1);
  151. }
  152. }
  153. return 0;
  154. }
  155. const u8 PartitionDataManager::MAX_KEYBLOB_SOURCE_HASH = CalculateMaxKeyblobSourceHash();
  156. template <size_t key_size = 0x10>
  157. std::array<u8, key_size> FindKeyFromHex(const std::vector<u8>& binary,
  158. const std::array<u8, 0x20>& hash) {
  159. if (binary.size() < key_size)
  160. return {};
  161. std::array<u8, 0x20> temp{};
  162. for (size_t i = 0; i < binary.size() - key_size; ++i) {
  163. mbedtls_sha256_ret(binary.data() + i, key_size, temp.data(), 0);
  164. if (temp != hash)
  165. continue;
  166. std::array<u8, key_size> out{};
  167. std::memcpy(out.data(), binary.data() + i, key_size);
  168. return out;
  169. }
  170. return {};
  171. }
  172. std::array<u8, 16> FindKeyFromHex16(const std::vector<u8>& binary, std::array<u8, 32> hash) {
  173. return FindKeyFromHex<0x10>(binary, hash);
  174. }
  175. static std::array<Key128, 0x20> FindEncryptedMasterKeyFromHex(const std::vector<u8>& binary,
  176. const Key128& key) {
  177. if (binary.size() < 0x10)
  178. return {};
  179. SHA256Hash temp{};
  180. Key128 dec_temp{};
  181. std::array<Key128, 0x20> out{};
  182. AESCipher<Key128> cipher(key, Mode::ECB);
  183. for (size_t i = 0; i < binary.size() - 0x10; ++i) {
  184. cipher.Transcode(binary.data() + i, dec_temp.size(), dec_temp.data(), Op::Decrypt);
  185. mbedtls_sha256_ret(dec_temp.data(), dec_temp.size(), temp.data(), 0);
  186. for (size_t k = 0; k < out.size(); ++k) {
  187. if (temp == master_key_hashes[k]) {
  188. out[k] = dec_temp;
  189. break;
  190. }
  191. }
  192. }
  193. return out;
  194. }
  195. static FileSys::VirtualFile FindFileInDirWithNames(const FileSys::VirtualDir& dir,
  196. const std::string& name) {
  197. const auto upper = Common::ToUpper(name);
  198. for (const auto& fname : {name, name + ".bin", upper, upper + ".BIN"}) {
  199. if (dir->GetFile(fname) != nullptr) {
  200. return dir->GetFile(fname);
  201. }
  202. }
  203. return nullptr;
  204. }
  205. PartitionDataManager::PartitionDataManager(const FileSys::VirtualDir& sysdata_dir)
  206. : boot0(FindFileInDirWithNames(sysdata_dir, "BOOT0")),
  207. fuses(FindFileInDirWithNames(sysdata_dir, "fuses")),
  208. kfuses(FindFileInDirWithNames(sysdata_dir, "kfuses")),
  209. package2({
  210. FindFileInDirWithNames(sysdata_dir, "BCPKG2-1-Normal-Main"),
  211. FindFileInDirWithNames(sysdata_dir, "BCPKG2-2-Normal-Sub"),
  212. FindFileInDirWithNames(sysdata_dir, "BCPKG2-3-SafeMode-Main"),
  213. FindFileInDirWithNames(sysdata_dir, "BCPKG2-4-SafeMode-Sub"),
  214. FindFileInDirWithNames(sysdata_dir, "BCPKG2-5-Repair-Main"),
  215. FindFileInDirWithNames(sysdata_dir, "BCPKG2-6-Repair-Sub"),
  216. }),
  217. prodinfo(FindFileInDirWithNames(sysdata_dir, "PRODINFO")),
  218. secure_monitor(FindFileInDirWithNames(sysdata_dir, "secmon")),
  219. package1_decrypted(FindFileInDirWithNames(sysdata_dir, "pkg1_decr")),
  220. secure_monitor_bytes(secure_monitor == nullptr ? std::vector<u8>{}
  221. : secure_monitor->ReadAllBytes()),
  222. package1_decrypted_bytes(package1_decrypted == nullptr ? std::vector<u8>{}
  223. : package1_decrypted->ReadAllBytes()) {
  224. }
  225. PartitionDataManager::~PartitionDataManager() = default;
  226. bool PartitionDataManager::HasBoot0() const {
  227. return boot0 != nullptr;
  228. }
  229. FileSys::VirtualFile PartitionDataManager::GetBoot0Raw() const {
  230. return boot0;
  231. }
  232. PartitionDataManager::EncryptedKeyBlob PartitionDataManager::GetEncryptedKeyblob(
  233. std::size_t index) const {
  234. if (HasBoot0() && index < NUM_ENCRYPTED_KEYBLOBS)
  235. return GetEncryptedKeyblobs()[index];
  236. return {};
  237. }
  238. PartitionDataManager::EncryptedKeyBlobs PartitionDataManager::GetEncryptedKeyblobs() const {
  239. if (!HasBoot0())
  240. return {};
  241. EncryptedKeyBlobs out{};
  242. for (size_t i = 0; i < out.size(); ++i)
  243. boot0->Read(out[i].data(), out[i].size(), 0x180000 + i * 0x200);
  244. return out;
  245. }
  246. std::vector<u8> PartitionDataManager::GetSecureMonitor() const {
  247. return secure_monitor_bytes;
  248. }
  249. std::array<u8, 16> PartitionDataManager::GetPackage2KeySource() const {
  250. return FindKeyFromHex(secure_monitor_bytes, source_hashes[2]);
  251. }
  252. std::array<u8, 16> PartitionDataManager::GetAESKekGenerationSource() const {
  253. return FindKeyFromHex(secure_monitor_bytes, source_hashes[3]);
  254. }
  255. std::array<u8, 16> PartitionDataManager::GetTitlekekSource() const {
  256. return FindKeyFromHex(secure_monitor_bytes, source_hashes[5]);
  257. }
  258. std::array<std::array<u8, 16>, 32> PartitionDataManager::GetTZMasterKeys(
  259. std::array<u8, 0x10> master_key) const {
  260. return FindEncryptedMasterKeyFromHex(secure_monitor_bytes, master_key);
  261. }
  262. std::array<u8, 16> PartitionDataManager::GetRSAKekSeed3() const {
  263. return FindKeyFromHex(secure_monitor_bytes, source_hashes[14]);
  264. }
  265. std::array<u8, 16> PartitionDataManager::GetRSAKekMask0() const {
  266. return FindKeyFromHex(secure_monitor_bytes, source_hashes[15]);
  267. }
  268. std::vector<u8> PartitionDataManager::GetPackage1Decrypted() const {
  269. return package1_decrypted_bytes;
  270. }
  271. std::array<u8, 16> PartitionDataManager::GetMasterKeySource() const {
  272. return FindKeyFromHex(package1_decrypted_bytes, source_hashes[1]);
  273. }
  274. std::array<u8, 16> PartitionDataManager::GetKeyblobMACKeySource() const {
  275. return FindKeyFromHex(package1_decrypted_bytes, source_hashes[0]);
  276. }
  277. std::array<u8, 16> PartitionDataManager::GetKeyblobKeySource(std::size_t revision) const {
  278. if (keyblob_source_hashes[revision] == SHA256Hash{}) {
  279. LOG_WARNING(Crypto,
  280. "No keyblob source hash for crypto revision {:02X}! Cannot derive keys...",
  281. revision);
  282. }
  283. return FindKeyFromHex(package1_decrypted_bytes, keyblob_source_hashes[revision]);
  284. }
  285. bool PartitionDataManager::HasFuses() const {
  286. return fuses != nullptr;
  287. }
  288. FileSys::VirtualFile PartitionDataManager::GetFusesRaw() const {
  289. return fuses;
  290. }
  291. std::array<u8, 16> PartitionDataManager::GetSecureBootKey() const {
  292. if (!HasFuses())
  293. return {};
  294. Key128 out{};
  295. fuses->Read(out.data(), out.size(), 0xA4);
  296. return out;
  297. }
  298. bool PartitionDataManager::HasKFuses() const {
  299. return kfuses != nullptr;
  300. }
  301. FileSys::VirtualFile PartitionDataManager::GetKFusesRaw() const {
  302. return kfuses;
  303. }
  304. bool PartitionDataManager::HasPackage2(Package2Type type) const {
  305. return package2.at(static_cast<size_t>(type)) != nullptr;
  306. }
  307. FileSys::VirtualFile PartitionDataManager::GetPackage2Raw(Package2Type type) const {
  308. return package2.at(static_cast<size_t>(type));
  309. }
  310. static bool AttemptDecrypt(const std::array<u8, 16>& key, Package2Header& header) {
  311. Package2Header temp = header;
  312. AESCipher<Key128> cipher(key, Mode::CTR);
  313. cipher.SetIV(header.header_ctr);
  314. cipher.Transcode(&temp.header_ctr, sizeof(Package2Header) - sizeof(Package2Header::signature),
  315. &temp.header_ctr, Op::Decrypt);
  316. if (temp.magic == Common::MakeMagic('P', 'K', '2', '1')) {
  317. header = temp;
  318. return true;
  319. }
  320. return false;
  321. }
  322. void PartitionDataManager::DecryptPackage2(const std::array<Key128, 0x20>& package2_keys,
  323. Package2Type type) {
  324. FileSys::VirtualFile file = std::make_shared<FileSys::OffsetVfsFile>(
  325. package2[static_cast<size_t>(type)],
  326. package2[static_cast<size_t>(type)]->GetSize() - 0x4000, 0x4000);
  327. Package2Header header{};
  328. if (file->ReadObject(&header) != sizeof(Package2Header))
  329. return;
  330. std::size_t revision = 0xFF;
  331. if (header.magic != Common::MakeMagic('P', 'K', '2', '1')) {
  332. for (std::size_t i = 0; i < package2_keys.size(); ++i) {
  333. if (AttemptDecrypt(package2_keys[i], header)) {
  334. revision = i;
  335. }
  336. }
  337. }
  338. if (header.magic != Common::MakeMagic('P', 'K', '2', '1'))
  339. return;
  340. const auto a = std::make_shared<FileSys::OffsetVfsFile>(
  341. file, header.section_size[1], header.section_size[0] + sizeof(Package2Header));
  342. auto c = a->ReadAllBytes();
  343. AESCipher<Key128> cipher(package2_keys[revision], Mode::CTR);
  344. cipher.SetIV(header.section_ctr[1]);
  345. cipher.Transcode(c.data(), c.size(), c.data(), Op::Decrypt);
  346. const auto ini_file = std::make_shared<FileSys::VectorVfsFile>(c);
  347. const FileSys::INI ini{ini_file};
  348. if (ini.GetStatus() != Loader::ResultStatus::Success)
  349. return;
  350. for (const auto& kip : ini.GetKIPs()) {
  351. if (kip.GetStatus() != Loader::ResultStatus::Success)
  352. return;
  353. if (kip.GetName() != "FS" && kip.GetName() != "spl") {
  354. continue;
  355. }
  356. const auto& text = kip.GetTextSection();
  357. const auto& rodata = kip.GetRODataSection();
  358. const auto& data = kip.GetDataSection();
  359. std::vector<u8> out;
  360. out.reserve(text.size() + rodata.size() + data.size());
  361. out.insert(out.end(), text.begin(), text.end());
  362. out.insert(out.end(), rodata.begin(), rodata.end());
  363. out.insert(out.end(), data.begin(), data.end());
  364. if (kip.GetName() == "FS")
  365. package2_fs[static_cast<size_t>(type)] = std::move(out);
  366. else if (kip.GetName() == "spl")
  367. package2_spl[static_cast<size_t>(type)] = std::move(out);
  368. }
  369. }
  370. const std::vector<u8>& PartitionDataManager::GetPackage2FSDecompressed(Package2Type type) const {
  371. return package2_fs.at(static_cast<size_t>(type));
  372. }
  373. std::array<u8, 16> PartitionDataManager::GetKeyAreaKeyApplicationSource(Package2Type type) const {
  374. return FindKeyFromHex(package2_fs.at(static_cast<size_t>(type)), source_hashes[6]);
  375. }
  376. std::array<u8, 16> PartitionDataManager::GetKeyAreaKeyOceanSource(Package2Type type) const {
  377. return FindKeyFromHex(package2_fs.at(static_cast<size_t>(type)), source_hashes[7]);
  378. }
  379. std::array<u8, 16> PartitionDataManager::GetKeyAreaKeySystemSource(Package2Type type) const {
  380. return FindKeyFromHex(package2_fs.at(static_cast<size_t>(type)), source_hashes[8]);
  381. }
  382. std::array<u8, 16> PartitionDataManager::GetSDKekSource(Package2Type type) const {
  383. return FindKeyFromHex(package2_fs.at(static_cast<size_t>(type)), source_hashes[9]);
  384. }
  385. std::array<u8, 32> PartitionDataManager::GetSDSaveKeySource(Package2Type type) const {
  386. return FindKeyFromHex<0x20>(package2_fs.at(static_cast<size_t>(type)), source_hashes[10]);
  387. }
  388. std::array<u8, 32> PartitionDataManager::GetSDNCAKeySource(Package2Type type) const {
  389. return FindKeyFromHex<0x20>(package2_fs.at(static_cast<size_t>(type)), source_hashes[11]);
  390. }
  391. std::array<u8, 16> PartitionDataManager::GetHeaderKekSource(Package2Type type) const {
  392. return FindKeyFromHex(package2_fs.at(static_cast<size_t>(type)), source_hashes[12]);
  393. }
  394. std::array<u8, 32> PartitionDataManager::GetHeaderKeySource(Package2Type type) const {
  395. return FindKeyFromHex<0x20>(package2_fs.at(static_cast<size_t>(type)), source_hashes[13]);
  396. }
  397. const std::vector<u8>& PartitionDataManager::GetPackage2SPLDecompressed(Package2Type type) const {
  398. return package2_spl.at(static_cast<size_t>(type));
  399. }
  400. std::array<u8, 16> PartitionDataManager::GetAESKeyGenerationSource(Package2Type type) const {
  401. return FindKeyFromHex(package2_spl.at(static_cast<size_t>(type)), source_hashes[4]);
  402. }
  403. bool PartitionDataManager::HasProdInfo() const {
  404. return prodinfo != nullptr;
  405. }
  406. FileSys::VirtualFile PartitionDataManager::GetProdInfoRaw() const {
  407. return prodinfo;
  408. }
  409. void PartitionDataManager::DecryptProdInfo(std::array<u8, 0x20> bis_key) {
  410. if (prodinfo == nullptr)
  411. return;
  412. prodinfo_decrypted = std::make_shared<XTSEncryptionLayer>(prodinfo, bis_key);
  413. }
  414. FileSys::VirtualFile PartitionDataManager::GetDecryptedProdInfo() const {
  415. return prodinfo_decrypted;
  416. }
  417. std::array<u8, 576> PartitionDataManager::GetETicketExtendedKek() const {
  418. std::array<u8, 0x240> out{};
  419. if (prodinfo_decrypted != nullptr)
  420. prodinfo_decrypted->Read(out.data(), out.size(), 0x3890);
  421. return out;
  422. }
  423. } // namespace Core::Crypto