VolumeWii.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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() = default;
  141. bool VolumeWii::Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const
  142. {
  143. if (partition == PARTITION_NONE)
  144. return m_reader->Read(offset, length, buffer);
  145. auto it = m_partitions.find(partition);
  146. if (it == m_partitions.end())
  147. return false;
  148. const PartitionDetails& partition_details = it->second;
  149. const u64 partition_data_offset = partition.offset + *partition_details.data_offset;
  150. if (m_has_hashes && m_has_encryption &&
  151. m_reader->SupportsReadWiiDecrypted(offset, length, partition_data_offset))
  152. {
  153. return m_reader->ReadWiiDecrypted(offset, length, buffer, partition_data_offset);
  154. }
  155. if (!m_has_hashes)
  156. {
  157. return m_reader->Read(partition_data_offset + offset, length, buffer);
  158. }
  159. Common::AES::Context* aes_context = nullptr;
  160. std::unique_ptr<u8[]> read_buffer = nullptr;
  161. if (m_has_encryption)
  162. {
  163. aes_context = partition_details.key->get();
  164. if (!aes_context)
  165. return false;
  166. read_buffer = std::make_unique<u8[]>(BLOCK_TOTAL_SIZE);
  167. }
  168. while (length > 0)
  169. {
  170. // Calculate offsets
  171. u64 block_offset_on_disc = partition_data_offset + offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE;
  172. u64 data_offset_in_block = offset % BLOCK_DATA_SIZE;
  173. if (m_last_decrypted_block != block_offset_on_disc)
  174. {
  175. if (m_has_encryption)
  176. {
  177. // Read the current block
  178. if (!m_reader->Read(block_offset_on_disc, BLOCK_TOTAL_SIZE, read_buffer.get()))
  179. return false;
  180. // Decrypt the block's data
  181. DecryptBlockData(read_buffer.get(), m_last_decrypted_block_data, aes_context);
  182. }
  183. else
  184. {
  185. // Read the current block
  186. if (!m_reader->Read(block_offset_on_disc + BLOCK_HEADER_SIZE, BLOCK_DATA_SIZE,
  187. m_last_decrypted_block_data))
  188. {
  189. return false;
  190. }
  191. }
  192. m_last_decrypted_block = block_offset_on_disc;
  193. }
  194. // Copy the decrypted data
  195. u64 copy_size = std::min(length, BLOCK_DATA_SIZE - data_offset_in_block);
  196. memcpy(buffer, &m_last_decrypted_block_data[data_offset_in_block],
  197. static_cast<size_t>(copy_size));
  198. // Update offsets
  199. length -= copy_size;
  200. buffer += copy_size;
  201. offset += copy_size;
  202. }
  203. return true;
  204. }
  205. bool VolumeWii::HasWiiHashes() const
  206. {
  207. return m_has_hashes;
  208. }
  209. bool VolumeWii::HasWiiEncryption() const
  210. {
  211. return m_has_encryption;
  212. }
  213. std::vector<Partition> VolumeWii::GetPartitions() const
  214. {
  215. std::vector<Partition> partitions;
  216. for (const auto& pair : m_partitions)
  217. partitions.push_back(pair.first);
  218. return partitions;
  219. }
  220. Partition VolumeWii::GetGamePartition() const
  221. {
  222. return m_game_partition;
  223. }
  224. std::optional<u32> VolumeWii::GetPartitionType(const Partition& partition) const
  225. {
  226. auto it = m_partitions.find(partition);
  227. return it != m_partitions.end() ? it->second.type : std::optional<u32>();
  228. }
  229. std::optional<u64> VolumeWii::GetTitleID(const Partition& partition) const
  230. {
  231. const IOS::ES::TicketReader& ticket = GetTicket(partition);
  232. if (!ticket.IsValid())
  233. return {};
  234. return ticket.GetTitleId();
  235. }
  236. const IOS::ES::TicketReader& VolumeWii::GetTicket(const Partition& partition) const
  237. {
  238. auto it = m_partitions.find(partition);
  239. return it != m_partitions.end() ? *it->second.ticket : INVALID_TICKET;
  240. }
  241. const IOS::ES::TMDReader& VolumeWii::GetTMD(const Partition& partition) const
  242. {
  243. auto it = m_partitions.find(partition);
  244. return it != m_partitions.end() ? *it->second.tmd : INVALID_TMD;
  245. }
  246. const std::vector<u8>& VolumeWii::GetCertificateChain(const Partition& partition) const
  247. {
  248. auto it = m_partitions.find(partition);
  249. return it != m_partitions.end() ? *it->second.cert_chain : INVALID_CERT_CHAIN;
  250. }
  251. const FileSystem* VolumeWii::GetFileSystem(const Partition& partition) const
  252. {
  253. auto it = m_partitions.find(partition);
  254. return it != m_partitions.end() ? it->second.file_system->get() : nullptr;
  255. }
  256. u64 VolumeWii::OffsetInHashedPartitionToRawOffset(u64 offset, const Partition& partition,
  257. u64 partition_data_offset)
  258. {
  259. if (partition == PARTITION_NONE)
  260. return offset;
  261. return partition.offset + partition_data_offset + (offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE) +
  262. (offset % BLOCK_DATA_SIZE);
  263. }
  264. u64 VolumeWii::PartitionOffsetToRawOffset(u64 offset, const Partition& partition) const
  265. {
  266. auto it = m_partitions.find(partition);
  267. if (it == m_partitions.end())
  268. return offset;
  269. const u64 data_offset = *it->second.data_offset;
  270. if (!m_has_hashes)
  271. return partition.offset + data_offset + offset;
  272. return OffsetInHashedPartitionToRawOffset(offset, partition, data_offset);
  273. }
  274. std::string VolumeWii::GetGameTDBID(const Partition& partition) const
  275. {
  276. return GetGameID(partition);
  277. }
  278. Region VolumeWii::GetRegion() const
  279. {
  280. return RegionCodeToRegion(m_reader->ReadSwapped<u32>(0x4E000));
  281. }
  282. std::map<Language, std::string> VolumeWii::GetLongNames() const
  283. {
  284. std::vector<char16_t> names(NAMES_TOTAL_CHARS);
  285. names.resize(ReadFile(*this, GetGamePartition(), "opening.bnr",
  286. reinterpret_cast<u8*>(names.data()), NAMES_TOTAL_BYTES, 0x5C));
  287. return ReadWiiNames(names);
  288. }
  289. std::vector<u32> VolumeWii::GetBanner(u32* width, u32* height) const
  290. {
  291. *width = 0;
  292. *height = 0;
  293. const std::optional<u64> title_id = GetTitleID(GetGamePartition());
  294. if (!title_id)
  295. return std::vector<u32>();
  296. return WiiSaveBanner(*title_id).GetBanner(width, height);
  297. }
  298. Platform VolumeWii::GetVolumeType() const
  299. {
  300. return Platform::WiiDisc;
  301. }
  302. bool VolumeWii::IsDatelDisc() const
  303. {
  304. return m_game_partition == PARTITION_NONE;
  305. }
  306. BlobType VolumeWii::GetBlobType() const
  307. {
  308. return m_reader->GetBlobType();
  309. }
  310. u64 VolumeWii::GetDataSize() const
  311. {
  312. return m_reader->GetDataSize();
  313. }
  314. DataSizeType VolumeWii::GetDataSizeType() const
  315. {
  316. return m_reader->GetDataSizeType();
  317. }
  318. u64 VolumeWii::GetRawSize() const
  319. {
  320. return m_reader->GetRawSize();
  321. }
  322. const BlobReader& VolumeWii::GetBlobReader() const
  323. {
  324. return *m_reader;
  325. }
  326. std::array<u8, 20> VolumeWii::GetSyncHash() const
  327. {
  328. auto context = Common::SHA1::CreateContext();
  329. // Disc header
  330. ReadAndAddToSyncHash(context.get(), 0, 0x80, PARTITION_NONE);
  331. // Region code
  332. ReadAndAddToSyncHash(context.get(), 0x4E000, 4, PARTITION_NONE);
  333. // The data offset of the game partition - an important factor for disc drive timings
  334. const u64 data_offset = PartitionOffsetToRawOffset(0, GetGamePartition());
  335. context->Update(reinterpret_cast<const u8*>(&data_offset), sizeof(data_offset));
  336. // TMD
  337. AddTMDToSyncHash(context.get(), GetGamePartition());
  338. // Game partition contents
  339. AddGamePartitionToSyncHash(context.get());
  340. return context->Finish();
  341. }
  342. bool VolumeWii::CheckH3TableIntegrity(const Partition& partition) const
  343. {
  344. auto it = m_partitions.find(partition);
  345. if (it == m_partitions.end())
  346. return false;
  347. const PartitionDetails& partition_details = it->second;
  348. const std::vector<u8>& h3_table = *partition_details.h3_table;
  349. if (h3_table.size() != WII_PARTITION_H3_SIZE)
  350. return false;
  351. const IOS::ES::TMDReader& tmd = *partition_details.tmd;
  352. if (!tmd.IsValid())
  353. return false;
  354. const std::vector<IOS::ES::Content> contents = tmd.GetContents();
  355. if (contents.size() != 1)
  356. return false;
  357. return Common::SHA1::CalculateDigest(h3_table) == contents[0].sha1;
  358. }
  359. bool VolumeWii::CheckBlockIntegrity(u64 block_index, const u8* encrypted_data,
  360. const Partition& partition) const
  361. {
  362. auto it = m_partitions.find(partition);
  363. if (it == m_partitions.end())
  364. return false;
  365. const PartitionDetails& partition_details = it->second;
  366. if (block_index / BLOCKS_PER_GROUP * Common::SHA1::DIGEST_LEN >=
  367. partition_details.h3_table->size())
  368. {
  369. return false;
  370. }
  371. HashBlock hashes;
  372. u8 cluster_data_buffer[BLOCK_DATA_SIZE];
  373. const u8* cluster_data;
  374. if (m_has_encryption)
  375. {
  376. Common::AES::Context* aes_context = partition_details.key->get();
  377. if (!aes_context)
  378. return false;
  379. DecryptBlockHashes(encrypted_data, &hashes, aes_context);
  380. DecryptBlockData(encrypted_data, cluster_data_buffer, aes_context);
  381. cluster_data = cluster_data_buffer;
  382. }
  383. else
  384. {
  385. std::memcpy(&hashes, encrypted_data, BLOCK_HEADER_SIZE);
  386. cluster_data = encrypted_data + BLOCK_HEADER_SIZE;
  387. }
  388. for (u32 hash_index = 0; hash_index < 31; ++hash_index)
  389. {
  390. if (Common::SHA1::CalculateDigest(&cluster_data[hash_index * 0x400], 0x400) !=
  391. hashes.h0[hash_index])
  392. {
  393. return false;
  394. }
  395. }
  396. if (Common::SHA1::CalculateDigest(hashes.h0) != hashes.h1[block_index % 8])
  397. return false;
  398. if (Common::SHA1::CalculateDigest(hashes.h1) != hashes.h2[block_index / 8 % 8])
  399. return false;
  400. Common::SHA1::Digest h3_digest;
  401. auto h3_digest_ptr =
  402. partition_details.h3_table->data() + block_index / 64 * Common::SHA1::DIGEST_LEN;
  403. memcpy(h3_digest.data(), h3_digest_ptr, sizeof(h3_digest));
  404. if (Common::SHA1::CalculateDigest(hashes.h2) != h3_digest)
  405. return false;
  406. return true;
  407. }
  408. bool VolumeWii::CheckBlockIntegrity(u64 block_index, const Partition& partition) const
  409. {
  410. auto it = m_partitions.find(partition);
  411. if (it == m_partitions.end())
  412. return false;
  413. const PartitionDetails& partition_details = it->second;
  414. const u64 cluster_offset =
  415. partition.offset + *partition_details.data_offset + block_index * BLOCK_TOTAL_SIZE;
  416. std::vector<u8> cluster(BLOCK_TOTAL_SIZE);
  417. if (!m_reader->Read(cluster_offset, cluster.size(), cluster.data()))
  418. return false;
  419. return CheckBlockIntegrity(block_index, cluster.data(), partition);
  420. }
  421. bool VolumeWii::HashGroup(const std::array<u8, BLOCK_DATA_SIZE> in[BLOCKS_PER_GROUP],
  422. HashBlock out[BLOCKS_PER_GROUP],
  423. const std::function<bool(size_t block)>& read_function)
  424. {
  425. std::array<std::future<void>, BLOCKS_PER_GROUP> hash_futures;
  426. bool success = true;
  427. for (size_t i = 0; i < BLOCKS_PER_GROUP; ++i)
  428. {
  429. if (read_function && success)
  430. success = read_function(i);
  431. hash_futures[i] = std::async(std::launch::async, [&in, &out, &hash_futures, success, i]() {
  432. const size_t h1_base = Common::AlignDown(i, 8);
  433. if (success)
  434. {
  435. // H0 hashes
  436. for (size_t j = 0; j < 31; ++j)
  437. out[i].h0[j] = Common::SHA1::CalculateDigest(in[i].data() + j * 0x400, 0x400);
  438. // H0 padding
  439. out[i].padding_0 = {};
  440. // H1 hash
  441. out[h1_base].h1[i - h1_base] = Common::SHA1::CalculateDigest(out[i].h0);
  442. }
  443. if (i % 8 == 7)
  444. {
  445. for (size_t j = 0; j < 7; ++j)
  446. hash_futures[h1_base + j].get();
  447. if (success)
  448. {
  449. // H1 padding
  450. out[h1_base].padding_1 = {};
  451. // H1 copies
  452. for (size_t j = 1; j < 8; ++j)
  453. out[h1_base + j].h1 = out[h1_base].h1;
  454. // H2 hash
  455. out[0].h2[h1_base / 8] = Common::SHA1::CalculateDigest(out[i].h1);
  456. }
  457. if (i == BLOCKS_PER_GROUP - 1)
  458. {
  459. for (size_t j = 0; j < 7; ++j)
  460. hash_futures[j * 8 + 7].get();
  461. if (success)
  462. {
  463. // H2 padding
  464. out[0].padding_2 = {};
  465. // H2 copies
  466. for (size_t j = 1; j < BLOCKS_PER_GROUP; ++j)
  467. out[j].h2 = out[0].h2;
  468. }
  469. }
  470. }
  471. });
  472. }
  473. // Wait for all the async tasks to finish
  474. hash_futures.back().get();
  475. return success;
  476. }
  477. bool VolumeWii::EncryptGroup(
  478. u64 offset, u64 partition_data_offset, u64 partition_data_decrypted_size,
  479. const std::array<u8, AES_KEY_SIZE>& key, BlobReader* blob,
  480. std::array<u8, GROUP_TOTAL_SIZE>* out,
  481. const std::function<void(HashBlock hash_blocks[BLOCKS_PER_GROUP])>& hash_exception_callback)
  482. {
  483. std::vector<std::array<u8, BLOCK_DATA_SIZE>> unencrypted_data(BLOCKS_PER_GROUP);
  484. std::vector<HashBlock> unencrypted_hashes(BLOCKS_PER_GROUP);
  485. const bool success =
  486. HashGroup(unencrypted_data.data(), unencrypted_hashes.data(), [&](size_t block) {
  487. if (offset + (block + 1) * BLOCK_DATA_SIZE <= partition_data_decrypted_size)
  488. {
  489. if (!blob->ReadWiiDecrypted(offset + block * BLOCK_DATA_SIZE, BLOCK_DATA_SIZE,
  490. unencrypted_data[block].data(), partition_data_offset))
  491. {
  492. return false;
  493. }
  494. }
  495. else
  496. {
  497. unencrypted_data[block].fill(0);
  498. }
  499. return true;
  500. });
  501. if (!success)
  502. return false;
  503. if (hash_exception_callback)
  504. hash_exception_callback(unencrypted_hashes.data());
  505. const unsigned int threads =
  506. std::min(BLOCKS_PER_GROUP, std::max<unsigned int>(1, std::thread::hardware_concurrency()));
  507. std::vector<std::future<void>> encryption_futures(threads);
  508. auto aes_context = Common::AES::CreateContextEncrypt(key.data());
  509. for (size_t i = 0; i < threads; ++i)
  510. {
  511. encryption_futures[i] = std::async(
  512. std::launch::async,
  513. [&unencrypted_data, &unencrypted_hashes, &aes_context, &out](size_t start, size_t end) {
  514. for (size_t j = start; j < end; ++j)
  515. {
  516. u8* out_ptr = out->data() + j * BLOCK_TOTAL_SIZE;
  517. aes_context->CryptIvZero(reinterpret_cast<u8*>(&unencrypted_hashes[j]), out_ptr,
  518. BLOCK_HEADER_SIZE);
  519. aes_context->Crypt(out_ptr + 0x3D0, unencrypted_data[j].data(),
  520. out_ptr + BLOCK_HEADER_SIZE, BLOCK_DATA_SIZE);
  521. }
  522. },
  523. i * BLOCKS_PER_GROUP / threads, (i + 1) * BLOCKS_PER_GROUP / threads);
  524. }
  525. for (std::future<void>& future : encryption_futures)
  526. future.get();
  527. return true;
  528. }
  529. void VolumeWii::DecryptBlockHashes(const u8* in, HashBlock* out, Common::AES::Context* aes_context)
  530. {
  531. aes_context->CryptIvZero(in, reinterpret_cast<u8*>(out), sizeof(HashBlock));
  532. }
  533. void VolumeWii::DecryptBlockData(const u8* in, u8* out, Common::AES::Context* aes_context)
  534. {
  535. aes_context->Crypt(&in[0x3d0], &in[sizeof(HashBlock)], out, BLOCK_DATA_SIZE);
  536. }
  537. } // namespace DiscIO