diff options
| author | 2018-08-04 21:17:10 -0400 | |
|---|---|---|
| committer | 2018-08-04 21:17:10 -0400 | |
| commit | cd96c04339f0b457154ae17810408a9c39959962 (patch) | |
| tree | 46691894e3dab41efe835b9c7924668aecc079ef /src/core/crypto/aes_util.cpp | |
| parent | Merge pull request #923 from lioncash/pragma (diff) | |
| parent | aes_util: Add static assertion to Transcode() and XTSTranscode() to ensure we... (diff) | |
| download | yuzu-cd96c04339f0b457154ae17810408a9c39959962.tar.gz yuzu-cd96c04339f0b457154ae17810408a9c39959962.tar.xz yuzu-cd96c04339f0b457154ae17810408a9c39959962.zip | |
Merge pull request #921 from lioncash/view
core/crypto: Minor changes
Diffstat (limited to 'src/core/crypto/aes_util.cpp')
| -rw-r--r-- | src/core/crypto/aes_util.cpp | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/src/core/crypto/aes_util.cpp b/src/core/crypto/aes_util.cpp index 4690af5f8..a9876c83e 100644 --- a/src/core/crypto/aes_util.cpp +++ b/src/core/crypto/aes_util.cpp | |||
| @@ -3,10 +3,22 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <mbedtls/cipher.h> | 5 | #include <mbedtls/cipher.h> |
| 6 | #include "common/assert.h" | ||
| 7 | #include "common/logging/log.h" | ||
| 6 | #include "core/crypto/aes_util.h" | 8 | #include "core/crypto/aes_util.h" |
| 7 | #include "core/crypto/key_manager.h" | 9 | #include "core/crypto/key_manager.h" |
| 8 | 10 | ||
| 9 | namespace Core::Crypto { | 11 | namespace Core::Crypto { |
| 12 | namespace { | ||
| 13 | std::vector<u8> CalculateNintendoTweak(size_t sector_id) { | ||
| 14 | std::vector<u8> out(0x10); | ||
| 15 | for (size_t i = 0xF; i <= 0xF; --i) { | ||
| 16 | out[i] = sector_id & 0xFF; | ||
| 17 | sector_id >>= 8; | ||
| 18 | } | ||
| 19 | return out; | ||
| 20 | } | ||
| 21 | } // Anonymous namespace | ||
| 10 | 22 | ||
| 11 | static_assert(static_cast<size_t>(Mode::CTR) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_CTR), | 23 | static_assert(static_cast<size_t>(Mode::CTR) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_CTR), |
| 12 | "CTR has incorrect value."); | 24 | "CTR has incorrect value."); |
| @@ -56,27 +68,28 @@ void AESCipher<Key, KeySize>::SetIV(std::vector<u8> iv) { | |||
| 56 | } | 68 | } |
| 57 | 69 | ||
| 58 | template <typename Key, size_t KeySize> | 70 | template <typename Key, size_t KeySize> |
| 59 | void AESCipher<Key, KeySize>::Transcode(const u8* src, size_t size, u8* dest, Op op) { | 71 | void AESCipher<Key, KeySize>::Transcode(const u8* src, size_t size, u8* dest, Op op) const { |
| 60 | size_t written = 0; | 72 | auto* const context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context; |
| 61 | |||
| 62 | const auto context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context; | ||
| 63 | 73 | ||
| 64 | mbedtls_cipher_reset(context); | 74 | mbedtls_cipher_reset(context); |
| 65 | 75 | ||
| 76 | size_t written = 0; | ||
| 66 | if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) { | 77 | if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) { |
| 67 | mbedtls_cipher_update(context, src, size, dest, &written); | 78 | mbedtls_cipher_update(context, src, size, dest, &written); |
| 68 | if (written != size) | 79 | if (written != size) { |
| 69 | LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.", | 80 | LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.", |
| 70 | size, written); | 81 | size, written); |
| 82 | } | ||
| 71 | } else { | 83 | } else { |
| 72 | const auto block_size = mbedtls_cipher_get_block_size(context); | 84 | const auto block_size = mbedtls_cipher_get_block_size(context); |
| 73 | 85 | ||
| 74 | for (size_t offset = 0; offset < size; offset += block_size) { | 86 | for (size_t offset = 0; offset < size; offset += block_size) { |
| 75 | auto length = std::min<size_t>(block_size, size - offset); | 87 | auto length = std::min<size_t>(block_size, size - offset); |
| 76 | mbedtls_cipher_update(context, src + offset, length, dest + offset, &written); | 88 | mbedtls_cipher_update(context, src + offset, length, dest + offset, &written); |
| 77 | if (written != length) | 89 | if (written != length) { |
| 78 | LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.", | 90 | LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.", |
| 79 | length, written); | 91 | length, written); |
| 92 | } | ||
| 80 | } | 93 | } |
| 81 | } | 94 | } |
| 82 | 95 | ||
| @@ -97,16 +110,6 @@ void AESCipher<Key, KeySize>::XTSTranscode(const u8* src, size_t size, u8* dest, | |||
| 97 | } | 110 | } |
| 98 | } | 111 | } |
| 99 | 112 | ||
| 100 | template <typename Key, size_t KeySize> | ||
| 101 | std::vector<u8> AESCipher<Key, KeySize>::CalculateNintendoTweak(size_t sector_id) { | ||
| 102 | std::vector<u8> out(0x10); | ||
| 103 | for (size_t i = 0xF; i <= 0xF; --i) { | ||
| 104 | out[i] = sector_id & 0xFF; | ||
| 105 | sector_id >>= 8; | ||
| 106 | } | ||
| 107 | return out; | ||
| 108 | } | ||
| 109 | |||
| 110 | template class AESCipher<Key128>; | 113 | template class AESCipher<Key128>; |
| 111 | template class AESCipher<Key256>; | 114 | template class AESCipher<Key256>; |
| 112 | } // namespace Core::Crypto \ No newline at end of file | 115 | } // namespace Core::Crypto \ No newline at end of file |