WiiEncryptionCache.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2020 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <limits>
  6. #include <memory>
  7. #include "Common/CommonTypes.h"
  8. #include "DiscIO/VolumeWii.h"
  9. namespace DiscIO
  10. {
  11. class BlobReader;
  12. class WiiEncryptionCache
  13. {
  14. public:
  15. using Key = std::array<u8, VolumeWii::AES_KEY_SIZE>;
  16. using HashExceptionCallback = std::function<void(
  17. VolumeWii::HashBlock hash_blocks[VolumeWii::BLOCKS_PER_GROUP], u64 offset)>;
  18. // The blob pointer is kept around for the lifetime of this object.
  19. explicit WiiEncryptionCache(BlobReader* blob);
  20. ~WiiEncryptionCache();
  21. WiiEncryptionCache(WiiEncryptionCache&&) = default;
  22. WiiEncryptionCache& operator=(WiiEncryptionCache&&) = default;
  23. // It would be possible to write a custom copy constructor and assignment operator
  24. // for this class, but there has been no reason to do so.
  25. WiiEncryptionCache(const WiiEncryptionCache&) = delete;
  26. WiiEncryptionCache& operator=(const WiiEncryptionCache&) = delete;
  27. // Encrypts exactly one group.
  28. // If the returned pointer is nullptr, reading from the blob failed.
  29. // If the returned pointer is not nullptr, it is guaranteed to be valid until
  30. // the next call of this function or the destruction of this object.
  31. const std::array<u8, VolumeWii::GROUP_TOTAL_SIZE>*
  32. EncryptGroup(u64 offset, u64 partition_data_offset, u64 partition_data_decrypted_size,
  33. const Key& key, const HashExceptionCallback& hash_exception_callback = {});
  34. // Encrypts a variable number of groups, as determined by the offset and size parameters.
  35. // Supports reading groups partially.
  36. bool EncryptGroups(u64 offset, u64 size, u8* out_ptr, u64 partition_data_offset,
  37. u64 partition_data_decrypted_size, const Key& key,
  38. const HashExceptionCallback& hash_exception_callback = {});
  39. private:
  40. BlobReader* m_blob;
  41. std::unique_ptr<std::array<u8, VolumeWii::GROUP_TOTAL_SIZE>> m_cache;
  42. u64 m_cached_offset = 0;
  43. };
  44. } // namespace DiscIO