AES.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2017 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <memory>
  5. #include "Common/CommonTypes.h"
  6. // Dolphin only uses/implements AES-128-CBC.
  7. namespace Common::AES
  8. {
  9. enum class Mode
  10. {
  11. Decrypt,
  12. Encrypt,
  13. };
  14. class Context
  15. {
  16. protected:
  17. static constexpr size_t Nk = 4;
  18. static constexpr size_t Nb = 4;
  19. static constexpr size_t Nr = 10;
  20. static constexpr size_t WORD_SIZE = sizeof(u32);
  21. static constexpr size_t NUM_ROUND_KEYS = Nr + 1;
  22. public:
  23. static constexpr size_t KEY_SIZE = Nk * WORD_SIZE;
  24. static constexpr size_t BLOCK_SIZE = Nb * WORD_SIZE;
  25. Context() = default;
  26. virtual ~Context() = default;
  27. virtual bool Crypt(const u8* iv, u8* iv_out, const u8* buf_in, u8* buf_out, size_t len) const = 0;
  28. bool Crypt(const u8* iv, const u8* buf_in, u8* buf_out, size_t len) const
  29. {
  30. return Crypt(iv, nullptr, buf_in, buf_out, len);
  31. }
  32. bool CryptIvZero(const u8* buf_in, u8* buf_out, size_t len) const
  33. {
  34. return Crypt(nullptr, nullptr, buf_in, buf_out, len);
  35. }
  36. };
  37. std::unique_ptr<Context> CreateContextEncrypt(const u8* key);
  38. std::unique_ptr<Context> CreateContextDecrypt(const u8* key);
  39. // OFB decryption for WiiConnect24
  40. void CryptOFB(const u8* key, const u8* iv, u8* iv_out, const u8* buf_in, u8* buf_out, size_t size);
  41. } // namespace Common::AES