VolumeWiiCrypted.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Copyright 2008 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <cstddef>
  5. #include <cstring>
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include <polarssl/aes.h>
  10. #include <polarssl/sha1.h>
  11. #include "Common/CommonFuncs.h"
  12. #include "Common/CommonTypes.h"
  13. #include "Common/MsgHandler.h"
  14. #include "Common/Logging/Log.h"
  15. #include "DiscIO/Blob.h"
  16. #include "DiscIO/FileMonitor.h"
  17. #include "DiscIO/Filesystem.h"
  18. #include "DiscIO/Volume.h"
  19. #include "DiscIO/VolumeCreator.h"
  20. #include "DiscIO/VolumeGC.h"
  21. #include "DiscIO/VolumeWiiCrypted.h"
  22. namespace DiscIO
  23. {
  24. CVolumeWiiCrypted::CVolumeWiiCrypted(IBlobReader* _pReader, u64 _VolumeOffset,
  25. const unsigned char* _pVolumeKey)
  26. : m_pReader(_pReader),
  27. m_AES_ctx(new aes_context),
  28. m_pBuffer(nullptr),
  29. m_VolumeOffset(_VolumeOffset),
  30. m_dataOffset(0x20000),
  31. m_LastDecryptedBlockOffset(-1)
  32. {
  33. aes_setkey_dec(m_AES_ctx.get(), _pVolumeKey, 128);
  34. m_pBuffer = new u8[s_block_total_size];
  35. }
  36. bool CVolumeWiiCrypted::ChangePartition(u64 offset)
  37. {
  38. m_VolumeOffset = offset;
  39. m_LastDecryptedBlockOffset = -1;
  40. u8 volume_key[16];
  41. DiscIO::VolumeKeyForParition(*m_pReader, offset, volume_key);
  42. aes_setkey_dec(m_AES_ctx.get(), volume_key, 128);
  43. return true;
  44. }
  45. CVolumeWiiCrypted::~CVolumeWiiCrypted()
  46. {
  47. delete[] m_pBuffer;
  48. m_pBuffer = nullptr;
  49. }
  50. bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, bool decrypt) const
  51. {
  52. if (m_pReader == nullptr)
  53. return false;
  54. if (!decrypt)
  55. return m_pReader->Read(_ReadOffset, _Length, _pBuffer);
  56. FileMon::FindFilename(_ReadOffset);
  57. while (_Length > 0)
  58. {
  59. // Calculate block offset
  60. u64 Block = _ReadOffset / s_block_data_size;
  61. u64 Offset = _ReadOffset % s_block_data_size;
  62. if (m_LastDecryptedBlockOffset != Block)
  63. {
  64. // Read the current block
  65. if (!m_pReader->Read(m_VolumeOffset + m_dataOffset + Block * s_block_total_size, s_block_total_size, m_pBuffer))
  66. return false;
  67. // Decrypt the block's data.
  68. // 0x3D0 - 0x3DF in m_pBuffer will be overwritten,
  69. // but that won't affect anything, because we won't
  70. // use the content of m_pBuffer anymore after this
  71. aes_crypt_cbc(m_AES_ctx.get(), AES_DECRYPT, s_block_data_size, m_pBuffer + 0x3D0,
  72. m_pBuffer + s_block_header_size, m_LastDecryptedBlock);
  73. m_LastDecryptedBlockOffset = Block;
  74. // The only thing we currently use from the 0x000 - 0x3FF part
  75. // of the block is the IV (at 0x3D0), but it also contains SHA-1
  76. // hashes that IOS uses to check that discs aren't tampered with.
  77. // http://wiibrew.org/wiki/Wii_Disc#Encrypted
  78. }
  79. // Copy the decrypted data
  80. u64 MaxSizeToCopy = s_block_data_size - Offset;
  81. u64 CopySize = (_Length > MaxSizeToCopy) ? MaxSizeToCopy : _Length;
  82. memcpy(_pBuffer, &m_LastDecryptedBlock[Offset], (size_t)CopySize);
  83. // Update offsets
  84. _Length -= CopySize;
  85. _pBuffer += CopySize;
  86. _ReadOffset += CopySize;
  87. }
  88. return true;
  89. }
  90. bool CVolumeWiiCrypted::GetTitleID(u8* _pBuffer) const
  91. {
  92. // Tik is at m_VolumeOffset size 0x2A4
  93. // TitleID offset in tik is 0x1DC
  94. return Read(m_VolumeOffset + 0x1DC, 8, _pBuffer, false);
  95. }
  96. std::unique_ptr<u8[]> CVolumeWiiCrypted::GetTMD(u32 *size) const
  97. {
  98. *size = 0;
  99. u32 tmd_size;
  100. u32 tmd_address;
  101. Read(m_VolumeOffset + 0x2a4, sizeof(u32), (u8*)&tmd_size, false);
  102. Read(m_VolumeOffset + 0x2a8, sizeof(u32), (u8*)&tmd_address, false);
  103. tmd_size = Common::swap32(tmd_size);
  104. tmd_address = Common::swap32(tmd_address) << 2;
  105. if (tmd_size > 1024 * 1024 * 4)
  106. {
  107. // The size is checked so that a malicious or corrupt ISO
  108. // can't force Dolphin to allocate up to 4 GiB of memory.
  109. // 4 MiB should be much bigger than the size of TMDs and much smaller
  110. // than the amount of RAM in a computer that can run Dolphin.
  111. PanicAlert("TMD > 4 MiB");
  112. tmd_size = 1024 * 1024 * 4;
  113. }
  114. std::unique_ptr<u8[]> buf{ new u8[tmd_size] };
  115. Read(m_VolumeOffset + tmd_address, tmd_size, buf.get(), false);
  116. *size = tmd_size;
  117. return buf;
  118. }
  119. std::string CVolumeWiiCrypted::GetUniqueID() const
  120. {
  121. if (m_pReader == nullptr)
  122. return std::string();
  123. char ID[7];
  124. if (!Read(0, 6, (u8*)ID, false))
  125. return std::string();
  126. ID[6] = '\0';
  127. return ID;
  128. }
  129. IVolume::ECountry CVolumeWiiCrypted::GetCountry() const
  130. {
  131. if (!m_pReader)
  132. return COUNTRY_UNKNOWN;
  133. u8 country_code;
  134. m_pReader->Read(3, 1, &country_code);
  135. return CountrySwitch(country_code);
  136. }
  137. std::string CVolumeWiiCrypted::GetMakerID() const
  138. {
  139. if (m_pReader == nullptr)
  140. return std::string();
  141. char makerID[3];
  142. if (!Read(0x4, 0x2, (u8*)&makerID, false))
  143. return std::string();
  144. makerID[2] = '\0';
  145. return makerID;
  146. }
  147. u16 CVolumeWiiCrypted::GetRevision() const
  148. {
  149. if (!m_pReader)
  150. return 0;
  151. u8 revision;
  152. if (!m_pReader->Read(7, 1, &revision))
  153. return 0;
  154. return revision;
  155. }
  156. std::string CVolumeWiiCrypted::GetInternalName() const
  157. {
  158. char name_buffer[0x60];
  159. if (m_pReader != nullptr && Read(0x20, 0x60, (u8*)&name_buffer, false))
  160. return DecodeString(name_buffer);
  161. return "";
  162. }
  163. std::map<IVolume::ELanguage, std::string> CVolumeWiiCrypted::GetNames(bool prefer_long) const
  164. {
  165. std::unique_ptr<IFileSystem> file_system(CreateFileSystem(this));
  166. std::vector<u8> opening_bnr(NAMES_TOTAL_BYTES);
  167. opening_bnr.resize(file_system->ReadFile("opening.bnr", opening_bnr.data(), opening_bnr.size(), 0x5C));
  168. return ReadWiiNames(opening_bnr);
  169. }
  170. u64 CVolumeWiiCrypted::GetFSTSize() const
  171. {
  172. if (m_pReader == nullptr)
  173. return 0;
  174. u32 size;
  175. if (!Read(0x428, 0x4, (u8*)&size, true))
  176. return 0;
  177. return (u64)Common::swap32(size) << 2;
  178. }
  179. std::string CVolumeWiiCrypted::GetApploaderDate() const
  180. {
  181. if (m_pReader == nullptr)
  182. return std::string();
  183. char date[16];
  184. if (!Read(0x2440, 0x10, (u8*)&date, true))
  185. return std::string();
  186. date[10] = '\0';
  187. return date;
  188. }
  189. IVolume::EPlatform CVolumeWiiCrypted::GetVolumeType() const
  190. {
  191. return WII_DISC;
  192. }
  193. u8 CVolumeWiiCrypted::GetDiscNumber() const
  194. {
  195. u8 disc_number;
  196. m_pReader->Read(6, 1, &disc_number);
  197. return disc_number;
  198. }
  199. u64 CVolumeWiiCrypted::GetSize() const
  200. {
  201. if (m_pReader)
  202. return m_pReader->GetDataSize();
  203. else
  204. return 0;
  205. }
  206. u64 CVolumeWiiCrypted::GetRawSize() const
  207. {
  208. if (m_pReader)
  209. return m_pReader->GetRawSize();
  210. else
  211. return 0;
  212. }
  213. bool CVolumeWiiCrypted::CheckIntegrity() const
  214. {
  215. // Get partition data size
  216. u32 partSizeDiv4;
  217. Read(m_VolumeOffset + 0x2BC, 4, (u8*)&partSizeDiv4, false);
  218. u64 partDataSize = (u64)Common::swap32(partSizeDiv4) * 4;
  219. u32 nClusters = (u32)(partDataSize / 0x8000);
  220. for (u32 clusterID = 0; clusterID < nClusters; ++clusterID)
  221. {
  222. u64 clusterOff = m_VolumeOffset + m_dataOffset + (u64)clusterID * 0x8000;
  223. // Read and decrypt the cluster metadata
  224. u8 clusterMDCrypted[0x400];
  225. u8 clusterMD[0x400];
  226. u8 IV[16] = { 0 };
  227. if (!m_pReader->Read(clusterOff, 0x400, clusterMDCrypted))
  228. {
  229. NOTICE_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read metadata", clusterID);
  230. return false;
  231. }
  232. aes_crypt_cbc(m_AES_ctx.get(), AES_DECRYPT, 0x400, IV, clusterMDCrypted, clusterMD);
  233. // Some clusters have invalid data and metadata because they aren't
  234. // meant to be read by the game (for example, holes between files). To
  235. // try to avoid reporting errors because of these clusters, we check
  236. // the 0x00 paddings in the metadata.
  237. //
  238. // This may cause some false negatives though: some bad clusters may be
  239. // skipped because they are *too* bad and are not even recognized as
  240. // valid clusters. To be improved.
  241. bool meaningless = false;
  242. for (u32 idx = 0x26C; idx < 0x280; ++idx)
  243. if (clusterMD[idx] != 0)
  244. meaningless = true;
  245. if (meaningless)
  246. continue;
  247. u8 clusterData[0x7C00];
  248. if (!Read((u64)clusterID * 0x7C00, 0x7C00, clusterData, true))
  249. {
  250. NOTICE_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read data", clusterID);
  251. return false;
  252. }
  253. for (u32 hashID = 0; hashID < 31; ++hashID)
  254. {
  255. u8 hash[20];
  256. sha1(clusterData + hashID * 0x400, 0x400, hash);
  257. // Note that we do not use strncmp here
  258. if (memcmp(hash, clusterMD + hashID * 20, 20))
  259. {
  260. NOTICE_LOG(DISCIO, "Integrity Check: fail at cluster %d: hash %d is invalid", clusterID, hashID);
  261. return false;
  262. }
  263. }
  264. }
  265. return true;
  266. }
  267. } // namespace