VolumeWii.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DiscIO/VolumeWii.h"
  4. #include <algorithm>
  5. #include <array>
  6. #include <cstddef>
  7. #include <cstring>
  8. #include <future>
  9. #include <map>
  10. #include <memory>
  11. #include <optional>
  12. #include <string>
  13. #include <thread>
  14. #include <utility>
  15. #include <vector>
  16. #include "Common/Align.h"
  17. #include "Common/Assert.h"
  18. #include "Common/CommonTypes.h"
  19. #include "Common/Crypto/AES.h"
  20. #include "Common/Crypto/SHA1.h"
  21. #include "Common/Logging/Log.h"
  22. #include "Common/Swap.h"
  23. #include "DiscIO/Blob.h"
  24. #include "DiscIO/DiscExtractor.h"
  25. #include "DiscIO/DiscUtils.h"
  26. #include "DiscIO/Enums.h"
  27. #include "DiscIO/FileSystemGCWii.h"
  28. #include "DiscIO/Filesystem.h"
  29. #include "DiscIO/Volume.h"
  30. #include "DiscIO/WiiSaveBanner.h"
  31. namespace DiscIO
  32. {
  33. VolumeWii::VolumeWii(std::unique_ptr<BlobReader> reader)
  34. : m_reader(std::move(reader)), m_game_partition(PARTITION_NONE),
  35. m_last_decrypted_block(UINT64_MAX)
  36. {
  37. ASSERT(m_reader);
  38. m_has_hashes = m_reader->ReadSwapped<u8>(0x60) == u8(0);
  39. m_has_encryption = m_reader->ReadSwapped<u8>(0x61) == u8(0);
  40. if (m_has_encryption && !m_has_hashes)
  41. ERROR_LOG_FMT(DISCIO, "Wii disc has encryption but no hashes! This probably won't work well");
  42. for (u32 partition_group = 0; partition_group < 4; ++partition_group)
  43. {
  44. const std::optional<u32> number_of_partitions =
  45. m_reader->ReadSwapped<u32>(0x40000 + (partition_group * 8));
  46. if (!number_of_partitions)
  47. continue;
  48. const std::optional<u64> partition_table_offset =
  49. ReadSwappedAndShifted(0x40000 + (partition_group * 8) + 4, PARTITION_NONE);
  50. if (!partition_table_offset)
  51. continue;
  52. for (u32 i = 0; i < number_of_partitions; i++)
  53. {
  54. const std::optional<u64> partition_offset =
  55. ReadSwappedAndShifted(*partition_table_offset + (i * 8), PARTITION_NONE);
  56. if (!partition_offset)
  57. continue;
  58. const Partition partition(*partition_offset);
  59. const std::optional<u32> partition_type =
  60. m_reader->ReadSwapped<u32>(*partition_table_offset + (i * 8) + 4);
  61. if (!partition_type)
  62. continue;
  63. // If this is the game partition, set m_game_partition
  64. if (m_game_partition == PARTITION_NONE && *partition_type == 0)
  65. m_game_partition = partition;
  66. auto get_ticket = [this, partition]() -> IOS::ES::TicketReader {
  67. std::vector<u8> ticket_buffer(sizeof(IOS::ES::Ticket));
  68. if (!m_reader->Read(partition.offset, ticket_buffer.size(), ticket_buffer.data()))
  69. return INVALID_TICKET;
  70. return IOS::ES::TicketReader{std::move(ticket_buffer)};
  71. };
  72. auto get_tmd = [this, partition]() -> IOS::ES::TMDReader {
  73. const std::optional<u32> tmd_size =
  74. m_reader->ReadSwapped<u32>(partition.offset + WII_PARTITION_TMD_SIZE_ADDRESS);
  75. const std::optional<u64> tmd_address = ReadSwappedAndShifted(
  76. partition.offset + WII_PARTITION_TMD_OFFSET_ADDRESS, PARTITION_NONE);
  77. if (!tmd_size || !tmd_address)
  78. return INVALID_TMD;
  79. if (!IOS::ES::IsValidTMDSize(*tmd_size))
  80. {
  81. // This check is normally done by ES in ES_DiVerify, but that would happen too late
  82. // (after allocating the buffer), so we do the check here.
  83. ERROR_LOG_FMT(DISCIO, "Invalid TMD size");
  84. return INVALID_TMD;
  85. }
  86. std::vector<u8> tmd_buffer(*tmd_size);
  87. if (!m_reader->Read(partition.offset + *tmd_address, *tmd_size, tmd_buffer.data()))
  88. return INVALID_TMD;
  89. return IOS::ES::TMDReader{std::move(tmd_buffer)};
  90. };
  91. auto get_cert_chain = [this, partition]() -> std::vector<u8> {
  92. const std::optional<u32> size =
  93. m_reader->ReadSwapped<u32>(partition.offset + WII_PARTITION_CERT_CHAIN_SIZE_ADDRESS);
  94. const std::optional<u64> address = ReadSwappedAndShifted(
  95. partition.offset + WII_PARTITION_CERT_CHAIN_OFFSET_ADDRESS, PARTITION_NONE);
  96. if (!size || !address)
  97. return {};
  98. std::vector<u8> cert_chain(*size);
  99. if (!m_reader->Read(partition.offset + *address, *size, cert_chain.data()))
  100. return {};
  101. return cert_chain;
  102. };
  103. auto get_h3_table = [this, partition]() -> std::vector<u8> {
  104. if (!m_has_hashes)
  105. return {};
  106. const std::optional<u64> h3_table_offset = ReadSwappedAndShifted(
  107. partition.offset + WII_PARTITION_H3_OFFSET_ADDRESS, PARTITION_NONE);
  108. if (!h3_table_offset)
  109. return {};
  110. std::vector<u8> h3_table(WII_PARTITION_H3_SIZE);
  111. if (!m_reader->Read(partition.offset + *h3_table_offset, WII_PARTITION_H3_SIZE,
  112. h3_table.data()))
  113. return {};
  114. return h3_table;
  115. };
  116. auto get_key = [this, partition]() -> std::unique_ptr<Common::AES::Context> {
  117. const IOS::ES::TicketReader& ticket = *m_partitions[partition].ticket;
  118. if (!ticket.IsValid())
  119. return nullptr;
  120. return Common::AES::CreateContextDecrypt(ticket.GetTitleKey().data());
  121. };
  122. auto get_file_system = [this, partition]() -> std::unique_ptr<FileSystem> {
  123. auto file_system = std::make_unique<FileSystemGCWii>(this, partition);
  124. return file_system->IsValid() ? std::move(file_system) : nullptr;
  125. };
  126. auto get_data_offset = [this, partition]() -> u64 {
  127. return ReadSwappedAndShifted(partition.offset + 0x2b8, PARTITION_NONE).value_or(0);
  128. };
  129. m_partitions.emplace(
  130. partition, PartitionDetails{Common::Lazy<std::unique_ptr<Common::AES::Context>>(get_key),
  131. Common::Lazy<IOS::ES::TicketReader>(get_ticket),
  132. Common::Lazy<IOS::ES::TMDReader>(get_tmd),
  133. Common::Lazy<std::vector<u8>>(get_cert_chain),
  134. Common::Lazy<std::vector<u8>>(get_h3_table),
  135. Common::Lazy<std::unique_ptr<FileSystem>>(get_file_system),
  136. Common::Lazy<u64>(get_data_offset), *partition_type});
  137. }
  138. }
  139. }
  140. VolumeWii::~VolumeWii()
  141. {
  142. }
  143. bool VolumeWii::Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const
  144. {
  145. if (partition == PARTITION_NONE)
  146. return m_reader->Read(offset, length, buffer);
  147. auto it = m_partitions.find(partition);
  148. if (it == m_partitions.end())
  149. return false;
  150. const PartitionDetails& partition_details = it->second;
  151. const u64 partition_data_offset = partition.offset + *partition_details.data_offset;
  152. if (m_has_hashes && m_has_encryption &&
  153. m_reader->SupportsReadWiiDecrypted(offset, length, partition_data_offset))
  154. {
  155. return m_reader->ReadWiiDecrypted(offset, length, buffer, partition_data_offset);
  156. }
  157. if (!m_has_hashes)
  158. {
  159. return m_reader->Read(partition_data_offset + offset, length, buffer);
  160. }
  161. Common::AES::Context* aes_context = nullptr;
  162. std::unique_ptr<u8[]> read_buffer = nullptr;
  163. if (m_has_encryption)
  164. {
  165. aes_context = partition_details.key->get();
  166. if (!aes_context)
  167. return false;
  168. read_buffer = std::make_unique<u8[]>(BLOCK_TOTAL_SIZE);
  169. }
  170. while (length > 0)
  171. {
  172. // Calculate offsets
  173. u64 block_offset_on_disc = partition_data_offset + offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE;
  174. u64 data_offset_in_block = offset % BLOCK_DATA_SIZE;
  175. if (m_last_decrypted_block != block_offset_on_disc)
  176. {
  177. if (m_has_encryption)
  178. {
  179. // Read the current block
  180. if (!m_reader->Read(block_offset_on_disc, BLOCK_TOTAL_SIZE, read_buffer.get()))
  181. return false;
  182. // Decrypt the block's data
  183. DecryptBlockData(read_buffer.get(), m_last_decrypted_block_data, aes_context);
  184. }
  185. else
  186. {
  187. // Read the current block
  188. if (!m_reader->Read(block_offset_on_disc + BLOCK_HEADER_SIZE, BLOCK_DATA_SIZE,
  189. m_last_decrypted_block_data))
  190. {
  191. return false;
  192. }
  193. }
  194. m_last_decrypted_block = block_offset_on_disc;
  195. }
  196. // Copy the decrypted data
  197. u64 copy_size = std::min(length, BLOCK_DATA_SIZE - data_offset_in_block);
  198. memcpy(buffer, &m_last_decrypted_block_data[data_offset_in_block],
  199. static_cast<size_t>(copy_size));
  200. // Update offsets
  201. length -= copy_size;
  202. buffer += copy_size;
  203. offset += copy_size;
  204. }
  205. return true;
  206. }
  207. bool VolumeWii::HasWiiHashes() const
  208. {
  209. return m_has_hashes;
  210. }
  211. bool VolumeWii::HasWiiEncryption() const
  212. {
  213. return m_has_encryption;
  214. }
  215. std::vector<Partition> VolumeWii::GetPartitions() const
  216. {
  217. std::vector<Partition> partitions;
  218. for (const auto& pair : m_partitions)
  219. partitions.push_back(pair.first);
  220. return partitions;
  221. }
  222. Partition VolumeWii::GetGamePartition() const
  223. {
  224. return m_game_partition;
  225. }
  226. std::optional<u32> VolumeWii::GetPartitionType(const Partition& partition) const
  227. {
  228. auto it = m_partitions.find(partition);
  229. return it != m_partitions.end() ? it->second.type : std::optional<u32>();
  230. }
  231. std::optional<u64> VolumeWii::GetTitleID(const Partition& partition) const
  232. {
  233. const IOS::ES::TicketReader& ticket = GetTicket(partition);
  234. if (!ticket.IsValid())
  235. return {};
  236. return ticket.GetTitleId();
  237. }
  238. const IOS::ES::TicketReader& VolumeWii::GetTicket(const Partition& partition) const
  239. {
  240. auto it = m_partitions.find(partition);
  241. return it != m_partitions.end() ? *it->second.ticket : INVALID_TICKET;
  242. }
  243. const IOS::ES::TMDReader& VolumeWii::GetTMD(const Partition& partition) const
  244. {
  245. auto it = m_partitions.find(partition);
  246. return it != m_partitions.end() ? *it->second.tmd : INVALID_TMD;
  247. }
  248. const std::vector<u8>& VolumeWii::GetCertificateChain(const Partition& partition) const
  249. {
  250. auto it = m_partitions.find(partition);
  251. return it != m_partitions.end() ? *it->second.cert_chain : INVALID_CERT_CHAIN;
  252. }
  253. const FileSystem* VolumeWii::GetFileSystem(const Partition& partition) const
  254. {
  255. auto it = m_partitions.find(partition);
  256. return it != m_partitions.end() ? it->second.file_system->get() : nullptr;
  257. }
  258. u64 VolumeWii::OffsetInHashedPartitionToRawOffset(u64 offset, const Partition& partition,
  259. u64 partition_data_offset)
  260. {
  261. if (partition == PARTITION_NONE)
  262. return offset;
  263. return partition.offset + partition_data_offset + (offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE) +
  264. (offset % BLOCK_DATA_SIZE);
  265. }
  266. u64 VolumeWii::PartitionOffsetToRawOffset(u64 offset, const Partition& partition) const
  267. {
  268. auto it = m_partitions.find(partition);
  269. if (it == m_partitions.end())
  270. return offset;
  271. const u64 data_offset = *it->second.data_offset;
  272. if (!m_has_hashes)
  273. return partition.offset + data_offset + offset;
  274. return OffsetInHashedPartitionToRawOffset(offset, partition, data_offset);
  275. }
  276. std::string VolumeWii::GetGameTDBID(const Partition& partition) const
  277. {
  278. return GetGameID(partition);
  279. }
  280. Region VolumeWii::GetRegion() const
  281. {
  282. return RegionCodeToRegion(m_reader->ReadSwapped<u32>(0x4E000));
  283. }
  284. std::map<Language, std::string> VolumeWii::GetLongNames() const
  285. {
  286. std::vector<char16_t> names(NAMES_TOTAL_CHARS);
  287. names.resize(ReadFile(*this, GetGamePartition(), "opening.bnr",
  288. reinterpret_cast<u8*>(names.data()), NAMES_TOTAL_BYTES, 0x5C));
  289. return ReadWiiNames(names);
  290. }
  291. std::vector<u32> VolumeWii::GetBanner(u32* width, u32* height) const
  292. {
  293. *width = 0;
  294. *height = 0;
  295. const std::optional<u64> title_id = GetTitleID(GetGamePartition());
  296. if (!title_id)
  297. return std::vector<u32>();
  298. return WiiSaveBanner(*title_id).GetBanner(width, height);
  299. }
  300. Platform VolumeWii::GetVolumeType() const
  301. {
  302. return Platform::WiiDisc;
  303. }
  304. bool VolumeWii::IsDatelDisc() const
  305. {
  306. return m_game_partition == PARTITION_NONE;
  307. }
  308. BlobType VolumeWii::GetBlobType() const
  309. {
  310. return m_reader->GetBlobType();
  311. }
  312. u64 VolumeWii::GetDataSize() const
  313. {
  314. return m_reader->GetDataSize();
  315. }
  316. DataSizeType VolumeWii::GetDataSizeType() const
  317. {
  318. return m_reader->GetDataSizeType();
  319. }
  320. u64 VolumeWii::GetRawSize() const
  321. {
  322. return m_reader->GetRawSize();
  323. }
  324. const BlobReader& VolumeWii::GetBlobReader() const
  325. {
  326. return *m_reader;
  327. }
  328. std::array<u8, 20> VolumeWii::GetSyncHash() const
  329. {
  330. auto context = Common::SHA1::CreateContext();
  331. // Disc header
  332. ReadAndAddToSyncHash(context.get(), 0, 0x80, PARTITION_NONE);
  333. // Region code
  334. ReadAndAddToSyncHash(context.get(), 0x4E000, 4, PARTITION_NONE);
  335. // The data offset of the game partition - an important factor for disc drive timings
  336. const u64 data_offset = PartitionOffsetToRawOffset(0, GetGamePartition());
  337. context->Update(reinterpret_cast<const u8*>(&data_offset), sizeof(data_offset));
  338. // TMD
  339. AddTMDToSyncHash(context.get(), GetGamePartition());
  340. // Game partition contents
  341. AddGamePartitionToSyncHash(context.get());
  342. return context->Finish();
  343. }
  344. bool VolumeWii::CheckH3TableIntegrity(const Partition& partition) const
  345. {
  346. auto it = m_partitions.find(partition);
  347. if (it == m_partitions.end())
  348. return false;
  349. const PartitionDetails& partition_details = it->second;
  350. const std::vector<u8>& h3_table = *partition_details.h3_table;
  351. if (h3_table.size() != WII_PARTITION_H3_SIZE)
  352. return false;
  353. const IOS::ES::TMDReader& tmd = *partition_details.tmd;
  354. if (!tmd.IsValid())
  355. return false;
  356. const std::vector<IOS::ES::Content> contents = tmd.GetContents();
  357. if (contents.size() != 1)
  358. return false;
  359. return Common::SHA1::CalculateDigest(h3_table) == contents[0].sha1;
  360. }
  361. bool VolumeWii::CheckBlockIntegrity(u64 block_index, const u8* encrypted_data,
  362. const Partition& partition) const
  363. {
  364. auto it = m_partitions.find(partition);
  365. if (it == m_partitions.end())
  366. return false;
  367. const PartitionDetails& partition_details = it->second;
  368. if (block_index / BLOCKS_PER_GROUP * Common::SHA1::DIGEST_LEN >=
  369. partition_details.h3_table->size())
  370. {
  371. return false;
  372. }
  373. HashBlock hashes;
  374. u8 cluster_data_buffer[BLOCK_DATA_SIZE];
  375. const u8* cluster_data;
  376. if (m_has_encryption)
  377. {
  378. Common::AES::Context* aes_context = partition_details.key->get();
  379. if (!aes_context)
  380. return false;
  381. DecryptBlockHashes(encrypted_data, &hashes, aes_context);
  382. DecryptBlockData(encrypted_data, cluster_data_buffer, aes_context);
  383. cluster_data = cluster_data_buffer;
  384. }
  385. else
  386. {
  387. std::memcpy(&hashes, encrypted_data, BLOCK_HEADER_SIZE);
  388. cluster_data = encrypted_data + BLOCK_HEADER_SIZE;
  389. }
  390. for (u32 hash_index = 0; hash_index < 31; ++hash_index)
  391. {
  392. if (Common::SHA1::CalculateDigest(&cluster_data[hash_index * 0x400], 0x400) !=
  393. hashes.h0[hash_index])
  394. {
  395. return false;
  396. }
  397. }
  398. if (Common::SHA1::CalculateDigest(hashes.h0) != hashes.h1[block_index % 8])
  399. return false;
  400. if (Common::SHA1::CalculateDigest(hashes.h1) != hashes.h2[block_index / 8 % 8])
  401. return false;
  402. Common::SHA1::Digest h3_digest;
  403. auto h3_digest_ptr =
  404. partition_details.h3_table->data() + block_index / 64 * Common::SHA1::DIGEST_LEN;
  405. memcpy(h3_digest.data(), h3_digest_ptr, sizeof(h3_digest));
  406. if (Common::SHA1::CalculateDigest(hashes.h2) != h3_digest)
  407. return false;
  408. return true;
  409. }
  410. bool VolumeWii::CheckBlockIntegrity(u64 block_index, const Partition& partition) const
  411. {
  412. auto it = m_partitions.find(partition);
  413. if (it == m_partitions.end())
  414. return false;
  415. const PartitionDetails& partition_details = it->second;
  416. const u64 cluster_offset =
  417. partition.offset + *partition_details.data_offset + block_index * BLOCK_TOTAL_SIZE;
  418. std::vector<u8> cluster(BLOCK_TOTAL_SIZE);
  419. if (!m_reader->Read(cluster_offset, cluster.size(), cluster.data()))
  420. return false;
  421. return CheckBlockIntegrity(block_index, cluster.data(), partition);
  422. }
  423. bool VolumeWii::HashGroup(const std::array<u8, BLOCK_DATA_SIZE> in[BLOCKS_PER_GROUP],
  424. HashBlock out[BLOCKS_PER_GROUP],
  425. const std::function<bool(size_t block)>& read_function)
  426. {
  427. std::array<std::future<void>, BLOCKS_PER_GROUP> hash_futures;
  428. bool success = true;
  429. for (size_t i = 0; i < BLOCKS_PER_GROUP; ++i)
  430. {
  431. if (read_function && success)
  432. success = read_function(i);
  433. hash_futures[i] = std::async(std::launch::async, [&in, &out, &hash_futures, success, i]() {
  434. const size_t h1_base = Common::AlignDown(i, 8);
  435. if (success)
  436. {
  437. // H0 hashes
  438. for (size_t j = 0; j < 31; ++j)
  439. out[i].h0[j] = Common::SHA1::CalculateDigest(in[i].data() + j * 0x400, 0x400);
  440. // H0 padding
  441. out[i].padding_0 = {};
  442. // H1 hash
  443. out[h1_base].h1[i - h1_base] = Common::SHA1::CalculateDigest(out[i].h0);
  444. }
  445. if (i % 8 == 7)
  446. {
  447. for (size_t j = 0; j < 7; ++j)
  448. hash_futures[h1_base + j].get();
  449. if (success)
  450. {
  451. // H1 padding
  452. out[h1_base].padding_1 = {};
  453. // H1 copies
  454. for (size_t j = 1; j < 8; ++j)
  455. out[h1_base + j].h1 = out[h1_base].h1;
  456. // H2 hash
  457. out[0].h2[h1_base / 8] = Common::SHA1::CalculateDigest(out[i].h1);
  458. }
  459. if (i == BLOCKS_PER_GROUP - 1)
  460. {
  461. for (size_t j = 0; j < 7; ++j)
  462. hash_futures[j * 8 + 7].get();
  463. if (success)
  464. {
  465. // H2 padding
  466. out[0].padding_2 = {};
  467. // H2 copies
  468. for (size_t j = 1; j < BLOCKS_PER_GROUP; ++j)
  469. out[j].h2 = out[0].h2;
  470. }
  471. }
  472. }
  473. });
  474. }
  475. // Wait for all the async tasks to finish
  476. hash_futures.back().get();
  477. return success;
  478. }
  479. bool VolumeWii::EncryptGroup(
  480. u64 offset, u64 partition_data_offset, u64 partition_data_decrypted_size,
  481. const std::array<u8, AES_KEY_SIZE>& key, BlobReader* blob,
  482. std::array<u8, GROUP_TOTAL_SIZE>* out,
  483. const std::function<void(HashBlock hash_blocks[BLOCKS_PER_GROUP])>& hash_exception_callback)
  484. {
  485. std::vector<std::array<u8, BLOCK_DATA_SIZE>> unencrypted_data(BLOCKS_PER_GROUP);
  486. std::vector<HashBlock> unencrypted_hashes(BLOCKS_PER_GROUP);
  487. const bool success =
  488. HashGroup(unencrypted_data.data(), unencrypted_hashes.data(), [&](size_t block) {
  489. if (offset + (block + 1) * BLOCK_DATA_SIZE <= partition_data_decrypted_size)
  490. {
  491. if (!blob->ReadWiiDecrypted(offset + block * BLOCK_DATA_SIZE, BLOCK_DATA_SIZE,
  492. unencrypted_data[block].data(), partition_data_offset))
  493. {
  494. return false;
  495. }
  496. }
  497. else
  498. {
  499. unencrypted_data[block].fill(0);
  500. }
  501. return true;
  502. });
  503. if (!success)
  504. return false;
  505. if (hash_exception_callback)
  506. hash_exception_callback(unencrypted_hashes.data());
  507. const unsigned int threads =
  508. std::min(BLOCKS_PER_GROUP, std::max<unsigned int>(1, std::thread::hardware_concurrency()));
  509. std::vector<std::future<void>> encryption_futures(threads);
  510. auto aes_context = Common::AES::CreateContextEncrypt(key.data());
  511. for (size_t i = 0; i < threads; ++i)
  512. {
  513. encryption_futures[i] = std::async(
  514. std::launch::async,
  515. [&unencrypted_data, &unencrypted_hashes, &aes_context, &out](size_t start, size_t end) {
  516. for (size_t j = start; j < end; ++j)
  517. {
  518. u8* out_ptr = out->data() + j * BLOCK_TOTAL_SIZE;
  519. aes_context->CryptIvZero(reinterpret_cast<u8*>(&unencrypted_hashes[j]), out_ptr,
  520. BLOCK_HEADER_SIZE);
  521. aes_context->Crypt(out_ptr + 0x3D0, unencrypted_data[j].data(),
  522. out_ptr + BLOCK_HEADER_SIZE, BLOCK_DATA_SIZE);
  523. }
  524. },
  525. i * BLOCKS_PER_GROUP / threads, (i + 1) * BLOCKS_PER_GROUP / threads);
  526. }
  527. for (std::future<void>& future : encryption_futures)
  528. future.get();
  529. return true;
  530. }
  531. void VolumeWii::DecryptBlockHashes(const u8* in, HashBlock* out, Common::AES::Context* aes_context)
  532. {
  533. aes_context->CryptIvZero(in, reinterpret_cast<u8*>(out), sizeof(HashBlock));
  534. }
  535. void VolumeWii::DecryptBlockData(const u8* in, u8* out, Common::AES::Context* aes_context)
  536. {
  537. aes_context->Crypt(&in[0x3d0], &in[sizeof(HashBlock)], out, BLOCK_DATA_SIZE);
  538. }
  539. } // namespace DiscIO