|
|
@ -2,6 +2,7 @@ |
|
|
// Licensed under GPLv2 or any later version
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
// Refer to the license.txt file included.
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
|
|
|
|
#include <array>
|
|
|
#include <mbedtls/cipher.h>
|
|
|
#include <mbedtls/cipher.h>
|
|
|
#include "common/assert.h"
|
|
|
#include "common/assert.h"
|
|
|
#include "common/logging/log.h"
|
|
|
#include "common/logging/log.h"
|
|
|
@ -10,8 +11,10 @@ |
|
|
|
|
|
|
|
|
namespace Core::Crypto { |
|
|
namespace Core::Crypto { |
|
|
namespace { |
|
|
namespace { |
|
|
std::vector<u8> CalculateNintendoTweak(std::size_t sector_id) { |
|
|
|
|
|
std::vector<u8> out(0x10); |
|
|
|
|
|
|
|
|
using NintendoTweak = std::array<u8, 16>; |
|
|
|
|
|
|
|
|
|
|
|
NintendoTweak CalculateNintendoTweak(std::size_t sector_id) { |
|
|
|
|
|
NintendoTweak out{}; |
|
|
for (std::size_t i = 0xF; i <= 0xF; --i) { |
|
|
for (std::size_t i = 0xF; i <= 0xF; --i) { |
|
|
out[i] = sector_id & 0xFF; |
|
|
out[i] = sector_id & 0xFF; |
|
|
sector_id >>= 8; |
|
|
sector_id >>= 8; |
|
|
@ -63,13 +66,6 @@ AESCipher<Key, KeySize>::~AESCipher() { |
|
|
mbedtls_cipher_free(&ctx->decryption_context); |
|
|
mbedtls_cipher_free(&ctx->decryption_context); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
template <typename Key, std::size_t KeySize> |
|
|
|
|
|
void AESCipher<Key, KeySize>::SetIV(std::vector<u8> iv) { |
|
|
|
|
|
ASSERT_MSG((mbedtls_cipher_set_iv(&ctx->encryption_context, iv.data(), iv.size()) || |
|
|
|
|
|
mbedtls_cipher_set_iv(&ctx->decryption_context, iv.data(), iv.size())) == 0, |
|
|
|
|
|
"Failed to set IV on mbedtls ciphers."); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template <typename Key, std::size_t KeySize> |
|
|
template <typename Key, std::size_t KeySize> |
|
|
void AESCipher<Key, KeySize>::Transcode(const u8* src, std::size_t size, u8* dest, Op op) const { |
|
|
void AESCipher<Key, KeySize>::Transcode(const u8* src, std::size_t size, u8* dest, Op op) const { |
|
|
auto* const context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context; |
|
|
auto* const context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context; |
|
|
@ -124,6 +120,13 @@ void AESCipher<Key, KeySize>::XTSTranscode(const u8* src, std::size_t size, u8* |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template <typename Key, std::size_t KeySize> |
|
|
|
|
|
void AESCipher<Key, KeySize>::SetIVImpl(const u8* data, std::size_t size) { |
|
|
|
|
|
ASSERT_MSG((mbedtls_cipher_set_iv(&ctx->encryption_context, data, size) || |
|
|
|
|
|
mbedtls_cipher_set_iv(&ctx->decryption_context, data, size)) == 0, |
|
|
|
|
|
"Failed to set IV on mbedtls ciphers."); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
template class AESCipher<Key128>; |
|
|
template class AESCipher<Key128>; |
|
|
template class AESCipher<Key256>; |
|
|
template class AESCipher<Key256>; |
|
|
} // namespace Core::Crypto
|
|
|
} // namespace Core::Crypto
|