aes_util.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <mbedtls/cipher.h>
  5. #include "common/assert.h"
  6. #include "common/logging/log.h"
  7. #include "core/crypto/aes_util.h"
  8. #include "core/crypto/key_manager.h"
  9. namespace Core::Crypto {
  10. namespace {
  11. std::vector<u8> CalculateNintendoTweak(std::size_t sector_id) {
  12. std::vector<u8> out(0x10);
  13. for (std::size_t i = 0xF; i <= 0xF; --i) {
  14. out[i] = sector_id & 0xFF;
  15. sector_id >>= 8;
  16. }
  17. return out;
  18. }
  19. } // Anonymous namespace
  20. static_assert(static_cast<std::size_t>(Mode::CTR) ==
  21. static_cast<std::size_t>(MBEDTLS_CIPHER_AES_128_CTR),
  22. "CTR has incorrect value.");
  23. static_assert(static_cast<std::size_t>(Mode::ECB) ==
  24. static_cast<std::size_t>(MBEDTLS_CIPHER_AES_128_ECB),
  25. "ECB has incorrect value.");
  26. static_assert(static_cast<std::size_t>(Mode::XTS) ==
  27. static_cast<std::size_t>(MBEDTLS_CIPHER_AES_128_XTS),
  28. "XTS has incorrect value.");
  29. // Structure to hide mbedtls types from header file
  30. struct CipherContext {
  31. mbedtls_cipher_context_t encryption_context;
  32. mbedtls_cipher_context_t decryption_context;
  33. };
  34. template <typename Key, std::size_t KeySize>
  35. Crypto::AESCipher<Key, KeySize>::AESCipher(Key key, Mode mode)
  36. : ctx(std::make_unique<CipherContext>()) {
  37. mbedtls_cipher_init(&ctx->encryption_context);
  38. mbedtls_cipher_init(&ctx->decryption_context);
  39. ASSERT_MSG((mbedtls_cipher_setup(
  40. &ctx->encryption_context,
  41. mbedtls_cipher_info_from_type(static_cast<mbedtls_cipher_type_t>(mode))) ||
  42. mbedtls_cipher_setup(
  43. &ctx->decryption_context,
  44. mbedtls_cipher_info_from_type(static_cast<mbedtls_cipher_type_t>(mode)))) == 0,
  45. "Failed to initialize mbedtls ciphers.");
  46. ASSERT(
  47. !mbedtls_cipher_setkey(&ctx->encryption_context, key.data(), KeySize * 8, MBEDTLS_ENCRYPT));
  48. ASSERT(
  49. !mbedtls_cipher_setkey(&ctx->decryption_context, key.data(), KeySize * 8, MBEDTLS_DECRYPT));
  50. //"Failed to set key on mbedtls ciphers.");
  51. }
  52. template <typename Key, std::size_t KeySize>
  53. AESCipher<Key, KeySize>::~AESCipher() {
  54. mbedtls_cipher_free(&ctx->encryption_context);
  55. mbedtls_cipher_free(&ctx->decryption_context);
  56. }
  57. template <typename Key, std::size_t KeySize>
  58. void AESCipher<Key, KeySize>::SetIV(std::vector<u8> iv) {
  59. ASSERT_MSG((mbedtls_cipher_set_iv(&ctx->encryption_context, iv.data(), iv.size()) ||
  60. mbedtls_cipher_set_iv(&ctx->decryption_context, iv.data(), iv.size())) == 0,
  61. "Failed to set IV on mbedtls ciphers.");
  62. }
  63. template <typename Key, std::size_t KeySize>
  64. void AESCipher<Key, KeySize>::Transcode(const u8* src, std::size_t size, u8* dest, Op op) const {
  65. auto* const context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context;
  66. mbedtls_cipher_reset(context);
  67. std::size_t written = 0;
  68. if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) {
  69. mbedtls_cipher_update(context, src, size, dest, &written);
  70. if (written != size) {
  71. LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.",
  72. size, written);
  73. }
  74. } else {
  75. const auto block_size = mbedtls_cipher_get_block_size(context);
  76. if (size < block_size) {
  77. std::vector<u8> block(block_size);
  78. std::memcpy(block.data(), src, size);
  79. Transcode(block.data(), block.size(), block.data(), op);
  80. std::memcpy(dest, block.data(), size);
  81. return;
  82. }
  83. for (std::size_t offset = 0; offset < size; offset += block_size) {
  84. auto length = std::min<std::size_t>(block_size, size - offset);
  85. mbedtls_cipher_update(context, src + offset, length, dest + offset, &written);
  86. if (written != length) {
  87. if (length < block_size) {
  88. std::vector<u8> block(block_size);
  89. std::memcpy(block.data(), src + offset, length);
  90. Transcode(block.data(), block.size(), block.data(), op);
  91. std::memcpy(dest + offset, block.data(), length);
  92. return;
  93. }
  94. LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.",
  95. length, written);
  96. }
  97. }
  98. }
  99. mbedtls_cipher_finish(context, nullptr, nullptr);
  100. }
  101. template <typename Key, std::size_t KeySize>
  102. void AESCipher<Key, KeySize>::XTSTranscode(const u8* src, std::size_t size, u8* dest,
  103. std::size_t sector_id, std::size_t sector_size, Op op) {
  104. ASSERT_MSG(size % sector_size == 0, "XTS decryption size must be a multiple of sector size.");
  105. for (std::size_t i = 0; i < size; i += sector_size) {
  106. SetIV(CalculateNintendoTweak(sector_id++));
  107. Transcode<u8, u8>(src + i, sector_size, dest + i, op);
  108. }
  109. }
  110. template class AESCipher<Key128>;
  111. template class AESCipher<Key256>;
  112. } // namespace Core::Crypto