summaryrefslogtreecommitdiffstats
path: root/src/core/crypto/aes_util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/crypto/aes_util.cpp')
-rw-r--r--src/core/crypto/aes_util.cpp35
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 @@
// Refer to the license.txt file included.
#include <mbedtls/cipher.h>
+#include "common/assert.h"
+#include "common/logging/log.h"
#include "core/crypto/aes_util.h"
#include "core/crypto/key_manager.h"
namespace Core::Crypto {
+namespace {
+std::vector<u8> CalculateNintendoTweak(size_t sector_id) {
+ std::vector<u8> out(0x10);
+ for (size_t i = 0xF; i <= 0xF; --i) {
+ out[i] = sector_id & 0xFF;
+ sector_id >>= 8;
+ }
+ return out;
+}
+} // Anonymous namespace
static_assert(static_cast<size_t>(Mode::CTR) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_CTR),
"CTR has incorrect value.");
@@ -56,27 +68,28 @@ void AESCipher<Key, KeySize>::SetIV(std::vector<u8> iv) {
}
template <typename Key, size_t KeySize>
-void AESCipher<Key, KeySize>::Transcode(const u8* src, size_t size, u8* dest, Op op) {
- size_t written = 0;
-
- const auto context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context;
+void AESCipher<Key, KeySize>::Transcode(const u8* src, size_t size, u8* dest, Op op) const {
+ auto* const context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context;
mbedtls_cipher_reset(context);
+ size_t written = 0;
if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) {
mbedtls_cipher_update(context, src, size, dest, &written);
- if (written != size)
+ if (written != size) {
LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.",
size, written);
+ }
} else {
const auto block_size = mbedtls_cipher_get_block_size(context);
for (size_t offset = 0; offset < size; offset += block_size) {
auto length = std::min<size_t>(block_size, size - offset);
mbedtls_cipher_update(context, src + offset, length, dest + offset, &written);
- if (written != length)
+ if (written != length) {
LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.",
length, written);
+ }
}
}
@@ -97,16 +110,6 @@ void AESCipher<Key, KeySize>::XTSTranscode(const u8* src, size_t size, u8* dest,
}
}
-template <typename Key, size_t KeySize>
-std::vector<u8> AESCipher<Key, KeySize>::CalculateNintendoTweak(size_t sector_id) {
- std::vector<u8> out(0x10);
- for (size_t i = 0xF; i <= 0xF; --i) {
- out[i] = sector_id & 0xFF;
- sector_id >>= 8;
- }
- return out;
-}
-
template class AESCipher<Key128>;
template class AESCipher<Key256>;
} // namespace Core::Crypto \ No newline at end of file